diff --git a/phylo.py b/phylo.py index 13a1a17975be35a6ea6235e8fc2de3e6fc0050af..19ea3ec74f1bbd05403787b83213a648ca8e9884 100644 --- a/phylo.py +++ b/phylo.py @@ -193,37 +193,18 @@ class PhyloNode: for i in range(len(stubs) - 1): stubstr += stubs[i] + ',' return stubstr + stubs[-1] + ')' + label + dist - # there is no label - ''' - if not self.left and self.right: - return ',' + right - elif self.left and not self.right: - return left + ',' - elif self.left and self.right: - return '(' + left + ',' + right + ')' + dist - ''' - - # def __le__(self, other): - # """ Returns indication of less than other node. """ - # return other and self.__hash__() <= other.__hash__() - # - # def __eq__(self, other): - # """ Returns indication of equivalence to other node. """ - # return other and self.__hash__() == other.__hash__() - # - # def __hash__(self): - # """ Returns hash of object. """ - # return hash((self.label, self.dist, self.sequence)) def _printSequences(self, start, end): """ Returns string with node (incl descendants) in a Newick style. """ stubs = ['' for _ in range(self.nChildren())] label = dist = '' for i in range(self.nChildren()): - stubs[i] = self._printSequences(self.children[i], start, end) + stubs[i] = self.children[i]._printSequences(start, end) if self.dist or self.dist == 0.0: dist = ':' + str(self.dist) - if self.label != None: + if self.sequence != None: + label = "".join(self.sequence[start:end]) + "" + else: # there is no sequence, just use label label = str(self.label) if self.nChildren() == 0: return label + dist @@ -263,20 +244,6 @@ class PhyloNode: self.sequence = seq break - """ # Not sure if this is required (putting nodes into a canonical ordering) - def _canonise(self): - if self.left == None and self.right == None: # at leaf - return self.label - myleft = self.left._canonise() - myright = self.right._canonise(); - if myleft > myright: - tmpnode = self.left - self.left = self.right - self.right = tmpnode - return myright - return myleft - """ - def _forwardParsimony(self, aln): """ Internal function that operates recursively to first initialise each node (forward), stopping only once a sequence has been assigned to the node, @@ -343,10 +310,10 @@ class PhyloNode: child_symb = [None for _ in range(self.nChildren())] for i in range(self.nChildren()): child_symb[i] = self.backptr[i][col][a_parent] - childbuf.append(aln.alphabet[child_symb[i]]) + childbuf[i].append(aln.alphabet[child_symb[i]]) col += 1 for i in range(self.nChildren()): - self.children[i]._backwardParsimony(aln, sequence.Sequence(childbuf[i], aln.alphabet, self.label, gappy=True)) + self.children[i]._backwardParsimony(aln, sequence.Sequence(childbuf[i], aln.alphabet, self.children[i].label or "Child of "+self.label, gappy=True)) return self.sequence def getSequence(self): @@ -623,6 +590,12 @@ def get_unique_tree(tree): def unpack_list(list): return (" ".join(["%s"] * len(list)) + "!") % (x for x in list) -if __name__ == '__main__': +if __name__ == '__main__1': tree = readNewick('/Users/mikael/simhome/ASR/edge1.nwk') - print(tree) \ No newline at end of file + print(tree) + +if __name__ == '__main__': + tree = readNewick('/Users/mikael/simhome/ASR/parsitest.nwk') + tree.putAlignment(sequence.Alignment(sequence.readFastaFile('/Users/mikael/simhome/ASR/parsitest.aln', sequence.DNA_Alphabet))) + tree.parsimony() + print(tree.strSequences()) \ No newline at end of file