sam/ bam format

sam or bam (compressed version) is used to store NGS alignment data.

QNAMEthe sequence/ read name
FLAGbitwise flag
RNAMEthe reference sequence name
POSthe position in the reference sequence (1-based indices)
MAPQthe mapping quaility
CIGARCIGAR strings
RNEXTRef. name of the mate/next read
PNEXTPosition of the mate/next read
TLENthe template length for paired-end reads
SEQthe read sequence
QUALthe base call qualities of the read sequence (same as in FASTQ format)

References:
  1. SAMv1 specs
  2. Galaxy Wiki

sam/ bam bitwise FLAGs

Flag (bin)Flag (hex)Meaning
10x1Paired-end sequence (or multiple-segment, as in stroble sequencing)
20x2Aligned in proper pair (according to the aligner)
40x4Unmapped
80x8Mate pair unmapped (or next segment, if multipe-segment)
160x10Sequence is reverse complemented
320x20Sequence of mate pair is reversed
640x40The first read in the pair (or segment, if multiple-segment)
1280x80The second read in the pair (or segment, if multiple-segment)
2560x100Secondary alignment
5120x200QC failure
10240x400PRC or optical duplicate
20480x800Supplementary alignment

See SAMv1 specs

Count number of reads in a SAM/ BAM file

  1. Assuming each read consists of four lines:
    my.sam | wc -l
    
    Divide by four Note be aware of headers!
    samtools view my.bam | wc -l
    
    Divide by four, e.g. echo $((`samtools view my.bam | wc -l` / 4))
  2. Using samtools and awk:
    samtools idxstats my.bam | awk '{c+=$3+$4} END {print c}'
    

SAM/ BAM flags

Flag (bin)Flag (hex)Meaning
10x1Paired-end sequence (or multiple-segment, as in stroble sequencing)
20x2Aligned in proper pair (according to the aligner)
40x4Unmapped
80x8Mate pair unmapped (or next segment, if multipe-segment)
160x10Sequence is reverse complemented
320x20Sequence of mate pair is reversed
640x40The first read in the pair (or segment, if multiple-segment)
1280x80The second read in the pair (or segment, if multiple-segment)
2560x100Secondary alignment
5120x200QC failure
10240x400PRC or optical duplicate
20480x800Supplementary alignment

Source: Buffalo2015

See also: broadinstitute

bowtie2 paired-end alignment

Bowtie 2 version 2.2.6 by Ben Langmead (langmea@cs.jhu.edu, www.cs.jhu.edu/~langmea)
Usage: 
  bowtie2 [options]* -x  {-1  -2  | -U } [-S ]
time bowtie2 -p 8 -x reference/human/hg19 -1 input_1.fastq -2 input_2.fastq > output.sam
48755614 reads; of these:
  48755614 (100.00%) were paired; of these:
    24164921 (49.56%) aligned concordantly 0 times
    14767863 (30.29%) aligned concordantly exactly 1 time
    9822830 (20.15%) aligned concordantly >1 times
    ----
    24164921 pairs aligned concordantly 0 times; of these:
      4355288 (18.02%) aligned discordantly 1 time
    ----
    19809633 pairs aligned 0 times concordantly or discordantly; of these:
      39619266 mates make up the pairs; of these:
        27256949 (68.80%) aligned 0 times
        8927823 (22.53%) aligned exactly 1 time
        3434494 (8.67%) aligned >1 times
72.05% overall alignment rate

real	44m49.082s
product: Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz