samtools—手册

官方手册:http://www.htslib.org/doc/samtools.html

Samtools 是一组实用程序,用于处理 SAM(序列对齐/映射)、BAM 和 CRAM 格式的对齐。它在格式之间进行转换,进行排序、合并和索引,并且可以快速检索任何区域的读取。

1.下载:

(1)直接conda安装

conda install -c bioconda samtools
#然后自己加入环境变量
#输入samtools检查一下是否安装成功

2.使用

samtools
Program: samtools (Tools for alignments in the SAM format)
Version: 1.11 (using htslib 1.11)

Usage:   samtools <command> [options]

Commands:
  -- Indexing
     dict           create a sequence dictionary file
     faidx          index/extract FASTA
     fqidx          index/extract FASTQ
     index          index alignment

  -- 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
     ampliconclip   clip oligos from the end of reads

  -- 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

  -- Statistics
     bedcov         read depth per BED region
     coverage       alignment depth and percent coverage
     depth          compute the depth
     flagstat       simple stats
     idxstats       BAM index stats
     phase          phase heterozygotes
     stats          generate stats (former bamcheck)
     ampliconstats  generate amplicon specific stats

  -- Viewing
     flags          explain BAM flags
     tview          text alignment viewer
     view           SAM<->BAM<->CRAM conversion
     depad          convert padded BAM to unpadded BAM

3.常见操作

view

samtools view [options] in.sam|in.bam|in.cram [region...]
Output options:
  -b, --bam                  Output BAM    输出BAM格式
  -C, --cram                 Output CRAM (requires -T)    输出CRAM文件
  -1, --fast                 Use fast BAM compression (and default to --bam)    快速压缩(需要-b)
  -u, --uncompressed         Uncompressed BAM output (and default to --bam) 输出未压缩的bam文件,节约时间,占据较多磁盘空间(需要-b)
  -h, --with-header          Include header in SAM output  默认输出sam文件不带表头,该参数设定后输出带表头信息
  -H, --header-only          Print SAM header only (no alignments)  仅仅输出表头信息
      --no-header            Print SAM alignment records only [default]  
  -c, --count                Print only the count of matching records   仅打印匹配数
  -o, --output FILE          Write output to FILE [standard output]  输出文件(stdout标准输出)
  -U, --unoutput FILE, --output-unselected FILE
                             Output reads not selected by filters to FILE 输出没有经过过滤选择的reads
  -p, --unmap                Set flag to UNMAP on reads not selected
                             then write to output file.
  -P, --fetch-pairs          Retrieve complete pairs even when outside of region
Input options:
  -t, --fai-reference FILE   FILE listing reference names and lengths 制表分隔符文件(需要提供额外的参考数据,比如参考基因组的索引)
  -M, --use-index            Use index and multi-region iterator for regions 
      --region[s]-file FILE  Use index to include only reads overlapping FILE
  -X, --customized-index     Expect extra index file argument after <in.bam>

Filtering options (Only include in output reads that...):
  -L, --target[s]-file FILE  ...overlap (BED) regions in FILE 仅包括和bed文件有重叠的reads
  -r, --read-group STR       ...are in read group STR 仅输出在STR读段组中的reads
  -R, --read-group-file FILE ...are in a read group listed in FILE 仅输出特定reads
  -N, --qname-file FILE      ...whose read name is listed in FILE
  -d, --tag STR1[:STR2]      ...have a tag STR1 (with associated value STR2)
  -D, --tag-file STR:FILE    ...have a tag STR whose value is listed in FILE
  -q, --min-MQ INT           ...have mapping quality >= INT 定位的质量大于INT[默认0]
  -l, --library STR          ...are in library STR 仅输出在STR 库中的reads
  -m, --min-qlen INT         ...cover >= INT query bases (as measured via CIGAR)
  -e, --expr STR             ...match the filter expression STR
  -f, --require-flags FLAG   ...have all of the FLAGs present  获得未必对上(unmapped)的过滤设置[默认0]
  -F, --excl[ude]-flags FLAG ...have none of the FLAGs present
      --rf, --incl-flags, --include-flags FLAG   获得比对上(mapped)的过滤设置[默认0]
                             ...have some of the FLAGs present
  -G FLAG                    EXCLUDE reads with all of the FLAGs present
      --subsample FLOAT      Keep only FLOAT fraction of templates/read pairs
      --subsample-seed INT   Influence WHICH reads are kept in subsampling [0]
  -s INT.FRAC                Same as --subsample 0.FRAC --subsample-seed INT

Processing options:
      --add-flags FLAG       Add FLAGs to reads
      --remove-flags FLAG    Remove FLAGs from reads
  -x, --remove-tag STR
               Comma-separated read tags to strip (repeatable) [null]
      --keep-tag STR
               Comma-separated read tags to preserve (repeatable) [null].
               Equivalent to "-x ^STR"
  -B, --remove-B             Collapse the backward CIGAR operation

General options:
  -?, --help   Print long help, including note about region specification
  -S           Ignored (input format is auto-detected)
      --no-PG  Do not add a PG line
      --input-fmt-option OPT[=VAL]
               Specify a single input file format option in the form
               of OPTION or OPTION=VALUE
  -O, --output-fmt FORMAT[,OPT[=VAL]]...
               Specify output format (SAM, BAM, CRAM)
      --output-fmt-option OPT[=VAL]
               Specify a single output file format option in the form
               of OPTION or OPTION=VALUE
  -T, --reference FILE
               Reference sequence FASTA FILE [null]
  -@, --threads INT
               Number of additional threads to use [0]
      --write-index
               Automatically index the output files [off]
      --verbosity INT
               Set level of verbosity

  1. 要对来自BWA或其他比对工具产生的SAM输出文件进行进一步的操作,我们需要首先将SAM转换为其对应的二进制格式BAM。二进制格式对于计算机来说更容易处理,而且减小了内存。要将SAM转换为BAM,使用samtools view命令,使用-S参数指定我们的输入是SAM格式(默认BAM),使用-b参数指定输出格式是BAM(默认BAM)。Samtools会将返回结果发送到标准输出(UNIX STDOUT),因此需要使用重定向运算符 (“>”) 将其重定向到BAM文件,或者添加-o参数,指定输出文件名。
  2. view命令中,对sam文件头部(序列ID)的输入(-t或-T)和输出(-h)是单独的一些参数来控制的。
# 将sam文件转换成bam文件
samtools view -S -b sample.sam > sample.bam
samtools view -S -b sample.sam -o sample.bam
samtools view -bS abc.sam > abc.bam

# BAM转换为SAM
samtools view -h -o out.sam out.bam
samtools view -h  out.sam  > out.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

#筛选出比对质量值大于30的情况
samtools view -q 30 abc.bam|awk '{print $1,$5}' | grep '[IDNSHPX]' |head -4

#筛选出比对成功,但是并不是完全匹配的序列
samtools view -q 30 abc.bam|awk '{print $6}' | grep '[IDNSHPX]' |head -4

# 提取没有比对到参考序列上的比对结果
samtools view -bf 4 abc.bam > abc.f.bam

#比对到反向互补链的reads
samtools view -f 16 abc.bam |head -1

#比对到正向链的reads
samtools view -F 16 abc.bam |head -1

#统计共有多少条reads(pair-end-reads这里算一条)参与了比对参考基因组
echo `samtools view -c abc.bam`/2 |bc

# 提取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

#除格式转换外,顾名思义,view用于查看文件内容
samtools view sample.bam | head
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容