[0473b3]: / tests / test_genomicarray.py

Download this file

284 lines (225 with data), 13.1 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
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
import os
import numpy as np
import pytest
from pybedtools import Interval
from janggu.data import GenomicIndexer
from janggu.data import create_genomic_array
from janggu.data.genomicarray import get_collapser
from janggu.data.genomicarray import get_normalizer
def test_get_collapser():
with pytest.raises(Exception):
# this collapser is not available
get_collapser('blabla')
def test_get_normalizer():
with pytest.raises(Exception):
# this normalizer is not available
get_normalizer('blabla')
def test_invalid_storage():
with pytest.raises(Exception):
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=True,
typecode='int8',
storage='storgae', resolution=1, cache=False)
def test_resolution_negative():
with pytest.raises(Exception):
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=True,
typecode='int8',
storage='ndarray', cache=False,
resolution=-1)
def test_hdf5_no_cache():
with pytest.raises(Exception):
# cache must be True
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}),
stranded=True, typecode='int8',
storage='hdf5', cache=None)
def test_invalid_access():
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=False,
typecode='int8',
storage='ndarray')
with pytest.raises(Exception):
# access only via genomic interval
ga[1]
with pytest.raises(Exception):
# access only via genomic interval and condition
ga[1] = 1
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=False,
typecode='int8',
storage='sparse')
with pytest.raises(Exception):
# access only via genomic interval
ga[1]
with pytest.raises(Exception):
# access only via genomic interval and condition
ga[1] = 1
def test_bwga_instance_unstranded_taged(tmpdir):
os.environ['JANGGU_OUTPUT'] = tmpdir.strpath
iv = Interval('chr10', 100, 120, strand='.')
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=False, typecode='int8',
storage='ndarray', datatags='test_bwga_instance_unstranded')
with pytest.raises(Exception):
# access only via genomic interval
ga[1]
with pytest.raises(Exception):
# access only via genomic interval and condition
ga[1] = 1
np.testing.assert_equal(ga[iv].shape, (20, 1, 1))
np.testing.assert_equal(ga[iv], np.zeros((20, 1, 1)))
ga[iv, 0] = np.ones((20,1))
np.testing.assert_equal(ga[iv], np.ones((20, 1, 1)))
np.testing.assert_equal(ga[iv].sum(), 20)
iv = Interval('chr10', 0, 300, strand='.')
np.testing.assert_equal(ga[iv].sum(), 20)
def test_bwga_instance_unstranded(tmpdir):
iv = Interval('chr10', 100, 120, strand='.')
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=False, typecode='int8',
storage='ndarray', cache=False)
np.testing.assert_equal(ga[iv].shape, (20, 1, 1))
np.testing.assert_equal(ga[iv], np.zeros((20, 1, 1)))
ga[iv, 0] = np.ones((20,1))
np.testing.assert_equal(ga[iv], np.ones((20, 1, 1)))
np.testing.assert_equal(ga[iv].sum(), 20)
iv = Interval('chr10', 0, 300, strand='.')
np.testing.assert_equal(ga[iv].sum(), 20)
def test_bwga_instance_stranded(tmpdir):
os.environ['JANGGU_OUTPUT'] = tmpdir.strpath
iv = Interval('chr10', 100, 120, strand='+')
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=True, typecode='int8',
storage='ndarray')
np.testing.assert_equal(ga[iv].shape, (20, 2, 1))
np.testing.assert_equal(ga[iv], np.zeros((20, 2, 1)))
x = np.zeros((20, 2, 1))
x[:, :1, :] = 1
ga[iv, 0] = x[:,:,0]
np.testing.assert_equal(ga[iv], x)
np.testing.assert_equal(ga[iv].sum(), 20)
iv = Interval('chr10', 0, 300)
np.testing.assert_equal(ga[iv].sum(), 20)
def test_bwga_instance_stranded_notcached(tmpdir):
iv = Interval('chr10', 100, 120, strand='+')
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr10': 300}), stranded=True, typecode='int8',
storage='ndarray', cache=False)
np.testing.assert_equal(ga[iv].shape, (20, 2, 1))
np.testing.assert_equal(ga[iv], np.zeros((20, 2, 1)))
x = np.zeros((20, 2, 1))
x[:, :1, :] = 1
ga[iv, 0] = x[:,:,0]
np.testing.assert_equal(ga[iv], x)
np.testing.assert_equal(ga[iv].sum(), 20)
iv = Interval('chr10', 0, 300)
np.testing.assert_equal(ga[iv].sum(), 20)
def test_zscore_normalization(tmpdir):
os.environ['JANGGU_OUTPUT'] = tmpdir.strpath
def loading(garray):
garray[Interval('chr1', 0, 150), 0] = np.repeat(1, 150).reshape(-1,1)
garray[Interval('chr2', 0, 300), 0] = np.repeat(-1, 300).reshape(-1,1)
return garray
for store in ['ndarray', 'hdf5']:
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage=store, cache=True, loader=loading,
normalizer=['zscore'])
np.testing.assert_allclose(ga.weighted_mean(), np.asarray([0.0]),
rtol=1e-5, atol=1e-5)
np.testing.assert_allclose(ga.weighted_sd(), np.asarray([1.]),
rtol=1e-5, atol=1e-5)
np.testing.assert_allclose(ga[Interval('chr1', 100, 101)],
np.asarray([[[1.412641340027806]]]),
rtol=1e-5, atol=1e-5)
np.testing.assert_allclose(ga[Interval('chr2', 100, 101)],
np.asarray([[[-0.706320670013903]]]),
rtol=1e-5, atol=1e-5)
def test_logzscore_normalization(tmpdir):
os.environ['JANGGU_OUTPUT'] = tmpdir.strpath
def loading(garray):
garray[Interval('chr1', 0, 150), 0] = np.repeat(10, 150).reshape(-1, 1)
garray[Interval('chr2', 0, 300), 0] = np.repeat(100, 300).reshape(-1,1)
return garray
from janggu.data.genomicarray import LogTransform, ZScore
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage='ndarray', cache=None, loader=loading)
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage='ndarray', cache=None, loader=loading,
normalizer=[LogTransform()])
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage='ndarray', cache=None, loader=loading,
normalizer=[ZScore()])
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage='ndarray', cache=None, loader=loading,
normalizer=[LogTransform(), ZScore()])
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage='ndarray', cache=None, loader=loading,
normalizer=['zscorelog'])
for store in ['ndarray', 'hdf5']:
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage=store, cache="cache_file", loader=loading,
normalizer=['zscorelog'])
np.testing.assert_allclose(ga.weighted_mean(), np.asarray([0.0]),
rtol=1e-5, atol=1e-5)
np.testing.assert_allclose(ga.weighted_sd(), np.asarray([1.]),
rtol=1e-5, atol=1e-5)
np.testing.assert_allclose(ga[Interval('chr1', 100, 101)],
np.asarray([[[-1.412641340027806]]]),
rtol=1e-5, atol=1e-5)
np.testing.assert_allclose(ga[Interval('chr2', 100, 101)],
np.asarray([[[0.706320670013903]]]),
rtol=1e-5, atol=1e-5)
def test_perctrim(tmpdir):
os.environ['JANGGU_OUTPUT'] = tmpdir.strpath
def loading(garray):
garray[Interval('chr1', 0, 150), 0] = np.random.normal(loc=10, size=150).reshape(-1, 1)
garray[Interval('chr2', 0, 300), 0] = np.random.normal(loc=100, size=300).reshape(-1, 1)
return garray
for store in ['ndarray', 'hdf5']:
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}),
stranded=False, typecode='float32',
storage=store, cache="cache_file", loader=loading,
normalizer=['binsizenorm', 'perctrim'])
def test_tmp_normalization(tmpdir):
os.environ['JANGGU_OUTPUT'] = tmpdir.strpath
def loading(garray):
garray[Interval('chr1', 0, 150), 0] = np.repeat(10, 150).reshape(-1, 1)
garray[Interval('chr2', 0, 300), 0] = np.repeat(1, 300).reshape(-1, 1)
return garray
for store in ['ndarray', 'hdf5']:
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}), stranded=False, typecode='float32',
storage=store, cache="cache_file", resolution=50, loader=loading,
collapser='sum',
normalizer=['tpm'])
np.testing.assert_allclose(ga[Interval('chr1', 100, 101)], np.asarray([[[10 * 1000/50 * 1e6/(720.)]]]))
np.testing.assert_allclose(ga[Interval('chr2', 100, 101)], np.asarray([[[1 * 1000/50 * 1e6/(720.)]]]))
def test_check_resolution_collapse_compatibility(tmpdir):
os.environ['JANGGU_OUTPUT'] = tmpdir.strpath
def loading(garray):
garray[Interval('chr1', 0, 150), 0] = np.repeat(10, 150).reshape(-1, 1)
garray[Interval('chr2', 0, 300), 0] = np.repeat(1, 300).reshape(-1, 1)
return garray
with pytest.raises(Exception):
# Error because resolution=50 but no collapser defined
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}), stranded=False, typecode='float32',
storage="ndarray", cache=None, resolution=50, loader=loading,
collapser=None,
normalizer=['tpm'])
with pytest.raises(Exception):
# Error because resolution=None but no collapser defined
ga = create_genomic_array(GenomicIndexer.create_from_genomesize({'chr1': 150, 'chr2': 300}), stranded=False, typecode='float32',
storage="ndarray", cache=None, resolution=None, loader=loading,
collapser=None,
normalizer=['tpm'])
ga = create_genomic_array(GenomicIndexer.create_from_file([Interval('chr1', 0, 150), Interval('chr2', 0, 150), Interval('chr2', 150, 300)], binsize=150, stepsize=None, ),
stranded=False, typecode='float32',
storage="ndarray", cache=None, resolution=1, loader=loading)
ga = create_genomic_array(GenomicIndexer.create_from_file([Interval('chr1', 0, 150), Interval('chr2', 0, 300)], binsize=None, stepsize=None, collapse=True),
stranded=False, typecode='float32',
storage="ndarray", cache='test', resolution=None, loader=loading,
store_whole_genome=None,
collapser='sum')
ga = create_genomic_array(GenomicIndexer.create_from_file([Interval('chr1', 0, 150), Interval('chr2', 0, 300)], binsize=None, stepsize=None, collapse=True),
stranded=False, typecode='float32',
storage="ndarray", cache=None, resolution=None, loader=loading,
collapser='sum',
normalizer=['tpm'])