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.
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:
sudo apt update
sudo apt install bcftools -yStarting here without the earlier tools?
Install all three tools before continuing:
sudo apt update
sudo apt install minimap2 samtools bcftools -yCreate the working folder and open it in Windows Explorer:
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:
ls -lh reference.fasta reads_part9.fastq.gz
minimap2 --version
samtools --version
bcftools --versionKeep 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.
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.bamThis 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.
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.gzmpileup 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.
bcftools filter -i 'QUAL>=30' -Oz -o passing.vcf.gz variants.vcf.gz
bcftools index passing.vcf.gzBase 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×.
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.
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.
bcftools consensus -f reference.fasta -m lowcov.bed passing.vcf.gz > consensus.faBCFtools applies the listed alternate alleles to the reference and replaces the masked positions with N.
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.
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 -cThe 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.
| Check | Tested result | Interpretation |
|---|---|---|
| Passing variant | 877: G→A | One variant passed the example QUAL filter. |
| Variant-site depth | 26 reads | The passing VCF records DP=26. |
| Reference positions | 2,400 | The depth table includes every reference position. |
| Positions at ≥10× | 1,599 | These positions are outside the low-depth mask. |
| Zero-depth gap | 350 bases: 1051–1400 | Every position in the designed gap is N. |
| Consensus length | 2,400 bases | This substitution-only example retains reference length. |
| Masked positions | 801 Ns | The 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:
bash run_part9.shThe 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.
- Why can a high mapping rate coexist with a region without coverage?
- What evidence supports the introduced G→A change?
- Why are there more than 350 N characters in the output?
- 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.