-
Notifications
You must be signed in to change notification settings - Fork 9
/
pyenzyme_server.py
405 lines (307 loc) · 11.9 KB
/
pyenzyme_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
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
import os
from fastapi import FastAPI, UploadFile, File, Request, Body
from starlette.responses import FileResponse, JSONResponse, HTMLResponse
from starlette.background import BackgroundTasks
from fastapi.templating import Jinja2Templates
from pyenzyme.enzymeml.core.enzymemldocument import EnzymeMLDocument
from pyenzyme.enzymeml.core.measurement import Measurement
from pyenzyme.enzymeml.tools.validator import EnzymeMLValidator
from pyenzyme.utils.rest_examples import create_full_example
from pyenzyme.enzymeml.core.exceptions import (
MeasurementDataSpeciesIdentifierError,
ECNumberError,
ChEBIIdentifierError,
DataError,
SpeciesNotFoundError,
UniProtIdentifierError,
ParticipantIdentifierError,
)
# * Settings
app = FastAPI(title="PyEnzyme REST-API", version="1.2", docs_url="/")
templates = Jinja2Templates(directory="static")
# * Functions
def remove_file(path: str) -> None:
os.unlink(path)
# ! Basic operations
@app.post(
"/create",
summary="Creates an EnzymeML document based on a JSON body",
description="Please note, in the schema 'aditionalPropsX' represent the individual IDs of the entities. However, these will not be included, since PyEnzyme takes car for that. Thus, feel free to use any string you like. Returns a binary file, which is an OMEX archive",
tags=["Basic operations"],
)
async def create_EnzymeML(
background_tasks: BackgroundTasks,
enzmldoc: EnzymeMLDocument = Body(..., example=create_full_example()),
):
# Recreate the EnzymeML document
nu_enzmldoc = EnzymeMLDocument.fromJSON(enzmldoc.json())
# Write the new EnzymeML file
dirpath = "."
file_name = f"{nu_enzmldoc.name.replace(' ', '_')}.omex"
file_path = os.path.join(dirpath, file_name)
nu_enzmldoc.toFile(dirpath, name=nu_enzmldoc.name)
try:
response = FileResponse(file_path, filename=file_name)
return response
except Exception as e:
raise
finally:
background_tasks.add_task(remove_file, path=file_name)
def parse_measurement_data(measurement, key, nu_measurement, enzmldoc):
for measurement_data in measurement.species_dict[key].values():
nu_measurement.addData(
init_conc=measurement_data.init_conc,
unit=measurement_data.unit,
protein_id=measurement_data.protein_id,
reactant_id=measurement_data.reactant_id,
)
nu_measurement.addReplicates(measurement_data.replicates, enzmldoc=enzmldoc)
@app.post(
"/upload/dataverse",
summary="Uploads an OMEX archive to a dataverse installation.",
description="Use this endpoint as form-data and specify the document to be uploaded via the key 'omex_archive' for a succesfull upload.",
tags=["Databases"],
)
async def upload_to_dataverse(
background_tasks: BackgroundTasks,
dataverse_name: str,
api_token: str,
base_url: str,
omex_archive: UploadFile = File(...),
):
# Write to file
file_name = omex_archive.filename
content = await omex_archive.read()
with open(file_name, "wb") as file_handle:
file_handle.write(content)
# Read EnzymeML document
try:
enzmldoc = EnzymeMLDocument.fromFile(file_name)
enzmldoc.uploadToDataverse(
dataverse_name=dataverse_name, base_url=base_url, api_token=api_token
)
except Exception as e:
return f"{e.__class__.__name__}: {str(e)}"
finally:
background_tasks.add_task(remove_file, path=file_name)
@app.post(
"/read",
summary="Reads an EnzymeML document served in an OMEX archive to a JSON representation",
description="Use this endpoint as form-data and specify the document to be uploaded via the key 'omex_archive' for a succesfull request.",
tags=["Basic operations"],
)
async def read_enzymeml(
background_tasks: BackgroundTasks, omex_archive: UploadFile = File(...)
):
# Write to file
file_name = omex_archive.filename
content = await omex_archive.read()
with open(file_name, "wb") as file_handle:
file_handle.write(content)
# Read EnzymeML document
try:
enzmldoc = EnzymeMLDocument.fromFile(file_name)
return enzmldoc.dict(
exclude_none=True,
exclude={
"log": ...,
"unit_dict": ...,
"file_dict": ...,
"protein_dict": {"Protein": {"__all__": {"_unit_id"}}},
},
by_alias=True,
)
except Exception as e:
return f"{e.__class__.__name__}: {str(e)}"
finally:
background_tasks.add_task(remove_file, path=file_name)
# ! Modifications
@app.post(
"/add_measurement",
summary="Adds a measurement to an existing EnzymeML document",
description="By using this endpoint, you can add successive raw data without having to create a new EnzymeML dcoument. Returns a binary file, which is an OMEX Archive",
tags=["Modifications"],
)
async def add_Measurement(
background_tasks: BackgroundTasks,
measurement: Measurement,
omex_archive: UploadFile = File(...),
):
# Check if the species_dict keys are correct
for key in measurement.species_dict:
if key not in ["reactants", "proteins"]:
return {
"error": f"Key '{key}' is not valid for species dict. Please use either 'reactants' or 'proteins'"
}
# Write to file
file_name = omex_archive.filename
content = await omex_archive.read()
with open(file_name, "wb") as file_handle:
file_handle.write(content)
# Read EnzymeML document
enzmldoc = EnzymeMLDocument.fromFile(file_name)
# Add the measurement
enzmldoc.addMeasurement(measurement)
# Write the new EnzymeML file
try:
dirpath = "."
file_name = f"{enzmldoc.name.replace(' ', '_')}.omex"
file_path = os.path.join(dirpath, file_name)
enzmldoc.toFile(dirpath)
return FileResponse(file_path, filename=file_name)
except Exception as e:
return str(e)
finally:
background_tasks.add_task(remove_file, path=file_name)
# ! TEMPLATE
@app.get(
"/template/download",
summary="Download the EnzymeML spreadsheet template.",
tags=["EnzymeML spreadsheet"],
)
async def get_enzymeml_template():
return FileResponse(
"./templates/EnzymeML_Template.xlsm", filename="EnzymeML_Template.xlsm"
)
@app.get(
"/template/upload",
summary="HTML website to upload the EnzymeML spreadsheet template and convert it to EnzymeML.",
tags=["EnzymeML spreadsheet"],
response_class=HTMLResponse,
)
def upload_enzymeml_template(request: Request):
return templates.TemplateResponse("template_upload.html", {"request": request})
@app.post(
"/template/convert",
summary="Converts an EnzymeML spreadsheet template to an EnzymeML document.",
tags=["EnzymeML spreadsheet"],
)
async def convert_template(
background_tasks: BackgroundTasks, enzymeml_template: UploadFile = File(...)
):
# Write to file
file_name = enzymeml_template.filename
content = await enzymeml_template.read()
with open(file_name, "wb") as file_handle:
file_handle.write(content)
# Generate the new EnzymeML file
try:
enzmldoc = EnzymeMLDocument.fromTemplate(file_name)
except Exception as e:
return str(e)
finally:
background_tasks.add_task(remove_file, path=file_name)
# Write the new EnzymeML file
try:
dirpath = "."
enzml_name = f"{enzmldoc.name.replace(' ', '_')}.omex"
file_path = os.path.join(dirpath, enzml_name)
enzmldoc.toFile(dirpath)
return FileResponse(file_path, filename=enzml_name)
except Exception as e:
return str(e)
finally:
background_tasks.add_task(remove_file, path=enzml_name)
# ! Validation
@app.get(
"/validation/download",
summary="Download the EnzymeML Validation template.",
tags=["EnzymeML Validation"],
)
async def get_validation_template():
return FileResponse(
"templates/EnzymeML_Validation_Template.xlsx",
filename="EnzymeML_Validation_Template.xlsm",
)
@app.get(
"/validation/upload",
summary="HTML site to upload the EnzymeML Validation template and convert it to a YAML file.",
tags=["EnzymeML Validation"],
response_class=HTMLResponse,
)
def upload_validation_template(request: Request):
return templates.TemplateResponse("validation_upload.html", {"request": request})
@app.post(
"/validation/convert",
summary="Convert the EnzymeML Validation template to a YAML template to use for validation on server site.",
tags=["EnzymeML Validation"],
)
async def convert_validation_template(
background_tasks: BackgroundTasks, validation_template: UploadFile = File(...)
):
# Write to file
file_name = validation_template.filename
content = await validation_template.read()
with open(file_name, "wb") as file_handle:
file_handle.write(content)
# Generate the new EnzymeML file
try:
EnzymeMLValidator.convertSheetToYAML(
path=file_name,
filename="validation",
)
return FileResponse(
"validation.yaml", filename="EnzymeML_Validation_Template.yaml"
)
except Exception as e:
return str(e)
finally:
background_tasks.add_task(remove_file, path=file_name)
background_tasks.add_task(remove_file, path="validation.yaml")
@app.post(
"/validation/",
summary="Vaidates an EnzymeML document based on a given EnzymeML Validation YAML file. Returns a boolean and a report, if validation has failed.",
tags=["EnzymeML Validation"],
)
async def validate_enzymeml(
background_tasks: BackgroundTasks,
omex_archive: UploadFile = File(...),
validation_template: UploadFile = File(...),
):
# Write EnzymeML to file
omex_name = omex_archive.filename
content = await omex_archive.read()
with open(omex_name, "wb") as file_handle:
file_handle.write(content)
# Read EnzymeML document
try:
enzmldoc = EnzymeMLDocument.fromFile(omex_name)
except Exception as e:
return f"{e.__class__.__name__}: {str(e)}"
finally:
background_tasks.add_task(remove_file, path=omex_name)
# Write YAML to file
valid_name = validation_template.filename
content = await validation_template.read()
with open(valid_name, "wb") as file_handle:
file_handle.write(content)
# Read YAML and validate accordingly
try:
report, is_valid = enzmldoc.validateDocument(yaml_path=valid_name)
return {"is_valid": is_valid, "report": report}
finally:
background_tasks.add_task(remove_file, path=valid_name)
# * Exception handlers
@app.exception_handler(MeasurementDataSpeciesIdentifierError)
async def handle_meas_species_id_error(
req: Request, exc: MeasurementDataSpeciesIdentifierError
):
return JSONResponse(status_code=406, content={"message": str(exc)})
@app.exception_handler(ECNumberError)
async def handle_ecnumber_error(req: Request, exc: ECNumberError):
return JSONResponse(status_code=406, content={"message": str(exc)})
@app.exception_handler(ChEBIIdentifierError)
async def handle_chebi_error(req: Request, exc: ChEBIIdentifierError):
return JSONResponse(status_code=406, content={"message": str(exc)})
@app.exception_handler(DataError)
async def handle_data_error(req: Request, exc: DataError):
return JSONResponse(status_code=406, content={"message": str(exc)})
@app.exception_handler(SpeciesNotFoundError)
async def handle_species_error(req: Request, exc: SpeciesNotFoundError):
return JSONResponse(status_code=406, content={"message": str(exc)})
@app.exception_handler(UniProtIdentifierError)
async def handle_uniprotid_error(req: Request, exc: UniProtIdentifierError):
return JSONResponse(status_code=406, content={"message": str(exc)})
@app.exception_handler(ParticipantIdentifierError)
async def handle_participant_error(req: Request, exc: ParticipantIdentifierError):
return JSONResponse(status_code=406, content={"message": str(exc)})