Skip to content

Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.

Add genomic-coordinates skill

Timothy Kassisce67928122a6
diff --git a/skills/genomic-coordinates/SKILL.md b/skills/genomic-coordinates/SKILL.md
new file mode 100644
index 00000000..3bbb844a
--- /dev/null
+++ b/skills/genomic-coordinates/SKILL.md
@@ -0,0 +1,189 @@
+---
+name: genomic-coordinates
+description: Convert genomic intervals between coordinate conventions, normalise and compare variant representations, and detect assembly or contig-naming mismatches before they corrupt an analysis. Use whenever coordinates cross a format, tool, or assembly boundary - converting between BED, GFF/GTF, VCF, SAM/BAM, WIG, PSL, genePred, Picard interval_list, or region strings; reconciling 0-based half-open with 1-based inclusive; left-aligning or trimming indels; checking whether two variant records describe the same change; mapping genomic to transcript, CDS, or protein positions; auditing a BED/GTF/VCF for convention violations; or diagnosing GRCh37 vs hg19 vs GRCh38 vs T2T, chr-prefix, and liftover problems. Triggers include "off by one", "0-based", "1-based", "half-open", "coordinate system", "left-align", "normalize variant", "bcftools norm", "chr prefix", "wrong genome build", "liftover", "REF mismatch", and "HGVS".
+license: MIT
+compatibility: Requires Python 3.11+. Scripts use only the standard library - no third-party packages and no network access. Variant normalisation needs a reference FASTA, and uses its .fai index when one is present.
+allowed-tools: Read Write Edit Bash
+metadata:
+ version: "1.0"
+ skill-author: K-Dense Inc.
+---
+
+# Genomic Coordinates
+
+## When to use
+
+Any time a coordinate crosses a boundary: between two file formats, between two
+tools, between two assemblies, or between the genome and a transcript.
+
+## The rule
+
+**A coordinate is three facts, not one: the number, the convention it is written
+in, and the assembly it was measured against.** Carry all three or the number is
+not interpretable.
+
+Coordinate errors are the quietest class of bug in genomics. An off-by-one BED
+file parses, sorts, and intersects without complaint. A GRCh37 VCF joined against
+a GRCh38 annotation returns rows. A right-shifted indel simply fails to match its
+entry in ClinVar, and the result is a variant reported as novel. Nothing raises
+an error; the answer is just wrong, and it is wrong in a direction that looks
+plausible.
+
+So: convert with the table, not from memory, and verify against the reference
+whenever a reference is available.
+
+## The two conversions
+
+```
+1-based inclusive -> 0-based half-open : start - 1, end
+0-based half-open -> 1-based inclusive : start + 1, end
+```
+
+The end coordinate never moves. If a conversion changed both numbers, it is wrong.
+
+## Which format is which
+
+| 0-based, half-open | 1-based, inclusive |
+| --- | --- |
+| BED, bedGraph, bigWig, narrowPeak | GFF3, GTF, VCF |
+| BAM/CRAM (binary POS) | SAM (text POS) |
+| PSL, genePred, refFlat | WIG, Picard interval_list |
+| MAF (UCSC multiple alignment) | MAF (TCGA mutation annotation) |
+| PyRanges, pybedtools | GRanges/IRanges, samtools & UCSC & Ensembl region strings |
+
+Both "MAF" formats exist, they mean different things, and they disagree. UCSC
+serves 0-based files through a 1-based browser box. `references/format-conventions.md`
+has the full table with per-format detail.
+
+```bash
+cd skills/genomic-coordinates/scripts
+
+python3 convert_coords.py --list # the table
+python3 convert_coords.py --from bed --to gff chr1 999 1000
+python3 convert_coords.py --from ucsc --to bed "chr7:5,530,601-5,530,625"
+python3 convert_coords.py --from granges --to pyranges --input regions.tsv
+```
+
+```
+contig input output length status detail
+chr7 chr7:5530601-5530625 5530600-5530625 25 ok
+```
+
+Zero-length BED features (`chromStart == chromEnd`, a legal insertion point) are
+reported as `unrepresentable` rather than converted to `end = start - 1`. Exit
+code is 1 when any interval is degenerate or invalid.
+
+## Variants are not intervals
+
+A VCF `POS` for an indel is the **anchor base** — the base *before* the event,
+itself unchanged. And the same change can be written many ways:
+`chr1:7:CAC:C`, `chr1:3:CAC:C` and `chr1:2:GCA:G` are one deletion. Joining,
+deduplicating, or looking up variants before normalising loses real matches
+silently, and it loses them preferentially in repeats, where indels concentrate.
+
+Normalise — trim to parsimony, then left-align against the reference — before any
+comparison:
+
+```bash
+python3 normalize_variant.py --fasta ref.fa chr1 7 CAC C
+python3 normalize_variant.py --fasta ref.fa --split --input cohort.vcf
+python3 normalize_variant.py --fasta ref.fa --compare chr1:7:CAC:C chr1:2:GCA:G
+```
+
+```
+input normalized type pos_shift ref_check changed
+chr1:7:CAC:C chr1:2:GCA:G deletion 5 ok yes
+```
+
+Every record's `REF` is checked against the FASTA first. A `MISMATCH` means the
+variants and the reference are different assemblies — stop and run
+`check_contigs.py` rather than adjusting coordinates. Multi-allelic records must
+be split with `--split` **before** normalising, never after.
+
+HGVS shifts indels the opposite way, 3'-most along the transcript. For a
+minus-strand gene that is the opposite genomic direction from VCF's
+left-alignment. Details and the full procedure: `references/variant-representation.md`.
+
+## Check the assembly before trusting a join
+
+```bash
+python3 check_contigs.py --identify unknown.fa.fai
+python3 check_contigs.py variants.vcf annotation.gtf --genome GRCh38.fa.fai
+```
+
+```
+file kind contigs naming assembly detail
+ref.fa.fai sizes 25 plain GRCh37 24/24 primary chromosome lengths match;
+ chrM is 16569 bp, i.e. GRCh37/38 (rCRS MT)
+```
+
+The script reads `.fai`, `.chrom.sizes`, VCF headers, SAM headers, FASTA, BED,
+and GTF/GFF, identifies the assembly from primary-chromosome lengths, and reports
+every reason a join between two files would go wrong: naming mismatch, length
+conflict, coordinates past a contig end, contigs present in one file only. Exit
+code 1 on any incompatibility.
+
+**GRCh37 and hg19 differ only in the mitochondrion** — 16,569 bp (rCRS) versus
+16,571 bp. Nuclear coordinates are identical, so a mixed pipeline runs fine and
+only the mtDNA results are wrong. `check_contigs.py` reports which one it found.
+Builds, naming schemes, ALT contigs, and liftover pitfalls:
+`references/reference-builds.md`.
+
+## Audit a file against its own format
+
+```bash
+python3 audit_intervals.py peaks.bed
+python3 audit_intervals.py gencode.gtf --genome hg38.chrom.sizes
+python3 audit_intervals.py cohort.vcf --genome GRCh38.fa.fai
+```
+
+Looks for the evidence that a coordinate mistake leaves behind:
+
+| Finding | What it proves |
+| --- | --- |
+| `start_below_one` in GFF/GTF | 0-based data in a 1-based file; everything is one base left |
+| `many_zero_length` in BED | 1-based single-base features written into a 0-based file |
+| `past_contig_end` | wrong assembly, or an off-by-one at the contig edge |
+| `mixed_contig_naming` | any join will silently match one subset |
+| `first_block_offset` | BED12 `blockStarts` written as absolute coordinates |
+| `not_parsimonious` | untrimmed alleles; normalise before joining |
+| `bad_alt_allele` | Ensembl/VEP `-` notation in a VCF, which has no anchor base |
+
+Exit code 1 on any fatal finding, so it works as a CI gate on a data directory.
+
+## Transcript, CDS, and protein positions
+
+`c.742` and `chr17:7,674,220` are both "position", and neither converts to the
+other by arithmetic. Transcript coordinates count spliced bases in transcription
+order — decreasing genomic coordinate on the minus strand — and `c.1` is the `A`
+of the initiator `ATG`, not the start of the transcript.
+
+The rules that get mis-remembered: there is no `c.0`; 5' UTR positions are
+negative and 3' UTR positions take a `*`; GFF phase is the bases to *remove* to
+reach the next codon, not `start % 3`; and a `c.` description is meaningless
+without a versioned transcript accession, because the same variant numbers
+differently in each transcript. `references/transcript-coordinates.md` has the
+conversion procedure and the boundary cases.
+
+Do the conversion with a tool that holds the transcript model — VEP,
+`bcftools csq`, Mutalyzer, the `hgvs` package — not by hand.
+
+## Reporting results
+
+State the assembly next to the coordinates, every time.
+`chr7:5,530,601-5,530,625` is not a location; `chr7:5,530,601-5,530,625 (GRCh38)`
+is. Say which convention a coordinate column is in, in the column header or the
+file's documentation. When a conversion produced a result, say which direction it
+went.
+
+## References
+
+- `references/format-conventions.md` — every format's convention, with per-format
+ detail, BED12 block rules, region-string syntax, and tool behaviour.
+- `references/variant-representation.md` — VCF allele conventions, the
+ normalisation algorithm, equivalence checking, multi-allelic splitting, and how
+ HGVS disagrees with VCF.
+- `references/reference-builds.md` — build signatures, GRCh37 vs hg19, ALT
+ contigs, naming schemes, and liftover failure modes.
+- `references/transcript-coordinates.md` — genomic ↔ transcript ↔ CDS ↔ protein,
+ HGVS numbering, phase, and transcript choice.
diff --git a/skills/genomic-coordinates/references/format-conventions.md b/skills/genomic-coordinates/references/format-conventions.md
new file mode 100644
index 00000000..f6772a56
--- /dev/null
+++ b/skills/genomic-coordinates/references/format-conventions.md
@@ -0,0 +1,205 @@
+# Coordinate conventions, format by format
+
+Two independent choices define a convention, and formats mix them freely:
+
+- **Base**: is the first base of a contig called 0 or 1?
+- **Closure**: is the end coordinate part of the interval (inclusive) or one past
+ it (half-open)?
+
+There is no correlation between a format's age, its authorship, or its purpose and
+which pair it picked. UCSC alone ships both.
+
+## The table
+
+| Format | Convention | Length | Notes |
+| --- | --- | --- | --- |
+| BED (3/6/12) | 0-based half-open | `end - start` | `chromStart` may be 0 |
+| bedGraph | 0-based half-open | `end - start` | despite sitting next to WIG |
+| bigWig / bigBed | 0-based half-open | `end - start` | binary; matches BED |
+| narrowPeak / broadPeak | 0-based half-open | `end - start` | BED6+4 and BED6+3 |
+| WIG (fixedStep, variableStep) | 1-based inclusive | `end - start + 1` | the trap next to bedGraph |
+| GFF3 | 1-based inclusive | `end - start + 1` | `start <= end` always |
+| GTF / GFF2 | 1-based inclusive | `end - start + 1` | GENCODE, Ensembl |
+| VCF / BCF | 1-based inclusive | `len(REF)` | `POS` is the anchor, not the event |
+| SAM (text) | 1-based inclusive | from CIGAR | `POS` is the leftmost mapped base |
+| BAM / CRAM (binary) | 0-based | from CIGAR | the same field, decremented |
+| genePred / refFlat | 0-based half-open | `end - start` | `exonEnds` are exclusive |
+| PSL (BLAT) | 0-based half-open | `end - start` | see the minus-strand note below |
+| Picard interval_list | 1-based inclusive | `end - start + 1` | GATK targets, bait sets |
+| MAF — Mutation Annotation | 1-based inclusive | `End - Start + 1` | TCGA somatic calls |
+| MAF — Multiple Alignment | 0-based half-open | `size` field | UCSC whole-genome alignments |
+| samtools / tabix region string | 1-based inclusive | `end - start + 1` | `chr3:1000-2000` is 1001 bp |
+| UCSC browser position box | 1-based inclusive | `end - start + 1` | 1-based UI over 0-based files |
+| Ensembl REST region string | 1-based inclusive | `end - start + 1` | `chr:start..end:strand` |
+| IGV locus box | 1-based inclusive | `end - start + 1` | matches the UCSC box |
+| Bioconductor GRanges / IRanges | 1-based inclusive | `width()` | R ecosystem default |
+| PyRanges / pybedtools | 0-based half-open | `End - Start` | Python ecosystem default |
+
+`scripts/convert_coords.py --list` prints this table; `--from`/`--to` converts
+between any two rows of it.
+
+## The conversions worth memorising
+
+Only two, because everything else composes from them:
+
+```
+1-based inclusive -> 0-based half-open : start - 1, end
+0-based half-open -> 1-based inclusive : start + 1, end
+```
+
+The end coordinate never changes. Only the start moves, and only by one. A
+conversion that changed both numbers is wrong.
+
+## Per-format detail
+
+### BED
+
+`chromStart` is 0-based, `chromEnd` is exclusive. The first base of a chromosome
+is `0 1`. A single base at 1-based position 100 is `99 100`.
+
+`chromStart == chromEnd` is a **legal zero-length feature** — an insertion point
+between two bases, used by some variant tracks. It has no representation in any
+1-based inclusive format, which is why `convert_coords.py` reports it as
+`unrepresentable` rather than emitting `end = start - 1`.
+
+BED12 block fields have exact rules that hand-written files routinely break:
+
+- `blockStarts` are offsets **from `chromStart`**, not absolute coordinates.
+- `blockStarts[0]` must be `0`.
+- `chromStart + blockStarts[-1] + blockSizes[-1]` must equal `chromEnd`.
+- `blockCount` must equal the length of both lists.
+
+`thickStart`/`thickEnd` delimit the CDS and must lie within `chromStart`/`chromEnd`;
+`thickStart == thickEnd` marks a non-coding transcript.
+
+narrowPeak's tenth column, `peak`, is an offset **from `chromStart`**, or `-1` when
+no summit was called. Adding it to `chromStart` gives the summit; treating it as an
+absolute coordinate puts the summit on the wrong chromosome arm.
+
+### GFF3 and GTF
+
+Both are 1-based inclusive across nine tab-separated columns. `start <= end` is
+required **regardless of strand** — a minus-strand exon is still written with the
+smaller coordinate first, and orientation lives only in column 7. A GFF file with
+`start > end` is corrupt, not reverse-stranded.
+
+`start == 0` cannot occur in a valid file. When it does, the file holds BED-style
+coordinates and every feature is one base to the left of where it claims to be.
+
+Column 8 is **phase** in GFF3 and **frame** in GTF, and they mean the same thing:
+the number of bases to remove from the start of this feature to reach the first
+base of the next codon. Values are `0`, `1`, `2`, or `.`. It is not the reading
+frame of the feature's start position, and it is not `start % 3`. Every CDS
+feature must declare it.
+
+Attribute syntax differs and parsers key on it:
+
+```
+GFF3 ID=exon1;Parent=transcript1;gene_name=TP53
+GTF gene_id "ENSG00000141510"; transcript_id "ENST00000269305";
+```
+
+A `.gtf` file containing GFF3 attributes parses to zero attributes in most tools,
+silently.
+
+`exon_number` in GTF counts in **transcription order**, so on the minus strand
+exon 1 has the largest genomic coordinate. Sorting exons by coordinate and
+numbering them reproduces the right answer only on the plus strand.
+
+### VCF
+
+`POS` is 1-based and refers to the first base of `REF`. The interval a record
+occupies is `POS` to `POS + len(REF) - 1`.
+
+For indels, `POS` is the **anchor base**, which is the base *before* the event and
+is itself unchanged:
+
+```
+reference ... A C G T T T A ...
+positions 4 5 6 7 8 9 10
+
+deletion of TT at 8-9 POS=7 REF=GTT ALT=G
+insertion of AA after 7 POS=7 REF=G ALT=GAA
+SNV at 7 POS=7 REF=G ALT=T
+```
+
+So an indel's `POS` is not where the change is. Plotting VCF indels against a
+gene model without accounting for the anchor puts every one of them one base
+early. `-` is never a valid allele — that is Ensembl/VEP notation, which drops
+the anchor and uses a different coordinate for the same event.
+
+`POS = 0` and `POS = N+1` are reserved for telomere records and carry no real
+allele. `*` as an ALT marks a spanning deletion from an upstream record. `<DEL>`,
+`<DUP>` and friends are symbolic alleles whose extent lives in `INFO/END` and
+`INFO/SVLEN`, not in `REF`.
+
+Allele representation has its own reference: `variant-representation.md`.
+
+### SAM, BAM, CRAM
+
+SAM text `POS` is 1-based; the BAM and CRAM encodings of the same field are
+0-based. Any library that reads BAM presents one or the other, and they disagree:
+
+- `pysam`'s `AlignmentSegment.reference_start` is **0-based**.
+- `pysam`'s `.pos` is the same 0-based number.
+- The `POS` you see in `samtools view` output is **1-based**.
+
+`reference_end` in pysam is 0-based exclusive, and is `None` for unmapped reads.
+`pysam.AlignmentFile.fetch(contig, start, end)` takes **0-based half-open**
+coordinates, but `fetch(region="chr1:100-200")` takes a **1-based inclusive**
+region string. The same method, two conventions, chosen by which argument you pass.
+
+### Region strings
+
+`RNAME[:STARTPOS[-ENDPOS]]`, 1-based, both endpoints included, so `chr3:1000-2000`
+spans 1001 bases.
+
+Omitting the end does **not** mean a single base. `chr2:1000000` means position
+1,000,000 to the end of the chromosome. `scripts/convert_coords.py` refuses a
+region string without an explicit end rather than guessing which reading was meant.
+
+GRCh38 contig names can contain colons — `HLA-DRB1*12:17` is a real contig — so a
+region string is ambiguous without escaping. htslib resolves this with braces:
+
+```
+{HLA-DRB1*12:17} the whole contig
+{HLA-DRB1*12:17}:100-200 a region on it
+```
+
+Commas as thousands separators are accepted by htslib with
+`HTS_PARSE_THOUSANDS_SEP` and by the UCSC and IGV boxes, so `chr1:1,000,000-2,000,000`
+is valid input in most places and invalid in most file formats.
+
+### The UCSC split
+
+The UCSC Genome Browser displays and accepts 1-based inclusive coordinates in its
+position box, while the BED files it serves and consumes are 0-based half-open.
+Both are correct; they are different interfaces to the same data. A coordinate
+copied out of the browser window into a BED file is one base too far right.
+
+The UCSC Table Browser applies the same split per output format: BED output is
+0-based, "all fields from selected table" output of a genePred table is 0-based,
+and the position column shown in the browser is 1-based.
+
+### PSL
+
+0-based half-open, but for a minus-strand alignment `qStart` and `qEnd` are
+offsets into the **reverse-complemented** query, not the query as submitted. To
+get coordinates in the original query, use `qSize - qEnd` and `qSize - qStart`.
+`tStart`/`tEnd` are always on the forward target strand.
+
+## Tool behaviour
+
+`bedtools` reads each input in that input's own convention — BED as 0-based, GFF
+and VCF as 1-based — and converts internally. Output is BED-conventioned
+regardless of input. Mixing a GFF and a BED in one `intersect` is therefore
+correct; converting the GFF to BED coordinates first and then passing it as a GFF
+double-shifts it.
+
+`bedtools slop` and `flank` clip at contig ends only when given a `-g` genome
+file, and silently produce negative starts without one.
+
+R and Python disagree by default: `GenomicRanges` is 1-based inclusive,
+`PyRanges` is 0-based half-open. `rtracklayer::import()` converts BED to 1-based
+GRanges on read and back on write, so a round trip through R is safe — but
+building a GRanges by hand from numbers read out of a BED file is off by one.
diff --git a/skills/genomic-coordinates/references/reference-builds.md b/skills/genomic-coordinates/references/reference-builds.md
new file mode 100644
index 00000000..c941be80
--- /dev/null
+++ b/skills/genomic-coordinates/references/reference-builds.md
@@ -0,0 +1,154 @@
+# Reference builds, contig naming, and liftover
+
+A coordinate is meaningless without the assembly it was measured against. Two
+files can share contig names, share a coordinate range, join cleanly, and refer
+to different parts of the genome.
+
+All lengths below were read from the UCSC `bigZips` `chrom.sizes` for each
+assembly and cross-checked against the NCBI assembly report for GRCh37.p13,
+verified 2026-07-26. `scripts/check_contigs.py` carries the same table and
+matches files against it.
+
+## Discriminating lengths
+
+| Contig | GRCh37 / hg19 | GRCh38 / hg38 | T2T-CHM13v2.0 / hs1 |
+| --- | --- | --- | --- |
+| chr1 | 249,250,621 | 248,956,422 | 248,387,328 |
+| chr2 | 243,199,373 | 242,193,529 | 242,696,752 |
+| chrX | 155,270,560 | 156,040,895 | 154,259,566 |
+| chrY | 59,373,566 | 57,227,415 | 62,460,029 |
+| chrM / MT | 16,571 *(hg19)* / 16,569 *(GRCh37)* | 16,569 | 16,569 |
+
+```bash
+python3 check_contigs.py --identify unknown.fa.fai
+```
+
+## GRCh37 is not hg19
+
+They are the same assembly for every primary chromosome except the
+mitochondrion. UCSC's hg19 kept the older `NC_001807` sequence at **16,571 bp**;
+GRCh37 adopted the revised Cambridge Reference Sequence (rCRS, `NC_012920`) at
+**16,569 bp**. GRCh38 also uses rCRS, so chrM length distinguishes hg19 from
+everything else but does not distinguish GRCh37 from GRCh38.
+
+Consequences:
+
+- Every mitochondrial coordinate differs between an hg19 BAM and a GRCh37 VCF.
+ Nuclear coordinates are identical, so the pipeline runs and only mtDNA results
+ are wrong — which is the hardest kind of error to notice.
+- Mitochondrial heteroplasmy and haplogroup calls made against hg19 cannot be
+ compared to anything rCRS-based without re-calling.
+
+The two also differ in naming and in alternate-haplotype handling:
+
+| | GRCh37 (Ensembl/NCBI) | hg19 (UCSC) |
+| --- | --- | --- |
+| Autosomes | `1`, `2`, … | `chr1`, `chr2`, … |
+| Mitochondrion | `MT` (16,569) | `chrM` (16,571) |
+| Alt haplotypes | `GL000250.1`-style | 9 `chr6_cox_hap2`-style contigs |
+| Unplaced | `GL000191.1`-style | `chrUn_gl000191` |
+
+### The b37 family
+
+`b37` (Broad) is GRCh37 with plain naming and rCRS `MT`. `hs37d5` (1000 Genomes
+phase 2) is b37 plus a decoy contig (`hs37d5`) and the EBV genome. Primary
+coordinates are identical across all three, so they interconvert by renaming
+contigs — no liftover. Reads that map to the decoy in `hs37d5` will map somewhere
+in the primary assembly in b37, which changes coverage and variant calls in the
+affected regions even though the coordinate system did not move.
+
+## GRCh38 and its ALT contigs
+
+hg38 as UCSC ships it has 25 primary contigs, **261 `_alt`** contigs, 42
+`_random`, and 127 `chrUn_`. The ALT contigs are alternate representations of
+regions that are genuinely polymorphic — mostly MHC, and the HLA haplotypes.
+
+They break naive analysis in a specific way: a read from an ALT region can map
+equally well to the primary contig and to its ALT, so both alignments get
+`MAPQ 0` and every variant caller with a MAPQ filter drops the region entirely.
+Coverage plots show a hole where the MHC should be.
+
+The usual fixes:
+
+- **No-ALT analysis set** — the primary assembly with ALT contigs removed. The
+ simplest option and the right default unless you specifically want HLA typing.
+- **ALT-aware alignment** — `bwa-mem` with the `.alt` file and `bwa-postalt.js`,
+ which lifts ALT alignments back to the primary contigs.
+
+Analysis sets also hard-mask the pseudoautosomal regions on chrY, so that PAR
+reads map to chrX rather than splitting between the two. Contig *lengths* are
+unchanged by masking, so `check_contigs.py` still identifies a masked analysis
+set as GRCh38 — masking is invisible in the contig table and has to be checked
+by looking at the sequence.
+
+Patch releases (`GRCh38.p13`, `p14`) add `_fix` and new `_alt` contigs but never
+move a coordinate on a primary chromosome. A p13 coordinate is a p14 coordinate.
+
+## T2T-CHM13
+
+CHM13v2.0 is a genuinely different assembly, not a patch: every coordinate
+differs, and it adds sequence that has no GRCh38 coordinate at all (centromeric
+satellite arrays, acrocentric short arms). There is no clean liftover for the
+newly resolved regions, because there is nothing to lift them to. Most public
+annotation, most clinical variant databases, and most published coordinates are
+still GRCh38.
+
+## Contig naming
+
+Four naming schemes are in circulation for the same chromosome:
+
+```
+chr1 UCSC
+1 Ensembl, NCBI, GATK b37
+NC_000001.11 RefSeq accession (GRCh38); NC_000001.10 is GRCh37
+CM000663.2 GenBank accession (GRCh38); CM000663.1 is GRCh37
+```
+
+Note that the accession's version suffix, not the base accession, carries the
+build. `NC_000001.10` and `NC_000001.11` differ only in the last character and
+are different assemblies.
+
+Renaming is the fix, and `bcftools annotate --rename-chrs`, `samtools reheader`,
+and a two-column mapping file all do it. Two rules:
+
+- Rename the **smaller, cheaper** file, and rename it to match the reference —
+ never rename the reference.
+- `chrM` ↔ `MT` is a rename **only** between GRCh37 and GRCh38-family files. Between
+ hg19 and anything rCRS-based it is a lie, because the sequences differ.
+
+A join across naming schemes does not error. It returns the rows that happen to
+match — often zero, sometimes a misleading subset when one file is partly
+renamed. `check_contigs.py` reports the naming style of each file and refuses to
+call two files compatible when they disagree.
+
+## Liftover
+
+`liftOver` (UCSC, with a `.chain` file) and `CrossMap` (which also handles BAM,
+VCF, and BigWig) are the working tools. Both are approximate by nature:
+
+- **Coordinates can vanish.** A region deleted from the newer assembly has no
+ target. liftOver writes these to its unmapped file, which is easy to ignore and
+ should be counted every time.
+- **Mappings can be one-to-many.** A region duplicated in the target maps to
+ several places; taking the first is a silent choice.
+- **Strand can flip.** Inverted segments between builds mean a plus-strand
+ feature lifts to the minus strand. Interval files carry this fine; anything
+ where sequence orientation matters (primer sites, guide RNAs, motif hits) does
+ not.
+- **Interval endpoints can lift independently.** A long feature can lift to a
+ different length, or split.
+- **Variants need more than coordinates.** After lifting a VCF, `REF` may no
+ longer match the new reference, and if the segment inverted, `REF` and `ALT`
+ need reverse-complementing. `CrossMap vcf` handles this; a coordinate-only lift
+ does not. Always re-run `normalize_variant.py` against the *target* reference
+ afterwards and count the `MISMATCH` rows.
+
+Lifting twice — 37 → 38 → 37 — does not reliably return the original
+coordinates. When the original data can be re-processed against the target build,
+that is more accurate than any liftover.
+
+## A note on what to record
+
+Coordinates in a results table, a figure, or a supplementary file should say
+which build they are in, next to the numbers. "chr7:5,530,601-5,530,625" is not a
+location. "chr7:5,530,601-5,530,625 (GRCh38)" is.
diff --git a/skills/genomic-coordinates/references/transcript-coordinates.md b/skills/genomic-coordinates/references/transcript-coordinates.md
new file mode 100644
index 00000000..c59c09e0
--- /dev/null
+++ b/skills/genomic-coordinates/references/transcript-coordinates.md
@@ -0,0 +1,141 @@
+# Transcript, CDS, and protein coordinates
+
+Four coordinate spaces describe the same locus, and a position number is
+meaningless without saying which one it is in.
+
+| Space | Prefix | Origin | Counts |
+| --- | --- | --- | --- |
+| Genomic | `g.` | contig base 1 | every base, introns included |
+| Transcript | `n.` | transcript base 1 | spliced bases, UTRs included |
+| Coding | `c.` | the `A` of the initiator `ATG` | spliced coding bases |
+| Protein | `p.` | initiator methionine | residues |
+
+"Position 250" in a paper, a spreadsheet column, or a variant list is ambiguous
+between all four, and the four differ by hundreds of bases.
+
+## Genomic to transcript
+
+The transcript is the concatenation of its exons in **transcription order**.
+Introns are not numbered. On the minus strand, transcription order is decreasing
+genomic coordinate, and the transcript sequence is the reverse complement.
+
+Worked example, a two-exon minus-strand transcript on GRCh38:
+
+```
+exon 2: chr1:1,000-1,099 (100 bp) transcribed second
+exon 1: chr1:2,000-2,199 (200 bp) transcribed first
+```
+
+Transcript position 1 is genomic 2,199 — the *highest* coordinate. Positions
+1–200 walk down exon 1 to genomic 2,000; position 201 jumps to genomic 1,099;
+positions 201–300 walk down exon 2 to genomic 1,000.
+
+Converting a genomic position to a transcript position:
+
+1. Confirm the position falls inside an exon. If it does not, it is intronic and
+ has no plain transcript coordinate — see the intronic notation below.
+2. Sum the lengths of all exons before it in transcription order.
+3. Add its offset within its own exon, counted in transcription order:
+ `pos - exon_start + 1` on the plus strand, `exon_end - pos + 1` on the minus.
+
+Getting step 3's strand handling wrong is the single most common error here, and
+it fails silently: the number produced is a valid transcript coordinate, just the
+wrong one, mirrored within the exon.
+
+## Transcript to coding
+
+`c.1` is the first base of the initiator codon, not the first base of the
+transcript. If the 5' UTR is 150 bases long, transcript position 151 is `c.1`.
+
+HGVS coding numbering has no zero and uses four distinct forms:
+
+| Region | Notation | Example |
+| --- | --- | --- |
+| 5' UTR | negative, counting back from `c.1` | `c.-15` |
+| CDS | positive | `c.742` |
+| 3' UTR | `*`, counting from the base after the stop codon | `c.*23` |
+| Intron | nearest exonic base, then offset | `c.742+3`, `c.743-12` |
+
+Intronic offsets are relative to the nearest exon boundary: `+` counts forward
+from the last base of the preceding exon, `-` counts back from the first base of
+the following exon. Bases in the 5' half of an intron take the `+` form, those in
+the 3' half take the `-` form. `c.742+1` and `c.742+2` are the donor
+dinucleotide; `c.743-2` and `c.743-1` are the acceptor.
+
+There is no `c.0`. A tool that emits one has an off-by-one at the UTR boundary.
+
+## Coding to protein
+
+```
+codon = (c_pos - 1) // 3 + 1
+in_codon = (c_pos - 1) % 3 + 1 # 1, 2 or 3
+```
+
+`p.1` is the initiator methionine. `c.1`, `c.2` and `c.3` all map to `p.1`, so
+protein coordinates lose information — three different nucleotide variants share
+one protein position, and two of them may be synonymous.
+
+Note the asymmetry: `c.` → `p.` is a function; `p.` → `c.` is not. A protein
+position corresponds to three nucleotide positions, and a protein *change*
+usually corresponds to several possible nucleotide changes. Back-translating a
+`p.` description into a genomic coordinate requires the transcript sequence and
+still may be ambiguous. Never do it arithmetically.
+
+## Phase, and why it is not frame
+
+GFF3 column 8 (`phase`, called `frame` in GTF) is the number of bases to remove
+from the **start of this CDS feature** to reach the first base of the next codon.
+It takes the values 0, 1, and 2.
+
+It is not `start % 3`, and it is not a property of the genomic position. It is
+determined by how many coding bases precede this feature in the transcript:
+
+```
+phase = (3 - (coding_bases_before_this_CDS % 3)) % 3
+```
+
+The first CDS feature of a transcript has phase 0. On the minus strand, "start of
+the feature" means the end with the **higher** genomic coordinate, because that is
+where translation reaches first.
+
+Concatenating CDS features in genomic order and translating produces protein for
+plus-strand genes and nonsense for minus-strand genes. Sort in transcription
+order, reverse-complement, then translate.
+
+## Which transcript
+
+A gene has many transcripts and the same variant gets a different `c.` and `p.`
+in each. A `c.` description without a versioned transcript accession is not
+actionable.
+
+| Source | Default choice |
+| --- | --- |
+| MANE Select | one transcript per protein-coding gene, identical in RefSeq and Ensembl |
+| Ensembl canonical | MANE Select where one exists, otherwise Ensembl's own rule |
+| RefSeq Select | one per gene, not always the same as Ensembl canonical |
+| UCSC canonical | historically the longest CDS; now largely MANE-aligned |
+| VEP default output | **every** transcript, one consequence line each |
+
+MANE Select is the right default for anything clinical or cross-database, because
+it is the one choice where the RefSeq and Ensembl transcripts have identical
+sequence and identical exon coordinates.
+
+The version suffix matters. `ENST00000269305.9` and `ENST00000269305.8` can differ
+in UTR length, which shifts every `c.-` and `c.*` coordinate even though the CDS is
+unchanged. Record the version; a bare `ENST00000269305` is under-specified.
+
+## Two traps at boundaries
+
+**Exon edges.** A variant at the last base of an exon is exonic in one transcript
+and intronic in another whose exon is two bases shorter. Its consequence changes
+from missense to splice-region accordingly. This is a real disagreement between
+annotation sources, not a bug in either.
+
+**Indels near boundaries.** HGVS shifts indels 3'-most along the *transcript*;
+VCF left-aligns along the *genome*. For a minus-strand gene these run in opposite
+genomic directions, so a deletion can be intronic in its VCF representation and
+exonic in its HGVS one. See `variant-representation.md`.
+
+Both are reasons to convert with a tool that holds the transcript model — VEP,
+`bcftools csq`, Mutalyzer, or the `hgvs` Python package — rather than by
+arithmetic on exon coordinates.
diff --git a/skills/genomic-coordinates/references/variant-representation.md b/skills/genomic-coordinates/references/variant-representation.md
new file mode 100644
index 00000000..7ea25d93
--- /dev/null
+++ b/skills/genomic-coordinates/references/variant-representation.md
@@ -0,0 +1,155 @@
+# Variant representation and normalisation
+
+The same change to a genome can be written many ways. Two records that share no
+field values can describe one variant, and two records with identical `POS` can
+describe different ones. Any comparison, join, deduplication, or annotation
+lookup performed before normalisation loses real matches silently — nothing
+errors, the intersection is just smaller than it should be.
+
+## Why one variant has many spellings
+
+Take this reference:
+
+```
+position 1 2 3 4 5 6 7 8 9 10
+base G G C A C A C A C T
+```
+
+Deleting `AC` from the `CACACAC` run yields `GGCACACT` no matter which adjacent
+`AC` you remove. All of these are the same variant:
+
+```
+POS=7 REF=CAC ALT=C
+POS=5 REF=CAC ALT=C
+POS=3 REF=CAC ALT=C
+POS=2 REF=GCA ALT=G
+```
+
+Any caller may emit any of them. Repeat regions, which is where indels
+concentrate, are exactly where the ambiguity is worst.
+
+Redundant flanking bases add a second axis. `POS=3 REF=CA ALT=CT` and
+`POS=4 REF=A ALT=T` are the same SNV; the first just carries a base that does not
+change.
+
+## The normalisation rule
+
+A variant is normalised when it is **parsimonious** (as few bases as possible,
+while keeping at least one) and **left-aligned** (shifted as far towards the
+start of the contig as it can go without changing the sequence it describes).
+This is the definition from Tan, Abecasis & Kang, *Unified representation of
+genetic variants*, Bioinformatics 31(13):2202–2204, 2015, and it is what
+`bcftools norm` and `vt normalize` implement.
+
+The procedure:
+
+1. While the alleles all end with the same base: if any allele is down to one
+ base, extend every allele one base to the left using the reference and
+ decrement `POS`; then drop the last base of every allele.
+2. While every allele has at least two bases and they all start with the same
+ base: drop the first base of every allele and increment `POS`.
+
+Step 1 walks the variant left through a repeat. Step 2 strips redundant padding.
+Both terminate. `scripts/normalize_variant.py` implements exactly this:
+
+```bash
+python3 normalize_variant.py --fasta ref.fa chr1 7 CAC C
+# chr1:7:CAC:C -> chr1:2:GCA:G pos_shift 5
+```
+
+`pos_shift` is positive when left-alignment moved the anchor left through a
+repeat, negative when trimming moved it right onto a shorter, equivalent record.
+
+## Checking equivalence
+
+Normalise both and compare the four fields:
+
+```bash
+python3 normalize_variant.py --fasta ref.fa \
+ --compare chr1:7:CAC:C chr1:3:CAC:C chr1:2:GCA:G
+# verdict: identical -- all 3 records normalise to chr1:2:GCA:G
+```
+
+The verdict goes to stderr so the per-record table on stdout stays parseable.
+
+## Normalisation needs the right reference
+
+Left-alignment reads reference bases. Handed the wrong assembly it will produce a
+confident, wrong answer, so the `REF` field is checked against the FASTA first and
+a mismatch stops that record:
+
+```
+ref_check MISMATCH REF says A but the reference has C at chr1:3
+```
+
+A `REF` mismatch is the cheapest assembly-mismatch detector there is. If more
+than a handful of records fail, the variants and the FASTA are different builds —
+run `scripts/check_contigs.py` rather than adjusting anything.
+
+## Multi-allelic records
+
+`ALT=G,GG` is two variants sharing a line. They must be split **before**
+normalising, because the shared `REF` that made them representable together is
+not the parsimonious `REF` for either one:
+
+```bash
+python3 normalize_variant.py --fasta ref.fa --split --input cohort.vcf
+```
+
+Splitting after normalising, or normalising a multi-allelic record as a unit,
+gives records that are individually wrong. `bcftools norm -m -any -f ref.fa` does
+both in the right order. Note that splitting rewrites the genotype and `INFO`
+fields; per-allele `INFO` entries with `Number=A` are split alongside, and
+anything else is duplicated to both records.
+
+## The other direction: HGVS shifts right
+
+VCF left-aligns. HGVS does the opposite: *"in the case of ambiguity, the most 3'
+position possible of the reference sequence is arbitrarily assigned to have been
+changed."* The two standards are deliberately opposite, and the difference is
+real — the same deletion has different coordinates in a VCF and in a clinical
+report.
+
+Worse, HGVS's "3'" is relative to **the reference sequence being described**:
+
+| Description | Shifted towards | On a plus-strand gene | On a minus-strand gene |
+| --- | --- | --- | --- |
+| VCF `POS` | contig start | leftmost genomic | leftmost genomic |
+| HGVS `g.` | contig end | rightmost genomic | rightmost genomic |
+| HGVS `c.` / `n.` / `p.` | transcript 3' end | rightmost genomic | **leftmost** genomic |
+
+So for a minus-strand gene, an HGVS `c.` description and a left-aligned VCF
+record can coincide, and for a plus-strand gene they systematically will not.
+Never convert between the two by adjusting coordinates; round-trip through a
+tool that knows the transcript model (`bcftools csq`, VEP, Mutalyzer,
+`hgvs` in Python).
+
+## Symbolic and structural alleles
+
+`<DEL>`, `<DUP>`, `<INV>`, `<CNV>`, `<INS>` and breakend (`BND`) records carry no
+literal sequence. `REF` is the single anchor base at `POS`; the extent lives in
+`INFO/END` and `INFO/SVLEN`. They cannot be normalised, and
+`normalize_variant.py` passes them through with `ref_check = skipped` rather than
+pretending otherwise.
+
+`*` as an ALT allele means "this sample's allele is deleted by a different record
+overlapping this position". It is not a variant; counting `*` alleles as alternate
+observations inflates allele frequencies.
+
+## What to run before comparing two variant sets
+
+```bash
+# 1. same assembly, same contig naming?
+python3 check_contigs.py setA.vcf setB.vcf --genome ref.fa.fai
+
+# 2. structural conventions intact?
+python3 audit_intervals.py setA.vcf --genome ref.fa.fai
+
+# 3. split, check REF, trim, left-align -- both sets, same reference
+python3 normalize_variant.py --fasta ref.fa --split --input setA.vcf -o A.norm.tsv
+python3 normalize_variant.py --fasta ref.fa --split --input setB.vcf -o B.norm.tsv
+```
+
+Only then join on `CHROM:POS:REF:ALT`. An intersection computed before step 3 is
+an underestimate of unknown size, and it is biased: it under-counts indels in
+repeats, which is where most of the interesting ones are.
diff --git a/skills/genomic-coordinates/scripts/_common.py b/skills/genomic-coordinates/scripts/_common.py
new file mode 100644
index 00000000..b7e43db1
--- /dev/null
+++ b/skills/genomic-coordinates/scripts/_common.py
@@ -0,0 +1,335 @@
+"""Shared helpers for the genomic-coordinates scripts.
+
+Everything here is standard library only. The single organising idea is that all
+intervals are converted to one canonical form on the way in and back out again on
+the way out, so no script ever has to reason about two conventions at once.
+
+Canonical form: ``(start0, end_exclusive)`` -- 0-based, half-open, the same
+convention BED and Python slices use. ``end0 - start0`` is always the length, and
+a zero-length interval (an insertion point between two bases) is representable.
+"""
+
+from __future__ import annotations
+
+import json
+import re
+import sys
+from dataclasses import dataclass
+from pathlib import Path
+
+# --------------------------------------------------------------------------
+# coordinate conventions
+# --------------------------------------------------------------------------
+
+
+@dataclass(frozen=True)
+class Convention:
+ """How one file format or API writes an interval down."""
+
+ name: str
+ base: int # 0 or 1: what the first base of a contig is called
+ half_open: bool # True: end is exclusive. False: end is inclusive.
+ note: str = ""
+
+ @property
+ def label(self) -> str:
+ closure = "half-open" if self.half_open else "inclusive"
+ return f"{self.base}-based {closure}"
+
+
+# Every entry is sourced from the format's own specification; see
+# references/format-conventions.md for the citation behind each one.
+CONVENTIONS: dict[str, Convention] = {
+ "bed": Convention("bed", 0, True, "BED3/6/12, narrowPeak, broadPeak"),
+ "bedgraph": Convention("bedgraph", 0, True, "bedGraph, and bigWig internals"),
+ "gff": Convention("gff", 1, False, "GFF3"),
+ "gff3": Convention("gff3", 1, False, "GFF3"),
+ "gtf": Convention("gtf", 1, False, "GTF/GFF2, GENCODE, Ensembl"),
+ "vcf": Convention("vcf", 1, False, "POS..POS+len(REF)-1"),
+ "sam": Convention("sam", 1, False, "SAM text POS (BAM stores it 0-based)"),
+ "wig": Convention("wig", 1, False, "fixedStep/variableStep"),
+ "psl": Convention("psl", 0, True, "BLAT"),
+ "genepred": Convention("genepred", 0, True, "genePred, refFlat, UCSC tables"),
+ "interval-list": Convention("interval-list", 1, False, "Picard/GATK"),
+ "maf-tcga": Convention("maf-tcga", 1, False, "Mutation Annotation Format"),
+ "maf-ucsc": Convention("maf-ucsc", 0, True, "Multiple Alignment Format"),
+ "ucsc": Convention("ucsc", 1, False, "browser position box, region string"),
+ "ensembl": Convention("ensembl", 1, False, "REST region string"),
+ "samtools": Convention("samtools", 1, False, "samtools/tabix region string"),
+ "igv": Convention("igv", 1, False, "IGV locus box"),
+ "granges": Convention("granges", 1, False, "Bioconductor GRanges/IRanges"),
+ "pyranges": Convention("pyranges", 0, True, "PyRanges, pybedtools"),
+ "python": Convention("python", 0, True, "list/str slice semantics"),
+}
+
+# Formats whose canonical text form is "contig:start-end" rather than columns.
+REGION_STRING_FORMATS = {"ucsc", "ensembl", "samtools", "igv"}
+
+
+def get_convention(name: str) -> Convention:
+ key = name.strip().lower()
+ if key not in CONVENTIONS:
+ known = ", ".join(sorted(CONVENTIONS))
+ raise ValueError(f"unknown coordinate format {name!r}; known formats: {known}")
+ return CONVENTIONS[key]
+
+
+def to_canonical(conv: Convention, start: int, end: int) -> tuple[int, int]:
+ """Convert an interval written in ``conv`` to 0-based half-open."""
+ start0 = start - conv.base
+ end0 = end if conv.half_open else end - conv.base + 1
+ return start0, end0
+
+
+def from_canonical(conv: Convention, start0: int, end0: int) -> tuple[int, int]:
+ """Convert a 0-based half-open interval into ``conv``.
+
+ A zero-length interval yields ``end < start`` in every inclusive convention.
+ That is arithmetically correct and usually means the interval should not have
+ been converted at all; callers are expected to surface it rather than hide it.
+ """
+ start = start0 + conv.base
+ end = end0 if conv.half_open else end0 + conv.base - 1
+ return start, end
+
+
+# --------------------------------------------------------------------------
+# region strings
+# --------------------------------------------------------------------------
+
+# A contig name is either brace-quoted -- htslib's escape for GRCh38 names that
+# contain a colon, such as HLA-DRB1*12:17 -- or runs up to the first colon.
+_REGION_RE = re.compile(
+ r"^\s*(?:\{(?P<braced>[^}]+)\}|(?P<contig>[^\s:]+))"
+ r"(?::(?P<start>[\d,_]+)"
+ r"(?:\s*(?:-|\.\.)\s*(?P<end>[\d,_]+))?)?"
+ r"(?::(?P<strand>[-+.]))?\s*$"
+)
+
+
+def parse_region(text: str, conv: Convention) -> tuple[str, int, int, str | None]:
+ """Parse ``chr1:1,000-2,000`` into ``(contig, start, end, strand)``.
+
+ Numbers come back in the convention's own coordinates, unconverted.
+
+ Neither a bare contig nor a bare ``contig:start`` is accepted. Both mean
+ "to the end of the contig" in samtools, which cannot be converted without a
+ genome file, and inventing an end is exactly the class of bug this skill
+ exists to stop.
+ """
+ stripped = text.strip()
+ ambiguous = ValueError(
+ f"contig name in {text!r} is ambiguous: GRCh38 ALT contigs such as "
+ "HLA-DRB1*12:17 contain colons, so there is no way to tell the name from "
+ "the coordinates. Brace-quote it, as htslib does: {HLA-DRB1*12:17}:100-200"
+ )
+ # Checked before parsing: a '*' in an unbraced name means the split point
+ # cannot be inferred, whether or not the rest happens to parse.
+ if not stripped.startswith("{") and "*" in stripped:
+ raise ambiguous
+
+ m = _REGION_RE.match(text)
+ if not m:
+ if stripped.count(":") > 1:
+ raise ambiguous
+ raise ValueError(f"cannot parse region string {text!r}")
+ contig = m.group("braced") or m.group("contig")
+ if not m.group("braced"):
+ allowed = 2 if m.group("strand") else 1
+ if stripped.count(":") > allowed:
+ raise ambiguous
+ if m.group("start") is None:
+ raise ValueError(
+ f"region {text!r} names a whole contig; give contig:start-end"
+ )
+ start = int(m.group("start").replace(",", "").replace("_", ""))
+ if m.group("end") is None:
+ raise ValueError(
+ f"region {text!r} has a start but no end. In samtools and tabix that "
+ "means 'from here to the end of the contig', not a single base -- "
+ "write the end explicitly"
+ )
+ end = int(m.group("end").replace(",", "").replace("_", ""))
+ return contig, start, end, m.group("strand")
+
+
+def format_region(contig: str, start: int, end: int, strand: str | None = None) -> str:
+ """Render a region string, brace-quoting names that would otherwise be
+ ambiguous (GRCh38 HLA contigs contain colons)."""
+ name = f"{{{contig}}}" if ":" in contig else contig
+ text = f"{name}:{start}-{end}"
+ return f"{text}:{strand}" if strand else text
+
+
+# --------------------------------------------------------------------------
+# contig naming
+# --------------------------------------------------------------------------
+
+_ROMAN = re.compile(r"^(chr)?([IVXL]+)$", re.IGNORECASE)
+
+
+def naming_style(names: list[str]) -> str:
+ """Classify a set of contig names: how a join against another file will fail."""
+ if not names:
+ return "empty"
+ if any(re.match(r"^(NC|NT|NW|GL|KI|CM|GCA|GCF)_?\d", n) for n in names):
+ return "accession"
+ prefixed = sum(1 for n in names if n.startswith("chr"))
+ if prefixed == len(names):
+ return "chr-prefixed"
+ if prefixed == 0:
+ return "plain"
+ return "mixed"
+
+
+def strip_chr(name: str) -> str:
+ return name[3:] if name.startswith("chr") else name
+
+
+def canonical_contig(name: str) -> str:
+ """Fold ``chr1``/``1`` and ``chrM``/``MT``/``chrMT`` onto one key.
+
+ For comparing contig *sets* across files only. Never write the result into a
+ data file -- the naming style of the file you are writing has to be preserved.
+ """
+ bare = strip_chr(name).upper()
+ if bare in {"M", "MT"}:
+ return "M"
+ return bare
+
+
+# --------------------------------------------------------------------------
+# reference sequence
+# --------------------------------------------------------------------------
+
+
+class ReferenceError(Exception):
+ """The reference FASTA could not answer the question that was asked."""
+
+
+class Reference:
+ """Random access to a FASTA, through its ``.fai`` index when one exists.
+
+ With an index, only the bases actually asked for are read. Without one the
+ contig is read into memory on first use, which is fine for the small
+ references these scripts are usually pointed at and slow but correct for a
+ whole genome.
+ """
+
+ def __init__(self, path: str | Path) -> None:
+ self.path = Path(path)
+ if not self.path.exists():
+ raise ReferenceError(f"reference not found: {self.path}")
+ self._index: dict[str, tuple[int, int, int, int]] = {}
+ self._cache: dict[str, str] = {}
+ self._alias: dict[str, str] = {}
+ fai = Path(str(self.path) + ".fai")
+ if fai.exists():
+ self._load_fai(fai)
+ else:
+ self._load_all()
+ for name in list(self._index) + list(self._cache):
+ self._alias.setdefault(canonical_contig(name), name)
+
+ def _load_fai(self, fai: Path) -> None:
+ for line in fai.read_text().splitlines():
+ if not line.strip():
+ continue
+ parts = line.split("\t")
+ if len(parts) < 5:
+ raise ReferenceError(f"malformed .fai line: {line!r}")
+ name, length, offset, linebases, linewidth = parts[:5]
+ self._index[name] = (int(length), int(offset), int(linebases), int(linewidth))
+
+ def _load_all(self) -> None:
+ name = None
+ chunks: list[str] = []
+ with self.path.open() as handle:
+ for line in handle:
+ if line.startswith(">"):
+ if name is not None:
+ self._cache[name] = "".join(chunks)
+ name = line[1:].split()[0]
+ chunks = []
+ elif name is not None:
+ chunks.append(line.strip())
+ if name is not None:
+ self._cache[name] = "".join(chunks)
+
+ def _resolve(self, contig: str) -> str:
+ if contig in self._index or contig in self._cache:
+ return contig
+ alias = self._alias.get(canonical_contig(contig))
+ if alias is None:
+ available = sorted(set(self._index) | set(self._cache))
+ shown = ", ".join(available[:8]) + ("..." if len(available) > 8 else "")
+ raise ReferenceError(
+ f"contig {contig!r} is not in {self.path.name} (has: {shown}). "
+ "A missing contig usually means a chr-prefix mismatch or the wrong "
+ "assembly -- run check_contigs.py before going further."
+ )
+ return alias
+
+ @property
+ def contigs(self) -> dict[str, int]:
+ if self._index:
+ return {name: meta[0] for name, meta in self._index.items()}
+ return {name: len(seq) for name, seq in self._cache.items()}
+
+ def length(self, contig: str) -> int:
+ return self.contigs[self._resolve(contig)]
+
+ def fetch(self, contig: str, start0: int, end0: int) -> str:
+ """Return ``[start0, end0)`` in upper case. Out-of-range is an error."""
+ name = self._resolve(contig)
+ if start0 < 0:
+ raise ReferenceError(f"negative start {start0} on {contig}")
+ size = self.length(name)
+ if end0 > size:
+ raise ReferenceError(
+ f"{contig}:{start0}-{end0} runs past the end of {name} (length {size}); "
+ "the coordinates and this reference are not the same assembly"
+ )
+ if end0 <= start0:
+ return ""
+ if name in self._cache:
+ return self._cache[name][start0:end0].upper()
+ _, offset, linebases, linewidth = self._index[name]
+ with self.path.open("rb") as handle:
+ begin = offset + (start0 // linebases) * linewidth + (start0 % linebases)
+ stop = offset + (end0 // linebases) * linewidth + (end0 % linebases)
+ handle.seek(begin)
+ raw = handle.read(stop - begin)
+ return re.sub(rb"\s", b"", raw).decode("ascii").upper()
+
+
+# --------------------------------------------------------------------------
+# output
+# --------------------------------------------------------------------------
+
+
+def emit(rows: list[dict], columns: list[str], fmt: str, out: str | None) -> None:
+ """Write ``rows`` as TSV or JSON to a path or stdout."""
+ if fmt == "json":
+ text = json.dumps(rows, indent=2)
+ else:
+ lines = ["\t".join(columns)]
+ for row in rows:
+ lines.append("\t".join(str(row.get(col, "")) for col in columns))
+ text = "\n".join(lines)
+ if out:
+ Path(out).write_text(text + "\n")
+ else:
+ sys.stdout.write(text + "\n")
+
+
+def iter_data_lines(path: str | Path):
+ """Yield ``(lineno, line)`` for non-blank, non-comment lines of a text file."""
+ with Path(path).open() as handle:
+ for lineno, line in enumerate(handle, start=1):
+ stripped = line.rstrip("\n\r")
+ if not stripped.strip():
+ continue
+ if stripped.startswith(("#", "track ", "browser ", "@")):
+ continue
+ yield lineno, stripped
diff --git a/skills/genomic-coordinates/scripts/audit_intervals.py b/skills/genomic-coordinates/scripts/audit_intervals.py
new file mode 100644
index 00000000..6c266826
--- /dev/null
+++ b/skills/genomic-coordinates/scripts/audit_intervals.py
@@ -0,0 +1,511 @@
+#!/usr/bin/env python3
+"""Check an interval or variant file against its own format's conventions.
+
+A BED file holding 1-based coordinates parses cleanly, sorts cleanly, and
+intersects cleanly. Nothing downstream complains; every result is shifted by one
+base. These checks look for the evidence that survives that kind of mistake --
+coordinates a format cannot legally hold, features whose length contradicts
+their intent, positions past the end of the contig.
+
+ python3 audit_intervals.py peaks.bed
+ python3 audit_intervals.py gencode.gtf --genome hg38.chrom.sizes
+ python3 audit_intervals.py cohort.vcf --genome GRCh38.fa.fai
+
+Exit codes: 0 clean or warnings only, 1 at least one fatal finding,
+2 usage error.
+"""
+
+from __future__ import annotations
+
+import argparse
+import re
+import sys
+from collections import Counter
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from _common import canonical_contig, emit, naming_style # noqa: E402
+
+COLUMNS = ["severity", "rule", "line", "detail"]
+VALID_BASES = set("ACGTNacgtn")
+
+
+class Findings:
+ """Collected findings, capped per rule so one broken file cannot flood stdout."""
+
+ def __init__(self, max_examples: int) -> None:
+ self.rows: list[dict] = []
+ self.counts: Counter = Counter()
+ self.by_severity: Counter = Counter()
+ self.max_examples = max_examples
+
+ def add(self, severity: str, rule: str, line: int | str, detail: str) -> None:
+ self.counts[rule] += 1
+ self.by_severity[severity] += 1
+ if self.counts[rule] <= self.max_examples:
+ self.rows.append(
+ {"severity": severity, "rule": rule, "line": line, "detail": detail}
+ )
+ elif self.counts[rule] == self.max_examples + 1:
+ self.rows.append(
+ {
+ "severity": severity,
+ "rule": rule,
+ "line": "...",
+ "detail": "further occurrences suppressed (--max-examples)",
+ }
+ )
+
+ @property
+ def fatal(self) -> int:
+ """Every fatal occurrence, including those suppressed from the table."""
+ return self.by_severity["fatal"]
+
+
+def detect_format(path: Path) -> str:
+ suffixes = [s.lower() for s in path.suffixes]
+ for suffix, fmt in (
+ (".bed", "bed"), (".narrowpeak", "bed"), (".broadpeak", "bed"),
+ (".bedgraph", "bed"), (".gtf", "gtf"), (".gff3", "gff3"), (".gff", "gff3"),
+ (".vcf", "vcf"),
+ ):
+ if suffix in suffixes:
+ return fmt
+ raise SystemExit(
+ f"error: cannot infer the format of {path.name}; pass --format "
+ "bed|gtf|gff3|vcf"
+ )
+
+
+def load_genome(path: Path | None) -> dict[str, int]:
+ if path is None:
+ return {}
+ sizes = {}
+ for line in path.read_text().splitlines():
+ if not line.strip() or line.startswith("#"):
+ continue
+ parts = line.split("\t") if "\t" in line else line.split()
+ if len(parts) >= 2 and parts[1].isdigit():
+ sizes[canonical_contig(parts[0])] = int(parts[1])
+ return sizes
+
+
+def check_bounds(
+ find: Findings,
+ genome: dict[str, int],
+ lineno: int,
+ contig: str,
+ far: int,
+ source: str = "the genome file",
+) -> None:
+ """``far`` is the largest 1-based position the record touches."""
+ if not genome:
+ return
+ key = canonical_contig(contig)
+ if key not in genome:
+ find.add(
+ "fatal", "contig_not_in_genome", lineno,
+ f"{contig} is not in {source}, even ignoring the chr prefix",
+ )
+ elif far > genome[key]:
+ find.add(
+ "fatal", "past_contig_end", lineno,
+ f"{contig}:{far} is past the end of {contig} ({genome[key]} bp per "
+ f"{source}); wrong assembly, or an off-by-one",
+ )
+
+
+class SortState:
+ """Tracks the ordering rule that actually matters: no interleaved contigs,
+ ascending positions within each one."""
+
+ def __init__(self) -> None:
+ self.seen: set[str] = set()
+ self.current: str | None = None
+ self.position = -1
+
+ def check(self, find: Findings, lineno: int, contig: str, pos: int, why: str) -> None:
+ if contig != self.current:
+ if contig in self.seen:
+ find.add(
+ "warn", "interleaved_contigs", lineno,
+ f"{contig} reappears after {self.current}; records for one contig "
+ f"must be contiguous. {why}",
+ )
+ self.seen.add(contig)
+ self.current = contig
+ self.position = pos
+ return
+ if pos < self.position:
+ find.add(
+ "warn", "unsorted", lineno,
+ f"{contig}:{pos} follows {contig}:{self.position}. {why}",
+ )
+ self.position = pos
+
+
+# --------------------------------------------------------------------------
+# per-format checks
+# --------------------------------------------------------------------------
+
+
+def audit_bed(path: Path, find: Findings, genome: dict[str, int]) -> None:
+ contigs: list[str] = []
+ widths: Counter = Counter()
+ total = zero_length = starts_at_zero = 0
+ order = SortState()
+
+ for lineno, raw in enumerate(path.read_text().splitlines(), start=1):
+ if not raw.strip() or raw.startswith(("#", "track ", "browser ")):
+ continue
+ fields = raw.split("\t")
+ widths[len(fields)] += 1
+ if len(fields) < 3:
+ if len(raw.split()) >= 3:
+ find.add(
+ "fatal", "space_separated", lineno,
+ "fields are separated by spaces, not tabs. BED is tab-delimited; "
+ "a contig name containing a space would be unparseable, and some "
+ "readers take the whole line as one field",
+ )
+ else:
+ find.add(
+ "fatal", "too_few_columns", lineno,
+ "BED needs at least 3 columns",
+ )
+ continue
+ contig = fields[0]
+ contigs.append(contig)
+ try:
+ start, end = int(fields[1]), int(fields[2])
+ except ValueError:
+ find.add(
+ "fatal", "non_integer_coordinate", lineno,
+ f"chromStart/chromEnd are {fields[1]!r}/{fields[2]!r}",
+ )
+ continue
+ total += 1
+
+ if start < 0:
+ find.add("fatal", "negative_start", lineno, f"chromStart is {start}")
+ if end < start:
+ find.add(
+ "fatal", "end_before_start", lineno,
+ f"chromEnd {end} < chromStart {start}",
+ )
+ if start == 0:
+ starts_at_zero += 1
+ if end == start:
+ zero_length += 1
+
+ if len(fields) >= 6 and fields[5] not in {"+", "-", "."}:
+ find.add(
+ "fatal", "bad_strand", lineno,
+ f"strand column is {fields[5]!r}; BED allows only +, - or .",
+ )
+ if len(fields) >= 8:
+ try:
+ thick_start, thick_end = int(fields[6]), int(fields[7])
+ if thick_start < start or thick_end > end:
+ find.add(
+ "warn", "thick_outside_feature", lineno,
+ f"thickStart/thickEnd {thick_start}-{thick_end} fall outside "
+ f"the feature {start}-{end}",
+ )
+ except ValueError:
+ find.add("fatal", "non_integer_coordinate", lineno, "thickStart/thickEnd")
+ if len(fields) >= 12:
+ audit_bed12(find, lineno, fields, start, end)
+
+ order.check(
+ find, lineno, contig, start,
+ "bedtools and tabix assume sorted input and give wrong answers without it",
+ )
+ check_bounds(find, genome, lineno, contig, end)
+
+ if len(widths) > 1:
+ find.add(
+ "warn", "ragged_columns", "file",
+ "column count varies across the file ("
+ + ", ".join(f"{n} cols x{c}" for n, c in sorted(widths.items()))
+ + "); BED is positional, so parsers will misread the narrower rows",
+ )
+ if total and zero_length / total > 0.1:
+ find.add(
+ "warn", "many_zero_length", "file",
+ f"{zero_length}/{total} features have chromEnd == chromStart. In BED "
+ "that is a zero-length insertion point, not a single base. Single-base "
+ "features need chromEnd = chromStart + 1 -- this looks like 1-based "
+ "data written into a 0-based format",
+ )
+ if total >= 100 and starts_at_zero == 0:
+ find.add(
+ "info", "no_zero_start", "file",
+ f"no feature starts at 0 across {total} features. Weak on its own, but "
+ "consistent with 1-based coordinates; check one feature against the "
+ "reference sequence before trusting the file",
+ )
+ audit_naming(find, contigs)
+
+
+def audit_bed12(find: Findings, lineno: int, fields: list[str], start: int, end: int) -> None:
+ """The BED12 block rules are exact and routinely broken by hand-written files."""
+ try:
+ count = int(fields[9])
+ sizes = [int(v) for v in fields[10].rstrip(",").split(",") if v != ""]
+ offsets = [int(v) for v in fields[11].rstrip(",").split(",") if v != ""]
+ except (ValueError, IndexError):
+ find.add("fatal", "bad_block_fields", lineno, "blockCount/Sizes/Starts unparseable")
+ return
+ if not (count == len(sizes) == len(offsets)):
+ find.add(
+ "fatal", "block_count_mismatch", lineno,
+ f"blockCount {count} but {len(sizes)} sizes and {len(offsets)} starts",
+ )
+ return
+ if offsets and offsets[0] != 0:
+ find.add(
+ "fatal", "first_block_offset", lineno,
+ f"blockStarts[0] is {offsets[0]}; it must be 0, because block starts are "
+ "offsets from chromStart, not absolute coordinates",
+ )
+ if offsets and start + offsets[-1] + sizes[-1] != end:
+ find.add(
+ "fatal", "last_block_end", lineno,
+ f"chromStart + last blockStart + last blockSize = "
+ f"{start + offsets[-1] + sizes[-1]}, but chromEnd is {end}; the spec "
+ "requires them to be equal",
+ )
+
+
+def audit_gff(path: Path, find: Findings, genome: dict[str, int], flavour: str) -> None:
+ contigs: list[str] = []
+ gtf_attrs = gff3_attrs = 0
+
+ for lineno, raw in enumerate(path.read_text().splitlines(), start=1):
+ if not raw.strip() or raw.startswith("#"):
+ continue
+ fields = raw.split("\t")
+ if len(fields) != 9:
+ find.add(
+ "fatal", "wrong_column_count", lineno,
+ f"{len(fields)} columns; GFF/GTF is exactly 9, tab separated",
+ )
+ continue
+ contig, _, feature, start_s, end_s, _, strand, phase, attrs = fields
+ contigs.append(contig)
+ try:
+ start, end = int(start_s), int(end_s)
+ except ValueError:
+ find.add("fatal", "non_integer_coordinate", lineno, f"{start_s!r}/{end_s!r}")
+ continue
+
+ if start < 1:
+ find.add(
+ "fatal", "start_below_one", lineno,
+ f"start is {start}. GFF/GTF is 1-based, so 0 cannot occur -- this is "
+ "BED-style 0-based data in a 1-based file, and every feature is "
+ "shifted one base left",
+ )
+ if end < start:
+ find.add(
+ "fatal", "end_before_start", lineno,
+ f"end {end} < start {start}. GFF/GTF always writes start <= end, "
+ "including on the minus strand; strand lives in column 7",
+ )
+ if strand not in {"+", "-", ".", "?"}:
+ find.add("fatal", "bad_strand", lineno, f"strand column is {strand!r}")
+ if phase not in {"0", "1", "2", "."}:
+ find.add(
+ "fatal", "bad_phase", lineno,
+ f"phase column is {phase!r}; allowed values are 0, 1, 2 and .",
+ )
+ if feature == "CDS" and phase == ".":
+ find.add(
+ "warn", "cds_without_phase", lineno,
+ "CDS features must declare phase; without it the reading frame is "
+ "undefined and translation will be wrong",
+ )
+ if "=" in attrs and '"' not in attrs:
+ gff3_attrs += 1
+ elif '"' in attrs:
+ gtf_attrs += 1
+ check_bounds(find, genome, lineno, contig, end)
+
+ if flavour == "gtf" and gff3_attrs > gtf_attrs:
+ find.add(
+ "warn", "attribute_syntax", "file",
+ 'attributes use GFF3 syntax (key=value;) but the file is named .gtf, '
+ 'which expects key "value";. Parsers keyed on the extension will read '
+ "no attributes at all",
+ )
+ if flavour == "gff3" and gtf_attrs > gff3_attrs:
+ find.add(
+ "warn", "attribute_syntax", "file",
+ 'attributes use GTF syntax (key "value";) but the file is named .gff3',
+ )
+ audit_naming(find, contigs)
+
+
+def audit_vcf(path: Path, find: Findings, genome: dict[str, int]) -> None:
+ contigs: list[str] = []
+ header_seen = False
+ order = SortState()
+ declared: dict[str, int] = {}
+
+ for lineno, raw in enumerate(path.read_text().splitlines(), start=1):
+ if raw.startswith("##contig="):
+ ident = re.search(r"ID=([^,>]+)", raw)
+ length = re.search(r"length=(\d+)", raw)
+ if ident and length:
+ declared[canonical_contig(ident.group(1))] = int(length.group(1))
+ continue
+ if raw.startswith("#CHROM"):
+ header_seen = True
+ continue
+ if raw.startswith("#") or not raw.strip():
+ continue
+
+ fields = raw.split("\t")
+ if len(fields) < 5:
+ find.add(
+ "fatal", "too_few_columns", lineno,
+ "VCF data lines need at least CHROM POS ID REF ALT",
+ )
+ continue
+ contig, pos_s, _, ref, alt = fields[:5]
+ contigs.append(contig)
+ if not pos_s.isdigit():
+ find.add("fatal", "non_integer_position", lineno, f"POS is {pos_s!r}")
+ continue
+ pos = int(pos_s)
+
+ if pos < 1:
+ find.add(
+ "fatal", "pos_below_one", lineno,
+ f"POS is {pos}. VCF is 1-based; POS 0 is reserved for telomere "
+ "records and cannot carry a REF allele",
+ )
+ if not ref or set(ref) - VALID_BASES:
+ find.add(
+ "fatal", "bad_ref_allele", lineno,
+ f"REF is {ref!r}; it must be a non-empty ACGTN string. A '-' or an "
+ "empty field is Ensembl/VEP notation, not VCF -- VCF represents "
+ "indels with an anchor base shared by REF and ALT",
+ )
+ for one in alt.split(","):
+ if one in {"-", ""}:
+ find.add(
+ "fatal", "bad_alt_allele", lineno,
+ f"ALT {one!r} is Ensembl/VEP notation; VCF needs the anchor base",
+ )
+ elif one == ref:
+ find.add("warn", "ref_equals_alt", lineno, f"REF and ALT are both {ref}")
+ elif (
+ not one.startswith(("<", "*", "."))
+ and len(ref) > 1
+ and len(one) > 1
+ and ref[-1] == one[-1]
+ ):
+ find.add(
+ "warn", "not_parsimonious", lineno,
+ f"{ref}>{one} share a trailing base, so this record is not "
+ "trimmed. Run normalize_variant.py before comparing or joining "
+ "on these alleles",
+ )
+ if "," in alt:
+ find.add(
+ "info", "multiallelic", lineno,
+ "multi-allelic record; split it before normalising or joining",
+ )
+ order.check(
+ find, lineno, contig, pos,
+ "tabix indexing requires coordinate-sorted input",
+ )
+
+ far = pos + max(len(ref) - 1, 0)
+ check_bounds(
+ find,
+ genome or declared,
+ lineno,
+ contig,
+ far,
+ "the genome file" if genome else "the file's own ##contig headers",
+ )
+
+ if not header_seen:
+ find.add(
+ "warn", "missing_chrom_header", "file",
+ "no #CHROM line; the file is not a valid VCF and column meanings are "
+ "being guessed",
+ )
+ audit_naming(find, contigs)
+
+
+def audit_naming(find: Findings, contigs: list[str]) -> None:
+ style = naming_style(sorted(set(contigs)))
+ if style == "mixed":
+ prefixed = sorted({c for c in contigs if c.startswith("chr")})[:3]
+ plain = sorted({c for c in contigs if not c.startswith("chr")})[:3]
+ find.add(
+ "fatal", "mixed_contig_naming", "file",
+ f"the file mixes chr-prefixed ({', '.join(prefixed)}) and plain "
+ f"({', '.join(plain)}) contig names; any join will match one subset",
+ )
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description="Audit a BED/GTF/GFF3/VCF file against its format's conventions."
+ )
+ parser.add_argument("file", type=Path)
+ parser.add_argument("--format", choices=("bed", "gtf", "gff3", "vcf"))
+ parser.add_argument(
+ "--genome", type=Path, help=".chrom.sizes or .fai to bounds-check against"
+ )
+ parser.add_argument("--max-examples", type=int, default=5)
+ parser.add_argument("--output-format", choices=("tsv", "json"), default="tsv")
+ parser.add_argument("-o", "--output")
+ return parser
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = build_parser()
+ args = parser.parse_args(argv)
+ if not args.file.exists():
+ print(f"error: no such file: {args.file}", file=sys.stderr)
+ return 2
+ if args.genome and not args.genome.exists():
+ print(f"error: no such file: {args.genome}", file=sys.stderr)
+ return 2
+
+ fmt = args.format or detect_format(args.file)
+ genome = load_genome(args.genome)
+ find = Findings(args.max_examples)
+
+ if fmt == "bed":
+ audit_bed(args.file, find, genome)
+ elif fmt in {"gtf", "gff3"}:
+ audit_gff(args.file, find, genome, fmt)
+ else:
+ audit_vcf(args.file, find, genome)
+
+ if find.rows:
+ emit(find.rows, COLUMNS, args.output_format, args.output)
+ else:
+ print(f"{args.file.name}: no findings ({fmt} conventions)")
+ sys.stdout.flush()
+
+ if find.rows:
+ summary = ", ".join(
+ f"{count} {severity}"
+ for severity in ("fatal", "warn", "info")
+ if (count := find.by_severity[severity])
+ )
+ print(f"\n{args.file.name}: {summary}", file=sys.stderr)
+ return 1 if find.fatal else 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/skills/genomic-coordinates/scripts/check_contigs.py b/skills/genomic-coordinates/scripts/check_contigs.py
new file mode 100644
index 00000000..b1c0741d
--- /dev/null
+++ b/skills/genomic-coordinates/scripts/check_contigs.py
@@ -0,0 +1,382 @@
+#!/usr/bin/env python3
+"""Identify the assembly behind a file, and check that two files can be joined.
+
+The two ways a genomics pipeline produces confident nonsense are a chr-prefix
+mismatch (the join returns nothing, or worse, returns only the contigs that
+happen to agree) and an assembly mismatch (the join succeeds and every
+coordinate means something else). Both are visible in the contig list.
+
+ python3 check_contigs.py --identify ref.fa.fai
+ python3 check_contigs.py variants.vcf annotation.gtf ref.fa.fai
+ python3 check_contigs.py peaks.bed --genome hg38.chrom.sizes
+
+Exit codes: 0 compatible, 1 incompatible or unidentifiable, 2 usage error.
+"""
+
+from __future__ import annotations
+
+import argparse
+import re
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from _common import canonical_contig, emit, naming_style # noqa: E402
+
+# Primary-chromosome lengths, read from the UCSC bigZips chrom.sizes for each
+# assembly (hg19, hg38, hs1) and cross-checked against the NCBI assembly report
+# for GRCh37.p13. Verified 2026-07-26.
+#
+# GRCh37 and hg19 are the same assembly for every contig here except chrM: UCSC
+# kept the older NC_001807 mitochondrion (16,571 bp) while GRCh37 adopted the
+# rCRS (16,569 bp). That two-base difference is the only signal in the primary
+# chromosomes that tells the two apart, and it is why an hg19 BAM and a GRCh37
+# VCF can disagree about every mitochondrial variant.
+BUILDS: dict[str, dict[str, int]] = {
+ "GRCh37/hg19": {
+ "1": 249250621, "2": 243199373, "3": 198022430, "4": 191154276,
+ "5": 180915260, "6": 171115067, "7": 159138663, "8": 146364022,
+ "9": 141213431, "10": 135534747, "11": 135006516, "12": 133851895,
+ "13": 115169878, "14": 107349540, "15": 102531392, "16": 90354753,
+ "17": 81195210, "18": 78077248, "19": 59128983, "20": 63025520,
+ "21": 48129895, "22": 51304566, "X": 155270560, "Y": 59373566,
+ },
+ "GRCh38/hg38": {
+ "1": 248956422, "2": 242193529, "3": 198295559, "4": 190214555,
+ "5": 181538259, "6": 170805979, "7": 159345973, "8": 145138636,
+ "9": 138394717, "10": 133797422, "11": 135086622, "12": 133275309,
+ "13": 114364328, "14": 107043718, "15": 101991189, "16": 90338345,
+ "17": 83257441, "18": 80373285, "19": 58617616, "20": 64444167,
+ "21": 46709983, "22": 50818468, "X": 156040895, "Y": 57227415,
+ },
+ "T2T-CHM13v2.0/hs1": {
+ "1": 248387328, "2": 242696752, "3": 201105948, "4": 193574945,
+ "5": 182045439, "6": 172126628, "7": 160567428, "8": 146259331,
+ "9": 150617247, "10": 134758134, "11": 135127769, "12": 133324548,
+ "13": 113566686, "14": 101161492, "15": 99753195, "16": 96330374,
+ "17": 84276897, "18": 80542538, "19": 61707364, "20": 66210255,
+ "21": 45090682, "22": 51324926, "X": 154259566, "Y": 62460029,
+ },
+}
+
+MITO = {16571: "hg19 (UCSC NC_001807 chrM)", 16569: "GRCh37/38 (rCRS MT)"}
+
+COLUMNS = ["file", "kind", "contigs", "naming", "assembly", "detail"]
+
+
+# --------------------------------------------------------------------------
+# readers
+# --------------------------------------------------------------------------
+
+
+def read_sizes(path: Path) -> tuple[dict[str, int], str]:
+ """.fai, .chrom.sizes, or .genome -- name in column 1, length in column 2."""
+ sizes = {}
+ for line in path.read_text().splitlines():
+ if not line.strip() or line.startswith("#"):
+ continue
+ parts = line.split("\t") if "\t" in line else line.split()
+ if len(parts) >= 2 and parts[1].isdigit():
+ sizes[parts[0]] = int(parts[1])
+ return sizes, "sizes"
+
+
+def read_fasta_names(path: Path) -> tuple[dict[str, int], str]:
+ sizes: dict[str, int] = {}
+ name, count = None, 0
+ with path.open() as handle:
+ for line in handle:
+ if line.startswith(">"):
+ if name is not None:
+ sizes[name] = count
+ name, count = line[1:].split()[0], 0
+ elif name is not None:
+ count += len(line.strip())
+ if name is not None:
+ sizes[name] = count
+ return sizes, "fasta"
+
+
+def read_vcf(path: Path) -> tuple[dict[str, int], str]:
+ """Prefer the ##contig header; fall back to the CHROM column."""
+ header: dict[str, int] = {}
+ seen: dict[str, int] = {}
+ with path.open() as handle:
+ for line in handle:
+ if line.startswith("##contig="):
+ ident = re.search(r"ID=([^,>]+)", line)
+ length = re.search(r"length=(\d+)", line)
+ if ident:
+ header[ident.group(1)] = int(length.group(1)) if length else 0
+ elif not line.startswith("#") and line.strip():
+ fields = line.split("\t")
+ if len(fields) >= 2 and fields[1].isdigit():
+ pos = int(fields[1])
+ seen[fields[0]] = max(seen.get(fields[0], 0), pos)
+ if header:
+ return header, "vcf header"
+ return seen, "vcf records (max POS, not contig length)"
+
+
+def read_sam_header(path: Path) -> tuple[dict[str, int], str]:
+ sizes: dict[str, int] = {}
+ with path.open() as handle:
+ for line in handle:
+ if not line.startswith("@"):
+ break
+ if line.startswith("@SQ"):
+ ident = re.search(r"SN:(\S+)", line)
+ length = re.search(r"LN:(\d+)", line)
+ if ident and length:
+ sizes[ident.group(1)] = int(length.group(1))
+ return sizes, "sam header"
+
+
+def read_intervals(path: Path, start_col: int, end_col: int) -> tuple[dict[str, int], str]:
+ """BED/GTF/GFF: names from column 1, plus the largest coordinate observed."""
+ sizes: dict[str, int] = {}
+ with path.open() as handle:
+ for line in handle:
+ if not line.strip() or line.startswith(("#", "track", "browser")):
+ continue
+ fields = line.rstrip("\n").split("\t")
+ if len(fields) <= end_col:
+ continue
+ try:
+ far = max(int(fields[start_col]), int(fields[end_col]))
+ except ValueError:
+ continue
+ sizes[fields[0]] = max(sizes.get(fields[0], 0), far)
+ return sizes, "intervals (max coordinate, not contig length)"
+
+
+def load(path: Path) -> tuple[dict[str, int], str]:
+ suffixes = [s.lower() for s in path.suffixes]
+ name = path.name.lower()
+ if name.endswith((".fai", ".chrom.sizes", ".sizes", ".genome")):
+ return read_sizes(path)
+ if ".vcf" in suffixes:
+ return read_vcf(path)
+ if ".sam" in suffixes or name.endswith(".header") or name.endswith(".header.txt"):
+ return read_sam_header(path)
+ if ".gtf" in suffixes or ".gff" in suffixes or ".gff3" in suffixes:
+ return read_intervals(path, 3, 4)
+ if ".bed" in suffixes or ".narrowpeak" in suffixes or ".broadpeak" in suffixes:
+ return read_intervals(path, 1, 2)
+ if ".fa" in suffixes or ".fasta" in suffixes or ".fna" in suffixes:
+ return read_fasta_names(path)
+ raise SystemExit(
+ f"error: cannot tell what kind of file {path.name} is. Supported: "
+ ".fai, .chrom.sizes, .vcf, .sam (header), .bed, .gtf, .gff, .fasta"
+ )
+
+
+# --------------------------------------------------------------------------
+# identification
+# --------------------------------------------------------------------------
+
+
+def identify(sizes: dict[str, int], exact: bool) -> tuple[str, str]:
+ """Match observed contig lengths against the known assemblies.
+
+ ``exact`` is False for interval files, where the numbers are the largest
+ coordinate seen rather than the contig length -- there the useful question is
+ only whether anything overflows a candidate assembly.
+ """
+ folded = {canonical_contig(n): length for n, length in sizes.items()}
+ mito = folded.get("M")
+
+ scores = []
+ for build, lengths in BUILDS.items():
+ shared = [c for c in lengths if c in folded]
+ if not shared:
+ continue
+ if exact:
+ agree = sum(1 for c in shared if folded[c] == lengths[c])
+ else:
+ agree = sum(1 for c in shared if 0 < folded[c] <= lengths[c])
+ scores.append((agree / len(shared), agree, len(shared), build))
+ if not scores:
+ return "unknown", "no primary chromosomes to match against"
+ scores.sort(reverse=True)
+ fraction, agree, total, build = scores[0]
+
+ if not exact:
+ fits = [b for f, _, _, b in scores if f == 1.0]
+ if len(fits) == 1:
+ return f"consistent with {fits[0]}", "no coordinate overflows this assembly"
+ if not fits:
+ over = [c for c in folded if c in BUILDS[build] and folded[c] > BUILDS[build][c]]
+ return "CONFLICT", (
+ f"coordinates run past the end of every known assembly (closest is "
+ f"{build}, overflowing on {', '.join(sorted(over)[:4])}); wrong "
+ "assembly, or 1-based data written into a 0-based file"
+ )
+ return "ambiguous", f"coordinates fit any of: {', '.join(fits)}"
+
+ if fraction < 0.9:
+ return "unknown", (
+ f"closest is {build} at {agree}/{total} chromosomes -- not a match"
+ )
+ detail = f"{agree}/{total} primary chromosome lengths match"
+ if build == "GRCh37/hg19" and mito is not None:
+ detail += f"; chrM is {mito} bp, i.e. {MITO.get(mito, 'a non-standard mitochondrion')}"
+ build = "hg19" if mito == 16571 else "GRCh37" if mito == 16569 else build
+ elif mito is not None and mito not in MITO:
+ detail += f"; chrM length {mito} is not a standard human mitochondrion"
+ if agree != total:
+ wrong = [c for c in BUILDS[scores[0][3]] if c in folded and folded[c] != BUILDS[scores[0][3]][c]]
+ detail += f"; disagrees on {', '.join(sorted(wrong))}"
+ return build, detail
+
+
+# --------------------------------------------------------------------------
+# comparison
+# --------------------------------------------------------------------------
+
+
+def compare(loaded: list[tuple[Path, dict[str, int], str, bool]]) -> list[str]:
+ """Report every reason a join between these files would go wrong.
+
+ Each entry carries ``exact``: True when its numbers are declared contig
+ lengths, False when they are only the largest coordinate observed. Two exact
+ files must agree exactly; an inexact one can only ever be caught overflowing.
+ """
+ problems: list[str] = []
+ styles = {path.name: naming_style(list(sizes)) for path, sizes, _, _ in loaded}
+ distinct = {s for s in styles.values() if s != "empty"}
+ if len(distinct) > 1:
+ detail = ", ".join(f"{name}={style}" for name, style in styles.items())
+ problems.append(
+ f"contig naming differs between files ({detail}). A join on the raw "
+ "name returns zero rows for every contig; rename one side first"
+ )
+
+ for i in range(len(loaded)):
+ for j in range(i + 1, len(loaded)):
+ path_a, sizes_a, _, exact_a = loaded[i]
+ path_b, sizes_b, _, exact_b = loaded[j]
+ fold_a = {canonical_contig(n): v for n, v in sizes_a.items()}
+ fold_b = {canonical_contig(n): v for n, v in sizes_b.items()}
+ shared = set(fold_a) & set(fold_b)
+ if not shared:
+ problems.append(
+ f"{path_a.name} and {path_b.name} share no contigs at all, "
+ "even ignoring the chr prefix"
+ )
+ continue
+
+ if exact_a and exact_b:
+ clash = sorted(
+ c for c in shared
+ if fold_a[c] and fold_b[c] and fold_a[c] != fold_b[c]
+ )
+ if clash:
+ example = clash[0]
+ problems.append(
+ f"{path_a.name} and {path_b.name} disagree on contig length "
+ f"for {', '.join(clash[:5])}"
+ f"{'...' if len(clash) > 5 else ''} "
+ f"(e.g. {example}: {fold_a[example]} vs {fold_b[example]}) -- "
+ "these are different assemblies and their coordinates are "
+ "not comparable"
+ )
+ else:
+ # One side holds only the largest coordinate observed, so equality
+ # proves nothing. Overflowing a declared contig length still does.
+ pairs = []
+ if exact_b and not exact_a:
+ pairs.append((path_a.name, fold_a, path_b.name, fold_b))
+ if exact_a and not exact_b:
+ pairs.append((path_b.name, fold_b, path_a.name, fold_a))
+ for name_hi, hi, name_lo, lo in pairs:
+ over = sorted(c for c in shared if lo[c] and hi[c] > lo[c])
+ if over:
+ example = over[0]
+ problems.append(
+ f"{name_hi} has coordinates past the end of {name_lo} on "
+ f"{', '.join(over[:5])}{'...' if len(over) > 5 else ''} "
+ f"(e.g. {example}: {hi[example]} > {lo[example]}) -- "
+ "wrong assembly, or an off-by-one from a 1-based source"
+ )
+
+ only_a = sorted(set(fold_a) - set(fold_b))
+ if exact_a and exact_b and only_a and len(only_a) <= len(fold_a) / 2:
+ problems.append(
+ f"{len(only_a)} contigs in {path_a.name} are absent from "
+ f"{path_b.name} ({', '.join(only_a[:5])}"
+ f"{'...' if len(only_a) > 5 else ''}); records on them are "
+ "dropped silently by most tools"
+ )
+ return problems
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description="Identify assemblies and check contig compatibility between files."
+ )
+ parser.add_argument("files", nargs="+", type=Path)
+ parser.add_argument(
+ "--identify",
+ action="store_true",
+ help="only report the assembly of each file, skipping the cross-file checks",
+ )
+ parser.add_argument(
+ "--genome",
+ type=Path,
+ help="a .chrom.sizes/.fai to check every other file against",
+ )
+ parser.add_argument("--format", choices=("tsv", "json"), default="tsv")
+ parser.add_argument("-o", "--output")
+ return parser
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = build_parser()
+ args = parser.parse_args(argv)
+
+ paths = list(args.files)
+ if args.genome:
+ paths.append(args.genome)
+ for path in paths:
+ if not path.exists():
+ print(f"error: no such file: {path}", file=sys.stderr)
+ return 2
+
+ loaded = []
+ rows = []
+ for path in paths:
+ sizes, kind = load(path)
+ exact = kind in {"sizes", "fasta", "vcf header", "sam header"}
+ loaded.append((path, sizes, kind, exact))
+ assembly, detail = identify(sizes, exact)
+ rows.append(
+ {
+ "file": path.name,
+ "kind": kind,
+ "contigs": len(sizes),
+ "naming": naming_style(list(sizes)),
+ "assembly": assembly,
+ "detail": detail,
+ }
+ )
+
+ emit(rows, COLUMNS, args.format, args.output)
+ sys.stdout.flush()
+
+ failed = any(r["assembly"] == "CONFLICT" for r in rows)
+ if not args.identify and len(loaded) > 1:
+ problems = compare(loaded)
+ if problems:
+ failed = True
+ print("\nincompatibilities:", file=sys.stderr)
+ for problem in problems:
+ print(f" - {problem}", file=sys.stderr)
+ else:
+ print("\nno contig incompatibilities found", file=sys.stderr)
+ return 1 if failed else 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/skills/genomic-coordinates/scripts/convert_coords.py b/skills/genomic-coordinates/scripts/convert_coords.py
new file mode 100644
index 00000000..718dbee1
--- /dev/null
+++ b/skills/genomic-coordinates/scripts/convert_coords.py
@@ -0,0 +1,180 @@
+#!/usr/bin/env python3
+"""Convert intervals between genomic coordinate conventions.
+
+Every conversion goes through one canonical form (0-based half-open), so the
+answer never depends on remembering which pair of formats is involved.
+
+ python3 convert_coords.py --from bed --to gff chr1 999 1000
+ python3 convert_coords.py --from ucsc --to bed "chr7:5,530,601-5,530,625"
+ python3 convert_coords.py --from bed --to ensembl --input peaks.bed
+ python3 convert_coords.py --list
+
+Exit codes: 0 fine, 1 at least one interval is degenerate or invalid,
+2 usage error.
+"""
+
+from __future__ import annotations
+
+import argparse
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from _common import ( # noqa: E402
+ CONVENTIONS,
+ REGION_STRING_FORMATS,
+ Convention,
+ emit,
+ format_region,
+ from_canonical,
+ get_convention,
+ iter_data_lines,
+ parse_region,
+ to_canonical,
+)
+
+COLUMNS = ["contig", "input", "output", "length", "status", "detail"]
+
+
+def convert_one(
+ contig: str,
+ start: int,
+ end: int,
+ src: Convention,
+ dst: Convention,
+ strand: str | None = None,
+) -> dict:
+ """Convert one interval and describe anything the caller should look at."""
+ start0, end0 = to_canonical(src, start, end)
+ length = end0 - start0
+
+ status, detail = "ok", ""
+ if length < 0:
+ status = "invalid"
+ detail = (
+ f"end precedes start in {src.label} coordinates; "
+ "an interval file with start > end is corrupt, not merely mis-converted"
+ )
+ elif length == 0:
+ if dst.half_open:
+ status = "zero_length"
+ detail = "zero-length interval (an insertion point between two bases)"
+ else:
+ status = "unrepresentable"
+ detail = (
+ f"zero-length interval cannot be written in {dst.label}; "
+ f"the arithmetic gives end = start - 1. Keep it in a half-open "
+ "format, or record the insertion against its anchor base as VCF does"
+ )
+
+ out_start, out_end = from_canonical(dst, start0, end0)
+
+ def render(conv: Convention, s: int, e: int) -> str:
+ if conv.name in REGION_STRING_FORMATS:
+ return format_region(contig, s, e, strand)
+ return f"{s}-{e}"
+
+ return {
+ "contig": contig,
+ "input": render(src, start, end),
+ "output": render(dst, out_start, out_end),
+ "length": length,
+ "status": status,
+ "detail": detail,
+ "start": out_start,
+ "end": out_end,
+ "from": src.name,
+ "to": dst.name,
+ }
+
+
+def read_intervals(path: str, conv: Convention) -> list[tuple[str, int, int, str | None]]:
+ """Read intervals from a file: columnar for file formats, one region per line
+ for the region-string formats."""
+ out: list[tuple[str, int, int, str | None]] = []
+ for lineno, line in iter_data_lines(path):
+ try:
+ if conv.name in REGION_STRING_FORMATS or ":" in line.split("\t")[0]:
+ contig, start, end, strand = parse_region(line.split("\t")[0], conv)
+ else:
+ fields = line.split("\t")
+ if len(fields) < 3:
+ raise ValueError("need at least contig, start, end columns")
+ if conv.name == "gff" or conv.name == "gff3" or conv.name == "gtf":
+ contig, start, end = fields[0], int(fields[3]), int(fields[4])
+ strand = fields[6] if len(fields) > 6 else None
+ elif conv.name == "vcf":
+ contig, pos, ref = fields[0], int(fields[1]), fields[3]
+ start, end, strand = pos, pos + len(ref) - 1, None
+ else:
+ contig, start, end = fields[0], int(fields[1]), int(fields[2])
+ strand = fields[5] if len(fields) > 5 and fields[5] in "+-." else None
+ except (ValueError, IndexError) as exc:
+ raise SystemExit(f"{path}:{lineno}: {exc}") from exc
+ out.append((contig, start, end, strand))
+ return out
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description="Convert intervals between genomic coordinate conventions.",
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ )
+ parser.add_argument("contig", nargs="?", help="contig name, or a region string")
+ parser.add_argument("start", nargs="?", type=int)
+ parser.add_argument("end", nargs="?", type=int)
+ parser.add_argument("--from", dest="src", help="source convention")
+ parser.add_argument("--to", dest="dst", help="target convention")
+ parser.add_argument("--input", help="file of intervals in the source convention")
+ parser.add_argument("--format", choices=("tsv", "json"), default="tsv")
+ parser.add_argument("-o", "--output", help="write here instead of stdout")
+ parser.add_argument(
+ "--list", action="store_true", help="print the convention table and exit"
+ )
+ return parser
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = build_parser()
+ args = parser.parse_args(argv)
+
+ if args.list:
+ width = max(len(name) for name in CONVENTIONS)
+ print(f"{'format'.ljust(width)} convention notes")
+ for name, conv in CONVENTIONS.items():
+ print(f"{name.ljust(width)} {conv.label.ljust(20)} {conv.note}")
+ return 0
+
+ if not args.src or not args.dst:
+ parser.error("--from and --to are both required")
+ try:
+ src = get_convention(args.src)
+ dst = get_convention(args.dst)
+ except ValueError as exc:
+ parser.error(str(exc))
+
+ intervals: list[tuple[str, int, int, str | None]] = []
+ if args.input:
+ intervals = read_intervals(args.input, src)
+ elif args.contig is not None:
+ if args.start is None:
+ try:
+ intervals = [parse_region(args.contig, src)]
+ except ValueError as exc:
+ parser.error(str(exc))
+ else:
+ end = args.end
+ if end is None:
+ end = args.start if not src.half_open else args.start + 1
+ intervals = [(args.contig, args.start, end, None)]
+ else:
+ parser.error("give a region, a contig/start/end triple, or --input")
+
+ rows = [convert_one(c, s, e, src, dst, strand) for c, s, e, strand in intervals]
+ emit(rows, COLUMNS, args.format, args.output)
+ return 1 if any(r["status"] in {"invalid", "unrepresentable"} for r in rows) else 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/skills/genomic-coordinates/scripts/normalize_variant.py b/skills/genomic-coordinates/scripts/normalize_variant.py
new file mode 100644
index 00000000..82eda38d
--- /dev/null
+++ b/skills/genomic-coordinates/scripts/normalize_variant.py
@@ -0,0 +1,290 @@
+#!/usr/bin/env python3
+"""Normalise VCF-style variants: check REF, trim, and left-align.
+
+Two variant records can describe exactly the same change to the genome and share
+no field values at all. Comparing, joining, or deduplicating variants without
+normalising first silently loses real matches. This implements the parsimony +
+left-alignment procedure of Tan, Abecasis & Kang (2015), which is what
+``bcftools norm`` and ``vt normalize`` implement.
+
+ python3 normalize_variant.py --fasta ref.fa chr1 7 CAC C
+ python3 normalize_variant.py --fasta ref.fa --input variants.vcf
+ python3 normalize_variant.py --fasta ref.fa --compare chr1:7:CAC:C chr1:3:CAC:C
+
+Exit codes: 0 all records verified against the reference, 1 at least one REF
+mismatch or invalid record, 2 usage or reference error.
+"""
+
+from __future__ import annotations
+
+import argparse
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from _common import Reference, ReferenceError, emit, iter_data_lines # noqa: E402
+
+COLUMNS = [
+ "input",
+ "normalized",
+ "type",
+ "pos_shift",
+ "ref_check",
+ "changed",
+ "detail",
+]
+
+SYMBOLIC_PREFIXES = ("<", "*", ".")
+DEFAULT_WINDOW = 1000
+
+
+class VariantError(ValueError):
+ """The record cannot be normalised as written."""
+
+
+def classify(ref: str, alt: str) -> str:
+ if len(ref) == len(alt) == 1:
+ return "snv"
+ if len(ref) == len(alt):
+ return "mnv"
+ if len(ref) == 1 and len(alt) > 1 and alt.startswith(ref):
+ return "insertion"
+ if len(alt) == 1 and len(ref) > 1 and ref.startswith(alt):
+ return "deletion"
+ return "complex"
+
+
+def normalize(
+ reference: Reference,
+ contig: str,
+ pos: int,
+ ref: str,
+ alt: str,
+ window: int = DEFAULT_WINDOW,
+) -> dict:
+ """Normalise a single bi-allelic record. ``pos`` is 1-based, as in VCF.
+
+ Returns the normalised record plus how far it moved and whether the stated
+ REF actually matched the reference sequence. ``shifted`` is positive when
+ left-alignment walked the anchor left through a repeat, and negative when
+ trimming redundant flanking bases moved it right onto a parsimonious
+ representation (``3 CA>CT`` is really the SNV ``4 A>T``).
+ """
+ if pos < 1:
+ raise VariantError(f"POS {pos} is not 1-based; VCF positions start at 1")
+ if not ref:
+ raise VariantError("REF is empty; VCF requires at least the anchor base")
+ if alt.startswith(SYMBOLIC_PREFIXES):
+ return {
+ "pos": pos,
+ "ref": ref,
+ "alt": alt,
+ "shifted": 0,
+ "ref_check": "skipped",
+ "type": "symbolic",
+ "detail": "symbolic, missing, or spanning-deletion ALT: left as written",
+ }
+
+ ref, alt = ref.upper(), alt.upper()
+ observed = reference.fetch(contig, pos - 1, pos - 1 + len(ref))
+ if observed != ref:
+ return {
+ "pos": pos,
+ "ref": ref,
+ "alt": alt,
+ "shifted": 0,
+ "ref_check": "MISMATCH",
+ "type": classify(ref, alt),
+ "detail": (
+ f"REF says {ref} but the reference has {observed or '(past contig end)'} "
+ f"at {contig}:{pos}. Do not normalise this record -- the variants and "
+ "the FASTA are different assemblies, or the coordinates are off by one"
+ ),
+ }
+ if ref == alt:
+ raise VariantError(f"REF and ALT are both {ref}; this record asserts no change")
+
+ start_pos = pos
+ limit = max(0, pos - 1 - window)
+
+ # Right-trim and left-extend until the alleles no longer share a final base.
+ while ref[-1] == alt[-1]:
+ if len(ref) == 1 or len(alt) == 1:
+ if pos - 1 <= limit:
+ break
+ base = reference.fetch(contig, pos - 2, pos - 1)
+ if not base:
+ break
+ pos -= 1
+ ref, alt = base + ref, base + alt
+ ref, alt = ref[:-1], alt[:-1]
+
+ # Left-trim any shared leading bases, keeping one anchor base for indels.
+ while len(ref) > 1 and len(alt) > 1 and ref[0] == alt[0]:
+ ref, alt = ref[1:], alt[1:]
+ pos += 1
+
+ shifted = start_pos - pos
+ detail = ""
+ if shifted and pos - 1 <= limit:
+ detail = (
+ f"left-alignment stopped at the {window} bp window; the repeat may extend "
+ "further. Re-run with a larger --window to confirm"
+ )
+ return {
+ "pos": pos,
+ "ref": ref,
+ "alt": alt,
+ "shifted": shifted,
+ "ref_check": "ok",
+ "type": classify(ref, alt),
+ "detail": detail,
+ }
+
+
+def parse_spec(text: str) -> tuple[str, int, str, str]:
+ """Parse ``contig:pos:ref:alt``."""
+ parts = text.split(":")
+ if len(parts) != 4:
+ raise VariantError(f"expected contig:pos:ref:alt, got {text!r}")
+ contig, pos, ref, alt = parts
+ if not pos.isdigit():
+ raise VariantError(f"POS {pos!r} in {text!r} is not a number")
+ return contig, int(pos), ref, alt
+
+
+def read_records(path: str, split: bool) -> list[tuple[str, int, str, str]]:
+ """Read CHROM/POS/REF/ALT from a VCF, or from a bare 4-column TSV.
+
+ Five or more columns are read as VCF (CHROM POS ID REF ALT); exactly four as
+ CHROM POS REF ALT.
+ """
+ records: list[tuple[str, int, str, str]] = []
+ for lineno, line in iter_data_lines(path):
+ fields = line.split("\t")
+ if len(fields) >= 5:
+ contig, pos, ref, alt = fields[0], fields[1], fields[3], fields[4]
+ elif len(fields) == 4:
+ contig, pos, ref, alt = fields[0], fields[1], fields[2], fields[3]
+ else:
+ raise SystemExit(f"{path}:{lineno}: need CHROM, POS, REF, ALT columns")
+ if not pos.isdigit():
+ raise SystemExit(f"{path}:{lineno}: POS {pos!r} is not a number")
+ for one in alt.split(",") if split else [alt]:
+ records.append((contig, int(pos), ref, one))
+ return records
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ description="Check, trim, and left-align VCF-style variants."
+ )
+ parser.add_argument("contig", nargs="?")
+ parser.add_argument("pos", nargs="?", type=int)
+ parser.add_argument("ref", nargs="?")
+ parser.add_argument("alt", nargs="?")
+ parser.add_argument("--fasta", required=True, help="reference FASTA (.fai used if present)")
+ parser.add_argument("--input", help="VCF, or a TSV of contig/pos/ref/alt")
+ parser.add_argument(
+ "--compare",
+ nargs="+",
+ metavar="CONTIG:POS:REF:ALT",
+ help="normalise two or more records and report whether they are the same variant",
+ )
+ parser.add_argument(
+ "--split",
+ action="store_true",
+ help="split comma-separated ALTs into one record each before normalising",
+ )
+ parser.add_argument("--window", type=int, default=DEFAULT_WINDOW)
+ parser.add_argument("--format", choices=("tsv", "json"), default="tsv")
+ parser.add_argument("-o", "--output")
+ return parser
+
+
+def run(records, reference, window) -> list[dict]:
+ rows = []
+ for contig, pos, ref, alt in records:
+ label = f"{contig}:{pos}:{ref}:{alt}"
+ try:
+ result = normalize(reference, contig, pos, ref, alt, window)
+ except (VariantError, ReferenceError) as exc:
+ rows.append(
+ {
+ "input": label,
+ "normalized": "",
+ "type": "",
+ "pos_shift": "",
+ "ref_check": "error",
+ "changed": "",
+ "detail": str(exc),
+ }
+ )
+ continue
+ norm = f"{contig}:{result['pos']}:{result['ref']}:{result['alt']}"
+ rows.append(
+ {
+ "input": label,
+ "normalized": norm,
+ "type": result["type"],
+ "pos_shift": result["shifted"],
+ "ref_check": result["ref_check"],
+ "changed": "yes" if norm != label else "no",
+ "detail": result["detail"],
+ }
+ )
+ return rows
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = build_parser()
+ args = parser.parse_args(argv)
+
+ try:
+ reference = Reference(args.fasta)
+ except ReferenceError as exc:
+ print(f"error: {exc}", file=sys.stderr)
+ return 2
+
+ records: list[tuple[str, int, str, str]] = []
+ if args.compare:
+ try:
+ records = [parse_spec(spec) for spec in args.compare]
+ except VariantError as exc:
+ parser.error(str(exc))
+ elif args.input:
+ records = read_records(args.input, args.split)
+ elif args.contig and args.pos is not None and args.ref and args.alt:
+ alts = args.alt.split(",") if args.split else [args.alt]
+ records = [(args.contig, args.pos, args.ref, a) for a in alts]
+ else:
+ parser.error("give contig pos ref alt, or --input, or --compare")
+
+ rows = run(records, reference, args.window)
+ emit(rows, COLUMNS, args.format, args.output)
+ sys.stdout.flush()
+
+ if args.compare:
+ if any(row["ref_check"] != "ok" for row in rows):
+ print("\nverdict: cannot compare -- at least one record failed", file=sys.stderr)
+ return 1
+ keys = {row["normalized"] for row in rows}
+ if len(keys) == 1:
+ print(
+ f"\nverdict: identical -- all {len(rows)} records normalise to "
+ f"{keys.pop()}",
+ file=sys.stderr,
+ )
+ else:
+ print(
+ f"\nverdict: distinct -- {len(keys)} different variants after "
+ "normalisation",
+ file=sys.stderr,
+ )
+
+ return 1 if any(row["ref_check"] in {"MISMATCH", "error"} for row in rows) else 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/tests/genomic-coordinates/fixtures/GRCh37.fa.fai b/tests/genomic-coordinates/fixtures/GRCh37.fa.fai
new file mode 100644
index 00000000..451ee5bf
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/GRCh37.fa.fai
@@ -0,0 +1,25 @@
+1 249250621 0 60 61
+2 243199373 0 60 61
+3 198022430 0 60 61
+4 191154276 0 60 61
+5 180915260 0 60 61
+6 171115067 0 60 61
+7 159138663 0 60 61
+X 155270560 0 60 61
+8 146364022 0 60 61
+9 141213431 0 60 61
+10 135534747 0 60 61
+11 135006516 0 60 61
+12 133851895 0 60 61
+13 115169878 0 60 61
+14 107349540 0 60 61
+15 102531392 0 60 61
+16 90354753 0 60 61
+17 81195210 0 60 61
+18 78077248 0 60 61
+20 63025520 0 60 61
+Y 59373566 0 60 61
+19 59128983 0 60 61
+22 51304566 0 60 61
+21 48129895 0 60 61
+MT 16569 0 60 61
diff --git a/tests/genomic-coordinates/fixtures/bad.gtf b/tests/genomic-coordinates/fixtures/bad.gtf
new file mode 100644
index 00000000..6950e1d1
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/bad.gtf
@@ -0,0 +1,3 @@
+chr1 src exon 0 100 . + . gene_id "A"; transcript_id "B";
+chr1 src CDS 200 150 . + . gene_id "A"; transcript_id "B";
+chr1 src CDS 300 400 . * . gene_id "A"; transcript_id "B";
diff --git a/tests/genomic-coordinates/fixtures/bad_blocks.bed b/tests/genomic-coordinates/fixtures/bad_blocks.bed
new file mode 100644
index 00000000..1e47f324
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/bad_blocks.bed
@@ -0,0 +1 @@
+chr1 1000 2000 tx 0 + 1000 2000 0 2 100,200, 100,800,
diff --git a/tests/genomic-coordinates/fixtures/clean.bed b/tests/genomic-coordinates/fixtures/clean.bed
new file mode 100644
index 00000000..46c20890
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/clean.bed
@@ -0,0 +1,3 @@
+chr1 1000 2000 peakA 0 +
+chr1 5000 6000 peakB 0 -
+chr2 500 900 peakC 0 .
diff --git a/tests/genomic-coordinates/fixtures/clean.gtf b/tests/genomic-coordinates/fixtures/clean.gtf
new file mode 100644
index 00000000..7089cf21
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/clean.gtf
@@ -0,0 +1,3 @@
+chr1 HAVANA exon 11869 12227 . + . gene_id "ENSG00000223972"; transcript_id "ENST00000456328";
+chr1 HAVANA CDS 12613 12721 . + 0 gene_id "ENSG00000223972"; transcript_id "ENST00000456328";
+chr1 HAVANA CDS 13221 14409 . + 2 gene_id "ENSG00000223972"; transcript_id "ENST00000456328";
diff --git a/tests/genomic-coordinates/fixtures/clean.vcf b/tests/genomic-coordinates/fixtures/clean.vcf
new file mode 100644
index 00000000..f9b9c65d
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/clean.vcf
@@ -0,0 +1,6 @@
+##fileformat=VCFv4.2
+#CHROM POS ID REF ALT QUAL FILTER INFO
+chr1 100 . A G . . .
+chr1 250 . C T . . .
+chr2 50 . G A . . .
+chr2 900 . T C . . .
diff --git a/tests/genomic-coordinates/fixtures/grch38.vcf b/tests/genomic-coordinates/fixtures/grch38.vcf
new file mode 100644
index 00000000..a5aee3e7
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/grch38.vcf
@@ -0,0 +1,4 @@
+##fileformat=VCFv4.2
+##contig=<ID=chr1,length=248956422>
+#CHROM POS ID REF ALT QUAL FILTER INFO
+chr1 100 . A G . . .
diff --git a/tests/genomic-coordinates/fixtures/hg19.chrom.sizes b/tests/genomic-coordinates/fixtures/hg19.chrom.sizes
new file mode 100644
index 00000000..3b8edc62
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/hg19.chrom.sizes
@@ -0,0 +1,25 @@
+chr1 249250621
+chr2 243199373
+chr3 198022430
+chr4 191154276
+chr5 180915260
+chr6 171115067
+chr7 159138663
+chrX 155270560
+chr8 146364022
+chr9 141213431
+chr10 135534747
+chr11 135006516
+chr12 133851895
+chr13 115169878
+chr14 107349540
+chr15 102531392
+chr16 90354753
+chr17 81195210
+chr18 78077248
+chr20 63025520
+chrY 59373566
+chr19 59128983
+chr22 51304566
+chr21 48129895
+chrM 16571
diff --git a/tests/genomic-coordinates/fixtures/hg38.chrom.sizes b/tests/genomic-coordinates/fixtures/hg38.chrom.sizes
new file mode 100644
index 00000000..4cbc1f01
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/hg38.chrom.sizes
@@ -0,0 +1,25 @@
+chr1 248956422
+chr2 242193529
+chr3 198295559
+chr4 190214555
+chr5 181538259
+chr6 170805979
+chr7 159345973
+chrX 156040895
+chr8 145138636
+chr9 138394717
+chr11 135086622
+chr10 133797422
+chr12 133275309
+chr13 114364328
+chr14 107043718
+chr15 101991189
+chr16 90338345
+chr17 83257441
+chr18 80373285
+chr20 64444167
+chr19 58617616
+chrY 57227415
+chr22 50818468
+chr21 46709983
+chrM 16569
diff --git a/tests/genomic-coordinates/fixtures/interleaved.vcf b/tests/genomic-coordinates/fixtures/interleaved.vcf
new file mode 100644
index 00000000..830fe67a
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/interleaved.vcf
@@ -0,0 +1,5 @@
+##fileformat=VCFv4.2
+#CHROM POS ID REF ALT QUAL FILTER INFO
+chr1 100 . A G . . .
+chr2 50 . G A . . .
+chr1 250 . C T . . .
diff --git a/tests/genomic-coordinates/fixtures/many_bad.gtf b/tests/genomic-coordinates/fixtures/many_bad.gtf
new file mode 100644
index 00000000..ed6ab3d7
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/many_bad.gtf
@@ -0,0 +1,10 @@
+chr1 src exon 0 100 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 101 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 102 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 103 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 104 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 105 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 106 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 107 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 108 . + . gene_id "A"; transcript_id "B";
+chr1 src exon 0 109 . + . gene_id "A"; transcript_id "B";
diff --git a/tests/genomic-coordinates/fixtures/messy.vcf b/tests/genomic-coordinates/fixtures/messy.vcf
new file mode 100644
index 00000000..81ae42b2
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/messy.vcf
@@ -0,0 +1,8 @@
+##fileformat=VCFv4.2
+##contig=<ID=chr1,length=248956422>
+#CHROM POS ID REF ALT QUAL FILTER INFO
+chr1 100 . AT GT . . .
+chr1 0 . A G . . .
+chr1 500 . A - . . .
+chr1 600 . A G,GG . . .
+2 700 . C T . . .
diff --git a/tests/genomic-coordinates/fixtures/onebased.bed b/tests/genomic-coordinates/fixtures/onebased.bed
new file mode 100644
index 00000000..897bef98
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/onebased.bed
@@ -0,0 +1,4 @@
+chr1 100 100 snv1 0 +
+chr1 205 205 snv2 0 -
+chr1 300 310 okay 0 .
+chr2 400 400 snv3 0 +
diff --git a/tests/genomic-coordinates/fixtures/overflow.bed b/tests/genomic-coordinates/fixtures/overflow.bed
new file mode 100644
index 00000000..2d452c2b
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/overflow.bed
@@ -0,0 +1,2 @@
+chr1 1000 2000 peakA
+chr1 249260000 249270000 peakB
diff --git a/tests/genomic-coordinates/fixtures/readme.txt b/tests/genomic-coordinates/fixtures/readme.txt
new file mode 100644
index 00000000..78d25aa0
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/readme.txt
@@ -0,0 +1 @@
+Not a genomics file. Used to check that unknown extensions are refused.
diff --git a/tests/genomic-coordinates/fixtures/spaced.bed b/tests/genomic-coordinates/fixtures/spaced.bed
new file mode 100644
index 00000000..6a246f89
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/spaced.bed
@@ -0,0 +1,2 @@
+chr1 1000 2000 peakA
+chr1 5000 6000 peakB
diff --git a/tests/genomic-coordinates/fixtures/tiny.fa b/tests/genomic-coordinates/fixtures/tiny.fa
new file mode 100644
index 00000000..eb5e7579
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/tiny.fa
@@ -0,0 +1,2 @@
+>chr1
+GGCACACACT
diff --git a/tests/genomic-coordinates/fixtures/tiny.vcf b/tests/genomic-coordinates/fixtures/tiny.vcf
new file mode 100644
index 00000000..9acb0cd8
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/tiny.vcf
@@ -0,0 +1,4 @@
+##fileformat=VCFv4.2
+#CHROM POS ID REF ALT QUAL FILTER INFO
+chr1 7 . CAC C . . .
+chr1 3 . C A . . .
diff --git a/tests/genomic-coordinates/fixtures/valid12.bed b/tests/genomic-coordinates/fixtures/valid12.bed
new file mode 100644
index 00000000..c218c249
--- /dev/null
+++ b/tests/genomic-coordinates/fixtures/valid12.bed
@@ -0,0 +1 @@
+chr1 1000 2000 tx 0 + 1000 2000 0 2 100,200, 0,800,
diff --git a/tests/genomic-coordinates/test_scripts.py b/tests/genomic-coordinates/test_scripts.py
new file mode 100644
index 00000000..8c6dccbf
--- /dev/null
+++ b/tests/genomic-coordinates/test_scripts.py
@@ -0,0 +1,654 @@
+"""Unit tests for the genomic-coordinates skill scripts.
+
+Everything runs offline against the fixtures in ``fixtures/``. The reference
+FASTA is ten bases long and deliberately contains a ``CACACAC`` repeat, which is
+what makes the left-alignment cases interesting.
+
+ uv run --with pytest python -m pytest tests/genomic-coordinates -q
+"""
+
+from __future__ import annotations
+
+import importlib.util
+import io
+import json
+import re
+import sys
+import tempfile
+import unittest
+from contextlib import redirect_stdout, redirect_stderr
+from pathlib import Path
+
+SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "genomic-coordinates"
+SCRIPTS_DIR = SKILL_ROOT / "scripts"
+FIXTURES = Path(__file__).resolve().parent / "fixtures"
+
+
+def _load_script(name: str):
+ """Load a bundled script as a module regardless of cwd."""
+ if str(SCRIPTS_DIR) not in sys.path:
+ sys.path.insert(0, str(SCRIPTS_DIR))
+ spec = importlib.util.spec_from_file_location(name, SCRIPTS_DIR / f"{name}.py")
+ assert spec and spec.loader
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[name] = module
+ spec.loader.exec_module(module)
+ return module
+
+
+common = _load_script("_common")
+convert_coords = _load_script("convert_coords")
+normalize_variant = _load_script("normalize_variant")
+check_contigs = _load_script("check_contigs")
+audit_intervals = _load_script("audit_intervals")
+
+
+def run_main(module, argv):
+ """Invoke a script's main() and capture (exit code, stdout, stderr)."""
+ out, err = io.StringIO(), io.StringIO()
+ with redirect_stdout(out), redirect_stderr(err):
+ code = module.main(argv)
+ return code, out.getvalue(), err.getvalue()
+
+
+def rows_from_tsv(text):
+ lines = [line for line in text.strip().splitlines() if line]
+ header = lines[0].split("\t")
+ return [dict(zip(header, line.split("\t"))) for line in lines[1:]]
+
+
+# ---------------------------------------------------------------------------
+# coordinate conversion
+# ---------------------------------------------------------------------------
+
+
+class ConventionTests(unittest.TestCase):
+ def test_round_trip_through_canonical_is_identity(self):
+ for name, conv in common.CONVENTIONS.items():
+ with self.subTest(format=name):
+ start, end = (10, 20) if conv.half_open else (11, 20)
+ canonical = common.to_canonical(conv, start, end)
+ self.assertEqual(common.from_canonical(conv, *canonical), (start, end))
+
+ def test_only_the_start_moves_between_conventions(self):
+ bed = common.get_convention("bed")
+ gff = common.get_convention("gff")
+ start, end = common.from_canonical(gff, *common.to_canonical(bed, 999, 1000))
+ self.assertEqual((start, end), (1000, 1000))
+
+ def test_length_is_convention_independent(self):
+ bed = common.to_canonical(common.get_convention("bed"), 999, 1000)
+ gff = common.to_canonical(common.get_convention("gff"), 1000, 1000)
+ self.assertEqual(bed, gff)
+ self.assertEqual(bed[1] - bed[0], 1)
+
+ def test_samtools_region_spans_both_endpoints(self):
+ # The samtools manual: chr3:1000-2000 is a 1001 bp region.
+ conv = common.get_convention("samtools")
+ contig, start, end, _ = common.parse_region("chr3:1000-2000", conv)
+ start0, end0 = common.to_canonical(conv, start, end)
+ self.assertEqual(contig, "chr3")
+ self.assertEqual(end0 - start0, 1001)
+
+ def test_unknown_format_is_rejected(self):
+ with self.assertRaises(ValueError):
+ common.get_convention("bed12ish")
+
+
+class RegionStringTests(unittest.TestCase):
+ def test_thousands_separators_are_accepted(self):
+ conv = common.get_convention("ucsc")
+ self.assertEqual(
+ common.parse_region("chr7:5,530,601-5,530,625", conv)[1:3],
+ (5530601, 5530625),
+ )
+
+ def test_ensembl_double_dot_and_strand(self):
+ conv = common.get_convention("ensembl")
+ contig, start, end, strand = common.parse_region("1:100..200:-", conv)
+ self.assertEqual((contig, start, end, strand), ("1", 100, 200, "-"))
+
+ def test_start_without_end_is_refused(self):
+ # In samtools, chr2:1000000 runs to the end of the contig -- not one base.
+ with self.assertRaises(ValueError) as caught:
+ common.parse_region("chr2:1000000", common.get_convention("samtools"))
+ self.assertIn("end of the contig", str(caught.exception))
+
+ def test_bare_contig_is_refused(self):
+ with self.assertRaises(ValueError):
+ common.parse_region("chr2", common.get_convention("samtools"))
+
+ def test_braced_hla_contig_round_trips(self):
+ conv = common.get_convention("samtools")
+ contig, start, end, _ = common.parse_region("{HLA-DRB1*12:17}:100-200", conv)
+ self.assertEqual(contig, "HLA-DRB1*12:17")
+ self.assertEqual(
+ common.format_region(contig, start, end), "{HLA-DRB1*12:17}:100-200"
+ )
+
+ def test_unbraced_colon_bearing_name_is_refused(self):
+ with self.assertRaises(ValueError) as caught:
+ common.parse_region(
+ "HLA-DRB1*12:17:100-200", common.get_convention("samtools")
+ )
+ self.assertIn("ambiguous", str(caught.exception))
+
+
+class ConvertCoordsCliTests(unittest.TestCase):
+ def test_bed_to_gff(self):
+ code, out, _ = run_main(convert_coords, ["--from", "bed", "--to", "gff",
+ "chr1", "999", "1000"])
+ self.assertEqual(code, 0)
+ row = rows_from_tsv(out)[0]
+ self.assertEqual(row["output"], "1000-1000")
+ self.assertEqual(row["length"], "1")
+
+ def test_ucsc_region_to_bed(self):
+ code, out, _ = run_main(
+ convert_coords,
+ ["--from", "ucsc", "--to", "bed", "chr7:5,530,601-5,530,625"],
+ )
+ self.assertEqual(code, 0)
+ row = rows_from_tsv(out)[0]
+ self.assertEqual(row["output"], "5530600-5530625")
+ self.assertEqual(row["length"], "25")
+
+ def test_zero_length_bed_feature_is_unrepresentable_in_gff(self):
+ code, out, _ = run_main(convert_coords, ["--from", "bed", "--to", "gff",
+ "chr1", "500", "500"])
+ self.assertEqual(code, 1)
+ row = rows_from_tsv(out)[0]
+ self.assertEqual(row["status"], "unrepresentable")
+
+ def test_zero_length_survives_a_half_open_target(self):
+ code, out, _ = run_main(convert_coords, ["--from", "bed", "--to", "pyranges",
+ "chr1", "500", "500"])
+ self.assertEqual(code, 0)
+ self.assertEqual(rows_from_tsv(out)[0]["status"], "zero_length")
+
+ def test_inverted_interval_is_invalid(self):
+ code, out, _ = run_main(convert_coords, ["--from", "gff", "--to", "bed",
+ "chr1", "200", "100"])
+ self.assertEqual(code, 1)
+ self.assertEqual(rows_from_tsv(out)[0]["status"], "invalid")
+
+ def test_list_covers_every_convention(self):
+ code, out, _ = run_main(convert_coords, ["--list"])
+ self.assertEqual(code, 0)
+ for name in common.CONVENTIONS:
+ self.assertIn(name, out)
+
+ def test_json_output_is_parseable(self):
+ _, out, _ = run_main(convert_coords, ["--from", "bed", "--to", "gff",
+ "chr1", "0", "10", "--format", "json"])
+ self.assertEqual(json.loads(out)[0]["length"], 10)
+
+ def test_reads_a_bed_file(self):
+ code, out, _ = run_main(
+ convert_coords,
+ ["--from", "bed", "--to", "gff", "--input", str(FIXTURES / "clean.bed")],
+ )
+ self.assertEqual(code, 0)
+ self.assertEqual(len(rows_from_tsv(out)), 3)
+
+
+# ---------------------------------------------------------------------------
+# variant normalisation
+# ---------------------------------------------------------------------------
+
+
+REF_FASTA = FIXTURES / "tiny.fa"
+
+
+class ReferenceTests(unittest.TestCase):
+ def test_fetch_is_zero_based_half_open(self):
+ reference = common.Reference(REF_FASTA)
+ # GGCACACACT
+ self.assertEqual(reference.fetch("chr1", 0, 2), "GG")
+ self.assertEqual(reference.fetch("chr1", 9, 10), "T")
+ self.assertEqual(reference.length("chr1"), 10)
+
+ def test_chr_prefix_is_resolved_both_ways(self):
+ reference = common.Reference(REF_FASTA)
+ self.assertEqual(reference.fetch("1", 0, 2), "GG")
+
+ def test_past_the_end_is_an_error_naming_the_assembly(self):
+ reference = common.Reference(REF_FASTA)
+ with self.assertRaises(common.ReferenceError) as caught:
+ reference.fetch("chr1", 5, 40)
+ self.assertIn("assembly", str(caught.exception))
+
+ def test_missing_contig_names_the_likely_cause(self):
+ reference = common.Reference(REF_FASTA)
+ with self.assertRaises(common.ReferenceError) as caught:
+ reference.fetch("chr22", 0, 1)
+ self.assertIn("check_contigs", str(caught.exception))
+
+ def test_indexed_and_unindexed_readers_agree(self):
+ """A .fai must give the same bases as reading the FASTA sequentially."""
+ with tempfile.TemporaryDirectory() as temporary:
+ fasta = Path(temporary) / "wrapped.fa"
+ sequence = "ACGT" * 25
+ fasta.write_text(
+ ">chr1\n" + "\n".join(sequence[i:i + 10] for i in range(0, 100, 10)) + "\n"
+ )
+ plain = common.Reference(fasta)
+ # offset of the first base = len(">chr1\n") = 6; 10 bases per line, 11 wide
+ Path(str(fasta) + ".fai").write_text("chr1\t100\t6\t10\t11\n")
+ indexed = common.Reference(fasta)
+ for start, end in [(0, 4), (8, 15), (0, 100), (95, 100), (37, 38)]:
+ self.assertEqual(
+ indexed.fetch("chr1", start, end),
+ plain.fetch("chr1", start, end),
+ f"disagreement on [{start}, {end})",
+ )
+
+
+class NormalisationTests(unittest.TestCase):
+ """Reference is GGCACACACT -- a CACACAC repeat at positions 3-9."""
+
+ def setUp(self):
+ self.reference = common.Reference(REF_FASTA)
+
+ def normalise(self, pos, ref, alt, **kwargs):
+ return normalize_variant.normalize(
+ self.reference, "chr1", pos, ref, alt, **kwargs
+ )
+
+ def test_deletion_left_aligns_through_the_repeat(self):
+ result = self.normalise(7, "CAC", "C")
+ self.assertEqual((result["pos"], result["ref"], result["alt"]), (2, "GCA", "G"))
+ self.assertEqual(result["shifted"], 5)
+ self.assertEqual(result["type"], "deletion")
+
+ def test_every_spelling_of_one_deletion_agrees(self):
+ forms = [(7, "CAC", "C"), (5, "CAC", "C"), (3, "CAC", "C"), (2, "GCA", "G")]
+ results = {
+ (r["pos"], r["ref"], r["alt"])
+ for r in (self.normalise(*form) for form in forms)
+ }
+ self.assertEqual(results, {(2, "GCA", "G")})
+
+ def test_insertion_left_aligns(self):
+ result = self.normalise(9, "C", "CAC")
+ self.assertEqual((result["pos"], result["ref"], result["alt"]), (2, "G", "GCA"))
+ self.assertEqual(result["type"], "insertion")
+
+ def test_untrimmed_snv_becomes_parsimonious(self):
+ result = self.normalise(3, "CA", "CT")
+ self.assertEqual((result["pos"], result["ref"], result["alt"]), (4, "A", "T"))
+ self.assertEqual(result["type"], "snv")
+ self.assertEqual(result["shifted"], -1)
+
+ def test_normalised_snv_is_untouched(self):
+ result = self.normalise(3, "C", "A")
+ self.assertEqual((result["pos"], result["ref"], result["alt"]), (3, "C", "A"))
+ self.assertEqual(result["shifted"], 0)
+
+ def test_normalisation_is_idempotent(self):
+ once = self.normalise(7, "CAC", "C")
+ twice = self.normalise(once["pos"], once["ref"], once["alt"])
+ self.assertEqual(
+ (once["pos"], once["ref"], once["alt"]),
+ (twice["pos"], twice["ref"], twice["alt"]),
+ )
+
+ def test_cannot_shift_past_the_contig_start(self):
+ result = self.normalise(1, "G", "GAC")
+ self.assertEqual(result["pos"], 1)
+ self.assertEqual(result["ref_check"], "ok")
+
+ def test_window_limits_the_shift_and_says_so(self):
+ result = self.normalise(7, "CAC", "C", window=2)
+ self.assertLessEqual(result["shifted"], 2)
+ self.assertIn("window", result["detail"])
+
+ def test_ref_mismatch_is_reported_not_normalised(self):
+ result = self.normalise(3, "A", "T")
+ self.assertEqual(result["ref_check"], "MISMATCH")
+ self.assertEqual(result["shifted"], 0)
+ self.assertIn("assembl", result["detail"])
+
+ def test_symbolic_allele_passes_through(self):
+ result = self.normalise(7, "CAC", "<DEL>")
+ self.assertEqual(result["type"], "symbolic")
+ self.assertEqual(result["ref_check"], "skipped")
+ self.assertEqual(result["pos"], 7)
+
+ def test_spanning_deletion_allele_passes_through(self):
+ self.assertEqual(self.normalise(7, "C", "*")["type"], "symbolic")
+
+ def test_ref_equal_to_alt_is_rejected(self):
+ with self.assertRaises(normalize_variant.VariantError):
+ self.normalise(3, "C", "C")
+
+ def test_zero_position_is_rejected(self):
+ with self.assertRaises(normalize_variant.VariantError):
+ self.normalise(0, "C", "A")
+
+
+class NormaliseCliTests(unittest.TestCase):
+ def test_compare_declares_identity(self):
+ code, out, err = run_main(
+ normalize_variant,
+ ["--fasta", str(REF_FASTA), "--compare", "chr1:7:CAC:C", "chr1:2:GCA:G"],
+ )
+ self.assertEqual(code, 0)
+ self.assertIn("identical", err)
+ self.assertEqual(len(rows_from_tsv(out)), 2)
+
+ def test_compare_declares_difference(self):
+ code, _, err = run_main(
+ normalize_variant,
+ ["--fasta", str(REF_FASTA), "--compare", "chr1:7:CAC:C", "chr1:3:C:A"],
+ )
+ self.assertEqual(code, 0)
+ self.assertIn("distinct", err)
+
+ def test_mismatch_sets_a_failing_exit_code(self):
+ code, out, _ = run_main(
+ normalize_variant, ["--fasta", str(REF_FASTA), "chr1", "3", "A", "T"]
+ )
+ self.assertEqual(code, 1)
+ self.assertEqual(rows_from_tsv(out)[0]["ref_check"], "MISMATCH")
+
+ def test_split_expands_multiallelic_alt(self):
+ code, out, _ = run_main(
+ normalize_variant,
+ ["--fasta", str(REF_FASTA), "--split", "chr1", "7", "CAC", "C,CACAC"],
+ )
+ self.assertEqual(code, 0)
+ self.assertEqual(len(rows_from_tsv(out)), 2)
+
+ def test_reads_a_vcf(self):
+ code, out, _ = run_main(
+ normalize_variant,
+ ["--fasta", str(REF_FASTA), "--input", str(FIXTURES / "tiny.vcf")],
+ )
+ rows = rows_from_tsv(out)
+ self.assertEqual(code, 0)
+ self.assertEqual(len(rows), 2)
+ self.assertEqual(rows[0]["normalized"], "chr1:2:GCA:G")
+
+ def test_missing_reference_is_a_usage_error(self):
+ code, _, err = run_main(
+ normalize_variant, ["--fasta", "/nonexistent.fa", "chr1", "1", "G", "A"]
+ )
+ self.assertEqual(code, 2)
+ self.assertIn("not found", err)
+
+
+# ---------------------------------------------------------------------------
+# assembly identification
+# ---------------------------------------------------------------------------
+
+
+class BuildTableTests(unittest.TestCase):
+ def test_every_build_lists_the_same_chromosomes(self):
+ keys = [set(lengths) for lengths in check_contigs.BUILDS.values()]
+ self.assertTrue(all(k == keys[0] for k in keys))
+ self.assertEqual(len(keys[0]), 24)
+
+ def test_builds_are_mutually_distinguishable(self):
+ """No two assemblies may share a full primary-chromosome signature."""
+ signatures = {
+ build: tuple(sorted(lengths.items()))
+ for build, lengths in check_contigs.BUILDS.items()
+ }
+ self.assertEqual(len(set(signatures.values())), len(signatures))
+
+ def test_grch37_and_grch38_differ_on_chr1(self):
+ self.assertNotEqual(
+ check_contigs.BUILDS["GRCh37/hg19"]["1"],
+ check_contigs.BUILDS["GRCh38/hg38"]["1"],
+ )
+
+
+class IdentifyTests(unittest.TestCase):
+ def test_hg19_is_identified_by_its_mitochondrion(self):
+ code, out, _ = run_main(
+ check_contigs, ["--identify", str(FIXTURES / "hg19.chrom.sizes")]
+ )
+ row = rows_from_tsv(out)[0]
+ self.assertEqual(code, 0)
+ self.assertEqual(row["assembly"], "hg19")
+ self.assertIn("16571", row["detail"])
+ self.assertEqual(row["naming"], "chr-prefixed")
+
+ def test_grch37_is_identified_by_its_mitochondrion(self):
+ _, out, _ = run_main(
+ check_contigs, ["--identify", str(FIXTURES / "GRCh37.fa.fai")]
+ )
+ row = rows_from_tsv(out)[0]
+ self.assertEqual(row["assembly"], "GRCh37")
+ self.assertEqual(row["naming"], "plain")
+
+ def test_grch38_is_identified(self):
+ _, out, _ = run_main(
+ check_contigs, ["--identify", str(FIXTURES / "hg38.chrom.sizes")]
+ )
+ self.assertEqual(rows_from_tsv(out)[0]["assembly"], "GRCh38/hg38")
+
+ def test_grch37_and_hg19_are_reported_as_incompatible(self):
+ code, _, err = run_main(
+ check_contigs,
+ [str(FIXTURES / "GRCh37.fa.fai"), str(FIXTURES / "hg19.chrom.sizes")],
+ )
+ self.assertEqual(code, 1)
+ self.assertIn("naming differs", err)
+ self.assertIn("16569", err)
+
+ def test_matching_files_are_compatible(self):
+ code, _, err = run_main(
+ check_contigs,
+ [str(FIXTURES / "hg19.chrom.sizes"), str(FIXTURES / "hg19.chrom.sizes")],
+ )
+ self.assertEqual(code, 0)
+ self.assertIn("no contig incompatibilities", err)
+
+ def test_vcf_header_lengths_are_read(self):
+ _, out, _ = run_main(
+ check_contigs, ["--identify", str(FIXTURES / "grch38.vcf")]
+ )
+ row = rows_from_tsv(out)[0]
+ self.assertEqual(row["kind"], "vcf header")
+ self.assertEqual(row["assembly"], "GRCh38/hg38")
+
+ def test_grch38_vcf_against_hg19_genome_fails(self):
+ code, _, err = run_main(
+ check_contigs,
+ [str(FIXTURES / "grch38.vcf"), "--genome", str(FIXTURES / "hg19.chrom.sizes")],
+ )
+ self.assertEqual(code, 1)
+ self.assertIn("different assemblies", err)
+
+ def test_bed_max_coordinate_is_not_treated_as_a_contig_length(self):
+ """A BED whose features are well inside the contigs must not look like a
+ length conflict just because the numbers are smaller."""
+ code, _, err = run_main(
+ check_contigs,
+ [str(FIXTURES / "clean.bed"), "--genome", str(FIXTURES / "hg19.chrom.sizes")],
+ )
+ self.assertEqual(code, 0)
+ self.assertIn("no contig incompatibilities", err)
+
+ def test_bed_past_the_contig_end_is_caught(self):
+ code, _, err = run_main(
+ check_contigs,
+ [str(FIXTURES / "overflow.bed"), "--genome", str(FIXTURES / "hg19.chrom.sizes")],
+ )
+ self.assertEqual(code, 1)
+ self.assertIn("past the end", err)
+
+ def test_unknown_extension_is_refused(self):
+ with self.assertRaises(SystemExit):
+ run_main(check_contigs, [str(FIXTURES / "readme.txt")])
+
+
+# ---------------------------------------------------------------------------
+# file auditing
+# ---------------------------------------------------------------------------
+
+
+class AuditBedTests(unittest.TestCase):
+ def test_clean_bed_has_no_findings(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "clean.bed")])
+ self.assertEqual(code, 0)
+ self.assertIn("no findings", out)
+
+ def test_one_based_data_in_a_bed_is_flagged(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "onebased.bed")])
+ rules = {row["rule"] for row in rows_from_tsv(out)}
+ self.assertIn("many_zero_length", rules)
+
+ def test_bed12_absolute_block_starts_are_fatal(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "bad_blocks.bed")])
+ self.assertEqual(code, 1)
+ self.assertIn("first_block_offset", {r["rule"] for r in rows_from_tsv(out)})
+
+ def test_valid_bed12_passes(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "valid12.bed")])
+ self.assertEqual(code, 0)
+ self.assertIn("no findings", out)
+
+ def test_space_separated_bed_is_named_as_such(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "spaced.bed")])
+ self.assertEqual(code, 1)
+ rules = {row["rule"] for row in rows_from_tsv(out)}
+ self.assertIn("space_separated", rules)
+ self.assertNotIn("too_few_columns", rules)
+
+
+class AuditGffTests(unittest.TestCase):
+ def test_zero_start_proves_zero_based_data(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "bad.gtf")])
+ self.assertEqual(code, 1)
+ rows = {row["rule"]: row for row in rows_from_tsv(out)}
+ self.assertIn("start_below_one", rows)
+ self.assertIn("1-based", rows["start_below_one"]["detail"])
+
+ def test_end_before_start_is_fatal_regardless_of_strand(self):
+ _, out, _ = run_main(audit_intervals, [str(FIXTURES / "bad.gtf")])
+ self.assertIn("end_before_start", {r["rule"] for r in rows_from_tsv(out)})
+
+ def test_clean_gtf_passes(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "clean.gtf")])
+ self.assertEqual(code, 0)
+ self.assertIn("no findings", out)
+
+
+class AuditVcfTests(unittest.TestCase):
+ def test_vep_dash_allele_is_fatal(self):
+ code, out, _ = run_main(audit_intervals, [str(FIXTURES / "messy.vcf")])
+ self.assertEqual(code, 1)
+ rows = {row["rule"] for row in rows_from_tsv(out)}
+ self.assertIn("bad_alt_allele", rows)
+ self.assertIn("pos_below_one", rows)
+ self.assertIn("mixed_contig_naming", rows)
+
+ def test_untrimmed_alleles_are_warned_about(self):
+ _, out, _ = run_main(audit_intervals, [str(FIXTURES / "messy.vcf")])
+ rows = {row["rule"]: row for row in rows_from_tsv(out)}
+ self.assertEqual(rows["not_parsimonious"]["severity"], "warn")
+
+ def test_contig_order_not_coordinate_order_drives_the_sort_check(self):
+ """Moving to a new contig is not 'unsorted'; revisiting one is."""
+ _, out, _ = run_main(audit_intervals, [str(FIXTURES / "clean.vcf")])
+ rules = {row["rule"] for row in rows_from_tsv(out)}
+ self.assertNotIn("unsorted", rules)
+ self.assertNotIn("interleaved_contigs", rules)
+
+ def test_interleaved_contigs_are_caught(self):
+ _, out, _ = run_main(audit_intervals, [str(FIXTURES / "interleaved.vcf")])
+ self.assertIn("interleaved_contigs", {r["rule"] for r in rows_from_tsv(out)})
+
+
+class AuditCliTests(unittest.TestCase):
+ def test_format_can_be_forced(self):
+ code, out, _ = run_main(
+ audit_intervals, [str(FIXTURES / "clean.bed"), "--format", "bed"]
+ )
+ self.assertEqual(code, 0)
+
+ def test_unknown_extension_is_refused(self):
+ with self.assertRaises(SystemExit):
+ run_main(audit_intervals, [str(FIXTURES / "tiny.fa")])
+
+ def test_missing_file_is_a_usage_error(self):
+ code, _, err = run_main(audit_intervals, ["/nonexistent.bed"])
+ self.assertEqual(code, 2)
+ self.assertIn("no such file", err)
+
+ def test_examples_are_capped(self):
+ code, out, _ = run_main(
+ audit_intervals, [str(FIXTURES / "many_bad.gtf"), "--max-examples", "2"]
+ )
+ rows = [r for r in rows_from_tsv(out) if r["rule"] == "start_below_one"]
+ self.assertEqual(len(rows), 3) # two examples plus the suppression notice
+ self.assertEqual(rows[-1]["line"], "...")
+
+ def test_suppressed_findings_still_count_towards_the_exit_code(self):
+ code, _, err = run_main(
+ audit_intervals, [str(FIXTURES / "many_bad.gtf"), "--max-examples", "1"]
+ )
+ self.assertEqual(code, 1)
+ self.assertIn("10 fatal", err)
+
+
+# ---------------------------------------------------------------------------
+# skill structure
+# ---------------------------------------------------------------------------
+
+
+class SkillStructureTests(unittest.TestCase):
+ def test_frontmatter_name_matches_the_directory(self):
+ text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
+ self.assertRegex(text, r"^---\nname: genomic-coordinates\n")
+
+ def test_version_is_a_quoted_string(self):
+ text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
+ self.assertIn('version: "1.0"', text)
+
+ def test_skill_md_is_within_the_line_budget(self):
+ lines = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8").splitlines()
+ self.assertLess(len(lines), 500)
+
+ def test_no_tests_ship_inside_the_skill(self):
+ self.assertEqual(list(SKILL_ROOT.rglob("test_*.py")), [])
+ self.assertEqual(list(SKILL_ROOT.rglob("*.pyc")), [])
+
+ def test_local_markdown_links_resolve(self):
+ pattern = re.compile(r"\[[^\]]*\]\(([^)]+)\)")
+ docs = [SKILL_ROOT / "SKILL.md", *sorted((SKILL_ROOT / "references").glob("*.md"))]
+ for path in docs:
+ for target in pattern.findall(path.read_text(encoding="utf-8")):
+ if target.startswith(("http://", "https://", "#", "mailto:")):
+ continue
+ with self.subTest(doc=path.name, target=target):
+ self.assertTrue((path.parent / target.split("#")[0]).exists())
+
+ def test_every_referenced_file_exists(self):
+ text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
+ for name in re.findall(r"`(references/[\w.-]+|scripts/[\w.-]+)`", text):
+ with self.subTest(path=name):
+ self.assertTrue((SKILL_ROOT / name).exists())
+
+ def test_every_script_is_mentioned_in_the_skill(self):
+ text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
+ for script in SCRIPTS_DIR.glob("*.py"):
+ if script.name.startswith("_"):
+ continue
+ with self.subTest(script=script.name):
+ self.assertIn(script.name, text)
+
+ def test_scripts_import_nothing_outside_the_standard_library(self):
+ third_party = {"numpy", "pandas", "pysam", "requests", "scipy", "yaml"}
+ for script in SCRIPTS_DIR.glob("*.py"):
+ text = script.read_text(encoding="utf-8")
+ for module in re.findall(r"^\s*(?:import|from)\s+([\w.]+)", text, re.M):
+ with self.subTest(script=script.name, module=module):
+ self.assertNotIn(module.split(".")[0], third_party)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tests/skill-requirements.toml b/tests/skill-requirements.toml
index 85a62ce3..d6df5717 100644
--- a/tests/skill-requirements.toml
+++ b/tests/skill-requirements.toml
@@ -445,6 +445,9 @@ packages = ["pandas"]
# --- genomics and sequence analysis ---------------------------------------
+[skills.genomic-coordinates]
+packages = []
+
[skills.biopython]
packages = ["biopython", "matplotlib", "numpy", "reportlab"]