[be69fa]: / tests / test_extractors.py

Download this file

130 lines (114 with data), 4.2 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
from genomelake import backend
from genomelake.extractors import ArrayExtractor, BigwigExtractor, FastaExtractor
import numpy as np
from pybedtools import Interval
import pyBigWig
import pytest
array_extractor_fasta_params = [
("numpy", True),
("numpy", False),
("bcolz", True),
("bcolz", False),
("tiledb", False),
("tiledb", True),
]
def test_fasta_extractor_valid_intervals():
extractor = FastaExtractor("tests/data/fasta_test.fa")
intervals = [Interval("chr1", 0, 10), Interval("chr2", 0, 10)]
expected_data = np.array(
[
[
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
],
[
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0.25, 0.25, 0.25, 0.25],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0.25, 0.25, 0.25, 0.25],
],
],
dtype=np.float32,
)
data = extractor(intervals)
assert (data == expected_data).all()
def test_fasta_extractor_over_chr_end():
extractor = FastaExtractor("tests/data/fasta_test.fa")
intervals = [Interval("chr1", 0, 100), Interval("chr1", 1, 101)]
with pytest.raises(ValueError):
data = extractor(intervals)
@pytest.mark.parametrize("mode,in_memory", array_extractor_fasta_params)
def test_array_extractor_fasta(mode, in_memory):
data_dir = "tests/data/fasta_test_dir_{}_{}".format(mode, in_memory)
backend.extract_fasta_to_file(
"tests/data/fasta_test.fa", data_dir, mode=mode, overwrite=True
)
extractor = ArrayExtractor(data_dir, in_memory=in_memory)
intervals = [Interval("chr1", 0, 10), Interval("chr2", 0, 10)]
expected_data = np.array(
[
[
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
],
[
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0.25, 0.25, 0.25, 0.25],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0.25, 0.25, 0.25, 0.25],
],
],
dtype=np.float32,
)
data = extractor(intervals)
assert (data == expected_data).all()
@pytest.fixture
def test_bigwig_and_intervals():
bw_path = "tests/data/test_bigwig.bw"
intervals = [Interval("chr1", 0, 10), Interval("chr2", 0, 10)]
expected_chr1 = np.array([0.1] * 10, dtype=np.float32)
expected_chr2 = np.array([0] + [9] * 9, dtype=np.float32)
expected_data = np.stack([expected_chr1, expected_chr2])
return (bw_path, intervals, expected_data)
@pytest.mark.parametrize("mode,in_memory", array_extractor_fasta_params)
def test_array_extractor_bigwig(test_bigwig_and_intervals, mode, in_memory):
bw_path, intervals, expected_data = test_bigwig_and_intervals
bw_dir_path = "{}.dir".format(bw_path)
backend.extract_bigwig_to_file(bw_path, bw_dir_path, mode=mode, overwrite=True)
extractor = ArrayExtractor(bw_dir_path, in_memory=in_memory)
data = extractor(intervals)
assert (data == expected_data).all()
def test_bigwig_extractor(test_bigwig_and_intervals):
bw_path, intervals, expected_data = test_bigwig_and_intervals
extractor = BigwigExtractor(bw_path)
data = extractor(intervals)
extractor.close()
assert (data == expected_data).all()