-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance_script.py
49 lines (44 loc) · 1.62 KB
/
distance_script.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
# Importing the necessary modules
import dendropy
import sys
# Specifying the paths of our tree list(s). Include full path if not in wd
file1 = sys.argv[1]
if len(sys.argv) == 4:
file2 = sys.argv[2]
output = sys.argv[3]
elif len(sys.argv) == 3:
output = sys.argv[2]
# Reads our tree list(s) into a dendropy dataset under a common taxon namespace
# Edit to add a second tree list
namespace = dendropy.TaxonNamespace()
trees = dendropy.DataSet()
trees.attach_taxon_namespace(namespace)
tree_list1 = trees.new_tree_list()
tree_list1.read(path=file1, schema='newick')
if len(sys.argv) == 4:
tree_list2 = trees.new_tree_list()
tree_list2.read(path=file2, schema='newick')
# Function that accepts one list of trees, iterating over each tree to calculate the distance between it and
# every other tree (excluding itself)
# uses indexing magic to avoid redundant calculations
def one_list(list1):
with open(output, 'wb') as f:
for i in range(len(list1)-1):
ls = range(i+1, len(list1))
for j in ls:
f.write(str(dendropy.calculate.treecompare.symmetric_difference(list1[i], list1[j])))
f.write('\n')
print 'Tree ' + str(i+1)
# Function that accepts two lists of trees, iterating over each tree in the first list and calculating the distance
# between it and every tree in the second list
def two_list(list1, list2):
with open(output, 'wb') as f:
for i in range(len(list1)):
for j in range(len(list2)):
f.write(str(dendropy.calculate.treecompare.symmetric_difference(list1[i], list2[j])))
f.write('\n')
print 'Tree ' + str(i+1)
if 'file2' not in locals():
one_list(tree_list1)
else:
two_list(tree_list1, tree_list2)