๐ง Metagenomics Fundamentals
๐ฏ What is Metagenomics?
Metagenomics is the study of genetic material recovered directly from environmental samples, bypassing the need for isolation and lab cultivation of individual species. It enables the study of entire microbial communities, including unculturable organisms. Binning is the process of grouping assembled metagenomic contigs into draft genomes, called metagenome-assembled genomes (MAGs).
Why Metagenomics Matters
Unculturable: Access hidden diversity. Community: Whole microbiome. Function: What microbes do. Ecology: Interactions. Health: Human microbiome. Biotech: Novel enzymes.
# Metagenomics & Binning โ the big picture
# Studying microbial communities directly from environmental samples.
print("""
=== Metagenomics & Binning ===
What it is:
- Study genetic material from environment
- No culturing needed
- Whole microbial communities
- Binning: group contigs into MAGs
Why it matters:
- Access unculturable microbes
- Understand community structure
- Functional potential
- Ecology and evolution
- Human microbiome
- Biotechnology
Key concepts:
- Shotgun metagenomics
- Amplicon sequencing (16S/18S/ITS)
- Taxonomic profiling
- Assembly
- Binning
- MAGs
- Functional annotation
Applications:
- Human microbiome
- Environmental microbiology
- Biotechnology
- Agriculture
- Bioremediation
- Infectious disease
""")
print("=== Historical Context ===")
print("""
1998: First metagenomic library (Handelsman)
2004: Sargasso Sea (Venter)
2006: Human Microbiome Project
2010: Metagenomic assembly
2014: Binning (MetaBAT, MaxBin, CONCOCT)
2017: MAGs (Parks et al.)
2020: Large-scale metagenomics
2025: AI, single-cell, pangenomes
""")
print("=== Why It's Hard ===")
print("""
1. COMPLEXITY
- Thousands of species
- Uneven abundance
- Strain variation
2. ASSEMBLY
- Mixed reads
- Coverage gaps
- Repeats
3. BINNING
- Similar species
- Strain-level
- Incomplete
4. FUNCTION
- Unknown genes
- Annotation
- Pathways
5. SCALE
- Terabytes of data
- Computational
- Time
""")๐ฏ Why Metagenomics Matters
๐ฏ The Value of Metagenomics
Metagenomics has revolutionized microbiology by allowing direct study of microbial communities without culturing, revealing the vast majority of microbial life that was previously inaccessible.
Key Benefits
Unculturable: 99% of microbes. Community: Whole microbiome. Function: Metabolic potential. Novel: New enzymes. Health: Microbiome. Ecology: Interactions.
# Why Metagenomics Matters
print("=== 1. Access Unculturable Microbes ===")
print("""
Unculturable:
- ~99% of microbes
- Not grown in lab
- Hidden diversity
Example:
Soil, ocean, gut
โ Metagenomics
โ Novel lineages
""")
print("\n=== 2. Community Structure ===")
print("""
Community:
- Who is there?
- Relative abundance
- Diversity
Example:
Gut microbiome
โ Bacteria, archaea, fungi, viruses
โ Composition
""")
print("\n=== 3. Functional Potential ===")
print("""
Function:
- What can they do?
- Metabolic pathways
- Enzymes
Example:
Metagenome
โ Genes
โ Pathways
โ Functions
""")
print("\n=== 4. Novel Enzymes ===")
print("""
Novel:
- Bioprospecting
- Industrial enzymes
- Drugs
Example:
Metagenome
โ Novel enzyme
โ Biotech
""")
print("\n=== 5. Human Microbiome ===")
print("""
Health:
- Gut, skin, oral
- Disease
- Therapeutics
Example:
Gut microbiome
โ Obesity, IBD, cancer
โ Therapeutics
""")
print("\n=== 6. Ecology ===")
print("""
Ecology:
- Interactions
- Biogeochemistry
- Evolution
Example:
Ocean
โ Carbon cycle
โ Climate
""")
print("\n=== 7. Agriculture ===")
print("""
Agriculture:
- Soil health
- Plant growth
- Pathogens
Example:
Soil microbiome
โ Crop yield
โ Sustainable
""")
print("\n=== 8. Bioremediation ===")
print("""
Bioremediation:
- Pollution
- Degradation
- Cleanup
Example:
Oil spill
โ Oil-degrading microbes
โ Bioremediation
""")
print("\n=== 9. Infectious Disease ===")
print("""
Infectious disease:
- Pathogen detection
- Outbreaks
- Diagnostics
Example:
Clinical sample
โ Metagenomics
โ Pathogen
""")
print("\n=== 10. Biotechnology ===")
print("""
Biotech:
- Enzymes
- Pathways
- Products
Example:
Metagenome
โ Novel genes
โ Industry
""")โ๏ธ Metagenomics Workflow
๐ฏ The Metagenomics Pipeline
A typical metagenomics workflow goes from sample collection to biological interpretation, involving sequencing, QC, assembly, binning, and annotation.
Workflow
Sample: Collect. Sequence: Shotgun. QC: Trim. Assembly: Contigs. Binning: MAGs. Annotate: Function.
# Metagenomics Workflow
print("=== Overview ===")
print("""
Step 1: Sample collection
Step 2: DNA extraction
Step 3: Library preparation
Step 4: Sequencing (shotgun)
Step 5: Quality control
Step 6: Taxonomic profiling
Step 7: Assembly
Step 8: Binning
Step 9: MAG quality
Step 10: Functional annotation
Step 11: Analysis & interpretation
""")
print("\n=== Step 1: Sample Collection ===")
print("""
Sample:
- Environment (soil, water, gut)
- Clinical
- Industrial
- Storage
- Metadata
Example:
Gut swab
โ DNA extraction
""")
print("\n=== Step 2: DNA Extraction ===")
print("""
Extraction:
- Lysis
- Purification
- Quality
- Quantity
Methods:
- Phenol-chloroform
- Column
- Magnetic beads
""")
print("\n=== Step 3: Library Prep ===")
print("""
Library:
- Fragmentation
- Adapters
- Size selection
- Amplification
Kits:
- Illumina
- Nextera
- TruSeq
""")
print("\n=== Step 4: Sequencing ===")
print("""
Sequencing:
- Shotgun
- Illumina (short)
- PacBio (long)
- Nanopore (long)
Platforms:
- NovaSeq
- HiSeq
- MiSeq
- PacBio
- Nanopore
""")
print("\n=== Step 5: QC ===")
print("""
QC:
- Adapter trimming
- Quality filtering
- Length filtering
- Contamination
Tools:
- FastQC
- Trimmomatic
- fastp
- BBDuk
""")
print("\n=== Step 6: Taxonomic Profiling ===")
print("""
Taxonomy:
- Who is there?
- Relative abundance
Tools:
- Kraken2
- MetaPhlAn
- Centrifuge
- mOTUs
""")
print("\n=== Step 7: Assembly ===")
print("""
Assembly:
- Reads โ contigs
- De Bruijn graph
- Multiple samples
Tools:
- MEGAHIT
- SPAdes
- IDBA-UD
- metaSPAdes
""")
print("\n=== Step 8: Binning ===")
print("""
Binning:
- Contigs โ MAGs
- Composition + coverage
- Multiple tools
Tools:
- MetaBAT2
- MaxBin2
- CONCOCT
- DAS Tool
""")
print("\n=== Step 9: MAG Quality ===")
print("""
Quality:
- Completeness
- Contamination
- Strain heterogeneity
Tools:
- CheckM
- CheckM2
- GTDB-Tk
- BUSCO
""")
print("\n=== Step 10: Functional Annotation ===")
print("""
Annotation:
- Genes
- Pathways
- Functions
Tools:
- Prokka
- Prodigal
- KEGG
- eggNOG
- CAZy
""")
print("\n=== Step 11: Analysis ===")
print("""
Analysis:
- Comparative
- Statistics
- Visualization
- Interpretation
Tools:
- R
- Python
- QIIME2
- phyloseq
""")๐ฌ Sequencing Approaches
๐ฏ How to Sequence a Metagenome
There are two main approaches: amplicon sequencing (targeted, e.g., 16S rRNA) and shotgun metagenomics (whole community). Each has advantages.
Approaches
Amplicon: Marker genes. Shotgun: All DNA. 16S: Bacteria/archaea. ITS: Fungi. 18S: Eukaryotes. Long reads: Nanopore/PacBio.
# Sequencing Approaches
print("=== 1. Amplicon Sequencing ===")
print("""
Amplicon:
- Marker gene
- 16S rRNA (bacteria)
- 18S rRNA (eukaryotes)
- ITS (fungi)
Pros:
- Cheap
- Deep
- Focused
Cons:
- Only marker
- No functional
- PCR bias
Example:
16S V3-V4
โ Taxonomy
""")
print("\n=== 2. Shotgun Metagenomics ===")
print("""
Shotgun:
- All DNA
- Whole community
- Functional potential
Pros:
- Functional
- No PCR bias
- MAGs
Cons:
- Expensive
- Complex
- Assembly
Example:
Whole metagenome
โ Assembly
โ MAGs
""")
print("\n=== 3. 16S rRNA ===")
print("""
16S:
- Bacteria/archaea
- Conserved + variable
- Phylogenetic marker
Regions:
- V1-V9
- V3-V4 common
- V4 common
Databases:
- SILVA
- Greengenes
- RDP
""")
print("\n=== 4. ITS ===")
print("""
ITS:
- Fungi
- Internal transcribed spacer
- ITS1, ITS2
Databases:
- UNITE
- ITSoneDB
""")
print("\n=== 5. 18S ===")
print("""
18S:
- Eukaryotes
- rRNA
- Phylogenetic
Databases:
- SILVA
- PR2
""")
print("\n=== 6. Long Reads ===")
print("""
Long reads:
- Nanopore
- PacBio
- 10-100 kb
- Better assembly
Pros:
- Long
- Better MAGs
- Repeat resolution
Cons:
- Error rate
- Cost
- DNA quality
""")
print("\n=== 7. Comparison ===")
print(f"{'Approach':<20} {'Target':<20} {'Cost':<15} {'Info':<20}")
print("-" * 75)
print(f"{'16S':<20} {'Bacteria':<20} {'Low':<15} {'Taxonomy':<20}")
print(f"{'ITS':<20} {'Fungi':<20} {'Low':<15} {'Taxonomy':<20}")
print(f"{'18S':<20} {'Eukaryotes':<20} {'Low':<15} {'Taxonomy':<20}")
print(f"{'Shotgun':<20} {'All DNA':<20} {'High':<15} {'Taxonomy+Function':<20}")
print(f"{'Long reads':<20} {'All DNA':<20} {'High':<15} {'Better MAGs':<20}")
print("\n=== 8. Choosing ===")
print("""
Choose:
- 16S: quick, cheap, taxonomy
- Shotgun: function, MAGs
- Long reads: better assembly
- Hybrid: best of both
""")
print("\n=== 9. Platforms ===")
print("""
Platforms:
- Illumina (short)
- PacBio (long)
- Nanopore (long)
- MGI (short)
Read length:
- Illumina: 150-300 bp
- PacBio: 10-20 kb
- Nanopore: 10-100 kb
""")
print("\n=== 10. Use Cases ===")
print("""
Use cases:
- Human microbiome: 16S + shotgun
- Environmental: shotgun
- Clinical: shotgun
- Food: 16S
- Fungal: ITS
""")๐งน Quality Control
๐ฏ Cleaning Raw Reads
Quality control (QC) removes adapters, low-quality bases, and contaminants before assembly. It's essential for good results.
QC
Adapters: Trim. Quality: Filter. Length: Filter. Contamination: Remove. Host: Deplete. Duplicates: Remove.
# Quality Control
print("=== 1. FastQC ===")
print("""
FastQC:
- Quality report
- Per base quality
- GC content
- Adapters
- Duplicates
Command:
fastqc reads.fq
""")
print("\n=== 2. Trimmomatic ===")
print("""
Trimmomatic:
- Adapter trimming
- Quality filtering
- Length filtering
Command:
trimmomatic PE reads_1.fq reads_2.fq \\
out_1.fq out_2.fq \\
ILLUMINACLIP:adapters.fa:2:30:10 \\
LEADING:3 TRAILING:3 \\
SLIDINGWINDOW:4:15 MINLEN:36
""")
print("\n=== 3. fastp ===")
print("""
fastp:
- Fast
- Adapter
- Quality
- Reports
Command:
fastp -i reads_1.fq -I reads_2.fq \\
-o out_1.fq -O out_2.fq \\
--detect_adapter_for_pe
""")
print("\n=== 4. BBDuk ===")
print("""
BBDuk:
- Adapter trimming
- Quality filtering
- Contamination
Command:
bbduk.sh in=reads.fq out=clean.fq \\
ref=adapters.fa ktrim=r k=23 mink=11
""")
print("\n=== 5. Host Depletion ===")
print("""
Host:
- Remove host reads
- Important for microbiome
- Human, plant, etc.
Tools:
- Bowtie2
- BWA
- KneadData
Example:
bowtie2 -x human_genome -U reads.fq --un nonhost.fq
""")
print("\n=== 6. Contamination ===")
print("""
Contamination:
- Lab contaminants
- Kit contaminants
- Negative controls
Tools:
- Decontam (R)
- KneadData
- BLAST
Example:
Decontam
โ Remove contaminants
""")
print("\n=== 7. Duplicates ===")
print("""
Duplicates:
- PCR duplicates
- Sequencing duplicates
- Remove
Tools:
- FastUniq
- Dedup
- samtools markdup
Example:
fastuniq -i reads.fq -o dedup.fq
""")
print("\n=== 8. QC Pipeline ===")
print("""
Pipeline:
1. FastQC (raw)
2. Trimmomatic / fastp
3. FastQC (clean)
4. Host depletion
5. Contamination
6. Duplicates
7. Final QC
""")
print("\n=== 9. Python QC ===")
print("""
import subprocess
def qc_pipeline(reads_1, reads_2, output_prefix):
\"\"\"QC pipeline.\"\"\"
# FastQC
subprocess.run(["fastqc", reads_1, reads_2])
# Trim
subprocess.run([
"fastp",
"-i", reads_1, "-I", reads_2,
"-o", f"{output_prefix}_1.fq",
"-O", f"{output_prefix}_2.fq",
"--detect_adapter_for_pe"
])
return f"{output_prefix}_1.fq", f"{output_prefix}_2.fq"
# Use
# qc_pipeline("reads_1.fq", "reads_2.fq", "clean")
""")
print("\n=== 10. Best Practices ===")
print("""
Best practices:
- Always QC
- Use negative controls
- Check contamination
- Document
- Keep raw data
""")๐ณ Taxonomic Profiling
๐ฏ Who Is There?
Taxonomic profiling identifies which organisms are present in a metagenomic sample and their relative abundances.
Taxonomy
Kraken2: k-mer. MetaPhlAn: Markers. Centrifuge: Index. mOTUs: Markers. Bracken: Abundance. QIIME2: 16S.
# Taxonomic Profiling
print("=== 1. Kraken2 ===")
print("""
Kraken2:
- k-mer based
- Fast
- Accurate
- Database
Database:
- Standard
- PlusPF
- GTDB
Command:
kraken2 --db database --paired reads_1.fq reads_2.fq \\
--output output.kraken --report report.txt
""")
print("\n=== 2. Bracken ===")
print("""
Bracken:
- Abundance estimation
- From Kraken2
- Bayesian
Command:
bracken -d database -i report.txt -o bracken.txt -r 150
""")
print("\n=== 3. MetaPhlAn ===")
print("""
MetaPhlAn:
- Marker genes
- Species-level
- Accurate
Command:
metaphlan reads.fq --input_type fastq -o profile.txt
""")
print("\n=== 4. Centrifuge ===")
print("""
Centrifuge:
- Index-based
- Fast
- Compressed
Command:
centrifuge -x database -U reads.fq -S classification.txt
""")
print("\n=== 5. mOTUs ===")
print("""
mOTUs:
- Marker genes
- Species-level
- Profiling
Command:
motus profile -f reads.fq -o profile.txt
""")
print("\n=== 6. QIIME2 ===")
print("""
QIIME2:
- 16S analysis
- DADA2
- Taxonomy
Commands:
qiime tools import
qiime dada2 denoise-paired
qiime feature-classifier classify-sklearn
""")
print("\n=== 7. Comparison ===")
print(f"{'Tool':<20} {'Method':<20} {'Speed':<15} {'Best For':<20}")
print("-" * 75)
print(f"{'Kraken2':<20} {'k-mer':<20} {'Fast':<15} {'General':<20}")
print(f"{'Bracken':<20} {'Bayesian':<20} {'Fast':<15} {'Abundance':<20}")
print(f"{'MetaPhlAn':<20} {'Markers':<20} {'Medium':<15} {'Species':<20}")
print(f"{'Centrifuge':<20} {'Index':<20} {'Fast':<15} {'Large-scale':<20}")
print(f"{'mOTUs':<20} {'Markers':<20} {'Fast':<15} {'Species':<20}")
print(f"{'QIIME2':<20} {'16S':<20} {'Medium':<15} {'16S':<20}")
print("\n=== 8. Output ===")
print("""
Output:
- Taxonomic profile
- Relative abundance
- Species, genus, family
Example:
Bacteria: 80%
Archaea: 5%
Eukaryota: 10%
Viruses: 5%
""")
print("\n=== 9. Visualization ===")
print("""
Visualization:
- Bar plot
- Heatmap
- PCA
- Diversity
Tools:
- R (phyloseq)
- Python (matplotlib)
- QIIME2
- GraPhlAn
""")
print("\n=== 10. Python Taxonomy ===")
print("""
import pandas as pd
import matplotlib.pyplot as plt
def plot_taxonomy(profile):
\"\"\"Plot taxonomic profile.\"\"\"
df = pd.read_csv(profile, sep="\\t")
plt.figure(figsize=(12, 6))
plt.bar(df["Taxon"], df["Abundance"], color='purple')
plt.xlabel("Taxon")
plt.ylabel("Abundance")
plt.title("Taxonomic Profile")
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig("taxonomy.png")
plt.show()
# Use
# plot_taxonomy("profile.txt")
""")๐งฉ Assembly
๐ฏ From Reads to Contigs
Metagenomic assembly reconstructs longer sequences (contigs) from short reads, using De Bruijn graphs. It's the foundation for binning and MAGs.
Assembly
De Bruijn: Graph. k-mer: Key. Coverage: Depth. Contigs: Output. Tools: MEGAHIT, SPAdes. Co-assembly: Multiple samples.
# Assembly
print("=== 1. De Bruijn Graph ===")
print("""
De Bruijn:
- Graph of k-mers
- Nodes: k-mers
- Edges: overlaps
- Path: sequence
Example:
Reads:
ACGTACGT
CGTACGTA
k=3:
ACG โ CGT โ GTA โ TAC โ ACG
""")
print("\n=== 2. MEGAHIT ===")
print("""
MEGAHIT:
- Fast
- Memory efficient
- Metagenomics
Command:
megahit -1 reads_1.fq -2 reads_2.fq -o assembly
""")
print("\n=== 3. SPAdes ===")
print("""
SPAdes:
- metaSPAdes
- Accurate
- Multiple k-mers
Command:
spades.py --meta -1 reads_1.fq -2 reads_2.fq -o assembly
""")
print("\n=== 4. IDBA-UD ===")
print("""
IDBA-UD:
- Multiple k-mers
- Uneven coverage
- Metagenomics
Command:
idba_ud -r reads.fq -o assembly
""")
print("\n=== 5. Co-assembly ===")
print("""
Co-assembly:
- Multiple samples
- Combined
- Better coverage
- Strain resolution
Example:
Sample 1 + Sample 2
โ Co-assembly
โ More complete
""")
print("\n=== 6. k-mer Selection ===")
print("""
k-mer:
- Odd number
- 21-141
- Multiple
Example:
k=21: sensitive
k=33: specific
k=55: more specific
""")
print("\n=== 7. Assembly Stats ===")
print("""
Stats:
- N50
- Total length
- Number of contigs
- Longest contig
Tools:
- QUAST
- metaQUAST
Example:
N50: 50 kb
โ Good assembly
""")
print("\n=== 8. Binning Preparation ===")
print("""
Preparation:
- Filter by length
- Filter by coverage
- Map reads
- Coverage profile
Example:
Contigs > 1000 bp
โ Binning
""")
print("\n=== 9. Python Assembly ===")
print("""
import subprocess
def assemble(reads_1, reads_2, output="assembly"):
\"\"\"Assemble metagenome.\"\"\"
subprocess.run([
"megahit",
"-1", reads_1,
"-2", reads_2,
"-o", output,
"--min-contig-len", "1000"
], check=True)
return f"{output}/final.contigs.fa"
# Use
# contigs = assemble("reads_1.fq", "reads_2.fq")
""")
print("\n=== 10. Best Practices ===")
print("""
Best practices:
- Multiple k-mers
- Filter short contigs
- Check stats
- Co-assembly for multiple samples
- Long reads if possible
""")๐ฆ Binning
๐ฏ Grouping Contigs into Genomes
Binning is the process of grouping assembled contigs into draft genomes (MAGs). It uses sequence composition (k-mer frequencies) and coverage (read depth) to separate contigs from different organisms.
Binning
Composition: k-mer. Coverage: Depth. Tools: MetaBAT2, MaxBin2, CONCOCT. Consensus: DAS Tool. MAGs: Output. Quality: CheckM.
# Binning
print("=== 1. What is Binning? ===")
print("""
Binning:
- Contigs โ MAGs
- Same organism
- Composition + coverage
- Unsupervised
Goal:
- Reconstruct genomes
- Individual species
- MAGs
""")
print("\n=== 2. Composition ===")
print("""
Composition:
- k-mer frequencies
- Tetranucleotide
- GC content
- Species-specific
Example:
Species A: GC=60%
Species B: GC=40%
โ Separate
""")
print("\n=== 3. Coverage ===")
print("""
Coverage:
- Read depth
- Multiple samples
- Co-variation
Example:
Species A: high coverage
Species B: low coverage
โ Separate
""")
print("\n=== 4. MetaBAT2 ===")
print("""
MetaBAT2:
- Coverage + composition
- Fast
- Accurate
Command:
metabat2 -i contigs.fa -a depth.txt -o bins/
""")
print("\n=== 5. MaxBin2 ===")
print("""
MaxBin2:
- Expectation-maximization
- Coverage + composition
- Multiple samples
Command:
run_MaxBin.pl -contig contigs.fa -abund depth.txt -out bins/
""")
print("\n=== 6. CONCOCT ===")
print("""
CONCOCT:
- Multivariate statistics
- Coverage + composition
- Multiple samples
Command:
concoct --coverage_file coverage.csv --composition_file contigs.fa -b bins/
""")
print("\n=== 7. DAS Tool ===")
print("""
DAS Tool:
- Consensus binning
- Combines tools
- Best bins
Command:
DAS_Tool -i bins1,bins2,bins3 -c contigs.fa -o final_bins
""")
print("\n=== 8. Semi-supervised ===")
print("""
Semi-supervised:
- Binning with markers
- Better resolution
- Strain-level
Tools:
- MetaBAT2
- MaxBin2
- CONCOCT
- MetaDecoder
""")
print("\n=== 9. Python Binning ===")
print("""
import subprocess
def bin_contigs(contigs, depth, output="bins"):
\"\"\"Bin contigs with MetaBAT2.\"\"\"
subprocess.run([
"metabat2",
"-i", contigs,
"-a", depth,
"-o", output
], check=True)
return output
# Use
# bins = bin_contigs("contigs.fa", "depth.txt")
""")
print("\n=== 10. Best Practices ===")
print("""
Best practices:
- Multiple samples
- Multiple tools
- DAS Tool
- Check quality
- Filter
""")๐งฌ MAGs & Quality
๐ฏ Metagenome-Assembled Genomes
MAGs (Metagenome-Assembled Genomes) are draft genomes reconstructed from metagenomic data. Their quality is assessed by completeness and contamination.
MAGs
Completeness: % of genome. Contamination: % foreign. CheckM: Quality. GTDB-Tk: Taxonomy. BUSCO: Completeness. Quality: High/medium/low.
# MAGs & Quality
print("=== 1. What is a MAG? ===")
print("""
MAG:
- Metagenome-Assembled Genome
- Draft genome
- From metagenomics
- Unculturable
Example:
Bin 1 โ MAG
โ Species A
""")
print("\n=== 2. Quality Metrics ===")
print("""
Quality:
- Completeness: % of expected genes
- Contamination: % foreign
- Strain heterogeneity
Standards:
- High: >90% complete, <5% contam
- Medium: >50% complete, <10% contam
- Low: <50% complete
""")
print("\n=== 3. CheckM ===")
print("""
CheckM:
- Completeness
- Contamination
- Lineage-specific
Command:
checkm lineage_wf bins/ output/ -x fa
""")
print("\n=== 4. CheckM2 ===")
print("""
CheckM2:
- Machine learning
- Faster
- Accurate
Command:
checkm2 predict --input bins/ --output-directory output/
""")
print("\n=== 5. GTDB-Tk ===")
print("""
GTDB-Tk:
- Taxonomic assignment
- GTDB database
- Standard
Command:
gtdbtk classify_wf --genome_dir bins/ --out_dir output/
""")
print("\n=== 6. BUSCO ===")
print("""
BUSCO:
- Completeness
- Universal markers
- Genome quality
Command:
busco -i bins/ -o output -m genome
""")
print("\n=== 7. MAG Statistics ===")
print("""
Stats:
- Number of MAGs
- Completeness
- Contamination
- Size
- GC
Example:
100 MAGs
โ 20 high quality
โ 50 medium
โ 30 low
""")
print("\n=== 8. MAG Annotation ===")
print("""
Annotation:
- Genes (Prodigal)
- Taxonomy (GTDB-Tk)
- Function (KEGG, eggNOG)
- CAZy
Command:
prodigal -i mag.fa -o genes.gff -a proteins.faa
""")
print("\n=== 9. Python MAG ===")
print("""
import subprocess
import pandas as pd
def check_mags(bins_dir, output="checkm"):
\"\"\"Check MAG quality.\"\"\"
subprocess.run([
"checkm", "lineage_wf",
bins_dir, output,
"-x", "fa"
], check=True)
# Parse
df = pd.read_csv(f"{output}/storage/qa.tsv", sep="\\t")
return df
# Use
# df = check_mags("bins/")
""")
print("\n=== 10. Best Practices ===")
print("""
Best practices:
- CheckM/CheckM2
- GTDB-Tk
- Filter quality
- Dereplicate
- Submit to databases
""")๐ฌ Functional Annotation
๐ฏ What Can They Do?
Functional annotation assigns functions to genes in MAGs and metagenomes, revealing metabolic potential and ecological roles.
Functional
Prodigal: Genes. KEGG: Pathways. eggNOG: Orthologs. CAZy: Enzymes. Pfam: Domains. GO: Ontology.
# Functional Annotation
print("=== 1. Gene Prediction ===")
print("""
Prodigal:
- Prokaryotic genes
- Fast
- Accurate
Command:
prodigal -i contigs.fa -o genes.gff -a proteins.faa -p meta
""")
print("\n=== 2. KEGG ===")
print("""
KEGG:
- Pathways
- KO numbers
- Functions
Tools:
- KofamKOALA
- GhostKOALA
- BlastKOALA
Example:
proteins.faa
โ KO numbers
โ Pathways
""")
print("\n=== 3. eggNOG ===")
print("""
eggNOG:
- Orthologs
- Functional categories
- COG
Command:
emapper.py -i proteins.faa -o output --cpu 4
""")
print("\n=== 4. CAZy ===")
print("""
CAZy:
- Carbohydrate-active enzymes
- CAZymes
- Biotech
Tools:
- dbCAN2
- CAZy database
Example:
proteins.faa
โ CAZymes
โ Biotech
""")
print("\n=== 5. Pfam ===")
print("""
Pfam:
- Protein domains
- Families
- HMMER
Command:
hmmscan --domtblout output.domtbl Pfam-A.hmm proteins.faa
""")
print("\n=== 6. GO ===")
print("""
GO:
- Gene Ontology
- BP, MF, CC
- Annotation
Tools:
- InterProScan
- GOATOOLS
Example:
proteins.faa
โ GO terms
โ Functions
""")
print("\n=== 7. Functional Profiles ===")
print("""
Profiles:
- KEGG modules
- Pathways
- CAZymes
- COG categories
Example:
Community
โ KEGG modules
โ Functions
""")
print("\n=== 8. Comparison ===")
print("""
Tools:
- Prokka: annotation
- Prodigal: genes
- KofamKOALA: KEGG
- eggNOG: orthologs
- dbCAN2: CAZy
- InterProScan: domains
""")
print("\n=== 9. Python Annotation ===")
print("""
import subprocess
import pandas as pd
def annotate_genes(proteins, output="annotation"):
\"\"\"Annotate genes with eggNOG.\"\"\"
subprocess.run([
"emapper.py",
"-i", proteins,
"-o", output,
"--cpu", "4"
], check=True)
# Parse
df = pd.read_csv(f"{output}.emapper.annotations", sep="\\t")
return df
# Use
# df = annotate_genes("proteins.faa")
""")
print("\n=== 10. Best Practices ===")
print("""
Best practices:
- Multiple tools
- Combine
- Validate
- Visualize
- Interpret
""")๐ ๏ธ Tools (Kraken2, MetaBAT2, GTDB-Tk)
๐ฏ Metagenomics Tools
Many tools exist for metagenomics analysis. Each has strengths and is suited to different use cases.
Tools
Kraken2: Taxonomy. MetaBAT2: Binning. GTDB-Tk: Taxonomy. CheckM: Quality. MEGAHIT: Assembly. Prodigal: Genes.
# Tools
print("=== Kraken2 ===")
print("""
Kraken2:
- Taxonomic classification
- k-mer based
- Fast
- Accurate
Command:
kraken2 --db database --paired reads_1.fq reads_2.fq \\
--output output.kraken --report report.txt
""")
print("\n=== MetaBAT2 ===")
print("""
MetaBAT2:
- Binning
- Coverage + composition
- Fast
Command:
metabat2 -i contigs.fa -a depth.txt -o bins/
""")
print("\n=== GTDB-Tk ===")
print("""
GTDB-Tk:
- Taxonomic assignment
- GTDB
- Standard
Command:
gtdbtk classify_wf --genome_dir bins/ --out_dir output/
""")
print("\n=== CheckM ===")
print("""
CheckM:
- MAG quality
- Completeness
- Contamination
Command:
checkm lineage_wf bins/ output/ -x fa
""")
print("\n=== MEGAHIT ===")
print("""
MEGAHIT:
- Assembly
- Fast
- Memory efficient
Command:
megahit -1 reads_1.fq -2 reads_2.fq -o assembly
""")
print("\n=== Prodigal ===")
print("""
Prodigal:
- Gene prediction
- Prokaryotic
- Fast
Command:
prodigal -i contigs.fa -o genes.gff -a proteins.faa -p meta
""")
print("\n=== Other Tools ===")
print("""
1. SPAdes
- Assembly
2. MaxBin2
- Binning
3. CONCOCT
- Binning
4. DAS Tool
- Consensus binning
5. CheckM2
- Quality
6. Prokka
- Annotation
7. eggNOG
- Orthologs
8. KofamKOALA
- KEGG
9. dbCAN2
- CAZy
10. QIIME2
- 16S
""")
print("\n=== Tool Comparison ===")
print(f"{'Tool':<20} {'Step':<20} {'Speed':<15} {'Best For':<20}")
print("-" * 75)
print(f"{'Kraken2':<20} {'Taxonomy':<20} {'Fast':<15} {'General':<20}")
print(f"{'MetaBAT2':<20} {'Binning':<20} {'Fast':<15} {'General':<20}")
print(f"{'GTDB-Tk':<20} {'Taxonomy':<20} {'Medium':<15} {'Standard':<20}")
print(f"{'CheckM':<20} {'Quality':<20} {'Medium':<15} {'MAGs':<20}")
print(f"{'MEGAHIT':<20} {'Assembly':<20} {'Fast':<15} {'Metagenomics':<20}")
print(f"{'Prodigal':<20} {'Genes':<20} {'Fast':<15} {'Prokaryotes':<20}")
print("\n=== When to Use Which ===")
print("""
Taxonomy:
โ Kraken2
โ MetaPhlAn
โ GTDB-Tk
Binning:
โ MetaBAT2
โ MaxBin2
โ CONCOCT
โ DAS Tool
Assembly:
โ MEGAHIT
โ SPAdes
Quality:
โ CheckM
โ CheckM2
Annotation:
โ Prodigal
โ Prokka
โ eggNOG
โ KofamKOALA
""")๐ Python Integration
๐ฏ Metagenomics with Python
Python is a powerful language for metagenomics analysis, automation, and visualization. Here's how.
Python
Subprocess: Run tools. Pandas: Data. Matplotlib: Plots. Biopython: Sequences. Scikit-bio: Analysis. Automate: Pipeline.
# Python Integration
print("=== 1. Run Kraken2 ===")
print("""
import subprocess
def run_kraken2(reads_1, reads_2, db, output="kraken"):
\"\"\"Run Kraken2.\"\"\"
subprocess.run([
"kraken2",
"--db", db,
"--paired", reads_1, reads_2,
"--output", f"{output}.kraken",
"--report", f"{output}.report"
], check=True)
return f"{output}.report"
# Use
# report = run_kraken2("reads_1.fq", "reads_2.fq", "database")
""")
print("\n=== 2. Parse Kraken2 ===")
print("""
import pandas as pd
def parse_kraken_report(filepath):
\"\"\"Parse Kraken2 report.\"\"\"
df = pd.read_csv(filepath, sep="\\t", header=None,
names=["Percent", "Reads", "Taxon_Reads",
"Rank", "TaxID", "Name"])
return df
# Use
# df = parse_kraken_report("kraken.report")
""")
print("\n=== 3. Run MetaBAT2 ===")
print("""
def run_metabat2(contigs, depth, output="bins"):
\"\"\"Run MetaBAT2.\"\"\"
subprocess.run([
"metabat2",
"-i", contigs,
"-a", depth,
"-o", output
], check=True)
return output
# Use
# bins = run_metabat2("contigs.fa", "depth.txt")
""")
print("\n=== 4. Run CheckM ===")
print("""
def run_checkm(bins_dir, output="checkm"):
\"\"\"Run CheckM.\"\"\"
subprocess.run([
"checkm", "lineage_wf",
bins_dir, output,
"-x", "fa"
], check=True)
return output
# Use
# checkm_output = run_checkm("bins/")
""")
print("\n=== 5. Parse CheckM ===")
print("""
def parse_checkm(output):
\"\"\"Parse CheckM output.\"\"\"
df = pd.read_csv(f"{output}/storage/qa.tsv", sep="\\t")
return df
# Use
# df = parse_checkm("checkm")
""")
print("\n=== 6. Run GTDB-Tk ===")
print("""
def run_gtdbtk(bins_dir, output="gtdbtk"):
\"\"\"Run GTDB-Tk.\"\"\"
subprocess.run([
"gtdbtk", "classify_wf",
"--genome_dir", bins_dir,
"--out_dir", output
], check=True)
return output
# Use
# gtdb_output = run_gtdbtk("bins/")
""")
print("\n=== 7. Visualization ===")
print("""
import matplotlib.pyplot as plt
def plot_taxonomy(df, output="taxonomy.png"):
\"\"\"Plot taxonomy.\"\"\"
df = df[df["Rank"] == "S"].head(20)
plt.figure(figsize=(12, 6))
plt.barh(df["Name"], df["Percent"], color='purple')
plt.xlabel("Relative Abundance (%)")
plt.title("Taxonomic Profile")
plt.tight_layout()
plt.savefig(output)
plt.show()
# Use
# plot_taxonomy(df)
""")
print("\n=== 8. MAG Quality ===")
print("""
def plot_mag_quality(df, output="mag_quality.png"):
\"\"\"Plot MAG quality.\"\"\"
plt.figure(figsize=(10, 6))
plt.scatter(df["Completeness"], df["Contamination"],
c=df["Genome_Size"], cmap='viridis')
plt.xlabel("Completeness (%)")
plt.ylabel("Contamination (%)")
plt.title("MAG Quality")
plt.colorbar(label="Genome Size")
plt.tight_layout()
plt.savefig(output)
plt.show()
# Use
# plot_mag_quality(df)
""")
print("\n=== 9. Complete Pipeline ===")
print("""
import subprocess
import pandas as pd
def metagenomics_pipeline(reads_1, reads_2, output="meta"):
\"\"\"Complete metagenomics pipeline.\"\"\"
# QC
subprocess.run([
"fastp", "-i", reads_1, "-I", reads_2,
"-o", f"{output}_1.fq", "-O", f"{output}_2.fq"
], check=True)
# Assembly
subprocess.run([
"megahit", "-1", f"{output}_1.fq", "-2", f"{output}_2.fq",
"-o", f"{output}_assembly"
], check=True)
# Binning
subprocess.run([
"metabat2", "-i", f"{output}_assembly/final.contigs.fa",
"-a", f"{output}_depth.txt",
"-o", f"{output}_bins"
], check=True)
# Quality
subprocess.run([
"checkm", "lineage_wf",
f"{output}_bins", f"{output}_checkm",
"-x", "fa"
], check=True)
return output
# Use
# metagenomics_pipeline("reads_1.fq", "reads_2.fq")
""")
print("\n=== 10. Libraries ===")
print("""
Libraries:
- Biopython
- scikit-bio
- pandas
- numpy
- matplotlib
- seaborn
- pysam
- ete3
""")โ๏ธ Tool Comparison
๐ฏ Which Tool?
Choosing the right metagenomics tool depends on your data, goals, and computational resources.
Comparison
Kraken2: Fast taxonomy. MetaBAT2: Binning. GTDB-Tk: Taxonomy. CheckM: Quality. MEGAHIT: Assembly. DAS Tool: Consensus.
| Tool | Step | Speed | Input | Best For |
|---|---|---|---|---|
| Kraken2 | Taxonomy | Fast | Reads | General |
| MetaPhlAn | Taxonomy | Medium | Reads | Species |
| MEGAHIT | Assembly | Fast | Reads | Metagenomics |
| SPAdes | Assembly | Medium | Reads | Accurate |
| MetaBAT2 | Binning | Fast | Contigs | General |
| MaxBin2 | Binning | Medium | Contigs | Multiple samples |
| CONCOCT | Binning | Medium | Contigs | Multiple samples |
| DAS Tool | Consensus | Fast | Bins | Best bins |
| CheckM | Quality | Medium | Bins | MAGs |
| GTDB-Tk | Taxonomy | Medium | Bins | Standard |
# Decision Guide
print("=== Choose Tool by Step ===")
print("""
Taxonomy:
โ Kraken2 (fast)
โ MetaPhlAn (accurate)
Assembly:
โ MEGAHIT (fast)
โ SPAdes (accurate)
Binning:
โ MetaBAT2 (fast)
โ MaxBin2 (multi-sample)
โ CONCOCT (multi-sample)
โ DAS Tool (consensus)
Quality:
โ CheckM
โ CheckM2
Annotation:
โ Prodigal
โ Prokka
โ eggNOG
โ KofamKOALA
""")
print("\n=== Speed Comparison ===")
print(f"{'Tool':<20} {'1 GB':<15} {'10 GB':<15} {'100 GB':<15}")
print("-" * 65)
print(f"{'Kraken2':<20} {'1 min':<15} {'10 min':<15} {'1 hour':<15}")
print(f"{'MEGAHIT':<20} {'10 min':<15} {'1 hour':<15} {'10 hours':<15}")
print(f"{'MetaBAT2':<20} {'1 min':<15} {'10 min':<15} {'1 hour':<15}")
print(f"{'CheckM':<20} {'5 min':<15} {'1 hour':<15} {'10 hours':<15}")
print(f"{'GTDB-Tk':<20} {'10 min':<15} {'2 hours':<15} {'20 hours':<15}")
print("\n=== When to Use Which ===")
print("""
USE Kraken2:
โ Fast taxonomy
โ General
โ Large-scale
USE MetaBAT2:
โ Fast binning
โ General
โ Standard
USE GTDB-Tk:
โ Taxonomy
โ Standard
โ MAGs
USE CheckM:
โ Quality
โ MAGs
โ Standard
USE MEGAHIT:
โ Fast assembly
โ Metagenomics
โ Large-scale
USE DAS Tool:
โ Consensus
โ Best bins
โ Multi-tool
""")โ FAQ
๐ฏ Frequently Asked Questions
Answers to common questions about metagenomics and binning.
What is metagenomics?
Metagenomics is the study of genetic material recovered directly from environmental samples, bypassing the need for isolation and lab cultivation of individual species. It enables the study of entire microbial communities, including unculturable organisms.
What is binning in metagenomics?
Binning is the process of grouping assembled metagenomic contigs into draft genomes, called metagenome-assembled genomes (MAGs). It uses sequence composition (k-mer frequencies) and coverage (read depth across samples) to separate contigs belonging to the same organism.
What is a MAG?
A MAG (Metagenome-Assembled Genome) is a draft genome reconstructed from metagenomic data through assembly and binning. MAGs represent the genetic content of individual microbial species in a community and are essential for studying unculturable microbes.
Which tools are used for binning?
Common binning tools include MetaBAT2 (coverage and composition), MaxBin2 (expectation-maximization), CONCOCT (multivariate statistics), and DAS Tool (consensus binning). Quality assessment is done with CheckM and GTDB-Tk.
What is taxonomic profiling?
Taxonomic profiling identifies which organisms are present in a metagenomic sample and their relative abundances. Tools include Kraken2 (k-mer based), MetaPhlAn (marker genes), and Centrifuge (index-based).
What is the difference between 16S and shotgun metagenomics?
16S rRNA sequencing targets a specific marker gene and is cheaper but only provides taxonomy. Shotgun metagenomics sequences all DNA and provides both taxonomy and functional information, but is more expensive and computationally intensive.
What is a good MAG quality?
A high-quality MAG has >90% completeness and <5% contamination. Medium-quality is >50% completeness and <10% contamination. Low-quality is <50% completeness. Quality is assessed with CheckM or CheckM2.
How do I annotate MAGs?
Annotate MAGs with Prodigal (gene prediction), KofamKOALA (KEGG), eggNOG-mapper (orthologs), dbCAN2 (CAZymes), and InterProScan (domains). These reveal metabolic potential and ecological roles.
What is the best assembly tool?
MEGAHIT is fast and memory-efficient, ideal for large datasets. SPAdes (metaSPAdes) is more accurate but slower. Use MEGAHIT for large-scale, SPAdes for smaller, high-quality assemblies.
What is co-assembly?
Co-assembly combines reads from multiple samples into a single assembly. This increases coverage and improves binning, especially for low-abundance organisms. It's useful for time-series or multiple related samples.
# Quick Reference
print("=== Quick Answers ===")
print("""
Q: What is metagenomics?
A: Study of genetic material from environment.
Q: What is binning?
A: Group contigs into MAGs.
Q: What is a MAG?
A: Metagenome-assembled genome.
Q: Best taxonomy tool?
A: Kraken2 (fast), MetaPhlAn (accurate).
Q: Best assembly tool?
A: MEGAHIT (fast), SPAdes (accurate).
Q: Best binning tool?
A: MetaBAT2 (fast), DAS Tool (consensus).
Q: Best quality tool?
A: CheckM, CheckM2.
Q: Best taxonomy for MAGs?
A: GTDB-Tk.
Q: Annotation?
A: Prodigal, KofamKOALA, eggNOG.
Q: 16S vs shotgun?
A: 16S = taxonomy; shotgun = function.
Q: Free?
A: Yes โ all major tools are free.
""")๐ Future
๐ฏ Where Metagenomics Is Going
The future is long reads, single-cell, AI, pangenomes, clinical, and global.
Future Directions
Long reads: Better MAGs. Single-cell: Resolution. AI: Prediction. Pangenomes: Strains. Clinical: Diagnostics. Global: Microbiomes.
# Future of Metagenomics
print("=== 1. Long Reads ===")
print("""
Trend: Long reads
Current:
- Short reads
- Fragmented
Future:
- Nanopore
- PacBio
- Complete genomes
- Strain-level
Examples:
- Nanopore
- PacBio HiFi
- Hybrid
Future:
- Complete MAGs
- Strain resolution
- Better assembly
""")
print("\n=== 2. Single-Cell ===")
print("""
Trend: Single-cell
Current:
- Bulk
- Community
Future:
- Single-cell
- Individual microbes
- Genome
Examples:
- scMicrobiome
- Microfluidic
- Single-cell genomics
Future:
- Single-cell MAGs
- Heterogeneity
- Function
""")
print("\n=== 3. AI ===")
print("""
Trend: AI + metagenomics
Ideas:
- Deep learning
- Binning
- Annotation
- Prediction
Examples:
- Deep learning
- Graph neural networks
- Transformer
Future:
- Better binning
- Better annotation
- Predictive
""")
print("\n=== 4. Pangenomes ===")
print("""
Trend: Pangenomes
Current:
- Single genome
- MAGs
Future:
- Pangenome
- Strains
- Variation
Examples:
- Prokaryotic pangenomes
- Strain-level
- Graph
Future:
- Complete pangenomes
- Strain resolution
- Variation
""")
print("\n=== 5. Clinical ===")
print("""
Trend: Clinical
Current:
- Research
Future:
- Clinical
- Diagnostics
- Therapeutics
Examples:
- Pathogen detection
- Microbiome therapy
- Diagnostics
Future:
- Clinical implementation
- Precision
- Equity
""")
print("\n=== 6. Global ===")
print("""
Trend: Global microbiomes
Current:
- Local
Future:
- Global
- Earth microbiome
- Time-series
Examples:
- Earth Microbiome Project
- Global
- Time-series
Future:
- Global maps
- Dynamics
- Climate
""")
# TIMELINE
print("\n=== Timeline ===")
print("""
Past:
1998: First metagenomic library
2004: Sargasso Sea
2006: HMP
2014: Binning
2017: MAGs
2020: Large-scale
Present:
2025: Long reads
2026: Single-cell
Future:
2030: AI
2035: Clinical
2040: Global?
""")
print("\n=== Final Thoughts ===")
print("""
Metagenomics is:
- Transforming microbiology
- Accessing unculturable
- Understanding communities
- Evolving
The future is:
- Long reads
- Single-cell
- AI
- Pangenomes
- Clinical
- Global
"The unseen majority โ now seen."
Keep exploring. Keep building.
""")