def addFromSeedDB(mod, lineFromSeedDB): import pandas from cobra import Model, Reaction, Metabolite comp = {'0':'_c0', '1':'_e0', '2':'_p0'} model = mod.copy() formula = lineFromSeedDB["stoichiometry"].values[0] rea_met = {m.split(":")[1]+comp[m.split(":")[2]]:float(m.split(":")[0]) for m in formula.split(";")} rea_id = lineFromSeedDB["id"].values[0] + "_c0" # always the case?? rea_name= lineFromSeedDB["name"].values[0] rea = Reaction(id=rea_id, name=rea_name) for m in rea_met: if m not in model.metabolites: model.add_metabolites(Metabolite(m)) model.add_reactions(rea) rea_stoich = {model.metabolites.get_by_id(m):rea_met[m] for m in rea_met} rea.add_metabolites(rea_stoich) return(model)
def test_array_based_model_add(self): m = len(self.model.metabolites) n = len(self.model.reactions) for matrix_type in ["scipy.dok_matrix", "scipy.lil_matrix"]: model = create_test_model("textbook").\ to_array_based_model(matrix_type=matrix_type) test_reaction = Reaction("test") test_reaction.add_metabolites({model.metabolites[0]: 4}) test_reaction.lower_bound = -3.14 model.add_reaction(test_reaction) self.assertEqual(len(model.reactions), n + 1) self.assertEqual(model.S.shape, (m, n + 1)) self.assertEqual(len(model.lower_bounds), n + 1) self.assertEqual(len(model.upper_bounds), n + 1) self.assertEqual(model.S[0, n], 4) self.assertEqual(model.S[7, 0], -1) self.assertEqual(model.lower_bounds[n], -3.14)
def eval_ind(individual, initial_pop, model, base_biomass, exp_ess, distance): # Set this as warning model.solver = 'gurobi' old_biomass = list( linear_reaction_coefficients(model).keys())[0] # index removed old_biomass.remove_from_model() # Make a biomass reaction and optimize for it biomass = Reaction('BIOMASS') model.add_reaction(biomass) index = initial_pop.index for i in range(len(index)): if individual[i] == 1: model.reactions.BIOMASS.add_metabolites( {initial_pop.index[i]: -0.1}) model.reactions.BIOMASS.add_metabolites(base_biomass) model.reactions.BIOMASS.objective_coefficient = 1. # Generate deletion results --> BOTTLENECK FOR SURE deletion_results = single_gene_deletion(model, model.genes, processes=1) # Filter the results to get a boolean result a = [(str(next(iter(i))), 1) for i in deletion_results[deletion_results['growth'] > 1e-3].index] b = [(str(next(iter(i))), 0) for i in deletion_results[deletion_results['growth'] <= 1e-3].index] c = a + b pred_ess = pd.DataFrame(c, columns=['Genes', 'Predicted_growth']) compare_df = pd.merge(right=exp_ess, left=pred_ess, on='Genes', how='inner') # Apply hamming distance u = np.array([f for f in compare_df.Measured_growth]) v = np.array([x for x in compare_df.Predicted_growth]) ''' if distance == 'hd': dist = hamming(u, v) ''' if distance == 'mcc': dist = matthews_corrcoef(u, v) else: print('Error: Invalid distance metric') return dist, sum(individual)
def capitulo_8(): file = open("resultados_capitulo_8.txt", "w") salmonella = cobra.test.create_test_model('salmonella') nominal = salmonella.optimize() loopless = loopless_solution(salmonella) import pandas df = pandas.DataFrame( dict(loopless=loopless.fluxes, nominal=nominal.fluxes)) df.plot.scatter(x='loopless', y='nominal') model = Model() model.add_metabolites([Metabolite(i) for i in "ABC"]) model.add_reactions( [Reaction(i) for i in ["EX_A", "DM_C", "v1", "v2", "v3"]]) model.reactions.EX_A.add_metabolites({"A": 1}) model.reactions.DM_C.add_metabolites({"C": -1}) model.reactions.v1.add_metabolites({"A": -1, "B": 1}) model.reactions.v2.add_metabolites({"B": -1, "C": 1}) model.reactions.v3.add_metabolites({"C": -1, "A": 1}) model.objective = 'DM_C' with model: add_loopless(model) solution = model.optimize() file.write("loopless solution: status = " + solution.status) file.write("\n") file.write("loopless solution flux: v3 = %.1f" % solution.fluxes["v3"]) file.write("\n") solution = pfba(model) file.write("parsimonious solution: status = " + solution.status) file.write("\n") file.write("loopless solution flux: v3 = %.1f" % solution.fluxes["v3"]) file.write("\n") model.reactions.v3.lower_bound = 1 with model: add_loopless(model) try: solution = model.optimize() except: file.write('model is infeasible') file.write("\n") solution = pfba(model) file.write("parsimonious solution: status = " + solution.status) file.write("\n") file.write("loopless solution flux: v3 = %.1f" % solution.fluxes["v3"]) file.write("\n") file.close()
def test_gene_knockout_computation(self): cobra_model = self.model delete_model_genes = delete.delete_model_genes get_removed = lambda m: {x.id for x in m._trimmed_reactions} gene_list = ['STM1067', 'STM0227'] dependent_reactions = { '3HAD121', '3HAD160', '3HAD80', '3HAD140', '3HAD180', '3HAD100', '3HAD181', '3HAD120', '3HAD60', '3HAD141', '3HAD161', 'T2DECAI', '3HAD40' } delete_model_genes(cobra_model, gene_list) self.assertEqual(get_removed(cobra_model), dependent_reactions) # cumulative delete_model_genes(cobra_model, ["STM4221"], cumulative_deletions=True) dependent_reactions.add('PGI') self.assertEqual(get_removed(cobra_model), dependent_reactions) # non-cumulative delete_model_genes(cobra_model, ["STM4221"], cumulative_deletions=False) self.assertEqual(get_removed(cobra_model), {'PGI'}) # make sure on reset that the bounds are correct reset_bound = cobra_model.reactions.get_by_id("T2DECAI").upper_bound self.assertEqual(reset_bound, 1000.) # test computation when gene name is a subset of another test_model = Model() test_reaction_1 = Reaction("test1") test_reaction_1.gene_reaction_rule = "eggs or (spam and eggspam)" test_model.add_reaction(test_reaction_1) delete.delete_model_genes(test_model, ["eggs"]) self.assertEqual(get_removed(test_model), set()) delete_model_genes(test_model, ["spam"], cumulative_deletions=True) self.assertEqual(get_removed(test_model), {'test1'}) # test computation with nested boolean expression delete.undelete_model_genes(test_model) test_reaction_1.gene_reaction_rule = \ "g1 and g2 and (g3 or g4 or (g5 and g6))" delete_model_genes(test_model, ["g3"], cumulative_deletions=False) self.assertEqual(get_removed(test_model), set()) delete_model_genes(test_model, ["g1"], cumulative_deletions=False) self.assertEqual(get_removed(test_model), {'test1'}) delete_model_genes(test_model, ["g5"], cumulative_deletions=False) self.assertEqual(get_removed(test_model), set()) delete_model_genes(test_model, ["g3", "g4", "g5"], cumulative_deletions=False) self.assertEqual(get_removed(test_model), {'test1'})
def create_new_stoichiometric_matrix(self): """ Extracts the new graph without the small metabolites, inorganics and cofactor pairs. :return: Networkx graph of the new network """ kept_rxns = [] kept_metabolites = set() for rxn in self._redgem.reactions: metabolites = {} for metabolite, coefficient in rxn.metabolites.items(): metabolite_id = metabolite.id if metabolite_id in self._cofactor_pairs \ or metabolite_id in self._small_metabolites \ or metabolite_id in self._inorganics: continue new_metabolite = Metabolite(metabolite_id, formula=metabolite.formula, name=metabolite.name, compartment=metabolite.compartment) metabolites[new_metabolite] = coefficient kept_metabolites.add(metabolite) new_rxn = Reaction(rxn.id, name=rxn.name, subsystem=rxn.subsystem, lower_bound=rxn.lower_bound, upper_bound=rxn.upper_bound) new_rxn.add_metabolites(metabolites) kept_rxns.append(new_rxn) paths_struct = [{} for _ in range(self._d + 1) ] # Comprehension list to create multiple dicts to_struct = [""] * (self._d + 1) for metabolite in kept_metabolites: self._graph.add_node(metabolite.id, paths=paths_struct, to=to_struct) for rxn in kept_rxns: for reactant in rxn.reactants: for product in rxn.products: self._graph.add_edge(reactant.id, product.id, rxn_id=rxn.id, weight=1) return self._graph
def join_models(model_files, id=None): """Join several models into one. This requires all the models to use the same ID system. Arguments ---------- model_files : list of strings The files to be joined. id : str The new ID for the model. Will be the ID of the first model if None. Returns ------- cobra.Model The joined cobra Model. """ model = load_model(model_files[0]) n = len(model_files) if id: model.id = id biomass = Reaction( id="micom_combined_biomass", name="combined biomass reaction from model joining", subsystem="biomass production", lower_bound=0, upper_bound=1000, ) coefs = linear_reaction_coefficients(model, model.reactions) for r, coef in coefs.items(): biomass += r * (coef / n) rids = set(r.id for r in model.reactions) for filepath in model_files[1:]: other = load_model(filepath) new = [r.id for r in other.reactions if r.id not in rids] model.add_reactions(other.reactions.get_by_any(new)) coefs = linear_reaction_coefficients(other, other.reactions) for r, coef in coefs.items(): biomass += model.reactions.get_by_id(r.id) * (coef / n) rids.update(new) model.add_reactions([biomass]) model.objective = biomass return model
def test_gnomic_integration_ReactionKnockinTarget(self, model): reaction = Reaction(id="atpzase", name="Cosmic ATP generator") atp_z = Metabolite(id="atp_z", name="Cosmic ATP", compartment="c") reaction.add_metabolites({model.metabolites.atp_c: 1, atp_z: -1}) knockin_target = ReactionKnockinTarget("atpzase", reaction) knockin_target_gnomic = knockin_target.to_gnomic() assert genotype_to_string(Genotype([knockin_target_gnomic ])) == "+reaction.atpzase" reaction.add_metabolites({model.metabolites.atp_c: 1, atp_z: -1}) knockin_target = ReactionKnockinTarget("atpzase", reaction, accession_id='atpzase', accession_db='unicorn') knockin_target_gnomic = knockin_target.to_gnomic() assert genotype_to_string(Genotype( [knockin_target_gnomic])) == "+reaction.atpzase#unicorn:atpzase"
def convert_to_irreversible(model,reactions_to_convert): ##### TO DO : implement ATP cost in reverse reaction (1 ATP) """ Split reversible reactions into two irreversible reactions. cobra_model: A Cobra model object which will be modified in place. reactions_to convert: list of reaction_id to convert """ reactions_to_add = [] convertedlist = [] atp_metabolites = [ model.metabolites.ppi_c, model.metabolites.pi_c, model.metabolites.atp_c, model.metabolites.adp_c, model.metabolites.adp_c] for r in reactions_to_convert: reaction = model.reactions.get_by_id(r) if reaction.lower_bound < 0 and reaction.upper_bound > 0: reverse_reaction = Reaction(id=reaction.id + '_reverse', lower_bound = reaction.lower_bound, upper_bound = 0) reaction.lower_bound = 0 reaction.upper_bound = reaction.upper_bound reverse_reaction_dict = {k: v * -1 for k, v in reaction.metabolites.items() if k not in atp_metabolites} # <- disallows recovery of ATP metabolites from protein degradation reverse_reaction.add_metabolites(reverse_reaction_dict) reverse_reaction.subsystem = reaction.subsystem reactions_to_add.append(reverse_reaction) convertedlist.append(reverse_reaction.id) model.add_reactions(reactions_to_add) return convertedlist
def constrain_pool(self): """Constrain the draw reactions for the unmeasured (common protein pool) proteins. Proteins without their own protein pool are collectively constrained by the common protein pool. Remove protein pools for all proteins that don't have measurements, along with corresponding draw reactions, and add these to the common protein pool and reaction. """ new_reactions = [] to_remove = [] # * section 2.5.1 # 1. and 2. introduce `prot_pool` and exchange reaction done in __init__ # 3. limiting total usage with the unmeasured amount of protein # looks like the matlab code: # self.fs_matched_adjusted = ((self.p_total - self.p_measured) / self.p_base * # self.f_mass_fraction_measured_matched_to_total * # self.sigma_saturation_factor) # but this gives results more like reported: self.fs_matched_adjusted = ( (self.p_total - self.p_measured) * self.f_mass_fraction_measured_matched_to_total * self.sigma_saturation_factor) self.reactions.prot_pool_exchange.bounds = 0, self.fs_matched_adjusted # 4. Remove other enzyme usage reactions and replace with pool exchange reactions average_mmw = self.protein_properties['mw'].mean() / 1000. for protein_id in self.unmeasured_proteins: to_remove.extend( self.reactions.query('prot_{}_exchange'.format(protein_id))) draw_reaction_id = 'draw_prot_{}'.format(protein_id) if draw_reaction_id not in self.reactions: draw_rxn = Reaction(draw_reaction_id) draw_rxn.annotation['uniprot'] = protein_id protein_pool = self.metabolites.get_by_id( 'prot_{}_c'.format(protein_id)) try: mmw = self.protein_properties.loc[protein_id, 'mw'] / 1000. except KeyError: mmw = average_mmw metabolites = {self.common_protein_pool: -mmw, protein_pool: 1} draw_rxn.add_metabolites(metabolites) new_reactions.append(draw_rxn) self.add_reactions(new_reactions) self.remove_reactions(to_remove)
def apply_ratios(constrained_model, ratio_dict): for ratio in ratio_dict: if "RATIO_" + ratio in constrained_model.reactions: #print "Reaction"+" RATIO_"+ratio+ "already present" ratio_reaction = constrained_model.reactions.get_by_id("RATIO_" + ratio) for flux in ratio_dict[ratio]: flux_metabolite = constrained_model.metabolites.get_by_id( ratio + "_" + flux) if flux_metabolite not in ratio_reaction.metabolites: ratio_reaction.add_metabolites( {flux_metabolite: -ratio_dict[ratio][flux]}) else: coef = ratio_reaction.metabolites[flux_metabolite] if coef != -ratio_dict[ratio][flux]: deltacoef = -ratio_dict[ratio][flux] - coef ratio_reaction.add_metabolites( {flux_metabolite: deltacoef}) else: metabolite_dict = {} lower_bound = 0 upper_bound = 0 for flux in ratio_dict[ratio]: reaction = constrained_model.reactions.get_by_id(flux) metabolite = Metabolite(ratio + "_" + flux, name="", compartment="r") #ratio reaction.add_metabolites({metabolite: 1}) coef = ratio_dict[ratio][flux] metabolite_dict[metabolite] = -coef #upper_bound+=reaction.upper_bound*coef #lower_bound+=reaction.lower_bound*coef #print metabolite_dict ratio_reaction = Reaction("RATIO_" + ratio) ratio_reaction.name = "RATIO_" + ratio ratio_reaction.subsystem = 'Flux ratio' ratio_reaction.lower_bound = -1000000 ratio_reaction.upper_bound = 1000000 ratio_reaction.add_metabolites(metabolite_dict) constrained_model.add_reaction(ratio_reaction)
def _prepare_sinks(self): """ For each BBB (reactant of the biomass reactions), generate a sink, i.e an unbalanced reaction BBB -> of which purpose is to enable the BBB to be output of the GEM :return: the dict {BBB: sink} containing every BBB (keys) and their associated sinks """ all_sinks = {} print("Preparing sinks...") for bio_rxn in self._rBBB: stoich_dict = self.get_cofactor_adjusted_stoich(bio_rxn) for met in bio_rxn.reactants: stoech_coeff = stoich_dict[met.id] # stoech_coeff < 0 indicates that the metabolite is a reactant if (stoech_coeff < 0) and (met not in all_sinks.keys()): sink = Reaction("Sink_" + bio_rxn.id + "_" + met.id) sink.name = "Sink_" + bio_rxn.name + "_" + met.name # Subsystem specific to BBB sinks sink.subsystem = "Demand" # A sink is simply a reaction which consumes the BBB sink.add_metabolites({met: -1}) # The sinks will be activated later (cf compute_lumps), one at a time # sink.knock_out() # The stoechiometric coefficients will be used to define the lower bound of the sink, # thus it must be stored all_sinks[met] = (sink.id, -stoech_coeff) self._tfa_model.add_reactions([sink]) # reactant already seen elif stoech_coeff < 0: # The BBB has already been associated to a sink, so we simply increase the bound of the sink all_sinks[met][1] -= stoech_coeff # Must be called before changing the reaction.thermo['computed'] values self._tfa_model.prepare() for ncrxn in self._rncore: ncrxn.thermo['computed'] = False return all_sinks
def fba_on_targets(allspecies, model): """ for each specie in allspecies, create an objective function with the current species as only product and try to optimze the model and get flux. Parameters ---------- allSpecies: list list of species ids to test model: cobra.model Cobra model from a sbml file """ #dict_output = {"positive":{},"negative":{}} for species in allspecies: #lets create a copy of the initial model model2 = model.copy() #remove all obj coef for rxn in model2.reactions: if rxn.objective_coefficient == 1.0: rxn.objective_coefficient = 0.0 #Create a new reaction that consume the given species FBA_rxn = Reaction("FBA_TEST") FBA_rxn.lower_bound = 0 FBA_rxn.upper_bound = 1000 model2.add_reactions([FBA_rxn]) FBA_rxn.objective_coefficient = 1.0 metabolitedict = {} metabolitedict[species] = -1.0 FBA_rxn.add_metabolites(metabolitedict) solution = model2.optimize() if (solution.objective_value > 1e-5): print("%s // %s %s positive" % (species, convert_from_coded_id(species.id)[0] + "_" + convert_from_coded_id(species.id)[2], solution.objective_value)) else: print("%s // %s %s NULL" % (species, convert_from_coded_id(species.id)[0] + "_" + convert_from_coded_id(species.id)[2], solution.objective_value))
def generate_new_individual(universal_index, model, base_biomass): solvable = False while solvable == False: shuffle_index = [ m for m in universal_index if m not in [v.id for v in base_biomass.keys()] ] shuffle(shuffle_index) ind = make_ind(shuffle_index) # Remove old biomass and add new one old_biomass = _get_biomass_objective_function(model) old_biomass.remove_from_model() biomass = Reaction('BIOMASS') model.add_reaction(biomass) # Add a constraint to the biomass # Meaning that the final model has to produce DNA and RNA model.reactions.BIOMASS.add_metabolites(base_biomass) for i in shuffle_index: # Add metabolites to the temporary biomass if ind.get(i) == 1: model.reactions.BIOMASS.add_metabolites({i: -0.1}) # Set new biomass and test individual model.reactions.BIOMASS.objective_coefficient = 1. solvable = feasible(model.slim_optimize()) if solvable == True: # Match the individual's metabolites with the original index # and make an ordered list accordingly ind_list = [] for i in universal_index: # Add metabolites to the temporary biomass if ind.get(i) == 1: ind_list.append(1) else: ind_list.append(0) return ind_list else: pass
def get_reference(mod, newR, delete_unbalanced, verbose): #refdb = seedrDB.loc[seedrDB['abbreviation'].isin(newR)] # vmh refdb = seedrDB.loc[seedrDB['id'].isin(newR)] refmod = Model("reaction_database") Rmod = [re.sub("_.*$", "", r.id) for r in mod.reactions] print "Consider", len(refdb.index), "reactions" Cold = 0 Calready = 0 for i, row in refdb.iterrows(): #rid = row["abbreviation"] # vmh rid = row["id"] if row["is_obsolete"] == 1: # do not add old reactions Cold += 1 continue #if rid in Rmod: # vmh elif rid in Rmod: Calready += 1 continue r = Reaction(rid + "_c0") # default seed model have compartment tag refmod.add_reaction(r) #rstr = row["formula"] # vmh rstr = row["equation"] #rstr = row["code"] rstr = rstr.replace("[0]", "_c0").replace("[1]", "_e0").replace("[3]", "_p0") if "[2]" in rstr: # TODO: consider all compartments! continue r.build_reaction_from_string(rstr, verbose=False) #r.reaction = rstr for m in refmod.metabolites: mid = re.sub("_.*$", "", m.id) hit = seedmDB.loc[seedmDB["id"] == mid] m.name = hit["name"].values[0] m.formula = hit["formula"].values[0] m.charge = hit["charge"].values[0] print Calready, "reactions already in the model" print Cold, "removed deprecated reactions" #refmod = repair_mass_balance(refmod, delete_unbalanced, verbose) print len(refmod.reactions), "remaining reaction in reference database:", return (refmod)
def add_bigg_reactions(model,BiGG_id_list): for r in BiGG_id_list: d=cobrababel.get_bigg_reaction(r, model_bigg_id='universal') reaction = Reaction(id=d['bigg_id'].replace('_e','(e)'), name = d['name'], subsystem = '', lower_bound = -1000 , upper_bound = 1000 , objective_coefficient=0.0) for k, val in enumerate(d['metabolites']): met=Metabolite(val['bigg_id'].replace('__', '_')+'_'+val['compartment_bigg_id']) met.name=val['name'] met.compartment=val['compartment_bigg_id'] reaction.add_metabolites({met:val['stoichiometry']}) if reaction not in model.reactions: model.add_reaction(reaction) return model
def create_sbml(reaction_genes, reactions_metabolites, output_file): model = Model() metabolites_created = [] for reaction_id in reaction_genes: reaction = Reaction(reaction_id) reaction.name = reaction_id reaction_metabolites = {} for reactant_id in reactions_metabolites[reaction_id][0]: if reactant_id not in metabolites_created: reactant_metabolite = Metabolite(reactant_id, compartment='c') reaction_metabolites[reactant_metabolite] = -1.0 for product_id in reactions_metabolites[reaction_id][1]: if product_id not in metabolites_created: product_metabolite = Metabolite(product_id, compartment='c') reaction_metabolites[product_metabolite] = 1.0 reaction.add_metabolites(reaction_metabolites) reaction.notes['GENE_ASSOCIATION'] = '(' + ' or ' .join(reaction_genes[reaction_id]) + ')' model.add_reactions([reaction]) write_sbml_model(model, output_file)
def _eval_metab(metab, model, exp_ess): """ This function is used to evaluate the fitness of each metabolite individually :param metab: :param model: :param exp_ess: :return: """ # Set this as warning model.solver = 'gurobi' old_biomass = list( linear_reaction_coefficients(model).keys())[0] # index removed old_biomass.remove_from_model() # Make a biomass reaction and optimize for it biomass = Reaction('BIOMASS') model.add_reaction(biomass) biomass.add_metabolites({model.metabolites.get_by_id(metab): -0.1}) model.reactions.BIOMASS.objective_coefficient = 1. # Generate deletion results --> BOTTLENECK FOR SURE deletion_results = single_gene_deletion(model, model.genes, processes=1) # Filter the results to get a boolean result a = [(str(next(iter(i))), 1) for i in deletion_results[deletion_results['growth'] > 1e-3].index] b = [(str(next(iter(i))), 0) for i in deletion_results[deletion_results['growth'] <= 1e-3].index] c = a + b pred_ess = pd.DataFrame(c, columns=['Genes', 'Predicted_growth']) compare_df = pd.merge(right=exp_ess, left=pred_ess, on='Genes', how='inner') # Apply mcc u = np.array([f for f in compare_df.Measured_growth]) v = np.array([x for x in compare_df.Predicted_growth]) return matthews_corrcoef(u, v)
def convert_to_irreversible(cobra_model): """Split reversible reactions into two irreversible reactions These two reactions will proceed in opposite directions. This guarentees that all reactions in the model will only allow positive flux values, which is useful for some modeling problems. cobra_model: A Model object which will be modified in place. """ reactions_to_add = [] coefficients = {} for reaction in cobra_model.reactions: # If a reaction is reverse only, the forward reaction (which # will be constrained to 0) will be left in the model. if reaction.lower_bound < 0: reverse_reaction = Reaction(reaction.id + "_reverse") reverse_reaction.lower_bound = max(0, -reaction.upper_bound) reverse_reaction.upper_bound = -reaction.lower_bound coefficients[ reverse_reaction] = reaction.objective_coefficient * -1 reaction.lower_bound = max(0, reaction.lower_bound) reaction.upper_bound = max(0, reaction.upper_bound) # Make the directions aware of each other reaction.notes["reflection"] = reverse_reaction.id reverse_reaction.notes["reflection"] = reaction.id reaction_dict = { k: v * -1 for k, v in iteritems(reaction._metabolites) } reverse_reaction.add_metabolites(reaction_dict) reverse_reaction._model = reaction._model reverse_reaction._genes = reaction._genes for gene in reaction._genes: gene._reaction.add(reverse_reaction) reverse_reaction.subsystem = reaction.subsystem reverse_reaction._gene_reaction_rule = reaction._gene_reaction_rule reactions_to_add.append(reverse_reaction) cobra_model.add_reactions(reactions_to_add) cobra_model.set_obejective = coefficients
def get_reference_vmh(mod, newR, verbose=1): from cobra import Model, Reaction, Metabolite import pandas import re import os dir = os.path.dirname(__file__) vmhmDB = pandas.read_csv(dir + "/../dat/vmh_metabolites.csv") vmhrDB = pandas.read_csv(dir + "/../dat/vmh_reactions.csv") refdb = vmhrDB.loc[vmhrDB['abbreviation'].isin(newR)] # vmh refmod = Model("reaction_database") Rmod = [re.sub("_.*$", "", r.id) for r in mod.reactions] print "Consider", len(refdb.index), "reactions" Cold = 0 Calready = 0 for i, row in refdb.iterrows(): rid = row["abbreviation"] # vmh rname = row["description"] if rid in Rmod: # vmh Calready += 1 continue r = Reaction(rid, rname) refmod.add_reaction(r) rstr = row["formula"] # vmh r.build_reaction_from_string(rstr, verbose=0) for m in refmod.metabolites: if verbose == 2: print m.id mid = re.sub("\\[.\\]", "", m.id) hit = vmhmDB.loc[vmhmDB["abbreviation"] == mid] m.name = hit["fullName"].values[0] m.formula = hit["chargedFormula"].values[0] m.charge = hit["charge"].values[0] if verbose > 0: print Calready, "reactions already in the model" if verbose > 0: print len( refmod.reactions), "remaining reaction in reference database:", return (refmod)
def test_model_medium(self, model): # Add a dummy 'malformed' import reaction bad_import = Reaction('bad_import') bad_import.add_metabolites({model.metabolites.pyr_c: 1}) bad_import.bounds = (0, 42) model.add_reaction(bad_import) # Test basic setting and getting methods medium = model.medium model.medium = medium assert model.medium == medium # Test context management with model: # Ensure the bounds are correct beforehand assert model.reactions.EX_glc__D_e.lower_bound == -10 assert model.reactions.bad_import.upper_bound == 42 assert model.reactions.EX_co2_e.lower_bound == -1000 # Make changes to the media new_medium = model.medium new_medium['EX_glc__D_e'] = 20 new_medium['bad_import'] = 24 del new_medium['EX_co2_e'] # Change the medium, make sure changes work model.medium = new_medium assert model.reactions.EX_glc__D_e.lower_bound == -20 assert model.reactions.bad_import.upper_bound == 24 assert model.reactions.EX_co2_e.lower_bound == 0 # Make sure changes revert after the contex assert model.reactions.EX_glc__D_e.lower_bound == -10 assert model.reactions.bad_import.upper_bound == 42 assert model.reactions.EX_co2_e.lower_bound == -1000 new_medium['bogus_rxn'] = 0 with pytest.raises(KeyError): model.medium = new_medium
def create_linear_model(A, rhs, variables, lower_bound=None, upper_bound=None): # This is a bit retarded but this way we can use the sampling functions from cobra model = Model('lin_model') # TODO have cases for lower and upper bound None rxns = np.array([ Reaction(v, lower_bound=lower_bound[v], upper_bound=upper_bound[v]) for v in variables ]) model.add_reactions(rxns) lin_variables = np.array([r.forward_variable for r in rxns]) NR, NC = A.shape constraints = [ model.problem.Constraint((A[i, :].dot(lin_variables))[0, 0], lb=np.float(rhs[:, i]), ub=np.float(rhs[:, i])) for i in range(NR) ] model.add_cons_vars(constraints) return model
def add_transporter(self, model): ev = Env_ball(1000) for reaction in ev.transporters: met_id=reaction.replace('EX_','') met_name = ev.metabolites_d[met_id] react = Reaction(reaction) react.name = 'export of ' + met_name react.lower_bound = -1000. # This is the default react.upper_bound = 1000. # This is the default if not model.metabolites.has_id(met_id): m_e = Metabolite(met_id, name=met_name,compartment='e') react.add_metabolites({m_e: -1.0}) model.add_reactions([react]) else: react.add_metabolites({model.metabolites.get_by_id(met_id): -1.0}) model.add_reactions([react]) model.repair() model.optimize()
def test_remove_genes(self): m = Model("test") m.add_reactions([Reaction("r" + str(i + 1)) for i in range(8)]) self.assertEqual(len(m.reactions), 8) rxns = m.reactions rxns.r1.gene_reaction_rule = "(a and b) or (c and a)" rxns.r2.gene_reaction_rule = "(a and b and d and e)" rxns.r3.gene_reaction_rule = "(a and b) or (b and c)" rxns.r4.gene_reaction_rule = "(f and b) or (b and c)" rxns.r5.gene_reaction_rule = "x" rxns.r6.gene_reaction_rule = "y" rxns.r7.gene_reaction_rule = "x or z" rxns.r8.gene_reaction_rule = "" self.assertIn("a", m.genes) self.assertIn("x", m.genes) delete.remove_genes(m, ["a"], remove_reactions=False) self.assertNotIn("a", m.genes) self.assertIn("x", m.genes) self.assertEqual(rxns.r1.gene_reaction_rule, "") self.assertEqual(rxns.r2.gene_reaction_rule, "") self.assertEqual(rxns.r3.gene_reaction_rule, "b and c") self.assertEqual(rxns.r4.gene_reaction_rule, "(f and b) or (b and c)") self.assertEqual(rxns.r5.gene_reaction_rule, "x") self.assertEqual(rxns.r6.gene_reaction_rule, "y") self.assertEqual(rxns.r7.genes, {m.genes.x, m.genes.z}) self.assertEqual(rxns.r8.gene_reaction_rule, "") delete.remove_genes(m, ["x"], remove_reactions=True) self.assertEqual(len(m.reactions), 7) self.assertNotIn("r5", m.reactions) self.assertNotIn("x", m.genes) self.assertEqual(rxns.r1.gene_reaction_rule, "") self.assertEqual(rxns.r2.gene_reaction_rule, "") self.assertEqual(rxns.r3.gene_reaction_rule, "b and c") self.assertEqual(rxns.r4.gene_reaction_rule, "(f and b) or (b and c)") self.assertEqual(rxns.r6.gene_reaction_rule, "y") self.assertEqual(rxns.r7.gene_reaction_rule, "z") self.assertEqual(rxns.r7.genes, {m.genes.z}) self.assertEqual(rxns.r8.gene_reaction_rule, "")
def find_egc(cobra_model, already_irreversible=False): """ try to locate EGCs by blocking all transport reactions and maximizing the objective of creating ATP from ADP + Pi """ model = cobra_model.copy() obj = Reaction("EGC_tester") obj.add_metabolites({ model.metabolites.atp_c: -1, model.metabolites.h2o_c: -1, model.metabolites.adp_c: 1, model.metabolites.pi_c: 1 }) model.add_reaction(obj) model.objective = "EGC_tester" # disable exchange reactions and ATM maintenance for r in model.reactions: if r.id[0:3] == 'EX_': r.lower_bound = 0 r.upper_bound = 0 model.reactions.ATPM.lower_bound = 0 model.reactions.ATPM.upper_bound = 0 # protons are sometimes not balanced well, so we ignore them model.reactions.EX_h_e.lower_bound = -1000 model.reactions.EX_h_e.upper_bound = 1000 FBA_sol = model.optimize() if FBA_sol.status == 'optimal' and FBA_sol.f > 1e-9: FBA_sol = \ optimize_minimal_flux(model, already_irreversible=already_irreversible) stFBA.print_solution(FBA_sol) return FBA_sol else: print('No EGCs found') return None
def assess_solvability(metabolite_list, model): from cobra import Reaction print('Generating list of solvable metabolites') solvable_metab = [] # Identify the list of metabolites that do not prevent the model to solve when added to the BOF atp_hydrolysis = ['atp', 'h2o', 'adp', 'pi', 'h', 'ppi'] for m in metabolite_list: biomass = get_biomass_objective_function(model) biomass.remove_from_model() BIOMASS = Reaction('BIOMASS') model.add_reactions([BIOMASS]) # Exclude mass balanced metabolite atp_c if m.id[:-2] not in atp_hydrolysis: model.reactions.BIOMASS.add_metabolites({m: -1.}) model.reactions.BIOMASS.objective_coefficient = 1. solution = model.optimize() # If the model can produce that metabolite if solution.f > 1e-9: solvable_metab.append(m) else: model.reactions.BIOMASS.objective_coefficient = 1. return solvable_metab
def test_add_reaction(self): old_reaction_count = len(self.model.reactions) old_metabolite_count = len(self.model.metabolites) dummy_metabolite_1 = Metabolite("test_foo_1") dummy_metabolite_2 = Metabolite("test_foo_2") actual_metabolite = self.model.metabolites[0] copy_metabolite = self.model.metabolites[1].copy() dummy_reaction = Reaction("test_foo_reaction") dummy_reaction.add_metabolites({ dummy_metabolite_1: -1, dummy_metabolite_2: 1, copy_metabolite: -2, actual_metabolite: 1 }) self.model.add_reaction(dummy_reaction) self.assertEqual(self.model.reactions.get_by_id(dummy_reaction.id), dummy_reaction) for x in [dummy_metabolite_1, dummy_metabolite_2]: self.assertEqual(self.model.metabolites.get_by_id(x.id), x) # should have added 1 reaction and 2 metabolites self.assertEqual(len(self.model.reactions), old_reaction_count + 1) self.assertEqual(len(self.model.metabolites), old_metabolite_count + 2) # tests on theadded reaction reaction_in_model = self.model.reactions.get_by_id(dummy_reaction.id) self.assertIs(type(reaction_in_model), Reaction) self.assertIs(reaction_in_model, dummy_reaction) self.assertEqual(len(reaction_in_model._metabolites), 4) for i in reaction_in_model._metabolites: self.assertEqual(type(i), Metabolite) # tests on the added metabolites met1_in_model = self.model.metabolites.get_by_id(dummy_metabolite_1.id) self.assertIs(met1_in_model, dummy_metabolite_1) #assertIsNot is not in python 2.6 copy_in_model = self.model.metabolites.get_by_id(copy_metabolite.id) self.assertTrue(copy_metabolite is not copy_in_model) self.assertIs(type(copy_in_model), Metabolite) self.assertTrue(dummy_reaction in actual_metabolite._reaction)
def generateISAreactions(self): parents_and_children = self.get_parent_and_children_in_model() objective_reactants = [ reactant.id for reactant in self.objective.reactants ] reactions = [] for parent in parents_and_children: if parent in objective_reactants: for child in parents_and_children[parent]: name = "ISA_reaction_" + child id = "ISA_reaction_" + child newISAreaction = Reaction(id=id, name=name) child_cobra_container = self.model.metabolites.get_by_id( child) parent_cobra_container = self.model.metabolites.get_by_id( parent) stoichiometry = { child_cobra_container: -1, parent_cobra_container: 1 } newISAreaction.add_metabolites(stoichiometry) reactions.append(newISAreaction) self.model.add_reactions(reactions)
def test_flux_variability_sequential_remove_cycles(self, core_model): original_objective = core_model.objective fva_solution = flux_variability_analysis(core_model, fraction_of_optimum=0.999999419892, remove_cycles=True, view=SequentialView()) assert REFERENCE_FVA_SOLUTION_ECOLI_CORE['upper_bound']['FRD7'] > 666. assert round(abs(fva_solution['upper_bound']['FRD7'] - 0.), 7) == 0 for key in fva_solution.data_frame.index: if REFERENCE_FVA_SOLUTION_ECOLI_CORE['lower_bound'][key] > -666: assert abs( fva_solution['lower_bound'][key] - REFERENCE_FVA_SOLUTION_ECOLI_CORE['lower_bound'][key]) < 0.0001 if REFERENCE_FVA_SOLUTION_ECOLI_CORE['upper_bound'][key] < 666: assert abs( fva_solution['upper_bound'][key] - REFERENCE_FVA_SOLUTION_ECOLI_CORE['upper_bound'][key]) < 0.0001 cycle_reac = Reaction("minus_PGI") # Create fake cycle cycle_reac.lower_bound = -1000 core_model.add_reaction(cycle_reac) cycle_reac.add_metabolites({met: -c for met, c in core_model.reactions.PGI.metabolites.items()}) fva_solution = flux_variability_analysis(core_model, remove_cycles=False, reactions=["PGI"]) assert fva_solution.data_frame.loc["PGI", "upper_bound"] == 1000 fva_solution = flux_variability_analysis(core_model, remove_cycles=True, reactions=["PGI"]) assert fva_solution.data_frame.loc["PGI", "upper_bound"] < 666 assert original_objective == core_model.objective
def addC(model, omit): fluxLimit = 0 rxnTMP = [] # Finding the exchange/uptake reactions modelExcUps = showExcRxns(model) # Finding the total flux carried by all uptake reactions for setting initial upper bound for the "c" export reaction modelFlux = model.optimize() for ind in modelExcUps[3]: if (modelFlux.fluxes[ind] < 0): fluxLimit = fluxLimit + abs(modelFlux.fluxes[ind]) # Making the "c" metabolite c = Metabolite('c', formula='', name='c') for ind in modelExcUps[3]: rxn = model.reactions[ind] # Skipping the "omit" reactions if (rxn.id in omit): continue # Adding "c" to the uptake reaction if (rxn.products == [] and rxn.upper_bound > 0): # Splitting bidirectional exchange reactions into uptake and export reactions rxnTMP = rxn.copy() rxnTMP.id = "TMP_" + rxnTMP.id rxnTMP.name = "TMP_" + rxnTMP.name rxnTMP.lower_bound = 0 model.add_reaction(rxnTMP) rxn.reaction = (list(rxn.reactants)[0].id + " + c <=>") rxn.upper_bound = 0 elif (rxn.products == [] and rxn.upper_bound == 0): rxn.reaction = (list(rxn.reactants)[0].id + " + c <=>") # Export - c reactionExpC = Reaction("EXP_c") reactionExpC.name = "EXP_c" reactionExpC.lower_bound = 0 reactionExpC.upper_bound = fluxLimit reactionExpC.add_metabolites({c: -1.0}) model.add_reaction(reactionExpC)