# HG changeset patch # User luca_milaz # Date 1721591307 0 # Node ID 2590e2f603072769848f290869b8332f7f526f07 # Parent ee770fb7beef75c73e25604e704da1f7172a7246 Uploaded diff -r ee770fb7beef -r 2590e2f60307 marea_2/utils/general_utils.py --- a/marea_2/utils/general_utils.py Sun Jul 21 19:40:32 2024 +0000 +++ b/marea_2/utils/general_utils.py Sun Jul 21 19:48:27 2024 +0000 @@ -10,6 +10,7 @@ from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union import pandas as pd +import cobra # FILES class FileFormat(Enum): @@ -547,5 +548,24 @@ path = customPath if self is Model.Custom else FilePath(f"{self.name}_map", FileFormat.SVG, prefix = f"{toolDir}/local/svg metabolic maps/") self.__raiseMissingPathErr(path) return readSvg(path, customErr = DataErr(path, f"custom map in wrong format")) + + def getCOBRAmodel(self, toolDir = ".", customPath :Optional[FilePath] = None, customExtension :Optional[FilePath]=None)->cobra.Model: + if(self is Model.Custom): + return self.load_custom_model(customPath, customExtension) + else: + return cobra.io.read_sbml_model(FilePath(f"{self.name}", FileFormat.XML, prefix = f"{toolDir}/local/models/").show()) + + def load_custom_model(self, file_path :FilePath, ext :Optional[FileFormat] = None) -> cobra.Model: + ext = ext if ext else file_path.ext + try: + if ext is FileFormat.XML: + return cobra.io.read_sbml_model(file_path.show()) + + if ext is FileFormat.JSON: + return cobra.io.load_json_model(file_path.show()) + + except Exception as e: raise DataErr(file_path, e.__str__()) + raise DataErr(file_path, + f"Fomat \"{file_path.ext}\" is not recognized, only JSON and XML files are supported.") def __str__(self) -> str: return self.value \ No newline at end of file