def save_reduced_json_me_model(me0, file_name): """ Save a stripped-down JSON version of the ME-model. This will exclude all of ME-Model information except the reaction stoichiometry information and the reaction bounds. Saving/loading a model in this format will thus occur much quicker, but limit the ability to edit the model and use most of its features. Parameters ---------- me0 : :class:`cobrame.core.model.MEModel` A full ME-model file_name : str or file-like object Filename of the JSON output """ me = copy.deepcopy(me0) for rxn in me.reactions: for met in rxn.metabolites: s = rxn._metabolites[met] if isinstance(s, Basic): rxn._metabolites[met] = str(s) if isinstance(rxn.lower_bound, Basic): rxn.lower_bound = str(rxn.lower_bound) if isinstance(rxn.upper_bound, Basic): rxn.upper_bound = str(rxn.upper_bound) for met in me.metabolites: if isinstance(met._bound, Basic): met._bound = str(met._bound) save_json_model(me, file_name)
def export_model(self,model_id_I,filename_I,filetype_I = 'sbml',ko_list=[],flux_dict={}): '''export a cobra model INPUT: model_id_I = model id filename_I = filename filetype_I = if specified, the model will be written to the desired filetype default: model file in the database ''' # get the model cobra_model_sbml = {}; cobra_model_sbml = self.get_row_modelID_dataStage02IsotopomerModels(model_id_I); if not cobra_model_sbml: print('model not found'); return; # load the model cobra_model = self.writeAndLoad_modelTable(cobra_model_sbml); # Constrain the model self.constrain_modelModelVariables(cobra_model=cobra_model,ko_list=ko_list,flux_dict=flux_dict); # export the model if filetype_I == 'sbml': cobra_model = self.writeAndLoad_modelTable(cobra_model_sbml); #export the model to sbml write_cobra_model_to_sbml_file(cobra_model,filename_I); elif filetype_I == 'json': cobra_model = self.writeAndLoad_modelTable(cobra_model_sbml); #export the model to json save_json_model(cobra_model,filename_I); else: print('file_type not supported')
def save_json_me(me0, file_name, pretty=False): """ Save ME model as json model : :class:`~cobrame.core.MEModel.MEmodel` object file_name : str or file-like object """ me = copy.deepcopy(me0) for rxn in me.reactions: for met in rxn.metabolites: s = rxn._metabolites[met] if isinstance(s, Basic): rxn._metabolites[met] = str(s) if isinstance(rxn.lower_bound, Basic): rxn.lower_bound = str(rxn.lower_bound) if isinstance(rxn.upper_bound, Basic): rxn.upper_bound = str(rxn.upper_bound) for met in me.metabolites: if isinstance(met._bound, Basic): met._bound = str(met._bound) save_json_model(me, file_name)
def make_modelFromRxnsAndMetsTables(self,model_id_I=None,model_id_O=None,date_O=None,ko_list=[],flux_dict={},description=None): '''make/update the model using the modelReactions and modelMetabolites table''' if model_id_I and model_id_O: #make a new model based off of a modification of an existing model in the database # get the model reactions and metabolites from the database reactions = []; metabolites = []; reactions = self.get_rows_modelID_dataStage02IsotopomerModelReactions(model_id_I); metabolites = self.get_rows_modelID_dataStage02IsotopomerModelMetabolites(model_id_I); # creat the model cobra_model = self.create_modelFromReactionsAndMetabolitesTables(reactions,metabolites) # Constrain the model self.constrain_modelModelVariables(cobra_model=cobra_model,ko_list=ko_list,flux_dict=flux_dict); # Change description, if any: if description: cobra_model.description = description; # test the model if self.test_model(cobra_model): # write the model to a temporary file save_json_model(cobra_model,'cobra_model_tmp.json') # add the model information to the database dataStage02IsotopomerModelRxns_data = []; dataStage02IsotopomerModelMets_data = []; dataStage02IsotopomerModels_data,\ dataStage02IsotopomerModelRxns_data,\ dataStage02IsotopomerModelMets_data = self._parse_model_json(model_id_O, date_O, 'cobra_model_tmp.json') self.add_data_stage02_isotopomer_models(dataStage02IsotopomerModels_data); elif model_id_I and not model_id_O: #update an existing model in the database # get the model reactions and metabolites from the database reactions = []; metabolites = []; reactions = self.get_rows_modelID_dataStage02IsotopomerModelReactions(model_id_I); metabolites = self.get_rows_modelID_dataStage02IsotopomerModelMetabolites(model_id_I); # creat the model cobra_model = self.create_modelFromReactionsAndMetabolitesTables(reactions,metabolites) # Constrain the model self.constrain_modelModelVariables(cobra_model=cobra_model,ko_list=ko_list,flux_dict=flux_dict); # Change description, if any: if description: cobra_model.description = description; # test the model if self.test_model(cobra_model): # write the model to a temporary file save_json_model(cobra_model,'cobra_model_tmp.json') # upload the model to the database # add the model information to the database dataStage02IsotopomerModelRxns_data = []; dataStage02IsotopomerModelMets_data = []; dataStage02IsotopomerModels_data,\ dataStage02IsotopomerModelRxns_data,\ dataStage02IsotopomerModelMets_data = self._parse_model_json(model_id_I, date_O, 'cobra_model_tmp.json') self.update_data_stage02_isotopomer_models(dataStage02IsotopomerModels_data); else: print('need to specify either an existing model_id!') return
for other_id, mnx_id in reac_xref[['XREF', 'MNX_ID']].values: cleaned_key = _apply_sanitize_rules( _apply_sanitize_rules(other_id.split(':')[1], REVERSE_ID_SANITIZE_RULES_SIMPHENY), ID_SANITIZE_RULES_TAB_COMPLETION) all2mnx[cleaned_key] = mnx_id metanetx['all2mnx'] = all2mnx # with open('../cameo/data/metanetx.pickle', 'wb') as f: # pickle.dump(metanetx, f) # generate universal reaction models db_combinations = [('bigg',), ('rhea',), ('bigg', 'rhea'), ('bigg', 'rhea', 'kegg'), ('bigg', 'rhea', 'kegg', 'brenda')] for db_combination in db_combinations: universal_model = construct_universal_model(db_combination) # The following is a hack; uncomment the following from cobra.io.json import _REQUIRED_REACTION_ATTRIBUTES _REQUIRED_REACTION_ATTRIBUTES.add('annotation') # d_model = _to_dict(universal_model) with open('../cameo/models/universal_models/{model_name}.json'.format(model_name=universal_model.id), 'w') as f: save_json_model(universal_model, f) # json.dump(d_model, f) # save_json_model(universal_model, '../cameo/models/universal_models/{model_name}.json'.format(model_name=universal_model.id)) chem_prop_filtered = chem_prop[ [any([source.startswith(db) for db in ('bigg', 'rhea', 'kegg', 'brenda', 'chebi')]) for source in chem_prop.source]] chem_prop_filtered = chem_prop_filtered.dropna(subset=['name']) # with gzip.open('../cameo/data/metanetx_chem_prop.pklz', 'wb') as f: # pickle.dump(chem_prop_filtered, f)
def make_modelAndMappingFromRxnsAndMetsTables(self,model_id_I=None,model_id_O=None,mapping_id_I=None,mapping_id_O=None,date_O=None,ko_list=[],flux_dict={},description=None): '''make/update the model AND model mappings using the modelReactions and modelMetabolites table''' if model_id_I and model_id_O and mapping_id_I and mapping_id_O: #make a new model based off of a modification of an existing model in the database # get the model reactions and metabolites from the database reactions = []; metabolites = []; reactions = self.get_rows_modelID_dataStage02IsotopomerModelReactions(model_id_I); metabolites = self.get_rows_modelID_dataStage02IsotopomerModelMetabolites(model_id_I); reactions_mappings = []; metabolites_mappings = []; reactions_mappings = self.get_rows_mappingID_dataStage02IsotopomerAtomMappingReactions(mapping_id_I); metabolites_mappings = self.get_rows_mappingID_dataStage02IsotopomerAtomMappingMetabolites(mapping_id_I); # rename the reactions and metabolite model_ids for rxn_cnt,rxn in enumerate(reactions): reactions[rxn_cnt]['model_id'] = model_id_O; for met_cnt,met in enumerate(metabolites): metabolites[met_cnt]['model_id'] = model_id_O; # rename the reactions and metabolite mapping_ids for rxn_cnt,rxn in enumerate(reactions_mappings): reactions_mappings[rxn_cnt]['mapping_id'] = mapping_id_O; for met_cnt,met in enumerate(metabolites_mappings): metabolites_mappings[met_cnt]['mapping_id'] = mapping_id_O; # create the model cobra_model = self.create_modelFromReactionsAndMetabolitesTables(reactions,metabolites) # Apply KOs, if any: for ko in ko_list: cobra_model.reactions.get_by_id(ko).lower_bound = 0.0; cobra_model.reactions.get_by_id(ko).upper_bound = 0.0; # Apply flux constraints, if any: for rxn,flux in flux_dict.items(): cobra_model.reactions.get_by_id(rxn).lower_bound = flux['lb']; cobra_model.reactions.get_by_id(rxn).upper_bound = flux['ub']; # Change description, if any: if description: cobra_model.description = description; # test the model if self.test_model(cobra_model): # write the model to a temporary file save_json_model(cobra_model,'cobra_model_tmp.json') # add the model information to the database dataStage02IsotopomerModelRxns_data = []; dataStage02IsotopomerModelMets_data = []; dataStage02IsotopomerModels_data,\ dataStage02IsotopomerModelRxns_data,\ dataStage02IsotopomerModelMets_data = self._parse_model_json(model_id_O, date_O, 'cobra_model_tmp.json') self.add_data_stage02_isotopomer_models(dataStage02IsotopomerModels_data); # add the metabolite and reaction information to the database self.add_data_stage02_isotopomer_modelReactions(reactions); self.add_data_stage02_isotopomer_modelMetabolites(metabolites); # add the metabolites and reactions mappings to the database self.add_data_dataStage02IsotopomerAtomMappingReactions(reactions_mappings); self.add_data_dataStage02IsotopomerAtomMappingMetabolites(metabolites_mappings); elif model_id_I and not model_id_O and mapping_id_I and not mapping_id_O: #update an existing model in the database # get the model reactions and metabolites from the database reactions = []; metabolites = []; reactions = self.get_rows_modelID_dataStage02IsotopomerModelReactions(model_id_I); metabolites = self.get_rows_modelID_dataStage02IsotopomerModelMetabolites(model_id_I); # creat the model cobra_model = self.create_modelFromReactionsAndMetabolitesTables(reactions,metabolites) # Apply KOs, if any: for ko in ko_list: cobra_model.reactions.get_by_id(ko).lower_bound = 0.0; cobra_model.reactions.get_by_id(ko).upper_bound = 0.0; # Apply flux constraints, if any: for rxn,flux in flux_dict.items(): cobra_model.reactions.get_by_id(rxn).lower_bound = flux['lb']; cobra_model.reactions.get_by_id(rxn).upper_bound = flux['ub']; # Change description, if any: if description: cobra_model.description = description; # test the model if self.test_model(cobra_model): # write the model to a temporary file save_json_model(cobra_model,'cobra_model_tmp.json') # upload the model to the database # add the model information to the database dataStage02IsotopomerModelRxns_data = []; dataStage02IsotopomerModelMets_data = []; dataStage02IsotopomerModels_data,\ dataStage02IsotopomerModelRxns_data,\ dataStage02IsotopomerModelMets_data = self._parse_model_json(model_id_I, date_I, 'cobra_model_tmp.json') self.update_data_stage02_isotopomer_models(dataStage02IsotopomerModels_data); else: print('need to specify an existing model_id/mapping_id!') return
db_combinations = [('bigg', ), ('rhea', ), ('bigg', 'rhea'), ('bigg', 'rhea', 'kegg'), ('bigg', 'rhea', 'kegg', 'brenda')] largest_universal_model = None for db_combination in db_combinations: universal_model = construct_universal_model(db_combination, reac_xref, reac_prop, chem_prop) if largest_universal_model is None: largest_universal_model = universal_model else: if len(set(largest_universal_model.metabolites)) < len( set(universal_model.metabolites)): largest_universal_model = universal_model # The following is a hack; uncomment the following from cobra.io.json import _REQUIRED_REACTION_ATTRIBUTES _REQUIRED_REACTION_ATTRIBUTES.add('annotation') with open( '../cameo/models/universal_models/{model_name}.json'.format( model_name=universal_model.id), 'w') as f: save_json_model(universal_model, f) metabolite_ids_in_largest_model = [ m.id for m in largest_universal_model.metabolites ] chem_prop_filtered = chem_prop.dropna(subset=['name']) chem_prop_filtered = chem_prop_filtered[chem_prop_filtered.index.isin( metabolite_ids_in_largest_model)] with gzip.open('../cameo/data/metanetx_chem_prop.json.gz', 'wt') as f: chem_prop_filtered.to_json(f, force_ascii=True)
def make_modelFromRxnsAndMetsTables(self, model_id_I=None,model_id_O=None,date_O=None,description=None, ko_list=[],flux_dict={},rxn_include_list=[], convert2irreversible_I=False,revert2reversible_I=False, convertPathway2individualRxns_I=False, model_id_template_I='iJO1366',pathway_model_id_I=None ): '''make/update the model using the modelReactions and modelMetabolites table INPUT: model_id_I = existing model_id (if specified, an existing model in the database will be retrieved and modified) model_file_name_I = new model from file (if specified, a new model form file will be retrieved and modified) model_id_O = new model_id date_O = date the model was made description = description for the model INPUT (constraints in order): ko_list = list of reactions to constrain to zero flux flux_dict = dictionary of fluxes to constrain the model INPUT (model manipulation in order): rxn_include_list = list of reactions to include in the new model convertPathway2individualRxns_I = boolean, if True, lumped rxns will be converted to individual rxns template_model_id_I = model_id to use as a template when adding in new reactions pathway_model_id_I = model_id for the pathway convert2irreversible_I = boolean, if True, the model will be converted to irreversible from reversible revert2reversible_I = boolean, if True, the model will be revert to reversible from irreversible ''' # get the model reactions and metabolites from the database reactions = []; metabolites = []; reactions = self.get_rows_modelID_dataStage02PhysiologyModelReactions(model_id_I); metabolites = self.get_rows_modelID_dataStage02PhysiologyModelMetabolites(model_id_I); # break apart lumped reactions if convertPathway2individualRxns_I: metabolites_O = []; reactions,metabolites_O = self.convert_lumpedRxns2IndividualRxns( model_id_template_I=model_id_template_I, pathway_model_id_I=pathway_model_id_I, reactions_I=reactions); # add in any new metabolites that may have been found met_ids = [x['met_id'] for x in metabolites]; for met in metabolites_O: if not met['met_id'] in met_ids: metabolites.append(met); # create the model cobra_model = self.create_modelFromReactionsAndMetabolitesTables(reactions,metabolites) # Constrain the model self.constrain_modelModelVariables(cobra_model, ko_list=ko_list,flux_dict=flux_dict); # Use only a subset of reactions, if specified if rxn_include_list: remove_reactions=[]; for rxn in cobra_model.reactions: if not rxn.id in rxn_include_list: remove_reactions.append(rxn); cobra_model.remove_reactions(remove_reactions,delete=True,remove_orphans=True); if convert2irreversible_I: convert_to_irreversible(cobra_model); if revert2reversible_I: self.repair_irreversibleModel(cobra_model); self.revert2reversible(cobra_model); # Change description, if any: if description: cobra_model.description = description; # test the model if self.test_model(cobra_model): # write the model to a temporary file save_json_model(cobra_model,'cobra_model_tmp.json') # add the model information to the database dataStage02PhysiologyModelRxns_data = []; dataStage02PhysiologyModelMets_data = []; dataStage02PhysiologyModels_data,\ dataStage02PhysiologyModelRxns_data,\ dataStage02PhysiologyModelMets_data = self._parse_model_json(model_id_O, date_O, 'cobra_model_tmp.json') if model_id_I and model_id_O: #make a new model based off of a modification of an existing model in the database self.add_dataStage02PhysiologyModelMetabolites(dataStage02PhysiologyModelMets_data); self.add_dataStage02PhysiologyModelReactions(dataStage02PhysiologyModelRxns_data); self.add_dataStage02PhysiologyModels(dataStage02PhysiologyModels_data); elif model_id_I and not model_id_O: #update an existing model in the database self.update_data_stage02_isotopomer_models(dataStage02PhysiologyModels_data); else: print('need to specify either an existing model_id!') return