def test_merge(self): model = native.NativeModel() model.compounds.update([ entry.DictCompoundEntry({ 'id': 'g6p_c', 'name': 'G6P' }), entry.DictCompoundEntry({ 'id': 'g6p_e', 'name': 'G6P' }) ]) model.reactions.update([ entry.DictReactionEntry({ 'id': 'TP_g6p', 'equation': parse_reaction('g6p_c[c] <=> g6p_e[e]') }) ]) exchange_compound = Compound('g6p_e', 'e') model.exchange[exchange_compound] = (exchange_compound, 'EX_g6p_e', -10, 0) sbml.merge_equivalent_compounds(model) self.assertEqual({entry.id for entry in model.compounds}, {'g6p'}) self.assertEqual(model.reactions['TP_g6p'].equation, parse_reaction('g6p[c] <=> g6p[e]')) new_exchange_compound = Compound('g6p', 'e') self.assertEqual(model.exchange[new_exchange_compound], (new_exchange_compound, 'EX_g6p_e', -10, 0))
def test_long_string_model(self): long_string = '''--- name: Test model biomass: rxn_1 reactions: - id: rxn_1 equation: '|A_\u2206[e]| => |B[c]|' genes: - gene_1 - gene_2 - id: rxn_2_\u03c0 equation: '|B[c]| => |C[e]|' genes: 'gene_3 or (gene_4 and gene_5)' compounds: - id: A_\u2206 - id: B - id: C media: - compartment: e compounds: - id: A_\u2206 - id: C limits: - reaction: rxn_2_\u03c0 upper: 100 ''' m = native.NativeModel(long_string) self.assertEqual(m.name, 'Test model') self.assertEqual(m.biomass_reaction, 'rxn_1') self.assertEqual(m.extracellular_compartment, 'e') reactions = list(m.parse_reactions()) self.assertEqual(reactions[0].id, 'rxn_1')
def setUp(self): self.model = native.NativeModel() self.model.reactions.update([ entry.DictReactionEntry({ 'id': 'EX_g6p', 'equation': parse_reaction('g6p[e] <=>') }), entry.DictReactionEntry({ 'id': 'EX_glc-D', 'equation': parse_reaction('glc-D[e] <=>') }), entry.DictReactionEntry({ 'id': 'SINK_glc-D', 'equation': parse_reaction('glc-D[c] <=>') }), entry.DictReactionEntry({ 'id': 'rxn_1', 'equation': parse_reaction('g6p[c] <=> glc-D[c]') }), entry.DictReactionEntry({ 'id': 'TP_glc-D', 'equation': parse_reaction('glc-D[c] <=> glc-D[e]') }) ])
def test_long_string_model_include(self): longString = '''--- name: Test model biomass: rxn_1 reactions: - include: medium.yaml ''' m = native.NativeModel(longString) with self.assertRaises(context.ContextError): list(m.parse_reactions())
def test_parse_model_file_with_media(self): path = self.write_model_file( 'model.yaml', '\n'.join([ 'extracellular: Ex', 'media:', ' - compounds:', ' - id: A', ' - id: B', ' compartment: c' ])) model = native.NativeModel(path) medium = list(model.parse_medium()) self.assertEqual(medium[0][0], Compound('A', 'Ex')) self.assertEqual(medium[1][0], Compound('B', 'c'))
def test_parse_model_file(self): path = self.write_model_file( 'model.yaml', '\n'.join([ 'name: Test model', 'biomass: biomass_reaction_id', 'extracellular: Extra', 'default_flux_limit: 500' ])) model = native.NativeModel(path) self.assertEqual(model.get_name(), 'Test model') self.assertEqual(model.get_biomass_reaction(), 'biomass_reaction_id') self.assertEqual(model.get_extracellular_compartment(), 'Extra') self.assertEqual(model.get_default_flux_limit(), 500)
def test_properties(self): model = native.NativeModel() model.name = 'Test model' model.version_string = '1.0' model.biomass_reaction = 'rxn_1' model.extracellular_compartment = 'e' model.default_compartment = 'c' model.default_flux_limit = 1000 self.assertEqual(model.name, 'Test model') self.assertEqual(model.version_string, '1.0') self.assertEqual(model.biomass_reaction, 'rxn_1') self.assertEqual(model.extracellular_compartment, 'e') self.assertEqual(model.default_compartment, 'c') self.assertEqual(model.default_flux_limit, 1000)
def test_dict_model(self): dict_model = { 'name': 'Test model', 'biomass': 'rxn_1', 'reactions': [{ 'id': 'rxn_1', 'equation': '|A_\u2206[e]| => |B[c]|', 'genes': ['gene_1', 'gene_2'] }, { 'id': 'rxn_2_\u03c0', 'equation': '|B[c]| => |C[e]|', 'genes': 'gene_3 or (gene_4 and gene_5)' }], 'compounds': [{ 'id': 'A_\u2206' }, { 'id': 'B' }, { 'id': 'C' }], 'media': [{ 'compartment': 'e', 'compounds': [{ 'id': 'A_\u2206' }, { 'id': 'C' }] }], 'limits': [{ 'reaction': 'rxn_2_\u03c0', 'upper': 100 }] } dmodel = native.NativeModel(dict_model) self.assertEqual(dmodel.name, 'Test model') self.assertEqual(dmodel.biomass_reaction, 'rxn_1') self.assertEqual(dmodel.extracellular_compartment, 'e')
def test_invalid_model_type(self): with self.assertRaises(ValueError): native.NativeModel(42.2)
def _import(self, file, name): model_doc = loadmat(file) if name is None: names = [ k for k in model_doc.keys() if k not in ['__header__', '__version__', '__globals__'] ] modelnames = list() for i in names: if (isinstance(model_doc[i], np.ndarray) and len(model_doc[i].dtype) > 1): modelnames.append(i) if len(modelnames) == 1: name = str(modelnames[0]) model_doc = model_doc[modelnames[0]] elif len(modelnames) > 1: raise ModelLoadError(('More than one model exist, ' 'please choose from: %s ' 'using --model-name' % modelnames)) else: raise ModelLoadError('Incorrect format') else: if name not in model_doc.keys(): raise ModelLoadError('Incorrect model name specified') else: model_doc = model_doc[name] if not (isinstance(model_doc, np.ndarray) and len(model_doc.dtype) > 1): raise ModelLoadError('Incorrect format') model = native.NativeModel() model.name = name self._compartments_from_compound = set() self._origid_compound = dict() model.compounds.update(self._read_compounds(model_doc)) model.compartments.update(self._compartments_from_compound) self._boundary_compartment = None # Add model level compartment information if all(var in model_doc.dtype.names for var in ['comps', 'compNames']): model.compartments.update(self._read_compartments(model_doc)) for comp in model.compartments: if comp.name.lower() == 'boundary': # has boundary compartment self._boundary_compartment = comp.id model.compartments.discard(self._boundary_compartment) for reaction, lower, upper in self._read_reactions(model_doc): model.reactions.add_entry(reaction) if lower is not None and upper is not None: model.limits[reaction.id] = reaction.id, lower, upper biomass_reaction = None objective_reactions = set() if 'c' in model_doc.dtype.names: for i in range(len(model_doc['c'][0, 0])): if model_doc['c'][0, 0][i][0] != 0: objective_reactions.add( str(model_doc['rxns'][0, 0][i][0][0])) if len(objective_reactions) == 1: biomass_reaction = next(iter(objective_reactions)) logger.info( 'Detected biomass reaction: {}'.format(biomass_reaction)) elif len(objective_reactions) > 1: logger.warning( 'Multiple reactions are used as the' ' biomass reaction: {}'.format(objective_reactions)) else: logger.warning('No objective reaction') model.biomass_reaction = biomass_reaction return model