Skip to content

Commit

Permalink
Model.to_file
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl committed May 19, 2022
1 parent 7d9dae8 commit 3d4cda5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
10 changes: 10 additions & 0 deletions petab/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from __future__ import annotations

import abc
from pathlib import Path
from typing import Any, Iterable, Tuple

from . import MODEL_TYPE_SBML, known_model_types


Expand All @@ -25,6 +27,14 @@ def from_file(filepath_or_buffer: Any) -> Model:
"""
...

@abc.abstractmethod
def to_file(self, filename: [str, Path]):
"""Save the model to the given file
:param filename: Destination filename
"""
...

@classmethod
@property
@abc.abstractmethod
Expand Down
6 changes: 6 additions & 0 deletions petab/models/sbml_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Functions for handling SBML models"""

import itertools
from pathlib import Path
from typing import Iterable, Optional, Tuple

import libsbml
Expand Down Expand Up @@ -64,6 +65,11 @@ def from_file(filepath_or_buffer):
sbml_document=sbml_document,
)

def to_file(self, filename: [str, Path]):
from ..sbml import write_sbml
write_sbml(self.sbml_document or self.sbml_model.getSBMLDocument(),
filename)

def get_parameter_ids(self) -> Iterable[str]:
return (
p.getId() for p in self.sbml_model.getListOfParameters()
Expand Down
18 changes: 13 additions & 5 deletions petab/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def to_files(self,
yaml_file: Union[None, str, Path] = None,
prefix_path: Union[None, str, Path] = None,
relative_paths: bool = True,
model_file: Union[None, str, Path] = None,
) -> None:
"""
Write PEtab tables to files for this problem
Expand Down Expand Up @@ -404,11 +405,18 @@ def add_prefix(path0: Union[None, str, Path]) -> str:
yaml_file = add_prefix(yaml_file)

if sbml_file:
if self.sbml_document is not None:
sbml.write_sbml(self.sbml_document, sbml_file)
else:
raise ValueError("Unable to save SBML model with no "
"sbml_doc set.")
warn("The `sbml_file` argument is deprecated and will be "
"removed in a future version. Use `model_file` instead.",
DeprecationWarning, stacklevel=2)

if model_file:
raise ValueError("Must provide either `sbml_file` or "
"`model_file` argument, but not both.")

model_file = sbml_file

if model_file:
self.model.to_file(model_file)

def error(name: str) -> ValueError:
return ValueError(f"Unable to save non-existent {name} table")
Expand Down

0 comments on commit 3d4cda5

Please sign in to comment.