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