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)
['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 ])
#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 print "C%05d - %s:" % (cid, H_withpka.kegg.cid2name(cid)) print H_withpka.kegg.cid2inchi(cid) print "Pseudoisomers:\n", H_withpka.cid2PseudoisomerMap(cid) print "dG0'_f = %.1f kJ/mol" % H_withpka.cid2PseudoisomerMap( cid).Transform(pH=7, I=0, pMg=10, T=298.15)
def ConvertFormation2Reaction(self, output_fname): logging.info("Converting all formation energies to reactions") output_csv = csv.writer(open(output_fname, 'w')) # keep the format used for TECRDB output_csv.writerow( ('ref', 'ID', 'method', 'eval', 'EC', 'name', 'kegg_reaction', 'reaction', 'dG0\'', 'T', 'I', 'pH', 'pMg')) atom2cid = {} for atom, (name, stoich) in KeggObservation.ATOM2ELEMENT.iteritems(): cid, _, _ = self.kegg.name2cid(name, 0) if cid is None: raise Exception( "Cannot find the element %s in the KEGG database" % name) atom2cid[atom] = (cid, stoich) #output_csv.writerow(('element', # 'C%05d' % cid, 'formation', 'A', '', # 'formation of %s' % self.kegg.cid2name(cid), # "C%05d" % cid, # name, 0, self.T, self.I, self.pH, self.pMg)) for label in ['training', 'testing']: ptable = PsuedoisomerTableThermodynamics.FromCsvFile( self.FormationEnergyFileName, label=label) for cid in ptable.get_all_cids(): pmatrix = ptable.cid2PseudoisomerMap(cid).ToMatrix() if len(pmatrix) != 1: raise Exception("multiple training species for C%05d" % cid) nH, _charge, nMg, dG0 = pmatrix[0] diss_table = dissociation.GetDissociationTable(cid, False) if diss_table is None: continue diss_table.SetFormationEnergyByNumHydrogens(dG0, nH, nMg) dG0_prime = diss_table.Transform(pH=self.pH, I=self.I, pMg=self.pMg, T=self.T) ref = ptable.cid2SourceString(cid) atom_bag = self.kegg.cid2atom_bag(cid) if not atom_bag: continue ne = self.kegg.cid2num_electrons(cid) elem_ne = 0 sparse = {cid: 1} for elem, count in atom_bag.iteritems(): if elem == 'H': continue elem_ne += count * Molecule.GetAtomicNum(elem) elem_cid, elem_coeff = atom2cid[elem] sparse.setdefault(elem_cid, 0) sparse[elem_cid] += -count * elem_coeff # use the H element to balance the electrons in the formation # reactions (we don't need to balance protons since this is # a biochemical reaction, so H+ are 'free'). H_cid, H_coeff = atom2cid['H'] sparse[H_cid] = (elem_ne - ne) * H_coeff reaction = Reaction( "formation of %s" % self.kegg.cid2name(cid), sparse) output_csv.writerow( (ref, 'C%05d' % cid, 'formation', 'A', '', 'formation of %s' % self.kegg.cid2name(cid), reaction.FullReactionString(), reaction.FullReactionString(show_cids=False), '%.2f' % dG0_prime, self.T, self.I, self.pH, self.pMg))