forked from satijalab/seurat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixscape.R
1346 lines (1292 loc) · 48.1 KB
/
mixscape.R
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
#' @include generics.R
#'
NULL
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Calculate a perturbation Signature
#'
#' Function to calculate perturbation signature for pooled CRISPR screen datasets.
#' For each target cell (expressing one target gRNA), we identified 20 cells
#' from the control pool (non-targeting cells) with the most similar mRNA
#' expression profiles. The perturbation signature is calculated by subtracting the
#' averaged mRNA expression profile of the non-targeting neighbors from the mRNA
#' expression profile of the target cell.
#'
#' @param object An object of class Seurat.
#' @param assay Name of Assay PRTB signature is being calculated on.
#' @param features Features to compute PRTB signature for. Defaults to the
#' variable features set in the assay specified.
#' @param slot Data slot to use for PRTB signature calculation.
#' @param gd.class Metadata column containing target gene classification.
#' @param nt.cell.class Non-targeting gRNA cell classification identity.
#' @param split.by Provide metadata column if multiple biological replicates
#' exist to calculate PRTB signature for every replicate separately.
#' @param num.neighbors Number of nearest neighbors to consider.
#' @param ndims Number of dimensions to use from dimensionality reduction method.
#' @param reduction Reduction method used to calculate nearest neighbors.
#' @param new.assay.name Name for the new assay.
#' @param verbose Display progress + messages
#' @return Returns a Seurat object with a new assay added containing the
#' perturbation signature for all cells in the data slot.
#'
#' @importFrom RANN nn2
#' @export
#' @concept mixscape
#'
CalcPerturbSig <- function(
object,
assay = NULL,
features = NULL,
slot = "data",
gd.class = "guide_ID",
nt.cell.class = "NT",
split.by = NULL,
num.neighbors = NULL,
reduction = "pca",
ndims = 15,
new.assay.name = "PRTB",
verbose = TRUE
) {
assay <- assay %||% DefaultAssay(object = object )
if (is.null(x = reduction)) {
stop('Please provide dimensionality reduction name.')
}
if (is.null(x = num.neighbors)) {
stop("Please specify number of nearest neighbors to consider")
}
if (is.null(x = ndims)) {
stop("Please provide number of ", reduction, " dimensions to consider")
}
features <- features %||% VariableFeatures(object = object[[assay]])
if (length(x = features) == 0) {
features <- rownames(x = GetAssayData(object = object[[assay]], slot = slot))
}
if (! is.null(x = split.by)) {
Idents(object = object) <- split.by
} else {
Idents(object = object) <- "rep1"
}
replicate <- unique(x = Idents(object = object))
all_diff <- list()
all_nt_cells <- Cells(x = object)[which(x = object[[]][gd.class] == nt.cell.class)]
all_neighbors <- list()
for (r in replicate) {
if (verbose) {
message("Processing ", r)
}
all_cells <- WhichCells(object = object, idents = r)
nt_cells <- intersect(x = all_nt_cells, all_cells)
# get pca cell embeddings
all_mtx <- Embeddings(object = object, reduction = reduction)[all_cells, ]
nt_mtx <- Embeddings(object = object, reduction = reduction)[nt_cells, ]
# run nn2 to find the 20 nearest NT neighbors for all cells. Use the same
# number of PCs as the ones you used for umap
neighbors <- NNHelper(
data = nt_mtx[, 1:ndims],
query = all_mtx[, 1:ndims],
k = num.neighbors,
method = "rann"
)
diff <- PerturbDiff(
object = object,
assay = assay,
slot = slot,
all_cells = all_cells,
nt_cells = nt_cells,
features = features,
neighbors = neighbors,
verbose = verbose
)
all_diff[[r]] <- diff
all_neighbors[[make.names(names = paste0(new.assay.name, "_", r))]] <- neighbors
}
slot(object = object, name = "tools")[[paste("CalcPerturbSig", assay, reduction, sep = ".")]] <- all_neighbors
all_diff <- do.call(what = cbind, args = all_diff)
prtb.assay <- suppressWarnings(
# TODO: restore once check.matrix is in SeuratObject
# expr = CreateAssayObject(
# data = all_diff[, colnames(x = object)],
# min.cells = -Inf,
# min.features = -Inf,
# check.matrix = FALSE
# )
expr = CreateAssayObject(
data = all_diff[, colnames(x = object)],
min.cells = -Inf,
min.features = -Inf
)
)
object[[new.assay.name]] <- prtb.assay
object <- LogSeuratCommand(object = object)
return(object)
}
#' DE and EnrichR pathway visualization barplot
#'
#' @inheritParams FindMarkers
#' @param object Name of object class Seurat.
#' @param ident.1 Cell class identity 1.
#' @param ident.2 Cell class identity 2.
#' @param balanced Option to display pathway enrichments for both negative and
#' positive DE genes.If false, only positive DE gene will be displayed.
#' @param max.genes Maximum number of genes to use as input to enrichR.
#' @param p.val.cutoff Cutoff to select DE genes.
#' @param cols A list of colors to use for barplots.
#' @param enrich.database Database to use from enrichR.
#' @param num.pathway Number of pathways to display in barplot.
#' @param return.gene.list Return list of DE genes
#'
#' @return Returns one (only enriched) or two (both enriched and depleted)
#' barplots with the top enriched/depleted GO terms from EnrichR.
#'
#' @importFrom ggplot2 ggplot geom_bar geom_density coord_flip scale_fill_manual
#' ylab ggtitle theme_classic theme element_text
#' @importFrom patchwork wrap_plots
#'
#' @export
#' @concept mixscape
DEenrichRPlot <- function(
object,
ident.1 = NULL,
ident.2 = NULL,
balanced = TRUE,
logfc.threshold = 0.25,
assay = NULL,
max.genes,
test.use = 'wilcox',
p.val.cutoff = 0.05,
cols = NULL,
enrich.database = NULL,
num.pathway = 10,
return.gene.list = FALSE,
...
) {
enrichr.installed <- PackageCheck("enrichR", error = FALSE)
if (!enrichr.installed[1]) {
stop(
"Please install the enrichR package to use DEenrichRPlot",
"\nThis can be accomplished with the following command: ",
"\n----------------------------------------",
"\ninstall.packages('enrichR')",
"\n----------------------------------------",
call. = FALSE
)
}
if (is.null(x = enrich.database)) {
stop("Please specify the name of enrichR database to use")
}
if (!is.numeric(x = max.genes)) {
stop("please set max.genes")
}
assay <- assay %||% DefaultAssay(object = object)
DefaultAssay(object = object) <- assay
all.markers <- FindMarkers(
object = object,
ident.1 = ident.1,
ident.2 = ident.2,
only.pos = FALSE,
logfc.threshold = logfc.threshold,
test.use = test.use,
assay = assay
)
pos.markers <- all.markers[all.markers[, 2] > logfc.threshold & all.markers[, 1] < p.val.cutoff, , drop = FALSE]
if(nrow(pos.markers) == 0){
message("No positive markers pass the logfc.thershold")
pos.er <- c()
}
else{
pos.markers.list <- rownames(x = pos.markers)[1:min(max.genes, nrow(x = pos.markers))]
pos.er <- enrichR::enrichr(genes = pos.markers.list, databases = enrich.database)
pos.er <- do.call(what = cbind, args = pos.er)
pos.er$log10pval <- -log10(x = pos.er[, paste(enrich.database, sep = ".", "P.value")])
pos.er$term <- pos.er[, paste(enrich.database, sep = ".", "Term")]
pos.er <- pos.er[1:num.pathway, ]
pos.er$term <- factor(x = pos.er$term, levels = pos.er$term[order(pos.er$log10pval)])
gene.list <- list(pos = pos.er)
}
if (isTRUE(x = balanced)) {
neg.markers <- all.markers[all.markers[, 2] < logfc.threshold & all.markers[, 1] < p.val.cutoff, , drop = FALSE]
neg.markers.list <- rownames(x = neg.markers)[1:min(max.genes, nrow(x = neg.markers))]
neg.er <- enrichR::enrichr(genes = neg.markers.list, databases = enrich.database)
neg.er <- do.call(what = cbind, args = neg.er)
neg.er$log10pval <- -log10(x = neg.er[, paste(enrich.database, sep = ".", "P.value")])
neg.er$term <- neg.er[, paste(enrich.database, sep = ".", "Term")]
neg.er <- neg.er[1:num.pathway, ]
neg.er$term <- factor(x = neg.er$term, levels = neg.er$term[order(neg.er$log10pval)])
if(isTRUE(length(neg.er$term) == 0) & isTRUE(length(pos.er == 0))){
stop("No positive or negative marker genes identified")
}
else{
if(isTRUE(length(neg.er$term) == 0)){
gene.list <- list(pos = pos.er)
}
else{
gene.list <- list(pos = pos.er, neg = neg.er)
}
}
}
if (return.gene.list) {
return(gene.list)
}
if(nrow(pos.markers) == 0){
message("No positive markers to plot")
if (isTRUE(x = balanced)) {
p2 <- ggplot(data = neg.er, aes_string(x = "term", y = "log10pval")) +
geom_bar(stat = "identity", fill = "indianred2") +
coord_flip() + xlab("Pathway") +
scale_fill_manual(values = cols, drop = FALSE) +
ylab("-log10(pval)") +
ggtitle(paste(enrich.database, ident.1, sep = "_", "negative markers")) +
theme_classic() +
geom_text(aes_string(label = "term", y = 0),
size = 5,
color = "black",
position = position_dodge(1),
hjust = 0)+
theme(axis.title.y= element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p <- p2
}
else{
stop("Nothing to plot")
}
}
else {
p <- ggplot(data = pos.er, aes_string(x = "term", y = "log10pval")) +
geom_bar(stat = "identity", fill = "dodgerblue") +
coord_flip() + xlab("Pathway") +
scale_fill_manual(values = cols, drop = FALSE) +
ylab("-log10(pval)") +
ggtitle(paste(enrich.database, ident.1, sep = "_", "positive markers")) +
theme_classic() +
geom_text(aes_string(label = "term", y = 0),
size = 5,
color = "black",
position = position_dodge(1),
hjust = 0)+
theme(axis.title.y= element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
if (isTRUE(x = balanced)) {
p2 <- ggplot(data = neg.er, aes_string(x = "term", y = "log10pval")) +
geom_bar(stat = "identity", fill = "indianred2") +
coord_flip() + xlab("Pathway") +
scale_fill_manual(values = cols, drop = FALSE) +
ylab("-log10(pval)") +
ggtitle(paste(enrich.database, ident.1, sep = "_", "negative markers")) +
theme_classic() +
geom_text(aes_string(label = "term", y = 0),
size = 5,
color = "black",
position = position_dodge(1),
hjust = 0)+
theme(axis.title.y= element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p <- p+p2
}
}
return(p)
}
#' Linear discriminant analysis on pooled CRISPR screen data.
#'
#' This function performs unsupervised PCA on each mixscape class separately and projects each subspace onto all
#' cells in the data. Finally, it uses the first 10 principle components from each projection as input to lda in MASS package together with mixscape class labels.
#'
#' @inheritParams PrepLDA
#' @inheritParams RunLDA
#'
#' @return Returns a Seurat object with LDA added in the reduction slot.
#'
#' @export
#' @concept mixscape
#'
MixscapeLDA <- function(
object,
assay = NULL,
ndims.print = 1:5,
nfeatures.print = 30,
reduction.key = "LDA_",
seed = 42,
pc.assay = "PRTB",
labels = "gene",
nt.label = "NT",
npcs = 10,
verbose = TRUE,
logfc.threshold = 0.25
) {
projected_pcs <- PrepLDA(
object = object,
de.assay = assay,
pc.assay = pc.assay,
labels = labels,
nt.label = nt.label,
npcs = npcs ,
verbose = verbose
)
lda.lables <- object[[labels]][,]
object_lda <- RunLDA(
object = projected_pcs,
labels = lda.lables,
assay = assay,
verbose = verbose
)
object[["lda"]] <- object_lda
return(object)
}
#' Function to prepare data for Linear Discriminant Analysis.
#'
#' This function performs unsupervised PCA on each mixscape class separately and projects each subspace onto all
#' cells in the data.
#'
#' @param object An object of class Seurat.
#' @param de.assay Assay to use for selection of DE genes.
#' @param pc.assay Assay to use for running Principle components analysis.
#' @param labels Meta data column with target gene class labels.
#' @param nt.label Name of non-targeting cell class.
#' @param npcs Number of principle components to use.
#' @param verbose Print progress bar.
#' @inheritParams FindMarkers
#' @return Returns a list of the first 10 PCs from each projection.
#'
#' @export
#' @concept mixscape
#'
PrepLDA <- function(
object,
de.assay = "RNA",
pc.assay = "PRTB",
labels = "gene",
nt.label = "NT",
npcs = 10,
verbose = TRUE,
logfc.threshold = 0.25
) {
projected_pcs <- list()
gene_list <- setdiff(x = unique(x = object[[labels]][, 1]), y = nt.label)
Idents(object = object) <- labels
DefaultAssay(object = object) <- pc.assay
all_genes <- list()
nt.cells <- WhichCells(object = object, idents = nt.label)
for (g in gene_list) {
if (verbose) {
message(g)
}
gd.cells <- WhichCells(object = object, idents = g)
gene_set <- TopDEGenesMixscape(
object = object,
ident.1 = gd.cells,
ident.2 = nt.cells,
de.assay = de.assay,
logfc.threshold = logfc.threshold,
labels = labels,
verbose = verbose
)
if (length(x = gene_set) < (npcs + 1)) {
all_genes[[g]] <- character()
next
}
all_genes[[g]] <- gene_set
}
all_markers <- unique(x = unlist(x = all_genes))
missing_genes <- all_markers[!all_markers %in% rownames(x = object[[pc.assay]])]
object <- GetMissingPerturb(object = object, assay = pc.assay, features = missing_genes, verbose = verbose)
for (g in gene_list) {
if (verbose) {
message(g)
}
gene_subset <- subset(x = object, idents = c(g, nt.label))
gene_set <- all_genes[[g]]
if (length(x = gene_set) == 0) {
next
}
gene_subset <- ScaleData(
object = gene_subset,
features = gene_set,
verbose = FALSE
)
gene_subset <- RunPCA(
object = gene_subset,
features = gene_set,
npcs = npcs,
verbose = FALSE
)
project_pca <- ProjectCellEmbeddings(
reference = gene_subset,
query = object,
dims = 1:npcs,
verbose = FALSE
)
colnames(x = project_pca) <- paste(g, colnames(x = project_pca), sep = "_")
projected_pcs[[g]] <- project_pca
}
return(projected_pcs)
}
#' @param object Input values for LDA (numeric), with observations as rows
#' @param labels Observation labels for LDA
#' @param assay Name of Assay LDA is being run on
#' @param ndims.print PCs to print genes for
#' @param nfeatures.print Number of genes to print for each PC
#' @param reduction.key dimensional reduction key, specifies the string before
#' the number for the dimension names. LDA by default
#' @param seed Set a random seed. By default, sets the seed to 42. Setting
#' NULL will not set a seed.
#'
#' @importFrom MASS lda
#' @importFrom stats predict
#'
#' @rdname RunLDA
#' @concept mixscape
#' @export
#' @method RunLDA default
#'
RunLDA.default <- function(
object,
labels,
assay = NULL,
verbose = TRUE,
ndims.print = 1:5,
nfeatures.print = 30,
reduction.key = "LDA_",
seed = 42,
...
) {
if (!is.null(x = seed)) {
set.seed(seed = seed)
}
object <- data.frame(object)
var_names <- colnames(x = object)
object$lda_cluster_label <- labels
lda_results <- lda(formula = lda_cluster_label ~ ., data = object)
lda_predictions <- predict(object = lda_results, newdata = object)
lda_cv <-lda(
formula = lda_cluster_label ~ .,
data = object,
CV = TRUE
)$posterior
feature.loadings <- lda_results$scaling
cell.embeddings <- lda_predictions$x
lda.assignments <- lda_predictions$class
lda.posterior <- lda_predictions$posterior
colnames(x = lda.posterior) <- paste0("LDAP_", colnames(x = lda.posterior))
rownames(x = feature.loadings) <- var_names
colnames(x = feature.loadings) <- paste0(reduction.key, 1:ncol(x = cell.embeddings))
rownames(x = cell.embeddings) <- rownames(x = object)
colnames(x = cell.embeddings) <- colnames(x = feature.loadings)
reduction.data <- CreateDimReducObject(
embeddings = cell.embeddings,
loadings = feature.loadings,
assay = assay,
key = reduction.key,
misc = list(
assignments = lda.assignments,
posterior = lda.posterior,
model = lda_results,
cv = lda_cv
)
)
if (verbose) {
print(x = reduction.data, dims = ndims.print, nfeatures = nfeatures.print)
}
return(reduction.data)
}
#' Function to perform Linear Discriminant Analysis.
#'
#' @param ndims.print Number of LDA dimensions to print.
#' @param nfeatures.print Number of features to print for each LDA component.
#' @param reduction.key Reduction key name.
#'
#' @rdname RunLDA
#' @concept mixscape
#' @export
#' @method RunLDA Assay
#'
RunLDA.Assay <- function(
object,
assay = NULL,
labels,
features = NULL,
verbose = TRUE,
ndims.print = 1:5,
nfeatures.print = 30,
reduction.key = "LDA_",
seed = 42,
...
) {
data.use <- PrepDR(
object = object,
features = features,
verbose = verbose
)
reduction.data <- RunLDA(
object = t(x = data.use),
assay = assay,
labels = labels,
verbose = verbose,
ndims.print = ndims.print,
nfeatures.print = nfeatures.print,
reduction.key = reduction.key,
seed = seed,
...
)
return(reduction.data)
}
#' @param object An object of class Seurat.
#' @param assay Assay to use for performing Linear Discriminant Analysis (LDA).
#' @param labels Meta data column with target gene class labels.
#' @param features Features to compute LDA on
#' @param reduction.name dimensional reduction name, lda by default
#' @param reduction.key Reduction key name.
#' @param seed Value for random seed
#' @param verbose Print the top genes associated with high/low loadings for
#' the PCs
#' @param ndims.print Number of LDA dimensions to print.
#' @param nfeatures.print Number of features to print for each LDA component.
#'
#' @rdname RunLDA
#' @concept mixscape
#' @export
#' @method RunLDA Seurat
#'
RunLDA.Seurat <- function(
object,
assay = NULL,
labels,
features = NULL,
reduction.name = "lda",
reduction.key = "LDA_",
seed = 42,
verbose = TRUE,
ndims.print = 1:5,
nfeatures.print = 30,
...
) {
assay <- assay %||% DefaultAssay(object = object)
assay.data <- GetAssay(object = object, assay = assay)
reduction.data <- RunLDA(
object = assay.data,
assay = assay,
labels = labels,
features = features,
verbose = verbose,
ndims.print = ndims.print,
nfeatures.print = nfeatures.print,
reduction.key = reduction.key,
seed = seed,
...
)
object[[reduction.name]] <- reduction.data
object$lda.assignments <- slot(object = object[[reduction.name]], name = "misc")[["assignments"]]
object <- AddMetaData(
object = object,
metadata = as.data.frame(
x = slot(object = object[[reduction.name]], name = "misc")[["posterior"]]
)
)
object <- LogSeuratCommand(object = object)
object <- ProjectDim(
object = object,
reduction = reduction.name,
assay = assay,
verbose = verbose,
dims.print = ndims.print,
nfeatures.print = nfeatures.print
)
Loadings(object = object[[reduction.name]]) <- Loadings(
object = object[[reduction.name]],
projected = TRUE
)
return(object)
}
#' Run Mixscape
#'
#' Function to identify perturbed and non-perturbed gRNA expressing cells that
#' accounts for multiple treatments/conditions/chemical perturbations.
#'
#' @inheritParams FindMarkers
#' @importFrom ggplot2 geom_density position_dodge
#' @param object An object of class Seurat.
#' @param assay Assay to use for mixscape classification.
#' @param slot Assay data slot to use.
#' @param labels metadata column with target gene labels.
#' @param nt.class.name Classification name of non-targeting gRNA cells.
#' @param new.class.name Name of mixscape classification to be stored in
#' metadata.
#' @param min.de.genes Required number of genes that are differentially
#' expressed for method to separate perturbed and non-perturbed cells.
#' @param min.cells Minimum number of cells in target gene class. If fewer than
#' this many cells are assigned to a target gene class during classification,
#' all are assigned NP.
#' @param de.assay Assay to use when performing differential expression analysis.
#' Usually RNA.
#' @param iter.num Number of normalmixEM iterations to run if convergence does
#' not occur.
#' @param verbose Display messages
#' @param split.by metadata column with experimental condition/cell type
#' classification information. This is meant to be used to account for cases a
#' perturbation is condition/cell type -specific.
#' @param fine.mode When this is equal to TRUE, DE genes for each target gene
#' class will be calculated for each gRNA separately and pooled into one DE list
#' for calculating the perturbation score of every cell and their subsequent
#' classification.
#' @param fine.mode.labels metadata column with gRNA ID labels.
#' @param prtb.type specify type of CRISPR perturbation expected for labeling mixscape classifications. Default is KO.
#' @return Returns Seurat object with with the following information in the
#' meta data and tools slots:
#' \describe{
#' \item{mixscape_class}{Classification result with cells being either
#' classified as perturbed (KO, by default) or non-perturbed (NP) based on their target
#' gene class.}
#' \item{mixscape_class.global}{Global classification result (perturbed, NP or NT)}
#' \item{p_ko}{Posterior probabilities used to determine if a cell is KO (default). Name of this item will change to match prtb.type parameter setting.
#' (>0.5) or NP}
#' \item{perturbation score}{Perturbation scores for every cell calculated in
#' the first iteration of the function.}
#' }
#'
#' @export
#' @concept mixscape
#'
RunMixscape <- function(
object,
assay = "PRTB",
slot = "scale.data",
labels = "gene",
nt.class.name = "NT",
new.class.name = "mixscape_class",
min.de.genes = 5,
min.cells = 5,
de.assay = "RNA",
logfc.threshold = 0.25,
iter.num = 10,
verbose = FALSE,
split.by = NULL,
fine.mode = FALSE,
fine.mode.labels = "guide_ID",
prtb.type = "KO"
) {
mixtools.installed <- PackageCheck("mixtools", error = FALSE)
if (!mixtools.installed[1]) {
stop("Please install the mixtools package to use RunMixscape",
"\nThis can be accomplished with the following command: ",
"\n----------------------------------------",
"\ninstall.packages('mixtools')",
"\n----------------------------------------", call. = FALSE)
}
assay <- assay %||% DefaultAssay(object = object)
if (is.null(x = labels)) {
stop("Please specify target gene class metadata name")
}
prtb_markers <- list()
object[[new.class.name]] <- object[[labels]]
object[[new.class.name]][, 1] <- as.character(x = object[[new.class.name]][, 1])
object[[paste0(new.class.name, "_p_", tolower(x = prtb.type))]] <- 0
#create list to store perturbation scores.
gv.list <- list()
if (is.null(x = split.by)) {
split.by <- splits <- "con1"
} else {
splits <- as.character(x = unique(x = object[[split.by]][, 1]))
}
# determine gene sets across all splits/groups
cells.s.list <- list()
for (s in splits) {
Idents(object = object) <- split.by
cells.s <- WhichCells(object = object, idents = s)
cells.s.list[[s]] <- cells.s
genes <- setdiff(x = unique(x = object[[labels]][cells.s, 1]), y = nt.class.name)
Idents(object = object) <- labels
for (gene in genes) {
if (isTRUE(x = verbose)) {
message("Processing ", gene)
}
orig.guide.cells <- intersect(x = WhichCells(object = object, idents = gene), y = cells.s)
nt.cells <- intersect(x = WhichCells(object = object, idents = nt.class.name), y = cells.s)
if (isTRUE(x = fine.mode)) {
guides <- setdiff(x = unique(x = object[[fine.mode.labels]][orig.guide.cells, 1]), y = nt.class.name)
all.de.genes <- c()
for (gd in guides) {
gd.cells <- rownames(x = object[[]][orig.guide.cells, ])[which(x = object[[]][orig.guide.cells, fine.mode.labels] == gd)]
de.genes <- TopDEGenesMixscape(
object = object,
ident.1 = gd.cells,
ident.2 = nt.cells,
de.assay = de.assay,
logfc.threshold = logfc.threshold,
labels = fine.mode.labels,
verbose = verbose
)
all.de.genes <- c(all.de.genes, de.genes)
}
all.de.genes <- unique(all.de.genes)
} else {
all.de.genes <- TopDEGenesMixscape(
object = object,
ident.1 = orig.guide.cells,
ident.2 = nt.cells,
de.assay = de.assay,
logfc.threshold = logfc.threshold,
labels = labels,
verbose = verbose
)
}
prtb_markers[[s]][[gene]] <- all.de.genes
if (length(x = all.de.genes) < min.de.genes) {
prtb_markers[[s]][[gene]] <- character()
}
}
}
all_markers <- unique(x = unlist(x = prtb_markers))
missing_genes <- all_markers[!all_markers %in% rownames(x = object[[assay]])]
object <- GetMissingPerturb(object = object, assay = assay, features = missing_genes, verbose = verbose)
for (s in splits) {
cells.s <- cells.s.list[[s]]
genes <- setdiff(x = unique(x = object[[labels]][cells.s, 1]), y = nt.class.name)
if (verbose) {
message("Classifying cells for: ")
}
for (gene in genes) {
Idents(object = object) <- labels
post.prob <- 0
orig.guide.cells <- intersect(x = WhichCells(object = object, idents = gene), y = cells.s)
nt.cells <- intersect(x = WhichCells(object = object, idents = nt.class.name), y = cells.s)
all.cells <- c(orig.guide.cells, nt.cells)
if (length(x = prtb_markers[[s]][[gene]]) == 0) {
if (verbose) {
message(" Fewer than ", min.de.genes, " DE genes for ", gene,
". Assigning cells as NP.")
}
object[[new.class.name]][orig.guide.cells, 1] <- paste0(gene, " NP")
} else {
if (verbose) {
message(" ", gene)
}
de.genes <- prtb_markers[[s]][[gene]]
dat <- GetAssayData(object = object[[assay]], slot = "data")[de.genes, all.cells, drop = FALSE]
if (slot == "scale.data") {
dat <- ScaleData(object = dat, features = de.genes, verbose = FALSE)
}
converged <- FALSE
n.iter <- 0
old.classes <- object[[new.class.name]][all.cells, ]
while (!converged && n.iter < iter.num) {
Idents(object = object) <- new.class.name
guide.cells <- intersect(x = WhichCells(object = object, idents = gene), y = cells.s)
vec <- rowMeans2(x = dat[, guide.cells, drop = FALSE]) - rowMeans2(x = dat[, nt.cells, drop = FALSE])
pvec <- apply(X = dat, MARGIN = 2, FUN = ProjectVec, v2 = vec)
if (n.iter == 0){
#store pvec
gv <- as.data.frame(x = pvec)
gv[, labels] <- nt.class.name
gv[intersect(x = rownames(x = gv), y = guide.cells), labels] <- gene
gv.list[[gene]][[s]] <- gv
}
guide.norm <- DefineNormalMixscape(pvec[guide.cells])
nt.norm <- DefineNormalMixscape(pvec[nt.cells])
mm <- mixtools::normalmixEM(
x = pvec,
mu = c(nt.norm$mu, guide.norm$mu),
sigma = c(nt.norm$sd, guide.norm$sd),
k = 2,
mean.constr = c(nt.norm$mu, NA),
sd.constr = c(nt.norm$sd, NA),
verb = FALSE,
maxit = 5000,
maxrestarts = 100
)
lik.ratio <- dnorm(x = pvec[orig.guide.cells], mean = mm$mu[1], sd = mm$sigma[1]) /
dnorm(x = pvec[orig.guide.cells], mean = mm$mu[2], sd = mm$sigma[2])
post.prob <- 1/(1 + lik.ratio)
object[[new.class.name]][names(x = which(post.prob > 0.5)), 1] <- gene
object[[new.class.name]][names(x = which(post.prob < 0.5)), 1] <- paste(gene, " NP", sep = "")
if (length(x = which(x = object[[new.class.name]] == gene & Cells(x = object) %in% cells.s)) < min.de.genes) {
if (verbose) {
message("Fewer than ", min.cells, " cells assigned as ",
gene, "Assigning all to NP.")
}
object[[new.class.name]][guide.cells, 1] <- "NP"
converged <- TRUE
}
if (all(object[[new.class.name]][all.cells, ] == old.classes)) {
converged <- TRUE
}
old.classes <- object[[new.class.name]][all.cells, ]
n.iter <- n.iter + 1
}
object[[new.class.name]][which(x = object[[new.class.name]] == gene & Cells(x = object) %in% cells.s), 1] <- paste(gene, prtb.type, sep = " ")
}
object[[paste0(new.class.name, ".global")]] <- as.character(x = sapply(X = as.character(x = object[[new.class.name]][, 1]), FUN = function(x) {strsplit(x = x, split = " (?=[^ ]+$)", perl = TRUE)[[1]][2]}))
object[[paste0(new.class.name, ".global")]][which(x = is.na(x = object[[paste0(new.class.name, ".global")]])), 1] <- nt.class.name
object[[paste0(new.class.name,"_p_", tolower(prtb.type))]][names(x = post.prob), 1] <- post.prob
}
}
Tool(object = object) <- gv.list
Idents(object = object) <- new.class.name
return(object)
}
#' Differential expression heatmap for mixscape
#'
#' Draws a heatmap of single cell feature expression with cells ordered by their
#' mixscape ko probabilities.
#'
#' @inheritParams FindMarkers
#' @inheritParams DoHeatmap
#' @param max.cells.group Number of cells per identity to plot.
#' @param max.genes Total number of DE genes to plot.
#' @param balanced Plot an equal number of genes with both groups of cells.
#' @param order.by.prob Order cells on heatmap based on their mixscape knockout
#' probability from highest to lowest score.
#' @param group.by (Deprecated) Option to split densities based on mixscape
#' classification. Please use mixscape.class instead
#' @param mixscape.class metadata column with mixscape classifications.
#' @param prtb.type specify type of CRISPR perturbation expected for labeling
#' mixscape classifications. Default is KO.
#' @param fc.name Name of the fold change, average difference, or custom
#' function column in the output data.frame. Default is avg_log2FC
#' @param pval.cutoff P-value cut-off for selection of significantly DE genes.
#' @return A ggplot object.
#'
#' @importFrom stats median
#' @importFrom scales hue_pal
#' @importFrom ggplot2 annotation_raster coord_cartesian ggplot_build aes_string
#' @export
#' @concept mixscape
#'
MixscapeHeatmap <- function(
object,
ident.1 = NULL,
ident.2 = NULL,
balanced = TRUE,
logfc.threshold = 0.25,
assay = "RNA",
max.genes = 100,
test.use ='wilcox',
max.cells.group = NULL,
order.by.prob = TRUE,
group.by = NULL,
mixscape.class = "mixscape_class",
prtb.type = "KO",
fc.name = "avg_log2FC",
pval.cutoff = 5e-2,
...
)
{
if (!is.null(x = group.by)) {
message("The group.by parameter is being deprecated. Please use ",
"mixscape.class instead. Setting mixscape.class = ", group.by,
" and continuing.")
mixscape.class <- group.by
}
DefaultAssay(object = object) <- assay
if (is.numeric(x = max.genes)) {
all.markers <- FindMarkers(
object = object,
ident.1 = ident.1,
ident.2 = ident.2,
only.pos = FALSE,
logfc.threshold = logfc.threshold,
test.use = test.use
)
if (balanced) {
pos.markers <- all.markers[which(x = all.markers[,fc.name] > (logfc.threshold)), ]
neg.markers <- all.markers[which(x = all.markers[,fc.name] < (-logfc.threshold)), ]
if (length(x = rownames(x = subset(x = pos.markers, p_val < pval.cutoff))) < max.genes ) {
marker.list <- c(rownames(x = subset(x = pos.markers, p_val < pval.cutoff)))
if (length(x = rownames(x = subset(x = neg.markers, p_val < pval.cutoff))) < max.genes){
marker.list <- c(marker.list, rownames(x = subset(x = neg.markers, p_val < pval.cutoff)))
} else {
marker.list <- c(marker.list, rownames(x = subset(x = neg.markers, p_val < pval.cutoff))[1:max.genes])
}
} else {
marker.list <- c(rownames(x = subset(x = pos.markers, p_val < pval.cutoff))[1:max.genes])
if (length(x = rownames(x = subset(x = neg.markers, p_val < pval.cutoff))) < max.genes) {
marker.list <- c(marker.list, rownames(x = subset(x = neg.markers, p_val < pval.cutoff)))
} else {
marker.list <- c(marker.list, rownames(x = subset(x = neg.markers, p_val < pval.cutoff))[1:max.genes])
}
}
}
else {
pos.markers <- all.markers[which(x = all.markers[, fc.name] > (logfc.threshold)),]
if (length(x = rownames(x = subset(x = pos.markers, p_val < pval.cutoff))) < max.genes ){
marker.list <- c(rownames(x = subset(x = pos.markers, p_val < pval.cutoff)))
} else {
marker.list <- c(rownames(x = subset(x = pos.markers, p_val < pval.cutoff))[1:max.genes])
}
}
if (is.null(x = max.cells.group)) {
if (is.null(x = group.by)) {
sub2 <- subset(x = object, idents = c(ident.1, ident.2))
} else{
sub2 <- subset(x = object, idents = c(ident.1, ident.2))
Idents(object = sub2) <- group.by
}
}
else {
if (is.null(x = group.by)) {
sub2 <- subset(x = object, idents = c(ident.1, ident.2), downsample = max.cells.group)
} else {
sub <- subset(x = object, idents = c(ident.1, ident.2))
Idents(object = sub) <- group.by
sub2 <- subset(x = sub, downsample = max.cells.group)
}
}
sub2 <- ScaleData(object = sub2, features = marker.list, assay = assay)
if (isTRUE(x = order.by.prob)) {
p_ko <- sub2[[paste0(mixscape.class, "_p_", tolower(x = prtb.type) )]][, 1, drop = FALSE]
ordered.cells <- rownames(x = p_ko)[order(p_ko[,1], decreasing = TRUE)]
p <- DoHeatmap(object = sub2, features = marker.list, label = TRUE, cells = ordered.cells, assay = assay, ...)
} else{
p <- DoHeatmap(object = sub2, features = marker.list, label = TRUE, cells = sample(x = Cells(x = sub2)), assay = assay, ...)
}
return(p)
}
}
#' Function to plot perturbation score distributions.
#'
#' Density plots to visualize perturbation scores calculated from RunMixscape
#' function.
#'
#' @param object An object of class Seurat.
#' @param target.gene.ident Target gene name to visualize perturbation scores for.
#' @param target.gene.class meta data column specifying all target gene names in the experiment.
#' @param before.mixscape Option to split densities based on mixscape classification (default) or original target gene classification.
#' Default is set to NULL and plots cells by original class ID.
#' @param col Specify color of target gene class or knockout cell class. For
#' control non-targeting and non-perturbed cells, colors are set to different
#' shades of grey.
#' @param mixscape.class meta data column specifying mixscape classifications.
#' @param prtb.type specify type of CRISPR perturbation expected for labeling mixscape classifications. Default is KO.
#' @param split.by For datasets with more than one cell type. Set equal TRUE to visualize perturbation scores for each cell type separately.
#' @return A ggplot object.
#'
#' @importFrom stats median