def test_unnamed_parameters(self): """ If there's a parameter without a name, we should use its id as a name instead. """ model = SBML() model.load_file("input/goodwin3.xml") params = model.get_all_param() self.assertEqual(len(params), 4)
def run_task (model_file, priors_file, experiment_file, \ iterations_phase1, sigma_update_n, iterations_phase2, \ iterations_phase3, nof_process, signetms_path, seed=42): # pylint: disable=reimported import time start_time = time.time() # importing local modules... sys.path.insert(0, signetms_path) from model.SBML import SBML from experiment.ExperimentSet import ExperimentSet from model.SBMLtoODES import sbml_to_odes from marginal_likelihood.MarginalLikelihood \ import MarginalLikelihood from model.PriorsReader import define_sbml_params_priors import seed_manager # Now the actual code... seed_manager.set_seed(seed) sbml = SBML() sbml.load_file(model_file) odes = sbml_to_odes(sbml) experiments = ExperimentSet(experiment_file) theta_priors = define_sbml_params_priors(sbml, priors_file) ml = MarginalLikelihood (iterations_phase1, sigma_update_n, iterations_phase2, iterations_phase3, 20, 2, \ verbose=False, n_process=nof_process) log_l = ml.estimate_marginal_likelihood(experiments, odes, theta_priors) elapsed_time = time.time() - start_time return log_l, elapsed_time
def setUp (self): sbml = SBML () sbml.load_file ('input/simple_enzymatic.xml') self.__model = sbml_to_odes (sbml) self.__experiments = ExperimentSet ('input/simple_enzymatic.data') self.__theta_priors = define_sbml_params_priors (sbml, 'input/simple_enzymatic.priors')
def test_priors_fully_defined(self): """ Tests if priors file define priors for all parameters of a model. """ model = SBML() model.load_file('input/model1.xml') self.assertRaises(ValueError, define_sbml_params_priors, model, 'input/incomplete.priors')
def test_no_spurious_parameter(self): """ Tests if warning is showed whenever the priors file define some parameter that is not present on the sbml parameters. """ model = SBML() model.load_file('input/model1.xml') self.assertWarns (Warning, define_sbml_params_priors, model, \ 'input/overcomplete.priors')
def test_get_all_reaction(self): """ Tests if one can get all Reactions from an SBML model. """ model = SBML() model.load_file("input/model1_bioinformatics.xml") gotten_reac_ids = [r.id for r in model.get_all_reactions()] reac_ids = ["reaction_0", "reaction_1", "reaction_2", \ "reaction_3"] for reac_id in reac_ids: assert reac_id in gotten_reac_ids
def test_definition_of_sbml_parameters(self): """ Tests if the priors have the correct parameter names of the sbml model. """ model = SBML() model.load_file("input/model1.xml") theta = define_sbml_params_priors(model, 'input/model1.priors') sbml_parameters = model.get_all_param() for t in theta.get_model_parameters(): assert (t.name in sbml_parameters)
def test_with_sbml(self): """ Tests if the module can define the prior of sbml parameters. """ model = SBML() model.load_file("input/model1.xml") theta = define_sbml_params_priors(model, 'input/model1.priors') self.assertEqual(theta.get_size() - 1, len(model.get_all_param())) sigma = theta.get_experimental_error() assert (sigma > 0)
def test_remove_reaction(self): """ Tests if it is possible to remove a reaction from an SBML model. """ model = SBML() model.load_file("input/model1_bioinformatics.xml") model.remove_reaction("reaction_0") removed_formula = "compartment * k1 * S" all_formula = model.get_all_reaction_formulas() assert (removed_formula not in all_formula)
def test_sbml_to_odes (self): """ Tests if you can create an ODES object from an SBML model. """ model = SBML () model.load_file ("input/model2.xml") odes = sbml_to_odes (model) t = [0, .1, .2] y = odes.evaluate_on ([0]) self.assertListEqual (y["EGF"], [0]) self.assertListEqual (y["ERK"], [10000]) t = [0, .1, .2] y = odes.evaluate_on (t)
def test_get_copy(self): """ Tests if one can copy the SBML object. """ model = SBML() copy = model.get_copy() self.assertEqual(len(copy.get_species_list()), 0) model.load_file("input/model1_bioinformatics.xml") copy = model.get_copy() self.assertEqual(len(copy.get_species_list()), 5) self.assertEqual(copy.get_initial_concentration('S'), 1) self.assertEqual(copy.get_initial_concentration('dS'), 0) self.assertEqual(copy.get_initial_concentration('R'), 1) self.assertEqual(copy.get_initial_concentration('RS'), 0) self.assertEqual(copy.get_initial_concentration('Rpp'), 0)
def test_get_original_name_with_id(self): """ If there's a parameter without a name, when you call get_original_param_name you should get the parameter's id. """ model = SBML() model.load_file("input/goodwin3.xml") params = model.get_all_param() names = [] for p in params: original_name = model.get_original_param_name(p) names.append(original_name) assert ("m" in names) assert ("k1" in names) assert ("k2" in names) assert ("k3" in names)
def test_add_reaction_with_former_unused_species(self): """ Tests if it we can add a new reaction using a species that was already defined in the model but was never present in any reaction. """ model = SBML() model.load_file("input/model_unused_species.xml") parameters = [{ "name": "kcat", "value": .5 }, { "name": "Km", "value": 5 }] new_reaction = Reaction (\ "unused --Rpp--> S", ["unused"], ["S"], ["Rpp"], \ parameters, "kcat * Rpp * unused / (Km + unused)") nof_species_before = len(model.get_species_list()) model.add_reaction(new_reaction) nof_species_after = len(model.get_species_list()) self.assertEqual(nof_species_after, nof_species_before)
def perform_marginal_likelihood (sbml_file, priors_file, \ experiment_file, burnin1_iterations, sigma_update_n, \ burnin2_iterations, sampling_iterations, verbose=False, \ n_process=0, sample_output_file=None, seed=0): print ("Performing marginal likelihood calculations of model: " + \ sbml_file) sbml = SBML() sbml.load_file(sbml_file) odes = sbml_to_odes(sbml) experiments = ExperimentSet(experiment_file) theta_priors = define_sbml_params_priors(sbml, priors_file) seed_manager.set_seed(seed) ml = MarginalLikelihood (burnin1_iterations, sigma_update_n, burnin2_iterations, sampling_iterations, 20, 2, \ verbose=verbose, n_process=n_process) log_l = ml.estimate_marginal_likelihood(experiments, odes, theta_priors) ml.print_sample(output_file=sample_output_file) print("log_l = " + str(log_l)) return log_l
def test_add_reaction(self): """ Tests if it is possible to add a reaction to an SBML model. """ model = SBML() model.load_file("input/model1_bioinformatics.xml") parameters = [{ "name": "kcat", "value": .5 }, { "name": "Km", "value": 5 }] new_reaction = Reaction("R --dS--> Rpp", ["R"], ["Rpp"], ["dS"], parameters, "kcat * dS * R / (Km + R)") model.add_reaction(new_reaction) all_formula = model.get_all_reaction_formulas() assert (new_reaction.formula in all_formula) # new products/reactants model = SBML() model.load_file("input/model1_bioinformatics.xml") new_reaction = Reaction("Dummy ---> Dummy2", ["Dummy"], ["Dummy2"], [], parameters, "kcat * Dummy / (Km + Dummy)") model.add_reaction(new_reaction) all_formula = model.get_all_reaction_formulas() assert (new_reaction.formula in all_formula) # new modifiers model = SBML() model.load_file("input/model1_bioinformatics.xml") new_reaction = Reaction("R --Dummy--> Rpp", ["R"], ["Rpp"], ["Dummy"], parameters, "kcat * Dummy * R / (Km + R)") model.add_reaction(new_reaction) all_formula = model.get_all_reaction_formulas() assert (new_reaction.formula in all_formula)
class TestSBMLMethods(unittest.TestCase): def setUp(self): self.model = SBML() self.model.load_file("input/model1.xml") def test_SBML_parsing(self): """ Tests if SBML model can store the model name. """ self.assertEqual("Model 1: One branch", self.model.get_name()) def test_species_list(self): """ Tests if SBML stores the list of species in the model. """ species_list = self.model.get_species_list() self.assertEqual(27, len(species_list)) def test_species_kinetic_law(self): """ Tests if SBML can return the equation of rate change of a species. """ law = self.model.get_species_kinetic_law("unboundEGFR") m1 = re.search(r'\- (([A-z]|_)\w*) \* EGF', law) m2 = re.search(r'\+ (([A-z]|_)\w*) \* boundEGF', law) self.assertGreater(len(m1.groups()), 0) self.assertGreater(len(m2.groups()), 0) p1_str = m1.group(1) p2_str = m2.group(1) p1 = self.model.get_param_value(p1_str) p2 = self.model.get_param_value(p2_str) #eq = '- 1.6665105 * EGF * unboundEGFR + 687.15641 * boundEGFR' self.assertEqual(p1, 1.6665105) self.assertEqual(p2, 687.15641) def test_lambdas_on_laws(self): """ Tests if it is possible to use functions on the kinetic laws. """ self.model = SBML() self.model.load_file("input/lambda_model.xml") law = self.model.get_species_kinetic_law("R") assert ("EXPLE" not in law) def test_species_initial_concentration(self): """ Tests if SBML can return the initial concentration of a species """ c = self.model.get_initial_concentration("EGF") self.assertEqual(c, 1000) c = self.model.get_initial_concentration("MEK") self.assertEqual(c, 3000) def test_species_initial_amount(self): """ Tests if SBML can return the initial concentration of a species even whe it was defined as initial amount. """ self.model = SBML() self.model.load_file("input/simple_enzymatic.xml") c = self.model.get_initial_concentration("E") self.assertEqual(c, 10) c = self.model.get_initial_concentration("S") self.assertEqual(c, 100) def test_parameter_consistency(self): """ Tests if parameters from reactions are consistently used on the equations of rate change """ law = self.model.get_species_kinetic_law("unboundEGFR") m1 = re.search(r'\- (([A-z]|_)\w*) \* EGF', law) p1_str = m1.group(1) law2 = self.model.get_species_kinetic_law("boundEGFR") m2 = re.search(r'\+ (([A-z]|_)\w*) \* EGF', law) p2_str = m1.group(1) self.assertEqual(p1_str, p2_str) self.assertEqual(self.model.get_param_value(p1_str), self.model.get_param_value(p2_str)) def test_get_original_parameter_name(self): """ Given a parameter of the our sbml model, we should be able to know its original name on the sbml file. """ law = self.model.get_species_kinetic_law("EGF") m = re.search(r'\- (([A-z]|_)\w*) \* EGF \* unboundEGFR', law) p_str = m.group(1) original_name = self.model.get_original_param_name(p_str) self.assertEqual(original_name, "k1") law = self.model.get_species_kinetic_law("inactiveSos") m = re.search(r'\- (([A-z]|_)\w*) \* ERKPP \* inactiveSos', law) p_str = m.group(1) original_name = self.model.get_original_param_name(p_str) self.assertEqual(original_name, "SosKcat") def test_rate_when_reactant_and_product(self): """ If a chemical species is both reactant and product of a non-reversible reaction, then the reaction shouldn't contribute to the derivative of the concentration of the species. """ law = self.model.get_species_kinetic_law("ERKPP") self.assertFalse(re.search("activeSos", law)) def test_unnamed_parameters(self): """ If there's a parameter without a name, we should use its id as a name instead. """ model = SBML() model.load_file("input/goodwin3.xml") params = model.get_all_param() self.assertEqual(len(params), 4) def test_get_original_name_with_id(self): """ If there's a parameter without a name, when you call get_original_param_name you should get the parameter's id. """ model = SBML() model.load_file("input/goodwin3.xml") params = model.get_all_param() names = [] for p in params: original_name = model.get_original_param_name(p) names.append(original_name) assert ("m" in names) assert ("k1" in names) assert ("k2" in names) assert ("k3" in names) def test_add_reaction(self): """ Tests if it is possible to add a reaction to an SBML model. """ model = SBML() model.load_file("input/model1_bioinformatics.xml") parameters = [{ "name": "kcat", "value": .5 }, { "name": "Km", "value": 5 }] new_reaction = Reaction("R --dS--> Rpp", ["R"], ["Rpp"], ["dS"], parameters, "kcat * dS * R / (Km + R)") model.add_reaction(new_reaction) all_formula = model.get_all_reaction_formulas() assert (new_reaction.formula in all_formula) # new products/reactants model = SBML() model.load_file("input/model1_bioinformatics.xml") new_reaction = Reaction("Dummy ---> Dummy2", ["Dummy"], ["Dummy2"], [], parameters, "kcat * Dummy / (Km + Dummy)") model.add_reaction(new_reaction) all_formula = model.get_all_reaction_formulas() assert (new_reaction.formula in all_formula) # new modifiers model = SBML() model.load_file("input/model1_bioinformatics.xml") new_reaction = Reaction("R --Dummy--> Rpp", ["R"], ["Rpp"], ["Dummy"], parameters, "kcat * Dummy * R / (Km + R)") model.add_reaction(new_reaction) all_formula = model.get_all_reaction_formulas() assert (new_reaction.formula in all_formula) def test_add_reaction_with_former_unused_species(self): """ Tests if it we can add a new reaction using a species that was already defined in the model but was never present in any reaction. """ model = SBML() model.load_file("input/model_unused_species.xml") parameters = [{ "name": "kcat", "value": .5 }, { "name": "Km", "value": 5 }] new_reaction = Reaction (\ "unused --Rpp--> S", ["unused"], ["S"], ["Rpp"], \ parameters, "kcat * Rpp * unused / (Km + unused)") nof_species_before = len(model.get_species_list()) model.add_reaction(new_reaction) nof_species_after = len(model.get_species_list()) self.assertEqual(nof_species_after, nof_species_before) def test_remove_reaction(self): """ Tests if it is possible to remove a reaction from an SBML model. """ model = SBML() model.load_file("input/model1_bioinformatics.xml") model.remove_reaction("reaction_0") removed_formula = "compartment * k1 * S" all_formula = model.get_all_reaction_formulas() assert (removed_formula not in all_formula) def test_get_all_reaction(self): """ Tests if one can get all Reactions from an SBML model. """ model = SBML() model.load_file("input/model1_bioinformatics.xml") gotten_reac_ids = [r.id for r in model.get_all_reactions()] reac_ids = ["reaction_0", "reaction_1", "reaction_2", \ "reaction_3"] for reac_id in reac_ids: assert reac_id in gotten_reac_ids def test_get_copy(self): """ Tests if one can copy the SBML object. """ model = SBML() copy = model.get_copy() self.assertEqual(len(copy.get_species_list()), 0) model.load_file("input/model1_bioinformatics.xml") copy = model.get_copy() self.assertEqual(len(copy.get_species_list()), 5) self.assertEqual(copy.get_initial_concentration('S'), 1) self.assertEqual(copy.get_initial_concentration('dS'), 0) self.assertEqual(copy.get_initial_concentration('R'), 1) self.assertEqual(copy.get_initial_concentration('RS'), 0) self.assertEqual(copy.get_initial_concentration('Rpp'), 0)
from model.SBMLtoODES import sbml_to_odes from experiment.ExperimentSet import ExperimentSet if len (sys.argv) != 4: print ("Usage: " + sys.argv[0] + " trace_file model_file " + \ "experiment_file") exit() trace_file = sys.argv[1] model_file = sys.argv[2] experiment_file = sys.argv[3] # define model sbml = SBML () sbml.load_file (model_file) ode = sbml_to_odes (sbml) ode.print_equations () # get param names param_names = [] for param in sbml.get_all_param (): param_names.append (param) # experiment info exp_set = ExperimentSet (experiment_file) experiment_times = exp_set[-1].times experiment_measure = exp_set[-1].measure_expression experiment_observations = [] for exp in exp_set: obs = exp.values
import numpy as np # This script can create an artificial experimental data. It runs a # model and creates measurements of an artificial metric, then add # gaussian error. def add_noise(vals): for j in range(len(vals)): eps = np.random.normal(0, .01) if vals[j] + eps > 0: vals[j] += eps sbml = SBML() sbml.load_file('../input/bioinformatics/model1.xml') odes = sbml_to_odes(sbml) time = [0, 2, 5, 10, 20, 40, 60, 100] values = odes.evaluate_on(time) exp_set = ExperimentSet() for i in range(3): noised_values = {} for x in values: noised_values[x] = list(values[x]) add_noise(noised_values["Rpp"]) experiment = Experiment(time, noised_values["Rpp"], "Rpp") exp_set.add(experiment) exp_set.save_to_file('../input/bioinformatics/experiment.data')
from model.SBML import SBML import argparse from lxml import etree parser = argparse.ArgumentParser() parser.add_argument("model", help="SBML file with model definition") parser.add_argument ("output_file", help="A file to output the priors "\ + "sketch file.") args = parser.parse_args() sbml_file = args.model output_file = args.output_file sbml = SBML() sbml.load_file(sbml_file) original_names = [sbml.get_original_param_name (p) for p in \ sbml.get_all_param ()] priors_names = list(set(original_names)) priors_elm = etree.Element("priors") for param_name in priors_names: param_elm = etree.SubElement(priors_elm, "prior") param_elm.set("distribution", "?") param_elm.set("a", "?") param_elm.set("b", "?") param_elm.set("name", param_name) exp_error_elm = etree.SubElement(priors_elm, "experimental_error") exp_error_elm.set("distribution", "?") exp_error_elm.set("a", "?")
from model.ODES import ODES from model.SBMLtoODES import sbml_to_odes from experiment.ExperimentSet import ExperimentSet from experiment.Experiment import Experiment import numpy as np def add_noise(values): for i in range(len(values)): eps = np.random.normal(0, .01) if values[i] + eps > 0: values[i] += eps sbml = SBML() sbml.load_file('Correct-model.sbml') odes = sbml_to_odes(sbml) time = [0, 30, 60, 90, 120, 150, 180, 210, 240] values = odes.evaluate_on(time) experiment_set = ExperimentSet() for i in range(3): noised_values = {} for x in values: noised_values[x] = list(values[x]) add_noise(noised_values["RasGTP"]) experiment = Experiment(time[1:], noised_values["RasGTP"][1:], "RasGTP") experiment_set.add(experiment) experiment_set.save_to_file('experiment.data')