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