File: test_gff3.py

package info (click to toggle)
python-skbio 0.5.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 17,492 kB
  • sloc: python: 46,829; ansic: 672; makefile: 184; javascript: 50; sh: 19
file content (348 lines) | stat: -rw-r--r-- 13,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------

from unittest import TestCase, main
import io

from skbio.util import get_data_path
from skbio.metadata import IntervalMetadata
from skbio import DNA, Sequence
from skbio.io import GFF3FormatError
from skbio.io.format.gff3 import (_yield_record,
                                  _parse_record,
                                  _parse_attr,
                                  _gff3_sniffer,
                                  _gff3_to_interval_metadata,
                                  _interval_metadata_to_gff3,
                                  _gff3_to_generator,
                                  _generator_to_gff3,
                                  _gff3_to_sequence,
                                  _sequence_to_gff3,
                                  _gff3_to_dna,
                                  _dna_to_gff3,
                                  _serialize_interval_metadata)


class GFF3IOTests(TestCase):
    def setUp(self):
        self.multi_fp = get_data_path('gff3_multi_record')
        self.single_fp = get_data_path('gff3_single_record')

        intvls = [{'bounds': [(0, 4641652)],
                   'metadata': {'source': 'European Nucleotide Archive',
                                'type': 'chromosome',
                                'score': '.',
                                'strand': '.',
                                'ID': 'chromosome:Chromosome',
                                'Alias': 'U00096.3',
                                'Is_circular': 'true'}},
                  {'bounds': [(147, 148)],
                   'metadata': {'source': 'regulondb_feature',
                                'type': 'biological_region',
                                'score': '.',
                                'strand': '+',
                                'external_name':
                                'Promoter thrLp (RegulonDB:ECK120010236)',
                                'logic_name': 'regulondb_promoter'}},
                  {'bounds': [(336, 2799)],
                   'metadata': {'source': 'Prodigal_v2.60',
                                'type': 'gene',
                                'score': '1.8',
                                'strand': '+',
                                'phase': 0,
                                'ID': '1_1',
                                'gc_cont': '0.427'}},
                  {'bounds': [(336, 2799)],
                   'metadata': {'source': 'Prodigal_v2.60',
                                'type': 'CDS',
                                'score': '333.8',
                                'strand': '+',
                                'phase': 0,
                                'ID': '1_2',
                                'Parent': '1_1',
                                'rbs_motif': 'GGAG/GAGG',
                                'rbs_spacer': '5-10bp'}},
                  {'bounds': [(0, 50), (55, 100)],
                   'metadata': {'source': 'Prodigal_v2.60',
                                'type': 'gene',
                                'score': '1.8',
                                'strand': '+',
                                'phase': 0,
                                'ID': '1_1',
                                'gene': 'FXR receptor'}}]

        self.upper_bound = 4641652
        self.imd1 = IntervalMetadata(self.upper_bound)
        self.imd1.add(**intvls[0])
        self.imd1.add(**intvls[1])

        self.imd2 = IntervalMetadata(None)
        self.imd2.add(**intvls[2])
        self.imd2.add(**intvls[3])

        self.imd3 = IntervalMetadata(None)
        self.imd3.add(**intvls[4])

        self.seq_fp = get_data_path('gff3_dna')
        self.seq = Sequence('ATGCATGCATGC',
                            metadata={'id': 'NC_1',
                                      'description': 'species X'})
        self.seq.interval_metadata.add(
            [(0, 9)],
            metadata={'source': 'Prodigal_v2.60',
                      'type': 'gene',
                      'score': '.',
                      'strand': '+',
                      'phase': 0,
                      'ID': 'gene1',
                      'Name': 'FXR'})
        self.dna = DNA(self.seq)


class SnifferTests(TestCase):
    def setUp(self):
        self.positive_fps = map(get_data_path, [
            'gff3_multi_record',
            'gff3_single_record',
            'gff3_dna'])
        self.negative_fps = map(get_data_path, [
            'empty',
            'whitespace_only',
            'gff3_bad_missing_directive'])

    def test_positive(self):
        for fp in self.positive_fps:
            self.assertEqual(_gff3_sniffer(fp), (True, {}))

    def test_negative(self):
        for fp in self.negative_fps:
            self.assertEqual(_gff3_sniffer(fp), (False, {}))


class ReaderTests(GFF3IOTests):
    def test_parse_attr(self):
        s = 'Dbxref=GO:000152,GO:001234;Note=fooo'
        obs = _parse_attr(s)
        exp = {'db_xref': 'GO:000152,GO:001234', 'note': 'fooo'}
        self.assertEqual(exp, obs)

    def test_yield_record(self):
        obs = [('data', 'seqid1', ['seqid1\txxx', 'seqid1\tyyy']),
               ('data', 'seqid2', ['seqid2\tzzz'])]
        s = ('seqid1\txxx\n'
             'seqid1\tyyy\n'
             'seqid2\tzzz\n')
        fh = io.StringIO(s)
        for i, j in zip(_yield_record(fh), obs):
            self.assertEqual(i, j)

    def test_parse_record_raise(self):
        chars = 'abc?!'
        for char in chars:
            lines = [
                'ctg123\t.\tgene\t1000\t9000\t.\t+\t%s\tID=gene00001' % char]
            with self.assertRaisesRegex(
                    GFF3FormatError,
                    r"unknown value for phase column: '%s'" % char):
                _parse_record(lines, 10000)

    def test_yield_record_raise(self):
        s = '##gff-version   3\nseq_1 . gene 1 3 . + . ID=gene01\n'
        with io.StringIO(s) as fh:
            with self.assertRaises(GFF3FormatError):
                list(_yield_record(fh))

    def test_gff3_to_interval_metadata(self):
        obs = _gff3_to_interval_metadata(
            self.single_fp, seq_id='Chromosome')

        self.assertEqual(obs, self.imd1)

    def test_gff3_to_interval_metadata_empty(self):
        exp = IntervalMetadata(None)
        obs = _gff3_to_interval_metadata(
            # the seq id does not exist
            self.single_fp, seq_id='foo')
        self.assertEqual(obs, exp)

    def test_gff3_to_interval_metadata_bad(self):
        with self.assertRaisesRegex(GFF3FormatError,
                                    r'do not have 9 columns in this line'):
            _gff3_to_interval_metadata(
                get_data_path('gff3_bad_wrong_columns'),
                seq_id='Chromosome')

    def test_gff3_to_generator(self):
        exps = [('Chromosome', self.imd1),
                ('gi|556503834|ref|NC_000913.3|', self.imd2)]
        obss = _gff3_to_generator(self.multi_fp)
        for obs, exp in zip(obss, exps):
            self.assertEqual(obs, exp)

    def test_gff3_to_generator_empty(self):
        empty_fps = map(get_data_path, ['empty', 'whitespace_only'])
        for empty_fp in empty_fps:
            obs = list(_gff3_to_generator(empty_fp))
            self.assertEqual(obs, [])

    def test_gff3_to_sequence(self):
        obs = _gff3_to_sequence(self.seq_fp)
        self.assertEqual(obs, self.seq)

    def test_gff3_to_dna(self):
        obs = _gff3_to_dna(self.seq_fp)
        self.assertEqual(obs, self.dna)


class WriterTests(GFF3IOTests):
    def test_interval_metadata_to_gff3(self):
        with io.StringIO() as fh:
            _interval_metadata_to_gff3(self.imd1, fh, seq_id='Chromosome')
            # only compare the uncommented lines because the comments are not
            # stored in IntervalMetadata
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]

        with open(self.single_fp) as f:
            exp = [i.rstrip() for i in f.readlines() if not i.startswith('#')]

        self.assertEqual(obs, exp)

    def test_interval_metadata_to_gff3_missing_field(self):
        exp = 'ctg123\t.\tgene\t1\t9\t.\t.\t.\tID=gene00001;Name=EDEN'
        imd = IntervalMetadata(9)
        imd.add([(0, 9)], metadata={
            'type': 'gene', 'ID': 'gene00001', 'Name': 'EDEN'})
        with io.StringIO() as fh:
            _interval_metadata_to_gff3(imd, fh, seq_id='ctg123')
            # only compare the uncommented lines because the comments are not
            # stored in IntervalMetadata
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]

        self.assertEqual([exp], obs)

    def test_interval_metadata_to_gff3_escape(self):
        # test escape of reserved char in GFF3
        exp = 'ctg123\t.\tgene\t1\t9\t.\t.\t.\tID=a%3B%3D%26%2Cb'
        imd = IntervalMetadata(9)
        imd.add([(0, 9)], metadata={
            'type': 'gene', 'ID': 'a;=&,b'})
        with io.StringIO() as fh:
            _interval_metadata_to_gff3(imd, fh, seq_id='ctg123')
            # only compare the uncommented lines because the comments are not
            # stored in IntervalMetadata
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]

        self.assertEqual([exp], obs)

    def test_interval_metadata_to_gff3_multiple_values(self):
        # test multiple values of db_xref are correctly serialized
        exp = 'ctg123\t.\tgene\t1\t9\t.\t.\t.\tDbxref=GO:000152,GO:001234'
        imd = IntervalMetadata(9)
        imd.add([(0, 9)], metadata={
            'type': 'gene', 'db_xref': ['GO:000152', 'GO:001234']})
        with io.StringIO() as fh:
            _interval_metadata_to_gff3(imd, fh, seq_id='ctg123')
            # only compare the uncommented lines because the comments are not
            # stored in IntervalMetadata
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]

        self.assertEqual([exp], obs)

    def test_interval_metadata_to_gff3_empty(self):
        imd = IntervalMetadata(None)
        with io.StringIO() as fh:
            _interval_metadata_to_gff3(imd, fh, seq_id='foo')
            obs = fh.getvalue()
        self.assertEqual(obs, '##gff-version 3\n')

    def test_interval_metadata_to_gff3_sub_region(self):
        seq_id = 'NC 7'
        with open(self.multi_fp) as f:
            exp = [i.strip() for i in f if i.startswith(seq_id)]

        with io.StringIO() as fh:
            _serialize_interval_metadata(
                self.imd3, seq_id=seq_id, fh=fh, skip_subregion=False)
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]
        self.assertEqual(exp, obs)

        with io.StringIO() as fh:
            _serialize_interval_metadata(self.imd3, seq_id=seq_id, fh=fh)
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]
        # all the rest lines except the 1st are sub-region lines, so only
        # compare the first line from exp
        self.assertEqual(exp[:1], obs)

    def test_sequence_to_gff3(self):
        with io.StringIO() as fh:
            _sequence_to_gff3(self.seq, fh)
            obs = fh.getvalue()

        with open(self.seq_fp) as fh:
            exp = fh.read()

        self.assertEqual(exp, obs)

    def test_dna_to_gff3(self):
        with io.StringIO() as fh:
            _dna_to_gff3(self.dna, fh)
            obs = fh.getvalue()

        with open(self.seq_fp) as fh:
            exp = fh.read()

        self.assertEqual(exp, obs)

    def test_raise_subregion(self):
        im = IntervalMetadata(None)
        im.add([(0, 3), (7, 9)], metadata={'type': 'gene'})
        with io.StringIO() as fh:
            with self.assertRaises(GFF3FormatError):
                _serialize_interval_metadata(
                    im, seq_id='a', fh=fh, skip_subregion=False)


class RoundtripTests(GFF3IOTests):
    def test_roundtrip_interval_metadata(self):
        ''''''
        with io.StringIO() as fh:
            _interval_metadata_to_gff3(
                _gff3_to_interval_metadata(
                    self.single_fp,
                    seq_id='Chromosome'),
                fh,
                seq_id='Chromosome')
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]

        with open(self.single_fp) as f:
            exp = [i.rstrip() for i in f.readlines() if not i.startswith('#')]

        self.assertEqual(obs, exp)

    def test_roundtrip_interval_metadata_generator(self):
        with io.StringIO() as fh:
            _generator_to_gff3(
                _gff3_to_generator(self.multi_fp), fh, skip_subregion=False)
            obs = [i for i in fh.getvalue().splitlines()
                   if not i.startswith('#')]

        with open(self.multi_fp) as f:
            exp = [i.rstrip() for i in f.readlines() if not i.startswith('#')]

        self.assertEqual(obs, exp)


if __name__ == '__main__':
    main()