Mark PCR duplicates with samtools markdup
Flag PCR duplicates in an aligned BAM using the four-step samtools collate, fixmate -m, sort, and markdup pipeline.
Prerequisites
- samtools 1.10 or newer installed (`samtools markdup` available)
- An aligned BAM file (e.g. sorted.bam)
1Group mates together and add the ms tag
markdup needs each read's mate beside it plus an ms (mate-score) tag, and neither exists in a plain coordinate-sorted BAM. Run collate to pull read pairs together, then fixmate with -m to write the ms and MC tags. Both commands finish silently on success.
samtools collate -@ 4 -o collated.bam sorted.bam
samtools fixmate -m collated.bam fixmate.bamExpected output
(collate and fixmate print nothing on success; collated.bam and fixmate.bam are written)2Coordinate-sort, then mark duplicates
markdup requires coordinate order, so sort the fixmate output before marking. Running markdup then flags duplicate read pairs in place using the 0x400 flag rather than deleting them, so the reads stay in the BAM for downstream tools to filter or keep.
samtools sort -@ 4 -o positionsort.bam fixmate.bam
samtools markdup positionsort.bam markdup.bamExpected output
[bam_sort_core] merging from 4 files and 4 in-memory blocks...3Verify the duplicate count
Confirm the run with flagstat and filter to the duplicates line. The count reports how many reads now carry the duplicate flag.
samtools flagstat markdup.bam | grep duplicatesExpected output
18342 + 0 duplicatesTroubleshooting
samtools markdup: error, no MC or ms tag
You skipped `samtools fixmate -m`. markdup relies on the MC and ms tags, so always collate first, then run fixmate with -m, before sorting and marking.
You want duplicates removed from the BAM, not just flagged
Add -r to markdup so it drops duplicate reads instead of tagging them: `samtools markdup -r positionsort.bam dedup.bam`.