Before you start
- You've worked through Read Alignment and the samtools survival kit. This lesson stitches them together.
- A free Google account to run the notebook. No installs on your machine.
- Any term new? The Glossary has it.
Learning objectives
By the end of this lesson you will be able to: take raw sequencing reads through alignment to a list of variants (a VCF), mark duplicates, and judge a called variant by its depth and quality.
What "calling a variant" means
Every person's genome differs from the reference at millions of spots. A variant is one of those differences: a single letter changed (a SNP), or a few letters inserted or deleted (an indel). Variant calling is the process of reading the stacked-up reads at each position and deciding, statistically, "the reference says A here, but the reads clearly say G, so this sample carries a variant."
The skill is knowing the difference between a real variant and a sequencing error. One read disagreeing with the reference is probably noise. Twenty reads all agreeing on the same change is a variant. A variant caller does exactly this reasoning, position by position, across the whole genome.
Decode the jargonSNP and indel
A SNP (single-nucleotide polymorphism) is a one-letter difference, like an A where the reference has a G. An indel is a small insertion or deletion of bases. Together they're the most common kinds of variants, and the ones you'll call in this lesson.
The whole pipeline on one screen
You already know the first four boxes. This lesson adds the last one, bcftools, and connects them all into a single runnable pipeline.
โถ Run the full pipeline in Google Colab
The notebook builds a reference, plants known variants in a sample, then recovers them, so you can check the answer.
1Install and prepare
We need three tools: bwa to align, samtools to sort and index, and bcftools to call variants.
!apt-get -qq install -y bwa samtools bcftools
The notebook then creates a reference and a sample genome that's identical except for a few planted SNPs at positions we record. Reads are generated from the sample, so when we call variants we should recover those planted changes, a built-in answer key. (You may also see one low-quality call at the very edge of the genome, where only a read or two reaches; the filter step below removes it, which is a nice preview of why filtering matters.)
2Align, sort, index
This is everything from the last two lessons, compressed into four commands. Notice how each tool hands off to the next.
!bwa index reference.fasta !bwa mem reference.fasta sample.fastq | samtools sort -o sample.sorted.bam !samtools markdup sample.sorted.bam sample.markdup.bam # flag PCR/optical duplicates !samtools index sample.markdup.bam
Why mark duplicates?
PCR and optical duplicates are copies of the same original fragment. Left in, they inflate the apparent support for a position and cause false variant calls, so real pipelines flag them before calling. On paired-end data you run samtools fixmate -m (on a name-sorted BAM) before the coordinate sort so markdup has the mate information it needs.
3Call variants with bcftools
Calling is two steps piped together. bcftools mpileup walks the genome and, at every position, gathers the pile of read bases sitting there. bcftools call then judges each pile and decides whether it's a variant.
!bcftools mpileup -f reference.fasta sample.markdup.bam | \ bcftools call -mv -Oz -o calls.vcf.gz !bcftools norm -m -any -f reference.fasta calls.vcf.gz -Oz -o calls.norm.vcf.gz # split multi-allelic sites, left-align indels !bcftools index calls.norm.vcf.gz
The extra bcftools norm step left-aligns and normalises indels so the same insertion or deletion is always written the same way, which matters the moment you compare or annotate variants on real data.
The flags read as: -m use the modern multiallelic caller, -v output only variant sites (skip positions that match the reference), and -Oz write a compressed VCF.
Decode the jargonPileup
A pileup is the column of read bases stacked over a single genome position, like reading straight down through all the reads that cover that spot. The caller looks at each column and asks: do the reads agree with the reference, or do enough of them disagree to call a variant?
4Read your VCF
VCF (Variant Call Format) is the standard text file listing variants. After header lines (starting with #), each row is one variant.
!bcftools view calls.vcf.gz | grep -v '^##' | head
| Column | Meaning |
|---|---|
CHROM | Which chromosome the variant is on. |
POS | The position of the variant. |
REF | The base in the reference genome. |
ALT | The alternative base seen in your sample, the change. |
QUAL | How confident the caller is that this variant is real. |
INFO | Extra details, such as depth (DP) at this site. |
Compare the POS and ALT columns to the variants the notebook planted. If they match, you've just validated your own pipeline end to end.
โ ๏ธ Not every call is real
Variant callers make mistakes, especially at low coverage or in repetitive regions. Real projects filter the VCF by quality and depth (for example, dropping calls with QUAL below a threshold or very low DP) before trusting it. Raw calls are a starting point, not a final answer.
bcftools filter -e 'QUAL<20 || DP<10' calls.norm.vcf.gz -Oz -o calls.filt.vcf.gz
๐ Your Track 2 deliverable
Turn the notebook into something you can show. Build a documented variant-calling mini-pipeline:
- Run the full FASTQ โ VCF pipeline end to end in the notebook.
- Add a markdown cell at the top explaining, in your own words, what each stage does and why.
- Report two numbers: your mapping rate (from
flagstat) and how many variants you called. - Confirm the called variants match the planted ones, and write one sentence on what would change with real data.
Save it to your GitHub and you have a portfolio piece that shows you can run the core genomics workflow.
๐ถ Level up: real data and GATK
For human germline work, the common choices are the GATK Best Practices pipeline, which adds base-quality recalibration and joint genotyping across many samples, and DeepVariant, a deep-learning caller. bcftools (what you used here) is lighter, faster, and excellent for many projects, especially microbial genomes and quick analyses. Same concepts, more rigor. The section below runs the pipeline on a real E. coli run from the ENA.
Now do it on real data: E. coli from the ENA
The toy run above is honest about being a rehearsal. Here is the same pipeline on a real sequencing run of E. coli, downloaded from the European Nucleotide Archive (ENA). Everything runs in Colab; the download and alignment take a few minutes, not seconds, because this is real data. The reads come from the Lenski long-term evolution experiment (run SRR2584866), a famous study that has grown E. coli for tens of thousands of generations. The right reference is the experiment's ancestor strain, E. coli B REL606. That choice matters: if you used the more common K-12 lab strain instead, most of your "variants" would just be the normal differences between two strains, not mutations that arose during the experiment.
!apt-get -qq install -y bwa samtools bcftools fastp # 1. Reference genome: the experiment's ancestor, E. coli B REL606 (GCA_000017985.1) !wget -q https://ftp.ncbi.nlm.nih.gov/genomes/all/GCA/000/017/985/GCA_000017985.1_ASM1798v1/GCA_000017985.1_ASM1798v1_genomic.fna.gz !gunzip -f GCA_000017985.1_ASM1798v1_genomic.fna.gz && mv GCA_000017985.1_ASM1798v1_genomic.fna ecoli_rel606.fasta # 2. A real paired-end sequencing run from the ENA !wget -q https://ftp.sra.ebi.ac.uk/vol1/fastq/SRR258/006/SRR2584866/SRR2584866_1.fastq.gz !wget -q https://ftp.sra.ebi.ac.uk/vol1/fastq/SRR258/006/SRR2584866/SRR2584866_2.fastq.gz # 3. QC and trim (paired-end: fastp auto-detects adapters from the read overlap) !fastp -i SRR2584866_1.fastq.gz -I SRR2584866_2.fastq.gz -o r1.fq.gz -O r2.fq.gz -h fastp.html # 4. Align, then fixmate -> sort -> markdup (the real paired-end dedup flow) !bwa index ecoli_rel606.fasta !bwa mem ecoli_rel606.fasta r1.fq.gz r2.fq.gz | samtools sort -n -o namesorted.bam !samtools fixmate -m namesorted.bam fixmate.bam !samtools sort -o sorted.bam fixmate.bam !samtools markdup sorted.bam markdup.bam && samtools index markdup.bam # 5. How well did the reads ACTUALLY map? (this number will not be 100%) !samtools flagstat markdup.bam # 6. Call and normalise !bcftools mpileup -f ecoli_rel606.fasta markdup.bam | bcftools call -mv --ploidy 1 -Oz -o ecoli.vcf.gz !bcftools norm -m -any -f ecoli_rel606.fasta ecoli.vcf.gz -Oz -o ecoli.norm.vcf.gz && bcftools index ecoli.norm.vcf.gz !bcftools stats ecoli.norm.vcf.gz | grep "number of records:"
What real data breaks (that the toy run hid)
Look at flagstat: the mapping rate is high but not 100%, because real reads include contamination, low-quality ends, and repeats that map ambiguously. markdup now flags a real percentage of duplicates. There is no planted answer key; you judge each call by its depth (DP) and quality (QUAL) instead of checking it against a list. And the calls now reflect real mutations that arose during the experiment (plus any errors your filters still need to catch), including some indels, which is exactly why the norm step matters. This is the gap between "ran the notebook" and "ran an analysis," and you just crossed it.
Try it here: call variants in your browser
The practice data is simulated: a 10,000-base chromosome called chr1 and 1,780 reads of 100 bases, built so the results are small, fast and checkable. Press Run on each box; a real Linux terminal opens at the bottom of the page. No install, no account. The simulated sample carries 10 planted variants (8 SNPs, 1 deletion, 1 insertion), so you can check your calls against the truth. Type the commands without the Colab !.
bwa index reference.fasta bwa mem reference.fasta sample.fastq | samtools sort -o sample.sorted.bam samtools markdup sample.sorted.bam sample.markdup.bam samtools index sample.markdup.bam
bcftools mpileup -f reference.fasta sample.markdup.bam | bcftools call -mv -Oz -o calls.vcf.gz bcftools norm -m -any -f reference.fasta calls.vcf.gz -Oz -o calls.norm.vcf.gz bcftools index calls.norm.vcf.gz bcftools view -H calls.norm.vcf.gz | cut -f1-5
bcftools filter -e 'QUAL<20 || DP<10' calls.norm.vcf.gz -Oz -o calls.filt.vcf.gz bcftools view -H calls.filt.vcf.gz | cut -f1-5
What you should see
All 10 planted variants are called: SNPs at 1203, 2450, 3333, 4120, 5555, 6781, 7802 and 9010, a 3-base deletion and a 3-base insertion at 8400. The deletion was planted at positions 6000-6002 but is reported at 5998 (GGCT to G): the same deletion, written in its leftmost form, which is exactly what bcftools norm is for. After filtering, 9 remain: the real SNP at 5555 is removed because only 9 reads cover it, below the DP<10 cutoff. Filters remove true variants too, which is why thresholds are a judgement call.
Check your understanding
bcftools mpileup and bcftools call each contribute?mpileup gathers the evidence: at each position it assembles the pile of read bases and their qualities. call makes the decision: it judges each pile statistically and outputs the positions it believes are genuine variants.Sources & further reading
- Danecek P, Bonfield JK, Liddle J, et al. Twelve years of SAMtools and BCFtools. GigaScience 10: giab008, 2021. (mpileup, call, norm, filter) doi:10.1093/gigascience/giab008
- Li H. A statistical framework for SNP calling, mutation discovery, association mapping and population genetical parameter estimation from sequencing data. Bioinformatics 27: 2987-2993, 2011. (the bcftools calling model) doi:10.1093/bioinformatics/btr509
- Tan A, Abecasis GR, Kang HM. Unified representation of genetic variants. Bioinformatics 31: 2202-2204, 2015. (why normalization matters) doi:10.1093/bioinformatics/btv112
- Jeong H, Barbe V, Lee CH, et al. Genome sequences of Escherichia coli B strains REL606 and BL21(DE3). Journal of Molecular Biology 394: 644-652, 2009. (the REL606 reference) doi:10.1016/j.jmb.2009.09.052
- Tenaillon O, Barrick JE, Ribeck N, et al. Tempo and mode of genome evolution in a 50,000-generation experiment. Nature 536: 165-170, 2016. (the Lenski experiment genomes) doi:10.1038/nature18959
- Poplin R, Chang PC, Alexander D, et al. A universal SNP and small-indel variant caller using deep neural networks. Nature Biotechnology 36: 983-987, 2018. (DeepVariant) doi:10.1038/nbt.4235
Last reviewed: October 2026.
Take the Track 2 checkpoint โ
Practice terminal powered by sandbox.bio (runs in your browser; your commands stay on your computer).