Skip to content

Commit

Permalink
Add support for reading PEtab COMBINE archives
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl committed Feb 7, 2020
1 parent c47e234 commit 1e012aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion petab/C.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@


# YAML

FORMAT_VERSION = 'format_version'
PARAMETER_FILE = 'parameter_file'
PROBLEMS = 'problems'
SBML_FILES = 'sbml_files'
Expand Down
36 changes: 35 additions & 1 deletion petab/problem.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""PEtab Problem class"""

import os
import tempfile
from warnings import warn

import pandas as pd
import libcombine
import libsbml
from typing import Optional, List, Union, Dict, Iterable
from . import (parameter_mapping, measurements, conditions, parameters,
sampling, sbml, yaml, core, observables)
sampling, sbml, yaml, core, observables, format_version)
from .C import * # noqa: F403


Expand Down Expand Up @@ -161,6 +163,11 @@ def from_yaml(yaml_config: Union[Dict, str]) -> 'Problem':
'Consider using '
'petab.CompositeProblem.from_yaml() instead.')

if yaml_config[FORMAT_VERSION] != format_version.__format_version__:
raise ValueError("Provided PEtab files are of unsupported version"
f"{yaml_config[FORMAT_VERSION]}. Expected "
f"{format_version.__format_version__}.")

problem0 = yaml_config['problems'][0]

yaml.assert_single_condition_and_sbml_file(problem0)
Expand Down Expand Up @@ -218,6 +225,33 @@ def from_folder(folder: str, model_name: str = None) -> 'Problem':
sbml_file=get_default_sbml_file_name(model_name, folder),
)

@staticmethod
def from_combine(filename: str) -> 'Problem':
"""Read PEtab COMBINE archive (http://co.mbine.org/documents/archive).
See also ``create_combine_archive``.
Arguments:
filename: Path to the PEtab-COMBINE archive
Returns:
A ``petab.Problem`` instance.
"""

archive = libcombine.CombineArchive()
if archive.initializeFromArchive(filename) is None:
print(f"Invalid Combine Archive: {filename}")
return None

with tempfile.TemporaryDirectory() as tmpdirname:
archive.extractTo(tmpdirname)
problem = Problem.from_yaml(
os.path.join(tmpdirname,
archive.getMasterFile().getLocation()))
archive.cleanUp()

return problem

def to_files(self,
sbml_file: Optional[str] = None,
condition_file: Optional[str] = None,
Expand Down

0 comments on commit 1e012aa

Please sign in to comment.