-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreetest.py
More file actions
100 lines (80 loc) · 1.81 KB
/
treetest.py
File metadata and controls
100 lines (80 loc) · 1.81 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class Sugar:
def __init__(self, name):
self.name = name
self.bonds = [None, None, None, None, None, None]
self.adj = [None, None, None, None, None, None]
def id_string(self):
bondCount = 0
bondType = []
for i in xrange(len(self.bonds)):
if self.bonds[i]:
bondCount += 1
bondType.append(self.bonds[i] + str(i+1))
string = str(bondCount) + self.name
for bond in bondType:
string += bond
string += '-'
return string
def __str__(self):
return self.name
class Glycome:
def __init__(self):
self.root = None
self.id = ""
def add_root(self, sugar):
self.root = sugar
def add_bond(self, parent, child, pos, bond):
parent.adj[pos] = child
parent.bonds[pos] = bond
def gen_id(self):
self.gen_id_helper(self.root)
return self.id
def gen_id_helper(self, node):
self.id += node.id_string()
hasChild = False
for i in xrange(len(node.adj)):
if node.adj[i]:
hasChild = True
self.gen_id_helper(node.adj[i])
if not hasChild:
self.id += '/'
def __str__(self):
full = self.root.name
que = []
que.append(self.root)
while len(que) > 0:
node = que.pop(0)
end = True
for i in xrange(6):
if node.adj[i]:
full += "-" + node.bonds[i] + str(i) + "-" + node.adj[i].name
que.append(node.adj[i])
end = False
if end:
full += "/"
return full
def main():
lst = []
a = Sugar('one')
b = Sugar('two')
c = Sugar('three')
d = Sugar('four')
e = Sugar('five')
f = Sugar('six')
lst.append(a)
lst.append(b)
lst.append(c)
lst.append(d)
lst.append(e)
lst.append(f)
tree = Glycome()
tree.add_root(a)
tree.add_bond(lst[0], lst[1], 0, 'a')
tree.add_bond(lst[0], lst[2], 4, 'b')
tree.add_bond(lst[1], lst[3], 2, 'a')
tree.add_bond(lst[1], lst[4], 5, 'b')
tree.add_bond(lst[3], lst[5], 1, 'a')
print tree
print
print tree.gen_id()
main()