]> git.donarmstrong.com Git - samtools.git/blob - samtools.1
Incorporate patches by Marcel Martin for read counting.
[samtools.git] / samtools.1
1 .TH samtools 1 "27 October 2010" "samtools-0.1.9" "Bioinformatics tools"
2 .SH NAME
3 .PP
4 samtools - Utilities for the Sequence Alignment/Map (SAM) format
5 .SH SYNOPSIS
6 .PP
7 samtools view -bt ref_list.txt -o aln.bam aln.sam.gz
8 .PP
9 samtools sort aln.bam aln.sorted
10 .PP
11 samtools index aln.sorted.bam
12 .PP
13 samtools idxstats aln.sorted.bam
14 .PP
15 samtools view aln.sorted.bam chr2:20,100,000-20,200,000
16 .PP
17 samtools merge out.bam in1.bam in2.bam in3.bam
18 .PP
19 samtools faidx ref.fasta
20 .PP
21 samtools pileup -vcf ref.fasta aln.sorted.bam
22 .PP
23 samtools mpileup -C50 -agf ref.fasta -r chr3:1,000-2,000 in1.bam in2.bam
24 .PP
25 samtools tview aln.sorted.bam ref.fasta
26
27 .SH DESCRIPTION
28 .PP
29 Samtools is a set of utilities that manipulate alignments in the BAM
30 format. It imports from and exports to the SAM (Sequence Alignment/Map)
31 format, does sorting, merging and indexing, and allows to retrieve reads
32 in any regions swiftly.
33
34 Samtools is designed to work on a stream. It regards an input file `-'
35 as the standard input (stdin) and an output file `-' as the standard
36 output (stdout). Several commands can thus be combined with Unix
37 pipes. Samtools always output warning and error messages to the standard
38 error output (stderr).
39
40 Samtools is also able to open a BAM (not SAM) file on a remote FTP or
41 HTTP server if the BAM file name starts with `ftp://' or `http://'.
42 Samtools checks the current working directory for the index file and
43 will download the index upon absence. Samtools does not retrieve the
44 entire alignment file unless it is asked to do so.
45
46 .SH COMMANDS AND OPTIONS
47
48 .TP 10
49 .B view
50 samtools view [-bchuHS] [-t in.refList] [-o output] [-f reqFlag] [-F
51 skipFlag] [-q minMapQ] [-l library] [-r readGroup] [-R rgFile] <in.bam>|<in.sam> [region1 [...]]
52
53 Extract/print all or sub alignments in SAM or BAM format. If no region
54 is specified, all the alignments will be printed; otherwise only
55 alignments overlapping the specified regions will be output. An
56 alignment may be given multiple times if it is overlapping several
57 regions. A region can be presented, for example, in the following
58 format: `chr2' (the whole chr2), `chr2:1000000' (region starting from
59 1,000,000bp) or `chr2:1,000,000-2,000,000' (region between 1,000,000 and
60 2,000,000bp including the end points). The coordinate is 1-based.
61
62 .B OPTIONS:
63 .RS
64 .TP 8
65 .B -b
66 Output in the BAM format.
67 .TP
68 .BI -f " INT"
69 Only output alignments with all bits in INT present in the FLAG
70 field. INT can be in hex in the format of /^0x[0-9A-F]+/ [0]
71 .TP
72 .BI -F " INT"
73 Skip alignments with bits present in INT [0]
74 .TP
75 .B -h
76 Include the header in the output.
77 .TP
78 .B -H
79 Output the header only.
80 .TP
81 .BI -l " STR"
82 Only output reads in library STR [null]
83 .TP
84 .BI -o " FILE"
85 Output file [stdout]
86 .TP
87 .BI -q " INT"
88 Skip alignments with MAPQ smaller than INT [0]
89 .TP
90 .BI -r " STR"
91 Only output reads in read group STR [null]
92 .TP
93 .BI -R " FILE"
94 Output reads in read groups listed in
95 .I FILE
96 [null]
97 .TP
98 .B -S
99 Input is in SAM. If @SQ header lines are absent, the
100 .B `-t'
101 option is required.
102 .TP
103 .B -c
104 Instead of printing the alignments, only count them and print the
105 total number. All filter options, such as
106 .B `-f',
107 .B `-F'
108 and
109 .B `-q'
110 , are taken into account.
111 .TP
112 .BI -t " FILE"
113 This file is TAB-delimited. Each line must contain the reference name
114 and the length of the reference, one line for each distinct reference;
115 additional fields are ignored. This file also defines the order of the
116 reference sequences in sorting. If you run `samtools faidx <ref.fa>',
117 the resultant index file
118 .I <ref.fa>.fai
119 can be used as this
120 .I <in.ref_list>
121 file.
122 .TP
123 .B -u
124 Output uncompressed BAM. This option saves time spent on
125 compression/decomprssion and is thus preferred when the output is piped
126 to another samtools command.
127 .RE
128
129 .TP
130 .B tview
131 samtools tview <in.sorted.bam> [ref.fasta]
132
133 Text alignment viewer (based on the ncurses library). In the viewer,
134 press `?' for help and press `g' to check the alignment start from a
135 region in the format like `chr10:10,000,000' or `=10,000,000' when
136 viewing the same reference sequence.
137
138 .TP
139 .B pileup
140 samtools pileup [-2sSBicv] [-f in.ref.fasta] [-t in.ref_list] [-l
141 in.site_list] [-C capMapQ] [-M maxMapQ] [-T theta] [-N nHap] [-r
142 pairDiffRate] [-m mask] [-d maxIndelDepth] [-G indelPrior]
143 <in.bam>|<in.sam>
144
145 Print the alignment in the pileup format. In the pileup format, each
146 line represents a genomic position, consisting of chromosome name,
147 coordinate, reference base, read bases, read qualities and alignment
148 mapping qualities. Information on match, mismatch, indel, strand,
149 mapping quality and start and end of a read are all encoded at the read
150 base column. At this column, a dot stands for a match to the reference
151 base on the forward strand, a comma for a match on the reverse strand,
152 a '>' or '<' for a reference skip, `ACGTN' for a mismatch on the forward
153 strand and `acgtn' for a mismatch on the reverse strand. A pattern
154 `\\+[0-9]+[ACGTNacgtn]+' indicates there is an insertion between this
155 reference position and the next reference position. The length of the
156 insertion is given by the integer in the pattern, followed by the
157 inserted sequence. Similarly, a pattern `-[0-9]+[ACGTNacgtn]+'
158 represents a deletion from the reference. The deleted bases will be
159 presented as `*' in the following lines. Also at the read base column, a
160 symbol `^' marks the start of a read. The ASCII of the character
161 following `^' minus 33 gives the mapping quality. A symbol `$' marks the
162 end of a read segment.
163
164 If option
165 .B -c
166 is applied, the consensus base, Phred-scaled consensus quality, SNP
167 quality (i.e. the Phred-scaled probability of the consensus being
168 identical to the reference) and root mean square (RMS) mapping quality
169 of the reads covering the site will be inserted between the `reference
170 base' and the `read bases' columns. An indel occupies an additional
171 line. Each indel line consists of chromosome name, coordinate, a star,
172 the genotype, consensus quality, SNP quality, RMS mapping quality, #
173 covering reads, the first alllele, the second allele, # reads supporting
174 the first allele, # reads supporting the second allele and # reads
175 containing indels different from the top two alleles.
176
177 The position of indels is offset by -1.
178
179 .B OPTIONS:
180 .RS
181 .TP 10
182 .B -B
183 Disable the BAQ computation. See the
184 .B mpileup
185 command for details.
186 .TP
187 .B -c
188 Call the consensus sequence using SOAPsnp consensus model. Options
189 .BR -T ", " -N ", " -I " and " -r
190 are only effective when
191 .BR -c " or " -g
192 is in use.
193 .TP
194 .BI -C " INT"
195 Coefficient for downgrading the mapping quality of poorly mapped
196 reads. See the
197 .B mpileup
198 command for details. [0]
199 .TP
200 .BI -d " INT"
201 Use the first
202 .I NUM
203 reads in the pileup for indel calling for speed up. Zero for unlimited. [1024]
204 .TP
205 .BI -f " FILE"
206 The reference sequence in the FASTA format. Index file
207 .I FILE.fai
208 will be created if
209 absent.
210 .TP
211 .B -g
212 Generate genotype likelihood in the binary GLFv3 format. This option
213 suppresses -c, -i and -s. This option is deprecated by the
214 .B mpileup
215 command.
216 .TP
217 .B -i
218 Only output pileup lines containing indels.
219 .TP
220 .BI -I " INT"
221 Phred probability of an indel in sequencing/prep. [40]
222 .TP
223 .BI -l " FILE"
224 List of sites at which pileup is output. This file is space
225 delimited. The first two columns are required to be chromosome and
226 1-based coordinate. Additional columns are ignored. It is
227 recommended to use option
228 .TP
229 .BI -m " INT"
230 Filter reads with flag containing bits in
231 .I INT
232 [1796]
233 .TP
234 .BI -M " INT"
235 Cap mapping quality at INT [60]
236 .TP
237 .BI -N " INT"
238 Number of haplotypes in the sample (>=2) [2]
239 .TP
240 .BI -r " FLOAT"
241 Expected fraction of differences between a pair of haplotypes [0.001]
242 .TP
243 .B -s
244 Print the mapping quality as the last column. This option makes the
245 output easier to parse, although this format is not space efficient.
246 .TP
247 .B -S
248 The input file is in SAM.
249 .TP
250 .BI -t " FILE"
251 List of reference names ane sequence lengths, in the format described
252 for the
253 .B import
254 command. If this option is present, samtools assumes the input
255 .I <in.alignment>
256 is in SAM format; otherwise it assumes in BAM format.
257 .B -s
258 together with
259 .B -l
260 as in the default format we may not know the mapping quality.
261 .TP
262 .BI -T " FLOAT"
263 The theta parameter (error dependency coefficient) in the maq consensus
264 calling model [0.85]
265 .RE
266
267 .TP
268 .B mpileup
269 samtools mpileup [-Bug] [-C capQcoef] [-r reg] [-f in.fa] [-l list] [-M capMapQ] [-Q minBaseQ] [-q minMapQ] in.bam [in2.bam [...]]
270
271 Generate BCF or pileup for one or multiple BAM files. Alignment records
272 are grouped by sample identifiers in @RG header lines. If sample
273 identifiers are absent, each input file is regarded as one sample.
274
275 .B OPTIONS:
276 .RS
277 .TP 8
278 .B -B
279 Disable probabilistic realignment for the computation of base alignment
280 quality (BAQ). BAQ is the Phred-scaled probability of a read base being
281 misaligned. Applying this option greatly helps to reduce false SNPs
282 caused by misalignments.
283 .TP
284 .BI -C " INT"
285 Coefficient for downgrading mapping quality for reads containing
286 excessive mismatches. Given a read with a phred-scaled probability q of
287 being generated from the mapped position, the new mapping quality is
288 about sqrt((INT-q)/INT)*INT. A zero value disables this
289 functionality; if enabled, the recommended value is 50. [0]
290 .TP
291 .BI -f " FILE"
292 The reference file [null]
293 .TP
294 .B -g
295 Compute genotype likelihoods and output them in the binary call format (BCF).
296 .TP
297 .B -u
298 Similar to
299 .B -g
300 except that the output is uncompressed BCF, which is preferred for pipeing.
301 .TP
302 .BI -l " FILE"
303 File containing a list of sites where pileup or BCF is outputted [null]
304 .TP
305 .BI -q " INT"
306 Minimum mapping quality for an alignment to be used [0]
307 .TP
308 .BI -Q " INT"
309 Minimum base quality for a base to be considered [13]
310 .TP
311 .BI -r " STR"
312 Only generate pileup in region
313 .I STR
314 [all sites]
315 .RE
316
317 .TP
318 .B reheader
319 samtools reheader <in.header.sam> <in.bam>
320
321 Replace the header in
322 .I in.bam
323 with the header in
324 .I in.header.sam.
325 This command is much faster than replacing the header with a
326 BAM->SAM->BAM conversion.
327
328 .TP
329 .B sort
330 samtools sort [-no] [-m maxMem] <in.bam> <out.prefix>
331
332 Sort alignments by leftmost coordinates. File
333 .I <out.prefix>.bam
334 will be created. This command may also create temporary files
335 .I <out.prefix>.%d.bam
336 when the whole alignment cannot be fitted into memory (controlled by
337 option -m).
338
339 .B OPTIONS:
340 .RS
341 .TP 8
342 .B -o
343 Output the final alignment to the standard output.
344 .TP
345 .B -n
346 Sort by read names rather than by chromosomal coordinates
347 .TP
348 .B -m INT
349 Approximately the maximum required memory. [500000000]
350 .RE
351
352 .TP
353 .B merge
354 samtools merge [-nur] [-h inh.sam] [-R reg] <out.bam> <in1.bam> <in2.bam> [...]
355
356 Merge multiple sorted alignments.
357 The header reference lists of all the input BAM files, and the @SQ headers of
358 .IR inh.sam ,
359 if any, must all refer to the same set of reference sequences.
360 The header reference list and (unless overridden by
361 .BR -h )
362 `@' headers of
363 .I in1.bam
364 will be copied to
365 .IR out.bam ,
366 and the headers of other files will be ignored.
367
368 .B OPTIONS:
369 .RS
370 .TP 8
371 .BI -h " FILE"
372 Use the lines of
373 .I FILE
374 as `@' headers to be copied to
375 .IR out.bam ,
376 replacing any header lines that would otherwise be copied from
377 .IR in1.bam .
378 .RI ( FILE
379 is actually in SAM format, though any alignment records it may contain
380 are ignored.)
381 .TP
382 .BI -R " STR"
383 Merge files in the specified region indicated by
384 .I STR
385 .TP
386 .B -r
387 Attach an RG tag to each alignment. The tag value is inferred from file names.
388 .TP
389 .B -n
390 The input alignments are sorted by read names rather than by chromosomal
391 coordinates
392 .TP
393 .B -u
394 Uncompressed BAM output
395 .RE
396
397 .TP
398 .B index
399 samtools index <aln.bam>
400
401 Index sorted alignment for fast random access. Index file
402 .I <aln.bam>.bai
403 will be created.
404
405 .TP
406 .B idxstats
407 samtools idxstats <aln.bam>
408
409 Retrieve and print stats in the index file. The output is TAB delimited
410 with each line consisting of reference sequence name, sequence length, #
411 mapped reads and # unmapped reads.
412
413 .TP
414 .B faidx
415 samtools faidx <ref.fasta> [region1 [...]]
416
417 Index reference sequence in the FASTA format or extract subsequence from
418 indexed reference sequence. If no region is specified,
419 .B faidx
420 will index the file and create
421 .I <ref.fasta>.fai
422 on the disk. If regions are speficified, the subsequences will be
423 retrieved and printed to stdout in the FASTA format. The input file can
424 be compressed in the
425 .B RAZF
426 format.
427
428 .TP
429 .B fixmate
430 samtools fixmate <in.nameSrt.bam> <out.bam>
431
432 Fill in mate coordinates, ISIZE and mate related flags from a
433 name-sorted alignment.
434
435 .TP
436 .B rmdup
437 samtools rmdup [-sS] <input.srt.bam> <out.bam>
438
439 Remove potential PCR duplicates: if multiple read pairs have identical
440 external coordinates, only retain the pair with highest mapping quality.
441 In the paired-end mode, this command
442 .B ONLY
443 works with FR orientation and requires ISIZE is correctly set. It does
444 not work for unpaired reads (e.g. two ends mapped to different
445 chromosomes or orphan reads).
446
447 .B OPTIONS:
448 .RS
449 .TP 8
450 .B -s
451 Remove duplicate for single-end reads. By default, the command works for
452 paired-end reads only.
453 .TP 8
454 .B -S
455 Treat paired-end reads and single-end reads.
456 .RE
457
458 .TP
459 .B calmd
460 samtools calmd [-eubSr] [-C capQcoef] <aln.bam> <ref.fasta>
461
462 Generate the MD tag. If the MD tag is already present, this command will
463 give a warning if the MD tag generated is different from the existing
464 tag. Output SAM by default.
465
466 .B OPTIONS:
467 .RS
468 .TP 8
469 .B -e
470 Convert a the read base to = if it is identical to the aligned reference
471 base. Indel caller does not support the = bases at the moment.
472 .TP
473 .B -u
474 Output uncompressed BAM
475 .TP
476 .B -b
477 Output compressed BAM
478 .TP
479 .B -S
480 The input is SAM with header lines
481 .TP
482 .BI -C " INT"
483 Coefficient to cap mapping quality of poorly mapped reads. See the
484 .B pileup
485 command for details. [0]
486 .TP
487 .B -r
488 Perform probabilistic realignment to compute BAQ, which will be used to
489 cap base quality.
490 .RE
491
492 .SH SAM FORMAT
493
494 SAM is TAB-delimited. Apart from the header lines, which are started
495 with the `@' symbol, each alignment line consists of:
496
497 .TS
498 center box;
499 cb | cb | cb
500 n | l | l .
501 Col     Field   Description
502 _
503 1       QNAME   Query (pair) NAME
504 2       FLAG    bitwise FLAG
505 3       RNAME   Reference sequence NAME
506 4       POS     1-based leftmost POSition/coordinate of clipped sequence
507 5       MAPQ    MAPping Quality (Phred-scaled)
508 6       CIAGR   extended CIGAR string
509 7       MRNM    Mate Reference sequence NaMe (`=' if same as RNAME)
510 8       MPOS    1-based Mate POSistion
511 9       ISIZE   Inferred insert SIZE
512 10      SEQ     query SEQuence on the same strand as the reference
513 11      QUAL    query QUALity (ASCII-33 gives the Phred base quality)
514 12      OPT     variable OPTional fields in the format TAG:VTYPE:VALUE
515 .TE
516
517 .PP
518 Each bit in the FLAG field is defined as:
519
520 .TS
521 center box;
522 cb | cb | cb
523 l | c | l .
524 Flag    Chr     Description
525 _
526 0x0001  p       the read is paired in sequencing
527 0x0002  P       the read is mapped in a proper pair
528 0x0004  u       the query sequence itself is unmapped
529 0x0008  U       the mate is unmapped
530 0x0010  r       strand of the query (1 for reverse)
531 0x0020  R       strand of the mate
532 0x0040  1       the read is the first read in a pair
533 0x0080  2       the read is the second read in a pair
534 0x0100  s       the alignment is not primary
535 0x0200  f       the read fails platform/vendor quality checks
536 0x0400  d       the read is either a PCR or an optical duplicate
537 .TE
538
539 .SH EXAMPLES
540 .IP o 2
541 Import SAM to BAM when
542 .B @SQ
543 lines are present in the header:
544
545   samtools view -bS aln.sam > aln.bam
546
547 If
548 .B @SQ
549 lines are absent:
550
551   samtools faidx ref.fa
552   samtools view -bt ref.fa.fai aln.sam > aln.bam
553
554 where
555 .I ref.fa.fai
556 is generated automatically by the
557 .B faidx
558 command.
559
560 .IP o 2
561 Attach the
562 .B RG
563 tag while merging sorted alignments:
564
565   perl -e 'print "@RG\\tID:ga\\tSM:hs\\tLB:ga\\tPL:Illumina\\n@RG\\tID:454\\tSM:hs\\tLB:454\\tPL:454\\n"' > rg.txt
566   samtools merge -rh rg.txt merged.bam ga.bam 454.bam
567
568 The value in a
569 .B RG
570 tag is determined by the file name the read is coming from. In this
571 example, in the
572 .IR merged.bam ,
573 reads from
574 .I ga.bam
575 will be attached 
576 .IR RG:Z:ga ,
577 while reads from
578 .I 454.bam
579 will be attached
580 .IR RG:Z:454 .
581
582 .IP o 2
583 Call SNPs and short indels for one diploid individual:
584
585   samtools pileup -vcf ref.fa aln.bam > var.raw.plp
586   samtools.pl varFilter -D 100 var.raw.plp > var.flt.plp
587   awk '($3=="*"&&$6>=50)||($3!="*"&&$6>=20)' var.flt.plp > var.final.plp
588
589 The
590 .B -D
591 option of varFilter controls the maximum read depth, which should be
592 adjusted to about twice the average read depth.  One may consider to add
593 .B -C50
594 to
595 .B pileup
596 if mapping quality is overestimated for reads containing excessive
597 mismatches. Applying this option usually helps
598 .B BWA-short
599 but may not other mappers. It also potentially increases reference
600 biases.
601
602 .IP o 2
603 Call SNPs (not short indels) for multiple diploid individuals:
604
605   samtools mpileup -augf ref.fa *.bam | bcftools view -bcv - > snp.raw.bcf
606   bcftools view snp.raw.bcf | vcfutils.pl filter4vcf -D 2000 | bgzip > snp.flt.vcf.gz
607
608 Individuals are identified from the
609 .B SM
610 tags in the
611 .B @RG
612 header lines. Individuals can be pooled in one alignment file; one
613 individual can also be separated into multiple files. In addition, one
614 may consider to apply
615 .B -C50
616 to
617 .BR mpileup .
618
619 SNP calling with mpileup also works for single sample and has the
620 advantage of enabling more powerful filtering. The drawback is the lack
621 of short indel calling, which may be implemented in future.
622
623 .IP o 2
624 Derive the allele frequency spectrum (AFS) on a list of sites from multiple individuals:
625
626   samtools mpileup -gf ref.fa *.bam > all.bcf
627   bcftools view -bl sites.list all.bcf > sites.bcf
628   bcftools view -cGP cond2 sites.bcf > /dev/null 2> sites.1.afs
629   bcftools view -cGP sites.1.afs sites.bcf > /dev/null 2> sites.2.afs
630   bcftools view -cGP sites.2.afs sites.bcf > /dev/null 2> sites.3.afs
631   ......
632
633 where
634 .I sites.list
635 contains the list of sites with each line consisting of the reference
636 sequence name and position. The following
637 .B bcftools
638 commands estimate AFS by EM.
639
640 .IP o 2
641 Dump BAQ applied alignment for other SNP callers:
642
643   samtools calmd -br aln.bam > aln.baq.bam
644
645 It adds and corrects the
646 .B NM
647 and
648 .B MD
649 tags at the same time. The
650 .B calmd
651 command also comes with the
652 .B -C
653 option, the same as the one in
654 .B pileup
655 and
656 .BR mpileup .
657 Apply if it helps.
658
659 .SH LIMITATIONS
660 .PP
661 .IP o 2
662 Unaligned words used in bam_import.c, bam_endian.h, bam.c and bam_aux.c.
663 .IP o 2
664 In merging, the input files are required to have the same number of
665 reference sequences. The requirement can be relaxed. In addition,
666 merging does not reconstruct the header dictionaries
667 automatically. Endusers have to provide the correct header. Picard is
668 better at merging.
669 .IP o 2
670 Samtools paired-end rmdup does not work for unpaired reads (e.g. orphan
671 reads or ends mapped to different chromosomes). If this is a concern,
672 please use Picard's MarkDuplicate which correctly handles these cases,
673 although a little slower.
674
675 .SH AUTHOR
676 .PP
677 Heng Li from the Sanger Institute wrote the C version of samtools. Bob
678 Handsaker from the Broad Institute implemented the BGZF library and Jue
679 Ruan from Beijing Genomics Institute wrote the RAZF library. John
680 Marshall and Petr Danecek contribute to the source code and various
681 people from the 1000 Genomes Project have contributed to the SAM format
682 specification.
683
684 .SH SEE ALSO
685 .PP
686 Samtools website: <http://samtools.sourceforge.net>