bionetgen.modelapi package


bionetgen.modelapi.blocks module

class bionetgen.modelapi.blocks.ActionBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Action block object, subclass of ModelBlock.This is the one object that doesn’t need a begin/end block tag to be in the model.


list of supported action names

add_action(name, action_type=None, action_args={})

adds an action by making a new Action object and passing the args/kwargs to its initialization.


add_action(action_type, action_args) None[source]

adds action, needs type as string and args as list of tuples (which preserve order) of (argument, value) pairs

add_item(item_tpl) None[source]
clear_actions() None[source]
gen_string() str[source]
class bionetgen.modelapi.blocks.CompartmentBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Compartment block object, subclass of ModelBlock.

add_compartment(name, dim, size, outside=None)

adds a compartment by making a new Compartment object and passing the args/kwargs to its initialization.

add_compartment(*args, **kwargs) None[source]
class bionetgen.modelapi.blocks.FunctionBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Function block object, subclass of ModelBlock.

add_function(name, name, expr, args=None)

adds a function by making a new Function object and passing the args/kwargs to its initialization.

add_function(*args, **kwargs) None[source]
class bionetgen.modelapi.blocks.ModelBlock[source]

Bases: object

Base block object that will be used for each block in the model.


Name of the block which will be used to write the BNGL text

comment(str, str)

comment at the begin {block} or end {block} statements, tuple


all the model objects in the block


a dictionary to keep track of all the changes done in a block after it is originally created


a tag that tells a potential future simulator if the model needs to be recompiled. TODO: has to be computed from _changes property upon get request.


resets _recompile and _changes tags for the block


sets self.item[name] = value to add a particular model object into a block


loops over every element in the list and uses add_item on it


for every block this method generates the BNGL string of the block. it has to be overwritten for each block.

add_item(item_tpl) None[source]
add_items(item_list) None[source]
gen_string() str[source]
reset_compilation_tags() None[source]
class bionetgen.modelapi.blocks.MoleculeTypeBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Molecule type block, subclass of ModelBlock.

add_molecule_type(name, name, components)

adds a molecule type by making a new MoleculeType object and passing the args/kwargs to its initialization.

add_molecule_type(name, components) None[source]
class bionetgen.modelapi.blocks.ObservableBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Observable block object, subclass of ModelBlock.

add_observable(name, otype, patterns=[])

adds an observable by making a new Observable object and passing the args/kwargs to its initialization.

add_observable(*args, **kwargs) None[source]
class bionetgen.modelapi.blocks.ParameterBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Parameter block object, subclass of ModelBlock.

add_parameter(name, value, expr=None)

adds a parameter by making a new Parameter object and passing the args/kwargs to its initialization.

add_parameter(*args, **kwargs) None[source]
class bionetgen.modelapi.blocks.RuleBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Rule block object, subclass of ModelBlock.

add_rule(name, name, reactants=[], products=[], rate_constants=())

adds a rule by making a new Rule object and passing the args/kwargs to its initialization.


XML loading makes it so that reversible rules are split into two unidirectional rules. This find them and combines them into a single rule to correctly represent the original model rule.

add_rule(*args, **kwargs) None[source]
consolidate_rules() None[source]

Generated XML only has unidirectional rules and uses “_reverse_” tag to make bidirectional rules for NFSim. Take all the “_reverse_” tagged rules and convert them to bidirectional rules

class bionetgen.modelapi.blocks.SpeciesBlock[source]

Bases: bionetgen.modelapi.blocks.ModelBlock

Species block object, subclass of ModelBlock.

add_species(name, pattern=Pattern(), count=0)

adds a species by making a new Species object and passing the args/kwargs to its initialization.

add_species(*args, **kwargs) None[source]

bionetgen.modelapi.bngfile module

class bionetgen.modelapi.bngfile.BNGFile(path, BNGPATH='/home/docs/checkouts/')[source]

Bases: object

File object designed to deal with .bngl file manipulations.

Usage: BNGFile(bngl_path)

BNGFile(bngl_path, BNGPATH)


path to the file the object needs to deal with


list of acceptible actions


optional path to bng folder that contains


path to

generate_xml(xml_file, model_file=None)bool

takes the given BNGL file and generates a BNG-XML from it

strip_actions(model_path, folder)str

deletes actions from a given BNGL file

write_xml(open_file, xml_type=”bngxml”, bngl_str=None)bool

given a bngl file or a string, writes an SBML or BNG-XML from it

generate_xml(xml_file, model_file=None) bool[source]

generates an BNG-XML file from a given model file. Defaults to self.path if model_file is not given

strip_actions(model_path, folder) str[source]

Strips actions from a BNGL file and makes a copy into the given folder

write_xml(open_file, xml_type='bngxml', bngl_str=None) bool[source]

write new BNG-XML or SBML of file by calling again or can take BNGL string in as well.

bionetgen.modelapi.bngparser module

class bionetgen.modelapi.bngparser.BNGParser(path, BNGPATH='/home/docs/checkouts/', parse_actions=True)[source]

Bases: object

Parser object that deals with reading in the BNGL file and setting up the model object

Usage: BNGParser(bngl_path)

BNGParser(bngl_path, BNGPATH)


BNGFile object that’s responsible for .bngl file manipulations


parses the BNGL model at the given path and adds everything to a given model object


parses given xml string and adds everything to a given model object

parse_model(model_obj) None[source]

Will determine the parser route eventually and call the right parser

parse_xml(xml_str, model_obj) None[source]

bionetgen.modelapi.model module

class bionetgen.modelapi.model.bngmodel(bngl_model, BNGPATH='/home/docs/checkouts/')[source]

Bases: object

Main model object and entry point for model API. The goal of this object is to generate and read the BNGXML of a given BNGL model and give the user a pythonic interface to the resulting model object.

Usage: bngmodel(bng_model)

bngmodel(bng_model, BNGPATH)


a list of the blocks that have been parsed in the model


BNGParser object that’s responsible for .bngl file reading and model setup


name of the model, generally set from the given BNGL file


a tag to keep track if any changes have been made to the model via the XML API by the user


a list of changes the user have made to the model


resets compilation tags of each block to keep track of any changes the user makes to the model via the API

add_action(action_type, action_args)

adds the action of action_type with arguments given by the optional keyword argument action_args which is a list of lists where each element is of the form [ArgumentName, ArgumentValue]


write the model in BNGL format to the path given


sets up a simulator in bngmodel.simulator where the only current supported type of simulator is libRR for libRoadRunner simulator.

add_action(action_type, action_args=[])[source]
property changes
property recompile

Sets up a simulator attribute that is a generic front-end to all other simulators. At the moment only libroadrunner is supported


write the model to file

bionetgen.modelapi.pattern module

class bionetgen.modelapi.pattern.Component[source]

Bases: object

Component object that describes the state, label and bonding for each component. Molecules can optionally contain components


name of the component


label of the component


state of the component, not used for molecule types


list of states for molecule types


list of bond objects that describes bonding of the component


not implemented. will eventually be used to add additional states to an existing component


not implemented. will eventually be used to add additional bonds to an existing component

property bonds
property label
property name
property state
property states
class bionetgen.modelapi.pattern.Molecule(name='0', components=[], compartment=None, label=None)[source]

Bases: object

Molecule object. A pattern is a list of molecules. This object also handles molecule types where components have a list of possible states.


name of the molecule


compartment of the molecule


label of the molecule


list of components for this molecule

add_component(name, state=None, states=[])

add a component object to the list of components with name “name”, current state “state” or a list of states (for molecule types) “states”

add_component(name, state=None, states=[])[source]
property compartment
property components
property label
property name
class bionetgen.modelapi.pattern.Pattern(molecules=[], bonds=None, compartment=None, label=None)[source]

Bases: object

Pattern object. Fundamentally it’s just a list of molecules which are defined later.


setting a pattern requires you to keep track of all bonds to correctly label them, this object tracks everything


compartment of the overall pattern (not the same thing as molecule compartment, those have their own)


label of the overall pattern (not the same thing as molecule label, those have their own)


list of molecule objects that are in the pattern


used for constant species, sets “$” at the beginning of the pattern string


used for matchOnce syntax, “{MatchOnce}PatternStr”

property compartment
property label

bionetgen.modelapi.runner module, out=None, suppress=False, timeout=None)[source]

Convenience function to run as a library

Usage: run(path_to_input_file, output_folder)


this has to point to a BNGL file


(optional) this points to a folder to put the results into. If it doesn’t exist, it will be created.

bionetgen.modelapi.structs module

class bionetgen.modelapi.structs.Action(action_type=None, action_args={})[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all actions in the model, subclass of ModelObj.

In BNGL actions are of the form

action_type({arg1=>value1, arg2=>value2, …})


type of action, e.g. simulate or writeFile

argsdict[arg_name] = arg_value

action arguments as keys and their values as values

gen_string() str[source]
print_line() str[source]
class bionetgen.modelapi.structs.Compartment(name, dim, size, outside=None)[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all compartments in the model, subclass of ModelObj.

In BNGL the compartments are of the form

compartment_name dimensions size


compartment_name dimensions size parent_compartment

the second form only applies when one compartment is contained in another compartment.


name of the compartment


dimensionality of the compartment


size/volume of the compartment


parent compartment, if exists


boolean that describes if the size is a volume or an expression

gen_string() str[source]
class bionetgen.modelapi.structs.Function(name, expr, args=None)[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all functions in the model, subclass of ModelObj.

In BNGL functions are of the form

function_name function_expression


function_name = function_expression

and functions can have arguments

function_name(arg1, arg2, …, argN)


name of the function


function expression


optional list of arguments for the function

gen_string() str[source]
class bionetgen.modelapi.structs.ModelObj[source]

Bases: object

The base class for all items in a model (parameter, observable etc.).


comment at the end of the line/object


line label at the beginning of the line/object


generates the actual line string with line label and comments if applicable


generates the BNGL string of the object itself, separate from line attributes

property comment: None
property line_label: str
print_line() str[source]
class bionetgen.modelapi.structs.MoleculeType(name, components)[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all molecule types in the model, subclass of ModelObj.

In BNGL the molecule types are of the form


where all possible states of each component of a molecule is listed, e.g.

A(b, p~0~1, k~ON~OFF~NULL)


a molecule type only contains a molecule object which can also handle multiple component states

gen_string() str[source]
class bionetgen.modelapi.structs.Observable(name, otype, patterns=[])[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all observables in the model, subclass of ModelObj.

In BNGL the observables are of the form

observable_type observable_name observable_patterns

where patterns can include multiple patterns separated by commas.


name of the observable


type of the observable, Molecules or Species


list of patterns of the observable


add a Pattern object into the list of patterns for this observable

add_pattern(pat) None[source]
gen_string() str[source]
class bionetgen.modelapi.structs.Parameter(name, value, expr=None)[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all parameters in the model, subclass of ModelObj.

In BNGL parameters are of the form

parameter_name parameter_value/expression


parameter_name = parameter_value/expression


name of the parameter


value of the parameter, if loaded from XML this will always exist since NFsim needs the value and not the expression


this exists if the parameter is a math expression, not necerssary


this is a boolean that determines if the generated string has is in expression form or in value form.

gen_string() str[source]
class bionetgen.modelapi.structs.Rule(name, reactants=[], products=[], rate_constants=(), rule_mod=Rule modifier of tyoe None, operations=[])[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all rules in the model, subclass of ModelObj.


name of the rule, optional


list of patterns for reactants


list of patterns for products


modifier (moveConnected, TotalRate, etc.) used by a given rule


list of operations


sets forward and backwards rate constants, backwards rate constants are optional and if not given, will set the rule to be a unidirectional rule


given a list of patterns, return a string formatted to be on one side of a rule definition

class bionetgen.modelapi.structs.Species(pattern=, count=0)[source]

Bases: bionetgen.modelapi.structs.ModelObj

Class for all species in the model, subclass of ModelObj.

In BNGL the species/seed species are of the form

species count

where species is a single pattern and count is the starting value for that specific pattern


pattern of the seed species


starting value of the seed species

gen_string() str[source]

bionetgen.modelapi.utils module

class bionetgen.modelapi.utils.ActionList[source]

Bases: object


A simple function finds the path to from * Environment variable * Assuming it’s under PATH * Given optional path as argument

Usage: test_bngexec(path)



(optional) path to the folder that contains

bionetgen.modelapi.utils.run_command(command, suppress=False, timeout=None)[source]

A simple function that test if given runs

Usage: test_bngexec(path)


path to to test

bionetgen.modelapi.xmlparsers module

class bionetgen.modelapi.xmlparsers.BondsXML(bonds_xml=None)[source]

Bases: object

Bonds XML parser, derived from XMLObj.

class bionetgen.modelapi.xmlparsers.CompartmentBlockXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

CompartmentBlock XML parser, derived from XMLObj. Creates a CompartmentBlock that contains the compartments parsed.

class bionetgen.modelapi.xmlparsers.FunctionBlockXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

FunctionBlock XML parser, derived from XMLObj. Creates a FunctionBlock that contains the functions parsed.


parses the argument list XML and returns a list of argument names

get_arguments(xml) list[source]
class bionetgen.modelapi.xmlparsers.MoleculeTypeBlockXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

MoleculeTypeBlock XML parser, derived from XMLObj. Creates a MoleculeTypeBlock that contains the molecule types parsed.


parses the XML to create a MoleculeType object and adds it to a given MoleculeTypeBlock object

add_molecule_type_to_block(block, xml)[source]
class bionetgen.modelapi.xmlparsers.ObservableBlockXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

ObservableBlock XML parser, derived from XMLObj. Creates an ObservableBlock that contains the observables parsed.

parse_xml(xml) bionetgen.modelapi.blocks.ObservableBlock[source]
class bionetgen.modelapi.xmlparsers.Operation[source]

Bases: object

To be used for parsing & storing ListOfOperations information.

valid_ops = ['AddBond', 'DeleteBond', 'ChangeCompartment', 'StateChange', 'Add', 'Delete']
class bionetgen.modelapi.xmlparsers.ParameterBlockXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

PatternBlock XML parser, derived from XMLObj. Creates a ParameterBlock that contains the parameters parsed.

parse_xml(xml) bionetgen.modelapi.blocks.ParameterBlock[source]
class bionetgen.modelapi.xmlparsers.PatternListXML(xml)[source]

Bases: object

Pattern list XML parser. Takes a list of patterns and parses them using PatternXML and generates a list of patterns.


list of patterns parsed from the XML dict list

parse_xml(xml) list[source]
class bionetgen.modelapi.xmlparsers.PatternXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

Pattern XML parser, derived from XMLObj.


processes a molecule XML dictionary and returns a Molecule object

_process_comp(self, xml)

processes a component XML dictionary and returns a list of Compartment objects

parse_xml(xml) bionetgen.modelapi.pattern.Pattern[source]
class bionetgen.modelapi.xmlparsers.RuleBlockXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

RuleBlock XML parser, derived from XMLObj. Creates a RuleBlock that contains the rules parsed.


parses a rate law XML and returns the rate constant


parses either reactants or products XML and returns a list of Pattern objects


creates and populates a list of operations and their arguments


stores rule modifier used by a given rule as a string

class bionetgen.modelapi.xmlparsers.SpeciesBlockXML(xml)[source]

Bases: bionetgen.modelapi.xmlparsers.XMLObj

SpeciesBlock XML parser, derived from XMLObj. Creates a SpeciesBlock that contains the species parsed.

class bionetgen.modelapi.xmlparsers.XMLObj(xml)[source]

Bases: object

Base object that contains XMLs that is the parent of all XML object that will be used for BNGL blocks as we read from BNGXML. This sets up the python internals such as __repr__ and __str__ for the rest of the XML classes.

This base class assumes at least two methods will be defined by each class that inherits this base object which are defined in the methods below.


XML loaded via xmltodict to be parsed. Either a list of items or an OrderedDict.


appropriate parsed object, one of the Block objects


the method that parses the XML given and is written separately for each subclass of this one


the method to generate the string from the parsed object


Module contents