def get_thermo_data(): # Load Thermo data thermo_data = load_thermoDB(pjoin(file_dir,'../../../pytfa/data/thermo_data.thermodb')) lexicon = read_lexicon('thermo_data/iJO1366_lexicon.csv') # lexicon = curate_lexicon(read_lexicon('thermo_data/iJO1366_lexicon.csv')) compartment_data = read_compartment_data('thermo_data/iJO1366_compartment_data.json') def curate_lexicon(lexicon): ix = pd.Series(lexicon.index) ix = ix.apply(lambda s: str.replace(s,'-','__')) ix = ix.apply(lambda s: '_'+s if s[0].isdigit() else s) lexicon.index = ix return lexicon lexicon = curate_lexicon(lexicon) return thermo_data, lexicon, compartment_data
def load_data(model_name): """ Loads pre-curated model-specific thermodynamic information. Parameters ---------- model_name : str The name of a model. Returns ------- thermo_data : dict A thermodynamic database. lexicon : pandas.DataFrame A dataframe linking metabolite IDs to SEED compound IDs. compartment_data : dict A dictionary with information about each compartment of the model. """ thermo_data = load_thermoDB(static_path("thermo_data.thermodb")) lexicon = read_lexicon(static_path(model_name, "lexicon.csv")) compartment_data = read_compartment_data( static_path(model_name, "compartment_data.json")) return thermo_data, lexicon, compartment_data
solver = GUROBI case = 'full' # 'reduced' or full' # Load reaction DB print("Loading thermo data...") thermo_data = load_thermoDB(thermo_database) print("Done !") #biomass_rxn = 'BIOMASS_Ec_iJO1366_WT_53p95M' biomass_rxn = 'Ec_biomass_iJO1366_WT_53p95M' # We import pre-compiled data as it is faster for bigger models model_path = '/projectnb2/bioinfor/SEGRE/goldford/CoenzymeSpecificity/pytfa/models' cobra_model = load_json_model(model_path + '/iJO1366_NAD_ratio_1.fromTFA.json') lexicon = read_lexicon(model_path + '/iJO1366/lexicon.csv') compartment_data = read_compartment_data(model_path + '/iJO1366/compartment_data.json') # Initialize the cobra_model mytfa = pytfa.ThermoModel(thermo_data, cobra_model) # Annotate the cobra_model annotate_from_lexicon(mytfa, lexicon) apply_compartment_data(mytfa, compartment_data) mytfa.name = 'iJO1366[NAD]' mytfa.solver = solver mytfa.objective = biomass_rxn # Solver settings
return model m = cobra_model.copy() rxn_ids = [x.id for x in cobra_model.reactions] m = cobra_model.copy() rxn_ids = [x.id for x in cobra_model.reactions] for rxnid in rxn_ids: m = single_coenzyme_transform(m, rxnid) m.remove_reactions([x for x in m.reactions if x.id == 'NADTRHD[condensed]'][0]) m.objective = 'Ec_biomass_iJO1366_WT_53p95M[condensed]' # Load reaction DB thermo_data = load_thermoDB(root_dir + 'data/thermo_data.thermodb') lexicon = read_lexicon(root_dir + 'models/small_ecoli/lexicon.csv') compartment_data = read_compartment_data( root_dir + 'models/small_ecoli/compartment_data.json') def convert2thermo(model, name): # Initialize the model tmodel = pytfa.ThermoModel(thermo_data, model) tmodel.name = name # Annotate the model annotate_from_lexicon(tmodel, lexicon) apply_compartment_data(tmodel, compartment_data) ## TFA conversion tmodel.prepare()
# Load reaction DB print("Loading thermo data...") thermo_data = load_thermoDB('../data/thermo_data.thermodb') print("Done !") if case == 'reduced': cobra_model = import_matlab_model('../models/small_ecoli.mat') mytfa = pytfa.ThermoModel(thermo_data, cobra_model) biomass_rxn = 'Ec_biomass_iJO1366_WT_53p95M' elif case == 'full': # We import pre-compiled data as it is faster for bigger models cobra_model = load_json_model('../models/iJO1366.json') lexicon = read_lexicon('../models/iJO1366/lexicon.csv') compartment_data = read_compartment_data('../models/iJO1366/compartment_data.json') # Initialize the cobra_model mytfa = pytfa.ThermoModel(thermo_data, cobra_model) # Annotate the cobra_model annotate_from_lexicon(mytfa, lexicon) apply_compartment_data(mytfa, compartment_data) biomass_rxn = 'Ec_biomass_iJO1366_WT_53p95M' mytfa.name = 'tutorial_basics' mytfa.solver = solver mytfa.objective = biomass_rxn
def apply_annotation_data(self): # for met in self.model.metabolites: # if 'seed.compound' in met.annotation: # met.annotation = {'seed_id': met.annotation['seed.compound'][0]} annotate_from_lexicon( self.model, read_lexicon(join(data_dir, 'thermo/lexicon.csv')))
def __init__(self, model_code='ecoli:iJO1366', solver='gurobi', min_biomass=0.55): start_time = time.time() super().__init__(model_code, solver, min_biomass) if self.species == 'ecoli': # Add cystein -> selenocystein transformation for convenience selcys = Metabolite(id='selcys__L_c', compartment='c', formula='C3H7NO2Se') selcys_rxn = Reaction(id='PSEUDO_selenocystein_synthase', name='PSEUDO Selenocystein_Synthase') selcys_rxn.add_metabolites({ self.model.metabolites.cys__L_c: -1, selcys: +1 }) self.model.add_reactions([selcys_rxn]) self._sanitize_varnames() # self.model.reactions.EX_glc__D_e.lower_bound = -1 * glc_uptake - glc_uptake_std # self.model.reactions.EX_glc__D_e.upper_bound = -1 * glc_uptake + glc_uptake_std # time_str = get_timestr() coupling_dict = get_coupling_dict(self.model, mode='kmax', atps_name='ATPS4rpp', infer_missing_enz=True) aa_dict, rna_nucleotides, rna_nucleotides_mp, dna_nucleotides = get_monomers_dict( ) essentials = get_essentials() # if has_thermo: thermo_db = load_thermoDB( join(data_dir, 'thermo/thermo_data.thermodb')) self.model = ThermoMEModel(thermo_db, model=self.model, growth_reaction=self.biomass_reaction, mu_range=mu_range, n_mu_bins=n_mu_bins) self.model.name = self.model_name # annotate_from_lexicon(self.model, read_lexicon(dir_path + '/data/thermo/lexicon.csv')) # compartment_data = read_compartment_data(dir_path + '/data/thermo/compartment_data.json') # apply_compartment_data(self.model, compartment_data) apply_compartment_data( self.model, read_compartment_data( join(data_dir, 'thermo/compartment_data.json'))) annotate_from_lexicon( self.model, read_lexicon(join(data_dir, 'thermo/lexicon.csv'))) self.model.prepare() # self.model.reactions.MECDPS.thermo['computed'] = False # self.model.reactions.NDPK4.thermo['computed'] = False # self.model.reactions.TMDPP.thermo['computed'] = False # self.model.reactions.ARGAGMt7pp.thermo['computed'] = False self.model.convert() # else: # self.model = MEModel(model=self.model, growth_reaction=growth_reaction_id, mu_range=mu_range, # n_mu_bins=n_mu_bins, name=name) # mrna_dict = get_mrna_dict(self.model) # nt_sequences = get_nt_sequences() nt_sequences = pd.read_csv(join( data_dir, f'{self.species}/{self.model_name}_nt_seq_kegg.csv'), index_col=0, header=None).iloc[:, 0] mrna_dict = self.get_mrna_dict(nt_sequences) rnap = get_rnap() rib = get_rib() # Remove nucleotides and amino acids from biomass reaction as they will be # taken into account by the expression remove_from_biomass_equation(model=self.model, nt_dict=rna_nucleotides, aa_dict=aa_dict, essentials_dict=essentials) self.model.add_nucleotide_sequences(nt_sequences) self.model.add_essentials(essentials=essentials, aa_dict=aa_dict, rna_nucleotides=rna_nucleotides, rna_nucleotides_mp=rna_nucleotides_mp) self.model.add_mrnas(mrna_dict.values()) self.model.add_ribosome(rib, free_ratio=0.2) # http://bionumbers.hms.harvard.edu/bionumber.aspx?id=102348&ver=1&trm=rna%20polymerase%20half%20life&org= # Name Fraction of active RNA Polymerase # Bionumber ID 102348 # Value 0.17-0.3 unitless # Source Bremer, H., Dennis, P. P. (1996) Modulation of chemical composition and other parameters of the cell by growth rate. # Neidhardt, et al. eds. Escherichia coli and Salmonella typhimurium: Cellular # and Molecular Biology, 2nd ed. chapter 97 Table 1 self.model.add_rnap(rnap, free_ratio=0.75) self.model.build_expression() self.model.add_enzymatic_coupling(coupling_dict) # if has_neidhardt: # nt_ratios, aa_ratios = get_ratios() # chromosome_len, gc_ratio = get_ecoli_gen_stats() # kdeg_mrna, mrna_length_avg = get_mrna_metrics() # kdeg_enz, peptide_length_avg = get_enz_metrics() # neidhardt_mu, neidhardt_rrel, neidhardt_prel, neidhardt_drel = get_neidhardt_data() # # add_interpolation_variables(self.model) # self.model.add_dummies(nt_ratios=nt_ratios, mrna_kdeg=kdeg_mrna, mrna_length=mrna_length_avg, # aa_ratios=aa_ratios, enzyme_kdeg=kdeg_enz, peptide_length=peptide_length_avg) # add_protein_mass_requirement(self.model, neidhardt_mu, neidhardt_prel) # add_rna_mass_requirement(self.model, neidhardt_mu, neidhardt_rrel) # add_dna_mass_requirement(self.model, mu_values=neidhardt_mu, dna_rel=neidhardt_drel, gc_ratio=gc_ratio, # chromosome_len=chromosome_len, dna_dict=dna_nucleotides) # Need to put after, because dummy has to be taken into account if used. self.model.populate_expression() self.model.add_trna_mass_balances() # self.model.growth_reaction.lower_bound = objective_lb self.model.repair() print( f"Building ETFL model costs {time.time() - start_time:.2f} seconds!" ) try: start_time = time.time() self.model.optimize() except (AttributeError, SolverError): print( f"Solving no relaxed model costs {time.time() - start_time:.2f} seconds!" ) start_time = time.time() self.model, _, _ = relax_dgo(self.model, in_place=True) print( f"Relaxing model costs {time.time() - start_time:.2f} seconds!" ) # self.model.growth_reaction.lower_bound = 0 # print(f"Build ETFL model for {time.time() - start_time:.2f} seconds!") self.model.print_info()
path_to_params = join(this_directory, '..', 'tests/redgem_params.yml') thermoDB = join(this_directory, '..', 'data/thermo_data.thermodb') path_to_lexicon = join(this_directory, '..', 'models/iJO1366/lexicon.csv') path_to_compartment_data = join(this_directory, '..', 'models/iJO1366/compartment_data.json') # Scaling to avoid numerical errors with bad lumps for rxn in model.reactions: if rxn.id.startswith('LMPD_'): rxn.add_metabolites( {x: v * (0.1 - 1) for x, v in rxn.metabolites.items()}) thermo_data = load_thermoDB(thermoDB) lexicon = read_lexicon(path_to_lexicon) compartment_data = read_compartment_data(path_to_compartment_data) tfa_model = ThermoModel(thermo_data, model) annotate_from_lexicon(tfa_model, lexicon) apply_compartment_data(tfa_model, compartment_data) tfa_model.name = 'Lumped Model' tfa_model.prepare() tfa_model.convert() # tfa_model.solver.configuration.verbosity = True tfa_model.logger.setLevel = 30 def test_redgem():
import re data_dir = '../organism_data/info_ecoli' vanilla_model = cobra.io.load_json_model('iJO1366_with_xrefs.json') solver = 'optlang-gurobi' # solver = 'optlang-cplex' # Relax ATPM # vanilla_model.reactions.ATPM.lower_bound = 0 # Load Thermo data thermo_data = load_thermoDB('../../pytfa/data/thermo_data.thermodb') lexicon = read_lexicon('thermo_data/iJO1366_lexicon.csv') # lexicon = curate_lexicon(read_lexicon('thermo_data/iJO1366_lexicon.csv')) compartment_data = read_compartment_data('thermo_data/iJO1366_compartment_data.json') # McCloskey2014 values glc_uptake = 7.54 glc_uptake_std = 0.56 observed_growth = 0.61 - 0.02 vanilla_model.reactions.EX_glc__D_e.lower_bound = -1*glc_uptake - glc_uptake_std vanilla_model.reactions.EX_glc__D_e.upper_bound = -1*glc_uptake + glc_uptake_std growth_reaction_id = 'BIOMASS_Ec_iJO1366_WT_53p95M' #------------------------------------------------------------
from pytfa.optim.variables import DeltaG, DeltaGstd, ThermoDisplacement from pytfa.analysis import variability_analysis, \ apply_reaction_variability, \ apply_generic_variability, \ apply_directionality CPLEX = 'optlang-cplex' GUROBI = 'optlang-gurobi' GLPK = 'optlang-glpk' # Load the cobra_model cobra_model = load_matlab_model('../models/small_ecoli.mat') # Load reaction DB thermo_data = load_thermoDB('../data/thermo_data.thermodb') lexicon = read_lexicon('../models/small_ecoli/lexicon.csv') compartment_data = read_compartment_data( '../models/small_ecoli/compartment_data.json') # Initialize the cobra_model tmodel = pytfa.ThermoModel(thermo_data, cobra_model) tmodel.name = 'tutorial' # Annotate the cobra_model annotate_from_lexicon(tmodel, lexicon) apply_compartment_data(tmodel, compartment_data) # Set the solver tmodel.solver = GLPK ## TFA conversion