def test_m2m_recon_call(): """ Test m2m recon when called in terminal. """ sbml_file_path = os.path.join( *['recon_data_output', 'sbml', 'fatty_acid_beta_oxydation_I.sbml']) padmet_path = os.path.join( *['recon_data_output', 'padmet', 'fatty_acid_beta_oxydation_I.padmet']) subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc']) subprocess.call([ 'm2m', 'recon', '-g', 'recon_data', '-o', 'recon_data_output', '-c', '1', '-p' ]) reader = SBMLReader() document = reader.readSBML(sbml_file_path) expected_fabo_reactions = [ convert_from_coded_id(reaction.getId())[0] for reaction in document.getModel().getListOfReactions() ] assert set(get_fabo_reactions()).issubset(set(expected_fabo_reactions)) padmet = PadmetSpec(padmet_path) fabo_rxns = [ node.id for node in padmet.dicOfNode.values() if node.type == "reaction" ] assert set(get_fabo_reactions()).issubset(set(fabo_rxns)) shutil.rmtree('recon_data_output') subprocess.call([ 'm2m', 'recon', '-g', 'recon_data', '-o', 'recon_data_output', '-c', '1', '--pwt-xml' ]) reader = SBMLReader() document = reader.readSBML(sbml_file_path) # Extract reaction ID from annotaiton. fabo_reactions = [ reaction.name for reaction in document.getModel().getListOfReactions() ] known_fabo_reactions = get_fabo_reactions() results = {} for known_fabo_reaction in known_fabo_reactions: presence_reaction = sum([ 1 if known_fabo_reaction in fabo_reaction else 0 for fabo_reaction in fabo_reactions ]) if presence_reaction > 0: results[known_fabo_reaction] = True assert all(results.values()) shutil.rmtree('recon_data_output') subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc'])
def load_sbml_model(filename, kind=STOICHIOMETRIC): """ Loads a metabolic model from a file. Arguments: filename : String -- SBML file path kind : {STOICHIOMETRIC (default), CONSTRAINT_BASED, GPR_CONSTRAINED} -- define kind of model to load (optional) Returns: StoichiometricModel -- Stoichiometric model or respective subclass """ reader = SBMLReader() document = reader.readSBML(filename) sbml_model = document.getModel() if sbml_model is None: raise IOError('Failed to load model.') if kind == STOICHIOMETRIC: model = _load_stoichiometric_model(sbml_model) elif kind == CONSTRAINT_BASED: model = _load_constraintbased_model(sbml_model) elif kind == GPR_CONSTRAINED: model = _load_gprconstrained_model(sbml_model) else: model = None return model
def test_m2m_recon_call(): """ Test m2m recon when called in terminal. """ subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc']) subprocess.call([ 'm2m', 'recon', '-g', 'recon_data', '-o', 'recon_data_output', '-c', '1', '-p' ]) reader = SBMLReader() document = reader.readSBML( 'recon_data_output/sbml/fatty_acid_beta_oxydation_I.sbml') expected_fabo_reactions = [ convert_from_coded_id(reaction.getId())[0] for reaction in document.getModel().getListOfReactions() ] assert set(fabo_reactions()).issubset(set(expected_fabo_reactions)) padmet = PadmetSpec( 'recon_data_output/padmet/fatty_acid_beta_oxydation_I.padmet') fabo_rxns = [ node.id for node in padmet.dicOfNode.values() if node.type == "reaction" ] assert set(fabo_reactions()).issubset(set(fabo_rxns)) shutil.rmtree('recon_data_output') subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc'])
def load_sbml_model(filename, kind=None, flavor=None, exchange_detection_mode=None, load_gprs=True, load_metadata=True): """ Loads a metabolic model from a file. Arguments: filename (str): SBML file path kind (str): define kind of model to load ('cb' or 'ode', optional) flavor (str): adapt to different modeling conventions (optional, see Notes) exchange_detection_mode (str): detect exchange reactions (optional, see Notes) Returns: Model: Simple model or respective subclass Notes: Currently supported flavors: * 'cobra': UCSD models in the old cobra toolbox format * 'cobra:other': other models using the old cobra toolbox format * 'seed': modelSEED format * 'bigg': BiGG database format (uses sbml-fbc2) * 'fbc2': other models using sbml-fbc2 Supported exchange detection modes: * 'unbalanced': Exchange reactions is the one that have either only reactants or products * 'boundary': Exchange reaction is the one that have single boundary metabolite on one side * <regular expression pattern>: Regular expression which is executed against reaction ID Note that some flavors (cobra, bigg) have their own exchange detection mode. """ if not os.path.exists(filename): raise IOError("Model file was not found") reader = SBMLReader() document = reader.readSBML(filename) sbml_model = document.getModel() if sbml_model is None: document.printErrors() raise IOError('Failed to load model {}.'.format(filename)) if kind and kind.lower() == CB_MODEL: model = _load_cbmodel(sbml_model, flavor, exchange_detection_mode=exchange_detection_mode, load_gprs=load_gprs, load_metadata=load_metadata) elif kind and kind.lower() == ODE_MODEL: model = _load_odemodel(sbml_model) else: model = _load_stoichiometric_model(sbml_model) if load_metadata: _load_metadata(sbml_model, model) return model
def readModelFromFile(filePath): reader = SBMLReader() document = reader.readSBML(filePath) assert document.getNumErrors() == 0, "Error detected in sbml file" model = document.getModel() return (model)
def readModelFromFile(filePath): reader = SBMLReader() document = reader.readSBML(filePath) assert document.getNumErrors() == 0, "Error detected in sbml file" model = document.getModel() return model
def test_m2m_metacom_call(): """ Test m2m metacom when called in terminal. """ # RUN THE COMMAND inppath = 'metabolic_data/' respath = 'metacom_output/' if not os.path.exists(respath): os.makedirs(respath) with tarfile.open(inppath + 'toy_bact.tar.gz') as tar: tar.extractall(path=respath) subprocess.call([ 'm2m', 'metacom', '-n', respath + '/toy_bact', '-o', respath, '-s', inppath + '/seeds_toy.sbml', '-q' ]) target_file = respath + 'community_analysis/targets.sbml' iscope_file = respath + 'indiv_scopes/indiv_scopes.json' cscope_file = respath + 'community_analysis/comm_scopes.json' resfile = respath + 'community_analysis/mincom.json' # ISCOPE ANALYSIS # ensure there is the right number of computed indiv scopes with open(iscope_file, 'r') as json_idata: d_iscope = json.load(json_idata) assert len(d_iscope) == NUMBER_BACT # ensure the union and intersection are ok d_iscope_set = {} for elem in d_iscope: d_iscope_set[elem] = set(d_iscope[elem]) union_scope = set.union(*list(d_iscope_set.values())) assert len(union_scope) == SIZE_UNION intersection_scope = set.intersection(*list(d_iscope_set.values())) assert len(intersection_scope) == SIZE_INTERSECTION # CSCOPE ANALYSIS with open(cscope_file, 'r') as json_cdata: d_cscope = json.load(json_cdata) assert len(d_cscope['com_scope']) == SIZE_CSCOPE # ADDEDVALUE ANALYSIS reader = SBMLReader() document = reader.readSBML(target_file) new_targets = set([specie.getId() for specie in document.getModel().getListOfSpecies()]) assert new_targets == EXPECTED_TARGETS # MINCOM ANALYSIS with open(resfile, 'r') as json_data: d_mincom = json.load(json_data) # ensure the minimal number of bacteria in a minimal community is ok assert len(d_mincom['bacteria']) == MIN_SIZE_COM # ensure the bacteria in union are ok assert set(d_mincom['union_bacteria']) == UNION # ensure the bacteria in intersection are ok assert set(d_mincom['inter_bacteria']) == INTERSECTION # ensure the newly producible targets are ok assert set(d_mincom['newly_prod']) == NEWLYPROD_TARGETS # clean shutil.rmtree(respath)
def get_sbml_level(sbml_file): """Get SBML Level of a file Args: sbml_file (str): SBML file Returns: int: SBML Level """ reader = SBMLReader() document = reader.readSBML(sbml_file) return document.getLevel()
def load_sbml_model(filename, kind=None, flavor=None, exchange_detection_mode=None, load_gprs=True, load_metadata=True): """ Loads a metabolic model from a file. Arguments: filename (str): SBML file path kind (str): define kind of model to load ('cb' or 'ode', optional) flavor (str): adapt to different modeling conventions (optional, see Notes) exchange_detection_mode (str): detect exchange reactions (optional, see Notes) Returns: Model: Simple model or respective subclass Notes: Currently supported flavors: * 'cobra': UCSD models in the old cobra toolbox format * 'cobra:other': other models using the old cobra toolbox format * 'seed': modelSEED format * 'bigg': BiGG database format (uses sbml-fbc2) * 'fbc2': other models using sbml-fbc2 Supported exchange detection modes: * 'unbalanced': Exchange reactions is the one that have either only reactants or products * 'boundary': Exchange reaction is the one that have single boundary metabolite on one side * <regular expression pattern>: Regular expression which is executed against reaction ID Note that some flavors (cobra, bigg) have their own exchange detection mode. """ if not os.path.exists(filename): raise IOError("Model file was not found") reader = SBMLReader() document = reader.readSBML(str(filename)) sbml_model = document.getModel() if sbml_model is None: document.printErrors() raise IOError('Failed to load model {}.'.format(filename)) if kind and kind.lower() == CB_MODEL: model = _load_cbmodel(sbml_model, flavor, exchange_detection_mode=exchange_detection_mode, load_gprs=load_gprs, load_metadata=load_metadata) elif kind and kind.lower() == ODE_MODEL: model = _load_odemodel(sbml_model) else: model = _load_stoichiometric_model(sbml_model) if load_metadata: _load_metadata(sbml_model, model) return model
def get_compounds(sbml_file): """Get target from sbml Args: sbml_file (str): SBML file Returns: list: target """ reader = SBMLReader() document = reader.readSBML(sbml_file) model = document.getModel() targets = [target.id for target in model.getListOfSpecies()] return targets
def test_m2m_addedvalue_call(): """ Test m2m addedvalue when called in terminal. """ # RUN THE COMMAND inppath = 'metabolic_data' respath = 'addedvalue_output' toy_tgz_bact = os.path.join(inppath, 'toy_bact.tar.gz') toy_bact = os.path.join(respath, 'toy_bact') seeds_path = os.path.join(inppath, 'seeds_toy.sbml') if not os.path.exists(respath): os.makedirs(respath) with tarfile.open(toy_tgz_bact) as tar: tar.extractall(path=respath) subprocess.call([ 'm2m', 'addedvalue', '-n', toy_bact, '-o', respath, '-s', seeds_path, '-q' ]) target_file = os.path.join( *[respath, 'community_analysis', 'targets.sbml']) iscope_file = os.path.join(*[respath, 'indiv_scopes', 'indiv_scopes.json']) cscope_file = os.path.join( *[respath, 'community_analysis', 'comm_scopes.json']) # ISCOPE ANALYSIS # ensure there is the right number of computed indiv scopes with open(iscope_file, 'r') as json_idata: d_iscope = json.load(json_idata) assert len(d_iscope) == NUMBER_BACT # ensure the union and intersection are ok d_iscope_set = {} for elem in d_iscope: d_iscope_set[elem] = set(d_iscope[elem]) union_scope = set.union(*list(d_iscope_set.values())) assert len(union_scope) == SIZE_UNION intersection_scope = set.intersection(*list(d_iscope_set.values())) assert len(intersection_scope) == SIZE_INTERSECTION # CSCOPE ANALYSIS with open(cscope_file, 'r') as json_cdata: d_cscope = json.load(json_cdata) assert len(d_cscope['com_scope']) == SIZE_CSCOPE # ADDEDVALUE ANALYSIS reader = SBMLReader() document = reader.readSBML(target_file) new_targets = set( [specie.getId() for specie in document.getModel().getListOfSpecies()]) assert new_targets == EXPECTED_TARGETS # clean shutil.rmtree(respath)
def get_compounds(sbml_file): """Get compound from sbml Args: sbml_file (str): SBML file Returns: list: compound """ reader = SBMLReader() document = reader.readSBML(sbml_file) model = document.getModel() if model is None: logger.critical( 'SBML file "' + sbml_file + '" not well formatted. Is this file a SBML? Does it contains <model></model> tags?' ) sys.exit(1) compounds = [compound.id for compound in model.getListOfSpecies()] return compounds
def load_allosteric_model(filename): reader = SBMLReader() document = reader.readSBML(filename) sbml_model = document.getModel() if sbml_model is None: raise IOError('Failed to load model.') model = AllostericModel(sbml_model.getId()) model.add_compartments(_load_compartments(sbml_model)) model.add_metabolites(_load_metabolites(sbml_model)) model.add_reactions(_load_reactions(sbml_model)) model.add_stoichiometry(_load_stoichiometry(sbml_model)) model.add_regulators(_load_regulators(sbml_model)) bounds, coefficients = _load_cb_parameters(sbml_model) model.set_bounds(bounds) model.set_objective_coefficients(coefficients) genes, rules = _load_gpr(sbml_model) model.add_genes(genes) model.set_rules(rules) return model
def extend_model_with_DB_SBML(model, reactionsDB): """ Extends the given model with the reactions in reactionsDB. This is done by loading the model in sbml format reactionsDB and adding only the reactions that are not part of "model" to it. Arguments: model : the constraint based model to be extended reactionsDB (str): SBML file path for the reactionsDB model Returns: model: the given model extended with the database reactions/metabolites db_reactions: the reactions that were added from the database to the given model """ reader = SBMLReader() document = reader.readSBML(reactionsDB) sbml_model = document.getModel() if sbml_model is None: raise IOError('Failed to load model.') (model, db_reactions) = _load_constraintbased_model(sbml_model, model) return (model, db_reactions)
help="output filename for families statistics", required=True) args = parser.parse_args() mn_dir = args.dir json_family = args.json outfile = args.output species_by_mn = {} for mn in os.listdir(mn_dir): print(mn) mn_name = mn.rstrip(".sbml") reader = SBMLReader() model = reader.readSBML(mn_dir + '/' + mn).getModel() species = [ convert_from_coded_id(i.getId())[0] for i in model.getListOfSpecies() ] species_by_mn[mn_name] = species with open(json_family, "r") as f: family_dict = json.load(f) # count_dict = {i:0 for i in list(set(sum(family_dict.values(), [])))} families_by_mn = {} for mn in species_by_mn: families_by_mn[mn] = { i: 0
def test_m2m_metacom_targets_import(): """ Test m2m metacom when called in terminal. """ # RUN THE COMMAND inppath = 'metabolic_data/' respath = 'metacom_output/' toy_bact_tgz_path = os.path.join(inppath, 'toy_bact.tar.gz') toy_bact_path = os.path.join(respath, 'toy_bact') seeds_path = os.path.join(inppath, 'seeds_toy.sbml') targets_path = os.path.join(inppath, 'targets_toy.sbml') if not os.path.exists(respath): os.makedirs(respath) with tarfile.open(toy_bact_tgz_path) as tar: tar.extractall(path=respath) metage2metabo.m2m_workflow.metacom_analysis(sbml_dir=toy_bact_path, out_dir=respath, seeds=seeds_path, host_mn=None, targets_file=targets_path) iscope_file = os.path.join(*[respath, 'indiv_scopes', 'indiv_scopes.json']) cscope_file = os.path.join( *[respath, 'community_analysis', 'comm_scopes.json']) resfile = os.path.join(*[respath, 'community_analysis', 'mincom.json']) targetfile = os.path.join(*[respath, 'producibility_targets.json']) # ISCOPE ANALYSIS # ensure there is the right number of computed indiv scopes with open(iscope_file, 'r') as json_idata: d_iscope = json.load(json_idata) assert len(d_iscope) == NUMBER_BACT # ensure the union and intersection are ok d_iscope_set = {} for elem in d_iscope: d_iscope_set[elem] = set(d_iscope[elem]) union_scope = set.union(*list(d_iscope_set.values())) assert len(union_scope) == SIZE_UNION intersection_scope = set.intersection(*list(d_iscope_set.values())) assert len(intersection_scope) == SIZE_INTERSECTION # CSCOPE ANALYSIS with open(cscope_file, 'r') as json_cdata: d_cscope = json.load(json_cdata) assert len(d_cscope['com_scope']) == SIZE_CSCOPE # MINCOM ANALYSIS with open(resfile, 'r') as json_data: d_mincom = json.load(json_data) # Targets results with open(targetfile, 'r') as json_data: d_target = json.load(json_data) # ensure the minimal number of bacteria in a minimal community is ok assert len(d_mincom['bacteria']) == MIN_SIZE_COM # ensure the bacteria in union are ok assert set(d_mincom['union_bacteria']) == UNION # ensure the bacteria in intersection are ok assert set(d_mincom['inter_bacteria']) == INTERSECTION # ensure the newly producible targets are ok assert set(d_mincom['producible']) == PROD_TARGETS # ensure the bacteria in union are ok assert set(d_target['keystone_species']) == UNION # ensure the newly producible targets are ok assert set(d_target['mincom_producible']) == PROD_TARGETS # Ensure the final producers in com_only_producers contains reactions producing the targets sbml_products = {} for sbml_file in os.listdir(toy_bact_path): reader = SBMLReader() sbml_path = os.path.join(toy_bact_path, sbml_file) document = reader.readSBML(sbml_path) model = document.getModel() sbml_name, _ = os.path.splitext(sbml_file) sbml_products[sbml_name] = [ product.getSpecies() for sbml_reaction in model.getListOfReactions() for product in sbml_reaction.getListOfProducts() ] for target in d_target['com_only_producers']: for species in d_target['com_only_producers'][target]: assert target in sbml_products[species] # clean shutil.rmtree(respath)
('00050', "./00050/00050-sbml-l2v4.xml"), ('00100', "./00100/00100-sbml-l2v4.xml"), ('00150', "./00150/00150-sbml-l2v4.xml"), ('00200', "./00200/00200-sbml-l2v4.xml"), ('00250', "./00250/00250-sbml-l2v4.xml"), ('00300', "./00300/00300-sbml-l2v4.xml"), ('00350', "./00350/00350-sbml-l2v4.xml"), ('00400', "./00400/00400-sbml-l2v4.xml"), ('00450', "./00450/00450-sbml-l2v4.xml"), ('00500', "./00500/00500-sbml-l2v4.xml")] for t in tests: name = t[0] path = t[1] reader = SBMLReader() document = reader.readSBML(path) errors = document.getNumErrors(); if (errors > 0): print("Encountered the following SBML errors:" + "\n", file=stderr); document.printErrors(); print("Conversion skipped. Please correct the problems above first." + "\n", file=stderr); continue model = document.getModel() print('{}: {} species and {} reactions'.format(name, model.getNumSpecies(), model.getNumReactions()))
'00150', '00200', '00250', '00300', '00350', '00400', '00450', '00500'] for d in dirs: modelfile = filter(lambda x: '.xml' in x, listdir(d))[0] print(modelfile) reader = SBMLReader() document = reader.readSBML(os.path.join(d,modelfile)) errors = document.getNumErrors(); if (errors > 0): print("Encountered the following SBML errors:" + "\n"); document.printErrors(); print("Conversion skipped. Please correct the problems above first." + "\n"); break # # If the given model is not already L2v4, assume that the user wants to # convert it to the latest release of SBML (which is L2v4 currently). # If the model is already L2v4, assume that the user wants to attempt to # convert it down to Level 1 (specifically L1v2).
def load_model_sbml(self, path): reader = SBMLReader() self.document = reader.readSBML(path) self.model = self.document.getModel() self.fill_data_sbml() self.type = "sbml"
from os import listdir import sys from sys import exit dirs = [ '00001', '00002', '00050', '00100', '00150', '00200', '00250', '00300', '00350', '00400', '00450', '00500' ] for d in dirs: modelfile = filter(lambda x: '.xml' in x, listdir(d))[0] print(modelfile) reader = SBMLReader() document = reader.readSBML(os.path.join(d, modelfile)) errors = document.getNumErrors() if (errors > 0): print("Encountered the following SBML errors:" + "\n") document.printErrors() print("Conversion skipped. Please correct the problems above first." + "\n") break # # If the given model is not already L2v4, assume that the user wants to # convert it to the latest release of SBML (which is L2v4 currently). # If the model is already L2v4, assume that the user wants to attempt to # convert it down to Level 1 (specifically L1v2).