File: test_sequence_feature_vocabulary.py

package info (click to toggle)
python-skbio 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,556 kB
  • ctags: 7,222
  • sloc: python: 42,085; ansic: 670; makefile: 180; sh: 10
file content (102 lines) | stat: -rw-r--r-- 3,794 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
# ----------------------------------------------------------------------------
# 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

from skbio.io.format._sequence_feature_vocabulary import (
    _parse_loc_str, _parse_section_default, _serialize_location)
from skbio.io import FileFormatError
from skbio.metadata import IntervalMetadata


class Tests(TestCase):
    def test_parse_section_default(self):
        lines = [
            ['FOO  blah blah',
             '     blah'],
            ['FOO=blah',
             '    blah'],
            ['FOO']]
        kwargs = [{'join_delimiter': '=', 'return_label': False},
                  {'label_delimiter': '=', 'join_delimiter': '',
                   'return_label': True},
                  {'label_delimiter': '=', 'join_delimiter': '=',
                   'return_label': True}]
        expects = ['blah blah=blah',
                   ('FOO', 'blahblah'),
                   ('FOO', '')]
        for i, j, k in zip(lines, kwargs, expects):
            self.assertEqual(k, _parse_section_default(i, **j))

    def test_parse_loc_str(self):
        examples = [
            '9',  # a single base in the presented sequence
            '3..8',
            '<3..8',
            '3..>8',
            'complement(3..>8)',
            'complement(join(3..>5,<7..9))',
            'join(J00194.1:1..9,3..8)',
            'join(3..8,J00194.1:1..9)',
            '1.9',
            '1^2']

        expects = [
            ([(8, 9)], [(False, False)], {'strand': '+'}),
            ([(2, 8)], [(False, False)], {'strand': '+'}),
            ([(2, 8)], [(True, False)],  {'strand': '+'}),
            ([(2, 8)], [(False, True)],  {'strand': '+'}),
            ([(2, 8)], [(False, True)],  {'strand': '-'}),
            ([(2, 5), (6, 9)], [(False, True), (True, False)],
             {'strand': '-'}),
            ([(2, 8)], [(False, False)], {'strand': '+'}),
            ([(2, 8)], [(False, False)], {'strand': '+'}),
            ([(0, 9)], [(False, False)], {'strand': '+'}),
            ([(0, 1)], [(False, False)], {'strand': '+'})]

        for example, expect in zip(examples, expects):
            parsed = _parse_loc_str(example)
            self.assertEqual(parsed, expect)

    def test_parse_loc_str_invalid(self):
        examples = [
            'abc',
            '3-8']
        for example in examples:
            with self.assertRaisesRegex(FileFormatError,
                                        'Could not parse location string: '
                                        '"%s"' % example):
                _parse_loc_str(example)

    def test_serialize_location(self):
        imd = IntervalMetadata(9)
        i1 = imd.add([(0, 1)])
        self.assertEqual(_serialize_location(i1), '1')

        i2 = imd.add([(0, 2)], [(True, True)])
        self.assertEqual(_serialize_location(i2), '<1..>2')

        i3 = imd.add([(0, 2)], [(False, True)])
        self.assertEqual(_serialize_location(i3), '1..>2')

        i4 = imd.add([(0, 2)], [(True, False)])
        self.assertEqual(_serialize_location(i4), '<1..2')

        i5 = imd.add([(0, 2), (3, 9)], metadata={'strand': '-'})
        self.assertEqual(_serialize_location(i5),
                         'complement(join(1..2,4..9))')

        i6 = imd.add([(0, 2), (3, 9)],
                     [(True, False), (False, True)],
                     metadata={'strand': '-'})
        self.assertEqual(_serialize_location(i6),
                         'complement(join(<1..2,4..>9))')


if __name__ == '__main__':
    main()