Beispiel #1
0
    def run(self,
            matrix,
            lb,
            ub,
            wtSolution,
            eqs=[],
            ineqs=[],
            numIter=1,
            weights=None):
        """ construct and solve the quadratic optimization problem

        Keyword arguments:

        matrix     -- the stoichiometric matrix of the metabolic network
        lb         -- list of lower bounds (indexed like matrix columns)
        ub         -- list of upper bounds (indexed like matrix columns)
        wtSolution -- FBA solution for the wildtype
                     (list indexed like matrix columns)
        eqs        -- list of (coefficient vector, right-hand side) pairs for
                      additional equality constraints
        ineqs      -- list of - '' - for additional inequality constraints
        numIter    -- number of iterations of NLP to perform
        weights    -- weight vector for weighted MOMA (None -> perform regular
                      MOMA, else: weight flux i with weights[i])

        Returns:
        distance, solution, status

        distance   -- minimum possible distance from wtSolution with the given
                      matrix & constraints
        solution   -- a flux vector with minimal distance to wtSolution
                     (list indexed like matrix columns)
        status     -- SolverStatus after optimization
        """
        wtVec = array(wtSolution)
        # Construct matrices of original equality and inequality constraints
        Aeq, beq, Aineq, bineq = FbAnalyzer.makeConstraintMatrices(
            matrix, eqs, ineqs)
        try:
            if MomaProblem.isCvxQpSolver(self.solver):
                ps = MomaProblem(Aeq, beq, Aineq, bineq, lb, ub, self.solver,
                                 wtVec, weights)
            else:
                ps = NonLinearProblem(Aeq, beq, Aineq, bineq, lb, ub,
                                      self.solver)
                if weights is None:
                    ps.setObjective(lambda x: dot(x - wtVec, x - wtVec))
                    ps.setObjGrad(lambda x: 2 * (x - wtVec))
                else:
                    if not isinstance(weights, ndarray):
                        weights = array(weights)
                    ps.setObjective(lambda x: dot(x - wtVec,
                                                  (x - wtVec) * weights))
                    ps.setObjGrad(lambda x: 2 * sqrt(weights) * (x - wtVec))
                spIter = StartPointIterator(len(lb), numIter)
                spIter.setRange(-1., 1.)
                ps.setStartPointIterator(spIter)

        except ValueError, strerror:
            print strerror
            exit()
Beispiel #2
0
    def run(self, objective, matrix, lb, ub, threshold=1.0, eqs=[], ineqs=[]):
        """ successively formulate and solve linear problems for each metabolite
            flux with inequality constraint 'objective value <= threshold'

        Arguments refer to split fluxes (i.e. non-negative flux variables).

        Keyword arguments:

        objective      -- coefficient vector for linear objective function
        matrix         -- stoichiometric matrix of the metabolic network
        lb             -- list of lower bounds (indexed like matrix columns)
        ub             -- list of upper bounds (indexed like matrix columns)
        threshold      -- objective function threshold
        eqs            -- list of (coefficient vector, right-hand side) pairs
                          for additional equality constraints
        ineqs          -- list of - '' - for additional inequality constraints

        Returns:  list of minimum values, indexed like metabolites
        """
        matrix = array(matrix)
        nMetabolites, nCols = matrix.shape
        if nMetabolites == 0:
            return []  # Nothing to do

        # Construct matrices of original equality and inequality constraints
        Aeq, beq, Aineq, bineq = FbAnalyzer.makeConstraintMatrices(
            matrix, eqs, ineqs)
        # Combine original inequality constraints and
        # 'objective function <= threshold'
        if Aineq is None:
            Aineq = [objective]
            bineq = [threshold]
        else:
            Aineq = vstack((Aineq, [objective]))
            bineq = append(bineq, threshold)

        result = []
        ubVec = []
        for i in range(nCols):
            # Impose artificial upper bounds so that all LPs are bounded
            if isinf(ub[i]):
                ubVec.append(LARGE_NUM)
            else:
                ubVec.append(ub[i])

        psSplit = LinearProblem(Aeq, beq, Aineq, bineq, array(lb),
                                array(ubVec), self.solver)

        # Compute coefficient vectors of producing metabolite fluxes
        # - pick only positive coefficients from stoichiometric matrix
        posMatrix = matrix.copy()
        for i in range(nMetabolites):
            for j in range(nCols):
                if posMatrix[i, j] < 0.:
                    posMatrix[i, j] = 0.

        # Successively minimize each flux
        for index in range(nMetabolites):
            try:
                psSplit.setObjective(map(float, posMatrix[index, :]))
            except Exception:
                # Skip all-zero rows
                result.append(0.)
                continue

            minval, s = psSplit.minimize()
            psSplit.resetObjective()

            if len(s) == 0:
                result.append(nan)
            else:
                result.append(minval)

        return result
Beispiel #3
0
    def run(self, objective, matrix, lb, ub, threshold=.95, eqs=[], ineqs=[]):
        """ successively formulate and solve linear problems for each flux with
            inequality constraint 'objective value <= threshold'

        Keyword arguments:

        objective -- coefficient vector for linear objective function
        matrix    -- stoichiometric matrix
        lb        -- list of lower bounds, indexed like reactions
        ub        -- list of upper bounds, indexed like reactions
        threshold -- objective function threshold
        eqs       -- list of (coefficient vector, right-hand side) pairs for
                     additional equality constraints
        ineqs     -- list of - '' - for additional inequality constraints

        Returns:  list of pairs (minimum, maximum), indexed like reactions
        """
        # Construct matrices of original equality and inequality constraints
        Aeq, beq, Aineq, bineq = FbAnalyzer.makeConstraintMatrices(
            matrix, eqs, ineqs)
        #lb = [-1000]*939
        #ub = [1000]*939
        # Combine original inequality constraints and
        # 'objective function <= threshold'
        if Aineq is None:
            Aineq = [objective]
            bineq = [threshold]
        else:
            Aineq = vstack((Aineq, [objective]))
            bineq = append(bineq, threshold)

        nReactions = len(lb)
        lbVec, ubVec = [], []
        for i in range(nReactions):
            if isinf(lb[i]):
                lbVec.append(-LARGE_NUM)
            else:
                lbVec.append(lb[i])
            if isinf(ub[i]):
                ubVec.append(LARGE_NUM)
            else:
                ubVec.append(ub[i])

        ps = LinearProblem(Aeq, beq, Aineq, bineq, lbVec, ubVec, self.solver)
        minmax = [None] * nReactions

        # First maximize each flux
        for index in range(nReactions):

            ps.setObjective([0.] * index + [-1.] + [0.] *
                            (nReactions - index - 1))
            s = ps.minimize()[1]
            ps.resetObjective()

            # catch random infeasible solution that resolves if the solver is recreated
            if len(s) == 0:
                ps = LinearProblem(Aeq, beq, Aineq, bineq, lbVec, ubVec,
                                   self.solver)
                ps.setObjective([0.] * index + [-1.] + [0.] *
                                (nReactions - index - 1))
                s = ps.minimize()[1]
                ps.resetObjective()

            if len(s) != 0:
                maxval = s[index]
            else:
                maxval = nan
                s = None
            minmax[index] = maxval

        # Now minimize each flux
        for index in range(nReactions):

            ps.setObjective([0.] * index + [1.] + [0.] *
                            (nReactions - index - 1))
            s = ps.minimize()[1]
            ps.resetObjective()

            # catch random infeasible solution that resolves if the solver is recreated
            if len(s) == 0:
                ps = LinearProblem(Aeq, beq, Aineq, bineq, lbVec, ubVec,
                                   self.solver)
                ps.setObjective([0.] * index + [-1.] + [0.] *
                                (nReactions - index - 1))
                s = ps.minimize()[1]
                ps.resetObjective()

            if len(s) != 0:
                minval = s[index]
            else:
                minval = nan
                s = None
            minmax[index] = minval, minmax[index]

        return minmax