-
Notifications
You must be signed in to change notification settings - Fork 21
/
CellRouter_Class.R
4524 lines (3992 loc) · 201 KB
/
CellRouter_Class.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
##### CellRouter Class #####
#install.packages("devtools")
#devtools::install_github('hadley/devtools')
#devtools::install_github("klutometis/roxygen")
#library(roxygen2)
suppressWarnings(suppressMessages(require('reshape')))
suppressWarnings(suppressMessages(require('reshape2')))
suppressWarnings(suppressMessages(require('pheatmap')))
suppressWarnings(suppressMessages(library('scales')))
#library('geomnet')
suppressWarnings(suppressMessages(require('ggplot2')))
suppressWarnings(suppressMessages(require('grid')))
CellRouter <- setClass("CellRouter", slots=
c(rawdata="data.frame", ndata="data.frame", scale.data="data.frame",
sampTab="data.frame", rdimension="data.frame", pca="list", tsne="list", dc="list", dr.custom="list",
var.genes="character",graph="list",signatures="list", sources="character", targets="character",
directory="list", paths="data.frame", networks="list",
genes.trajectory="character", pathsinfo="list",
dynamics="list", clusters="list", correlation="list",
top.correlations="list", pathwayenrichment="list"))
#' Check CellRouter object
#' @param object CellRouter object
setValidity("CellRouter",
function(object){
msg <- NULL
if(!is.data.frame(object@rawdata)){
msg <- c(msg, "expression data must be a data.frame")
}else if(sum(apply(is.na(object@rawdata), 1, sum) > 0)){
msn <- c(msg, "expression data must not have NAs")
}
if(is.null(msg)){
TRUE
}else{
msg
}
})
setGeneric("dotplot", function(object, genes.use, thr,width, height, filename) standardGeneric("dotplot"))
setMethod("dotplot",
signature = "CellRouter",
definition = function(object, genes.use, thr, width, height, filename){
sampTab <- cellrouter@sampTab
perc <- data.frame(matrix(0, nrow=length(genes.use), ncol=0))
exp <- perc
rownames(perc) <- genes.use
for(i in unique(sampTab$population)){
cells.population <- rownames(sampTab[which(sampTab$population == i),])
p <- apply(object@ndata[genes.use, cells.population], 1, function(x){sum(x>thr)/length(x)})
perc <- cbind(perc, p)
v <- apply(object@ndata[genes.use, cells.population], 1, mean)
exp <- cbind(exp, v)
}
colnames(perc) <- unique(sampTab$population)
colnames(exp) <- unique(sampTab$population)
rownames(exp) <- sapply(strsplit(rownames(exp), split='__', fixed=TRUE), function(x){x[1]})
perc$gene <- rownames(perc)
perc = melt(perc, id.vars = 'gene')
exp$gene <- rownames(exp)
exp$gene <- factor(exp$gene, levels=genes.use)
exp <- melt(exp, id.vars='gene')
exp$Percentage <- perc$value*100
pdf(file=filename, width=width, height=height)
g <- ggplot(exp, aes(gene, variable)) + geom_point(aes(colour=value, size=Percentage)) +
theme_bw() + xlab("") + ylab("") +
#theme(axis.text.x=element_text(size=12, angle=45, hjust=1),
theme(axis.text.x=element_text(size=12, angle=45, hjust=1),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(), legend.spacing.y = unit(0, "cm"),
panel.border=element_rect(fill = NA, colour=alpha('black', 1),size=1)) +
#scale_color_gradient("Mean expression",low ="blue", high = "red") + coord_flip()
scale_colour_gradientn("Mean expression", colours=c("midnightblue","dodgerblue3","goldenrod1","darkorange2")) +
guides(col=guide_legend(direction="vertical", keywidth = 0.75, keyheight = 0.75, override.aes = list(size=3)))
print(g)
dev.off()
print(g)
}
)
setGeneric("dotplotSignatureScore", function(object, genes.use, width, height, filename) standardGeneric("dotplotSignatureScore"))
setMethod("dotplotSignatureScore",
signature = "CellRouter",
definition = function(object, genes.use, width, height, filename){
sampTab <- object@sampTab
perc <- data.frame(matrix(0, nrow=length(genes.use), ncol=0))
exp <- perc
#rownames(perc) <- genes.use
for(i in unique(sampTab$population)){
cells.population <- rownames(sampTab[which(sampTab$population == i),])
##p <- apply(sampTab[cells.population,], 1, function(x){sum(x>thr)/length(x)})
#perc <- cbind(perc, p)
v <- apply(sampTab[cells.population, genes.use], 2, mean)
exp <- cbind(exp, v)
}
#colnames(perc) <- unique(sampTab$population)
colnames(exp) <- unique(sampTab$population)
rownames(exp) <- sapply(strsplit(rownames(exp), split='__', fixed=TRUE), function(x){x[1]})
#perc$gene <- rownames(perc)
#perc = melt(perc, id.vars = 'gene')
exp$gene <- rownames(exp)
exp$gene <- factor(exp$gene, levels=genes.use)
exp <- melt(exp, id.vars='gene')
#exp$Percentage <- perc$value*100
pdf(file=filename, width=width, height=height)
g <- ggplot(exp, aes(gene, variable)) + geom_point(aes(colour=value, size=value)) +
theme_bw() + xlab("") + ylab("") +
#theme(axis.text.x=element_text(size=12, angle=45, hjust=1),
theme(axis.text.x=element_text(size=12, angle=45, hjust=1),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(), legend.spacing.y = unit(0, "cm"),
panel.border=element_rect(fill = NA, colour=alpha('black', 1),size=1)) +
#scale_color_gradient("Mean expression",low ="blue", high = "red") + coord_flip()
scale_colour_gradientn("value", colours=c("midnightblue","dodgerblue3","goldenrod1","darkorange2")) +
guides(fill= guide_legend(), size=guide_legend(), col=guide_legend(direction="vertical", keywidth = 0.75, keyheight = 0.75, override.aes = list(size=3)))
print(g)
dev.off()
print(g)
}
)
plotEnrichR <- function(enrichments, annotation='Reactome_2016', num.pathways=10, order, width=8, height=8, filename){
database <- list()
for(r in names(enrichments)){
x <- enrichments[[r]][[annotation]][1:num.pathways,]
x$timepoint <- r
database[[r]] <- x
}
database <- do.call(rbind, database)
df <- database
df$pvalue <- -log10(df$Adjusted.P.value)
goterms <- as.vector(df$Term) #only top GO terms
clusters <-unique(df$timepoint)
shared.combined <- data.frame(matrix(NA, nrow=length(clusters), ncol=length(goterms)))
rownames(shared.combined) <- clusters
colnames(shared.combined) <- goterms
for(c in clusters){
for(r in goterms){
pval <- df[which(as.vector(df$Term) == r & as.vector(df$timepoint) == c), ]
if(nrow(pval) == 1){
shared.combined[as.character(pval$timepoint), as.character(pval$Term)] <- pval[, 'pvalue']
}else{
shared.combined[as.character(pval$timepoint), as.character(pval$Term)] <- NA
}
}
}
shared.combined$Cluster <- rownames(shared.combined)
shared.m <- melt(shared.combined, id.vars = c('Cluster'))
colnames(shared.m) <- c('Cluster', 'Description', 'pvalue')
shared.m$Cluster <- factor(shared.m$Cluster, levels=order)
shared.m$Description<-gsub("_.*","",as.character(shared.m$Description))
shared.m$Description <- factor(shared.m$Description, levels=unique(shared.m$Description))
pdf(file=filename, width=width, height=height)
g <- ggplot(shared.m, aes(Cluster, Description)) +
geom_point(aes(colour=pvalue, size=pvalue)) +
theme_bw() + xlab("") + ylab("") +
#theme(axis.text.x=element_text(size=12, angle=45, hjust=1),
theme(axis.text.x=element_text(size=12, angle=45, hjust=1),
panel.border=element_rect(fill = NA, colour=alpha('black', 1),size=1)) +
guides(color= guide_legend(title="-log10\n(p-value)"), size=guide_legend(title="-log10\n(p-value)")) +
#scale_color_gradient("-log10(pvalue)",low ="blue", high = "red")
scale_colour_gradientn("pvalue", colours=c("midnightblue","dodgerblue3","goldenrod1","darkorange2"))
print(g)
dev.off()
}
#' Scale and center the data. Individually regress variables provided in vars.regress using using a linear model. Other models are under development.
#' @param object CellRouter object
#' @param genes.use Vector of genes to scale/center. Default is all genes in object@@ndata
#' @param scale.max Max value in scaled data. Default id 10
#' @param vars.regress Variables to regress out
#'
#' @return CellRouter object
#' @export
setGeneric("scaleData", function(object, genes.use=NULL, scale.max=10, vars.regress=NULL) standardGeneric("scaleData"))
setMethod("scaleData",
signature="CellRouter",
definition=function(object, genes.use, scale.max, vars.regress){
if(is.null(genes.use)){
#genes.use <- object@var.genes
genes.use <- rownames(object@ndata)
}
data.use <- object@ndata[genes.use,]
if(!is.null(vars.regress)){
print('Regression...')
cat(vars.regress, '\n')
#data.use <- object@ndata[genes.use, , drop = FALSE];
#gene.expr <- as.matrix(x = data.use[genes.use, , drop = FALSE])
gene.expr <- as.matrix(object@ndata[genes.use, , drop = FALSE])
latent.data <- as.data.frame(object@sampTab[,vars.regress])
rownames(latent.data) <- rownames(object@sampTab)
colnames(latent.data) <- vars.regress
new.data <- sapply(X=genes.use, FUN=function(x){
regression.mat <- cbind(latent.data, gene.expr[x,])#cbind(latent.data, gene.expr[x,])
colnames(regression.mat) <- c(colnames(latent.data), "GENE")
fmla <- as.formula(
object = paste0(
"GENE ",
" ~ ",
paste(vars.regress, collapse = "+")
)
)
lm(formula = fmla, data = regression.mat)$residuals
})
data.use <- t(new.data)
}
#scale.data <- t(scale(t(object@ndata[genes.use,])))
scale.data <- t(scale(t(data.use)))
scale.data[is.na(scale.data)] <- 0
scale.data[which(scale.data > scale.max)] <- scale.max
object@scale.data <- as.data.frame(scale.data)
gc(verbose = FALSE)
object
}
)
#' Principal component analysis
#' @param object CelLRouter object
#' @param num.pcs Number of principlal components to compute
#' @param genes.use Genes used for principlam component analysis. Default is all genes in object@@ndata
#' @param seed seed
#' @export
setGeneric("computePCA", function(object, num.pcs, genes.use=NULL, seed=1) standardGeneric("computePCA"))
setMethod("computePCA",
signature="CellRouter",
definition=function(object, num.pcs, genes.use, seed){
#computePCA <- function(data, num.pcs, seed=7){
library(irlba)
if (!is.null(seed)) {
set.seed(seed = seed)
}
if(is.null(genes.use)){
#genes.use <- object@var.genes
genes.use <- rownames(object@ndata)
}
pca <- irlba(A = t(object@scale.data[genes.use,]), nv = num.pcs)
gene.loadings <- pca$v
#cell.embeddings <- pca$u
cell.embeddings <- pca$u %*% diag(pca$d)
sdev <- pca$d/sqrt(max(1, ncol(data) - 1))
rownames(gene.loadings) <- rownames(object@scale.data)
colnames(gene.loadings) <- paste0('PC', 1:num.pcs)
rownames(cell.embeddings) <- colnames(object@scale.data)
colnames(cell.embeddings) <- colnames(gene.loadings)
object@pca <- list(gene.loadings = gene.loadings, cell.embeddings=cell.embeddings, sdev=sdev)
object@rdimension <- as.data.frame(object@pca$cell.embeddings)
object
}
)
#' Perform dimensionality reduction using t-SNE
#' @param object do something
#' @param num.pcs Number of principal components used for dimensionality reduction using t-SNE
#' @param perplexity Perplexity
#' @param max_iter, Max number of iterations
#' @param seed seed
#' @import Rtsne
#' @export
setGeneric("computeTSNE", function(object, num.pcs, perplexity=30, max_iter=2000, seed=7) standardGeneric("computeTSNE"))
setMethod("computeTSNE",
signature="CellRouter",
definition=function(object, num.pcs, perplexity, max_iter, seed){
#computeTSNE <- function(pca, num.pcs, perplexity=40, max_iter=2000, seed=7){
library(Rtsne)
if (!is.null(seed)) {
set.seed(seed = seed)
}
#tsne.done <- Rtsne(pca$cell.embeddings[,1:num.pcs], perplexity = perplexity, max_iter = max_iter) #implement this type of tsne analysis in cellrouter, using PCA....
pca <- object@pca
#tsne.done <- Rtsne(pca$cell.embeddings[,1:num.pcs], max_iter = max_iter) #implement this type of tsne analysis in cellrouter, using PCA....
tsne.done <- Rtsne(pca$cell.embeddings[,1:num.pcs], perplexity=perplexity, max_iter = max_iter, check_duplicates = FALSE) #implement this type of tsne analysis in cellrouter, using PCA....
#tsne.done <- Rtsne(pca$cell.embeddings[,1:num.pcs])
#plot(tsne.done$Y, xlab= 't-SNE 1',ylab= 't-SNE 2', pch=20)
m <- tsne.done$Y
rownames(m) <- rownames(pca$cell.embeddings)
colnames(m) <- c('tSNE 1', 'tSNE 2')
object@tsne <- list(cell.embeddings=m)
object
}
)
#' Dimensionality reduction using diffusion components
#' @param object CellRouter objecr
#' @param genes.use Genes used for dimensionality reduction
#' @param k Parameter k to be used by the DiffusionMap function from destiny pakcage. default is 20
#' @param sigma Parameter sigma to be used by the DiffusionMap function from destiny pakcage. default is local
#' @param seed seed
#' @export
computeDC <- function(object, genes.use=NULL, k=20,sigma='local', seed=1){
library(destiny) #anyoing error with DLLs all the time...
if (!is.null(seed)) {
set.seed(seed = seed)
}
if(is.null(genes.use)){
genes.use <- object@var.genes
}
pca <- object@pca
diff.comp <- DiffusionMap(as.matrix(t(object@scale.data[genes.use,])), k=20, sigma='local')
dc <- eigenvectors(diff.comp)
rownames(dc) <- colnames(object@scale.data)
object@dc <- list(cell.embeddings=dc)
object
}
setGeneric("customSpace", function(object, matrix) standardGeneric("customSpace"))
setMethod("customSpace",
signature="CellRouter",
definition=function(object, matrix){
object@dr.custom <- list(cell.embeddings=matrix)
return(object)
}
)
#' Normalize the data
#' @param object CellRouter object
#' @export
setGeneric("Normalize", function(object) standardGeneric("Normalize"))
setMethod("Normalize",
signature="CellRouter",
definition=function(object){
x <- object@ndata
x <- t(t(x)/apply(x,2,sum))
x <- log1p(x * 10000)
object@ndata <- as.data.frame(x)
return(object)
}
)
#' Identify clusters baed on graph-clustering or model based clustering
#' @param object CellRouter object
#' @param method Method: graph-based clustering or model-based clustering
#' @param k number of nearest neighbors to build a k-nearest neighbors graph
#' @param num.pcs number of principal components that will define the space from where the kNN graph is identified. For example, if num.pcs=10, the kNN graph will be created from a 10-dimensional PCA space
#' @param sim.type Updates the kNN graph to encode cell-cell similarities. Only the Jaccard similarity is implemented in the current version
#' @export
setGeneric("findClusters", function(object, method='graph.clustering', k=20, num.pcs=20, sim.type='jaccard') standardGeneric("findClusters"))
setMethod("findClusters",
signature = "CellRouter",
definition = function(object, method, k, num.pcs, sim.type){
if(method=='graph.clustering'){
cat('Graph-based clustering\n')
cat('k: ', k, '\n')
cat('similarity type: ', sim.type, '\n')
cat('number of principal components: ', num.pcs, '\n')
object <- graphClustering(object, k=k, num.pcs=num.pcs, sim.type)
}else if(method=='model.clustering'){
cat('Model-based clustering\n')
cat('number of principal components: ', num.pcs)
object <- modelClustering(object, num.pcs=num.pcs)
}
object
}
)
#' Model-based clustering using the Mclust package
#' @param object CellRouter object
#' @param num.pcs number of principal components that will define the space used as input to perform model-based clustering
#' @export
setGeneric("modelClustering", function(object, num.pcs) standardGeneric("modelClustering"))
setMethod("modelClustering",
signature = "CellRouter",
definition = function(object, num.pcs){
library(mclust)
sampTab <- object@sampTab
colname <- 'population'
matrix <- object@pca$cell.embeddings[,1:num.pcs]
mclust <- Mclust(matrix)
sampTab[names(mclust$classification),colname] <- as.character(mclust$classification)
sampTab <- sampTab[order(as.numeric(sampTab[[colname]])),]
colors <- cRampClust(1:length(unique(sampTab[[colname]])), 8)
names(colors) <- unique(sampTab[[colname]])
replicate_row <- as.vector(unlist(lapply(split(sampTab, sampTab[[colname]]), nrow)))
colors_row <- rep(colors, times=replicate_row)
sampTab[,'colors'] <- colors_row
object@sampTab <- sampTab
object
}
)
#' Graph-based clustering
#' @param object CellRouter object
#' @param k number of nearest neighbors to build a k-nearest neighbors graph
#' @param num.pcs number of principal components that will define the space from where the kNN graph is identified. For example, if num.pcs=10, the kNN graph will be created from a 10-dimensional PCA space
#' @param sim.type Updates the kNN graph to encode cell-cell similarities. Only the Jaccard similarity is implemented in the current version
#' @param filename Save .gml file containing the kNN graph
#' @export
setGeneric("graphClustering", function(object, k=5, num.pcs, sim.type="jaccard",
filename="graph_subpopulations.gml") standardGeneric("graphClustering"))
setMethod("graphClustering",
signature = "CellRouter",
definition = function(object, k, num.pcs, sim.type, filename){
library('cccd')
library('proxy') # Library of similarity/dissimilarity measures for 'dist()'
#matrix <- object@rdimension
sampTab <- object@sampTab
matrix <- object@pca$cell.embeddings[,1:num.pcs]
print('building k-nearest neighbors graph')
dm <- as.matrix(dist(matrix))
h <- nng(dx=dm,k=k)
if(sim.type == 'jaccard'){
sim <- similarity.jaccard(h, vids=V(h), loops=FALSE)
}else if(sim.type == 'invlog'){
sim <- similarity.invlogweighted(h, vids=V(h))
}
el <- get.edgelist(h)
weights <- sim[el]
E(h)$weight <- weights
V(h)$name <- rownames(matrix)
edges <- as.data.frame(get.edgelist(h))
rownames(edges) <- paste(edges$V1, edges$V2, sep='_')
edges$weight <- as.numeric(E(h)$weight)
rownames(sim) <- V(h)$name
colnames(sim) <- V(h)$name
## Community detection to discover subpopulation structure
print('discoverying subpopulation structure')
comms <- multilevel.community(as.undirected(h), weights = E(h)$weight)
V(h)$comms <- membership(comms)
cell.comms <- commToNames(comms, '') #SP means SubPopulations
allcells <- as.vector(unlist(cell.comms))
## Making sure that color mappings are correct
sampTab <- sampTab[allcells,] #changesorder of cells in the table
sampTab$population <- ''
sampTab$colors <- ''
comm.colors <- cRampClust(unique(membership(comms)), 8)
#comm.colors <- cRampClust(unique(membership(comms)), length(unique(membership(comms))))
names(comm.colors) <- names(cell.comms)
for(comm in names(cell.comms)){
sampTab[cell.comms[[comm]], 'population'] <- comm
sampTab[cell.comms[[comm]], 'colors'] <- comm.colors[comm]
}
#sampTab$community <- as.vector(unlist(lapply(strsplit(sampTab$population, split="_"), "[", 2)))
sampTab$community <- as.vector(sampTab$population)
## mapping information to the igraph object
V(h)[rownames(sampTab)]$subpopulation <- sampTab$colors
V(h)[rownames(sampTab)]$colors <- sampTab$colors
V(h)[names(nodeLabels(sampTab,'community'))]$label <- nodeLabels(sampTab, 'community')
V(h)$size <- 5
E(h)$arrow.size <- 0.01
colors <- rainbow(max(membership(comms)))
#print("plotting graph in RStudio")
#plot(h,vertex.color=V(h)$colors, vertex.frame.color=V(h)$colors, layout=as.matrix(matrix[,dim.plot]))
#print('done plotting graph')
## Useful information about the graph
graph <- list()
graph[['network']] <- h
graph[['edges']] <- edges
graph[['similarity_matrix']] <- sim
graph[['subpopulation']] <- cell.comms
graph[['communities']] <- comms
print('updating CellRouter object')
object@graph <- graph
if(nrow(object@rawdata) > 0){
object@rawdata <- object@rawdata[,rownames(sampTab)]
}
object@ndata <- object@ndata[,rownames(sampTab)]
object@sampTab <- sampTab
write.graph(graph = h, file = filename, format = 'gml')
rm(h)
rm(edges)
rm(sim)
return(object)
}
)
#https://briatte.github.io/ggnetwork/
#' Plot kNN graph`
#' @param object CellRouter object
#' @param reduction.type The reduced dimension space used to visualize the kNN graph: tsne, pca, dc or custom
#' @param column.ann column in the metadata table used to annotate the kNN graph. For example, clusters, sorted cell populations
#' @param column.color column in the metadata table corresponding to color used to annotate the kNN graph. Should correspond to the metadata in column.ann
#' @param dims.use dimensions to plot
#' @param width width of output file
#' @param height height og outpur file
#' @param filename name of pdf file generated
#' @import ggplot2
#' @export
setGeneric("plotKNN", function(object, reduction.type="tsne", column.ann, column.color,
dims.use=c(1,2), width=10, height=10, filename='kNN_graph.pdf') standardGeneric("plotKNN"))
setMethod("plotKNN",
signature = "CellRouter",
definition = function(object, reduction.type, column.ann, column.color,
dims.use, width, height, filename){
library(ggnetwork)
h <- object@graph$network
matrix <- slot(object, reduction.type)$cell.embeddings[V(h)$name,dims.use]
colors <- unique(object@sampTab[[column.color]])
names(colors) <- unique(as.vector(object@sampTab[[column.ann]]))
g <- ggnetwork(h, layout=as.matrix(matrix), na.rm=TRUE)
pdf(file=filename, width=width, height=height)
g2 <- ggplot(g, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(alpha=0.3) +
geom_nodes(aes(color = factor(comms)), size=1) +
theme_blank() + scale_color_manual("", values=colors) +
guides(col=guide_legend(direction="vertical", keywidth = 0.75, keyheight = 0.85, override.aes = list(size=3)))
plot(g2)
dev.off()
plot(g2)
}
)
#' Build kNN graph
#' @param object CellRouter object
#' @param k number of nearest neighbors to build a k-nearest neighbors graph for trajectory reconstruction
#' @param column.ann Column in the metadata table specifying the transitions to be identified. For example, if 'population' is provided, transitions will be identified between clusters previously identified. However, sorted cell populations or customized states can also be used. Check our tutorials for detailed examples.
#' @param num.pcs number of principal components that will define the space from where the kNN graph is identified. For example, if num.pcs=10, the kNN graph will be created from a 10-dimensional PCA space
#' @param sim.type Updates the kNN graph to encode cell-cell similarities. Only the Jaccard similarity is implemented in the current version
#' @param filename name of gml file containing the kNN graph
#' @export
setGeneric("buildKNN", function(object, k=5, column.ann, num.pcs=20, sim.type="jaccard", filename="graph_clusters.gml") standardGeneric("buildKNN"))
setMethod("buildKNN",
signature = "CellRouter",
definition = function(object, k, column.ann, num.pcs, sim.type, filename){
suppressWarnings(suppressMessages(library('cccd')))
suppressWarnings(suppressMessages(library('proxy'))) # Library of similarity/dissimilarity measures for 'dist()'
matrix <- object@pca$cell.embeddings[,1:num.pcs]
sampTab <- object@sampTab
smapTab <- sampTab[order(sampTab[[column.ann]]),]
print('building k-nearest neighbors graph')
dm <- as.matrix(dist(matrix))
h <- nng(dx=dm,k=k)
if(sim.type == 'jaccard'){
sim <- similarity.jaccard(h, vids=V(h), loops=FALSE)
}else if(sim.type == 'invlog'){
sim <- similarity.invlogweighted(h, vids=V(h))
}
el <- get.edgelist(h)
weights <- sim[el]
E(h)$weight <- weights
V(h)$name <- rownames(matrix)
edges <- as.data.frame(get.edgelist(h))
rownames(edges) <- paste(edges$V1, edges$V2, sep='_')
edges$weight <- as.numeric(E(h)$weight)
rownames(sim) <- V(h)$name
colnames(sim) <- V(h)$name
V(h)[rownames(sampTab)]$comms <- as.vector(sampTab[[column.ann]]) #cluster->celltype
cell.comms <- split(sampTab, sampTab[[column.ann]])
cell.comms <- lapply(cell.comms, rownames)
#allcells <- as.vector(unlist(cell.comms))
#sampTab <- sampTab[allcells,] #change order of cells in the table
#V(h)[names(nodeLabels(sampTab,column.ann))]$label <- nodeLabels(sampTab, column.ann)
## Useful information about the graph
graph <- list()
graph[['network']] <- h
graph[['edges']] <- edges
graph[['similarity_matrix']] <- sim
graph[['subpopulation']] <- cell.comms
#graph[['communities']] <- comms
print('updating CellRouter object')
object@graph <- graph
#object@rawdata <- object@rawdata[,rownames(sampTab)]
object@ndata <- object@ndata[,rownames(sampTab)]
object@sampTab <- sampTab
write.graph(graph = h, file = filename, format = 'gml')
rm(h)
rm(edges)
rm(sim)
return(object)
}
)
#' Plot heatmap with gene signatures
#' @param object CellRouter object
#' @param markers Genes preferentially expressed in each column.ann. For example, in clusters or sorted populations
#' @param column.ann Column in the metadata table used to annotate the kNN graph. For example, clusters, sorted cell populations
#' @param column.color Color
#' @param num.cells Number of cells to show in the heatmap
#' @param threshold Threshold used to center the data
#' @param genes.show #Vector of gene names to show in the heatmap
#' @param low Color for low expression
#' @param intermediate Color for intermediate expression
#' @param high Color for high expression
#' @param width width
#' @param height height
#' @param filename filename
#' @export
plotSignaturesHeatmap <- function(object, markers, column.ann, column.color, num.cells=NULL, threshold=2, genes.show=NULL,
low='purple', intermediate='black', high='yellow', order=NULL, width, height, filename){
if(is.null(num.cells)){
print('here')
cells.keep <- rownames(object@sampTab)
print(table(object@sampTab[[column.ann]]))
}else{
#cells.use <- object@sampTab %>% group_by_(column.ann) %>% sample_n(size = num.cells, replace = TRUE)
cells.use <- split(object@sampTab, object@sampTab[[column.ann]])
cells.use <- lapply(cells.use, function(x){
if(nrow(x) < num.cells){
cells.use.x <- x[sample(rownames(x), size = nrow(x)),]
}else{
cells.use.xx <- x[sample(rownames(x), size = num.cells),]
}
})
cells.use.tmp <- do.call(rbind, cells.use)
cells.keep <- as.vector(cells.use.tmp$sample_id)
}
#data <- object@ndata[,cells.use]
matrix <- center_with_threshold(object@ndata[,cells.keep], threshold)
paletteLength <- 100
#myColor <- colorRampPalette(c("purple","black","yellow"))(paletteLength)
myColor <- colorRampPalette(c(low, intermediate, high))(paletteLength)
myBreaks <- c(seq(min(matrix), 0, length.out=ceiling(paletteLength/2) + 1),
seq(max(matrix)/paletteLength, max(matrix), length.out=floor(paletteLength/2)))
library(data.table)
markers2 <- as.data.frame(markers)
#markers2 <- as.data.frame(markers)
#markers2 <- as.data.table(markers2)[, .SD[which.max(fc.column)], by=gene]
#markers2 <- as.data.frame(markers2)
#markers2 <- as.data.frame(markers)
#markers2 <- markers2[!duplicated(markers2$gene),] #need to make sure there is no duplicated element...
sampTab <- object@sampTab
sampTab <- sampTab[cells.keep,]
if(column.ann == 'population'){
markers2 <- markers2[order(as.numeric(markers2$population)),]
rownames(markers2) <- as.vector(markers2$gene)
sampTab <- sampTab[order(as.numeric(sampTab$population)),]
}else if(!is.null(order)){
markers2 <- markers2[order(factor(markers2$population, levels=order)),]
sampTab <- sampTab[order(factor(sampTab[[column.ann]], levels=order)),]
}else{
markers2 <- markers2[order(as.character(markers2$population)),]
rownames(markers2) <- as.vector(markers2$gene)
sampTab <- sampTab[order(as.character(sampTab[[column.ann]])),]
}
#clusters <- as.vector(object@sampTab$population)
clusters <- as.vector(sampTab[[column.ann]])
names(clusters) <- rownames(sampTab)
#clusters <- clusters[order(clusters)]
ann_col <- data.frame(population=as.vector(clusters), stringsAsFactors = FALSE)
rownames(ann_col) <- names(clusters)
ann_row <- data.frame(signature=as.vector(markers2$population), stringsAsFactors = FALSE)
rownames(ann_row) <- as.vector(markers2$gene)
if(!is.null(order)){
ann_col$population <- factor(ann_col$population, levels=order)
ann_row$signature <- factor(ann_row$signature, levels=order)
}
#colors <- cRampClust(cluster.vector, 8)
#names(colors) <- cluster.vector
colors <- unique(sampTab[[column.color]])
names(colors) <- unique(as.vector(sampTab[[column.ann]]))
color_lists <- list(population=colors, signature=colors)
#replicate_row <- as.vector(unlist(lapply(split(ann_row, ann_row$signature), nrow)))
#colors_row <- rep(colors, times=replicate_row)
#replicate_col <- as.vector(unlist(lapply(split(ann_col, ann_col$population), nrow)))
#colors_col <- rep(colors, times=replicate_col)
index <- getIndexes(ann_col, ann_row, order.columns = unique(ann_col$population), order.rows = unique(ann_row$signature))
if(is.null(genes.show)){
genes.show <- markers2 %>% group_by(population) %>% top_n(5, fc)
genes.show <- as.vector(genes.show$gene)
selected <- as.vector(markers2$gene)
selected[!(selected %in% genes.show)] <- ""
}else{
selected <- as.vector(markers2$gene)
selected[!(selected %in% genes.show)] <- ""
}
pheatmap(matrix[rownames(ann_row),rownames(ann_col)], cluster_rows=FALSE, cluster_cols=FALSE, color = myColor, breaks=myBreaks, fontsize=15,
gaps_row = index$rowsep, gaps_col = index$colsep, annotation_col = ann_col, annotation_row = ann_row, annotation_colors = color_lists,
labels_row = selected,
labels_col = rep("", ncol(matrix)), width=width, height=height, filename=filename)
#pheatmap(matrix[rownames(ann_row),rownames(ann_col)], cluster_rows=FALSE, cluster_cols=FALSE, color = myColor, breaks=myBreaks,
# gaps_row = index$rowsep, gaps_col = index$colsep, annotation_col = ann_col, annotation_row = ann_row, annotation_colors = color_lists,
# labels_row = selected,
# labels_col = rep("", ncol(matrix)))
gc(verbose = FALSE)
#pdf(file='results/heatmap_2.pdf', width=10, height=8)
#heatmap.2(as.matrix(matrix[rownames(ann_row),rownames(ann_col)]), col=myColor,trace="none",
# density.info="none", scale="none",margin=c(5,5), key=TRUE, Colv=F, Rowv=F,
# srtCol=60, dendrogram="none", cexCol=0.75, cexRow=0.65, labRow=FALSE,
# labCol = FALSE, symm=T,symkey=T,
## ColSideColors=colors_col,
# RowSideColors=colors_row,
# colsep=index$colsep, rowsep=index$rowsep, sepcolor = 'black')
#dev.off()
}
#' Compute mean expression of each gene in each population
#' @param object CellRouter object
#' @param column Column in the metadata table to group cells for differential expression. For example, if 'population' is specified, population-specific gene signatures will be identified
#' @param pos.only Only uses genes upregulated
#' @param fc.threshold FOld change threshold
#' @export
setGeneric("computeValue", function(object, column='population', fun='max') standardGeneric("computeValue"))
#computeFC(object, column, pos.only, fc.threshold)
setMethod("computeValue",
signature = "CellRouter",
definition = function(object, column, fun){
print('discovering subpopulation-specific gene signatures')
expDat <- object@ndata
#membs <- as.vector(object@sampTab$population)
membs <- as.vector(object@sampTab[[column]])
diffs <- list()
for(i in unique(membs)){
cat('cluster ', i, '\n')
if(sum(membs == i) == 0) next
if(fun == 'max'){
m <- if(sum(membs != i) > 1) apply(expDat[, membs != i], 1, max) else expDat[, membs != i]
n <- if(sum(membs == i) > 1) apply(expDat[, membs == i], 1, max) else expDat[, membs == i]
}else if(fun == 'median'){
m <- if(sum(membs != i) > 1) apply(expDat[, membs != i], 1, median) else expDat[, membs != i]
n <- if(sum(membs == i) > 1) apply(expDat[, membs == i], 1, median) else expDat[, membs == i]
}else{
m <- if(sum(membs != i) > 1) apply(expDat[, membs != i], 1, mean) else expDat[, membs != i]
n <- if(sum(membs == i) > 1) apply(expDat[, membs == i], 1, mean) else expDat[, membs == i]
}
d <- data.frame(np=m, p=n) #log scale
diffs[[i]] <- d
}
return (diffs)
}
)
#' Compute fold changes and find gene signatures
#' @param object CellRouter object
#' @param column Column in the metadata table to group cells for differential expression. For example, if 'population' is specified, population-specific gene signatures will be identified
#' @param pos.only Only uses genes upregulated
#' @param fc.threshold FOld change threshold
#' @export
setGeneric("computeFC", function(object, column='population', pos.only=TRUE, fc.threshold=0.25) standardGeneric("computeFC"))
#computeFC(object, column, pos.only, fc.threshold)
setMethod("computeFC",
signature = "CellRouter",
definition = function(object, column, pos.only, fc.threshold){
print('discovering subpopulation-specific gene signatures')
expDat <- object@ndata
#membs <- as.vector(object@sampTab$population)
membs <- as.vector(object@sampTab[[column]])
diffs <- list()
for(i in unique(membs)){
cat('cluster ', i, '\n')
if(sum(membs == i) == 0) next
m <- if(sum(membs != i) > 1) apply(expDat[, membs != i], 1, mean) else expDat[, membs != i]
n <- if(sum(membs == i) > 1) apply(expDat[, membs == i], 1, mean) else expDat[, membs == i]
#pv <- binompval(m/sum(m),sum(n),n)
#d <- data.frame(mean.np=m, mean.p=n, fc=n-m, pv=pv) #log scale
d <- data.frame(mean.np=m, mean.p=n, fc=n-m) #log scale
if(pos.only){
genes.use <- rownames(d[which(d$fc > fc.threshold),])
}else{
genes.use <- rownames(d[which(abs(d$fc) > fc.threshold),])
}
m <- m[genes.use]
n <- n[genes.use]
print(length(genes.use))
#diffs[[i]] <- d
#d <- data.frame(mean.np=m, mean.p=n, fc=n/m, pv=pv) #linear scale
#d <- d[!is.infinite(d$fc),]
#d <- d[which(d$pv < 0.05),]
#d <- d[order(d$pv, decreasing=FALSE),]
#d <- d[order(d$mean.p, decreasing=TRUE),]
#diffs[[i]] <- d[which(d$pv < pvalue & d$fc > foldchange),]
#for wilcox test
coldata <- object@sampTab
coldata[membs == i, "group"] <- "Group1"
coldata[membs != i, "group"] <- "Group2"
coldata$group <- factor(x = coldata$group)
countdata.test <- as.matrix(expDat[genes.use, rownames(x = coldata)])
p_val <- sapply(X = 1:nrow(x = countdata.test), FUN = function(x) {
return(wilcox.test(countdata.test[x, ] ~ coldata$group)$p.value)
})
#to.return <- data.frame(p_val, row.names = rownames(countdata.test))
d2 <- data.frame(mean.np=m, mean.p=n, fc=n-m, pv=p_val, p.adj=p.adjust(p_val, method='bonferroni'))
#d2 <- data.frame(mean.np=m, mean.p=n, fc=n-m)
#diffs[[i]] <- d2[which(d2$pv < pvalue),]
diffs[[i]] <- d2
}
object@signatures <- diffs
return (object)
}
)
#' Compute fold changes and find gene signatures
#' @param object CellRouter object
#' @param column Column in the metadata table to group cells for differential expression. For example, if 'population' is specified, population-specific gene signatures will be identified
#' @param pos.only Only uses genes upregulated
#' @param fc.threshold FOld change threshold
#' @export
#condition #control
setGeneric("compareTwoGroups", function(object, column='population', group1, group2, fc.threshold=0.25) standardGeneric("compareTwoGroups"))
#computeFC(object, column, pos.only, fc.threshold)
setMethod("compareTwoGroups",
signature = "CellRouter",
definition = function(object, column, group1, group2, fc.threshold){
print('discovering subpopulation-specific gene signatures')
expDat <- object@ndata
#membs <- as.vector(object@sampTab$population)
membs <- as.vector(object@sampTab[[column]])
diffs <- list()
m <- if(sum(membs == group2) > 1) apply(expDat[, membs == group2], 1, mean) else expDat[, membs == group2]
n <- if(sum(membs == group1) > 1) apply(expDat[, membs == group1], 1, mean) else expDat[, membs == group1]
d <- data.frame(mean.np=m, mean.p=n, fc=n-m) #log scale
genes.use <- rownames(d[which(abs(d$fc) > fc.threshold),])
m <- m[genes.use]
n <- n[genes.use]
print(length(genes.use))
#for wilcox test
coldata <- object@sampTab
coldata[membs == group1, "group"] <- "Group1"
coldata[membs == group2, "group"] <- "Group2"
coldata$group <- factor(x = coldata$group)
countdata.test <- as.matrix(expDat[genes.use, rownames(x = coldata)])
p_val <- sapply(X = 1:nrow(x = countdata.test), FUN = function(x) {
return(wilcox.test(countdata.test[x, ] ~ coldata$group)$p.value)
})
#to.return <- data.frame(p_val, row.names = rownames(countdata.test))
d2 <- data.frame(mean.np=m, mean.p=n, fc=n-m, pv=p_val, p.adj=p.adjust(p_val, method='bonferroni'))
#d2 <- data.frame(mean.np=m, mean.p=n, fc=n-m)
#diffs[[i]] <- d2[which(d2$pv < pvalue),]
return (d2)
}
)
#' Find gene signatures based on a template-matching approach
#' @param expDat Expression matrix
#' @param sampTab Sample annotation table
#' @param qtile qyantile to select top genes correlated with the idealized expression pattern
#' @param remove Remove overlaping genes
#' @param dLevel Groups to compare
#' @export
ranked_findSpecGenes<-function# find genes that are preferentially expressed in specified samples
(expDat, ### expression matrix
sampTab, ### sample table
qtile=0.95, ### quantile
remove=FALSE,
dLevel="population_name" #### annotation level to group on
){
cat("Template matching...\n")
myPatternG<-cn_sampR_to_pattern(as.vector(sampTab[,dLevel]));
specificSets<-apply(myPatternG, 1, cn_testPattern, expDat=expDat);
# adaptively extract the best genes per lineage
cat("First pass identification of specific gene sets...\n")
cvalT<-vector();
ctGenes<-list();
ctNames<-unique(as.vector(sampTab[,dLevel]));
for(ctName in ctNames){
x<-specificSets[[ctName]];
cval<-quantile(x$cval, qtile, na.rm = TRUE);
tmp<-rownames(x[x$cval>cval,]);
specificSets[[ctName]] <- specificSets[[ctName]][tmp,]
ctGenes[[ctName]]<-tmp;
cvalT<-append(cvalT, cval);
}
if(remove){
cat("Remove common genes...\n");
# now limit to genes exclusive to each list
specGenes<-list();
for(ctName in ctNames){
others<-setdiff(ctNames, ctName);
x<-setdiff( ctGenes[[ctName]], unlist(ctGenes[others]));
specificSets[[ctName]] <- specificSets[[ctName]][x,]
specificSets[[ctName]]$gene <- rownames(specificSets[[ctName]])
specGenes[[ctName]]<-x;
}
result <- specGenes
}else {
result <- ctGenes;
}
specificSets <- lapply(specificSets, function(x){x[order(x$cval, decreasing = TRUE),]})
specificSets <- lapply(specificSets, function(x){colnames(x) <- c('tm.pval', 'cval', 'tm.padj', 'gene'); x})
specificSets
}
#' Helper function
#' @param sampR sampR
cn_sampR_to_pattern<-function# return a pattern for use in cn_testPattern (template matching)
(sampR){
d_ids<-unique(as.vector(sampR));
nnnc<-length(sampR);
ans<-matrix(nrow=length(d_ids), ncol=nnnc);
for(i in seq(length(d_ids))){
x<-rep(0,nnnc);
x[which(sampR==d_ids[i])]<-1;
ans[i,]<-x;
}
colnames(ans)<-as.vector(sampR);
rownames(ans)<-d_ids;
ans;
}
#' Helper function
#' @param pattern pattern
#' @param expDat expression matrix
cn_testPattern<-function(pattern, expDat){
pval<-vector();
cval<-vector();
geneids<-rownames(expDat);
llfit<-ls.print(lsfit(pattern, t(expDat)), digits=25, print=FALSE);
xxx<-matrix( unlist(llfit$coef), ncol=8,byrow=TRUE);
ccorr<-xxx[,6];
cval<- sqrt(as.numeric(llfit$summary[,2])) * sign(ccorr);
pval<-as.numeric(xxx[,8]);
#qval<-qvalue(pval)$qval;
holm<-p.adjust(pval, method='holm');
#data.frame(row.names=geneids, pval=pval, cval=cval, qval=qval, holm=holm);
data.frame(row.names=geneids, pval=pval, cval=cval,holm=holm);
}
#' Identify gene signatures
#' @param object CellRouter object
#' @param column Specify the groups to compare
#' @param test.use Differential expression test to use. Default is wilcox. Alternative is based on template-matching. Possible values: wilcox or template
#' @param pos.only Use only upregulated genes
#' @param fc.threshold Fold change threshold
#' @param fc.tm Wheter to include fold change values in the template-matching differential expression analysis
#' @export