SBML merge

sbmlutils provides functionality for merging multiple SBML models. The merging uses the SBML comp extension.

In the following example we will merge the first biomodels in a single model. Models are provided as dictionary

{
    'model1_id': model1_path,
    'model2_id': model2_path,
    ...
}

The model_ids are used as ids for the ExternalModelDefinitions and will be the respective prefixes in the flattened model.

[1]:
from pathlib import Path
import tempfile

from sbmlutils.manipulation import merge_models
from sbmlutils import RESOURCES_DIR
from sbmlutils.validation import ValidationOptions


def merge_models_example() -> None:
    """Demonstrate merging of models."""

    input_dir = RESOURCES_DIR / "testdata" / "manipulation" / "merge"

    # dictionary of ids & paths of models which should be combined
    # here we just bring together the first Biomodels
    model_ids = [f"BIOMD000000000{k}" for k in range(1, 5)]
    model_paths = dict(zip(model_ids, [input_dir / f"{mid}.xml" for mid in model_ids]))

    temp_dir = tempfile.TemporaryDirectory()
    merge_models(model_paths, output_dir=temp_dir.name, validation_options=ValidationOptions(units_consistency=False))


if __name__ == "__main__":
    merge_models_example()
WARNING  'output_dir' should be a Path but: '<class 'str'>'                                             merge.py:59
────────────────────────────────────────────────── Validate SBML ──────────────────────────────────────────────────
file:///home/mkoenig/git/sbmlutils/src/sbmlutils/resources/testdata/manipulation/merge/BIOMD0000000001.xml
valid                    : TRUE
check time (s)           : 0.010
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────── Validate SBML ──────────────────────────────────────────────────
file:///home/mkoenig/git/sbmlutils/src/sbmlutils/resources/testdata/manipulation/merge/BIOMD0000000002.xml
valid                    : TRUE
check time (s)           : 0.009
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────── Validate SBML ──────────────────────────────────────────────────
file:///home/mkoenig/git/sbmlutils/src/sbmlutils/resources/testdata/manipulation/merge/BIOMD0000000003.xml
valid                    : TRUE
check time (s)           : 0.004
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────── Validate SBML ──────────────────────────────────────────────────
file:///home/mkoenig/git/sbmlutils/src/sbmlutils/resources/testdata/manipulation/merge/BIOMD0000000004.xml
valid                    : TRUE
check time (s)           : 0.004
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────── Validate SBML ──────────────────────────────────────────────────
file:///tmp/tmphrb1emar/merged.xml
valid                    : TRUE
check time (s)           : 0.054
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────── Flatten SBML ───────────────────────────────────────────────────
<SBMLDocument>
flattend                 : TRUE
flatten time (ms)        : 0.104
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
INFO     Flattened model created: '/tmp/tmphrb1emar/merged_flat.xml'                                 flatten.py:109
────────────────────────────────────────────────── Validate SBML ──────────────────────────────────────────────────
file:///tmp/tmphrb1emar/merged_flat.xml
valid                    : TRUE
check time (s)           : 0.024
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────

For the additional options see the API documentation

[2]:
from rich import inspect
inspect(merge_models)
╭─────────────────────────────────── <function merge_models at 0x7feeb82f5b80> ───────────────────────────────────╮
 def merge_models(model_paths: Dict[str, pathlib.Path], output_dir: pathlib.Path, merged_id: str = 'merged',     
 flatten: bool = True, validate: bool = True, validate_input: bool = True, validation_options:                   
 Optional[sbmlutils.validation.ValidationOptions] = None, sbml_level: int = 3, sbml_version: int = 1) ->         
 libsbml.SBMLDocument:                                                                                           
                                                                                                                 
 Merge SBML models.                                                                                              
                                                                                                                 
 35 attribute(s) not shown. Run inspect(inspect) for options.                                                    
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
[ ]: