Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (36 sloc) 1.46 KB
from nwck_class_3 import Newick_Tree
class Parsimony:
def __init__(self,tree,sequences,alphabet,length):
self.sequences=sequences
self.seq=dict()
self.costV=dict()
self.cost=dict()
self.tree=tree
self.alphabet=alphabet
self.N=length
for taxon in tree.labels.keys():
self.seq[tree.labels[taxon]]=self.sequences[taxon]
for i in range(self.N):
self.costV[(tree.labels[taxon],i,sequences[taxon][i])]=0
self.cost[(tree.labels[taxon],i)]=0
self.sankoff()
def sankoff(self,base=None):
if not base:
base=self.tree.root
if len(self.tree.dtree[base])==0:
return
for v in self.tree.dtree[base]:
self.sankoff(v)
for i in range(self.N):
for c in self.alphabet:
m=0
for v in self.tree.dtree[base]:
if (v,i,c) in self.costV:
m=m+min(self.costV[(v,i,c)],self.cost[(v,i)]+1)
else:
m=m+self.cost[(v,i)]+1
self.costV[(base,i,c)]=m
self.cost[(base,i)]=min([self.costV[(base,i,c)] for c in self.alphabet])
return
def score(self):
return sum([self.costV[(self.tree.root,i,self.sequences[self.tree.taxa[self.tree.node_to_taxon[self.tree.root]]][i])] for i in range(self.N)])
You can’t perform that action at this time.