linux 中shell统计fasta文件中每条染色体中的碱基数目

 

1、测试数据

复制代码
[root@centos7 test]# ls
test.fa
[root@centos7 test]# cat test.fa
>chr1
addgg
ddges
df
>chr2
ertfg
sdf
>chr3
edret
dfdff
sfdfd
d
>chr4
iejie
sdgg
复制代码

 

2、在末尾生成一个标记染色体

复制代码
[root@centos7 test]# sed -i '$a >chrxxx' test.fa
[root@centos7 test]# cat test.fa
>chr1
addgg
ddges
df
>chr2
ertfg
sdf
>chr3
edret
dfdff
sfdfd
d
>chr4
iejie
sdgg
>chrxxx
复制代码

 

3、生成循环配置文件

复制代码
[root@centos7 test]# grep ">chr" test.fa | sed '$d' | paste - <(grep ">chr" test.fa | sed '1d')
>chr1   >chr2
>chr2   >chr3
>chr3   >chr4
>chr4   >chrxxx
[root@centos7 test]# grep ">chr" test.fa | sed '$d' | paste - <(grep ">chr" test.fa | sed '1d') > conf.txt
[root@centos7 test]# ls
conf.txt  test.fa
[root@centos7 test]# cat conf.txt
>chr1   >chr2
>chr2   >chr3
>chr3   >chr4
>chr4   >chrxxx
复制代码

 

4、统计每条染色体上的染色体数目

复制代码
[root@centos7 test]# ls
conf.txt  test.fa
[root@centos7 test]# cat conf.txt
>chr1   >chr2
>chr2   >chr3
>chr3   >chr4
>chr4   >chrxxx
[root@centos7 test]# cat conf.txt | while read {i,j}; do sed -n "/$i/,/$j/{/$i\|$j/b; p}" test.fa | sed 's/[\t ]*//g' | paste -d "" -s | awk -v a=$i '{print a, length}' >> result.txt; done
[root@centos7 test]# ls
conf.txt  result.txt  test.fa
[root@centos7 test]# cat result.txt   ## 查看结果
>chr1 12
>chr2 8
>chr3 16
>chr4 9
[root@centos7 test]# cat test.fa      ## 验证结果
>chr1
addgg
ddges
df
>chr2
ertfg
sdf
>chr3
edret
dfdff
sfdfd
d
>chr4
iejie
sdgg
>chrxxx
复制代码

 

5、或者

复制代码
[root@centos7 test]# ls
test.fa
[root@centos7 test]# awk '/^>/{if (l!="") print l; print; l=0; next}{l+=length($0)}END{print l}' test.fa
>chr1
12
>chr2
8
>chr3
16
>chr4
9
[root@centos7 test]# cat test.fa   ## 验证结果
>chr1
addgg
ddges
df
>chr2
ertfg
sdf
>chr3
edret
dfdff
sfdfd
d
>chr4
iejie
sdgg
复制代码

 

posted @   小鲨鱼2018  阅读(559)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-04-15 R语言中sample函数
2021-04-15 linux系统awk命令求一行值的和、平均值、最大值和最小值
2021-04-15 linux系统中awk命令求一列值的最大值、最小值、和及平均值
2021-04-15 R语言中找交集、并集、找不同、判断是否相同
2021-04-15 R语言中unique函数
2021-04-15 linux系统中将一列数据转换为指定的行
2021-04-15 linux系统中实现文本转置
点击右上角即可分享
微信分享提示