-
Notifications
You must be signed in to change notification settings - Fork 83
Support node_labels in write_nexus via TRANSLATE
#3442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
|
|
||
| import functools | ||
| import io | ||
| import random | ||
| import textwrap | ||
|
|
||
| import dendropy | ||
|
|
@@ -334,6 +335,85 @@ def test_nexus_no_trees_or_alignments(self): | |
| ) | ||
|
|
||
|
|
||
| class TestNexusNodeLabels: | ||
| @tests.cached_example | ||
| def balanced_tree(self): | ||
| # 4 | ||
| # ┏━┻┓ | ||
| # ┃ 3 | ||
| # ┃ ┏┻┓ | ||
| # 0 1 2 | ||
| return tskit.Tree.generate_balanced(3) | ||
|
|
||
| def test_as_nexus_labels_basic(self): | ||
| ts = self.balanced_tree().tree_sequence | ||
| labels = {0: "human", 1: "chimp", 2: "bonobo"} | ||
| expected = textwrap.dedent( | ||
| """\ | ||
| #NEXUS | ||
| BEGIN TAXA; | ||
| DIMENSIONS NTAX=3; | ||
| TAXLABELS human chimp bonobo; | ||
| END; | ||
| BEGIN TREES; | ||
| TRANSLATE n0 human, n1 chimp, n2 bonobo; | ||
| TREE t0^1 = [&R] (n0:2,(n1:1,n2:1):1); | ||
| END; | ||
| """ | ||
| ) | ||
| assert expected == ts.as_nexus(include_alignments=False, node_labels=labels) | ||
|
|
||
| def test_as_nexus_labels_partial(self): | ||
| ts = self.balanced_tree().tree_sequence | ||
| labels = {0: "human", 2: "bonobo"} | ||
| expected = textwrap.dedent( | ||
| """\ | ||
| #NEXUS | ||
| BEGIN TAXA; | ||
| DIMENSIONS NTAX=3; | ||
| TAXLABELS human n1 bonobo; | ||
| END; | ||
| BEGIN TREES; | ||
| TRANSLATE n0 human, n2 bonobo; | ||
| TREE t0^1 = [&R] (n0:2,(n1:1,n2:1):1); | ||
| END; | ||
| """ | ||
| ) | ||
| assert expected == ts.as_nexus(include_alignments=False, node_labels=labels) | ||
|
|
||
| def test_as_nexus_labels_none(self): | ||
| ts = self.balanced_tree().tree_sequence | ||
| expected = textwrap.dedent( | ||
| """\ | ||
| #NEXUS | ||
| BEGIN TAXA; | ||
| DIMENSIONS NTAX=3; | ||
| TAXLABELS n0 n1 n2; | ||
| END; | ||
| BEGIN TREES; | ||
| TREE t0^1 = [&R] (n0:2,(n1:1,n2:1):1); | ||
| END; | ||
| """ | ||
| ) | ||
| assert expected == ts.as_nexus(include_alignments=False, node_labels=None) | ||
|
|
||
| @pytest.mark.parametrize("ts", get_example_tree_sequences()) | ||
| def test_parseable(self, ts): | ||
| for tree in ts.trees(): | ||
| if not tree.has_single_root: | ||
| return | ||
|
|
||
| labels = {} | ||
| samples = ts.samples() | ||
| k = random.randint(1, len(samples)) | ||
| for node in random.sample(list(samples), k): | ||
| labels[node] = f"new_node_which_was_{node}" | ||
|
|
||
| nexus = ts.as_nexus(include_alignments=False, node_labels=labels) | ||
| print(nexus) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stray print |
||
| dendropy.DataSet.get(data=nexus, schema="nexus") | ||
|
|
||
|
|
||
| class TestNewickCodePaths: | ||
| """ | ||
| Test that the different code paths we use under the hood lead to | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6797,6 +6797,7 @@ def write_nexus( | |||||
| reference_sequence=None, | ||||||
| missing_data_character=None, | ||||||
| isolated_as_missing=None, | ||||||
| node_labels=None, | ||||||
| ): | ||||||
| """ | ||||||
| Returns a `nexus encoding <https://en.wikipedia.org/wiki/Nexus_file>`_ | ||||||
|
|
@@ -6896,6 +6897,10 @@ def write_nexus( | |||||
| :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 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Index is ambigous here, better to be clear |
||||||
| the names must not have whitespace (spaces should be replaced by | ||||||
| underscores) or puncuation in them. | ||||||
| :return: A nexus representation of this :class:`TreeSequence` | ||||||
| :rtype: str | ||||||
| """ | ||||||
|
|
@@ -6908,6 +6913,7 @@ def write_nexus( | |||||
| reference_sequence=reference_sequence, | ||||||
| missing_data_character=missing_data_character, | ||||||
| isolated_as_missing=isolated_as_missing, | ||||||
| node_labels=node_labels, | ||||||
| ) | ||||||
|
|
||||||
| def as_nexus(self, **kwargs): | ||||||
|
|
||||||
There was a problem hiding this comment.
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
And then after just check that
labels[u]is recognised as a taxon ID by dendropy?