cobra.flux_analysis.gapfilling
==============================

.. py:module:: cobra.flux_analysis.gapfilling

.. autoapi-nested-parse::

   Provide the base class and utility function for gap filling.



Attributes
----------

.. autoapisummary::

   cobra.flux_analysis.gapfilling.logger


Classes
-------

.. autoapisummary::

   cobra.flux_analysis.gapfilling.GapFiller


Functions
---------

.. autoapisummary::

   cobra.flux_analysis.gapfilling.gapfill


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

.. py:data:: logger

.. py:class:: GapFiller(model: cobra.core.Model, universal: Optional[cobra.core.Model] = None, lower_bound: float = 0.05, penalties: Optional[Union[Dict[str, int], Dict[cobra.Reaction, int]]] = None, exchange_reactions: bool = False, demand_reactions: bool = True, integer_threshold: float = 1e-06, **kwargs)

   The base class for performing gap filling.

   This class implements gap filling based on a mixed-integer approach,
   very similar to that described in [1]_ and the 'no-growth but growth'
   part of [2]_ but with minor adjustments. In short, we add indicator
   variables for using the reactions in the universal model, z_i and then
   solve problem

   minimize: \sum_i c_i * z_i
   s.t.    : Sv = 0
             v_o \ge t
             lb_i \le v_i \le ub_i
             v_i = 0 if z_i = 0

   where lb, ub are respectively the upper, lower flux bounds for reaction i,
   c_i is a cost parameter and the objective v_o is greater than the lower
   bound t. The default costs are 1 for reactions from the universal model,
   100 for exchange (uptake) reactions added and 1 for added demand reactions.

   Note that this is a mixed-integer linear program and as such will be
   expensive to solve for large models. Consider using alternatives [3]_
   such as CORDA instead [4,5]_ .

   :param model: The model to perform gap filling on.
   :type model: cobra.Model
   :param universal: A universal model with reactions that can be used to complete the
                     `model` (default None).
   :type universal: cobra.Model, optional
   :param lower_bound: The minimally accepted flux for the objective in the filled model
                       (default 0.05).
   :type lower_bound: float, optional
   :param penalties: A dictionary with keys being 'universal' (all reactions included in
                     the universal model), 'exchange' and 'demand' (all additionally
                     added exchange and demand reactions) for the three reaction types.
                     Can also have reaction identifiers for reaction specific costs.
                     Defaults are 1, 100 and 1 respectively (default None).
   :type penalties: dict, optional
   :param exchange_reactions: Consider adding exchange (uptake) reactions for all metabolites
                              in the model (default False).
   :type exchange_reactions: bool, optional
   :param demand_reactions: Consider adding demand reactions for all metabolites (default True).
   :type demand_reactions: bool, optional
   :param integer_threshold: The threshold at which a value is considered non-zero (aka
                             integrality threshold). If gapfilled models fail to validate,
                             you may want to lower this value (default 1E-6).
   :type integer_threshold: float, optional

   .. attribute:: indicators

      The list of symbolic indicator variables.

      :type: list of optlang.interface.Variable

   .. attribute:: costs

      The dictionary with symbolic variables as keys and their cost as
      values.

      :type: dict of {optlang.interface.Variable: float}

   .. rubric:: References

   .. [1] Reed, Jennifer L., Trina R. Patel, Keri H. Chen, Andrew R. Joyce,
      Margaret K. Applebee, Christopher D. Herring, Olivia T. Bui, Eric M.
      Knight, Stephen S. Fong, and Bernhard O. Palsson. “Systems Approach
      to Refining Genome Annotation.” Proceedings of the National Academy
      of Sciences 103, no. 46 (2006): 17480–17484.

   .. [2] Kumar, Vinay Satish, and Costas D. Maranas. “GrowMatch: An
      Automated Method for Reconciling In Silico/In Vivo Growth
      Predictions.” Edited by Christos A. Ouzounis. PLoS Computational
      Biology 5, no. 3 (March 13, 2009): e1000308.
      doi:10.1371/journal.pcbi.1000308.

   .. [3] http://opencobra.github.io/cobrapy/tags/gapfilling/

   .. [4] Schultz, André, and Amina A. Qutub. “Reconstruction of
      Tissue-Specific Metabolic Networks Using CORDA.” Edited by Costas D.
      Maranas. PLOS Computational Biology 12, no. 3 (March 4, 2016):
      e1004808. doi:10.1371/journal.pcbi.1004808.

   .. [5] Diener, Christian https://github.com/cdiener/corda


   .. py:attribute:: original_model


   .. py:attribute:: lower_bound
      :value: 0.05



   .. py:attribute:: model


   .. py:attribute:: integer_threshold
      :value: 1e-06



   .. py:attribute:: universal


   .. py:attribute:: penalties


   .. py:attribute:: indicators
      :value: []



   .. py:attribute:: costs


   .. py:method:: extend_model(exchange_reactions: bool = False, demand_reactions: bool = True) -> None

      Extend gap filling model.

      Add reactions from universal model and optionally exchange and
      demand reactions for all metabolites in the model to perform
      gap filling on.

      :param exchange_reactions: Consider adding exchange (uptake) reactions for all metabolites
                                 in the model (default False).
      :type exchange_reactions: bool, optional
      :param demand_reactions: Consider adding demand reactions for all metabolites
                               (default True).
      :type demand_reactions: bool, optional



   .. py:method:: update_costs() -> None

      Update coefficients for the indicator variables in the objective.

      Done incrementally so that second time the function is called,
      active indicators in the current solutions gets higher cost than the
      unused indicators.




   .. py:method:: add_switches_and_objective() -> None

      Update gap filling model with switches and indicator objective.



   .. py:method:: fill(iterations: int = 1) -> List[List[cobra.Reaction]]

      Perform the gap filling.

      With every iteration, it solves the model, updates the costs and
      records the used reactions.

      :param iterations: The number of rounds of gap filling to perform. For every
                         iteration, the penalty for every used reaction increases
                         linearly. This way, the algorithm is encouraged to search for
                         alternative solutions which may include previously used
                         reactions i.e., with enough iterations pathways including 10
                         steps will eventually be reported even if the shortest pathway
                         is a single reaction (default 1).
      :type iterations: int, optional

      :returns: A list of lists where each element is a list of reactions that
                were used to gap fill the model.
      :rtype: list of list of cobra.Reaction

      :raises RuntimeError: If the model fails to be validated (i.e. the original model with
          the proposed reactions added, still cannot get the required flux
          through the objective).



   .. py:method:: validate(reactions: List[cobra.Reaction]) -> bool

      Validate the model.

      :param reactions: The reactions to add to the model for validation.
      :type reactions: list of cobra.Reaction

      :returns: Whether the model is valid or not.
      :rtype: bool



.. py:function:: gapfill(model: cobra.core.Model, universal: Optional[cobra.core.Model] = None, lower_bound: float = 0.05, penalties: Optional[Dict[str, cobra.Reaction]] = None, demand_reactions: bool = True, exchange_reactions: bool = False, iterations: int = 1)

   Perform gap filling on a model.

   :param model: The model to perform gap filling on.
   :type model: cobra.Model
   :param universal: A universal model with reactions that can be used to complete the
                     model. Only gapfill considering demand and exchange reactions if
                     left missing (default None).
   :type universal: cobra.Model, optional
   :param lower_bound: The minimally accepted flux for the objective in the filled model.
                       (default 0.05).
   :type lower_bound: float, optional
   :param penalties: A dictionary with keys being 'universal' (all reactions included in
                     the universal model), 'exchange' and 'demand' (all additionally
                     added exchange and demand reactions) for the three reaction types.
                     Can also have reaction identifiers for reaction specific costs.
                     Defaults are 1, 100 and 1 respectively (default None).
   :type penalties: dict, optional
   :param exchange_reactions: Consider adding exchange (uptake) reactions for all metabolites
                              in the model (default False).
   :type exchange_reactions: bool, optional
   :param demand_reactions: Consider adding demand reactions for all metabolites (default True).
   :type demand_reactions: bool, optional
   :param iterations: The number of rounds of gap filling to perform. For every iteration,
                      the penalty for every used reaction increases linearly. This way,
                      the algorithm is encouraged to search for alternative solutions
                      which may include previously used reactions i.e., with enough
                      iterations pathways including 10 steps will eventually be reported
                      even if the shortest pathway is a single reaction (default 1).
   :type iterations: int, optional

   :returns: A list of lists with on set of reactions that completes the model per
             requested iteration.
   :rtype: list of list of cobra.Reaction

   .. rubric:: Examples

   >>> from cobra.io import load_model
   >>> from cobra import Model
   >>> from cobra.flux_analysis import gapfill
   >>> model = load_model("iYS1720")
   >>> universal = Model("universal")
   >>> universal.add_reactions([model.reactions.GF6PTA.copy()])
   >>> model.remove_reactions([model.reactions.GF6PTA])
   >>> gapfill(model, universal)
   [[<Reaction GF6PTA at 0x12206a280>]]


