def ParseReactionFormula(name, formula): """ parse a two-sided formula such as: 2 C00001 = C00002 + C00003 return the set of substrates, products and the direction of the reaction """ try: left, right = formula.split(' = ', 1) except ValueError: raise KeggParseException("There should be exactly one '=' sign") sparse_reaction = {} for cid, amount in NistRowData.ParseReactionFormulaSide( left).iteritems(): sparse_reaction[cid] = -amount for cid, amount in NistRowData.ParseReactionFormulaSide( right).iteritems(): if (cid in sparse_reaction): raise KeggParseException( "C%05d appears on both sides of this formula" % cid) sparse_reaction[cid] = amount reaction = Reaction([name], sparse_reaction, None, '=>') kegg = Kegg.getInstance() rid = kegg.reaction2rid(reaction) or kegg.reaction2rid( reaction.reverse()) reaction.rid = rid return reaction
def row2string(S_row, cids): active_cids = list(np.nonzero(S_row)[0].flat) sparse = dict((cids[c], S_row[c]) for c in active_cids if abs(S_row[c]) > 1e-10) r = Reaction("", sparse) return r.FullReactionString(show_cids=False)
def GetReactionString(self, r, show_cids=False): rid = self.rids[r] sparse = dict([(self.cids[c], self.S[c, r]) for c in self.S[:, r].nonzero()[0].flat]) if self.fluxes[0, r] >= 0: direction = '=>' else: direction = '<=' reaction = Reaction(self.kegg.rid2string(rid), sparse, rid=rid, direction=direction) return reaction.to_hypertext(show_cids=show_cids)
def stoichiometric_matrix2html(html_writer, A, cids, eps=1e-10): """ Print a table in HTML format. A is a stoichiometric matrix where each row is a reaction and each column is a compound, corresponding in position to the list "cids". """ dict_list = [] for i in xrange(A.shape[0]): sparse_reaction = dict([(cids[j], A[i, j]) for j in xrange(A.shape[1]) if abs(A[i, j]) > eps]) r = Reaction("reaction%d" % i, sparse_reaction=sparse_reaction) dict_list.append({"reaction": r.to_hypertext()}) html_writer.write_ul( ["%d rows" % A.shape[0], "%d columns" % A.shape[1], "%d rank" % LinearRegression.MatrixRank(A)] ) html_writer.write_table(dict_list, headers=["#", "reaction"])
def runBeta2Alpha(thermo, reactionList): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/Beta2Alpha.html'), thermo=thermo, max_solutions=None, max_reactions=15, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl) add_redox_reactions(pl) for r in reactionList: pl.add_reaction(Reaction.FromFormula(r, "Auto generate #%s" % hash(r))) r = Reaction.FromFormula("C00099 => C01401") pl.find_path("Beta2Alpha", r)
def GetC1Thermodynamics( html_writer, reaction_fname='../data/thermodynamics/c1_reaction_thermodynamics.csv' ): html_writer.write("<h1>C1 thermodynamics</h1>\n") dict_list = [] db_public = SqliteDatabase('../data/public_data.sqlite') alberty = PsuedoisomerTableThermodynamics.FromDatabase(\ db_public, 'alberty_pseudoisomers', name='alberty') alberty.AddPseudoisomer(101, nH=23, z=0, nMg=0, dG0=0) reacthermo = ReactionThermodynamics(alberty, 'C1') reacthermo.pH = 7 reacthermo.I = 0.1 reacthermo.T = 298.15 reacthermo.pMg = 14 c1_reactions = [] for row in csv.DictReader(open(reaction_fname, 'r')): r = Reaction.FromFormula(row['formula']) r.Balance(balance_water=False) r.SetNames(row['enzyme']) dG0_r_prime = float(row['dG0_r_prime']) pH, I, pMg, T = [float(row[k]) for k in ['pH', 'I', 'pMg', 'T']] reacthermo.AddReaction(r, dG0_r_prime, pH=pH, I=I, pMg=pMg, T=T) c1_reactions.append(r) row['formula'] = r.to_hypertext(show_cids=False) dict_list.append(row) html_writer.write_table( dict_list, headers=['acronym', 'enzyme', 'formula', 'dG0_r_prime']) reacthermo._Recalculate() return reacthermo
def GetFullOxidationReaction(cid): kegg = Kegg.getInstance() basic_cids = [1, 7, 9, 11, 14] # H2O, O2, Pi, CO2, NH3 basic_elements = ['C', 'O', 'P', 'N', 'e-'] element_mat = np.matrix(np.zeros((len(basic_elements), len(basic_cids)))) for j in xrange(len(basic_cids)): atom_bag = kegg.cid2atom_bag(basic_cids[j]) atom_bag['e-'] = kegg.cid2num_electrons(basic_cids[j]) for i in xrange(len(basic_elements)): element_mat[i, j] = atom_bag.get(basic_elements[i], 0) cs_element_vec = np.zeros((len(basic_elements), 1)) atom_bag = kegg.cid2atom_bag(cid) atom_bag['e-'] = kegg.cid2num_electrons(cid) for i in xrange(len(basic_elements)): cs_element_vec[i, 0] = atom_bag.get(basic_elements[i], 0) x = np.linalg.inv(element_mat) * cs_element_vec sparse = dict([(basic_cids[i], np.round(x[i, 0], 3)) for i in xrange(len(basic_cids))]) sparse[cid] = -1 r = Reaction("complete oxidation of %s" % kegg.cid2name(cid), sparse) return r
def combine(fname, text_out): modules = {} for row in csv.DictReader(open(fname, 'r')): options = modules.setdefault(row['Module'], {}) enzymes = options.setdefault(row['Option'], []) reaction = Reaction.FromFormula(row['Formula']) reaction.Balance(balance_water=False, exception_if_unknown=False) enzymes.append((row['Enzyme'], row['Formula'], float(row['Flux']))) l_mod = [] for module, options in sorted(modules.iteritems()): l_opt = [] for option, enzymes in sorted(options.iteritems()): l_opt.append(("%s%s" % (module, option), enzymes)) l_mod.append(l_opt) for combination in itertools.product(*l_mod): entry = '_'.join([mod for mod, enzymes in combination]) text_out.write('ENTRY %s\n' % entry) text_out.write('THERMO merged\n') firstrow = True for mod, enzymes in combination: for enzyme, formula, flux in enzymes: if firstrow: text_out.write('REACTION %-6s %s (x%g)\n' % (enzyme, formula, flux)) firstrow = False else: text_out.write(' %-6s %s (x%g)\n' % (enzyme, formula, flux)) text_out.write('///\n')
def MakeReaction(self, rid, vec): sparse_reaction = {} for j, stoich in enumerate(vec): if stoich == 0: continue sparse_reaction[self.compound_ids[j]] = stoich return Reaction(rid, sparse_reaction)
def GetForamtionEnergies(self, thermo): self.db.CreateTable(self.GIBBS_ENERGY_TABLE_NAME, "equation TEXT, dG0 REAL, dGc REAL", drop_if_exists=True) self.db.CreateIndex('gibbs_equation_idx', self.GIBBS_ENERGY_TABLE_NAME, 'equation', unique=True, drop_if_exists=True) all_equations = set() for row in self.db.Execute("SELECT distinct(equation) FROM %s" % (self.EQUATION_TABLE_NAME)): all_equations.add(str(row[0])) from pygibbs.kegg import Kegg kegg = Kegg.getInstance() all_kegg_cids = set(kegg.get_all_cids()) for equation in all_equations: try: rxn = Reaction.FromFormula(equation) if not rxn.get_cids().issubset(all_kegg_cids): raise KeggNonCompoundException rxn.Balance(balance_water=True, exception_if_unknown=True) dG0 = thermo.GetTransfromedKeggReactionEnergies([rxn], conc=1)[0, 0] dGc = thermo.GetTransfromedKeggReactionEnergies([rxn], conc=1e-3)[0, 0] self.db.Insert(self.GIBBS_ENERGY_TABLE_NAME, [equation, dG0, dGc]) except (KeggParseException, KeggNonCompoundException, KeggReactionNotBalancedException): self.db.Insert(self.GIBBS_ENERGY_TABLE_NAME, [equation, None, None]) self.db.Commit()
def write_module_to_html(html_writer, S, rids, fluxes, cids): from pygibbs.kegg_reaction import Reaction reactions = [] for r in xrange(S.shape[0]): sparse = dict([(cids[c], S[r, c]) for c in pylab.find(S[r, :])]) reaction = Reaction('R%05d' % rids[r], sparse, rid=rids[r]) reactions.append(reaction) write_kegg_pathway(html_writer, reactions, fluxes)
def stoichiometric_matrix2html(html_writer, A, cids, eps=1e-10): """ Print a table in HTML format. A is a stoichiometric matrix where each row is a reaction and each column is a compound, corresponding in position to the list "cids". """ dict_list = [] for i in xrange(A.shape[0]): sparse_reaction = dict([(cids[j], A[i, j]) for j in xrange(A.shape[1]) if abs(A[i, j]) > eps]) r = Reaction("reaction%d" % i, sparse_reaction=sparse_reaction) dict_list.append({'reaction': r.to_hypertext()}) html_writer.write_ul([ '%d rows' % A.shape[0], '%d columns' % A.shape[1], '%d rank' % LinearRegression.MatrixRank(A) ]) html_writer.write_table(dict_list, headers=['#', 'reaction'])
def ParseReactionFormula(name, formula): """ parse a two-sided formula such as: 2 C00001 = C00002 + C00003 return the set of substrates, products and the direction of the reaction """ try: left, right = formula.split(' = ', 1) except ValueError: raise KeggParseException("There should be exactly one '=' sign") sparse_reaction = {} for cid, amount in NistRowData.ParseReactionFormulaSide(left).iteritems(): sparse_reaction[cid] = -amount for cid, amount in NistRowData.ParseReactionFormulaSide(right).iteritems(): if (cid in sparse_reaction): raise KeggParseException("C%05d appears on both sides of this formula" % cid) sparse_reaction[cid] = amount reaction = Reaction([name], sparse_reaction, None, '=>') kegg = Kegg.getInstance() rid = kegg.reaction2rid(reaction) or kegg.reaction2rid(reaction.reverse()) reaction.rid = rid return reaction
def example_formate(thermo, product_cid=22, co2_conc=1e-5): co2_hydration = Reaction.FromFormula("C00011 + C00001 => C00288") co2_hydration_dG0_prime = float(thermo.GetTransfromedKeggReactionEnergies([co2_hydration])) carbonate_conc = co2_conc * np.exp(-co2_hydration_dG0_prime / (R*default_T)) thermo.bounds[11] = (co2_conc, co2_conc) thermo.bounds[288] = (carbonate_conc, carbonate_conc) pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=20, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl, free_ATP_hydrolysis=True) add_redox_reactions(pl, NAD_only=False) pl.delete_reaction(134) # formate:NADP+ oxidoreductase pl.delete_reaction(519) # Formate:NAD+ oxidoreductase pl.delete_reaction(24) # Rubisco pl.delete_reaction(581) # L-serine:NAD+ oxidoreductase (deaminating) pl.delete_reaction(220) # L-serine ammonia-lyase pl.delete_reaction(13) # glyoxylate carboxy-lyase (dimerizing; tartronate-semialdehyde-forming) pl.delete_reaction(585) # L-Serine:pyruvate aminotransferase pl.delete_reaction(1440) # D-Xylulose-5-phosphate:formaldehyde glycolaldehydetransferase pl.delete_reaction(5338) # 3-hexulose-6-phosphate synthase pl.add_reaction(Reaction.FromFormula("C06265 => C00011", name="CO2 uptake")) pl.add_reaction(Reaction.FromFormula("C06265 => C00288", name="carbonate uptake")) pl.add_reaction(Reaction.FromFormula("C06265 => C00058", name="formate uptake")) r = Reaction.FromFormula("5 C06265 + C00058 => C%05d" % product_cid) # at least one formate to product #r.Balance() kegg = Kegg.getInstance() pl.find_path("formate to %s" % kegg.cid2name(product_cid), r)
def example_oxidative(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=10, maximal_dG=0, thermodynamic_method=OptimizationMethods.MAX_TOTAL, update_file=None) add_cofactor_reactions(pl) add_redox_reactions(pl, NAD_only=False) r = Reaction.FromFormula("C00022 => 3 C00011") #r.Balance() pl.find_path("oxidative", r)
def example_reductive(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=15, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl) add_redox_reactions(pl) r = Reaction.FromFormula("3 C00011 => C00022") #r.Balance() pl.find_path("reductive", r)
def runPathologic(thermo, reactionList): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/mog_finder.html'), thermo=thermo, max_solutions=None, max_reactions=15, maximal_dG=-3.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl) add_redox_reactions(pl) for r in reactionList: pl.add_reaction(Reaction.FromFormula(r, "Auto generate #%s" % hash(r))) pl.delete_reaction(134) pl.delete_reaction(344) pl.delete_reaction(575) pl.delete_reaction(212) #pl.add_reaction(Reaction.FromFormula('C00149 + C00006 <=> C00036 + C00005 + C00080', # 'malate + NADP+ = oxaloacetate + NADPH',343)) #pl.add_reaction(Reaction.FromFormula('C00222 + C00010 + C00006 <=> C00083 + C00005', # 'malonate-semialdehyde + CoA + NADP+ = malonyl-CoA + NADPH',740)) r = Reaction.FromFormula("2 C00288 => C00048") pl.find_path("MOG_finder", r)
def example_lower_glycolysis(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=8, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl) add_redox_reactions(pl) #r = Reaction.FromFormula("C00003 + C00118 + C00001 => C00022 + C00004 + C00009") r = Reaction.FromFormula("C00118 => C00022") #r.Balance() pl.find_path("GAP => PYR", r)
def example_three_acetate(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=20, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl) #add_redox_reactions(pl) pl.delete_reaction(761) # F6P + Pi = E4P + acetyl-P pl.delete_reaction(1621) # X5P + Pi = GA3P + acetyl-P r = Reaction.FromFormula("C00031 => 3 C00033") #r.Balance() pl.find_path("three_acetate", r)
def example_glycolysis(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=15, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl, free_ATP_hydrolysis=False) ban_toxic_compounds(pl) #add_carbon_counts(pl) #r = Reaction.FromFormula("C00031 => 6 C06265") r = Reaction.FromFormula("C00031 + 3 C00008 => 2 C00186 + 3 C00002") #r.Balance() pl.find_path("GLC => 2 LAC, 3 ATP, No methylglyoxal", r)
def example_rpi_bypass(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=10, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) add_cofactor_reactions(pl) #add_redox_reactions(pl) pl.delete_reaction(1056) # ribose-phosphate isomerase pl.delete_reaction(1081) # ribose isomerase r = Reaction.FromFormula("C00117 => C01182") #r.Balance() pl.find_path("rpi_bypass", r)
def FromCsv(csv_fname, formation_thermo): data = [] for row in csv.DictReader(open(csv_fname, 'r')): r = Reaction.FromFormula(row['formula']) r.Balance(balance_water=False) r.SetNames(row['enzyme']) dG0_r_prime = float(row['dG0_r_prime']) pH = float(row['pH']) I = float(row['I']) pMg = float(row.get('pMg', '10')) T = float(row['T']) data.append((r, dG0_r_prime, pH, I, pMg, T)) reacthermo = ReactionThermodynamics(formation_thermo) reacthermo.SetConditions(pH=pH, I=I, pMg=pMg, T=T) for r, dG0, pH, I, pMg, T in data: reacthermo.AddReaction(r, dG0, pH=pH, I=I, pMg=pMg, T=T) reacthermo._Recalculate() return reacthermo
def example_glucose_to_ethanol_and_formate(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=15, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) #add_cofactor_reactions(pl) #add_XTP_reactions(pl, '=>') #add_redox_reactions(pl) #pl.delete_reaction(761) # F6P + Pi = E4P + acetyl-P #pl.delete_reaction(1621) # X5P + Pi = GA3P + acetyl-P r = Reaction.FromFormula("2 C00031 + 3 C00001 => 6 C00058 + 3 C00469") r.Balance() pl.find_path("glucose_to_ethanol_and_formate", r)
def example_more_than_two_pyruvate(thermo): pl = Pathologic(db=SqliteDatabase('../res/gibbs.sqlite', 'r'), public_db=SqliteDatabase('../data/public_data.sqlite'), html_writer=HtmlWriter('../res/pathologic.html'), thermo=thermo, max_solutions=None, max_reactions=20, maximal_dG=0.0, thermodynamic_method=OptimizationMethods.GLOBAL, update_file=None) #add_cofactor_reactions(pl) #add_XTP_reactions(pl, '=>') #add_redox_reactions(pl) #pl.delete_reaction(761) # F6P + Pi = E4P + acetyl-P #pl.delete_reaction(1621) # X5P + Pi = GA3P + acetyl-P r = Reaction.FromFormula("3 C00031 + 3 C00011 + C00003 => 7 C00022 + 3 C00001 + C00004") r.Balance() pl.find_path("more_than_two_pyr", r)
def GetStoichiometries(self): self.db.CreateTable(self.STOICHIOMETRY_TABLE_NAME, "equation TEXT, compound TEXT, coefficient REAL", drop_if_exists=True) self.db.CreateIndex('stoichiometry_equation_idx', self.STOICHIOMETRY_TABLE_NAME, 'equation', unique=False, drop_if_exists=True) self.db.CreateIndex('stoichiometry_compound_idx', self.STOICHIOMETRY_TABLE_NAME, 'compound', unique=False, drop_if_exists=True) all_kegg_reactions = [] all_equations = [] for row in self.db.Execute("SELECT distinct(equation) FROM %s" % (self.EQUATION_TABLE_NAME)): try: r = Reaction.FromFormula(str(row[0])) all_equations.append(str(row[0])) all_kegg_reactions.append(r) except (KeggParseException, KeggNonCompoundException): pass for i, equation in enumerate(all_equations): for compound, coefficient in all_kegg_reactions[i].iteritems(): self.db.Insert(self.STOICHIOMETRY_TABLE_NAME, [equation, "cpd:C%05d" % compound, coefficient]) self.db.Commit()
def AddRedoxCarriers(self, anchored=True): redox_carriers = RedoxCarriers() for name, rc in redox_carriers.iteritems(): # make sure all redox carriers have a pKa table. # for some which don't (usually because their structure is not explicit) # we assume that the table is empty (i.e. no pKas exist). if self.dissociation.GetDissociationTable(rc.cid_ox) is None: self.dissociation.SetOnlyPseudoisomer(rc.cid_ox, rc.nH_ox, rc.z_ox) if self.dissociation.GetDissociationTable(rc.cid_red) is None: self.dissociation.SetOnlyPseudoisomer(rc.cid_red, rc.nH_red, rc.z_red) obs_id = name + " redox" self.cid2nH_nMg[rc.cid_ox] = (rc.nH_ox, 0) self.cid2nH_nMg[rc.cid_red] = (rc.nH_red, 0) sparse = {rc.cid_ox: -1, rc.cid_red: 1} dG0 = rc.ddG0_prime if not self.transformed: reaction = Reaction(obs_id, sparse) ddG0 = self.dissociation.ReverseTransformReaction( reaction, pH=rc.pH, I=0, pMg=default_pMg, T=default_T, cid2nH_nMg=self.cid2nH_nMg) dG0 -= ddG0 self.AddObservation(obs_id=obs_id, obs_type=KeggObservation.TYPE_REDOX, url="", anchored=anchored, dG0=dG0, sparse=sparse)
def add_carbon_counts(pl): """Add reactions counting carbons in various plausible fermentation products. """ reactions = [ #Reaction.FromFormula("C00246 => 4 C06265", name="Butyrate makeup"), #Reaction.FromFormula("C02632 => 4 C06265", name="Isobutyrate makeup"), Reaction.FromFormula("C00042 => 4 C06265", name="Succinate makeup"), #Reaction.FromFormula("C00022 => 3 C06265", name="Pyruvate makeup"), #Reaction.FromFormula("C00163 => 3 C06265", name="Propionate makeup"), #Reaction.FromFormula("C01013 => 3 C06265", name="3-hydroxypropionate makeup"), Reaction.FromFormula("C00186 => 3 C06265", name="Lactate makeup"), Reaction.FromFormula("C00033 => 2 C06265", name="Acetate makeup"), Reaction.FromFormula("C00469 => 2 C06265", name="Ethanol makeup"), Reaction.FromFormula("C00058 => 1 C06265", name="Formate makeup"), Reaction.FromFormula("C00011 => 1 C06265", name="CO2 makeup"), ] for rxn in reactions: pl.add_reaction(rxn) """
def pH_dependence(): analyze_this_reaction = [] I_mid = [] I_tolerance = [] T_mid = [] T_tolerance = [] analyze_this_reaction += [Reaction(['glucose kinase'], {2:-1, 31:-1, 8:1, 92:1})] I_mid += [0.01] I_tolerance += [0.02] T_mid += [303.1] T_tolerance += [0.1] analyze_this_reaction += [Reaction(['L-serine kinase'], {1:-1, 1005:-1, 9:1, 65:1})] I_mid += [0.25] I_tolerance += [0.01] T_mid += [311.15] T_tolerance += [0.1] analyze_this_reaction += [Reaction(['pyrophosphatase'], {1:-1, 13:-1, 9:2}, rid=4)] I_mid += [0.05] I_tolerance += [0.05] T_mid += [298.1] T_tolerance += [15] analyze_this_reaction += [Reaction(['Fructose bisphosphate aldolase'], {354:-1, 111:1, 118:1})] I_mid += [0.01] I_tolerance += [0.011] T_mid += [300] T_tolerance += [12] pylab.figure() pylab.rcParams['text.usetex'] = True pylab.rcParams['legend.fontsize'] = 4 pylab.rcParams['font.family'] = 'sans-serif' pylab.rcParams['font.size'] = 6 pylab.rcParams['lines.linewidth'] = 0.5 pylab.rcParams['lines.markersize'] = 3 for i, reaction in enumerate(analyze_this_reaction): pylab.subplot(2,2,i+1) logging.info("Compound parameters from Alberty's table:") for cid in reaction.get_cids(): A.cid2PseudoisomerMap(cid).Display() logging.info("Compound parameters from Hatzimanikatis' table:") for cid in reaction.get_cids(): H.cid2PseudoisomerMap(cid).Display() M_obs = [] sys.stdout.write("%5s | %5s | %5s | %6s | %6s | %s\n" % ("Match", "pH", "I", "T", "dG0(N)", "link")) for row in nist.data: if (reaction != None and reaction != row.reaction): continue try: #evaluation = row[3] # A, B, C, D if (reaction == row.reaction and abs(row.I-I_mid[i]) < I_tolerance[i] and abs(row.T-T_mid[i]) < T_tolerance[i]): M_obs.append([row.pH, row.dG0_r]) sys.stdout.write(" *** | ") else: sys.stdout.write(" | ") sys.stdout.write("%5.2f | %5.2f | %6.1f | %6.2f | %s\n" % (row.pH, row.I, row.T, row.dG0_r, row.ref_id)) except MissingCompoundFormationEnergy: continue if len(M_obs) == 0: sys.stderr.write("There are now data points matching this reaction with the specific I and T") continue M_obs = pylab.matrix(M_obs) leg = ['NIST'] pH_range = pylab.arange(M_obs[:,0].min()-1, M_obs[:,0].max()+1, 0.01) M_est = [] I_low = max(I_mid[i]-I_tolerance[i], 0.0) I_high = I_mid[i]+I_tolerance[i] for pH in pH_range: predictions = [] for predictor in [A, H]: predictions.append(reaction.PredictReactionEnergy(predictor, pH=pH, I=I_low ,T=T_mid[i])) predictions.append(reaction.PredictReactionEnergy(predictor, pH=pH, I=I_mid[i] ,T=T_mid[i])) predictions.append(reaction.PredictReactionEnergy(predictor, pH=pH, I=I_high ,T=T_mid[i])) M_est.append(predictions) M_est = pylab.matrix(M_est) pylab.plot(M_obs[:,0], M_obs[:,1], 'co') pylab.plot(pH_range, M_est[:,0], 'b:') pylab.plot(pH_range, M_est[:,1], 'b-') pylab.plot(pH_range, M_est[:,2], 'b:') pylab.plot(pH_range, M_est[:,3], 'g:') pylab.plot(pH_range, M_est[:,4], 'g-') pylab.plot(pH_range, M_est[:,5], 'g:') pylab.plot(pH_range, M_est[:,6], 'r:') pylab.plot(pH_range, M_est[:,7], 'r-') pylab.plot(pH_range, M_est[:,8], 'r:') for predictor_name in ['Alberty', 'Hatzi', 'Rugged']: for I in [I_low, I_mid[i], I_high]: leg.append("%s (I = %.2f)" % (predictor_name, I)) pylab.legend(leg, loc='best') if (i == 2 or i == 3): pylab.xlabel('pH') if (i == 0 or i == 2): pylab.ylabel(r"$\Delta_r G'^\circ$ [kJ/mol]") s_title = gc.kegg.reaction2string(reaction, cids=False) + "\n" s_title += "($I = %.2f \pm %.2f$ $M$, $T = %.1f \pm %.1f$ $K$)" % (I_mid[i], I_tolerance[i], T_mid[i]-273.15, T_tolerance[i]) pylab.title(s_title, fontsize=5) pylab.savefig('../res/compare_pH.pdf', format='pdf')
plt.legend(['value in iAF1260', 'UGCM estimation']) plt.savefig(FIG_FNAME + "_fig3.svg", fmt='.svg') db = SqliteDatabase('../res/gibbs.sqlite', 'w') ugc = UnifiedGroupContribution(db) ugc.LoadGroups(True) ugc.LoadObservations(True) ugc.LoadGroupVectors(True) ugc.LoadData(True) ugc.init() r_list = [] #r_list += [Reaction.FromFormula("C00036 + C00044 = C00011 + C00035 + C00074")] #r_list += [Reaction.FromFormula("C00003 + C00037 + C00101 = C00004 + C00011 + C00014 + C00080 + C00143")] # glycine synthase r_list += [ Reaction.FromFormula( "C00001 + C00002 + C00064 + C04376 => C00008 + C00009 + C00025 + C04640" ) ] #r_list += [Reaction.FromFormula("C00001 + 2 C00002 + C00064 + C00288 <=> 2 C00008 + C00009 + C00025 + C00169")] kegg = Kegg.getInstance() S, cids = kegg.reaction_list_to_S(r_list) logging.getLogger('').setLevel(logging.DEBUG) dG0_prime = ugc.GetTransfromedReactionEnergies(S, cids, pH=pH, I=I, pMg=pMg, T=T)
['C%05d' % all_cids[c], '%8.1f' % dG0_f[c, 0], 'Calculated']) #figure() #plot(b, dot(S_red, dG0_f[unknown_columns]), '.') figure() plot(dG0_r[known_rows], dot(S[known_rows, :], dG0_f), '.') show() csv_out = csv.writer(open('../res/acetogens_reactions.csv', 'w')) csv_out.writerow([ "RID", "EC", "REACTION", "dG0 measured", "dG0 calculated", "pH", "I", "T" ]) for r in xrange(len(reactions)): (rid, ec, sparse, dG0, pH, I, T) = reactions[r] reaction = Reaction(ec, sparse, rid=rid) dG0_calculated = dot(S[r, :], dG0_f) if r in known_rows: csv_out.writerow([ rid, ec, reaction.FullReactionString(), '%.1f' % dG0, '%.1f' % dG0_calculated, pH, I, T ]) else: csv_out.writerow([ rid, ec, reaction.FullReactionString(), 'N/A', '%.1f' % dG0_calculated, pH, I, T ])
def GetTotalReactionString(self, show_cids=False): total_S = self.S * self.fluxes.T sparse = dict([(self.cids[c], total_S[c, 0]) for c in total_S.nonzero()[0].flat]) reaction = Reaction("Total", sparse, direction="=>") return reaction.to_hypertext(show_cids=show_cids)
def write_current_solution(self, exp_html, lp, experiment_name, output_kegg_file=None): solution = lp.get_active_reaction_data() solution_id = '%03d' % lp.solution_index exp_html.write('%d reactions, flux = %g, \n' % (len(solution.reactions), float(solution.fluxes.sum(1)))) # draw network as a graph and link to it Gdot = self.kegg_pathologic.draw_pathway(solution.reactions, list(solution.fluxes.flat)) svg_fname = '%s/%s_graph' % (experiment_name, solution_id) exp_html.embed_dot_inline(Gdot, width=240, height=320, name=svg_fname) # write the solution for the concentrations in a table if solution.concentrations is not None: exp_html.insert_toggle(start_here=True) rowdicts = [] for c, compound in enumerate(solution.compounds): rowdict = {} rowdict['KEGG ID'] = '<a href="%s">C%05d</a>' % (compound.get_link(), compound.cid) rowdict['Compound'] = compound.name rowdict[symbol_df_G0_prime + "[kJ/mol]"] = solution.dG0_f[0, c] rowdict[symbol_df_G_prime + "[kJ/mol]"] = solution.dG_f[0, c] if np.isfinite(solution.concentrations[0, c]): rowdict['Conc. [M]'] = '%.1e' % solution.concentrations[0, c] else: rowdict['Conc. [M]'] = 'N/A' rowdicts.append(rowdict) headers=['KEGG ID', 'Compound', symbol_df_G0_prime + "[kJ/mol]", symbol_df_G_prime + "[kJ/mol]", 'Conc. [M]'] exp_html.write('Compound Concentrations<br>\n') exp_html.write_table(rowdicts, headers, decimal=1) total_reaction = Reaction('total', {}) rowdicts = [] for r, reaction in enumerate(solution.reactions): rowdict = {} flux = solution.fluxes[0, r] rowdict['KEGG ID'] = '<a href="%s">R%05d</a>' % (reaction.get_link(), reaction.rid) rowdict['Reaction'] = reaction.to_hypertext(show_cids=False) rowdict['Flux'] = flux rowdict[symbol_dr_Gc_prime + "[kJ/mol]"] = solution.dGc_r[0, r] rowdict[symbol_dr_G_prime + "[kJ/mol]"] = solution.dG_r[0, r] rowdicts.append(rowdict) total_reaction += (flux * reaction) rowdict = {} rowdict['KEGG ID'] = 'total' rowdict['Reaction'] = total_reaction.to_hypertext(show_cids=False) rowdict[symbol_dr_Gc_prime + "[kJ/mol]"] = float(solution.dGc_r.sum(1)) rowdict[symbol_dr_G_prime + "[kJ/mol]"] = float(solution.dG_r.sum(1)) rowdicts.append(rowdict) headers=['KEGG ID', 'Reaction', symbol_dr_Gc_prime + "[kJ/mol]", symbol_dr_G_prime + "[kJ/mol]", "Flux"] exp_html.write('Reaction Gibbs energies<br>\n') exp_html.write_table(rowdicts, headers, decimal=1) exp_html.div_end() # write the pathway in KEGG format if output_kegg_file is not None: write_kegg_pathway(output_kegg_file, entry=experiment_name + ' ' + solution_id, reactions=solution.reactions, fluxes=list(solution.fluxes.flat)) exp_html.write('<br>\n')
ugc.init() if args.test: r_list = [] # r_list += [Reaction.FromFormula("C00002 + C00001 = C00008 + C00009")] # r_list += [Reaction.FromFormula("C00036 + C00024 = C00022 + C00083")] # r_list += [Reaction.FromFormula("C00036 + C00100 = C00022 + C00683")] # r_list += [Reaction.FromFormula("C01013 + C00010 + C00002 = C05668 + C00020 + C00013")] # r_list += [Reaction.FromFormula("C00091 + C00005 = C00232 + C00010 + C00006")] # r_list += [Reaction.FromFormula("C00002 + C00493 = C00008 + C03175")] # r_list += [Reaction.FromFormula("C00243 + C00125 = C05403 + C00126")] # r_list += [Reaction.FromFormula("2 C00206 = C00360 + C00131")] # r_list += [Reaction.FromFormula("C04171 + C00003 = C00196 + C00080 + C00004")] # r_list += [Reaction.FromFormula("C00036 + C00044 = C00011 + C00035 + C00074")] r_list += [ Reaction.FromFormula( "C00001 + C00022 + C17569 = C00011 + C00033 + C00390") ] kegg = Kegg.getInstance() S, cids = kegg.reaction_list_to_S(r_list) dG0_prime = ugc.GetTransfromedReactionEnergies(S, cids, pH=7.0, I=0.15) ln_conc = np.matrix(np.ones((1, S.shape[0]))) * np.log(0.001) if 1 in cids: ln_conc[0, cids.index(1)] = 0 # H2O should have a concentration of 1 RT = R * default_T dGc_prime = dG0_prime + RT * ln_conc * S for i in xrange(len(r_list)): r_list[i].Balance()
def FromFiles(): feist = Feist() reaction_file = '../data/metabolic_models/iAF1260_reactions.csv' # Read compounds ids 2 Kegg cid's mapping file into a dict counters = { 'kegg_error': 0, 'translocation': 0, 'exchange': 0, 'sink': 0, 'unbalanced': 0, 'okay': 0 } for row in csv.DictReader(open(reaction_file, 'r')): #if 'Transport' in row['subSystem']: # counters['translocation'] += 1 # continue if row['abbreviation'][0:3] == 'EX_': counters['exchange'] += 1 continue if row['abbreviation'][0:3] == 'DM_': counters['sink'] += 1 continue sparse = Feist.parse_equation(row['equation']) kegg_sparse = {} for biggID, coeff in sparse.iteritems(): keggID = feist.bigg2kegg[biggID] kegg_sparse[keggID] = kegg_sparse.get(keggID, 0) + coeff if 0 in kegg_sparse: logging.debug('Some compounds are missing KEGG IDs in %s: %s' % (row['abbreviation'], row['equation'])) counters['kegg_error'] += 1 continue for keggID in [k for k, v in kegg_sparse.iteritems() if v == 0]: del kegg_sparse[keggID] directionality = row["directionality without uncertainty (pH 7.2)"] #directionaliry = row['reconstruction directionality'] if directionality == 'reversible': direction = '<=>' elif directionality == 'forward only': direction = '=>' elif directionality == 'reverse only': direction = '<=' else: raise ValueError('unknown directionality tag: ' + directionality) reaction = Reaction(row['abbreviation'], kegg_sparse, direction=direction) if row['delta G (pH 7)'] == 'Not calculated': dG0 = np.nan else: dG0 = float(row['delta G (pH 7)']) * J_per_cal try: reaction.Balance(balance_water=True, exception_if_unknown=False) counters['okay'] += 1 except KeggReactionNotBalancedException as e: logging.debug(str(e) + ' - ' + str(reaction)) counters['unbalanced'] += 1 continue feist.reactions.append(reaction) feist.dG0s.append(dG0) logging.debug(" ; ".join( ["%s : %d" % (key, val) for (key, val) in counters.iteritems()])) return feist
H_nopka = Hatzi(use_pKa=False) H_withpka = Hatzi(use_pKa=True) H_withpka.ToDatabase(db, 'hatzi_thermodynamics') #H.ToDatabase(db, 'hatzi_gc') #H.I = 0.25 #H.T = 300; #sparse_reaction = {13:-1, 1:-1, 9:2} #sparse_reaction = {36:-1, 3981:1} #sparse_reaction = {6:-1, 143:-1, 234:1, 5:1} #sparse_reaction = {1:-1, 499:-1, 603:1, 86:1} #sparse_reaction = {1:-1, 6:-1, 311:-1, 288:1, 5:1, 80:2, 26:1} #sparse_reaction = {408:-1, 6:-1, 4092:1, 5:1} #sparse_reaction = {588:-1, 1:-1, 114:1, 9:1} #sparse_reaction = {1:-1, 3:-1, 149:-1, 288:1, 4:1, 80:2, 22:1} react = Reaction("reaction", {408:-1, 6:-1, 4092:1, 5:1}) #sys.stdout.write("The dG0_r of PPi + H20 <=> 2 Pi: \n\n") react.Balance() print react.FullReactionString() sys.stdout.write("%5s | %5s | %6s | %6s\n" % ("pH", "I", "T", "dG0_r")) for pH in np.arange(5, 10.01, 0.25): H_withpka.pH = pH sys.stdout.write("%5.2f | %5.2f | %6.1f | %6.2f\n" % (H_withpka.pH, H_withpka.I, H_withpka.T, react.PredictReactionEnergy(H_withpka))) for cid in react.get_cids(): print '-'*50