예제 #1
0
    def run(self, reactions, matrix, lb, ub, fbaParams):
        """ analyze objective function, and solve the LP/QP/NLP

        Keyword arguments:

        reactions  -- dictionary of all reactions { name : matrix column } or
                      { name : tuple of indices } for split fluxes
        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)
        fbaParams -- optimization parameters (incl. objective function)

        Returns:
        obj_value, solution

        obj_value -- optimal value of objective function
        solution  -- a solution where the objective function assumes obj_value
        """
        maxmin, objStr, numIter = (fbaParams.maxmin, fbaParams.objStr,
                                   fbaParams.numIter)
        # Multiply by -1 for maximization
        maxmin_factor = -1. if maxmin == True else 1.
        nCols = len(lb)

        threshold = 0.95
        nReactions = len(lb)

        # Get additional linear equality and inequality constraints
        try:
            Aeq, beq, Aineq, bineq = self.makeConstraintMatrices(
                matrix,
                *ParamParser.linConstraintsToVectors(fbaParams.linConstraints,
                                                     reactions, nCols))
        except ValueError:
            # If any linear constraint is contradictory, return empty solution
            return 0., []

        try:
            objective = ParamParser.convertObjFuncToLinVec(
                objStr, reactions, nCols, maxmin)
        except Exception, strerror:
            print(
                "Error while trying to build coefficient vector of "
                "linear objective function:")
            print strerror
            exit()
예제 #2
0
def checkLinConstraints(solution, model, linConstraints):
    """ check if flux distribution 'solution' violates any of the given linear
        equality and inequality constraints

    Keyword arguments:

    solution       -- flux distribution (MetabolicFlux object)
    model          -- MetabolicModel (for generating flux and coefficient
                                      vectors)
    linConstraints -- list of LinearConstraint objects

    Returns:
    eqErr, ineqErr

    eqErr   -- dict {index in linConstraints : deviation} for each equality
               constraint
    ineqErr -- dict {index in linConstraints : deviation} for each inequality
               constraint - if deviation is negative, constraint is satisfied
    """
    solutionVec = solution.getVecOrderedByModel(model)
    eqs, ineqs = ParamParser.linConstraintsToVectors(linConstraints,
                                                     model.reactionDict)
    eqIndex, ineqIndex = [], []
    for i in range(len(linConstraints)):
        if linConstraints[i].isEq:
            eqIndex.append(i)
        else:
            ineqIndex.append(i)

    eqErr, ineqErr = {}, {}
    if eqs:
        A = [row[0] for row in eqs]
        b = [row[1] for row in eqs]
        dotVec = dot(A, solutionVec)
        for i in range(len(b)):
            eqErr[eqIndex[i]] = abs(dotVec[i] - b[i])

    if ineqs:
        A = [row[0] for row in ineqs]
        b = [row[1] for row in ineqs]
        dotVec = dot(A, solutionVec)
        for i in range(len(b)):
            ineqErr[ineqIndex[i]] = dotVec[i] - b[i]

    return eqErr, ineqErr
예제 #3
0
        conforms = not negViolations and not posViolations

        for rea in negViolations:
            print("Irreversible reaction %s has negative flux (%g)." %
                  (rea, negViolations[rea]))
        for rea in posViolations:
            print("Flipped irreversible reaction %s has positive flux (%g)." %
                  (rea, posViolations[rea]))
    else:
        conforms = True
        model = None

    # 4. Parse scenario file (if given) and check LB/UB constraints

    if options.paramFile:
        pparser = ParamParser()
        try:
            lb, ub = pparser.parse(options.paramFile)[4:]
        except IOError, strerror:
            print("An error occurred while trying to read file %s:" %
                  os.path.basename(options.paramFile))
            print strerror
            exit()
        except SyntaxError, strerror:
            print("Error in scenario file %s:" %
                  os.path.basename(options.paramFile))
            print strerror
            exit()
        except ValueError, strerror:
            print strerror
            exit()
예제 #4
0
        model.addReactionsFromFile(options.reactionFile, rparser)
    except IOError, strerror:
        print("An error occurred while trying to read file %s:" %
              os.path.basename(options.reactionFile))
        print strerror
        exit()
    except SyntaxError, strerror:
        print("Error in reaction file %s:" %
              os.path.basename(options.reactionFile))
        print strerror
        exit()

    # 3. Parse scenario file

    model_messages = []
    pparser = ParamParser()
    try:
        # Parse file, get maxmin, name of objective function, and solver name
        maxmin, objStr, solver, numIter, lb , ub = \
            pparser.parse(options.paramFile)
        if solver == "":
            solver = "default"
        fbaParams = FbaParam(solver, maxmin, objStr, numIter)
        # Set flux bounds in model
        model.setFiniteBounds(lb, ub, True, model_messages)
    except IOError, strerror:
        print("An error occurred while trying to read file %s:" %
              os.path.basename(options.paramFile))
        print strerror
        exit()
    except SyntaxError, strerror:
예제 #5
0
import os
import sys
sys.path.insert(0, os.path.abspath('..'))

from paramparser import ParamParser

parser = ParamParser('test.txt')

a = parser.read('a', 'str')
print a
b = parser.read('b', 'int')
print b
c = parser.read('c', 'float')
print c
d = parser.read('d', 'bool')
print d
e = parser.read('e', 'bool')
print e
f = parser.read('f', 'int')
print f
g = parser.read('g', 'bool')
print g
h = parser.read('h')
print h
i = parser.read('i')
print i
j = parser.read('j')
print j

parser.change_value('j', 'fxxk', 'test2.txt')
예제 #6
0
        print("An error occurred while trying to read file %s:" %
              os.path.basename(options.reactionFile))
        print strerror
        exit()
    except SyntaxError, strerror:
        print("Error in reaction file %s:" %
              os.path.basename(options.reactionFile))
        print strerror
        exit()
    reactions = model.reactionDict
    matrix = array(model.getStoichiometricMatrix())

    # 3. Parse scenario file

    model_messages = []
    pparser = ParamParser()
    try:
        # Parse file, get maxmin, name of objective function, and solver name
        maxmin, obj_name, solver, numIter, lb, ub = pparser.parse(
            options.paramFile)
        fbaParams = FbaParam(solver, maxmin, obj_name, numIter)
        # Set flux bounds in model
        model.setFiniteBounds(lb, ub, True, model_messages)
    except IOError, strerror:
        print("An error occurred while trying to read file %s:" %
              os.path.basename(options.paramFile))
        print strerror
        exit()
    except SyntaxError, strerror:
        print("Error in scenario file %s:" %
              os.path.basename(options.paramFile))
예제 #7
0
    def runOnModel(self,
                   model,
                   fbaParams,
                   threshp=.95,
                   objFuncVal=None,
                   rmDeadEnds=True):
        """ perform metabolite flux minimization on the given model with
            objective value <= threshp * maximum

        Keyword arguments:

        model        -- the MetabolicModel
        fbaParams    -- FBA parameters
        threshp      -- threshold percentage (objective_value <= thresp*maximum)
        objFuncVal   -- FBA optimum for objective function (optional)
        rmDeadEnds   -- if True, remove all reactions with dead ends before
                        analysis (faster and gives an optimal solution, as well)

        Returns:
        minVec, dimReduced

        minVec       -- list of minimum values, indexed like metabolites
        dimReduced   -- pair (nRows, nColumns) with dimensions of reduced matrix
        """
        if rmDeadEnds:
            deadReactions = model.findDeadEnds(True)[1]
            modelRed = model.getSubModelByExcludeList(deadReactions)
            cbz = model.canBeZero(deadReactions)
            nonZeroDeadEnds = [
                deadReactions[i] for i in range(len(deadReactions))
                if not cbz[i]
            ]
            if nonZeroDeadEnds:
                print(
                    "The following blocked reactions are constrained to a "
                    "non-zero flux:\n  " + "\n  ".join(nonZeroDeadEnds) +
                    "\nThe problem is infeasible.")
                return [], array(modelRed.getStoichiometricMatrix()).shape
        else:
            modelRed = model

        matrix = array(modelRed.getStoichiometricMatrix())
        dimReduced = matrix.shape
        lb, ub = modelRed.getBounds()

        # Split fluxes into non-negative components
        matrixSplit, reactionsSplit, lbSplit, ubSplit = \
            FbAnalyzer.splitFluxes(matrix, modelRed.getReactionNames(), lb, ub)

        # Build (negative) objective function vector for split fluxes
        objective = ParamParser.convertObjFuncToLinVec(fbaParams.objStr,
                                                       reactionsSplit,
                                                       len(lbSplit),
                                                       fbaParams.maxmin)
        maxmin_factor = -1. if fbaParams.maxmin else 1.

        # If the optimum of the objective function is not given, perform FBA
        if objFuncVal is None:
            fba = FbAnalyzer(self.solver)

            objFuncVal, sFlux = fba.run(reactionsSplit, matrixSplit, lbSplit,
                                        ubSplit, fbaParams)

            if len(sFlux) == 0:
                return [], dimReduced

        # Use negative threshold (objective value >= threshold is equivalent to
        # -objective value <= -threshold, and objective already has coefficient
        # -1 due to maximization)
        threshold = maxmin_factor * objFuncVal * threshp
        print "obj func. opt:", objFuncVal
        print "Threshold: ", threshold
        try:
            eqs, ineqs = ParamParser.linConstraintsToVectors(
                fbaParams.linConstraints, modelRed.reactionDict, len(lbSplit))
        except ValueError, e:
            # If any linear constraint is contradictory, report error
            print "Optimization not possible due to contradictory constraints:"
            print "  " + e
            exit()
예제 #8
0
    def runOnModel(self,
                   model,
                   wtSolution,
                   linConstraints=[],
                   numIter=1,
                   weights=None,
                   blockedReactions=[]):
        """ construct and solve the quadratic optimization problem
          - this function runs directly on a MetabolicModel and a MetabolicFlux

        Keyword arguments:

        model          -- the MetabolicModel
        wtSolution     -- FBA solution for the wildtype (given as MetabolicFlux)
        linConstraints -- list of LinearConstraint objects
        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])
        blockedReactions
                       -- remove the given blocked reactions before analysis
                          (faster and gives an optimal solution, as well)

        Returns:
        distance, solution, status, dimReduced

        distance   -- minimum possible distance from wtSolution with the given
                      matrix & constraints
        solution   -- a solution with minimal distance to wtSolution
                     (as MetabolicFlux)
        status     -- SolverStatus after optimization
        dimReduced -- pair (nRows, nColumns) with dimensions of reduced matrix
        """
        if blockedReactions:
            modelRed = model.getSubModelByExcludeList(blockedReactions)
            cbz = model.canBeZero(blockedReactions)
            nonZeroDeadEnds = [
                blockedReactions[i] for i in range(len(blockedReactions))
                if not cbz[i]
            ]
            if nonZeroDeadEnds:
                print(
                    "The following blocked reactions are constrained to a "
                    "non-zero flux:\n  " + "\n  ".join(nonZeroDeadEnds) +
                    "\nThe problem is infeasible.")
                return (nan, MetabolicFlux(), SolverStatus.PRIM_INFEAS,
                        array(modelRed.getStoichiometricMatrix()).shape)

            reactionsRed = set(modelRed.getReactionNames())
            if weights is None:
                weightsRed = None
            else:
                weightsRed = [0.] * len(modelRed)
                for rea in wtSolution:
                    if rea in reactionsRed:
                        weightsRed[modelRed.reactionDict[rea]] = \
                            weights[model.reactionDict[rea]]
                    weightsRed = array(weightsRed)
        else:
            modelRed = model
            weightsRed = weights

        matrix = array(modelRed.getStoichiometricMatrix())
        dimReduced = matrix.shape
        lb, ub = map(array, modelRed.getBounds())
        try:
            eqs, ineqs = ParamParser.linConstraintsToVectors(
                linConstraints, modelRed.reactionDict)
        except ValueError:
            # If any linear constraint is contradictory, return empty solution
            return (nan, MetabolicFlux(), SolverStatus.PRIM_INFEAS,
                    array(modelRed.getStoichiometricMatrix()).shape)

        distance, solution, status = self.run(
            matrix, lb, ub, wtSolution.getVecOrderedByModel(modelRed), eqs,
            ineqs, numIter, weightsRed)
        flux = MetabolicFlux(modelRed, solution)

        # Add removed reactions with flux 0. and original bounds to solution
        if len(modelRed) != len(model) and solution != []:
            reactionsRed = set(modelRed.getReactionNames())
            for rea in model:
                if rea.name not in reactionsRed:
                    flux.fluxDict[rea.name] = 0.
                    flux.boundsDict[rea.name] = (rea.lb, rea.ub)

        return distance, flux, status, dimReduced
예제 #9
0
    def run(self, reactions, matrix, lb, ub, fbaParams):
        """ analyze objective function, and solve the LP/QP/NLP

        Keyword arguments:

        reactions  -- dictionary of all reactions { name : matrix column } or
                      { name : tuple of indices } for split fluxes
        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)
        fbaParams -- optimization parameters (incl. objective function)

        Returns:
        obj_value, solution

        obj_value -- optimal value of objective function
        solution  -- a solution where the objective function assumes obj_value
        """
        maxmin, objStr, numIter = (fbaParams.maxmin, fbaParams.objStr,
                                   fbaParams.numIter)
        # Multiply by -1 for maximization
        maxmin_factor = -1. if maxmin == True else 1.
        nCols = len(lb)

        # Get additional linear equality and inequality constraints
        try:
            Aeq, beq, Aineq, bineq = self.makeConstraintMatrices(matrix,
                *ParamParser.linConstraintsToVectors(fbaParams.linConstraints,
                                                     reactions, nCols))
        except ValueError:
            # If any linear constraint is contradictory, return empty solution
            return 0., []

        if objStr.lower().startswith("per_flux"):
            # special nonlinear objective function: single flux per flux unit
            # e.g. "per_flux(Biomass)"
            try:
                str_perflux, str_flux = objStr.split('(', 2)
            except ValueError:
                print ("Error in scenario file in objective function "
                       "definition.\nPER_FLUX syntax: "
                       "PER_FLUX(<reaction_name>)")
                exit()

            if str_perflux.strip().lower() != "per_flux":
                print ("Error: Objective function '%s' is not a reaction name "
                       "or a PER_FLUX definition.\n"
                       "Currently, objective function must be a linear "
                       "combination of reaction fluxes or PER_FLUX(<reaction>)."
                       % objStr)
                exit()

            rea_name = str_flux.split(')', 1)[0].strip()
            try:
                biomass_index = reactions[rea_name][0]
            except TypeError:
                biomass_index = reactions[rea_name]
            obj_func = lambda x : -biomass_per_flux(x, biomass_index)

            if numIter < 0:
                numIter = FbaParam.DEFAULT_NUMITER

            try:
                ps = NonLinearProblem(Aeq, beq, Aineq, bineq, lb, ub,
                                      self.solver)
                ps.setObjective(obj_func)
                ps.setObjGrad(lambda x :
                              neg_grad_biomass_per_flux(x, biomass_index))
                if fbaParams.nlc != []:
                    ps.setNonlinearConstraints(fbaParams.nlc)
                    ps.setNlcGrad(fbaParams.nlc_grad)
                spIter = StartPointIterator(nCols, numIter)
                spIter.setRange(-1., 1.)
                ps.setStartPointIterator(spIter)

            except ValueError, strerror:
                print strerror
                exit()
예제 #10
0
    def runOnModel(self,
                   model,
                   fbaParams,
                   threshp=.95,
                   solution=None,
                   splitFluxes=True,
                   rmDeadEnds=True):
        """ perform flux variability analysis on the given model with
            objective value <= threshp * maximum

        Keyword arguments:

        model        -- the MetabolicModel
        fbaParams    -- FBA parameters
        threshp      -- threshold percentage (objective_value <= thresp*maximum)
        solution     -- optional: either FBA solution file or solution as
                        MetabolicFlux object
        splitFluxes  -- if True, run split fluxes (resulting in non-negative
                        flux variables) before FBA
        rmDeadEnds   -- if True, remove all reactions with dead ends before
                        analysis (faster and gives an optimal solution, as well)

        Returns:
        minmax, sFlux, lb, ub, dimReduced

        minmax       -- list of pairs (minimum, maximum), indexed like reactions
        sFlux        -- FBA solution (vector indexed like reactions)
        lb, ub       -- lower/upper bounds vectors, indexed like reactions
        dimReduced   -- pair (nRows, nColumns) with dimensions of reduced matrix
        """
        if rmDeadEnds:
            deadReactions = model.findDeadEnds(True)[1]
            modelRed = model.getSubModelByExcludeList(deadReactions)
            cbz = model.canBeZero(deadReactions)
            nonZeroDeadEnds = [
                deadReactions[i] for i in range(len(deadReactions))
                if not cbz[i]
            ]
            if nonZeroDeadEnds:
                print(
                    "The following blocked reactions are constrained to a "
                    "non-zero flux:\n  " + "\n  ".join(nonZeroDeadEnds) +
                    "\nThe problem is infeasible.")
                lbFull, ubFull = map(array, model.getBounds())
                return ([], MetabolicFlux(), lbFull, ubFull,
                        array(modelRed.getStoichiometricMatrix()).shape)
        else:
            modelRed = model

        matrix = array(modelRed.getStoichiometricMatrix())
        dimReduced = matrix.shape
        lb, ub = map(array, modelRed.getBounds())

        # Build (negative) objective function vector
        objective = ParamParser.convertObjFuncToLinVec(fbaParams.objStr,
                                                       modelRed.reactionDict,
                                                       -1, fbaParams.maxmin)
        maxmin_factor = -1. if fbaParams.maxmin else 1.

        # Case 1. If an FBA solution is given, use that

        if isinstance(solution, MetabolicFlux):
            # Sort solution like matrix columns
            sFlux = solution.getVecOrderedByModel(modelRed)
            # Evaluate the objective function at solution
            obj_value = maxmin_factor * dot(objective, sFlux)

        # Case 2. If solution file is given, read solution

        elif solution is not None:
            sFlux = MetabolicFlux()
            try:
                sFlux.readFromFile(solution)
            except IOError, strerror:
                print("An error occurred while trying to read file %s:" %
                      os.path.basename(solution))
                print strerror
                exit()
            except SyntaxError, strerror:
                print("An error occurred parsing file %s:" %
                      os.path.basename(solution))
                print strerror
                exit()
예제 #11
0
            else:
                obj_value, sFlux = fba.run(modelRed.getReactionNames(), matrix,
                                           lb, ub, fbaParams)

            if len(sFlux) == 0:
                lbFull, ubFull = map(array, model.getBounds())
                return [], sFlux, lbFull, ubFull, dimReduced

        # Use negative threshold (objective value >= threshold is equivalent to
        # -objective value <= -threshold, and objective already has coefficient
        # -1 due to maximization)
        threshold = maxmin_factor * obj_value * threshp
        print "obj func. opt:", obj_value
        print "Threshold: ", threshold
        try:
            eqs, ineqs = ParamParser.linConstraintsToVectors(
                fbaParams.linConstraints, modelRed.reactionDict)
        except ValueError, e:
            # If any linear constraint is contradictory, report error
            print "Optimization not possible due to contradictory constraints:"
            print "  " + e
            exit()

        minmax = self.run(objective, matrix, lb, ub, threshold, eqs, ineqs)

        # Add removed reactions with min = max = 0. to minmax and solution
        if len(modelRed) != len(model):
            minmaxFull, sFluxFull = [], []
            reactionsRed = set(modelRed.getReactionNames())

            for rea in model:
                if rea.name in reactionsRed:
예제 #12
0
    def runOnModel(self,
                   model,
                   fbaParams,
                   splitFluxes=True,
                   minimizeTotalFlux=False,
                   rmDeadEnds=True):
        """ run flux-balance analysis on the given MetabolicModel

        Keyword arguments:

        model       -- the MetabolicModel with reactions and bounds
        fbaParams   -- optimization parameters (incl. objective function)
        splitFluxes -- if True, run split fluxes (resulting in non-negative flux
                       variables) before analysis
        minimizeTotalFlux
                    -- if True, find a solution with minimal total absolute flux
        rmDeadEnds   -- if True, remove all reactions with dead ends before
                        analysis (faster and gives an optimal solution, as well)

        Returns:
        obj_value, solution, dimReduced

        obj_value   -- optimal value of objective function
        solution    -- a solution where the objective function assumes obj_value
        dimReduced  -- pair (nRows, nColumns) with dimensions of reduced matrix
        """
        maxmin = fbaParams.maxmin
        objStr = fbaParams.objStr

        if isinstance(maxmin, str):
            maxmin = {"max": True, "min": False}[maxmin.lower()]

        if rmDeadEnds:
            deadReactions = model.findDeadEnds(True)[1]
            modelRed = model.getSubModelByExcludeList(deadReactions)
            cbz = model.canBeZero(deadReactions)
            nonZeroDeadEnds = [
                deadReactions[i] for i in range(len(deadReactions))
                if not cbz[i]
            ]
            if nonZeroDeadEnds:
                print(
                    "The following blocked reactions are constrained to a "
                    "non-zero flux:\n  " + "\n  ".join(nonZeroDeadEnds) +
                    "\nThe problem is infeasible.")
                return (0., MetabolicFlux(),
                        array(modelRed.getStoichiometricMatrix()).shape)
        else:
            modelRed = model

        matrix = array(modelRed.getStoichiometricMatrix())
        dimReduced = matrix.shape
        reaction_names = modelRed.getReactionNames()
        reactions = modelRed.reactionDict
        lb, ub = modelRed.getBounds()

        if splitFluxes:
            matrixSplit, reactionsSplit, lbSplit, ubSplit = \
                self.splitFluxes(matrix, reaction_names, lb, ub)
            try:
                ParamParser.convertObjFuncToLinVec(objStr, reactionsSplit,
                                                   len(lbSplit), maxmin)
            except KeyError, s:
                if deadReactions:
                    print(
                        "Error while trying to construct the objective "
                        "function vector:\n%s\nThis may be due to removal "
                        "of nonfunctional reactions." % s)
                    return 0., MetabolicFlux(), dimReduced

            obj_value, solutionSplit = self.run(reactionsSplit, matrixSplit,
                                                lbSplit, ubSplit, fbaParams)
            solution = []
            try:
                if solutionSplit == []:
                    solution = []
                else:
                    if minimizeTotalFlux:
                        # Optimize solution by limiting total flux
                        limit, obj_value, solutionSplit, nSteps = \
                            self.runWithTotalFluxMinimization(reactionsSplit,
                                matrixSplit, lbSplit, ubSplit, fbaParams,
                                sum(solutionSplit), 1e-8, 1e-12)
                        print(
                            "Found optimal flux limit of %.10g in %u steps." %
                            (limit, nSteps))

                    solution = array(
                        FbAnalyzer.rejoinFluxes(solutionSplit, reactionsSplit,
                                                reactions))
            except NameError, strerror:
                print strerror
예제 #13
0
    def runWithTotalFluxMinimization(self,
                                     reactions,
                                     matrix,
                                     lb,
                                     ub,
                                     fbaParams,
                                     start_value_total=None,
                                     tolerance_total=EPSILON,
                                     tolerance_obj=EPSILON):
        """ perform FBA with LP iteratively to find a flux distribution with
            optimal value of objective function value and minimum total flux

        This only works with non-negative flux variables, i.e. split fluxes!
        The objective function and all constraints must be linear.

        This function limits the total flux to a parameter and then iteratively
        fits this parameter until either the change in total flux is less than
        tolerance_total or the change in the objective function is less than
        tolerance_obj

        Keyword arguments:

        reactions  -- dictionary { name : tuple of indices } for split fluxes
        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)
        fbaParams -- optimization parameters (incl. objective function)
        start_value_total -- initial flux limit (if None: set to len(reactions))
        tolerance_total   -- stopping criterion: change in flux limit below this
        tolerance_obj     -- stopping criterion: change in objective function
                                                 value below this

        Returns:
        limit, obj_value, solution, nSteps

        limit     -- limit of total flux
        obj_value -- optimal value of objective function
        solution  -- a solution where the objective function assumes obj_value,
                     obtained with limited total flux
        nSteps    -- number of steps until optimal limit was found
        """
        maxmin, objStr = (fbaParams.maxmin, fbaParams.objStr)
        # Multiply by -1 for maximization
        maxmin_factor = -1. if maxmin == True else 1.
        nCols = len(lb)

        for value in lb:
            if value < 0.:
                print(
                    "Error: Minimization of total flux requires non-negative"
                    " flux variables.")
                exit()

        # Get additional linear equality and inequality constraints
        try:
            Aeq, beq, Aineq, bineq = self.makeConstraintMatrices(
                matrix,
                *ParamParser.linConstraintsToVectors(fbaParams.linConstraints,
                                                     reactions, nCols))
        except ValueError:
            # If any linear constraint is contradictory, return empty solution
            return 0., []

        # Prepare additional constraint: total flux < threshold
        if Aineq is None:
            Aineq, bineq = array([[1.] * nCols]), []
        else:
            # Aineq has extra line for total flux
            Aineq = vstack((Aineq, [[1.] * nCols]))
            # Convert bineq to list (allows easy addition of extra value)
            bineq = list(bineq)

        # Build linear objective function (given as coefficient vector)
        try:
            objective = ParamParser.convertObjFuncToLinVec(
                objStr, reactions, nCols, maxmin)
        except Exception, strerror:
            print(
                "Error while trying to build coefficient vector of "
                "linear objective function:")
            print strerror
            exit()
예제 #14
0
        model.addReactionsFromFile(options.reactionFile, rparser)
    except IOError, strerror:
        print ("An error occurred while trying to read file %s:" %
               os.path.basename(options.reactionFile))
        print strerror
        exit()
    except SyntaxError, strerror:
        print ("Error in reaction file %s:" %
               os.path.basename(options.reactionFile))
        print strerror
        exit()

    # 3. Parse scenario file

    model_messages = []
    pparser = ParamParser()
    try:
        # Parse file, get maxmin, name of objective function, and solver name
        maxmin, obj_name, solver, numIter, lb , ub = \
            pparser.parse(options.inputFile)
        # Set flux bounds in model (for parameter check)
        model.setFiniteBounds(lb, ub, True, model_messages)
    except IOError, strerror:
        print ("An error occurred while trying to read file %s:" %
               os.path.basename(options.inputFile))
        print strerror
        exit()
    except SyntaxError, strerror:
        print ("Error in scenario file %s:" %
               os.path.basename(options.inputFile))
        print strerror
예제 #15
0
def main():

    # Parse command line
    usage = ("Usage: %prog <delta-G-file> [options]\n\n"
             "Gibbs free energy changes are taken from the <delta-g-file> if "
             "given.\nElse, they are computed from the data specified via the "
             "-r/-c/-d/-s/-t set of\noptions.")
    version = "%prog\n" + COPYRIGHT_VERSION_STRING
    parser = OptionParser(usage=usage, version=version)
    standardOptGroup = OptionGroup(parser, "Standard parameters")
    standardOptGroup.add_option("-p",
                                "--parameters",
                                dest="paramFile",
                                help="check the given scenario FILE",
                                metavar="FILE")
    standardOptGroup.add_option("-o",
                                "--output",
                                dest="outputFile",
                                help="write modified scenario file to FILE "
                                "(must not be the same as scenario file)",
                                metavar="FILE")
    standardOptGroup.add_option(
        "-e",
        "--epsilon",
        dest="epsilon",
        help="set THRESHOLD for recognizing Gibbs free "
        "energy change as clearly positive (default 0)",
        metavar="THRESHOLD")
    parser.add_option_group(standardOptGroup)

    computeOptGroup = OptionGroup(
        parser, "Parameters for computation of "
        "Gibbs free energies", "These are only needed"
        " if delta-G values are not read from file.")
    computeOptGroup.add_option("-r",
                               "--reactions",
                               dest="reactionFile",
                               help="use reactions from reaction FILE",
                               metavar="FILE")
    computeOptGroup.add_option("-c",
                               "--concentrations",
                               dest="concentrationFile",
                               help="use "
                               "concentrations from FILE",
                               metavar="FILE")
    computeOptGroup.add_option("-d",
                               "--thermodynamics",
                               dest="thermodynFile",
                               help="use thermodynamic data from FILE",
                               metavar="FILE")
    computeOptGroup.add_option("-s",
                               "--synonyms",
                               dest="synonymFile",
                               help="use metabolite synonyms from FILE",
                               metavar="FILE")
    computeOptGroup.add_option("-t",
                               "--temperature",
                               dest="temperature",
                               help="set temperature to VALUE (in degrees "
                               "Celsius)",
                               metavar="VALUE")
    parser.add_option_group(computeOptGroup)
    parser.set_defaults(epsilon='0.')

    options, args = parser.parse_args()
    parser.check_required("-p")
    parser.check_required("-o")
    parser.check_required("-t")

    try:
        epsilon = float(options.epsilon)
    except ValueError:
        print("Error: Invalid floating point value for epsilon (%s)" %
              options.epsilon)
        exit()
    if (os.path.exists(options.outputFile)
            and os.path.samefile(options.outputFile, options.paramFile)):
        print("Error: Input and output scenario files are the same (%s)" %
              options.paramFile)
        exit()

    if epsilon < 0.:
        print "Warning: epsilon < 0. Using default value of 0 instead."
        epsilon = 0.

    if len(args) > 0:
        gibbsR = readReaEnthalpiesFromFile(args[0])
    else:
        print(
            "\nInfo: No file with Gibbs free energies given. Launching "
            "computation of values.\n")
        parser.check_required("-r")
        parser.check_required("-c")
        parser.check_required("-d")
        parser.check_required("-s")
        parser.check_required("-t")
        try:
            temperature = float(options.temperature)
        except ValueError:
            print("Error: Invalid floating point value for temperature (%s)" %
                  options.temperature)
            exit()

        # Compute Gibbs free energies from the given data
        gibbsR = getReaEnthalpies(options.concentrationFile,
                                  options.thermodynFile, options.synonymFile,
                                  options.reactionFile, temperature)

    # Parse scenario file
    pparser = ParamParser()
    try:
        # Parse file
        maxmin, obj_name, solver, numiter, lb, ub =\
            pparser.parse(options.paramFile)
    except IOError, strerror:
        print("An error occurred while trying to read file %s:" %
              os.path.basename(options.paramFile))
        print strerror
        exit()
예제 #16
0
        model.addReactionsFromFile(options.reactionFile, rparser)
    except IOError, strerror:
        print("An error occurred while trying to read file %s:" %
              os.path.basename(options.reactionFile))
        print strerror
        exit()
    except SyntaxError, strerror:
        print("Error in reaction file %s:" %
              os.path.basename(options.reactionFile))
        print strerror
        exit()

    # 3. Parse scenario file

    model_messages = []
    pparser = ParamParser()
    try:
        lb, ub = pparser.parse(options.paramFile)[-2:]
        linConstraints = pparser.lin_constraints
        # Set flux bounds in model
        model.setFiniteBounds(lb, ub, True, model_messages)
    except IOError, strerror:
        print("An error occurred while trying to read file %s:" %
              os.path.basename(options.paramFile))
        print strerror
        exit()
    except SyntaxError, strerror:
        print("Error in scenario file %s:" %
              os.path.basename(options.paramFile))
        print strerror
        exit()
예제 #17
0
            model.addReactionsFromFile(options.reactionFile, rparser)
        except IOError, strerror:
            print("An error occurred while trying to read file %s:" %
                  os.path.basename(options.reactionFile))
            print strerror
            _mpi_exit(comm, 1)
        except SyntaxError, strerror:
            print("Error in reaction file %s:" %
                  os.path.basename(options.reactionFile))
            print strerror
            _mpi_exit(comm, 1)

        # 3. Parse scenario file

        model_messages = []
        pparser = ParamParser()
        try:
            maxmin, objStr, fbaSolver, numIter, lb , ub = \
                pparser.parse(options.paramFile)
            # Set flux bounds in model
            model.setFiniteBounds(lb, ub, True, model_messages)
            if fbaSolver == "":
                fbaSolver = "default"
            fbaParams = FbaParam(fbaSolver, maxmin, objStr, numIter)
            lin_constraints = pparser.lin_constraints
            fbaParams.setLinConstraints(lin_constraints)
        except IOError, strerror:
            print("An error occurred while trying to read file %s:" %
                  os.path.basename(options.paramFile))
            print strerror
            _mpi_exit(comm, 1)
예제 #18
0
                              neg_grad_biomass_per_flux(x, biomass_index))
                if fbaParams.nlc != []:
                    ps.setNonlinearConstraints(fbaParams.nlc)
                    ps.setNlcGrad(fbaParams.nlc_grad)
                spIter = StartPointIterator(nCols, numIter)
                spIter.setRange(-1., 1.)
                ps.setStartPointIterator(spIter)

            except ValueError, strerror:
                print strerror
                exit()

        else:
            # Build linear objective function (given as coefficient vector)
            try:
                objective = ParamParser.convertObjFuncToLinVec(objStr,
                                                    reactions, nCols, maxmin)
            except Exception, strerror:
                print ("Error while trying to build coefficient vector of "
                       "linear objective function:")
                print strerror
                exit()

            if fbaParams.nlc == []:
                # Linear function and linear constraints only

                # If flux variables are not split, use GLPK through OpenOpt
                solver = (_OOGLPK if self.solver.lower() == _GLPK and
                          nCols == len(reactions) else self.solver)
                try:
                    ps = LinearProblem(Aeq, beq, Aineq, bineq, lb, ub, solver)
                except ValueError, strerror: