samtools 的常用命令

 

001、生成统计文件

[root@PC1 test01]# ls        ## 测试bam文件
SRR21814498.sorted.bam
[root@PC1 test01]# samtools flagstat SRR21814498.sorted.bam > stat.txt     ## 生成统计文件
[root@PC1 test01]# ls
SRR21814498.sorted.bam  stat.txt

 

002、结果文件

[root@PC1 test01]# cat stat.txt
24350195 + 0 in total (QC-passed reads + QC-failed reads)    ## ??
23343284 + 0 primary                     ## 总共的reads数目
1006911 + 0 secondary                    ## ???
0 + 0 supplementary
0 + 0 duplicates
0 + 0 primary duplicates
21647554 + 0 mapped (88.90% : N/A)             ## ????
20640643 + 0 primary mapped (88.42% : N/A)     ## 初级匹配率
23343284 + 0 paired in sequencing            ##有多少reads属于pairedreads
11671642 + 0 read1                      ## read1中的read数目
11671642 + 0 read2                      ## read2中的read数目
20123714 + 0 properly paired (86.21% : N/A)   ## 完美匹配率,比对到同一条参考序列,并且两条reads之间的距离符合设置的阈值
20553332 + 0 with itself and mate mapped      ##paired reads中两条都比对到参考序列上的reads数
87311 + 0 singletons (0.37% : N/A)            ## 单独一条匹配到参考序列上的reads数,和上一个相加,则是总的匹配上的reads数
232868 + 0 with mate mapped to a different chr    ##paired reads中两条分别比对到两条不同的参考序列的reads数
76469 + 0 with mate mapped to a different chr (mapQ>=5)

 。

 

003、验证;总的reads数目

[root@PC1 002_result]# ls
SRR21814498_1.fastq.gz  SRR21814498_2.fastq.gz  SRR21814498.sorted.bam  stat.txt
[root@PC1 002_result]# zcat SRR21814498_1.fastq.gz | wc -l | awk '{print $0/4}'    ## 统计read1的reads数目
11671642
[root@PC1 002_result]# zcat SRR21814498_2.fastq.gz | wc -l | awk '{print $0/4}'    ## read2的reads数目
11671642

 

004、 samtools idxstats 统计结果

[root@PC1 002_result]# samtools idxstats SRR21814498.sorted.bam | column -t    ## 统计命令
NC_003070.9  30427671  3908352  19878       ## 第一列scaffold名称, 第二列长度,第三列,比对上的reand数目
NC_003071.7  19698289  3063170  13234       ## 第四列  未比对上的read数目
NC_003074.8  23459830  3294763  13086
NC_003075.7  18585056  2691582  9587
NC_003076.8  26975502  3788593  24044
NC_037304.1  367808    214049   373
NC_000932.1  154478    4687045  7109
*            0         0        2615330

 。

 

005、bam转sam

[root@PC1 002_result]# ls
SRR21814498.sorted.bam  SRR21814498.sorted.bam.bai
[root@PC1 002_result]# samtools view -h SRR21814498.sorted.bam -o test.sam   ## bam转换未sam          
[root@PC1 002_result]# ls SRR21814498.sorted.bam SRR21814498.sorted.bam.bai test.sam [root@PC1 002_result]# head test.sam
-n 3 test.sam ## 查看sam ==> test.sam <== @HD VN:1.6 SO:coordinate @SQ SN:NC_003070.9 LN:30427671 @SQ SN:NC_003071.7 LN:19698289 ==> test.sam <== @HD VN:1.6 SO:coordinate @SQ SN:NC_003070.9 LN:30427671 @SQ SN:NC_003071.7 LN:19698289

 

006、提取比对到参考基因组上的结果

[root@PC1 002_result]# ls
SRR21814498.sorted.bam  SRR21814498.sorted.bam.bai
[root@PC1 002_result]# samtools view -bF 4 SRR21814498.sorted.bam > test.F4.bam   ###提取比对到参考序列上的比对结果
[root@PC1 002_result]# ls
SRR21814498.sorted.bam  SRR21814498.sorted.bam.bai  test.F4.bam
[root@PC1 002_result]# ll -h
total 3.2G
-rw-r--r--. 1 root root 1.7G Jul  6 20:47 SRR21814498.sorted.bam
-rw-r--r--. 1 root root 340K Jul  6 21:16 SRR21814498.sorted.bam.bai
-rw-r--r--. 1 root root 1.6G Jul  6 21:42 test.F4.bam

 

007、提取paired reads中两条reads都比对到参考序列上的比对结果

[root@PC1 003_result]# ls
SRR21814498.sorted.bam  SRR21814498.sorted.bam.bai
[root@PC1 003_result]# samtools view -bF 12 SRR21814498.sorted.bam > test.F12.bam ##提取paired reads中两条reads都比对到参考序列上的比对结果
[root@PC1 003_result]# ls
SRR21814498.sorted.bam  SRR21814498.sorted.bam.bai  test.F12.bam
[root@PC1 003_result]# ll -h
total 3.2G
-rw-r--r--. 1 root root 1.7G Jul  6 21:41 SRR21814498.sorted.bam
-rw-r--r--. 1 root root 340K Jul  6 21:41 SRR21814498.sorted.bam.bai
-rw-r--r--. 1 root root 1.5G Jul  6 21:46 test.F12.bam

 

008、#提取没有比对到参考序列上的比对结果

[root@PC1 002_result]# ls
SRR21814498.sorted.bam  SRR21814498.sorted.bam.bai                         ## 小写f
[root@PC1 002_result]# samtools view -bf 4 SRR21814498.sorted.bam > test.f4.bam   ##提取没有比对到参考序列上的比对结果
[root@PC1 002_result]# ls
SRR21814498.sorted.bam  SRR21814498.sorted.bam.bai  test.f4.bam
[root@PC1 002_result]# ll -h
total 1.9G
-rw-r--r--. 1 root root 1.7G Jul  6 20:47 SRR21814498.sorted.bam
-rw-r--r--. 1 root root 340K Jul  6 21:16 SRR21814498.sorted.bam.bai
-rw-r--r--. 1 root root 156M Jul  6 21:45 test.f4.bam

 。

参考:https://mp.weixin.qq.com/s?__biz=Mzk0MzM5NzQ3Mw==&mid=2247485374&idx=1&sn=f4c113c19c4743f6e9e0c0bffda1af5c&chksm=c335c8b0f44241a6ce03198d51381d94b940956c440f03d100ca4253e8ac4fec072a5b349f27&cur_album_id=2548290874787807234&scene=189#wechat_redirect

 

posted @ 2023-07-06 20:54  小鲨鱼2018  阅读(347)  评论(0编辑  收藏  举报