/
_peakvi.py
581 lines (531 loc) · 21.6 KB
/
_peakvi.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
from __future__ import annotations
import logging
from collections.abc import Iterable, Sequence
from functools import partial
from typing import Literal
import numpy as np
import pandas as pd
import torch
from anndata import AnnData
from scipy.sparse import csr_matrix, vstack
from scvi._constants import REGISTRY_KEYS
from scvi.data import AnnDataManager
from scvi.data.fields import (
CategoricalJointObsField,
CategoricalObsField,
LayerField,
NumericalJointObsField,
)
from scvi.model._utils import (
_get_batch_code_from_category,
scatac_raw_counts_properties,
)
from scvi.model.base import UnsupervisedTrainingMixin
from scvi.module import PEAKVAE
from scvi.train._callbacks import SaveBestState
from scvi.utils._docstrings import de_dsp, devices_dsp, setup_anndata_dsp
from .base import ArchesMixin, BaseModelClass, VAEMixin
from .base._utils import _de_core
logger = logging.getLogger(__name__)
class PEAKVI(ArchesMixin, VAEMixin, UnsupervisedTrainingMixin, BaseModelClass):
"""Peak Variational Inference for chromatin accessilibity analysis :cite:p:`Ashuach22`.
Parameters
----------
adata
AnnData object that has been registered via :meth:`~scvi.model.PEAKVI.setup_anndata`.
n_hidden
Number of nodes per hidden layer. If `None`, defaults to square root
of number of regions.
n_latent
Dimensionality of the latent space. If `None`, defaults to square root
of `n_hidden`.
n_layers_encoder
Number of hidden layers used for encoder NN.
n_layers_decoder
Number of hidden layers used for decoder NN.
dropout_rate
Dropout rate for neural networks
model_depth
Model sequencing depth / library size (default: True)
region_factors
Include region-specific factors in the model (default: True)
latent_distribution
One of
* ``'normal'`` - Normal distribution (Default)
* ``'ln'`` - Logistic normal distribution (Normal(0, I) transformed by softmax)
deeply_inject_covariates
Whether to deeply inject covariates into all layers of the decoder. If False (default),
covariates will only be included in the input layer.
**model_kwargs
Keyword args for :class:`~scvi.module.PEAKVAE`
Examples
--------
>>> adata = anndata.read_h5ad(path_to_anndata)
>>> scvi.model.PEAKVI.setup_anndata(adata, batch_key="batch")
>>> vae = scvi.model.PEAKVI(adata)
>>> vae.train()
Notes
-----
See further usage examples in the following tutorials:
1. :doc:`/tutorials/notebooks/atac/PeakVI`
"""
_module_cls = PEAKVAE
def __init__(
self,
adata: AnnData,
n_hidden: int | None = None,
n_latent: int | None = None,
n_layers_encoder: int = 2,
n_layers_decoder: int = 2,
dropout_rate: float = 0.1,
model_depth: bool = True,
region_factors: bool = True,
use_batch_norm: Literal["encoder", "decoder", "none", "both"] = "none",
use_layer_norm: Literal["encoder", "decoder", "none", "both"] = "both",
latent_distribution: Literal["normal", "ln"] = "normal",
deeply_inject_covariates: bool = False,
encode_covariates: bool = False,
**model_kwargs,
):
super().__init__(adata)
n_cats_per_cov = (
self.adata_manager.get_state_registry(REGISTRY_KEYS.CAT_COVS_KEY).n_cats_per_key
if REGISTRY_KEYS.CAT_COVS_KEY in self.adata_manager.data_registry
else []
)
self.module = self._module_cls(
n_input_regions=self.summary_stats.n_vars,
n_batch=self.summary_stats.n_batch,
n_hidden=n_hidden,
n_latent=n_latent,
n_layers_encoder=n_layers_encoder,
n_layers_decoder=n_layers_decoder,
n_continuous_cov=self.summary_stats.get("n_extra_continuous_covs", 0),
n_cats_per_cov=n_cats_per_cov,
dropout_rate=dropout_rate,
model_depth=model_depth,
region_factors=region_factors,
use_batch_norm=use_batch_norm,
use_layer_norm=use_layer_norm,
latent_distribution=latent_distribution,
deeply_inject_covariates=deeply_inject_covariates,
encode_covariates=encode_covariates,
**model_kwargs,
)
self._model_summary_string = (
"PeakVI Model with params: \nn_hidden: {}, n_latent: {}, n_layers_encoder: {}, "
"n_layers_decoder: {} , dropout_rate: {}, latent_distribution: {}, deep injection: {}, "
"encode_covariates: {}"
).format(
self.module.n_hidden,
self.module.n_latent,
n_layers_encoder,
n_layers_decoder,
dropout_rate,
latent_distribution,
deeply_inject_covariates,
encode_covariates,
)
self.n_latent = n_latent
self.init_params_ = self._get_init_params(locals())
@devices_dsp.dedent
def train(
self,
max_epochs: int = 500,
lr: float = 1e-4,
accelerator: str = "auto",
devices: int | list[int] | str = "auto",
train_size: float = 0.9,
validation_size: float | None = None,
shuffle_set_split: bool = True,
batch_size: int = 128,
weight_decay: float = 1e-3,
eps: float = 1e-08,
early_stopping: bool = True,
early_stopping_patience: int = 50,
save_best: bool = True,
check_val_every_n_epoch: int | None = None,
n_steps_kl_warmup: int | None = None,
n_epochs_kl_warmup: int | None = 50,
datasplitter_kwargs: dict | None = None,
plan_kwargs: dict | None = None,
**kwargs,
):
"""Trains the model using amortized variational inference.
Parameters
----------
max_epochs
Number of passes through the dataset.
lr
Learning rate for optimization.
%(param_accelerator)s
%(param_devices)s
train_size
Size of training set in the range [0.0, 1.0].
validation_size
Size of the test set. If `None`, defaults to 1 - `train_size`. If
`train_size + validation_size < 1`, the remaining cells belong to a test set.
shuffle_set_split
Whether to shuffle indices before splitting. If `False`, the val, train, and test set are split in the
sequential order of the data according to `validation_size` and `train_size` percentages.
batch_size
Minibatch size to use during training.
weight_decay
weight decay regularization term for optimization
eps
Optimizer eps
early_stopping
Whether to perform early stopping with respect to the validation set.
early_stopping_patience
How many epochs to wait for improvement before early stopping
save_best
Save the best model state with respect to the validation loss (default), or use the final
state in the training procedure
check_val_every_n_epoch
Check val every n train epochs. By default, val is not checked, unless `early_stopping` is `True`.
If so, val is checked every epoch.
n_steps_kl_warmup
Number of training steps (minibatches) to scale weight on KL divergences from 0 to 1.
Only activated when `n_epochs_kl_warmup` is set to None. If `None`, defaults
to `floor(0.75 * adata.n_obs)`.
n_epochs_kl_warmup
Number of epochs to scale weight on KL divergences from 0 to 1.
Overrides `n_steps_kl_warmup` when both are not `None`.
datasplitter_kwargs
Additional keyword arguments passed into :class:`~scvi.dataloaders.DataSplitter`.
plan_kwargs
Keyword args for :class:`~scvi.train.TrainingPlan`. Keyword arguments passed to
`train()` will overwrite values present in `plan_kwargs`, when appropriate.
**kwargs
Other keyword args for :class:`~scvi.train.Trainer`.
"""
update_dict = {
"lr": lr,
"weight_decay": weight_decay,
"eps": eps,
"n_epochs_kl_warmup": n_epochs_kl_warmup,
"n_steps_kl_warmup": n_steps_kl_warmup,
"optimizer": "AdamW",
}
if plan_kwargs is not None:
plan_kwargs.update(update_dict)
else:
plan_kwargs = update_dict
if save_best:
if "callbacks" not in kwargs.keys():
kwargs["callbacks"] = []
kwargs["callbacks"].append(SaveBestState(monitor="reconstruction_loss_validation"))
super().train(
max_epochs=max_epochs,
train_size=train_size,
accelerator=accelerator,
devices=devices,
validation_size=validation_size,
shuffle_set_split=shuffle_set_split,
early_stopping=early_stopping,
early_stopping_monitor="reconstruction_loss_validation",
early_stopping_patience=early_stopping_patience,
datasplitter_kwargs=datasplitter_kwargs,
plan_kwargs=plan_kwargs,
check_val_every_n_epoch=check_val_every_n_epoch,
batch_size=batch_size,
**kwargs,
)
@torch.inference_mode()
def get_library_size_factors(
self,
adata: AnnData | None = None,
indices: Sequence[int] = None,
batch_size: int = 128,
) -> dict[str, np.ndarray]:
"""Return library size factors.
Parameters
----------
adata
AnnData object with equivalent structure to initial AnnData. If `None`, defaults to the
AnnData object used to initialize the model.
indices
Indices of cells in adata to use. If `None`, all cells are used.
batch_size
Minibatch size for data loading into model. Defaults to `scvi.settings.batch_size`.
Returns
-------
Library size factor for expression and accessibility
"""
adata = self._validate_anndata(adata)
scdl = self._make_data_loader(adata=adata, indices=indices, batch_size=batch_size)
library_sizes = []
for tensors in scdl:
inference_inputs = self.module._get_inference_input(tensors)
outputs = self.module.inference(**inference_inputs)
library_sizes.append(outputs["d"].cpu())
return torch.cat(library_sizes).numpy().squeeze()
@torch.inference_mode()
def get_region_factors(self):
"""Return region-specific factors."""
if self.module.region_factors is None:
raise RuntimeError("region factors were not included in this model")
return torch.sigmoid(self.module.region_factors).cpu().numpy()
@torch.inference_mode()
def get_accessibility_estimates(
self,
adata: AnnData | None = None,
indices: Sequence[int] = None,
n_samples_overall: int | None = None,
region_list: Sequence[str] | None = None,
transform_batch: str | int | None = None,
use_z_mean: bool = True,
threshold: float | None = None,
normalize_cells: bool = False,
normalize_regions: bool = False,
batch_size: int = 128,
return_numpy: bool = False,
) -> pd.DataFrame | np.ndarray | csr_matrix:
"""Impute the full accessibility matrix.
Returns a matrix of accessibility probabilities for each cell and genomic region in the input
(for return matrix A, A[i,j] is the probability that region j is accessible in cell i).
Parameters
----------
adata
AnnData object that has been registered with scvi. If `None`, defaults to the
AnnData object used to initialize the model.
indices
Indices of cells in adata to use. If `None`, all cells are used.
n_samples_overall
Number of samples to return in total
region_list
Return accessibility estimates for this subset of regions. if `None`, all regions are used.
This can save memory when dealing with large datasets.
transform_batch
Batch to condition on.
If transform_batch is:
- None, then real observed batch is used
- int, then batch transform_batch is used
use_z_mean
If True (default), use the distribution mean. Otherwise, sample from the distribution.
threshold
If provided, values below the threshold are replaced with 0 and a sparse matrix
is returned instead. This is recommended for very large matrices. Must be between 0 and 1.
normalize_cells
Whether to reintroduce library size factors to scale the normalized probabilities.
This makes the estimates closer to the input, but removes the library size correction.
False by default.
normalize_regions
Whether to reintroduce region factors to scale the normalized probabilities. This makes
the estimates closer to the input, but removes the region-level bias correction. False by
default.
batch_size
Minibatch size for data loading into model
return_numpy
If `True` and `threshold=None`, return :class:`~numpy.ndarray`. If `True` and `threshold` is
given, return :class:`~scipy.sparse.csr_matrix`. If `False`, return :class:`~pandas.DataFrame`.
DataFrame includes regions names as columns.
"""
adata = self._validate_anndata(adata)
adata_manager = self.get_anndata_manager(adata, required=True)
if indices is None:
indices = np.arange(adata.n_obs)
if n_samples_overall is not None:
indices = np.random.choice(indices, n_samples_overall)
post = self._make_data_loader(adata=adata, indices=indices, batch_size=batch_size)
transform_batch = _get_batch_code_from_category(adata_manager, transform_batch)
if region_list is None:
region_mask = slice(None)
else:
all_regions = adata.var_names
region_mask = [region in region_list for region in all_regions]
if threshold is not None and (threshold < 0 or threshold > 1):
raise ValueError("the provided threshold must be between 0 and 1")
imputed = []
for tensors in post:
get_generative_input_kwargs = {"transform_batch": transform_batch[0]}
generative_kwargs = {"use_z_mean": use_z_mean}
inference_outputs, generative_outputs = self.module.forward(
tensors=tensors,
get_generative_input_kwargs=get_generative_input_kwargs,
generative_kwargs=generative_kwargs,
compute_loss=False,
)
p = generative_outputs["p"].cpu()
if normalize_cells:
p *= inference_outputs["d"].cpu()
if normalize_regions:
p *= torch.sigmoid(self.module.region_factors).cpu()
if threshold:
p[p < threshold] = 0
p = csr_matrix(p.numpy())
if region_list is not None:
p = p[:, region_mask]
imputed.append(p)
if threshold: # imputed is a list of csr_matrix objects
imputed = vstack(imputed, format="csr")
else: # imputed is a list of tensors
imputed = torch.cat(imputed).numpy()
if return_numpy:
return imputed
elif threshold:
return pd.DataFrame.sparse.from_spmatrix(
imputed,
index=adata.obs_names[indices],
columns=adata.var_names[region_mask],
)
else:
return pd.DataFrame(
imputed,
index=adata.obs_names[indices],
columns=adata.var_names[region_mask],
)
@de_dsp.dedent
def differential_accessibility(
self,
adata: AnnData | None = None,
groupby: str | None = None,
group1: Iterable[str] | None = None,
group2: str | None = None,
idx1: Sequence[int] | Sequence[bool] | str | None = None,
idx2: Sequence[int] | Sequence[bool] | str | None = None,
mode: Literal["vanilla", "change"] = "change",
delta: float = 0.05,
batch_size: int | None = None,
all_stats: bool = True,
batch_correction: bool = False,
batchid1: Iterable[str] | None = None,
batchid2: Iterable[str] | None = None,
fdr_target: float = 0.05,
silent: bool = False,
two_sided: bool = True,
**kwargs,
) -> pd.DataFrame:
r"""\.
A unified method for differential accessibility analysis.
Implements `"vanilla"` DE :cite:p:`Lopez18`. and `"change"` mode DE :cite:p:`Boyeau19`.
Parameters
----------
%(de_adata)s
%(de_groupby)s
%(de_group1)s
%(de_group2)s
%(de_idx1)s
%(de_idx2)s
%(de_mode)s
%(de_delta)s
%(de_batch_size)s
%(de_all_stats)s
%(de_batch_correction)s
%(de_batchid1)s
%(de_batchid2)s
%(de_fdr_target)s
%(de_silent)s
two_sided
Whether to perform a two-sided test, or a one-sided test.
**kwargs
Keyword args for :meth:`scvi.model.base.DifferentialComputation.get_bayes_factors`
Returns
-------
Differential accessibility DataFrame with the following columns:
prob_da
the probability of the region being differentially accessible
is_da_fdr
whether the region passes a multiple hypothesis correction procedure with the target_fdr
threshold
bayes_factor
Bayes Factor indicating the level of significance of the analysis
effect_size
the effect size, computed as (accessibility in population 2) - (accessibility in population 1)
emp_effect
the empirical effect, based on observed detection rates instead of the estimated accessibility
scores from the PeakVI model
est_prob1
the estimated probability of accessibility in population 1
est_prob2
the estimated probability of accessibility in population 2
emp_prob1
the empirical (observed) probability of accessibility in population 1
emp_prob2
the empirical (observed) probability of accessibility in population 2
"""
adata = self._validate_anndata(adata)
col_names = adata.var_names
model_fn = partial(
self.get_accessibility_estimates, use_z_mean=False, batch_size=batch_size
)
# TODO check if change_fn in kwargs and raise error if so
def change_fn(a, b):
return a - b
if two_sided:
def m1_domain_fn(samples):
return np.abs(samples) >= delta
else:
def m1_domain_fn(samples):
return samples >= delta
result = _de_core(
adata_manager=self.get_anndata_manager(adata, required=True),
model_fn=model_fn,
representation_fn=None,
groupby=groupby,
group1=group1,
group2=group2,
idx1=idx1,
idx2=idx2,
all_stats=all_stats,
all_stats_fn=scatac_raw_counts_properties,
col_names=col_names,
mode=mode,
batchid1=batchid1,
batchid2=batchid2,
delta=delta,
batch_correction=batch_correction,
fdr=fdr_target,
change_fn=change_fn,
m1_domain_fn=m1_domain_fn,
silent=silent,
**kwargs,
)
# manually change the results DataFrame to fit a PeakVI differential accessibility results
result = pd.DataFrame(
{
"prob_da": result.proba_de,
"is_da_fdr": result.loc[:, f"is_de_fdr_{fdr_target}"],
"bayes_factor": result.bayes_factor,
"effect_size": result.scale2 - result.scale1,
"emp_effect": result.emp_mean2 - result.emp_mean1,
"est_prob1": result.scale1,
"est_prob2": result.scale2,
"emp_prob1": result.emp_mean1,
"emp_prob2": result.emp_mean2,
},
)
return result
@classmethod
@setup_anndata_dsp.dedent
def setup_anndata(
cls,
adata: AnnData,
batch_key: str | None = None,
labels_key: str | None = None,
categorical_covariate_keys: list[str] | None = None,
continuous_covariate_keys: list[str] | None = None,
layer: str | None = None,
**kwargs,
):
"""%(summary)s.
Parameters
----------
%(param_adata)s
%(param_batch_key)s
%(param_labels_key)s
%(param_cat_cov_keys)s
%(param_cont_cov_keys)s
%(param_layer)s
"""
setup_method_args = cls._get_setup_method_args(**locals())
anndata_fields = [
LayerField(REGISTRY_KEYS.X_KEY, layer, is_count_data=True),
CategoricalObsField(REGISTRY_KEYS.BATCH_KEY, batch_key),
CategoricalObsField(REGISTRY_KEYS.LABELS_KEY, labels_key),
CategoricalJointObsField(REGISTRY_KEYS.CAT_COVS_KEY, categorical_covariate_keys),
NumericalJointObsField(REGISTRY_KEYS.CONT_COVS_KEY, continuous_covariate_keys),
]
adata_manager = AnnDataManager(fields=anndata_fields, setup_method_args=setup_method_args)
adata_manager.register_fields(adata, **kwargs)
cls.register_manager(adata_manager)