Example #1
0
    def testInfeasibleDummyProblem(self):
        stoich_model = FakeStoichModel()
        thermo = FakeInfeasibleThermoData()
        kdata = kinetic_data.UniformKineticData()

        opt = protein_optimizer.ProteinOptimizer(stoich_model, thermo, kdata)
        res = opt.FindOptimum()
        self.assertEqual(optimized_pathway.OptimizationStatus.INFEASIBLE,
                         res.status.status)
Example #2
0
    def testDummyProblem(self):
        stoich_model = FakeStoichModel()
        thermo = FakeThermoData()
        kdata = kinetic_data.UniformKineticData()

        opt = protein_optimizer.ProteinOptimizer(stoich_model, thermo, kdata)
        res = opt.FindOptimum()
        self.assertEqual(optimized_pathway.OptimizationStatus.SUCCESSFUL,
                         res.status.status)

        result = res.opt_val
        self.assertAlmostEqual(0.048032, result, 3)
Example #3
0
def Main():
    options, _ = MakeOpts().parse_args(sys.argv)
    estimators = thermodynamic_estimators.LoadAllEstimators()

    input_filename = path.abspath(options.input_filename)
    if not path.exists(input_filename):
        logging.fatal('Input filename %s doesn\'t exist' % input_filename)

    print 'Will read pathway definitions from %s' % input_filename

    # Make thermodynamic and kinetic data containers
    thermo = estimators[options.thermodynamics_source]
    print "Using the thermodynamic estimations of: " + thermo.name
    thermo_data = thermodynamic_data.WrapperThermoData(thermo)

    # Uniform kinetic data
    kin_data = kinetic_data.UniformKineticData(kcat=100, km=1e-4)

    # Create a kegg instance
    kegg_instance = kegg.Kegg.getInstance()

    # Create output directories
    out_dir = options.output_dir
    if not path.exists(out_dir):
        util._mkdir(out_dir)
    pathgraph_dir = path.join(out_dir, 'pathway_graphs/')
    util._mkdir(pathgraph_dir)

    print 'Executing Protein Cost analysis'
    pathway_iterator = pathway.KeggPathwayIterator.FromFilename(input_filename)
    mtdfs = []
    protein_scores = []
    names = []
    num_atp = []
    path_lengths = []
    for pathway_data in pathway_iterator:
        if pathway_data.skip:
            print 'Skipping pathway', pathway_data.name
            continue

        print 'Analyzing pathway', pathway_data.name

        model = pathway_data.GetStoichiometricModel(kegg_instance)
        model_bounds = pathway_data.GetBounds()

        protein_opt = protein_optimizer.ProteinOptimizer(
            model, thermo_data, kin_data)
        mtdf_opt = mtdf_optimizer.MTDFOptimizer(model, thermo_data)

        # Solve MTDF.
        mtdf_res = mtdf_opt.FindMTDF(model_bounds)
        mtdf_status = mtdf_res.status
        if mtdf_status.IsFailure() or mtdf_status.IsInfeasible():
            print '\tFailed to optimize', pathway_data.name
            continue

        # Solve protein.
        protein_res = protein_opt.FindOptimum(model_bounds)
        protein_status = protein_res.status
        if protein_status.IsFailure() or protein_status.IsInfeasible():
            print '\tFailed to optimize', pathway_data.name
            continue

        mtdfs.append(mtdf_res.opt_val)
        protein_scores.append(protein_res.opt_val)
        names.append(model.name)

        net_reaction = mtdf_res.net_reaction.sparse
        atp_produced = net_reaction.get(2, 0)
        num_atp.append(atp_produced)
        path_lengths.append(len(mtdf_res.reaction_ids))

        pylab.figure()
        pylab.title(model.name)
        dGr0_tag = mtdf_res.dGr0_tag.flatten().tolist()
        dgmtdf = mtdf_res.dGr_tag.flatten().tolist()
        dgprotein = protein_res.dGr_tag.flatten().tolist()
        dgbio = mtdf_res.dGr_bio.flatten().tolist()
        dg0_profile = np.cumsum([0] + dGr0_tag)
        dgmtdf_profile = np.cumsum([0] + dgmtdf)
        dgprotein_profile = np.cumsum([0] + dgprotein)
        dgbio_profile = np.cumsum([0] + dgbio)

        rxn_range = pylab.arange(len(mtdf_res.reaction_ids) + 1)
        pylab.plot(rxn_range,
                   dg0_profile,
                   'b--',
                   linewidth=2,
                   label='Standard Conditions')
        pylab.plot(rxn_range,
                   dgbio_profile,
                   'c--',
                   linewidth=2,
                   label='Biological Conditions')
        mtdf_label = 'MTDF Optimized (MTDF = %.2g kJ/mol)' % mtdf_res.opt_val
        pylab.plot(rxn_range,
                   dgmtdf_profile,
                   'r-',
                   linewidth=2,
                   label=mtdf_label)
        pc_label = 'Protein Optimized (Cost = %.2g)' % protein_res.opt_val
        pylab.plot(rxn_range,
                   dgprotein_profile,
                   'g-',
                   linewidth=2,
                   label=pc_label)
        pylab.xticks(rxn_range[:-1] + 0.5, mtdf_res.reaction_ids)
        pylab.xlabel('Reaction step')
        pylab.ylabel('Cumulative dG (kJ/mol)')
        pylab.legend(loc='upper right', prop=LEGEND_FONT)

    pylab.figure()
    pylab.plot(num_atp, protein_scores, 'b.')
    #pylab.xlabel('MTDF (kJ/mol)')
    pylab.xlabel('Net ATP Production')
    pylab.ylabel('Protein Cost')
    for x, y, s in zip(num_atp, protein_scores, names):
        pylab.text(x, y, s, fontsize=10)

    max_protein = np.max(protein_scores)
    pylab.plot([0, 0], [0, max_protein], 'r--', label='0 ATP Produced')
    pylab.plot([1, 1], [0, max_protein], 'g--', label='1 ATP Produced')
    pylab.plot([2, 2], [0, max_protein], 'b--', label='2 ATP Produced')

    #pylab.yscale('log')
    pylab.xticks([])
    pylab.xlim((-1, 3))
    pylab.legend()

    odbs = np.tanh(np.array(mtdfs) / (2 * RT))

    pylab.figure()
    pylab.plot(protein_scores, odbs, 'b.')
    pylab.xlabel('Protein Cost')
    pylab.ylabel('ODB (unitless)')

    #for x,y,s in zip(protein_scores, length_scaled_cost, names):
    #    pylab.text(x, y, s, fontsize=10)
    pylab.show()
Example #4
0
def Main():
    np.seterr('raise')
    parser = MakeOpts()
    args = parser.parse_args()
    estimators = thermodynamic_estimators.LoadAllEstimators()
    
    input_filename = path.abspath(args.input_filename)
    if not path.exists(input_filename):
        logging.fatal('Input filename %s doesn\'t exist' % input_filename)
        
    print 'Will read pathway definitions from %s' % input_filename

    # Make thermodynamic and kinetic data containers
    thermo = estimators[args.thermodynamics_source]
    print "Using the thermodynamic estimations of: " + thermo.name
    thermo_data = thermodynamic_data.WrapperThermoData(thermo)
    
    # Fetch kinetic data.
    kin_data = kinetic_data.UniformKineticData(kcat=200, km=2e-4, mass=40)
    if args.kinetics_filename is not None:
        print 'Parsing kinetic data from', args.kinetics_filename
        kin_data = kinetic_data.KineticDataWithDefault.FromArrenFile(
            args.kinetics_filename)
        
    """
    kin_data = kinetic_data.KineticDataWithDefault.FromFiles(
        '../data/enzymatics/glycolytic_pathway_enzymes_kcat.csv',
        '../data/enzymatics/glycolytic_pathway_enzymes_km.csv')
    kin_data.SetDefaultKcat(100)
    kin_data.SetDefaultKM(1e-4)
    kin_data.SetDefaultMass(35)
    """
    
    # Create a kegg instance
    kegg_instance = kegg.Kegg.getInstance()

    # Create output directories
    out_dir = args.output_dir
    if not path.exists(out_dir):
        util._mkdir(out_dir)
    pathgraph_dir = path.join(out_dir, 'pathway_graphs/')
    util._mkdir(pathgraph_dir)
    
    print 'Executing Protein Cost analysis'
    pathway_iterator = pathway.KeggPathwayIterator.FromFilename(input_filename)
    results = []
    for pathway_data in pathway_iterator:
        if pathway_data.skip:
            print 'Skipping pathway', pathway_data.name
            continue
        
        print 'Analyzing pathway', pathway_data.name
                
        model = pathway_data.GetStoichiometricModel(kegg_instance)
        model_bounds = pathway_data.GetBounds()

        opt = protein_optimizer.ProteinOptimizer(model, thermo_data, kin_data)
        it = feasible_concentrations_iterator.FeasibleConcentrationsIterator(
            model, thermo_data, model_bounds)
        
        # Now solve with the default initial conditions.
        success = None
        result = None
        optima = []
        for i, x0 in enumerate(it):
            result = opt.FindOptimum(model_bounds, initial_concentrations=x0)
            status = result.status
            print '\t%s optimization %d' % (pathway_data.name, i)
            if status.failure:          
                print '\tFailed to optimize', pathway_data.name
                print '\t%s' % status
            elif status.infeasible:      
                print '\t', pathway_data.name, 'is infeasible!'
                print '\t%s' % status
            else:
                print '\t*Optimization successful'
                optima.append(result.opt_val)
                if not success:
                    success = result
                elif result.opt_val < success.opt_val:
                    success = result
        
        mean, error = None, None
        if optima:
            try:
                mean, error = stats.MeanWithConfidenceInterval(optima)
            except Exception, e:
                mean, error = None, None
                print optima
        result_dict = {'result': None,
                       'num_optima': len(optima),
                       'mean_opt': mean,
                       'error': error}
        
        if success is not None:
            success.WriteAllGraphs(pathgraph_dir)
            result_dict['result'] = success
        
            cost = success.opt_val
            if cost is not None:
                print '\t*Protein Cost for', pathway_data.name, '= %.2g' % cost
            if optima:
                print 'Found', len(optima), 'near-optima for', pathway_data.name 
                optima = np.array(optima)
                mean_opt = np.mean(optima)
                mean_diff = np.mean(np.abs(optima - mean_opt))
                print 'Mean optimum', mean_opt
                print 'Mean diff from mean', mean_diff
                print 'Percent diff %s%%' % (100*mean_diff / mean_opt)
                print 'StdDev opt', np.std(optima)
        else:
            # Use default conditions to show the failure
            res = opt.FindOptimum(model_bounds)
            result_dict['result'] = res            
        results.append(result_dict)