๐ง Read Alignment Fundamentals
๐ฏ What is Read Alignment?
Read alignment (also called read mapping) is the process of finding the position in a reference genome where each sequencing read most likely originated. It's a fundamental step in NGS analysis, enabling variant calling, RNA-Seq quantification, ChIP-Seq peak calling, and many other applications. It's the bridge between raw sequencing data and biological insight.
Why Alignment Matters
Position: Where reads map. Variants: Compare to reference. Expression: Count reads. Peaks: ChIP-Seq. Splicing: RNA. Foundation: All NGS analysis.
# Read Alignment โ the big picture
# Mapping sequencing reads to a reference genome.
# CONCEPTUAL OVERVIEW
print("""
=== Read Alignment ===
What it is:
- Find read position in reference
- Read mapping
- Foundation of NGS analysis
Why it matters:
- Variant calling
- RNA-Seq
- ChIP-Seq
- ATAC-Seq
- Metagenomics
- Clinical
Key concepts:
- Reference genome
- Index (BWT, FM-index)
- Seed and extend
- CIGAR
- MAPQ
- Paired-end
Algorithms:
- Hash table (old)
- Burrows-Wheeler Transform (BWT)
- FM-index
- Suffix array
- Minimizer
Tools:
- BWA-MEM (short reads)
- BWA-MEM2 (faster)
- Bowtie2 (short reads)
- minimap2 (long reads)
- HISAT2 (RNA)
- STAR (RNA)
Workflow:
1. Build index
2. Align reads
3. Sort BAM
4. Mark duplicates
5. Index BAM
6. QC (flagstat)
7. Downstream analysis
Applications:
- WGS
- WES
- RNA-Seq
- ChIP-Seq
- ATAC-Seq
- Metagenomics
""")
# WHY IT MATTERS
print("=== Why It Matters ===")
print("""
Biological questions:
- Where does this read come from?
- What variants does this sample have?
- What genes are expressed?
- Where do proteins bind?
- What is the structure?
Alignment answers:
- Genomic coordinates
- Variant positions
- Expression counts
- Binding sites
- Structural variants
""")
# HISTORICAL CONTEXT
print("=== Historical Context ===")
print("""
1990s: BLAST, FASTA (pairwise)
2001: BLAT (fast)
2005: NGS era
2008: Bowtie (BWT)
2009: BWA (BWT)
2012: BWA-MEM
2013: Bowtie2
2018: minimap2 (long reads)
2020: BWA-MEM2
2020s: Graph genomes
""")
# CHALLENGES
print("=== Challenges ===")
print("""
1. SPEED
- Billions of reads
- Human genome (3 Gb)
- Need fast algorithms
2. ACCURACY
- Sequencing errors
- Variants
- Repeats
3. REPEATS
- Multi-mapping
- Ambiguity
- MAPQ
4. SPLICING
- RNA-Seq
- Introns
- Novel junctions
5. STRUCTURAL VARIANTS
- Short reads
- Long reads
- Complex
""")๐ฏ Why Alignment Matters
๐ฏ The Value of Alignment
Alignment is the foundational step that enables virtually all downstream NGS analyses.
Key Applications
Variants: Compare. Expression: Count. Peaks: ChIP. Splicing: RNA. SVs: Structure. Clinical: Diagnostics.
# Why Alignment Matters
print("=== 1. Variant Calling ===")
print("""
Variant calling:
- Compare reads to reference
- Identify differences
- SNPs, indels
Workflow:
1. Align reads (BWA)
2. Sort BAM
3. Call variants (GATK)
4. Filter
5. Annotate
Examples:
- Clinical
- Cancer
- Population
Tools:
- BWA
- GATK
- VEP
""")
print("\n=== 2. RNA-Seq ===")
print("""
RNA-Seq:
- Align reads to genome/transcriptome
- Count expression
- Detect splicing
Workflow:
1. Align (STAR)
2. Quantify (featureCounts)
3. DE analysis (DESeq2)
4. Pathway
Examples:
- Disease
- Drug
- Development
Tools:
- STAR
- HISAT2
- Salmon
""")
print("\n=== 3. ChIP-Seq ===")
print("""
ChIP-Seq:
- Align reads
- Find peaks
- Binding sites
Workflow:
1. Align (Bowtie2)
2. Sort BAM
3. Peak calling (MACS2)
4. Motif (HOMER)
Examples:
- TF binding
- Histone marks
- Enhancers
Tools:
- Bowtie2
- MACS2
- HOMER
""")
print("\n=== 4. ATAC-Seq ===")
print("""
ATAC-Seq:
- Align reads
- Open chromatin
- Regulatory elements
Workflow:
1. Align (Bowtie2)
2. Filter (MAPQ)
3. Peak calling (MACS2)
4. Motif (HOMER)
Tools:
- Bowtie2
- MACS2
- HOMER
""")
print("\n=== 5. Structural Variants ===")
print("""
SV:
- Discordant pairs
- Split reads
- Coverage
Workflow:
1. Align (BWA, minimap2)
2. Call SVs (Delly, Manta)
3. Filter
4. Annotate
Tools:
- BWA
- minimap2
- Delly
- Manta
""")
print("\n=== 6. Metagenomics ===")
print("""
Metagenomics:
- Align to reference
- Taxonomic assignment
- Functional
Workflow:
1. Align (Bowtie2)
2. Filter host
3. Taxonomic (Kraken)
4. Assembly
Tools:
- Bowtie2
- Kraken2
- MetaPhlAn
""")
print("\n=== 7. Clinical ===")
print("""
Clinical:
- Align patient reads
- Call variants
- Report
Workflow:
1. Align (BWA)
2. Call (GATK)
3. Annotate (VEP)
4. Report
Examples:
- Rare disease
- Cancer
- Pharmacogenomics
Tools:
- BWA
- GATK
- VEP
""")
print("\n=== 8. Single-Cell ===")
print("""
Single-cell:
- Align reads
- Cell barcodes
- UMI
Workflow:
1. Cell Ranger (align)
2. Count matrix
3. Seurat/Scanpy
Tools:
- Cell Ranger
- STARsolo
- Kallisto|Bustools
""")๐ Key Concepts
๐ฏ Understanding the Terminology
Read alignment has its own vocabulary. Understanding key concepts is essential.
Concepts
Reference: Genome. Index: BWT. Seed: Match. Extend: Align. CIGAR: Alignment. MAPQ: Quality. Paired: Two reads.
# Key Concepts
print("=== Reference Genome ===")
print(""
Reference:
- Assembled genome
- FASTA format
- Chromosomes
- Used for alignment
Examples:
- GRCh38 (human)
- GRCm39 (mouse)
- TAIR10 (Arabidopsis)
- K-12 MG1655 (E. coli)
Download:
- UCSC
- Ensembl
- NCBI
""")
print("\n=== Index ===")
print(""
Index:
- Preprocessed reference
- Fast lookup
- BWT, FM-index
Types:
- BWA index
- Bowtie2 index
- minimap2 index
- STAR index
Building:
bwa index reference.fa
bowtie2-build reference.fa index_prefix
""")
print("\n=== Seed and Extend ===")
print(""
Seed:
- Short exact match
- Anchor
- k-mer
Extend:
- Align around seed
- Allow mismatches
- Dynamic programming
Example:
Read: ACGTACGTACGT
Seed: ACGT (matches reference)
Extend: align full read
""")
print("\n=== CIGAR ===")
print(""
CIGAR:
- Compact Idiosyncratic Gapped Alignment Report
- How read aligns
Operations:
M: Match/mismatch
I: Insertion
D: Deletion
N: Skipped (intron)
S: Soft clip
H: Hard clip
P: Padding
=: Match
X: Mismatch
Example:
100M = 100 bp match
50M100N50M = 50M, 100N, 50M
10S90M = 10 bp soft clip, 90M
""")
print("\n=== MAPQ ===")
print(""
MAPQ:
- Mapping quality
- Phred-scaled
- Confidence
Interpretation:
- 0: Multi-mapping
- 1-20: Ambiguous
- 20-40: Good
- 60: Unique (BWA)
Filtering:
- MAPQ >= 20: Common
- MAPQ >= 30: Stringent
- MAPQ >= 60: Unique only
Note:
- Different scales (BWA, Bowtie2)
- Not directly comparable
""")
print("\n=== Paired-End ===")
print(""
Paired-end:
- Two reads per fragment
- Known distance
- Better alignment
Advantages:
- Better mapping
- SV detection
- Assembly
- Repeat resolution
Flags:
- 0x1: Paired
- 0x2: Proper pair
- 0x4: Unmapped
- 0x8: Mate unmapped
- 0x10: Reverse
- 0x20: Mate reverse
- 0x40: First in pair
- 0x80: Second in pair
""")
print("\n=== Soft vs Hard Clip ===")
print(""
Soft clip (S):
- Clipped from alignment
- Kept in sequence
- Can be real
Hard clip (H):
- Clipped from alignment
- Removed from sequence
- Usually adapter
Example:
10S90M: 10 bp soft clip, 90M
10H90M: 10 bp hard clip, 90M
""")
print("\n=== Duplicates ===")
print(""
Duplicates:
- Same start/end
- PCR artifact
- Remove
Types:
- PCR duplicates
- Optical duplicates
Tools:
- Picard MarkDuplicates
- samtools markdup
Impact:
- Coverage bias
- Variant calling
""")
print("\n=== Multi-Mapping ===")
print(""
Multi-mapping:
- Read maps to multiple places
- Repeats
- Ambiguous
Handling:
- MAPQ 0
- Report all (NH tag)
- Choose one
- Filter
Examples:
- rRNA
- Transposons
- Segmental duplications
""")โ๏ธ Alignment Algorithms
๐ฏ How Alignment Works
Read alignment algorithms use sophisticated data structures to quickly find matches in a reference genome.
Algorithms
Hash: Old. BWT: Burrows-Wheeler. FM-index: Compressed. Seed: Anchor. Extend: Align. Minimizer: Long reads.
# Alignment Algorithms
print("=== 1. Hash Table ===")
print(""
Hash:
- Precompute all k-mers
- Hash table lookup
- Fast lookup
- Memory heavy
Tools:
- MAQ
- SOAP
- Early aligners
Limitations:
- Memory
- Repeats
- Not for large genomes
""")
print("\n=== 2. Burrows-Wheeler Transform ===")
print(""
BWT:
- Reversible transformation
- Compressed index
- Fast search
Steps:
1. Build BWT
2. FM-index
3. Backward search
Advantages:
- Memory efficient
- Fast
- Exact matching
Tools:
- BWA
- Bowtie
- Bowtie2
""")
print("\n=== 3. FM-index ===")
print(""
FM-index:
- Full-text index
- BWT + suffix array
- Backward search
Advantages:
- Compressed
- Fast
- Exact matching
Used by:
- BWA
- Bowtie2
- BWA-MEM2
""")
print("\n=== 4. Seed and Extend ===")
print(""
Seed and extend:
1. Find seeds (exact matches)
2. Extend to full read
3. Allow mismatches
4. Score
Seeds:
- k-mers
- Pigeonhole
- Spaced seeds
Extension:
- Dynamic programming
- Banded
- SIMD
""")
print("\n=== 5. Minimizer ===")
print(""
Minimizer:
- Representative k-mer
- Reduce k-mer set
- Fast
- Long reads
Used by:
- minimap2
- Winnowmap
- Mash
Advantages:
- Fast
- Memory efficient
- Long reads
""")
print("\n=== 6. Smith-Waterman ===")
print(""
Smith-Waterman:
- Local alignment
- Dynamic programming
- Optimal
- Slow
Use:
- Validation
- Small sequences
- Seeds
Complexity:
O(mn)
""")
print("\n=== 7. Banded Alignment ===")
print(""
Banded:
- Diagonal band
- Reduce complexity
- Fast
Use:
- Extension
- Similar sequences
Complexity:
O(kn)
k = band width
""")
print("\n=== Algorithm Comparison ===")
print(f"{'Algorithm':<20} {'Speed':<15} {'Memory':<15} {'Tools':<20}")
print("-" * 70)
print(f"{'Hash':<20} {'Fast':<15} {'High':<15} {'MAQ':<20}")
print(f"{'BWT':<20} {'Fast':<15} {'Low':<15} {'BWA':<20}")
print(f"{'FM-index':<20} {'Fast':<15} {'Low':<15} {'Bowtie2':<20}")
print(f"{'Seed-extend':<20} {'Medium':<15} {'Medium':<15} {'BWA-MEM':<20}")
print(f"{'Minimizer':<20} {'Fast':<15} {'Low':<15} {'minimap2':<20}")
print(f"{'Smith-Waterman':<20} {'Slow':<15} {'Low':<15} {'SSEARCH':<20}")
print("\n=== Modern Aligners ===")
print(""
BWA-MEM:
- BWT + seed-extend
- Short reads
- Standard
BWA-MEM2:
- Faster BWA
- SIMD
- Same output
Bowtie2:
- BWT + FM-index
- Short reads
- Sensitive
minimap2:
- Minimizer
- Long reads
- Fast
STAR:
- Suffix array
- RNA-Seq
- Splicing
HISAT2:
- Hierarchical
- RNA-Seq
- Graph
""")๐ต BWA & BWA-MEM
๐ฏ The Standard Aligner
BWA (Burrows-Wheeler Aligner) is the most widely used short-read aligner, with BWA-MEM as the standard algorithm.
BWA
BWA-MEM: Standard. BWA-MEM2: Faster. BWT: Index. Seed: Anchor. Extend: Align. Paired: Support.
# BWA & BWA-MEM
print("=== BWA Algorithms ===")
print(""
BWA:
- Burrows-Wheeler Aligner
- Short reads
Algorithms:
- BWA-backtrack: <= 100 bp
- BWA-SW: 70-1M bp
- BWA-MEM: 70 bp - 1M bp (recommended)
BWA-MEM:
- Seed and extend
- Paired-end
- Split reads
- Standard
""")
print("\n=== Build Index ===")
print(""
Command:
bwa index reference.fa
Output:
reference.fa.amb
reference.fa.ann
reference.fa.bwt
reference.fa.pac
reference.fa.sa
Note:
- One-time
- ~1 hour for human
- 5-6 GB index
""")
print("\n=== Align Reads ===")
print(""
Single-end:
bwa mem -t 8 reference.fa reads.fq > sample.sam
Paired-end:
bwa mem -t 8 reference.fa reads_1.fq reads_2.fq > sample.sam
Options:
-t 8: Threads
-R: Read group
-M: Mark split
-K: Chunk size
-Y: Soft clip
""")
print("\n=== Common Options ===")
print(""
-t INT:
- Threads
- Default 1
-k INT:
- Minimum seed length
- Default 19
-w INT:
- Band width
- Default 100
-A INT:
- Match score
- Default 1
-B INT:
- Mismatch penalty
- Default 4
-O INT:
- Gap open penalty
- Default 6
-E INT:
- Gap extension
- Default 1
-R STR:
- Read group
- Important!
-Y:
- Soft clip
- For SV
""")
print("\n=== Read Group ===")
print(""
Read group:
- Sample metadata
- Required for GATK
Format:
-R '@RG\tID:sample1\tSM:sample1\tPL:ILLUMINA\tLB:lib1\tPU:unit1'
Tags:
ID: Read group ID
SM: Sample
PL: Platform
LB: Library
PU: Platform unit
Important:
- Always specify
- Required downstream
""")
print("\n=== Full Workflow ===")
print(""
# 1. Index
bwa index reference.fa
# 2. Align
bwa mem -t 8 \
-R '@RG\tID:s1\tSM:s1\tPL:ILLUMINA' \
reference.fa \
reads_1.fq.gz reads_2.fq.gz | \
samtools sort -@ 8 -o sample.bam -
# 3. Index BAM
samtools index sample.bam
# 4. QC
samtools flagstat sample.bam
""")
print("\n=== BWA-MEM2 ===")
print(""
BWA-MEM2:
- Faster BWA-MEM
- SIMD
- Same output
Speed:
- 2-3x faster
- Same accuracy
Command:
bwa-mem2 index reference.fa
bwa-mem2 mem -t 8 reference.fa reads.fq > sample.sam
Note:
- Larger index
- Same options
""")
print("\n=== Best Practices ===")
print(""
1. Use BWA-MEM
2. Specify read group
3. Use paired-end
4. Set threads
5. Pipe to samtools
6. Sort BAM
7. Index BAM
8. Check flagstat
9. Mark duplicates
10. Document
""")๐ข Bowtie2
๐ฏ Memory-Efficient Aligner
Bowtie2 is a fast, memory-efficient aligner, widely used for ChIP-Seq, ATAC-Seq, and other short-read applications.
Bowtie2
BWT: Index. FM-index: Search. Sensitive: Good. Memory: Low. ChIP: Standard. Local: Mode.
# Bowtie2
print("=== Overview ===")
print(""
Bowtie2:
- BWT + FM-index
- Short reads
- Memory efficient
- Sensitive
Advantages:
- Low memory
- Fast
- Good sensitivity
- Local + end-to-end
Best for:
- ChIP-Seq
- ATAC-Seq
- RNA-Seq
- Small genomes
""")
print("\n=== Build Index ===")
print(""
Command:
bowtie2-build reference.fa index_prefix
Output:
index_prefix.1.bt2
index_prefix.2.bt2
index_prefix.3.bt2
index_prefix.4.bt2
index_prefix.rev.1.bt2
index_prefix.rev.2.bt2
Note:
- One-time
- ~1 hour for human
- 3-4 GB index
""")
print("\n=== Align Reads ===")
print(""
Single-end:
bowtie2 -p 8 -x index_prefix -U reads.fq -S sample.sam
Paired-end:
bowtie2 -p 8 -x index_prefix \
-1 reads_1.fq -2 reads_2.fq \
-S sample.sam
Options:
-p 8: Threads
-x: Index prefix
-U: Single-end
-1, -2: Paired-end
-S: Output SAM
--very-sensitive: More sensitive
""")
print("\n=== Presets ===")
print(""
--very-fast:
- Fastest
- Least sensitive
--fast:
- Fast
- Less sensitive
--sensitive:
- Default
- Balanced
--very-sensitive:
- Most sensitive
- Slower
For ChIP-Seq:
--very-sensitive
For RNA-Seq:
--sensitive
For ATAC-Seq:
--very-sensitive
""")
print("\n=== Common Options ===")
print(""
-p INT:
- Threads
-x STR:
- Index prefix
-1, -2:
- Paired reads
-U:
- Single-end
-S:
- Output SAM
--no-unal:
- No unaligned reads
--no-mixed:
- No mixed pairs
--no-discordant:
- No discordant
--dovetail:
- Dovetail pairs
""")
print("\n=== Local vs End-to-End ===")
print(""
End-to-end:
- Full read alignment
- No clipping
- Default
Local:
- Soft clipping
- Partial alignment
- --local
Use local:
- ChIP-Seq
- ATAC-Seq
- Reads with adapters
Use end-to-end:
- WGS
- High-quality reads
""")
print("\n=== Full Workflow ===")
print(""
# 1. Index
bowtie2-build reference.fa index_prefix
# 2. Align
bowtie2 -p 8 \
-x index_prefix \
-1 reads_1.fq.gz -2 reads_2.fq.gz \
--very-sensitive \
--no-unal | \
samtools sort -@ 8 -o sample.bam -
# 3. Index BAM
samtools index sample.bam
# 4. QC
samtools flagstat sample.bam
""")
print("\n=== BWA vs Bowtie2 ===")
print(f"{'Feature':<20} {'BWA-MEM':<20} {'Bowtie2':<20}")
print("-" * 60)
print(f"{'Index':<20} {'BWT':<20} {'BWT+FM':<20}")
print(f"{'Memory':<20} {'Medium':<20} {'Low':<20}")
print(f"{'Speed':<20} {'Fast':<20} {'Fast':<20}")
print(f"{'Sensitivity':<20} {'High':<20} {'High':<20}")
print(f"{'Best for':<20} {'DNA':<20} {'ChIP/ATAC':<20}")
print(f"{'Local mode':<20} {'No':<20} {'Yes':<20}")
print(f"{'Split reads':<20} {'Yes':<20} {'No':<20}")
print("\n=== Best Practices ===")
print(""
1. Use --very-sensitive
2. Use --no-unal
3. Use --no-mixed
4. Use --no-discordant
5. Use local mode (ChIP)
6. Specify read group
7. Sort BAM
8. Index BAM
9. Check flagstat
10. Document
""")๐ฃ minimap2 (Long Reads)
๐ฏ The Long-Read Aligner
minimap2 is the standard aligner for long reads (PacBio, Nanopore) and is also excellent for RNA-Seq and assembly alignment.
minimap2
Minimizer: Fast. Long reads: Standard. RNA: Splicing. Assembly: Alignment. Fast: Very. Accurate: Good.
# minimap2
print("=== Overview ===")
print(""
minimap2:
- Minimizer-based
- Long reads
- Fast
- Versatile
Presets:
- map-pb: PacBio CLR
- map-hifi: PacBio HiFi
- map-ont: Nanopore
- map10k: Old Nanopore
- splice: RNA-Seq
- asm5, asm10, asm20: Assembly
Advantages:
- Very fast
- Low memory
- Accurate
- Versatile
""")
print("\n=== Build Index ===")
print(""
Command:
minimap2 -d reference.mmi reference.fa
Or align directly:
minimap2 -a reference.fa reads.fq > sample.sam
Note:
- Index optional
- Fast to build
- Can align directly
""")
print("\n=== Align Long Reads ===")
print(""
PacBio CLR:
minimap2 -ax map-pb reference.fa reads.fq > sample.sam
PacBio HiFi:
minimap2 -ax map-hifi reference.fa reads.fq > sample.sam
Nanopore:
minimap2 -ax map-ont reference.fa reads.fq > sample.sam
Options:
-a: SAM output
-x: Preset
-t: Threads
""")
print("\n=== Align RNA-Seq ===")
print(""
RNA-Seq:
minimap2 -ax splice reference.fa reads.fq > sample.sam
Options:
-ax splice: Spliced alignment
--junc-bed: Known junctions
--cs: CS tag
For Nanopore direct RNA:
minimap2 -ax splice -uf -k14 reference.fa reads.fq > sample.sam
""")
print("\n=== Align Assembly ===")
print(""
Assembly to reference:
minimap2 -ax asm5 reference.fa assembly.fa > sample.sam
Presets:
asm5: < 5% divergence
asm10: < 10% divergence
asm20: < 20% divergence
Options:
--cs: CS tag
-r: Chaining
""")
print("\n=== Common Options ===")
print(""
-a:
- SAM output
-x STR:
- Preset
-t INT:
- Threads
-k INT:
- K-mer size
- Default 15
-w INT:
- Minimizer window
- Default 10
-A INT:
- Match score
-B INT:
- Mismatch penalty
-O INT:
- Gap open
-E INT:
- Gap extension
--cs:
- CS tag
- Differences
--MD:
- MD tag
""")
print("\n=== Full Workflow ===")
print(""
# 1. Align
minimap2 -ax map-ont -t 8 \
reference.fa reads.fq | \
samtools sort -@ 8 -o sample.bam -
# 2. Index
samtools index sample.bam
# 3. QC
samtools flagstat sample.bam
# 4. Variant calling
bcftools mpileup -f reference.fa sample.bam | \
bcftools call -mv -Oz -o variants.vcf.gz
""")
print("\n=== Advantages ===")
print(""
minimap2:
โ Very fast
โ Low memory
โ Long reads
โ RNA splicing
โ Assembly
โ Versatile
โ Active development
โ Free
""")
print("\n=== Best Practices ===")
print(""
1. Use correct preset
2. Use -a for SAM
3. Set threads
4. Pipe to samtools
5. Sort BAM
6. Index BAM
7. Check flagstat
8. Use --cs for variants
9. Document
10. Cite
""")๐ SAM/BAM Format
๐ฏ The Alignment Format
SAM (Sequence Alignment/Map) is the standard text format for storing alignments. BAM is its binary, compressed version.
SAM/BAM
SAM: Text. BAM: Binary. CRAM: Compressed. Header: @. Alignment: 11+ fields. CIGAR: Alignment.
# SAM/BAM Format
print("=== SAM Header ===")
print(""
@HD: Header
VN: Version
SO: Sort order
@SQ: Sequence
SN: Name
LN: Length
@RG: Read group
ID: ID
SM: Sample
PL: Platform
LB: Library
PU: Unit
@PG: Program
ID: ID
PN: Name
VN: Version
CL: Command line
Example:
@HD VN:1.6 SO:coordinate
@SQ SN:chr1 LN:248956422
@RG ID:s1 SM:sample1 PL:ILLUMINA
@PG ID:bwa PN:bwa VN:0.7.17
""")
print("\n=== SAM Alignment ===")
print(""
Columns:
1. QNAME: Read name
2. FLAG: Bitwise flag
3. RNAME: Reference name
4. POS: Position (1-based)
5. MAPQ: Mapping quality
6. CIGAR: Alignment
7. RNEXT: Mate reference
8. PNEXT: Mate position
9. TLEN: Template length
10. SEQ: Sequence
11. QUAL: Quality
12+: Optional tags
Example:
read1 99 chr1 100 60 100M = 200 200 ACGT... IIII... NM:i:0
""")
print("\n=== FLAG ===")
print(""
FLAG:
- Bitwise
- 0x1: Paired
- 0x2: Proper pair
- 0x4: Unmapped
- 0x8: Mate unmapped
- 0x10: Reverse
- 0x20: Mate reverse
- 0x40: First in pair
- 0x80: Second in pair
- 0x100: Secondary
- 0x200: QC fail
- 0x400: Duplicate
- 0x800: Supplementary
Common:
99: Paired, proper, mate reverse, first
147: Paired, proper, reverse, second
0: Unpaired, forward
16: Unpaired, reverse
4: Unmapped
""")
print("\n=== CIGAR ===")
print(""
CIGAR operations:
M: Match/mismatch
I: Insertion
D: Deletion
N: Skipped (intron)
S: Soft clip
H: Hard clip
P: Padding
=: Match
X: Mismatch
Examples:
100M: 100 bp match
50M100N50M: 50M, 100N, 50M
10S90M: 10 bp soft clip, 90M
10H90M: 10 bp hard clip, 90M
50M2I48M: 50M, 2 bp insertion, 48M
50M2D48M: 50M, 2 bp deletion, 48M
""")
print("\n=== Optional Tags ===")
print(""
Tags:
NM: Edit distance
MD: Mismatch positions
AS: Alignment score
SA: Supplementary alignment
RG: Read group
NH: Number of hits
HI: Hit index
XS: Suboptimal score
XA: Alternative hits
Example:
NM:i:0 MD:Z:100 AS:i:100 RG:Z:s1
""")
print("\n=== BAM ===")
print(""
BAM:
- Binary SAM
- Compressed
- Indexed
- Standard
Advantages:
- Smaller
- Faster
- Random access
- Standard
Commands:
samtools view -b sample.sam > sample.bam
samtools sort sample.bam -o sample.sorted.bam
samtools index sample.sorted.bam
""")
print("\n=== CRAM ===")
print(""
CRAM:
- Compressed BAM
- Reference-based
- Smaller
Advantages:
- 30-60% smaller
- Reference-based
- EBI standard
Commands:
samtools view -C -T reference.fa sample.bam > sample.cram
samtools view -T reference.fa sample.cram
""")
print("\n=== samtools ===")
print(""
samtools:
- Standard tool
- View, sort, index
- Statistics
Commands:
samtools view sample.bam
samtools view -b sample.sam > sample.bam
samtools sort sample.bam -o sorted.bam
samtools index sorted.bam
samtools flagstat sorted.bam
samtools idxstats sorted.bam
samtools depth sorted.bam
samtools mpileup -f ref.fa sorted.bam
""")
print("\n=== Best Practices ===")
print(""
1. Use BAM not SAM
2. Sort BAM
3. Index BAM
4. Use CRAM for storage
5. Include read groups
6. Include @PG lines
7. Use samtools
8. Document
""")๐ง Post-Alignment Processing
๐ฏ Preparing BAM for Analysis
After alignment, BAM files need sorting, deduplication, and indexing before downstream analysis.
Post-Alignment
Sort: Coordinate. Dedup: Remove duplicates. Index: Random access. Filter: MAPQ. BQSR: GATK. QC: flagstat.
# Post-Alignment Processing
print("=== Sort BAM ===")
print(""
Command:
samtools sort -@ 8 -o sample.sorted.bam sample.bam
Options:
-@ 8: Threads
-o: Output
-m: Memory per thread
Why:
- Coordinate order
- Required by many tools
- Faster access
Output:
sample.sorted.bam
""")
print("\n=== Mark Duplicates ===")
print(""
Picard:
picard MarkDuplicates \
I=sample.sorted.bam \
O=sample.dedup.bam \
M=metrics.txt \
REMOVE_DUPLICATES=true
samtools:
samtools markdup -r sample.sorted.bam sample.dedup.bam
Why:
- PCR duplicates
- Optical duplicates
- Coverage bias
When:
- WGS: Yes
- WES: Yes
- RNA-Seq: No (or careful)
- ChIP-Seq: Yes
- ATAC-Seq: Yes (or no)
""")
print("\n=== Index BAM ===")
print(""
Command:
samtools index sample.dedup.bam
Output:
sample.dedup.bam.bai
Why:
- Random access
- IGV
- Variant calling
- Feature counting
Note:
- Required for most tools
- Fast
""")
print("\n=== Filter BAM ===")
print(""
By MAPQ:
samtools view -q 30 -b sample.bam > sample.filtered.bam
By flag:
samtools view -F 4 -b sample.bam > mapped.bam
samtools view -F 0x904 -b sample.bam > primary.bam
By region:
samtools view -b sample.bam chr1:1000-2000 > region.bam
Remove duplicates:
samtools view -F 0x400 -b sample.bam > nodup.bam
""")
print("\n=== BQSR (GATK) ===")
print(""
BQSR (Base Quality Score Recalibration):
- Corrects base quality
- Machine learning
- GATK
Steps:
1. BaseRecalibrator
2. ApplyBQSR
Commands:
gatk BaseRecalibrator \
-R reference.fa \
-I sample.dedup.bam \
--known-sites dbsnp.vcf \
-O recal.table
gatk ApplyBQSR \
-R reference.fa \
-I sample.dedup.bam \
--bqsr-recal-file recal.table \
-O sample.recal.bam
When:
- GATK best practices
- Variant calling
- Human
""")
print("\n=== QC ===")
print(""
samtools flagstat:
samtools flagstat sample.bam
Output:
- Total reads
- Mapped reads
- Properly paired
- Duplicates
- Singletons
samtools idxstats:
samtools idxstats sample.bam
Output:
- Chromosome
- Length
- Mapped
- Unmapped
samtools stats:
samtools stats sample.bam > stats.txt
""")
print("\n=== Full Workflow ===")
print(""
# 1. Align
bwa mem -t 8 -R '@RG\tID:s1\tSM:s1' \
reference.fa reads_1.fq.gz reads_2.fq.gz | \
samtools sort -@ 8 -o sample.sorted.bam -
# 2. Mark duplicates
picard MarkDuplicates \
I=sample.sorted.bam \
O=sample.dedup.bam \
M=metrics.txt
# 3. Index
samtools index sample.dedup.bam
# 4. QC
samtools flagstat sample.dedup.bam
# 5. (Optional) BQSR
gatk BaseRecalibrator ...
gatk ApplyBQSR ...
# 6. Downstream
gatk HaplotypeCaller ...
""")
print("\n=== Best Practices ===")
print(""
1. Always sort BAM
2. Mark duplicates
3. Index BAM
4. Check flagstat
5. Filter MAPQ
6. BQSR for GATK
7. Keep intermediate files
8. Document
""")๐ Alignment QC
๐ฏ Is My Alignment Good?
Alignment quality control ensures that reads are mapped correctly and coverage is adequate for downstream analysis.
QC
Flagstat: Summary. Idxstats: Per chrom. Stats: Detailed. Coverage: Depth. Insert: Size. MapQ: Quality.
# Alignment QC
print("=== samtools flagstat ===")
print(""
Command:
samtools flagstat sample.bam
Output:
- Total reads
- Mapped (%)
- Properly paired (%)
- Duplicates (%)
- Singletons (%)
- Read1, Read2
Example:
1000000 + 0 in total
980000 + 0 mapped (98.0%)
950000 + 0 properly paired (95.0%)
50000 + 0 duplicates (5.0%)
""")
print("\n=== samtools idxstats ===")
print(""
Command:
samtools idxstats sample.bam
Output:
Chromosome, Length, Mapped, Unmapped
Example:
chr1 248956422 3000000 0
chr2 242193529 2800000 0
* 0 20000 0
""")
print("\n=== samtools stats ===")
print(""
Command:
samtools stats sample.bam > stats.txt
Output:
- Summary numbers
- Coverage
- Insert size
- GC content
- Quality
Plot:
plot-bamstats -p plots/ stats.txt
""")
print("\n=== Coverage ===")
print(""
samtools depth:
samtools depth sample.bam > depth.txt
Coverage:
- Mean depth
- Breadth
- Uniformity
Tools:
- samtools depth
- bedtools genomecov
- mosdepth
- GATK DepthOfCoverage
Example:
samtools depth -a sample.bam | \
awk '{sum+=$3} END {print sum/NR}'
""")
print("\n=== Insert Size ===")
print(""
Insert size:
- Fragment length
- Paired-end
- Expected distribution
Check:
- Picard CollectInsertSizeMetrics
- samtools stats
Expected:
- Library-dependent
- 300-500 bp typical
- Tight distribution
""")
print("\n=== MAPQ Distribution ===")
print(""
MAPQ:
- Mapping quality
- Distribution
Command:
samtools view sample.bam | \
awk '{print $5}' | sort -n | uniq -c
Expected:
- Peak at 60 (unique)
- Small at 0 (multi)
""")
print("\n=== Contamination ===")
print(""
Contamination:
- Foreign DNA
- Cross-species
- Microbial
Check:
- FastQ Screen
- Kraken2
- Conpair
- VerifyBamID
Common:
- Mycoplasma
- E. coli
- Human (in non-human)
""")
print("\n=== MultiQC ===")
print(""
MultiQC:
- Aggregate all QC
- One report
- Compare samples
Command:
multiqc . -o multiqc_report/
Includes:
- FastQC
- samtools
- Picard
- STAR
- etc.
""")
print("\n=== Best Practices ===")
print(""
1. Check flagstat
2. Check coverage
3. Check insert size
4. Check MAPQ
5. Check contamination
6. Use MultiQC
7. Compare samples
8. Document
""")๐ Python Integration
๐ฏ Alignment from Python
You can run aligners from Python using subprocess and parse BAM files with pysam.
Python
subprocess: Run CLI. pysam: BAM. Biopython: SAM. pandas: Stats. matplotlib: Plot. Pipeline: Automate.
# Python Integration
print("=== Run BWA ===")
print(""
import subprocess
def run_bwa(reference, reads_1, reads_2, output_bam,
threads=8, read_group=None):
\"\"\"Run BWA-MEM alignment.\"\"\"
cmd = f"bwa mem -t {threads}"
if read_group:
cmd += f" -R '{read_group}'"
cmd += f" {reference} {reads_1} {reads_2} | "
cmd += f"samtools sort -@ {threads} -o {output_bam} -"
result = subprocess.run(cmd, shell=True, capture_output=True,
text=True)
if result.returncode != 0:
raise RuntimeError(f"BWA failed: {result.stderr}")
# Index
subprocess.run(['samtools', 'index', output_bam], check=True)
return output_bam
# Use
# bam = run_bwa("ref.fa", "r1.fq", "r2.fq", "sample.bam")
""")
print("\n=== Run Bowtie2 ===")
print(""
def run_bowtie2(index, reads_1, reads_2, output_bam,
threads=8):
\"\"\"Run Bowtie2 alignment.\"\"\"
cmd = (
f"bowtie2 -p {threads} -x {index} "
f"-1 {reads_1} -2 {reads_2} "
f"--very-sensitive --no-unal | "
f"samtools sort -@ {threads} -o {output_bam} -"
)
result = subprocess.run(cmd, shell=True, capture_output=True,
text=True)
if result.returncode != 0:
raise RuntimeError(f"Bowtie2 failed: {result.stderr}")
subprocess.run(['samtools', 'index', output_bam], check=True)
return output_bam
# Use
# bam = run_bowtie2("index_prefix", "r1.fq", "r2.fq", "sample.bam")
""")
print("\n=== Run minimap2 ===")
print(""
def run_minimap2(reference, reads, output_bam,
preset="map-ont", threads=8):
\"\"\"Run minimap2 alignment.\"\"\"
cmd = (
f"minimap2 -ax {preset} -t {threads} "
f"{reference} {reads} | "
f"samtools sort -@ {threads} -o {output_bam} -"
)
result = subprocess.run(cmd, shell=True, capture_output=True,
text=True)
if result.returncode != 0:
raise RuntimeError(f"minimap2 failed: {result.stderr}")
subprocess.run(['samtools', 'index', output_bam], check=True)
return output_bam
# Use
# bam = run_minimap2("ref.fa", "reads.fq", "sample.bam")
""")
print("\n=== Parse BAM with pysam ===")
print(""
import pysam
def parse_bam(bam_file):
\"\"\"Parse BAM file.\"\"\"
bam = pysam.AlignmentFile(bam_file, "rb")
# Header
print(f"References: {bam.references[:3]}")
print(f"Read groups: {list(bam.header.get('RG', []))}")
# Iterate
count = 0
for read in bam:
print(f"Read: {read.query_name}")
print(f" Flag: {read.flag}")
print(f" Ref: {read.reference_name}:{read.reference_start}")
print(f" MapQ: {read.mapping_quality}")
print(f" CIGAR: {read.cigarstring}")
count += 1
if count >= 5:
break
bam.close()
# Use
# parse_bam("sample.bam")
""")
print("\n=== Filter BAM ===")
print(""
def filter_bam(input_bam, output_bam, min_mapq=30):
\"\"\"Filter BAM by MAPQ.\"\"\"
with pysam.AlignmentFile(input_bam, "rb") as in_bam:
with pysam.AlignmentFile(output_bam, "wb",
header=in_bam.header) as out_bam:
for read in in_bam:
if read.mapping_quality >= min_mapq:
out_bam.write(read)
# Index
pysam.index(output_bam)
return output_bam
# Use
# filtered = filter_bam("sample.bam", "filtered.bam", min_mapq=30)
""")
print("\n=== BAM Statistics ===")
print(""
def bam_stats(bam_file):
\"\"\"Compute BAM statistics.\"\"\"
bam = pysam.AlignmentFile(bam_file, "rb")
total = 0
mapped = 0
properly_paired = 0
duplicates = 0
for read in bam:
total += 1
if not read.is_unmapped:
mapped += 1
if read.is_proper_pair:
properly_paired += 1
if read.is_duplicate:
duplicates += 1
print(f"Total: {total}")
print(f"Mapped: {mapped} ({mapped/total*100:.1f}%)")
print(f"Properly paired: {properly_paired} "
f"({properly_paired/total*100:.1f}%)")
print(f"Duplicates: {duplicates} ({duplicates/total*100:.1f}%)")
bam.close()
# Use
# bam_stats("sample.bam")
""")
print("\n=== Coverage ===")
print(""
def compute_coverage(bam_file, region=None):
\"\"\"Compute coverage.\"\"\"
bam = pysam.AlignmentFile(bam_file, "rb")
if region:
coverage = bam.count_coverage(region)
else:
coverage = bam.count_coverage("chr1")
# Sum across bases
total = [sum(c) for c in zip(*coverage)]
mean_cov = sum(total) / len(total)
print(f"Mean coverage: {mean_cov:.1f}")
bam.close()
return mean_cov
# Use
# cov = compute_coverage("sample.bam")
""")
print("\n=== Complete Pipeline ===")
print(""
import subprocess
import os
def alignment_pipeline(reference, reads_1, reads_2,
output_dir="align_output", threads=8):
\"\"\"Complete alignment pipeline.\"\"\"
os.makedirs(output_dir, exist_ok=True)
# 1. Trim
print("Trimming...")
subprocess.run([
'fastp', '-i', reads_1, '-I', reads_2,
'-o', f'{output_dir}/clean_1.fq.gz',
'-O', f'{output_dir}/clean_2.fq.gz',
'--detect_adapter_for_pe'
], check=True)
# 2. Align
print("Aligning...")
bam = run_bwa(
reference,
f'{output_dir}/clean_1.fq.gz',
f'{output_dir}/clean_2.fq.gz',
f'{output_dir}/sample.sorted.bam',
threads,
read_group="@RG\\tID:s1\\tSM:sample1\\tPL:ILLUMINA"
)
# 3. Mark duplicates
print("Marking duplicates...")
subprocess.run([
'picard', 'MarkDuplicates',
f'I={bam}',
f'O={output_dir}/sample.dedup.bam',
f'M={output_dir}/metrics.txt'
], check=True)
# 4. Index
subprocess.run(['samtools', 'index',
f'{output_dir}/sample.dedup.bam'], check=True)
# 5. QC
print("QC...")
subprocess.run(['samtools', 'flagstat',
f'{output_dir}/sample.dedup.bam'],
stdout=open(f'{output_dir}/flagstat.txt', 'w'),
check=True)
print("Pipeline complete!")
return f'{output_dir}/sample.dedup.bam'
# Use
# bam = alignment_pipeline("ref.fa", "r1.fq", "r2.fq")
""")โ๏ธ Tool Comparison
๐ฏ Which Aligner?
Choosing the right aligner depends on read type, application, and computational resources.
Comparison
BWA: DNA. BWA-MEM2: Faster. Bowtie2: ChIP. minimap2: Long. STAR: RNA. HISAT2: RNA.
| Tool | Read Type | Speed | Memory | Best For |
|---|---|---|---|---|
| BWA-MEM | Short | Fast | Medium | DNA (standard) |
| BWA-MEM2 | Short | Very fast | High | DNA (faster) |
| Bowtie2 | Short | Fast | Low | ChIP/ATAC |
| minimap2 | Long/Short | Very fast | Low | Long reads |
| STAR | Short (RNA) | Fast | High | RNA-Seq |
| HISAT2 | Short (RNA) | Fast | Low | RNA-Seq |
| Salmon | Short (RNA) | Very fast | Low | RNA quantification |
| GraphAligner | Long | Fast | Medium | Graph genomes |
# Decision Guide
print("=== Choose Aligner by Data Type ===")
print(""
Short reads (DNA):
โ BWA-MEM (standard)
โ BWA-MEM2 (faster)
Short reads (ChIP/ATAC):
โ Bowtie2 (--very-sensitive)
Short reads (RNA):
โ STAR (accurate)
โ HISAT2 (memory)
Long reads:
โ minimap2
โ NGMLR
RNA quantification:
โ Salmon (alignment-free)
โ kallisto
Assembly:
โ minimap2
โ nucmer
""")
print("\n=== Speed Comparison ===")
print(f"{'Tool':<20} {'10M reads':<15} {'50M reads':<15}")
print("-" * 50)
print(f"{'BWA-MEM':<20} {'5 min':<15} {'25 min':<15}")
print(f"{'BWA-MEM2':<20} {'2 min':<15} {'10 min':<15}")
print(f"{'Bowtie2':<20} {'5 min':<15} {'25 min':<15}")
print(f"{'minimap2':<20} {'1 min':<15} {'5 min':<15}")
print(f"{'STAR':<20} {'3 min':<15} {'15 min':<15}")
print(f"{'HISAT2':<20} {'5 min':<15} {'25 min':<15}")
print("\n=== Memory Comparison ===")
print(f"{'Tool':<20} {'Human index':<15} {'Runtime RAM':<15}")
print("-" * 50)
print(f"{'BWA-MEM':<20} {'5 GB':<15} {'4 GB':<15}")
print(f"{'BWA-MEM2':<20} {'10 GB':<15} {'8 GB':<15}")
print(f"{'Bowtie2':<20} {'4 GB':<15} {'2 GB':<15}")
print(f"{'minimap2':<20} {'6 GB':<15} {'4 GB':<15}")
print(f"{'STAR':<20} {'30 GB':<15} {'30 GB':<15}")
print(f"{'HISAT2':<20} {'5 GB':<15} {'4 GB':<15}")
print("\n=== When to Use Which ===")
print(""
USE BWA-MEM:
โ DNA standard
โ WGS
โ WES
โ SV
โ GATK
USE BWA-MEM2:
โ Faster BWA
โ Same output
USE BOWTIE2:
โ ChIP-Seq
โ ATAC-Seq
โ Low memory
โ Local alignment
USE MINIMAP2:
โ Long reads
โ RNA
โ Assembly
โ Versatile
USE STAR:
โ RNA-Seq
โ Splicing
โ Accurate
USE HISAT2:
โ RNA-Seq
โ Low memory
""")๐ผ Use Cases
๐ฏ Where Alignment Is Used
Read alignment is used in virtually every area of NGS analysis: variant calling, RNA-Seq, ChIP-Seq, and more.
Use Cases
WGS: Variants. WES: Coding. RNA: Expression. ChIP: Peaks. ATAC: Chromatin. SV: Structure. Clinical: Diagnostics.
# Use Cases
print("=== 1. Whole Genome Sequencing ===")
print(""
WGS:
- Align to reference
- Call variants
- Structural variants
Workflow:
1. BWA-MEM
2. Sort BAM
3. Mark duplicates
4. BQSR
5. GATK HaplotypeCaller
Tools:
- BWA
- samtools
- Picard
- GATK
""")
print("\n=== 2. Whole Exome Sequencing ===")
print(""
WES:
- Align to reference
- Call variants
- Coding regions
Workflow:
1. BWA-MEM
2. Sort BAM
3. Mark duplicates
4. BQSR
5. GATK
Coverage:
- 50-100x
Tools:
- BWA
- GATK
- VEP
""")
print("\n=== 3. RNA-Seq ===")
print(""
RNA-Seq:
- Align to genome
- Splicing-aware
- Quantify
Workflow:
1. STAR or HISAT2
2. Sort BAM
3. featureCounts
4. DESeq2
Tools:
- STAR
- HISAT2
- featureCounts
- DESeq2
""")
print("\n=== 4. ChIP-Seq ===")
print(""
ChIP-Seq:
- Align to genome
- Peak calling
- Motif
Workflow:
1. Bowtie2
2. Sort BAM
3. Filter MAPQ
4. MACS2
5. HOMER
Tools:
- Bowtie2
- MACS2
- HOMER
""")
print("\n=== 5. ATAC-Seq ===")
print(""
ATAC-Seq:
- Align to genome
- Open chromatin
- Peaks
Workflow:
1. Bowtie2
2. Sort BAM
3. Filter (MAPQ, mito)
4. MACS2
5. HOMER
Tools:
- Bowtie2
- MACS2
- HOMER
""")
print("\n=== 6. Structural Variants ===")
print(""
SV:
- Align to reference
- Discordant pairs
- Split reads
Workflow:
1. BWA-MEM (or minimap2)
2. Sort BAM
3. Delly / Manta
4. Filter
Tools:
- BWA
- minimap2
- Delly
- Manta
""")
print("\n=== 7. Metagenomics ===")
print(""
Metagenomics:
- Align to reference
- Taxonomic
- Functional
Workflow:
1. Bowtie2
2. Filter host
3. Taxonomic (Kraken2)
4. Assembly
Tools:
- Bowtie2
- Kraken2
- MetaPhlAn
""")
print("\n=== 8. Clinical ===")
print(""
Clinical:
- Align patient
- Call variants
- Report
Workflow:
1. BWA-MEM
2. GATK
3. VEP
4. ClinVar
5. Report
Tools:
- BWA
- GATK
- VEP
- ClinVar
""")
print("\n=== Comparison ===")
print(f"{'Use Case':<25} {'Aligner':<20} {'Downstream':<20}")
print("-" * 65)
print(f"{'WGS':<25} {'BWA-MEM':<20} {'GATK':<20}")
print(f"{'WES':<25} {'BWA-MEM':<20} {'GATK':<20}")
print(f"{'RNA-Seq':<25} {'STAR':<20} {'DESeq2':<20}")
print(f"{'ChIP-Seq':<25} {'Bowtie2':<20} {'MACS2':<20}")
print(f"{'ATAC-Seq':<25} {'Bowtie2':<20} {'MACS2':<20}")
print(f"{'SV':<25} {'minimap2':<20} {'Delly':<20}")
print(f"{'Metagenome':<25} {'Bowtie2':<20} {'Kraken2':<20}")
print(f"{'Clinical':<25} {'BWA-MEM':<20} {'VEP':<20}")โ FAQ
๐ฏ Frequently Asked Questions
Answers to common questions about read alignment.
What is read alignment?
Read alignment (also called read mapping) is the process of finding the position in a reference genome where each sequencing read most likely originated. It's a fundamental step in NGS analysis, enabling variant calling, RNA-Seq quantification, ChIP-Seq peak calling, and many other applications.
What is the difference between BWA and Bowtie2?
BWA-MEM is the standard for DNA alignment, using a Burrows-Wheeler transform and seed-and-extend strategy; it's fast, accurate, and handles longer reads. Bowtie2 is also BWT-based, with excellent memory efficiency and sensitivity, and is often used for ChIP-Seq and ATAC-Seq. Both are excellent; choose based on your application and preference.
What is MAPQ and how do I interpret it?
MAPQ (Mapping Quality) is a Phred-scaled probability that a read is aligned incorrectly. MAPQ 0 means multi-mapping (ambiguous); MAPQ 60 (BWA) means uniquely mapped with high confidence. Common filtering: MAPQ >= 20-30 for most analyses, higher for variant calling. MAPQ is critical for filtering ambiguous alignments.
What is a CIGAR string in SAM format?
CIGAR (Compact Idiosyncratic Gapped Alignment Report) describes how a read aligns to the reference. Operations: M (match/mismatch), I (insertion), D (deletion), N (skipped region, e.g. intron), S (soft clip), H (hard clip), P (padding), = (match), X (mismatch). Example: 100M = 100 bp match; 50M100N50M = 50 bp match, 100 bp intron, 50 bp match.
Why do I need to sort and index BAM files?
Sorting arranges alignments by genomic coordinate, which is required by most downstream tools (variant calling, visualization in IGV, peak calling). Indexing creates a .bai file that enables fast random access to specific genomic regions without reading the entire file. Both are standard steps after alignment.
Should I mark duplicates before or after sorting?
Mark duplicates after sorting. Picard MarkDuplicates and samtools markdup require coordinate-sorted BAM. Duplicates are reads with identical start/end positions, typically PCR artifacts. Remove them for WGS, WES, ChIP-Seq; be cautious for RNA-Seq and amplicon sequencing (duplicates may be real).
What is BQSR and do I need it?
BQSR (Base Quality Score Recalibration) is a GATK step that corrects systematic errors in base quality scores using machine learning. It's part of the GATK Best Practices for germline variant calling. It requires known variant sites (e.g., dbSNP) and improves variant calling accuracy. Recommended for human WGS/WES.
What is the difference between soft and hard clipping?
Soft clipping (S) removes bases from the alignment but keeps them in the sequence; the bases are still available for downstream analysis. Hard clipping (H) removes bases from both the alignment and the sequence. Soft clipping is used for partial alignments (e.g., reads spanning breakpoints); hard clipping is used for adapter sequences.
Which aligner should I use for long reads?
Use minimap2 for long reads (PacBio, Nanopore). It's fast, memory-efficient, and versatile. Presets: -ax map-pb (PacBio CLR), -ax map-hifi (PacBio HiFi), -ax map-ont (Nanopore), -ax splice (RNA-Seq). For assembly alignment, use -ax asm5/asm10/asm20.
How do I know if my alignment is good?
Check: (1) Mapping rate > 90-95% (samtools flagstat), (2) Properly paired > 90%, (3) Duplication < 20%, (4) Coverage uniform and sufficient (e.g., 30x for WGS), (5) Insert size distribution tight, (6) MAPQ mostly high, (7) No contamination. Use MultiQC to aggregate.
# Quick Reference
print("=== Quick Answers ===")
print(""
Q: What is alignment?
A: Mapping reads to reference.
Q: Best DNA aligner?
A: BWA-MEM.
Q: Best ChIP aligner?
A: Bowtie2.
Q: Best long-read aligner?
A: minimap2.
Q: Best RNA aligner?
A: STAR, HISAT2.
Q: MAPQ?
A: Mapping quality (0-60).
Q: CIGAR?
A: Alignment operations.
Q: Sort and index?
A: Yes โ required downstream.
Q: BQSR?
A: GATK base recalibration.
Q: Mark duplicates?
A: Yes โ for most DNA.
Q: Free tools?
A: Yes โ BWA, Bowtie2, minimap2, samtools.
""")๐ Future
๐ฏ Where Alignment Is Going
The future is graph genomes, long-read first, AI-augmented, real-time, and cloud-native.
Future Directions
Graph: Pangenome. Long-read: First. AI: Learned. Real-time: Streaming. Cloud: Serverless. GPU: Accelerated.
# Future of Read Alignment
print("=== 1. Graph Genomes ===")
print(""
Trend: Pangenome
Current:
- Linear reference
- Bias
Future:
- Graph reference
- Multiple genomes
- Less bias
Tools:
- vg
- GraphAligner
- minigraph
- PanSN
Future:
- Graph-first
- Pangenome
- Clinical
""")
print("\n=== 2. Long-Read First ===")
print(""
Trend: Long reads
Current:
- Short reads standard
- Long reads specialized
Future:
- Long reads routine
- Complete genomes
- SVs
- Isoforms
Tools:
- minimap2
- GraphAligner
- winnowmap
Future:
- Long-read first
- Real-time
- Clinical
""")
print("\n=== 3. AI-Augmented Alignment ===")
print(""
Trend: Deep learning
Ideas:
- Learned aligners
- Neural networks
- Base calling
- Variant calling
Examples:
- DeepVariant
- Bonito (Nanopore)
- DeepConsensus (PacBio)
Future:
- End-to-end
- Learned
- Clinical
""")
print("\n=== 4. Real-Time ===")
print(""
Trend: Real-time
Current:
- Batch
- Slow
Future:
- Real-time
- Streaming
- Adaptive
- Portable
Examples:
- Nanopore
- Real-time surveillance
- Clinical
Tools:
- minimap2
- EPI2ME
- Nextstrain
""")
print("\n=== 5. Cloud-Native ===")
print(""
Trend: Cloud
Current:
- On-prem
- Manual
Future:
- Serverless
- Auto-scaling
- GPU
- Collaborative
Examples:
- AWS HealthOmics
- Terra
- Galaxy
- Nextflow
Future:
- No install
- Global
- Fast
""")
print("\n=== 6. GPU Acceleration ===")
print(""
Trend: GPU
Current:
- CPU
- Slow
Future:
- GPU
- 10-100x faster
- Deep learning
Tools:
- NVIDIA Parabricks
- BWA-MEM (GPU)
- minimap2 (GPU)
Future:
- Routine
- Clinical
- Real-time
""")
# TIMELINE
print("\n=== Timeline ===")
print(""
Past:
1990s: BLAST
2001: BLAT
2008: Bowtie
2009: BWA
2012: BWA-MEM
2013: Bowtie2
2018: minimap2
2020: BWA-MEM2
2022: Graph genomes
Present:
2025: Long-read first
2026: Pangenome
Future:
2030: Graph-first?
2035: AI alignment?
2040: Real-time clinical?
""")
print("\n=== Final Thoughts ===")
print(""
Alignment is:
- Foundational
- Evolving
- Critical
- Everywhere
The future is:
- Graph genomes
- Long-read first
- AI-augmented
- Real-time
- Cloud-native
- GPU-accelerated
"Alignment is where reads
find their home in the genome."
Keep exploring. Keep building.
""")