Skip to content

search: dynamic PAM filter (--prune-pam) — one PAM-less index serves any PAM ≤ flank width - #46

Merged
lucapinello merged 25 commits into
masterfrom
feat/dynamic-pam-filter
Oct 30, 2026
Merged

lucapinello merged 25 commits into
masterfrom
feat/dynamic-pam-filter

Conversation

@lucapinello

@lucapinello lucapinello commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

CRISPRitz 2.9.1

A patch release: one additive, opt-in search feature plus correctness fixes to the search engine, the index builder and the enricher. Without --prune-pam, searches on existing indexes are byte-identical to 2.9.0, and dedicated (per-PAM) indexes build bit-identically. Full details are in CHANGELOG.md under [2.9.1].

Added

  • search --prune-pam: one PAM-less index serves any 3′ PAM. Build an index with an all-N flank of width K (K = 8 recommended), then search any 3′ PAM of length L ≤ K, such as NGG, NRG, NGA or NNGRRT.
    • Candidates whose stored flank does not match the search PAM are skipped at leaf emit, before they are written.
    • A shorter 3′ PAM is N-padded at its distal end.
    • The targets are the same rows, with the same coordinates and sequences, as a dedicated per-PAM index's.
    • A 5′ PAM needs an index of exactly its width; a shorter one is refused. L > K is a hard error.
    • Also enabled by the 12th searchTST argument, or by CRISPRITZ_PAM_PRUNE set to any value but empty or 0.
    • A wider flank costs +2 bytes per leaf: +9.3% for K = 8 vs K = 3.
  • New CI coverage:
    • test/run_pam_filter_test.sh (31 assertions)
    • test/run_flank_n_test.sh
    • test/run_short_contig_test.sh
    • test/run_add_variants_status_test.sh
    • verify_enricher.sh: 17 cases; both enricher ports must produce byte-identical outputs, file sets and run logs.

Changed

  • The compiled enricher is installed by conda/build.sh, so it is now the default enrichment path. It is byte-identical to enricher.py on the full chr22 of HGDP + 1000 Genomes, and 1.82x faster.

Fixed

  • Search:
    • A non-.bin file in the index directory (a sidecar or a subdirectory) crashed the search. Such entries are now skipped, and a directory with no .bin fails loudly.
    • A PAM/flank width mismatch mis-parsed the index, and the crash looked like an empty result. The width check now always runs.
    • buildTST, searchTST and searchBruteForce now have their exit status checked, so a crash fails the command.
  • Index build:
    • A genomic N in the stored PAM/flank region dropped the target from PAM-less indexes; synthetic indel genomes were hit hardest.
    • A contig too short for one target window, such as a header-only FASTA, ran buildTST out of memory with an all-N PAM, or segfaulted with a constrained one. It is now skipped.
  • Enricher:
    • The C++ port dropped a SNP wherever an indel shared its position. No published index is affected.
    • A multiallelic site compounded its indel alleles into one contig.
    • Indel windows that would start before the chromosome start are skipped in both ports.
    • enricher.py dropped a real SNP at a multiallelic site whose ALTs include *, N or a lowercase base.
  • add-variants:
    • It now exits non-zero, naming the chromosome, when an enricher fails or is killed.
    • It runs enricher.py under its own interpreter; Ubuntu 24.04 has no python.
    • It creates the shared enriched-genome directory race-safely.

Known issues

  • Profile outputs under --prune-pam: the profiles (-p/-t) differ from a dedicated index's, while the targets list is identical.
    • The profiles count the engine's duplicate rows, and how many there are depends on the shape of the index tree.
    • Their guide label carries the PAM padded to K.
    • CRISPRme does not read the profiles.
  • Several SNP records at one position: only the last record's ALT is kept, in both enricher ports. This is pre-existing and is being investigated separately.

🤖 Generated with Claude Code

lucapinello and others added 4 commits October 24, 2026 21:42
…es a restrictive PAM at leaf emit)

Reconstructed baseline of the prior work (searchOnTST leaf-time PAM skip +
crispritz.py --prune-pam + test/run_pam_filter_test.sh). Equivalent to the
unpushed commit 11e4d07 on /tmp/CRISPRitz-v290.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…any PAM <= K via N-padding

Relax the --prune-pam length guard from == to <= the stored flank width K:
a search PAM of length L <= K is N-padded to width K at the distal side (end
for a 3prime PAM, front for a 5prime PAM — real PAM letters are proximal to the
protospacer, pad-N covers the outer stored positions). L > K stays a hard error.
Build ONE generous all-N flank index (recommended K=8) and serve any PAM up to K
with no dedicated per-PAM index; proven same-loci as a dedicated index (3prime NGG
and 5prime TTTV) in the extended test.

- searchOnTST.cpp: defer searchPamBit build to the guard (once K is known from
  the .bin prefix); N-pad pamRNA to K on the correct side; L>K hard error.
- crispritz.py: updated --prune-pam help (generous-flank / N-padding workflow).
- README section 3.3.3: build recipe + search workflow + measured size table.
- CHANGELOG [Unreleased]: feature + linear size-cost (+2 bytes/leaf, +9.3%).
- test/run_pam_filter_test.sh: +14 assertions (K=8 build, N-pad PAM partition for
  NGG/NRG/NRR/NNNN/6-nt, 8-flank==3-flank NGG equivalence, 5prime TTTV, L>K error,
  K=8 default-off byte-identity). 32 assertions total, all pass.

Measured (5 Mb hg38 chr22 slice, ref-only mismatch index, ~10M leaves):
K=3 216.0 MB vs K=8 236.0 MB => +2 bytes/leaf (+9.3%); full-hg38 ~134 -> ~146 GB.
Flank is 4-bit-packed (2 bases/byte): linear per-leaf cost, NOT a multiplicative
explosion. Golden + minimal regression tests still byte-identical.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
When a search PAM of length L is applied via --prune-pam to a PAM-less index of
stored flank width K with L < K (N-padded L->K), the K-wide window starts (K-L)
nt further left on the MINUS strand, so the reported minus-strand coordinate was
(K-L) smaller than a dedicated L-PAM index. Plus-strand was already correct
(protospacer start is flank-independent).

Add prune_pad = (K-L) to the minus-strand Position + Cluster Position in the 3'
padded case (pam_at_start ? 0 : prune_pad). No-op when not pruning or L==K
(prune_pad stays 0) -> byte-identical to the previous default path.

PROVEN: K=8 NNNNNNNN index + --prune-pam NRG == dedicated NRG, chr22 ACTGA 4/1/1,
SET-IDENTICAL with NO coordinate normalization (23,862=23,862, 0/0; minus-strand
13,488=13,488, 0/0). Was off-by-5 on all 13,488 minus-strand rows before.

5' (pam_at_start) coordinate reconciliation is intentionally left as 0 (deferred):
no 5' index ships yet; a future 5' index needs its own verification.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…d 3' search

On a 3' padded --prune-pam (L<K) the output crRNA/DNA showed the full K-wide stored
flank (e.g. ...NNNNNNNN for K=8) instead of the effective L-nt PAM. Emit only the
proximal (K - prune_pad) PAM chars: (K-prune_pad) Ns to the guide crRNA, and the first
(K-prune_pad) genomic flank chars to the target DNA. No-op when prune_pad==0 ->
byte-identical default; 5' (g.insert path) untouched.

With this + the minus-strand coord fix, K=8 NNNNNNNN + --prune-pam NRG is now
BYTE-IDENTICAL to a dedicated NRG index: chr22 ACTGA 4/1/1, all 10 columns incl
crRNA+DNA sequences, ZERO normalization, md5-equal (23,862=23,862, 0/0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lucapinello

Copy link
Copy Markdown
Contributor Author

Follow-up: padded-prune output is now byte-identical to a dedicated index

Validating the K=8 generous-flank index against a dedicated NRG index (ACTGA, 4mm/1/1, hg38 HGDP+1kGP chr22) surfaced two output-reconciliation bugs in the padded --prune-pam path (search PAM length L < stored flank width K). Both are now fixed:

  • 8b3aeaa — minus-strand coordinate. The K-wide window starts (K−L) nt further left on the minus strand, so the reported minus-strand Position/Cluster Position was (K−L) smaller than a dedicated L-PAM index. Add prune_pad = (K−L) to the minus-strand coordinate for a 3′ padded prune. Plus-strand was already correct.
  • 206f3ec — crRNA/DNA sequence columns. The output showed the full K-wide stored flank (…NNNNNNNN) instead of the effective L-nt PAM; now emits only the proximal (K−prune_pad) PAM chars.

Both are no-ops when not pruning or L==K (prune_pad stays 0) → byte-identical to the previous default path. 5′ (pam_at_start) reconciliation is intentionally deferred (no 5′ index ships; needs its own verification).

Proof (re-run, zero normalization): NNNNNNNN_3 (K=8) + --prune-pam NRG == a dedicated NRG_3 index — SET-IDENTICAL across all 10 columns incl. crRNA + DNA sequences, md5-equal (23,862 = 23,862; minus-strand 13,488 = 13,488; 0/0). Before: all 13,488 minus-strand rows were off by 5 bp and the sequence columns carried 5 extra flank chars.

Measured cost on the same chr22 slice (8 threads): K=8+prune-NRG ≈ 450 s vs dedicated NRG ≈ 97 s ≈ 4.6× slower (expected — the pam-less tree is traversed in full; the win is one index serving any PAM ≤ K).

lucapinello and others added 21 commits October 26, 2026 15:20
…o CI

The existing equivalence assertions used K==L (NNN=3 vs NGG=3) so they never
exercised the PADDED path (L<K) where the minus-strand coord + output-PAM-width
bugs lived. Now:
- Add a padded-3prime equivalence (NNNNN K=5 + NGG) asserting byte-identity to a
  dedicated NGG index (all columns incl coords+sequences).
- Rewrite sections 8 & 9 (K=8 generous flank): previously compared with normalizers
  that COMPENSATED for the old buggy output (distal-flank trim + no coord adjust);
  now that padded output is byte-identical to a dedicated index, assert plain SET
  equality against a freshly-built dedicated index for each short PAM
  (NGG/NRG/NRR/NNNN/NGGNRN) and against the cas9 golden for NGG. mm+bulges exercise
  both emit paths. 5prime (section 10) is unchanged (the fix does not touch 5prime).
- Wire test/run_pam_filter_test.sh into CI as a new pam-filter job.

28 assertions pass; this suite now fails on any (K-L) coordinate or output-PAM-width
regression in the padded --prune-pam path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ogress to stdout

searchTST scanned the index directory and loaded EVERY entry as a TST tree, so a
provenance sidecar (.pam_build, index_meta.json, .display_label) or a subdirectory
was read as a corrupt tree (numNodes=0) -> out-of-bounds write in deSerialize -> SIGSEGV.
This surfaced on PAM-less (all-N) indexes, which ship a .pam_build sidecar; a dedicated
per-PAM index does not, which is why only --prune-pam/PAM-less searches hit it.

- Filter the directory scan to REGULAR *.bin files (S_ISREG + anchored ".bin" suffix;
  rejects .pam_build / index_meta.json / a subdir named *.bin / foo.bin.tmp).
- loadTST fails fast on an implausible numLeaves/numNodes or a short read.
- Error loudly when an index dir contains no .bin trees (no silent zero-result).
- Move the informational [PAM-FILTER] progress lines from stderr to stdout; genuine
  errors and the L>K hard error stay on stderr.

Verified on the full hg38 NNN (K=3) and NNNNNNNN (K=8) indexes at th=8: crash-free,
empty stderr, bit-identical across repeated runs, and byte-identical to a dedicated NGG
index (3368 rows, same md5). L>K still errors with a nonzero exit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… *.bin subdir)

Section 12 of run_pam_filter_test.sh: build a valid NNN index, add a .pam_build
provenance dotfile, an index_meta.json sidecar, and a SUBDIRECTORY named *.bin, then
run searchTST at th=8 with --prune-pam. Asserts the search (a) does NOT crash (rc=0),
(b) writes NOTHING to stderr (so CRISPRme _search_end_or_fail does not false-fail on a
success), and (c) returns a target set identical to a clean NNN+NGG search. This guards
the fix in the preceding commit (directory scan now skips non-regular-.bin entries).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…mic N

A genomic 'N' converts to the 0000 nibble, which intersects nothing, so it
failed a PAM match at every position even when the PAM stated no requirement
there. Three separate gates acted on that, which is why fixing any one of them
in isolation changed nothing:

  1. searchPAMonGenome tested every PAM position against the genome. The loop
     runs to the stored flank width, so a PAM-less index of width K rejected
     strictly more sites than a dedicated index of a shorter PAM over the same
     guide region.
  2. The build discarded any target with an 'N' anywhere in the extracted
     window, whose width is pamlen + max_bulges. This was the dominant gate.
  3. pamPrune_reject tested the N-pad that widens a search PAM to the stored
     width, so a stored 'N' at a pad position rejected the target.

Gate 1 is now relaxed only when the PAM is fully unconstrained; gate 2 is
narrowed to the guide region, which genuinely cannot hold an 'N' because the
tree has no encoding for one; gate 3 tests only the positions the real PAM
covers.

Carrying 'N' through to the tree needed two encoding defects fixed first, which
is why an earlier attempt to decode 0000 as 'N' produced no targets at all:

  - the writer shifted the nibble conditionally on its VALUE, so a nucleotide
    encoding to 0x0 lost its slot and displaced every following nibble. It now
    shifts on the pending count, which is byte-identical for N-free flanks.
  - the loader's `case 0x0` was a no-op. It is unreachable in a well-formed
    stream, since an odd PAM width is handled by the j == 0 shift, so it is free
    to carry 'N'.

Which end of the extracted window holds the PAM depends on the PAM side AND the
strand -- a minus-strand 3' PAM sits at the FRONT, because that branch
complements without reversing. Assuming "3' PAM is always at the back" admits
guide-region N's into tree keys, which silently produces empty target files, so
guideRegionHasN takes the end explicitly at all four call sites.

Impact is concentrated on the synthetic indel genome, which is N-separated every
~56 bases: counting usable window starts on one contig, a width-31 window saw
34,690,315 against a width-26 window's 41,454,105, so 16.3% of start positions
were invisible to a PAM-less index. Real assembly N sits in megabase gaps where
nothing matches, which is why reference and SNP searches were unaffected.

A dedicated index built by the patched builder is bit-identical to one built by
the pristine builder, and the patched engine returns identical sorted rows on
pre-fix indices, so published indices are unaffected.

test/run_flank_n_test.sh plants targets on BOTH strands with the 'N' at an exact
flank offset and asserts that the dedicated index finds exactly the planted set,
so equivalence cannot pass by both sides being equally wrong.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…one contig

indel_to_fasta extracts one reference window per VCF record, then loops over the
record's carried indel alleles emitting one fake contig each. The substitution
result was assigned back into the window, so allele 2 was applied on top of
allele 1's output: its contig carried BOTH insertions -- a haplotype no sample
need carry -- and because refseq and end_fake_pos both derive from the window's
length, the logged reference sequence and the fake->real coordinate mapping
drifted with it.

Measured on a synthetic site G -> GA, GAA. The second allele's contig was
...CCG AAA GCTTCG (three A's, for an allele that inserts two) against ...CCG AA
GCTTCG, with FAKEPOS 56 bases against 55.

Not reachable through CRISPRme's own VCFs, which are split: 0 of 1,998,332 chr22
records in the shipped HGDP+1kGP panel carry a comma in ALT, so the loop runs
once per record and the reassignment cannot compound. `gnomAD-converter
--multiallelic` deliberately merges variants into multiallelic sites, and those
VCFs feed add-variants. Dropping the `break` in the carrier scan (#44) raised the
exposure, since more alleles now reach the emit gate. Across the shipped index's
own logs, 781,032 of 14,641,550 sites have two or more carried indel alleles,
which would be 2,550,831 affected contigs -- 14.8% of all indel windows.

Each allele is now derived from the reference window. For a single-allele record
the two are byte-identical, so no existing index moves.

Also: start_position = POS - 26 had no floor, and the window is anchored with the
REF base at index 25, which a negative start cannot satisfy. Python sliced from
the far end of the chromosome and returned an EMPTY window; C++ passed a negative
offset to substr, which converts to size_t and throws. So the ports did not even
agree on it. Both now skip the site with a warning on stdout -- not stderr, which
is fatal to the CRISPRme stage that calls this. No shipped index is affected: 0
of 17,192,381 log rows have a negative window start, hg38 chromosomes opening
with a telomeric N run.

verify_enricher.sh covered a "multiallelic indel" that was one SNP allele plus
one insertion, so its indels list had a single entry and the per-allele loop ran
once -- it could not catch this. Added a site with two carried indel alleles, and
wired the script into CI: the packaged install falls back to enricher.py when the
binary is missing, so the two ports must not drift.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Version pins in crispritz.py and the in-repo conda stub, and the changelog's
[Unreleased] dated as 2.9.1 with the three fixes this release carries alongside
--prune-pam: the non-.bin index-directory crash, the genomic-N flank gates, and
the enricher's multiallelic indel compounding.

--prune-pam is additive and a strict no-op when the flag is absent, so this is a
patch release rather than a minor one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
… the index

loadTST consumes exactly ceil(pamRNA.size()/2) stored PAM bytes per leaf, so the
search PAM length must equal the width the index stored. Only the --prune-pam path
reconciled that -- it reads the stored width K off the .bin filename prefix, errors
when L > K, and N-pads the search PAM up to K. With the filter OFF none of that ran,
so a width-8 PAM-less index searched with a 3-nt PAM desynced the leaf stream at the
first leaf: every later PAM, "next" link and finally numNodes was read from a
shifted offset, ending in a SIGSEGV.

That is not a loud failure. A segfault writes nothing to stderr and leaves a 0-byte
targets file, and subprocess.run's return code was never inspected, so the caller
saw a completed step with no output and reported a clean, empty off-target result.
An empty report is indistinguishable from a real negative, which makes it the worst
possible outcome for an off-target screen.

Three changes:
  - the stored-width check moves OUT of the PAM_PRUNE branch, so any width mismatch
    is refused up front with an error naming both widths, and pointing at
    --prune-pam when the index is PAM-less.
  - --prune-pam now refuses a SHORTER PAM off a wider flank for a 5' PAM. The loci
    were right -- which is all the equivalence assertion checked, and it passed --
    but the emit path trims prune_pad from the END of the reported crRNA/DNA while a
    5' pad sits at the FRONT, so every row carried pad N's inside its PAM region. A
    downstream PAM filter then drops all of them. Refused until the 5' emit and
    coordinate reconciliation is implemented; equal-width 5' is unaffected.
  - buildTST, searchTST and searchBruteForce now run through _run_checked, which
    exits non-zero on a bad status and names the signal. Nothing inspected these
    before, so any crash in any of them looked like a completed step.

Every 3' path stays byte-identical to its dedicated index (NGG, NRG, NRR, NNNN,
NGGNRN off a K=8 flank), and the golden suite is unchanged. The 5' assertion becomes
a refusal test, plus a new one for the supported equal-width 5' case and one for the
width mismatch with the filter off: 31 assertions.

Found by adversarial review; the desync was reproduced against the compiled engine.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…s position

add_variants.sh called the compiled enricher "a byte-identical port of
enricher.py". It was not, and verify_enricher.sh passed anyway.

python's iupac_code holds only the 2-, 3- and 4-base combinations -- 12 + 24 + 24
entries, none of length 1 -- so when a record's allele set collapses to a single
base the lookup finds no key and the enriched genome is left UNTOUCHED.
iupacFromMask instead returned that base (case 1/2/4/8 -> 'A'/'C'/'G'/'T') and
the caller wrote it, clobbering the IUPAC code a SNP record at the same position
had already placed.

The set collapses on EVERY indel record: REF is length 1, so SNPsProcess does not
bail out, and the longer ALT is skipped by the size() > 1 test, leaving only the
reference base. So a position carrying both a SNP and an indel as SEPARATE
biallelic records -- how every CRISPRme-supplied VCF is normalised -- lost its
SNP from the enriched genome. That is exactly the co-occurrence the SNP+indel
feature exists to find.

Measured through the real `crispritz.py add-variants` CLI on a 120,000-record
chr22 slice with 4,091 samples: 176 positions were the ONLY bytes differing from
enricher.py in 50.8 MB -- R->G x39, Y->T x38, M->C x37, W->T x30, K->T x20,
S->G x12, every one a position with a SNP record and an indel record at the same
POS. After the fix all five outputs are byte-identical, and the port is 1.75x
faster (51s vs 89s on that slice).

NO PUBLISHED INDEX IS AFFECTED. conda/build.sh compiles `enricher` and never
installs it, and CRISPRme's Dockerfile builds only buildTST/searchTST/
searchBruteForce, so crispritz.py `_enricher_command()` has always fallen back to
enricher.py. Verified inside the shipped image: no `enricher` on PATH and none
beside the reference, only enricher.cpp.

verify_enricher.sh could not have caught this: its SNP+indel case put both
alleles in one MULTIALLELIC record, while 0 of 1,998,332 real chr22 records carry
a comma in ALT. New case 8 plants a SNP and an indel at one position as two
separate biallelic records and reproduces the defect on the fixture
(GGCTAWCACATCG vs GGCTAACACATCG), then passes after the fix.

All five suites green: golden (byte-for-byte), minimal, pam-filter (31
assertions), flank-N, verify_enricher (BYTE-IDENTICAL: PASS).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…t path

conda/build.sh compiled `enricher` and was the only place that never installed
it, so a packaged install fell back to enricher.py while a source install
(CRISPRme's install_from_source.sh) used the compiled port -- a silent divergence
in which implementation enriches a genome. It is now copied to both places
crispritz.py's _enricher_command() checks, in its order: $PREFIX/bin (PATH) and
beside the reference implementation.

Measured 1.75x faster than enricher.py through the real `add-variants` CLI on
chr22 with HGDP + 1000 Genomes (4,091 samples), on the stage that dominates a
from-scratch genome-wide build.

Platform-safe by construction: zlib is already in the recipe's build/host/run
deps, and enricher.cpp contains no OpenMP pragmas (0 matches for omp.h or
#pragma omp), so the -fopenmp in Makefile_conda is inert for it. It therefore
ships wherever buildTST/searchTST/searchBruteForce already do, through the same
compiler invocation and the same copy step.

Only safe as of this release: before the iupacFromMask fix in 5de91ae the port
wrote a plain base where enricher.py writes an IUPAC code at any position
carrying both a SNP and an indel, silently dropping the SNP from the enriched
genome. verify_enricher.sh case 8 now covers that shape and runs in CI.

Also corrects the exposure note in the 2.9.1 CHANGELOG: it said the compiled port
"never ran in a shipped install", which is true of the image and the conda
package but NOT of install_from_source.sh, which has installed it for some time.
No published index is affected either way -- confirmed by measuring the artifacts
rather than reasoning from the build recipe: chr22:10684299 carries a SNP (C>A)
and an indel (C>CTATA) as separate records, and every enriched chr22.enriched.fa
on the build host holds the IUPAC `M` there, not a plain `C`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The install entry cited a 120,000-record slice. Re-run through the real
add-variants CLI on all 1,998,332 chr22 records of HGDP + 1000 Genomes (4,091
samples, 2.7 GB of output): all five outputs match enricher.py hash for hash, and
the compiled port takes 974 s against 1,776 s -- 1.82x, slightly better than the
slice suggested.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…ect a stale note

conda/build.sh installs the compiled enricher as of 2.9.1, but add_variants.sh
still said the recipe "compiles this binary and does NOT install it, so a
packaged install falls back to enricher.py". That was true when written and is
now the opposite of what the recipe does; it records the change instead.

The CHANGELOG entry for installing the binary claimed full-chr22 byte-identity on
Linux/libstdc++ only, while the package also ships to osx. Measured: built
natively on macOS 15.7 arm64 with Apple clang 17 (libc++), the port reproduces
the five full-chr22 output hashes of the Linux run exactly (1,998,332 records,
4,091 samples), and enricher.py does too under pandas 3.0.5 -- relevant because
the recipe leaves pandas unpinned and the indel log is written through a
DataFrame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… set and run log

verify_enricher.sh covered 8 variant shapes. Cases 9-16 add, one per cell of the
SNP+indel matrix: SNPs inside an indel's flanking window on the same haplotype,
the other haplotype and another sample, at and just past the window edges; a
deletion spanning SNP positions; indels with overlapping windows; multiallelic
records mixing SNP and indel ALTs (ALT order, one AF for three ALTs, 11 ALTs so
GT indices reach 10 and 11, a symbolic <DEL>); unphased, missing, haploid and
GT:DP genotypes; a REF that does not match the FASTA, lowercase REF/ALT, a FASTA
N run and an IUPAC base; same-POS SNP/indel orders beyond case 8, including a
split multiallelic SNP (both ports keep only the last record's ALT; pinned for
port agreement only); and the chromosome edges (window floor, end truncation).
The chromosome grows to 1,800 bp; its first 400 bp are the same draws, so cases
1-8 are unchanged. Besides the four outputs, the set of files each port writes
and the run log (stdout+stderr, the skip warnings included) must now match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…clude '*', 'N' or a lowercase base

enricher.py built a SNP's IUPAC code from the set {FASTA base} + {1-char ALTs}
and looked the joined string up among A/C/G/T spellings, so one member that is
not A/C/G/T left the base unchanged. At a GATK-style multiallelic record such
as C>A,* (the '*' of a spanning deletion) the real SNP A was therefore never
coded, and an off-target needing it could not be found, although the SNP dict
listed its carriers. enricher.cpp coded the A/C/G/T subset instead, so the two
ports also disagreed on such input.

enricher.py now follows enricher.cpp's rule: each member is an A/C/G/T bit, any
other member adds nothing, and the code is written when two or more bases
remain. Byte-identical to before wherever enricher.py wrote a code; the change
reaches only multiallelic records mixing A/C/G/T ALTs with another member, or a
FASTA N carrying two ALTs. Split (biallelic) input, which every CRISPRme-supplied
VCF is, cannot trigger it.

verify_enricher.sh case 17 pins it: C>A,* -> M; G>A,C,* -> V; A>C,N -> M;
G>A,c -> R (a lowercase ALT is ignored by both ports, as before); a FASTA N
with A,C -> M; and a split T>A record followed by T>C,* -> Y. Both ports agree
on all 17 cases; the previous enricher.py differs from enricher.cpp on case 17,
in the enriched genome only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pandas for enricher.py

add-variants runs one enricher per chromosome through Pool.apply_async and never
read the results, so an enricher that crashed, was killed (OOM, SIGKILL) or ran
out of disk left add-variants at exit 0, with that chromosome's enriched genome,
SNP dict, indel log and contigs missing or truncated. CRISPRme's index build
trusts that exit status alone and then indexes whatever exists, so a genome-wide
build could ship without a chromosome and say nothing.

The worker now returns the enricher's exit status, and the parent checks every
result after join(), names each failed chromosome with its exit code or signal
on stderr, and exits 1. The worker deliberately does not exit itself (as
_run_checked does): a SystemExit in a Pool worker loses its result and leaves
pool.join() waiting forever. _exit_detail is shared with _run_checked.

test/run_add_variants_status_test.sh pins it with a stub enricher on PATH: all
succeed -> 0; one exits 3 -> 1, named with "exit code 3"; one killed -> 1, named
with "signal SIGKILL"; and the shipped enricher on valid input -> 0 with the
enriched FASTA written. Each run is under a timeout, so a hang fails the test.
The previous crispritz.py exits 0 where the stub exits 3.

ci.yml: the enricher-ports job installs python3-pandas. enricher.py imports
pandas and the runner's python3 has none, so the job could not pass as
committed (reproduced locally in ubuntu:24.04: ModuleNotFoundError); it also
runs the new test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es, a known issue; correct counts

- Fixed gains the b5e5e17 entry (a PAM/flank width mismatch mis-parsed the index
  and the crash read as an empty result; buildTST/searchTST/searchBruteForce now
  have their exit status checked), the enricher.py '*' fix and the add-variants
  exit-status fix.
- The --prune-pam entry no longer says a shorter 5' PAM is N-padded at the front
  and proven for TTTV: since b5e5e17 a shorter 5' PAM off a wider flank is
  refused, and only equal-width 5' is supported.
- run_pam_filter_test.sh has 31 assertions, not 32; the enricher ports produce
  four outputs (change_version.txt is fixed text from crispritz.py, not a port
  output); verify_enricher.sh has 17 cases, not 8 (also in add_variants.sh).
- Known issues: at a position carrying more than one SNP record only the last
  record's ALT is kept, in both ports; measured on HGDP + 1000 Genomes chr22,
  45,837 of 1,757,775 SNP records (2.6%) at 43,389 positions. Investigated
  separately, since a fix changes every enriched genome.
- Dated 2026-10-30.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With no compiled enricher installed, add-variants ran enricher.py through its
`#!/usr/bin/env python` line. Ubuntu 24.04 ships only python3, so there every
chromosome failed with exit 127 (`/usr/bin/env: 'python': No such file or
directory`) -- which, until the previous commit made add-variants check the
enricher's exit status, left it at exit 0 with nothing enriched. Caught by
run_add_variants_status_test.sh's real-enricher case in a fresh ubuntu:24.04.

The script now runs under sys.executable: the interpreter running crispritz.py,
which is also the one with this install's dependencies (enricher.py imports
pandas). In a conda install that is the binary the shebang resolved to anyway,
so production output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
add-variants runs one enricher per chromosome in parallel, all writing into
SNPs_genome/<genome>_enriched/. enricher.py created it with
`if not isdir: mkdir`, so two workers reaching chromosomeSave together could
both see it missing; the second mkdir raised FileExistsError and that
chromosome's enriched FASTA, SNP dict and indel log were never written. Only
the first creation is exposed, so it needs two chromosomes to finish together:
rare on real chromosomes, but 3 of 6 runs of run_add_variants_status_test.sh
(two 300 bp chromosomes) hit it on the previous code. Before add-variants
checked exit statuses that loss was silent.

os.makedirs(..., exist_ok=True), which is what enricher.cpp already does
(EEXIST is success). Output unchanged; verify_enricher.sh still byte-identical,
and the test passes 6 of 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s test exposed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… out of memory

searchPAMonGenome's scan bounds are `genome_sequence.length() - k` on an
unsigned length, so a sequence shorter than the target window wraps them to
~2^32. For a constrained PAM the scan then read past the sequence and
segfaulted, as it always had. 5c612c0 makes an all-N PAM skip that read (a
PAM-less index must not reject a site on PAM content), so on such a contig the
scan instead ran ~2^31 iterations pushing two indices each: 20-28 GB within
15 s on a header-only FASTA until killed for memory -- on a shared host the
OOM killer may take other users' jobs first. The enricher writes a header-only
fake<chr>.fa for every chromosome with no carried indel, so this is ordinary
input. Found by the release review (independent agent).

A sequence shorter than pam_len + max_bulges holds no target on either strand
(the unwrapped bounds admit none), so return before the scans. buildTST then
finishes at once, writes no .bin and exits 0 for any PAM, and `crispritz.py
index-genome` skips such a contig instead of stopping at it now that buildTST's
exit status is checked. Nothing that fits a window changes. pamGenerator.cpp is
CRLF; patched in binary mode, 10 inserted lines.

test/run_short_contig_test.sh: header-only, 10-bp and one-base-short contigs
with 3' all-N (K=3, K=8), 5' all-N and NGG PAMs -> exit 0, no .bin; a normal
contig still indexes; index-genome over a normal + a header-only chromosome
exits 0 and indexes the normal one. Under a 60 s timeout and, on Linux, a 4 GB
address-space cap, so a regression fails the test instead of the host: the
previous code fails it at once with std::bad_alloc. Runs in the flank-n job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…--prune-pam docs match the code

- searchOnTST.cpp enabled the filter for any value of CRISPRITZ_PAM_PRUNE,
  including 0; empty or "0" now leave it off (checked: unset/""/0 off, 1/yes on,
  same targets on a dedicated index either way).
- The comment claiming --prune-pam output "is identical to searching a
  dedicated index" holds for the targets list only; the profile outputs count
  duplicate rows that depend on the tree's shape and carry the K-padded PAM in
  their guide label. Said so.
- crispritz.py's --prune-pam help and README 3.3.3 still described a shorter 5'
  PAM as front-padded, recommended a 5' generous index and listed TTTV as served
  off the 3' index; since b5e5e17 a shorter 5' PAM is refused and only an
  equal-width 5' index works. README also said the DNA/crRNA columns carry the
  K-wide flank, which 206f3ec removed, and its NGA/NNGRRT examples omitted
  -bDNA/-bRNA, so crispritz.py routed them to the brute-force search, which
  exits 0 having done nothing. All corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…profile outputs; a writer "bug" that was not one

- Fixed: buildTST on a contig too short for one target (all-N PAM runaway).
- CRISPRITZ_PAM_PRUNE: any value but empty or 0.
- Known issues: under --prune-pam the profile outputs differ from a dedicated
  index's (the targets list does not).
- The genomic-N entry said the old writer's value-conditional nibble shift lost
  a slot for a 0x0 nibble and displaced the rest. It did not: shifting a zero
  nibble is a no-op, and old and new writers give identical bytes for all
  6,725,600 PAM strings of length 1-8 over ACGTRYN (release review). Reworded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lucapinello
lucapinello merged commit 69a9d92 into master Oct 30, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant