Skip to content

Commit

Permalink
network update
Browse files Browse the repository at this point in the history
  • Loading branch information
eaton-lab committed Oct 29, 2024
1 parent 64f85fb commit 1aa1ee5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
1 change: 0 additions & 1 deletion toytree/network/src/parse_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ def test_interactive():


if __name__ == "__main__":

# toytree.set_log_level("TRACE")
# t0, a0 = test_interactive()
t0, a0 = test3B()
Expand Down
35 changes: 21 additions & 14 deletions toytree/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import re
from loguru import logger
import toytree
from toytree.io.src.parse import WHITE_SPACE
from toytree.io.src.utils import replace_whitespace

logger = logger.bind(name="toytree")

Expand Down Expand Up @@ -114,7 +114,8 @@ def _get_network_as_newick_str(self, net: Union[str, Path]) -> str:
if ";" in net:
net = net.split(";")[0] + ';'
# strip whitespace
net = WHITE_SPACE.sub("", net)
net = replace_whitespace(net)
# net = WHITE_SPACE.sub("", net)
return net

def _set_tree_with_admix_as_node_labels(self) -> toytree.ToyTree:
Expand Down Expand Up @@ -155,12 +156,12 @@ def get_major_tree_and_admix_edges(self) -> Tuple:
self._set_tree_with_admix_as_node_labels()
self.tree._draw_browser(ts='s', layout='unr', width=700, node_labels="name", node_colors="pink")
self.tree = self._pseudo_unroot()
self.tree._draw_browser(ts='s', width=500, node_labels="idx", node_colors="pink")
self.tree._draw_browser(ts='s', width=500, node_labels="idx", node_colors="pink", tmpdir="~")

# restart traversal after each pair of nodes is fixed.
while 1:
try:
hnodes = self.tree.get_nodes("#H*", regex=True)
hnodes = self.tree.get_nodes("~#H*")
hnodes = sorted(hnodes, key=lambda x: x.name)
except ValueError:
self.tree.mod.remove_unary_nodes(inplace=True)
Expand All @@ -178,8 +179,8 @@ def get_major_tree_and_admix_edges(self) -> Tuple:

# remove the tip node representing a src
logger.debug(f"deleting minor node: {hnodes[1].name} {hnodes[1].idx}")
hnodes[1]._delete()
desc1 = tuple(hnodes[1]._up.get_leaf_names())
hnodes[1]._delete()
desc1 = [i for i in desc1 if not i.startswith("#H")]
logger.trace(f"desc1: {desc1}")

Expand Down Expand Up @@ -376,12 +377,18 @@ def test_interactive_3():

toytree.set_log_level("TRACE")
# t0, a0 = test3B()
t0, a0 = test_am_2()
# t0 = t0.root("fimbriatus")
t0._draw_browser(
ts='s',
width=500, height=800,
node_labels="idx",
use_edge_lengths=True,
admixture_edges=a0,
)
NET = "(r1,(r2,(r3,(r4,#H6):10.0):1.949048825686914):10.0,(r0)#H6);"
# parse_network(NET)
parser = NetworkToMajorTree(NET)
tree, edges = parser.get_major_tree_and_admix_edges()
tree._draw_browser(tmpdir="~", admixture_edges=edges)

# t0, a0 = test_am_2()
# # t0 = t0.root("fimbriatus")
# t0._draw_browser(
# ts='s',
# width=500, height=800,
# node_labels="idx",
# use_edge_lengths=True,
# admixture_edges=a0,
# )

0 comments on commit 1aa1ee5

Please sign in to comment.