-
Notifications
You must be signed in to change notification settings - Fork 53
/
test_create_ingest.py
664 lines (611 loc) · 21 KB
/
test_create_ingest.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# import filecmp
import importlib.util
import os
import os.path as op
import tempfile
import h5py
import numpy as np
import pandas as pd
import pytest
from pandas.api import types
import cooler
from cooler.cli.cload import pairix as cload_pairix
from cooler.cli.cload import pairs as cload_pairs
from cooler.cli.cload import tabix as cload_tabix
from cooler.cli.load import load
pysam_missing = importlib.util.find_spec("pysam") is None
pairix_missing = importlib.util.find_spec("pypairix") is None
_pandas_major_version = int(pd.__version__.split(".")[0])
tmp = tempfile.gettempdir()
testdir = op.realpath(op.dirname(__file__))
testcool_path = op.join(tmp, "test.cool")
def _alternating_bins(chromsizes, steps):
def _each(chrom):
clen = chromsizes[chrom]
edges = [0]
total = 0
i = 0
while edges[i] < clen:
step = steps[i % len(steps)]
total += step
i += 1
edges.append(total)
print(edges)
edges[-1] = clen
return pd.DataFrame(
{"chrom": chrom, "start": edges[:-1], "end": edges[1:]},
columns=["chrom", "start", "end"],
)
return pd.concat(map(_each, chromsizes.keys()), axis=0, ignore_index=True)
def test_from_hdf5_pairs():
def should_not_depend_on_chunksize(chromsizes, bintable, mock_pairs):
# try different chunk sizes
binner = cooler.create.HDF5Aggregator(
mock_pairs, chromsizes, bintable, chunksize=66
)
cooler.create.create(testcool_path, bintable, binner)
with h5py.File(testcool_path, "r") as h5:
oc1 = h5["indexes"]["chrom_offset"][:]
ob1 = h5["indexes"]["bin1_offset"][:]
p1 = cooler.api.pixels(h5, join=False)
binner = cooler.create.HDF5Aggregator(
mock_pairs, chromsizes, bintable, chunksize=666
)
cooler.create.create(testcool_path, bintable, binner)
with h5py.File(testcool_path, "r") as h5:
oc2 = h5["indexes"]["chrom_offset"][:]
ob2 = h5["indexes"]["bin1_offset"][:]
p2 = cooler.api.pixels(h5, join=False)
assert np.all(oc1 == oc2)
assert np.all(ob1 == ob2)
assert np.all(p1.values == p2.values)
def should_raise_if_input_not_sorted(chromsizes, bintable, mock_pairs):
# not sorted by chrm1
# with h5py.File(testcool_path, 'w') as h5:
bad_reads = {
"chrms1": mock_pairs["chrms2"],
"cuts1": mock_pairs["cuts2"],
"chrms2": mock_pairs["chrms1"],
"cuts2": mock_pairs["cuts1"],
}
with pytest.raises(ValueError):
cooler.create.HDF5Aggregator(bad_reads, chromsizes, bintable, chunksize=66)
# not triu
bad_reads = {
"chrms1": mock_pairs["chrms1"].copy(),
"cuts1": mock_pairs["cuts1"].copy(),
"chrms2": mock_pairs["chrms2"].copy(),
"cuts2": mock_pairs["cuts2"].copy(),
}
bad_reads["chrms1"][0] = 0
bad_reads["chrms2"][0] = 0
bad_reads["cuts1"][0] = 10
bad_reads["cuts2"][0] = 9
binner = cooler.create.HDF5Aggregator(
bad_reads, chromsizes, bintable, chunksize=66
)
with pytest.raises(ValueError):
cooler.create.create(testcool_path, bintable, binner)
def should_work_with_int32_cols(chromsizes, bintable, mock_pairs):
# int64
binner = cooler.create.HDF5Aggregator(
mock_pairs, chromsizes, bintable, chunksize=66
)
cooler.create.create(testcool_path, bintable, binner)
with h5py.File(testcool_path, "r") as h5:
oc1 = h5["indexes"]["chrom_offset"][:]
ob1 = h5["indexes"]["bin1_offset"][:]
p1 = cooler.api.pixels(h5, join=False)
# int32
mock_pairs32 = {
"chrms1": mock_pairs["chrms1"].astype(np.int32),
"cuts1": mock_pairs["cuts1"].astype(np.int32),
"chrms2": mock_pairs["chrms2"].astype(np.int32),
"cuts2": mock_pairs["cuts2"].astype(np.int32),
}
binner = cooler.create.HDF5Aggregator(
mock_pairs32, chromsizes, bintable, chunksize=66
)
cooler.create.create(testcool_path, bintable, binner)
with h5py.File(testcool_path, "r") as h5:
oc2 = h5["indexes"]["chrom_offset"][:]
ob2 = h5["indexes"]["bin1_offset"][:]
p2 = cooler.api.pixels(h5, join=False)
assert np.all(oc1 == oc2)
assert np.all(ob1 == ob2)
assert np.all(p1.values == p2.values)
def _mock_hdf5_pairs():
np.random.seed(1)
chrms = np.random.randint(0, n_chroms, n_records * 2)
cuts = np.random.randint(0, clen, n_records * 2)
abs_cuts = np.array([clen * chrm + cut for chrm, cut in zip(chrms, cuts)])
abs_cuts1, abs_cuts2 = abs_cuts[:n_records], abs_cuts[n_records:]
mock_pairs = {
"chrms1": chrms[:n_records],
"cuts1": cuts[:n_records],
"chrms2": chrms[n_records:],
"cuts2": cuts[n_records:],
}
# Triu-sort
mask = abs_cuts1 > abs_cuts2
mock_pairs["chrms1"][mask], mock_pairs["chrms2"][mask] = (
mock_pairs["chrms2"][mask],
mock_pairs["chrms1"][mask],
)
mock_pairs["cuts1"][mask], mock_pairs["cuts2"][mask] = (
mock_pairs["cuts2"][mask],
mock_pairs["cuts1"][mask],
)
abs_cuts1[mask], abs_cuts2[mask] = abs_cuts2[mask], abs_cuts1[mask]
idx = np.lexsort([abs_cuts2, abs_cuts1])
for key in mock_pairs:
mock_pairs[key] = mock_pairs[key][idx]
return mock_pairs
n_chroms = 2
clen = 2000
n_records = 3000
chromsizes = pd.Series(index=["chr1", "chr2"], data=[clen, clen])
mock_pairs = _mock_hdf5_pairs()
# uniform bins
bintable = cooler.binnify(chromsizes, 100)
should_not_depend_on_chunksize(chromsizes, bintable, mock_pairs)
should_raise_if_input_not_sorted(chromsizes, bintable, mock_pairs)
should_work_with_int32_cols(chromsizes, bintable, mock_pairs)
# non-uniform bins
bintable = _alternating_bins(chromsizes, [10, 100])
should_not_depend_on_chunksize(chromsizes, bintable, mock_pairs)
should_raise_if_input_not_sorted(chromsizes, bintable, mock_pairs)
should_work_with_int32_cols(chromsizes, bintable, mock_pairs)
@pytest.mark.skipif(pysam_missing, reason="pysam not installed")
@pytest.mark.filterwarnings("ignore")
@pytest.mark.parametrize(
"bins_path,pairs_path,ref_path,nproc",
[
(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.pairs.subsample.sorted.txt.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.cool"),
8,
),
(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.pairs.subsample.sorted.txt.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.cool"),
1,
),
],
)
def test_cload_tabix(bins_path, pairs_path, ref_path, nproc):
cload_tabix.callback(
bins_path,
pairs_path,
testcool_path,
metadata=None,
assembly="hg19",
nproc=nproc,
zero_based=False,
max_split=2,
)
with h5py.File(testcool_path, "r") as f1, h5py.File(ref_path, "r") as f2:
assert np.all(f1["pixels/bin1_id"][:] == f2["pixels/bin1_id"][:])
assert np.all(f1["pixels/bin2_id"][:] == f2["pixels/bin2_id"][:])
assert np.all(f1["pixels/count"][:] == f2["pixels/count"][:])
try:
os.remove(testcool_path)
except OSError:
pass
@pytest.mark.skipif(pairix_missing, reason="pairix not installed")
@pytest.mark.parametrize(
"bins_path,pairs_path,ref_path,nproc",
[
(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.pairs.subsample.blksrt.txt.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.cool"),
8,
),
(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.pairs.subsample.blksrt.txt.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.cool"),
1,
),
],
)
def test_cload_pairix(bins_path, pairs_path, ref_path, nproc):
cload_pairix.callback(
bins_path,
pairs_path,
testcool_path,
metadata=None,
assembly="hg19",
nproc=nproc,
zero_based=False,
max_split=2,
block_char="|",
)
with h5py.File(testcool_path, "r") as f1, h5py.File(ref_path, "r") as f2:
assert np.all(f1["pixels/bin1_id"][:] == f2["pixels/bin1_id"][:])
assert np.all(f1["pixels/bin2_id"][:] == f2["pixels/bin2_id"][:])
assert np.all(f1["pixels/count"][:] == f2["pixels/count"][:])
try:
os.remove(testcool_path)
except OSError:
pass
@pytest.mark.skipif(pairix_missing, reason="pairix not installed")
def test_cload_pairix_wrong_block_char():
with pytest.raises(ValueError):
cload_pairix.callback(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.pairs.subsample.blksrt.txt.gz"),
testcool_path,
metadata=None,
assembly="hg19",
nproc=1,
zero_based=False,
max_split=2,
block_char="?",
)
@pytest.mark.skipif(
_pandas_major_version < 1, reason="hash fix only works with pandas >= 1.0"
)
@pytest.mark.parametrize(
"bins_path,pairs_path,ref_path",
[
(
op.join(testdir, "data", "toy.bins.var.bed"),
op.join(testdir, "data", "toy.pairs"),
op.join(testdir, "data", "toy.symm.upper.var.cool"),
),
(
op.join(testdir, "data", "toy.bins.var.bed"),
op.join(testdir, "data", "toy_hash.pairs"),
op.join(testdir, "data", "toy.symm.upper.var.cool"),
),
(
op.join(testdir, "data", "toy.bins.var.bed"),
op.join(testdir, "data", "toy_hash.pairs.gz"),
op.join(testdir, "data", "toy.symm.upper.var.cool"),
),
],
)
def test_cload_pairs(bins_path, pairs_path, ref_path):
kwargs = dict(
metadata=None,
assembly="hg19",
chunksize=int(15e6),
zero_based=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
field=(),
temp_dir=None,
no_delete_temp=False,
storage_options=None,
no_count=False,
max_merge=200,
chrom1=2,
pos1=3,
chrom2=4,
pos2=5,
append=False,
)
cload_pairs.callback(bins_path, pairs_path, testcool_path, **kwargs)
with h5py.File(testcool_path, "r") as f1, h5py.File(ref_path, "r") as f2:
assert np.all(f1["pixels/bin1_id"][:] == f2["pixels/bin1_id"][:])
assert np.all(f1["pixels/bin2_id"][:] == f2["pixels/bin2_id"][:])
assert np.all(f1["pixels/count"][:] == f2["pixels/count"][:])
try:
os.remove(testcool_path)
except OSError:
pass
@pytest.mark.parametrize(
"bins_path,pairs_path",
[
(
op.join(testdir, "data", "toy.chrom.sizes") + ":2",
op.join(testdir, "data", "toy.pairs"),
)
],
)
def test_cload_field(bins_path, pairs_path):
kwargs = dict(
metadata=None,
assembly="toy",
chunksize=10,
zero_based=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
temp_dir=None,
no_delete_temp=False,
storage_options=None,
no_count=True,
max_merge=200,
chrom1=2,
pos1=3,
chrom2=4,
pos2=5,
append=False,
)
cload_pairs.callback(
bins_path, pairs_path, testcool_path, field=("score=8:dtype=float",), **kwargs
)
pixels = cooler.Cooler(testcool_path).pixels()[:]
assert "count" in pixels.columns and types.is_integer_dtype(pixels.dtypes["count"])
assert "score" in pixels.columns and types.is_float_dtype(pixels.dtypes["score"])
@pytest.mark.parametrize(
"bins_path,pairs_path",
[
(
op.join(testdir, "data", "toy.chrom.sizes") + ":2",
op.join(testdir, "data", "toy.pairs"),
)
],
)
def test_cload_custom_tempdir(bins_path, pairs_path):
for temp_dir in [op.join(testdir, "data"), "-"]:
cload_pairs.callback(
bins_path,
pairs_path,
testcool_path,
metadata=None,
assembly="toy",
chunksize=10,
zero_based=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
field=(),
temp_dir=temp_dir,
no_delete_temp=False,
storage_options=None,
no_count=True,
max_merge=200,
chrom1=2,
pos1=3,
chrom2=4,
pos2=5,
append=False,
)
pixels = cooler.Cooler(testcool_path).pixels()[:]
assert "count" in pixels.columns and types.is_integer_dtype(
pixels.dtypes["count"]
)
def test_load_bg2_vs_coo():
kwargs = dict(
metadata=None,
assembly="hg19",
chunksize=int(20e6),
field=(),
count_as_float=False,
one_based=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
temp_dir=None,
no_delete_temp=False,
storage_options=None,
append=False,
)
out_path1 = op.join(tmp, "test1.cool")
out_path2 = op.join(tmp, "test2.cool")
load.callback(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.bg2.gz"),
out_path1,
format="bg2",
**kwargs,
)
load.callback(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.coo.txt"),
out_path2,
format="coo",
**kwargs,
)
with h5py.File(out_path1, "r") as f1, h5py.File(out_path2, "r") as f2:
for col in ["bin1_id", "bin2_id", "count"]:
assert np.all(f1["pixels"][col][:] == f2["pixels"][col][:])
for fp in [out_path1, out_path2]:
try:
os.remove(fp)
except OSError:
pass
def test_load_zero_one_based_bg2():
kwargs = dict(
format="bg2",
metadata=None,
assembly="toy",
chunksize=10,
field=(),
count_as_float=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
temp_dir=None,
no_delete_temp=False,
storage_options=None,
append=False,
)
# 1-based-start BG2 input
ref = "toy.symm.upper.1.ob.bg2"
bins_path = op.join(testdir, "data", "toy.chrom.sizes") + ":1"
pixels_path = op.join(testdir, "data", ref)
load.callback(bins_path, pixels_path, testcool_path, one_based=True, **kwargs)
# reference, 1-based starts
ref_df = pd.read_csv(
pixels_path,
sep="\t",
names=["chrom1", "start1", "end1", "chrom2", "start2", "end2", "count"],
)
# output
out_df = cooler.Cooler(testcool_path).pixels(join=True)[:]
out_df["start1"] += 1
out_df["start2"] += 1
assert np.all(out_df == ref_df)
# 0-based-start BG2 input
ref = "toy.symm.upper.1.zb.bg2"
bins_path = op.join(testdir, "data", "toy.chrom.sizes") + ":1"
pixels_path = op.join(testdir, "data", ref)
load.callback(bins_path, pixels_path, testcool_path, one_based=False, **kwargs)
# reference, 0-based starts
ref_df = pd.read_csv(
pixels_path,
sep="\t",
names=["chrom1", "start1", "end1", "chrom2", "start2", "end2", "count"],
)
# output
out_df = cooler.Cooler(testcool_path).pixels(join=True)[:]
assert np.all(out_df == ref_df)
def test_load_zero_one_based_coo():
kwargs = dict(
format="coo",
metadata=None,
assembly="toy",
chunksize=10,
field=(),
count_as_float=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
temp_dir=None,
no_delete_temp=False,
storage_options=None,
append=False,
)
# 1-based-start COO input
ref = "toy.symm.upper.1.ob.coo"
bins_path = op.join(testdir, "data", "toy.chrom.sizes") + ":1"
pixels_path = op.join(testdir, "data", ref)
load.callback(bins_path, pixels_path, testcool_path, one_based=True, **kwargs)
# reference, 1-based starts
ref_df = pd.read_csv(pixels_path, sep="\t", names=["bin1_id", "bin2_id", "count"])
# output
out_df = cooler.Cooler(testcool_path).pixels()[:]
out_df["bin1_id"] += 1
out_df["bin2_id"] += 1
assert np.all(out_df == ref_df)
# 0-based-start COO input
ref = "toy.symm.upper.1.zb.coo"
bins_path = op.join(testdir, "data", "toy.chrom.sizes") + ":1"
pixels_path = op.join(testdir, "data", ref)
load.callback(bins_path, pixels_path, testcool_path, one_based=False, **kwargs)
# reference, 0-based starts
ref_df = pd.read_csv(pixels_path, sep="\t", names=["bin1_id", "bin2_id", "count"])
# output
out_df = cooler.Cooler(testcool_path).pixels()[:]
assert np.all(out_df == ref_df)
def test_array_loader():
chromsizes = cooler.util.read_chromsizes(
op.join(testdir, "data", "toy.chrom.sizes")
)
bins = cooler.util.binnify(chromsizes, 10)
n = len(bins)
array = np.ones((n, n))
iterator = cooler.create.ArrayLoader(bins, array, chunksize=100)
list(iterator)
array = np.ones((n + 1, n + 1))
with pytest.raises(ValueError):
cooler.create.ArrayLoader(bins, array, chunksize=100)
def test_cload_append_mode():
for append in (True, False):
out_path = op.join(tmp, "test.cool")
with h5py.File(out_path, "w") as f:
f.attrs["xxxx"] = True
kwargs = dict(
metadata=None,
assembly="hg19",
chunksize=int(15e6),
zero_based=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
field=(),
temp_dir=None,
no_delete_temp=False,
storage_options=None,
no_count=False,
max_merge=200,
chrom1=2,
pos1=3,
chrom2=4,
pos2=5,
append=append,
)
cload_pairs.callback(
op.join(testdir, "data", "toy.bins.var.bed"),
op.join(testdir, "data", "toy.pairs"),
out_path,
**kwargs,
)
with h5py.File(out_path, "r") as f:
if append:
assert "xxxx" in f.attrs
else:
assert "xxxx" not in f.attrs
try:
os.remove(out_path)
except OSError:
pass
def test_load_append_mode():
for append in (True, False):
out_path = op.join(tmp, "test.cool")
with h5py.File(out_path, "w") as f:
f.attrs["xxxx"] = True
kwargs = dict(
metadata=None,
assembly="hg19",
chunksize=int(20e6),
field=(),
count_as_float=False,
one_based=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
temp_dir=None,
no_delete_temp=False,
storage_options=None,
append=append,
)
load.callback(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.coo.txt"),
out_path,
format="coo",
**kwargs,
)
with h5py.File(out_path, "r") as f:
if append:
assert "xxxx" in f.attrs
else:
assert "xxxx" not in f.attrs
try:
os.remove(out_path)
except OSError:
pass
def test_load_custom_tempdir():
for temp_dir in [op.join(testdir, "data"), "-"]:
load.callback(
op.join(testdir, "data", "hg19.bins.2000kb.bed.gz"),
op.join(testdir, "data", "hg19.GM12878-MboI.matrix.2000kb.coo.txt"),
testcool_path,
format="coo",
metadata=None,
assembly="hg19",
chunksize=int(20e6),
field=(),
count_as_float=False,
one_based=False,
comment_char="#",
input_copy_status="unique",
no_symmetric_upper=False,
temp_dir=temp_dir,
no_delete_temp=False,
storage_options=None,
append=False,
)
pixels = cooler.Cooler(testcool_path).pixels()[:]
assert "count" in pixels.columns and types.is_integer_dtype(
pixels.dtypes["count"]
)