bionetgen.modelapi package

Submodules

bionetgen.modelapi.model module

class bionetgen.modelapi.model.bngmodel(bngl_model, BNGPATH='/home/docs/checkouts/readthedocs.org/user_builds/pybionetgen/checkouts/latest/bionetgen/bng-linux', generate_network=False, suppress=True)[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)

Attributes

active_blockslist[str]

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

bngparserBNGParser

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

model_namestr

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

model_pathstr

path to the model file initially given

recompilebool

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

changesdict

a list of changes the user have made to the model

Methods

reset_compilation_tags()

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 dictionary where each element is of the form “ArgumentName”:ArgumentValue

write_model(model_name)

write the model in BNGL format to the path given

setup_simulator(sim_type)

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

add_block(BlockObject)

adds a given block object (e.g. ParametersBlock) to the model

add_empty_block(block_type)

adds an empty block of type block_type to the model where block_type can be one of: “parameters”, “compartments”, “molecule_types”, “species”, “observables”, “functions”, “energy_patterns”, “population_maps”, “rules”, “reaction_rules”, “actions”.

add_action(action_type, action_args={})[source]

Adds an action to the actions block of the model object. If an actions block doesn’t exist, this will make an empty actions block and append the action to the block.

Arguments

action_type: str

the type of action being added

action_args: dict

a dictionary where the key is the argument type and the value is the value of that argument.

add_actions_block(block=None)[source]

Adds an actions block to the model object.

add_block(block)[source]

Adds the given block object to the model, uses the name of the block object to determine what block it is

add_compartments_block(block=None)[source]

Adds a compartments block to the model object.

add_empty_block(block_name)[source]

Makes an empty block object from a given block name and adds it to the model object.

add_energy_patterns_block(block=None)[source]

Adds an energy patterns block to the model object.

add_functions_block(block=None)[source]

Adds a functions block to the model object.

add_molecule_types_block(block=None)[source]

Adds a molecule types block to the model object.

add_observables_block(block=None)[source]

Adds an observable block to the model object.

add_parameters_block(block=None)[source]

Adds a parameters block to the model object.

add_population_maps_block(block=None)[source]

Adds a population maps block to the model object.

add_rules_block(block=None)[source]

Adds a rules block to the model object.

add_species_block(block=None)[source]

Adds a species block to the model object.

property changes
property recompile
reset_compilation_tags()[source]

This function resets all internal tags used for keeping track of changes done to a model after it’s loaded. Resetting these tags will remove all history of changes.

setup_simulator(sim_type='libRR')[source]

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

write_model(file_name)[source]

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

Attributes

namestr

name of the component

_labelstr

label of the component

_statestr

state of the component, not used for molecule types

_stateslist[str]

list of states for molecule types

_bondslist[Bond]

list of bond objects that describes bonding of the component

Methods

add_state()

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

add_bond()

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

add_bond()[source]
add_state()[source]
property bonds
property label
property name
print_canonical()[source]

Returns canonical label for the pattern

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.

Attributes

_namestr

name of the molecule

_compartmentstr

compartment of the molecule

_labelstr

label of the molecule

_componentslist[Component]

list of components for this molecule

Methods

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
print_canonical()[source]

Returns canonical label for the pattern

class bionetgen.modelapi.pattern.Pattern(molecules=[], bonds=None, compartment=None, label=None, canonicalize=False)[source]

Bases: object

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

Attributes

_bondsBonds

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

compartmentstr

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

_labelstr

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

moleculeslist[Molecule]

list of molecule objects that are in the pattern

fixedbool

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

MatchOncebool

used for matchOnce syntax, “{MatchOnce}PatternStr”

relationstr

alongside quantity this is only used for patterns of the form e.g. pattern==5, pattern<=3 etc

quantitystr

alongside relation this is only used for patterns of the form e.g. pattern==5, pattern<=3 etc

nautyGGraph

if canonicalization was done on the pattern this will return a graph object from the library pynauty which is just python bindings to the canonical labelling library nauty.

canonical_certificatestr

if canonicalization was done on the pattern this will return a string that has the canonical labelling for the underlying graph. this doesn’t take into account node names so it can only be used to compare the graph topology

canonical_labelstr

if canonicalization was done on the pattern this will return a canonical label for the entire molecule that’s unique and all isomorphic patterns will have the same label. comparing this label to another molecules canonical label will tell you if they are the same molecule or not.

Methods

canonicalizeNone

This method will generate a canonical label stored in canonical_label attribute. This label can be used to compare patterns to see if they are the same pattern quickly. This method will only run if pynauty is installed. See [nauty documentation](https://users.cecs.anu.edu.au/~bdm/nauty/) for more information

canonicalize()[source]

This method will use pynauty library to generate a canonical label for the pattern. This pattern will be stored in canonical_label attribute.

property compartment
consolidate_molecule_compartments()[source]
property label
print_canonical()[source]

Returns canonical label for the pattern

bionetgen.modelapi.runner module

bionetgen.modelapi.runner.run(inp, out=None, suppress=False, timeout=None)[source]

Convenience function to run BNG2.pl as a library

Usage: run(path_to_input_file, output_folder)

Arguments

path_to_input_filestr

this has to point to a BNGL file

output_folderstr

(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: ModelObj

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

In BNGL actions are of the form

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

Attributes

typestr

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: ModelObj

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

In BNGL the compartments are of the form

compartment_name dimensions size

or

compartment_name dimensions size parent_compartment

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

Attributes

namestr

name of the compartment

dimstr

dimensionality of the compartment

sizestr

size/volume of the compartment

outsidestr

parent compartment, if exists

write_exprbool

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

gen_string() str[source]
class bionetgen.modelapi.structs.EnergyPattern(name, pattern, expression)[source]

Bases: ModelObj

Class for all energy patterns in the model, subclass of ModelObj.

In BNGL the energy patterns are of the form

EP_pattern EP_expression

Attributes

namestr

id of the energy pattern

patternPattern

Pattern object representing the energy pattern

expressionstr

expression used for energy pattern

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

Bases: ModelObj

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

In BNGL functions are of the form

function_name function_expression

or

function_name = function_expression

and functions can have arguments

function_name(arg1, arg2, …, argN)

Attributes

namestr

name of the function

exprstr

function expression

argslist

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.).

Attributes

commentstr

comment at the end of the line/object

line_labelstr

line label at the beginning of the line/object

Methods

print_line()

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

gen_string()

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: ModelObj

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

In BNGL the molecule types are of the form

molecule_type

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

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

Attributes

moleculeMolecule

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: 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.

Attributes

namestr

name of the observable

typestr

type of the observable, Molecules or Species

patternslist[Pattern]

list of patterns of the observable

Methods

add_pattern

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: ModelObj

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

In BNGL parameters are of the form

parameter_name parameter_value/expression

or

parameter_name = parameter_value/expression

Attributes

namestr

name of the parameter

valuestr

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

exprstr

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

write_exprbool

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.PopulationMap(name, struct_species, pop_species, rate)[source]

Bases: ModelObj

Class for all population maps in the model, subclass of ModelObj.

In BNGL the population maps are of the form

structured_species -> population_species lumping_parameter

Attributes

namestr

id of the population map

struct_speciesPattern

Pattern object representing the species to be mapped

pop_speciesPattern

Pattern object representing the population count

ratestr

lumping parameter used in population mapping

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

Bases: ModelObj

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

Attributes

namestr

name of the rule, optional

reactantslist[Pattern]

list of patterns for reactants

productslist[Pattern]

list of patterns for products

rule_modRuleMod

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

operationslist[Operation]

list of operations

Methods

set_rate_constants((k_fwd,k_bck))

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

side_string(list[Pattern])

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

gen_string()[source]
set_rate_constants(rate_cts)[source]
side_string(patterns)[source]
class bionetgen.modelapi.structs.Species(pattern=, count=0)[source]

Bases: 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

Attributes

patternPattern

pattern of the seed species

countstr

starting value of the seed species

gen_string() str[source]

bionetgen.modelapi.utils module

bionetgen.modelapi.xmlparsers module

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

Bases: object

Bonds XML parser, derived from XMLObj.

get_bond_id(comp)[source]
get_tpl_from_id(id_str)[source]
resolve_xml(bonds_xml)[source]
set_xml(bonds_xml)[source]
tpls_from_bond(bond)[source]
class bionetgen.modelapi.xmlparsers.CompartmentBlockXML(xml)[source]

Bases: XMLObj

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

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

Bases: XMLObj

EnergyPatternBlock XML parser, derived from XMLObj. Creates an EnergyPatternBlock that contains the energy patterns parsed.

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

Bases: XMLObj

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

Methods

get_arguments(xml)

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

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

Bases: XMLObj

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

Methods

add_molecule_type_to_block(block,xml)

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

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

Bases: XMLObj

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

parse_xml(xml) 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: XMLObj

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

parse_xml(xml) 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.

Attributes

patternslist[Pattern]

list of patterns parsed from the XML dict list

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

Bases: XMLObj

Pattern XML parser, derived from XMLObj.

Methods

_process_mol(xml)

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) Pattern[source]
class bionetgen.modelapi.xmlparsers.PopulationMapBlockXML(xml)[source]

Bases: XMLObj

PopulationMapBlock XML parser, derived from XMLObj. Creates a PopulationMapBlock that contains the population maps parsed.

Methods

resolve_ratelaw(xml)

parses a rate law XML and returns the rate constant

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

Bases: XMLObj

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

Methods

resolve_ratelaw(xml)

parses a rate law XML and returns the rate constant

resolve_rxn_side(xml)

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

get_operations(xml)

creates and populates a list of operations and their arguments

get_rule_mod(xml)

stores rule modifier used by a given rule as a string

get_operations(xml)[source]
get_rule_mod(xml)[source]
parse_xml(xml)[source]
resolve_ratelaw(xml)[source]
resolve_rxn_side(xml)[source]
class bionetgen.modelapi.xmlparsers.SpeciesBlockXML(xml)[source]

Bases: XMLObj

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

parse_xml(xml)[source]
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.

Attributes

xmllist/OrderedDict

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

parsed_objobj

appropriate parsed object, one of the Block objects

Methods

resolve_xml(xml)

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

gen_string()

the method to generate the string from the parsed object

gen_string()[source]
parse_xml(xml)[source]

Module contents