petab.sbml

Functions for interacting with SBML models

Functions

add_global_parameter(sbml_model, parameter_id)

Add new global parameter to SBML model

add_model_output(sbml_model, observable_id, …)

Add PEtab-style output to model

add_model_output_sigma(sbml_model, …)

Add PEtab-style sigma for the given observable id

add_model_output_with_sigma(sbml_model, …)

Add PEtab-style output and corresponding sigma with single (newly created) parameter

assignment_rules_to_dict(sbml_model[, …])

Turn assignment rules into dictionary.

create_assigment_rule(sbml_model, …[, …])

Create SBML AssignmentRule

get_model_parameters(sbml_model[, with_values])

Return SBML model parameters which are not AssignmentRule targets for observables or sigmas

get_observables(sbml_model[, remove])

Get observables defined in SBML model according to PEtab format.

get_sbml_model(filepath_or_buffer)

Get an SBML model from file or URL or file handle

get_sigmas(sbml_model[, remove])

Get sigmas defined in SBML model according to PEtab format.

globalize_parameters(sbml_model[, …])

Turn all local parameters into global parameters with the same properties

is_sbml_consistent(sbml_document[, check_units])

Check for SBML validity / consistency

load_sbml_from_file(sbml_file)

Load SBML model from file

load_sbml_from_string(sbml_string)

Load SBML model from string

log_sbml_errors(sbml_document[, …])

Log libsbml errors

sbml_parameter_is_observable(sbml_parameter)

Returns whether the libsbml.Parameter sbml_parameter matches the defined observable format.

sbml_parameter_is_sigma(sbml_parameter)

Returns whether the libsbml.Parameter sbml_parameter matches the defined sigma format.

write_sbml(sbml_doc, filename)

Write PEtab visualization table

petab.sbml.add_global_parameter(sbml_model: libsbml.Model, parameter_id: str, parameter_name: Optional[str] = None, constant: bool = False, units: str = 'dimensionless', value: float = 0.0)libsbml.Parameter[source]

Add new global parameter to SBML model

Parameters
  • sbml_model – SBML model

  • parameter_id – ID of the new parameter

  • parameter_name – Name of the new parameter

  • constant – Is parameter constant?

  • units – SBML unit ID

  • value – parameter value

Returns

The created parameter

petab.sbml.add_model_output(sbml_model: libsbml.Model, observable_id: str, formula: str, observable_name: Optional[str] = None)None[source]

Add PEtab-style output to model

We expect that all formula parameters are added to the model elsewhere.

Parameters
  • sbml_model – Model to add output to

  • formula – Formula string for model output

  • observable_id – ID without “observable_” prefix

  • observable_name – Any observable name

petab.sbml.add_model_output_sigma(sbml_model: libsbml.Model, observable_id: str, formula: str)None[source]

Add PEtab-style sigma for the given observable id

We expect that all formula parameters are added to the model elsewhere.

Parameters
  • sbml_model – Model to add to

  • observable_id – Observable id for which to add sigma

  • formula – Formula for sigma

petab.sbml.add_model_output_with_sigma(sbml_model: libsbml.Model, observable_id: str, observable_formula: str, observable_name: Optional[str] = None)None[source]

Add PEtab-style output and corresponding sigma with single (newly created) parameter

We expect that all formula parameters are added to the model elsewhere.

Parameters
  • sbml_model – Model to add output to

  • observable_formula – Formula string for model output

  • observable_id – ID without “observable_” prefix

  • observable_name – Any name

petab.sbml.assignment_rules_to_dict(sbml_model: libsbml.Model, filter_function=<function <lambda>>, remove: bool = False)Dict[str, Dict[str, Any]][source]

Turn assignment rules into dictionary.

Parameters
  • sbml_model – a sbml model instance.

  • filter_function – callback function taking assignment variable as input and returning True/False to indicate if the respective rule should be turned into an observable.

  • remove – Remove the all matching assignment rules from the model

Returns

{
    assigneeId:
    {
        'name': assigneeName,
        'formula': formulaString
    }
}

petab.sbml.create_assigment_rule(sbml_model: libsbml.Model, assignee_id: str, formula: str, rule_id: Optional[str] = None, rule_name: Optional[str] = None)libsbml.AssignmentRule[source]

Create SBML AssignmentRule

Parameters
  • sbml_model – Model to add output to

  • assignee_id – Target of assignment

  • formula – Formula string for model output

  • rule_id – SBML id for created rule

  • rule_name – SBML name for created rule

Returns

The created AssignmentRule

petab.sbml.get_handle(path_or_buf: Union[PathLike[str], str, IO[pandas._typing.T], io.RawIOBase, io.BufferedIOBase, io.TextIOBase, _io.TextIOWrapper, mmap.mmap], mode: str, encoding: Optional[str] = None, compression: Optional[Union[str, Dict[str, Any]]] = None, memory_map: bool = False, is_text: bool = True, errors: Optional[str] = None, storage_options: Optional[Dict[str, Any]] = None)pandas.io.common.IOHandles[source]

Get file handle for given path/buffer and mode.

Parameters
  • path_or_buf (str or file handle) – File path or object.

  • mode (str) – Mode to open path_or_buf with.

  • encoding (str or None) – Encoding to use.

  • compression (str or dict, default None) –

    If string, specifies compression mode. If dict, value at key ‘method’ specifies compression mode. Compression mode must be one of {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}. If compression mode is ‘infer’ and filepath_or_buffer is path-like, then detect compression from the following extensions: ‘.gz’, ‘.bz2’, ‘.zip’, or ‘.xz’ (otherwise no compression). If dict and compression mode is one of {‘zip’, ‘gzip’, ‘bz2’}, or inferred as one of the above, other entries passed as additional compression options.

    Changed in version 1.0.0: May now be a dict with key ‘method’ as compression mode and other keys as compression options if compression mode is ‘zip’.

    Changed in version 1.1.0: Passing compression options as keys in dict is now supported for compression modes ‘gzip’ and ‘bz2’ as well as ‘zip’.

  • memory_map (boolean, default False) – See parsers._parser_params for more information.

  • is_text (boolean, default True) – Whether the type of the content passed to the file/buffer is string or bytes. This is not the same as “b” not in mode. If a string content is passed to a binary file/buffer, a wrapper is inserted.

  • errors (str, default 'strict') – Specifies how encoding and decoding errors are to be handled. See the errors argument for open() for a full list of options.

  • storage_options (StorageOptions = None) – Passed to _get_filepath_or_buffer

  • versionchanged: (.) – 1.2.0:

  • the dataclass IOHandles (Returns) –

petab.sbml.get_model_parameters(sbml_model: libsbml.Model, with_values=False)Union[List[str], Dict[str, float]][source]

Return SBML model parameters which are not AssignmentRule targets for observables or sigmas

Parameters
  • sbml_model – SBML model

  • with_values – If False, returns list of SBML model parameter IDs which are not AssignmentRule targets for observables or sigmas. If True, returns a dictionary with those parameter IDs as keys and parameter values from the SBML model as values.

petab.sbml.get_observables(sbml_model: libsbml.Model, remove: bool = False)dict[source]

Get observables defined in SBML model according to PEtab format.

Returns

Dictionary of observable definitions. See assignment_rules_to_dict for details.

petab.sbml.get_sbml_model(filepath_or_buffer)Tuple[libsbml.SBMLReader, libsbml.SBMLDocument, libsbml.Model][source]

Get an SBML model from file or URL or file handle

Parameters

filepath_or_buffer – File or URL or file handle to read the model from

Returns

The SBML document, model and reader

petab.sbml.get_sigmas(sbml_model: libsbml.Model, remove: bool = False)dict[source]

Get sigmas defined in SBML model according to PEtab format.

Returns

Dictionary of sigma definitions.

Keys are observable IDs, for values see assignment_rules_to_dict for details.

petab.sbml.globalize_parameters(sbml_model: libsbml.Model, prepend_reaction_id: bool = False)None[source]

Turn all local parameters into global parameters with the same properties

Local parameters are currently ignored by other PEtab functions. Use this function to convert them to global parameters. There may exist local parameters with identical IDs within different kinetic laws. This is not checked here. If in doubt that local parameter IDs are unique, enable prepend_reaction_id to create global parameters named ${reaction_id}_${local_parameter_id}.

Parameters
  • sbml_model – The SBML model to operate on

  • prepend_reaction_id – Prepend reaction id of local parameter when creating global parameters

petab.sbml.is_file_like(obj)bool[source]

Check if the object is a file-like object.

For objects to be considered file-like, they must be an iterator AND have either a read and/or write method as an attribute.

Note: file-like objects must be iterable, but iterable objects need not be file-like.

Parameters

obj (The object to check) –

Returns

is_file_like – Whether obj has file-like properties.

Return type

bool

Examples

>>> import io
>>> buffer = io.StringIO("data")
>>> is_file_like(buffer)
True
>>> is_file_like([1, 2, 3])
False
petab.sbml.is_sbml_consistent(sbml_document: libsbml.SBMLDocument, check_units: bool = False)bool[source]

Check for SBML validity / consistency

Parameters
  • sbml_document – SBML document to check

  • check_units – Also check for unit-related issues

Returns

False if problems were detected, otherwise True

petab.sbml.is_url(url)bool[source]

Check to see if a URL has a valid protocol.

Parameters

url (str or unicode) –

Returns

isurl – If url has a valid protocol return True otherwise False.

Return type

bool

petab.sbml.load_sbml_from_file(sbml_file: str)Tuple[libsbml.SBMLReader, libsbml.SBMLDocument, libsbml.Model][source]

Load SBML model from file

Parameters

sbml_file – Filename of the SBML file

Returns

The SBML document, model and reader

petab.sbml.load_sbml_from_string(sbml_string: str)Tuple[libsbml.SBMLReader, libsbml.SBMLDocument, libsbml.Model][source]

Load SBML model from string

Parameters

sbml_string – Model as XML string

Returns

The SBML document, model and reader

petab.sbml.log_sbml_errors(sbml_document: libsbml.SBMLDocument, minimum_severity=1)None[source]

Log libsbml errors

Parameters
  • sbml_document – SBML document to check

  • minimum_severity – Minimum severity level to report (see libsbml)

petab.sbml.sbml_parameter_is_observable(sbml_parameter: libsbml.Parameter)bool[source]

Returns whether the libsbml.Parameter sbml_parameter matches the defined observable format.

petab.sbml.sbml_parameter_is_sigma(sbml_parameter: libsbml.Parameter)bool[source]

Returns whether the libsbml.Parameter sbml_parameter matches the defined sigma format.

petab.sbml.warn(message, category=None, stacklevel=1, source=None)

Issue a warning, or maybe ignore it or raise an exception.

petab.sbml.write_sbml(sbml_doc: libsbml.SBMLDocument, filename: str)None[source]

Write PEtab visualization table

Parameters
  • sbml_doc – SBML document containing the SBML model

  • filename – Destination file name