Skip to content

Commit

Permalink
Handle missing ontology file and improve OWL to OBO error handling
Browse files Browse the repository at this point in the history
Add checks to handle the case where the ontology file is missing. Enhance error handling in the OWL to OBO conversion, ensuring appropriate notifications are sent when conversion fails.
  • Loading branch information
Zerskk committed Nov 19, 2024
1 parent a865064 commit ba6d135
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 18 deletions.
13 changes: 13 additions & 0 deletions app/helpers/obo_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ def get_ontology_name(self):
return ontology.name

def parse(self, notifications: Notifications):

if self.ontology_file is None:
self.obo_file.ontologies = self.ontolgies
self.obo_file.terms = self.obo_file.terms
self.obo_file.relationships = self.obo_file.relationships

notifications.messages.append(Message(type="fail", message="Ontology could not be read"))



return self.obo_file.dict()


# try to read ontology file
try:
graph = obonet.read_obo(self.ontology_file, ignore_obsolete=False)
Expand Down
173 changes: 155 additions & 18 deletions app/tasks/ontology_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
from resource import *

from app.tasks.database_tasks import add_ontologies
from app.helpers.notifications.models.notification_model import Notifications
from app.helpers.notifications.models.notification_model import Notifications, Message

from celery.backends.s3 import S3Backend
from celery.backends.base import BaseKeyValueStoreBackend
from app.helpers.storage_backend import StorageBackend

from app.helpers.models.ontology.obo_file import *


# def convert_owl_to_obo_in_memory(input_file: str):
# # Load the OWL file
Expand Down Expand Up @@ -69,20 +71,38 @@
#
# return graph

def convert_owl_to_obo(input_file: str) -> str:
# Load the OWL file
ontology = pronto.Ontology(input_file)
# Create a binary stream instead of a file
binary_stream = io.BytesIO()
# Save the ontology in OBO format
ontology.dump(binary_stream, format='obo')
# Get the OBO content from the binary stream
obo_content = binary_stream.getvalue().decode('utf-8')
# Close the stream
binary_stream.close()
return obo_content
# def convert_owl_to_obo(input_file: str) -> str:
# # Load the OWL file
# ontology = pronto.Ontology(input_file)
# # Create a binary stream instead of a file
# binary_stream = io.BytesIO()
# # Save the ontology in OBO format
# ontology.dump(binary_stream, format='obo')
# # Get the OBO content from the binary stream
# obo_content = binary_stream.getvalue().decode('utf-8')
# # Close the stream
# binary_stream.close()
# print(f"Conversion and parsing successful: {input_file}")
# return obo_content


def convert_owl_to_obo(input_file: str) -> str:
try:
# Load the OWL file
ontology = pronto.Ontology(input_file)
# Create a binary stream instead of a file
binary_stream = io.BytesIO()
# Save the ontology in OBO format
ontology.dump(binary_stream, format='obo')
# Get the OBO content from the binary stream
obo_content = binary_stream.getvalue().decode('utf-8')
# Close the stream
binary_stream.close()
print("returning obo conversion")
return obo_content
except Exception as e:
print("Failed to convert OWL to OBO: %s", e)
raise RuntimeError(f"Failed to convert OWL to OBO: {e}")

@app.task
def delete_ontology_task(payload):
Expand Down Expand Up @@ -113,20 +133,137 @@ def add_ontology_task(self, url, **notis):

current_file = general_downloader.download_file()

# if url.endswith(".owl"):
# print("owl file detected")
# ontology_buffer = convert_owl_to_obo(current_file)
# else:
# ontology_buffer = io.TextIOWrapper(current_file, newline=None)

# data = None
#
#
# try:
# if url.endswith(".owl"):
# print("owl file detected")
# ontology_buffer = convert_owl_to_obo(url)
# else:
# ontology_buffer = io.TextIOWrapper(current_file, newline=None)
#
# obo_parser = OBO_Parser(ontology_buffer)
# data = obo_parser.parse(notifications)
# except Exception as e:
# print("e", e)
# notifications.messages.append(f"Error in Pronto conversion. Detailed message: {e}")
# # raise self.retry(exc=e)

data = None

# if url.endswith(".owl"):
# print("owl file detected")
# try:
# obo_converted = convert_owl_to_obo(current_file)
#
# # ontology_buffer = io.TextIOWrapper(obo_converted, newline=None)
#
# ontology_buffer = io.TextIOWrapper(io.BytesIO(obo_converted.encode('utf-8')), newline=None)
#
# obo_parser = OBO_Parser(ontology_buffer)
#
# data = obo_parser.parse(notifications)
# except:
# print("failed")
# data = None
# notifications.messages.append(Message(type="fail", message="Error in Pronto conversion"))
#
# print(f"notificatoins {notifications}")
# else:
# ontology_buffer = io.TextIOWrapper(current_file, newline=None)
#
# # print("ontology buffer", ontology_buffer)
# print("after ontology buffer", ontology_buffer)
#
# obo_parser = OBO_Parser(ontology_buffer)
# data = obo_parser.parse(notifications)
#
#
# print("data is", data)
#
# # print("data", data)
#
# print("finished")


# if url.endswith(".owl"):
# print("owl file detected")
# try:
# obo_converted = convert_owl_to_obo(current_file)
#
# # ontology_buffer = io.TextIOWrapper(obo_converted, newline=None)
#
# ontology_buffer = io.TextIOWrapper(io.BytesIO(obo_converted.encode('utf-8')), newline=None)
#
# obo_parser = OBO_Parser(ontology_buffer)
#
# data = obo_parser.parse(notifications)
# except:
# print("failed")
# data = None
# notifications.messages.append(Message(type="fail", message="Error in Pronto conversion"))
#
# print(f"notificatoins {notifications}")
# else:
# ontology_buffer = io.TextIOWrapper(current_file, newline=None)
#
# # print("ontology buffer", ontology_buffer)
# print("after ontology buffer", ontology_buffer)
#
# obo_parser = OBO_Parser(ontology_buffer)
# data = obo_parser.parse(notifications)
#
#
# print("data is", data)
#
# # print("data", data)
#
# print("finished")


if url.endswith(".owl"):
print("owl file detected")
ontology_buffer = convert_owl_to_obo(current_file)
try:
obo_converted = convert_owl_to_obo(current_file)

ontology_buffer = io.TextIOWrapper(io.BytesIO(obo_converted.encode('utf-8')), newline=None)

# obo_parser = OBO_Parser(ontology_buffer)

# data = obo_parser.parse(notifications)
except:
print("failed")
# data = None
ontology_buffer = None
notifications.messages.append(Message(type="fail", message="Error in Pronto conversion"))

print(f"notificatoins {notifications}")
else:
ontology_buffer = io.TextIOWrapper(current_file, newline=None)


obo_parser = OBO_Parser(ontology_buffer)
data = obo_parser.parse(notifications)




# stop if ontology could not be parsed
if data is None:
notifications_json = notifications.dict()
res = {"task_id": self.request.id, "notifications": notifications_json}
return res
# if data is None:
# print("data is None")
# notifications_json = notifications.dict()
# res = {"task_id": self.request.id, "notifications": notifications_json}
# return res

# if data is None:
# data = {"terms":[], "relationships":[], "ontologies":[]}

print("parsing finished")

Expand Down

0 comments on commit ba6d135

Please sign in to comment.