BioNetGen integration (pysb.bng)

class pysb.bng.BngBaseInterface(model=None, verbose=False, cleanup=False, output_prefix=None, output_dir=None, model_additional_species=None, model_population_maps=None)[source]

Abstract base class for interfacing with BNG

abstract action(action, **kwargs)[source]

Generates code to execute a BNG action command

Parameters:
action: string

The name of the BNG action function

kwargs: kwargs, optional

Arguments and values to supply to BNG

property base_filename

Returns the base filename (without extension) for BNG output files

property bng_filename

Returns the BNG command list (.bngl) filename (does not check whether the file exists)

property net_filename

Returns the BNG network filename (does not check whether the file exists)

read_netfile()[source]

Reads a BNG network file as a string. Note that you must execute network generation separately before attempting this, or the file will not be found. :return: Contents of the BNG network file as a string

read_simulation_results()[source]

Read the results of a BNG simulation as a numpy array

Returns:
numpy.ndarray

Simulation results in a 2D matrix (time on Y axis, species/observables/expressions on X axis depending on simulation type)

static read_simulation_results_multi(base_filenames)[source]

Read the results of multiple BNG simulations

Parameters:
base_filenames: list of str

A list of filename stems to read simulation results in from, including the full path but not including any file extension.

Returns:
list of numpy.ndarray

List of simulation results, each in a 2D matrix (time on Y axis, species/observables/expressions on X axis depending on simulation type)

class pysb.bng.BngConsole(model=None, verbose=False, cleanup=True, output_dir=None, output_prefix=None, timeout=30, suppress_warnings=False, model_additional_species=None)[source]

Interact with BioNetGen through BNG Console

action(action, **kwargs)[source]

Generates a BNG action command and executes it through the console, returning any console output

Parameters:
actionstring

The name of the BNG action function

kwargskwargs, optional

Arguments and values to supply to BNG

generate_network(overwrite=False)[source]

Generates a network in BNG and returns the network file contents as a string

Parameters:
overwrite: bool, optional

Overwrite existing network file, if any

load_bngl(bngl_file)[source]

Load a BNGL file in the BNG console

Parameters:
bngl_filestring

The filename of a .bngl file

class pysb.bng.BngFileInterface(model=None, verbose=False, output_dir=None, output_prefix=None, cleanup=True, model_additional_species=None, model_population_maps=None)[source]
action(action, **kwargs)[source]

Generates a BNG action command and adds it to the command queue

Parameters:
actionstring

The name of the BNG action function

kwargskwargs, optional

Arguments and values to supply to BNG

execute(reload_netfile=False, skip_file_actions=True)[source]

Executes all BNG commands in the command queue.

Parameters:
reload_netfile: bool or str

If true, attempts to reload an existing .net file from a previous execute() iteration. If a string, the filename specified in the string is supplied to BNG’s readFile (which can be any file type BNG supports, such as .net or .bngl). This is useful for running multiple actions in a row, where results need to be read into PySB before a new series of actions is executed.

skip_file_actions: bool

Only used if the previous argument is not False. Set this argument to True to ignore any actions block in the loaded file.

set_concentration(cplx_pat, value)[source]

Generates a BNG action command and adds it to the command queue

Parameters:
cplx_pat: pysb.ComplexPattern or string

Species ComplexPattern, or a BNG format string representation

value: float-like

Initial concentration

set_parameter(name, value)[source]

Generates a BNG action command and adds it to the command queue

Parameters:
name: string

The name of the parameter to set

value: float-like

Value of parameter

exception pysb.bng.BngInterfaceError[source]

BNG reported an error

exception pysb.bng.NoInitialConditionsError[source]

Model initial_conditions is empty.

exception pysb.bng.NoRulesError[source]

Model rules is empty.

pysb.bng.generate_equations(model, cleanup=True, verbose=False, **kwargs)[source]

Generate math expressions for reaction rates and species in a model.

This fills in the following pieces of the model:

  • species

  • reactions

  • reactions_bidirectional

  • observables (just coefficients and species fields for each element)

Parameters:
modelModel

Model to pass to generate_network.

cleanupbool, optional

If True (default), delete the temporary files after the simulation is finished. If False, leave them in place (in output_dir). Useful for debugging.

verbosebool or int, optional (default: False)

Sets the verbosity level of the logger. See the logging levels and constants from Python’s logging module for interpretation of integer values. False is equal to the PySB default level (currently WARNING), True is equal to DEBUG.

pysb.bng.generate_network(model, cleanup=True, append_stdout=False, verbose=False, **kwargs)[source]

Return the output from BNG’s generate_network function given a model.

The output is a BNGL model definition with additional sections ‘reactions’ and ‘groups’, and the ‘species’ section expanded to contain all possible species. BNG refers to this as a ‘net’ file.

Parameters:
modelModel

Model to pass to generate_network.

cleanupbool, optional

If True (default), delete the temporary files after the simulation is finished. If False, leave them in place (in output_dir). Useful for debugging.

append_stdoutbool, optional

This option is no longer supported and has been left here for API compatibility reasons.

verbosebool or int, optional (default: False)

Sets the verbosity level of the logger. See the logging levels and constants from Python’s logging module for interpretation of integer values. False is equal to the PySB default level (currently WARNING), True is equal to DEBUG.

pysb.bng.load_equations(model, netfile)[source]

Load model equations from a specified netfile

Useful for large models where BioNetGen network generation takes a long time - the .net file can be saved and reloaded using this function at a later date.

Parameters:
model: pysb.Model

PySB model file

netfile: str

BNG netfile

pysb.bng.parse_bngl_expr(text, *args, **kwargs)[source]

Convert a BNGL math expression string to a sympy Expr.

pysb.bng.run_ssa(model, t_end=10, n_steps=100, param_values=None, output_dir=None, output_file_basename=None, cleanup=True, verbose=False, **additional_args)[source]

Simulate a model with BNG’s SSA simulator and return the trajectories.

Parameters:
modelModel

Model to simulate.

t_endnumber, optional

Final time point of the simulation.

n_stepsint, optional

Number of steps in the simulation.

param_valuesvector-like or dictionary, optional

Values to use for every parameter in the model. Ordering is determined by the order of model.parameters. If not specified, parameter values will be taken directly from model.parameters.

output_dirstring, optional

Location for temporary files generated by BNG. If None (the default), uses a temporary directory provided by the system. A temporary directory with a random name is created within the supplied location.

output_file_basenamestring, optional

This argument is used as a prefix for the temporary BNG output directory, rather than the individual files.

cleanupbool, optional

If True (default), delete the temporary files after the simulation is finished. If False, leave them in place. Useful for debugging.

verbosebool or int, optional (default: False)

Sets the verbosity level of the logger. See the logging levels and constants from Python’s logging module for interpretation of integer values. False is equal to the PySB default level (currently WARNING), True is equal to DEBUG.

additional_args: kwargs, optional

Additional arguments to pass to BioNetGen

pysb.bng.set_bng_path(dir)[source]

Deprecated. Use pysb.pathfinder.set_path() instead.