-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.R
1397 lines (1140 loc) · 49.7 KB
/
startup.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
#Project:
#CellCode in vitro screen
#Packages
library(monocle3)
#Tutorial for monocle3
#https://cole-trapnell-lab.github.io/monocle3/docs/introduction/
library(DescTools)
library(tidyr)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(RColorBrewer)
suppressPackageStartupMessages(library(ComplexHeatmap))
#Refernce manual for Heatmaps - very very helpful
#https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html#titles-for-splitting
library(viridis)
library(circlize)
#library(randomForest)
library(caret)
library(pbmcapply)
library(pbapply)
library(patchwork)
#library(VennDiagram)
library(clusterProfiler)
library("org.Mm.eg.db", character.only = TRUE)
library(ClassDiscovery)
library(Matrix)
library(apcluster)
library(ggrepel)
library(glmnet)
library(dendextend)
library(clusterSim)
#library(factoextra)
library(lsa)
library(ggridges)
#User-defined functions
## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
## data: a data frame.
## measurevar: the name of a column that contains the variable to be summarized
## groupvars: a vector containing names of columns that contain grouping variables
## na.rm: a boolean that indicates whether to ignore NA's
## conf.interval: the percent range of the confidence interval (default is 95%)
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
conf.interval=.95, .drop=TRUE) {
#library(plyr)
# New version of length which can handle NA's: if na.rm==T, don't count them
length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else length(x)
}
# This does the summary. For each group's data frame, return a vector with
# N, mean, and sd
datac <- ddply(data, groupvars, .drop=.drop,
.fun = function(xx, col) {
c(N = length2(xx[[col]], na.rm=na.rm),
mean = mean (xx[[col]], na.rm=na.rm),
sd = sd (xx[[col]], na.rm=na.rm)
)
},
measurevar
)
# Rename the "mean" column
datac <- rename(datac, c("mean" = measurevar))
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
if (!is.null(conf.interval)) {
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
datac$ci <- datac$se * ciMult
}
return(datac)
}
compute_percentage <- function(x) {x/sum(x) * 100}
compute_fold_change <- function(x, standard) {log2(x/x[standard])}
RowRange <- function(x) {max(x) - min(x)}
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
#Need to update - may not be useful anymore
sc_pseudobulk <- function(cds, gene_groups, zscore = T, cell_group_column = "unique") {
pData <- as.data.frame(colData(cds))
rownames(pData(cds)) <- pData$barcode
pData <- as.data.frame(colData(cds))
cell_groups <- pData[,c("barcode", cell_group_column)]
if (!is.null(gene_groups)) {
agg0 <- aggregate_gene_expression(cds, cell_group_df = cell_groups, gene_group_df = gene_groups, norm_method = "log", scale_agg_values = zscore)
} else {
agg0 <- aggregate_gene_expression(cds, cell_group_df = cell_groups, norm_method = "log", scale_agg_values = zscore)
#Swap target id for gene short name
id2g <- as.data.frame(rowData(cds))
old_row_names <- rownames(agg0)
new_row_names <- match(old_row_names, id2g$id)
rownames(agg0) <- id2g$gene_short_name[new_row_names]
#agg0 <- agg0[gene_module_df$gene,]
}
return(as.matrix(agg0))
}
#Need to update
make_gene_module_hm <- function(mat, gene_module_df = NULL, aggregate_genes = T, title) {
if (aggregate_genes) {
hm <- Heatmap(mat,
show_row_dend = F,
show_column_dend = F,
show_column_names = T,
column_names_gp = gpar(fontsize = 5),
show_row_names = T,
row_names_side = "left",
cluster_columns = T,
cluster_rows = T,
border = T,
column_title = title,
row_title = "Gene Module",
name = "Expr")
} else {
split <- as.data.frame(gene_module_df$module)
rownames(split) <- rownames(gene_module_df)
colnames(split) <- "split"
split$split <- factor(split$split, levels = c(1:max(gene_module_df$module)))
hm <- Heatmap(mat,
show_row_dend = F,
show_column_dend = F,
show_column_names = T,
column_names_gp = gpar(fontsize = 5),
show_row_names = F,
row_names_side = "left",
cluster_columns = T,
cluster_rows = T,
#column_split = c(rep("Stim+IL-12", 27), rep("Stim", 29)),
#cluster_row_slices = F,
row_split = split,
row_gap = unit(3, "mm"),
border = T,
column_title = title,
#row_title = "Gene Module",
name = "Expr")
}
return(hm)
}
#Need to update
plot_aggregated_gene_modules <- function(mat, range_threshold = NULL, apcluster_pref = 0.3, return_grouping = F, normalize = F) {
if(normalize) {
agg_norm <- matrix(apply(mat, 1, compute_fold_change, standard = "Null_"), ncol = ncol(mat), nrow = nrow(mat), dimnames = dimnames(mat), byrow = T)
gm_range <- apply(agg_norm, 1, RowRange)
agg_norm2 <- agg_norm[,colnames(agg_norm) != "Null_"]
if (!is.null(range_threshold)) {
#Remove modules with minimal change
agg_norm2 <- agg_norm2[unname(which(gm_range >= range_threshold)),]
}
} else {
agg_norm2 <- mat
}
s <- negDistMat(t(agg_norm2))
#Run clustering
ap_cyt <- apcluster(s = s, q = apcluster_pref)
groups <- ap_cyt@clusters
ap_cyt2 <- aggExCluster(s = s, x = ap_cyt, includeSim = T)
cyt_order <- names(unlist(groups[ap_cyt2@order]))
#Create data frame to store cytokine groups
cyt_groups <- bind_rows(mapply(FUN = function(x, i) {
out_df <- data.frame(index = x, group = i, cytokine = names(x))
return(out_df)
}, x = groups, i = 1:length(groups), SIMPLIFY = F))
#reorder matrix columns
agg_norm3 <- agg_norm2[,cyt_order]
#Add bar annotations to identify cytokine groups
#Sort data frame to match matrix order
cyt_groups_ordered <- cyt_groups[cyt_order,]
cyt_groups_ha <- HeatmapAnnotation(Module = cyt_groups_ordered$group,
col = list(Module = colorRamp2(1:length(groups), gg_color_hue(length(groups)))),
simple_anno_size = unit(2, "mm"),
show_annotation_name = FALSE, show_legend = F)
col_split <- data.frame(split = factor(cyt_groups_ordered$group))
hm <- Heatmap(agg_norm3,
#col = colorRamp2(c(-1,-0.5,0,0.5,1), c("darkblue", "blue", "white", "red", "darkred")),
show_row_dend = F,
show_column_dend = F,
show_column_names = T,
column_names_gp = gpar(fontsize = 5),
row_names_gp = gpar(fontsize = 8),
show_row_names = T,
row_names_side = "left",
cluster_columns = F,
cluster_rows = T,
bottom_annotation = cyt_groups_ha,
#right_annotation = rowAnnotation(FCrange = anno_barplot(gm_range), annotation_name_rot = 90, gap = unit(2, "mm")),
column_split = col_split,
column_title_side = "bottom",
border = T,
row_title = "Gene Module",
name = "z-score")
if (return_grouping) {
return(list(hm, cyt_groups_ordered))
} else {
return(hm)
}
}
#Convert monocle cds into Seurat object for use with Seurat functions
#I currently don't have a method to convert back. The easiest way is to add the new colData columns to colData of the original cds.
monocle_to_seurat <- function(monocle_cds) {
requireNamespace("Seurat")
data <- exprs(monocle_cds)
export_cds <- Seurat::CreateSeuratObject(counts = data,
normalization.method = "LogNormalize",
do.scale = TRUE,
do.center = TRUE,
project = "exportCDS")
[email protected] <- as.data.frame(colData(monocle_cds))
return(export_cds)
}
## Plot heatmap of the composition of cells from each condition in each of the UMAP clusters.
## df: a data frame.
## column_var: defaults to cluster - must have this variable in the data frame to work (i.e run cluster_cells)
## row_var: column indicating condition to group cells by. Rows get normalized to 1.
## annotation_bar_groupings: column of data frame or vector of groupings for making a color bar annotation.
## annotation_colors: color key for annotation bar. Must match length of unique group IDs from annotation_bar_groupings
## row_label_annotation_thresh: maximum column (i.e cluster) enrichment needed for a row (i.e condition) to be annotated. 1 (default) hides all row names.
plot_cluster_composition_heatmap <- function(df,
column_var = "cluster",
row_var,
cluster_columns = TRUE,
column_reorder = TRUE,
annotation_bar_groupings = NULL,
annotation_colors = NULL,
label_size = 6,
row_label_annotation_thresh = 1) {
#This normalizes the frequencies for the row variable (i.e each row sums to 1)
hmData <- t(as.matrix(prop.table(table(df[,column_var], df[,row_var]), margin = 2)))
hmData <- matrix(hmData, ncol = ncol(hmData), dimnames = dimnames(hmData))
#Cluster columns
# d <- hclust(dist(hmData), method = "average")
# set.seed(12)
# d <- reorder(as.dendrogram(d), hmData[,column_reorder_var])
if (!is.logical(column_reorder)) {
column_reorder <- hmData[column_reorder,] #which row to use for ordering weights
#HACK to move another branch
column_reorder[13] <- 1#column_reorder[2] * 1.5
}
#Determine which rows to annotate
row_maximums <- rowMax(hmData)
rows_to_label_pos <- which(row_maximums >= row_label_annotation_thresh)
#annotate null conditions
null_conditions <- c(unique(grep("Null", df$unique, value = TRUE)), unique(grep("CD3/28_IL-12_1ng/mL", df$unique, value = TRUE)))
null_pos <- match(null_conditions, rownames(hmData))
rows_to_label_pos <- unique(c(rows_to_label_pos, null_pos))
rows_to_label <- rownames(hmData)[rows_to_label_pos]
#Add annotation color bar
if(!is.null(annotation_bar_groupings)) {
if (length(annotation_bar_groupings) == 1) {
classifier <- df[,annotation_bar_groupings][match(rownames(hmData), df[,row_var])]
} else {
classifier <- annotation_bar_groupings
}
#Set color key if none provided
if (is.null(annotation_colors)) {
annotation_colors = colorRamp2(1:length(unique(classifier)), sample(gg_color_hue(length(unique(classifier)))))
}
#Don't annotate specific rows for now
# row_annot <- rowAnnotation(Group = factor(classifier),
# Cond = anno_mark(at= rows_to_label_pos, labels = rows_to_label, which="row", side = "right", labels_gp = gpar(col= "black", fontsize = label_size)),
# col = list(Group = annotation_colors),
# simple_anno_size = unit(2, "mm"),
# show_annotation_name = F, show_legend = T)
row_annot <- rowAnnotation(Group = factor(classifier),
col = list(Group = annotation_colors),
simple_anno_size = unit(2, "mm"),
show_annotation_name = F, show_legend = T)
hm <- ComplexHeatmap::Heatmap(hmData,
column_title = column_var,
column_title_side = "bottom",
cluster_columns = cluster_columns,
row_title = row_var,
row_title_side = "right",
show_row_names = TRUE,
row_dend_reorder = TRUE,
column_dend_reorder = column_reorder,
column_names_gp = gpar(fontsize = 8),
row_names_gp = gpar(fontsize = label_size),
right_annotation = row_annot,
name = "Freq")
} else {
row_annot <- rowAnnotation(Cond = anno_mark(at= rows_to_label_pos, labels = rows_to_label, which="row", side = "right", labels_gp = gpar(col= "black", fontsize = label_size)),
show_annotation_name = F, show_legend = T)
hm <- ComplexHeatmap::Heatmap(hmData,
column_title = column_var,
column_title_side = "bottom",
cluster_columns = cluster_columns,
show_row_names = F,
row_title = row_var,
row_title_side = "right",
row_dend_reorder = TRUE,
column_dend_reorder = column_reorder,
column_names_gp = gpar(fontsize = 8),
#row_names_gp = gpar(fontsize = label_size),
right_annotation = row_annot,
name = "Freq")
}
return(hm)
}
## Fisher exact test for cluster enrichment by cells from a single condition
## df: a data frame.
## group_cells_by: column variable name indicating how to group cells.
## split_by: Column variable to split the cell groups by. Each subgroup is tested against its own control. If NULL (default), every condition is tested against the same control condition (named by control_identifier).
## control_identifier: If split_by is used, a string matching to a unique condition(s) to test against. If split_by is not used, a string matching exactly to one and only one group_cells_by names.
## enrichment_probs: if TRUE (default), test for enrichment. If FALSE, test probabilities less than the null distribution to determine cluster depletion instead of enrichment.
test_cluster_enrichment <- function(df,
group_cells_by = "unique",
split_by = NULL,
control_identifier,
greater_than = TRUE) {
if (!is.null(split_by)) {
dfs <- split(df, df[,split_by])
results2 <- bind_rows(lapply(dfs, FUN = function(df_x) {
ctrl <- unique(grep(control_identifier, df_x[,group_cells_by], value = TRUE))
#Create counts table of clustering assignments
counts_true <- as.data.frame(table(df_x[,"cluster"], df_x[,group_cells_by]))
counts_split <- split(counts_true, counts_true$Var2)
#Iterate through each condition to get a p-value from Fisher's exact test for significant enrichment
#of cells from that condition in each cluster relative to the control condition
results <- pblapply(counts_split, FUN = function(x_test, x_control) {
#Initialize dataframe to store probabilities for each cluster
probs <- data.frame()
x_total <- bind_rows(x_test,x_control)
#Now iterate over each cluster and get probabilities and p-values
for (clust in levels(x_test$Var1)) {
# Initialize variables
m <- sum(x_total$Freq[which(x_total$Var1 == clust)]) # cells IN cluster
n <- sum(x_total$Freq) - m # cells NOT IN cluster
k <- sum(x_test$Freq) # cells from test condition
x <- c(0:k) # cells both IN cluster and from test condition
# Use the dhyper built-in function for hypergeometric density
probabilities <- dhyper(x, m, n, k, log = FALSE)
probs[clust,1:length(probabilities)] <- probabilities
}
probs <- as.data.frame(t(probs))
probs$x <- x
probs <- pivot_longer(probs, cols = 1:length(levels(x_test$Var1)), names_to = "cluster", values_to = "Prob")
probs$cluster <- factor(probs$cluster)
#Now calculate p-values from these probability distributions
sig_test <- data.frame("unique" = rep(x_test$Var2[1], length(levels(probs$cluster))), "cluster" = levels(probs$cluster), "pval" = NA)
rownames(sig_test) <- levels(probs$cluster)
for (clust in sig_test$cluster) {
#Cells from test condition and in cluster
xIN <- x_test$Freq[which(x_test$Var1 == clust)]
if (greater_than) {
probs2 <- subset(probs, cluster == clust & x >= xIN)
} else {
probs2 <- subset(probs, cluster == clust & x <= xIN)
}
pval <- sum(probs2$Prob)
sig_test[clust,"pval"] <- pval
}
return(sig_test)
}, x_control = counts_split[[ctrl]])
results <- bind_rows(results)
return(results)
}))
} else {
ctrl <- control_identifier
#Create counts table of clustering assignments
counts_true <- as.data.frame(table(df[,"cluster"], df[,group_cells_by]))
counts_split <- split(counts_true, counts_true$Var2)
#Iterate through each condition to get a p-value from Fisher's exact test for significant enrichment
#of cells from that condition in each cluster relative to the control condition
results <- pblapply(counts_split, FUN = function(x_test, x_control) {
#Initialize dataframe to store probabilities for each cluster
probs <- data.frame()
x_total <- bind_rows(x_test,x_control)
#Now iterate over each cluster and get probabilities and p-values
for (clust in levels(x_test$Var1)) {
# Initialize variables
m <- sum(x_total$Freq[which(x_total$Var1 == clust)]) # cells IN cluster
n <- sum(x_total$Freq) - m # cells NOT IN cluster
k <- sum(x_test$Freq) # cells from test condition
x <- c(0:k) # cells both IN cluster and from test condition
# Use the dhyper built-in function for hypergeometric density
probabilities <- dhyper(x, m, n, k, log = FALSE)
probs[clust,1:length(probabilities)] <- probabilities
}
probs <- as.data.frame(t(probs))
probs$x <- x
probs <- pivot_longer(probs, cols = 1:length(levels(x_test$Var1)), names_to = "cluster", values_to = "Prob")
probs$cluster <- factor(probs$cluster)
#Now calculate p-values from these probability distributions
sig_test <- data.frame("unique" = rep(x_test$Var2[1], length(levels(probs$cluster))), "cluster" = levels(probs$cluster), "pval" = NA)
rownames(sig_test) <- levels(probs$cluster)
for (clust in sig_test$cluster) {
#Cells from test condition and in cluster
xIN <- x_test$Freq[which(x_test$Var1 == clust)]
if (greater_than) {
probs2 <- subset(probs, cluster == clust & x >= xIN)
} else {
probs2 <- subset(probs, cluster == clust & x <= xIN)
}
pval <- sum(probs2$Prob)
sig_test[clust,"pval"] <- pval
}
return(sig_test)
}, x_control = counts_split[[ctrl]])
results2 <- bind_rows(result)
}
results2$logP <- round((log10(results2$pval)*-1),6)
results2 <- results2[,-3]
results2 <- as.data.frame(pivot_wider(results2, names_from = cluster, values_from = logP))
rownames(results2) <- results2[,1]
results2 <- results2[,-1]
results2 <- results2[,as.character(sort(as.numeric(colnames(results2))))]
return(results2)
}
#Plotting function for Fisher exact test results
plot_cluster_enrichment <- function(test_results,
annotation_bar_groupings = NULL,
annotation_colors = NULL,
label_size = 6,
cluster_column_var = TRUE,
row_label_annotation_pval_thresh = 0.05) {
#Determine which rows to annotate
row_maximums <- rowMax(test_results)
rows_to_label_pos <- which(row_maximums >= (log10(row_label_annotation_pval_thresh) * -1))
rows_to_label <- rownames(test_results)[rows_to_label_pos]
#Add annotation color bar
if(!is.null(annotation_bar_groupings)) {
classifier <- annotation_bar_groupings
#Set color key if none provided
if (is.null(annotation_colors)) {
annotation_colors = colorRamp2(1:length(unique(classifier)), sample(gg_color_hue(length(unique(classifier)))))
}
row_annot <- rowAnnotation(Group = factor(classifier),
Cond = anno_mark(at= rows_to_label_pos, labels = rows_to_label, which="row", side = "right", labels_gp = gpar(col= "black", fontsize = label_size)),
col = list(Group = annotation_colors),
simple_anno_size = unit(2, "mm"),
show_annotation_name = F, show_legend = T)
hm <- ComplexHeatmap::Heatmap(as.matrix(test_results),
col = colorRamp2(c(0, 1.3, 1.4, 7), c("grey", "grey", "red", "darkred")),
column_title = "cluster",
column_title_side = "bottom",
cluster_columns = cluster_column_var,
row_title = NULL,
#row_title_side = "right",
show_row_names = F,
row_dend_reorder = TRUE,
column_names_gp = gpar(fontsize = label_size),
#row_names_gp = gpar(fontsize = label_size),
right_annotation = row_annot,
name = "-log(pval)")
} else {
row_annot <- rowAnnotation(Cond = anno_mark(at= rows_to_label_pos, labels = rows_to_label, which="row", side = "right", labels_gp = gpar(col= "black", fontsize = label_size)),
show_annotation_name = F, show_legend = T)
hm <- ComplexHeatmap::Heatmap(as.matrix(test_results),
#col = colorRamp2(c(0, 1.3, 7), c("blue", "grey", "red")),
col = colorRamp2(c(0, 1.3, 1.4, 7), c("grey", "grey", "red", "darkred")),
column_title = "cluster",
column_title_side = "bottom",
cluster_columns = cluster_column_var,
row_title = NULL,
#row_title_side = "right",
show_row_names = F,
row_dend_reorder = TRUE,
column_names_gp = gpar(fontsize = label_size),
#row_names_gp = gpar(fontsize = label_size),
right_annotation = row_annot,
name = "-log(pval)")
}
return(hm)
}
#New plotting function for Fisher exact test results in new wide data format
plot_cluster_enrichment2 <- function(test_results,
annotation_colors,
column_split_groups,
col_map = NULL,
label_size = 6) {
n_clusters <- ncol(test_results)/length(annotation_colors)
col_annot <- columnAnnotation(Base = rep(names(annotation_colors), each = n_clusters),
col = list(Base = annotation_colors),
simple_anno_size = unit(2, "mm"),
show_annotation_name = F, show_legend = T)
if (is.null(col_map)) {
col_map <- colorRamp2(c(0, 1.3, 2.7, 7), c("white", "grey", "red", "darkred"))
}
hm <- ComplexHeatmap::Heatmap(as.matrix(test_results),
col = col_map,
column_title = "cluster",
column_title_side = "bottom",
cluster_columns = F,
column_split = column_split_groups,
column_gap = unit(5, "mm"),
row_title = NULL,
show_row_names = T,
row_dend_reorder = TRUE,
column_names_gp = gpar(fontsize = label_size),
row_names_gp = gpar(fontsize = label_size),
top_annotation = col_annot,
border = T,
name = "-log10(pval)")
return(hm)
}
## Plot single cell scores for genesets
## df: a data frame.
## set: column name of the geneset score to plot
plot_geneset_scores <- function(df, set, label_size = 6, plot_type = c("ridgeline", "boxplot", "UMAP")) {
df$median <- ave(df[,set], as.factor(df[,"unique"]), FUN=median)
if (plot_type == "ridgeline") {
p <- ggplot(df, aes_string(x = set, y = "unique", fill = "median")) +
geom_density_ridges(rel_min_height = 0.01) +
scale_fill_viridis(option = "C", direction = 1) +
xlim(quantile(geneset_scores[,set], probs = 0.01), quantile(geneset_scores[,set], probs = 0.99)) +
facet_wrap(~base_new, scales = "free_y") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.text.y = element_text(size = label_size))
} else if (plot_type == "boxplot") {
p <- ggplot(df, aes_string(x = set, y = "unique", color = "median")) +
geom_boxplot() +
scale_color_viridis(option = "C", direction = 1) +
facet_wrap(~base_new, scales = "free_y") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.text.y = element_text(size = label_size))
} else if (plot_type == "UMAP") {
p <- ggplot(df, aes_string(x = "umap_1", y = "umap_2", color = set)) +
geom_point(size = 0.3, alpha = 1) +
scale_color_viridis(option = "C", direction = 1) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
}
return(p)
}
## Test for differential expression of genesets
## df: a data frame of geneset scores.
## test: which statistical test to run, KS or Wilcoxin
## control: string indicating control condition to test against
##MJW - update to allow for splitting by base condition. Take method from Fisher test function
geneset_DE_test <- function(df, test = c("KS", "Wilcoxin"), control) {
#Group cells into conditions
df_split <- split(df, df$unique)
#control arg must match one of the names of df_split
assertthat::assert_that(control %in% names(df_split),
msg = paste('Control condition must be one of the conditions in the dataframe'))
#Parse the columns to keep
n_sets <- length(colnames(df)) - 4
control_group <- df_split[[control]]
result <- lapply(df_split, FUN = function(x) {
res_out <- mapply(FUN = function(df_test,df_ctrl) {
if (test == "KS") {
res <- suppressWarnings(ks.test(df_test, df_ctrl))
} else {
res <- wilcox.test(df_test, df_ctrl, paired = F)
}
return(res$p.value)
}, df_test = x[,1:n_sets], df_ctrl = control_group[,1:n_sets])
return(res_out)
})
test_results <- as.matrix(do.call(cbind, result))
test_results <- log10(test_results) * -1
return(test_results)
}
GO_enrichment_analysis <- function(gene_list, list_names, target_id_source = NULL, sim_cutoff = 0.7) {
if (is.null(target_id_source) == FALSE) {
ind <- match(gene_list, target_id_source$gene_short_name)
gene_list_ENS <- target_id_source$id[ind]
} else {
gene_list_ENS <- gene_list
}
ego <- enrichGO(gene = gene_list_ENS,
OrgDb = org.Mm.eg.db,
ont = "BP",
keyType = "ENSEMBL",
readable = TRUE)
ego2 <- simplify(ego, cutoff = sim_cutoff)
ego2 <- as.data.frame(ego2)
if (nrow(ego2) > 0) {
ego2$GeneRatio <- DOSE::parse_ratio(ego2$GeneRatio)
ego2 <- ego2[!is.na(ego2$Description), ]
ego2$cluster <- list_names
} else {
ego2 <- NULL
}
return(ego2)
}
filter_GOdata <- function(df, n = 20, GeneRatio_thresh = 0) {
if (!is.null(df)) {
df <- subset(df, GeneRatio >= GeneRatio_thresh)
df <- df[order(-df$GeneRatio),]
if (length(df$GeneRatio) >= n) {
df <- df[1:n, ]
}
}
return(df)
}
#LASSO Regression functions (Cao et al.)
do_lasso <- function(gene_matrix, gene_vector, seed = 1) {
M1 = gene_matrix
y = gene_vector
set.seed(seed)
cv.out1 = cv.glmnet(M1, y, alpha=1, lambda=exp(seq(log(0.001), log(10), length.out=100)))
r2_1 = r2_glmnet(cv.out1, y)
bestlam = cv.out1$lambda.1se
cor_list = coef(cv.out1, s= bestlam)
cor_length = length(cor_list)
df_cor = data.frame("id" = row.names(cor_list)[2:cor_length], "corcoef" = cor_list[2:cor_length])
return(list(r2_1, df_cor))
}
r2_glmnet <- function(cv.out, y) {
bestlam = cv.out$lambda.1se
i = which(cv.out$lambda == bestlam)
e <- cv.out$cvm[i]
r2 <- 1 - e / var(y)
if(r2 < 0)
{
r2 = 0
}
return(r2)
}
#Matt's
create_lasso_ggc_matrix <- function(lasso_output, gene_list, symmetrize = TRUE) {
# #Drop coefficients into a new correlation matrix
# ggc_mat <- matrix(nrow = length(gene_list), ncol = length(gene_list))
# rownames(ggc_mat) <- gene_list
# colnames(ggc_mat) <- gene_list
#
# for (i in 1:length(lasso_output)) {
# result <- lasso_output[[i]][[2]]
# row_match <- match(result$id, rownames(ggc_mat))
# ggc_mat[row_match,i] <- result$corcoef
# }
ggc_mat <- pblapply(1:length(lasso_output), function(x) {
result <- lasso_output[[x]][[2]]
#Add back in self correlation
result <- rbind(c(gene_list[x],NA), result)
#Put into order
result$index <- match(result$id, gene_list)
result <- result[order(result$index, decreasing = F),]
return(as.numeric(result$corcoef))
})
#Create matrix
ggc_mat <- as.matrix(do.call(cbind, ggc_mat))
rownames(ggc_mat) <- colnames(ggc_mat) <- gene_list
#Filter genes with no correlation
coeff_sums <- Matrix::colSums(abs(ggc_mat), na.rm = T)
top_genes <- rownames(ggc_mat)[which(coeff_sums > 0.00)]
ggc_mat <- ggc_mat[top_genes,top_genes]
if(symmetrize) {
ggc_mat = (ggc_mat+t(ggc_mat))/2 # symmetrize the coeff matrix
}
return(ggc_mat)
}
bootstrap_ggc_matrix <- function(expr_matrix, sampling_iterations = 10) {
all_genes <- colnames(expr_matrix)
barcodes <- rownames(expr_matrix)
#Sample 70% of the cells in the expression matrix containing the ~1800 genes in the lasso matrix
#Make pearson correlation matrix of the genes
message("Sampling Data")
sampled_matrices <- pbmclapply(1:sampling_iterations, function(i) {
samp <- sample(barcodes, (0.7*length(barcodes)), replace = F)
new_mat <- expr_matrix[samp,]
cor_mat <- cor(new_mat)
return(cor_mat)
}, mc.cores = 2)
return(sampled_matrices)
}
bootstrap_apclust <- function(ggc_matrices, q_range) {
ap.out <- lapply(q_range, FUN = function(q) {
message("Peforming clustering for q = ", q)
output <- pbmclapply(ggc_matrices, function(mat) {
res <- apcluster::apcluster(s = mat, q = q)
return(res)
}, mc.cores = 2)
return(output)
})
return(ap.out)
}
make_consensus_matrix <- function(cluster_results, ggc_matrix) {
#Initialize zero matrices for counts
counts.paired <- ggc_matrix
counts.paired[,] <- 0
counts.total <- counts.paired
message("Generating consensus matrix")
for (res in cluster_results) {
res.clust <- res@clusters
#Get all genes sampled in this sub_matrix
all_sampled <- names(unlist(res.clust))
#Count all pairs sampled together
counts.total[all_sampled,all_sampled] <- counts.total[all_sampled,all_sampled] + 1
#Count all pairs clustered together
for (c in res.clust) {
ids <- names(c)
counts.paired[ids,ids] <- counts.paired[ids,ids] + 1
}
}
consensus.out <- counts.paired/counts.total
return(consensus.out)
}
plot_consensus_matrix <- function(mat) {
d <- distanceMatrix(mat, metric = 'spearman')
d <- hclust(d, method = "average")
set.seed(12)
d <- reorder(as.dendrogram(d), rowMeans(mat, na.rm = T))
mat2 <- mat[labels(d),labels(d)]
hm <- Heatmap(mat2,
show_row_dend = F,
show_column_dend = F,
show_column_names = F,
show_row_names = F,
cluster_columns = F,
cluster_rows = F)
return(hm)
}
evaluate_clustering <- function(sim_matrix, ggc_mat, method = c("hclust", "AP"), min_size = 3) {
if (method == "AP") {
ap.out <- apcluster::apcluster(s = sim_matrix, q = 0)
clusters <- ap.out@clusters
#Create data frame to store module data
modules <- bind_rows(mapply(FUN = function(x, i) {
out_df <- data.frame(module = i, gene = names(x))
return(out_df)
}, x = clusters, i = 1:length(clusters), SIMPLIFY = F))
} else {
d <- hclust(dist(sim_matrix))
h_cutoff <- quantile(d$height, 0.99)
modules <- data.frame("module" = cutree(d, h = h_cutoff, order_clusters_as_data = F))
modules$gene <- rownames(modules)
}
#Generate random clusters
modules$gene_random <- sample(modules$gene)
mods <- split(modules, modules$module)
mod_size <- data.frame(module = 1:length(mods), n = sapply(mods, function(x) {length(x$gene)}))
mods <- mods[mod_size$module[which(mod_size$n >= min_size)]]
#Rename modules in case some were removed
mods2 <- bind_rows(mapply(FUN = function(mod, name) {
mod$module <- name
return(mod)
}, mod = mods, name = 1:length(mods), SIMPLIFY = F))
colnames(mods2)[2:3] <- c("true", "random")
mods2 <- pivot_longer(mods2, cols = 2:3, names_to = "set", values_to = "gene")
DB <- lapply(split(mods2, mods2$set), function(df) {
#Subset matrix on genes included in clusters
cor_mat2 <- ggc_mat[df$gene,df$gene]
indices <- match(df$gene, rownames(cor_mat2))
df$index <- indices
cl_df <- df[order(df$index, decreasing = F),]
testDB <- index.DB(x = cor_mat2, cl = cl_df$module)
})
return(DB)
}
tune_clusters <- function(optimal_consensus_matrix, min_module_size = 5, min_n_clusters = 30, DB_prc = 0.99) {
#Get initial clusters
apclust <- apcluster::apcluster(s = optimal_consensus_matrix, q = 0)
apclust2 <- apcluster::aggExCluster(s = optimal_consensus_matrix, x = apclust, includeSim = T)
og_mods <- apclust@clusters
mods <- og_mods
minimum <- min(sapply(mods, function(x) {length(x)}))
#Merge clusters until the smallest module is above the minimum size threshold while maintaining a minimum number of total clusters
while (minimum < min_module_size) {
current_k <- length(mods)
new_k <- current_k-1
merge <- apcluster::cutree(apclust2, k=new_k)
mods <- merge@clusters
minimum <- min(sapply(mods, function(x) {length(x)}))
#If this minimum resulted in too few clusters, reduce the minimum module size and restart
if (minimum >= min_module_size & length(mods) < min_n_clusters) {
mods <- og_mods
minimum <- min(sapply(mods, function(x) {length(x)}))
min_module_size <- min_module_size-1
}
}