def bngl_import_compare_nfsim(bng_file): m = model_from_bngl(bng_file) BNG_SEED = 123 # Simulate using the BNGL file directly with BngFileInterface(model=None) as bng: bng.action('readFile', file=bng_file, skip_actions=1) bng.action('simulate', method='nf', n_steps=10, t_end=100, seed=BNG_SEED) bng.execute() yfull1 = bng.read_simulation_results() # Convert to a PySB model, then simulate using BNG with BngFileInterface(model=m) as bng: bng.action('simulate', method='nf', n_steps=10, t_end=100, seed=BNG_SEED) bng.execute() yfull2 = bng.read_simulation_results() # Check all species trajectories are equal (within numerical tolerance) for i in range(len(m.observables)): print(i) print(yfull1[i]) print(yfull2[i]) print(yfull1[i] == yfull2[i]) assert yfull1[i] == yfull2[i]
def bngl_import_compare_simulations(bng_file, force=False, sim_times=range(0, 100, 10)): """ Test BNGL file import by running an ODE simulation on the imported model and on the BNGL file directly to compare trajectories. """ m = model_from_bngl(bng_file, force=force) # Simulate using the BNGL file directly with BngConsole(model=None, suppress_warnings=True) as bng: bng.load_bngl(bng_file) bng.generate_network() bng.action('simulate', method='ode', sample_times=sim_times) yfull1 = bng.read_simulation_results() # Convert to a PySB model, then simulate using BNG with BngConsole(m, suppress_warnings=True) as bng: bng.generate_network() bng.action('simulate', method='ode', sample_times=sim_times) yfull2 = bng.read_simulation_results() # Check all species trajectories are equal (within numerical tolerance) for species in m.species: print(species) print(yfull1[species]) print(yfull2[species]) print(numpy.allclose(yfull1[species], yfull2[species], atol=1e-8, rtol=1e-8)) assert numpy.allclose(yfull1[species], yfull2[species], atol=1e-8, rtol=1e-8)
def model_from_sbml(filename, force=False, cleanup=True, **kwargs): """ Create a PySB Model object from an Systems Biology Markup Language (SBML) file, using BioNetGen's `sbmlTranslator <http://bionetgen.org/index.php/SBML2BNGL>`_, which can attempt to extrapolate higher-level (rule-based) structure from an SBML source file (argument atomize=True). The model is first converted into BioNetGen language by sbmlTranslator, then PySB's :class:`BnglBuilder` class converts the BioNetGen language model into a PySB Model. Notes ----- Requires the sbmlTranslator program (also known at Atomizer). If PySB was installed using "conda", you can install sbmlTranslator using "conda install -c alubbock atomizer". It is bundled with BioNetGen if BNG is installed by manual download and unzip. Read the `sbmlTranslator documentation <http://bionetgen.org/index.php/SBML2BNGL>`_ for further information on sbmlTranslator's limitations. Parameters ---------- filename : A Systems Biology Markup Language .sbml file force : bool, optional The default, False, will raise an Exception if there are any errors importing the model to PySB, e.g. due to unsupported features. Setting to True will attempt to ignore any import errors, which may lead to a model that only poorly represents the original. Use at own risk! cleanup : bool Delete temporary directory on completion if True. Set to False for debugging purposes. **kwargs: kwargs Keyword arguments to pass on to :func:`sbml_translator` """ logger = get_logger(__name__, log_level=kwargs.get('verbose')) tmpdir = tempfile.mkdtemp() logger.debug("Performing SBML to BNGL translation in temporary " "directory %s" % tmpdir) try: bngl_file = os.path.join(tmpdir, 'model.bngl') sbml_translator(filename, bngl_file, **kwargs) return model_from_bngl(bngl_file, force=force, cleanup=cleanup) finally: if cleanup: shutil.rmtree(tmpdir)
def bngl_import_compare_simulations(bng_file, force=False, precision=1e-12, sim_times=range(0, 100, 10)): """ Test BNGL file import by running an ODE simulation on the imported model and on the BNGL file directly to compare trajectories. """ m = model_from_bngl(bng_file, force=force) if sim_times is None: # Skip simulation check return # Simulate using the BNGL file directly with BngFileInterface(model=None) as bng: bng.action('readFile', file=bng_file, skip_actions=1) bng.action('generate_network') bng.action('simulate', method='ode', sample_times=sim_times) bng.execute() yfull1 = bng.read_simulation_results() # Convert to a PySB model, then simulate using BNG with BngFileInterface(model=m) as bng: bng.action('generate_network') bng.action('simulate', method='ode', sample_times=sim_times) bng.execute() yfull2 = bng.read_simulation_results() # Don't check trajectories on forced examples if force: return # Check all species trajectories are equal (within numerical tolerance) assert len(yfull1.dtype.names) == len(yfull2.dtype.names) for species in yfull1.dtype.names: logger.debug(species) logger.debug(yfull1[species]) if species in yfull2.dtype.names: renamed_species = species else: renamed_species = 'Obs_{}'.format(species) logger.debug(yfull2[renamed_species]) assert numpy.allclose(yfull1[species], yfull2[renamed_species], atol=precision, rtol=precision)
def model_from_sbml(filename, force=False, cleanup=True, **kwargs): """ Create a PySB Model object from an Systems Biology Markup Language (SBML) file, using BioNetGen's `sbmlTranslator <http://bionetgen.org/index.php/SBML2BNGL>`_, which can attempt to extrapolate higher-level (rule-based) structure from an SBML source file (argument atomize=True). The model is first converted into BioNetGen language by sbmlTranslator, then PySB's :class:`BnglBuilder` class converts the BioNetGen language model into a PySB Model. Limitations ----------- Read the `sbmlTranslator documentation <http://bionetgen.org/index.php/SBML2BNGL>`_ for further information on sbmlTranslator's limitations. Parameters ---------- filename : A Systems Biology Markup Language .sbml file force : bool, optional The default, False, will raise an Exception if there are any errors importing the model to PySB, e.g. due to unsupported features. Setting to True will attempt to ignore any import errors, which may lead to a model that only poorly represents the original. Use at own risk! cleanup : bool Delete temporary directory on completion if True. Set to False for debugging purposes. **kwargs: kwargs Keyword arguments to pass on to :func:`SbmlBuilder.sbml_translator` """ tmpdir = tempfile.mkdtemp() verbose = kwargs.get('verbose', False) if verbose: print("Performing SBML to BNGL translation in temporary " "directory %s" % tmpdir) try: bngl_file = os.path.join(tmpdir, 'model.bngl') sbml_translator(filename, bngl_file, **kwargs) return model_from_bngl(bngl_file, force=force) finally: if cleanup: shutil.rmtree(tmpdir)
def bngl_import_compare_simulations(bng_file, force=False, sim_times=range(0, 100, 10)): """ Test BNGL file import by running an ODE simulation on the imported model and on the BNGL file directly to compare trajectories. """ m = model_from_bngl(bng_file, force=force) # Simulate using the BNGL file directly with BngFileInterface(model=None) as bng: bng.action('readFile', file=bng_file, skip_actions=1) bng.action('generate_network') bng.action('simulate', method='ode', sample_times=sim_times) bng.execute() yfull1 = bng.read_simulation_results() # Convert to a PySB model, then simulate using BNG with BngFileInterface(model=m) as bng: bng.action('generate_network') bng.action('simulate', method='ode', sample_times=sim_times) bng.execute() yfull2 = bng.read_simulation_results() # Check all species trajectories are equal (within numerical tolerance) for species in m.species: print(species) print(yfull1[species]) print(yfull2[species]) print( numpy.allclose(yfull1[species], yfull2[species], atol=1e-8, rtol=1e-8)) assert numpy.allclose(yfull1[species], yfull2[species], atol=1e-8, rtol=1e-8)
def atom_rules_view(model, visualize_args, rule_name=None, verbose=False, cleanup=True, layout_name='fcose'): """ Uses the BioNetGen atom-rules to visualize large rule-base models. For more information regarding atom-rules and its parameters please visit: Sekar et al (2017), Automated visualization of rule-based models https://doi.org/10.1371/journal.pcbi.1005857 The visualize_args parameter contains all the arguments that will be passed to the BioNetGen visualize function. It is a dictionary and supports the following key, value pairs. - `type` * `conventional` => Conventional rule visualization * `compact` => Compact rule visualization (using graph operation nodes) * `regulatory` => Rule-derived regulatory graph * `opts` => Options template for regulatory graph * `contactmap` => Contact map * `reaction_network` => Reaction network - `suffix` * str => add suffix string to output filename - `each` * 1 => Show all rules in separate GML files * 0 => Show all rules the same GML file. - `opts` * file path => import options from file - `background` * 1 => Enable background * 0 => Disable background - `groups` * 1 => Enable groups * 0 => Disable groups - `collapse` * 1 => Enable collapsing of groups * 0 => Disable collapsing of groups - `ruleNames` * 1 => Enable display of rule names * 0 => Disable display of rule names - `doNotUseContextWhenGrouping` * 1 => Use permissive edge signature * 0 => Use strict edge signature - `doNotCollapseEdges`: * 1 => When collapsing nodes, retain duplicate edges * 0 => When collapsing nodes, remove duplicate edges Parameters ---------- model: pysb.model or bngl file Model to visualize visualize_args: dict Contains all the arguments that will be passed to the BioNetGen visualize function. The following key, value pairs are available rule_name : str Name of the rule to visualize, when `each` is set to 1 in visualize_args. cleanup : bool, optional If True (default), delete the temporary files after the simulation is finished. If False, leave them in place. Useful for debugging. verbose : bool 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. Returns ------- """ from pyvipr.pysb_viz.static_viz import PysbStaticViz from pysb.core import Model from pysb.importers.bngl import model_from_bngl if isinstance(model, Model): pviz = PysbStaticViz(model, generate_eqs=False) elif isinstance(model, str): pysb_model = model_from_bngl(model) pviz = PysbStaticViz(pysb_model, generate_eqs=False) else: raise TypeError('Only PySB and bngl models are supported') data = pviz.atom_rules_view(visualize_args, rule_name, verbose, cleanup) return Viz(data=data, type_of_viz='', layout_name=layout_name)
def check_convert(model, format): """ Test exporters run without error """ exported_file = None try: if format == 'json': exported_file = JsonExporter(model).export(include_netgen=True) else: exported_file = export.export(model, format) except export.ExpressionsNotSupported: pass except export.CompartmentsNotSupported: pass except export.LocalFunctionsNotSupported: pass except Exception as e: # Some example models are deliberately incomplete, so here we # will treat any of these "expected" exceptions as a success. exception_class = expected_exceptions.get(base_name(model)) if not exception_class or not isinstance(e, exception_class): raise if exported_file is not None: if format == 'python': # linspace arguments picked to avoid VODE warning exec( exported_file + 'Model().simulate(tspan=numpy.linspace(0,1,501))\n', {'_use_inline': False}) elif format == 'pysb_flat': exec(exported_file, {'__name__': model.name}) elif format == 'sbml': # Skip the simulation comparison if roadrunner not available if roadrunner is None: raise SkipTest( "SBML Simulation test skipped (requires roadrunner)") roadrunner.Logger.setLevel(roadrunner.Logger.LOG_ERROR) # Simulate SBML using roadrunner rr = roadrunner.RoadRunner(exported_file) rr.timeCourseSelections = \ ['__s{}'.format(i) for i in range(len(model.species))] + \ ['__obs{}'.format(i) for i in range(len(model.observables))] rr_result = rr.simulate(0, 10, 100) # Simulate original using PySB df = ScipyOdeSimulator(model).run( tspan=np.linspace(0, 10, 100)).dataframe # Compare species' trajectories for sp_idx in range(len(model.species)): rr_sp = rr_result[:, sp_idx] py_sp = df.iloc[:, sp_idx] is_close = np.allclose(rr_sp, py_sp, rtol=1e-4) if not is_close: print(pd.DataFrame(dict(rr=rr_sp, pysb=py_sp))) raise ValueError( 'Model {}, species __s{} trajectories do not match:'. format(model.name, sp_idx)) # Compare observables' trajectories for obs_idx in range(len(model.observables)): rr_obs = rr_result[:, obs_idx + len(model.species)] py_obs = df.iloc[:, obs_idx + len(model.species)] is_close = np.allclose(rr_obs, py_obs, rtol=1e-4) if not is_close: print(pd.DataFrame(dict(rr=rr_obs, pysb=py_obs))) raise ValueError( 'Model {}, observable__o{} "{}" trajectories do not match:' .format(model.name, obs_idx, model.observables[obs_idx].name)) elif format == 'json': # Round-trip the model by re-importing the JSON m = model_from_json(exported_file) # Check network generation and force RHS evaluation if model.name not in ('pysb.examples.tutorial_b', 'pysb.examples.tutorial_c'): ScipyOdeSimulator(m, compiler='cython') if sys.version_info.major >= 3: # Only check on Python 3 to avoid string-to-unicode encoding # issues check_model_against_component_list(m, model.all_components()) elif format == 'bngl': if model.name.endswith('tutorial_b') or \ model.name.endswith('tutorial_c'): # Models have no rules return with tempfile.NamedTemporaryFile(suffix='.bngl', delete=False) as tf: tf.write(exported_file.encode('utf8')) # Cannot have two simultaneous file handled on Windows tf.close() try: m = model_from_bngl(tf.name) # Generate network and force RHS evaluation ScipyOdeSimulator(m, compiler='cython') finally: os.unlink(tf.name)
def data_to_json(value, widget): """ Generate a json file from the data passed to the widget Parameters ---------- value: pysb.Model, pysb.SimulationResult, str Value passed to the widget that is going to be visualized widget: Widget Widget instance Returns ------- """ if is_pysb_model(value): from pyvipr.pysb_viz.static_viz import PysbStaticViz viz = PysbStaticViz(value) jsondata = static_data(viz, widget) return jsondata elif is_pysb_sim(value): from pyvipr.pysb_viz.dynamic_viz import PysbDynamicViz viz = PysbDynamicViz(value, widget.sim_idx, widget.cmap) jsondata = dynamic_data(viz, widget) return jsondata elif isinstance(value, str): file_extension = os.path.splitext(value)[1] if file_extension in ['.bngl', '.sbml', '.xml'] and widget.type_of_viz != 'sbgn_xml'\ or value.startswith('BIOMD'): try: from pysb.importers.sbml import model_from_sbml, model_from_biomodels from pysb.importers.bngl import model_from_bngl except ImportError: raise Exception( 'PySB must be installed to visualize models from files') from pyvipr.pysb_viz.static_viz import PysbStaticViz if file_extension == '.bngl': model = model_from_bngl(value) elif file_extension in ['.sbml', '.xml']: model = model_from_sbml(value) elif value.startswith('BIOMD'): model = model_from_biomodels(value) viz = PysbStaticViz(model) jsondata = static_data(viz, widget) return jsondata elif file_extension == '.graphml' or widget.type_of_viz == 'sbgn_xml': with open(value, 'r') as file: data = file.read().replace('\n', '') return data elif file_extension == '.json': import json with open(value, 'r') as file: data = file.read().replace('\n', '') jsondata = json.loads(data) return jsondata elif file_extension == '.sif': with open(value, 'r') as file: data = file.read() data = data.rstrip('\n') return data # elif file_extension == '.ka': # subprocess.run(['truml', '-k', value]) # bngl_model_path = re.sub('ka', 'bngl', value) # model = model_from_bngl(bngl_model_path) # os.remove(bngl_model_path) else: raise ValueError('Format not supported') elif is_tellurium_model(value): if widget.type_of_viz.startswith('dynamic'): from pyvipr.tellurium_viz.dynamic_viz import TelluriumDynamicViz viz = TelluriumDynamicViz(value, widget.cmap) jsondata = dynamic_data(viz, widget) else: from pyvipr.tellurium_viz.static_viz import TelluriumStaticViz viz = TelluriumStaticViz(value) jsondata = static_data(viz, widget) return jsondata elif isinstance( value, (nx.DiGraph, nx.Graph, nx.MultiDiGraph, nx.MultiGraph, dict)): from pyvipr.network_viz.network_viz import NetworkViz viz = NetworkViz(value) if widget.type_of_viz: jsondata = getattr(viz, widget.type_of_viz)() else: jsondata = value return jsondata elif is_ecell_model(value): try: from pysb.importers.sbml import model_from_sbml except ImportError: raise Exception( 'PySB must be installed to visualize models from files') from pyvipr.pysb_viz.static_viz import PysbStaticViz import tempfile ecell4 = sys.modules['ecell4'] f_sbml = tempfile.NamedTemporaryFile(suffix='.sbml') # In ecell4 species don't have initial conditions as attributes. Hence, the # initial conditions are passed as a dictionary to the save_sbml function. # If no initial conditions are passed ecell4 sets the initial condition of the # species to 0, and PySB throws an error when the initial condition of all the species # are zero. For visualization purposes we then set the initial conditions to 1. y0 = {sp.serial(): 1 for sp in value.list_species()} ecell4.util.ports.save_sbml(f_sbml.name, value, y0=y0) model = model_from_sbml(f_sbml.name) viz = PysbStaticViz(model) jsondata = static_data(viz, widget) return jsondata elif is_pysces_model(value): try: from pysb.importers.sbml import model_from_sbml except ImportError: raise Exception( 'PySB must be installed to visualize models from files') from pyvipr.pysb_viz.static_viz import PysbStaticViz import tempfile # Note: Importing a pysces model to sbml doesn't work in python 3.7 pysces = sys.modules['pysces'] f_sbml = tempfile.NamedTemporaryFile(suffix='.xml') pysces.interface.writeMod2SBML(value, f_sbml.name) model = model_from_sbml(f_sbml.name) viz = PysbStaticViz(model) jsondata = static_data(viz, widget) return jsondata else: raise TypeError( 'Only pysb Model, pysb SimulationResult, tellurium Model, ' 'PySCeS Model, and networkx graphs are supported')