Ejemplo n.º 1
0
 def test_otherData(self):
     # load pH, ionic_strength, and temperature parameters
     other_data = thermodynamics_otherData()
     other_data.load_defaultData()
     other_data.check_data()
     assert(other_data.pH['c']['pH'] == 7.5)
     assert(other_data.pH['e']['pH'] == 7.0)
     assert(other_data.pH['p']['pH'] == 7.0)
     assert(other_data.ionic_strength['c']['ionic_strength'] == 0.2)
     assert(other_data.ionic_strength['c']['ionic_strength_units'] == 'M')
     assert(other_data.ionic_strength['e']['ionic_strength'] == 0.2)
     assert(other_data.ionic_strength['e']['ionic_strength_units'] == 'M')
     assert(other_data.ionic_strength['p']['ionic_strength'] == 0.2)
     assert(other_data.ionic_strength['p']['ionic_strength_units'] == 'M')        
     assert(other_data.temperature['c']['temperature'] == 310.15)
     assert(other_data.temperature['c']['temperature_units'] == 'K')
     assert(other_data.temperature['e']['temperature'] == 310.15)
     assert(other_data.temperature['e']['temperature_units'] == 'K')
     assert(other_data.temperature['p']['temperature'] == 310.15)
     assert(other_data.temperature['p']['temperature_units'] == 'K')
    def execute_calculate_dG_r(self,experiment_id_I,models_I,model_ids_I = [],
                            time_points_I=[],sample_name_abbreviations_I=[],
                            inconsistent_dG_f_I=[],inconsistent_concentrations_I=[],
                            measured_concentration_coverage_criteria_I=0.5,
                            measured_dG_f_coverage_criteria_I=0.99):

        '''calculate dG0_r, dG_r, displacements, and perform a thermodynamic consistency check'''
        print('calculating dG_r for experiment_id ' + experiment_id_I);
        dG0_r_O = [];
        dG_r_O = [];
        tcc_O = [];
        # get the model ids:
        if model_ids_I:
            model_ids = model_ids_I;
        else:
            model_ids = [];
            model_ids = self.get_modelID_experimentID_dataStage03QuantificationSimulation(experiment_id_I);
        for model_id in model_ids:
            print('calculating dG_r for model_id ' + model_id);
            # get the cobra model
            cobra_model = models_I[model_id];
            # get simulated data
            fva_data,sra_data = {},{};
            fva_data,sra_data = self.get_rowsDict_experimentIDAndModelID_dataStage03QuantificationSimulatedData(experiment_id_I,model_id);
            # load simulated data
            simulated_data = thermodynamics_simulatedData(fva_data_I=fva_data,sra_data_I=sra_data);
            simulated_data.check_data();
            # get the time-points
            if time_points_I:
                time_points = time_points_I;
            else:
                time_points = [];
                time_points = self.get_timePoints_experimentIDAndModelID_dataStage03QuantificationSimulation(experiment_id_I,model_id);
            for tp in time_points:
                # get sample_name_abbreviations
                if sample_name_abbreviations_I:
                    sample_name_abbreviations = sample_name_abbreviations_I;
                else:
                    sample_name_abbreviations = [];
                    sample_name_abbreviations = self.get_sampleNameAbbreviations_experimentIDAndModelIDAndTimePoint_dataStage03QuantificationSimulation(experiment_id_I,model_id,tp);
                for sna in sample_name_abbreviations:
                    print('calculating dG_r for sample_name_abbreviation ' + sna);
                    # get otherData
                    pH,temperature,ionic_strength = {},{},{}
                    pH,temperature,ionic_strength = self.get_rowsFormatted_experimentIDAndTimePointAndSampleNameAbbreviation_dataStage03QuantificationOtherData(experiment_id_I,tp,sna);
                    # load pH, ionic_strength, and temperature parameters
                    other_data = thermodynamics_otherData(pH_I=pH,temperature_I=temperature,ionic_strength_I=ionic_strength);
                    other_data.check_data();
                    # get dG_f data:
                    dG_f = {};
                    dG_f = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationDGf(experiment_id_I,model_id,tp,sna);
                    dG_f_data = thermodynamics_dG_f_data(dG_f_I=dG_f);
                    dG_f_data.format_dG_f();
                    dG_f_data.generate_estimated_dG_f(cobra_model)
                    dG_f_data.check_data(); 
                    # remove an inconsistent dGf values
                    if inconsistent_dG_f_I: dG_f_data.remove_measured_dG_f(inconsistent_dG_f_I)
                    # query metabolomicsData
                    concentrations = [];
                    concentrations = self.get_rowsDict_experimentIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationMetabolomicsData(experiment_id_I,tp,sna);
                    # load metabolomicsData
                    metabolomics_data = thermodynamics_metabolomicsData(measured_concentrations_I=concentrations);
                    metabolomics_data.generate_estimated_metabolomics_data(cobra_model);
                    # remove an inconsistent concentration values
                    if inconsistent_concentrations_I: metabolomics_data.remove_measured_concentrations(inconsistent_concentrations_I);
                    # calculate dG0r, dGr, displacements, and perform a thermodynamic consistency check based on model simulations
                    tcc = thermodynamics_dG_r_data();
                    tcc.calculate_dG0_r_v3(cobra_model, dG_f_data.measured_dG_f, dG_f_data.estimated_dG_f, other_data.temperature); # calculate the change in free energy of reaction without accounting for metabolite concentrations
                    tcc.calculate_dG_r_v3(cobra_model,metabolomics_data.measured_concentrations, metabolomics_data.estimated_concentrations,
                                        other_data.pH, other_data.ionic_strength, other_data.temperature); # adjust the change in free energy of reaction for intracellular metabolite concentrations
                    tcc.check_thermodynamicConsistency(cobra_model,simulated_data.fva_data,
                                        metabolomics_data.measured_concentrations,
                                        metabolomics_data.estimated_concentrations,
                                        other_data.pH,other_data.ionic_strength,other_data.temperature,
                                        measured_concentration_coverage_criteria_I,
                                        measured_dG_f_coverage_criteria_I); # check the thermodynamic consistency of the data
                    tcc.calculate_displacement_v3(cobra_model,metabolomics_data.measured_concentrations, metabolomics_data.estimated_concentrations); # calculate the displacements from equillibrium
                    #tcc.simulate_infeasibleReactions(cobra_model); # simulate thermodynamically inconsistent data
                    #tcc.constrain_infeasibleReactions(cobra_model); # remove thermodynamically inconsistent reactions from the model
                    # upload dG0r, dGr, displacements, and results of tcc
                    for k,v in tcc.dG_r.items():
                        dG0_r_tmp = {'experiment_id':experiment_id_I,
                                'model_id':model_id,
                                'sample_name_abbreviation':sna,
                                'time_point':tp,
                                'rxn_id':k,
                                'Keq_lb':tcc.dG0_r[k]['Keq_lb'],
                                'Keq_ub':tcc.dG0_r[k]['Keq_ub'],
                                'dG0_r':tcc.dG0_r[k]['dG_r'],
                                'dG0_r_var':tcc.dG0_r[k]['dG_r_var'],
                                'dG0_r_units':tcc.dG0_r[k]['dG_r_units'],
                                'dG0_r_lb':tcc.dG0_r[k]['dG_r_lb'],
                                'dG0_r_ub':tcc.dG0_r[k]['dG_r_ub'],
                                'used_':True,
                                'comment_':None};
                        dG0_r_O.append(dG0_r_tmp);
                        dG_r_tmp = {'experiment_id':experiment_id_I,
                                'model_id':model_id,
                                'sample_name_abbreviation':sna,
                                'time_point':tp,
                                'rxn_id':k,
                                'Keq_lb':tcc.dG_r[k]['Keq_lb'],
                                'Keq_ub':tcc.dG_r[k]['Keq_ub'],
                                'dG_r':tcc.dG_r[k]['dG_r'],
                                'dG_r_var':tcc.dG_r[k]['dG_r_var'],
                                'dG_r_units':tcc.dG_r[k]['dG_r_units'],
                                'dG_r_lb':tcc.dG_r[k]['dG_r_lb'],
                                'dG_r_ub':tcc.dG_r[k]['dG_r_ub'],
                                'displacement_lb':tcc.displacement[k]['displacement_lb'],
                                'displacement_ub':tcc.displacement[k]['displacement_ub'],
                                'Q_lb':tcc.displacement[k]['Q_lb'],
                                'Q_ub':tcc.displacement[k]['Q_ub'],
                                'used_':True,
                                'comment_':None};
                        dG_r_O.append(dG_r_tmp);
                        tcc_tmp = {'experiment_id':experiment_id_I,
                                'model_id':model_id,
                                'sample_name_abbreviation':sna,
                                'time_point':tp,
                                'rxn_id':k,
                                'feasible':tcc.thermodynamic_consistency_check[k],
                                'measured_concentration_coverage_criteria':measured_concentration_coverage_criteria_I,
                                'measured_dG_f_coverage_criteria':measured_dG_f_coverage_criteria_I,
                                'measured_concentration_coverage':tcc.metabolomics_coverage[k],
                                'measured_dG_f_coverage':tcc.dG_r_coverage[k],
                                'used_':True,
                                'comment_':None};
                        tcc_O.append(tcc_tmp);
                        #try:
                        #    row = None;
                        #    row = data_stage03_quantification_dG0_r(experiment_id_I,
                        #            model_id,
                        #            sna,
                        #            tp,
                        #            k,
                        #            tcc.dG0_r[k]['Keq_lb'],
                        #            tcc.dG0_r[k]['Keq_ub'],
                        #            tcc.dG0_r[k]['dG_r'],
                        #            tcc.dG0_r[k]['dG_r_var'],
                        #            tcc.dG0_r[k]['dG_r_units'],
                        #            tcc.dG0_r[k]['dG_r_lb'],
                        #            tcc.dG0_r[k]['dG_r_ub'],
                        #            True,
                        #            None);
                        #    self.session.add(row);
                        #    row = None;
                        #    row = data_stage03_quantification_dG_r(experiment_id_I,
                        #            model_id,
                        #            sna,
                        #            tp,
                        #            k,
                        #            tcc.dG_r[k]['Keq_lb'],
                        #            tcc.dG_r[k]['Keq_ub'],
                        #            tcc.dG_r[k]['dG_r'],
                        #            tcc.dG_r[k]['dG_r_var'],
                        #            tcc.dG_r[k]['dG_r_units'],
                        #            tcc.dG_r[k]['dG_r_lb'],
                        #            tcc.dG_r[k]['dG_r_ub'],
                        #            tcc.displacement[k]['displacement_lb'],
                        #            tcc.displacement[k]['displacement_ub'],
                        #            tcc.displacement[k]['Q_lb'],
                        #            tcc.displacement[k]['Q_ub'],
                        #            True,
                        #            None);
                        #    self.session.add(row);
                        #    row = None;
                        #    row = data_stage03_quantification_tcc(experiment_id_I,
                        #            model_id,
                        #            sna,
                        #            tp,
                        #            k,
                        #            tcc.thermodynamic_consistency_check[k],
                        #            measured_concentration_coverage_criteria_I,
                        #            measured_dG_f_coverage_criteria_I,
                        #            tcc.metabolomics_coverage[k],
                        #            tcc.dG_r_coverage[k],
                        #            True,
                        #            None);
                        #    self.session.add(row);
                        #except sqlalchemy.exc.IntegrityError as e:
                        #    print(e);
                        #    print("Press any key to continue")
                        #    a=input();
                    #self.session.commit();  
        # add datat to the DB
        self.add_dataStage03QuantificationDG0r(dG0_r_O);
        self.add_dataStage03QuantificationDGr(dG_r_O);
        self.add_dataStage03QuantificationTcc(tcc_O);
    def execute_thermodynamicSampling(self,simulation_id_I,models_I,
                    data_dir_I,rxn_ids_I=[],
                    inconsistent_dG_f_I=[],inconsistent_concentrations_I=[],
                    inconsistent_tcc_I=[],
                    measured_concentration_coverage_criteria_I=0.5,
                    measured_dG_f_coverage_criteria_I=0.99,
                    solver_I='glpk'):
        '''execute a thermodynamic analysis using the thermodynamic
        module for cobrapy

        Input:
           inconsistent_dG_f_I = dG_f measured values to be substituted for estimated values
           inconsistent_concentrations_I = concentration measured values to be substituted for estimated values
           inconsistent_tcc_I = reactions considered feasible to be changed to infeasible so that dG0_r constraints do not break the model
           measured_concentration_coverage_criteria_I = float, minimum concentration coverage to consider for feasibility
           measured_dG_f_coveragea_criteria_I = float, minimum dG_f coverage to consider for feasibility
           data_dir_I = directory of sampled points
           solver_I = string, solver name
        '''
        modelsCOBRA = models_COBRA_dependencies();
        print('execute_thermodynamicSampling...')
        # get simulation information
        simulation_info_all = [];
        simulation_info_all = self.get_rows_simulationIDAndSimulationType_dataStage03QuantificationSimulation(simulation_id_I,'sampling')
        if not simulation_info_all:
            print('simulation not found!')
            return;
        simulation_info = simulation_info_all[0]; # unique constraint guarantees only 1 row will be returned
        # get simulation parameters
        simulation_parameters_all = [];
        simulation_parameters_all = self.get_rows_simulationID_dataStage03QuantificationSimulationParameters(simulation_id_I);
        if not simulation_parameters_all:
            print('simulation not found!')
            return;
        simulation_parameters = simulation_parameters_all[0]; # unique constraint guarantees only 1 row will be returned
        # get the cobra model
        cobra_model = models_I[simulation_info['model_id']];
        # copy the model
        cobra_model_copy = cobra_model.copy();
        # get rxn_ids
        if rxn_ids_I:
            rxn_ids = rxn_ids_I;
        else:
            rxn_ids = [];
            rxn_ids = self.get_rows_experimentIDAndModelIDAndSampleNameAbbreviation_dataStage03QuantificationMeasuredFluxes(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['sample_name_abbreviation']);
        for rxn in rxn_ids:
            # constrain the model
            cobra_model_copy.reactions.get_by_id(rxn['rxn_id']).lower_bound = rxn['flux_lb'];
            cobra_model_copy.reactions.get_by_id(rxn['rxn_id']).upper_bound = rxn['flux_ub'];
        # make the model irreversible
        convert_to_irreversible(cobra_model_copy);
        # get otherData
        pH,temperature,ionic_strength = {},{},{}
        pH,temperature,ionic_strength = self.get_rowsFormatted_experimentIDAndTimePointAndSampleNameAbbreviation_dataStage03QuantificationOtherData(simulation_info['experiment_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        # load pH, ionic_strength, and temperature parameters
        other_data = thermodynamics_otherData(pH_I=pH,temperature_I=temperature,ionic_strength_I=ionic_strength);
        other_data.check_data();
        # get dG_f data:
        dG_f = {};
        dG_f = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationDGf(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        dG_f_data = thermodynamics_dG_f_data(dG_f_I=dG_f);
        dG_f_data.format_dG_f();
        dG_f_data.generate_estimated_dG_f(cobra_model)
        dG_f_data.check_data(); 
        # remove an inconsistent dGf values
        if inconsistent_dG_f_I: dG_f_data.remove_measured_dG_f(inconsistent_dG_f_I)
        # query metabolomicsData
        concentrations = [];
        concentrations = self.get_rowsDict_experimentIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationMetabolomicsData(simulation_info['experiment_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        # load metabolomicsData
        metabolomics_data = thermodynamics_metabolomicsData(measured_concentrations_I=concentrations);
        metabolomics_data.generate_estimated_metabolomics_data(cobra_model);
        # remove an inconsistent concentration values
        if inconsistent_concentrations_I: metabolomics_data.remove_measured_concentrations(inconsistent_concentrations_I);
        # get dG0r, dGr, and tcc data
        dG0_r = {};
        dG0_r = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationDG0r(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation'])
        measured_concentration_coverage,measured_dG_f_coverage,feasible = {},{},{};
        measured_concentration_coverage,measured_dG_f_coverage,feasible = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationTCC(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation'],0,0)
        tcc = thermodynamics_dG_r_data(dG0_r_I = dG0_r,
                 dG_r_coverage_I = measured_dG_f_coverage,
                 metabolomics_coverage_I = measured_concentration_coverage,
                 thermodynamic_consistency_check_I = feasible);
        if inconsistent_tcc_I: tcc.change_feasibleReactions(inconsistent_tcc_I);
        # apply tfba constraints
        tfba = thermodynamics_tfba()
        tfba._add_conc_ln_constraints_transport(cobra_model_copy, metabolomics_data.measured_concentrations, metabolomics_data.estimated_concentrations,
                                             tcc.dG0_r, other_data.pH,other_data.temperature,tcc.metabolomics_coverage,
                                             tcc.dG_r_coverage, tcc.thermodynamic_consistency_check,
                                             measured_concentration_coverage_criteria_I, measured_dG_f_coverage_criteria_I,
                                             use_measured_concentrations=True,use_measured_dG0_r=True);
        # Test model
        if modelsCOBRA.test_model(cobra_model_I=cobra_model_copy):
            sampling = cobra_sampling(data_dir_I = data_dir_I);
            if simulation_parameters['sampler_id']=='gpSampler':
                filename_model = simulation_id_I + '.mat';
                filename_script = simulation_id_I + '.m';
                filename_points = simulation_id_I + '_points' + '.mat';
                sampling.export_sampling_matlab(cobra_model=cobra_model_copy,filename_model=filename_model,filename_script=filename_script,filename_points=filename_points,\
                    solver_id_I = simulation_parameters['solver_id'],\
                    n_points_I = simulation_parameters['n_points'],\
                    n_steps_I = simulation_parameters['n_steps'],\
                    max_time_I = simulation_parameters['max_time']);
            elif simulation_parameters['sampler_id']=='optGpSampler':
                return;
            else:
                print('sampler_id not recognized');
        else:
            print('no solution found!');  
    def execute_analyzeThermodynamicSamplingPoints(self,simulation_id_I,models_I,
                    data_dir_I,data_dir_O,rxn_ids_I=[],
                    inconsistent_dG_f_I=[],inconsistent_concentrations_I=[],
                    inconsistent_tcc_I=[],
                    measured_concentration_coverage_criteria_I=0.5,
                    measured_dG_f_coverage_criteria_I=0.99,
                    remove_pointsNotInSolutionSpace_I=True,
                    min_pointsInSolutionSpace_I=1000):
        '''Load and analyze sampling points

        Input:
           inconsistent_dG_f_I = dG_f measured values to be substituted for estimated values
           inconsistent_concentrations_I = concentration measured values to be substituted for estimated values
           inconsistent_tcc_I = reactions considered feasible to be changed to infeasible so that dG0_r constraints do not break the model
           measured_concentration_coverage_criteria_I = float, minimum concentration coverage to consider for feasibility
           measured_dG_f_coveragea_criteria_I = float, minimum dG_f coverage to consider for feasibility
           remove_pointsNotInSolutionSpace_I = boolean, remove points not in the solution space (i.e., within the lower/upper bounds)
           min_pointsInSolutionSpace_I = int, minimum number of points in the solution space.
                                        if the number of points is less that the minimum, the solution space will be increased by
                                        (upper_bounds-lower_bounds)/4 until the minimum number of points is met
           data_dir_I = directory of sampled points
           data_dir_O = director to write QC'd sampled points
           solver_I = string, solver name
           
           '''

        print('analyzing sampling points');
        
        modelsCOBRA = models_COBRA_dependencies();
        # get simulation information
        simulation_info_all = [];
        simulation_info_all = self.get_rows_simulationIDAndSimulationType_dataStage03QuantificationSimulation(simulation_id_I,'sampling')
        if not simulation_info_all:
            print('simulation not found!')
            return;
        simulation_info = simulation_info_all[0]; # unique constraint guarantees only 1 row will be returned
        # get simulation parameters
        simulation_parameters_all = [];
        simulation_parameters_all = self.get_rows_simulationID_dataStage03QuantificationSimulationParameters(simulation_id_I);
        if not simulation_parameters_all:
            print('simulation not found!')
            return;
        simulation_parameters = simulation_parameters_all[0]; # unique constraint guarantees only 1 row will be returned
        # get the cobra model
        cobra_model = models_I[simulation_info['model_id']];
        # copy the model
        cobra_model_copy = cobra_model.copy();
        # get rxn_ids
        if rxn_ids_I:
            rxn_ids = rxn_ids_I;
        else:
            rxn_ids = [];
            rxn_ids = self.get_rows_experimentIDAndModelIDAndSampleNameAbbreviation_dataStage03QuantificationMeasuredFluxes(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['sample_name_abbreviation']);
        for rxn in rxn_ids:
            # constrain the model
            cobra_model_copy.reactions.get_by_id(rxn['rxn_id']).lower_bound = rxn['flux_lb'];
            cobra_model_copy.reactions.get_by_id(rxn['rxn_id']).upper_bound = rxn['flux_ub'];
        # make the model irreversible
        convert_to_irreversible(cobra_model_copy);
        # get otherData
        pH,temperature,ionic_strength = {},{},{}
        pH,temperature,ionic_strength = self.get_rowsFormatted_experimentIDAndTimePointAndSampleNameAbbreviation_dataStage03QuantificationOtherData(simulation_info['experiment_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        # load pH, ionic_strength, and temperature parameters
        other_data = thermodynamics_otherData(pH_I=pH,temperature_I=temperature,ionic_strength_I=ionic_strength);
        other_data.check_data();
        # get dG_f data:
        dG_f = {};
        dG_f = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationDGf(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        dG_f_data = thermodynamics_dG_f_data(dG_f_I=dG_f);
        dG_f_data.format_dG_f();
        dG_f_data.generate_estimated_dG_f(cobra_model)
        dG_f_data.check_data(); 
        # remove an inconsistent dGf values
        if inconsistent_dG_f_I: dG_f_data.remove_measured_dG_f(inconsistent_dG_f_I)
        # query metabolomicsData
        concentrations = [];
        concentrations = self.get_rowsDict_experimentIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationMetabolomicsData(simulation_info['experiment_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        # load metabolomicsData
        metabolomics_data = thermodynamics_metabolomicsData(measured_concentrations_I=concentrations);
        metabolomics_data.generate_estimated_metabolomics_data(cobra_model);
        # remove an inconsistent concentration values
        if inconsistent_concentrations_I: metabolomics_data.remove_measured_concentrations(inconsistent_concentrations_I);
        # get dG0r, dGr, and tcc data
        dG0_r = {};
        dG0_r = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationDG0r(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation'])
        measured_concentration_coverage,measured_dG_f_coverage,feasible = {},{},{};
        measured_concentration_coverage,measured_dG_f_coverage,feasible = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationTCC(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation'],0,0)
        tcc = thermodynamics_dG_r_data(dG0_r_I = dG0_r,
                 dG_r_coverage_I = measured_dG_f_coverage,
                 metabolomics_coverage_I = measured_concentration_coverage,
                 thermodynamic_consistency_check_I = feasible);
        if inconsistent_tcc_I: tcc.change_feasibleReactions(inconsistent_tcc_I);
        # apply tfba constraints
        tfba = thermodynamics_tfba()
        tfba._add_conc_ln_constraints_transport(cobra_model_copy, metabolomics_data.measured_concentrations, metabolomics_data.estimated_concentrations,
                                             tcc.dG0_r, other_data.pH,other_data.temperature,tcc.metabolomics_coverage,
                                             tcc.dG_r_coverage, tcc.thermodynamic_consistency_check,
                                             measured_concentration_coverage_criteria_I, measured_dG_f_coverage_criteria_I,
                                             use_measured_concentrations=True,use_measured_dG0_r=True);
        # Test each model
        if modelsCOBRA.test_model(cobra_model_I=cobra_model_copy):
            sampling = cobra_sampling(data_dir_I = data_dir_I,model_I = cobra_model_copy);
            if simulation_parameters['sampler_id']=='gpSampler':
                # load the results of sampling
                filename_points = simulation_id_I + '_points' + '.mat';
                sampling.get_points_matlab(filename_points,'sampler_out');
                # check if points were sampled outside the solution space
                if remove_pointsNotInSolutionSpace_I:
                    pruned_reactions = sampling.remove_points_notInSolutionSpace(min_points_I=min_pointsInSolutionSpace_I);
                ## check if the model contains loops
                #sampling.simulate_loops(data_fva=settings.workspace_data + '/loops_fva_tmp.json');
                #sampling.find_loops(data_fva=settings.workspace_data + '/loops_fva_tmp.json');
                #sampling.remove_loopsFromPoints();
                sampling.descriptive_statistics();
            elif simulation_parameters['sampler_id']=='optGpSampler':
                return;
            else:
                print('sampler_id not recognized');
            # add data to the database
            row = {'simulation_id':simulation_id_I,
                'simulation_dateAndTime':sampling.simulation_dateAndTime,
                'mixed_fraction':sampling.mixed_fraction,
                'data_dir':data_dir_I+'/'+filename_points,
                'infeasible_loops':sampling.loops,
                'used_':True,
                'comment_':None
                };
            self.add_dataStage03QuantificationSampledPoints([row])
            #row = None;
            #row = data_stage03_quantification_sampledPoints(
            #    simulation_id_I,
            #    sampling.simulation_dateAndTime,
            #    sampling.mixed_fraction,
            #    data_dir_I+'/'+filename_points,
            #    sampling.loops,
            #    True,
            #    None);
            #self.session.add(row);
            # write points to json file

            # add data to the database
            sampledData_O = [];
            for k,v in sampling.points_statistics.items():
                type,units = tfba.get_variableTypeAndUnits(k);
                row = {'simulation_id':simulation_id_I,
                    'simulation_dateAndTime':sampling.simulation_dateAndTime,
                    'variable_id':k,
                    'variable_type':type,
                    'variable_units':units,
                    'sampling_points':None, #v['points'],
                    'sampling_ave':v['ave'],
                    'sampling_var':v['var'],
                    'sampling_lb':v['lb'],
                    'sampling_ub':v['ub'],
                    'sampling_ci':0.95,
                    'sampling_min':v['min'],
                    'sampling_max':v['max'],
                    'sampling_median':v['median'],
                    'sampling_iq_1':v['iq_1'],
                    'sampling_iq_3':v['iq_3'],
                    'used_':True,
                    'comment_':None};
                sampledData_O.append(row);
                #row = None;
                #row = data_stage03_quantification_sampledData(
                #    simulation_id_I,
                #    sampling.simulation_dateAndTime,
                #    k,
                #    type,
                #    units,
                #    None, #v['points'],
                #    v['ave'],
                #    v['var'],
                #    v['lb'],
                #    v['ub'],
                #    v['min'],
                #    0.95,
                #    v['max'],
                #    v['median'],
                #    v['iq_1'],
                #    v['iq_3'],
                #    True,
                #    None);
                #self.session.add(row);
            self.add_dataStage03QuantificationSampledData(sampledData_O);
        else:
            print('no solution found!'); 
    def check_thermodynamicConstraints(self,simulation_id_I,models_I,rxn_ids_I=[],
                    inconsistent_dG_f_I=[],inconsistent_concentrations_I=[],
                    measured_concentration_coverage_criteria_I=0.5,
                    measured_dG_f_coverage_criteria_I=0.99,
                    n_checks_I = 5,
                    diagnose_solver_I='glpk',diagnose_threshold_I=0.98,diagnose_break_I=0.1):
        
        print('check_thermodynamicConstraints...')
        # get simulation information
        simulation_info_all = [];
        simulation_info_all = self.get_rows_simulationID_dataStage03QuantificationSimulation(simulation_id_I);
        if not simulation_info_all:
            print('simulation not found!')
            return;
        simulation_info = simulation_info_all[0]; # unique constraint guarantees only 1 row will be returned
        # get simulation parameters
        simulation_parameters_all = [];
        simulation_parameters_all = self.get_rows_simulationID_dataStage03QuantificationSimulationParameters(simulation_id_I);
        if not simulation_parameters_all:
            print('simulation not found!')
            return;
        simulation_parameters = simulation_parameters_all[0]; # unique constraint guarantees only 1 row will be returned
        # get the cobra model
        cobra_model = models_I[simulation_info['model_id']];
        # copy the model
        cobra_model_copy = cobra_model.copy();
        # get rxn_ids
        if rxn_ids_I:
            rxn_ids = rxn_ids_I;
        else:
            rxn_ids = [];
            rxn_ids = self.get_rows_experimentIDAndModelIDAndSampleNameAbbreviation_dataStage03QuantificationMeasuredFluxes(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['sample_name_abbreviation']);
        for rxn in rxn_ids:
            # constrain the model
            cobra_model_copy.reactions.get_by_id(rxn['rxn_id']).lower_bound = rxn['flux_lb'];
            cobra_model_copy.reactions.get_by_id(rxn['rxn_id']).upper_bound = rxn['flux_ub'];
        # make the model irreversible
        convert_to_irreversible(cobra_model_copy);
        # get otherData
        pH,temperature,ionic_strength = {},{},{}
        pH,temperature,ionic_strength = self.get_rowsFormatted_experimentIDAndTimePointAndSampleNameAbbreviation_dataStage03QuantificationOtherData(simulation_info['experiment_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        # load pH, ionic_strength, and temperature parameters
        other_data = thermodynamics_otherData(pH_I=pH,temperature_I=temperature,ionic_strength_I=ionic_strength);
        other_data.check_data();
        # get dG_f data:
        dG_f = {};
        dG_f = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationDGf(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        dG_f_data = thermodynamics_dG_f_data(dG_f_I=dG_f);
        dG_f_data.format_dG_f();
        dG_f_data.generate_estimated_dG_f(cobra_model)
        dG_f_data.check_data(); 
        # remove an inconsistent dGf values
        if inconsistent_dG_f_I: dG_f_data.remove_measured_dG_f(inconsistent_dG_f_I)
        # query metabolomicsData
        concentrations = [];
        concentrations = self.get_rowsDict_experimentIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationMetabolomicsData(simulation_info['experiment_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation']);
        # load metabolomicsData
        metabolomics_data = thermodynamics_metabolomicsData(measured_concentrations_I=concentrations);
        metabolomics_data.generate_estimated_metabolomics_data(cobra_model);
        # remove an inconsistent concentration values
        if inconsistent_concentrations_I: metabolomics_data.remove_measured_concentrations(inconsistent_concentrations_I);
        # get dG0r, dGr, and tcc data
        dG0_r = {};
        dG0_r = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationDG0r(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation'])
        measured_concentration_coverage,measured_dG_f_coverage,feasible = {},{},{};
        measured_concentration_coverage,measured_dG_f_coverage,feasible = self.get_rowsDict_experimentIDAndModelIDAndTimePointAndSampleNameAbbreviations_dataStage03QuantificationTCC(simulation_info['experiment_id'],simulation_info['model_id'],simulation_info['time_point'],simulation_info['sample_name_abbreviation'],0,0)
        tcc = thermodynamics_dG_r_data(dG0_r_I = dG0_r,
                 dG_r_coverage_I = measured_dG_f_coverage,
                 metabolomics_coverage_I = measured_concentration_coverage,
                 thermodynamic_consistency_check_I = feasible);
        # apply tfba constraints
        tfba = thermodynamics_tfba()
        thermodynamic_constraints_check,diagnose_variables_1,diagnose_variables_2,diagnose_variables_3 = tfba.check_conc_ln_constraints_transport(cobra_model_copy,
                                             metabolomics_data.measured_concentrations, metabolomics_data.estimated_concentrations,
                                             tcc.dG0_r, other_data.pH,other_data.temperature,tcc.metabolomics_coverage,
                                             tcc.dG_r_coverage, tcc.thermodynamic_consistency_check,
                                             measured_concentration_coverage_criteria_I, measured_dG_f_coverage_criteria_I,
                                             n_checks_I = 5,
                                             diagnose_solver_I=None,diagnose_threshold_I=0.98,diagnose_break_I=0.1);

        return thermodynamic_constraints_check,diagnose_variables_1,diagnose_variables_2,diagnose_variables_3;
def _main_():
    ##PART 1: Working
    #-------
    # Read in the model sbml file and define the model conditions
    # Aerobic specific changes
    cobra_model_oxic = load_thermoModel(anoxic = False)
    convert_to_irreversible(cobra_model_oxic)

    ##PART 2: Working
    #-------
    # make/load simulated data for aerobic conditions
    data_fva_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_fva_irrev_oxic.json'
    data_srd_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_srd_irrev_oxic.json'
    simulated_data_oxic = thermodynamics_simulatedData()
    # simulated_data_oxic.generate_sra_data(cobra_model_oxic) # perform single reaction deletion analysis
    # simulated_data_oxic.generate_fva_data(cobra_model_oxic) # perform flux variability analysis
    # simulated_data_oxic.export_sra_data(data_srd_oxic) # save results for later use
    # simulated_data_oxic.export_fva_data(data_fva_oxic) # save results for later use
    simulated_data_oxic.import_sra_data(data_srd_oxic)
    simulated_data_oxic.import_fva_data(data_fva_oxic)
    simulated_data_oxic.check_data()

    ##PART 3: Working
    #-------
    # load pH, ionic_strength, and temperature parameters
    other_data = thermodynamics_otherData()
    other_data.load_defaultData()
    other_data.check_data()

    # generate dG_f data for all model compounds
    # calculate the dG_f for each compound in each compartment
    # export/load and check the dG_f data
    #data_dG0_transformed = 'thermodynamics_data/ijo1366_dG_f.json'
    data_dG0_transformed = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_dG_f01.json'
    dG_f_data = thermodynamics_dG_f_data(id2KEGGID_filename_I='/home/user/code/thermodynamics/thermodynamics_data/id2KEGGID.csv')
    # # # dG_f_data.make_dG0_f_pH0() # only if the data has not been generated previously!
    # dG_f_data.get_transformed_dG_f('/home/user/code/thermodynamics/thermodynamics_data/compounds_dG0_f.json',cobra_model_oxic,other_data.pH,other_data.temperature,other_data.ionic_strength) # adjust the non-transformed dG0_f data to physiological pH, temperature, and ionic strength (this step has already been completed)
    #dG_f_data.export_dG_f(data_dG0_transformed) # save results for later use
    dG_f_data.import_dG_f(data_dG0_transformed)
    dG_f_data.format_dG_f()
    dG_f_data.generate_estimated_dG_f(cobra_model_oxic)
    dG_f_data.check_data()

    ##PART 4: Working
    #-------
    # load metabolomics data for oxic conditions
    data_concentrations_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_geo01.json'
    metabolomics_data_oxic = thermodynamics_metabolomicsData()
    metabolomics_data_oxic.import_metabolomics_data(data_concentrations_oxic)
    metabolomics_data_oxic.format_metabolomics_data() # add compartment identifiers to metabolite ids
    metabolomics_data_oxic.generate_estimated_metabolomics_data(cobra_model_oxic)

    ##PART 5: Working
    #-------
    # calculate dG_r and perform a consistency check based on model simulations for oxic conditions
    data_ta_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_ta.csv'
    data_dG0_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_orrev_dG0.json'
    data_dG_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_dG.json'
    data_tcc_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tcc.json'
    tcc_oxic = thermodynamics_dG_r_data()
    tcc_oxic.import_dG0_r_json(data_dG0_oxic)
    tcc_oxic.import_dG_r_json(data_dG_oxic)
    tcc_oxic.import_tcc_json(data_tcc_oxic)
    # tcc_oxic.calculate_dG0_r(cobra_model_oxic, dG_f_data.measured_dG_f, dG_f_data.estimated_dG_f, other_data.temperature) # calculate the change in free energy of reaction without accounting for metabolite concentrations
    # tcc_oxic.calculate_dG_r(cobra_model_oxic,metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #                    other_data.pH, other_data.ionic_strength, other_data.temperature) # adjust the change in free energy of reaction for intracellular metabolite concentrations
    # tcc_oxic.check_thermodynamicConsistency(cobra_model_oxic,simulated_data_oxic.fva_data,
    #                    metabolomics_data_oxic.measured_concentrations,
    #                    metabolomics_data_oxic.estimated_concentrations,
    #                    other_data.pH,other_data.ionic_strength,other_data.temperature) # check the thermodynamic consistency of the data
    # tcc_oxic.export_dG0_r_json(data_dG0_oxic) # save for later use
    # tcc_oxic.export_dG_r_json(data_dG_oxic) # save for later use
    # tcc_oxic.export_tcc_json(data_tcc_oxic) # save for later use
    # tcc_oxic.export_summary(cobra_model_oxic,simulated_data_oxic.fva_data,data_ta_oxic) # write summary of the analysis to csv file    
    
    ##PART 8:
    #-------
	# Diagnose model variables and constraints prior to FBA, FVA, and sampling (Oxic condition only)

    # identified inconsistent concentrations/dG_f/tcc values
    inconsistent_concentrations_I = []
    inconsistent_dG_f_I = []
    inconsistent_tcc_I = ['EX_glc_LPAREN_e_RPAREN__reverse']
    # remove an inconsistent dGf values
    dG_f_data.remove_measured_dG_f(inconsistent_dG_f_I)
    # remove an inconsistent concentration values
    metabolomics_data_oxic.remove_measured_concentrations(inconsistent_concentrations_I)
    # remove an inconcsistent tcc
    tcc_oxic.change_feasibleReactions(inconsistent_tcc_I)    
    # diagnose tfba constraints
    tfba = thermodynamics_tfba()
    # thermodynamic_constraints_check,\
    #     inconsistent_tcc,diagnose_variables_1,\
    #     diagnose_variables_2,\
    #     diagnose_variables_3 = tfba.check_conc_ln_constraints_transport(
    #         cobra_model_oxic,
    #         metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #         tcc_oxic.dG0_r, other_data.pH,other_data.temperature,tcc_oxic.metabolomics_coverage,
    #         tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #         0.5, 0.99,n_checks_I = 0,
    #         diagnose_solver_I=None,diagnose_threshold_I=0.98,diagnose_break_I=0.1)        
    # tfba._add_conc_ln_constraints_transport(cobra_model_oxic,
    #         metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #         tcc_oxic.dG0_r, other_data.pH,other_data.temperature,tcc_oxic.metabolomics_coverage,
    #         tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #         0.5, 0.99)
    # # cobra_model_oxic.optimize()
    # from cobra.flux_analysis import flux_variability_analysis
    # fva_data = flux_variability_analysis(cobra_model_oxic, fraction_of_optimum=0.9,
    #                                   objective_sense='maximize',
    #                                   reaction_list=[cobra_model_oxic.reactions.get_by_id('conc_lnv_fum_c')],
    #                                   )
    
    # #PART 9:
    # -------
	# perform thermodynamic FBA and FVA (Oxic condition only)

    # # run TFBA
    # cobra_model_copy = cobra_model_oxic.copy()
    # tfba.tfba(cobra_model_copy,
    #     tcc_oxic.dG0_r,other_data.temperature,
    #     tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #     use_measured_dG_r=True, solver='glpk',)

    # cobra_model_copy = cobra_model_oxic.copy()
    # tfba.tfba_conc_ln(cobra_model_copy, 
    #     metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #     tcc_oxic.dG0_r,other_data.temperature,tcc_oxic.metabolomics_coverage,
    #     tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #     measured_concentration_coverage_criteria = 0.5, measured_dG_f_coverage_criteria = 0.99,
    #     use_measured_concentrations=True,use_measured_dG0_r=True, solver='glpk',)
    
    # run TFVA
    data_tfva_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tfva.csv'
    data_tfva_analysis_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tfva_analysis.csv'
    data_tfva_dG_r_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_orrev_tfva_dG_r.json'
    data_tfva_concentrations_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tfva_concentrations.json'
    cobra_model_copy = cobra_model_oxic.copy()
    tfba.tfva(cobra_model_copy, 
        tcc_oxic.dG0_r,other_data.temperature,
        tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
        use_measured_dG0_r=True, reaction_list=None,fraction_of_optimum=1.0, solver='glpk',
        objective_sense="maximize")
    tfba.export_tfva_data(data_tfva_oxic)
    tfba.analyze_tfva_results(flux_threshold=1e-6)
    tfba.export_tfva_analysis(data_tfva_analysis_oxic)

    cobra_model_copy = cobra_model_oxic.copy()
    tfba.tfva_dG_r(cobra_model_copy, 
        tcc_oxic.dG0_r,other_data.temperature,
        tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
        use_measured_dG0_r=True, fraction_of_optimum=1.0, solver='glpk',
        objective_sense="maximize")
    tfba.export_tfva_dG_r_data(data_tfva_dG_r_oxic)

    cobra_model_copy = cobra_model_oxic.copy()
    tfba.tfva_concentrations(cobra_model_copy, 
        metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
        tcc_oxic.dG0_r,other_data.temperature,tcc_oxic.metabolomics_coverage,
        tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
        measured_concentration_coverage_criteria = 0.5, measured_dG_f_coverage_criteria = 0.99,
        use_measured_concentrations=True,use_measured_dG0_r=True,fraction_of_optimum=1.0, solver='glpk',
        objective_sense="maximize")
    tfba.export_tfva_concentrations_data(data_tfva_concentrations_oxic)
    
    # ##PART 10:
    # #-------
	# # perform thermodynamic Tsampling (Oxic condition only)
    # # NOTE: requires optGpSampler
    
    # # run Tsampling
    # data_dir = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo'
    # sampling = optGpSampler_sampling(data_dir_I = data_dir);
    # simulation_id_I = 'aerobicAnaerobic01_oxic_tsampling'
    # filename_model = simulation_id_I + '.json';
    # filename_script = simulation_id_I + '.py';
    # filename_points = simulation_id_I + '_points' + '.json';
    # filename_warmup = simulation_id_I + '_warmup' + '.json';
    # sampling.export_sampling_optGpSampler(cobra_model=cobra_model_oxic,
    #     filename_model=filename_model,
    #     filename_script=filename_script,
    #     filename_points=filename_points,
    #     filename_warmup=filename_warmup,
    #     solver_id_I = 'optGpSampler',
    #     n_points_I = 2*len(cobra_model_oxic.reactions),
    #     n_steps_I = 5000,
    #     n_threads_I = 2)
    
    ##PART 11:
    #-------
	# Analyze thermodynamic sampling (Oxic condition only)

    sampling = optGpSampler_sampling(
        data_dir_I = data_dir,
        model_I=cobra_model_oxic);
    sampling.get_points_json(filename_points);
    sampling.get_warmup_json(filename_warmup);
    sampling.calculate_mixFraction();
    # check if the model contains loops
    #loops_bool = self.sampling.check_loops();
    sampling.simulate_loops(
        data_fva='/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/loops_fva_tmp.json',
        solver_I = 'optGpSampler');
    sampling.find_loops(data_fva='/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/loops_fva_tmp.json');
    sampling.remove_loopsFromPoints();
    # calculate the flux descriptive statistics
    sampling.descriptive_statistics(points_I='flux');
    # calculate descriptive stats for metabolites
    sampling.convert_points2MetabolitePoints();
    sampling.descriptive_statistics(points_I='metabolite');
    # calculate descriptive stats for subsystems
    sampling.convert_points2SubsystemPoints();
    sampling.descriptive_statistics(points_I='subsystem');
    

    # visualize the results
    ##TODO: methods are defined, but an example is not yet given
def _main_():
    ##PART 1: Working
    #-------
    # Read in the model sbml file and define the model conditions
    # Aerobic specific changes
    cobra_model_oxic = load_thermoModel(anoxic=False)
    convert_to_irreversible(cobra_model_oxic)

    ##PART 2: Working
    #-------
    # make/load simulated data for aerobic conditions
    data_fva_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_fva_irrev_oxic.json'
    data_srd_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_srd_irrev_oxic.json'
    simulated_data_oxic = thermodynamics_simulatedData()
    # simulated_data_oxic.generate_sra_data(cobra_model_oxic) # perform single reaction deletion analysis
    # simulated_data_oxic.generate_fva_data(cobra_model_oxic) # perform flux variability analysis
    # simulated_data_oxic.export_sra_data(data_srd_oxic) # save results for later use
    # simulated_data_oxic.export_fva_data(data_fva_oxic) # save results for later use
    simulated_data_oxic.import_sra_data(data_srd_oxic)
    simulated_data_oxic.import_fva_data(data_fva_oxic)
    simulated_data_oxic.check_data()

    ##PART 3: Working
    #-------
    # load pH, ionic_strength, and temperature parameters
    other_data = thermodynamics_otherData()
    other_data.load_defaultData()
    other_data.check_data()

    # generate dG_f data for all model compounds
    # calculate the dG_f for each compound in each compartment
    # export/load and check the dG_f data
    #data_dG0_transformed = 'thermodynamics_data/ijo1366_dG_f.json'
    data_dG0_transformed = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_dG_f01.json'
    dG_f_data = thermodynamics_dG_f_data(
        id2KEGGID_filename_I=
        '/home/user/code/thermodynamics/thermodynamics_data/id2KEGGID.csv')
    # # # dG_f_data.make_dG0_f_pH0() # only if the data has not been generated previously!
    # dG_f_data.get_transformed_dG_f('/home/user/code/thermodynamics/thermodynamics_data/compounds_dG0_f.json',cobra_model_oxic,other_data.pH,other_data.temperature,other_data.ionic_strength) # adjust the non-transformed dG0_f data to physiological pH, temperature, and ionic strength (this step has already been completed)
    #dG_f_data.export_dG_f(data_dG0_transformed) # save results for later use
    dG_f_data.import_dG_f(data_dG0_transformed)
    dG_f_data.format_dG_f()
    dG_f_data.generate_estimated_dG_f(cobra_model_oxic)
    dG_f_data.check_data()

    ##PART 4: Working
    #-------
    # load metabolomics data for oxic conditions
    data_concentrations_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_geo01.json'
    metabolomics_data_oxic = thermodynamics_metabolomicsData()
    metabolomics_data_oxic.import_metabolomics_data(data_concentrations_oxic)
    metabolomics_data_oxic.format_metabolomics_data(
    )  # add compartment identifiers to metabolite ids
    metabolomics_data_oxic.generate_estimated_metabolomics_data(
        cobra_model_oxic)

    ##PART 5: Working
    #-------
    # calculate dG_r and perform a consistency check based on model simulations for oxic conditions
    data_ta_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_ta.csv'
    data_dG0_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_orrev_dG0.json'
    data_dG_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_dG.json'
    data_tcc_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tcc.json'
    tcc_oxic = thermodynamics_dG_r_data()
    tcc_oxic.import_dG0_r_json(data_dG0_oxic)
    tcc_oxic.import_dG_r_json(data_dG_oxic)
    tcc_oxic.import_tcc_json(data_tcc_oxic)
    # tcc_oxic.calculate_dG0_r(cobra_model_oxic, dG_f_data.measured_dG_f, dG_f_data.estimated_dG_f, other_data.temperature) # calculate the change in free energy of reaction without accounting for metabolite concentrations
    # tcc_oxic.calculate_dG_r(cobra_model_oxic,metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #                    other_data.pH, other_data.ionic_strength, other_data.temperature) # adjust the change in free energy of reaction for intracellular metabolite concentrations
    # tcc_oxic.check_thermodynamicConsistency(cobra_model_oxic,simulated_data_oxic.fva_data,
    #                    metabolomics_data_oxic.measured_concentrations,
    #                    metabolomics_data_oxic.estimated_concentrations,
    #                    other_data.pH,other_data.ionic_strength,other_data.temperature) # check the thermodynamic consistency of the data
    # tcc_oxic.export_dG0_r_json(data_dG0_oxic) # save for later use
    # tcc_oxic.export_dG_r_json(data_dG_oxic) # save for later use
    # tcc_oxic.export_tcc_json(data_tcc_oxic) # save for later use
    # tcc_oxic.export_summary(cobra_model_oxic,simulated_data_oxic.fva_data,data_ta_oxic) # write summary of the analysis to csv file

    ##PART 8:
    #-------
    # Diagnose model variables and constraints prior to FBA, FVA, and sampling (Oxic condition only)

    # identified inconsistent concentrations/dG_f/tcc values
    inconsistent_concentrations_I = []
    inconsistent_dG_f_I = []
    inconsistent_tcc_I = ['EX_glc_LPAREN_e_RPAREN__reverse']
    # remove an inconsistent dGf values
    dG_f_data.remove_measured_dG_f(inconsistent_dG_f_I)
    # remove an inconsistent concentration values
    metabolomics_data_oxic.remove_measured_concentrations(
        inconsistent_concentrations_I)
    # remove an inconcsistent tcc
    tcc_oxic.change_feasibleReactions(inconsistent_tcc_I)
    # diagnose tfba constraints
    tfba = thermodynamics_tfba()
    # thermodynamic_constraints_check,\
    #     inconsistent_tcc,diagnose_variables_1,\
    #     diagnose_variables_2,\
    #     diagnose_variables_3 = tfba.check_conc_ln_constraints_transport(
    #         cobra_model_oxic,
    #         metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #         tcc_oxic.dG0_r, other_data.pH,other_data.temperature,tcc_oxic.metabolomics_coverage,
    #         tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #         0.5, 0.99,n_checks_I = 0,
    #         diagnose_solver_I=None,diagnose_threshold_I=0.98,diagnose_break_I=0.1)
    # tfba._add_conc_ln_constraints_transport(cobra_model_oxic,
    #         metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #         tcc_oxic.dG0_r, other_data.pH,other_data.temperature,tcc_oxic.metabolomics_coverage,
    #         tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #         0.5, 0.99)
    # # cobra_model_oxic.optimize()
    # from cobra.flux_analysis import flux_variability_analysis
    # fva_data = flux_variability_analysis(cobra_model_oxic, fraction_of_optimum=0.9,
    #                                   objective_sense='maximize',
    #                                   reaction_list=[cobra_model_oxic.reactions.get_by_id('conc_lnv_fum_c')],
    #                                   )

    # #PART 9:
    # -------
    # perform thermodynamic FBA and FVA (Oxic condition only)

    # # run TFBA
    # cobra_model_copy = cobra_model_oxic.copy()
    # tfba.tfba(cobra_model_copy,
    #     tcc_oxic.dG0_r,other_data.temperature,
    #     tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #     use_measured_dG_r=True, solver='glpk',)

    # cobra_model_copy = cobra_model_oxic.copy()
    # tfba.tfba_conc_ln(cobra_model_copy,
    #     metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
    #     tcc_oxic.dG0_r,other_data.temperature,tcc_oxic.metabolomics_coverage,
    #     tcc_oxic.dG_r_coverage, tcc_oxic.thermodynamic_consistency_check,
    #     measured_concentration_coverage_criteria = 0.5, measured_dG_f_coverage_criteria = 0.99,
    #     use_measured_concentrations=True,use_measured_dG0_r=True, solver='glpk',)

    # run TFVA
    data_tfva_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tfva.csv'
    data_tfva_analysis_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tfva_analysis.csv'
    data_tfva_dG_r_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_orrev_tfva_dG_r.json'
    data_tfva_concentrations_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_irrev_tfva_concentrations.json'
    cobra_model_copy = cobra_model_oxic.copy()
    tfba.tfva(cobra_model_copy,
              tcc_oxic.dG0_r,
              other_data.temperature,
              tcc_oxic.dG_r_coverage,
              tcc_oxic.thermodynamic_consistency_check,
              use_measured_dG0_r=True,
              reaction_list=None,
              fraction_of_optimum=1.0,
              solver='glpk',
              objective_sense="maximize")
    tfba.export_tfva_data(data_tfva_oxic)
    tfba.analyze_tfva_results(flux_threshold=1e-6)
    tfba.export_tfva_analysis(data_tfva_analysis_oxic)

    cobra_model_copy = cobra_model_oxic.copy()
    tfba.tfva_dG_r(cobra_model_copy,
                   tcc_oxic.dG0_r,
                   other_data.temperature,
                   tcc_oxic.dG_r_coverage,
                   tcc_oxic.thermodynamic_consistency_check,
                   use_measured_dG0_r=True,
                   fraction_of_optimum=1.0,
                   solver='glpk',
                   objective_sense="maximize")
    tfba.export_tfva_dG_r_data(data_tfva_dG_r_oxic)

    cobra_model_copy = cobra_model_oxic.copy()
    tfba.tfva_concentrations(cobra_model_copy,
                             metabolomics_data_oxic.measured_concentrations,
                             metabolomics_data_oxic.estimated_concentrations,
                             tcc_oxic.dG0_r,
                             other_data.temperature,
                             tcc_oxic.metabolomics_coverage,
                             tcc_oxic.dG_r_coverage,
                             tcc_oxic.thermodynamic_consistency_check,
                             measured_concentration_coverage_criteria=0.5,
                             measured_dG_f_coverage_criteria=0.99,
                             use_measured_concentrations=True,
                             use_measured_dG0_r=True,
                             fraction_of_optimum=1.0,
                             solver='glpk',
                             objective_sense="maximize")
    tfba.export_tfva_concentrations_data(data_tfva_concentrations_oxic)

    # ##PART 10:
    # #-------
    # # perform thermodynamic Tsampling (Oxic condition only)
    # # NOTE: requires optGpSampler

    # # run Tsampling
    # data_dir = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo'
    # sampling = optGpSampler_sampling(data_dir_I = data_dir);
    # simulation_id_I = 'aerobicAnaerobic01_oxic_tsampling'
    # filename_model = simulation_id_I + '.json';
    # filename_script = simulation_id_I + '.py';
    # filename_points = simulation_id_I + '_points' + '.json';
    # filename_warmup = simulation_id_I + '_warmup' + '.json';
    # sampling.export_sampling_optGpSampler(cobra_model=cobra_model_oxic,
    #     filename_model=filename_model,
    #     filename_script=filename_script,
    #     filename_points=filename_points,
    #     filename_warmup=filename_warmup,
    #     solver_id_I = 'optGpSampler',
    #     n_points_I = 2*len(cobra_model_oxic.reactions),
    #     n_steps_I = 5000,
    #     n_threads_I = 2)

    ##PART 11:
    #-------
    # Analyze thermodynamic sampling (Oxic condition only)

    sampling = optGpSampler_sampling(data_dir_I=data_dir,
                                     model_I=cobra_model_oxic)
    sampling.get_points_json(filename_points)
    sampling.get_warmup_json(filename_warmup)
    sampling.calculate_mixFraction()
    # check if the model contains loops
    #loops_bool = self.sampling.check_loops();
    sampling.simulate_loops(
        data_fva=
        '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/loops_fva_tmp.json',
        solver_I='optGpSampler')
    sampling.find_loops(
        data_fva=
        '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/loops_fva_tmp.json'
    )
    sampling.remove_loopsFromPoints()
    # calculate the flux descriptive statistics
    sampling.descriptive_statistics(points_I='flux')
    # calculate descriptive stats for metabolites
    sampling.convert_points2MetabolitePoints()
    sampling.descriptive_statistics(points_I='metabolite')
    # calculate descriptive stats for subsystems
    sampling.convert_points2SubsystemPoints()
    sampling.descriptive_statistics(points_I='subsystem')
def _main_():
    ##PART 1: Working
    #-------
    # Read in the model sbml file and define the model conditions
    # Anaerobic specific changes:
    cobra_model_anoxic = load_thermoModel(anoxic = True)
    #convert_to_irreversible(cobra_model_anoxic)
    # Aerobic specific changes
    cobra_model_oxic = load_thermoModel(anoxic = False)
    #convert_to_irreversible(cobra_model_oxic)

    ##PART 2: Working
    #-------
    # make/load simulated data for anaerobic conditions
    data_fva_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_fva_anoxic.json'
    data_srd_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_srd_anoxic.json'
    simulated_data_anoxic = thermodynamics_simulatedData()
    # simulated_data_anoxic.generate_sra_data(cobra_model_anoxic) # perform single reaction deletion analysis
    # simulated_data_anoxic.generate_fva_data(cobra_model_anoxic) # perform flux variability analysis
    # simulated_data_anoxic.export_sra_data(data_srd_anoxic) # save results for later use
    # simulated_data_anoxic.export_fva_data(data_fva_anoxic) # save results for later use
    simulated_data_anoxic.import_sra_data(data_srd_anoxic)
    simulated_data_anoxic.import_fva_data(data_fva_anoxic)
    simulated_data_anoxic.check_data()

    # make/load simulated data for aerobic conditions
    data_fva_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_fva_oxic.json'
    data_srd_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_srd_oxic.json'
    simulated_data_oxic = thermodynamics_simulatedData()
    # simulated_data_oxic.generate_sra_data(cobra_model_oxic) # perform single reaction deletion analysis
    # simulated_data_oxic.generate_fva_data(cobra_model_oxic) # perform flux variability analysis
    # simulated_data_oxic.export_sra_data(data_srd_oxic) # save results for later use
    # simulated_data_oxic.export_fva_data(data_fva_oxic) # save results for later use
    simulated_data_oxic.import_sra_data(data_srd_oxic)
    simulated_data_oxic.import_fva_data(data_fva_oxic)
    simulated_data_oxic.check_data()

    ##PART 3: Working
    #-------
    # load pH, ionic_strength, and temperature parameters
    other_data = thermodynamics_otherData()
    other_data.load_defaultData()
    other_data.check_data()

    # generate dG_f data for all model compounds
    # calculate the dG_f for each compound in each compartment
    # export/load and check the dG_f data
    #data_dG0_transformed = 'thermodynamics_data/ijo1366_dG_f.json'
    data_dG0_transformed = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_dG_f01.json'
    dG_f_data = thermodynamics_dG_f_data(id2KEGGID_filename_I='/home/user/code/thermodynamics/thermodynamics_data/id2KEGGID.csv')
    # # # dG_f_data.make_dG0_f_pH0() # only if the data has not been generated previously!
    # dG_f_data.get_transformed_dG_f('/home/user/code/thermodynamics/thermodynamics_data/compounds_dG0_f.json',cobra_model_oxic,other_data.pH,other_data.temperature,other_data.ionic_strength) # adjust the non-transformed dG0_f data to physiological pH, temperature, and ionic strength (this step has already been completed)
    #dG_f_data.export_dG_f(data_dG0_transformed) # save results for later use
    dG_f_data.import_dG_f(data_dG0_transformed)
    dG_f_data.format_dG_f()
    dG_f_data.generate_estimated_dG_f(cobra_model_oxic)
    dG_f_data.check_data()

    ##PART 4: Working
    #-------
    # load metabolomics data for anoxic conditions
    data_concentrations_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_geo01.json'
    metabolomics_data_anoxic = thermodynamics_metabolomicsData()
    metabolomics_data_anoxic.import_metabolomics_data(data_concentrations_anoxic)
    metabolomics_data_anoxic.format_metabolomics_data() # add compartment identifiers to metabolite ids
    metabolomics_data_anoxic.generate_estimated_metabolomics_data(cobra_model_anoxic)
    # load metabolomics data for oxic conditions
    data_concentrations_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_geo01.json'
    metabolomics_data_oxic = thermodynamics_metabolomicsData()
    metabolomics_data_oxic.import_metabolomics_data(data_concentrations_oxic)
    metabolomics_data_oxic.format_metabolomics_data() # add compartment identifiers to metabolite ids
    metabolomics_data_oxic.generate_estimated_metabolomics_data(cobra_model_oxic)

    #PART 5: Working
    #-------
    # calculate dG_r and perform a consistency check based on model simulations for anoxic conditions
    data_ta_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_ta.csv'
    data_dG0_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_dG0.json'
    data_dG_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_dG.json'
    data_tcc_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_tcc.json'
    tcc_anoxic = thermodynamics_dG_r_data()
    tcc_anoxic.calculate_dG0_r(cobra_model_anoxic, dG_f_data.measured_dG_f, dG_f_data.estimated_dG_f, other_data.temperature) # calculate the change in free energy of reaction without accounting for metabolite concentrations
    tcc_anoxic.calculate_dG_r(cobra_model_anoxic,metabolomics_data_anoxic.measured_concentrations, metabolomics_data_anoxic.estimated_concentrations,
                       other_data.pH, other_data.ionic_strength, other_data.temperature) # adjust the change in free energy of reaction for intracellular metabolite concentrations
    tcc_anoxic.check_thermodynamicConsistency(cobra_model_anoxic,simulated_data_anoxic.fva_data,
                       metabolomics_data_anoxic.measured_concentrations,
                       metabolomics_data_anoxic.estimated_concentrations,
                       other_data.pH,other_data.ionic_strength,other_data.temperature) # check the thermodynamic consistency of the data
    tcc_anoxic.export_dG0_r_json(data_dG0_anoxic) # save for later use
    tcc_anoxic.export_dG_r_json(data_dG_anoxic) # save for later use
    tcc_anoxic.export_tcc_json(data_ta_anoxic) # save for later use
    tcc_anoxic.export_summary(cobra_model_anoxic,simulated_data_anoxic.fva_data,data_ta_anoxic) # write summary of the analysis to csv file

    # calculate dG_r and perform a consistency check based on model simulations for oxic conditions
    data_ta_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_ta.csv'
    data_dG0_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_dG0.json'
    data_dG_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_dG.json'
    data_tcc_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_tcc.json'
    tcc_oxic = thermodynamics_dG_r_data()
    # tcc_oxic.import_dG0_r_json(data_dG0_oxic)
    # tcc_oxic.import_dG_r_json(data_dG_oxic)
    # tcc_oxic.import_tcc_json(data_tcc_oxic)
    tcc_oxic.calculate_dG0_r(cobra_model_oxic, dG_f_data.measured_dG_f, dG_f_data.estimated_dG_f, other_data.temperature) # calculate the change in free energy of reaction without accounting for metabolite concentrations
    tcc_oxic.calculate_dG_r(cobra_model_oxic,metabolomics_data_oxic.measured_concentrations, metabolomics_data_oxic.estimated_concentrations,
                       other_data.pH, other_data.ionic_strength, other_data.temperature) # adjust the change in free energy of reaction for intracellular metabolite concentrations
    tcc_oxic.check_thermodynamicConsistency(cobra_model_oxic,simulated_data_oxic.fva_data,
                       metabolomics_data_oxic.measured_concentrations,
                       metabolomics_data_oxic.estimated_concentrations,
                       other_data.pH,other_data.ionic_strength,other_data.temperature) # check the thermodynamic consistency of the data
    tcc_oxic.export_dG0_r_json(data_dG0_oxic) # save for later use
    tcc_oxic.export_dG_r_json(data_dG_oxic) # save for later use
    tcc_oxic.export_tcc_json(data_tcc_oxic) # save for later use
    tcc_oxic.export_summary(cobra_model_oxic,simulated_data_oxic.fva_data,data_ta_oxic) # write summary of the analysis to csv file

    ##PART 6: Working
    #-------
    # inspect the thermodynamic analysis results

    # constrain the model solution and simulate optimal growth
    gr_analysis_anoxic = simulate_thermoConstraints(cobra_model_anoxic,['PGCD','ACACT1r','NDPK2'])
    gr_analysis_oxic = simulate_thermoConstraints(cobra_model_oxic,['PGCD','ACACT1r'])

    # expand the reaction set of the anoxic model to reflect the enzyme permiscuity of pykA
    add_pykA(cobra_model_anoxic)
    gr_analysis_anoxic = simulate_thermoConstraints(cobra_model_anoxic,['PGCD','ACACT1r','NDPK2'])

    ##PART 7: Working
    #-------
    # calculate the dG for biosynthetic pathways

    # calculate the dG for biosynthetic pathways for anoxic conditions
    tccp_anoxic = thermodynamics_dG_p_data()
    tccp_anoxic.calculate_dG_p(cobra_model_anoxic,tcc_anoxic.dG0_r,tcc_anoxic.dG_r)
    # calculate the dG for biosynthetic pathways for oxic conditions
    tccp_oxic = thermodynamics_dG_p_data()
    tccp_oxic.calculate_dG_p(cobra_model_oxic,tcc_oxic.dG0_r,tcc_oxic.dG_r)    

    # visualize the results
    ##TODO: methods are defined, but an example is not yet given
def _main_():
    ##PART 1: Working
    #-------
    # Read in the model sbml file and define the model conditions
    # Anaerobic specific changes:
    cobra_model_anoxic = load_thermoModel(anoxic=True)
    #convert_to_irreversible(cobra_model_anoxic)
    # Aerobic specific changes
    cobra_model_oxic = load_thermoModel(anoxic=False)
    #convert_to_irreversible(cobra_model_oxic)

    ##PART 2: Working
    #-------
    # make/load simulated data for anaerobic conditions
    data_fva_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_fva_anoxic.json'
    data_srd_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_srd_anoxic.json'
    simulated_data_anoxic = thermodynamics_simulatedData()
    # simulated_data_anoxic.generate_sra_data(cobra_model_anoxic) # perform single reaction deletion analysis
    # simulated_data_anoxic.generate_fva_data(cobra_model_anoxic) # perform flux variability analysis
    # simulated_data_anoxic.export_sra_data(data_srd_anoxic) # save results for later use
    # simulated_data_anoxic.export_fva_data(data_fva_anoxic) # save results for later use
    simulated_data_anoxic.import_sra_data(data_srd_anoxic)
    simulated_data_anoxic.import_fva_data(data_fva_anoxic)
    simulated_data_anoxic.check_data()

    # make/load simulated data for aerobic conditions
    data_fva_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_fva_oxic.json'
    data_srd_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_srd_oxic.json'
    simulated_data_oxic = thermodynamics_simulatedData()
    # simulated_data_oxic.generate_sra_data(cobra_model_oxic) # perform single reaction deletion analysis
    # simulated_data_oxic.generate_fva_data(cobra_model_oxic) # perform flux variability analysis
    # simulated_data_oxic.export_sra_data(data_srd_oxic) # save results for later use
    # simulated_data_oxic.export_fva_data(data_fva_oxic) # save results for later use
    simulated_data_oxic.import_sra_data(data_srd_oxic)
    simulated_data_oxic.import_fva_data(data_fva_oxic)
    simulated_data_oxic.check_data()

    ##PART 3: Working
    #-------
    # load pH, ionic_strength, and temperature parameters
    other_data = thermodynamics_otherData()
    other_data.load_defaultData()
    other_data.check_data()

    # generate dG_f data for all model compounds
    # calculate the dG_f for each compound in each compartment
    # export/load and check the dG_f data
    #data_dG0_transformed = 'thermodynamics_data/ijo1366_dG_f.json'
    data_dG0_transformed = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_dG_f01.json'
    dG_f_data = thermodynamics_dG_f_data(
        id2KEGGID_filename_I=
        '/home/user/code/thermodynamics/thermodynamics_data/id2KEGGID.csv')
    # # # dG_f_data.make_dG0_f_pH0() # only if the data has not been generated previously!
    # dG_f_data.get_transformed_dG_f('/home/user/code/thermodynamics/thermodynamics_data/compounds_dG0_f.json',cobra_model_oxic,other_data.pH,other_data.temperature,other_data.ionic_strength) # adjust the non-transformed dG0_f data to physiological pH, temperature, and ionic strength (this step has already been completed)
    #dG_f_data.export_dG_f(data_dG0_transformed) # save results for later use
    dG_f_data.import_dG_f(data_dG0_transformed)
    dG_f_data.format_dG_f()
    dG_f_data.generate_estimated_dG_f(cobra_model_oxic)
    dG_f_data.check_data()

    ##PART 4: Working
    #-------
    # load metabolomics data for anoxic conditions
    data_concentrations_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_geo01.json'
    metabolomics_data_anoxic = thermodynamics_metabolomicsData()
    metabolomics_data_anoxic.import_metabolomics_data(
        data_concentrations_anoxic)
    metabolomics_data_anoxic.format_metabolomics_data(
    )  # add compartment identifiers to metabolite ids
    metabolomics_data_anoxic.generate_estimated_metabolomics_data(
        cobra_model_anoxic)
    # load metabolomics data for oxic conditions
    data_concentrations_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_geo01.json'
    metabolomics_data_oxic = thermodynamics_metabolomicsData()
    metabolomics_data_oxic.import_metabolomics_data(data_concentrations_oxic)
    metabolomics_data_oxic.format_metabolomics_data(
    )  # add compartment identifiers to metabolite ids
    metabolomics_data_oxic.generate_estimated_metabolomics_data(
        cobra_model_oxic)

    #PART 5: Working
    #-------
    # calculate dG_r and perform a consistency check based on model simulations for anoxic conditions
    data_ta_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_ta.csv'
    data_dG0_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_dG0.json'
    data_dG_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_dG.json'
    data_tcc_anoxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_anoxic_tcc.json'
    tcc_anoxic = thermodynamics_dG_r_data()
    tcc_anoxic.calculate_dG0_r(
        cobra_model_anoxic, dG_f_data.measured_dG_f, dG_f_data.estimated_dG_f,
        other_data.temperature
    )  # calculate the change in free energy of reaction without accounting for metabolite concentrations
    tcc_anoxic.calculate_dG_r(
        cobra_model_anoxic, metabolomics_data_anoxic.measured_concentrations,
        metabolomics_data_anoxic.estimated_concentrations, other_data.pH,
        other_data.ionic_strength, other_data.temperature
    )  # adjust the change in free energy of reaction for intracellular metabolite concentrations
    tcc_anoxic.check_thermodynamicConsistency(
        cobra_model_anoxic, simulated_data_anoxic.fva_data,
        metabolomics_data_anoxic.measured_concentrations,
        metabolomics_data_anoxic.estimated_concentrations, other_data.pH,
        other_data.ionic_strength, other_data.temperature
    )  # check the thermodynamic consistency of the data
    tcc_anoxic.export_dG0_r_json(data_dG0_anoxic)  # save for later use
    tcc_anoxic.export_dG_r_json(data_dG_anoxic)  # save for later use
    tcc_anoxic.export_tcc_json(data_ta_anoxic)  # save for later use
    tcc_anoxic.export_summary(
        cobra_model_anoxic, simulated_data_anoxic.fva_data,
        data_ta_anoxic)  # write summary of the analysis to csv file

    # calculate dG_r and perform a consistency check based on model simulations for oxic conditions
    data_ta_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_ta.csv'
    data_dG0_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_dG0.json'
    data_dG_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_dG.json'
    data_tcc_oxic = '/home/user/code/thermodynamics/thermodynamics_data/aerobicAnaerobic01_geo/aerobicAnaerobic01_oxic_tcc.json'
    tcc_oxic = thermodynamics_dG_r_data()
    # tcc_oxic.import_dG0_r_json(data_dG0_oxic)
    # tcc_oxic.import_dG_r_json(data_dG_oxic)
    # tcc_oxic.import_tcc_json(data_tcc_oxic)
    tcc_oxic.calculate_dG0_r(
        cobra_model_oxic, dG_f_data.measured_dG_f, dG_f_data.estimated_dG_f,
        other_data.temperature
    )  # calculate the change in free energy of reaction without accounting for metabolite concentrations
    tcc_oxic.calculate_dG_r(
        cobra_model_oxic, metabolomics_data_oxic.measured_concentrations,
        metabolomics_data_oxic.estimated_concentrations, other_data.pH,
        other_data.ionic_strength, other_data.temperature
    )  # adjust the change in free energy of reaction for intracellular metabolite concentrations
    tcc_oxic.check_thermodynamicConsistency(
        cobra_model_oxic, simulated_data_oxic.fva_data,
        metabolomics_data_oxic.measured_concentrations,
        metabolomics_data_oxic.estimated_concentrations, other_data.pH,
        other_data.ionic_strength, other_data.temperature
    )  # check the thermodynamic consistency of the data
    tcc_oxic.export_dG0_r_json(data_dG0_oxic)  # save for later use
    tcc_oxic.export_dG_r_json(data_dG_oxic)  # save for later use
    tcc_oxic.export_tcc_json(data_tcc_oxic)  # save for later use
    tcc_oxic.export_summary(
        cobra_model_oxic, simulated_data_oxic.fva_data,
        data_ta_oxic)  # write summary of the analysis to csv file

    ##PART 6: Working
    #-------
    # inspect the thermodynamic analysis results

    # constrain the model solution and simulate optimal growth
    gr_analysis_anoxic = simulate_thermoConstraints(
        cobra_model_anoxic, ['PGCD', 'ACACT1r', 'NDPK2'])
    gr_analysis_oxic = simulate_thermoConstraints(cobra_model_oxic,
                                                  ['PGCD', 'ACACT1r'])

    # expand the reaction set of the anoxic model to reflect the enzyme permiscuity of pykA
    add_pykA(cobra_model_anoxic)
    gr_analysis_anoxic = simulate_thermoConstraints(
        cobra_model_anoxic, ['PGCD', 'ACACT1r', 'NDPK2'])

    ##PART 7: Working
    #-------
    # calculate the dG for biosynthetic pathways

    # calculate the dG for biosynthetic pathways for anoxic conditions
    tccp_anoxic = thermodynamics_dG_p_data()
    tccp_anoxic.calculate_dG_p(cobra_model_anoxic, tcc_anoxic.dG0_r,
                               tcc_anoxic.dG_r)
    # calculate the dG for biosynthetic pathways for oxic conditions
    tccp_oxic = thermodynamics_dG_p_data()
    tccp_oxic.calculate_dG_p(cobra_model_oxic, tcc_oxic.dG0_r, tcc_oxic.dG_r)
 def execute_adjust_dG_f(self,experiment_id_I,models_I,model_ids_I = [],time_points_I=[],sample_name_abbreviations_I=[]):
     '''adjust dG0_f to specified temperature, pH, and ionic strength'''
     
     dGf_O = [];
     # query dG0f data
     id2kegg = {};
     id2kegg = self.get_rowsDict_dataStage03QuantificationMetid2keggid();
     dG0_f = {};
     dG0_f = self.get_rowsDict_dataStage03QuantificationDG0f();
     # get the model ids:
     if model_ids_I:
         model_ids = model_ids_I;
     else:
         model_ids = [];
         model_ids = self.get_modelID_experimentID_dataStage03QuantificationSimulation(experiment_id_I);
     for model_id in model_ids:
         # get the cobra model
         cobra_model = models_I[model_id];
         # get the time-points
         if time_points_I:
             time_points = time_points_I;
         else:
             time_points = [];
             time_points = self.get_timePoints_experimentIDAndModelID_dataStage03QuantificationSimulation(experiment_id_I,model_id);
         for tp in time_points:
             # get sample_name_abbreviations
             if sample_name_abbreviations_I:
                 sample_name_abbreviations = sample_name_abbreviations_I;
             else:
                 sample_name_abbreviations = [];
                 sample_name_abbreviations = self.get_sampleNameAbbreviations_experimentIDAndModelIDAndTimePoint_dataStage03QuantificationSimulation(experiment_id_I,model_id,tp);
             for sna in sample_name_abbreviations:
                 # get otherData
                 pH,temperature,ionic_strength = {},{},{}
                 pH,temperature,ionic_strength = self.get_rowsFormatted_experimentIDAndTimePointAndSampleNameAbbreviation_dataStage03QuantificationOtherData(experiment_id_I,tp,sna);
                 # load pH, ionic_strength, and temperature parameters
                 other_data = thermodynamics_otherData(pH_I=pH,temperature_I=temperature,ionic_strength_I=ionic_strength);
                 #other_data.load_defaultData();
                 other_data.check_data();
                 # adjust dG0f to environmental conditions
                 dG_f_data = thermodynamics_dG_f_data(id2KEGGID_I=id2kegg);
                 dG_f_data.get_transformed_dG_f(dG0_f,cobra_model,other_data.pH,other_data.temperature,other_data.ionic_strength); # adjust the non-transformed dG0_f data to physiological pH, temperature, and ionic strength (this step has already been completed)
                 # upload to database for later use
                 for k,v in dG_f_data.dG_f.items():
                     compartment = k.split('_')[-1]; #compartment id is appended to the met_id
                     data_tmp={'experiment_id':experiment_id_I,
                         'model_id':model_id,
                         'sample_name_abbreviation':sna,
                         'time_point':tp,
                         'met_name':None,
                         'met_id':k,
                         'dG_f':v['dG_f'],
                         'dG_f_var':v['dG_f_var'],
                         'dG_f_units':v['dG_f_units'],
                         'dG_f_lb':None,
                         'dG_f_ub':None,
                         'temperature':temperature[compartment]['temperature'],
                         'temperature_units':temperature[compartment]['temperature_units'],
                         'ionic_strength':ionic_strength[compartment]['ionic_strength'],
                         'ionic_strength_units':ionic_strength[compartment]['ionic_strength_units'],
                         'pH':pH[compartment]['pH'],
                         'pH_units':None,
                         'measured':True,
                         'used_':True,
                         'comment_':None};
                     dGf_O.append(data_tmp);
                     #row = None;
                     #row = data_stage03_quantification_dG_f(experiment_id_I,
                     #    model_id,
                     #    sna,
                     #    tp,
                     #    None,
                     #    k,
                     #    v['dG_f'],
                     #    v['dG_f_var'],
                     #    v['dG_f_units'],
                     #    None,
                     #    None,
                     #    temperature[compartment]['temperature'],
                     #    temperature[compartment]['temperature_units'],
                     #    ionic_strength[compartment]['ionic_strength'],
                     #    ionic_strength[compartment]['ionic_strength_units'],
                     #    pH[compartment]['pH'],
                     #    None,
                     #    True,
                     #    True,
                     #    None);
                     #self.session.add(row);
     #add data to to the DB
     self.add_dataStage03QuantificationDGf(dGf_O);
Ejemplo n.º 11
0
 def init_otherData(self):
     other_data = thermodynamics_otherData()
     other_data.load_defaultData()
     other_data.check_data()
     self.other_data = other_data