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 | class RegionMultiWindow(chd.flow.Flow):
"""
Interpret a *pred* model positionally by censoring windows of across multiple window sizes.
"""
design = chd.flow.Stored()
"""
The design of the censoring windows.
"""
regions = chd.flow.Stored(default=set)
"""
The regions that have been scored.
"""
scores = chd.flow.SparseDataset()
interpolation = chd.flow.SparseDataset()
censorer = Stored()
@classmethod
def create(
cls, folds, transcriptome, fragments, censorer, path=None, phases=None, overwrite=False
) -> RegionMultiWindow:
self = super().create(path, reset=overwrite)
if phases is None:
# phases = ["train", "validation", "test"]
phases = ["validation", "test"]
regions = fragments.regions.var.index
coords_pointed = {
regions.name: regions,
"fold": pd.Index(range(len(folds)), name="fold"),
"phase": pd.Index(phases, name="phase"),
}
coords_fixed = {
censorer.design.index.name: censorer.design.index[1:],
}
self.censorer = censorer
design_censorer = censorer.design
if not self.o.scores.exists(self):
self.scores = chd.sparse.SparseDataset.create(
self.path / "scores",
variables={
"deltacor": {
"dimensions": (regions.name, "fold", "phase", design_censorer.index.name),
"dtype": np.float32,
},
"lost": {
"dimensions": (regions.name, "fold", "phase", design_censorer.index.name),
"dtype": np.float32,
},
"effect": {
"dimensions": (regions.name, "fold", "phase", design_censorer.index.name),
"dtype": np.float32,
},
"scored": {
"dimensions": (regions.name, "fold"),
"dtype": bool,
"sparse": False,
},
},
coords_pointed=coords_pointed,
coords_fixed=coords_fixed,
)
if not self.o.interpolation.exists(self):
positions_oi = np.arange(
self.design["window_start"].min(),
self.design["window_end"].max() + 1,
10,
)
self.design_interpolation = pd.DataFrame(
{
"position": positions_oi,
}
).set_index("position")
self.interpolation = chd.sparse.SparseDataset.create(
self.path / "interpolation",
variables={
"deltacor": {
"dimensions": (regions.name, self.design_interpolation.index.name),
"dtype": np.float32,
},
"lost": {"dimensions": (regions.name, self.design_interpolation.index.name), "dtype": np.float32},
"effect": {"dimensions": (regions.name, self.design_interpolation.index.name), "dtype": np.float32},
"interpolated": {"dimensions": (regions.name,), "dtype": bool, "sparse": False},
},
coords_pointed={regions.name: regions},
coords_fixed={"position": self.design_interpolation.index},
)
return self
def score(
self,
models,
folds=None,
fragments=None,
transcriptome=None,
regions=None,
force=False,
device=None,
min_fragments=3,
):
force_ = force
if regions is None:
# get regions from models
if models.regions_oi is not None:
regions = models.regions_oi
else:
regions = self.scores.coords_pointed[list(self.scores.coords_pointed)[0]]
if folds is None:
folds = models.folds
pbar = tqdm.tqdm(regions, leave=False)
for region in pbar:
pbar.set_description(region)
for fold_ix, fold in enumerate(folds):
force = force_
if not self.scores["scored"][region, fold_ix]:
force = True
if force:
model_name = f"{region}_{fold_ix}"
if model_name not in models:
continue
pbar.set_description(region + " " + str(fold_ix))
model = models[model_name]
predicted, expected, n_fragments = model.get_prediction_censored(
fragments=fragments,
transcriptome=transcriptome,
censorer=self.censorer,
cell_ixs=np.concatenate([fold["cells_validation"], fold["cells_test"]]),
regions=[region],
device=device,
min_fragments=min_fragments,
)
# select 1st region, given that we're working with one region anyway
predicted = predicted[..., 0]
expected = expected[..., 0]
n_fragments = n_fragments[..., 0]
cor = chd.utils.paircor(predicted, expected, dim=-1)
deltacor = cor[1:] - cor[0]
lost = (n_fragments[0] - n_fragments[1:]).mean(-1)
effect = (predicted[0] - predicted[1:]).mean(-1)
self.scores["deltacor"][region, fold_ix, "test"] = deltacor
self.scores["lost"][region, fold_ix, "test"] = lost
self.scores["effect"][region, fold_ix, "test"] = effect
self.scores["scored"][region, fold_ix] = True
return self
def score2(
self,
models,
folds=None,
fragments=None,
transcriptome=None,
regions=None,
force=False,
device=None,
min_fragments=3,
):
force_ = force
if regions is None:
# get regions from models
if models.regions_oi is not None:
regions = models.regions_oi
else:
regions = self.scores.coords_pointed[list(self.scores.coords_pointed)[0]]
if folds is None:
folds = models.folds
pbar = tqdm.tqdm(regions, leave=False)
for region in pbar:
pbar.set_description(region)
for fold_ix, fold in enumerate(folds):
force = force_
if not self.scores["scored"][region, fold_ix]:
force = True
if force:
model_name = f"{region}_{fold_ix}"
if model_name not in models:
continue
pbar.set_description(region + " " + str(fold_ix))
model = models[model_name]
deltacor, lost, effect = model.get_performance_censored(
fragments=fragments,
transcriptome=transcriptome,
censorer=self.censorer,
cell_ixs=np.concatenate([fold["cells_validation"], fold["cells_test"]]),
regions=[region],
device=device,
min_fragments=min_fragments,
)
self.scores["deltacor"][region, fold_ix, "test"] = deltacor
self.scores["lost"][region, fold_ix, "test"] = lost
self.scores["effect"][region, fold_ix, "test"] = effect
self.scores["scored"][region, fold_ix] = True
return self
@property
def design(self):
return self.censorer.design.iloc[1:]
def interpolate(self, regions=None, force=False, pbar=True):
force_ = force
if regions is None:
regions = self.scores.coords_pointed[list(self.scores.coords_pointed)[0]]
progress = regions
if pbar:
progress = tqdm.tqdm(progress, leave=False)
for region in progress:
if pbar:
progress.set_description(region)
if not all([self.scores["scored"][region, fold_ix] for fold_ix in self.scores.coords_pointed["fold"]]):
continue
force = force_
if not self.interpolation["interpolated"][region]:
force = True
if force:
deltacor, lost, effect = self._interpolate(region)
self.interpolation["deltacor"][region] = deltacor
self.interpolation["effect"][region] = effect
self.interpolation["lost"][region] = lost
self.interpolation["interpolated"][region] = True
return self
def _interpolate(self, region):
deltacors = []
effects = []
losts = []
for fold_ix in self.scores.coords_pointed["fold"]:
deltacors.append(self.scores["deltacor"][region, fold_ix, "test"])
effects.append(self.scores["effect"][region, fold_ix, "test"])
losts.append(self.scores["lost"][region, fold_ix, "test"])
deltacors = np.stack(deltacors)
effects = np.stack(effects)
losts = np.stack(losts)
scores_statistical = []
for i in range(deltacors.shape[1]):
if deltacors.shape[0] > 1:
scores_statistical.append(scipy.stats.ttest_1samp(deltacors[:, i], 0, alternative="less").pvalue)
else:
scores_statistical.append(0.0)
scores_statistical = pd.DataFrame({"pvalue": scores_statistical})
scores_statistical["qval"] = fdr_nan(scores_statistical["pvalue"])
plotdata = pd.DataFrame(
{
"deltacor": deltacors.mean(0),
"effect": effects.mean(0),
"lost": losts.mean(0),
},
index=self.design.index,
)
plotdata = self.design.join(plotdata)
plotdata["qval"] = scores_statistical["qval"].values
window_sizes_info = pd.DataFrame({"window_size": self.design["window_size"].unique()}).set_index("window_size")
window_sizes_info["ix"] = np.arange(len(window_sizes_info))
# interpolate
positions_oi = np.arange(
self.design["window_start"].min(),
self.design["window_end"].max() + 1,
10,
)
deltacor_interpolated = np.zeros((len(window_sizes_info), len(positions_oi)))
lost_interpolated = np.zeros((len(window_sizes_info), len(positions_oi)))
effect_interpolated = np.zeros((len(window_sizes_info), len(positions_oi)))
for window_size, window_size_info in window_sizes_info.iterrows():
plotdata_oi = plotdata.query("window_size == @window_size")
x = plotdata_oi["window_mid"].values.copy()
y = plotdata_oi["deltacor"].values.copy()
# y[(plotdata_oi["qval"] > 0.2) | pd.isnull(plotdata_oi["qval"])] = 0.0
deltacor_interpolated_ = np.clip(
np.interp(positions_oi, x, y) / window_size * 1000,
-np.inf,
0,
)
deltacor_interpolated[window_size_info["ix"], :] = deltacor_interpolated_
lost_interpolated_ = (
np.interp(positions_oi, plotdata_oi["window_mid"], plotdata_oi["lost"]) / window_size * 1000
)
lost_interpolated[window_size_info["ix"], :] = lost_interpolated_
effect_interpolated_ = (
np.interp(
positions_oi,
plotdata_oi["window_mid"],
plotdata_oi["effect"],
)
/ window_size
* 1000
)
effect_interpolated[window_size_info["ix"], :] = effect_interpolated_
return deltacor_interpolated.mean(0), lost_interpolated.mean(0), effect_interpolated.mean(0)
def get_plotdata(self, region):
if not self.interpolation["interpolated"][region]:
raise ValueError(f"Region {region} not interpolated. Run .interpolate() first.")
plotdata = self.interpolation.sel_xr(region, variables=["deltacor", "lost", "effect"]).to_pandas()
return plotdata
def get_scoring_path(self, region):
path = self.path / f"{region}"
path.mkdir(parents=True, exist_ok=True)
return path
def select_windows(self, region_id, max_merge_distance=500, min_length=50, padding=500, lost_cutoff=0.5):
from scipy.ndimage import convolve
def spread_true(arr, width=5):
kernel = np.ones(width, dtype=bool)
result = convolve(arr, kernel, mode="constant", cval=False)
result = result != 0
return result
plotdata = self.get_plotdata(region_id)
selection = pd.DataFrame({"chosen": (plotdata["lost"] > lost_cutoff)})
# add padding
step = plotdata.index.get_level_values("position")[1] - plotdata.index.get_level_values("position")[0]
k_padding = int(padding // step)
selection["chosen"] = spread_true(selection["chosen"], width=k_padding)
# select all contiguous regions where chosen is true
selection["selection"] = selection["chosen"].cumsum()
regions = pd.DataFrame(
{
"start": selection.index[
(np.diff(np.pad(selection["chosen"], (1, 1), constant_values=False).astype(int)) == 1)[:-1]
],
"end": selection.index[
(np.diff(np.pad(selection["chosen"], (1, 1), constant_values=False).astype(int)) == -1)[1:]
],
}
)
# merge regions that are close to each other
regions["distance_to_next"] = regions["start"].shift(-1) - regions["end"]
regions["merge"] = (regions["distance_to_next"] < max_merge_distance).fillna(False)
regions["group"] = (~regions["merge"]).cumsum().shift(1).fillna(0).astype(int)
regions = (
regions.groupby("group")
.agg({"start": "min", "end": "max", "distance_to_next": "last"})
.reset_index(drop=True)
)
# filter on length
regions["length"] = regions["end"] - regions["start"]
regions = regions[regions["length"] > min_length]
return regions
def extract_predictive_windows(self, region_id=None, deltacor_cutoff=-0.001):
"""
Extract predictive windows for one (or more) regions
"""
feature_name = list(self.scores.coords_pointed.keys())[0]
if region_id is None:
region_id = self.scores.coords_pointed[feature_name]
if isinstance(region_id, str):
region_id = [region_id]
extracted = []
for region_id in region_id:
if self.interpolation["interpolated"][region_id]:
plotdata = self.get_plotdata(region_id)
plotdata["chosen"] = (plotdata["deltacor"] < deltacor_cutoff) & (plotdata["effect"] > 0)
extracted_region_positive = pd.DataFrame(
{
"start": plotdata.index[
(np.diff(np.pad(plotdata["chosen"], (1, 1), constant_values=False).astype(int)) == 1)[:-1]
].astype(int),
"end": plotdata.index[
(np.diff(np.pad(plotdata["chosen"], (1, 1), constant_values=False).astype(int)) == -1)[1:]
].astype(int),
feature_name: region_id,
"effect_direction": +1,
}
)
extracted.append(extracted_region_positive)
plotdata["chosen"] = (plotdata["deltacor"] < deltacor_cutoff) & (plotdata["effect"] < 0)
extracted_region_negative = pd.DataFrame(
{
"start": plotdata.index[
(np.diff(np.pad(plotdata["chosen"], (1, 1), constant_values=False).astype(int)) == 1)[:-1]
],
"end": plotdata.index[
(np.diff(np.pad(plotdata["chosen"], (1, 1), constant_values=False).astype(int)) == -1)[1:]
],
feature_name: region_id,
"effect_direction": -1,
}
)
extracted.append(extracted_region_negative)
return pd.concat(extracted)
|