Example #1
0
 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)]))
Example #2
0
 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)]))
Example #3
0
 def test_reaction_equals_other(self):
     r = Reaction(Direction.Forward, [(Compound('Pb'), 1)],
                  [(Compound('Au'), 1)])
     self.assertEqual(
         r,
         Reaction(Direction.Forward, [(Compound('Pb'), 1)],
                  [(Compound('Au'), 1)]))
Example #4
0
 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)]))
Example #5
0
 def test_reaction_not_equals_other_with_different_direction(self):
     r = Reaction(Direction.Forward, [(Compound('Au'), 1)],
                  [(Compound('Pb'), 1)])
     self.assertNotEqual(
         r,
         Reaction(Direction.Reverse, [(Compound('Pb'), 1)],
                  [(Compound('Au'), 1)]))
Example #6
0
    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]'
                         }])
Example #7
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)]))
Example #8
0
 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)]))
Example #9
0
    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)
Example #10
0
 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)]))
Example #11
0
 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)]))
Example #12
0
 def test_reaction_minus_reaction(self):
     r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 1})
     s = Reaction(Direction.Reverse, {Compound('C'): -1, Compound('B'): 1})
     t = r - s
     self.assertEqual(t.direction, Direction.Forward)
     self.assertEqual(dict(t.compounds), {
         Compound('A'): -1,
         Compound('C'): 1})
Example #13
0
 def test_reaction_plus_reaction_forward(self):
     r = Reaction(Direction.Forward, {Compound('A'): -1, Compound('B'): 1})
     s = Reaction(Direction.Forward, {Compound('B'): -1, Compound('C'): 1})
     t = r + s
     self.assertEqual(t.direction, Direction.Forward)
     self.assertEqual(dict(t.compounds), {
         Compound('A'): -1,
         Compound('C'): 1})
Example #14
0
 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)')
Example #15
0
 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))
Example #16
0
    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)
Example #17
0
 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')))
Example #18
0
 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)|')
Example #19
0
 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_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)]))
Example #21
0
 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_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_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_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_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_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)]))
Example #27
0
 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})
Example #28
0
 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})
Example #29
0
 def test_kegg_parse_with_compound_argument(self):
     r = kegg.parse_reaction('C00039(n) <=> C00013 + C00039(n+1)')
     self.assertEqual(
         r,
         Reaction(Reaction.Bidir,
                  [(Compound('C00039', arguments=[Expression('n')]), 1)],
                  [(Compound('C00013'), 1),
                   (Compound('C00039', arguments=[Expression('n+1')]), 1)]))
 def test_reaction_parse(self):
     r = reaction.parse_reaction(
         '|H2O| + |PPi| => (2) |Phosphate| + (2) |H+|')
     self.assertEqual(
         r,
         Reaction(Direction.Forward, [(Compound('H2O'), 1),
                                      (Compound('PPi'), 1)],
                  [(Compound('Phosphate'), 2), (Compound('H+'), 2)]))
Example #31
0
 def test_reaction_init_from_dict(self):
     compounds = {
         Compound('A'): -2,
         Compound('B'): -3,
         Compound('C'): 1
     }
     r = Reaction(Direction.Forward, compounds)
     self.assertEqual(dict(r.compounds), compounds)