Repository navigation
Closed
Description
Setup
CPython
Windows-10-10.0.17134
1.73
Description
In Phylo._utils, we can use the draw_ascii() function to draw a command line-friendly representation of our phylogeny tree. The function can be found here:
Lines 206 to 269 in 0593e2f
| def draw_ascii(tree, file=None, column_width=80): | |
| """Draw an ascii-art phylogram of the given tree. | |
| The printed result looks like:: | |
| _________ Orange | |
| ______________| | |
| | |______________ Tangerine | |
| ______________| | |
| | | _________________________ Grapefruit | |
| _| |_________| | |
| | |______________ Pummelo | |
| | | |
| |__________________________________ Apple | |
| :Parameters: | |
| file : file-like object | |
| File handle opened for writing the output drawing. (Default: | |
| standard output) | |
| column_width : int | |
| Total number of text columns used by the drawing. | |
| """ | |
| if file is None: | |
| file = sys.stdout | |
| taxa = tree.get_terminals() | |
| # Some constants for the drawing calculations | |
| max_label_width = max(len(str(taxon)) for taxon in taxa) | |
| drawing_width = column_width - max_label_width - 1 | |
| drawing_height = 2 * len(taxa) - 1 | |
| def get_col_positions(tree): | |
| """Create a mapping of each clade to its column position.""" | |
| depths = tree.depths() | |
| # If there are no branch lengths, assume unit branch lengths | |
| if not max(depths.values()): | |
| depths = tree.depths(unit_branch_lengths=True) | |
| # Potential drawing overflow due to rounding -- 1 char per tree layer | |
| fudge_margin = int(math.ceil(math.log(len(taxa), 2))) | |
| cols_per_branch_unit = ((drawing_width - fudge_margin) / | |
| float(max(depths.values()))) | |
| return dict((clade, int(blen * cols_per_branch_unit + 1.0)) | |
| for clade, blen in depths.items()) | |
| def get_row_positions(tree): | |
| positions = dict((taxon, 2 * idx) for idx, taxon in enumerate(taxa)) | |
| def calc_row(clade): | |
| for subclade in clade: | |
| if subclade not in positions: | |
| calc_row(subclade) | |
| positions[clade] = ((positions[clade.clades[0]] + | |
| positions[clade.clades[-1]]) // 2) | |
| calc_row(tree.root) | |
| return positions | |
| col_positions = get_col_positions(tree) | |
| row_positions = get_row_positions(tree) | |
| char_matrix = [[' ' for x in range(drawing_width)] | |
| for y in range(drawing_height)] | |
We notice that the get_col_position function assigns a unit branch length when there are no depth values, using the following conditional statement:
"""Create a mapping of each clade to its column position."""
depths = tree.depths()
# If there are no branch lengths, assume unit branch lengths
if not max(depths.values()):
depths = tree.depths(unit_branch_lengths=True)However, I believe we could explicitize this condition by slightly modifying it:
depths = tree.depths()
# If there are no branch lengths, assume unit branch lengths
if max(depths.values()) == 0:
depths = tree.depths(unit_branch_lengths=True)Which, although doesn't affect the behavior, makes the code clearer (since this method is particularly useful for people who want to learn how to draw phylogeny trees in different context). It also follows PEP8 recommendations.
Metadata
Metadata
Assignees
Labels
No labels
Activity
peterjc commentedon Jan 25, 2019
PEP8 doesn't seem to cover this, but in general Python style would be
if expr:andif not expr:rather thanif expr == 0andif expr != 0:when testing if something is or isn't zero.However, I agree your proposal is clearer when reading the code. Over to @etal to decide.
xhluca commentedon Jan 25, 2019
Thanks!
mkcor commentedon Jan 28, 2019
@peterjc Ok in general but, here,
max(depths.values())is expected to be a number, so we might as well be explicit (instead of relying on the implicit type conversion), right?In between
depths = tree.depths()and theifstatement, should we include some type checking or exception handling?peterjc commentedon Feb 1, 2019
Looks like @etal is busy - do you want to make this into a pull request please @xhlulu?
For a small change like this, you don't actually need to use git at the command line - you could do it all from your web-browser as GitHub lets you edit files directly.
xhluca commentedon Feb 1, 2019
@peterjc Thanks for the heads up! I created the PR at #1916
peterjc commentedon May 15, 2019
Closed via #1916.