-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvector.py
More file actions
286 lines (240 loc) · 7.74 KB
/
vector.py
File metadata and controls
286 lines (240 loc) · 7.74 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
284
285
286
"""
Copyright 2018 Novartis Institutes for BioMedical Research Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import base64
import numpy as np
from typing import Callable, List
from server import bigwig, utils
Vector = List[float]
TILE_SIZE = 1024
TILESET_INFO = {
"filetype": "none",
"datatype": "vector",
"coordSystem": "hg19",
"coordSystem2": "hg19",
}
def get_values(
v: Vector,
v_res: int,
v_len_abs: int,
v_offset_abs: int,
offset: int,
start: int,
end: int,
bins: int,
res: int,
missing: float = np.nan,
aggregator: Callable = np.mean,
scaleup_aggregator: Callable = np.mean,
) -> Vector:
abs_start = offset + start
abs_end = offset + end
rel_start = max(abs_start, v_offset_abs)
rel_end = min(abs_end, v_offset_abs + v_len_abs)
out = np.zeros(bins)
out[:] = missing
# Imaging:
# - RS = relative start
# - RE = relative end
# - AS = absolute start
# - AE = absolute end
# - ____ region to be cut out
# - **** availabe data
# |---RS___RE---AS******AE---RS___RE---|
if rel_start >= abs_end or rel_end <= abs_start:
# interval is outside
return out
else:
# Either the start or the end lies within the available data vector:
# start_res and end_res correspond to the indices of the data vector
# associated the start and end of the requested tiles
start_res = np.floor((rel_start - v_offset_abs) / v_res).astype(int)
end_res = np.ceil((rel_end - v_offset_abs) / v_res).astype(int)
res_ratio = v_res / res
data = np.zeros(bins)
data[:] = missing
if res_ratio < 1:
# output resolution is greater (e.g., 10 kb) than the data resolution
# (e.g., 1 kb), hence, we need to aggregate the data
data = utils.zoom_array(
v[start_res:end_res], (bins,), aggregator=aggregator
)
elif res_ratio > 1:
data = utils.scaleup_vector(
v[start_res:end_res], bins, aggregator=scaleup_aggregator
)
if rel_end < abs_end:
# Intervals overlaps with the start
# ^ = Start; $ = end
# Tile req. (abs): ----------^=======$----
# Data : -------^=======$-------
# Rel : ----------^====$-------
out[: data.size] = data
elif rel_start > abs_start:
# Intervals overlaps with the end
# ^ = Start; $ = end;
# Tile req. (abs): ----^=======$----------
# Data : -------^=======$-------
# Rel : -------^====$----------
out[out.size - data.size :] = data
else:
out = data
return out
def get_tile(
v: np.ndarray,
v_res: int,
v_len_abs: int,
v_offset_abs: int,
zoom_level: int,
start_pos: int,
end_pos: int,
chrom_sizes,
chrom_offsets,
aggregator=np.mean,
scaleup_aggregator=np.mean,
):
resolutions = bigwig.get_zoom_resolutions(chrom_sizes)
resolution = resolutions[zoom_level] # Number of bp per bin
arrays = []
for cid, start, end in bigwig.abs2chr(chrom_sizes, start_pos, end_pos):
n_bins = int(np.ceil((end - start) / resolution))
try:
offset = chrom_offsets.values[cid]
clen = chrom_sizes.values[cid]
x = get_values(
v,
v_res,
v_len_abs,
v_offset_abs,
offset,
start,
end,
n_bins,
resolution,
aggregator=aggregator,
scaleup_aggregator=scaleup_aggregator,
)
# drop the very last bin if it is smaller than the resolution
if end == clen and clen % resolution != 0:
x = x[:-1]
except IndexError:
# beyond the range of the available chromosomes
# probably means we've requested a range of absolute
# coordinates that stretch beyond the end of the genome
x = np.zeros(n_bins)
arrays.append(x)
return np.concatenate(arrays)
def tiles(
v: np.ndarray,
v_res: int,
v_len_abs: int,
v_offset_abs: int,
tile_ids,
chrom_sizes,
aggregator=np.mean,
scaleup_aggregator=np.mean,
):
"""[summary].
[description]
Args:
v: np.ndarray: [description]
v_res: int: [description]
v_len_abs: int: [description]
v_offset_abs: int: [description]
tile_ids: [description]
chrom_sizes: [description]
Returns:
[description]
[type]
"""
generated_tiles = []
for tile_id in tile_ids:
tile_id_parts = tile_id.split(".")
tile_position = list(map(int, tile_id_parts[1:3]))
zoom_level = tile_position[0]
tile_pos = tile_position[1]
# this doesn't combine multiple consequetive ids, which
# would speed things up
chrom_offsets = np.cumsum(chrom_sizes) - chrom_sizes
max_depth = bigwig.get_quadtree_depth(chrom_sizes)
tile_size = TILE_SIZE * 2 ** (max_depth - zoom_level)
start_pos = tile_pos * tile_size
end_pos = start_pos + tile_size
dense = get_tile(
v,
v_res,
v_len_abs,
v_offset_abs,
zoom_level,
start_pos,
end_pos,
chrom_sizes,
chrom_offsets,
aggregator=aggregator,
scaleup_aggregator=scaleup_aggregator,
)
dense[np.isnan(dense)] = 0.0
if len(dense):
max_dense = max(dense)
min_dense = min(dense)
else:
max_dense = 0
min_dense = 0
min_f16 = np.finfo("float16").min
max_f16 = np.finfo("float16").max
has_nan = len([d for d in dense if np.isnan(d)]) > 0
if (
not has_nan
and max_dense > min_f16
and max_dense < max_f16
and min_dense > min_f16
and min_dense < max_f16
):
tile_value = {
"dense": base64.b64encode(dense.astype("float16")).decode("utf-8"),
"dtype": "float16",
}
else:
tile_value = {
"dense": base64.b64encode(dense.astype("float32")).decode("utf-8"),
"dtype": "float32",
}
generated_tiles += [(tile_id, tile_value)]
return generated_tiles
def tileset_info(chromsizes, resolution):
"""Get the tileset info for a bigWig file.
Parameters
----------
chromsizes: pd.Dataframe
Chromsizes dataframe
resolution:
Returns
-------
tileset_info: {
'min_pos': [],
'max_pos': [],
'max_width': 131072
'tile_size': 1024,
'max_zoom': 7
}
"""
min_tile_cover = np.ceil(sum(chromsizes) / TILE_SIZE)
step_max_zoom = int(np.floor(np.log2(resolution)))
max_zoom = int(np.ceil(np.log2(min_tile_cover)))
tileset_info = {
"min_pos": [0],
"max_pos": [TILE_SIZE * 2 ** max_zoom],
"max_width": TILE_SIZE * 2 ** max_zoom,
"tile_size": TILE_SIZE,
"max_zoom": max_zoom - step_max_zoom,
}
return tileset_info