Skip to content

[Discussion] get_col_position depth value testing in draw_ascii #1910

Closed

Description

@xhluca

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:

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.

Activity

  1. peterjc commented on Jan 25, 2019

    @peterjc
    Member

    PEP8 doesn't seem to cover this, but in general Python style would be if expr: and if not expr: rather than if expr == 0 and if 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.

  2. xhluca commented on Jan 25, 2019

    @xhluca
    ContributorAuthor

    Thanks!

  3. mkcor commented on Jan 28, 2019

    @mkcor

    @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 the if statement, should we include some type checking or exception handling?

  4. peterjc commented on Feb 1, 2019

    @peterjc
    Member

    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.

  5. xhluca commented on Feb 1, 2019

    @xhluca
    ContributorAuthor

    @peterjc Thanks for the heads up! I created the PR at #1916

  6. peterjc commented on May 15, 2019

    @peterjc
    Member

    Closed via #1916.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions