SAM (file format)


Sequence Alignment Map is a text-based format originally for storing biological sequences aligned to a reference sequence developed by Heng Li and Bob Handsaker et al. It is widely used for storing data, such as nucleotide sequences, generated by next generation sequencing technologies, and the standard has been broadened to include unmapped sequences. The format supports short and long reads produced by different sequencing platforms and is used to hold mapped data within the Genome Analysis Toolkit and across the Broad Institute, the Wellcome Sanger Institute, and throughout the 1000 Genomes Project.

Format

The SAM format consists of a header and an alignment section. The binary equivalent of a SAM file is a Binary Alignment Map file, which stores the same data in a compressed binary representation. SAM files can be analysed and edited with the software SAMtools. The header section must be prior to the alignment section if it is present. Headings begin with the '@' symbol, which distinguishes them from the alignment section. Alignment sections have 11 mandatory fields, as well as a variable number of optional fields.
ColFieldTypeBrief description
1QNAMEStringQuery template NAME
2FLAGIntbitwise FLAG
3RNAMEStringReferences sequence NAME
4POSInt1- based leftmost mapping POSition
5MAPQIntMAPping Quality
6CIGARStringCIGAR String
7RNEXTStringRef. name of the mate/next read
8PNEXTIntPosition of the mate/next read
9TLENIntobserved Template LENgth
10SEQStringsegment SEQuence
11QUALStringASCII of Phred-scaled base QUALity+33

Description

From the specification:
  1. QNAME: Query template NAME. Reads/segments having identical QNAME are regarded to come from the same template. A QNAME ‘*’ indicates the information is unavailable. In a SAM file, a read may occupy multiple alignment lines, when its alignment is chimeric or when multiple mappings are given.
  2. FLAG: Combination of bitwise FLAGs
  3. RNAME: Reference sequence NAME of the alignment. If @SQ header lines are present, RNAME must be present in one of the SQ-SN tag. An unmapped segment without coordinate has a ‘*’ at this field. However, an unmapped segment may also have an ordinary coordinate such that it can be placed at a desired position after sorting. If RNAME is ‘*’, no assumptions can be made about POS and CIGAR.
  4. POS: 1-based leftmost mapping POSition of the first matching base. The first base in a reference sequence has coordinate 1. POS is set as 0 for an unmapped read without coordinate. If POS is 0, no assumptions can be made about RNAME and CIGAR.
  5. MAPQ: MAPping Quality. It equals −10 log10 Pr, rounded to the nearest integer. A value 255 indicates that the mapping quality is not available.
  6. CIGAR: Concise Idiosyncratic Gapped Alignment Report string.
  7. RNEXT: Reference sequence name of the primary alignment of the NEXT read in the template. For the last read, the next read is the first read in the template. If @SQ header lines are present, RNEXT must be present in one of the SQ-SN tag. This field is set as ‘*’ when the information is unavailable, and set as ‘=’ if RNEXT is identical RNAME. If not ‘=’ and the next read in the template has one primary mapping, this field is identical to RNAME at the primary line of the next read. If RNEXT is ‘*’, no assumptions can be made on PNEXT and bit 0x20.
  8. PNEXT: Position of the primary alignment of the NEXT read in the template. Set as 0 when the information is unavailable. This field equals POS at the primary line of the next read. If PNEXT is 0, no assumptions can be made on RNEXT and bit 0x20.
  9. TLEN: signed observed Template LENgth. If all segments are mapped to the same reference, the unsigned observed template length equals the number of bases from the leftmost mapped base to the rightmost mapped base. The leftmost segment has a plus sign and the rightmost has a minus sign. The sign of segments in the middle is undefined. It is set as 0 for single-segment template or when the information is unavailable.
  10. SEQ: segment SEQuence. This field can be a ‘*’ when the sequence is not stored. If not a ‘*’, the length of the sequence must equal the sum of lengths of M/I/S/=/X operations in CIGAR. An ‘=’ denotes the base is identical to the reference base. No assumptions can be made on the letter cases.
  11. QUAL: ASCII of base QUALity plus 33. A base quality is the phred-scaled base error probability which equals −10 log10 Pr. This field can be a ‘*’ when quality is not stored. If not a ‘*’, SEQ must not be a ‘*’ and the length of the quality string ought to equal the length of SEQ.

    Bitwise flags

The FLAG field is displayed as a single integer, but is the sum of bitwise flags to denote multiple attributes of a read alignment. Each attribute denotes one bit in the binary representation of the integer.
IntegerBinaryDescription
1000000000001template having multiple templates in sequencing
2000000000010each segment properly aligned according to the aligner
4000000000100segment unmapped
8000000001000next segment in the template unmapped
16000000010000SEQ being reverse complemented
32000000100000SEQ of the next segment in the template being reverse complemented
64000001000000the first segment in the template
128000010000000the last segment in the template
256000100000000not primary alignment
512001000000000alignment fails quality checks
1024010000000000PCR or optical duplicate
2048100000000000supplementary alignment

The FLAG attributes are summed to get the final value, e.g. a SAM row resulting from an Illumina paired-end FASTQ record having the FLAG value 2145 would indicate:
Flag ValueMeaningFlag Sum
1read is paired1
32read2 was reverse complemented33
64read197
2048Supplementary alignment2145

Optional fields

From the specification:
The type may be one of A, B, f, H, i, or Z.
TagTypeDescription
AMiThe smallest template-independent mapping quality in the template
ASiAlignment score generated by aligner
BCZBarcode sequence identifying the sample
BQZOffset to base alignment quality
BZZPhred quality of the unique molecular barcode bases in the OX tag
CBZCell identifier
CCZReference name of the next hit
CGB,IBAM only: CIGAR in BAM's binary encoding if it consists of >65535 operators
CMiEdit distance between the color sequence and the color reference
COZFree-text comments
CPiLeftmost coordinate of the next hit
CQZColor read base qualities
CRZCellular barcode sequence bases
CSZColor read sequence
CTZComplete read annotation tag, used for consensus annotation dummy features
CYZPhred quality of the cellular barcode sequence in the CR tag
E2ZThe 2nd most likely base calls
FIiThe index of segment in the template
FSZSegment suffix
FZB,SFlow signal intensities
GC?Reserved for backwards compatibility reasons
GQ?Reserved for backwards compatibility reasons
GS?Reserved for backwards compatibility reasons
H0iNumber of perfect hits
H1iNumber of 1-difference hits
H2iNumber of 2-difference hits
HIiQuery hit index
IHiQuery hit total count
LBZLibrary
MCZCIGAR string for mate/next segment
MDZString for mismatching positions
MF?Reserved for backwards compatibility reasons
MIZMolecular identifier; a string that uniquely identifies the molecule from which the record was derived
MQiMapping quality of the mate/next segment
NHiNumber of reported alignments that contain the query in the current record
NMiEdit distance to the reference
OAZOriginal alignment
OCZOriginal CIGAR
OPiOriginal mapping position
OQZOriginal base quality
OXZOriginal unique molecular barcode bases
PGZProgram
PQiPhred likelihood of the template
PTZRead annotations for parts of the padded read sequence
PUZPlatform unit
Q2ZPhred quality of the mate/next segment sequence in the R2 tag
QTZPhred quality of the sample barcode sequence in the BC tag
QXZQuality score of the unique molecular identifier in the RX tag
R2ZSequence of the mate/next segment in the template
RGZRead group
RT?Reserved for backwards compatibility reasons
RXZSequence bases of the unique molecular identifier
S2?Reserved for backwards compatibility reasons
SAZOther canonical alignments in a chimeric alignment
SMiTemplate-independent mapping quality
SQ?Reserved for backwards compatibility reasons
TCiThe number of segments in the template
U2ZPhred probability of the 2nd call being wrong conditional on the best being wrong
UQiPhred likelihood of the segment, conditional on the mapping being correct
X??Reserved for end users
Y??Reserved for end users
Z??Reserved for end users