-
Notifications
You must be signed in to change notification settings - Fork 0
/
compare.py
62 lines (46 loc) · 2.05 KB
/
compare.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
import dendropy
import sys
import re
def compareTreesFromPath(treePath1, treePath2):
#print("Comparing {} with {}".format(treePath1, treePath2))
tax = dendropy.TaxonNamespace()
tr1 = dendropy.Tree.get(path=treePath1,
schema='newick',
rooting='force-unrooted',
taxon_namespace=tax,
preserve_underscores=True)
tr2 = dendropy.Tree.get(path=treePath2,
schema='newick',
rooting='force-unrooted',
taxon_namespace=tax,
preserve_underscores=True)
tr1.collapse_basal_bifurcation(set_as_unrooted_tree=True)
tr2.collapse_basal_bifurcation(set_as_unrooted_tree=True)
return compareDendropyTrees(tr1, tr2)
#print("RF distance on %d shared leaves: %d" % (nl, fp + fn))
def compareDendropyTrees(tr1, tr2):
from dendropy.calculate.treecompare \
import false_positives_and_negatives
lb1 = set([l.taxon.label for l in tr1.leaf_nodes()])
lb2 = set([l.taxon.label for l in tr2.leaf_nodes()])
com = lb1.intersection(lb2)
if com != lb1 or com != lb2:
com = list(com)
tns = dendropy.TaxonNamespace(com)
tr1.retain_taxa_with_labels(com)
tr1.migrate_taxon_namespace(tns)
tr2.retain_taxa_with_labels(com)
tr2.migrate_taxon_namespace(tns)
com = list(com)
tr1.update_bipartitions()
tr2.update_bipartitions()
nl = len(com)
ei1 = len(tr1.internal_edges(exclude_seed_edge=True))
ei2 = len(tr2.internal_edges(exclude_seed_edge=True))
[fp, fn] = false_positives_and_negatives(tr1, tr2)
rf = float(fp + fn) / (ei1 + ei2)
return (nl, ei1, ei2, fp, fn, rf)
answer = compareTreesFromPath(sys.argv[1],sys.argv[2])
dataset = sys.argv[1].split("/")
print(str(format(answer[3]/(answer[0]-3), '.4f'))+' '+str(format(answer[4]/(answer[0]-3), '.4f')), end=' ')
#The methods compareTreesFromPath() and compareDendropyTrees() has been implemented by Erin K. Molloy