File: test_period.py

package info (click to toggle)
python-cogent 2024.5.7a1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 74,600 kB
  • sloc: python: 92,479; makefile: 117; sh: 16
file content (272 lines) | stat: -rw-r--r-- 6,923 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
from unittest import TestCase

import numpy

from numpy.testing import assert_allclose, assert_equal

from cogent3.maths.period import AutoCorrelation, Hybrid, Ipdft, ipdft
from cogent3.maths.stats.period import (
    SeqToSymbols,
    blockwise_bootstrap,
    chi_square,
    circular_indices,
    factorial,
    g_statistic,
    seq_to_symbols,
)


class TestPeriodStat(TestCase):
    def setUp(self):
        x = [
            1,
            0,
            1,
            0,
            1,
            0,
            0,
            0,
            0,
            1,
            1,
            0,
            0,
            0,
            1,
            0,
            1,
            0,
            0,
            1,
            0,
            1,
            0,
            0,
            1,
            0,
            0,
            0,
            0,
            1,
            1,
            1,
            0,
            1,
            0,
            0,
            0,
            0,
            1,
            0,
            1,
            1,
            1,
            1,
            0,
            0,
            0,
            1,
            1,
            0,
            1,
            1,
            1,
            0,
            0,
            0,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            0,
            0,
            1,
            0,
            0,
            1,
            0,
            0,
            0,
            0,
            1,
            0,
            1,
            1,
            1,
            1,
            1,
            1,
            0,
            0,
            1,
            1,
            1,
            0,
            0,
            0,
            0,
            1,
            1,
            0,
            0,
            0,
            0,
            1,
            1,
            0,
            1,
            0,
            1,
            1,
            0,
            1,
            0,
            0,
            0,
            1,
            0,
            0,
            0,
            1,
            0,
            0,
            0,
            0,
            1,
            1,
            1,
            0,
            1,
            0,
            1,
            0,
            1,
            1,
            1,
            0,
            0,
            1,
            1,
            0,
            1,
            0,
            1,
            0,
            0,
            0,
            1,
            0,
            0,
            0,
            0,
            1,
            1,
            1,
            0,
            0,
            0,
        ]
        self.x = numpy.array(x)
        self.sig = numpy.array(self.x, numpy.float64)
        self.motifs = ["AA", "TT", "TA"]

    def test_chi_square(self):
        D, cs_p_val = chi_square(self.x, 10)
        self.assertEqual(f"{D:.4f}", "0.4786")
        self.assertEqual(f"{cs_p_val:.4f}", "0.4891")

    def test_factorial(self):
        self.assertEqual(factorial(1), 1)
        self.assertEqual(factorial(4), 24)
        self.assertEqual(factorial(0), 1)

    def test_g_statitic(self):
        """calc g-stat correctly"""
        X, periods = ipdft(self.sig, llim=2, ulim=39)
        g_obs, p_val = g_statistic(X)
        assert_allclose(p_val, 0.9997, rtol=1e-3)
        assert_allclose(g_obs, 0.0577, rtol=1e-3)

    def test_circular_indices(self):
        v = list(range(10))
        self.assertEqual(circular_indices(v, 8, 10, 4), [8, 9, 0, 1])
        self.assertEqual(circular_indices(v, 9, 10, 4), [9, 0, 1, 2])
        self.assertEqual(circular_indices(v, 4, 10, 4), [4, 5, 6, 7])

    def test_seq_to_symbol(self):
        """both py and pyx seq_to_symbol versions correctly convert a sequence"""
        motifs = [b"AA", b"AT", b"TT"]
        symbols = seq_to_symbols(b"AATGGTTA", motifs, 2)
        assert_equal(symbols, numpy.array([1, 1, 0, 0, 0, 1, 0, 0]))
        symbols = seq_to_symbols(b"AAGATT", motifs, 2, numpy.zeros(6, numpy.uint8))
        assert_equal(symbols, numpy.array([1, 0, 0, 1, 1, 0]))

    def test_seq_to_symbol_factory(self):
        """checks factory function for conversion works"""
        motifs = ["AA", "AT", "TT"]
        seq_to_symbols = SeqToSymbols(motifs)
        got = seq_to_symbols("AATGGTTA")
        assert_equal(got, numpy.array([1, 1, 0, 0, 0, 1, 0, 0]))
        got = seq_to_symbols("AAGATT")
        assert_equal(got, numpy.array([1, 0, 0, 1, 1, 0], numpy.uint8))

    def test_permutation(self):
        s = (
            "ATCGTTGGGACCGGTTCAAGTTTTGGAACTCGCAAGGGGTGAATGGTCTTCGTCTAACGCTGG"
            "GGAACCCTGAATCGTTGTAACGCTGGGGTCTTTAACCGTTCTAATTTAACGCTGGGGGGTTCT"
            "AATTTTTAACCGCGGAATTGCGTC"
        )
        seq_to_symbol = SeqToSymbols(self.motifs, length=len(s))
        hybrid_calc = Hybrid(len(s), llim=2, period=4)
        ipdft_calc = Ipdft(len(s), llim=2, period=4)
        stat, p = blockwise_bootstrap(
            s, hybrid_calc, block_size=10, num_reps=1000, seq_to_symbols=seq_to_symbol
        )
        stat, p = blockwise_bootstrap(
            s, ipdft_calc, block_size=10, num_reps=1000, seq_to_symbols=seq_to_symbol
        )

    def test_permutation_all(self):
        """performs permutation test of Hybrid, but considers all stats"""
        s = (
            "ATCGTTGGGACCGGTTCAAGTTTTGGAACTCGCAAGGGGTGAATGGTCTTCGTCTAACGCTGG"
            "GGAACCCTGAATCGTTGTAACGCTGGGGTCTTTAACCGTTCTAATTTAACGCTGGGGGGTTCT"
            "AATTTTTAACCGCGGAATTGCGTC"
        )
        seq_to_symbol = SeqToSymbols(self.motifs, length=len(s))
        hybrid_calc = Hybrid(len(s), period=4, return_all=True)
        stat, p = blockwise_bootstrap(
            s, hybrid_calc, block_size=10, num_reps=1000, seq_to_symbols=seq_to_symbol
        )
        # print 's=%s; p=%s' % (stat, p)

    def test_get_num_stats(self):
        """calculators should return correct num stats"""
        hybrid_calc = Hybrid(150, llim=2, period=4)
        ipdft_calc = Ipdft(150, llim=2, period=4)
        autocorr_calc = AutoCorrelation(150, llim=2, period=4)
        self.assertEqual(hybrid_calc.get_num_stats(), 1)
        self.assertEqual(ipdft_calc.get_num_stats(), 1)
        self.assertEqual(autocorr_calc.get_num_stats(), 1)
        hybrid_calc = Hybrid(150, llim=2, period=4, return_all=True)
        self.assertEqual(hybrid_calc.get_num_stats(), 3)

    def test_permutation_skips(self):
        """permutation test correctly handles data without symbols"""
        s = "N" * 150
        seq_to_symbol = SeqToSymbols(self.motifs, length=len(s))
        ipdft_calc = Ipdft(len(s), llim=2, period=4)
        stat, p = blockwise_bootstrap(
            s,
            ipdft_calc,
            block_size=10,
            num_reps=1000,
            seq_to_symbols=seq_to_symbol,
            num_stats=1,
        )
        self.assertEqual(stat, 0.0)
        self.assertEqual(p, 1.0)