Skip to content

Commit 05d3e8c

Browse files
committed
branch labels
Added option to add label branches by the id that appears in the html/css. This is not meant for fina output, but is instead useful if you want to secondarily edit the display properties of a particular branch and need to identify which ones you need to edit.
1 parent b703ae4 commit 05d3e8c

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

phy2html.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88

99
class Branch:
10-
def __init__(self, min_col: int = 0, col_span: int = 0, row: int = 0):
10+
def __init__(self, min_col: int = 0, col_span: int = 0, row: int = 0, label: str = " "):
1111
self.min_col = min_col
1212
self.col_span = col_span
1313
self.row = row
14+
self.label = label
1415

1516

1617
class VLine:
@@ -78,8 +79,9 @@ def write_tree_to_body(outlist: list, taxa: list, branches: list, vlines: list,
7879
outlist.append(" <div id=\"{0}taxon{1}\" "
7980
"class=\"{0}genus-species-name {0}taxon-name\">{2}</div>\n".format(prefix, i+1, t.node.name))
8081
outlist.append("\n")
81-
for b in range(len(branches)):
82-
outlist.append(" <div id=\"{0}branch{1}\" class=\"{0}branch-line\">&nbsp;</div>\n".format(prefix, b+1))
82+
for b, branch in enumerate(branches):
83+
outlist.append(" <div id=\"{0}branch{1}\" class=\"{0}branch-line\">{2}</div>\n".format(prefix, b+1,
84+
branch.label))
8385
outlist.append("\n")
8486
for v in range(len(vlines)):
8587
outlist.append(" <div id=\"{0}vline{1}\" class=\"{0}vert-line\">&nbsp;</div>\n".format(prefix, v+1))
@@ -93,7 +95,7 @@ def total_rows_per_node(n: int, rows_per_tip: int) -> int:
9395

9496

9597
def tree_recursion(tree, min_col: int, max_col: int, min_row: int, max_row: int, taxa: list, branches: list,
96-
vlines: list, rows_per_tip: int) -> int:
98+
vlines: list, rows_per_tip: int, label_branches: bool) -> int:
9799
"""
98100
calculate positions of taxa, branches, and vertical connectors on subtrees
99101
"""
@@ -124,7 +126,7 @@ def tree_recursion(tree, min_col: int, max_col: int, min_row: int, max_row: int,
124126
bottom_row = top_row + d_rows - 1
125127
# draw the descendant in its own smaller bounded box
126128
row = tree_recursion(d, min_col + col_span, max_col, top_row, bottom_row, taxa, branches, vlines,
127-
rows_per_tip)
129+
rows_per_tip, label_branches)
128130
"""
129131
the rows of the first and last descendants represent the positions to draw the vertical line
130132
connecting all of the descendants
@@ -157,15 +159,18 @@ def tree_recursion(tree, min_col: int, max_col: int, min_row: int, max_row: int,
157159
if col_span > 0:
158160
new_branch = Branch(min_col+1, col_span, row)
159161
branches.append(new_branch)
162+
if label_branches:
163+
new_branch.label = "branch" + str(len(branches))
160164

161165
return row
162166

163167

164-
def calculate_tree(tree: tree_utils.Node, nrows: int, ncols: int, rows_per_tip: int) -> Tuple[list, list, list]:
168+
def calculate_tree(tree: tree_utils.Node, nrows: int, ncols: int, rows_per_tip: int,
169+
label_branches: bool) -> Tuple[list, list, list]:
165170
taxa = []
166171
branches = []
167172
vlines = []
168-
tree_recursion(tree, 1, ncols, 1, nrows, taxa, branches, vlines, rows_per_tip)
173+
tree_recursion(tree, 1, ncols, 1, nrows, taxa, branches, vlines, rows_per_tip, label_branches)
169174
return taxa, branches, vlines
170175

171176

@@ -180,7 +185,8 @@ def add_node_depth(tree: tree_utils.Node, max_depth: int) -> None:
180185

181186

182187
def create_html_tree(inname: str, outname: str, col_width: str = "40px", row_height: str = "10px",
183-
name_width: str = "200px", prefix: str = "", rows_per_tip: int = 2) -> list:
188+
name_width: str = "200px", prefix: str = "", label_branches: bool = False,
189+
rows_per_tip: int = 2) -> list:
184190
with open(inname, "r") as infile:
185191
newick_str = infile.readline()
186192
print()
@@ -195,7 +201,7 @@ def create_html_tree(inname: str, outname: str, col_width: str = "40px", row_hei
195201
nrows = total_rows_per_node(ntips, rows_per_tip)
196202
ncols = tree.max_node_tip_count() + 1
197203
add_node_depth(tree, ncols+1)
198-
taxa, branches, vlines = calculate_tree(tree, nrows, ncols, rows_per_tip)
204+
taxa, branches, vlines = calculate_tree(tree, nrows, ncols, rows_per_tip, label_branches)
199205
outlist = []
200206
start_html(outlist)
201207
write_style_to_head(outlist, nrows, ncols, taxa, branches, vlines, col_width, row_height, name_width, prefix)
@@ -224,7 +230,12 @@ def main():
224230
row_height = query_user("Row height", "10px")
225231
name_width = query_user("Width of tip labels", "200px")
226232
prefix = query_user("(Optional) CSS ID prefix", "")
227-
create_html_tree(inname, outname, col_width, row_height, name_width, prefix)
233+
label_branches = query_user("Label branches [Y/N]", "N")
234+
if label_branches.lower() == "y":
235+
label_branches = True
236+
else:
237+
label_branches = False
238+
create_html_tree(inname, outname, col_width, row_height, name_width, prefix, label_branches)
228239

229240

230241
if __name__ == "__main__":

0 commit comments

Comments
 (0)