alignment-files/alignment-amplicon-clipping/SKILL.md
Trim PCR primers from aligned reads in amplicon-panel BAMs using samtools ampliconclip. Use when processing SARS-CoV-2 ARTIC, hereditary cancer panels, ctDNA hot-spot panels, or any amplicon assay where primer-derived bases would falsely confirm reference at primer footprints.
npx skillsauth add GPTomics/bioSkills bio-alignment-amplicon-clippingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Reference examples tested with: samtools 1.19+, pysam 0.22+
Before using code patterns, verify installed versions match. If versions differ:
pip show <package> then help(module.function) to check signatures<tool> --version then <tool> --help to confirm flagsIf code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
"Trim primer-derived bases from amplicon BAM" -> Soft- or hard-clip the 5' primer footprint after alignment using a primer BED, then repair fixmate/MD/NM tags.
samtools ampliconclip -b primers.bed input.bam -o clipped.bam (since samtools 1.11)iVar trim, BAMClipper, fgbio ClipBamAmplicon panels (SARS-CoV-2 ARTIC, hereditary cancer panels, ctDNA hot-spot panels, fusion panels, 16S rRNA) use designed PCR primers for enrichment. Primer-derived bases at read 5' ends do NOT reflect biological sequence -- they reflect the primer template. Without trimming:
Standard amplicon BAMs should NEVER be processed by samtools markdup -- by design every read at a primer location is a "duplicate" by coordinate. See duplicate-handling for the assay-aware decision.
| Tool | When | Notes |
|------|------|-------|
| samtools ampliconclip | Default for amplicon panels (since 1.11) | Soft- or hard-clip from BED; modifies CIGAR; invalidates MD/NM |
| iVar trim | Illumina SARS-CoV-2 / PrimalSeq route (Andersen lab) | Coordinates by primer name/position; soft-clips only + quality sliding-window |
| BAMClipper | Capture / hybrid panels with primer overlap | 5'-end clipping with overlap handling |
| fgbio ClipBam | When read-pair coordination matters | Soft/hard-clip with mate-aware end adjustment |
| cutadapt (pre-alignment) | Legacy / when alignment is downstream | Trims at FASTQ stage; less precise for amplicon |
Goal: Decide whether trimmed bases are kept in the BAM (reversible) or discarded (irreversible).
Approach: Soft-clip is the safe default; hard-clip only when archiving and disk is constrained.
| Mode | Flag | What it does | Reversible? |
|------|------|--------------|-------------|
| Soft-clip | (default) / --soft-clip | Bases kept in SEQ; CIGAR uses S; bases not aligned | Yes (CIGAR can be re-extended) |
| Hard-clip | --hard-clip | Bases removed from SEQ; CIGAR uses H | No (bases lost) |
Soft-clip is the recommended default. Hard-clip is irreversible -- once applied, the trimmed bases cannot be recovered for re-analysis with different primer coordinates.
Goal: Trim primers from a coordinate-sorted, indexed amplicon BAM and produce a downstream-ready BAM.
Approach: Run ampliconclip with primer BED, choosing either --strand (5' strand-aware) or --both-ends (read-through amplicons; note --both-ends overrides --strand), then re-fixmate (CIGAR changed) and re-calmd (MD/NM tags invalidated by clip).
# 1. Soft-clip primers (default; reversible). --strand clips only the designed strand.
samtools ampliconclip --strand --soft-clip \
-b primers.bed input.bam -o clipped.bam
# 2. Re-pair tags (CIGARs changed -- mate info needs refresh)
samtools sort -n clipped.bam | \
samtools fixmate -m - - | \
samtools sort -o sorted.bam -
# 3. Repair MD/NM tags (invalidated by clip; required by mpileup BAQ and IGV)
samtools calmd -b sorted.bam reference.fa > clipped_final.bam
samtools index clipped_final.bam
--strand clips primer bases only on the strand the primer is designed for. Without --strand, both strands are clipped at the primer site, removing valid biological sequence on the off-strand.
--both-ends allows clipping at both 5' and 3' positions of the read (some primers can appear at either end after alignment). Necessary for amplicon designs where reads can read through the entire amplicon. When --both-ends is set, --strand is ignored -- primer sites at both ends are clipped regardless of the BED strand column:
samtools ampliconclip --both-ends --soft-clip -b primers.bed input.bam -o clipped.bam
# tab-separated, 0-based half-open like all BED
chr1 100 125 primer_1_F +
chr1 500 525 primer_1_R -
chr1 600 625 primer_2_F +
chr1 1000 1025 primer_2_R -
Tools that consume the BED: column 1-3 (region), column 6 (strand) is required for --strand. ARTIC primer schemes ship pre-built BEDs (e.g., primer.bed from artic-network/primer-schemes).
| Tool | Approach | When |
|------|----------|------|
| samtools ampliconclip | Soft-clip from BED, post-alignment | General amplicon panels; modern ARTIC workflows |
| iVar trim | Soft-clip with primer-position parsing + quality trim | nf-core/viralrecon; Illumina PrimalSeq route (Andersen lab) |
Note: the ARTIC network's own nanopore field-bioinformatics pipeline (artic minion) trims primers with its align_trim tool, not iVar; iVar (Grubaugh et al. 2019, Genome Biol 20:8) is the Illumina/PrimalSeq route. Modern viral consensus pipelines tend to use ampliconclip then samtools consensus --config hiseq --ambig (Illumina preset) for IUPAC heterozygote handling. See reference-operations for consensus generation.
Clipping invalidates several tags and CIGAR-derived fields:
| Field | Impact | Repair |
|-------|--------|--------|
| CIGAR | New S or H operations added | Automatic from ampliconclip |
| MD:Z | Mismatch positions now wrong | samtools calmd -b in.bam ref.fa |
| NM:i | Edit distance recomputed | samtools calmd |
| TLEN | Template length changes when both mates clipped | samtools fixmate -m |
| ms, MC:Z | Mate score (lowercase per SAMtags) / mate CIGAR | samtools fixmate -m |
A clipped BAM that bypasses fixmate + calmd causes silent failures in bcftools mpileup BAQ (which depends on MD), IGV mismatch coloring, and any tool using NM for filtering.
Amplicon reads at primer locations are by design coordinate-degenerate -- every read mapped to the same amplicon shares the same start/end coordinates because they all come from the same primer pair. samtools markdup would mark essentially every read as a duplicate and erase the dataset. For amplicon panels:
fgbio GroupReadsByUmi -> CallMolecularConsensusReads instead of markdup. See duplicate-handling.| Error | Cause | Solution |
|-------|-------|----------|
| MD tag mismatch after clipping | calmd not run | Run samtools calmd -b clipped.bam ref.fa |
| Variant calls with strand bias at every amplicon end | Forgot --strand | Re-run with strand-aware clipping |
| Markdup output shows ~100% duplicates | Amplicon BAM was processed with markdup | Restart from raw alignment; use ampliconclip; skip markdup |
| Unexpected reference confirmation at primer-overlapping variants | ampliconclip not run | Run before variant calling |
| Task | Command |
|------|---------|
| Soft-clip primers (strand-aware) | samtools ampliconclip --strand -b primers.bed in.bam -o clipped.bam |
| Soft-clip primers (read-through amplicons) | samtools ampliconclip --both-ends -b primers.bed in.bam -o clipped.bam |
| Hard-clip (irreversible) | samtools ampliconclip --strand --hard-clip -b primers.bed in.bam -o clipped.bam |
| Repair MD/NM after clip | samtools calmd -b clipped.bam ref.fa > final.bam |
| Repair mate info | samtools sort -n - \| samtools fixmate -m - - \| samtools sort -o out.bam - |
-aa -A -d 600000 -B)tools
End-to-end CLIP-seq pipeline from FASTQ to ENCODE-compliant binding sites, single-nucleotide crosslink maps, annotation, motifs, and (optionally) differential binding. Use when running the full Yeo lab eCLIP / iCLIP / iCLIP2 / iCLIP3 / irCLIP / PAR-CLIP analysis with SMInput control, protocol-specific UMI extraction, ENCODE STAR parameters, CLIPper or Skipper peak calling with stringent log2 FC and -log10 p thresholds, IDR rescue and self-consistency QC, and downstream motif registration with mCross or PEKA.
development
Detect, date, and contextualize whole-genome duplication (WGD / paleopolyploidy) events using wgd v2 (Chen et al 2024), KsRates (Sensalari 2022 substitution-rate-corrected Ks dating), DupGen_finder (Qiao 2019), MAPS (Li 2018 phylogenomic), POInT (Conant 2008 ordered-block), SLEDGe (2024 ML-based), Whale.jl (Bayesian DL+WGD), and synteny-anchored paranome construction. Use when identifying ancient polyploidy from Ks distributions and synteny block analysis, positioning WGD events relative to speciation, distinguishing tandem from segmental from WGD duplications, dating the 2R/3R vertebrate / fish / salmonid WGDs, building paranome and Ks-age mixture models, applying KsRates substitution-rate correction across lineages, or testing alternative biased-fractionation / dosage-balance models post-WGD.
tools
Build whole-genome alignments using Progressive Cactus (Armstrong 2020 reference-free clade-level WGA), Minigraph-Cactus (Hickey 2024 pangenome-aware), LASTZ chain/net (UCSC pipeline), MUMmer4 (Marçais 2018 pairwise), minimap2 -x asm5/10/20 (Li 2018 fast pairwise), AnchorWave (Song 2022 WGD-aware), and Mauve / progressiveMauve (bacterial). Operates the HAL toolkit (Hickey 2013) for downstream extraction including halSynteny, halLiftover, halBranchMutations, and hal2maf. Use when constructing multi-species alignments for comparative-annotation projection (TOGA), synteny detection, conservation analyses (phyloP / PhastCons), or pangenome graph construction; selecting between reference-free (Cactus) and reference-anchored (LASTZ chains/nets) approaches; tuning sensitivity for closely vs distantly related genomes; or producing HAL files for genome-wide downstream tools.
development
Detect syntenic blocks and structural rearrangements between genomes using MCScanX (Wang 2012), JCVI/MCScan (Tang 2008 Python), GENESPACE (Lovell 2022) for orthology-anchored riparian visualization, SyRI for structural variation, AnchorWave for sequence-level synteny, i-ADHoRe 3.0 for highly diverged species, SynNet for synteny networks, and ntSynt for multi-genome macrosynteny. Use when identifying collinear gene blocks across species, distinguishing macrosynteny from microsynteny, detecting inversions/translocations/duplications, anchoring orthology in WGD lineages, producing publication riparian plots, computing synteny block age via Ks (cross-references whole-genome-duplication), or running synteny-aware ortholog inference in polyploids.