Repository navigation
Expand file tree
/
Copy pathcmp_trees.py
More file actions
executable file
·55 lines (49 loc) · 1.53 KB
/
Copy pathcmp_trees.py
File metadata and controls
executable file
·55 lines (49 loc) · 1.53 KB
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
#! /usr/bin/env python
import __main__
from ete2 import Tree
# cmp_trees.py -l tree_list -o tree_dist.txt
def main(args):
outstr=''
rf={}
max_rf={}
name=list()
with open(args.listfile, 'r') as inlist1:
for line1 in inlist1.readlines():
line1=line1.strip()
name.append(line1)
t1=Tree(line1)
rf[line1]={}
max_rf[line1]={}
with open(args.listfile, 'r') as inlist2:
for line2 in inlist2.readlines():
line2=line2.strip()
t2=Tree(line2)
rf_result=t1.robinson_foulds(t2,unrooted_trees=True)
rf[line1][line2]=rf_result[0]
max_rf[line1][line2]=rf_result[1]
col_width = max(len(word) for word in name) + 2
col_width2=4
for key1,val1 in sorted(rf.items()):
outstr+= str("".join(key1.ljust(col_width)))
tot=0
ct=0
for key2,val2 in sorted(rf.items()):
ct+=1
outstr+= str("".join(str(rf[key1][key2]).ljust(col_width2)))
tot+=rf[key1][key2]
avg=tot/ct
outstr+= "="+"".join(str(avg))+"/"+str(max_rf[key1][key2])
outstr+="\n"
print outstr
if args.output is not None:
with open(args.output, 'w') as outfile:
outfile.write(outstr)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Compare trees and calculate Robinson-Foulds distance",
epilog="Contact Rahil for help.")
parser.add_argument('-l', '--listfile', help='List of trees to compare with path', required=True)
parser.add_argument('-o', '--output', help='Output file to store the output matrix.', required=False)
args = parser.parse_args()
main(args)