-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebapp.py
executable file
·124 lines (100 loc) · 4.48 KB
/
webapp.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
#!/usr/bin/python
# Import modules for CGI handling
import cgi
import os, json
import crispr4p.crispr4p as crp
class viewForm(object):
def __call__(self):
text = self.print_html_header()
text += self.load_bahler_template()
return text
def print_html_header(self):
return "Content-type: text/html\n\n"
def load_bahler_template(self):
src_path = os.path.dirname(__file__)
src_path = "." if src_path == "" else src_path
try:
with open(src_path + '/template/bahler_template.html') as fh:
template_file = fh.read()
return template_file
except IOError as err:
print err
class PrimerDesignModel(object):
def __init__(self, name=None, cr=None, start=None, end=None):
self.name = name
self.cr = cr
self.start = start
self.end = end
self.primercheck = None
def run(self):
datapath = "data/"
FASTA = datapath + 'Schizosaccharomyces_pombe.ASM294v2.26.dna.toplevel.fa'
COORDINATES = datapath + 'COORDINATES.txt'
SYNONIMS = datapath + 'SYNONIMS.txt'
pd = crp.PrimerDesign(FASTA, COORDINATES, SYNONIMS, precomputed_folder='precomputed')
try:
self.tablePos_grna, self.hr_dna, self.primercheck, self.name, self.cr, self.start, self.end = pd.runWeb(self.name, self.cr, self.start, self.end, nMismatch=0)
except AssertionError as err:
print "Error: ", err
def result_html(self):
pm = self.primercheck[0] if self.primercheck else {}
result_dict = {'name': self.name,
'chromosome': self.cr,
'start': self.start,
'end': self.end,
'hrfw': self.hr_dna[0],
'hrrv': self.hr_dna[1],
'deleted_dna': self.hr_dna[2],
'primer_left': pm.get('PRIMER_LEFT_0_SEQUENCE', '-'),
'left_tm': "%d °C" % int(round(pm.get('PRIMER_LEFT_0_TM', '0'))),
'primer_right': pm.get('PRIMER_RIGHT_0_SEQUENCE', '-'),
'right_tm': "%d °C" % int(round(pm.get('PRIMER_RIGHT_0_TM', '0'))),
'deleted_dna_size': str(pm.get('PRIMER_PAIR_0_PRODUCT_SIZE', '-')) + " (bp)",
'negative_result_size': str(pm.get('negative_result', '-')) + " (bp)"}
result_dict['json_table'] = json.dumps(self.tablePos_grna)
src_path = os.path.dirname(__file__) if os.path.dirname(__file__) else '.'
with open(src_path + '/template/container_table.html') as fh:
template_file = fh.read()
return template_file % (result_dict)
class controller(object):
def __init__(self):
self.form = cgi.FieldStorage()
def check_form_action(self):
coordinate_form_names = ["coor_upper", "coor_lower", "chromosome"]
get_form_val = lambda x: str(self.form.getvalue(x))
if self.form.has_key("action"):
if self.form.has_key("name"):
return [get_form_val("name"), None, None, None]
elif all(j==True for j in [self.form.has_key(i) for i in coordinate_form_names]):
return [None, get_form_val("chromosome"),
get_form_val("coor_lower"),
get_form_val("coor_upper")]
else:
print '<font color="red"> Error: neither inputs of name mode nor\
coordinate mode is complete</font>' # for web user
raise ValueError("neither inputs of name mode nor coordinate\
mode is complete.")
else: return None
def is_render(self):
if self.form.has_key("render"): return True
else: return False
def run_model(self):
if not self.is_render():
model_arguments = self.check_form_action()
if model_arguments != None:
self.model = PrimerDesignModel(*model_arguments)
try:
self.model.run()
ans = self.model.result_html()
except:
ans = '<font color="red"><h2>ERROR: please contact to: <a href="mailto:[email protected]">[email protected]</a></h2></font>'
finally:
return ans
return ''
def webrun():
init_form = viewForm()
temp = init_form()
model = controller()
ans = model.run_model()
print temp % ans
webrun()