samtools的功能大全:查看bam文件外還能做什么

必須學(xué)習(xí)這篇英文:http://www.htslib.org/doc/samtools.html
【繼續(xù)更新ing】

samtools view -h

查看bam文件囊嘉,包含頭文件蹭秋,去掉-h菱父,不包含

samtools view -h s.bam|less -S
samtools view s.bam|less -S
# 提取chr1染色體脯丝,生成只有chr1的bam文件
samtools view -h -b s.bam chr1 >s.chr1.bam
samtools view -bt ref_list.txt -o aln.bam aln.sam.gz

samtools view -c

-c print only the count of matching records;統(tǒng)計(jì)比對(duì)條數(shù);結(jié)果等于samtools flagstat的總reads條數(shù)雳灵。

# 單端比對(duì)reads數(shù)目的統(tǒng)計(jì)
cuiqingmei 2019/10/09 10:57:10 /ifs9/BC_PS/cuiqingmei/ass/3.mapping/result_bam
$ samtools view -c CL100141207_L01_69.sort.bam
31504107
cuiqingmei 2019/10/09 10:57:38 /ifs9/BC_PS/cuiqingmei/ass/3.mapping/result_bam
$ zcat ../CL100141207_L01_69.fq.gz |wc -l
126016428
cuiqingmei 2019/10/09 11:02:26 /ifs9/BC_PS/cuiqingmei/ass/3.mapping/result_bam
$ echo "scale=4;126016428/4"|bc
31504107.0000
qmcui 12:07:32 /teach/project/1.rna/4.clean_data_25000reads
$ echo `zcat SRR1039510_1_val_1.100000.fq.gz|wc -l`/4|bc
25000
qmcui 12:08:12 /teach/project/1.rna/4.clean_data_25000reads
$ echo `zcat SRR1039510_2_val_2.100000.fq.gz|wc -l`/4|bc
25000
qmcui 12:45:49 /teach/project/1.rna/4.clean_data_25000reads
$ samtools view -c  ../5.sort.bam/SRR1039510.sort.bam 
55621

“Instead of printing the alignments, only count them and print the total number.” samtools view -c 算的不是reads祈餐,而是alignments數(shù)。當(dāng)read有很多位置可以align上同時(shí)又都輸出了扳还,用samtools view -c 會(huì)比實(shí)際reads樹(shù)木要多~~~ by: 嚴(yán)云

samtools view -F/-f 4

看下表才避,4是unmapped;所以我們-F(none of) 4就是找到map的reads數(shù)目
-q INT only include reads with mapping quality >= INT [0]
-m INT only include reads with number of CIGAR operations consuming
query sequence >= INT [0]
-f INT only include reads with all of the FLAGs in INT present [0]
-F INT only include reads with none of the FLAGS in INT present [0]
-G INT only EXCLUDE reads with all of the FLAGs in INT present [0]

So-f 4 only output alignments that areunmapped(flag 0x0004 is set) and -F 4only output alignments that are not unmapped (i.e. flag 0x0004 is not set), hence these would onlyinclude mapped alignments.

cuiqingmei 2019/10/09 11:02:58 /ifs9/BC_PS/cuiqingmei/ass/3.mapping/result_bam
$ samtools view -c CL100141207_L01_69.sort.bam -F 4
30441976
# 想知道有多少paired end reads有mate并且都有map時(shí)氨距,可以使用-f 1 -F 12來(lái)過(guò)濾桑逝。
samtools view -c -f 1 -F 12 test.bam
# 12是   read unmapped、   mate unmapped;12=8+4
# Mapped reads only
$ samtools view -c -F 4 HG00173.chrom11.ILLUMINA.bwa.FIN.low_coverage.20111114.bam
5068340

# Unmapped reads only
$ samtools view -c -f 4 HG00173.chrom11.ILLUMINA.bwa.FIN.low_coverage.20111114.bam
149982
Flag Chr Description
0×0001 p the read is paired in sequencing
0×0002 P the read is mapped in a proper pair
0×0004 u the query sequence itself is unmapped
0×0008 U the mate is unmapped
0×0010 r strand of the query (1 for reverse)
0×0020 R strand of the mate
0×0040 1 the read is the first read in a pair
0×0080 2 the read is the second read in a pair
0×0100 s the alignment is not primary
0×0200 f the read fails platform/vendor quality checks
0×0400 d the read is either a PCR or an optical duplicate

samtools sort

bam文件必須排序
一般按照默認(rèn)參數(shù)俏让,控制線程數(shù)均可楞遏,-@ 5即5個(gè)線程茬暇。
-n Sort by read name:按照reads名字來(lái)排序
-l INT Set compression level, from 0 (uncompressed) to 9 (best):設(shè)置壓縮倍數(shù)
-m INT Set maximum memory per thread; suffix K/M/G recognized [768M]:
Usage: samtools sort [-n] [-m <maxMem>] <in.bam> <out.prefix>
-m 參數(shù)默認(rèn)下是 500,000,000 即500M(不支持K,M寡喝,G等縮寫(xiě))糙俗。對(duì)于處理大數(shù)據(jù)時(shí),如果內(nèi)存夠用预鬓,則設(shè)置大點(diǎn)的值巧骚,以節(jié)約時(shí)間。
-n 設(shè)定排序方式按short reads的ID排序珊皿。默認(rèn)下是按序列在fasta文件中的順序(即header)和序列從左往右的位點(diǎn)排序。

$ samtools sort -@ 5  -o output.sort.bam input.sam
$ samtools sort -@ 5  -o output.sort.bam input.bam
# 排序sam巨税,bam均可蟋定,而且排序后結(jié)果默認(rèn)生成bam
# samtools sort -T /tmp/aln.sorted -o aln.sorted.bam aln.bam

samtools flags

explain BAM flags:解釋bam文件第二列標(biāo)簽的含義

cuiqingmei 2019/10/09 11:51:33 
$ samtools flags 4  
0x4 4   UNMAP
cuiqingmei 2019/10/09 11:52:44
$ samtools flags 355
0x163   355 PAIRED,PROPER_PAIR,MREVERSE,READ1,SECONDARY

samtools index

給sam或者bam文件建立索引,一般下游程序需要位置信息的時(shí)候草添,就必須在bam同目錄下存在bai索引文件驶兜。

# samtools 1.8版本
$ samtools index
Usage: samtools index [-bc] [-m INT] <in.bam> [out.index]
Options:
  -b       Generate BAI-format index for BAM files [default]
  -c       Generate CSI-format index for BAM files
  -m INT   Set minimum interval size for CSI indices to 2^INT [14]
  -@ INT   Sets the number of threads [none]
cuiqingmei 2019/10/09 11:56:21 /ifs9/
$ samtools index CL100141207_L01_69.sort.bam

samtools idxstat

結(jié)果解釋?zhuān)簉eference sequence name, sequence length, # mapped reads and # unmapped reads,\t分割

$ samtools idxstats CL100141207_L01_69.sort.bam   
chr1    249250621   2443169 0
chr2    243199373   2575343 0
chr3    198022430   2018108 0
chr4    191154276   1987277 0
chr5    180915260   1838927 0
...
*   0   0   1062131

samtools markdup

去重

EXAMPLE

# The first sort can be omitted if the file is already name ordered
samtools sort -n -o namesort.bam example.bam


# Add ms and MC tags for markdup to use later
samtools fixmate -m namesort.bam fixmate.bam
# -m:Add ms (mate score) tags. These are used by markdup to select the best reads to keep.

# Markdup needs position order
samtools sort -o positionsort.bam fixmate.bam


# Finally mark duplicates
samtools markdup positionsort.bam markdup.bam

samtools rmdup:過(guò)時(shí)

samtools rmdup [-sS] <input.srt.bam> <out.bam>
This command is obsolete. Use markdup instead.命令過(guò)時(shí)远寸,最好不要再使用抄淑。


值得學(xué)習(xí)翻譯鏈接:
http://qnot.org/2012/04/14/counting-the-number-of-reads-in-a-bam-file/
http://www.htslib.org/doc/samtools.html

還可以學(xué)習(xí):http://www.chenlianfu.com/?p=1399

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市驰后,隨后出現(xiàn)的幾起案子肆资,更是在濱河造成了極大的恐慌,老刑警劉巖灶芝,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件郑原,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡夜涕,警方通過(guò)查閱死者的電腦和手機(jī)犯犁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)女器,“玉大人酸役,你說(shuō)我怎么就攤上這事〖莸ǎ” “怎么了涣澡?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)丧诺。 經(jīng)常有香客問(wèn)我暑塑,道長(zhǎng),這世上最難降的妖魔是什么锅必? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任事格,我火速辦了婚禮惕艳,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘驹愚。我一直安慰自己远搪,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布逢捺。 她就那樣靜靜地躺著谁鳍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪劫瞳。 梳的紋絲不亂的頭發(fā)上倘潜,一...
    開(kāi)封第一講書(shū)人閱讀 51,292評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音志于,去河邊找鬼涮因。 笑死,一個(gè)胖子當(dāng)著我的面吹牛伺绽,可吹牛的內(nèi)容都是我干的养泡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼奈应,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼澜掩!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起杖挣,我...
    開(kāi)封第一講書(shū)人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤肩榕,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后惩妇,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體点把,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年屿附,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了郎逃。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡挺份,死狀恐怖褒翰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情匀泊,我是刑警寧澤优训,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站各聘,受9級(jí)特大地震影響揣非,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜躲因,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一早敬、第九天 我趴在偏房一處隱蔽的房頂上張望忌傻。 院中可真熱鬧,春花似錦搞监、人聲如沸水孩。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)俘种。三九已至,卻和暖如春绝淡,著一層夾襖步出監(jiān)牢的瞬間宙刘,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工牢酵, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留悬包,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓茁帽,卻偏偏與公主長(zhǎng)得像玉罐,于是被迫代替她去往敵國(guó)和親屈嗤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子潘拨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354