Пример #1
0
 def findMinMax(self, model, objectiveName, minValue, targets):
     positive = {}
     negative = {}
     imodel = LinearModel()
     imodel.extend(model)
     if minValue < 0:
         imodel.addColumnLimit(objectiveName,(None,minValue))
     else:
         imodel.addColumnLimit(objectiveName,(minValue,None))
     
     lp = LinearOptimization()
     lp.setModel(model)
     for t in targets:
         lp.clearObjective()
         lp.setObjectiveMap({t: -1})
         lp.runSimplex()
         fluxes = lp.getPredictionMap()
         lowValue = fluxes[t]
         negative[t] = lowValue
         
         lp.clearObjective()
         lp.setObjectiveMap({t: 1})
         lp.runSimplex()
         fluxes = lp.getPredictionMap()
         highValue = fluxes[t]
         positive[t] = highValue
         
     return (positive,negative)
 
     
def findBoundaryProduction(originalModel, bounds, combiLimits, objectiveName, productionName):
    model = LinearModel()
    model.extend(originalModel)
    if combiLimits != None:
        for (targetValues,limit) in combiLimits:
            model = addCombinationLimit(model, targetValues,limit)
    #objectiveMap = {objectiveName:-1.0}
    objectiveMap = {productionName:1.0}
    model.addColumnLimits(bounds)
    lp = LPSolver()
    fluxMap = lp.run(model,objectiveMap)
    objectiveValue = fluxMap[objectiveName]
    productionValue = fluxMap[productionName]
    return (fluxMap,objectiveValue,productionValue)
def LinearModelVariableBoundarys(originalModel, objectiveName, targets=None, pickleFileName=None, strict=False, minObjectivePercent=None,searchSize=1):
    '''
    Find min and max boundaries for the dependent variables of a linear model. 
    Uses limitMap as constraints on model.
     
    Add cumulative boundary persistence as pickle file function.
    
    @param linearModel: linear model to be analyzed
    @type linearModel: LinearModel
    @param limitMap: map of variable names and lower and upper limits
    @type limitMap: {variableName, (float, float)  
    @param targets: list of variables to perform analysis on
    @type targets: list
    @return: dictionary of lower and upper limits for each variable
    @rtype: {variableName: (lowerlimit, upperlimit)}
    '''
    
    linearModel = LinearModel()
    linearModel.extend(originalModel)
    
    if pickleFileName != None:
        if os.path.isfile(pickleFileName):
            print "loading saved flux boundaries"
            pFile = open(pickleFileName)
            result = pickle.load(pFile)
            pFile.close()
            return result
    
    originalLimits = linearModel.getColumnLimits()
    originalObjectiveLimit = originalLimits[objectiveName]
    objectiveVector = {objectiveName:-1.0}
    
    solver = LPSolver()
    originalValues = solver.run(model=linearModel, objective=objectiveVector)
    originalObjectiveValue = originalValues[objectiveName]
        
    if minObjectivePercent != None:
        minObjectiveValue = originalObjectiveValue * minObjectivePercent
        linearModel.addColumnLimit(objectiveName,(minObjectiveValue,None))
    
    if targets == None:
        targets = linearModel.getColumnNames()
    targets.add(objectiveName)

    print "Searching flux boundaries for %s > %s" % (objectiveName, minObjectiveValue)
    result = {}
    targetValues = targetValueCombinations(targets, [-1.0,1.0])
    controlSubSets = combinations(targetValues,searchSize)
    xcontrolSubSets = set(controlSubSets)
    
    for name in targets:
        nameTag = name
    #   if name not in linearModel.getColumnNames():
    #       print "target %s not found in model"
    #       print "boundary discovery not possible"
    #       continue
        
    #for iTargetValues in xcontrolSubSets:
    #    name = "testControlValue"
    #    nameTag = iTargetValues
    #    linearModel = combinationLimitBuilder(linearModel, iTargetValues, name)
        
        result[name] = (None,None)
        negObjective = {name:1.0}
        posObjective = {name:-1.0}
        
        solver.clearObjective()
        negValues = solver.run(linearModel, objective=negObjective)
        negValue = negValues[name]
        
        solver.clearObjective()
        posValues = solver.run(linearModel, objective=posObjective)
        posValue = posValues[name]
        
        delta = 1e-4
        if posValue != 0 or negValue !=0:
            pass
        
        if False:
            originalValue = originalValues[name]
            originalLimit = originalLimits[name]
        
            if not (negValue - delta <= originalValue <= posValue + delta):
                print "original value not in boundary"
                print "objective value [%s] (%s) %s <= %s <= %s (%s)" % (name, originalLimit[0], negValue, originalValue, posValue, originalLimit[1])
                #print "value neg code %s pos code %s" % (nscode, pscode)
                (negValue,posValue) = (originalLimit[0],originalLimit[1])
            else:
                #print "objective value [%s] (%s) %s <= %s <= %s (%s)" % (name, originalLimit[0], negValue, originalValue, posValue, originalLimit[1])
                pass
                
            if negValue == None or negValue == float("-inf"):
                continue
    
            if posValue == None or posValue == float("-inf"):
                continue
        
        result[nameTag] = (negValue,posValue)
        print "%s < [%s] < %s " % (negValue,nameTag,posValue)
    
    if pickleFileName != None:
        pFile = open(pickleFileName,'w')
        pickle.dump(result, pFile)
        pFile.close()
        
    solver.clear()
    del solver
    
    linearModel.addColumnLimit(objectiveName,originalObjectiveLimit)
    #targets.remove(objectiveName)
        
    return result