Skip to content

Commit

Permalink
Added Dockerfile and endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Dec 2, 2021
1 parent eb6edc6 commit 399e3cf
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.9-slim
WORKDIR /app

COPY . /app

RUN apt-get update \
&& apt-get install -y --no-install-recommends git \
&& apt-get install -y --no-install-recommends gcc \
&& apt-get install -y --no-install-recommends cmake \
&& apt-get purge -y --auto-remove


RUN pip3 install . --no-cache-dir

COPY pyenzyme_server.py /app

ENTRYPOINT ["uvicorn"]
CMD ["pyenzyme_server.py"]
Binary file added TEST.omex
Binary file not shown.
Binary file added examples/CreateEnzymeML/Hello.omex
Binary file not shown.
2 changes: 1 addition & 1 deletion pyenzyme/enzymeml/tools/enzymemlwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def _addReactions(self, model: libsbml.Model, reaction_dict: dict[str, EnzymeRea
conditionsMapping = {
'enzymeml:temperature': {
'value': 'temperature',
'unit': 'temperature_unit_id'
'unit': '_temperature_unit_id'
},
'enzymeml:ph': {
'value': 'ph'
Expand Down
76 changes: 72 additions & 4 deletions pyenzyme_server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import os
import shutil

from fastapi import FastAPI
from fastapi import FastAPI, UploadFile, File
from starlette.responses import FileResponse
from starlette.background import BackgroundTasks

from pyenzyme.enzymeml.core.enzymemldocument import EnzymeMLDocument
from pyenzyme.enzymeml.core.measurement import Measurement

app = FastAPI()


def remove_file(path: str) -> None:
os.unlink(path)


@app.post("/create")
def create_EnzymeML(enzmldoc: EnzymeMLDocument):
async def create_EnzymeML(enzmldoc: EnzymeMLDocument, background_tasks: BackgroundTasks):

def _convertUnits(dictionary: dict, key: str):
"""Converts all units to unit_ids and stores them in the model"""
Expand Down Expand Up @@ -77,4 +82,67 @@ def _convertUnits(dictionary: dict, key: str):
return str(e)

finally:
shutil.rmt
background_tasks.add_task(remove_file, path=file_name)


@app.post("/read")
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)
except Exception as e:
return str(e)
finally:
background_tasks.add_task(remove_file, path=file_name)


@app.post("/add_measurement")
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)
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
'numpy',
'pandas',
'python-libcombine',
'python-copasi',
'openpyxl',
'marshmallow',
'scipy',
Expand All @@ -40,7 +39,9 @@
'pydataverse',
'pydantic',
'deprecation',
'deepdiff'
'deepdiff',
'python-multipart',
'uvicorn'
],
extras_require={
'test': [
Expand Down

0 comments on commit 399e3cf

Please sign in to comment.