def _make_ccache(self):
     pseudo = json.load(open('/home/user/code/thermodynamics/thermodynamics_data/kegg_pseudoisomers.json'))
     rxns = []
     cnt = 0
     for i, item in enumerate(pseudo):
         rxns.append('1 ' + item['CID'] + ' <=> 1 ' + item['CID'])
         cnt = cnt + 1
         if cnt>2000:
             model = KeggModel.from_formulas(rxns) 
             model.ccache.dump()
             rxns = []
             cnt = 0
     model = KeggModel.from_formulas(rxns) 
     model.ccache.dump()
def calculate_deltaG0s(model,
                       kegg_compounds,
                       pH=default_pH,
                       I=default_I,
                       T=default_T):
    """ Calculate standard Gibbs Energy for reactions in model (as many as possible) using eQuilibrator.

    Args:
        model (CBModel): model
        kegg_compounds (list): KEGG compounds accepted by eQuilibrator
        pH (float): pH (default: 7.0)
        I (float): ionic strenght (default: 0.25)
        T (float): temperature (default: 298.15 K (25.0 C))

    Returns:
        dict: standard Gibbs Energies indexed by reaction ids
        dict: estimation error indexed by reaction ids

    """
    kegg_rxns = build_kegg_reactions(model, kegg_compounds)
    kmodel = KeggModel.from_formulas(kegg_rxns.values(), raise_exception=True)
    kmodel.add_thermo(CC.init())
    dG0, sdG0, _ = kmodel.get_transformed_dG0(pH, I, T)

    dG0 = dict(zip(kegg_rxns.keys(), dG0.A1))
    sdG0 = dict(zip(kegg_rxns.keys(), sdG0.A1))

    return dG0, sdG0
 def _make_ccache(self):
     pseudo = json.load(
         open(
             '/home/user/code/thermodynamics/thermodynamics_data/kegg_pseudoisomers.json'
         ))
     rxns = []
     cnt = 0
     for i, item in enumerate(pseudo):
         rxns.append('1 ' + item['CID'] + ' <=> 1 ' + item['CID'])
         cnt = cnt + 1
         if cnt > 2000:
             model = KeggModel.from_formulas(rxns)
             model.ccache.dump()
             rxns = []
             cnt = 0
     model = KeggModel.from_formulas(rxns)
     model.ccache.dump()
Exemple #4
0
    def generate_kegg_model(self, reactions):

        rstrings = []
        for r in reactions:
            k = r.kegg_reaction
            if k:
                if k.is_balanced() and not k.is_empty():
                    rstrings.append(k.write_formula())
                    self.reactions.append(r)
            else:
                self._not_balanced.append(r)
        return KeggModel.from_formulas(rstrings)
 def generate_kegg_model(self, reactions):
     
     rstrings = []
     for r in reactions:
         k = r.kegg_reaction
         if k:
             if k.is_balanced() and not k.is_empty():
                 rstrings.append(k.write_formula())
                 self.reactions.append(r)
         else:
             self._not_balanced.append(r)
     return KeggModel.from_formulas(rstrings)
def KeggFile2ModelList(pathway_file):
    kegg_file = ParsedKeggFile.FromKeggFile(pathway_file)
    entries = kegg_file.entries()
    pathways = []
    for entry in entries:
        fields = kegg_file[entry]
        rids, fluxes, reactions = ParsedKeggFile.ParseReactionModule(fields)
        bounds = ParsedKeggFile.ParseBoundModule(fields)
        model = KeggModel.from_formulas(reactions)
        model.rids = rids
        pH = fields.GetFloatField("PH", 7.5)
        I = fields.GetFloatField("I", 0.2)
        T = fields.GetFloatField("T", 298.15)
        pathways.append({"entry": entry, "model": model, "fluxes": fluxes, "bounds": bounds, "pH": pH, "I": I, "T": T})
    return pathways
Exemple #7
0
    def GenerateKeggModel(sbtab_dict):
        met2kegg = sbtab_dict.GetDictFromTable('Compound', 'Compound',
            'Compound:Identifiers:kegg.compound')
        
        reaction_names = sbtab_dict.GetColumnFromTable('Reaction', 'Reaction')
        reaction_formulas = sbtab_dict.GetColumnFromTable('Reaction', 'SumFormula')
        sparse_reactions = map(ParseReaction, reaction_formulas)
        
        map_met2kegg = lambda spr : {met2kegg.get(k): v for (k,v) in spr.iteritems()}
        sparse_reactions_kegg = map(map_met2kegg, sparse_reactions)
        kegg_reactions = [KeggReaction(s, rid=rid) for (s, rid) 
                          in zip(sparse_reactions_kegg, reaction_names)]

        model = KeggModel.from_kegg_reactions(kegg_reactions, has_reaction_ids=True)
        return model
Exemple #8
0
def KeggFile2ModelList(pathway_file):
    kegg_file = ParsedKeggFile.FromKeggFile(pathway_file)
    entries = kegg_file.entries()
    pathways = []
    for entry in entries:
        fields = kegg_file[entry]
        rids, fluxes, reactions = ParsedKeggFile.ParseReactionModule(fields)
        bounds = ParsedKeggFile.ParseBoundModule(fields)
        model = KeggModel.from_formulas(reactions)
        model.rids = rids
        pH = fields.GetFloatField('PH', 7.5)
        I = fields.GetFloatField('I', 0.2)
        T = fields.GetFloatField('T', 298.15)
        pathways.append({'entry': entry, 'model': model, 'fluxes': fluxes,
                         'bounds': bounds, 'pH': pH, 'I': I, 'T': T})
    return pathways
    def _component_contribution_wrapper(self, reaction_list_I, pH_I,
                                        temperature_I, ionic_strength_I):
        """wrapper for component contribution"""

        # Orginal implementation involves an input of a reaction list
        # the dG_prime and dG_std for each reaction are then calculated.
        # Because we are interested in accounting for transportation
        # and metabolite concentration,
        # we need to extraction out the dG_prime and dG_std
        # of formation and calculated the dG_prime and
        # dG_std for each reaction after accounting for
        # transportation and metabolite concentration.
        # TODO: change input from reaction list to metabolite list
        # however this may be problematic due to the implementation
        # of component_contribution (i.e., the group contribution
        # and reactant contribution are calculated along orthoganol planes
        # in order to gain greater coverage and accuracy of predicted
        # dG_f values)

        # Debugging:
        #wolf = ['C00049 + C00002 <=> C03082 + C00008',
        #        'C00026 + C00049 <=> C00025 + C00036',
        #        'C00158 <=> C00311',
        #        'C00631 <=> C00001 + C00074',
        #        'C00354 <=> C00111 + C00118',
        #        'C00020 + C00002 <=> 2 C00008',
        #        'C00002 + C00001 <=> C00008 + C00009',
        #        'C00015 + C00002 <=> C00075 + C00008',
        #        'C00033 + C00002 + C00010 <=> C00024 + C00020 + C00013']
        #model = KeggModel.from_formulas(wolf)

        model = KeggModel.from_formulas(reaction_list_I)

        td = TrainingData()
        cc = ComponentContribution(td)
        model.add_thermo(cc)

        dG0_prime_f, dG0_var_f = model.get_transformed_dG0(pH=pH_I,
                                                           I=ionic_strength_I,
                                                           T=temperature_I)

        return dG0_prime_f, dG0_var_f
Exemple #10
0
    def reaction2udG0_prime(self):
        '''
            Calculates the dG0 of a list of a reaction.
            Uses the component-contribution package (Noor et al) to estimate
            the standard Gibbs Free Energy of reactions based on 
            component contribution  approach and measured values (NIST and Alberty)
            
            Arguments:
                List of cobra model reaction ids
            Returns:
                Array of dG0 values and standard deviation of estimates
        '''

        Kmodel = KeggModel.from_formulas(self.reaction_strings)
        Kmodel.add_thermo(self.cc)
        dG0_prime, dG0_cov = Kmodel.get_transformed_dG0(pH=7.5,
                                                        I=0.2,
                                                        T=298.15)
        dG0_std = 1.96 * np.diag(dG0_cov.round(1))
        return unumpy.uarray((dG0_prime.flat, dG0_std.flat))
Exemple #11
0
    def __init__(self):
        self.prepare_mappings()

        reactions = self.get_reactions_from_model()
        self.reactions = []
        self._not_balanced = []

        self.rstrings = []
        for r in reactions:
            k = r.kegg_reaction
            if k:
                if k.is_balanced() and not k.is_empty():
                    self.rstrings.append(k.write_formula())
                    self.reactions.append(r)
            else:
                self._not_balanced.append(r)
        self.Kmodel = KeggModel.from_formulas(self.rstrings)

        self.cc = ComponentContribution.init()
        self.pH = 7.3
        self.I = 0.25
        self.RT = R * default_T
Exemple #12
0
def reaction2dG0(reaction_list):
    '''
        Calculates the dG0 of a list of a reaction.
        Uses the component-contribution package (Noor et al) to estimate
        the standard Gibbs Free Energy of reactions based on 
        component contribution  approach and measured values (NIST and Alberty)
        
        Arguments:
            List of reaction strings
        Returns:
            Array of dG0 values and standard deviation of estimates
    '''
    cc = ComponentContribution.init()
    
    Kmodel = KeggModel.from_formulas(reaction_list)

    Kmodel.add_thermo(cc)
    dG0_prime, dG0_std = Kmodel.get_transformed_dG0(pH=7.5, I=0.2, T=298.15)
    dG0_prime = np.array(map(lambda x: x[0,0], dG0_prime))
    
    dG0_prime = unumpy.uarray(dG0_prime, np.diag(dG0_std))          
    return dG0_prime
    def __init__(self):
        self.prepare_mappings()

        reactions = self.get_reactions_from_model()
        self.reactions = []
        self._not_balanced = []

        self.rstrings = []
        for r in reactions:
            k = r.kegg_reaction
            if k:
                if k.is_balanced() and not k.is_empty():
                    self.rstrings.append(k.write_formula())
                    self.reactions.append(r)
            else:
                self._not_balanced.append(r)
        self.Kmodel = KeggModel.from_formulas(self.rstrings)

        self.cc = ComponentContribution.init()
        self.pH = 7.3
        self.I = 0.25
        self.RT = R * default_T
    def _component_contribution_wrapper(self,reaction_list_I,pH_I,temperature_I,ionic_strength_I):
        """wrapper for component contribution"""

        # Orginal implementation involves an input of a reaction list
        # the dG_prime and dG_std for each reaction are then calculated.
        # Because we are interested in accounting for transportation
        # and metabolite concentration,
        # we need to extraction out the dG_prime and dG_std
        # of formation and calculated the dG_prime and
        # dG_std for each reaction after accounting for
        # transportation and metabolite concentration.
        # TODO: change input from reaction list to metabolite list
        # however this may be problematic due to the implementation
        # of component_contribution (i.e., the group contribution
        # and reactant contribution are calculated along orthoganol planes
        # in order to gain greater coverage and accuracy of predicted
        # dG_f values)

        # Debugging:
        #wolf = ['C00049 + C00002 <=> C03082 + C00008',
        #        'C00026 + C00049 <=> C00025 + C00036',
        #        'C00158 <=> C00311',
        #        'C00631 <=> C00001 + C00074',
        #        'C00354 <=> C00111 + C00118',
        #        'C00020 + C00002 <=> 2 C00008',
        #        'C00002 + C00001 <=> C00008 + C00009',
        #        'C00015 + C00002 <=> C00075 + C00008',
        #        'C00033 + C00002 + C00010 <=> C00024 + C00020 + C00013']
        #model = KeggModel.from_formulas(wolf) 

        model = KeggModel.from_formulas(reaction_list_I)    

        td = TrainingData()
        cc = ComponentContribution(td)
        model.add_thermo(cc)
    
        dG0_prime_f, dG0_var_f = model.get_transformed_dG0(pH=pH_I, I=ionic_strength_I, T=temperature_I)

        return dG0_prime_f, dG0_var_f
import numpy as np
from component_contribution.thermodynamic_constants import default_RT
from component_contribution.component_contribution_trainer import ComponentContribution
from component_contribution.kegg_model import KeggModel
import os

example_path = os.path.dirname(os.path.realpath(__file__))
REACTION_FNAME = os.path.join(example_path, 'wolf_reactions.txt')

cc = ComponentContribution.init()
with open(REACTION_FNAME, 'r') as fp:
    reaction_strings = fp.readlines()
model = KeggModel.from_formulas(reaction_strings, raise_exception=True)

model.add_thermo(cc)
dG0_prime, dG0_std, sqrt_Sigma = model.get_transformed_dG0(7.0, 0.1, 298.15)

mM_conc = 1e-3 * np.matrix(np.ones((len(model.cids), 1)))
if 'C00001' in model.cids:
    mM_conc[model.cids.index('C00001'), 0] = 1.0
dGm_prime = dG0_prime + default_RT * model.S.T * np.log(mM_conc)

for i, r in enumerate(reaction_strings):
    print '-' * 50
    print r.strip()
    print "dG0  = %8.1f +- %5.1f" % (model.dG0[i, 0], dG0_std[i, 0] * 1.96)
    print "dG'0 = %8.1f +- %5.1f" % (dG0_prime[i, 0], dG0_std[i, 0] * 1.96)
    print "dG'm = %8.1f +- %5.1f" % (dGm_prime[i, 0], dG0_std[i, 0] * 1.96)
Exemple #16
0
import logging
from scipy.io import savemat
from component_contribution.component_contribution import ComponentContribution
from component_contribution.kegg_model import KeggModel

REACTION_FNAME = '../examples/simeon_reactions.txt'
OUTPUT_FNAME = '../res/simeon.mat'

logger = logging.getLogger('')
logger.setLevel(logging.INFO)

cc = ComponentContribution.init()
reaction_strings = open(REACTION_FNAME, 'r').readlines()
model = KeggModel.from_formulas(reaction_strings)
model.add_thermo(cc)

dG0_prime, dG0_std, _ = model.get_transformed_dG0(7.0, 0.2, 298.15)

print "For a linear problem, define two vector variables 'x' and 'y', each of length Nr (i.e. " + \
      "the same length as the list of reactions). Then add these following " + \
      "constraints: \n" + \
      "-1 <= y <= 1\n" + \
      "x = dG0_prime + 3 * dG0_std * y\n" + \
      "Then use 'x' as the value of the standard Gibbs energy for all reactions."
print "The results are writteng to: " + OUTPUT_FNAME

savemat(OUTPUT_FNAME,
        {'dG0_prime': dG0_prime, 'dG0_std': dG0_std},
        oned_as='row')