Exemplo n.º 1
0
class TestMetabolicDatabase(unittest.TestCase):
    def setUp(self):
        self.database = DictDatabase()
        self.database.set_reaction('rxn_1', parse_reaction('=> 2 A'))
        self.database.set_reaction('rxn_2', parse_reaction('A <=> B'))
        self.database.set_reaction('rxn_3', parse_reaction('A => D[e]'))
        self.database.set_reaction('rxn_4', parse_reaction('A + 2 B => C'))
        self.database.set_reaction('rxn_5', parse_reaction('C => 3 D[e]'))
        self.database.set_reaction('rxn_6', parse_reaction('D[e] =>'))

    def test_reactions(self):
        self.assertEqual(
            set(self.database.reactions),
            {'rxn_1', 'rxn_2', 'rxn_3', 'rxn_4', 'rxn_5', 'rxn_6'})

    def test_compounds(self):
        self.assertEqual(
            set(self.database.compounds),
            {Compound('A'),
             Compound('B'),
             Compound('C'),
             Compound('D', 'e')})

    def test_compartments(self):
        self.assertEqual(set(self.database.compartments), {None, 'e'})

    def test_has_reaction_existing(self):
        self.assertTrue(self.database.has_reaction('rxn_3'))

    def test_has_reaction_not_existing(self):
        self.assertFalse(self.database.has_reaction('rxn_7'))

    def test_is_reversible_is_true(self):
        self.assertTrue(self.database.is_reversible('rxn_2'))

    def test_is_reversible_is_false(self):
        self.assertFalse(self.database.is_reversible('rxn_5'))

    def test_get_reaction_values(self):
        self.assertEqual(list(self.database.get_reaction_values('rxn_4')),
                         [(Compound('A'), -1), (Compound('B'), -2),
                          (Compound('C'), 1)])

    def test_get_compound_reactions(self):
        self.assertEqual(
            set(self.database.get_compound_reactions(Compound('A'))),
            {'rxn_1', 'rxn_2', 'rxn_3', 'rxn_4'})

    def test_reversible(self):
        self.assertEqual(set(self.database.reversible), {'rxn_2'})

    def test_get_reaction(self):
        reaction = parse_reaction('A + (2) B => C')
        self.assertEqual(self.database.get_reaction('rxn_4'), reaction)

    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_matrix_get_item(self):
        self.assertEqual(self.database.matrix[Compound('A'), 'rxn_1'], 2)
        self.assertEqual(self.database.matrix[Compound('A'), 'rxn_2'], -1)
        self.assertEqual(self.database.matrix[Compound('B'), 'rxn_2'], 1)
        self.assertEqual(self.database.matrix[Compound('A'), 'rxn_4'], -1)
        self.assertEqual(self.database.matrix[Compound('B'), 'rxn_4'], -2)
        self.assertEqual(self.database.matrix[Compound('C'), 'rxn_4'], 1)
        self.assertEqual(self.database.matrix[Compound('C'), 'rxn_5'], -1)
        self.assertEqual(self.database.matrix[Compound('D', 'e'), 'rxn_5'], 3)

    def test_matrix_get_item_invalid_key(self):
        with self.assertRaises(KeyError):
            a = self.database.matrix[Compound('A'), 'rxn_5']
        with self.assertRaises(KeyError):
            b = self.database.matrix['rxn_1']

    def test_matrix_set_item_is_invalid(self):
        with self.assertRaises(TypeError):
            self.database.matrix[Compound('A'), 'rxn_1'] = 4

    def test_matrix_iter(self):
        matrix_keys = {(Compound('A'), 'rxn_1'), (Compound('A'), 'rxn_2'),
                       (Compound('B'), 'rxn_2'), (Compound('A'), 'rxn_3'),
                       (Compound('D', 'e'), 'rxn_3'), (Compound('A'), 'rxn_4'),
                       (Compound('B'), 'rxn_4'), (Compound('C'), 'rxn_4'),
                       (Compound('C'), 'rxn_5'), (Compound('D', 'e'), 'rxn_5'),
                       (Compound('D', 'e'), 'rxn_6')}
        self.assertEqual(set(iter(self.database.matrix)), matrix_keys)

    def test_matrix_len(self):
        self.assertEqual(len(self.database.matrix), 11)
Exemplo n.º 2
0
class TestMetabolicDatabase(unittest.TestCase):
    def setUp(self):
        self.database = DictDatabase()
        self.database.set_reaction('rxn_1', parse_reaction('=> (2) |A|'))
        self.database.set_reaction('rxn_2', parse_reaction('|A| <=> |B|'))
        self.database.set_reaction('rxn_3', parse_reaction('|A| => |D[e]|'))
        self.database.set_reaction('rxn_4', parse_reaction('|A| => |C|'))
        self.database.set_reaction('rxn_5', parse_reaction('|C| => |D[e]|'))
        self.database.set_reaction('rxn_6', parse_reaction('|D[e]| =>'))

    def test_reactions(self):
        self.assertEqual(
            set(self.database.reactions),
            {'rxn_1', 'rxn_2', 'rxn_3', 'rxn_4', 'rxn_5', 'rxn_6'})

    def test_compounds(self):
        self.assertEqual(
            set(self.database.compounds),
            {Compound('A'), Compound('B'), Compound('C'), Compound('D', 'e')})

    def test_compartments(self):
        self.assertEqual(set(self.database.compartments), {None, 'e'})

    def test_has_reaction_existing(self):
        self.assertTrue(self.database.has_reaction('rxn_3'))

    def test_has_reaction_not_existing(self):
        self.assertFalse(self.database.has_reaction('rxn_7'))

    def test_is_reversible_is_true(self):
        self.assertTrue(self.database.is_reversible('rxn_2'))

    def test_is_reversible_is_false(self):
        self.assertFalse(self.database.is_reversible('rxn_5'))

    def test_get_reaction_values(self):
        self.assertEqual(set(self.database.get_reaction_values('rxn_2')),
                            { (Compound('A'), -1), (Compound('B'), 1) })

    def test_get_compound_reactions(self):
        self.assertEqual(set(self.database.get_compound_reactions(Compound('A'))),
                            { 'rxn_1', 'rxn_2', 'rxn_3', 'rxn_4' })

    def test_reversible(self):
        self.assertEqual(set(self.database.reversible), { 'rxn_2' })

    def test_get_reaction(self):
        reaction = parse_reaction('|A| => |D[e]|')
        self.assertEqual(self.database.get_reaction('rxn_3'), reaction)

    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_matrix_get_item(self):
        self.assertEqual(self.database.matrix[Compound('A'), 'rxn_1'], 2)
        self.assertEqual(self.database.matrix[Compound('A'), 'rxn_2'], -1)
        self.assertEqual(self.database.matrix[Compound('B'), 'rxn_2'], 1)
        self.assertEqual(self.database.matrix[Compound('A'), 'rxn_4'], -1)
        self.assertEqual(self.database.matrix[Compound('C'), 'rxn_4'], 1)
        self.assertEqual(self.database.matrix[Compound('C'), 'rxn_5'], -1)
        self.assertEqual(self.database.matrix[Compound('D', 'e'), 'rxn_5'], 1)

    def test_matrix_get_item_invalid_key(self):
        with self.assertRaises(KeyError):
            a = self.database.matrix[Compound('A'), 'rxn_5']
        with self.assertRaises(KeyError):
            b = self.database.matrix['rxn_1']

    def test_matrix_set_item_is_invalid(self):
        with self.assertRaises(TypeError):
            self.database.matrix[Compound('A'), 'rxn_1'] = 4

    def test_matrix_iter(self):
        matrix_keys = { (Compound('A'), 'rxn_1'),
                        (Compound('A'), 'rxn_2'),
                        (Compound('B'), 'rxn_2'),
                        (Compound('A'), 'rxn_3'),
                        (Compound('D', 'e'), 'rxn_3'),
                        (Compound('A'), 'rxn_4'),
                        (Compound('C'), 'rxn_4'),
                        (Compound('C'), 'rxn_5'),
                        (Compound('D', 'e'), 'rxn_5'),
                        (Compound('D', 'e'), 'rxn_6') }
        self.assertEqual(set(iter(self.database.matrix)), matrix_keys)

    def test_matrix_len(self):
        self.assertEqual(len(self.database.matrix), 10)