adesomics.io.gsmm
1from pathlib import Path 2import cobra 3 4# Force GLPK: an expired Gurobi/CPLEX license on this machine otherwise gets 5# picked up as cobra's default solver and breaks validation/optimization. 6cobra.Configuration().solver = "glpk" 7 8_XML_SUFFIXES = {".xml"} 9 10class Model: 11 """ 12 Represents a metabolic model. 13 14 Attributes 15 ---------- 16 model : cobra.Model 17 The metabolic model data. 18 source_path : Path or None 19 The path to the source file from which the model was loaded. 20 """ 21 22 def __init__(self, model: cobra.Model, source_path: Path | None = None, objective: str | None = None): 23 self.model = model 24 self.source_path = source_path 25 self.objective = objective 26 27 @classmethod 28 def from_file(cls, path: str | Path, objective: str | None = None) -> "Model": 29 """ 30 Load a metabolic model from a file. 31 32 Parameters 33 ---------- 34 path : str or Path 35 The path to the metabolic model file (.xml). 36 37 Raises 38 ------ 39 FileNotFoundError 40 If the specified file does not exist. 41 ValueError 42 If the file extension is not recognized or if there is a parse failure. 43 Returns 44 ------- 45 Model 46 An instance of Model containing the loaded cobra.Model. 47 """ 48 49 path = Path(path) 50 51 if not path.exists(): 52 raise FileNotFoundError(f"Metabolic model file not found: {path}") 53 54 suffix = path.suffix.lower() 55 56 if suffix in _XML_SUFFIXES: 57 try: 58 model = cobra.io.read_sbml_model(str(path)) 59 return cls(model=model, source_path=path) 60 except Exception as e: 61 raise ValueError(f"Could not parse SBML model file '{path}': {e}") from e 62 else: 63 raise ValueError(f"Unrecognized file extension for metabolic model: {suffix}") 64 65 def summary(self) -> str: 66 return ( 67 f"Metabolic model summary:\n" 68 f" Genes: {len(self.model.genes)}\n" 69 f" Reactions: {len(self.model.reactions)}\n" 70 f" Metabolites: {len(self.model.metabolites)}" 71 ) 72 73 def optimize(self): 74 """ 75 Optimize the metabolic model using the default solver. 76 77 Returns 78 ------- 79 cobra.Solution 80 The solution object containing optimization results. 81 """ 82 self.objective = self.model.optimize() 83 return self.objective 84 85 86 """ 87 def __repr__(self) -> str: 88 src = self.source_path.name if self.source_path else "in-memory" 89 return f"<Model {len(self.model.reactions)}r x {len(self.model.metabolites)}m from '{src}'>" 90 """
class
Model:
11class Model: 12 """ 13 Represents a metabolic model. 14 15 Attributes 16 ---------- 17 model : cobra.Model 18 The metabolic model data. 19 source_path : Path or None 20 The path to the source file from which the model was loaded. 21 """ 22 23 def __init__(self, model: cobra.Model, source_path: Path | None = None, objective: str | None = None): 24 self.model = model 25 self.source_path = source_path 26 self.objective = objective 27 28 @classmethod 29 def from_file(cls, path: str | Path, objective: str | None = None) -> "Model": 30 """ 31 Load a metabolic model from a file. 32 33 Parameters 34 ---------- 35 path : str or Path 36 The path to the metabolic model file (.xml). 37 38 Raises 39 ------ 40 FileNotFoundError 41 If the specified file does not exist. 42 ValueError 43 If the file extension is not recognized or if there is a parse failure. 44 Returns 45 ------- 46 Model 47 An instance of Model containing the loaded cobra.Model. 48 """ 49 50 path = Path(path) 51 52 if not path.exists(): 53 raise FileNotFoundError(f"Metabolic model file not found: {path}") 54 55 suffix = path.suffix.lower() 56 57 if suffix in _XML_SUFFIXES: 58 try: 59 model = cobra.io.read_sbml_model(str(path)) 60 return cls(model=model, source_path=path) 61 except Exception as e: 62 raise ValueError(f"Could not parse SBML model file '{path}': {e}") from e 63 else: 64 raise ValueError(f"Unrecognized file extension for metabolic model: {suffix}") 65 66 def summary(self) -> str: 67 return ( 68 f"Metabolic model summary:\n" 69 f" Genes: {len(self.model.genes)}\n" 70 f" Reactions: {len(self.model.reactions)}\n" 71 f" Metabolites: {len(self.model.metabolites)}" 72 ) 73 74 def optimize(self): 75 """ 76 Optimize the metabolic model using the default solver. 77 78 Returns 79 ------- 80 cobra.Solution 81 The solution object containing optimization results. 82 """ 83 self.objective = self.model.optimize() 84 return self.objective 85 86 87 """ 88 def __repr__(self) -> str: 89 src = self.source_path.name if self.source_path else "in-memory" 90 return f"<Model {len(self.model.reactions)}r x {len(self.model.metabolites)}m from '{src}'>" 91 """
Represents a metabolic model.
Attributes
model : cobra.Model The metabolic model data. source_path : Path or None The path to the source file from which the model was loaded.
Model( model: cobra.core.model.Model, source_path: pathlib.Path | None = None, objective: str | None = None)
28 @classmethod 29 def from_file(cls, path: str | Path, objective: str | None = None) -> "Model": 30 """ 31 Load a metabolic model from a file. 32 33 Parameters 34 ---------- 35 path : str or Path 36 The path to the metabolic model file (.xml). 37 38 Raises 39 ------ 40 FileNotFoundError 41 If the specified file does not exist. 42 ValueError 43 If the file extension is not recognized or if there is a parse failure. 44 Returns 45 ------- 46 Model 47 An instance of Model containing the loaded cobra.Model. 48 """ 49 50 path = Path(path) 51 52 if not path.exists(): 53 raise FileNotFoundError(f"Metabolic model file not found: {path}") 54 55 suffix = path.suffix.lower() 56 57 if suffix in _XML_SUFFIXES: 58 try: 59 model = cobra.io.read_sbml_model(str(path)) 60 return cls(model=model, source_path=path) 61 except Exception as e: 62 raise ValueError(f"Could not parse SBML model file '{path}': {e}") from e 63 else: 64 raise ValueError(f"Unrecognized file extension for metabolic model: {suffix}")
Load a metabolic model from a file.
Parameters
path : str or Path The path to the metabolic model file (.xml).
Raises
FileNotFoundError If the specified file does not exist. ValueError If the file extension is not recognized or if there is a parse failure.
Returns
Model An instance of Model containing the loaded cobra.Model.
def
optimize(self):
74 def optimize(self): 75 """ 76 Optimize the metabolic model using the default solver. 77 78 Returns 79 ------- 80 cobra.Solution 81 The solution object containing optimization results. 82 """ 83 self.objective = self.model.optimize() 84 return self.objective
Optimize the metabolic model using the default solver.
Returns
cobra.Solution The solution object containing optimization results.