Example #1
0
    def to_array_based_model(self, deepcopy_model=False, **kwargs):
        """Makes a `cobra.core.ArrayBasedModel` from a cobra.Model
        which may be used to perform linear algebra operations with the
        stoichiometric matrix.

        Deprecated (0.6). Use `cobra.util.array.create_stoichiometric_matrix`
        instead.

        Parameters
        ----------
        deepcopy_model : bool
            If False then the ArrayBasedModel points to the Model

        """
        warn("to_array_based_model is deprecated. "
             "use cobra.util.array.create_stoichiometric_matrix instead",
             DeprecationWarning)
        from cobra.core.arraybasedmodel import ArrayBasedModel
        return ArrayBasedModel(self, deepcopy_model=deepcopy_model, **kwargs)
Example #2
0
def compute_nullmatrix(label_model,
                       remove_inactive_reactions=True,
                       fname=None):
    reaction_n_dict = {}
    n_reaction_dict = {}
    reaction0_list = []
    model = copy.deepcopy(label_model.constrained_model)
    if remove_inactive_reactions:

        fva = flux_variability_analysis(model, fraction_of_optimum=0)
        reactions_to_remove = []
        print "The following reactions will be omitted as they cannot carry flux:"
        for x in fva:  #Remove inactive reactions
            if fva[x]["minimum"] == 0.0 and 0.0 == fva[x]["maximum"]:
                print x, fva[x]
                reactions_to_remove.append(model.reactions.get_by_id(x))
                reaction0_list.append(x)
        for r in reactions_to_remove:
            r.remove_from_model()
        metabolites2remove = []
        for x in model.metabolites:
            if len(x.reactions) == 0:
                metabolites2remove.append(x)
        for x in metabolites2remove:
            print x.id + " removed"
            x.remove_from_model()
    rgroup_dict = {}
    list_group_metabolites = []
    n = 0
    for reaction in model.reactions:  # sorted([x.id for x in model.reactions ]):
        #print str(n)+reaction_id
        #reaction=model.reactions.get_by_id(reaction_id)
        if "LABEL_RGROUP_" in reaction.id:
            group_dict = {}
            group_metabolite = reaction.metabolites.keys()[
                0]  #Group reactions only have one metabolite
            #Get the coefficients of reactions in the list
            for group_member_reaction in group_metabolite.reactions:
                if reaction != group_member_reaction:
                    group_dict[group_member_reaction.
                               id] = group_member_reaction.metabolites[
                                   group_metabolite]
            list_group_metabolites.append(group_metabolite)
            rgroup_dict[reaction.id] = group_dict
        else:
            reaction_n_dict[reaction.id] = n
            n_reaction_dict[n] = reaction.id
            n += 1
    print rgroup_dict
    if fname == None:
        array_based = ArrayBasedModel(model)
        sm = array_based.S.toarray()
        print len(sm), len(sm[0])
        sm_list = []
        for n_row, metabolite in enumerate(
                model.metabolites
        ):  #in enumerate(sorted([x.id for x in model.metabolites])):
            #metabolite=model.metabolites.get_by_id(metabolite_id)
            if metabolite in list_group_metabolites:
                continue
            for n_col, reaction in enumerate(model.reactions):
                if n_col not in n_reaction_dict:
                    continue
                coef = sm[n_row, n_col]
                if coef.is_integer():
                    sm_list.append(int(coef))
                else:
                    sm_list.append(sympy.Rational(coef))

        smm = sympy.Matrix(
            len(model.metabolites) - len(list_group_metabolites),
            len(reaction_n_dict), sm_list)
        #print len(smm),len(smm[0])
        print "computing null matrix..."
        nullms = nullspace(smm, True)
        #nullms=smm.nullspace(True) #Simplify True
        nullmnp = np.transpose(np.array(nullms, dtype=np.float64))
        #original_nullmnp=copy.deepcopy(nullmnp)
        #Adding group reactions:
    else:
        nullmnp = np.loadtxt(fname)
    for group_reaction in sorted(rgroup_dict.keys()):
        n = len(n_reaction_dict)
        reaction_n_dict[group_reaction] = n
        n_reaction_dict[n] = group_reaction
        if fname != None:  #If it has been loaded this is not necessary
            continue
        new_row = np.zeros((1, len(nullmnp[0])))
        for grouped_reaction in rgroup_dict[group_reaction]:
            print rgroup_dict[group_reaction]
            grouped_reaction_n = reaction_n_dict[grouped_reaction]
            grouped_reaction_coef = rgroup_dict[group_reaction][
                grouped_reaction]
            for n_coef, null_coef in enumerate(nullmnp[grouped_reaction_n]):
                new_row[0][n_coef] += null_coef * grouped_reaction_coef
        nullmnp = np.append(nullmnp, new_row, axis=0)
    for reaction0 in reaction0_list:
        n = len(n_reaction_dict)
        reaction_n_dict[reaction0] = n
        n_reaction_dict[n] = reaction0
        if fname != None:  #If it has been loaded this is not necessary
            continue
        new_row = np.zeros((1, len(nullmnp[0])))
        nullmnp = np.append(nullmnp, new_row, axis=0)
        #Create empty numpy array to append
    label_model.flux_solver_nullmnp = nullmnp
    label_model.flux_solver_n_reaction_dict = n_reaction_dict
    label_model.flux_solver_reaction_n_dict = reaction_n_dict
    label_model.compute_fluxes = get_compute_fluxes_function(label_model)
    return label_model.flux_solver_nullmnp