Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two new utilities #626

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions OZprivate/ServerScripts/Utilities/format_newick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
'''
Format a newick tree with indentation to make it more human readable. This is not meant for very large trees.

For example, if the input tree is:

(Tupaia_tana:8.5,(Tupaia_picta:8.0,(Tupaia_montana:7.0,Tupaia_splendidula:7.0):1.0):0.5):4.5;

The output is:

(
Tupaia_tana:8.5,
(
Tupaia_picta:8.0,
(
Tupaia_montana:7.0,
Tupaia_splendidula:7.0
):1.0
):0.5
):4.5;
'''

import argparse
import sys
from dendropy import Tree

parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('treefile', type=argparse.FileType('r'), nargs='?', default=sys.stdin, help='The tree file in newick form')
parser.add_argument('outfile', type=argparse.FileType('w'), nargs='?', default=sys.stdout, help='The output tree file')
parser.add_argument('--indent_spaces', '-i', default=2, type=int, help='the number of spaces for each indentation level')
args = parser.parse_args()

tree = Tree.get(stream=args.treefile, schema='newick', preserve_underscores=True, suppress_leaf_node_taxa=True, suppress_internal_node_taxa=True)
output_stream = args.outfile
indent_string = ' ' * args.indent_spaces

def process_node(node, depth):
output_stream.write(indent_string * depth)

if len(node.child_nodes()) > 0:
output_stream.write('(\n')

# Go through children and process them recursively, increasing the depth
for count, child_node in enumerate(node.child_nodes()):
process_node(child_node, depth+1)

# Commas between the children
if count < len(node.child_nodes())-1:
output_stream.write(',')

output_stream.write('\n')

output_stream.write(indent_string * depth + ')')

if node.label:
output_stream.write(node.label)
if node.edge_length:
output_stream.write(':' + str(node.edge_length))

process_node(tree.seed_node, 0)

# Need a ; at the end of the newick
output_stream.write(';\n')
57 changes: 57 additions & 0 deletions OZprivate/ServerScripts/Utilities/ultrametric_to_additive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
'''
Transform an ultrametric newick tree into an additive newick tree

e.g. if A split from B 7 MYA, and C split from A&B 20 MYA, the ultrametric tree would be:

(
(
A,
B
):7,
C
):20;

And this code turns that into a proper additive newick tree:

(
(
A:7,
B:7
):13,
C:20
);

Note that the input is a clear abuse of the newick branch length syntax, since
we're not actually storing length, but ages. It is however convenient as a
transitional format when translating an ultrametric diagram to newick format.
'''

import argparse
import sys
from dendropy import Tree

parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('treefile', type=argparse.FileType('r'), nargs='?', default=sys.stdin, help='The ultrametric tree in newick form')
parser.add_argument('outfile', type=argparse.FileType('w'), nargs='?', default=sys.stdout, help='The output newick tree file')
args = parser.parse_args()

def process_node(node, parent_age):
# If a input node has a length, it's semantically an age, not a length
node_age = node.edge_length if node.edge_length else 0

# The actual edge length is the difference between the parent age and the node age
node.edge_length = round(parent_age - node_age, 2)
if node.edge_length == 0:
node.edge_length = None

# Process children recursively,
for child_node in node.child_nodes():
process_node(child_node, node_age)

tree = Tree.get(stream=args.treefile, schema='newick', preserve_underscores=True,
suppress_leaf_node_taxa=True, suppress_internal_node_taxa=True)
process_node(tree.seed_node, tree.seed_node.edge_length if tree.seed_node.edge_length else 0)

tree.write(file=args.outfile, schema='newick', suppress_leaf_node_labels=False)