Repository navigation
Audit predict, match and train, and stop trusting a word the reference carries - #40
Merged
Merged
Conversation
A k-mer says what it matched, never where, and some markers of the WHO catalogue have an alternative k-mer the reference already holds elsewhere, or a reference window it holds twice, so they read the same in every genome whatever the sample carries. The reference is now read once while the index is built, and those markers are settled by an anchor unique in the reference as well as in the genome, or reported as not determined. The app's resistance card also shows the amino acid the sample's own codon reads when it differs from the catalogue label.
A record with no usable k-mer, such as an N-masked consensus, used to get an ordinary lineage call and confidence, and is now reported as Undetermined beside a Kmers_Used count. Train no longer prints an unmeasured accuracy as 0.00%, deals its cross-validation folds so none comes out empty, warns when trees never split, and breaks vote ties by label rather than by file order. The Excel writer keeps sample names as text, and predict's cleanup no longer deletes an earlier run's workbook.
The singleton filter switched itself on above 100,000 unique k-mers and the score was then a fraction of what remained, so 3% more reads could turn 0.8886 into a perfect 1.0000; it now applies only when asked for, and the report carries the denominator. Query k-mers are cut after removing the line breaks of a wrapped FASTA, Query_Files comes out in the same order on every run, and the Excel log names the file it wrote. The app's five match controls that reached nothing are replaced by the one that changes the answer.
The single test split was a plain shuffle, so a lineage with few genomes was often left out and the accuracy described only the others. Every lineage with two genomes or more is now tested, and the report adds the mean accuracy per lineage, naming those below 100%.
A node of twice --min-samples-leaf samples or fewer is never split, and the default of 5 kept the trees too shallow to separate lineages with few genomes; it is now 1 in the CLI, the app and Galaxy, which did better on every panel measured. The documentation now says that a feature bucket is the last ten bases of the canonical k-mer, which a full hash did not improve on, and the coordinates report prints the bases found at each position instead of the canonical k-mer.
Every page was checked against the code and every figure measured again with the final binary. The fixes cover a missing flag, examples without the new columns, two pages still describing dropped records, missing undetermined reasons, one figure that had never been measured, figures from before the paralogue guard, and counts given to the wrong module.
Paururo
force-pushed
the
fix/paralogous-kmer-guard
branch
from
November 1, 2026 16:03
26f0e38 to
51a8fe7
Compare
Every command creates its outputs before it has finished reading its inputs, so an output named after an input destroyed it: a sample list, a FASTA to predict, the references for match, the marker panel and the training data could all be truncated or replaced, and three of those runs exited successfully. Each command now compares its outputs with its inputs once paths are resolved and stops with a message naming both before anything is written.
The lineage:count column counts every prefix of a marker's path, which tallies each level of a lineage hierarchy, but a catalogue path is not a hierarchy and its prefixes run into the WHO grade, which has spaces in it. The space-separated pairs then came apart and the column could not be read back; with a resistance panel it now counts the markers found per drug, as undetermined:count already did.
split-fastq read each record's sequence with its newlines still in it, so every k-mer spanning a line break of a wrapped FASTA was skipped as holding an invalid base, and a variant whose k-mer crossed one was never seen. Both scans now read the sequence without them; FASTQ reads have none, so their results do not change.
Every output example is now an excerpt of a real run, and the hand-written ones they replace were inconsistent: a predict row whose vote shares added up to more than one, classify rows placing a marker outside the gene it named, and match counts no real run would produce. The input reference now describes the WHO resistance panel shape the tools recognise, and several pages that still described dropped records or the old columns say what the tools do today.
The list was read into a hash map, so the detailed table listed the samples in a different order on every run, and which of two masked FASTA files sharing a stem got the _2 suffix changed with it. Samples now come out in list order.
The input and output pages now run one small invented dataset through every command and show what pathotypr wrote, with the real-run excerpts kept below. The pass also fixed examples that disagreed with the code: marker rows whose REF is not the reference base, the GFF naming rule, the match note and the predict rows.
Paururo
force-pushed
the
fix/paralogous-kmer-guard
branch
from
November 1, 2026 17:56
81a0916 to
0fff018
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this is
An audit of the three subcommands nobody had been through (
predict,match,train), plus the two panel problems left open by #39. Every finding below was reproduced against the compiled binary before it was touched, and the numbers quoted are measured on this machine.The marker panels (
classify,split-fastq)A k-mer says what it matched, never where. Of the WHO catalogue markers
split-fastqindexes, 5 have an ALT k-mer that H37Rv already carries elsewhere, and 67 have a second copy of their reference window.classifyindexes a different set (it keeps indels and skips alleles longer than 11 bases) and finds 2 and 71. Those words are in every genome, so the first five read as present everywhere, the ancestor included, whatever the sample carries.The reference is now read once while the index is built, and a marker with a copy is settled by its anchors or reported as
alt_kmer_in_reference/ref_kmer_repeated. Counting the copies is what keeps this quiet in the ordinary case: a genome that shows as many as the reference has has proved the marker absent. On H37Rv itself with the DR panel: 0 called, 0 undetermined.Writing the test turned up a defect of its own. An anchor was trusted for occurring once in the genome, but when the neighbouring change had broken the real locus, the paralogous copy was the one left looking unique, and the allele was read from the wrong place. An anchor now has to be unique in the reference as well.
The app's resistance card showed a marker's catalogue label as if it were the sample's change. A catalogue row is one base of a codon, and its label is the amino acid that base alone would give; a sample with two changes in the same codon fires the rows of both bases, each with its own label, while its codon reads a third amino acid. On such a genome the card now shows the amino acid the sample's codon reads, with the catalogue label beside it as the entry that matched, and leaves the rows whose label agrees untouched.
predictL3 0.5100 0.0200 L2:0.49Undetermined,Kmers_Used0Kmers_Used1Not Enough Data: No records foundUndeterminedrow eachThe filter measured byte length, not whether a k-mer could be cut. An N-masked consensus is long but holds no window of A, C, G and T, so every tree sent the missing features down the same branch and agreed, and that agreement is what
Confidencemeasures. The newKmers_Usedcolumn is what shows a call resting on almost nothing, which no confidence figure can.Also: the cleanup deleted an
.xlsxleft by an earlier run even when--excelwas not given (verified by md5, now preserved);0048became the number48in Excel while the TSV kept0048, and1e5became100000; and vote ties went to whichever lineage appeared later in the training FASTA.matchThe singleton filter switched itself on above 100,000 unique query k-mers, and the score was then taken over the remainder:
Three per cent more data moved the score to a perfect 1.0000. Below a few fold coverage a real k-mer is seen once, so dropping singletons discards the evidence that tells two references apart and keeps the repeats, which match everything. The filter now applies only when asked, the default keeps every k-mer, and
Query_Kmersreports the denominator. The same two inputs now give 0.8886 and 0.8897.Three more: query k-mers were cut with the newlines of a wrapped FASTA left in, so the same megabase gave 999,970 k-mers on one line and 571,420 at 70 columns;
Query_Filescame out of a HashMap and printed six different orders over twenty identical runs; and the Excel line named the.tsvit was asked for rather than the.xlsxit wrote.The app offered five controls that reached nothing (
max_ref_freq,coarse_top_n,coarse_stride,auto_tune, the early-stop pair) and a checkbox whose tooltip promised "percentages remain comparable with older reports" while the flag was never read. They are gone, replaced by--min-kmer-count, which is the one setting that does change the answer and was the only one not exposed.train--cv-folds 10, seven folds came out empty, were skipped with a warning, and the summary still said "10-fold" over the three that ran, each holding out a third of the data. The cursor now carries across classes: 0 empty folds.0.00%.accuracy_pctis now absent rather than zero, and the CLI and the app say "not evaluated".--min-samples-leafforbids every split trained a forest of stumps and saved it without a word. With 10 sequences:Every tree is a single leaf, so this model predicts one lineage for every input, naming the reason and the fix. With 24:17 of the 100 trees are a single leaf.The test set covers 4 of 12 lineages.The two decisions, measured before taking them
Neither touches the published 498/498: that figure compares
predictwith phylogenetic placement on 498 independent RefSeq genomes, and does not come fromtrain's internal test split.Both were measured on synthetic genomes built on the MTB ancestor with the lineage panel's own SNPs plus 150 private SNPs each, six per lineage to train and four held out to predict, including the hardest case the panel offers: the M. bovis sublineages A4.1 to A4.8, two to five SNPs of their own apart.
The feature buckets stay as they are. The bucket is the last ten bases of the canonical k-mer rather than a hash of the whole of it, so the feature space is the ten-mers whatever k is (H37Rv occupies 27.8% of the buckets at k=11 and 28.3% at k=31). Measured against a full 64-bit hash:
The full hash gave slightly more decisive votes at twice the training time and would invalidate every model already trained, so it is not adopted; it is kept on
exp/full-kmer-hash. The documentation no longer claims the 2-bit code "acts as a hash function", and the importance reports are described as candidates: a bucket holds every k-mer sharing its last ten bases. The coordinates report printed the canonical k-mer beside a forward-strand position, so 49.5% of its rows matched the genome at their coordinate; it now prints the bases found there, and all 6,379 rows of a test run match.The single split is stratified. Same data, same model, only the held-out set changes:
L1 0/1 correct--min-samples-leafdefaults to 1, in the CLI, the app and Galaxy. A node of twice that many samples or fewer is never split, and the default of 5 kept the trees too shallow to separate lineages with few genomes (on a panel of ten genomes it leaves every tree a single leaf):1 is the usual value for a random forest, and it was better on every panel measured.
Found while checking the documentation against real runs
An output named after an input destroyed the input. Every command creates its outputs before it has finished reading its inputs:
classify -l run.tsv -o runpredict -i p.fasta -o p.fastamatch -r refs.fasta -o refs.fastaclassify -m panel.tsv -o paneltrain -i data.fasta -o data.fastaEach command now compares its outputs with its inputs once paths are resolved and stops before writing anything, naming both files.
A resistance summary could not be read back.
lineage:countseparates its pairs with spaces, but a catalogue path runs into the WHO grade, which has spaces in it, so one marker came apart into four pieces (41 for a genome with three markers). With a resistance panel the column now counts markers per drug, asundetermined:countalready did; lineage panels are unchanged.split-fastq lost k-mers across the line breaks of a wrapped FASTA, the same defect fixed in
matchabove: the same sequence was called on one line and missed when wrapped at 70 columns. FASTQ reads have no line breaks inside a sequence, so their results do not change.Every output example in the documentation is now an excerpt of a real run, checked line for line against the files the run wrote. The hand-written ones were inconsistent: a predict row whose vote shares added up to 1.04, classify rows placing a marker outside the gene they named, and a match denominator about 25 times lower than a real run gives. The input reference also describes the resistance panel shape the tools recognise, and how each WHO grade is read.
The input and output pages carry an invented example dataset. One small seeded dataset (a 4 kb reference, a lineage and a WHO-shaped panel, three samples, reads and a training set) goes through every command, and each page shows what pathotypr wrote for it, with the real-run excerpts kept below. Building it showed
classify -lwriting its detailed table in a different sample order on every run, because the list was read into a hash map; it now follows the list, and a test fails on the old behaviour.Verification
87 + 83 + 16 tests pass, the GUI and the bench target compile,
mkdocs build --strictis clean. Each new guard was deliberately broken to confirm the tests catch it: 7 of 7 mutations in the paralogue guards, 5 of 5 in the window logic, plus 11 checks of the label comparison against the real formats. The panel work is verified on a synthetic M. caprae genome built from the panel's own markers.