pku-yuangroup/openai4s/bio-alignment-files-duplicate-handling
Claude Code and agent skill by pku-yuangroup/openai4s
- community
Checking that the SKILL.md file is still in the repository.
Mark and remove PCR/optical duplicates using samtools fixmate and markdup. Use when preparing alignments for variant calling or when duplicate reads would bias analysis.
Install
npx vskill@latest install pku-yuangroup/openai4s/bio-alignment-files-duplicate-handlingSKILL.md
The same SKILL.md was published earlier as pku-yuangroup/openai4s/bio-alignment-files-duplicate-handling.
Version Compatibility
Reference examples tested with: picard 3.1+, pysam 0.22+, samtools 1.19+
Before using code patterns, verify installed versions match. If versions differ:
- Python:
pip show <package>thenhelp(module.function)to check signatures - CLI:
<tool> --versionthen<tool> --helpto confirm flags
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Duplicate Handling
"Remove PCR duplicates from my BAM file" -> Mark or remove duplicate reads using the fixmate-sort-markdup pipeline to prevent duplicate bias in variant calling.
- CLI:
samtools fixmate,samtools markdup(samtools) - Python:
pysam.fixmate(),pysam.markdup()(pysam)
Mark and remove PCR/optical duplicates using samtools.
Why Remove Duplicates?
PCR duplicates are identical copies of the same original molecule, created during library preparation. They inflate coverage, bias allele frequencies, and create false positive variant calls. Optical duplicates are flowcell-proximity artifacts: on unpatterned flowcells they arise when the imaging software splits one cluster into two adjacent calls; on patterned flowcells (NovaSeq, NovaSeq X, NextSeq 1000/2000, HiSeq X/4000) the dominant source is ExAmp (exclusion-amplification) "pad-hopping", where a library molecule re-seeds a nearby nanowell.
When to Mark Duplicates -- and When NOT To
Standard samtools markdup is the right tool for some assays and actively harmful for others. The decision is assay-driven:
| Assay | Standard markdup? | Recommended approach | |
|---|---|---|---|
| Germline WGS / WES (PCR or PCR-free) | YES | samtools markdup (PCR-free still has ~0.5% optical duplicates on patterned flowcells) | |
| Somatic tumor/normal (no UMI) | YES | Same | |
| Exome / target capture | YES (20-50% expected) | samtools markdup | |
| ChIP-seq | MARK, do not remove | Then use peak caller's auto-dup logic (macs3 --keep-dup auto) | |
| CUT&RUN / CUT&Tag | MARK, do not remove | Same | |
| ATAC-seq | YES, BEFORE Tn5 +4/-5 shift | Then shift coords for footprinting | |
| Bulk RNA-seq (no UMIs) | NO | Duplicates are biological at highly-expressed loci; removing them biases DE proportional to expression | |
| Bulk RNA-seq (with UMIs) | NO | umi_tools dedup | |
| scRNA (10x, STARsolo, drop-seq) | NO | umi_tools dedup with CB+UB tags, or rely on Cell Ranger UMI counts | |
| ctDNA / liquid biopsy / deep panel (UMI) | NO | fgbio GroupReadsByUmi -> CallDuplexConsensusReads | |
| Twist / IDT / Roche UMI capture | NO | fgbio or Picard UmiAwareMarkDuplicatesWithMateCigar | |
| Amplicon / hotspot panel (no UMI) | NO | Every read is a "duplicate" by coordinate; markdup erases the dataset. Use samtools ampliconclip instead -- see alignment-amplicon-clipping. | |
| Amplicon / hotspot panel (UMI) | NO | fgbio consensus | |
| Long-read native (ONT, PacBio HiFi unamplified) | NO | No PCR step; markdup is meaningless | |
| PacBio HiFi amplicon | YES (rare) | pbmarkdup | |
| Ancient DNA (aDNA) | YES + mapDamage | Run markdup, then mapDamage --rescale before variant calling | |
| Microbiome 16S/ITS | NO | Read counts encode community structure |
If the BAM came from 10x Cell Ranger / STARsolo and samtools markdup produces a 50-95% duplicate rate, it is the wrong tool, not a bug.
Tool Selection: markdup vs Picard vs UMI-aware
| Tool | Speed | Threading | Optical | UMI | Notes | |
|---|---|---|---|---|---|---|
samtools markdup | Fast | Yes | Yes (-d) | Limited (--barcode-tag exact-match) | Fast production choice (nf-core/sarek defaults to GATK MarkDuplicates) | |
picard MarkDuplicates | Slow | No | Yes | UmiAware variant (BETA, transcriptome bug) | GATK Best Practices reference | |
biobambam2 bammarkduplicates2 | Fastest | Yes | Yes | No | Sanger / 1KGP pipelines | |
samblaster | Streaming, fast | No | Optional | No | Pipe directly from aligner; no name sort | |
sambamba markdup | Fast | Yes | Yes | No | Less actively maintained | |
fgbio GroupReadsByUmi + CallMolecularConsensusReads | Fast | Yes | n/a | Best UMI tool | Graph-based; supports duplex | |
umi_tools dedup | Slow | No | n/a | Yes (mature) | Reference for scRNA / bulk UMI | |
pbmarkdup | Fast | Yes | n/a | n/a | PacBio HiFi amplicons only |
Picard UmiAwareMarkDuplicatesWithMateCigar is BETA and has known bugs on transcriptome-aligned BAMs (silently keeps duplicates). Avoid for RNA-seq UMIs.
Optical Distance Is Platform-Specific
samtools markdup default is -d 0, meaning optical-duplicate detection is disabled by default. Set explicitly per platform:
| Platform | -d value | Rationale | |
|---|---|---|---|
| HiSeq 2000/2500 (random) | 100 | Picard historic default | |
| HiSeq 3000/4000/X (patterned) | 2500 | Patterned tile size larger | |
| NovaSeq 6000 (patterned) | 2500 | Same as HiSeq X | |
| NovaSeq X (10B) | 2500 | Patterned; same starting point as NovaSeq 6000 | |
| NextSeq 1000/2000 (patterned) | 2500 | ExAmp duplicates span larger pixel distances | |
| MiSeq, NextSeq 500/550 | 100 | Smaller / unpatterned | |
| Element AVITI, MGI / DNBseq | Custom regex | Different read-name format -- supply via --read-coords |
Setting -d 2500 on a HiSeq run does no harm. Forgetting -d 2500 on NovaSeq systematically under-marks optical duplicates and overestimates library complexity.
Multi-Library Pooled Marking
Without --use-read-groups, multi-library BAMs systematically over-mark: independent molecules from different libraries with the same coordinates get wrongly flagged as PCR duplicates. With --use-read-groups, RG tags must also match for two reads to be a duplicate (verify availability with samtools markdup --help):
samtools --use-read-groups keys on RG ID; Picard's library-aware behavior keys on the LB tag (allowing dedup across multiple lanes of the same library). For multi-lane single-library BAMs, Picard MarkDuplicates with READ_NAME_REGEX is closer to canonical.
Duplicate Marking Workflow
Goal: Mark PCR/optical duplicates so they can be excluded from downstream variant calling and coverage analysis.
Approach: Name-sort, add mate tags with fixmate, coordinate-sort, then run markdup. The pipeline version avoids intermediate files.
Reference (samtools 1.19+):
Pipeline Version (Optimized)
This is ~30% faster than sort -n | fixmate | sort | markdup on typical 30x WGS.
Critical pitfall: samtools markdup requires ms (mate score, lowercase) and MC (mate CIGAR) tags from fixmate -m. A re-sort that loses aux tags via Python round-trip silently produces a markdup output that marks almost nothing. If duplicate counts look implausibly low, verify MC:Z: is present in the input to markdup.
samtools fixmate
Adds mate information required by markdup. Must be run on name-sorted BAM.
Basic Usage
Add Mate Score Tag (-m)
Multi-threaded
Remove Secondary/Unmapped
samtools markdup
Marks or removes duplicate alignments. Requires coordinate-sorted BAM with mate tags from fixmate.
Mark Duplicates (Keep in File)
Remove Duplicates
Output Statistics
Optical Duplicate Distance
Multi-threaded
Write Stats to File
Duplicate Statistics
Check Duplicate Rate
Count Duplicates
Percentage Duplicates
pysam Python Alternative
Full Pipeline
Check Duplicate Flag
Filter Out Duplicates
Production Tools, Not Hand-Rolled
For real BAMs, always use a production marker. A naive Python implementation keyed on (chrom, pos, strand) ignores 5' position correction for soft clips, ignores library/RG, treats optical = PCR, and mis-handles supplementary alignments. The result is silently wrong duplicate marks. Use samtools markdup, Picard, or fgbio depending on assay (see decision tables above).
Alternative: From Aligner
Some aligners can mark duplicates directly during streaming:
BWA-MEM2 with samblaster
Picard MarkDuplicates
UMI-Aware Deduplication
For UMI libraries (10x scRNA, ctDNA panels, Twist/IDT/Roche UMI capture), naive markdup destroys information. Use UMI-aware tools:
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/bioskills/bio-alignment-files-duplicate-handling/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Grok Build (Cursor)
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/pku-yuangroup/openai4s/bio-alignment-files-duplicate-handling)