def save_model_to_file(model, filename): """ Save a model to a file based on the extension of the file name. Parameters ---------- model : cobra.core.Model Model object loaded from file filename : str Path to model file Raises ------ IOError If model file extension is not supported. """ (root, ext) = splitext(filename) if ext == '.mat': save_matlab_model(model, filename) elif ext == '.xml' or ext == '.sbml': write_sbml_model(model, filename) elif ext == '.json': save_json_model(model, filename) else: raise IOError( 'Model file extension not supported for {0}'.format(filename)) return
def build_universal_model(path, use_cache=True, cache_location='.metacyc_universal.json'): """ Constructs a universal model from all the reactions in the metacyc database :param path: path to folder containing metacyc dat files :param use_cache: optionally store the resulting model in cached form :return: """ try: if use_cache and os.path.exists(cache_location): model = load_model(cache_location) return model except (IOError, TypeError): pass # This means cached model couldn't be loaded, we'll create a new one db = parse_db(path) model = Model() for reaction_id in db['reactions']: try: add_reaction(model, reaction_id, db) except KeyError: # Exception handling ignores badly formatted reactions pass if use_cache: save_json_model(model, cache_location) return model
def _summarize_models(args): tid, row, new_path = args files = row["file"].split("|") if len(files) > 1: mod = join_models(files, id=tid) else: mod = read_sbml_model(files[0]) save_json_model(mod, new_path)
def test_save_json_model(tmpdir, mini_model): """Test the writing of JSON model.""" jsonschema = pytest.importorskip("jsonschema") output_file = tmpdir.join("mini.json") cio.save_json_model(mini_model, output_file.strpath, pretty=True) # validate against JSONSchema with open(output_file, "r") as infile: loaded = json.load(infile) assert jsonschema.validate(loaded, cio.json.json_schema)
def _summarize_models(args): tid, row, new_path, folder = args files = [path.join(folder, r) for r in row["file"].split("|")] if len(files) > 1: mod = join_models(files, id=tid) save_json_model(mod, new_path) else: mod = read_sbml_model(path.join(folder, row["file"])) save_json_model(mod, new_path)
def test_reaction_bounds_json(data_directory, tmp_path): """Test reading and writing of model with inf bounds in json""" """Path to XML file with INF bounds""" path_to_xml_inf_file = join(data_directory, "fbc_ex1.xml") model_xml_inf = cio.read_sbml_model(path_to_xml_inf_file) path_to_output = join(str(tmp_path), "fbc_ex1_json.json") """Saving model with inf bounds in json form without error""" cio.save_json_model(model_xml_inf, path_to_output) """Path to JSON file with INF bounds""" path_to_JSON_inf_file = join(data_directory, "JSON_with_inf_bounds.json") model_json_inf = cio.load_json_model(path_to_JSON_inf_file) assert model_json_inf.reactions[0].upper_bound == float("inf")
def to_json( directory, models_directory, icc389=None, icc431=None, icc470=None, icc651=None, ): if icc389: model = read_sbml_model(join(directory, models_directory, icc389)) save_json_model(model, join(directory, models_directory, f'{model.id}.json')) if icc431: model = read_sbml_model(join(directory, models_directory, icc431)) save_json_model(model, join(directory, models_directory, f'{model.id}.json')) if icc470: model = read_sbml_model(join(directory, models_directory, icc470)) save_json_model(model, join(directory, models_directory, f'{model.id}.json')) if icc651: model = read_sbml_model(join(directory, models_directory, icc651)) save_json_model(model, join(directory, models_directory, f'{model.id}.json'))
def mvgem(input_model, output_model): """Convert GEM format INPUT_MODEL to OUTPUT_MODEL. The format will be infered based on the extension of OUTPUT_MODEL. Supported formats: .sbml, .xml, .json, .mat """ model = load_model(input_model) out_format = get_destination_format(output_model) if out_format == "SBML": write_sbml_model(model, output_model) elif out_format == "MAT": save_matlab_model(model, output_model) elif out_format == "JSON": save_json_model(model, output_model) else: click.BadParameter( "Output format %s could not be recognised" % out_format ).show()
def create_pair_model(pair, output_folder): """ Create a two species community model. Parameters ---------- pair : tuple Each element is a path to a single species model file output_folder : str Path to output folder where community model JSON file is saved Returns ------- str Path to two species community model file """ community = create_community_model(pair) community_filename = '{0}/{1}.json'.format(output_folder, community.id) save_json_model(community, community_filename) return community_filename
from re import search, sub from os import path, mkdir from glob import glob DO_ALL_MODELS = True media = path.join( '/media/sf_G_DRIVE/Dropbox/Dropbox/trabajo/paper pablo/mmb_chapter/', 'recon2_fva') modelfolder = path.join(media, 'models') sbmlfolder = path.join(modelfolder, 'sbml') jsonfolder = path.join(modelfolder, 'json') def model_list_all(): ml = {} for m in glob(path.join(sbmlfolder, '*.xml')): mname = path.basename(m) mname = sub('.xml', '', mname) ml[mname] = m return ml ml = model_list_all() for network in sorted(ml): print network model = read_sbml_model(ml[network]) print(jsonfolder) save_json_model(model, jsonfolder + "/" + network + ".json") print(model)
mini.reactions.get_by_id(i).lower_bound = mini.reactions.PGI.lower_bound # set names and annotation for g in mini.genes: try: tg = textbook.genes.get_by_id(g.id) except KeyError: continue g.name = tg.name g.annotation = tg.annotation mini.reactions.sort() mini.genes.sort() mini.metabolites.sort() # output to various formats with open("mini.pickle", "wb") as outfile: dump(mini, outfile, protocol=2) save_matlab_model(mini, "mini.mat") save_json_model(mini, "mini.json", pretty=True) write_sbml_model(mini, "mini_fbc2.xml") write_sbml_model(mini, "mini_fbc2.xml.bz2") write_sbml_model(mini, "mini_fbc2.xml.gz") write_sbml2(mini, "mini_fbc1.xml", use_fbc_package=True) write_sbml_model(mini, "mini_cobra.xml", use_fbc_package=False) # fva results fva_result = cobra.flux_analysis.flux_variability_analysis(textbook) clean_result = OrderedDict() for key in sorted(fva_result): clean_result[key] = {k: round(v, 5) for k, v in fva_result[key].items()} with open("textbook_fva.json", "w") as outfile: json_dump(clean_result, outfile)
def create_universal_model_from_source(source_models, file_name=None, validate=False): """ Create an universal model from a list of source models. Parameters ---------- source_models : list of str List of path names to source model files file_name : str, optional Path to file for saving universal COBRA model in JSON format validate : bool, optional When True, perform validity checks on universal COBRA model Returns ------- cobra.Model COBRA model object with universal reactions and metabolites """ # Create a new COBRApy Model object. universal = Model('universal', name='Universal') universal.notes['sources'] = list() # Add all of the source models to the universal model. for model_filename in source_models: # Load the model from a file. model = load_model_from_file(model_filename) # All metabolites need to have a compartment suffix. for metabolite in model.metabolites: metabolite.notes['type'] = _id_type(metabolite.id) unknown = model.metabolites.query(lambda x: 'unknown' in x['type'], 'notes') if len(unknown) > 0: raise Exception('Unknown compartment suffixes found in metabolites for {0}'.format(model_filename)) # Remove any duplicate reactions from model which will leave a model with just the new reactions. duplicates = model.reactions.query(lambda x: universal.reactions.has_id(x), 'id') model.remove_reactions(duplicates, remove_orphans=True) # Remove objectives and genes from model. model.objective = {} for reaction in model.reactions: reaction.gene_reaction_rule = '' # Add new metabolites and reactions from the model to the universal model. universal += model # Add attributes not automatically added above. universal.notes['sources'].append({'id': model.id, 'filename': model_filename}) for compartment in model.compartments: if compartment in universal.compartments: if model.compartments[compartment] != universal.compartments[compartment]: warn('Model {0} has a different meaning for compartment {1} {2}' .format(model.id, compartment, model.compartments[compartment])) else: universal.compartments[compartment] = model.compartments[compartment] # If requested, validate the COBRA model. if validate: warn('Coming soon') # If requested, save the COBRA model. if file_name is not None: save_json_model(universal, file_name) return universal
def create_species_models(genome_ids, output_folder, replace=False, optimize=False): """ Create ModelSEED models from a list of PATRIC genome IDs. The ModelSEED server can be overwhelmed by too many requests so reconstructing models is single-threaded (and got weird errors trying to use multiprocessing). Parameters ---------- genome_ids : list of str List of PATRIC genome IDs output_folder: str Path to folder where single species models are stored replace : bool, optional When True, always reconstruct model using ModelSEED service and replace local model optimize : bool, optional When True, optimize the model and check for growth Returns ------- list of str List of paths to single species model files """ # Get a model for each genome in the list. output_models = list() for genome_id in genome_ids: # If possible, use the model that was previously obtained from ModelSEED. model_filename = join(output_folder, '{0}.json'.format(genome_id)) if exists(model_filename) and not replace: output_models.append(model_filename) else: reconstruct = False try: get_modelseed_model_stats(genome_id) if replace: delete_modelseed_model(genome_id) reconstruct = True except ObjectNotFoundError: reconstruct = True # If needed, reconstruct and gap fill a model from the genome. if reconstruct: reconstruct_modelseed_model(genome_id) gapfill_modelseed_model(genome_id) # Create a COBRA model and save in JSON format. model = create_cobra_model_from_modelseed_model(genome_id) save_json_model(model, model_filename) output_models.append(model_filename) # If requested, verify ModelSEED models produce growth. if optimize: for model_filename in output_models: model = load_json_model(model_filename) solution = model.optimize() if solution.f <= 0.0001: warn('Model {0} does not produce growth'.format(model.id)) return output_models
# set names and annotation for g in mini.genes: try: tg = textbook.genes.get_by_id(g.id) except KeyError: continue g.name = tg.name g.annotation = tg.annotation mini.reactions.sort() mini.genes.sort() mini.metabolites.sort() # output to various formats with open("mini.pickle", "wb") as outfile: dump(mini, outfile, protocol=2) save_matlab_model(mini, "mini.mat") save_json_model(mini, "mini.json", pretty=True) write_sbml_model(mini, "mini_fbc2.xml") write_sbml_model(mini, "mini_fbc2.xml.bz2") write_sbml_model(mini, "mini_fbc2.xml.gz") write_sbml2(mini, "mini_fbc1.xml", use_fbc_package=True) write_sbml_model(mini, "mini_cobra.xml", use_fbc_package=False) raven = load_matlab_model("raven.mat") with open("raven.pickle", "wb") as outfile: dump(raven, outfile, protocol=2) # fva results fva_result = cobra.flux_analysis.flux_variability_analysis(textbook) clean_result = OrderedDict() for key in sorted(fva_result): clean_result[key] = {k: round(v, 5) for k, v in fva_result[key].items()} with open("textbook_fva.json", "w") as outfile:
) rxn_flux_samples_ARCH = sampling.sample(COBRA_MODEL, 1000, method='achr', thinning=100, processes=6, seed=None) print("\t... saving flux samples for base cobra model: ", MODEL_SAMPLES_FILE) rxn_flux_samples_ARCH.to_csv(MODEL_SAMPLES_FILE) ENSEMBLE_BASEMODEL_FILE = ENSEMBLE_DIR + "/base_cobra_model.json" if not os.path.exists(ENSEMBLE_BASEMODEL_FILE): from cobra.io import save_json_model print("\t... saving base cobra model: ", ENSEMBLE_BASEMODEL_FILE) save_json_model(COBRA_MODEL, ENSEMBLE_BASEMODEL_FILE) base_flux_samples = pd.read_csv(MODEL_SAMPLES_FILE, index_col=0) ### Create Species object SPECIES_MODEL = cs.Species("species_obj") COBRA_MODEL.solver = "glpk" SPECIES_MODEL.base_cobra_model = COBRA_MODEL SPECIES_MODEL.load_from_matrix(X_species_final, filter_model_genes=True, allele_gene_sep="_") for allele in SPECIES_MODEL.alleles: allele.cobra_gene = allele.id.split("_")[0] ### Determines the genes that the MNC will model the alleles of if not os.path.exists(GENE_LIST_FILE):
from cobra.test import create_test_model model_names = ['salmonella', 'iJO1366', 'Yersinia_pestis_CO92_iPC815'] for model_name in model_names: # read in old pickle and model from sbml model_pickle = model_name + '.pickle' if model_name == "iJO1366": new_model = read_legacy_sbml(model_name + '.xml') else: new_model = read_sbml_model(model_name + '.xml') # update other attributes if isfile(model_name + ".genes"): with open(model_name + ".genes", "rb") as infile: gene_names = load(infile) for gene in new_model.genes: gene.name = gene_names[gene.id] if isfile(model_name + ".media"): with open(model_name + ".media", "rb") as infile: new_model.media_compositions = load(infile) new_model._cobra_version = get_version() # write out new pickle with open(model_pickle, 'wb') as outfile: dump(new_model, outfile, protocol=2) # write out other formats for iJO1366 if model_name == "iJO1366": save_matlab_model(new_model, model_name + ".mat") save_json_model(new_model, model_name + ".json") if model_name == "salmonella": write_sbml_model(new_model, model_name + "_fbc.xml")