def loadVariousControlTable(fileNameTag,controls,sObjectiveTags,k,n,filter=lambda x: True):
    '''
    Parse control table files
    '''
    keyTag = "Row Names"
    controlTag = "Control"
    productionTag = ''
    #bestTargets = 'accA,fadD,fadE'.split(",")
    #bestTargets = 'aceE,fabH,fabD,accA,fabA,fabZ,fabB,fabF,fadA,fadI,fadB,fadD'.split(",")
    #mTargets = "pgi,pfkA,eno,fbaA,tpiA,gapA,pgk,gpmA".split(",")
    #bestTargets.extend(mTargets)
    bestTargets = 'accA,lpdA,aceE,aceF,gapA,pgk,fumB,fumC,mdh,acnA,scpC,sucC,sucD,sdhA,sdhB'.split(",")
    #bestTargets = 'lpdA,aceE,aceF,'.split(",")
    
    summaryReport = Report()
    targetReport = Report()
    
    print "best targets %s" % (bestTargets)
    for sObjective in sObjectiveTags:
        controlMaps = loadControlTableRange(fileNameTag, controls, sObjective, k, n, keyTag, controlTag, productionTag,filter)
        (uniqueTargets,targetValues,maxTargetSize,(bestMaps,score)) = processControlMaps(controlMaps, minProduction=0.0,bestTargets=bestTargets)
        print "Production %s => %s" % (sObjective,score)
        
        for map in bestMaps:
            print "%s" % (map)
        uTargetSize = len(uniqueTargets)
        summaryReport.add(sObjective, "Unique Target Number", uTargetSize)
        summaryReport.add(sObjective, "Largest Design", maxTargetSize)
        
        for (key,value) in targetValues.items():
            targetReport.add(key, sObjective, value)
        
    return (summaryReport,targetReport)
def findControlDependencies(con,originalModel,controls,exclusion,options,searchSize=0,targetPrecent = 0.40,verbose = False,usePersist=False):
    name = "".join(originalModel.modelName)
    naturalObjectiveName  = options.bioObj
    syntheticObjectiveName = options.synthObj
    controlTag = options.control
    
    persistTag = "Dependency_persist_%s_%s_%s_%s_%s_%s" % (controlTag,name,naturalObjectiveName,syntheticObjectiveName,str(searchSize),str(targetPercent))
    delta = 1e-4
    iter = 0
    iterSave = 100
    
    completed = set()
    controlSet = set()
    controlMap = []
    report = Report()
    
    persistValue = None
    if usePersist:
        persistValue = loadPersist(persistTag)
        if persistValue != None:
            print "loading persisted dependency search"
            (completed,controlMap,controlSet,report) = persistValue 
    
    controlSubSets = combinations(controls,searchSize)
    xcontrolSubSets = set(controlSubSets)
    xcontrolSubSets = xcontrolSubSets.difference(completed)
    
    print "Control dependency depth [%s] search sets [%s]" % (searchSize,len(xcontrolSubSets))
            
    lo = LPSolver()
    sObjective = {syntheticObjectiveName:-1.0}
    iPred = lo.run(model= originalModel, objective = sObjective)
    synthVal = iPred[syntheticObjectiveName]
    targetSVal = synthVal * targetPrecent
        
    #print "Synthetic Target [%s] = %s * %s" % (targetSVal,synthVal,targetPercent)
        
    controlMin = con.controlMin
    controlMax = con.controlMax
        
    con.controlMin = 0
    con.controlMax = 0
        
    targets = []
    gRMap = con.getGeneTargetMap()
        
    for (lib,prefix,target) in controls:
        if gRMap != None:
            if target in gRMap.keys():
                rTargets = gRMap[target]
                targets.extend(rTargets)
        else:
            targets.append(target)        
        
    #-----------------------------
    # Run optimization
    #-----------------------------
    iter = 0
    for controlSub in xcontrolSubSets:
        iter += 1
        completed.add(controlSub)
        
        #Analysis of control
        (prediction,objective) = simpleTestOpt(con, originalModel, controlSub, naturalObjectiveName,syntheticObjectiveName,verbose=False,naturalCoefficient=-0.5)
                
        #Save results        
        syntheticFlux = prediction[syntheticObjectiveName]
        #if verbose: print "Objective [%s]" % (objective)
        #if verbose: print "Flux [%s]" % (prediction)
            
        if syntheticFlux > targetSVal:    
            controlThresholds = controlThreshold([controlSub], exclusion)
            if controlSub in controlThresholds.keys():
                if syntheticFlux <= controlThresholds[controlSub] + delta:
                    #if verbose: print "no improvement"
                    continue
                
            if verbose: print "Synthetic Production [%s]" % (syntheticFlux)    
            
            controlResultMap = originalModel.annotateGenes(objective,annotationName = "bnumber", regex="[a-zA-Z0-9\(\)]+")
            controlTag = str(controlResultMap.keys())                
            
            controlSV = (controlSub,syntheticFlux)
            controlSet.add(controlSV)
            controlSV = (controlResultMap,syntheticFlux)
            controlMap.append(controlSV)
            
            #if verbose: print "Control Set [%s]" % (controlSub)
            if verbose: print "Control Map [%s]" % (controlResultMap)
                
            report.add(controlTag, "control values", controlResultMap)
            report.add(controlTag, "production", syntheticFlux)
                
        if iter % iterSave == 0:
            print "persisting iteration %s" % (iter)
            persistValue = (completed, controlSet,controlMap, report)
            persist(persistTag, persistValue)
                     
    con.controlMin = controlMin
    con.controlMax = controlMax
        
    #Persist dependency search
    if usePersist:
        persistValue = (completed, controlSet,controlMap, report)
        persist(persistTag, persistValue)

    return (controlMap,controlSet,report)