def test_add_reactions(self): r1 = Reaction('r1') r1.add_metabolites({Metabolite('A'): -1, Metabolite('B'): 1}) r1.lower_bound, r1.upper_bound = -999999., 999999. r2 = Reaction('r2') r2.add_metabolites({ Metabolite('A'): -1, Metabolite('C'): 1, Metabolite('D'): 1 }) r2.lower_bound, r2.upper_bound = 0., 999999. r2.objective_coefficient = 3. self.model.add_reactions([r1, r2]) self.assertEqual(self.model.reactions[-2], r1) self.assertEqual(self.model.reactions[-1], r2) self.assertTrue( isinstance(self.model.reactions[-2].reverse_variable, self.model.solver.interface.Variable)) self.assertEqual( self.model.objective.expression.coeff( self.model.reactions. Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2. forward_variable), 1.) self.assertEqual( self.model.objective.expression.coeff( self.model.reactions. Biomass_Ecoli_core_N_LPAREN_w_FSLASH_GAM_RPAREN__Nmet2. reverse_variable), -1.) self.assertEqual( self.model.objective.expression.coeff( self.model.reactions.r2.forward_variable), 3.) self.assertEqual( self.model.objective.expression.coeff( self.model.reactions.r2.reverse_variable), -3.)
def test_bad_exchange(self, model): with pytest.raises(ValueError): m = Metabolite("baddy", compartment="nonsense") model.add_boundary(m, type="exchange") m = Metabolite("goody", compartment="e") rxn = model.add_boundary(m, type="exchange") assert isinstance(rxn, Reaction)
def test_distance_based_on_molecular_formula( self): # from network_analysis.util met1 = Metabolite("H2O", formula="H2O") met2 = Metabolite("H2O2", formula="H2O2") met3 = Metabolite("C6H12O6", formula="C6H12O6") self.assertEqual( distance_based_on_molecular_formula(met1, met2, normalize=False), 1) self.assertEqual( distance_based_on_molecular_formula(met1, met2, normalize=True), 1. / 7) self.assertEqual( distance_based_on_molecular_formula(met2, met3, normalize=False), 20) self.assertEqual( distance_based_on_molecular_formula(met2, met3, normalize=True), 20. / 28) self.assertEqual( distance_based_on_molecular_formula(met1, met3, normalize=False), 21) self.assertEqual( distance_based_on_molecular_formula(met1, met3, normalize=True), 21. / 27)
def test_iadd(self): model = self.model PGI = model.reactions.PGI EX_h2o = model.reactions.EX_h2o_e original_PGI_gpr = PGI.gene_reaction_rule PGI += EX_h2o self.assertEqual(PGI.gene_reaction_rule, original_PGI_gpr) self.assertEqual(PGI.metabolites[model.metabolites.h2o_e], -1.0) # original should not have changed self.assertEqual(EX_h2o.gene_reaction_rule, '') self.assertEqual(EX_h2o.metabolites[model.metabolites.h2o_e], -1.0) # what about adding a reaction not in the model new_reaction = Reaction("test") new_reaction.add_metabolites({Metabolite("A"): -1, Metabolite("B"): 1}) PGI += new_reaction self.assertEqual(PGI.gene_reaction_rule, original_PGI_gpr) self.assertEqual(len(PGI.gene_reaction_rule), 5) # and vice versa new_reaction += PGI self.assertEqual(len(new_reaction.metabolites), 5) # not 7 self.assertEqual(len(new_reaction.genes), 1) self.assertEqual(new_reaction.gene_reaction_rule, original_PGI_gpr) # what about combining 2 gpr's model.reactions.ACKr += model.reactions.ACONTa self.assertEqual(model.reactions.ACKr.gene_reaction_rule, "(b2296 or b3115 or b1849) and (b0118 or b1276)") self.assertEqual(len(model.reactions.ACKr.genes), 5)
def test_add_metabolite(self): model = self.model reaction = model.reactions.get_by_id("PGI") reaction.add_metabolites({model.metabolites[0]: 1}) self.assertIn(model.metabolites[0], reaction._metabolites) fake_metabolite = Metabolite("fake") reaction.add_metabolites({fake_metabolite: 1}) self.assertIn(fake_metabolite, reaction._metabolites) self.assertTrue(model.metabolites.has_id("fake")) self.assertIs(model.metabolites.get_by_id("fake"), fake_metabolite) # test adding by string reaction.add_metabolites({"g6p_c": -1}) # already in reaction self.assertTrue( reaction._metabolites[model.metabolites.get_by_id("g6p_c")], -2) reaction.add_metabolites({"h_c": 1}) # not currently in reaction self.assertTrue( reaction._metabolites[model.metabolites.get_by_id("h_c")], 1) with self.assertRaises(KeyError): reaction.add_metabolites({"missing": 1}) # test adding to a new Reaction reaction = Reaction("test") self.assertEqual(len(reaction._metabolites), 0) reaction.add_metabolites({Metabolite("test_met"): -1}) self.assertEqual(len(reaction._metabolites), 1)
def test_loopless(self): try: solver = get_solver_name(mip=True) except: self.skipTest("no MILP solver found") test_model = Model() test_model.add_metabolites(Metabolite("A")) test_model.add_metabolites(Metabolite("B")) test_model.add_metabolites(Metabolite("C")) EX_A = Reaction("EX_A") EX_A.add_metabolites({test_model.metabolites.A: 1}) DM_C = Reaction("DM_C") DM_C.add_metabolites({test_model.metabolites.C: -1}) v1 = Reaction("v1") v1.add_metabolites({test_model.metabolites.A: -1, test_model.metabolites.B: 1}) v2 = Reaction("v2") v2.add_metabolites({test_model.metabolites.B: -1, test_model.metabolites.C: 1}) v3 = Reaction("v3") v3.add_metabolites({test_model.metabolites.C: -1, test_model.metabolites.A: 1}) DM_C.objective_coefficient = 1 test_model.add_reactions([EX_A, DM_C, v1, v2, v3]) feasible_sol = construct_loopless_model(test_model).optimize() v3.lower_bound = 1 infeasible_sol = construct_loopless_model(test_model).optimize() self.assertEqual(feasible_sol.status, "optimal") self.assertEqual(infeasible_sol.status, "infeasible")
def test_inequality(self): # The space enclosed by the constraints is a 2D triangle with # vertexes as (3, 0), (1, 2), and (0, 1) solver = self.solver # c1 encodes y - x > 1 ==> y > x - 1 # c2 encodes y + x < 3 ==> y < 3 - x c1 = Metabolite("c1") c2 = Metabolite("c2") x = Reaction("x") x.lower_bound = 0 y = Reaction("y") y.lower_bound = 0 x.add_metabolites({c1: -1, c2: 1}) y.add_metabolites({c1: 1, c2: 1}) c1._bound = 1 c1._constraint_sense = "G" c2._bound = 3 c2._constraint_sense = "L" m = Model() m.add_reactions([x, y]) # test that optimal values are at the vertices m.change_objective("x") self.assertAlmostEqual(solver.solve(m).f, 1.0) self.assertAlmostEqual(solver.solve(m).x_dict["y"], 2.0) m.change_objective("y") self.assertAlmostEqual(solver.solve(m).f, 3.0) self.assertAlmostEqual( solver.solve(m, objective_sense="minimize").f, 1.0)
def test_find_dead_end_reactions(self, core_model): assert len(structural.find_dead_end_reactions(core_model)) == 0 met1 = Metabolite("fake_metabolite_1") met2 = Metabolite("fake_metabolite_2") reac = Reaction("fake_reac") reac.add_metabolites({met1: -1, met2: 1}) core_model.add_reaction(reac) assert structural.find_dead_end_reactions(core_model) == {reac}
def three_components_closed(base): """Returns a simple model with 3 metabolites in a closed system.""" met_a = Metabolite("A") met_b = Metabolite("B") met_c = Metabolite("C") rxn_1 = Reaction("R1", lower_bound=-1000, upper_bound=1000) rxn_1.add_metabolites({met_a: -1, met_b: 1}) rxn_2 = Reaction("R2", lower_bound=-1000, upper_bound=1000) rxn_2.add_metabolites({met_b: -1, met_c: 1}) base.add_reactions([rxn_1, rxn_2]) return base
def generate_kegg_compound(model,modelseedid,compoundsIdConverter,modelseedCompoundsDb, compartment=""): """ Method to generate metabolites for each compartment of the model in the KEGG database format. This method tries to find the KEGG format of the new compounds. If it does not find, it will change it into the Model SEED format. :param cobrapy.Model model: cobrapy Model :param string modelseedid: model seed identifier of the compound :param CompoundsIDConverter compoundsIdConverter: a converter :param ModelSeedCompoundsDB modelseedCompoundsDb: the compounds Model SEED database :param (Optional) string compartment: compartment to introduce the new compound :return list: new compounds list """ if modelseedid and "KEGG" in compoundsIdConverter.get_modelSeedIdToDb().get(modelseedid): kegg_id = compoundsIdConverter.convert_modelSeedId_into_other_dbID(modelseedid, "KEGG")[0] kegg_metabolite = KeggCompound(kegg_id) aliases = compoundsIdConverter.get_all_aliases_by_modelSeedID(modelseedid) annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(aliases) compounds = [] if not compartment: for compartment in model.compartments: model_metabolite = Metabolite(kegg_id + "_" + compartment) model_metabolite.annotation = annotation model_metabolite.annotation["inchikey"] = kegg_metabolite.get_inchikey() model_metabolite.annotation["smiles"] = kegg_metabolite.get_smiles() model_metabolite.formula = kegg_metabolite.get_formula() model_metabolite.name = kegg_metabolite.get_name() model_metabolite.charge = 0 model_metabolite.compartment = compartment compounds.append(model_metabolite) else: model_metabolite = Metabolite(kegg_id + "_" + compartment) model_metabolite.annotation = annotation model_metabolite.annotation["inchikey"] = kegg_metabolite.get_inchikey() model_metabolite.annotation["smiles"] = kegg_metabolite.get_smiles() model_metabolite.formula = kegg_metabolite.get_formula() model_metabolite.name = kegg_metabolite.get_name() model_metabolite.charge = 0 model_metabolite.compartment = compartment compounds.append(model_metabolite) return compounds else: return generate_modelseed_compound(model, modelseedid, compoundsIdConverter,modelseedCompoundsDb)
def test_distance_based_on_molecular_formula(self): # from network_analysis.util met1 = Metabolite("H2O", formula="H2O") met2 = Metabolite("H2O2", formula="H2O2") met3 = Metabolite("C6H12O6", formula="C6H12O6") assert distance_based_on_molecular_formula(met1, met2, normalize=False) == 1 assert distance_based_on_molecular_formula(met1, met2, normalize=True) == 1. / 7 assert distance_based_on_molecular_formula(met2, met3, normalize=False) == 20 assert distance_based_on_molecular_formula(met2, met3, normalize=True) == 20. / 28 assert distance_based_on_molecular_formula(met1, met3, normalize=False) == 21 assert distance_based_on_molecular_formula(met1, met3, normalize=True) == 21. / 27
def x2_cycle_closed(base): x1 = Metabolite("x1") x2 = Metabolite("x2") x3 = Metabolite("x3") x4 = Metabolite("x4") x5 = Metabolite("x5") rxn_1 = Reaction("R1", lower_bound=-1000, upper_bound=1000) rxn_1.add_metabolites({x1: -1, x2: -1, x3: 1}) rxn_2 = Reaction("R2", lower_bound=-1000, upper_bound=1000) rxn_2.add_metabolites({x3: -1, x4: 1}) rxn_3 = Reaction("R3", lower_bound=-1000, upper_bound=1000) rxn_3.add_metabolites({x4: -1, x5: 1, x2: 1}) base.add_reactions([rxn_1, rxn_2, rxn_3]) return base
def test_change_coefficient(self): solver = self.solver c = Metabolite("c") c._bound = 6 x = Reaction("x") x.lower_bound = 1. y = Reaction("y") y.lower_bound = 0. x.add_metabolites({c: 1}) #y.add_metabolites({c: 1}) z = Reaction("z") z.add_metabolites({c: 1}) z.objective_coefficient = 1 m = Model("test_model") m.add_reactions([x, y, z]) # change an existing coefficient lp = solver.create_problem(m) solver.solve_problem(lp) sol1 = solver.format_solution(lp, m) solver.change_coefficient(lp, 0, 0, 2) solver.solve_problem(lp) sol2 = solver.format_solution(lp, m) self.assertAlmostEqual(sol1.f, 5.0) self.assertAlmostEqual(sol2.f, 4.0) # change a new coefficient z.objective_coefficient = 0. y.objective_coefficient = 1. lp = solver.create_problem(m) solver.change_coefficient(lp, 0, 1, 2) solver.solve_problem(lp) solution = solver.format_solution(lp, m) self.assertAlmostEqual(solution.x_dict["y"], 2.5)
def test_one_left_to_right_reaction_set_positive_ub(self): model = Model("Toy Model") m1 = Metabolite("M1") d1 = Reaction("ex1") d1.add_metabolites({m1: -1}) d1.upper_bound = 0 d1.lower_bound = -1000 model.add_reactions([d1]) self.assertEqual(d1.reverse_variable.lb, 0) self.assertEqual(d1.reverse_variable.ub, 1000) self.assertEqual(d1._lower_bound, -1000) self.assertEqual(d1.lower_bound, -1000) self.assertEqual(d1._upper_bound, 0) self.assertEqual(d1.upper_bound, 0) self.assertEqual(d1.forward_variable.lb, 0) self.assertEqual(d1.forward_variable.ub, 0) d1.upper_bound = .1 self.assertEqual(d1.forward_variable.lb, 0) self.assertEqual(d1.forward_variable.ub, .1) self.assertEqual(d1.reverse_variable.lb, 0) self.assertEqual(d1.reverse_variable.ub, 1000) self.assertEqual(d1._lower_bound, -1000) self.assertEqual(d1.upper_bound, .1) self.assertEqual(d1._lower_bound, -1000) self.assertEqual(d1.upper_bound, .1)
def cad_reaction(core_model): reaction = Reaction(id="CAD", name="Cis-Aconitate Decarboxylase") acon = core_model.metabolites.acon_DASH_C_c co2_c = core_model.metabolites.co2_c ita_c = Metabolite(id="ita_c", name="Itaconate", compartment="c") reaction.add_metabolites({acon: -1, co2_c: 1, ita_c: 1}) return reaction
def test_solve_mip(self): solver = self.solver if not hasattr(solver, "_SUPPORTS_MILP") or not solver._SUPPORTS_MILP: self.skipTest("no milp support") cobra_model = Model('MILP_implementation_test') constraint = Metabolite("constraint") constraint._bound = 2.5 x = Reaction("x") x.lower_bound = 0. x.objective_coefficient = 1. x.add_metabolites({constraint: 2.5}) y = Reaction("y") y.lower_bound = 0. y.objective_coefficient = 1. y.add_metabolites({constraint: 1.}) cobra_model.add_reactions([x, y]) float_sol = solver.solve(cobra_model) # add an integer constraint y.variable_kind = "integer" int_sol = solver.solve(cobra_model) self.assertAlmostEqual(float_sol.f, 2.5) self.assertAlmostEqual(float_sol.x_dict["y"], 2.5) self.assertEqual(int_sol.status, "optimal") self.assertAlmostEqual(int_sol.f, 2.2) self.assertAlmostEqual(int_sol.x_dict["y"], 2.0)
def createTokens(model,reactionmap): from cobra import Reaction from cobra import Metabolite ''' Compares a CobraPy model and a reaction map. If several model reactions are associated with the same experimental reaction, a token reaction is created whose flux will equal a linear combination of those reactions (the token flux). This token reaction can be used as a decision variable during quadratic optimization attempting to match the token flux to that of the experimental reaction. Noe that limiting the token flux to zero will prevent any flux through the separate reactions. ''' for linkdict in reactionmap: if len(linkdict['modrxns']) > 1: experimental_r_id = linkdict['exprxns'][0]['rxid'] token_id = 't_{exprxnid}'.format(exprxnid = experimental_r_id) new_token_reaction = Reaction(token_id) new_token_reaction.name = 'Token exchange reaction for experimental reaction {exprxnid}'.format(exprxnid = experimental_r_id) new_token_reaction.lower_bound = -1000 new_token_reaction.upper_bound = 1000 new_token_metabolite = Metabolite(token_id,name = 'Token metabolite for experimental reaction {exprxnid}'.format(exprxnid = experimental_r_id),compartment='c') new_token_reaction.add_metabolites({new_token_metabolite: -1.0}) model.add_reactions(new_token_reaction) for dictionary in linkdict['modrxns']: modelreaction = model.reactions.get_by_id(dictionary['rxid']) modelreaction.add_metabolites({new_token_metabolite: float(dictionary['coef'])}) return model
def test_add_metabolites_combine_true(self): test_metabolite = Metabolite('test') for reaction in self.model.reactions: reaction.add_metabolites({test_metabolite: -66}, combine=True) self.assertEqual(reaction.metabolites[test_metabolite], -66) self.assertIn(-66. * reaction.forward_variable, self.model.solver.constraints['test'].expression) self.assertIn(66. * reaction.reverse_variable, self.model.solver.constraints['test'].expression) already_included_metabolite = list( reaction.metabolites.keys())[0] previous_coefficient = reaction.get_coefficient( already_included_metabolite.id) reaction.add_metabolites({already_included_metabolite: 10}, combine=True) new_coefficient = previous_coefficient + 10 self.assertEqual( reaction.metabolites[already_included_metabolite], new_coefficient) self.assertIn( new_coefficient * reaction.forward_variable, self.model.solver.constraints[ already_included_metabolite.id].expression) self.assertIn( -1 * new_coefficient * reaction.reverse_variable, self.model.solver.constraints[ already_included_metabolite.id].expression)
def test_weird_left_to_right_reaction_issue(self): model = Model("Toy Model") m1 = Metabolite("M1") d1 = Reaction("ex1") d1.add_metabolites({m1: -1}) d1.upper_bound = 0 d1.lower_bound = -1000 # print d1.reaction, d1.lower_bound, d1.upper_bound model.add_reactions([d1]) self.assertFalse(d1.reversibility) self.assertEqual(d1.lower_bound, -1000) self.assertEqual(d1._lower_bound, -1000) self.assertEqual(d1.upper_bound, 0) self.assertEqual(d1._upper_bound, 0) with TimeMachine() as tm: d1.knock_out(time_machine=tm) self.assertEqual(d1.lower_bound, 0) self.assertEqual(d1._lower_bound, 0) self.assertEqual(d1.upper_bound, 0) self.assertEqual(d1._upper_bound, 0) self.assertEqual(d1.lower_bound, -1000) self.assertEqual(d1._lower_bound, -1000) self.assertEqual(d1.upper_bound, 0) self.assertEqual(d1._upper_bound, 0)
def FDA(kegg_reactions): ###kegg_reactions = ['R00509'] model_reactions = [] model_metabolites = [] for item1 in model.reactions: model_reactions.append(item1.id) for item2 in model.metabolites: model_metabolites.append(item2.id) for k_id in kegg_reactions: if k_id in bigg_reactions.keys(): if bigg_reactions[k_id]['id'] not in model_reactions: reaction = Reaction(bigg_reactions[k_id]['id']) print(reaction.id) reaction.name = '' reaction.subsystem = '' reaction.lower_bound = bigg_reactions[k_id][ 'lower_bound'] # This is the default reaction.upper_bound = bigg_reactions[k_id][ 'upper_bound'] # This is the default for key in bigg_reactions[k_id]['metabolites']: if key in model_metabolites: reaction.add_metabolites({ model.metabolites.get_by_id(key): bigg_reactions[k_id]['metabolites'][key] }) else: a = Metabolite(key, formula='', name='', compartment='') reaction.add_metabolites( {a: bigg_reactions[k_id]['metabolites'][key]}) reaction.gene_reaction_rule = bigg_reactions[k_id][ 'gene_reaction_rule'] model.add_reactions([reaction])
def LP7(model, the_reactions=None, epsilon=1e-3, solver=None): model_lp7 = model.copy() for reaction in model_lp7.reactions: reaction.objective_coefficient = 0 if the_reactions is None: the_reactions = [r.id for r in model_lp7.reactions] if not hasattr(list(the_reactions)[0], 'id'): the_reactions = map(model_lp7.reactions.get_by_id, the_reactions) for reaction in the_reactions: dummy_reaction = Reaction("dummy_rxn_" + reaction.id) dummy_reaction.lower_bound = 0 dummy_reaction.upper_bound = epsilon dummy_reaction.objective_coefficient = 1 model_lp7.add_reaction(dummy_reaction) dummy_metabolite = Metabolite("dummy_met_" + reaction.id) dummy_metabolite._constraint_sense = "L" dummy_metabolite._bound = 0 reaction.add_metabolites({dummy_metabolite: -1}) dummy_reaction.add_metabolites({dummy_metabolite: 1}) model_lp7.optimize(solver=solver) if model_lp7.solution is None or model_lp7.solution.x_dict is None: print "INFEASIBLE LP7" return {} return dict([(k, v) for k, v in model_lp7.solution.x_dict.items() if not k.startswith('dummy_')])
def add_metabolite(self, m_id, m_name, compartment): if m_id not in self.metabolites: ############## # model_data # ############## self.metabolites[m_id] = m_name self.listed_metabolites.append(m_id) self.metabolite_position[m_id] = len(self.listed_metabolites) - 1 self.compartment_metabolites[compartment].add(m_id) ######### # cobra # ######### if compartment not in self.model.compartments: self.model.compartments[compartment] = compartment metabolite = Metabolite(m_id, formula="", name=m_name, compartment=compartment) self.model.metabolites.append(metabolite) return 1 return 0
def parse_coeff_and_metabolites(term): try: coeff, metabolite_id = term.strip().split(' ') except: raise ValueError( 'Something is fishy with the provided term %s' % term) return Metabolite(metabolite_id), factor * float(coeff)
def test_reaction_delete(self): model = self.model old_reaction_count = len(model.reactions) tmp_metabolite = Metabolite("testing") # Delete without removing orphan model.reactions[0].add_metabolites({tmp_metabolite: 1}) self.assertEqual(len(tmp_metabolite.reactions), 1) model.reactions[0].delete(remove_orphans=False) # make sure it's still in the model self.assertIn(tmp_metabolite, model.metabolites) self.assertEqual(len(tmp_metabolite.reactions), 0) self.assertEqual(len(self.model.reactions), old_reaction_count - 1) # Now try it with removing orphans model.reactions[0].add_metabolites({tmp_metabolite: 1}) self.assertEqual(len(tmp_metabolite.reactions), 1) model.reactions[0].delete(remove_orphans=True) self.assertNotIn(tmp_metabolite, model.metabolites) self.assertEqual(len(tmp_metabolite.reactions), 0) self.assertEqual(len(self.model.reactions), old_reaction_count - 2) # It shouldn't remove orphans if it's in 2 reactions however model.reactions[0].add_metabolites({tmp_metabolite: 1}) model.reactions[1].add_metabolites({tmp_metabolite: 1}) self.assertEqual(len(tmp_metabolite.reactions), 2) model.reactions[0].delete(remove_orphans=False) self.assertIn(tmp_metabolite, model.metabolites) self.assertEqual(len(tmp_metabolite.reactions), 1) self.assertEqual(len(self.model.reactions), old_reaction_count - 3)
def get_emu_metabolite(emu_dict, label_model): """ Creates a cobra metabolite object for the new emu. Additionally it will add additional information if the emu is symmetryc label_model: label_model object emu_dict: dict dict that contains the ID of the metabolite (or group of metabolites) and the carbon range """ #print emu_dict met_id = emu_dict["met_id"] carbons = emu_dict["carbons"] iso_object = label_model.id_isotopomer_object_dict[met_id] emuid = "emu_" + met_id + "_" carbon_range_string = "" for carbon in carbons: carbon_range_string += str(carbon) if iso_object.symm == True: #build a symetryc dic symm_dict = {} forward_range = range(1, iso_object.n + 1) reverse_range = range(iso_object.n, 0, -1) for x in forward_range: symm_dict[x] = reverse_range[x - 1] #print symm_dict symm_carbons = [] for carbon in carbons: symm_carbons.append(symm_dict[carbon]) #print symm_carbons symm_carbons = sorted(symm_carbons) emu_dict["symm_carbons"] = symm_carbons if symm_carbons != carbons: #Check if they are not equal: #Identfy the lower range, which will be written first in the metabolite id symm_carbon_range_string = "" for carbon in symm_carbons: symm_carbon_range_string += str(carbon) if symm_carbons[0] < carbons[0]: emuid += symm_carbon_range_string + "_and_" + carbon_range_string else: emuid += carbon_range_string + "_and_" + symm_carbon_range_string else: emu_dict["symm_carbons"] = [] emuid += carbon_range_string else: emuid += carbon_range_string if emuid in label_model.emu_model.metabolites: emu_met = label_model.emu_model.metabolites.get_by_id(emuid) present_flag = True else: comp = label_model.simplified_metabolic_model.metabolites.get_by_id( emu_dict["met_id"]).compartment emu_met = Metabolite(emuid, formula='', name=emuid, compartment=comp) present_flag = False met_id = emu_dict["met_id"] label_model.emu_metabolite_dict[emu_met] = met_id if met_id not in label_model.metabolite_emu_dict: label_model.metabolite_emu_dict[met_id] = [] label_model.metabolite_emu_dict[met_id].append(emu_met.id) return (emu_met, emu_dict, present_flag)
def define_reaction_group(model, reaction_dict, group_reaction_id=None, lower_bound=None, upper_bound=None, objective_coefficient=0): new_reaction_id = "RGROUP" if group_reaction_id != None: if "RGROUP" in group_reaction_id: new_reaction_id = group_reaction_id else: new_reaction_id += "_" + group_reaction_id else: for reaction_id in reaction_dict: new_reaction_id += "_" + reaction_id if new_reaction_id in model.reactions: model.reactions.get_by_id(new_reaction_id).remove_from_model() new_reaction_name = "Reaction Group:" for reaction_id in reaction_dict: if reaction_dict[reaction_id] > 0: new_reaction_name += "+" + reaction_id else: new_reaction_name += "-" + reaction_id metabolite = Metabolite("m" + new_reaction_id, formula='', name="mGROUP" + new_reaction_id, compartment='gr') group_reaction = Reaction(new_reaction_id) group_reaction.name = new_reaction_name group_reaction.subsystem = 'Reaction group' if upper_bound != None: group_reaction.upper_bound = upper_bound group_reaction.add_metabolites({metabolite: -1}) if objective_coefficient == None: group_reaction.objective_coefficient = 0 model.add_reaction(group_reaction) group_reaction.objective_coefficient = objective_coefficient theoretical_lower_bound = 0 theoretical_upper_bound = 0 for reaction_id in reaction_dict: coef = reaction_dict[reaction_id] reaction = model.reactions.get_by_id(reaction_id) reaction.add_metabolites({metabolite: coef}) if coef >= 0: theoretical_upper_bound += reaction.upper_bound theoretical_lower_bound += reaction.lower_bound else: theoretical_upper_bound -= reaction.lower_bound theoretical_lower_bound -= reaction.upper_bound if lower_bound == None: group_reaction.lower_bound = min( round_down(theoretical_lower_bound, 2), 0) else: group_reaction.lower_bound = lower_bound if upper_bound == None: group_reaction.upper_bound = max(round_up(theoretical_upper_bound, 2), 1000) else: group_reaction.upper_bound = upper_bound
def make_metabolite(metab_id,metab_formula,metab_name,compartment): from cobra import Metabolite metab = Metabolite(metab_id, formula=metab_formula, name=metab_name, compartment=compartment) return metab
def test_impossible_req(self, model): mod, conf = model D = Metabolite("D") mod.add_metabolites([D]) opt = CORDA(mod, conf, met_prod=["D"]) need = opt.associated(["EX_CORDA_0"]) assert len(need) == 0 assert "EX_CORDA_0" in opt.impossible
def test_impossible_req(self): model = self.model.copy() D = Metabolite("D") model.add_metabolites([D]) opt = CORDA(model, self.conf, met_prod=["D"]) need = opt.associated(["EX_CORDA_0"]) self.assertTrue(len(need["EX_CORDA_0"]) == 0) self.assertTrue("EX_CORDA_0" in opt.impossible)
def add_met(model, met_id, name, formula, compartment): '''Add metabolite.''' met = Metabolite(met_id, formula=formula, name=name, compartment=compartment) return model.add_metabolites([met])