и тут я решила сначала добавить все ноды

 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
63
64
65
66
__author__ = 'Lenovo'
from treelib import Tree, Node
#class
#def representTreeAsStr(tree):
def addLeaf(leafIndex, tree, matrix):
#looking for closest neighbour (leaf or node)
tree.create_node(proteinList[leafIndex], parent = "root") #parent = founded node's parent
def makeNamesList(l):
for i in range(1, nProteins+1):
l[i-1] = l[i].split(" ")[0]
print l[i].split(" ")
print l[i-1]
f = open("C:\Users\Lenovo\PycharmProjects\PhylogeneticTree\data")
fd = f.read().split("Aligning...")
proteinList = fd[0].split(": ")
scoreList = fd[1].split("Sequences")
nProteins = len(proteinList)-1
makeNamesList(proteinList)
print proteinList
#building score's matrix
matrix = [[0 for i in range(len(proteinList) - 1)] for j in range(len(proteinList) - 1)]
pointInScoreList = 0
for i in range(nProteins - 1):
for j in range(i, nProteins - 1):
if j != i:
pointInScoreList += 1
if len(scoreList[pointInScoreList].split("Score: ")) >= 2:
score = float(scoreList[pointInScoreList].split("Score: ")[1].split("\n")[0])
#print score
matrix[i][j] = score
matrix[j][i] = score
print(matrix)
#finding max total score (general ancestor)
#totalScores = [0 for i in range(len(proteinList) - 1)]
ancestorIndex = 0
maxSum = 0
for i in range(nProteins - 1):
sum = 0
for j in range(nProteins - 1):
sum += matrix[i][j]
#totalScores[i] = sum
if sum > maxSum:
maxSum = sum
ancestorIndex = i
#print ancestorIndex
#building the string representig tree
tree = Tree()
tree.create_node("*", "root") #root node
tree.create_node(proteinList[0], parent = "root") #first leaf node
tree.show(key=lambda x: x.tag, reverse=True, line_type='ascii-em')
for i in range(1, 2): #for i in range(1, nProteins - 1):
addLeaf(i, tree, matrix)
tree.show(key=lambda x: x.tag, reverse=True, line_type='ascii-em')
print "Hello!"