[9b26b7]: / third_party / nucleus / util / utils_test.py

Download this file

169 lines (152 with data), 6.7 kB

  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
# Copyright 2018 Google LLC.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Tests for third_party.nucleus.util.utils."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
from absl.testing import absltest
from absl.testing import parameterized
import numpy as np
import numpy.testing as npt
from third_party.nucleus.testing import test_utils
from third_party.nucleus.util import ranges
from third_party.nucleus.util import utils
class UtilsTest(parameterized.TestCase):
def test_read_range(self):
"""Tests reads have their ranges calculated correctly."""
start = 10000001
read = test_utils.make_read(
'AAACAG',
chrom='chrX',
start=start,
cigar='2M1I3M',
quals=range(10, 16),
name='read1')
self.assertEqual(
ranges.make_range('chrX', start, start + 5), utils.read_range(read))
read = test_utils.make_read(
'AAACAG',
chrom='chrX',
start=start,
cigar='2M16D3M',
quals=range(10, 16),
name='read1')
self.assertEqual(
ranges.make_range('chrX', start, start + 5 + 16),
utils.read_range(read))
def test_read_end(self):
"""Tests reads have their ends calculated correctly."""
start = 10000001
read = test_utils.make_read(
'AAACAG',
chrom='chrX',
start=start,
cigar='2M1I3M',
quals=range(10, 16),
name='read1')
self.assertEqual(
start + 5, utils.read_end(read))
read = test_utils.make_read(
'AAACAG',
chrom='chrX',
start=start,
cigar='2M16D3M',
quals=range(10, 16),
name='read1')
self.assertEqual(
start + 5 + 16,
utils.read_end(read))
def test_reservoir_sample_length(self):
"""Tests samples have expected length."""
first_ten_ints = range(10)
# Test sampling with k > len(iterable).
self.assertEqual(len(utils.reservoir_sample(first_ten_ints, 11)), 10)
# Test sampling with k == len(iterable).
self.assertEqual(len(utils.reservoir_sample(first_ten_ints, 10)), 10)
# Test sampling with k < len(iterable).
self.assertEqual(len(utils.reservoir_sample(first_ten_ints, 9)), 9)
# Test sampling with k == 0.
self.assertEqual(len(utils.reservoir_sample(first_ten_ints, 0)), 0)
# Test sampling with k < 0 (bad args).
with self.assertRaises(ValueError):
utils.reservoir_sample(first_ten_ints, -1)
@parameterized.parameters(
(10, 0),
(1, 1),
(10, 1),
(1, 3),
(3, 3),
(6, 3),
(10, 3),
)
def test_reservoir_sample_frequency(self, iterable_size, k):
"""Tests observed frequency is close to expected frequency."""
# Use a fixed random number so our test is deterministic.
random = np.random.RandomState(123456789)
n_replicates = 100000
counts = collections.Counter(
item
for _ in range(n_replicates)
for item in utils.reservoir_sample(range(iterable_size), k, random))
expected_frequency = min(k / float(iterable_size), 1.0)
for c in counts.values():
observed_frequency = c / float(n_replicates)
npt.assert_allclose(observed_frequency, expected_frequency, atol=0.01)
@parameterized.parameters(
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=4, e2=10, expected=False),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=3, e2=10, expected=False),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=2, e2=10, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=1, e2=10, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=0, e2=10, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=0, e2=1, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=0, e2=2, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=0, e2=3, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=1, e2=2, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=1, e2=3, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=2, e2=3, expected=True),
# dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=3, e2=3, expected=False),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=0, e2=4, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr1', s2=1, e2=4, expected=True),
dict(ref1='chr1', s1=0, e1=3, ref2='chr2', s2=1, e2=4, expected=False),
)
def test_read_overlaps_region(self, ref1, s1, e1, ref2, s2, e2, expected):
def check_overlaps(chr1, start1, end1, chr2, start2, end2, expected):
nbp = end1 - start1
read = test_utils.make_read(
'A' * nbp, chrom=chr1, start=start1, cigar='{}M'.format(nbp))
region = ranges.make_range(chr2, start2, end2)
self.assertEqual(utils.read_overlaps_region(read, region), expected)
# This check ensures we get the same result calling ranges.ranges_overlap.
self.assertEqual(
ranges.ranges_overlap(region, utils.read_range(read)), expected)
check_overlaps(ref1, s1, e1, ref2, s2, e2, expected)
check_overlaps(ref2, s2, e2, ref1, s1, e1, expected)
if __name__ == '__main__':
absltest.main()