示例#1
0
def reaction_bounds(model, rxnIds, media):
    """
    Extract bounds for each reaction that are present in the SBML file.

    Parameters:
        model: SBML model
        rxnIds: List of reaction IDs from the model
        media: Media dictionary of compound IDs and other info. Value is None
               if a media file was not given.
    """
    # Generate list of external media compounds
    if media:
        mediaList = [m + "_e0" for m in media.keys()]
    rbounds = []
    for rIdx in rxnIds:
        r = model.getReaction(rIdx)
        rk = r.getKineticLaw()
        # Check if this is an exchange reaction
        # Need to make sure compound is present in media
        if "EX" in rIdx and media and rIdx.split("EX_")[-1] not in mediaList:
            lb = 0.0
        else:
            lb = float(rk.getParameter("LOWER_BOUND").getValue())
        ub = float(rk.getParameter("UPPER_BOUND").getValue())
        rbounds.append((lb, ub))

    if verbose:
        print "Number of reaction bounds: %d" % len(rbounds)
    lp.col_bounds(rbounds)
示例#2
0
def reaction_bounds(mdl, rctnids, rmedia):
    """
    Extract bounds for each reaction that are present in the SBML file.

    Parameters:
        mdl: SBML model
        rctnids: List of reaction IDs from the model
        rmedia: Media dictionary of compound IDs and other info. Value is None
               if a media file was not given.
    """
    # Generate list of external media compounds
    media_list = []
    if rmedia:
        media_list = [m + "_e0" for m in rmedia.keys()]
    rbounds = []
    for ridx in rctnids:
        r = mdl.getReaction(ridx)
        rk = r.getKineticLaw()
        # Check if this is an exchange reaction
        # Need to make sure compound is present in rmedia
        if "EX" in ridx and rmedia and ridx.split("EX_")[-1] not in media_list:
            lb = 0.0
        else:
            lb = float(rk.getParameter("LOWER_BOUND").getValue())
        ub = float(rk.getParameter("UPPER_BOUND").getValue())
        rbounds.append((lb, ub))

    if verbose:
        print(f"Number of reaction bounds: {len(rbounds)}")
    lp.col_bounds(rbounds)
示例#3
0
 def test_solve(self):
     """Test the complete linear programing solution, using the 
     example from the documentation"""
     mat = [
             [ 1.0, 1.0, 1.0],
             [10.0, 4.0, 5.0],
             [ 2.0, 2.0, 6.0],
     ]
     lp.load(mat)
     lp.objective_coefficients([ 10.0, 6.0, 4.0 ])
     lp.row_bounds([(None, 100.0), (None, 600.0), (None, 300.0)])
     lp.col_bounds([(0, None), (0, None), (0, None)])
     status, result = lp.solve()
     r = "%0.3f" % result
     self.assertEqual(r, "733.333")
     self.assertEqual(status, 'opt')
示例#4
0
    def test_bound_cols(self):
        """Test adding tuples of boundary conditions for columns"""
        mat = [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]
        ]
        lp.load(mat)

        boundsc = []
        # test that we don't have enough values
        self.assertRaises(ValueError, lp.col_bounds, boundsc)

        # a boundary of None should  be infiniity!
        boundsc = [(None, 1000), (1000, None), (1000, 1000), (None, None)]
        lp.col_bounds(boundsc)
示例#5
0
    def test_primals(self):
        """Test getting the primals back as a list"""
        mat = [
                [ 1.0, 1.0, 1.0],
                [10.0, 4.0, 5.0],
                [ 2.0, 2.0, 6.0],
        ]
        rh = ['a', 'b', 'c']
        ch = ['x', 'y', 'z']

        lp.load(mat, rh, ch)
        lp.objective_coefficients([ 10.0, 6.0, 4.0 ])
        lp.row_bounds([(None, 100.0), (None, 600.0), (None, 300.0)])
        lp.col_bounds([(0, None), (0, None), (0, None)])
        status, result = lp.solve()
        col_pri = [33.333333333333336, 66.66666666666666, 0.0]
        col_res = lp.col_primals()
        self.assertEqual(col_pri, col_res)
示例#6
0
文件: bounds.py 项目: linsalrob/PyFBA
def reaction_bounds(reactions, reactions_to_run, media, lower=-1000.0, mid=0.0, upper=1000.0, verbose=False):
    """
    Set the bounds for each reaction. We set the reactions to run between
    either lower/mid, mid/upper, or lower/upper depending on whether the
    reaction runs <=, =>, or <=> respectively.

    :param reactions: The dict of all reactions we know about
    :type reactions: dict of metabolism.Reaction
    :param reactions_to_run: The sorted list of reactions to run
    :type reactions_to_run: set
    :param media: The media compounds
    :type media: set
    :param lower: The default lower bound
    :type lower: float
    :param mid: The default mid value (typically 0)
    :type mid: float
    :param upper: The default upper bound
    :type upper: float
    :return: A dict of the reaction ID and the tuple of bounds
    :rtype: dict

    """

    rbvals = {}
    media_uptake_secretion_count = 0
    other_uptake_secretion_count = 0
    for r in reactions_to_run:
        # if we already know the bounds, eg from an SBML file
        if r is not 'BIOMASS_EQN' and reactions[r].lower_bound is not None and reactions[r].upper_bound is not None:
            rbvals[r] = (reactions[r].lower_bound, reactions[r].upper_bound)
            continue
        if r in reactions:
            direction = reactions[r].direction
        elif r == 'BIOMASS_EQN':
            direction = '>'
        else:
            sys.stderr.write("Did not find {} in reactions\n".format(r))
            direction = "="

        # this is where we define whether our media has the components
        if r != 'BIOMASS_EQN' and (reactions[r].is_uptake_secretion or reactions[r].is_transport):
            in_media = False
            override = False # if we have external compounds that are not in the media, we don't want to run this as a media reaction
            for c in reactions[r].left_compounds:
                if c.location == 'e':
                    if c in media:
                        in_media = True
                    else:
                        override = True
            # in this case, we have some external compounds that we should not import.
            # for example,
            if override:
                in_media = False

            if in_media:
                rbvals[r] = (lower, upper)
                media_uptake_secretion_count += 1
            else:
                rbvals[r] = (0.0, upper)
                other_uptake_secretion_count += 1
            continue

        if direction == "=":
            # This is what I think it should be:
            rbvals[r] = (lower, upper)
            # rbvals[r] = (mid, upper)
        elif direction == ">":
            # This is what I think it should be:
            rbvals[r] = (mid, upper)
            # rbvals[r] =  (lower, upper)
        elif direction == "<":
            # This is what I think it should be:
            # rbvals[r] = (lower, mid)
            rbvals[r] = (lower, upper)
        else:
            sys.stderr.write("DO NOT UNDERSTAND DIRECTION " + direction + " for " + r + "\n")
            rbvals[r] = (mid, upper)

    if verbose:
        sys.stderr.write("In parsing the bounds we found {} media uptake ".format(media_uptake_secretion_count) +
                         "and secretion reactions and {} other u/s reactions\n".format(other_uptake_secretion_count))

    rbounds = [rbvals[r] for r in reactions_to_run]
    lp.col_bounds(rbounds)
    return rbvals
示例#7
0
文件: bounds.py 项目: imagineyd/PyFBA
def reaction_bounds(reactions,
                    reactions_to_run,
                    media,
                    lower=-1000.0,
                    mid=0.0,
                    upper=1000.0,
                    verbose=False):
    """
    Set the bounds for each reaction. We set the reactions to run between
    either lower/mid, mid/upper, or lower/upper depending on whether the
    reaction runs <=, =>, or <=> respectively.

    :param reactions: The dict of all reactions we know about
    :type reactions: dict of metabolism.Reaction
    :param reactions_to_run: The sorted list of reactions to run
    :type reactions_to_run: set
    :param media: The media compounds
    :type media: set
    :param lower: The default lower bound
    :type lower: float
    :param mid: The default mid value (typically 0)
    :type mid: float
    :param upper: The default upper bound
    :type upper: float
    :return: A dict of the reaction ID and the tuple of bounds
    :rtype: dict

    """

    rbvals = {}
    media_uptake_secretion_count = 0
    other_uptake_secretion_count = 0
    for r in reactions_to_run:
        # if we already know the bounds, eg from an SBML file
        if r is not 'BIOMASS_EQN' and reactions[
                r].lower_bound is not None and reactions[
                    r].upper_bound is not None:
            rbvals[r] = (reactions[r].lower_bound, reactions[r].upper_bound)
            continue
        if r in reactions:
            direction = reactions[r].direction
        elif r == 'BIOMASS_EQN':
            direction = '>'
        else:
            sys.stderr.write("Did not find {} in reactions\n".format(r))
            direction = "="

        # this is where we define whether our media has the components
        if r != 'BIOMASS_EQN' and (reactions[r].is_uptake_secretion
                                   or reactions[r].is_transport):
            in_media = False
            override = False  # if we have external compounds that are not in the media, we don't want to run this as a media reaction
            for c in reactions[r].left_compounds:
                if c.location == 'e':
                    if c in media:
                        in_media = True
                    else:
                        override = True
            # in this case, we have some external compounds that we should not import.
            # for example,
            if override:
                in_media = False

            if in_media:
                rbvals[r] = (lower, upper)
                media_uptake_secretion_count += 1
            else:
                rbvals[r] = (0.0, upper)
                other_uptake_secretion_count += 1
            continue

        if direction == "=":
            # This is what I think it should be:
            rbvals[r] = (lower, upper)
            # rbvals[r] = (mid, upper)
        elif direction == ">":
            # This is what I think it should be:
            rbvals[r] = (mid, upper)
            # rbvals[r] =  (lower, upper)
        elif direction == "<":
            # This is what I think it should be:
            # rbvals[r] = (lower, mid)
            rbvals[r] = (lower, upper)
        else:
            sys.stderr.write("DO NOT UNDERSTAND DIRECTION " + direction +
                             " for " + r + "\n")
            rbvals[r] = (mid, upper)

    if verbose:
        sys.stderr.write("In parsing the bounds we found {} media uptake ".
                         format(media_uptake_secretion_count) +
                         "and secretion reactions and {} other u/s reactions\n"
                         .format(other_uptake_secretion_count))

    rbounds = [rbvals[r] for r in reactions_to_run]
    lp.col_bounds(rbounds)
    return rbvals