Skip to content

Commit

Permalink
ipa.treemix test for toytree < v3
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacovercast committed Apr 29, 2024
1 parent 1d67caf commit fc7a742
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions ipyrad/analysis/treemix.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
conda install toytree -c eaton-lab
""")

_VERSION_TOYTREE = IPyradError("""
Treemix analysis tool requires toytree < 3.0.0. You can roll
back to a working version of toytree with this command in the terminal.
conda install -c eaton-lab toytree=2.0.5
""")

_MISSING_TREEMIX = ImportError("""
This ipyrad tool requires the progam TREEMIX. See recommended installation
instructions here:
Expand Down Expand Up @@ -123,6 +130,10 @@ def __init__(
self.raise_root_error = raise_root_error
self._find_binary()

# Check toytree version is < 2
if int(toytree.__version__.split(".")[0]) > 2:
raise(_VERSION_TOYTREE)

# params dict
self.params = Params()
self.params.k = 0
Expand Down Expand Up @@ -265,7 +276,7 @@ def draw_tree(self, axes=None):
Returns a treemix plot on a toyplot.axes object.
"""
# create a toytree object from the treemix tree result
tre = toytree.tree(newick=self.results.tree)
tre = toytree.tree(self.results.tree)

# draw on axes or create new ones
if axes:
Expand Down Expand Up @@ -419,22 +430,37 @@ def _parse_results(self):
admix = self.results.admixture[aidx]

source = toytree.tree(admix[0] + ";")
if len(source) == 1:
if len(source.get_tip_labels()) == 1:
name = admix[0].split(":")[0]
sodx = tre.treenode.search_nodes(name=name)[0]
try:
sodx = tre.treenode.search_nodes(name=name)[0]
except AttributeError:
# The string of try/except here is handling differences between
# ToyTree V2 and V3
sodx = tre.get_nodes(name)[0]
sodx = sodx.idx
else:
lvs = source.get_tip_labels()
sodx = tre.treenode.get_common_ancestor(lvs).idx
try:
sodx = tre.treenode.get_common_ancestor(lvs).idx
except AttributeError:
sodx = tre.get_mrca_node(*lvs).idx

sink = toytree.tree(admix[1] + ";")
if len(sink) == 1:
if len(sink.get_tip_labels()) == 1:
name = admix[1].split(":")[0]
sidx = tre.treenode.search_nodes(name=name)[0]
try:
sidx = tre.treenode.search_nodes(name=name)[0]
except AttributeError:
# ToyTree V3
sidx = tre.get_nodes(name)[0]
sidx = sidx.idx
else:
lvs = sink.get_tip_labels()
sidx = tre.treenode.get_common_ancestor(lvs).idx
try:
sidx = tre.treenode.get_common_ancestor(lvs).idx
except AttributeError:
sidx = tre.get_mrca_node(*lvs).idx

self.results.admixture[aidx] = (
int(sodx),
Expand Down

0 comments on commit fc7a742

Please sign in to comment.