-
Notifications
You must be signed in to change notification settings - Fork 1
/
UCFormatter.py
409 lines (385 loc) · 13.8 KB
/
UCFormatter.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
from dash import Dash, dcc, html, Input, Output, State, ctx
from dash.exceptions import PreventUpdate
import dash_bootstrap_components as dbc
import dash
import json
import requests
import redis
import subprocess
import atexit
from dash_helpers import *
# Making requests is OK because this is a public repo
UCFs_folder = 'https://raw.githubusercontent.com/CIDARLAB/Cello-UCF/develop/files/v2'
schema_link = 'https://raw.githubusercontent.com/CIDARLAB/Cello-UCF/develop/schemas/v2'
# Retrieves ucf-list
ucf_list = ['empty']
ucf_txt_path = 'UCFs/ucf-list.txt'
try:
with open(ucf_txt_path, 'r') as f:
file_contents = f.read()
lines = file_contents.split('\n')
lines = list(filter(lambda x: x != '', lines))
ucf_list = lines
except Exception as e:
debug_print(str(e))
ucf_list = ['failed to read ucf-list file']
# set up in-memory caching for variables w redis
r = redis.Redis(host='localhost', port=6379, db=0)
# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# external_stylesheets += [dbc.themes.CYBORG]
app = Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
className='dark-theme',
children=[
dcc.Store(id='open-schema', data=None),
html.H1(
children='CELLO-V3',
style={
'textAlign': 'center',
'color': '#0073e5',
'flex': 1,
'backgroundColor': '#afafaf',
'height': 'max-height',
}
),
html.H3(
'''UCFormatter Tool''',
style={'flex': 1, 'textAlign': 'center', },
),
html.Br(),
html.Div(
children=[
html.Div(style={'flex': 0.2, 'textAlign': 'center'}),
html.Div(
'''
The Cello software designs the DNA sequences for programmable circuits
based on a high-level software description and a library of characterized
DNA parts representing Boolean logic gates.
The user constraints file (UCF) is
a JavaScript Object Notation (JSON) file that describes
a part and gate library for a particular organism.
''',
style={'flex': 0.6, 'textAlign': 'center'},
),
html.Div(style={'flex': 0.2, 'textAlign': 'center'}),
],
style={
'display': 'flex',
'flex-direction': 'row',
}
),
html.Br(),
html.Div(
children=[
html.Label('Select UCF template'),
html.Div(
children=[
html.Div(
children=[
dcc.Dropdown(
ucf_list, ucf_list[0], id='ucf-select'),
html.Br(),
],
style={'flex': 1}
),
html.Button(
'1. select UCF',
id='confirm-select',
style={'padding-bottom': -50}
),
], style={
'display': 'flex',
'align-items': 'stretch',
'flex-direction': 'row',
'padding-left': '100px',
'padding-right': '100px',
}
),
],
style={
'textAlign': 'center',
}
),
html.Div(
[
html.H5('UCF preview: '),
html.Div(
dcc.RangeSlider(
id='ucf-range-slider',
min=0,
max=30,
step=1,
value=[0, 10],
pushable=10,
drag_value=[1],
marks=None,
tooltip={'placement': 'bottom'},
),
style={
'padding-left': '100px',
'padding-right': '100px'
}
),
html.Div(
children=generate_ucf_preview(),
id='ucf-preview',
style={
'padding-left': '100px',
'padding-right': '100px'
}
),
# daq.Indicator(
# id='indicator-light',
# value=True,
# color='red'
# ),
html.Br(),
html.Br(),
html.Button(
'2. confirm selection',
id='refresh-page',
),
html.Br(),
html.Br(),
html.Label("Choose a collection to modify"),
html.Div(
[
html.Div(style={'flex': 0.3}),
html.Br(),
dcc.Dropdown(
ucf_list,
ucf_list[0],
id='collection-select',
style={
'flex': 0.4,
}
),
html.Div(style={'flex': 0.3}),
],
style={
'display': 'flex',
'flex-direction': 'row',
'alignItems': 'center',
}
),
html.Br(),
],
style={
'text-align': 'center',
}
),
html.Div(
children=[
html.Div(
id='schema-preview',
style={
'flex':0.5,
'max-width': '50%',
}
),
html.Div(style={'flex': 0.01}),
html.Div(
id='schema-preview-2',
children=[
generate_schema_preview()
],
style={
'flex':0.5
}
),
],
style={
'display': 'flex',
'flex-direction': 'row',
'alignItems': 'center',
'padding-left': '100px',
'padding-right': '100px'
}
),
html.Br(),
html.Div(
id='schema-input-form',
children='placeholder',
style={
'padding-left': '100px',
'padding-right': '100px'
}
),
html.Br(),
html.Div(
children=[
html.H5('Modified Preview: '),
# dcc.RangeSlider(
# id='ucf-range-slider-2',
# min=0,
# max=30,
# step=1,
# value=[0, 10],
# pushable=10,
# drag_value=[1],
# marks=None,
# tooltip={'placement': 'bottom'},
# ),
html.Div(
children=generate_ucf_preview(),
id='ucf-preview-2',
),
html.Br(),
html.Button(
'SUBMIT',
id='submit-modification',
# style={'padding-bottom': -50}
),
],
style={
'padding-left': '100px',
'padding-right': '100px',
'text-align': 'center',
}
),
html.Br(),
],
style={
'display': 'flex',
'flex-direction': 'column',
},
)
# CALLBACKS SECTION
@app.callback(
Output('ucf-range-slider', 'disabled'),
Output('ucf-preview', 'children'),
Output('ucf-preview-2', 'children'),
Output('ucf-range-slider', 'value'),
Output('ucf-range-slider', 'max'),
Input('confirm-select', 'n_clicks'),
Input('ucf-range-slider', 'value'),
State('ucf-select', 'value'),
State('ucf-range-slider', 'disabled')
)
# NOTE: generate UCF preview and sets slider value
def preview_ucf(selectedUCF, slider_value, ucf_name, slider_disabled):
slider_value = slider_helper(slider_value)
ucf_path = UCFs_folder + '/'
ucf_parse = ucf_name.split('.')
ucf_path += ucf_parse[1].lower() + '/'
# TODO: method needs update when UCF list expands
ucf_pre = ucf_parse[0]
group_name = ''
if ucf_pre[0] == 'S':
group_name = ucf_pre[:2]
else:
group_name = ucf_pre[:3]
ucf_path += group_name
try:
with requests.get(f'{ucf_path}/{ucf_name}') as response:
if response.ok:
ucf_data = json.loads(response.content)
r.set('ucf', response.content.decode())
print('\'Click\'')
print(json.dumps(ucf_data[0], indent=4))
return False, generate_ucf_preview(ucf_data, slider_value), generate_ucf_preview(ucf_data), slider_value, len(ucf_data)
else:
print(ucf_path)
debug_print(f'failed to retrieve {ucf_name}, error code ' + str(response.status_code))
return True, generate_ucf_preview(), generate_ucf_preview(), slider_value, 30
except Exception as e:
debug_print(f'failed to retrieve {ucf_name}, exception: ')
debug_print(e)
return True, generate_ucf_preview(), generate_ucf_preview(), slider_value, 30
@app.callback(
Output('collection-select', 'options'),
Output('collection-select', 'value'),
Input('refresh-page', 'n_clicks'),
Input('confirm-select', 'n_clicks'),
Input('ucf-select', 'value'),
)
# NOTE: updates the collection dropdown items when confirm-selction is clicked
# TODO: is it absolutely necessary?
# DEFAULT: msg, causes 404 for schema retrieval
def update_collections_dropdown(refresh_clicks, confirm_clicks, ucf_name):
triggered_id = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
if triggered_id == 'confirm-select':
return ['please confirm selection again.'], ''
else:
if refresh_clicks is not None:
ucf = json.loads(r.get("ucf"))
collections = []
for c in ucf:
collections.append(c["collection"])
collections = list(set(collections))
# options = [{'label': c, 'value': c} for c in collections]
# return options
return collections, ' '
else:
return ['first, confirm selection'], ''
@app.callback(
Output('refresh-page', 'style'),
[Input('refresh-page', 'n_clicks'),
Input('confirm-select', 'n_clicks')],
[State('refresh-page', 'style')]
)
# NOTE: click on confirm-select btn to make it green, and turns red whenever ucf btn is clicked
# makes sure that the
def autobots_roll_out(refresh_clicks, confirm_clicks, color):
triggered_id = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
debug_print(triggered_id)
if triggered_id == 'confirm-select':
# {'background-color': '#d62d20'}
return {'background-color': '#fa3c4c'}
elif refresh_clicks is not None:
ucf = json.loads(r.get("ucf"))
collections = []
for c in ucf:
collections.append(c["collection"])
collections = list(set(collections))
display = []
for c in collections:
display.append(html.Li(c))
return {'background-color': '#7ddc1f'} # green
else:
return {'background-color': '#fa3c4c'} # red
@app.callback(
Output('schema-preview', 'children'),
Output('schema-input-form', 'children'),
Output('open-schema', 'data'),
Output('schema-preview-2','children'),
Input('collection-select', 'options'),
Input('collection-select', 'value'),
State('refresh-page', 'style'),
)
def preview_schema(c_options, c_name, confirm_btn_color):
debug_print('c_options\n' + str(c_options))
schema1 = generate_schema_preview()
inputForm = 'placeholder'
schemaData = None
schema2 = generate_schema_preview()
print(confirm_btn_color)
if c_name not in c_options or confirm_btn_color['background-color'] == '#fa3c4c':
return schema1, inputForm, schemaData, schema2
try:
with requests.get(schema_link+'/'+str(c_name)+'.schema.json') as response:
if response.ok:
# r.set(c_name, response.content.decode())
schemaData = json.loads(response.content)
print('\'Click\'')
print(json.dumps(schemaData, indent=4))
schema1 = generate_schema_preview(schemaData)
inputForm = generate_input_components(schemaData['properties'])
else:
debug_print('Could not complete response to retrieve schema')
debug_print(str(response.status_code))
except Exception as e:
debug_print('Exception with completing response')
debug_print(str(e))
try:
collection_list = find_collection_in_ucf(c_name, json.loads(r.get("ucf")))
schema2 = generate_schema_preview(collection_list)
except Exception as e:
debug_print('Exception finding collection name in UCF file')
debug_print(str(e))
return schema1, inputForm, schemaData, schema2
if __name__ == '__main__':
start_redis_server()
atexit.register(stop_redis_server)
app.run_server(debug=True)