SH•Syed Adnan HaiderClinical & pathogen genomicsGet in touch
Menu

FASTQ to Phylogeny · Part 9 · Free beginner exercise

From mapped reads to a consensus genome.

Apply supported changes. Keep uncertainty visible.

Use BCFtools to call a synthetic variant, turn low-depth positions into an N mask, and build a consensus FASTA from the reference and read evidence.

Join the Part 9 discussion on LinkedIn ↗
Same reference, updated readsA new synthetic base change to inspectOne new tool: BCFtoolsContinue with Ubuntu/WSL2Make uncertainty visibleMask positions below the example cutoff

Everything for Part 9

Download. Build. Check.

Get the twelve-slide lesson and the complete practice files. Use the updated Part 9 reads and create a new alignment for this exercise.

Practice ZIP: about 11 KB · Slides: twelve pages, about 145 KB

Inside the ZIP: reference.fasta, reads_part9.fastq.gz, README.txt, DATASET_DESIGN.txt and run_part9.sh.

The files are entirely artificial training data. Extract the practice ZIP, but keep the FASTQ compressed.

Same reference. Updated reads.

The 2,400-base reference and designed coverage gap continue from Part 8. The new FASTQ introduces a G→A change at position 877. Your old Part 7/8 FASTQ or BAM will not demonstrate this change.

Step 01 · What are we building?

A consensus summarises the evidence.

Reference
The sequence against which the reads are aligned.
Variant call
A candidate difference from the reference, assessed from the aligned reads.
Masked consensus
A representative FASTA with selected changes applied and low-depth positions replaced by N.

A mismatch in one read is not enough to establish a variant. Depth, base quality, mapping and allele support all affect interpretation. A single consensus also does not fully describe mixtures or minor variants.

Step 02 · Start in a new folder

Prepare the Part 9 inputs.

Open Ubuntu/WSL2. If you completed Parts 7 and 8, BCFtools is the only new tool:

Ubuntu terminal · Install BCFtools
sudo apt update
sudo apt install bcftools -y
Starting here without the earlier tools?

Install all three tools before continuing:

Ubuntu terminal · Install all required tools
sudo apt update
sudo apt install minimap2 samtools bcftools -y

Create the working folder and open it in Windows Explorer:

Ubuntu terminal · Create the Part 9 folder
mkdir -p ~/bioinformatics_training/part9
cd ~/bioinformatics_training/part9
explorer.exe .

Copy all five extracted practice files into this folder. Return to Ubuntu and check the inputs and software versions:

Ubuntu terminal · Check inputs and versions
ls -lh reference.fasta reads_part9.fastq.gz
minimap2 --version
samtools --version
bcftools --version

Keep working in the Part 9 folder for every command below. The dataset contains 300 synthetic single-end reads, each 100 bases long.

Step 03 · Use the updated reads

Make a fresh alignment.

Ubuntu terminal · Map, sort and index
samtools faidx reference.fasta
minimap2 -ax sr reference.fasta reads_part9.fastq.gz > mapped.sam
samtools sort -o mapped.sorted.bam mapped.sam
samtools index mapped.sorted.bam

This recreates the alignment using the new FASTQ. The reference FASTA index, sorted BAM and BAM index are inputs to the following steps.

Step 04 · Examine differences

Call candidate variants.

Ubuntu terminal · Call and index candidate variants
bcftools mpileup -Ou -f reference.fasta mapped.sorted.bam > likelihoods.bcf
bcftools call -mv --ploidy 1 -Oz -o variants.vcf.gz likelihoods.bcf
bcftools index variants.vcf.gz

mpileup calculates genotype likelihoods from the aligned reads. call -mv uses the multiallelic caller and writes variant sites. --ploidy 1 makes this an introductory haploid example; it is not a complete model of a mixed population.

The synthetic design target is position 877, G→A. Inspect the actual VCF rather than assuming a call was made.

Step 05 · Apply the example filter

Keep calls meeting the site-quality cutoff.

Ubuntu terminal · Filter and index variants
bcftools filter -i 'QUAL>=30' -Oz -o passing.vcf.gz variants.vcf.gz
bcftools index passing.vcf.gz

Base quality describes confidence in an individual read base. Variant QUAL is a site-level variant-call quality measure. They are different measurements.

The QUAL cutoff here is a teaching example. A high QUAL alone does not rule out systematic error, mapping bias or other artifacts; real interpretation needs the appropriate read-level evidence and validated criteria.

Step 06 · Keep uncertainty visible

Mark every position below 10×.

Ubuntu terminal · Measure depth and write the BED mask
samtools depth -aa mapped.sorted.bam > depth.tsv
awk -v OFS='\t' '$3<10 {print $1,$2-1,$2}' depth.tsv > lowcov.bed

-aa keeps zero-depth positions, including the designed gap. The depth table is one-based; BED uses zero-based, end-exclusive intervals. Reference position 1051 therefore becomes the BED interval 1050 1051, with the fields separated by tabs.

The AWK command writes one one-base interval for each position below the threshold. The mask includes the entire 1051–1400 gap and other low-depth positions.

10× is a teaching choice.

This cutoff illustrates masking. It is not a universal standard for confident base calls, and the reads and bases counted depend on alignment and filtering settings.

Step 07 · Make the FASTA

Apply the changes and the mask.

Ubuntu terminal · Create the masked consensus
bcftools consensus -f reference.fasta -m lowcov.bed passing.vcf.gz > consensus.fa

BCFtools applies the listed alternate alleles to the reference and replaces the masked positions with N.

Reference bases can remain by default.

At unmasked sites without an alternate call, BCFtools keeps the reference base. That does not prove the base was directly observed. The consensus command applies a VCF; it does not independently evaluate the original read evidence.

Step 08 · Inspect before sharing

Check the variant and the sequence.

Ubuntu terminal · Inspect the outputs
bcftools view -H passing.vcf.gz | head
samtools faidx consensus.fa
grep -v '^>' consensus.fa | tr -d '\n' | wc -c
grep -v '^>' consensus.fa | tr -cd 'N' | wc -c

The final two commands count the consensus length and its N characters. The FASTA header and line breaks are excluded.

Results from the supplied practice files.

The optional script and manual workflow were checked in Ubuntu 22.04 with minimap2 2.24-r1122, SAMtools 1.13 and BCFtools 1.13.

Measured results for the supplied synthetic reads.
CheckTested resultInterpretation
Passing variant877: G→AOne variant passed the example QUAL filter.
Variant-site depth26 readsThe passing VCF records DP=26.
Reference positions2,400The depth table includes every reference position.
Positions at ≥10×1,599These positions are outside the low-depth mask.
Zero-depth gap350 bases: 1051–1400Every position in the designed gap is N.
Consensus length2,400 basesThis substitution-only example retains reference length.
Masked positions801 NsThe gap plus 451 other positions below 10×.

On a small screen, scroll the table sideways.

The consensus has A at position 877. All positions below 10× are N, and no other unmasked changes were introduced in the tested output. Results can differ with tool versions, alignments or filter settings; keep the dataset design notes separate from your measured results.

Prefer the optional complete script?

With the practice files in your Part 9 folder, run:

Ubuntu terminal · Run the complete exercise
bash run_part9.sh

The script checks that the required tools and inputs exist, then performs the mapping, calling, masking and output checks. It prints the passing variant, coverage totals, consensus length and N count.

Use a dedicated exercise folder. Running it again replaces the generated alignment, variant and consensus files; the supplied reference and reads are retained.

Ten-minute challenge

Explain the sequence you produced.

  1. Why can a high mapping rate coexist with a region without coverage?
  2. What evidence supports the introduced G→A change?
  3. Why are there more than 350 N characters in the output?
  4. Does this single consensus describe every allele in a mixed population?
Check your reasoning

The mapping rate describes reads, not coverage at each position. The passing VCF records the introduced variant and its supporting evidence. Masking all positions below 10× includes the zero-depth gap and other low-depth regions, giving 801 Ns in this test. A single haploid consensus does not fully represent minor variants or mixtures.

If you get stuck

The intended variant is missing.

Confirm that you used reads_part9.fastq.gz and recreated the BAM in the Part 9 folder. The old Parts 7/8 reads do not contain the demonstration change. Inspect the candidate and passing VCFs and record your tool versions.

My output has fewer or more masked positions.

Check the input BAM, the samtools depth -aa command and the $3<10 mask condition. Changing the threshold or the alignment and quality filters can change which positions become N.

A command or input file cannot be found.

Use pwd and ls -lh to confirm the folder and filenames. Follow the installation step if a required tool is missing.

Include the command, its error and the software versions when asking for help. Share it in the Part 9 LinkedIn discussion, contact me, or return to the series page.

Official references: BCFtools variant calling, consensus sequences, BCFtools command manual and SAMtools depth.

Coming next · Part 10

How do we compare consensus genomes?

Multiple-sequence alignment: put representative sequences alongside one another before exploring their relationships.