发那个太丢人

导航

 

1)samtools简介
--------------------------------------------------------------------------
背景:前面我们讲过sam/bam格式,sam文件虽然是可读的文本文件形式,但是通常是非常大,因此一般会对其压缩来节省磁盘空间,且对于很多软件来说,相比于对sam文件,对bam文件进行处理更加有效。SAMtools 是一款优秀的用以解析、处理sam/bam格式文件的一种软件包工具。其详细的文档可以在其官网里面找到。它主要包含以下功能:
1、格式转换(convert from other alignment formats)
2、排序 ,合并(sort and merge alignments)
3、去除PCR重复( remove PCR duplicate)
4、call SNP和indels( call SNPs and short indel variants)
5、数据提取(data extraction)
6、可视化(show alignments in a text-based viewer)
2)用法和命令
----------------------------------------------------------------------
像许多linux命令一样,SAMtool命令遵循流模型,允许将多个命令组合到数据处理管道中。标准流(stdin,stdout和stderr), 发送到stdout的数据默认打印到屏幕上,但很容易使用普通的Unix重定向器(>和>>)重定向到另一个文件,或通过管道(|)重定向到另一个命令。
SAMtools 命令的基本用法是: samtools <command> [options]

2.1) view
view命令用来过滤SAM或BAM格式的数据。输入通常是指定为参数的sam或bam文件,但可以是来自任何其他命令的sam或bam数据。 可选功能包括查看原始文件内容、BAM和SAM格式之间进行转换、将数据子集提取到新文件中, 提取读取的顺序得以保留。
 -- Viewing
     flags          explain BAM flags
     tview          text alignment viewer
     view           SAM<->BAM<->CRAM conversion
     depad          convert padded BAM to unpadded BAM

示例:
2.1.1) 将bam文件转化为sam文件,h 参数表示header
samtools view  -h RNA-seq.bam >RNA-seq.sam 
2.1.2) 将sam文件转化为bam文件,-b 意思使输出使BAM format,-S 意思使输入使SAM,如果@SQ 缺剩, 要写-t
samtools view -bS RNA-seq.sam > sample.bam

2.1.3) 提取某个区间的比对结果(需要索引文件)
samtools view -hb RNA-seq.bam  'chr17:40465680-40465850' >out.bam
2.14)tview命令启动基于ascii的交互式查看器,可用于可视化读取与参考基因组的指定区域。
Usage: samtools tview <aln.bam> [ref.fasta]
2.15)提取比对上的区域
samtools view -h -F 4 -b RNA-seq.bam > aln_only_mapped.bam

2.2)index
index命令创建一个新的索引文件,生成的* .sam.sai或* .bam.bai文件,允许快速查找(已排序)SAM或BAM中的数据。
  -- Indexing
     dict           create a sequence dictionary file
     faidx          index/extract FASTA
     fqidx          index/extract FASTQ
     index          index alignment

示例:
2.2.1)samtools  faidx longreads.fa  #对fasta建立索引,会生成.fai文件
2.2.2)samtools faidx longreads.fa r1 >r1.fas  #建立索引后就可以提取指定的序列,这里提r1
2.2.3)samtools  fqidx  longreads.fq   #对fastq建立索引
2.2.4)samtools index  RNA-seq.bam  #对比对文件建立索引

2.3)格式转换
-- File operations
     collate          shuffle and group alignments by name
     cat                concatenate BAMs
     merge            merge sorted alignments
     mpileup          multi-way pileup
     sort               sort alignment file
     split               splits a file by read group
     quickcheck     quickly check if SAM/BAM/CRAM file appears intact
     fastq             converts a BAM to a FASTQ
     fasta             converts a BAM to a FASTA

示例
2.3.1)samtools sort -n  RNA-seq.bam >RNA-seq_sorted_out.bam  #将bam文件进行排序
2.3.2)samtools fastq RNA-seq.bam >RNA-seq.fq            #将bam 文件转化为fastq
2.3.3)samtools fasta RNA-seq.bam >RNA-seq.fa            #将bam 文件转化为fasta

2.4)编辑
  -- Editing
     calmd          recalculate MD/NM tags and '=' bases
     fixmate        fix mate information
     reheader       replace BAM header
     targetcut      cut fosmid regions (for fosmid pool only)
     addreplacerg   adds or replaces RG tags
     markdup        mark duplicates

示例:
2.4.1)samtools markdup  RNA-seq.bam RNA-seq. markdup.bam   #进行duplicates标记

2.6)统计
  -- Statistics
     bedcov        read depth per BED region
     depth          compute the depth
     flagstat       simple stats
     idxstats       BAM index stats
     phase          phase heterozygotes
     stats          generate stats (former bamcheck)

示例:
2.6.1)samtools depth   RNA-seq.bam >depth.out   #统计depth信息
2.6.2)samtools flagstat RNA-seq.bam >flagstat.out  # 统计大致信息
2.6.1)samtools  idxstats   RNA-seq.bam >out   # 统计index信息

3)参考资源
----------------------------------------------------------------------

1)https://en.wikipedia.org/wiki/SAMtools
2)The Sequence Alignment/Map format and SAMtools
3)https://davetang.org/wiki/tiki-index.php?page=SAMTools
4)https://github.com/davetang/learning_bam_file

 
 
 
 
 
 
 

posted on 2018-08-27 10:37  发那个太丢人  阅读(1467)  评论(0编辑  收藏  举报