Skip to content

Create report counters once per read rather than per hit - #1066

Merged
ch4rr0 merged 4 commits into
DerrickWood:masterfrom
tfenne:tf_classify_report_counters
Oct 30, 2026
Merged

ch4rr0 merged 4 commits into
DerrickWood:masterfrom
tfenne:tf_classify_report_counters

Conversation

@tfenne

@tfenne tfenne commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Stacked on #1065: only the last commit, "Create report counters once per read rather than per hit", is new here; the branch rebases onto master cleanly once #1065 is merged.

Since lookups were batched, a taxon's counter is created by its first repeated hit (through increaseKmerCount), or by a new hit group only when k-mer data is kept. A taxon whose hits were all single, non-repeated minimizers therefore gets no counter in a plain --report run, and KrakenReportDFS, which orders sibling taxa by whether they have a counter, moves it among its zero-count siblings. On 72,166,933 dog read pairs against a 62.6 GB database, 271 lines of the --report-zero-counts report are in a different order from 2.17.2 and 2.18.0-RC1 (for example among the Candida albicans strains); the rows themselves are the same.

Every taxon with a hit is in the read's hit_counts, which holds a handful of taxa, so when no k-mer data is kept the counters are now created from hit_counts once per read. The per-hit lookups in the thread's taxon_counters_t (a std::map) are made only when --report-minimizer-data or a counters dump will read k-mer data back. The report is again byte-identical to 2.17.2 and RC1, and it is cheaper.

Measured with a production invocation (perl wrapper, mates fed through FIFOs by igzip with a 512 MB buffer each so that classify is not waiting on input, --memory-mapping on a huge=always tmpfs, --threads 16 --paired --report --report-zero-counts) on the same 72 million pairs, mean of three runs, against #1065 alone:

before after
r9g.4xlarge (Graviton5) 57.8 s, 759 CPU-s 56.5 s, 736 CPU-s
r8a.4xlarge (AMD EPYC 9R45) 52.0 s, 681 CPU-s 45.2 s, 641 CPU-s

Per-read output is unchanged. Output, --report-zero-counts report and --report-minimizer-data report are byte-identical to RC1 on 616,000 fixture-derived pairs and 20,000 single-fragment pairs at 1 and 8 threads, and in quick mode. The end-to-end suite passes, but no check in it distinguishes this change: the fixture database has no taxon whose hits are all single minimizers, so the reordering only shows on the larger real data set above.

ClassifySequence kept its MinimizerLookups in a static thread_local, constructed on a thread's first call with that call's database, hit list, hit counts, taxon counters and options, and never reset or rebound afterwards. That caused three defects.

The hit-group count was never reset, so it accumulated across every read a thread classified, and after a thread's first few reads every read passed --minimum-hit-groups, including the wrapper's default of 2. On 72,166,933 dog read pairs at -g 2, master classified 805,969 pairs (1.1%) that 2.18.0-RC1 leaves unclassified. On 20,000 pairs of random sequence each carrying one 36 bp reference fragment, 2.17.2 and RC1 classify 15,002 and master classified 19,827; a read with hit list "0:32 335341:5 0:29 |:| 0:66" is unclassified in RC1 and was classified in master.

The database was fixed at the first call. A daemon serves several databases from its index map, so a request against a second database looked its minimizers up in the first database's table. A daemon that served the e2e database and then a database built from half of data/ classified 402 of 440 pairs for the second request, where a direct run against that database classifies 264.

The hit list, hit counts and counters were bound by reference to the locals of the ProcessFiles call that first ran on the thread. A later ProcessFiles call, for the next file pair or the next daemon request, has new locals, and the references only worked when those landed at the same addresses.

MinimizerLookups is now constructed for each read, so it starts with a zero hit-group count and refers to the current database and outputs. Only its batch buffer is thread_local, so that it is still allocated once per thread.

Hit groups are also counted as they were before lookups were batched. The previous minimizer was local to get_batch, so it was forgotten at every batch boundary: after 1,024 positions in a long read, and after every minimizer in quick mode, where the batch size is 1. It was also updated only on a hit, so a hit whose minimizer repeated a minimizer that had missed, or followed a sampled-away minimizer, counted as a repeat rather than a new group. The previous minimizer is now a member, updated by every looked-up minimizer, cleared by a mate or frame border or a sampled-away minimizer, and left alone by an ambiguous span, as the consecutive-minimizer check in RC1 did.

classify.min_hit_groups_10.golden classifies tests/data/mhg_*.fq at --minimum-hit-groups 10, where five of the 18 pairs fall short. The simulated reads all clear 5, so classify.min_hit_groups_5.golden could not catch this. The golden files equal RC1's output and report; master fails the check.

Verified against RC1 on the e2e database: per-read output identical at -g 2, 10 and 20 on 616,000 pairs built from the fixture reads; output, -F output, --report-zero-counts report and --report-minimizer-data report identical on those and on the 20,000 single-fragment pairs at 8 threads; the two-database daemon request matches the direct run. The end-to-end suite passes.
Quick mode is meant to stop at the first hit that brings a read to --minimum-hit-groups. Since lookups were batched, the early exit compared a local minimizer_hit_groups that nothing increments, so it never fired at the wrapper's default of -g 2 and fired after the first minimizer, hit or not, at -g 0. On 616,000 pairs built from the fixture reads, 2.17.2 and 2.18.0-RC1 classify 588,000 in quick mode at -g 0 while master classified 460,600, and at -g 2 master made the same calls as normal mode, 47,600 of which differ from RC1's quick calls, and ran slower than normal mode.

The exit now tests the lookup's hit-group count once the read has a hit. Quick mode resolves each minimizer as it is added, and hits and hit groups change only on a hit, so the condition first holds at the hit that reaches the threshold, as before. The minimizer that triggers the exit is now counted in the total passed to ResolveTree.

tests/golden/paired.quick.out and paired.quick.report go back to their contents before afaa1ed and 0903ffd, which were 2.18.0-RC1's output, and the fixed build reproduces them.

Verified against 2.18.0-RC1 on the e2e database: quick-mode per-read output and --report-zero-counts report identical at -g 0, 2 and 5, on the 616,000 fixture pairs and on 20,000 pairs of random sequence carrying one reference fragment each. Quick mode at -g 2 now takes 0.35 s single-threaded on the 616,000 pairs, against 1.37 s for RC1's quick mode and 2.1 s for normal mode, since it stops looking minimizers up once the read is decided. The end-to-end suite passes.
A read shorter than k, or one made entirely of ambiguous bases, adds nothing to the hit list. ClassifySequence still compacts the list afterwards, reading taxa[0] and taxa[1] and writing taxa[j] and taxa[j + 1] on an empty vector. When the read is the first a thread classifies, the vector has no storage and classify crashes with a segmentation fault; on a later read it writes into spare capacity, and the following resize happens to leave the output right. Paired reads always carry the mate marker and are not affected, but single-end runs over trimmed reads routinely contain reads under 35 bp.

The compaction now runs only when the hit list is not empty, and such a read is written as "0:0" as before.

classify.single_short_first_read puts a 20 bp read ahead of the single-end fixture reads and classifies them with one thread. Master exits with SIGSEGV; with this change the short read is unclassified with hit list 0:0 and the rest of the output equals single.out. The end-to-end suite passes.
Since lookups were batched, a taxon's counter is created by its first repeated hit, through increaseKmerCount, or by a new hit group only when k-mer data is kept. A taxon whose hits were all single, non-repeated minimizers therefore gets no counter in a plain --report run, and KrakenReportDFS, which orders sibling taxa by whether they have a counter, moves it among its zero-count siblings. On 72,166,933 dog read pairs against a 62.6 GB database, 271 lines of the --report-zero-counts report are in a different order from 2.17.2 and 2.18.0-RC1, for example among the Candida albicans strains; the rows themselves are the same.

Every taxon with a hit is in the read's hit_counts, which holds a handful of taxa, so when no k-mer data is kept the counters are now created from hit_counts once per read. The per-hit counter lookups in the thread's taxon_counters_t, a std::map, are made only when --report-minimizer-data or a counters dump will read k-mer data back, as only those read kmerCount and distinctKmerCount. The report is again byte-identical to 2.17.2 and RC1.

Measured with the wgpb kraken-trim invocation (perl wrapper, mates fed through FIFOs by igzip with a 512 MB buffer each so that classify is not waiting on input, --memory-mapping on a huge=always tmpfs, --threads 16, --paired, --report, --report-zero-counts) on the same 72 million pairs, mean of three runs, against the previous commit:

  r9g.4xlarge (Graviton5): 57.8 s and 759 CPU-s before, 56.5 s and 736 CPU-s after
  r8a.4xlarge (AMD EPYC 9R45): 52.0 s and 681 CPU-s before, 45.2 s and 641 CPU-s after

Per-read output is unchanged. Output, --report-zero-counts report and --report-minimizer-data report are byte-identical to RC1 on 616,000 fixture-derived pairs and 20,000 single-fragment pairs at 1 and 8 threads, and in quick mode. The end-to-end suite passes.
@ch4rr0
ch4rr0 merged commit e469278 into DerrickWood:master Oct 30, 2026
8 checks passed
@tfenne
tfenne deleted the tf_classify_report_counters branch November 8, 2026 13:43
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.

2 participants