Tutorial: Sam File Format - Lesser Known Tips And Tricks
First the defintion of the Sequence Alignment/Map (SAM). It is aTAB-delimited. Apart from the header lines, which are started with the ‘@’ symbol, each alignment line consists of:
Column Fields Description
QNAMEQuery template/pair NAMEFLAGbitwise FLAGRNAMEReference sequence NAMEPOS1-based leftmost POSition/coordinate of clipped sequenceMAPQMAPping Quality (Phred-scaled)CIGARextended CIGAR stringMRNMMate Reference sequence NaMe (‘=’ if same as RNAME)MPOS1-based Mate POSistionLENinferred Template LENgth (insert size)SEQquery SEQuence on the same strand as the referenceQUALquery QUALity (ASCII-33 gives the Phred base quality)OPTvariable OPTional fields in the format TAG:VTYPE:VALUE
• 24,684 views
•
link
2 answers
Common challenges
- If the reverse strand flag is set in column 2 (
FLAG) ( with value 4) then the sequence reported in column 10 (SEQ) will be the reverse complement! - The 5' end of reads on the forward strand correspond to colum 4 (
POS). To find the 5' end of the read on the reverse strand you will need to use column 4 (POS) and add to that the length that you parse from theCIGARstring that indicates the length of the actual alignment. Writing custom code to do this properly and efficiently is a non-trivial task. One convenient approach is to convert to BED format via thebamtobedcommand ofBedTools. - The value of the
CIGARstring (column 5) and the value of the edit strings in the column 12 options (OPTS) may be different. This depends on the aligner. The alignment process often contains of two steps: a fast heuristics and an optimal alignment. The two values may correspond to each of these processes. - Color space aligners will usually produce a letter space sequence in the 10 (
SEQ) column. Reverting that to the original color space representation is also challenging task (I know of no tools that can do that). - Learn more about the value stored in column 5, mapping quality (
MAPQ) at C: C: C: A: Why there are a lot of MQ0 reads in some particular regions? - The SAM format is 1 based (like the GFF format). The BAM format is 0 based (like the BED format). Usually when viewing and processing BAM files we produce a SAM format from a BAM and the conversion is automatic. But if you read a BAM file directly you will need to account for the coordinate system differences.
Mathematical Model
See the mathematical models used in Samtools: http://www.broadinstitute.org/gatk/media/docs/Samtools.pdf
• 296 views
•
link
This is a handy website that will explain what the sam flags mean (convert numbers into flags and vice versa)
An unofficial fork by the main developer has some changes that have not yet been integrated into samtools.
• 1 views
•
link
Log in to answer this question.
Let's use this thread to add information on the SAM format that may not always be obvious or well documented.