sequence-manipulation/reverse-complement/SKILL.md
Generate reverse complements and complements of DNA/RNA sequences using Biopython, including IUPAC ambiguity codes, gapped alignments, and minus-strand features. Use when working with the opposite strand, building reverse primers, normalizing strand orientation before alignment, or extracting a coding sequence from a minus-strand feature.
npx skillsauth add GPTomics/bioSkills bio-reverse-complementInstall 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: BioPython 1.83+
Before using code patterns, verify installed versions match. If versions differ:
pip show <package> then help(module.function) to check signaturesIf code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Generate complementary and reverse complementary sequences using Biopython.
"Get the reverse complement" -> Produce the 5'-to-3' sequence of the opposite strand.
seq.reverse_complement() (BioPython Seq)samtools faidx ref.fa region --reverse-complement (extracts and RCs a region)Never hand-roll the complement table. Biopython's reverse_complement() already encodes the full IUPAC mapping correctly, case-insensitively, and on minus-strand features it is applied for the analyst automatically by SeqFeature.extract(). Every silent corruption in this domain comes from reimplementing what Biopython already does right: swapping ambiguity codes, forgetting that S/W/N are self-complementary, complementing the wrong molecule type, or reverse-complementing a second time after extract() already did it. Reach for the library method; reach for a guard (molecule_type) before it; never reach for a custom dictionary.
from Bio.Seq import Seq
| Question | Method | Output strand/direction |
|----------|--------|-------------------------|
| Opposite strand, conventional 5'->3' | reverse_complement() | 5'->3' of the complementary strand (the usual answer) |
| Base-paired sequence, same direction | complement() | 3'->5' of the complementary strand |
| Opposite strand of RNA, keep U | reverse_complement_rna() | 5'->3', emits U |
| Complement of RNA, keep U | complement_rna() | 3'->5', emits U |
| Coding strand from template (or vice versa) | reverse_complement() | the other strand, 5'->3' |
| mRNA sequence from the coding strand | transcribe() (NOT a complement) | same strand, T->U |
Returns the reverse complement (5'->3' of the opposite strand). This is the most commonly used operation.
seq = Seq('ATGCGATCG')
rc = seq.reverse_complement() # Returns Seq('CGATCGCAT')
Returns the complement without reversing. Less common - gives the opposite strand still written in 3'->5' order.
seq = Seq('ATGCGATCG')
comp = seq.complement() # Returns Seq('TACGCTAGC')
For RNA, the dedicated methods emit U:
rna = Seq('AUGCGAUCG')
rna.reverse_complement_rna() # Returns Seq('CGAUCGCAU')
rna.complement_rna() # Returns Seq('UACGCUAGC')
reverse_complement() complements all 15 IUPAC codes plus X correctly. The mapping is non-obvious for ambiguity codes - this is exactly why hand-rolling corrupts silently.
| Code | Bases | Complement | | Code | Bases | Complement | |------|-------|------------|-|------|-------|------------| | A | A | T | | M | A/C | K | | T | T | A | | B | C/G/T | V | | G | G | C | | V | A/C/G | B | | C | C | G | | D | A/G/T | H | | R | A/G | Y | | H | A/C/T | D | | Y | C/T | R | | S | G/C | S (self) | | K | G/T | M | | W | A/T | W (self) | | | | | | N | any | N (self) |
S, W, N, and X are SELF-complementary. The pairs that get swapped wrong by hand are B<->V and D<->H. The table is built for upper and lower case, so complementation is case-insensitive (Seq('atRY').reverse_complement() works).
reverse_complement() runs in DNA mode: it treats any U as a T and EMITS T (docstring: "Any U in the sequence is treated as a T"). It does not raise and does not leave U.
Seq('ACGU').reverse_complement() # Returns Seq('ACGT') -- U mapped to A, emitted as T
Seq('ACGU').reverse_complement_rna() # Returns Seq('ACGU') -- stays RNA
transcribe() does NOT complement. It swaps T->U on the SAME strand. Confusing "complement the template" with "transcribe the coding strand" is silent corruption. True biological transcription from the template strand is template_dna.reverse_complement().transcribe().
complement and reverse_complement do NOT validate the alphabet (unlike translate()). A gap - is not a table key, so it passes through unchanged and reversal preserves gap columns - the desired behavior for aligned sequences. Any other non-table character (?, *) also passes through silently.
Seq('ATG-CGA--TY').reverse_complement() # Returns Seq('RA--TCG-CAT') -- gaps preserved, Y->R
Because there is no alphabet check, garbage in produces garbage out without a warning (see the protein trap below).
def show_dsdna(seq):
print(f"5'-{seq}-3'")
print(f" {'|' * len(seq)}")
print(f"3'-{seq.complement()}-5'")
show_dsdna(Seq('ATGCGATCG'))
def is_palindrome(seq):
return seq == seq.reverse_complement()
is_palindrome(Seq('GAATTC')) # True -- EcoRI site
is_palindrome(Seq('ATGCGA')) # False
Goal: Produce a new FASTA file with all sequences reverse-complemented.
Approach: Parse records as a stream, build new SeqRecords from .reverse_complement(), write to output.
Reference (BioPython 1.83+):
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
def reverse_complement_records(records):
for record in records:
yield SeqRecord(record.seq.reverse_complement(), id=record.id + '_rc', description=record.description + ' reverse complement')
records = SeqIO.parse('sequences.fasta', 'fasta')
SeqIO.write(reverse_complement_records(records), 'sequences_rc.fasta', 'fasta')
Goal: Get the correct 5'->3' coding sequence for a gene annotated on the minus strand.
Approach: Call feature.extract(parent.seq). For strand == -1, extract() ALREADY reverse-complements the slice and returns the coding sequence. Do NOT reverse-complement again.
Reference (BioPython 1.83+):
from Bio.Seq import Seq
from Bio.SeqFeature import SeqFeature, SimpleLocation
parent = Seq('AAATGGGCCCTTTAAA')
feature = SeqFeature(SimpleLocation(3, 12, strand=-1), type='CDS')
cds = feature.extract(parent) # Already reverse-complemented; this is the coding sequence
# cds.reverse_complement() # WRONG -- double-RC bug, valid-looking but wrong strand
Goal: Find a motif on both strands and report forward-strand coordinates.
Approach: Search the forward sequence, then search its reverse complement, mapping minus-strand hits back to forward coordinates.
Reference (BioPython 1.83+):
def search_both_strands(seq, motif):
motif = Seq(motif)
results = []
pos = seq.find(motif)
while pos != -1:
results.append(('+', pos))
pos = seq.find(motif, pos + 1)
rc = seq.reverse_complement()
pos = rc.find(motif)
while pos != -1:
results.append(('-', len(seq) - pos - len(motif)))
pos = rc.find(motif, pos + 1)
return results
search_both_strands(Seq('ATGCGAATTCGATGAATTCGATC'), 'GAATTC')
inplace defaults to False (standardized in 1.79). On an immutable Seq, inplace=True raises TypeError: Sequence is immutable (a loud, useful error). In-place mutation works only on MutableSeq.
from Bio.Seq import MutableSeq
m = MutableSeq('ATGC')
m.reverse_complement(inplace=True) # m is now MutableSeq('GCAT')
Since the 1.78 alphabet removal there is no molecule-type checking. Reverse-complementing a protein produces SILENT GARBAGE with no warning: residues that are also nucleotide codes get complemented (Seq('MAIVMGR').reverse_complement() -> Seq('YCKBITK'); M->K, V->B), while protein-only letters E, F, I, L, P, Q, Z and * pass through unchanged. The old IUPAC.protein ValueError guard is gone. Guard on the molecule type, not the Seq:
if record.annotations.get('molecule_type') not in ('DNA', 'RNA'):
raise ValueError('reverse_complement is only valid for nucleotide sequences')
| Symptom | Cause | Fix |
|---------|-------|-----|
| U replaced by T in result | reverse_complement() runs in DNA mode (U treated as T) | Use reverse_complement_rna() to keep RNA |
| Result is meaningless letters, no error | Reverse-complemented a protein (silent since 1.78) | Guard on molecule_type, not the Seq |
| Coding sequence is the wrong strand | Called .reverse_complement() after extract() on a minus-strand feature | extract() already RC'd it; do not RC again |
| TypeError: Sequence is immutable | inplace=True on a Seq | Use a MutableSeq, or take the returned value |
| Ambiguity codes complement wrongly | Hand-rolled complement table (B/V, D/H swapped; S/W/N not self-complementary) | Use Biopython's reverse_complement(); never reinvent the table |
| Same strand returned instead of complement | Used transcribe() thinking it complements | transcribe() only swaps T->U; use reverse_complement() for the other strand |
| TypeError on a plain string | Passed a str instead of a Seq | Wrap input in Seq() first |
Cornish-Bowden A (1985) "Nomenclature for incompletely specified bases in nucleic acid sequences: recommendations 1984." Nucleic Acids Res 13(9):3021-3030 (PMID 2582368). Defines the IUPAC ambiguity codes (R, Y, S, W, K, M, B, D, H, V, N) that Biopython's complement table implements.
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.