Skip to content

Support node_labels in write_nexus via TRANSLATE#3442

Open
kaathewisegit wants to merge 1 commit intotskit-dev:mainfrom
kaathewisegit:node_labels
Open

Support node_labels in write_nexus via TRANSLATE#3442
kaathewisegit wants to merge 1 commit intotskit-dev:mainfrom
kaathewisegit:node_labels

Conversation

@kaathewisegit
Copy link
Copy Markdown

Description

Adds a node_labels parameter to write_nexus which allows renaming samples. The trees still use the fast Newick serializer, the remapping is done with a TRANSLATE directive.

Resolves #3435

PR Checklist:

  • [?] Tests that fully cover new/changed functionality.
  • Documentation including tutorial content if appropriate.
  • Changelogs, if there are API changes.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.92%. Comparing base (2f26dc6) to head (09b8efb).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3442   +/-   ##
=======================================
  Coverage   91.92%   91.92%           
=======================================
  Files          37       37           
  Lines       32153    32158    +5     
  Branches     5143     5145    +2     
=======================================
+ Hits        29556    29561    +5     
  Misses       2264     2264           
  Partials      333      333           
Flag Coverage Δ
C 82.70% <ø> (ø)
c-python 77.34% <ø> (ø)
python-tests 96.40% <100.00%> (+<0.01%) ⬆️
python-tests-no-jit 33.20% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Python API 98.70% <100.00%> (+<0.01%) ⬆️
Python C interface 91.23% <ø> (ø)
C library 88.86% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@jeromekelleher jeromekelleher left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @kaathewisegit! A few minor bits of input above.

It would be nice to verify that a third-party tool like dendropy or BioPython recogises this - would it be possible to check this?

@kaathewisegit
Copy link
Copy Markdown
Author

kaathewisegit commented Mar 25, 2026

I've changed write_nexus to raise errors if node_labels is empty or if it contains keys above ts.num_samples.

I also tried adding tests which use BioPython (generate a random ts and see if its NEXUS output is parseable), but it turns out that Bio.Nexus only accepts integer ids for TRANSLATE, so n{i} get rejected as illegal.

https://github.com/biopython/biopython/blob/d76a5581ca3317ec850d72450c458ec4e34e2583/Bio/Nexus/Nexus.py#L1113

Copy link
Copy Markdown
Member

@jeromekelleher jeromekelleher left a comment

Choose a reason for hiding this comment

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

Some comments on the implementation.

It seems like dendropy should support TRANSLATE, so should be easy enough to use that to verify? Just one example is fine, we just want to be sure that some other software can read this.

Co-authored-by: Jerome Kelleher <jk@well.ox.ac.uk>
@kaathewisegit
Copy link
Copy Markdown
Author

I've switched to a lenient translation table construction. And added dendropy parsing tests for all tree sequences from get_example_tree_sequences (except multiple-root and empty ones, as newick serialization doesn't work for those).

Copy link
Copy Markdown
Member

@jeromekelleher jeromekelleher left a comment

Choose a reason for hiding this comment

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

Great! Looks like it's almost ready to go in.

labels[node] = f"new_node_which_was_{node}"

nexus = ts.as_nexus(include_alignments=False, node_labels=labels)
print(nexus)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Stray print


labels = {}
samples = ts.samples()
k = random.randint(1, len(samples))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think the random does much here, I'd just do something like

u = ts.samples()[0]
labels[u] = f"new_node_which_was_{u}"

And then after just check that labels[u] is recognised as a taxon ID by dendropy?

:param str missing_data_character: As for the :meth:`.alignments` method,
but defaults to "?".
:param bool isolated_as_missing: As for the :meth:`.alignments` method.
:param node_labels: A map of type `{index: name}`. Samples present in
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
:param node_labels: A map of type `{index: name}`. Samples present in
:param node_labels: A map of type `{node_id: name}`. Samples present in

but defaults to "?".
:param bool isolated_as_missing: As for the :meth:`.alignments` method.
:param node_labels: A map of type `{index: name}`. Samples present in
the map will have the given name instead of `n{index}`. Note that
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
the map will have the given name instead of `n{index}`. Note that
the map will have the given name instead of `n{node_id}`. Note that

Index is ambigous here, better to be clear

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.

Supporting node_labels in as_nexus via translate

2 participants