Skip to content

Kraken: fix ZeroDivisionError on species rows with zero distinct minimizers - #3759

Open
rpinho wants to merge 3 commits into
MultiQC:mainfrom
rpinho:fix-kraken-zero-distinct-minimizers
Open

rpinho wants to merge 3 commits into
MultiQC:mainfrom
rpinho:fix-kraken-zero-distinct-minimizers

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Who hits this: anyone running MultiQC on Kraken 2 reports made with both --report-minimizer-data and --report-zero-counts, which writes a row for every species in the database, most with zero distinct minimizers. https://github.com/DerrickWood/kraken2/blob/master/docs/MANUAL.markdown

Description

parse_logs() in multiqc/modules/kraken/kraken.py has a guard against zero distinct minimizers:

minimizer_duplication = int(minimizer) / int(minimizer_distinct) if minimizer_distinct != 0 else 0.0

The fields come from line.split("\t"), so minimizer_distinct is the string "0", and "0" != 0 is always true. The guard never fires, and any species row with zero distinct minimizers raises ZeroDivisionError. This came in with the parser rewrite in #2756.

In a batch, one such report stops the whole Kraken module, so Kraken results are missing for every sample, including valid ones. With --strict MultiQC exits 1; without it, the module error is logged and the report has no Kraken section.

Changes

  • multiqc/modules/kraken/kraken.py: convert minimizer_distinct to int before the zero check, and only store a duplication ratio when the distinct count is non-zero. The duplication heatmap already skips falsy values, so leaving the key out gives the same output as storing 0.0. I checked this on all 17 files in test-data/data/modules/kraken, which produce identical output before and after. It also avoids storing a 0.0 for every zero-count species.
  • Two type annotations in the same file (table_pct_by_sample, dup_by_taxon_by_sample). These are needed because the new test imports kraken.py, and the mypy tests CI job then type-checks the module and fails on those two lines without them.
  • tests/test_kraken_minimizers.py: a parser-level test and a two-sample parse_logs test (one normal report, one with a zero-count species row).

Verification

  • Reproduced with a real Kraken 2.17.2 report (--report-minimizer-data --report-zero-counts): before the fix, a mixed two-sample batch exits 1 with ZeroDivisionError and exports Kraken data for 0 of 2 samples. After the fix it exports both, with the same counts, unclassified percentage and duplication heatmap value as the normal-report control.
  • Kraken 2 6-column, 8-column minimizer, Bracken and all-zero-species reports all produce the same output as before.
  • Full test suite: 1,045 passed, 5 skipped. mypy multiqc, mypy tests, ruff check and ruff format --check are clean.

Found and written with AI agents; a separate AI referee reproduced the problem before this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

@multiqc-bot

This comment has been minimized.

@ewels ewels added the bug: module Bug in a MultiQC module label Nov 27, 2026
@ewels ewels added this to the v1.36 milestone Nov 27, 2026
@ewels

ewels commented Nov 27, 2026

Copy link
Copy Markdown
Member

/review

@multiqc-bot

Copy link
Copy Markdown
Contributor

✅ MultiQC preview report generated for kraken. See run.

Screenshot

MultiQC report screenshot

Console output
$ multiqc -m kraken test-data/data/modules -n multiqc_report_pr3759.html -o report --no-ansi

/// MultiQC 🔍 v1.36.dev0

       file_search | Search path: /home/runner/work/MultiQC/MultiQC/test-data/data/modules
       file_search | Only using modules: kraken
            kraken | Found 17 reports
       file_search | Only using modules: kraken
     write_results | Data        : report/multiqc_report_pr3759_data
     write_results | Report      : report/multiqc_report_pr3759.html
           multiqc | MultiQC complete
How this works

The bot posts a preview report on PRs that edit modules:

  1. When the first build finishes, a few minutes after the PR opens. If you push more fixes before then, the report uses the latest commit.
  2. Each night, if there are new commits since the last report.
  3. When anyone comments /report. If the build is still running, the report posts when it finishes.

The bot only posts a new report when there are new commits. If there are none, it reacts 😕 to your comment. The bot hides older report comments.

@claude

claude Bot commented Nov 27, 2026

Copy link
Copy Markdown

Fixes a real crash: minimizer_distinct is a string, so the old != 0 guard never triggered, and any --report-zero-counts species row took down the whole Kraken module. The fix is correct and minimal, and it comes with a regression test. The only real ask is moving the test to the per-module test directory. Both red CI checks come from a Dict NameError in isoseq that came in with the merge from main (#3698); this PR didn't cause them.

Full review

CI status

Two checks are red, and neither failure comes from this PR:

  • MultiQC lint / prek fails with ruff F821 Undefined name 'Dict' at multiqc/modules/isoseq/isoseq.py:20.
  • Integration test / Python 3.10 fails with NameError: name 'Dict' is not defined when importing the same isoseq module.

Both come from #3698, which is now on main, and this branch picked it up through the Merge branch 'main' commit. Once main is fixed, a re-merge or rebase should turn CI green. Please don't fix isoseq in this PR. Maintainers: main currently fails lint and integration tests.

Findings

💡 Suggestion: tests/test_kraken_minimizers.py

Module-specific tests normally go in multiqc/modules/<toolname>/tests/ (19 modules already do this, e.g. multiqc/modules/biscuit/tests/test_biscuit.py). pytest already collects from there (testpaths = ["tests", "multiqc/modules"]). Consider moving this file to multiqc/modules/kraken/tests/test_kraken.py with an empty __init__.py.

This would also make the two type-annotation changes unnecessary. pyproject.toml excludes multiqc/modules/* from mypy. The only reason mypy now checks kraken.py is that a file under tests/ imports it. With the test inside the module directory, the annotation edits could be reverted and the diff would be just the fix.

💡 Suggestion: multiqc/modules/kraken/kraken.py:341

If you keep the annotations, dict[str | int, dict[str | int, int | None]] is wider than the data really is (sample names and taxon names are always strings). It's probably there to match the heatmap.plot input type. That's fine, but a short comment, or SampleName/str like you used at line 205, would make the intent clearer. This is optional.

🪶 Nit: multiqc/modules/kraken/kraken.py:98

There's an edge case here, but it's not a regression. If every species row in a minimizer report has zero distinct minimizers, min_dup_by_taxon comes back empty, and if min_dup_by_by_rank: leaves that sample out of the duplication heatmap entirely. That's the right behaviour (there's nothing to plot). Mentioning it only to confirm it was intended, since the old code would have stored 0.0 values (if it hadn't crashed).

Checks

  • Correctness: int(minimizer_distinct) now comes before the zero check, and documented fields are still accessed directly (no .get(..., 0)), which follows the crash-loudly rule. Leaving the key out instead of storing 0.0 matches how top_taxa_duplication_heatmap already handles it (if minimizer_duplication: at line 350), so the plot output doesn't change.
  • Performance: it's slightly better. Zero-count databases no longer store one 0.0 per species in the database.
  • Security: no new HTML interpolation and no input handling beyond int parsing.
  • Hygiene: the PR has a clear description, a root-cause explanation and a regression test. No em-dashes in the title or body. There's no issue to link, which is fine for a bug fix.

What's good

  • The root-cause write-up is precise. It traces the "0" != 0 bug back to Kraken: optimize memory and runtime #2756 and explains why a single bad file drops Kraken output for the whole batch.
  • There are two levels of regression test: a direct parser test, and an end-to-end parse_logs run with strict=True that mixes a normal sample with a padded one and asserts both survive and the heatmap is still produced.
  • Verified against all 17 existing Kraken test-data files, which produce identical output.

@rpinho

rpinho commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! I've done this.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug: module Bug in a MultiQC module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants