sbmlxdf Methods

Create Model object

class sbmlxdf.Model(import_file=None)[source]
__init__(import_file=None)[source]

Constructor.

import_file can be any of: - SBML coded model (.xml) - model from spreadsheet (.xlsx or .ods) - model from cvs directory (directory name)

If import_file not specified, it must be imported later.

see also: import_sbml(), from_excel(), from_csv(), from_df()

Parameters:

import_file (str, optional) – filename of model definition

Returns:

success/failure

Return type:

bool

Model import/exports

Interface SBML models

Model.import_sbml(sbml_file)[source]

Import SBML coded model.

Parameters:

sbml_file (str) – file name of SBML model (.xml)

Returns:

success/failure

Return type:

bool

Model.export_sbml(sbml_file)[source]

Export model as SBML coded model.

Recommended to first validate model against SBML specifications.

see also: validate_sbml()

Parameters:

sbml_file (str) – file name of new SBML model (.xml).

Returns:

success/failure of export

Return type:

bool

Interface spreadsheet documents

Model.from_excel(file_name)[source]

Import model coded in Excel or OpenOffice spreadsheet.

Note: spreadsheet structure and naming can be identified by importing an existing SBML coded model and subsequently exporting it to Excel or OpenOffice.

Note: Package testing made with Excel spreadsheet.

Only known sheets and columns are imported. Column order is arbitrary, except of first column (‘id’ columns) which in most cases is used as index.

Parameters:

file_name (str) – file name of spreadsheet document with model info

Returns:

success/failure

Return type:

bool

Model.to_excel(file_name, model_dict=None)[source]

Export model to Excel or OpenOffice spreadsheet.

Optional a model_dict could be provided, in case additional (unsupported) attributes should be exported.

Parameters:
  • file_name (str) – file name of new spreadsheet document (.xlsx or .ods)

  • model_dict (dict) – optional, pandas DataFrames of model components

Interface .csv files

Model.from_csv(dir_name)[source]

Import model coded in set of .csv files.

File names and header names are significant. Only known names are imported, other names may exist. With few exceptions, the ‘id’ column must be the first column in the tables.

Parameters:

dir_name (str) – directory name containing the .csv files of model

Returns:

success/failure

Return type:

bool

Model.to_csv(dir_name)[source]

Export model to comma-separated-value files (.csv).

Parameters:

dir_name (str) – directory name for .csv files

Interface with pandas dataframes

Model.from_df(model_dict)[source]

Import model coded in pandas DataFrames.

Keys of dict, header names and index of dataframes are significant. Only known names are imported, other names may exist. With few exceptions, index must be set on ‘id’. Keys ‘sbml’ and ‘modelAttrs’ reference pandas series objects.

Parameters:

model_dict (dict) – pandas DataFrames of model components

Returns:

success/failure

Return type:

bool

Model.to_df()[source]

Export model to a dict of pandas DataFrames.

Keys ‘sbml’ and ‘modelAttrs’ reference pandas Series objects. Index of dataframes is mainly set on ‘id’ attribute.

Returns:

pandas DataFrames of model components

Return type:

dict

Model validation

Validate compliance with SBML specification

Model.validate_sbml(sbml_file='tmp.xml', units_check=True)[source]

Validate model against SBML specifications.

Uses checkConsistency() method from libSBML. Model is exported as a SBML model with name sbml_file and written to directory ./tmp. Directory is created, if not existing. Line numbers in warning/errors messages can be checked against SBML file. Warnings and errors are copied to a text file with same name as sbml_file, having extension (.txt).

Parameters:
  • sbml_file (str) – file name of temporary SBML model (default: tmp.xml)

  • units_check (bool, optional) – units check on/off (default: on)

Returns:

Error types and number of occurrences

Return type:

dict

Miscellaneous

Data extraction helper functions

Model.get_s_matrix(sparse=False)[source]

Retrieve stoichiometric matrix.

rows: species ids columns: reaction ids values: stoichiometric coefficients (float)

Parameters:

sparse (bool, optional) – S-matrix in normal/sparse format (default: normal)

Returns:

stoichiometric matrix

Return type:

pandas DataFrame

sbmlxdf.misc.record_generator(records_str, sep=';')[source]

Generator to extract individual records from a string of records.

This generator does not yet handle nested records.

Example: parsing through species reference records, e.g. df_reaction[‘rectants’]

srefs = {}
for sref_str in sbmlxdf.record_generator(srefs_str):
    params = sbmlxdf.extract_params(sref_str)
    srefs[params['species']] = float(params['stoic'])
Parameters:
  • records_str (str) – containing records separated by sep

  • sep (str (default: ';')) – seperator used to separate records

Returns:

key-values pairs extracted from record

Return type:

dict

sbmlxdf.misc.extract_params(record)[source]

Extract parameters from a record.

A single record consists of comma separated key-value pairs. Example: ‘key1=val1, key2=val2, …’ is converted to {key1: val1, key2: val2, …}

Parameters:

record (str) – key ‘=’ value pairs separated by “,”

Returns:

key-values pairs extracted from record

Return type:

dict

sbmlxdf.misc.extract_xml_attrs(xml_annots, ns=None, token=None)[source]

Extract XML-attributes from given namespace and/or token.

Example of xml_annots: ‘ns_uri=http://www.hhu.de/ccb/bgm/ns, prefix=bgm, token=molecule, weight_Da=100’

XML_SPECIES_NS = 'http://www.hhu.de/ccb/rba/species/ns'
xml_attrs = sbmlxdf.misc.extract_xml_attrs(xml_annots, ns=XML_SPECIES_NS)
Parameters:
  • xml_annots (str) – XML-annotations separated by “;”

  • ns (str, optional) – namespace from which to collect attributes

  • token (str, optional) – token from which to collect attributes

Returns:

attribute names corresponding values

Return type:

dict

sbmlxdf.misc.mathml2numpy(mformula, np_ns='np')[source]

Convert mathml infix notation to a numpy notation.

mathml functions and operators are converted to numpy equivalents, where possible. Functions are prefixed with numpy namespace

Parameters:
  • mformula (str) – mathml infix notation extracted from SBML

  • np_ns (str) – numpy namespace prefix used in own Python code. Default: ‘np’

Returns:

mathml converted to numpy notation

Return type:

str