From 84b1f5eb47a80d9bf472c40e2381fb5ad015197b Mon Sep 17 00:00:00 2001 From: Ricardo Pinho Date: Thu, 19 Nov 2026 08:21:07 -0500 Subject: [PATCH] Apply NEXUS node labels to alignment rows --- python/CHANGELOG.rst | 5 +++++ python/tests/test_phylo_formats.py | 22 ++++++++++++++++++++++ python/tskit/text_formats.py | 14 +++++--------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/python/CHANGELOG.rst b/python/CHANGELOG.rst index 260381b3ca..9243963df1 100644 --- a/python/CHANGELOG.rst +++ b/python/CHANGELOG.rst @@ -10,6 +10,11 @@ In development list of individuals (``individuals=[]``), writing a sites-only VCF with no ``FORMAT`` or sample columns. (:user:`RClaudiuM`, :issue:`3463`) +**Bugfixes** + +- Apply ``node_labels`` to sequence alignments when exporting NEXUS. + (:user:`rpinho`, :pr:`3514`) + -------------------- [1.0.4] - 2026-10-30 -------------------- diff --git a/python/tests/test_phylo_formats.py b/python/tests/test_phylo_formats.py index 91bd86c542..f362008fea 100644 --- a/python/tests/test_phylo_formats.py +++ b/python/tests/test_phylo_formats.py @@ -281,6 +281,28 @@ def test_nexus_default(self): ) assert expected == self.ts().as_nexus(reference_sequence=ref) + @pytest.mark.parametrize("include_trees", [False, True]) + def test_nexus_labels_with_alignments(self, include_trees): + nexus = self.ts().as_nexus( + reference_sequence="ACTGACTGAC", + node_labels={0: "human", 2: "bonobo"}, + include_trees=include_trees, + ) + dataset = dendropy.DataSet.get(data=nexus, schema="nexus") + matrix = dataset.char_matrices[0] + assert {taxon.label: str(matrix[taxon]) for taxon in matrix} == { + "human": "ACGGACTGAT", + "n1": "ACAGACTGAC", + "bonobo": "ACAGACTGAC", + } + if include_trees: + tree = dataset.tree_lists[0][0] + assert {node.taxon.label for node in tree.leaf_node_iter()} == { + "human", + "n1", + "bonobo", + } + def test_nexus_no_trees(self): ref = "ACTGACTGAC" expected = textwrap.dedent( diff --git a/python/tskit/text_formats.py b/python/tskit/text_formats.py index 9898e6262a..e9db5c7161 100644 --- a/python/tskit/text_formats.py +++ b/python/tskit/text_formats.py @@ -136,13 +136,9 @@ def write_nexus( print("BEGIN TAXA;", file=out) print("", f"DIMENSIONS NTAX={ts.num_samples};", sep=indent, file=out) - if node_labels is not None: - taxlabels = " ".join( - node_labels[u] if u in node_labels else f"n{u}" for u in ts.samples() - ) - else: - taxlabels = " ".join(f"n{u}" for u in ts.samples()) - print("", f"TAXLABELS {taxlabels};", sep=indent, file=out) + labels = {} if node_labels is None else node_labels + sample_labels = [labels.get(u, f"n{u}") for u in ts.samples()] + print("", f"TAXLABELS {' '.join(sample_labels)};", sep=indent, file=out) print("END;", file=out) if include_alignments is None: @@ -165,8 +161,8 @@ def write_nexus( missing_data_character=missing_data_character, isolated_as_missing=isolated_as_missing, ) - for u, alignment in zip(ts.samples(), alignments): - print(2 * indent, f"n{u}", " ", alignment, sep="", file=out) + for label, alignment in zip(sample_labels, alignments): + print(f"{2 * indent}{label} {alignment}", file=out) print("", ";", sep=indent, file=out) print("END;", file=out)