cobra.flux_analysis.loopless
============================

.. py:module:: cobra.flux_analysis.loopless

.. autoapi-nested-parse::

   Provide functions to remove thermodynamically infeasible loops.



Functions
---------

.. autoapisummary::

   cobra.flux_analysis.loopless.add_loopless
   cobra.flux_analysis.loopless._add_cycle_free
   cobra.flux_analysis.loopless.loopless_solution
   cobra.flux_analysis.loopless.loopless_fva_iter


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

.. py:function:: add_loopless(model: cobra.Model, zero_cutoff: Optional[float] = None, method: str = 'fastSNP', reactions: Optional[List[str]] = None) -> None

   Modify a model so all feasible flux distributions are loopless.

   It adds variables and constraints to a model which will disallow flux
   distributions with loops. This function *will* modify your model.

   The used formulation is described in [1]_. If `method` is set to
   "fastSNP", it uses a faster implementation based on the Fast-SNP
   algorithm [2]_.

   In most cases you probably want to use the much faster
   `loopless_solution`. May be used in cases where you want to add complex
   constraints and objecives (for instance quadratic objectives) to the
   model afterwards or use an approximation of Gibbs free energy directions
   in your model.

   :param model: The model to which to add the constraints.
   :type model: cobra.Model
   :param zero_cutoff: Cutoff used for null space. Coefficients with an absolute value
                       smaller than `zero_cutoff` are considered to be zero. The default
                       uses the `model.tolerance` (default None).
   :type zero_cutoff: positive float, optional
   :param method: The method to use for finding the null space. The "original" method
                  uses the original method from [1]_, while "fastSNP" uses a faster
                  implementation based on the FastSNP algorithm. The "fastSNP" method
                  is much faster and should be used in most cases.
   :type method: str, "original" or "fastSNP", optional
   :param reactions: The list of reaction IDs to constrain. All cycles within these
                     reactions will be removed. If `None`, all reactions will be constrained.
   :type reactions: list of str, optional

   .. rubric:: References

   .. [1] Elimination of thermodynamically infeasible loops in steady-state
      metabolic models. Schellenberger J, Lewis NE, Palsson BO. Biophys J.
      2011 Feb 2;100(3):544-53. doi: 10.1016/j.bpj.2010.12.3707. Erratum
      in: Biophys J. 2011 Mar 2;100(5):1381.
   .. [2] Fast-SNP: a fast matrix pre-processing algorithm for efficient
      loopless flux optimization of metabolic models. Saa PA, Nielsen LK.
      Bioinformatics. 2016 Dec;32(24):3807–3814. doi: 10.1093/bioinformatics/btw555.


.. py:function:: _add_cycle_free(model: cobra.Model, fluxes: Dict[str, float]) -> None

   Add constraints for CycleFreeFlux.

   :param model: The model to operate on.
   :type model: cobra.Model
   :param fluxes: A dictionary having keys as reaction IDs and values as their flux
                  values.
   :type fluxes: dict of {str: float}


.. py:function:: loopless_solution(model: cobra.Model, fluxes: Optional[Dict[str, float]] = None) -> cobra.Solution

   Convert an existing solution to a loopless one.

   Removes as many loops as possible (see Notes).

   Uses the method from CycleFreeFlux [1]_ and is much faster than
   `add_loopless` and should therefore be the preferred option to get
   loopless flux distributions.

   :param model: The model to which to add the constraints.
   :type model: cobra.Model
   :param fluxes: A dictionary having keys as reaction IDs and values as their flux
                  values. If not None will use the provided flux values to obtain a
                  close loopless solution (default None).
   :type fluxes: dict of {str, float}, optional

   :returns: A solution object containing the fluxes with the least amount of
             loops possible or None if the optimization failed (usually happening
             if the flux distribution in `fluxes` is infeasible).
   :rtype: cobra.Solution

   .. rubric:: Notes

   The returned flux solution has the following properties:

   - It contains the minimal number of loops possible and no loops at all
     if all flux bounds include zero and the objective is not in a cycle.
   - It has the same objective value as the original flux solution and assumes
     that the objective does not participate in a cycle
     (which is usually true since it consumes metabolites).
   - It has the same exact exchange fluxes as the previous solution.
   - All fluxes have the same sign (flow in the same direction) as the
     previous solution.

   When providing fluxes to the method, please note that those have to come from the
   exact same model that you provided, meaning that no bounds or coefficients have
   been changed, and the optimum has remained the same.

   .. rubric:: References

   .. [1] CycleFreeFlux: efficient removal of thermodynamically infeasible
      loops from flux distributions. Desouki AA, Jarre F, Gelius-Dietrich
      G, Lercher MJ. Bioinformatics. 2015 Jul 1;31(13):2159-65. doi:
      10.1093/bioinformatics/btv096.


.. py:function:: loopless_fva_iter(model: cobra.Model, reaction: cobra.Reaction, solution: bool = False, zero_cutoff: Optional[float] = None) -> Union[float, Dict[str, float]]

   Plugin to get a loopless FVA solution from single FVA iteration.

   Assumes the following about `model` and `reaction`:
   1. The model objective is set to be `reaction`.
   2. The model has been optimized and contains the minimum/maximum flux
      for `reaction`.
   3. The model contains an auxiliary variable called "fva_old_objective"
      denoting the previous objective.

   :param model: The model to be used.
   :type model: cobra.Model
   :param reaction: The reaction currently minimized/maximized.
   :type reaction: cobra.Reaction
   :param solution: Whether to return the entire solution or only the minimum/maximum
                    for `reaction` (default False).
   :type solution: bool, optional
   :param zero_cutoff: Cutoff used for loop removal. Fluxes with an absolute value smaller
                       than `zero_cutoff` are considered to be zero. The default is to use
                       `model.tolerance` (default None).
   :type zero_cutoff: positive float, optional

   :returns: **single float or dict of {str** -- Returns the minimized/maximized flux through `reaction` if
             `solution` is False. Otherwise, returns a loopless flux
             solution object containing the minimum/maximum flux for `reaction`.
   :rtype: float}


