File: sequences_test.py

package info (click to toggle)
paleomix 1.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,156 kB
  • sloc: python: 21,423; sh: 182; makefile: 37
file content (146 lines) | stat: -rw-r--r-- 4,182 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python3
#
# Copyright (c) 2012 Mikkel Schubert <MikkelSch@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import itertools

import pytest

from paleomix.common.sequences import (
    complement,
    reverse_complement,
    encode_genotype,
    split,
)


###############################################################################
###############################################################################
# Tests for 'complement'

_REF_SRC = "ACGTMRWSYKVHDBNX"
_REF_DST = "TGCAKYWSRMBDHVNX"


@pytest.mark.parametrize("src, dst", zip(_REF_SRC, _REF_DST))
def test_complement__single_nt(src, dst):
    assert complement(src) == dst
    assert complement(src.lower()) == dst.lower()


def test_complement__multiple_nts_upper():
    assert complement(_REF_SRC) == _REF_DST


def test_complement__multiple_nts_lower():
    assert complement(_REF_SRC.lower()) == _REF_DST.lower()


def test_complement__multiple_nts_mixed_case():
    assert complement("aGtCn") == "tCaGn"


###############################################################################
###############################################################################
# Tests for 'complement'


def test_reverse_complement():
    assert reverse_complement(_REF_SRC) == _REF_DST[::-1]


###############################################################################
###############################################################################
# Tests for 'encode_genotype'

_IUB_SRC = (
    "A",
    "C",
    "G",
    "T",
    "AC",
    "AG",
    "AT",
    "CG",
    "CT",
    "GT",
    "ACG",
    "ACT",
    "AGT",
    "CGT",
    "ACGT",
)
_IUB_DST = "ACGTMRWSYKVHDB"


@pytest.mark.parametrize("src, dst", zip(_IUB_SRC, _IUB_DST))
def test_genotype__permutations(src, dst):
    for seq in itertools.permutations(src):
        assert encode_genotype("".join(src)) == dst


@pytest.mark.parametrize("value", ("a", "At", "Z", "+"))
def test_genotype__bad_input(value):
    with pytest.raises(ValueError):
        encode_genotype(value)


@pytest.mark.parametrize("sequence", ("CT", "C,T", ",C,T", "C,T,", ",C,T,"))
def test_comma_or_not(sequence):
    assert encode_genotype(sequence) == "Y"


###############################################################################
###############################################################################
# Tests for 'split'


def test_split__empty_sequence():
    assert split("") == {"1": "", "2": "", "3": ""}


def test_split__no_split_by():
    with pytest.raises(ValueError, match="No split_by specified"):
        split("", split_by="")


def test_split__single_group():
    assert split("ACGCAT", "111") == {"1": "ACGCAT"}


def test_split__two_groups():
    assert split("ACGCAT", "112") == {"1": "ACCA", "2": "GT"}


def test_split__three_groups():
    expected = {"1": "AC", "2": "CA", "3": "GT"}
    assert split("ACGCAT", "123") == expected
    assert split("ACGCAT") == expected


def test_split__empty_group():
    expected = {"1": "A", "2": "C", "3": ""}
    assert split("AC") == expected


def test_split__partial_group():
    expected = {"1": "AA", "2": "CA", "3": "G"}
    assert split("ACGAA") == expected