http://www.chenlianfu.com/?p=1399

q30.bam 文件是为了对质量进行控制

而过滤是为了去除PCR等重复,既可以使用samtools也可以使用picard

Options: -b       output BAM默认下输出是 SAM 格式文件,该参数设置输出 BAM 格式-h       print header for the SAM output默认下输出的 sam 格式文件不带 header,该参数设定输出sam文件时带 header 信息-H       print header only (no alignments)-S       input is SAM默认下输入是 BAM 文件,若是输入是 SAM 文件,则最好加该参数,否则有时候会报错。-u       uncompressed BAM output (force -b)该参数的使用需要有-b参数,能节约时间,但是需要更多磁盘空间。
将sam文件转换成bam文件
$ samtools view -bS abc.sam > abc.bam
$ samtools view -b -S abc.sam -o abc.bam提取比对到参考序列上的比对结果
$ samtools view -bF 4 abc.bam > abc.F.bam提取paired reads中两条reads都比对到参考序列上的比对结果,只需要把两个4+8的值12作为过滤参数即可
$ samtools view -bF 12 abc.bam > abc.F12.bam提取没有比对到参考序列上的比对结果
$ samtools view -bf 4 abc.bam > abc.f.bam提取bam文件中比对到caffold1上的比对结果,并保存到sam文件格式
$ samtools view abc.bam scaffold1 > scaffold1.sam提取scaffold1上能比对到30k到100k区域的比对结果
$ samtools view abc.bam scaffold1:30000-100000 $gt; scaffold1_30k-100k.sam根据fasta文件,将 header 加入到 sam 或 bam 文件中
$ samtools view -T genome.fasta -h scaffold1.sam > scaffold1.h.sam
$ samtools sort abc.bam abc.sort
$ samtools view abc.sort.bam | less -S

将2个或2个以上的已经sort了的bam文件融合成一个bam文件。融合后的文件不需要则是已经sort过了的。

$samtools merge control-merge.bam  control-1clean.bam  control-2clean.bam

必须对bam文件进行默认情况下的排序后,才能进行index。否则会报错。

建立索引后将产生后缀为.bai的文件,用于快速的随机处理。很多情况下需要有bai文件的存在,特别是显示序列比对情况下。比如samtool的tview命令就需要;gbrowse2显示reads的比对图形的时候也需要。

以下两种命令结果一样
$ samtools index abc.sort.bam
$ samtools index abc.sort.bam abc.sort.bam.bai

5. faidx

对fasta文件建立索引,生成的索引文件以.fai后缀结尾。该命令也能依据索引文件快速提取fasta文件中的某一条(子)序列

Usage: samtools faidx <in.bam> [ [...]]对基因组文件建立索引
$ samtools faidx genome.fasta
生成了索引文件genome.fasta.fai,是一个文本文件,分成了5列。第一列是子序列的名称;
第二列是子序列的长度;个人认为“第三列是序列所在的位置”,因为该数字从上往下逐渐变大,
最后的数字是genome.fasta文件的大小;第4和5列不知是啥意思。于是通过此文件,可以定
位子序列在fasta文件在磁盘上的存放位置,直接快速调出子序列。由于有索引文件,可以使用以下命令很快从基因组中提取到fasta格式的子序列
$ samtools faidx genome.fasta scffold_10 > scaffold_10.fasta

6. tview

tview能直观的显示出reads比对基因组的情况,和基因组浏览器有点类似。

Usage: samtools tview <aln.bam> [ref.fasta]当给出参考基因组的时候,会在第一排显示参考基因组的序列,否则,第一排全用N表示。
按下 g ,则提示输入要到达基因组的某一个位点。例子“scaffold_10:1000"表示到达第
10号scaffold的第1000个碱基位点处。
使用H(左)J(上)K(下)L(右)移动显示界面。大写字母移动快,小写字母移动慢。
使用空格建向左快速移动(和 L 类似),使用Backspace键向左快速移动(和 H 类似)。
Ctrl+H 向左移动1kb碱基距离; Ctrl+L 向右移动1kb碱基距离
可以用颜色标注比对质量,碱基质量,核苷酸等。30~40的碱基质量或比对质量使用白色表示;
20~30黄色;10~20绿色;0~10蓝色。
使用点号'.'切换显示碱基和点号;使用r切换显示read name等
还有很多其它的使用说明,具体按 ? 键来查看。

7. flagstat

给出BAM文件的比对结果

Usage: samtools flagstat <in.bam>$ samtools flagstat example.bam
11945742 + 0 in total (QC-passed reads + QC-failed reads)
#总共的reads数
0 + 0 duplicates
7536364 + 0 mapped (63.09%:-nan%)
#总体上reads的匹配率
11945742 + 0 paired in sequencing
#有多少reads是属于paired reads
5972871 + 0 read1
#reads1中的reads数
5972871 + 0 read2
#reads2中的reads数
6412042 + 0 properly paired (53.68%:-nan%)
#完美匹配的reads数:比对到同一条参考序列,并且两条reads之间的距离符合设置的阈值
6899708 + 0 with itself and mate mapped
#paired reads中两条都比对到参考序列上的reads数
636656 + 0 singletons (5.33%:-nan%)
#单独一条匹配到参考序列上的reads数,和上一个相加,则是总的匹配上的reads数。
469868 + 0 with mate mapped to a different chr
#paired reads中两条分别比对到两条不同的参考序列的reads数
243047 + 0 with mate mapped to a different chr (mapQ>=5)

#同上一个,只是其中比对质量>=5的reads的数量

8. 其它有用的命令

reheader 替换bam文件的头

$ samtools reheader <in.header.sam> <in.bam>

cat 连接多个bam文件,适用于非sorted的bam文件

$ samtools cat [-h header.sam] [-o out.bam] <in1.bam> <in2.bam> [ ... ]

idxstats 统计一个表格,4列,分别为”序列名,序列长度,比对上的reads数,unmapped reads number”。第4列应该是paired reads中有一端能匹配到该scaffold上,而另外一端不匹配到任何scaffolds上的reads数。

$ samtools idxstats <aln.bam>

9. 将bam文件转换为fastq文件

有时候,我们需要提取出比对到一段参考序列的reads,进行小范围的分析,以利于debug等。这时需要将bam或sam文件转换为fastq格式。
该网站提供了一个bam转换为fastq的程序:http://www.hudsonalpha.org/gsl/information/software/bam2fastq

$ wget http://www.hudsonalpha.org/gsl/static/software/bam2fastq-1.1.0.tgz
$ tar zxf bam2fastq-1.1.0.tgz
$ cd bam2fastq-1.1.0
$ make
$ ./bam2fastq <in.bam>

10. mpileup

samtools还有个非常重要的命令mpileup,以前为pileup。该命令用于生成bcf文件,再使用bcftools进行SNP和Indel的分析。bcftools是samtool中附带的软件,在samtools的安装文件夹中可以找到。

最常用的参数有2: -f 来输入有索引文件的fasta参考序列; -g 输出到bcf格式。用法和最简单的例子如下

Usage: samtools mpileup [-EBug] [-C capQcoef] [-r reg] [-f in.fa] [-l list] [-M capMapQ] [-Q minBaseQ] [-q minMapQ] in.bam [in2.bam [...]]$ samtools mpileup -f genome.fasta abc.bam > abc.txt
$ samtools mpileup -gSDf genome.fasta abc.bam > abc.bcf
$ samtools mpileup -guSDf genome.fasta abc.bam | \bcftools view -cvNg - > abc.vcf

mpileup不使用-u或-g参数时,则不生成二进制的bcf文件,而生成一个文本文件(输出到标准输出)。该文本文件统计了参考序列中每个碱基位点的比对情况;该文件每一行代表了参考序列中某一个碱基位点的比对结果。比如:

scaffold_1      2841    A       11      ,,,...,....     BHIGDGIJ?FF
scaffold_1      2842    C       12      ,$,,...,....^I. CFGEGEGGCFF+
scaffold_1      2843    G       11      ,,...,.....     FDDDDCD?DD+
scaffold_1      2844    G       11      ,,...,.....     FA?AAAA<AA+
scaffold_1      2845    G       11      ,,...,.....     F656666166*
scaffold_1      2846    A       11      ,,...,.....     (1.1111)11*
scaffold_1      2847    A       11      ,,+9acggtgaag.+9ACGGTGAAT.+9ACGGTGAAG.+9ACGGTGAAG,+9acggtgaag.+9ACGGTGAAG.+9ACGGTGAAG.+9ACGGTGAAG.+9ACGGTGAAG.+9ACGGTGAAG       %.+....-..)
scaffold_1      2848    N       11      agGGGgGGGGG     !!$!!!!!!!!
scaffold_1      2849    A       11      c$,...,.....    !0000000000
scaffold_1      2850    A       10      ,...,.....      353333333

mpileup生成的结果包含6行:参考序列名;位置;参考碱基;比对上的reads数;比对情况;比对上的碱基的质量。其中第5列比较复杂,解释如下:

1 ‘.’代表与参考序列正链匹配。
2 ‘,’代表与参考序列负链匹配。
3 ‘ATCGN’代表在正链上的不匹配。
4 ‘atcgn’代表在负链上的不匹配。
5 ‘*’代表模糊碱基
6 ‘^’代表匹配的碱基是一个read的开始;’^’后面紧跟的ascii码减去33代表比对质量;这两个符号修饰的是后面的碱基,其后紧跟的碱基(.,ATCGatcgNn)代表该read的第一个碱基。
7 ‘$’代表一个read的结束,该符号修饰的是其前面的碱基。
8 正则式’\+[0-9]+[ACGTNacgtn]+’代表在该位点后插入的碱基;比如上例中在scaffold_1的2847后插入了9个长度的碱基acggtgaag。表明此处极可能是indel。
9 正则式’-[0-9]+[ACGTNacgtn]+’代表在该位点后缺失的碱基;

pileup具体的参数如下:

输入参数
-6       Assume the quality is in the Illumina 1.3+ encoding. -A Do not skip anomalous read pairs in variant calling.
-B       Disable probabilistic realignment for the computation of base alignment quality (BAQ). BAQ is the Phred-scaled probability of a read base being misaligned. Applying this option greatly helps to reduce false SNPs caused by misalignments.
-b FILE  List of input BAM files, one file per line [null]
-C INT   Coefficient for downgrading mapping quality for reads containing excessive mismatches. Given a read with a phred-scaled probability q of being generated from the mapped position, the new mapping quality is about sqrt((INT-q)/INT)*INT. A zero value disables this functionality; if enabled, the recommended value for BWA is 50. [0]
-d INT   At a position, read maximally INT reads per input BAM. [250]
-E       Extended BAQ computation. This option helps sensitivity especially for MNPs, but may hurt specificity a little bit.
-f FILE  The faidx-indexed reference file in the FASTA format. The file can be optionally compressed by razip. [null]
-l FILE  BED or position list file containing a list of regions or sites where pileup or BCF should be generated [null]
-M INT       cap mapping quality at INT [60]
-q INT  Minimum mapping quality for an alignment to be used [0]
-Q INT  Minimum base quality for a base to be considered [13]
-r STR  Only generate pileup in region STR [all sites] 输出参数
-D  Output per-sample read depth (require -g/-u)
-g  Compute genotype likelihoods and output them in the binary call format (BCF).
-S  Output per-sample Phred-scaled strand bias P-value (require -g/-u)
-u  Similar to -g except that the output is uncompressed BCF, which is preferred for piping. Options for Genotype Likelihood Computation (for -g or -u):
-e INT  Phred-scaled gap extension sequencing error probability. Reducing INT leads to longer indels. [20]
-h INT  Coefficient for modeling homopolymer errors. Given an l-long homopolymer run, the sequencing error of an indel of size s is modeled as INT*s/l. [100]
-I  Do not perform INDEL calling
-L INT  Skip INDEL calling if the average per-sample depth is above INT. [250]
-o INT  Phred-scaled gap open sequencing error probability. Reducing INT leads to more indel calls. [40]
-P STR  Comma dilimited list of platforms (determined by @RG-PL) from which indel candidates are obtained. It is recommended to collect indel candidates from sequencing technologies that have low indel error rate such as ILLUMINA. [all]

11. 使用bcftools

bcftools和samtools类似,用于处理vcf(variant call format)文件和bcf(binary call format)文件。前者为文本文件,后者为其二进制文件。

bcftools使用简单,最主要的命令是view命令,其次还有index和cat等命令。index和cat命令和samtools中类似。此处主讲使用view命令来进行SNP和Indel calling。该命令的使用方法和例子为:

$ bcftools view [-AbFGNQSucgv] [-D seqDict] [-l listLoci] [-s listSample] [-i gapSNPratio] [-t mutRate] [-p varThres] [-P prior] [-1 nGroup1] [-d minFrac] [-U nPerm] [-X permThres] [-T trioType] in.bcf [region]$ bcftools view -cvNg abc.bcf > snp_indel.vcf

生成的结果文件为vcf格式,有10列,分别是:1 参考序列名;2 varianti所在的left-most位置;3 variant的ID(默认未设置,用’.’表示);4 参考序列的allele;5 variant的allele(有多个alleles,则用’,’分隔);6 variant/reference QUALity;7 FILTers applied;8 variant的信息,使用分号隔开;9 FORMAT of the genotype fields, separated by colon (optional); 10 SAMPLE genotypes and per-sample information (optional)。
例如:

scaffold_1      2847    .       A       AACGGTGAAG      194     .       INDEL;DP=11;VDB=0.0401;AF1=1;AC1=2;DP4=0,0,8,3;MQ=35;FQ=-67.5   GT:PL:GQ        1/1:235,33,0:63
scaffold_1      3908    .       G       A       111     .       DP=13;VDB=0.0085;AF1=1;AC1=2;DP4=0,0,5,7;MQ=42;FQ=-63   GT:PL:GQ        1/1:144,36,0:69
scaffold_1      4500    .       A       G       31.5    .       DP=8;VDB=0.0034;AF1=1;AC1=2;DP4=0,0,1,3;MQ=42;FQ=-39    GT:PL:GQ        1/1:64,12,0:21
scaffold_1      4581    .       TGGNGG  TGG     145     .       INDEL;DP=8;VDB=0.0308;AF1=1;AC1=2;DP4=0,0,0,8;MQ=42;FQ=-58.5    GT:PL:GQ        1/1:186,24,0:45
scaffold_1      4644    .       G       A       195     .       DP=21;VDB=0.0198;AF1=1;AC1=2;DP4=0,0,10,10;MQ=42;FQ=-87 GT:PL:GQ        1/1:228,60,0:99
scaffold_1      4827    .       NACAAAGA        NA      4.42    .       INDEL;DP=1;AF1=1;AC1=2;DP4=0,0,1,0;MQ=40;FQ=-37.5       GT:PL:GQ        0/1:40,3,0:3
scaffold_1      4854    .       A       G       48      .       DP=6;VDB=0.0085;AF1=1;AC1=2;DP4=0,0,2,1;MQ=41;FQ=-36    GT:PL:GQ        1/1:80,9,0:16
scaffold_1      5120    .       A       G       85      .       DP=8;VDB=0.0355;AF1=1;AC1=2;DP4=0,0,5,3;MQ=42;FQ=-51    GT:PL:GQ        1/1:118,24,0:45

第8列中显示了对variants的信息描述,比较重要,其中的 Tag 的描述如下:

Tag    Format  Description
AF1 double  Max-likelihood estimate of the site allele frequency (AF) of the first ALT allele
DP  int Raw read depth (without quality filtering)
DP4 int[4]  # high-quality reference forward bases, ref reverse, alternate for and alt rev bases
FQ  int Consensus quality. Positive: sample genotypes different; negative: otherwise
MQ  int Root-Mean-Square mapping quality of covering reads
PC2 int[2]  Phred probability of AF in group1 samples being larger (,smaller) than in group2
PCHI2   double  Posterior weighted chi^2 P-value between group1 and group2 samples
PV4 double[4]   P-value for strand bias, baseQ bias, mapQ bias and tail distance bias
QCHI2   int Phred-scaled PCHI2
RP  int # permutations yielding a smaller PCHI2
CLR int Phred log ratio of genotype likelihoods with and without the trio/pair constraint
UGT string  Most probable genotype configuration without the trio constraint
CGT string  Most probable configuration with the trio constraint

bcftools view 的具体参数如下:

Input/Output Options:
-A  Retain all possible alternate alleles at variant sites. By default, the view command discards unlikely alleles.
-b  Output in the BCF format. The default is VCF.
-D FILE Sequence dictionary (list of chromosome names) for VCF->BCF conversion [null]
-F  Indicate PL is generated by r921 or before (ordering is different).
-G  Suppress all individual genotype information.
-l FILE List of sites at which information are outputted [all sites]
-N  Skip sites where the REF field is not A/C/G/T
-Q  Output the QCALL likelihood format
-s FILE List of samples to use. The first column in the input gives the sample names and the second gives the ploidy, which can only be 1 or 2. When the 2nd column is absent, the sample ploidy is assumed to be 2. In the output, the ordering of samples will be identical to the one in FILE. [null]
-S  The input is VCF instead of BCF.
-u  Uncompressed BCF output (force -b). Consensus/Variant Calling Options:
-c  Call variants using Bayesian inference. This option automatically invokes option -e.
-d FLOAT When -v is in use, skip loci where the fraction of samples covered by reads is below FLOAT. [0]当有多个sample用于variants calling时,比如多个转录组数据或多个重测序数据需要比对到参考基因组上,设置该值,表明至少有该<float 0~1>比例的samples在该位点都有覆盖才计算入variant.所以对于只有一个sample的情况下,该值设置在0~1之间没有意义,大于1则得不到任何结果。
-e  Perform max-likelihood inference only, including estimating the site allele frequency, testing Hardy-Weinberg equlibrium and testing associations with LRT.
-g  Call per-sample genotypes at variant sites (force -c)
-i FLOAT Ratio of INDEL-to-SNP mutation rate [0.15]
-p FLOAT A site is considered to be a variant if P(ref|D)
-t FLOAT Scaled muttion rate for variant calling [0.001]
-T STR  Enable pair/trio calling. For trio calling, option -s is usually needed to be applied to configure the trio members and their ordering. In the file supplied to the option -s, the first sample must be the child, the second the father and the third the mother. The valid values of STR are ‘pair’, ‘trioauto’, ‘trioxd’ and ‘trioxs’, where ‘pair’ calls differences between two input samples, and ‘trioxd’ (‘trioxs’) specifies that the input is from the X chromosome non-PAR regions and the child is a female (male). [null]
-v  Output variant sites only (force -c) Contrast Calling and Association Test Options:
-1 INT  Number of group-1 samples. This option is used for dividing the samples into two groups for contrast SNP calling or association test. When this option is in use, the following VCF INFO will be outputted: PC2, PCHI2 and QCHI2. [0]
-U INT  Number of permutations for association test (effective only with -1) [0]
-X FLOAT Only perform permutations for P(chi^2)

使用bcftools得到variant calling结果后。需要对结果再次进行过滤。主要依据比对结果中第8列信息。其中的 DP4 一行尤为重要,提供了4个数据:1 比对结果和正链一致的reads数、2 比对结果和负链一致的reads数、3 比对结果在正链的variant上的reads数、4 比对结果在负链的variant上的reads数。可以设定 (value3 + value4)大于某一阈值,才算是variant。比如:

$ perl -ne 'print $_ if /DP4=(\d+),(\d+),(\d+),(\d+)/ && ($3+$4)>=10 && ($3+$4)/($1+$2+$3+$4)>=0.8' snp_indel.vcf > snp_indel.final.vcf

12. samtools rmdup

NGS上机测序前需要进行PCR一步,使一个模板扩增出一簇,从而在上机测序的时候表现出为1个点,即一个reads。若一个模板扩增出了多簇,结果得到了多个reads,这些reads的坐标(coordinates)是相近的。在进行了reads比对后需要将这些由PCR duplicates获得的reads去掉,并只保留最高比对质量的read。使用rmdup命令即可完成.

Usage:  samtools rmdup [-sS]
-s 对single-end reads。默认情况下,只对paired-end reads
-S 将Paired-end reads作为single-end reads处理。$ samtools input.sorted.bam output.bam

关于samtools相关推荐

  1. Samtools(CentOS Linux)安装及常用命令详解

    序列比对(将测序reads与已知序列信息的基因或基因组进行比对)是高通量测序数据分析中最重要的一环,无论是转录组还是重测序都是基于比对结果来进行后续各项分析的,比对结果格式比较常见的是sam和bam文 ...

  2. 宏基因组实战7. bwa序列比对, samtools查看, bedtools丰度统计

    前情提要 如果您在学习本教程中存在困难,可能因为缺少背景知识,建议先阅读本系统前期文章 宏基因组分析理论教程 微生物组入门圣经+宏基因组分析实操课程 1背景知识-Shell入门与本地blast实战 2 ...

  3. Samtools安装与使用

    由于二代测序中普遍采取短读长(50~150bp)的测序策略,在后续分析的流程中需要使用比对软件将reads片段匹配到参考基因组中从而产生比对/匹配文件,进而用于后续流程的分析. Samtools是一个 ...

  4. samtools 检测bam文件的完整度

    检测bam文件的完整度 samtools view T_recal.bam|head samtools view T_recal.bam|tail for i in *.bam ;do (samtoo ...

  5. samtools merge_【生信工具】Samtools 安装与使用 | “十年以后,工具难免沦为朋友”...

    1.Description | 简介 http://samtools.sourceforge.net/ http://samtools.sourceforge.net/ 由于二代测序中普遍采取短读长( ...

  6. samtools常用命令详解

    samtools的说明文档:http://samtools.sourceforge.net/samtools.shtml samtools是一个用于操作sam和bam文件的工具合集.包含有许多命令.以 ...

  7. samtools idxstats

    有很多种格式来记录mapping的结果,大多数都收录在UCSC的帮助文档中.在mapping之后,如何对mapping的结果进行评估呢? 最简单的,就是通过samtools来评估mapping质量了. ...

  8. mybatis 使用in 查询时报错_为什么使用samtools检查bam质量时报错?

     今天是生信星球陪你的第540天 大神一句话,菜鸟跑半年.我不是大神,但我可以缩短你走弯路的半年~ 就像歌儿唱的那样,如果你不知道该往哪儿走,就留在这学点生信好不好~ 这里有豆豆和花花的学习历程,从新 ...

  9. [samtools] 文本查看语法,浏览SNP/INDEL位点

    santools可以作为文本查看工具,查看比对结果文件,下面做一简单介绍: 1. 通过BWA比对获取sam比对文件,也可以将fastq文件转化为bam/sam文件: 2. 转换sam文件为bam文件, ...

  10. 使用samtools来对sam/bam/cram相互转换

    使用samtools来对sam/bam/cram相互转换 1.sam <=>bam samtools view -h NA12878.bam >NA12878_2.sam samto ...

最新文章

  1. 40.简述操作系统中调用过程?
  2. 如何在vmvare vsphere(ESXI)中移除磁盘中的文件
  3. RHEL 7.0已发布 CentOS 7 即将到来
  4. 脚本录制两种模式 HTML-based script和URL-based script模式
  5. 360mysql连接池_自己动手写个数据库连接池
  6. 在你的 Android App 中使用 Flutter | Google开发者大会
  7. 初窥JQuery(二)-事件机制(1)
  8. 3dmm计算特征向量,c++读写txt和二进制记录
  9. BP神经网络处理iris数据集(Pytorch实现)
  10. vue网易严选购物商城项目
  11. CentOS 7.5版本成功安装后修改基本配置以及可视化远程桌面解决办法
  12. PS|基础原理之‘图层混合模式’
  13. 5G中传和回传的承载解决方案
  14. Android ExpandableListView 使用实例
  15. Android 自定义View 实例2_Clipping Canvas
  16. 关于修改手机绑定那些事!
  17. mysql中的case when 与if else
  18. java-net-php-python-Java沈师周边美食资源管理系统计算机毕业设计程序
  19. 【读论文04】CVPR2022选读
  20. 计算机算法设计与分析——算法引论

热门文章

  1. 记一次php-fpm http502的调优
  2. 美团上市,王兴:格局上输了,再怎么努力都不可能赢
  3. noise函数 matlab,matlab未定义函数或变量 'add_noise1'怎么解决
  4. MinDoc在windows下的安装
  5. 计算机需要什么才能连接网络,计算机必须安装网卡,才可以连接到Internet 。
  6. MSYS2环境使用记录
  7. 一只喵引发的思考:动物们的开源技术和工具
  8. spyder(python3.7)使用教程_使用spyder3调试python程序的简明教程
  9. WiFi 無法上網(Wi-Fi列表不見了)
  10. asic面试题目 英伟达_NVIDIA校招面试经历