-
Notifications
You must be signed in to change notification settings - Fork 0
/
dna_vis.py
145 lines (110 loc) · 3.41 KB
/
dna_vis.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
11-19-15
Uses Kivy to present an interactive visualization of the DNA chain structure.
"""
from kivy.app import runTouchApp
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.properties import ListProperty, NumericProperty
from kivy.lang import Builder
from dna_chain import DNANode
from dna import DNA
DNANode.__repr__ = lambda a: str(a.name)
class NodeVis(Label):
color = ListProperty([0, .6, .5])
padding = NumericProperty('7dp')
Builder.load_string("""
<NodeVis>:
canvas.before:
Color:
rgb: self.color
Rectangle:
pos: self.x + self.padding, self.y + self.padding
size: self.width - self.padding * 2, self.height - self.padding * 2
Color:
rgb: 0, 0, 0
Line:
rectangle: self.x + self.padding, self.y + self.padding, \
self.width - self.padding * 2, self.height - self.padding * 2
width: 2.5
""")
class DNAVis(Widget):
def __init__(self, **kw):
super(DNAVis, self).__init__(**kw)
self.dna = DNA()
self.crawler = self.dna.spawn_crawler()
self.bind(size=self.redraw, pos=self.redraw)
def redraw(self, *ar):
for w in self.children[:]:
if isinstance(w, NodeVis):
self.remove_widget(w)
node_lookup = {}
y = self.top - 100
crawler = self.dna.spawn_crawler()
crawler.reset()
indent = 100
for n, indent_no in crawler.crawl_indents():
indent += 100 * indent_no
nv = NodeVis(size=(100, 100), text=n.name)
node_lookup[n] = nv
nv.x = self.x + indent
nv.y = y
self.add_widget(nv)
y -= 100
crawler = self.crawler
cv = NodeVis(
size=(100, 100),
text='CRAWLER',
color=[.7, .05, 0])
if crawler.current_node is not None:
cv.x = node_lookup[crawler.current_node].x - 100
cv.y = node_lookup[crawler.current_node].y
else:
cv.y = self.top - 100
cv.x = self.width - cv.width
self.add_widget(cv)
def eval_input(self, text):
dna = self.dna
crawler = self.crawler
if not hasattr(self, 'exec_locals'):
self.exec_locals = locals()
try:
to_run = compile(text, '', 'exec')
exec(to_run, globals(), self.exec_locals)
except (NameError, SyntaxError, AttributeError, TypeError) as err:
print(err)
self.redraw()
Builder.load_string("""
#:import Clock kivy.clock.Clock
<DNAVis>:
canvas.before:
Color:
rgb: 1, .9, .8
Rectangle:
size: self.size
pos: self.pos
TextInput:
id: input
pos: root.pos
size: root.width, '35dp'
multiline: False
on_text_validate:
root.eval_input(self.text)
self.text = ''
Clock.schedule_once(lambda dt: setattr(self, 'focus', True), .1)
""")
if __name__ == '__main__':
from kivy.clock import Clock
dv = DNAVis()
def the_deeds(*ar):
n = DNANode()
n.name = 'NODE 1'
dv.dna.head = n
c = dv.dna.spawn_crawler()
n = DNANode()
n.name = 'NODE 2'
c.add_child(n, dv.dna.head)
dv.crawler.reset()
dv.redraw()
Clock.schedule_once(the_deeds, 1.0)
runTouchApp(dv)