BWA & GATK Preprocessing
Why preprocessing exists
Section titled “Why preprocessing exists”A variant caller trusts where the aligner put each read and how sure the sequencer was about each base, and preprocessing earns that trust in four steps. Alignment places each read on the reference. Sorting puts the reads in genome order, so a caller can walk the genome and jump straight to a region. Duplicate marking makes each original DNA fragment count once. Base quality score recalibration, BQSR for short, replaces the sequencer’s quality scores with scores measured against the data.
The GATK Best Practices for data preprocessing put these steps before any germline calling, and sarek runs them in this order.
HG002_R1.fastq.gz + HG002_R2.fastq.gz │ │ bwa mem -R '@RG...' align, and tag every read with its read group │ samtools sort order the reads by chromosome and position ▼HG002.sorted.bam │ │ gatk MarkDuplicates flag the extra copies of each fragment ▼HG002.md.bam │ │ gatk BaseRecalibrator model the quality errors, skipping known sites ▼HG002.recal.table │ │ gatk ApplyBQSR rewrite the base qualities ▼HG002.recal.bam ──► variant callersThe file names on this page are examples. Sarek uses the same pattern with CRAM in place of BAM, and the commands below use the argument forms of BWA 0.7.19 and GATK 4.7.0.0, the versions sarek 3.11.0 pins.
Preparing the reference
Section titled “Preparing the reference”BWA, samtools and GATK each want their own index of the reference FASTA. You build them once and keep them beside the FASTA, and sarek builds any that you do not supply.
# Cut one chromosome out of the full GATK referencesamtools faidx Homo_sapiens_assembly38.fasta chr20 > chr20.fasta
# The BWA index: chr20.fasta.amb, .ann, .bwt, .pac and .sabwa index chr20.fasta
# The FASTA index: chr20.fasta.faisamtools faidx chr20.fasta
# The sequence dictionary: chr20.dictgatk CreateSequenceDictionary --REFERENCE chr20.fasta
# A tabix index for each known sites VCFgatk IndexFeatureFile -I dbsnp.region.vcf.gzgatk IndexFeatureFile -I mills.region.vcf.gzThe sequence dictionary lists every contig with its length. GATK compares it with the header of every BAM and VCF it reads, and it stops when the contig names or lengths disagree. That check catches most reference mismatches before they reach the calls.
Alignment with BWA-MEM
Section titled “Alignment with BWA-MEM”BWA-MEM is the BWA algorithm for reads of 70 bases and longer. It seeds each read with exact matches to the reference, extends the seeds into a full alignment, and uses the expected insert size to place both reads of a pair. A read that spans two distant places in the reference, such as a structural variant breakpoint, comes out as a primary alignment and a supplementary one.
bwa mem \ -t 8 \ -K 100000000 \ -Y \ -R '@RG\tID:HG002.L001\tSM:HG002\tLB:HG002\tPL:ILLUMINA\tPU:L001' \ chr20.fasta \ HG002_R1.fastq.gz HG002_R2.fastq.gz \ | samtools sort -@ 4 -o HG002.sorted.bam -
samtools index HG002.sorted.bam| Flag | What it does |
|---|---|
-t 8 |
runs eight threads |
-K 100000000 |
processes 100 million input bases in each batch whatever the thread count |
-Y |
soft clips supplementary alignments instead of hard clipping them |
-R |
sets the read group header line and attaches its ID to every read |
The -K flag is there for reproducibility. The
BWA manual explains that the inferred
insert size distribution depends slightly on the batch size, which by default grows with
the thread count, so the same reads on a different number of threads can align a little
differently. The -Y flag keeps the full read sequence in every record of a split read.
Many older tutorials add -M, which marks shorter split hits as secondary, and sarek does
not pass it.
A read that aligns equally well to several places gets a mapping quality of 0, as the SAM and BAM page explains, and those reads are where the repeat problem shows up first.
Read groups
Section titled “Read groups”A read group is the set of reads from one library on one lane. GATK requires the read group fields and stops with an error when they are missing.
| Field | Meaning | What uses it |
|---|---|---|
ID |
the read group identifier; for Illumina data, usually the flowcell and lane | BQSR treats each read group as its own run, unless PU is set |
SM |
the sample | GATK groups reads by sample, and the VCF sample column takes this name |
LB |
the DNA library | MarkDuplicates, to decide which lanes might hold copies of the same fragments |
PL |
the platform, such as ILLUMINA |
the only record of the sequencing technology |
PU |
the platform unit: flowcell, lane and sample barcode | BQSR uses it in place of ID when it is present |
The values in the command above are placeholders. Sarek builds its read groups from the samplesheet, as the nf-core/sarek page shows. To see what an alignment file carries, read its header.
samtools view -H HG002.sorted.bam | grep '^@RG'The common mistake is a different SM on two lanes of the same sample. GATK then treats
one person as two samples, and every lane gets genotyped on its own share of the depth.
Sorting by coordinate
Section titled “Sorting by coordinate”BWA writes reads in the order of the FASTQ file. samtools sort orders them by
chromosome and position, and samtools index writes the .bai file that lets a tool
jump to a region without reading the whole file, as the
SAM and BAM page explains.
MarkDuplicates
takes either order, but the order changes what it marks. With input sorted by coordinate
it does not mark the unmapped mates of mapped reads or the secondary and supplementary
alignments. With input grouped by read name, those reads are tested and can be marked.
Sarek sorts by coordinate before duplicate marking. The exception is
--use_gatk_spark markduplicates, because MarkDuplicatesSpark runs efficiently only on
reads sorted by name.
Duplicate marking
Section titled “Duplicate marking”gatk MarkDuplicates \ --INPUT HG002.sorted.bam \ --OUTPUT HG002.md.bam \ --METRICS_FILE HG002.md.metrics.txt
samtools index HG002.md.bamMarkDuplicates compares the 5’ positions of reads and read pairs. Within each set that
starts at the same places, it keeps the read or pair with the highest sum of base
qualities and sets the duplicate flag, 0x400 or 1024 in decimal, on the rest. It does not
compare the sequences themselves. The duplicates stay in the file, and the callers skip
flagged reads by default. Sarek passes --REMOVE_DUPLICATES false to keep it that way.
The search for duplicates runs within a library, which is why LB matters. Two lanes
with the same LB are compared with each other, because the same library sequenced twice
can produce the same fragment twice. Reads from two different libraries are never
compared.
The metrics file has one row per library.
| Column | Meaning |
|---|---|
READ_PAIRS_EXAMINED |
mapped pairs the tool looked at |
READ_PAIR_DUPLICATES |
pairs marked as duplicates |
READ_PAIR_OPTICAL_DUPLICATES |
the duplicate pairs that look like optical duplicates |
PERCENT_DUPLICATION |
the fraction of mapped reads marked as duplicates |
ESTIMATED_LIBRARY_SIZE |
the estimated number of unique molecules in the library |
Sarek writes this file to reports/markduplicates/, and the
Coverage & QC page reads it. You can also count the flagged
reads directly.
# Reads that carry the duplicate flagsamtools view -c -f 1024 HG002.md.bam
# A summary that includes the duplicate countsamtools flagstat HG002.md.bamA PCR-free library like HG002’s skips the PCR step, but optical duplicates remain, so the step still runs.
Base quality score recalibration
Section titled “Base quality score recalibration”Every base in a FASTQ file carries a Phred quality, as the FASTQ page explains. The sequencer’s scores are estimates, and they are off in systematic ways that depend on the run, the position in the read and the neighboring bases. BaseRecalibrator measures the real error rate. It treats every mismatch to the reference as an error and tabulates the mismatch rate by covariate, and the default covariates are the read group, the reported quality score, the machine cycle and the nucleotide context.
Real variants also mismatch the reference, so the tool must skip them, and the known
sites files tell it where they are. Without them every true SNP in the sample would count
as a sequencing error and push the measured error rate up. Sarek’s own GATK.GRCh38
reference takes dbSNP and the Mills and 1000G gold standard indels from the GATK resource
bundle, and this section uses the region subsets of those two.
gatk BaseRecalibrator \ --input HG002.md.bam \ --reference chr20.fasta \ --known-sites dbsnp.region.vcf.gz \ --known-sites mills.region.vcf.gz \ --intervals region.bed \ --output HG002.recal.table
gatk ApplyBQSR \ --input HG002.md.bam \ --reference chr20.fasta \ --bqsr-recal-file HG002.recal.table \ --intervals region.bed \ --output HG002.recal.bamThe first pass writes the recalibration table, a GATK report. The second pass reads the
table and writes every base with its new quality. Sarek gives both tools the same
intervals, and it builds the known sites list from --dbsnp and --known_indels
together. Its output documentation notes
that recalibrated CRAM files are usually two to three times larger than the CRAM files
from duplicate marking.
BQSR is not always worth running. The
DeepVariant documentation
recommends against it, since BQSR gave a small decrease in DeepVariant’s accuracy.
Sarek’s documentation says to turn it off for reads with UMIs and for alignments made
with DragMap, and --skip_tools baserecalibrator does that. A region of one chromosome
also gives BaseRecalibrator far fewer bases to learn from than a whole genome, so each
cell of the table rests on fewer observations.
Where these steps fail
Section titled “Where these steps fail”- Contig names disagree between files. The GATK reference calls the chromosome
chr20, and a known sites file built on Ensembl names calls it20. GATK stops with an incompatible contigs error, which is the good outcome. - The known sites come from another build. GRCh37 coordinates on a GRCh38 alignment point at the wrong bases, and BQSR then masks the wrong positions.
- The read groups are missing. GATK refuses the file, and you add them with
gatk AddOrReplaceReadGroupsor realign with-R. - The input is not sorted.
samtools indexfails, and so does every tool that needs a region. - Reads come from sequence that the reference lacks. With chr20 alone as the reference, reads from similar sequence on other chromosomes meet no competing placement, so they can pass every step above with good mapping qualities.
Compare the contig names of the reference and the known sites before a run.
# Contig names in the reference dictionarygrep '^@SQ' chr20.dict | cut -f2
# Contig names in a known sites VCFtabix -l dbsnp.region.vcf.gzHow sarek runs these steps
Section titled “How sarek runs these steps”| Step | Sarek process | Where the result goes |
|---|---|---|
| Alignment and sorting | BWAMEM1_MEM |
kept in the work folder unless you set --save_mapped |
| Duplicate marking | GATK4_MARKDUPLICATES |
preprocessing/markduplicates/<sample>/<sample>.md.cram |
| Recalibration table | GATK4_BASERECALIBRATOR |
preprocessing/recal_table/<sample>/<sample>.recal.table |
| Recalibration | GATK4_APPLYBQSR |
preprocessing/recalibrated/<sample>/<sample>.recal.cram |
To see the exact command that sarek ran for any step, find the task’s hash in the
execution trace under pipeline_info/ and open the .command.sh file in the matching
folder under work/. That file is the source of truth for every flag on this page.
Next steps
Section titled “Next steps”The next page, Coverage & QC, reads the depth, duplicate and alignment reports that sarek wrote for this section’s run.
Scientific references
Section titled “Scientific references”Li and Durbin, 2009 describe BWA and its short read alignment with the Burrows-Wheeler transform.
McKenna and colleagues, 2010 describe the Genome Analysis Toolkit and its MapReduce design for the analysis of sequencing data.
Danecek and colleagues, 2021 describe the development and the current features of SAMtools and BCFtools.
Bonfield and colleagues, 2021 describe HTSlib, the C library behind bgzip and tabix.
Further reading
Section titled “Further reading”The BWA 0.7.19 manual defines read-group, supplementary-alignment, and batch-size options. These settings explain the reference alignment command.
The DeepVariant r1.10 input guidance discusses preprocessing and base-quality recalibration. Its recommendations differ from a blanket GATK preprocessing rule.