Bio Duplicate Handling
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.
Unsigned, install at your own risk
UnverifiedThis skill has no cryptographic signature attached. We can't verify the contents match what the publisher intended.
Install this skill
No account required. Pick your tool, or use the command that detects it for you.
Run in your terminal. It detects your AI tool.
npx @skills-hub-ai/cli install openclaw-medical-bio-duplicate-handlingManual setup by platform
Claude Code
~/.claude/skills/openclaw-medical-bio-duplicate-handling/SKILL.md
Instructions
Duplicate Handling
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 artificially
- Bias allele frequencies
- Can create false positive variant calls
Optical duplicates are clusters read multiple times due to their proximity on the flowcell.
Duplicate Marking Workflow
The standard samtools workflow requires multiple steps:
# 1. Sort by name (required for fixmate)
samtools sort -n -o namesort.bam input.bam
# 2. Add mate information with fixmate
samtools fixmate -m namesort.bam fixmate.bam
# 3. Sort by coordinate (required for markdup)
samtools sort -o coordsort.bam fixmate.bam
# 4. Mark duplicates
samtools markdup coordsort.bam marked.bam
# 5. Index result
samtools index marked.bam
Pipeline Version
samtools sort -n input.bam | \
samtools fixmate -m - - | \
samtools sort - | \
samtools markdup - marked.bam
samtools index marked.bam
samtools fixmate
Adds mate information required by markdup. Must be run on name-sorted BAM.
Basic Usage
samtools fixmate namesorted.bam fixmate.bam
Add Mate Score Tag (-m)
# Required for markdup to work correctly
samtools fixmate -m namesorted.bam fixmate.bam
Multi-threaded
samtools fixmate -m -@ 4 namesorted.bam fixmate.bam
Remove Secondary/Unmapped
samtools fixmate -r -m namesorted.bam fixmate.bam
samtools markdup
Marks or removes duplicate alignments. Requires coordinate-sorted BAM with mate tags from fixmate.
Mark Duplicates (Keep in File)
samtools markdup input.bam marked.bam
Remove Duplicates
samtools markdup -r input.bam deduped.bam
Output Statistics
samtools markdup -s input.bam marked.bam 2> markdup_stats.txt
Optical Duplicate Distance
# Set pixel distance for optical duplicate detection (default: 100)
samtools markdup -d 2500 input.bam marked.bam
Multi-threaded
samtools markdup -@ 4 input.bam marked.bam
Write Stats to File
samtools markdup -f stats.txt input.bam marked.bam
Duplicate Statistics
Check Duplicate Rate
samtools flagstat marked.bam
# Look for "duplicates" line
Count Duplicates
# Count reads with duplicate flag
samtools view -c -f 1024 marked.bam
Percentage Duplicates
total=$(samtools view -c marked.bam)
dups=$(samtools view -c -f 1024 marked.bam)
echo "scale=2; $dups * 100 / $total" | bc
pysam Python Alternative
Full Pipeline
import pysam
# Sort by name
pysam.sort('-n', '-o', 'namesort.bam', 'input.bam')
# Fixmate
pysam.fixmate('-m', 'namesort.bam', 'fixmate.bam')
# Sort by coordinate
pysam.sort('-o', 'coordsort.bam', 'fixmate.bam')
# Mark duplicates
pysam.markdup('coordsort.bam', 'marked.bam')
# Index
pysam.index('marked.bam')
Check Duplicate Flag
import pysam
with pysam.AlignmentFile('marked.bam', 'rb') as bam:
total = 0
duplicates = 0
for read in bam:
total += 1
if read.is_duplicate:
duplicates += 1
print(f'Total: {total}')
print(f'Duplicates: {duplicates}')
print(f'Rate: {duplicates/total*100:.2f}%')
Filter Out Duplicates
import pysam
with pysam.AlignmentFile('marked.bam', 'rb') as infile:
with pysam.AlignmentFile('nodup.bam', 'wb', header=infile.header) as outfile:
for read in infile:
if not read.is_duplicate:
outfile.write(read)
Mark Duplicates Manually (Simple Case)
import pysam
from collections import defaultdict
def simple_markdup(input_bam, output_bam):
seen = defaultdict(set)
with pysam.AlignmentFile(input_bam, 'rb') as infile:
with pysam.AlignmentFile(output_bam, 'wb', header=infile.header) as outfile:
for read in infile:
if read.is_unmapped:
outfile.write(read)
continue
key = (read.reference_id, read.reference_start, read.is_reverse,
read.next_reference_id, read.next_reference_start)
if key in seen:
read.is_duplicate = True
else:
seen[key].add(read.query_name)
outfile.write(read)
simple_markdup('sorted.bam', 'marked.bam')
Alternative: From Aligner
Some aligners can mark duplicates directly:
BWA-MEM2 with samblaster
bwa-mem2 mem ref.fa R1.fq R2.fq | \
samblaster | \
samtools sort -o marked.bam
Using Picard (Alternative Tool)
java -jar picard.jar MarkDuplicates \
I=input.bam \
O=marked.bam \
M=metrics.txt
Quick Reference
| Task | Command |
|---|---|
| Full workflow | sort -n | fixmate -m | sort | markdup |
| Mark duplicates | samtools markdup in.bam out.bam |
| Remove duplicates | samtools markdup -r in.bam out.bam |
| Count duplicates | samtools view -c -f 1024 marked.bam |
| View non-duplicates | samtools view -F 1024 marked.bam |
| Get stats | samtools markdup -s in.bam out.bam |
Duplicate FLAG
| Flag | Value | Meaning |
|---|---|---|
| 0x400 | 1024 | PCR or optical duplicate |
Filter Commands
# View only duplicates
samtools view -f 1024 marked.bam
# View non-duplicates only
samtools view -F 1024 marked.bam
# Count non-duplicates
samtools view -c -F 1024 marked.bam
Common Errors
| Error | Cause | Solution |
|---|---|---|
mate not found | Input not name-sorted | Run samtools sort -n first |
no MC tag | fixmate not run with -m | Re-run fixmate with -m flag |
not coordinate sorted | Input to markdup not sorted | Run samtools sort after fixmate |
Related Skills
- alignment-sorting - Sort by name/coordinate for workflow
- alignment-filtering - Filter duplicates from output
- bam-statistics - Check duplicate rates with flagstat
- variant-calling - Duplicate marking before calling
Security
Reviews (0)
Related skills
Browse all →More from OpenClaw Medical
View source →More Build skills
Browse category →Frequently asked questions about bio-duplicate-handling
What does the bio-duplicate-handling skill do?
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. It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.
How do I install the bio-duplicate-handling skill?
Run `npx @skills-hub-ai/cli install openclaw-medical-bio-duplicate-handling` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/openclaw-medical-bio-duplicate-handling/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.
Which AI tools does bio-duplicate-handling work with?
bio-duplicate-handling runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.
Is the bio-duplicate-handling skill free?
Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.
How do I use bio-duplicate-handling after installing it?
In Claude Code, type `/openclaw-medical-bio-duplicate-handling` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.
Can I share the bio-duplicate-handling skill with my team?
Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.