def test_reaction_not_equals_other_with_different_direction(self): r = Reaction(Reaction.Right, [(Compound('Au'), 1)], [(Compound('Pb'), 1)]) self.assertNotEqual( r, Reaction(Reaction.Left, [(Compound('Pb'), 1)], [(Compound('Au'), 1)]))
def test_reaction_equals_other(self): r = Reaction(Reaction.Right, [(Compound('Pb'), 1)], [(Compound('Au'), 1)]) self.assertEqual( r, Reaction(Reaction.Right, [(Compound('Pb'), 1)], [(Compound('Au'), 1)]))
def test_reaction_format_with_arguments(self): pp1 = Compound('Polyphosphate', arguments=[4]) pp2 = Compound('Polyphosphate', arguments=[5]) r = Reaction(Reaction.Right, [(Compound('ATP'), 1), (pp1, 1)], [(Compound('ADP'), 1), (pp2, 1)]) self.assertEqual( str(r), '|ATP| + |Polyphosphate(4)| => |ADP| + |Polyphosphate(5)|')
def test_reaction_normalized_of_left(self): r = Reaction(Reaction.Left, [(Compound('Au'), 1)], [(Compound('Pb'), 1)]).normalized() self.assertEqual( r, Reaction(Reaction.Right, [(Compound('Pb'), 1)], [(Compound('Au'), 1)]))
def test_parse_medium(self): medium = list( native.parse_medium( { 'compartment': 'e', 'compounds': [{ 'id': 'ac' }, { 'id': 'glcD', 'lower': -10 }, { 'id': 'co2', 'upper': 50 }, { 'id': 'compound_x', 'compartment': 'c' }, { 'id': 'compound_y', 'reaction': 'EX_cpdy' }] }, 'e')) self.assertEqual(len(medium), 5) self.assertEqual(medium[0], (Compound('ac', 'e'), None, None, None)) self.assertEqual(medium[1], (Compound('glcD', 'e'), None, -10, None)) self.assertEqual(medium[2], (Compound('co2', 'e'), None, None, 50)) self.assertEqual(medium[3], (Compound('compound_x', 'c'), None, None, None)) self.assertEqual(medium[4], (Compound('compound_y', 'e'), 'EX_cpdy', None, None))
def test_write_reactions(self): stream = StringIO() reactions = [ entry.DictReactionEntry({ 'id': 'r1', 'name': 'Reaction 1', 'equation': Reaction(Direction.Both, {}) }), entry.DictReactionEntry({ 'id': 'r2', 'name': 'Reaction 2', 'equation': Reaction(Direction.Forward, { Compound('c1', 'c'): -1, Compound('c2', 'c'): 2 }) }) ] self.writer.write_reactions(stream, reactions) # The reaction equation for r1 is invalid (no compounds) and is # therefore skipped. self.assertEqual(yaml.safe_load(stream.getvalue()), [{ 'id': 'r1', 'name': 'Reaction 1' }, { 'id': 'r2', 'name': 'Reaction 2', 'equation': 'c1[c] => (2) c2[c]' }])
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_reaction_normalized_of_bidir(self): r = Reaction( Direction.Both, [(Compound('Au'), 1)], [(Compound('Pb'), 1)]).normalized() self.assertEqual( r, Reaction(Direction.Both, [(Compound('Au'), 1)], [(Compound('Pb'), 1)]))
def test_compound_set(self): self.assertEqual( set(self.model.compounds), {Compound('A'), Compound('B'), Compound('C'), Compound('D', 'e')})
def test_reaction_init_from_one_iterable_with_non_numbers(self): compounds = [ (Compound('A'), 'x'), (Compound('B'), 3) ] with self.assertRaises(TypeError): r = Reaction(Direction.Forward, iter(compounds))
def test_parse_reaction_table_file(self): path = self.write_model_file( 'reactions.tsv', '\n'.join([ 'id\tname\tequation', 'rxn_1\tReaction 1\t|A| => (2) |B|', 'rxn_2\tSecond reaction\t<=> |C|' ])) reactions = list(native.parse_reaction_file(path)) self.assertEqual(len(reactions), 2) self.assertEqual(reactions[0].id, 'rxn_1') self.assertEqual(reactions[0].name, 'Reaction 1') self.assertEqual(reactions[0].properties['name'], 'Reaction 1') self.assertEqual( reactions[0].equation, Reaction(Direction.Forward, [(Compound('A'), 1)], [(Compound('B'), 2)])) self.assertEqual(reactions[0].properties.get('ec'), None) self.assertEqual(reactions[0].genes, None) self.assertEqual(reactions[0].filemark.filecontext.filepath, path) self.assertEqual(reactions[0].filemark.line, 2) self.assertEqual(reactions[1].id, 'rxn_2') self.assertEqual(reactions[1].name, 'Second reaction') self.assertEqual(reactions[1].equation, Reaction(Direction.Both, [], [(Compound('C'), 1)])) self.assertEqual(reactions[1].filemark.filecontext.filepath, path) self.assertEqual(reactions[1].filemark.line, 3)
def test_parse_exchange_yaml_file(self): path = self.write_model_file( 'exchange.yaml', '\n'.join([ 'compartment: e', 'compounds:', ' - id: cpd_A', ' reaction: EX_A', ' lower: -40', ' - id: cpd_B', ' upper: 100', ' - id: cpd_C', ' lower: -100.0', ' upper: 500.0', ' - id: cpd_D', ' compartment: c', ' - id: cpd_E', ' fixed: 100.0', ])) exchange = list(native.parse_exchange_file(path, 'e')) self.assertEqual(exchange, [(Compound('cpd_A', 'e'), 'EX_A', -40, None), (Compound('cpd_B', 'e'), None, None, 100), (Compound('cpd_C', 'e'), None, -100, 500), (Compound('cpd_D', 'c'), None, None, None), (Compound('cpd_E', 'e'), None, 100, 100)])
def test_parse_reaction_list(self): reactions = list( native.parse_reaction_list('./test.yaml', [{ 'id': 'rxn1', 'equation': { 'reversible': True, 'left': [{ 'id': 'A', 'value': 1 }, { 'id': 'B', 'value': 2 }], 'right': [{ 'id': 'C', 'value': 1 }] } }])) self.assertEqual(len(reactions), 1) reaction = Reaction(Direction.Both, [(Compound('A'), 1), (Compound('B'), 2)], [(Compound('C'), 1)]) self.assertEqual(reactions[0].equation, reaction)
def test_parse_exchange(self): exchange = list( native.parse_exchange( { 'compartment': 'e', 'compounds': [{ 'id': 'ac' }, { 'id': 'glcD', 'lower': -10 }, { 'id': 'co2', 'upper': 50 }, { 'id': 'compound_x', 'compartment': 'c' }, { 'id': 'compound_y', 'reaction': 'EX_cpdy' }] }, 'e')) self.assertEqual(len(exchange), 5) self.assertEqual(exchange[0], (Compound('ac', 'e'), None, None, None)) self.assertEqual(exchange[1], (Compound('glcD', 'e'), None, -10, None)) self.assertEqual(exchange[2], (Compound('co2', 'e'), None, None, 50)) self.assertEqual(exchange[3], (Compound('compound_x', 'c'), None, None, None)) self.assertEqual(exchange[4], (Compound('compound_y', 'e'), 'EX_cpdy', None, None))
def test_set_reaction_with_zero_coefficient(self): reaction = Reaction(Direction.Both, [(Compound('A'), 1), (Compound('B'), 0)], [(Compound('C'), 1)]) self.database.set_reaction('rxn_new', reaction) self.assertNotIn('rxn_new', self.database.get_compound_reactions(Compound('B')))
def test_compounds(self): self.assertEqual( set(self.database.compounds), {Compound('A'), Compound('B'), Compound('C'), Compound('D', 'e')})
def test_gapfill_add_reaction(self): core = set(self.model.reactions) - {'rxn_4'} blocked = {Compound('D'), Compound('E')} add, rev = gapfill(self.model, core, blocked, self.solver) self.assertEqual(set(rev), set()) self.assertEqual(set(add), {'rxn_4'})
def test_kegg_parse_with_count_expression(self): r = kegg.parse_reaction('2n C00404 + n C00001 <=> (n+1) C02174') self.assertEqual( r, Reaction(Reaction.Bidir, [(Compound('C00404'), Expression('2n')), (Compound('C00001'), Expression('n'))], [(Compound('C02174'), Expression('n+1'))]))
def test_reaction_normalized_of_left(self): r = Reaction( Direction.Reverse, [(Compound('Au'), 1)], [(Compound('Pb'), 1)]).normalized() self.assertEqual( r, Reaction(Direction.Forward, [(Compound('Pb'), 1)], [(Compound('Au'), 1)]))
def test_kegg_parse(self): r = kegg.parse_reaction('C00013 + C00001 <=> 2 C00009') self.assertEqual( r, Reaction(Reaction.Bidir, [(Compound('C00013'), 1), (Compound('C00001'), 1)], [(Compound('C00009'), 2)]))
def test_reaction_parse_with_compartment(self): r = reaction.parse_reaction('(2) |H2| + |O2| => (2) |H2O[e]|') self.assertEqual( r, Reaction(Direction.Forward, [(Compound('H2'), 2), (Compound('O2'), 1)], [(Compound('H2O', compartment='e'), 2)]))
def test_reaction_translated_compounds(self): r = Reaction(Direction.Forward, [(Compound('Pb'), 1)], [(Compound('Au'), 1)]) rt = r.translated_compounds(lambda name: name.lower()) self.assertEqual( rt, Reaction(Direction.Forward, [(Compound('pb'), 1)], [(Compound('au'), 1)]))
def test_metnet_parse_with_numeric_id(self): r = self.parser.parse('[c] : 3pg + atp <==> 13dpg + adp') self.assertEqual( r, Reaction(Direction.Both, [(Compound('3pg', 'c'), 1), (Compound('atp', 'c'), 1)], [(Compound('13dpg', 'c'), 1), (Compound('adp', 'c'), 1)]))
def test_reaction_parse_with_decimal(self): r = reaction.parse_reaction('|H2| + (0.5) |O2| => |H2O|') self.assertEqual( r, Reaction(Direction.Forward, [(Compound('H2'), 1), (Compound('O2'), Decimal('0.5'))], [(Compound('H2O'), 1)]))
def test_sudensimple_parse_with_decimal(self): r = self.parser.parse('1 H2 + 0.5 O2 <=> 1 H2O') self.assertEqual( r, Reaction(Direction.Both, [(Compound('H2'), 1), (Compound('O2'), Decimal('0.5'))], [(Compound('H2O'), 1)]))
def test_metnet_parse_with_global_compartment(self): r = self.parser.parse('[c] : akg + ala-L <==> glu-L + pyr') self.assertEqual( r, Reaction(Direction.Both, [(Compound('akg', 'c'), 1), (Compound('ala-L', 'c'), 1)], [(Compound('glu-L', 'c'), 1), (Compound('pyr', 'c'), 1)]))
def test_sudensimple_parse_with_implicit_count(self): r = self.parser.parse('H2O + PPi <=> 2 Phosphate + 2 proton') self.assertEqual( r, Reaction(Direction.Both, [(Compound('H2O'), 1), (Compound('PPi'), 1)], [(Compound('Phosphate'), 2), (Compound('proton'), 2)]))
def test_reaction_multiply(self): r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 3}) s = 2 * r self.assertEqual(s.direction, Direction.Forward) self.assertEqual(dict(s.compounds), { Compound('A'): -2, Compound('B'): 6})
def test_reaction_negate(self): r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 1}) s = -r self.assertEqual(s.direction, Direction.Reverse) self.assertEqual(dict(s.compounds), { Compound('B'): -1, Compound('A'): 1})
def test_reaction_format_with_arguments(self): pp1 = Compound('Polyphosphate', arguments=[4]) pp2 = Compound('Polyphosphate', arguments=[5]) r = Reaction(Direction.Forward, [(Compound('ATP'), 1), (pp1, 1)], [(Compound('ADP'), 1), (pp2, 1)]) self.assertEqual( text_type(r), 'ATP + Polyphosphate(4) => ADP + Polyphosphate(5)')
def test_compound_in_compartment_when_assigned(self): c = Compound('H+', 'c') self.assertEqual(c.in_compartment('p'), Compound('H+', 'p')) self.assertIsNot(c.in_compartment('p'), c)
def test_compound_in_compartment_when_unassigned(self): c = Compound('H+') self.assertEquals(c.in_compartment('e'), Compound('H+', 'e')) self.assertIsNot(c.in_compartment('e'), c)
def test_compound_translate_name(self): c = Compound('Pb') self.assertEqual(c.translate(lambda x: x.lower()), Compound('pb')) self.assertIsNot(c.translate(lambda x: x.lower()), c)