-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathdds.py
1214 lines (1017 loc) · 46.1 KB
/
dds.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import time
import warnings
from typing import List
from typing import Literal
from typing import Optional
from typing import Union
from typing import cast
import anndata as ad # type: ignore
import numpy as np
import pandas as pd
from scipy.optimize import minimize
from scipy.special import polygamma # type: ignore
from scipy.stats import f # type: ignore
from scipy.stats import trim_mean # type: ignore
from pydeseq2.default_inference import DefaultInference
from pydeseq2.inference import Inference
from pydeseq2.preprocessing import deseq2_norm_fit
from pydeseq2.preprocessing import deseq2_norm_transform
from pydeseq2.utils import build_design_matrix
from pydeseq2.utils import dispersion_trend
from pydeseq2.utils import make_scatter
from pydeseq2.utils import mean_absolute_deviation
from pydeseq2.utils import n_or_more_replicates
from pydeseq2.utils import nb_nll
from pydeseq2.utils import replace_underscores
from pydeseq2.utils import robust_method_of_moments_disp
from pydeseq2.utils import test_valid_counts
from pydeseq2.utils import trimmed_mean
# Ignore AnnData's FutureWarning about implicit data conversion.
warnings.simplefilter("ignore", FutureWarning)
class DeseqDataSet(ad.AnnData):
r"""A class to implement dispersion and log fold-change (LFC) estimation.
The DeseqDataSet extends the `AnnData class
<https://anndata.readthedocs.io/en/latest/generated/anndata.AnnData.html#anndata.AnnData>`_.
As such, it implements the same methods and attributes, in addition to those that are
specific to pydeseq2.
Dispersions and LFCs are estimated following the DESeq2 pipeline
:cite:p:`DeseqDataSet-love2014moderated`.
Parameters
----------
adata : anndata.AnnData
AnnData from which to initialize the DeseqDataSet. Must have counts ('X') and
sample metadata ('obs') fields. If ``None``, both ``counts`` and ``metadata``
arguments must be provided.
counts : pandas.DataFrame
Raw counts. One column per gene, rows are indexed by sample barcodes.
metadata : pandas.DataFrame
DataFrame containing sample metadata.
Must be indexed by sample barcodes.
design_factors : str or list
Name of the columns of metadata to be used as design variables.
(default: ``'condition'``).
continuous_factors : list or None
An optional list of continuous (as opposed to categorical) factors. Any factor
not in ``continuous_factors`` will be considered categorical (default: ``None``).
ref_level : list or None
An optional list of two strings of the form ``["factor", "test_level"]``
specifying the factor of interest and the reference (control) level against which
we're testing, e.g. ``["condition", "A"]``. (default: ``None``).
trend_fit_type : str
Either "parametric" or "mean" for the type of fitting of the dispersions
trend curve. (default: ``"parametric"``).
min_mu : float
Threshold for mean estimates. (default: ``0.5``).
min_disp : float
Lower threshold for dispersion parameters. (default: ``1e-8``).
max_disp : float
Upper threshold for dispersion parameters.
Note: The threshold that is actually enforced is max(max_disp, len(counts)).
(default: ``10``).
refit_cooks : bool
Whether to refit cooks outliers. (default: ``True``).
min_replicates : int
Minimum number of replicates a condition should have
to allow refitting its samples. (default: ``7``).
beta_tol : float
Stopping criterion for IRWLS. (default: ``1e-8``).
.. math:: \vert dev_t - dev_{t+1}\vert / (\vert dev \vert + 0.1) < \beta_{tol}.
n_cpus : int
Number of cpus to use. If ``None`` and if ``inference`` is not provided, all
available cpus will be used by the ``DefaultInference``. If both are specified,
it will try to override the ``n_cpus`` attribute of the ``inference`` object.
(default: ``None``).
inference : Inference
Implementation of inference routines object instance.
(default:
:class:`DefaultInference <pydeseq2.default_inference.DefaultInference>`).
quiet : bool
Suppress deseq2 status updates during fit.
Attributes
----------
X
A ‘number of samples’ x ‘number of genes’ count data matrix.
obs
Key-indexed one-dimensional observations annotation of length 'number of
samples". Used to store design factors.
var
Key-indexed one-dimensional gene-level annotation of length ‘number of genes’.
uns
Key-indexed unstructured annotation.
obsm
Key-indexed multi-dimensional observations annotation of length
‘number of samples’. Stores "design_matrix" and "size_factors", among others.
varm
Key-indexed multi-dimensional gene annotation of length ‘number of genes’.
Stores "dispersions" and "LFC", among others.
layers
Key-indexed multi-dimensional arrays aligned to dimensions of `X`, e.g. "cooks".
n_processes : int
Number of cpus to use for multiprocessing.
non_zero_idx : ndarray
Indices of genes that have non-uniformly zero counts.
non_zero_genes : pandas.Index
Index of genes that have non-uniformly zero counts.
counts_to_refit : anndata.AnnData
Read counts after replacement, containing only genes
for which dispersions and LFCs must be fitted again.
new_all_zeroes_genes : pandas.Index
Genes which have only zero counts after outlier replacement.
quiet : bool
Suppress deseq2 status updates during fit.
fit_type: str
Either "parametric" or "mean" for the type of fitting of dispersions to the
mean intensity. "parametric": fit a dispersion-mean relation via a robust
gamma-family GLM. "mean": use the mean of gene-wise dispersion estimates.
(default: ``"parametric"``).
logmeans: numpy.ndarray
Gene-wise mean log counts, computed in ``preprocessing.deseq2_norm_fit()``.
filtered_genes: numpy.ndarray
Genes whose log means are different from -∞, computed in
preprocessing.deseq2_norm_fit().
References
----------
.. bibliography::
:keyprefix: DeseqDataSet-
"""
def __init__(
self,
*,
adata: Optional[ad.AnnData] = None,
counts: Optional[pd.DataFrame] = None,
metadata: Optional[pd.DataFrame] = None,
design_factors: Union[str, List[str]] = "condition",
continuous_factors: Optional[List[str]] = None,
ref_level: Optional[List[str]] = None,
trend_fit_type: Literal["parametric", "mean"] = "parametric",
min_mu: float = 0.5,
min_disp: float = 1e-8,
max_disp: float = 10.0,
refit_cooks: bool = True,
min_replicates: int = 7,
beta_tol: float = 1e-8,
n_cpus: Optional[int] = None,
inference: Optional[Inference] = None,
quiet: bool = False,
) -> None:
# Initialize the AnnData part
if adata is not None:
if counts is not None:
warnings.warn(
"adata was provided; ignoring counts.", UserWarning, stacklevel=2
)
if metadata is not None:
warnings.warn(
"adata was provided; ignoring metadata.", UserWarning, stacklevel=2
)
# Test counts before going further
test_valid_counts(adata.X)
# Copy fields from original AnnData
self.__dict__.update(adata.__dict__)
elif counts is not None and metadata is not None:
# Test counts before going further
test_valid_counts(counts)
super().__init__(X=counts.astype(int), obs=metadata)
else:
raise ValueError(
"Either adata or both counts and metadata arguments must be provided."
)
# Convert design_factors to list if a single string was provided.
self.design_factors = (
[design_factors] if isinstance(design_factors, str) else design_factors
)
self.continuous_factors = continuous_factors
if self.obs[self.design_factors].isna().any().any():
raise ValueError("NaNs are not allowed in the design factors.")
self.obs[self.design_factors] = self.obs[self.design_factors].astype(str)
# Check that design factors don't contain underscores. If so, convert them to
# hyphens.
if np.any(["_" in factor for factor in self.design_factors]):
warnings.warn(
"""Same factor names in the design contain underscores ('_'). They will
be converted to hyphens ('-').""",
UserWarning,
stacklevel=2,
)
new_factors = replace_underscores(self.design_factors)
self.obs.rename(
columns=dict(zip(self.design_factors, new_factors)),
inplace=True,
)
self.design_factors = new_factors
# Also check continuous factors
if self.continuous_factors is not None:
self.continuous_factors = replace_underscores(self.continuous_factors)
# If ref_level has underscores, covert them to hyphens
# Don't raise a warning: it will be raised by build_design_matrix()
if ref_level is not None:
ref_level = replace_underscores(ref_level)
# Build the design matrix
# Stored in the obsm attribute of the dataset
self.obsm["design_matrix"] = build_design_matrix(
metadata=self.obs,
design_factors=self.design_factors,
continuous_factors=self.continuous_factors,
ref_level=ref_level,
expanded=False,
intercept=True,
)
# Check that the design matrix has full rank
self._check_full_rank_design()
self.trend_fit_type = trend_fit_type
self.min_mu = min_mu
self.min_disp = min_disp
self.max_disp = np.maximum(max_disp, self.n_obs)
self.refit_cooks = refit_cooks
self.ref_level = ref_level
self.min_replicates = min_replicates
self.beta_tol = beta_tol
self.quiet = quiet
self.logmeans = None
self.filtered_genes = None
if inference:
if hasattr(inference, "n_cpus"):
inference.n_cpus = n_cpus
else:
warnings.warn(
"The provided inference object does not have an n_cpus "
"attribute, cannot override `n_cpus`.",
UserWarning,
stacklevel=2,
)
# Initialize the inference object.
self.inference = inference or DefaultInference(n_cpus=n_cpus)
def vst(
self,
use_design: bool = False,
fit_type: Literal["parametric", "mean"] = "parametric",
) -> None:
"""Fit a variance stabilizing transformation, and apply it to normalized counts.
Results are stored in ``dds.layers["vst_counts"]``.
Parameters
----------
use_design : bool
Whether to use the full design matrix to fit dispersions and the trend curve.
If False, only an intercept is used. (default: ``False``).
fit_type: str
Either "parametric" or "mean" for the type of fitting of dispersions to the
mean intensity. "parametric": fit a dispersion-mean relation via a robust
gamma-family GLM. "mean": use the mean of gene-wise dispersion estimates.
(default: ``"parametric"``).
"""
self.vst_fit(use_design=use_design, fit_type=fit_type)
self.layers["vst_counts"] = self.vst_transform()
def vst_fit(
self,
use_design: bool = False,
fit_type: Literal["parametric", "mean"] = "parametric",
) -> None:
"""Fit a variance stabilizing transformation.
Results are stored in ``dds.layers["vst_counts"]``.
Parameters
----------
use_design : bool
Whether to use the full design matrix to fit dispersions and the trend curve.
If False, only an intercept is used. (default: ``False``).
fit_type : str
Either "parametric" or "mean" for the type of fitting of dispersions to the
mean intensity. parametric - fit a dispersion-mean relation via a robust
gamma-family GLM. mean - use the mean of gene-wise dispersion estimates.
(default: ``"parametric"``).
"""
self.fit_type = fit_type # to re-use inside vst_transform
# Start by fitting median-of-ratio size factors if not already present,
# or if they were computed iteratively
if "size_factors" not in self.obsm or self.logmeans is None:
self.fit_size_factors() # by default, fit_type != "iterative"
if use_design:
# Check that the dispersion trend curve was fitted. If not, fit it.
# This will call previous functions in a cascade.
if "disp_function" not in self.uns:
self.fit_dispersion_trend()
else:
# Reduce the design matrix to an intercept and reconstruct at the end
self.obsm["design_matrix_buffer"] = self.obsm["design_matrix"].copy()
self.obsm["design_matrix"] = pd.DataFrame(
1, index=self.obs_names, columns=[["intercept"]]
)
# Fit the trend curve with an intercept design
self.fit_genewise_dispersions()
if self.fit_type == "parametric":
self.fit_dispersion_trend()
# Restore the design matrix and free buffer
self.obsm["design_matrix"] = self.obsm["design_matrix_buffer"].copy()
del self.obsm["design_matrix_buffer"]
def vst_transform(self, counts: Optional[np.ndarray] = None) -> np.ndarray:
"""Apply the variance stabilizing transformation.
Uses the results from the ``vst_fit`` method.
Parameters
----------
counts : numpy.ndarray
Counts to transform. If ``None``, use the counts from the current dataset.
(default: ``None``).
Returns
-------
numpy.ndarray
Variance stabilized counts.
"""
if "size_factors" not in self.obsm:
raise RuntimeError(
"The vst_fit method should be called prior to vst_transform."
)
if counts is None:
# the transformed counts will be the current ones
normed_counts = self.layers["normed_counts"]
else:
if self.logmeans is None:
# the size factors were still computed iteratively
warnings.warn(
"The size factors were fitted iteratively. They will "
"be re-computed with the counts to be transformed. In a train/test "
"setting with a downstream task, this would result in a leak of "
"data from test to train set.",
UserWarning,
stacklevel=2,
)
logmeans, filtered_genes = deseq2_norm_fit(counts)
else:
logmeans, filtered_genes = self.logmeans, self.filtered_genes
normed_counts, _ = deseq2_norm_transform(counts, logmeans, filtered_genes)
if self.fit_type == "parametric":
a0, a1 = self.uns["trend_coeffs"]
return np.log2(
(
1
+ a1
+ 2 * a0 * normed_counts
+ 2 * np.sqrt(a0 * normed_counts * (1 + a1 + a0 * normed_counts))
)
/ (4 * a0)
)
elif self.fit_type == "mean":
gene_dispersions = self.varm["genewise_dispersions"]
use_for_mean = gene_dispersions > 10 * self.min_disp
mean_disp = trim_mean(gene_dispersions[use_for_mean], proportiontocut=0.001)
return (
2 * np.arcsinh(np.sqrt(mean_disp * normed_counts))
- np.log(mean_disp)
- np.log(4)
) / np.log(2)
else:
raise NotImplementedError(
f"Found fit_type '{self.fit_type}'. Expected 'parametric' or 'mean'."
)
def deseq2(self) -> None:
"""Perform dispersion and log fold-change (LFC) estimation.
Wrapper for the first part of the PyDESeq2 pipeline.
"""
# Compute DESeq2 normalization factors using the Median-of-ratios method
self.fit_size_factors()
# Fit an independent negative binomial model per gene
self.fit_genewise_dispersions()
# Fit a parameterized trend curve for dispersions, of the form
# f(\mu) = \alpha_1/\mu + a_0
self.fit_dispersion_trend()
# Compute prior dispersion variance
self.fit_dispersion_prior()
# Refit genewise dispersions a posteriori (shrinks estimates towards trend curve)
self.fit_MAP_dispersions()
# Fit log-fold changes (in natural log scale)
self.fit_LFC()
# Compute Cooks distances to find outliers
self.calculate_cooks()
if self.refit_cooks:
# Replace outlier counts, and refit dispersions and LFCs
# for genes that had outliers replaced
self.refit()
def fit_size_factors(
self, fit_type: Literal["ratio", "iterative"] = "ratio"
) -> None:
"""Fit sample-wise deseq2 normalization (size) factors.
Uses the median-of-ratios method: see :func:`pydeseq2.preprocessing.deseq2_norm`,
unless each gene has at least one sample with zero read counts, in which case it
switches to the ``iterative`` method.
Parameters
----------
fit_type : str
The normalization method to use (default: ``"ratio"``).
"""
if not self.quiet:
print("Fitting size factors...", file=sys.stderr)
start = time.time()
if fit_type == "iterative":
self._fit_iterate_size_factors()
# Test whether it is possible to use median-of-ratios.
elif (self.X == 0).any(0).all():
# There is at least a zero for each gene
warnings.warn(
"Every gene contains at least one zero, "
"cannot compute log geometric means. Switching to iterative mode.",
UserWarning,
stacklevel=2,
)
self._fit_iterate_size_factors()
else:
self.logmeans, self.filtered_genes = deseq2_norm_fit(self.X)
(
self.layers["normed_counts"],
self.obsm["size_factors"],
) = deseq2_norm_transform(self.X, self.logmeans, self.filtered_genes)
end = time.time()
if not self.quiet:
print(f"... done in {end - start:.2f} seconds.\n", file=sys.stderr)
def fit_genewise_dispersions(self) -> None:
"""Fit gene-wise dispersion estimates.
Fits a negative binomial per gene, independently.
"""
# Check that size factors are available. If not, compute them.
if "size_factors" not in self.obsm:
self.fit_size_factors()
# Exclude genes with all zeroes
self.varm["non_zero"] = ~(self.X == 0).all(axis=0)
self.non_zero_idx = np.arange(self.n_vars)[self.varm["non_zero"]]
self.non_zero_genes = self.var_names[self.varm["non_zero"]]
if isinstance(self.non_zero_genes, pd.MultiIndex):
raise ValueError("non_zero_genes should not be a MultiIndex")
# Fit "method of moments" dispersion estimates
self._fit_MoM_dispersions()
# Convert design_matrix to numpy for speed
design_matrix = self.obsm["design_matrix"].values
# mu_hat is initialized differently depending on the number of different factor
# groups. If there are as many different factor combinations as design factors
# (intercept included), it is fitted with a linear model, otherwise it is fitted
# with a GLM (using rough dispersion estimates).
if (
len(self.obsm["design_matrix"].value_counts())
== self.obsm["design_matrix"].shape[-1]
):
mu_hat_ = self.inference.lin_reg_mu(
counts=self.X[:, self.non_zero_idx],
size_factors=self.obsm["size_factors"],
design_matrix=design_matrix,
min_mu=self.min_mu,
)
else:
_, mu_hat_, _, _ = self.inference.irls(
counts=self.X[:, self.non_zero_idx],
size_factors=self.obsm["size_factors"],
design_matrix=design_matrix,
disp=self.varm["_MoM_dispersions"][self.non_zero_idx],
min_mu=self.min_mu,
beta_tol=self.beta_tol,
)
self.layers["_mu_hat"] = np.full((self.n_obs, self.n_vars), np.NaN)
self.layers["_mu_hat"][:, self.varm["non_zero"]] = mu_hat_
if not self.quiet:
print("Fitting dispersions...", file=sys.stderr)
start = time.time()
dispersions_, l_bfgs_b_converged_ = self.inference.alpha_mle(
counts=self.X[:, self.non_zero_idx],
design_matrix=design_matrix,
mu=self.layers["_mu_hat"][:, self.non_zero_idx],
alpha_hat=self.varm["_MoM_dispersions"][self.non_zero_idx],
min_disp=self.min_disp,
max_disp=self.max_disp,
)
end = time.time()
if not self.quiet:
print(f"... done in {end - start:.2f} seconds.\n", file=sys.stderr)
self.varm["genewise_dispersions"] = np.full(self.n_vars, np.NaN)
self.varm["genewise_dispersions"][self.varm["non_zero"]] = np.clip(
dispersions_, self.min_disp, self.max_disp
)
self.varm["_genewise_converged"] = np.full(self.n_vars, np.NaN)
self.varm["_genewise_converged"][self.varm["non_zero"]] = l_bfgs_b_converged_
def fit_dispersion_trend(self) -> None:
"""Fit the dispersion trend curve.
The type of the trend curve is determined by ``self.trend_fit_type``.
"""
# Check that genewise dispersions are available. If not, compute them.
if "genewise_dispersions" not in self.varm:
self.fit_genewise_dispersions()
if not self.quiet:
print("Fitting dispersion trend curve...", file=sys.stderr)
start = time.time()
self.varm["_normed_means"] = self.layers["normed_counts"].mean(0)
if self.trend_fit_type == "parametric":
self._fit_parametric_dispersion_trend()
elif self.trend_fit_type == "mean":
self._fit_mean_dispersion_trend()
else:
raise NotImplementedError(
f"Expected 'parametric' or 'meand' trend curve fit "
f"types, received {self.trend_fit_type}"
)
end = time.time()
if not self.quiet:
print(f"... done in {end - start:.2f} seconds.\n", file=sys.stderr)
def disp_function(self, x):
"""Return the dispersion trend function at x."""
if self.uns["disp_function_type"] == "parametric":
return dispersion_trend(x, self.uns["trend_coeffs"])
elif self.uns["disp_function_type"] == "mean":
return np.full_like(x, self.uns["mean_disp"])
def fit_dispersion_prior(self) -> None:
"""Fit dispersion variance priors and standard deviation of log-residuals.
The computation is based on genes whose dispersions are above 100 * min_disp.
Note: when the design matrix has fewer than 3 degrees of freedom, the
estimate of log dispersions is likely to be imprecise.
"""
# Check that the dispersion trend curve was fitted. If not, fit it.
if "fitted_dispersions" not in self.varm:
self.fit_dispersion_trend()
# Exclude genes with all zeroes
num_samples = self.n_obs
num_vars = self.obsm["design_matrix"].shape[-1]
# Check the degrees of freedom
if (num_samples - num_vars) <= 3:
warnings.warn(
"As the residual degrees of freedom is less than 3, the distribution "
"of log dispersions is especially asymmetric and likely to be poorly "
"estimated by the MAD.",
UserWarning,
stacklevel=2,
)
# Fit dispersions to the curve, and compute log residuals
disp_residuals = np.log(
self[:, self.non_zero_genes].varm["genewise_dispersions"]
) - np.log(self[:, self.non_zero_genes].varm["fitted_dispersions"])
# Compute squared log-residuals and prior variance based on genes whose
# dispersions are above 100 * min_disp. This is to reproduce DESeq2's behaviour.
above_min_disp = self[:, self.non_zero_genes].varm["genewise_dispersions"] >= (
100 * self.min_disp
)
self.uns["_squared_logres"] = (
mean_absolute_deviation(disp_residuals[above_min_disp]) ** 2
)
self.uns["prior_disp_var"] = np.maximum(
self.uns["_squared_logres"] - polygamma(1, (num_samples - num_vars) / 2),
0.25,
)
def fit_MAP_dispersions(self) -> None:
"""Fit Maximum a Posteriori dispersion estimates.
After MAP dispersions are fit, filter genes for which we don't apply shrinkage.
"""
# Check that the dispersion prior variance is available. If not, compute it.
if "prior_disp_var" not in self.uns:
self.fit_dispersion_prior()
# Convert design matrix to numpy for speed
design_matrix = self.obsm["design_matrix"].values
if not self.quiet:
print("Fitting MAP dispersions...", file=sys.stderr)
start = time.time()
dispersions_, l_bfgs_b_converged_ = self.inference.alpha_mle(
counts=self.X[:, self.non_zero_idx],
design_matrix=design_matrix,
mu=self.layers["_mu_hat"][:, self.non_zero_idx],
alpha_hat=self.varm["fitted_dispersions"][self.non_zero_idx],
min_disp=self.min_disp,
max_disp=self.max_disp,
prior_disp_var=self.uns["prior_disp_var"].item(),
cr_reg=True,
prior_reg=True,
)
end = time.time()
if not self.quiet:
print(f"... done in {end-start:.2f} seconds.\n", file=sys.stderr)
self.varm["MAP_dispersions"] = np.full(self.n_vars, np.NaN)
self.varm["MAP_dispersions"][self.varm["non_zero"]] = np.clip(
dispersions_, self.min_disp, self.max_disp
)
self.varm["_MAP_converged"] = np.full(self.n_vars, np.NaN)
self.varm["_MAP_converged"][self.varm["non_zero"]] = l_bfgs_b_converged_
# Filter outlier genes for which we won't apply shrinkage
self.varm["dispersions"] = self.varm["MAP_dispersions"].copy()
self.varm["_outlier_genes"] = np.log(self.varm["genewise_dispersions"]) > np.log(
self.varm["fitted_dispersions"]
) + 2 * np.sqrt(self.uns["_squared_logres"])
self.varm["dispersions"][self.varm["_outlier_genes"]] = self.varm[
"genewise_dispersions"
][self.varm["_outlier_genes"]]
def fit_LFC(self) -> None:
"""Fit log fold change (LFC) coefficients.
In the 2-level setting, the intercept corresponds to the base mean,
while the second is the actual LFC coefficient, in natural log scale.
"""
# Check that MAP dispersions are available. If not, compute them.
if "dispersions" not in self.varm:
self.fit_MAP_dispersions()
# Convert design matrix to numpy for speed
design_matrix = self.obsm["design_matrix"].values
if not self.quiet:
print("Fitting LFCs...", file=sys.stderr)
start = time.time()
mle_lfcs_, mu_, hat_diagonals_, converged_ = self.inference.irls(
counts=self.X[:, self.non_zero_idx],
size_factors=self.obsm["size_factors"],
design_matrix=design_matrix,
disp=self.varm["dispersions"][self.non_zero_idx],
min_mu=self.min_mu,
beta_tol=self.beta_tol,
)
end = time.time()
if not self.quiet:
print(f"... done in {end-start:.2f} seconds.\n", file=sys.stderr)
self.varm["LFC"] = pd.DataFrame(
np.NaN,
index=self.var_names,
columns=self.obsm["design_matrix"].columns,
)
self.varm["LFC"].update(
pd.DataFrame(
mle_lfcs_,
index=self.non_zero_genes,
columns=self.obsm["design_matrix"].columns,
)
)
self.layers["_mu_LFC"] = np.full((self.n_obs, self.n_vars), np.NaN)
self.layers["_mu_LFC"][:, self.varm["non_zero"]] = mu_
self.layers["_hat_diagonals"] = np.full((self.n_obs, self.n_vars), np.NaN)
self.layers["_hat_diagonals"][:, self.varm["non_zero"]] = hat_diagonals_
self.varm["_LFC_converged"] = np.full(self.n_vars, np.NaN)
self.varm["_LFC_converged"][self.varm["non_zero"]] = converged_
def calculate_cooks(self) -> None:
"""Compute Cook's distance for outlier detection.
Measures the contribution of a single entry to the output of LFC estimation.
"""
# Check that MAP dispersions are available. If not, compute them.
if "dispersions" not in self.varm:
self.fit_MAP_dispersions()
num_vars = self.obsm["design_matrix"].shape[-1]
# Keep only non-zero genes
nonzero_data = self[:, self.non_zero_genes]
normed_counts = pd.DataFrame(
nonzero_data.X / self.obsm["size_factors"][:, None],
index=self.obs_names,
columns=self.non_zero_genes,
)
dispersions = robust_method_of_moments_disp(
normed_counts, self.obsm["design_matrix"]
)
V = (
nonzero_data.layers["_mu_LFC"]
+ dispersions.values[None, :] * nonzero_data.layers["_mu_LFC"] ** 2
)
squared_pearson_res = (nonzero_data.X - nonzero_data.layers["_mu_LFC"]) ** 2 / V
diag_mul = (
nonzero_data.layers["_hat_diagonals"]
/ (1 - nonzero_data.layers["_hat_diagonals"]) ** 2
)
self.layers["cooks"] = np.full((self.n_obs, self.n_vars), np.NaN)
self.layers["cooks"][:, self.varm["non_zero"]] = (
squared_pearson_res / num_vars * diag_mul
)
def refit(self) -> None:
"""Refit Cook outliers.
Replace values that are filtered out based on the Cooks distance with imputed
values, and then re-run the whole DESeq2 pipeline on replaced values.
"""
# Replace outlier counts
self._replace_outliers()
if not self.quiet:
print(
f"Replacing {sum(self.varm['replaced']) } outlier genes.\n",
file=sys.stderr,
)
if sum(self.varm["replaced"]) > 0:
# Refit dispersions and LFCs for genes that had outliers replaced
self._refit_without_outliers()
else:
# Store the fact that no sample was refitted
self.varm["refitted"] = np.full(
self.n_vars,
False,
)
def _fit_MoM_dispersions(self) -> None:
"""Rough method of moments initial dispersions fit.
Estimates are the max of "robust" and "method of moments" estimates.
"""
# Check that size_factors are available. If not, compute them.
if "normed_counts" not in self.layers:
self.fit_size_factors()
normed_counts = self.layers["normed_counts"][:, self.non_zero_idx]
rde = self.inference.fit_rough_dispersions(
normed_counts,
self.obsm["design_matrix"].values,
)
mde = self.inference.fit_moments_dispersions(
normed_counts, self.obsm["size_factors"]
)
alpha_hat = np.minimum(rde, mde)
self.varm["_MoM_dispersions"] = np.full(self.n_vars, np.NaN)
self.varm["_MoM_dispersions"][self.varm["non_zero"]] = np.clip(
alpha_hat, self.min_disp, self.max_disp
)
def plot_dispersions(
self, log: bool = True, save_path: Optional[str] = None, **kwargs
) -> None:
"""Plot dispersions.
Make a scatter plot with genewise dispersions, trend curve and final (MAP)
dispersions.
Parameters
----------
log : bool
Whether to log scale x and y axes (``default=True``).
save_path : str or None
The path where to save the plot. If left None, the plot won't be saved
(``default=None``).
**kwargs
Keyword arguments for the scatter plot.
"""
disps = [
self.varm["genewise_dispersions"],
self.varm["dispersions"],
self.varm["fitted_dispersions"],
]
legend_labels = ["Estimated", "Final", "Fitted"]
make_scatter(
disps,
legend_labels=legend_labels,
x_val=self.varm["_normed_means"],
log=log,
save_path=save_path,
**kwargs,
)
def _fit_parametric_dispersion_trend(self):
r"""Fit the dispersion curve according to a parametric model.
:math:`f(\mu) = \alpha_1/\mu + a_0`.
"""
# Exclude all-zero counts
targets = pd.Series(
self[:, self.non_zero_genes].varm["genewise_dispersions"].copy(),
index=self.non_zero_genes,
)
covariates = pd.Series(
1 / self[:, self.non_zero_genes].varm["_normed_means"],
index=self.non_zero_genes,
)
for gene in self.non_zero_genes:
if (
np.isinf(covariates.loc[gene]).any()
or np.isnan(covariates.loc[gene]).any()
):
targets.drop(labels=[gene], inplace=True)
covariates.drop(labels=[gene], inplace=True)
# Initialize coefficients
old_coeffs = pd.Series([0.1, 0.1])
coeffs = pd.Series([1.0, 1.0])
while (coeffs > 1e-10).all() and (
np.log(np.abs(coeffs / old_coeffs)) ** 2
).sum() >= 1e-6:
old_coeffs = coeffs
coeffs, predictions, converged = self.inference.dispersion_trend_gamma_glm(
covariates, targets
)
if not converged or (coeffs <= 1e-10).any():
warnings.warn(
"The dispersion trend curve fitting did not converge. "
"Switching to a mean-based dispersion trend.",
UserWarning,
stacklevel=2,
)
self._fit_mean_dispersion_trend()
return
# Filter out genes that are too far away from the curve before refitting
pred_ratios = (
self[:, covariates.index].varm["genewise_dispersions"] / predictions
)
targets.drop(
targets[(pred_ratios < 1e-4) | (pred_ratios >= 15)].index,
inplace=True,
)
covariates.drop(
covariates[(pred_ratios < 1e-4) | (pred_ratios >= 15)].index,
inplace=True,
)
self.uns["trend_coeffs"] = pd.Series(coeffs, index=["a0", "a1"])
self.varm["fitted_dispersions"] = np.full(self.n_vars, np.NaN)
self.uns["disp_function_type"] = "parametric"
self.varm["fitted_dispersions"][self.varm["non_zero"]] = self.disp_function(
self.varm["_normed_means"][self.varm["non_zero"]]
)
def _fit_mean_dispersion_trend(self):
"""Use the mean of dispersions as trend curve."""
self.uns["mean_disp"] = trim_mean(
self.varm["genewise_dispersions"][
self.varm["genewise_dispersions"] > 10 * self.min_disp
],
proportiontocut=0.001,
)
self.uns["disp_function_type"] = "mean"
self.varm["fitted_dispersions"] = np.full(self.n_vars, self.uns["mean_disp"])
def _replace_outliers(self) -> None:
"""Replace values that are filtered out (based on Cooks) with imputed values."""
# Check that cooks distances are available. If not, compute them.
if "cooks" not in self.layers:
self.calculate_cooks()
num_samples = self.n_obs
num_vars = self.obsm["design_matrix"].shape[1]
# Check whether cohorts have enough samples to allow refitting
self.obsm["replaceable"] = n_or_more_replicates(
self.obsm["design_matrix"], self.min_replicates
).values
if self.obsm["replaceable"].sum() == 0:
# No sample can be replaced. Set self.replaced to False and exit.
self.varm["replaced"] = pd.Series(False, index=self.var_names)
return
# Get positions of counts with cooks above threshold
cooks_cutoff = f.ppf(0.99, num_vars, num_samples - num_vars)
idx = self.layers["cooks"] > cooks_cutoff
self.varm["replaced"] = idx.any(axis=0)
if sum(self.varm["replaced"] > 0):
# Compute replacement counts: trimmed means * size_factors
self.counts_to_refit = self[:, self.varm["replaced"]].copy()
trim_base_mean = pd.DataFrame(
cast(
np.ndarray,
trimmed_mean(
self.counts_to_refit.X / self.obsm["size_factors"][:, None],
trim=0.2,
axis=0,
),
),
index=self.counts_to_refit.var_names,
)
replacement_counts = (
pd.DataFrame(
trim_base_mean.values * self.obsm["size_factors"],
index=self.counts_to_refit.var_names,
columns=self.counts_to_refit.obs_names,
)