-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcladeweb.py
executable file
·277 lines (227 loc) · 7.66 KB
/
cladeweb.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
#!/usr/bin/env python
"""Web browser interface for CladeCompare.
Input (GET):
Form for submitting FG, [BG, HMM].
Output (POST):
CladeReport (heat map) of submission.
"""
# TODO:
# - take boolean do_weight as a form option (checkbox?)
# ENH (in the report):
# - asterisks up top link to PDF pairlogos
# - below title, link to PyMOL script of PDB(s)
from __future__ import print_function
import logging
import os
import tempfile
import webbrowser
import bottle
from cladecomparelib import (core, pairlogo, pmlscript, urn, gtest, jsd,
phospho, report)
# ENH: include textarea as alternative to each file upload input
# ENH: dropdown alpha (or textinput w/ JS validation)
FORM_HTML = """\
<html>
<body>
<h1>CladeCompare</h1>
<form action="cladecompare" method="post" enctype="multipart/form-data">
<p>
Submission name:
<input type="text" name="name" />
</p>
<h2>Sequences</h2>
<p>
Sequence file 1 (required):
<br />
<input type="file" name="seqfile1" size=50 />
</p>
<p>
Sequence file 2:
<br />
<input type="file" name="seqfile2" size=50 />
</p>
<h2>Statistical strategy</h2>
<p>
<label>
<input type="radio" name="strategy" value="gtest" checked="checked" />
G-test (goodness-of-fit)
</label>
</p>
<p>
<label>
<input type="radio" name="strategy" value="urn" />
Ball-in-urn model (binomial)
</label>
</p>
<p>
<label>
<input type="radio" name="strategy" value="jsd" />
Jensen-Shannon divergence
</label>
</p>
<p>
<label>
<input type="radio" name="strategy" value="phospho" />
Phosphorylation site conservation
</label>
</p>
<p>
Significance cutoff (alpha):
<input type="text" name="alpha" value="0.005" />
</p>
<h2>Alignment profile</h2>
<p>
HMM (.hmm) profile:
<br />
<input type="file" name="profile" size=50 />
</p>
<!--
<h2>Structure</h2>
<p>
PDB ID:
<input type="text" name="pdbid" />
<br />
or upload a
PDB file:
<br />
<input type="file" name="pdbfile" size=50 />
</p>
-->
<p />
<p><input type="submit" /></p>
</form>
<hr />
<p>Project page: <a
href="http://github.com/etal/cladecompare">http://github.com/etal/cladecompare</a></p>
<p>If you use this software in a publication, please cite our paper that
describes it:</p>
<blockquote>Talevich, E. & Kannan, N. (2013)
<a href="http://www.biomedcentral.com/1471-2148/13/117">Structural and
evolutionary adaptation of rhoptry kinases and pseudokinases, a family of
coccidian virulence factors</a>.
<i>BMC Evolutionary Biology</i> 13:117 doi:10.1186/1471-2148-13-117
</blockquote>
</body>
</html>
"""
# --- Routes ---
@bottle.get('/cladecompare')
def form():
return FORM_HTML
# TODO - routes for downloading .pml, .pdf -- use static_file
@bottle.post('/cladecompare')
def form_submit():
# ENH: pick a unique, informative name -- e.g. date or hostname
name = bottle.request.forms.name
seqfile1 = bottle.request.files.seqfile1
if not hasattr(seqfile1, 'file'):
return "Error: You need to specify at least one sequence file."
seq1fname = handle2temp(seqfile1.file,
suffix=('.cma' if seqfile1.filename.endswith('.cma')
else '.seq'))
# Optional second sequence set -- if missing, do single mode
seqfile2 = bottle.request.files.seqfile2
if hasattr(seqfile2, 'file'):
seq2fname = handle2temp(seqfile2.file,
suffix=('.cma' if
seqfile2.filename.endswith('.cma') else
'.seq'))
if not name:
name = "%s-vs-%s" % (seqfile1.filename.rsplit('.', 1)[0],
seqfile2.filename.rsplit('.', 1)[0])
else:
seq2fname = ''
if not name:
name = seqfile1.filename
# Optional HMM profile for alignment
profile = bottle.request.files.profile
# Optional HMM profile for alignment
profile = bottle.request.files.profile
if hasattr(profile, 'file'):
if not profile.filename.endswith('.hmm'):
return "HMM profile file name must end in .hmm"
profname = handle2temp(profile.file, suffix='.hmm')
logging.info("Aligning %s with profile %s", seq1fname, profname)
fg_aln = core.hmm_align_and_read(profname, seq1fname)
if seq2fname:
logging.info("Aligning %s with profile %s", seq2fname, profname)
bg_aln = core.hmm_align_and_read(profname, seq2fname)
else:
profname = ''
fg_aln = core.read_aln(seq1fname, 'fasta')
if seq2fname:
bg_aln = core.read_aln(seq2fname, 'fasta')
pdbfile = bottle.request.files.pdbfile
if hasattr(pdbfile, 'file'):
if not profname:
return ("Error: to generate a PyMOL script for a PDB file you must"
"also specify an HMM profile")
pdbfname = handle2temp(pdbfile.file)
logging.info("Aligning %s with profile %s", pdbfile.filename, profname)
pdb_rec, pdb_resnums, pdb_inserts = core.pdb_hmm(profname,
pdbfname)
pdb_data = [(pdbfname, pdb_rec, pdb_resnums, pdb_inserts)]
else:
pdbfname = ''
pdb_data = None
# Mutually exclusive with pdbfname (above):
pdbid = bottle.request.forms.pdbid
if pdbid:
# If PDB ID: .pml should use "fetch" instead of "load"?
# Can get this info w/o dl'ing actual PDB file (e.g. via FASTA)?
pass
stat_module = dict(gtest=gtest, urn=urn, jsd=jsd, phospho=phospho,
)[bottle.request.forms.strategy]
try:
alpha = float(bottle.request.forms.alpha)
if not 0.0 <= alpha <= 1.0:
raise ValueError
except ValueError:
return "Error: alpha must be a number between 0 and 1"
_fdo, tmp_output = tempfile.mkstemp(suffix='.out')
os.close(_fdo)
_fdp, tmp_pattern = tempfile.mkstemp(suffix='.pttrn')
os.close(_fdp)
# Run the algorithms...
if seq2fname:
# Pair mode
fg_clean, bg_clean, hits = core.process_pair(fg_aln, bg_aln,
stat_module, False)
core.process_output(fg_clean, bg_clean, hits, alpha,
tmp_output, tmp_pattern,
pdb_data)
else:
# Single mode
aln, hits = core.process_one(fg_aln, stat_module, False)
core.process_output(aln, None, hits, alpha,
tmp_output, tmp_pattern,
pdb_data)
# Get the HTML report data
contents = report.do_single(tmp_output, tmp_pattern)[1]
cleanup(seq1fname)
cleanup(seq2fname)
cleanup(profname)
cleanup(tmp_output)
cleanup(tmp_pattern)
return report.html_page_tpl % dict(title=name, contents=contents)
# --- Helpers ---
def handle2temp(handle, suffix=''):
"""Write file handle contents to a temporary file, return tempfile name."""
_fd, fname = tempfile.mkstemp(suffix=suffix)
os.write(_fd, handle.read())
os.close(_fd)
return fname
def cleanup(fname):
"""Remove a temporary file that may or may not exist."""
if os.path.isfile(fname):
try:
os.remove(fname)
print("Cleaned up", fname)
except OSError:
print("Failed to clean up", fname)
# --- Run ---
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format="%(module)s [@%(lineno)s]: %(message)s")
webbrowser.open("http://localhost:8080/cladecompare")
bottle.run(host='localhost', port=8080)