xiaohongshu
xiaohongshu
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.
Full skill instructions
Mark and remove PCR/optical duplicates using samtools.
PCR duplicates are identical copies of the same original molecule, created during library preparation. They:
Optical duplicates are clusters read multiple times due to their proximity on the flowcell.
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
samtools sort -n input.bam | \
samtools fixmate -m - - | \
samtools sort - | \
samtools markdup - marked.bam
samtools index marked.bam
Adds mate information required by markdup. Must be run on name-sorted BAM.
samtools fixmate namesorted.bam fixmate.bam
# Required for markdup to work correctly
samtools fixmate -m namesorted.bam fixmate.bam
samtools fixmate -m -@ 4 namesorted.bam fixmate.bam
samtools fixmate -r -m namesorted.bam fixmate.bam
Marks or removes duplicate alignments. Requires coordinate-sorted BAM with mate tags from fixmate.
samtools markdup input.bam marked.bam
samtools markdup -r input.bam deduped.bam
samtools markdup -s input.bam marked.bam 2> markdup_stats.txt
# Set pixel distance for optical duplicate detection (default: 100)
samtools markdup -d 2500 input.bam marked.bam
samtools markdup -@ 4 input.bam marked.bam
samtools markdup -f stats.txt input.bam marked.bam
samtools flagstat marked.bam
# Look for "duplicates" line
# Count reads with duplicate flag
samtools view -c -f 1024 marked.bam
total=$(samtools view -c marked.bam)
dups=$(samtools view -c -f 1024 marked.bam)
echo "scale=2; $dups * 100 / $total" | bc
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')
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}%')
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)
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')
Some aligners can mark duplicates directly:
bwa-mem2 mem ref.fa R1.fq R2.fq | \
samblaster | \
samtools sort -o marked.bam
java -jar picard.jar MarkDuplicates \
I=input.bam \
O=marked.bam \
M=metrics.txt
| 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 |
| Flag | Value | Meaning |
|---|---|---|
| 0x400 | 1024 | PCR or optical duplicate |
# 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
| 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 |
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience