Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Genomics/parsimony_class.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
41 lines (36 sloc)
1.46 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]) | |