Exemplo n.º 1
0
    def _reaction2string(self):
        '''
            map COBRA reactions to reactions strings with KEGG cids, e.g.,
            "2 C19610 + C00027 + 2 C00080 <=> 2 C19611 + 2 C00001"
            
            Arguments:
                List of model reaction ids
            Returns:
                Reaction strings
        '''
        reaction_list = map(self.model.reactions.get_by_id, self.reactions)
        reaction_strings = []
        reaction_sparses = []
        indices = set()

        for i, r in enumerate(reaction_list):

            metabolites = map(lambda x: x.id, r.metabolites)
            CIDS = self._metabolite2cid(metabolites)

            # if not all reactants could be mapped to kegg CIDS,
            #return empty reaction string
            if None in CIDS:
                indices.add(i)
                warnings.warn("%s was removed from reaction_list. " % r.id +
                              "see class reactions for updated list")
                continue

            reactant2cid = dict(zip(metabolites, CIDS))
            sparse = {
                reactant2cid[m.id]: v
                for m, v in r.metabolites.iteritems()
            }

            # remove protons (H+) from reactions
            if 'C00080' in sparse:
                del sparse['C00080']

            assert r not in reaction_strings, 'Duplicate reaction!'

            try:
                kegg_reaction = KeggReaction(sparse)
                if kegg_reaction.is_balanced():
                    reaction_strings.append(str(KeggReaction(sparse)))
                    reaction_sparses.append(sparse)
                else:
                    indices.add(i)
            except TypeError:
                warnings.warn("%s not be converted to reaction string" %
                              str(sparse))

        for i in sorted(indices, reverse=True):
            del self.reactions[i]

        return reaction_sparses, reaction_strings
Exemplo n.º 2
0
    def get_reactions_from_model(self):
        """
            Read all the reaction descriptions in the SBML file, and
            map them to KEGG reaction (using another file of all
            E. coli metabolites and their BiGG and KEGG IDs)
        """
        cobra_model = create_cobra_model_from_sbml_file(
            settings.ECOLI_SBML_FNAME)
        for m in cobra_model.metabolites:
            m.CID = self.bigg2kegg.get(m.id[:-2], None)

        for r in cobra_model.reactions:
            CIDS = dict(
                zip(r.metabolites.keys(),
                    map(lambda x: x.CID, r.metabolites.keys())))
            if None in CIDS.values():
                r.kegg_reaction = None
            else:
                sparse = {
                    CIDS[m]: v
                    for m, v in r.metabolites.iteritems()
                    if CIDS[m] != 'C00080'
                }
                r.kegg_reaction = KeggReaction(sparse)

        return cobra_model.reactions
Exemplo n.º 3
0
def reaction2RI(reaction_list, fixed_conc=0.1):
    '''
        Calculates the reversibility index (RI) of a reaction.
        The RI represent the change in concentrations of metabolites
        (from equal reaction reactants) that will make the reaction reversible.
        That is, the higher RI is, the more irreversible the reaction.
        A convenient threshold for reversibility is RI>=1000, that is a change of
        1000% in metabolite concentrations is required in order to flip the
        reaction direction. 
        
        Arguments:
            List of cobra model reaction objects
        Returns:
            Array of RI values
    '''
    keq = reaction2Keq(reaction_list)
    
    sparse = map(lambda x: KeggReaction.parse_formula(x).sparse, reaction_list)
    
    N_P = np.zeros(len(sparse))
    N_S = np.zeros(len(sparse))    
    for i,s in enumerate(sparse):   
        N_P[i] = sum([v for v in s.itervalues() if v>0])
        N_S[i] = -sum([v for v in s.itervalues() if v<0])
    N = N_P + N_S
    Q_2prime = fixed_conc**(N_P-N_S)
    
    RI = ( keq*Q_2prime )**( 2.0/N )
    return RI
Exemplo n.º 4
0
    def add_to_reac(self, cobra_model):

        for r in cobra_model.reactions:
            CIDS = dict(
                zip(r.metabolites.keys(),
                    map(lambda x: x.CID, r.metabolites.keys())))
            if None in CIDS.values():
                r.kegg_reaction = None
            else:
                sparse = {
                    CIDS[m]: v
                    for m, v in r.metabolites.iteritems()
                    if CIDS[m] != 'C00080'
                }
                r.kegg_reaction = KeggReaction(sparse)
Exemplo n.º 5
0
def reaction2RI(reaction_list, metabolite_list, fixed_conc=0.1):
    '''
        Calculates the reversibility index (RI) of a reaction.
        The RI represent the change in concentrations of metabolites
        (from equal reaction reactants) that will make the reaction reversible.
        That is, the higher RI is, the more irreversible the reaction.
        A convenient threshold for reversibility is RI>=1000, that is a change of
        1000% in metabolite concentrations is required in order to flip the
        reaction direction. 
        
        ToDo
        Includes fixed meatbolite concentrations for the measured metabolites 
        
        Arguments:
            List of cobra model reaction objects
        Returns:
            Array of RI values
    '''
    keq = reaction2Keq(reaction_list)
    
    print metabolite_list
    sparse = map(lambda x: KeggReaction.parse_formula(x).sparse, reaction_list)

    # map concentrations to reactions
    N_u = np.array(map(lambda x: len(set(x.keys())-set(metabolite_list.keys())), sparse))

    concs = [{k:metabolite_list[k] if k in metabolite_list.keys() 
                                    else fixed_conc 
                                    for k in l.iterkeys()} 
                                    for l in sparse] #new array with concentrations instead of stoichiometric values
    
    Q =  np.zeros(len(concs))
    for i, (c, s) in enumerate(zip(concs, sparse)):
        Q[i] = np.prod(np.array([v**s[k] for k, v in c.iteritems()]))
    # ToDo: if all concentration are known, take only Keq/Q
    RI = ( keq/Q )**( 2.0/N_u )
    return RI,sparse, Q, N_u
                       help='path to a text file containing the reactions')
    parser.add_argument('--out_file', type=argparse.FileType('wb'),
                       help='path to the output .mat file')
    
    args = parser.parse_args()

    ccache = CompoundCacher()
    groups_data = inchi2gv.init_groups_data()
    decomposer = inchi2gv.InChIDecomposer(groups_data)
    w, b, G, cids, S = map(loadmat(args.train_file).get, ['w', 'b', 'G', 'cids', 'S'])
    cids = list(cids.flat)
    Nc, Ng = G.shape
    
    model_X = []
    model_G = []
    for line in args.rxn_file.readlines():
        reaction = KeggReaction.parse_formula(line)
        try:
            x, g = decompose_reaction(ccache, decomposer, cids, G, reaction)
        except inchi2gv.GroupDecompositionError:
            x = np.zeros((Nc, 1))
            g = np.zeros((Ng, 1))
        model_X.append(list(x.flat))
        model_G.append(list(g.flat))

    mdict = {
                'X' : np.array(model_X).T,
                'G': np.array(model_G).T
            }
    savemat(args.out_file, mdict, appendmat=False, format='5', 
            long_field_names=False, do_compression=True, oned_as='row')
Exemplo n.º 7
0
                        type=float,
                        help='pH increment.')

    args = parser.parse_args()

    I = args.ionic_strength
    T = default_T

    cc = ComponentContribution.init()
    pHs = np.arange(args.pH_min, args.pH_max + args.pH_step, args.pH_step)

    reactions_and_energies = []
    reader = csv.reader(args.infile)
    for row in reader:
        formula = row[0].strip()
        reaction = KeggReaction.parse_formula(formula)
        reaction_atom_bag = reaction._get_reaction_atom_bag()
        n_e = reaction_atom_bag.pop('e-', 0)
        if len(reaction_atom_bag) != 0:
            raise ValueError('This is not a half-reaction'
                             ' (i.e. cannot be balanced by adding e-)')

        dG0_r, u_r = cc.get_dG0_r(reaction)
        E0s = []
        for pH in pHs:
            ddG0_r = reaction.get_transform_ddG0(pH=pH, I=I, T=T)
            dG0_r_prime = dG0_r + ddG0_r
            E0_prime = 1000 * -dG0_r_prime / (n_e * F)  # mV
            E0s.append(E0_prime)

        reactions_and_energies.append((row, E0s))
Exemplo n.º 8
0
"""
from component_contribution.component_contribution_trainer import ComponentContribution
from component_contribution.kegg_reaction import KeggReaction
from component_contribution.thermodynamic_constants import F, default_T

#pH = 7
I = 0.2
T = default_T
cc = ComponentContribution.init()

formula = 'C00149 <=> C00036'
#formula = 'C00033 + C00282 <=> C00084 + C00001'
#formula = 'C00067 + C00001 <=> C00058 + C00010'
#formula = 'C00003 + C00282 <=> C00004'
#############################################################################
reaction = KeggReaction.parse_formula(formula)
reaction_atom_bag = reaction._get_reaction_atom_bag()
n_e = 0
if 'e-' in reaction_atom_bag:
    n_e = reaction_atom_bag['e-']
    del reaction_atom_bag['e-']
if len(reaction_atom_bag) != 0:
    raise Exception('This is not a half-reaction (i.e. cannot be balanced by adding e-)')

print reaction.write_formula() + ' + (%d) e-' % (-n_e)

dG0_r, u_r = cc.get_dG0_r(reaction)

for pH in xrange(6, 9):
    ddG0_r = reaction.get_transform_ddG0(pH=pH, I=I, T=T)
    dG0_r_prime = dG0_r + ddG0_r