cobra.util.solver
=================

.. py:module:: cobra.util.solver

.. autoapi-nested-parse::

   Additional helper functions for the optlang solvers.

   All functions integrate well with the context manager, meaning that
   all operations defined here are automatically reverted when used in a
   `with model:` block.

   The functions defined here together with the existing model functions
   should allow you to implement custom flux analysis methods with ease.



Attributes
----------

.. autoapisummary::

   cobra.util.solver.CONS_VARS
   cobra.util.solver.logger
   cobra.util.solver.solvers
   cobra.util.solver.qp_solvers
   cobra.util.solver.has_primals


Classes
-------

.. autoapisummary::

   cobra.util.solver.Components


Functions
---------

.. autoapisummary::

   cobra.util.solver.linear_reaction_coefficients
   cobra.util.solver._valid_atoms
   cobra.util.solver.set_objective
   cobra.util.solver.interface_to_str
   cobra.util.solver.get_solver_name
   cobra.util.solver.choose_solver
   cobra.util.solver.check_solver
   cobra.util.solver.add_cons_vars_to_problem
   cobra.util.solver.remove_cons_vars_from_problem
   cobra.util.solver.add_absolute_expression
   cobra.util.solver.fix_objective_as_constraint
   cobra.util.solver.check_solver_status
   cobra.util.solver.assert_optimal
   cobra.util.solver.add_lp_feasibility
   cobra.util.solver.add_lexicographic_constraints


Module Contents
---------------

.. py:data:: CONS_VARS

.. py:data:: logger

.. py:data:: solvers

.. py:data:: qp_solvers
   :value: ['cplex', 'gurobi', 'hybrid']


.. py:data:: has_primals

.. py:class:: Components

   Bases: :py:obj:`NamedTuple`


   Define an object for adding absolute expressions.


   .. py:attribute:: variable
      :type:  optlang.interface.Variable


   .. py:attribute:: upper_constraint
      :type:  optlang.interface.Constraint


   .. py:attribute:: lower_constraint
      :type:  optlang.interface.Constraint


.. py:function:: linear_reaction_coefficients(model: cobra.Model, reactions: Optional[List[cobra.Reaction]] = None) -> Dict[cobra.Reaction, float]

   Retrieve coefficient for the reactions in a linear objective.

   :param model: The cobra model defining the linear objective.
   :type model: cobra.Model
   :param reactions: An optional list of the reactions to get the coefficients for.
                     By default, all reactions are considered (default None).
   :type reactions: list of cobra.Reaction, optional

   :returns: A dictionary where the keys are the reaction objects and the values
             are the corresponding coefficient. Empty dictionary if there are no
             linear terms in the objective.
   :rtype: dict


.. py:function:: _valid_atoms(model: cobra.Model, expression: optlang.symbolics.Basic) -> bool

   Check whether a sympy expression references the correct variables.

   :param model: The model in which to check for variables.
   :type model: cobra.Model
   :param expression: A sympy expression.
   :type expression: sympy.Basic

   :returns: True if all referenced variables are contained in model, False
             otherwise.
   :rtype: bool


.. py:function:: set_objective(model: cobra.Model, value: Union[optlang.interface.Objective, optlang.symbolics.Basic, Dict[cobra.Reaction, float]], additive: bool = False) -> None

   Set the model objective.

   :param model: The model to set the objective for.
   :type model: cobra.Model
   :param value: If the model objective is linear, then the value can be a new
                 optlang.interface.Objective or a dictionary with linear
                 coefficients where each key is a reaction and the corresponding
                 value is the new coefficient (float).
                 If the objective is non-linear and `additive` is True, then only
                 values of class optlang.interface.Objective, are accepted.
   :type value: optlang.interface.Objective, optlang.symbolics.Basic, dict
   :param additive: If True, add the terms to the current objective, otherwise start with
                    an empty objective.
   :type additive: bool

   :raises ValueError: If model objective is non-linear and the `value` is a dict.
   :raises TypeError: If the type of `value` is not one of the accepted ones.


.. py:function:: interface_to_str(interface: Union[str, types.ModuleType]) -> str

   Give a string representation for an optlang interface.

   :param interface: Full name of the interface in optlang or cobra representation.
                     For instance, 'optlang.glpk_interface' or 'optlang-glpk'.
   :type interface: str, ModuleType

   :returns: The name of the interface as a string.
   :rtype: str


.. py:function:: get_solver_name(mip: bool = False, qp: bool = False) -> str

   Select a solver for a given optimization problem.

   :param mip: True if the solver requires mixed integer linear programming capabilities.
   :type mip: bool
   :param qp: True if the solver requires quadratic programming capabilities.
   :type qp: bool

   :returns: The name of the feasible solver.
   :rtype: str

   :raises SolverNotFound: If no suitable solver could be found.


.. py:function:: choose_solver(model: cobra.Model, solver: Optional[str] = None, qp: bool = False) -> types.ModuleType

   Choose a solver given a solver name and model.

   This will choose a solver compatible with the model and required
   capabilities. Also respects model.solver where it can.

   :param model: The model for which to choose the solver.
   :type model: cobra.Model
   :param solver: The name of the solver to be used (default None).
   :type solver: str, optional
   :param qp: True if the solver needs quadratic programming capabilities
              (default False).
   :type qp: boolean, optional

   :returns: Valid solver for the problem.
   :rtype: optlang.interface

   :raises SolverNotFound: If no suitable solver could be found.


.. py:function:: check_solver(obj)

   Check whether the chosen solver is valid.

   Check whether chosen solver is valid and also warn when using
   a specialized solver. Will return the optlang interface for the
   requested solver.

   :param obj: The chosen solver.
   :type obj: str or optlang.interface or optlang.interface.Model

   :raises SolverNotFound: If the solver is not valid.


.. py:function:: add_cons_vars_to_problem(model: cobra.Model, what: Union[List[CONS_VARS], Tuple[CONS_VARS], Components], **kwargs) -> None

   Add variables and constraints to a model's solver object.

   Useful for variables and constraints that can not be expressed with
   reactions and lower/upper bounds. It will integrate with the model's
   context manager in order to revert changes upon leaving the context.

   :param model: The model to which to add the variables and constraints.
   :type model: cobra.Model
   :param what:     optlang.interface.Constraint
                The variables and constraints to add to the model.
   :type what: list or tuple of optlang.interface.Variable or
   :param \*\*kwargs: Keyword arguments passed to solver's add() method.
   :type \*\*kwargs: keyword arguments


.. py:function:: remove_cons_vars_from_problem(model: cobra.Model, what: Union[List[CONS_VARS], Tuple[CONS_VARS], Components]) -> None

   Remove variables and constraints from a model's solver object.

   Useful to temporarily remove variables and constraints from a model's
   solver object.

   :param model: The model from which to remove the variables and constraints.
   :type model: cobra.Model
   :param what:     optlang.interface.Constraint
                The variables and constraints to remove from the model.
   :type what: list or tuple of optlang.interface.Variable or


.. py:function:: add_absolute_expression(model: cobra.Model, expression: str, name: str = 'abs_var', ub: Optional[float] = None, difference: float = 0.0, add: bool = True) -> Components

   Add the absolute value of an expression to the model.

   Also defines a variable for the absolute value that can be used in
   other objectives or constraints.

   :param model: The model to which to add the absolute expression.
   :type model: cobra.Model
   :param expression: Must be a valid symbolic expression within the model's solver object.
                      The absolute value is applied automatically on the expression.
   :type expression: str
   :param name: The name of the newly created variable (default "abs_var").
   :type name: str, optional
   :param ub: The upper bound for the variable (default None).
   :type ub: positive float, optional
   :param difference: The difference between the expression and the variable
                      (default 0.0).
   :type difference: positive float, optional
   :param add: Whether to add the variable to the model at once (default True).
   :type add: bool, optional

   :returns: A named tuple with variable and two constraints (upper_constraint,
             lower_constraint) describing the new variable and the constraints
             that assign the absolute value of the expression to it.
   :rtype: Components


.. py:function:: fix_objective_as_constraint(model: cobra.Model, fraction: float = 1.0, bound: Optional[float] = None, name: str = 'fixed_objective_{}') -> float

   Fix current objective as an additional constraint.

   When adding constraints to a model, such as done in pFBA which
   minimizes total flux, these constraints can become too powerful,
   resulting in solutions that satisfy optimality but sacrifices too
   much for the original objective function. To avoid that, we can fix
   the current objective value as a constraint to ignore solutions that
   give a lower (or higher depending on the optimization direction)
   objective value than the original model.

   When done with the model as a context, the modification to the
   objective will be reverted when exiting that context.

   :param model: The model to operate on.
   :type model: cobra.Model
   :param fraction: The fraction of the optimum the objective is allowed to reach
                    (default 1.0).
   :type fraction: float, optional
   :param bound: The bound to use instead of fraction of maximum optimal value.
                 If not None, `fraction` is ignored (default None).
   :type bound: float, optional
   :param name: Name of the objective. May contain one "{}" placeholder which is
                filled with the name of the old objective
                (default "fixed_objective_{}").
   :type name: str, optional

   :returns: The value of the optimized objective * fraction
   :rtype: float


.. py:function:: check_solver_status(status: str = None, raise_error: bool = False) -> None

   Perform standard checks on a solver's status.

   :param status: The status string obtained from the solver (default None).
   :type status: str, optional
   :param raise_error: If True, raise error or display warning if False (default False).
   :type raise_error: bool, optional

   :rtype: None

   :Warns: **UserWarning** -- If `status` is not optimal and `raise_error` is set to True.

   :raises OptimizationError: If `status` is None or is not optimal and `raise_error` is set to
       True.


.. py:function:: assert_optimal(model: cobra.Model, message: str = 'Optimization failed') -> None

   Assert model solver status is optimal.

   Do nothing if model solver status is optimal, otherwise throw
   appropriate exception depending on the status.

   :param model: The model to check the solver status for.
   :type model: cobra.Model
   :param message: Message for the exception if solver status is not optimal
                   (default "Optimization failed").
   :type message: str, optional

   :rtype: None

   :raises OptimizationError: If solver status is not optimal.


.. py:function:: add_lp_feasibility(model: cobra.Model) -> None

   Add a new objective and variables to ensure a feasible solution.

   The optimized objective will be zero for a feasible solution and
   otherwise represent the distance from feasibility (please see [1]_
   for more information).

   :param model: The model whose feasibility is to be tested.
   :type model: cobra.Model

   :rtype: None

   .. rubric:: References

   .. [1] Gomez, Jose A., Kai Höffner, and Paul I. Barton.
   “DFBAlab: A Fast and Reliable MATLAB Code for Dynamic Flux Balance
   Analysis.” BMC Bioinformatics 15, no. 1 (December 18, 2014): 409.
   https://doi.org/10.1186/s12859-014-0409-8.


.. py:function:: add_lexicographic_constraints(model: cobra.Model, objectives: List[cobra.Reaction], objective_direction: Union[str, List[str]] = 'max') -> pandas.Series

   Successively optimize separate targets in a specific order.

   For each objective, optimize the model and set the optimal value as a
   constraint. Proceed in the order of the objectives given. Due to the
   specific order this is called lexicographic FBA [1]_. This procedure
   is useful for returning unique solutions for a set of important
   fluxes. Typically this is applied to exchange fluxes.

   :param model: The model to be optimized.
   :type model: cobra.Model
   :param objectives: A list of reactions (or objectives) in the model for which unique
                      fluxes are to be determined.
   :type objectives: list of cobra.Reaction
   :param objective_direction: The desired objective direction for each reaction (if a list) or
                               the objective direction to use for all reactions (default "max").
   :type objective_direction: str or list of str, optional

   :returns: A pandas Series containing the optimized fluxes for each of the
             given reactions in `objectives`.
   :rtype: pandas.Series

   .. rubric:: References

   .. [1] Gomez, Jose A., Kai Höffner, and Paul I. Barton.
   “DFBAlab: A Fast and Reliable MATLAB Code for Dynamic Flux Balance
   Analysis.” BMC Bioinformatics 15, no. 1 (December 18, 2014): 409.
   https://doi.org/10.1186/s12859-014-0409-8.


