def raise_libsbml_errors(): with pytest.raises(ImportError): io.read_sbml_model('test') with pytest.raises(ImportError): io.write_sbml_model(None, 'test') with pytest.raises(ImportError): io.load_matlab_model('test') with pytest.raises(ImportError): io.write_legacy_sbml(None, 'test') with pytest.raises(ImportError): io.read_legacy_sbml(None, 'test')
def create_cobra_model_from_vmh_recon2(validate=False): """ Create a COBRA model from the current Recon2 model. Parameters ---------- validate : bool, optional When True, perform validity checks on COBRA model Returns ------- cobra.Model COBRA model created from Matlab representation of Recon2 model """ # Download the zip file that contains the Matlab file and extract it. response = requests.get('{0}{1}_.zip'.format(vmh_url, recon2_file_name), stream=True) if response.status_code != requests.codes.OK: response.raise_for_status() zipfile.ZipFile(io.BytesIO(response.content), 'r').extract(recon2_file_name, gettempdir()) # Convert to a cobra.Model object. temp_file = join(gettempdir(), recon2_file_name) model = load_matlab_model(temp_file) unlink(temp_file) # If requested, validate the COBRA model. if validate: warn('Coming soon') return model
def load_model_from_file(filename): """ Load a model from a file based on the extension of the file name. Parameters ---------- filename : str Path to model file Returns ------- cobra.core.Model Model object loaded from file Raises ------ IOError If model file extension is not supported. """ (root, ext) = splitext(filename) if ext == '.mat': model = load_matlab_model(filename) elif ext == '.xml' or ext == '.sbml': model = read_sbml_model(filename) elif ext == '.json': model = load_json_model(filename) else: raise IOError( 'Model file extension not supported for {0}'.format(filename)) return model
def test_mat_read_write(self): test_output_filename = "test_mat_write.mat" io.save_matlab_model(self.model, test_output_filename) reread = io.load_matlab_model(test_output_filename) self.assertEqual(len(self.model.reactions), len(reread.reactions)) self.assertEqual(len(self.model.metabolites), len(reread.metabolites)) for i in range(len(self.model.reactions)): self.assertEqual(len(self.model.reactions[i]._metabolites), len(reread.reactions[i]._metabolites)) self.assertEqual(self.model.reactions[i].id, reread.reactions[i].id) unlink(test_output_filename)
def test_nnz(self): S = load_matlab_model(testing_models_filepath, "ecoli_core").to_array_based_model().S self.assertEqual(nnz(S), 309) # lil sparse matrix self.assertEqual(nnz(S.tocsc()), 309) # csc sparse matrix self.assertEqual(nnz(S.todok()), 309) # dok sparse matrix self.assertEqual(nnz(S.todense()), 309) # matrix self.assertEqual(nnz(array(S.todense())), 309) # ndarray self.assertEqual(nnz(array([0])), 0) self.assertEqual(nnz(array([0, 1])), 1) self.assertEqual(nnz(array([[0, 0], [0, 0]])), 0) self.assertEqual(nnz(array([[0, 1], [0, 0]])), 1) self.assertEqual(nnz(array([[1, 0], [0, 1]])), 2)
def test_mat_read_write(self): from warnings import catch_warnings test_output_filename = join(gettempdir(), "test_mat_write.mat") io.save_matlab_model(self.model, test_output_filename) with catch_warnings(record=True) as w: reread = io.load_matlab_model(test_output_filename) self.assertEqual(len(self.model.reactions), len(reread.reactions)) self.assertEqual(len(self.model.metabolites), len(reread.metabolites)) for i in range(len(self.model.reactions)): self.assertEqual(len(self.model.reactions[i]._metabolites), \ len(reread.reactions[i]._metabolites)) self.assertEqual(self.model.reactions[i].id, reread.reactions[i].id) unlink(test_output_filename)
def test_mat_read_write(self): """read and write COBRA toolbox models in .mat files""" from warnings import catch_warnings test_output_filename = join(gettempdir(), "test_mat_write.mat") io.save_matlab_model(self.model, test_output_filename) with catch_warnings(record=True) as w: reread = io.load_matlab_model(test_output_filename) self.assertEqual(len(self.model.reactions), len(reread.reactions)) self.assertEqual(len(self.model.metabolites), len(reread.metabolites)) for i in range(len(self.model.reactions)): self.assertEqual(len(self.model.reactions[i]._metabolites), \ len(reread.reactions[i]._metabolites)) self.assertEqual(self.model.reactions[i].id, reread.reactions[i].id) unlink(test_output_filename)
def pub_model(request): # get a specific model. This fixture and all the tests that use it will run # for every model in the model_files list. model_file = request.param model_path = join(settings.model_directory, model_file) # load the file start = time() try: if model_path.endswith('.xml'): pub_model = read_sbml_model(model_path) elif model_path.endswith('.mat'): pub_model = load_matlab_model(model_path) else: raise Exception('Unrecongnized extension for model %s' % model_file) except IOError: raise Exception('Could not find model %s' % model_path) print("Loaded %s in %.2f sec" % (model_file, time() - start)) return pub_model
def model_loader(gem_file_path, gem_file_type): """Consolidated function to load a GEM using COBRApy. Specify the file type being loaded. Args: gem_file_path (str): Path to model file gem_file_type (str): GEM model type - ``sbml`` (or ``xml``), ``mat``, or ``json`` format Returns: COBRApy Model object. """ if gem_file_type.lower() == 'xml' or gem_file_type.lower() == 'sbml': model = read_sbml_model(gem_file_path) elif gem_file_type.lower() == 'mat': model = load_matlab_model(gem_file_path) elif gem_file_type.lower() == 'json': model = load_json_model(gem_file_path) else: raise ValueError('File type must be "sbml", "xml", "mat", or "json".') return model
def _load_model_from_file(path, handle): """Try to parse a model from a file handle using different encodings. Adapted from cameo. """ try: model = pickle.load(handle) except (TypeError, pickle.UnpicklingError): try: model = load_json_model(path) except ValueError: try: model = load_matlab_model(path) except ValueError: try: model = read_sbml_model(path) except AttributeError: click.ClickException( "cobrapy doesn't raise a proper exception" " if a file does not contain an SBML model" ).show() except Exception as e: click.ClickException(e).show() return model
def test_load_mat(db_model): if db_model.id.startswith('iCHO') or db_model.id == 'iJB785': # remove when this is solved: https://github.com/opencobra/cobrapy/issues/919 return model = load_matlab_model(join(static_model_dir, db_model.id + '.mat')) assert model.id == db_model.id
from pytfa.io import load_thermoDB, \ read_lexicon, annotate_from_lexicon, \ read_compartment_data, apply_compartment_data 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)
double_reaction_deletion) from cobra.manipulation import remove_genes from cobra.manipulation import * from cobra.flux_analysis.moma import moma sys.path.append(r"/Users/luho/PycharmProjects/model/cobrapy/code") sys.path.append(r"/Users/luho/PycharmProjects/model/strain_design/code") os.chdir('/Users/luho/PycharmProjects/model/strain_design/code') # import self function from mainFunction import * from in_silico_strain_design_scripts import * # load the ecYeast model ecYeast = load_matlab_model("../data/ec3HP.mat") """knock-out""" # pFBA method is used # run the gene deletion with pFBA method, minimization of proteins pool # in this simulation, 3HP production is as the objective function # qs is unconstrait def getModelWithRemoveGene(model0, gene_remove0): model1 = model0.copy() remove_genes(model1, gene_remove0, remove_reactions=True) # this script can't be used return model1 def solveEcModel(model, obj_id, target_id, glucose_uptake='r_1714_REV'):
def setUp(self): self.model = load_matlab_model(testing_models_filepath, "ecoli_core") self.S = self.model.to_array_based_model().S
def test_load_matlab_model(data_directory, mini_model, raven_model): """Test the reading of MAT model.""" mini_mat_model = io.load_matlab_model(join(data_directory, "mini.mat")) raven_mat_model = io.load_matlab_model(join(data_directory, "raven.mat")) assert compare_models(mini_model, mini_mat_model) is None assert compare_models(raven_model, raven_mat_model) is None
def test_null(self): S = load_matlab_model(testing_models_filepath, "ecoli_core").to_array_based_model().S.todense() N = null(S) self.assertEqual(N.shape, (84, 23)) self.assertAlmostEqual(abs(S * N).max(), 0)
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: json_dump(clean_result, outfile) # textbook solution # TODO: this needs a reference solution rather than circular solution checking! solution = cobra.flux_analysis.parsimonious.optimize_minimal_flux(textbook) with open('textbook_solution.pickle', 'wb') as f:
savemat(round_filename % (k, now()), {"fluxes": dok_matrix(fluxes)}, oned_as="column") # if no overall improvement occured in this round, we are done if nnz(fluxes) == prevNum: break # save the final result savemat(final_filename % now(), { "fluxes": dok_matrix(fluxes), "history": array(improvement_tracker), "nnz_log": array(nnz_log)}, oned_as="column") # done! return fluxes if __name__ == "__main__": from cobra.io import load_matlab_model from time import time model = load_matlab_model("testing_models.mat", "ecoli_core") S = model.to_array_based_model().S start = time() solved_fluxes = minspan(model, cores=1, verbose=True) print "solved in %.2f seconds" % (time() - start) print "nnz", nnz(solved_fluxes) print "rank", matrix_rank(solved_fluxes) print "max(S * v) =", abs(S * solved_fluxes).max() #from IPython import embed; embed()
savemat(round_filename % (k, now()), {"fluxes": dok_matrix(fluxes)}, oned_as="column") # if no overall improvement occured in this round, we are done if nnz(fluxes) == prevNum: break # save the final result savemat(final_filename % now(), { "fluxes": dok_matrix(fluxes), "history": array(improvement_tracker), "nnz_log": array(nnz_log) }, oned_as="column") # done! return fluxes if __name__ == "__main__": from cobra.io import load_matlab_model from time import time model = load_matlab_model("testing_models.mat", "ecoli_core") S = model.to_array_based_model().S start = time() solved_fluxes = minspan(model, cores=1, verbose=True) print "solved in %.2f seconds" % (time() - start) print "nnz", nnz(solved_fluxes) print "rank", matrix_rank(solved_fluxes) print "max(S * v) =", abs(S * solved_fluxes).max() #from IPython import embed; embed()
def test_load_mat(db_model): model = load_matlab_model(join(static_model_dir, db_model.id + '.mat')) assert model.id == db_model.id
def raise_scipy_errors(): with pytest.raises(ImportError): io.save_matlab_model(None, 'test') with pytest.raises(ImportError): io.load_matlab_model('test')
def stop_engines(mpi=False, verbose=False): profile = profile_minspan_mpi_dir if mpi else profile_minspan_dir silence = "" if verbose else "2>/dev/null" os.system('ipcluster stop --profile-dir="%s" %s' % (profile, silence)) if __name__ == "__main__": stop_engines() c, v = start_engines() print "%d engines connected" % (len(c.ids)) from minspan import * from cobra.io import load_matlab_model from time import time model = load_matlab_model("testing_models.mat", "inj661") S = model.to_array_based_model().S start = time() solved_fluxes = minspan(model, cores=1, verbose=True, mapper=v.map_sync, starting_fluxes="auto") print "solved in %.2f seconds" % (time() - start) print "nnz", nnz(solved_fluxes) print "rank", matrix_rank(solved_fluxes) print "max(S * v) =", abs(S * solved_fluxes).max() #from IPython import embed; embed() stop_engines()
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) # TODO:these need a reference solutions rather than circular solution checking! # 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) # fva with pfba constraint fva_result = cobra.flux_analysis.flux_variability_analysis(textbook,
v = c.direct_view() except Exception, e: print e sleep(5) return c, v def stop_engines(mpi=False, verbose=False): profile = profile_minspan_mpi_dir if mpi else profile_minspan_dir silence = "" if verbose else "2>/dev/null" os.system('ipcluster stop --profile-dir="%s" %s' % (profile, silence)) if __name__ == "__main__": stop_engines() c, v = start_engines() print "%d engines connected" % (len(c.ids)) from minspan import * from cobra.io import load_matlab_model from time import time model = load_matlab_model("testing_models.mat", "inj661") S = model.to_array_based_model().S start = time() solved_fluxes = minspan(model, cores=1, verbose=True, mapper=v.map_sync, starting_fluxes="auto") print "solved in %.2f seconds" % (time() - start) print "nnz", nnz(solved_fluxes) print "rank", matrix_rank(solved_fluxes) print "max(S * v) =", abs(S * solved_fluxes).max() #from IPython import embed; embed() stop_engines()
def import_matlab_model(path, variable_name=None): """ Convert at matlab cobra_model to a pyTFA cobra_model, with Thermodynamic values :param variable_name: :param string path: The path of the file to import :returns: The converted cobra_model :rtype: cobra.thermo.model.Model """ # We're going to import the Matlab cobra_model and convert it to COBApy mat_data = loadmat(path) if variable_name: mat_model = mat_data[variable_name][0, 0] else: meta_vars = {"__globals__", "__header__", "__version__"} possible_keys = sorted(i for i in mat_data if i not in meta_vars) if len(possible_keys) == 1: variable_name = possible_keys[0] else: raise Exception('Need to specify a variable name if several ' + 'variables are contained int the mat file') mat_model = mat_data[variable_name][0, 0] # cobra_model = Model(mat_model['description'][0]) cobra_model = load_matlab_model(path) cobra_model.description = mat_model['description'][0] cobra_model.name = mat_model['description'][0] metabolites = cobra_model.metabolites ## METABOLITES # In the Matlab cobra_model, the corresponding components are : # * mets = Identifiers # * metNames = Names # * metFormulas = formulas # * metCompSymbol = compartments # def read_mat_model(mat_struct, field_name, index): # try: # return mat_struct[field_name][index,0][0] # except IndexError: # return None # metabolites = [Metabolite( read_mat_model(mat_model,'mets',i), # formula=read_mat_model(mat_model,'metFormulas',i), # name=read_mat_model(mat_model,'metNames',i), # compartment=read_mat_model(mat_model,'metCompSymbol',i)) # for i in range(len(mat_model['metNames']))] # Get the metSEEDID seed_id = mat_model['metSEEDID'] for i, _ in enumerate(metabolites): metabolites[i].annotation = {"seed_id": seed_id[i, 0][0]} # ## REACTIONS # # In the Matlab cobra_model, the corresponding components are : # # * rxns = Names # # * rev = Reversibility (not used, see https://cobrapy.readthedocs.io/en/0.5.11/faq.html#How-do-I-change-the-reversibility-of-a-Reaction?) # # * lb = Lower bounds # # * ub = Upper bounds # # * subSystems = subsystem names # # * S : Reactions matrix # # 1 line = 1 metabolite # # 1 column = 1 reaction # # * c : Objective coefficient # # * genes : Genes names # # * rules : Genes rules # # Some utilities for gene generation rules (convert the IDs to the names of the genes) # gene_pattern = re.compile(r'x\([0-9]+\)') # def gene_id_to_name(match): # id_ = int(match.group()[2:-1]) # # /!\ These are indexed from 1, while python indexes from 0 # return mat_model['genes'][id_ - 1, 0][0] # # Add each reaction # for i in range(mat_model['S'].shape[1]): # # Name of the reaction # reaction = Reaction(str(mat_model['rxns'][i, 0][0])) # # Add the reaction to the cobra_model # cobra_model.add_reactions([reaction]) # # NOTE : The str() conversion above is needed, otherwise the CPLEX solver # # does not work : "Invalid matrix input type --" # # Reaction description # # reaction.name not set # # Subsystem (only if set in the Matlab cobra_model) # if (len(mat_model['subSystems'][i, 0])): # reaction.subsystem = mat_model['subSystems'][i, 0][0] # # Lower bound # reaction.lower_bound = float(mat_model['lb'][i, 0]) # # Upper bound # reaction.upper_bound = float(mat_model['ub'][i, 0]) # # Objective coefficient # reaction.objective_coefficient = float(mat_model['c'][i, 0]) # # Metabolites # react_mets = {} # # Iterate over each metabolite and see if it is part of the reaction # # (stoechiomectric coefficient not equal to 0) # for j, _ in enumerate(metabolites): # if (mat_model['S'][j, i] != 0): # react_mets[metabolites[j]] = mat_model['S'][j, i] # reaction.add_metabolites(react_mets) # # Genes # try: # if len(mat_model['rules'][i, 0]): # rule = mat_model['rules'][i, 0][0] # # Call the regex magic to convert IDs to gene names # rule = gene_pattern.sub(gene_id_to_name, rule) # # Add the data to the reaction # reaction.gene_reaction_rule = rule # except ValueError: # pass # except IndexError: # # The gene number is higher than the length of the gene list # warn('The gene reaction rule {} appears to be misaligned with ' # 'the gene list'.format(rule)) Compartments = dict() CompartmentDB = mat_model['CompartmentData'][0] num_comp = len(CompartmentDB['pH'][0][0]) comps = [{} for i in range(num_comp)] for i in range(num_comp): comp = comps[i] comp['pH'] = CompartmentDB['pH'][0][0][i] comp['ionicStr'] = CompartmentDB['ionicStr'][0][0][i] comp['symbol'] = CompartmentDB['compSymbolList'][0][0, i][0] comp['name'] = CompartmentDB['compNameList'][0][0, i][0] comp['c_max'] = CompartmentDB['compMaxConc'][0][0][i] comp['c_min'] = CompartmentDB['compMinConc'][0][0][i] # Register our compartment by its name if comp['symbol'] in Compartments: raise Exception("Duplicate compartment ID : " + comp['symbol']) Compartments[comp['symbol']] = comp # We need to iterate first once to set the names of the compartments, so that we have the keys for our dictionnary... for i in range(num_comp): comp = comps[i] comp['membranePot'] = {} for j in range(num_comp): comp['membranePot'][comps[j]['symbol']] = \ CompartmentDB['membranePot'][0][i, j] cobra_model.compartments = Compartments return cobra_model