-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNicheNet.Rmd
1184 lines (992 loc) · 47.9 KB
/
NicheNet.Rmd
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
---
title: "Re-analysis of NicheNet dataset"
---
```{r setup, include=F}
library(colorspace)
library(scales)
source("~/Dropbox/GDB/line2user.R")
knitr::opts_chunk$set(
echo=F,
message=F,
warning=F
)
```
# NicheNet calculation sanity check
```{r load_raw}
setwd("~/Dropbox/GDB_archive/CMapCorr_files/GSE47542_RAW/")
temp_TNF_p <- c("GSM1151945_Agilent_251485053931_S01_GE2_107_Sep09_1_4.txt.gz",
"GSM1151946_Agilent_251485053932_S01_GE2_107_Sep09_1_4.txt.gz",
"GSM1151950_Agilent_251485054074_S01_GE2_107_Sep09_1_4.txt.gz")
temp_TNF_m <- c("GSM1151933_Agilent_251485053931_S01_GE2_107_Sep09_1_2.txt.gz",
"GSM1151934_Agilent_251485053932_S01_GE2_107_Sep09_1_2.txt.gz",
"GSM1151938_Agilent_251485054074_S01_GE2_107_Sep09_1_2.txt.gz")
temp_CTRL_p <- c("GSM1151939_Agilent_251485053931_S01_GE2_107_Sep09_1_3.txt.gz",
"GSM1151940_Agilent_251485053932_S01_GE2_107_Sep09_1_3.txt.gz",
"GSM1151947_Agilent_251485054010_S01_GE2_107_Sep09_1_4.txt.gz")
temp_CTRL_m <- c("GSM1151928_Agilent_251485053932_S01_GE2_107_Sep09_1_1.txt.gz",
"GSM1151935_Agilent_251485054010_S01_GE2_107_Sep09_1_2.txt.gz",
"GSM1151951_Agilent_251485053931_S01_GE2_107_Sep09_1_1.txt.gz")
TNFp <- sapply(temp_TNF_p,function(X) {
DS <- read.table(X,sep="\t",header=T,
skip=which(grepl("^FEAT",scan(X,"character",sep="\n"))) - 1)
return(DS[DS$SystematicName == "NM_000201","LogRatio"])
},USE.NAMES=F)
TNFm <- sapply(temp_TNF_m,function(X) {
DS <- read.table(X,sep="\t",header=T,
skip=which(grepl("^FEAT",scan(X,"character",sep="\n"))) - 1)
return(DS[DS$SystematicName == "NM_000201","LogRatio"])
},USE.NAMES=F)
CTRLp <- sapply(temp_CTRL_p,function(X) {
DS <- read.table(X,sep="\t",header=T,
skip=which(grepl("^FEAT",scan(X,"character",sep="\n"))) - 1)
return(DS[DS$SystematicName == "NM_000201","LogRatio"])
},USE.NAMES=F)
CTRLm <- sapply(temp_CTRL_m,function(X) {
DS <- read.table(X,sep="\t",header=T,
skip=which(grepl("^FEAT",scan(X,"character",sep="\n"))) - 1)
return(DS[DS$SystematicName == "NM_000201","LogRatio"])
},USE.NAMES=F)
rm(list=grep("^temp",ls(),value=T))
```
```{r importNN}
nn_db <- readRDS(url("https://zenodo.org/record/3260758/files/expression_settings.rds"))
nn_db <- nn_db[sapply(sapply(nn_db,"[[","from"),length) == 1]
diff_M <- nn_db$cifola_Tnf$diffexp[nn_db$cifola_Tnf$diffexp$gene == "ICAM1",c("lfc","qval")]
diff_P <- nn_db$cifola_Tnf_plastic$diffexp[nn_db$cifola_Tnf_plastic$diffexp$gene == "ICAM1",c("lfc","qval")]
```
```{r plot_raw,fig.height=4,fig.width=6}
par(mar=c(3,3,3,1),mgp=2:0)
boxplot(list(TNFp=as.vector(TNFp),
CTRLp=as.vector(CTRLp),
TNFm=as.vector(TNFm),
CTRLm=as.vector(CTRLm)),
main="ICAM1 expression",
ylab="Log-ratio vs reference RNA")
text(1.5,par("usr")[3] + (par("usr")[4] - par("usr")[3]) / 2,
paste("NicheNet",
paste0("logFC = ",signif(diff_P$lfc,2)),
paste0("FDR = ",signif(diff_P$qval,2)),
sep="\n"))
text(3.5,par("usr")[3] + (par("usr")[4] - par("usr")[3]) / 2,
paste("NicheNet",
paste0("logFC = ",signif(diff_M$lfc,2)),
paste0("FDR = ",signif(diff_M$qval,2)),
sep="\n"))
```
Looks like the sign of the logFC is flipped in the NicheNet calculations. Multiplying logFC values from the NicheNet dataset by -1 for the remainder of this analysis.
```{r cleanup_sanity_check}
rm(list=ls())
source("~/Dropbox/GDB/line2user.R")
```
****
# NicheNet data summary
```{r AvC_load_data}
load("~/Dropbox/GDB_archive/CMapCorr_files/NN_ALLvCT_dat.RData")
load("~/Dropbox/GDB_archive/CMapCorr_files/NN_ALLvCT_corr.RData")
# load("~/Dropbox/GDB_archive/CMapCorr_files/NN_ALLvCT_DEovlp.RData")
```
```{r DSsummary,fig.height=6,fig.width=7}
# remove data from same lab in same cell type:
DSinfo <- DSinfo[!names(DSinfo) %in% c("IL10","IGF1")]
temp_DScount <- table(do.call(rbind,DSinfo)[,c("cell_type","ligand")])
temp_DScount <- temp_DScount[,rev(colnames(temp_DScount))]
temp_ligcount <- apply(temp_DScount,1,function(X) sum(X > 0))
temp_ctcount <- apply(temp_DScount,2,function(X) sum(X > 0))
layout(matrix(c(2,1,0,3),2),widths=c(4,1),heights=c(1,4))
par(mar=c(7,4,0,0),mgp=2:0)
image(x=1:nrow(temp_DScount),y=1:ncol(temp_DScount),z=temp_DScount,
xaxt="n",yaxt="n",xlab=NA,ylab=NA,
col=sequential_hcl(max(temp_DScount) + 1,palette="purple-yellow",rev=T))
abline(h=seq(1.5,ncol(temp_DScount)),col="grey80",lwd=0.5)
abline(v=seq(1.5,nrow(temp_DScount)),col="grey80",lwd=0.5)
text(1:nrow(temp_DScount),rep(line2user(0.5,1),nrow(temp_DScount)),
rownames(temp_DScount),srt=45,adj=c(1,0.5),xpd=NA,cex=0.9)
mtext("Cell Type",side=1,line=6,font=2)
mtext(colnames(temp_DScount),at=1:ncol(temp_DScount),
side=2,las=2,line=0.1,cex=0.9)
mtext("Ligand",side=2,line=3,font=2)
text(which(temp_DScount > 0,arr.ind=T),
labels=temp_DScount[temp_DScount > 0],
col="lightgoldenrodyellow",cex=0.9)
par(mar=c(0.1,4,1,0))
plot(temp_ligcount,type="h",lwd=20,lend=1,col="grey80",
bty="n",xaxt="n",xaxs="i",yaxs="i",
xlim=c(0.5,length(temp_ligcount) + 0.5),
ylim=c(0.5,max(temp_ligcount) + 0.5),
ylab=paste("Total","unique ligands",sep="\n"))
par(mar=c(7,0.1,0,1),mgp=c(3,1,0))
plot(NA,NA,xlim=c(0.5,max(temp_ctcount) + 0.5),ylim=c(0.5,length(temp_ctcount) + 0.5),
yaxt="n",yaxs="i",xaxs="i",bty="n",xlab=paste("Total unique","cell types",sep="\n"))
temp <- sapply(seq_along(temp_ctcount),function(X)
points(c(0,temp_ctcount[X]),c(X,X),type="l",lwd=15,lend=1,col="grey80"))
# plot(temp_ctcount,type="h",,
# xaxt="n",xaxs="i",bty="n",ylab=paste("Total","unique ligands",sep="\n"),
# xlim=c(0.5,length(temp_ligcount) + 0.5))
```
`r length(temp_ctcount)` ligands were assayed in multiple datasets, `r sum(temp_ctcount > 1)` of which occurred across more than one cell type.
```{r DSsummary_cleanup}
rm(list=grep("^temp",ls(),value=T))
```
****
# Comparing across vs within cell type per ligand treatment
Previous analysis (see below) included a "within lab" category that was unclear, so it has been removed.
All data (including time-series) is included in this analysis. NicheNet calculation of time-series data seems to result in all positive logFC values, so that makes some things a little screwy. In those cases, time-series data was only compared to other time-series data.
## Correlation
```{r AvC_corr_plot,fig.height=4,fig.width=8}
temp_order <- names(sort(sapply(unique(c(names(corr_all),names(corr_ct))),
function(X) mean(c(corr_all[[X]],corr_ct[[X]])))))
lig_col <- qualitative_hcl(length(nn_ligands),palette="dark3",alpha=0.5)
temp_colnames <- c(temp_order,nn_ligands[!nn_ligands %in% temp_order])
names(lig_col) <- temp_colnames[unlist(sapply(seq(1,ceiling(length(nn_ligands)/2)),
function(X) c(X,X + ceiling(length(nn_ligands)/2)),
simplify=F))]
temp_corr <- list(ALL=corr_all[temp_order[temp_order %in% names(corr_all)]],
CT=corr_ct[temp_order[temp_order %in% names(corr_ct)]])
temp_lig <- sapply(strsplit(names(unlist(temp_corr,recursive=F)),".",fixed=T),"[[",2)
layout(rbind(1:2),widths=c(4,1))
par(mar=c(5,3,1,0.5),mgp=2:0)
plot(NA,NA,xlim=c(1,length(unlist(temp_corr,recursive=F))),ylim=range(temp_corr),
xaxt="n",xlab=NA,ylab="Spearman correlation of log(fold-change)")
abline(v=sapply(temp_corr,length)[1] + 0.5,col="grey70")
abline(h=0,lty=2,col="grey50")
temp_x <- unlist(mapply(rep,x=seq_along(unlist(temp_corr,recursive=F)),
times=sapply(unlist(temp_corr,recursive=F),length)))
points(x=jitter(temp_x,amount=0.2),y=unlist(temp_corr),
pch=19,col=lig_col[temp_lig][temp_x])
mtext(temp_lig,side=1,line=0.1,at=seq_along(temp_lig),las=3,col=alpha(lig_col[temp_lig],1))
segments(x0=c(0,sapply(temp_corr,length)[1]) + 0.75,
x1=cumsum(sapply(temp_corr,length)) + 0.25,
y0=line2user(4,1),y1=line2user(4,1),xpd=NA,lwd=2)
segments(x0=c(c(0,sapply(temp_corr,length)[1]) + 0.75,
cumsum(sapply(temp_corr,length)) + 0.25),
x1=c(c(0,sapply(temp_corr,length)[1]) + 0.75,
cumsum(sapply(temp_corr,length)) + 0.25),
y0=line2user(4,1),y1=line2user(3.8,1),xpd=NA,lwd=2)
mtext(c("Pairwise across all cell types","Pairwise within same cell type"),
at=0.5 + cumsum(sapply(temp_corr,length)) - sapply(temp_corr,length) / 2,
side=1,line=4)
par(mar=c(5,0,1,3))
boxplot(list(ALL=unlist(corr_all),CT=unlist(corr_ct)),
xaxt="n",xlab=NA,yaxt="n",ylab=NA)
axis(4)
mtext("Spearman correlation of log(fold-change)",side=4,line=2)
mtext(c("Across all","Cell type"),side=1,line=0.1,at=1:2,las=3)
mtext(paste0("p=",signif(wilcox.test(unlist(corr_all),unlist(corr_ct))$p.value,2)),
side=1,line=-0.1,at=1.5,las=3,adj=0,cex=0.7)
rm(list=grep("^temp",ls(),value=T))
```
## Differential gene expression
Differentially expressed genes were determined by |logFC| >= 1 and FDR <= 0.1 (similar to Browaeys *et al.*, 2020). This figure shows number of DE genes overlapping across all datasets testing the same ligand vs. datasets testing the same ligand in the same cell type vs. datasets from the same accession number (and same ligand / cell type). P-values for overlap (y-axis) were determined after determining probability of overlapping DE genes by chance through bootstrapping.
```{r AvC_diffexp_boxplot_v2,fig.height=8,fig.width=9}
par(mfrow=c(2,6),mgp=2:0)
for (charLFC in c("2","4")) {
for (charFDR in c("1","01")) {
temp_ds <- load(paste0("~/Dropbox/GDB_archive/CMapCorr_files/NN_ALLvCT_DEovlp_",
"LFC",charLFC,"FDR",charFDR,".RData"))
LFC <- log2(as.integer(charLFC))
FDR <- as.numeric(paste0("0.",charFDR))
temp_y <- lapply(c(ALL="P_all",CT="P_ct"),function(X) -log10(unlist(get(X))))
temp_x <- list(ALL=sapply(de_all,length),
CT=sapply(unlist(de_ct,F,T),length))
temp_CTnames <- names(temp_y$CT)
names(temp_x$CT) <- names(temp_y$CT) <- sapply(strsplit(temp_CTnames,".",fixed=T),"[[",1)
par(mar=c(5,4,2,0.5))
boxplot(temp_y,ylim=range(temp_y),outline=T,xaxt="n",yaxt="n",ylab=NA)
text(1.5,line2user(-0.2,3),srt=90,adj=1,cex=1.2,
labels=paste("p =",signif(wilcox.test(unlist(P_all),unlist(P_ct))$p.value,2)))
axis(2,cex.axis=1.2)
mtext("-log10(P # DE overlapping by chance)",side=2,line=2.5)
text(1:2,line2user(0.5,1),xpd=NA,srt=45,adj=1,cex=1.4,font=2,
labels=c("Across all","Cell type"))
mtext(paste("DE: |Log2FC| >=",LFC,"& FDR <=",FDR),
side=3,line=0.2,at=2.5,adj=0,font=2)
par(mar=c(5,0,2,0.5))
temp <- mapply(function(X,Y,N) {
plot(NA,NA,xlim=range(X),ylim=range(temp_y),
xaxt="n",yaxt="n",ylab=NA,xlab=NA)
temp_X <- X; temp_Y <- Y
temp_X[temp_X == 0] <- -abs(jitter(temp_X[temp_X == 0],amount=abs(par("usr")[1]) / 2))
temp_Y[temp_Y == 0] <- -abs(jitter(temp_Y[temp_Y == 0],amount=abs(par("usr")[3]) / 2))
points(temp_X,temp_Y,pch=19,cex=1.5,
col=lig_col[names(temp_Y)])
Z <- Y >= -log10(0.05)
if (!any(Z)) { Z <- Y > 0 }
temp_labels <- scClustViz::spreadLabels2(temp_X[Z],temp_Y[Z],label=names(temp_Y)[Z],
str.cex=1.5,str.font=2)
text(temp_labels,labels=names(temp_Y)[Z],cex=1.2,font=2,
col=alpha(lig_col[names(temp_Y)[Z]],1))
axis(1,cex.axis=1.2)
mtext("# overlapping DE",side=1,line=2.5)
mtext(N,side=1,line=4,font=2)
},X=temp_x,Y=temp_y,N=c("Across cell types","Within cell type"))
rm(list=temp_ds)
# rm(list=grep("^temp",ls(),value=T))
}
}
```
```{r AvC_diffexp_boxplot_v3, fig.height=4,fig.width=9}
charLFC <- "4"; charFDR <- "01"
load(paste0("~/Dropbox/GDB_archive/CMapCorr_files/NN_ALLvCT_DEovlp_",
"LFC",charLFC,"FDR",charFDR,".RData"))
temp_y <- lapply(c(ALL="P_all",CT="P_ct"),function(X) -log10(unlist(get(X))))
temp_x <- list(ALL=sapply(de_all,length),
CT=sapply(unlist(de_ct,F,T),length))
temp_CTnames <- names(temp_y$CT)
names(temp_y$CT) <- sapply(strsplit(temp_CTnames,".",fixed=T),"[[",1)
names(temp_y$CT)[names(temp_y$CT) %in% unique(names(temp_y$CT)[duplicated(names(temp_y$CT))])] <-
temp_CTnames[names(temp_y$CT) %in% unique(names(temp_y$CT)[duplicated(names(temp_y$CT))])]
names(temp_x$CT) <- names(temp_y$CT)
Plist <- Pstat <- list()
for (charLFC in c("2","4")) {
for (charFDR in c("1","01")) {
temp_ds <- load(paste0("~/Dropbox/GDB_archive/CMapCorr_files/NN_ALLvCT_DEovlp_",
"LFC",charLFC,"FDR",charFDR,".RData"))
Plist[[length(Plist) + 1]] <- lapply(c(ALL="P_all",CT="P_ct"),function(X) -log10(unlist(get(X))))
Pstat[[length(Pstat) + 1]] <- wilcox.test(unlist(P_all),unlist(P_ct))$p.value
names(Plist)[length(Plist)] <- paste("|Log2FC| \u2265",
log2(as.integer(charLFC)),
"& FDR \u2264",
as.numeric(paste0("0.",charFDR)))
rm(list=temp_ds)
}
}
par(mfrow=c(1,2),mar=c(3,3,0.5,0.1),mgp=2:0)
plot(NA,NA,xlim=range(unlist(temp_x)),ylim=range(unlist(Plist)),
yaxt="n",ylab="P # DE overlapping by chance",
xlab=paste("# overlapping DE at |Log2FC| \u2265",
log2(as.integer(charLFC)),
"& FDR \u2264",
as.numeric(paste0("0.",charFDR))))
axis(2,at=-log10(c(1,0.1,0.05,1e-2,1e-3,1e-4,1e-5)),
labels=c("100%","10%","5%","1%","0.1%","0.01%","0.001%"))
points(jitter(temp_x$ALL,2),jitter(temp_y$ALL,2),pch=20,col=alpha("red",0.5))
points(jitter(temp_x$CT,2),jitter(temp_y$CT,2),pch=20,col=alpha("dodgerblue",0.5))
text(temp_x$ALL[temp_y$ALL > -log10(0.05)],temp_y$ALL[temp_y$ALL > -log10(0.05)],
names(temp_y$ALL)[temp_y$ALL > -log10(0.05)],col="red",cex=0.6,pos=3,offset=0.2)
temp_lab1 <- data.frame(x=temp_x$CT[temp_y$CT > -log10(0.05)],
y=temp_y$CT[temp_y$CT > -log10(0.05)])
temp_lab2 <- temp_lab1["TGFB1.fibroblast",,drop=F]
temp_lab1 <- temp_lab1[rownames(temp_lab1) != "TGFB1.fibroblast",]
text(temp_lab1,labels=rownames(temp_lab1),
col="dodgerblue",cex=0.6,offset=0.2,
pos=c(4,2)[(rownames(temp_lab1) == "BDNF") + 1])
text(temp_lab2,labels=rownames(temp_lab2),
col="dodgerblue",cex=0.6,offset=0.2,adj=c(-0.01,-0.2))
legend("topright",bty="n",pch=20,col=alpha(c("red","dodgerblue"),0.5),
legend=c("Across cell types","Within cell type"),cex=0.9,pt.cex=1,x.intersp=0.8)
par(mar=c(3,0.1,0.5,3))
boxplot(unlist(Plist,F,F),pch=20,yaxt="n",xaxt="n",ylab=NA,
at=seq(1,12)[-seq(3,by=3,length.out=4)],
col=alpha(c("red","dodgerblue"),0.5),
border=c("red","dodgerblue"))
axis(4,at=-log10(c(1,0.1,0.05,1e-2,1e-3,1e-4,1e-5)),
labels=c("100%","10%","5%","1%","0.1%","0.01%","0.001%"))
mtext("P # DE overlapping by chance",side=4,line=2)
mtext(sub("&","\n&",names(Plist)),side=1,line=1,
at=seq(1.5,by=3,length.out=4),cex=0.9)
mtext(paste("p =",signif(unlist(Pstat),2)),side=1,line=2,
at=seq(1.5,by=3,length.out=4),cex=0.9)
```
```{r cleanup_AvC}
rm(list=ls())
source("~/Dropbox/GDB/line2user.R")
```
****
# Using all data (including time-series)
NicheNet calculation of time-series data seems to result in all positive logFC values, so that makes some things a little screwy. In those cases, time-series data was only compared to other time-series data.
```{r load_data}
load("~/Dropbox/GDB_archive/CMapCorr_files/NN_all_dat.RData")
load("~/Dropbox/GDB_archive/CMapCorr_files/NN_all_corr.RData")
load("~/Dropbox/GDB_archive/CMapCorr_files/NN_all_DEovlp.RData")
load("~/Dropbox/GDB_archive/CMapCorr_files/NN_all_DEmeanlfc_old.RData")
```
## Correlation
```{r corr_boxplot,fig.height=4,fig.width=9}
par(mar=c(5,3,1,1),mgp=2:0)
temp <- unlist(sapply(nn_ligands,
function(X)
list(ALL=corr_all[[X]],
CT=corr_ct[[X]],
DS=corr_ds[[X]]),
simplify=F),recursive=F)
plot(NA,NA,xlim=c(0,length(temp)+1),ylim=range(temp),xaxt="n",xaxs="i",
xlab=NA,ylab="Spearman correlation of logFC")
boxplot(temp,xaxt="n",yaxt="n",add=T)
mtext(names(temp),side=1,line=0.1,at=seq_along(temp),adj=1,las=3,cex=0.8)
abline(v=seq(3.5,by=3,length.out=length(nn_ligands)-1),col="grey50")
rm(list=grep("^temp",ls(),value=T))
```
```{r corr_boxplot_compTS,fig.height=4,fig.width=9}
par(mfrow=c(1,2),mar=c(3,3,1,1),mgp=2:0)
boxplot(list(ALL=unlist(corr_all),CT=unlist(corr_ct),DS=unlist(corr_ds)),
ylab="Spearman correlation of logFC")
corr_all_nts <- corr_all_ts <- list()
for (LIG in names(corr_all)) {
temp_ts <- sapply(strsplit(names(corr_all[[LIG]]),".",fixed=T),
function(X) all(DSinfo[[LIG]][X,"time_series"]))
corr_all_ts[[LIG]] <- corr_all[[LIG]][temp_ts]
corr_all_nts[[LIG]] <- corr_all[[LIG]][!temp_ts]
}
boxplot(list(ALL_nTS=unlist(corr_all_nts),ALL_TS=unlist(corr_all_ts),
CT=unlist(corr_ct),DS=unlist(corr_ds)))
rm(list=c("LIG",grep("^temp",ls(),value=T)))
```
Despite the goofiness of the time-series logFC calculations (all are positive?), there's no discernible difference in correlation of logFC within time-series experiments vs non-time-series.
Note that calculating correlation between time-series and non-TS logFC values is chaos, so those comparisons are skipped in the following figure.
### Final correlation figure
```{r corr_plot,fig.height=4,fig.width=9}
temp_order <- names(sort(sapply(unique(c(names(corr_all),names(corr_ct),names(corr_ds))),
function(X) mean(c(corr_all[[X]],corr_ct[[X]],corr_ds[[X]])))))
lig_col <- qualitative_hcl(length(nn_ligands),palette="dark3",alpha=0.5)
temp_colnames <- c(temp_order,nn_ligands[!nn_ligands %in% temp_order])
names(lig_col) <- temp_colnames[unlist(sapply(seq(1,ceiling(length(nn_ligands)/2)),
function(X) c(X,X + ceiling(length(nn_ligands)/2)),
simplify=F))]
temp_corr <- list(ALL=corr_all[temp_order[temp_order %in% names(corr_all)]],
CT=corr_ct[temp_order[temp_order %in% names(corr_ct)]],
DS=corr_ds[temp_order[temp_order %in% names(corr_ds)]])
temp_lig <- sapply(strsplit(names(unlist(temp_corr,recursive=F)),".",fixed=T),"[[",2)
layout(rbind(1:2),widths=c(4,1))
par(mar=c(5,3,1,0.5),mgp=2:0)
plot(NA,NA,xlim=c(1,length(unlist(temp_corr,recursive=F))),ylim=range(temp_corr),
xaxt="n",xlab=NA,ylab="Spearman correlation of log(fold-change)")
abline(v=cumsum(sapply(temp_corr,length)[1:2]) + 0.5,col="grey70")
abline(h=0,lty=2,col="grey50")
temp_x <- unlist(mapply(rep,x=seq_along(unlist(temp_corr,recursive=F)),
times=sapply(unlist(temp_corr,recursive=F),length)))
points(x=jitter(temp_x,amount=0.2),y=unlist(temp_corr),
pch=19,col=lig_col[temp_lig][temp_x])
mtext(temp_lig,side=1,line=0.1,at=seq_along(temp_lig),las=3,col=alpha(lig_col[temp_lig],1))
segments(x0=c(0,cumsum(sapply(temp_corr,length)[1:2])) + 0.75,
x1=cumsum(sapply(temp_corr,length)) + 0.25,
y0=line2user(4,1),y1=line2user(4,1),xpd=NA,lwd=2)
segments(x0=c(c(0,cumsum(sapply(temp_corr,length)[1:2])) + 0.75,
cumsum(sapply(temp_corr,length)) + 0.25),
x1=c(c(0,cumsum(sapply(temp_corr,length)[1:2])) + 0.75,
cumsum(sapply(temp_corr,length)) + 0.25),
y0=line2user(4,1),y1=line2user(3.8,1),xpd=NA,lwd=2)
mtext(c("Pairwise across cell types / labs","Within same cell type","Same lab"),
at=0.5 + cumsum(sapply(temp_corr,length)) - sapply(temp_corr,length) / 2,
side=1,line=4)
par(mar=c(5,0,1,3))
boxplot(list(ALL=unlist(corr_all),CT=unlist(corr_ct),DS=unlist(corr_ds)),
xaxt="n",xlab=NA,yaxt="n",ylab=NA)
axis(4)
mtext("Spearman correlation of log(fold-change)",side=4,line=2)
mtext(c("Across all","Cell type","Lab"),side=1,line=0.1,at=1:3,las=3)
mtext(paste0("p=",c(signif(wilcox.test(unlist(corr_all),unlist(corr_ct))$p.value,2),
signif(wilcox.test(unlist(corr_ct),unlist(corr_ds))$p.value,2))),
side=1,line=-0.1,at=c(1.5,2.5),las=3,adj=0,cex=0.7)
rm(list=grep("^temp",ls(),value=T))
```
## Differential gene expression
### Overlap of DE genes
Differentially expressed genes were determined by logFC >= 1 and FDR <= 0.1 (as in Browaeys *et al.*, 2020). This figure shows number of DE genes overlapping across all datasets testing the same ligand vs. datasets testing the same ligand in the same cell type vs. datasets from the same accession number (and same ligand / cell type). P-values for overlap (y-axis) were determined after determining probability of overlapping DE genes by chance through bootstrapping.
```{r diffexp_boxplot,fig.height=4,fig.width=9}
par(mar=c(1.5,3,1,1),mgp=2:0)
temp_bxp <- list(ALL=-log10(unlist(P_all)),
CT=-log10(unlist(P_ct)),
DS=-log10(unlist(P_ds)))
boxplot(temp_bxp,ylim=range(temp_bxp),outline=F,
ylab="-log10(P # DE overlapping by chance)",xaxt="n")
mtext(c("Pairwise across cell types / labs","Within same cell type","Same lab"),
side=1,line=0.1,at=1:3)
temp <- c("P_all","P_ct","P_ds")
for (X in temp) {
temp_DE <- -log10(unlist(get(X)))
names(temp_DE) <- unlist(mapply(rep,x=names(get(X)),times=sapply(get(X),length)))
temp_DE <- sort(temp_DE)
temp_x <- seq(which(temp == X) - 0.3,which(temp == X) + 0.3,length.out=length(temp_DE))
points(temp_x,temp_DE,pch=19,col=lig_col[names(temp_DE)])
Z <- temp_DE >= -log10(0.05)
if (!any(Z)) {
Z <- temp_DE > 0
}
if (any(Z)) {
text(temp_x[Z],temp_DE[Z],pos=c(2,4),
labels=names(temp_DE)[Z],font=2,
col=alpha(lig_col[names(temp_DE)[Z]],1))
}
}
mtext(paste("p =",c(signif(wilcox.test(unlist(P_all),unlist(P_ct))$p.value,2),
signif(wilcox.test(unlist(P_ct),unlist(P_ds))$p.value,2))),
side=3,line=-0.9,at=c(1.5,2.5))
rm(list=c("X","Z",grep("^temp",ls(),value=T)))
```
```{r diffexp_boxplot_v2,fig.height=4,fig.width=9}
temp_y <- lapply(c("P_all","P_ct","P_ds"),function(X) {
temp <- -log10(unlist(get(X)))
names(temp) <- sapply(strsplit(names(unlist(get(X))),".",fixed=T),"[[",1)
return(temp)
})
temp_x <- sapply(list(ALL=de_all,CT=de_ct,DS=de_ds),function(DE)
sapply(unlist(DE,recursive=F,use.names=F),length),simplify=F)
par(mfrow=c(1,4),mar=c(5,4,1,0.5),mgp=2:0)
boxplot(temp_y,ylim=range(temp_y),outline=T,xaxt="n",yaxt="n",ylab=NA)
text(c(1.5,2.5),line2user(-0.2,3),srt=90,adj=1,cex=1.2,
labels=paste("p =",c(signif(wilcox.test(unlist(P_all),unlist(P_ct))$p.value,2),
signif(wilcox.test(unlist(P_ct),unlist(P_ds))$p.value,2))))
axis(2,cex.axis=1.2)
mtext("-log10(P # DE overlapping by chance)",side=2,line=2.5)
text(1:3,line2user(0.5,1),xpd=NA,srt=45,adj=1,cex=1.4,font=2,
labels=c("Across all","Cell type","Lab"))
par(mar=c(5,0,1,0.5))
temp <- mapply(function(X,Y,N) {
plot(NA,NA,xlim=range(X),ylim=range(temp_y),
xaxt="n",yaxt="n",ylab=NA,xlab=NA)
temp_X <- X; temp_Y <- Y
temp_X[temp_X == 0] <- -abs(jitter(temp_X[temp_X == 0],amount=abs(par("usr")[1]) / 2))
temp_Y[temp_Y == 0] <- -abs(jitter(temp_Y[temp_Y == 0],amount=abs(par("usr")[3]) / 2))
points(temp_X,temp_Y,pch=19,cex=1.5,
col=lig_col[names(temp_Y)])
Z <- Y >= -log10(0.05)
if (!any(Z)) { Z <- Y > 0 }
temp_labels <- scClustViz::spreadLabels2(temp_X[Z],temp_Y[Z],label=names(temp_Y)[Z],
str.cex=1.5,str.font=2)
text(temp_labels,labels=names(temp_Y)[Z],cex=1.2,font=2,
col=alpha(lig_col[names(temp_Y)[Z]],1))
axis(1,cex.axis=1.2)
mtext("# overlapping DE",side=1,line=2.5)
mtext(N,side=1,line=4,font=2)
},X=temp_x,Y=temp_y,N=c("Across cell types / labs","Within same cell type","Same lab"))
```
### Averaged logFC per ligand
Similar to CMap analyses, took mean LogFC across all samples treated with the same ligand, or same ligand in the same cell line, or from the same accession number (and same ligand / cell line).
```{r mean_LFC_hist,fig.height=6,fig.width=5}
temp_LFC <- list(ALL=unlist(sapply(mean_lfc,"[[","nts_lfc")),
CT=unlist(sapply(unlist(mean_lfc_CT,recursive=F),"[[","lfc")),
DS=unlist(sapply(unlist(mean_lfc_DS,recursive=F),"[[","lfc")))
temp_range <- range(unlist(temp_LFC))
temp_FDR <- list(ALL=unlist(sapply(mean_lfc,"[[","nts_fdr")),
CT=unlist(sapply(unlist(mean_lfc_CT,recursive=F),"[[","fdr")),
DS=unlist(sapply(unlist(mean_lfc_DS,recursive=F),"[[","fdr")))
par(mfrow=c(3,1),mar=c(4,3,1,1),mgp=2:0)
for (X in names(temp_FDR)) {
temp_main <- switch(X,
ALL="Mean LogFC per ligand",
CT="By cell type",
DS="By dataset")
# hist(temp_LFC[[X]],freq=F,xlim=temp_range,xlab="LogFC",main=temp_main)
hist(temp_FDR[[X]],freq=F,xlab="FDR",main=temp_main)
abline(v=mean(temp_FDR[[X]]),lwd=2,col="red")
if (X == "ALL") {
legend("topleft",bty="n",lwd=2,col="red",legend="Mean")
} else {
legend("topleft",bty="n",lwd=c(2,NA,NA),col=c("red",NA,NA),
legend=c("Mean","Wilcoxon rank-sum",
paste("p =",signif(wilcox.test(temp_FDR[[X]],temp_FDR[[Y]])$p.value,2))))
}
# hist(-log10(temp_FDR[[X]]),freq=F,xlim=c(0,temp_max),xlab="-log10(FDR)",main=temp_main)
# abline(v=mean(-log10(temp_FDR[[X]])),lwd=2,col="red")
# legend("topright",bty="n",legend="Mean",lwd=2,col="red")
Y <- X
}
```
### Averaged logFC per ligand compared to CMap
```{r DEdotplot_fx}
DE_dotplot <- function(LIG,GENES,exclude=c()) {
temp_DS <- rownames(DSinfo[[LIG]])
temp_DS <- temp_DS[!temp_DS %in% exclude]
temp_ord_ds <- hclust(dist(t(nn_lig_rep[[LIG]]$lfc[GENES,temp_DS])),"ward.D2")$order
temp_ord_gene <- hclust(dist(nn_lig_rep[[LIG]]$lfc[GENES,temp_DS]),"ward.D2")$order
CTcol <- qualitative_hcl(length(unique(DSinfo[[LIG]]$cell_type)),palette="dark3")
names(CTcol) <- unique(DSinfo[[LIG]]$cell_type)
ogLFC <- nn_lig_rep[[LIG]]$lfc[GENES,temp_DS]
ogLFC <- ogLFC[temp_ord_gene,temp_ord_ds]
temp_dsname <- DSinfo[[LIG]][colnames(ogLFC),"CtAcc"]
temp_dsname[DSinfo[[LIG]][colnames(ogLFC),"time_series"]] <-
paste(temp_dsname[DSinfo[[LIG]][colnames(ogLFC),"time_series"]],"*")
temp_labelcol <- CTcol[DSinfo[[LIG]][colnames(ogLFC),"cell_type"]]
names(temp_labelcol) <- colnames(ogLFC) <- temp_dsname
if (!is.null(mean_lfc[[LIG]])) {
LFC <- cbind(ogLFC,rep(0,nrow(ogLFC)))
temp_labelcol <- append(temp_labelcol,"black")
if (!is.null(mean_lfc[[LIG]]$nts_lfc)) {
LFC <- cbind(LFC,"Mean (no *)"=mean_lfc[[LIG]][GENES[temp_ord_gene],"nts_lfc"])
temp_labelcol <- append(temp_labelcol,"black")
}
if (!is.null(mean_lfc[[LIG]]$ts_lfc)) {
LFC <- cbind(LFC,"Mean (only *)"=mean_lfc[[LIG]][GENES[temp_ord_gene],"ts_lfc"])
temp_labelcol <- append(temp_labelcol,"black")
}
if (!is.null(mean_lfc_CT[[LIG]])){
LFC <- cbind(LFC,sapply(mean_lfc_CT[[LIG]],function(X) X[GENES[temp_ord_gene],"lfc"]))
temp_labelcol <- append(temp_labelcol,CTcol[names(mean_lfc_CT[[LIG]])])
}
if (!is.null(mean_lfc_DS[[LIG]])){
LFC <- cbind(LFC,sapply(mean_lfc_DS[[LIG]],function(X) X[GENES[temp_ord_gene],"lfc"]))
temp_labelcol <- append(temp_labelcol,temp_labelcol[names(mean_lfc_DS[[LIG]])])
}
} else {
LFC <- ogLFC
}
LFC <- t(matrix(
cut(c(-1,1,as.vector(LFC / max(LFC))),
breaks=1000,labels=F)[-1*(1:2)],
nrow(LFC)))
ogFDR <- nn_lig_rep[[LIG]]$qval[GENES,temp_DS]
ogFDR <- ogFDR[temp_ord_gene,temp_ord_ds]
colnames(ogFDR) <- temp_dsname
if (!is.null(mean_lfc[[LIG]])) {
FDR <- cbind(ogFDR,rep(1,nrow(ogFDR)))
if (!is.null(mean_lfc[[LIG]]$nts_lfc)) {
FDR <- cbind(FDR,"Mean (no *)"=mean_lfc[[LIG]][GENES[temp_ord_gene],"nts_fdr"])
}
if (!is.null(mean_lfc[[LIG]]$ts_lfc)) {
FDR <- cbind(FDR,"Mean (only *)"=mean_lfc[[LIG]][GENES[temp_ord_gene],"ts_fdr"])
}
if (!is.null(mean_lfc_CT[[LIG]])){
FDR <- cbind(FDR,sapply(mean_lfc_CT[[LIG]],function(X) X[GENES[temp_ord_gene],"fdr"]))
}
if (!is.null(mean_lfc_DS[[LIG]])){
FDR <- cbind(FDR,sapply(mean_lfc_DS[[LIG]],function(X) X[GENES[temp_ord_gene],"fdr"]))
}
} else {
FDR <- ogFDR
}
FDR <- -log10(FDR)
FDR[FDR > 2] <- 2
FDR <- t(FDR / max(FDR))
FDR[FDR == 0] <- NA
if (!any(DSinfo[[LIG]][temp_DS,"time_series"])) {
rownames(LFC)[rownames(LFC) == "Mean (no *)"] <- "Mean"
rownames(FDR)[rownames(FDR) == "Mean (no *)"] <- "Mean"
}
DE <- t(ogLFC >= 1 & ogFDR <= 0.1)
if (!is.null(mean_lfc[[LIG]])) {
DE <- rbind(DE,rep(F,ncol(DE)))
if (!is.null(mean_lfc[[LIG]]$nts_fdr)) {
DE <- rbind(DE,mean_lfc[[LIG]][GENES[temp_ord_gene],"nts_lfc"] >= 1 &
mean_lfc[[LIG]][GENES[temp_ord_gene],"nts_fdr"] <= 0.1)
}
if (!is.null(mean_lfc[[LIG]]$ts_fdr)) {
DE <- rbind(DE,mean_lfc[[LIG]][GENES[temp_ord_gene],"ts_lfc"] >= 1 &
mean_lfc[[LIG]][GENES[temp_ord_gene],"ts_fdr"] <= 0.1)
}
if (!is.null(mean_lfc_CT[[LIG]])){
DE <- rbind(DE,
t(sapply(mean_lfc_CT[[LIG]],function(X)
X[GENES[temp_ord_gene],"lfc"] >= 1 &
X[GENES[temp_ord_gene],"fdr"] <= 0.1)))
}
if (!is.null(mean_lfc_DS[[LIG]])){
DE <- rbind(DE,
t(sapply(mean_lfc_DS[[LIG]],function(X)
X[GENES[temp_ord_gene],"lfc"] >= 1 &
X[GENES[temp_ord_gene],"fdr"] <= 0.1)))
}
}
par(mar=c(8,7,1,1),mgp=2:0)
plot(x=NULL,y=NULL,xlim=c(0.5,nrow(LFC) + .5),ylim=c(0.5,ncol(LFC) + .5),
xaxs="i",yaxs="i",xaxt="n",yaxt="n",xlab=NA,ylab=NA,bty="n",asp=1)
# abline(v=1:nrow(LFC),col="grey90")
# abline(h=1:ncol(LFC),col="grey90")
temp_FG <- temp_BG <- diverging_hcl(1000,palette="Blue-Red3")[as.vector(LFC)]
temp_FG[as.vector(DE)] <- "mediumseagreen"
symbols(x=rep(1:nrow(FDR),ncol(FDR)),
y=as.vector(sapply(1:ncol(FDR),function(X) rep(X,nrow(FDR)))),
circles=as.vector(FDR)/2,inches=F,add=T,xpd=NA,
fg=temp_FG,bg=temp_BG)
mtext(colnames(FDR),side=2,at=seq_along(colnames(FDR)),las=2,line=0.1,cex=0.8)
text(x=seq_along(rownames(FDR)),y=rep(par("usr")[3] - 0.5,nrow(FDR)),
labels=rownames(FDR),col=temp_labelcol,xpd=NA,adj=1,srt=45,cex=0.8)
if (any(DSinfo[[LIG]][temp_DS,"time_series"])) {
text(x=0,y=par("usr")[3] - 0.5,labels="* Time-series dataset",
xpd=NA,adj=1,srt=45,cex=0.8)
}
temp_yh <- par("usr")[4] - par("usr")[3]
segments(x0=rep(line2user(5,2),1000),x1=rep(line2user(4,2),1000),
y0=seq(0.6 * temp_yh + par("usr")[3],
0.9 * temp_yh + par("usr")[3],
length.out=1000),
y1=seq(0.6 * temp_yh + par("usr")[3],
0.9 * temp_yh + par("usr")[3],
length.out=1000),
xpd=NA,col=diverging_hcl(1000,palette="Blue-Red3"))
rect(xleft=line2user(5,2),xright=line2user(4,2),
ytop=0.9 * temp_yh + par("usr")[3],
ybottom=(((0.9 - 0.75) * (1 / max(ogLFC))) + 0.75) * temp_yh + par("usr")[3],
xpd=NA,border="mediumseagreen")
mtext(c(signif(max(abs(ogLFC)),2),"logFC",-1 * signif(max(abs(ogLFC)),2)),
side=2,line=c(4,5.1,4),adj=c(-0.1,0.5,1.1),
at=c(0.9,0.75,0.6) * temp_yh + par("usr")[3])
temp_q <- rev(c(0.5,0.2,0.1,0.05,0.01))
symbols(x=rep(line2user(4.5,2),length(temp_q)),
y=seq(0.4 * temp_yh + par("usr")[3],
by=-1.5,length.out=length(temp_q)),
circles=-log10(temp_q)/4,inches=F,add=T,xpd=NA,
fg=ifelse(temp_q <= 0.1,"mediumseagreen","black"))
mtext(c(paste0("\u2264",temp_q[1] * 100,"%"),
paste0(temp_q[2:5] * 100,"%")),
side=2,line=5,cex=0.8,
at=seq(0.4 * temp_yh + par("usr")[3],
by=-1.5,length.out=length(temp_q)))
mtext("FDR",side=2,line=5.8,
at=0.4 * temp_yh + par("usr")[3] - 3)
mtext(LIG,side=3,line=-0.5,at=line2user(4.5,2),font=2,cex=1.5)
}
DE_dotplot_forCMap <- function(ogFDR,MAIN) {
FDR <- ogFDR[,colnames(ogFDR) %in% names(mean_lfc)]
FDR <- FDR[apply(FDR,1,function(X) any(X <= 0.1)),,drop=F]
# temp_hROW <- hclust(dist(t(FDR)),method="ward.D2")
temp_hROW <- list(order=order(colnames(FDR),decreasing=T))
temp_hGENE <- hclust(dist(FDR),method="ward.D2")
FDR <- FDR[temp_hGENE$order,temp_hROW$order]
FDR <- -log10(FDR)
FDR[FDR > 2] <- 2
FDR <- FDR / max(FDR)
FDR[FDR == 0] <- NA
ogZS <- sapply(colnames(FDR),function(LIG)
rowMeans(lvl4_data@mat[rownames(FDR),lvl4_data@cdesc$pert_iname == LIG]))
ZS <- matrix(cut(c(-1,1,as.vector(ogZS / max(abs(ogZS)))),
breaks=1000,labels=F)[-(1:2)],nrow=nrow(ogZS))
par(mar=c(5,7,2,1),mgp=2:0)
plot(x=NULL,y=NULL,xlim=c(0.5,nrow(FDR) + .5),ylim=c(0.5,ncol(FDR) + .5),
xaxs="i",yaxs="i",xaxt="n",yaxt="n",xlab=NA,ylab=NA,bty="n",asp=1)
abline(h=1:ncol(FDR),col="grey90")
symbols(x=rep(1:nrow(FDR),ncol(FDR)),
y=as.vector(sapply(1:ncol(FDR),function(X) rep(X,nrow(FDR)))),
circles=as.vector(FDR)/2,inches=F,add=T,xpd=NA,
fg=diverging_hcl(1000,palette="Blue-Red3")[as.vector(ZS)],
bg=diverging_hcl(1000,palette="Blue-Red3")[as.vector(ZS)])
mtext(colnames(FDR),side=2,at=seq_along(colnames(FDR)),las=2,line=0.1,cex=0.8)
mtext("Ligands",side=2,line=2.5,font=2)
mtext(lvl4_data@rdesc[rownames(FDR),"pr_gene_symbol"],
side=1,las=2,at=1:nrow(FDR),line=0.1,cex=0.8)
mtext("Genes differentially expressed",side=1,line=3.5,font=2)
temp_yh <- par("usr")[4] - par("usr")[3]
segments(x0=rep(line2user(5,2),1000),x1=rep(line2user(4,2),1000),
y0=seq(0.6 * temp_yh + par("usr")[3],
0.9 * temp_yh + par("usr")[3],
length.out=1000),
y1=seq(0.6 * temp_yh + par("usr")[3],
0.9 * temp_yh + par("usr")[3],
length.out=1000),
xpd=NA,col=diverging_hcl(1000,palette="Blue-Red3"))
mtext(c(signif(max(abs(ogZS)),2),"Z-score",-1 * signif(max(abs(ogZS)),2)),
side=2,line=c(4,5.1,4),adj=c(-0.1,0.5,1.1),
at=c(0.9,0.75,0.6) * temp_yh + par("usr")[3])
temp_q <- rev(c(0.5,0.2,0.1,0.05,0.01))
symbols(x=rep(line2user(4.5,2),length(temp_q)),
y=seq(0.4 * temp_yh + par("usr")[3],
by=-1.5,length.out=length(temp_q)),
circles=-log10(temp_q)/4,inches=F,add=T,xpd=NA,
fg="black")
mtext(c(paste0("\u2264",temp_q[1] * 100,"%"),
paste0(temp_q[2:5] * 100,"%")),
side=2,line=5,cex=0.8,
at=seq(0.4 * temp_yh + par("usr")[3],
by=-1.5,length.out=length(temp_q)))
mtext("FDR",side=2,line=5.8,
at=0.4 * temp_yh + par("usr")[3] - 3)
mtext(MAIN,side=3,line=0.5,font=2,cex=1.2)
}
```
Connectivity Map data from the same set of ligands assayed in multiple experiments from the NicheNet datasets.
```{r CMap_DE_load,fig.height=5,fig.width=9}
require(cmapR)
temp <- load("~/Dropbox/GDB_archive/CMapCorr_files/lvl4_inputs_allgenes.RData")
lvl4_data <- lvl4_data_all
rm(lvl4_data_all,lig16)
load("~/Dropbox/GDB_archive/CMapCorr_files/lig295_DE_allgenes_lig_FDR.RData")
FDRinf <- FDR_lig
load("~/Dropbox/GDB_archive/CMapCorr_files/lig295_DE_lig_FDR.RData")
FDRass <- FDR_lig
rm(FDR_lig)
par(mfrow=c(1,2))
DE_dotplot_forCMap(FDRass,"CMap (assayed genes only)")
DE_dotplot_forCMap(FDRinf,"CMap (including inferred)")
```
```{r DE_dot}
for (LIG in nn_ligands) {
# print(paste(LIG,which(nn_ligands == LIG),"/",length(nn_ligands)))
temp_genes <- Reduce(union,nn_DE[[LIG]])
if (LIG %in% colnames(FDRinf)) {
temp_cmapgenes <- unique(
lvl4_data@rdesc[c(rownames(FDRass)[FDRass[,LIG] <= 0.1],
rownames(FDRinf)[FDRinf[,LIG] <= 0.1]),"pr_gene_symbol"]
)
temp_cmapgenes <- temp_cmapgenes[temp_cmapgenes %in% temp_genes]
} else {
temp_cmapgenes <- character()
}
# print(paste("cmapgenes =",length(temp_cmapgenes)))
if (!is.null(mean_lfc[[LIG]])) {
temp_meanhits <- rownames(mean_lfc[[LIG]])[
apply(mean_lfc[[LIG]][,colnames(mean_lfc[[LIG]]) %in% c("nts_fdr","ts_fdr"),drop=F],1,
function(X) any(X <= 0.1))
]
# print(paste("mean_lfc =",length(temp_meanhits)))
if (length(temp_meanhits) < 1) {
temp_meanhits <- rownames(mean_lfc[[LIG]])[c(which.min(mean_lfc[[LIG]]$nts_fdr),
which.min(mean_lfc[[LIG]]$ts_fdr))]
}
} else {
temp_meanhits <- character()
}
temp_DEoverlap <- rowSums(sapply(nn_DE[[LIG]],function(X) temp_genes %in% X))
# print(table(temp_DEoverlap))
if (sum(temp_DEoverlap == max(temp_DEoverlap)) > 50) {
temp_commonDE <- names(sort(rowSums(
nn_lig_rep[[LIG]]$qval[temp_genes[temp_DEoverlap == max(temp_DEoverlap)],]
))[1:50])
} else if (length(c(temp_meanhits,temp_cmapgenes)) +
sum(temp_DEoverlap == max(temp_DEoverlap)) < 15) {
temp_commonDE <- c(temp_genes[temp_DEoverlap == max(temp_DEoverlap)],
names(sort(rowSums(nn_lig_rep[[LIG]]$qval[temp_genes,]))[1:10]))
} else {
temp_commonDE <- temp_genes[temp_DEoverlap == max(temp_DEoverlap)]
}
GENES <- unique(c(temp_commonDE,temp_meanhits,temp_cmapgenes))
GENES <- GENES[!is.na(GENES)]
# print(length(GENES))
# print("")
temp_Ylen <- length(GENES)
temp_Xlen <- nrow(DSinfo[[LIG]]) +
as.integer(!is.null(mean_lfc[[LIG]])) +
length(mean_lfc[[LIG]][1,]) / 3 +
length(mean_lfc_CT[[LIG]]) +
length(mean_lfc_DS[[LIG]])
Sys.sleep(0.1)
png(paste0("~/Dropbox/GDB/CMapCorr/docs/output_figs/NN_",LIG,".png"),
width=temp_Xlen * 0.2 + 1.6,height=temp_Ylen * 0.2 + 1.8,
units="in",res=150)
DE_dotplot(LIG,GENES)
dev.off()
rm(list=c("GENES",grep("^temp",ls(),value=T)))
}
```
```{r DE_dot_noTS}
for (LIG in nn_ligands) {
if (sum(!DSinfo[[LIG]]$time_series) < 2) { next }
# print(paste(LIG,which(nn_ligands == LIG),"/",length(nn_ligands)))
temp_genes <- Reduce(union,nn_DE[[LIG]])
if (LIG %in% colnames(FDRinf)) {
temp_cmapgenes <- unique(
lvl4_data@rdesc[c(rownames(FDRass)[FDRass[,LIG] <= 0.1],
rownames(FDRinf)[FDRinf[,LIG] <= 0.1]),"pr_gene_symbol"]
)
temp_cmapgenes <- temp_cmapgenes[temp_cmapgenes %in% temp_genes]
} else {
temp_cmapgenes <- character()
}
# print(paste("cmapgenes =",length(temp_cmapgenes)))
if (!is.null(mean_lfc[[LIG]]$nts_fdr)) {
temp_meanhits <- rownames(mean_lfc[[LIG]])[mean_lfc[[LIG]]$nts_fdr <= 0.1]
# print(paste("mean_lfc =",length(temp_meanhits)))
if (length(temp_meanhits) < 1) {
temp_meanhits <- rownames(mean_lfc[[LIG]])[which.min(mean_lfc[[LIG]]$nts_fdr)]
}
} else {
temp_meanhits <- character()
}
mean_lfc[[LIG]] <- mean_lfc[[LIG]][,!grepl("^ts",colnames(mean_lfc[[LIG]]))]
temp_DEoverlap <- rowSums(sapply(nn_DE[[LIG]],function(X) temp_genes %in% X))
# print(table(temp_DEoverlap))
if (sum(temp_DEoverlap == max(temp_DEoverlap)) > 50) {
temp_commonDE <- names(sort(rowSums(
nn_lig_rep[[LIG]]$qval[temp_genes[temp_DEoverlap == max(temp_DEoverlap)],]
))[1:50])
} else if (length(c(temp_meanhits,temp_cmapgenes)) +
sum(temp_DEoverlap == max(temp_DEoverlap)) < 15) {
temp_commonDE <- c(temp_genes[temp_DEoverlap == max(temp_DEoverlap)],
names(sort(rowSums(nn_lig_rep[[LIG]]$qval[temp_genes,]))[1:10]))
} else {
temp_commonDE <- temp_genes[temp_DEoverlap == max(temp_DEoverlap)]
}
GENES <- unique(c(temp_commonDE,temp_meanhits,temp_cmapgenes))
GENES <- GENES[!is.na(GENES)]
# print(length(GENES))
# print("")
temp_Ylen <- length(GENES)
temp_Xlen <- nrow(DSinfo[[LIG]]) +
as.integer(!is.null(mean_lfc[[LIG]])) +
length(mean_lfc[[LIG]][1,]) / 3 +
length(mean_lfc_CT[[LIG]]) +
length(mean_lfc_DS[[LIG]])
Sys.sleep(0.1)
png(paste0("~/Dropbox/GDB/CMapCorr/docs/output_figs/NN_",LIG,"_noTS.png"),
width=temp_Xlen * 0.2 + 1.6,height=temp_Ylen * 0.2 + 1.8,
units="in",res=150)
DE_dotplot(LIG,GENES,exclude=rownames(DSinfo[[LIG]])[DSinfo[[LIG]]$time_series])
dev.off()
rm(list=c("GENES",grep("^temp",ls(),value=T)))
}
```
Modified heatmaps showing logFC and FDR for selected genes from each sample from the NicheNet gold standard, as well as averages across all samples, all cell types, and all datasets where available. The figure below is an example for TNF.
```{r dotplot_links,echo=F,results="asis"}
temp_files <- list.files("docs/output_figs/",pattern="^NN")
temp_lig <- matrix(sort(nn_ligands),ncol=4,byrow=T)
for (L in 1:nrow(temp_lig)) {
cat("<p>")
for (LIG in temp_lig[L,]) {
if (!any(grepl(LIG,temp_files))) { next }
cat(" | ")
temp_ligfiles <- grep(paste0("_",LIG,"[._]"),temp_files,value=T)
cat(paste0('<a href="output_figs/',temp_ligfiles[!grepl("noTS",temp_ligfiles)],'">',LIG,'</a>'))
if (any(grepl("noTS",temp_ligfiles))) {
cat(paste0('<a href="output_figs/',
temp_ligfiles[grepl("noTS",temp_ligfiles)],
'">(no time-series)</a>'))
}
cat(" | ")
}
cat("</p>")
cat("\n")
}
cat('<p><img src="output_figs/NN_TNF_noTS.png"/></p>')
```
### Comparing NicheNet & CMap expression changes per ligand
Plots per ligand treatment of mean Z-score for each CMap cell line and logFC from NicheNet datasets.
```{r CMAP_NN_links,echo=F,results="asis"}
# figs generated by "CMapNN_outputfigs.R"