コード例 #1
0
 def test_reaction_inversion_target(self, model):
     inversion_target = ReactionInversionTarget("GND", value=-10, reference_value=10)
     assert inversion_target.fold_change == 0
     lower_bound = model.reactions.GND.lower_bound
     with model:
         inversion_target.apply(model)
         assert model.reactions.GND.lower_bound == -10
     assert model.reactions.GND.lower_bound == lower_bound
コード例 #2
0
    def _generate_designs(cls, solutions, reference_fva, reference_fluxes):
        """
        Generates strain designs for Differential FVA.

        The conversion method has three scenarios:
        #### 1. Knockout

            Creates a ReactionKnockoutTarget.

        #### 2. Flux reversal

            If the new flux is negative then it should be at least the upper bound of the interval.
            Otherwise it should be at least the lower bound of the interval.

        #### 3. The flux increases or decreases

            This table illustrates the possible combinations.
                * Gap is the sign of the normalized gap between the intervals.
                * Ref is the sign of the closest bound (see _closest_bound).
                * Bound is the value to use

            +-------------------+
            | Gap | Ref | Bound |
            +-----+-----+-------+
            |  -  |  -  |   LB  |
            |  -  |  +  |   UB  |
            |  +  |  -  |   UB  |
            |  +  |  +  |   LB  |
            +-----+-----+-------+


        Parameters
        ----------
        solutions: pandas.Panel
            The DifferentialFVA panel with all the solutions. Each DataFrame is a design.
        reference_fva: pandas.DataFrame
            The FVA limits for the reference strain.
        reference_fluxes:
            The optimal flux distribution for the reference strain.

        Returns
        -------
        list
            A list of cameo.core.strain_design.StrainDesign for each DataFrame in solutions.
        """
        designs = []
        for _, solution in solutions.groupby(('biomass', 'production')):
            targets = []
            relevant_targets = solution.loc[
                (numpy.abs(solution['normalized_gaps']) >
                 non_zero_flux_threshold)
                & (numpy.logical_not(solution['excluded'])) &
                (numpy.logical_not(solution['free_flux']))]
            for rid, relevant_row in relevant_targets.iterrows():
                if relevant_row.KO:
                    targets.append(ReactionKnockoutTarget(rid))
                elif relevant_row.flux_reversal:
                    if reference_fva['upper_bound'][rid] > 0:
                        targets.append(
                            ReactionInversionTarget(
                                rid,
                                value=float_ceil(relevant_row.upper_bound,
                                                 ndecimals),
                                reference_value=reference_fluxes[rid]))
                    else:
                        targets.append(
                            ReactionInversionTarget(
                                rid,
                                value=float_floor(relevant_row.lower_bound,
                                                  ndecimals),
                                reference_value=reference_fluxes[rid]))
                else:
                    gap_sign = relevant_row.normalized_gaps > 0

                    ref_interval = reference_fva[[
                        'lower_bound', 'upper_bound'
                    ]].loc[rid].values
                    row_interval = (relevant_row.lower_bound,
                                    relevant_row.upper_bound)

                    closest_bound, ref_sign = cls._closest_bound(
                        ref_interval, row_interval)

                    if gap_sign ^ ref_sign:
                        value = float_ceil(relevant_row.upper_bound, ndecimals)
                    else:
                        value = float_floor(relevant_row.lower_bound,
                                            ndecimals)

                    targets.append(
                        ReactionModulationTarget(
                            rid,
                            value=value,
                            reference_value=reference_fva[closest_bound][rid]))

            designs.append(StrainDesign(targets))
        return designs
コード例 #3
0
    def _generate_designs(cls, solutions, reference_fva):
        """
        Generates strain designs for Differential FVA.

        The conversion method has three scenarios:
        #### 1. Knockout

            Creates a ReactionKnockoutTarget.

        #### 2. Flux reversal

            If the new flux is negative then it should be at least the upper
            bound of the interval. Otherwise it should be at least the lower
            bound of the interval.

        #### 3. The flux increases or decreases

            This table illustrates the possible combinations.
                * Gap is the sign of the normalized gap between the intervals.
                * Ref is the sign of the closest bound (see _closest_bound).
                * Bound is the value to use

            +-------------------+
            | Gap | Ref | Bound |
            +-----+-----+-------+
            |  -  |  -  |   LB  |
            |  -  |  +  |   UB  |
            |  +  |  -  |   UB  |
            |  +  |  +  |   LB  |
            +-----+-----+-------+


        Parameters
        ----------
        solutions: pandas.Panel
            The DifferentialFVA panel with all the solutions. Each DataFrame is a design.
        reference_fva: pandas.DataFrame
            The FVA limits for the reference strain.

        Returns
        -------
        list
            A list of cameo.core.strain_design.StrainDesign for each DataFrame in solutions.
        """
        designs = []
        for _, solution in solutions.groupby(['biomass', 'production'],
                                             as_index=False,
                                             sort=False):
            targets = []
            relevant_targets = solution[
                (solution['normalized_gaps'].abs() > non_zero_flux_threshold)
                & (~solution['excluded']) & (~solution['free_flux'])]
            # Generate all knock-out targets.
            for rid in relevant_targets.loc[relevant_targets["KO"],
                                            "reaction"]:
                targets.append(ReactionKnockoutTarget(rid))
            # Generate all flux inversion targets.
            for row in relevant_targets[
                    relevant_targets["flux_reversal"]].itertuples():
                rid = row.Index
                ref_lower = reference_fva.at[rid, 'lower_bound']
                ref_upper = reference_fva.at[rid, 'upper_bound']
                if ref_upper > 0:
                    # Production point is negative so closest inversion is
                    # from reference lower bound to production upper bound.
                    targets.append(
                        ReactionInversionTarget(rid,
                                                value=row.upper_bound,
                                                reference_value=ref_lower))
                else:
                    # Production point is positive so closest inversion is
                    # from reference upper bound to production lower bound.
                    targets.append(
                        ReactionInversionTarget(rid,
                                                value=row.lower_bound,
                                                reference_value=ref_upper))
            # Generate all suddenly essential targets where we know the
            # reference interval lies around zero.
            for row in relevant_targets[
                    relevant_targets["suddenly_essential"]].itertuples():
                rid = row.Index
                if row.lower_bound > 0:
                    targets.append(
                        ReactionModulationTarget(
                            rid,
                            value=row.lower_bound,
                            reference_value=reference_fva.at[rid,
                                                             "upper_bound"]))
                else:
                    targets.append(
                        ReactionModulationTarget(
                            rid,
                            value=row.upper_bound,
                            reference_value=reference_fva.at[rid,
                                                             "lower_bound"]))
            # Generate all other flux modulation targets.
            for row in relevant_targets[
                (~relevant_targets["KO"])
                    & (~relevant_targets["flux_reversal"]) &
                (~relevant_targets["suddenly_essential"])].itertuples():
                rid = row.Index
                ref_lower = reference_fva.at[rid, 'lower_bound']
                ref_upper = reference_fva.at[rid, 'upper_bound']
                if row.normalized_gaps > 0:
                    # For now we ignore reactions that have a positive
                    # normalized gap, indicating that their flux is important
                    # for production, but where the reference flux is higher
                    # than the production flux.
                    if abs(ref_upper) > abs(row.lower_bound):
                        continue
                    targets.append(
                        ReactionModulationTarget(
                            rid,
                            value=row.lower_bound,
                            reference_value=ref_upper,
                            fold_change=row.normalized_gaps))
                else:
                    # For now we ignore reactions that have a negative
                    # normalized gap, indicating that their flux needs to
                    # decrease in production, but where the production
                    # interval is larger than the reference interval.
                    if abs(row.upper_bound) > abs(ref_lower):
                        continue
                    targets.append(
                        ReactionModulationTarget(
                            rid,
                            value=row.upper_bound,
                            reference_value=ref_lower,
                            fold_change=row.normalized_gaps))

            designs.append(StrainDesign(targets))
        return designs