Exemplo n.º 1
0
def _pfba_optlang(model,
                  objective=None,
                  reactions=None,
                  fraction_of_optimum=1.0):
    """Helper function to perform pFBA with the optlang interface

    Not meant to be used directly.

    Parameters
    ----------
    model : a cobra model
        The model to perform pFBA on
    objective :
        An objective to use in addition to the pFBA constraints.
    reactions : iterable
        List of reactions or reaction identifiers.

    Returns
    -------
    cobra.Solution
        The solution to the pFBA optimization.

    Updates everything in-place, returns model to original state at end.
    """
    reactions = model.reactions if reactions is None \
        else model.reactions.get_by_any(reactions)
    with model as m:
        add_pfba(m,
                 objective=objective,
                 fraction_of_optimum=fraction_of_optimum)
        m.slim_optimize(error_value=None)
        solution = get_solution(m, reactions=reactions)
    return solution
Exemplo n.º 2
0
    def optimize(self, objective_sense=None, raise_error=False):
        """
        Optimize the model using flux balance analysis.

        Parameters
        ----------
        objective_sense : {None, 'maximize' 'minimize'}, optional
            Whether fluxes should be maximized or minimized. In case of None,
            the previous direction is used.
        raise_error : bool
            If true, raise an OptimizationError if solver status is not
             optimal.

        Notes
        -----
        Only the most commonly used parameters are presented here.  Additional
        parameters for cobra.solvers may be available and specified with the
        appropriate keyword argument.

        """
        original_direction = self.objective.direction
        self.objective.direction = {
            "maximize": "max",
            "minimize": "min"
        }.get(objective_sense, original_direction)
        self.slim_optimize()
        solution = get_solution(self, raise_error=raise_error)
        self.objective.direction = original_direction
        return solution
Exemplo n.º 3
0
    def optimize(self, objective_sense=None, raise_error=False):
        """
        Optimize the model using flux balance analysis.

        Parameters
        ----------
        objective_sense : {None, 'maximize' 'minimize'}, optional
            Whether fluxes should be maximized or minimized. In case of None,
            the previous direction is used.
        raise_error : bool
            If true, raise an OptimizationError if solver status is not
             optimal.

        Notes
        -----
        Only the most commonly used parameters are presented here.  Additional
        parameters for cobra.solvers may be available and specified with the
        appropriate keyword argument.

        """
        original_direction = self.objective.direction
        self.objective.direction = \
            {"maximize": "max", "minimize": "min"}.get(
                objective_sense, original_direction)
        self.slim_optimize()
        solution = get_solution(self, raise_error=raise_error)
        self.objective.direction = original_direction
        return solution
Exemplo n.º 4
0
    def optimize(self, objective_sense=None, raise_error=False, **kwargs):
        """
        Optimize the model using flux balance analysis.

        Parameters
        ----------
        objective_sense : {None, 'maximize' 'minimize'}, optional
            Whether fluxes should be maximized or minimized. In case of None,
            the previous direction is used.
        raise_error : bool
            If true, raise an OptimizationError if solver status is not
             optimal.
        solver : {None, 'glpk', 'cglpk', 'gurobi', 'cplex'}, optional
            If unspecified will use the currently defined `self.solver`
            otherwise it will use the given solver and update the attribute.
        quadratic_component : {None, scipy.sparse.dok_matrix}, optional
            The dimensions should be (n, n) where n is the number of
            reactions. This sets the quadratic component (Q) of the
            objective coefficient, adding :math:`\\frac{1}{2} v^T \cdot Q
            \cdot v` to the objective. Ignored for optlang based solvers.
        tolerance_feasibility : float
            Solver tolerance for feasibility. Ignored for optlang based
            solvers
        tolerance_markowitz : float
            Solver threshold during pivot. Ignored for optlang based solvers
        time_limit : float
            Maximum solver time (in seconds). Ignored for optlang based solvers

        Notes
        -----
        Only the most commonly used parameters are presented here.  Additional
        parameters for cobra.solvers may be available and specified with the
        appropriate keyword argument.

        """
        legacy, solver = choose_solver(self, solver=kwargs.get("solver"))
        original_direction = self.objective.direction

        if legacy:
            if objective_sense is None:
                objective_sense = {
                    "max": "maximize",
                    "min": "minimize"
                }[original_direction]
            solution = optimize(self,
                                objective_sense=objective_sense,
                                **kwargs)
            check_solver_status(solution.status, raise_error=raise_error)
            return solution

        self.solver = solver
        self.objective.direction = \
            {"maximize": "max", "minimize": "min"}.get(
                objective_sense, original_direction)
        self.slim_optimize()
        solution = get_solution(self, raise_error=raise_error)
        self.objective.direction = original_direction
        return solution
Exemplo n.º 5
0
def pfba(model, fraction_of_optimum=1.0, objective=None, reactions=None):
    reactions = model.reactions if reactions is None \
        else model.reactions.get_by_any(reactions)
    with model as m:
        add_pfba(m,
                 objective=objective,
                 fraction_of_optinum=fraction_of_optinum)
        m.slim_optimize(error_value=None)
        solution = get_solution(m, reactions=reactions)
        return solutution
Exemplo n.º 6
0
    def optimize(self, medium, timestep, save_uptake, save_crossfeed=False):
        """does FBA for the bacterial species. sets bounds of exchange reactions based on medium"""
        with self.model as model:
            if medium != None:
                for reaction in self.model.exchanges:
                    if reaction.id in medium:

                        tmp = round(
                            -40000 * medium.get_component(reaction.id) /
                            self.get_biomass(), 8)
                        reaction.lower_bound = max(tmp, -1000)
                    else:
                        reaction.lower_bound = 0.0

            try:
                if self.data_watcher.get_pfba():
                    cobra.flux_analysis.parsimonious.add_pfba(model)

                objective_value = self.model.slim_optimize()
                solution = get_solution(self.model,
                                        reactions=self.model.exchanges)
            except OptimizationError:
                print(self.name + " Model infeasible")
                return

            self.set_biomass(
                (self.get_biomass() * round(solution.objective_value, 8) *
                 timestep + self.get_biomass()) *
                (1 -
                 self.data_watcher.get_death_rate()))  # updates the biomass

            for i in range(len(solution.fluxes.index)):
                name = solution.fluxes.index[i]
                if name[:3] == "EX_":
                    flux = solution.fluxes.iloc[i]
                    solution.fluxes.iloc[i] = round(
                        flux * self.get_biomass() * timestep, 8)

                    if save_crossfeed:
                        if flux < 0:
                            self.data_watcher.add_crossfeed_interaction(
                                self.name, name, True)
                        if flux > 0:
                            self.data_watcher.add_crossfeed_interaction(
                                self.name, name, False)

                    if save_uptake:
                        if flux < 0:
                            self.data_watcher.add_uptake(
                                self.name, name, solution.fluxes.iloc[i])

                    if flux < 0:
                        self.data_watcher.add_essential_nutrient(name)

            return solution
Exemplo n.º 7
0
def pfba(model, fraction_of_optimum=1.0, objective=None, reactions=None):
    """Perform basic pFBA (parsimonious Enzyme Usage Flux Balance Analysis)
    to minimize total flux.

    pFBA [1] adds the minimization of all fluxes the the objective of the
    model. This approach is motivated by the idea that high fluxes have a
    higher enzyme turn-over and that since producing enzymes is costly,
    the cell will try to minimize overall flux while still maximizing the
    original objective function, e.g. the growth rate.

    Parameters
    ----------
    model : cobra.Model
        The model
    fraction_of_optimum : float, optional
        Fraction of optimum which must be maintained. The original objective
        reaction is constrained to be greater than maximal_value *
        fraction_of_optimum.
    objective : dict or model.problem.Objective
        A desired objective to use during optimization in addition to the
        pFBA objective. Dictionaries (reaction as key, coefficient as value)
        can be used for linear objectives.
    reactions : iterable
        List of reactions or reaction identifiers. Implies `return_frame` to
        be true. Only return fluxes for the given reactions. Faster than
        fetching all fluxes if only a few are needed.

    Returns
    -------
    cobra.Solution
        The solution object to the optimized model with pFBA constraints added.

    References
    ----------
    .. [1] Lewis, N. E., Hixson, K. K., Conrad, T. M., Lerman, J. A.,
       Charusanti, P., Polpitiya, A. D., Palsson, B. O. (2010). Omic data
       from evolved E. coli are consistent with computed optimal growth from
       genome-scale models. Molecular Systems Biology, 6,
       390. doi:10.1038/msb.2010.47

    """
    reactions = (model.reactions if reactions is None else
                 model.reactions.get_by_any(reactions))
    with model as m:
        add_pfba(m,
                 objective=objective,
                 fraction_of_optimum=fraction_of_optimum)
        m.slim_optimize(error_value=None)
        solution = get_solution(m, reactions=reactions)
    return solution
Exemplo n.º 8
0
def pfba(model, fraction_of_optimum=1.0, objective=None, reactions=None):
    """Perform basic pFBA (parsimonious Enzyme Usage Flux Balance Analysis)
    to minimize total flux.

    pFBA [1] adds the minimization of all fluxes the the objective of the
    model. This approach is motivated by the idea that high fluxes have a
    higher enzyme turn-over and that since producing enzymes is costly,
    the cell will try to minimize overall flux while still maximizing the
    original objective function, e.g. the growth rate.

    Parameters
    ----------
    model : cobra.Model
        The model
    fraction_of_optimum : float, optional
        Fraction of optimum which must be maintained. The original objective
        reaction is constrained to be greater than maximal_value *
        fraction_of_optimum.
    objective : dict or model.problem.Objective
        A desired objective to use during optimization in addition to the
        pFBA objective. Dictionaries (reaction as key, coefficient as value)
        can be used for linear objectives.
    reactions : iterable
        List of reactions or reaction identifiers. Implies `return_frame` to
        be true. Only return fluxes for the given reactions. Faster than
        fetching all fluxes if only a few are needed.

    Returns
    -------
    cobra.Solution
        The solution object to the optimized model with pFBA constraints added.

    References
    ----------
    .. [1] Lewis, N. E., Hixson, K. K., Conrad, T. M., Lerman, J. A.,
       Charusanti, P., Polpitiya, A. D., Palsson, B. O. (2010). Omic data
       from evolved E. coli are consistent with computed optimal growth from
       genome-scale models. Molecular Systems Biology, 6,
       390. doi:10.1038/msb.2010.47

    """
    reactions = model.reactions if reactions is None \
        else model.reactions.get_by_any(reactions)
    with model as m:
        add_pfba(m, objective=objective,
                 fraction_of_optimum=fraction_of_optimum)
        m.slim_optimize(error_value=None)
        solution = get_solution(m, reactions=reactions)
    return solution