Beispiel #1
0
    def test_str(self):
        base = core.Base()

        base.id = 'dAMP'
        self.assertEqual(str(base), '[id: "dAMP"]')

        base.name = 'deoxyadenosine monophosphate'
        self.assertEqual(str(base), '[id: "dAMP" | name: "deoxyadenosine monophosphate"]')

        base.synonyms = set(['A', 'dAMP'])
        self.assertIn(' | synonym: "A"', str(base))
        self.assertIn(' | synonym: "dAMP"', str(base))

        base.identifiers = set([core.Identifier('chebi', 'CHEBI:58245'), core.Identifier('biocyc.compound', 'DAMP')])
        self.assertIn(' | identifier: chebi/CHEBI:58245', str(base))
        self.assertIn(' | identifier: biocyc.compound/DAMP', str(base))

        base.structure = dAMP_inchi
        self.assertIn(' | structure: {}]'.format(dAMP_inchi), str(base))

        base.delta_mass = 1.
        base.delta_charge = -1
        self.assertIn(' | delta-mass: 1', str(base))
        self.assertIn(' | delta-charge: -1', str(base))

        base.start_position = 3
        self.assertIn(' | position: 3-]', str(base))
        base.end_position = 5
        self.assertIn(' | position: 3-5]', str(base))
        base.start_position = None
        self.assertIn(' | position: -5]', str(base))

        base.comments = 'help "me"'
        self.assertIn(' | comments: "help \\"me\\""', str(base))
Beispiel #2
0
    def test_set(self):
        id_1 = core.Identifier('ec-code', '1.1.1.2')
        id_2 = core.Identifier('ec-code', '1.1.1.2')
        id_3 = core.Identifier('ec-code', '1.1.1.1')
        id_4 = core.Identifier('kegg.compound', '1.1.1.2')

        ids = set([id_1, id_2, id_3, id_4])
        self.assertEqual(len(ids), 3)
Beispiel #3
0
    def test_hash(self):
        id_1 = core.Identifier('ec-code', '1.1.1.2')
        id_2 = core.Identifier('ec-code', '1.1.1.2')
        id_3 = core.Identifier('ec-code', '1.1.1.1')
        id_4 = core.Identifier('kegg.compound', '1.1.1.2')

        self.assertEqual(id_1.__hash__(), id_2.__hash__())
        self.assertNotEqual(id_1.__hash__(), id_3.__hash__())
        self.assertNotEqual(id_1.__hash__(), id_4.__hash__())
Beispiel #4
0
 def test_identifiers_setter(self):
     base = core.Base()
     base.identifiers = core.IdentifierSet()
     base.identifiers = set([core.Identifier('ns', 'id')])
     base.identifiers = [core.Identifier('ns', 'id')]
     with self.assertRaises(ValueError):
         base.identifiers = None
     with self.assertRaises(ValueError):
         base.identifiers = 'A'
     with self.assertRaises(TypeError):
         base.identifiers = core.Identifier('ns', 'id')
Beispiel #5
0
    def test_update(self):
        id_1 = core.Identifier('ec-code', '1.1.1.2')
        id_2 = core.Identifier('ec-code', '1.1.1.1')
        id_3 = core.Identifier('kegg.compound', '1.1.1.2')

        ids = core.IdentifierSet()
        ids.update([id_1, id_2])
        self.assertEqual(len(ids), 2)

        with self.assertRaises(ValueError):
            ids.update([('ec-code', '1.1.1.2')])
Beispiel #6
0
    def test_eq(self):
        id_1 = core.Identifier('ec-code', '1.1.1.2')
        id_2 = core.Identifier('ec-code', '1.1.1.2')
        id_3 = core.Identifier('ec-code', '1.1.1.1')
        id_4 = core.Identifier('kegg.compound', '1.1.1.2')

        self.assertEqual(id_1, id_2)
        self.assertEqual(id_2, id_1)
        self.assertNotEqual(id_1, id_3)
        self.assertNotEqual(id_1, id_4)
        self.assertNotEqual(id_1, '')
        self.assertNotEqual(id_1, 0)
Beispiel #7
0
    def test_constructor(self):
        id_1 = core.Identifier('ec-code', '1.1.1.2')
        id_2 = core.Identifier('ec-code', '1.1.1.1')
        id_3 = core.Identifier('kegg.compound', '1.1.1.2')

        ids = core.IdentifierSet()
        self.assertEqual(len(ids), 0)

        ids = core.IdentifierSet([id_1, id_2, id_3])
        self.assertEqual(len(ids), 3)

        with self.assertRaises(ValueError):
            core.IdentifierSet(['ec-code'])
Beispiel #8
0
    def test_add(self):
        id_1 = core.Identifier('ec-code', '1.1.1.2')
        id_2 = core.Identifier('ec-code', '1.1.1.1')
        id_3 = core.Identifier('kegg.compound', '1.1.1.2')

        ids = core.IdentifierSet()
        ids.add(id_1)
        ids.add(id_2)
        ids.add(id_3)
        self.assertEqual(len(ids), 3)

        with self.assertRaises(ValueError):
            ids.add(('ec-code', '1.1.1.2'))
Beispiel #9
0
    def test_symmetric_difference_update(self):
        id_1 = core.Identifier('ec-code', '1.1.1.2')
        id_2 = core.Identifier('ec-code', '1.1.1.1')
        id_3 = core.Identifier('kegg.compound', '1.1.1.2')

        ids_1 = core.IdentifierSet([id_1, id_2])
        ids_2 = core.IdentifierSet([id_1, id_3])
        ids_1.symmetric_difference_update(ids_2)
        self.assertEqual(ids_1, core.IdentifierSet([id_2, id_3]))

        ids_1 = core.IdentifierSet([id_1, id_2])
        ids_2 = set([id_1, id_3])
        ids_1.symmetric_difference_update(ids_2)
        self.assertEqual(ids_1, core.IdentifierSet([id_2, id_3]))

        with self.assertRaises(TypeError):
            ids_1.symmetric_difference_update(id_1)
Beispiel #10
0
 def test_init(self):
     identifiers = set([
         core.Identifier('chebi', 'CHEBI:58245'),
         core.Identifier('pubchem.compound', '22848660'),
         core.Identifier('metacyc.compound', 'DAMP'),
     ])
     synonyms = set(['A', 'dAMP', 'deoxyadenosine monophosphate'])
     base = core.Base(id='dAMP', name='deoxyadenosine monophosphate', synonyms=synonyms, identifiers=identifiers,
                      structure=dAMP_inchi, delta_mass=1., delta_charge=-1, start_position=2, end_position=10, comments='Long string')
     self.assertEqual(base.id, 'dAMP')
     self.assertEqual(base.name, 'deoxyadenosine monophosphate')
     self.assertEqual(base.synonyms, synonyms)
     self.assertEqual(base.identifiers, identifiers)
     self.assertEqual(base.get_inchi(), dAMP_inchi)
     self.assertEqual(base.delta_mass, 1.)
     self.assertEqual(base.delta_charge, -1)
     self.assertEqual(base.start_position, 2)
     self.assertEqual(base.end_position, 10)
     self.assertEqual(base.comments, 'Long string')
Beispiel #11
0
    def test_constructor(self):
        id = core.Identifier('ec-code', '1.1.1.2')
        self.assertEqual(id.ns, 'ec-code')
        self.assertEqual(id.id, '1.1.1.2')

        id = core.Identifier('kegg.compound', 'C00001')
        self.assertEqual(id.ns, 'kegg.compound')
        self.assertEqual(id.id, 'C00001')

        id = core.Identifier('chebi', 'CHEBI:57566')
        self.assertEqual(id.ns, 'chebi')
        self.assertEqual(id.id, 'CHEBI:57566')

        id = core.Identifier('metacyc.compound', 'DAMP')
        self.assertEqual(id.ns, 'metacyc.compound')
        self.assertEqual(id.id, 'DAMP')

        id = core.Identifier('pubchem.compound', '22848660')
        self.assertEqual(id.ns, 'pubchem.compound')
        self.assertEqual(id.id, '22848660')

        with self.assertRaises(ValueError):
            id.ns = ''
        with self.assertRaises(ValueError):
            id.ns = '"pubchem"'
        with self.assertRaises(ValueError):
            id.ns = 0
        with self.assertRaises(ValueError):
            id.ns = None
        with self.assertRaises(ValueError):
            id.id = ''
        with self.assertRaises(ValueError):
            id.id = 0
        with self.assertRaises(ValueError):
            id.id = None
        with self.assertRaises(ValueError):
            id.id = '"22848660"'
        self.assertEqual(id.ns, 'pubchem.compound')
        self.assertEqual(id.id, '22848660')
Beispiel #12
0
    def test_from_str(self):
        self.assertTrue(dna.DnaForm.from_str('AAA').is_equal(dna.DnaForm([
            dna.dna_alphabet.A, dna.dna_alphabet.A, dna.dna_alphabet.A,
        ])))

        self.assertTrue(dna.DnaForm.from_str('ACTG').is_equal(dna.DnaForm([
            dna.dna_alphabet.A, dna.dna_alphabet.C, dna.dna_alphabet.T, dna.dna_alphabet.G,
        ])))

        with self.assertRaisesRegex(ValueError, 'not in alphabet'):
            self.assertTrue(dna.DnaForm.from_str('UAA').is_equal(dna.DnaForm([
                dna.dna_alphabet.A, dna.dna_alphabet.A, dna.dna_alphabet.A,
            ])))

        self.assertTrue(dna.DnaForm.from_str(
            'AA[id: "dI"'
            + ' | name: "2\'-deoxyinosine"'
            + ' | synonym: "2\'-deoxyinosine, 9-[(2R,4S,5R)-4-hydroxy-5-(hydroxymethyl)tetrahydrofuran-2-yl]-9H-purin-6-ol"'
            + ' | identifier: chebi/CHEBI:28997'
            + ' | structure: ' + dIMP_inchi
            + ' | delta-mass: -2.5'
            + ' | delta-charge: 3'
            + ' | position: 3-5'
            + ' | comments: "A purine 2\'-deoxyribonucleoside that is inosine ..."]A').is_equal(dna.DnaForm([
                dna.dna_alphabet.A,
                dna.dna_alphabet.A,
                core.Base(
                    id='dI',
                    name="2'-deoxyinosine",
                    synonyms=core.SynonymSet(
                        ["2'-deoxyinosine, 9-[(2R,4S,5R)-4-hydroxy-5-(hydroxymethyl)tetrahydrofuran-2-yl]-9H-purin-6-ol"]),
                    identifiers=core.IdentifierSet([core.Identifier('chebi', 'CHEBI:28997')]),
                    structure=dIMP_inchi,
                    delta_mass=-2.5,
                    delta_charge=3,
                    start_position=3,
                    end_position=5,
                    comments="A purine 2'-deoxyribonucleoside that is inosine ...",
                ),
                dna.dna_alphabet.A,
            ])))

        self.assertTrue(dna.DnaForm.from_str(
            'AA[id: "dI"'
            ' | position: 3-]A').is_equal(dna.DnaForm([
                dna.dna_alphabet.A,
                dna.dna_alphabet.A,
                core.Base(
                    id='dI',
                    start_position=3,
                    end_position=None,
                ),
                dna.dna_alphabet.A,
            ])))

        self.assertTrue(dna.DnaForm.from_str(
            'AA[id: "dI"'
            ' | position: -5]A').is_equal(dna.DnaForm([
                dna.dna_alphabet.A,
                dna.dna_alphabet.A,
                core.Base(
                    id='dI',
                    start_position=None,
                    end_position=5,
                ),
                dna.dna_alphabet.A,
            ])))

        with self.assertRaisesRegex(ValueError, 'cannot be repeated'):
            dna.DnaForm.from_str(
                'AA[id: "dI"'
                ' | name: "2\'-deoxyinosine"'
                ' | name: "2\'-deoxyinosine"]A')