forked from jrjhealey/bioinfo-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_dists.py
65 lines (54 loc) · 2.15 KB
/
tree_dists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#/usr/bin/env python
"""
Get the distances between nodes in a phylogenetic tree.
Requires dendropy.
"""
from __future__ import print_function
import argparse, sys
try:
import dendropy
except ImportError:
msg = """
The dendropy import failed, the module doesn't appear to be installed
(at least in the PYTHONPATH for this python binary").
Try running:
$ python -m pip install -U dendropy
or
$ conda install -c etetoolkit ete3 ete_toolchain
"""
print(msg)
sys.exit(1)
def get_args():
"""Parse command line arguments."""
try:
parser = argparse.ArgumentParser(description="Get the distances between nodes in a tree.",
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-i','--input', action='store', required=True, help='Input tree file.')
parser.add_argument('-s', '--schema', action='store', required=True, help='Input tree format.')
parser.add_argument('-m', '--mode', action='store', choices=['all', 'max'], default='max',
help='What distances to return: Largest = \'max\', or All = \'all\'.')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
except:
print("An exception occurred with argument parsing. Check your provided options.", file=sys.stderr)
sys.exit(1)
return parser.parse_args()
def err(string):
"""Print errors to stderr"""
print("%s" % string, file=sys.stderr)
def main():
"""Calculate the largest and smallest distances in a phylogenetic tree via Dendropy."""
args = get_args()
# Run dendropy functions
tree = dendropy.Tree.get(path=args.input, schema=args.schema)
pdm = tree.phylogenetic_distance_matrix()
if args.mode is 'max':
max1, max2 = pdm.max_pairwise_distance_taxa()
print("Distance between '%s' and '%s': %s" % (max1.label, max2.label, pdm(max1, max2)))
else:
for i, t1 in enumerate(tree.taxon_namespace[:-1]):
for t2 in tree.taxon_namespace[i+1:]:
print("Distance between '%s' and '%s': %s" % (t1.label, t2.label, pdm(t1, t2)))
if __name__ == "__main__":
main()