def __iter__(self):
        # First feasible solution: MTDF
        mtdf_opt = mtdf_optimizer.MTDFOptimizer(self._model, self._thermo)
        res = mtdf_opt.FindMTDF(
            concentration_bounds=self._concentration_bounds)

        # Bail entirely if the pathway is infeasible.
        status = res.status
        if status.IsInfeasible() or not res.ThermoFeasible():
            return

        # Only return data on successful optimization
        if status.IsSuccessful():
            yield np.matrix(res.ln_concentrations)

        # Second feasible solution: minimum sum of concentrations.
        conc_opt = concentration_optimizer.ConcentrationOptimizer(
            self._model, self._thermo)
        res = conc_opt.MinimizeConcentration(
            concentration_bounds=self._concentration_bounds)
        status = res.status
        if status.IsSuccessful():
            yield np.matrix(res.ln_concentrations)

        # Minimize each concentration separately.
        for i in xrange(self.Ncompounds):
            res = conc_opt.MinimizeConcentration(
                i, concentration_bounds=self._concentration_bounds)
            status = res.status
            if status.IsSuccessful():
                yield np.matrix(res.ln_concentrations)
Example #2
0
 def __init__(self, pathway_model, thermodynamic_data, kinetic_data,
              protein_cost_type=protein_cost_functors.ProteinCostFunc):
     """Initialize the MTDFOptimizer class.
     
     Args:
         pathway_model: the PathwayModel object.
         thermodynamic_data: the ThermodynamicData object.
         kinetic_data: the KineticData object.
         protein_cost_type: the functor type for calculating
           protein cost.
     """
     self._model = pathway_model
     self._thermo = thermodynamic_data
     self._kinetic_data = kinetic_data
     self.S = pathway_model.GetStoichiometricMatrix()
     self.Ncompounds, self.Nrxns = self.S.shape
     self.reactions = pathway_model.GetReactionIDs()
     self.compounds = pathway_model.GetCompoundIDs()
     self.fluxes = pathway_model.GetFluxes()
     self.dG0_r_prime = thermodynamic_data.GetDGrTagZero_ForModel(
             self._model)
     self.protein_cost_type = protein_cost_type
     
     self.mtdf_opt = mtdf_optimizer.MTDFOptimizer(pathway_model,
                                                  thermodynamic_data)
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

    thermo = estimators[options.thermodynamics_source]
    print "Using the thermodynamic estimations of: " + thermo.name
    thermo_data = thermodynamic_data.WrapperThermoData(thermo)

    # Create a bounds 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 MTDF 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()

        mtdf_opt = mtdf_optimizer.MTDFOptimizer(model, thermo_data)
        result = mtdf_opt.FindMTDF(model_bounds)

        print 'Optimization status', result.status

        result.WriteAllGraphs(pathgraph_dir)
        results.append(result)

        mtdf = result.opt_val
        print '\tMTDF for', pathway_data.name, '= %.2g' % mtdf

    output_filename = path.join(out_dir, 'results.html')
    print 'Writing output to', output_filename
    template_data = {'analysis_type': 'MTDF', 'results': results}
    templates.render_to_file('pathway_optimization_results.html',
                             template_data, output_filename)
Example #4
0
    def testDummyProblem(self):
        stoich_model = FakeStoichModel()
        thermo = FakeThermoData()

        opt = mtdf_optimizer.MTDFOptimizer(stoich_model, thermo)
        res = opt.FindMTDF()

        transformed_dgr = res.dGr_tag
        expected_mtdf = -np.min(transformed_dgr)
        mtdf = res.opt_val

        self.assertAlmostEqual(6.90989, mtdf, 3)
        self.assertAlmostEqual(expected_mtdf, mtdf, 3)
Example #5
0
    def testDummyProblemDifferentBounds(self):
        stoich_model = FakeStoichModel()
        thermo = FakeThermoData()

        b = self.MyBounds()
        opt = mtdf_optimizer.MTDFOptimizer(stoich_model, thermo)
        res = opt.FindMTDF(concentration_bounds=b)

        transformed_dgr = res.dGr_tag
        expected_mtdf = -np.min(transformed_dgr)
        mtdf = res.opt_val

        self.assertAlmostEqual(13.117132, mtdf, 3)
        self.assertAlmostEqual(expected_mtdf, mtdf, 3)
Example #6
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()