8000 Add KofamScan annotation of genome proteins by erikrikarddaniel · Pull Request #300 · nf-core/magmap · GitHub
[go: up one dir, main page]

Skip to content

Add KofamScan annotation of genome proteins - #300

Merged
erikrikarddaniel merged 9 commits into
nf-core:devfrom
erikrikarddaniel:kofamscan
Nov 25, 2026
Merged

erikrikarddaniel merged 9 commits into
nf-core:devfrom
erikrikarddaniel:kofamscan

Conversation

@erikrikarddaniel
Copy link
Copy Markdown
Member

PR checklist

  • This comment contains a description of changes (with reason).
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added a new tool - have you followed the pipeline conventions in the contribution docs
  • If necessary, also make a PR on the nf-core/magmap branch on the nf-core/test-datasets repository.
  • Make sure your code lints (nf-core pipelines lint).
  • Ensure the test suite passes (nextflow run . -profile test,docker --outdir <OUTDIR>).
  • Check for unexpected warnings in debug mode (nextflow run . -profile debug,test,docker --outdir <OUTDIR>).
  • Usage Documentation in docs/usage.md is updated.
  • Output Documentation in docs/output.md is updated.
  • CHANGELOG.md is updated.
  • README.md is updated (including new tool citations and authors/contributors).

Description

I added KofamScan to magmap, so that the proteins of every genome get KEGG Orthology (KO) assignments (part of #164).
The pipeline already annotates genomes with Prokka or Bakta, but gave no functional KO assignment.

KofamScan runs on one genome at a time and keeps its result in a storeDir (--kofamscan_store_dir, magmap_kofamscan by default), the same way Prokka does, so a genome is never annotated twice.
The KofamScan database (ko_list and the HMM profiles) is downloaded once into --kofam_dbpath.
The hits of all genomes are collected in summary_tables/magmap.kofamscan.tsv.gz, with one row per hit.
A second table, summary_tables/magmap.kofamscan_best.tsv.gz, has one row per protein: the hit with the highest score, with the lowest KO id winning a tie.
I did not apply the score threshold (thrshld) in the best-hit table.
Environmental and partial proteins often score below it, and which threshold to use is best left to the user, who can filter on score and thrshld.
Both tables are also converted to Parquet when --save_parquet is set.
The step is skipped with --skip_kofamscan.

  • The vendored nf-core kofamscan module is patched to read gzipped input and write a gzipped table. The patch is in modules/nf-core/kofamscan/kofamscan.diff.
  • The module is used with storeDir, so it does not emit its version on the versions topic. A local KOFAMSCAN_VERSION process reports it instead.
  • Output files are named by genome, so that genomes sharing a storeDir do not collide.
  • A genome without hits gives a header-only table.
  • The test profile uses its own kofam_dbpath and kofamscan_store_dir, both in .gitignore. The test data comes from the existing metatdenovo branch of nf-core/test-datasets, so no test-datasets PR is needed.
  • conf/test_full.config points to S3 paths for the database and the store. I based them on the Prokka paths and they need checking.
  • eggnog-mapper, the other part of Add eggnog-mapper and KOfamscan #164, is not done, so the issue stays open.

I verified it with:

  • module tests for KOFAMSCAN_BEST: the higher score wins even when listed second (a text sort would pick "9.5" over "80.0"), a protein with only sub-threshold hits keeps its best hit, a - threshold with a tie, no hits, and stub;
  • subworkflow tests: with three genomes, the best table has the same proteins as the full table and each score is the maximum for its protein;
  • the pipeline test tests/default.nf.test, including the Parquet files;
  • prek run -a, nf-core pipelines lint (0 failed) and nextflow lint . (13 warnings, all on lines already in dev).

nf-core modules lint kofamscan has two known failures, correct_meta_outputs and correct_meta_topics, caused by the patch and the storeDir use.
I did the work and reviewed it with Claude Code, and an independent Claude Code agent reviewed the diff.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH

erikrikarddaniel and others added 5 commits November 16, 2026 13:40
Run KofamScan once per genome on its protein FASTA (Prokka, Bakta or
gffread) and store each result in `magmap_kofamscan` with `storeDir`,
as for Prokka.
Concatenate the per-genome results into
`summary_tables/magmap.kofamscan.tsv.gz`, which also goes into the
Parquet conversion with `--save_parquet`.
`--skip_kofamscan` turns the step off.

The vendored kofamscan module is patched to read and write gzip and to
name its outputs by prefix, because a glob over the shared store
directory collected other genomes' files.
It also drops the versions topic emit, which `storeDir` rejects, so a
local `KOFAMSCAN_VERSION` module reports the version.

Only the default test runs KofamScan; the other test profiles skip it.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH
Point the full test at S3 store directories for the KofamScan database
and results, as for Prokka, so Batch does not write to a local path
and download the real database on every run.
Point the default test at its own store directories so that a test run
does not seed the database and results of a real run in the same
directory.

Let `KOFAMSCAN_TABLE` succeed when no genome has a hit: `grep -v`
exits 1 on header-only input and the pipeline shell uses `pipefail`.
A new subworkflow test with a single no-hit genome failed before the
change.

Require the `tsv` output of the patched kofamscan module, so that an
`ext.args` without `--format detail-tsv` fails the task instead of
silently dropping the summary table.
Update the module `meta.yml` and tests to the patched outputs.

Add the KofamScan citation and the kofamscan version to the CHANGELOG
dependencies, and drop two comments that repeated the code.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH
Run the `hmmfiles` test with `--save_parquet` and check that
`magmap.kofamscan.parquet` and `magmap.hmmrank.parquet` are written.
Only the default test profile runs KofamScan, and no Parquet test
covered this table or the hmmrank table.
The `hmmfiles` test is used so that CI does not run another full
default test.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH
KofamScan can report several KOs for one protein, and with environmental
and partial sequences the hit that reaches its own threshold is often
missing.
The new KOFAMSCAN_BEST module keeps one row per protein, the one with
the highest score among all hits, with the lowest KO id winning a tie.
No threshold is applied, so users filter on `score` and `thrshld`
themselves.
Scores are compared as numbers; as text "9.5" would beat "80.0".

The table is published as summary_tables/magmap.kofamscan_best.tsv.gz
and converted to Parquet with the other summary tables.

Tests: module test with inline hits (higher score listed second, only
sub-threshold hits, `-` threshold with a tie), header-only input and
stub; the subworkflow and pipeline tests check that the best table has
the same ORFs as the full table and that each score is the maximum.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH
@github-actions
github-actions Bot commented Nov 17, 2026 •
Copy link
Copy Markdown

nf-core pipelines lint overall result: Passed ✅

Posted for pipeline commit bccd318

+| ✅ 300 tests passed       |+
#| ❔   5 tests were ignored |#
Details

❔ Tests ignored:

  • files_unchanged - File ignored due to lint config: .github/workflows/branch.yml
  • files_unchanged - File ignored due to lint config: assets/nf-core-magmap_logo_light.png
  • files_unchanged - File ignored due to lint config: docs/images/nf-core-magmap_logo_light.png
  • files_unchanged - File ignored due to lint config: docs/images/nf-core-magmap_logo_dark.png
  • files_unchanged - File ignored due to lint config: .gitignore or .prettierignore

✅ Tests passed:

Run details

  • nf-core/tools version 4.2.2
  • Run at 2026-11-24 17:54:15

@erikrikarddaniel
erikrikarddaniel marked this pull request as ready for review November 17, 2026 11:51
@piplus2 piplus2 left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it's a really careful addition. The design follows the pipeline's existing patterns: storeDir caching like Prokka and Bakta, a separate *_VERSION process for the cached module, and the DuckDB heredoc style. The tests are thorough too, and the numeric-vs-text sort test case is a nice touch. The one thing to fix before merge: the KofamScan store is keyed only by accession. If a genome's proteins change between runs, for example after switching --annotator, the old hits get reused with ORF IDs that no longer match. The rest is optional.

Comment thread conf/modules.config Outdated

withName: KOFAMSCAN_SCAN {
ext.args = '--format detail-tsv'
ext.prefix = { "${meta.id}.kofamscan" }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required: The stored file is named <accno>.kofamscan.tsv.gz whatever produced the proteins. Prokka and Bakta use separate store dirs (--prokka_store_dir, --bakta_store_dir) and give different ORF IDs: Bakta uses a hash of the accno as its locus tag (L110–123). So if one run uses --annotator prokka and a later run uses --annotator bakta_all with the same --kofamscan_store_dir, the KofamScan result is silently reused with Prokka ORF IDs. Those IDs won't join to the rest of that run's tables. Could we add the protein source to meta in workflows/magmap.nf (e.g. PROKKA.out.faa.map { m, f -> [ m + [ source: 'prokka' ], f ] }, and likewise for Bakta and gffread) and use it in the prefix?

Suggested change
ext.prefix = { "${meta.id}.kofamscan" }
ext.prefix = { "${meta.id}.${meta.source}.kofamscan" }

We could also widen the warning in docs/output.md (~L210) to cover any change to a genome's proteins, such as an edited gff, not only database updates. This follows the repo's existing separate-store pattern for Prokka and Bakta (conf/modules.config L101–124).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed in d2a5ef9.
The proteins now carry meta.source (prokka, bakta or gffread) and the stored file is <accno>.<source>.kofamscan.tsv.gz, as you suggested.
A result from another source no longer matches, so switching annotator annotates the genome again.
A subworkflow test seeds the store with a Prokka result for two genomes, runs one as Bakta and one as Prokka, and checks that only the Bakta one is annotated again.
I also widened the warning in docs/output.md: it now covers a changed GFF, and says that the source in the file name is what triggers re-annotation.

@@ -0,0 +1,184 @@
Changes in component 'nf-core/kofamscan'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Most of this patch adds gzipped input and output. That seems useful upstream, and moving it there would shrink the patch here and restore the upstream tests it removes. Would you consider opening it as a PR to nf-core/modules? Then only the storeDir / versions change stays local. See nf-core module patching.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in nf-core/modules#13805, which is now merged.
I updated the vendored module in 5ad5f53, and the patch is down to dropping the versions topic and keeping tsv non-optional.
storeDir needs tsv to be non-optional to tell a missing result from a stored one.

Comment thread modules/nf-core/kofamscan/main.nf Outdated
script:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}"
extension = args.contains("--format detail-tsv") ? "tsv" :
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Only prefix needs to be global for the output: paths. extension can stay def extension = … (same at L53).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a109db5, extension is a local def again.
The module update in 5ad5f53 took this over from upstream.

- conda-forge
- bioconda
dependencies:
- conda-forge::wget=1.21.4
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The script only uses gunzip and tar, and the comment in main.nf explains why awscli is there. Is wget still needed? Relatedly, the meta.yml description says "Downloads", but Nextflow does the download when it stages the file() inputs. Maybe "Unpacks the KOfam ko_list and HMM profiles"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meta.yml description now says "Unpacks the KOfam ko_list and HMM profiles for KofamScan" (a109db5).
You are right that the script itself no longer needs wget, but I kept it for now: the image is shared with metatdenovo, and I would rather drop it in both pipelines at the next rebuild than change one image here.

Comment thread conf/test_full.config Outdated
Comment on lines +22 to +23
kofam_dbpath = 's3://nf-core-awsmegatests/magmap/input/databases/kofam_db'
kofamscan_store_dir = 's3://nf-core-awsmegatests/magmap/input/databases/kofamscan'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: You flagged these S3 prefixes as needing a check. Could a maintainer with access to nf-core-awsmegatests confirm magmap/input/databases/kofam_db and …/kofamscan are the right locations? I couldn't verify them from here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't verified these prefixes, and nobody has.
Since KofamScan is on by default, the full test would have run it against them, so I set skip_kofamscan = true in conf/test_full.config and dropped the two S3 paths.
I'll add KofamScan to the full test in a later PR, once the bucket layout is decided together with metatdenovo.

erikrikarddaniel and others added 2 commits November 17, 2026 15:16
The store file was named only by accession, so a result stored after a
Prokka run was reused by a later run with --annotator bakta_all.
Bakta and Prokka give different ORF IDs, so those hits did not join to
the other tables of the run.

The proteins now carry meta.source (prokka, bakta or gffread) and the
file is <accno>.<source>.kofamscan.tsv.gz.
A result from another source no longer matches, so the genome is
annotated again.
A changed GFF with the same source still reuses the stored result; the
output docs now say so.

Tests: a subworkflow test seeds the store with a Prokka result for two
genomes, runs one of them as Bakta and one as Prokka, and checks that
only the Bakta one is annotated again.
The pipeline test checks the name of the stored file.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH
Only prefix needs to be global, as the output paths use it.
Declaring extension with def again makes the patch two lines shorter.

The description of KOFAMSCAN_DOWNLOAD said it downloads, but Nextflow
stages the files, so it now says it unpacks them.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH
The module now reads gzipped FASTA and writes gzipped, prefix-named
output (nf-core/modules#13805), so the local patch shrinks to removing
the versions topic and keeping the tsv output non-optional, which
storeDir needs to tell a missing result from a stored one.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UvGYU6kuXJeVv9ffdSzdzH
KofamScan runs by default, so the full test would have used the S3 prefixes
for the KOfam database and the store, and nobody has checked that they
exist on nf-core-awsmegatests. The full test skips KofamScan until the
bucket layout is decided, together with metatdenovo.

Generated by Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@piplus2 piplus2 left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this all looks good now. Each point from my earlier review is covered:

  • Store keyed by protein source: <accno>.<source>.kofamscan.tsv.gz, with a subworkflow test showing that only the genome switched to Bakta is annotated again, and the wider warning in docs/output.md.
  • Upstream gzip support: great to see nf-core/modules#13805 merged. The local patch is now just the storeDir/versions change.
  • extension local again, the meta.yml wording updated, and keeping wget until the shared image is rebuilt makes sense.
  • Skipping KofamScan in test_full until the bucket layout is settled is sensible. Maybe open a follow-up issue so it is not forgotten.

I also checked locally (Nextflow 25.10) that a storeDir process with a remote file() input is skipped as Stored on later runs without downloading the input again, so profiles.tar.gz is fetched only once.

One optional idea for this PR or a later one: neither summary table has a genome accession column, and the best-hit table groups by orf alone. That is fine while ORF IDs are unique across genomes, but an accno column (taken from the stored file name in KOFAMSCAN_TABLE, and used in PARTITION BY accno, orf) would make the tables easier to join and protect against ID collisions between user-supplied GFFs.

LGTM!

@erikrikarddaniel
erikrikarddaniel merged commit 1d03c32 into nf-core:dev Nov 25, 2026
21 checks passed
@erikrikarddaniel
erikrikarddaniel deleted the kofamscan branch November 25, 2026 19:45
@erikrikarddaniel
Copy link
Copy Markdown
Member Author

Thanks @piplus2

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

0