-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
220 lines (190 loc) · 6.57 KB
/
views.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
from flask import abort, jsonify, render_template, request, redirect, url_for, send_file, send_from_directory
from app import app
import os
import random
import uuid
import string
import json
import requests
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem.Descriptors import ExactMolWt
from rdkit.Chem.Draw import MolToFile
from rdkit.DataStructs import FingerprintSimilarity
from rdkit.Chem.Fingerprints.FingerprintMols import FingerprintMol
from rdkit.Chem.rdMolDescriptors import CalcMolFormula
from decorators import rdkit_handle_error
from Molecule import Molecule, molecular_factory_dict
from adducts import ADDUCT_SET, get_adduct_mass
# This is the molecule factory that will take in a request and try to figure out what molecule it is
def molecular_factory(request) -> Molecule:
"""Given a flask request, create a molecule"""
return molecular_factory_dict(request.values)
@app.route("/")
def homepage():
return redirect('/dashinterface')
@app.route("/contributors")
def contributors():
return render_template("contributors.html")
# simple test run
# input: NADA
# output: NADA
@app.route("/heartbeat")
def heartbeat():
return "{}"
# Unified output in JSON format
# input: smiles, inchi, or inchikey
# output: json
@app.route("/convert", methods=['GET', 'POST'])
@rdkit_handle_error
def convert():
m = molecular_factory(request)
if not m.mol:
return {"message":"unable to import structure"}, 400
return jsonify(m.export_structure())
# get inchikey
# input: smiles, inchi, or inchikey
# output: inchikey
@app.route("/inchikey", methods=['GET', 'POST'])
@rdkit_handle_error
def inchikey():
m = molecular_factory(request)
if m.mol:
return str(m.inchikey)
else:
return {"message":"unable to import structure"}, 400
# get classyfire using either smiles or inchi
# input: smiles / inchi
# output: classyfire
@app.route("/classyfire", methods=['GET', 'POST'])
@rdkit_handle_error
def classyfire():
m = molecular_factory(request)
if m.mol:
r = requests.get("https://classyfire.gnps2.org/entities/{}.json".format(m.inchikey))
return r.text, r.status_code
else:
return {"message":"unable to import structure"}, 400
# get inchi using smiles
# input: smiles
# output: inchi
@app.route("/inchi", methods=['GET', 'POST'])
@rdkit_handle_error
def inchi():
m = molecular_factory(request)
if m.mol:
return str(m.inchi)
else:
return {"message":"unable to import structure"}, 400
# get smiles
# input: smiles, inchi, or inchikey
# output: smiles
@app.route("/smiles", methods=['GET', 'POST'])
@rdkit_handle_error
def smiles():
m = molecular_factory(request)
if m:
return str(m.smiles)
else:
return {"message":"unable to import structure"}, 400
# get molblock
# input: smiles, inchi, or inchikey
# output: molblock
@app.route("/mol", methods=['GET', 'POST'])
@rdkit_handle_error
def mol():
m = molecular_factory(request)
if m.mol:
return str(m.molblock)
else:
return {"message":"unable to import structure"}, 400
# get exact mass
# input: smiles, inchi, or inchikey
# output : mass (float) as string
@app.route("/structuremass", methods=['GET', 'POST'])
@rdkit_handle_error
def structuremass():
m = molecular_factory(request)
if m:
return str(m.exact_mass)
else:
return {"message":"unable to import structure"}, 400
# input: inchi / smiles
# output : formula
@app.route("/formula", methods=['GET', 'POST'])
@rdkit_handle_error
def formula():
m = molecular_factory(request)
if m:
return str(m.formula)
else:
return {"message":"unable to import structure"}, 400
# input: inchi / smiles
# output : adduct prediction
@app.route("/adductcalc", methods=['GET', 'POST'])
@rdkit_handle_error
def calculate_adduct():
m = molecular_factory(request)
if m:
exact_mass = m.exact_mass
mz = float(request.values.get('mz', 0))
all_calculations = []
for adduct in ADDUCT_SET:
calculated_mz, charge = get_adduct_mass(exact_mass, adduct)
result_dict = {}
result_dict["adduct"] = adduct
result_dict["mz"] = calculated_mz
result_dict["charge"] = charge
result_dict["delta"] = mz - calculated_mz
all_calculations.append(result_dict)
return json.dumps(all_calculations)
else:
return {"message":"unable to import structure"}, 400
# draw the image of structure
# input: smiles, inchi, or inchikey, width, height, imgType
@app.route("/structureimg", methods=['GET', 'POST'])
@rdkit_handle_error
def structureimg():
#Parsing out size
width = int(request.args.get('width') or 350)
height = int(request.args.get('width') or 350)
uuid_key = str(uuid.uuid4())
imgType = request.values.get("imgType", "png")
m = molecular_factory(request)
if not m.mol:
return send_from_directory("img", "GNPS2_logo.png", mimetype='image/png'), 400
if imgType == "png":
output_filename = os.path.join("structure_images", uuid_key + ".png")
m.save_image(output_filename, height=height, width=width, imageType="png")
return send_from_directory("structure_images", uuid_key + ".png", mimetype='image/png')
elif imgType == "svg":
output_filename = os.path.join("structure_images", uuid_key + ".svg")
m.save_image(output_filename, height=height, width=width, imageType="svg")
return send_from_directory("structure_images", uuid_key + ".svg", mimetype='image/svg+xml')
else:
return send_from_directory("img", "GNPS2_logo.png", mimetype='image/png'), 400
# Calculates the structural similarity
@app.route("/structuresimilarity", methods=['GET', 'POST'])
@rdkit_handle_error
def structuresimilarity():
smiles1 = request.values.get("smiles1", None)
inchi1 = request.values.get("inchi1", None)
smiles2 = request.values.get("smiles2", None)
inchi2 = request.values.get("inchi2", None)
mol1 = Molecule(smiles=smiles1, inchi=inchi1)
if not mol1.mol:
return {"message":"unable to import structure 1."}, 400
mol2 = Molecule(smiles=smiles2, inchi=inchi2)
if not mol2.mol:
return {"message":"unable to import structure 2."},400
return str(mol1.similarity(mol2))
# from inchi, smiles, get fingerprint
# circular/Morgan fingerprint with 512 bits
@app.route("/structurefingerprint", methods=['GET', 'POST'])
@rdkit_handle_error
def structurefingerprint():
m = molecular_factory(request)
if m.mol:
return str(m.fingerprint)
else:
return {"message":"unable to import structure"}, 400