From 3d4cda5e32550dd321662298733c822446140001 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Thu, 5 May 2022 13:22:57 +0200 Subject: [PATCH] Model.to_file --- petab/models/model.py | 10 ++++++++++ petab/models/sbml_model.py | 6 ++++++ petab/problem.py | 18 +++++++++++++----- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/petab/models/model.py b/petab/models/model.py index cfc94ab4..00270204 100644 --- a/petab/models/model.py +++ b/petab/models/model.py @@ -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 @@ -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 diff --git a/petab/models/sbml_model.py b/petab/models/sbml_model.py index 62aa887d..bac5c675 100644 --- a/petab/models/sbml_model.py +++ b/petab/models/sbml_model.py @@ -1,6 +1,7 @@ """Functions for handling SBML models""" import itertools +from pathlib import Path from typing import Iterable, Optional, Tuple import libsbml @@ -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() diff --git a/petab/problem.py b/petab/problem.py index faed0cf2..d9549a13 100644 --- a/petab/problem.py +++ b/petab/problem.py @@ -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 @@ -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")