forked from ViennaRNA/forna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforna_server.py
executable file
·185 lines (140 loc) · 6.11 KB
/
forna_server.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
#!/usr/bin/python
"""forna_server.py: A server for converting RNA secondary structures to force-directed graphs
and doing something more."""
__author__ = "Stefan Hammer"
__copyright__ = "Copyright 2014"
__version__ = "0.1"
__maintainer__ = "Stefan Hammer"
__email__ = "[email protected]"
from flask import Flask, request, abort
import forna
import json
import re
import sys
import os
import RNA
from optparse import OptionParser
import forgi.utilities.debug as fud
def create_app(static):
'''
Create the forna application given the options that were passed.
'''
app = Flask(__name__, static_folder='htdocs')
@app.errorhandler(400)
# pylint: disable=W0612
def custom405(error):
app.logger.info(error)
return error.description, 400
@app.route('/struct_graph', methods=['POST'])
# pylint: disable=W0612
def struct_graph():
app.logger.info(request.json);
if not request.json:
abort(400, "Missing a json in the request")
if 'seq' not in request.json and 'struct' not in request.json:
abort(400, "Missing seq and struct in the json file")
if re.match("^[ACGTUWSMKRYBDHV]+$", request.json['seq']) is None:
abort(400, "Invalid sequence: {}".format(request.json['seq']))
if re.match("^[\(\)\.\[\]\{\}]+[\*]?$", request.json['struct']) is None:
abort(400, "Invalid structure: {}".format(request.json['struct']))
if request.json['struct'][-1] == '*':
circular = True
structure = request.json['struct'].strip('*')
else:
circular = False
structure = request.json['struct']
fasta_text = ">{}\n{}\n{}".format(request.json['header'], request.json['seq'],
structure)
try:
result = forna.fasta_to_json(fasta_text, circular)
except Exception as ex:
app.logger.exception(ex)
abort(400, "Secondary structure parsing error: {}".format(str(ex)))
return json.dumps(result), 201
@app.route('/mfe_struct', methods=['POST'])
# pylint: disable=W0612
def mfe_struct():
app.logger.info(request.json);
if not request.json:
abort(400, "Request has no json.")
if 'seq' not in request.json:
abort(400, "Request has no sequence in the json.")
if re.match("^[ACGTUWSMKRYBDHV]+$", request.json['seq']) is None:
abort(400, "Invalid sequence: {}".format(request.json['seq']))
result = RNA.fold(str(request.json['seq']))[0]
return json.dumps(result), 201
@app.route('/colors_to_json', methods=['POST'])
def colors_to_json():
app.logger.info(request.json);
if not request.json:
abort(400, "Request has no json.")
if 'text' not in request.json:
abort(400, "Request has no text field.")
try:
color_json = forna.parse_colors_text(request.json['text'])
except Exception as ex:
app.logger.exception(ex)
abort(400, "Custom color error: {}".format(str(ex)))
return json.dumps(color_json)
@app.route('/pdb_to_graph', methods=['POST'])
def pdb_to_graph():
from werkzeug import secure_filename
name = secure_filename(request.files['pdb_file'].filename)
try:
result = forna.pdb_to_json(request.files['pdb_file'].read(), name)
except Exception as ex:
app.logger.exception(ex)
abort(400, "PDB file parsing error: {}".format(str(ex)))
return json.dumps(result), 201
if static:
print >> sys.stderr, " * Starting static"
# serving static files for developmental purpose
@app.route('/')
# pylint: disable=W0612
def index():
return app.send_static_file('index.html')
@app.route('/<file>')
# pylint: disable=W0612
def static_root(file):
return app.send_static_file(file)
@app.route('/js/<path:path>')
# pylint: disable=W0612
def static_js(path):
return app.send_static_file(os.path.join('js', path))
@app.route('/css/<path:path>')
# pylint: disable=W0612
def static_css(path):
return app.send_static_file(os.path.join('css', path))
@app.route('/fonts/<path:path>')
# pylint: disable=W0612
def static_fonts(path):
return app.send_static_file(os.path.join('fonts', path))
@app.route('/img/<path:path>')
# pylint: disable=W0612
def static_img(path):
return app.send_static_file(os.path.join('img', path))
# end serving static files
return app
def main():
usage = """
python forna_server.py"""
num_args = 0
parser = OptionParser(usage=usage)
#parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str')
parser.add_option('-p', '--port', dest='port', default=8008, help="Listen on this port", type='int')
parser.add_option('-d', '--debug', dest='debug', default=False, help="Run in debug mode", action='store_true')
parser.add_option('-o', '--host', dest='host', default='127.0.0.1', help='The host address', type='str')
parser.add_option('-s', '--static', dest='static', default=False, action='store_true', help='Also serve static files.')
parser.add_option('-l', '--log-file', dest='log_file', default='server.log', help='The file to store the logs to')
(options, args) = parser.parse_args()
if len(args) < num_args:
parser.print_help()
sys.exit(1)
import logging
logging.basicConfig(filename=options.log_file,level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(funcName)s:%(lineno)d]')
app = create_app(options.static)
app.run(host=options.host, debug=options.debug, port=options.port)
if __name__ == '__main__':
main()