Skip to content

Commit

Permalink
closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Oct 5, 2023
1 parent 1178c27 commit 5bef77c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## [Unreleased]

- Fixed bug whereby the correct precedence of taxa labels was not obeyed when
translating trees.


## [v1.6.0] - 2023-08-09

- `tools.normalise.normalise` now accepts a `remove_taxa` argument, making
Expand Down
4 changes: 2 additions & 2 deletions src/commonnexus/blocks/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ def validate(self, log=None):
@functools.cached_property
def translate_mapping(self):
mapping = {}
if 'TRANSLATE' in self.commands:
mapping.update(self.TRANSLATE.mapping)
if 'TAXA' in self.linked_blocks:
mapping.update({
str(k): v for k, v in self.linked_blocks['TAXA'].TAXLABELS.labels.items()})
elif self.nexus.TAXA and self.nexus.TAXA.TAXLABELS:
mapping.update({str(k): v for k, v in self.nexus.TAXA.TAXLABELS.labels.items()})
if 'TRANSLATE' in self.commands:
mapping.update(self.TRANSLATE.mapping)
return mapping

def translate(self, tree: typing.Union[Tree, newick.Node]) -> newick.Node:
Expand Down
2 changes: 1 addition & 1 deletion src/commonnexus/tools/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- CHARACTERS/DATA: Characters are aggregated across NEXUS files (with character labels prefixed,
for disambiguation).
- TREES: Trees are (translated and) aggregated across NEXUS files.
- DISTANCES?
"""
import collections

Expand Down
13 changes: 13 additions & 0 deletions tests/test_blocks_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ def test_Tree_translate_with_taxa_from_linked_block(nexus):
assert nex.TREES.translate(nex.TREES.TREE).newick == '(A,B)C'


def test_Tree_translate_with_numeric_labels():
nex = Nexus("""#NEXUS
BEGIN TAXA;
TAXLABELS Scarabaeus Drosophila Aranaeus;
END;
BEGIN TREES;
TRANSLATE 0 Scarabaeus, 1 Drosophila, 2 Aranaeus;
TREE tree = ((0,1),2);
END;""")
tree = nex.TREES.translate(nex.TREES.TREE)
assert {n.name for n in tree.walk() if n.name} == {'Scarabaeus', 'Drosophila', 'Aranaeus'}


def test_Trees_complex_newick():
s = """#nexus
BEGIN TAXA;
Expand Down

0 comments on commit 5bef77c

Please sign in to comment.