-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
1516 lines (1301 loc) · 53 KB
/
server.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
source("main.R")
# shinyServer -------------------------------------------------------------
server <- function(input, output) {
# Load Data and Modify Input Tables -------------------------------------------------------------------
#################### pre-run t-SNE Groups Analysis 1 ####################
#### Load Data
load_clinical_data<- reactive({ #Loading Clinical Data (without phenotype variable type)
load_clinical_data=read.csv(clinical_data_file,sep="\t")})
load_clinical_data1<- reactive({ #Loading Clinical Data (with phenotype variable type)
load_clinical_data=read.csv(clinical_data_file1,sep="\t")})
load_tsne_table1<-reactive ({ # Load TSNE Table 1
req(input$pathway1)
clinicalD=load_clinical_data()
cancer_name=paste(strsplit(input$cancer," ")[[1]],collapse="_")
data1=read.csv(paste(c(tsne_table_dir,cancer_name,paste(input$pathway1,".txt",sep="")),collapse = "/"))
load_tsne_table1=merge_clinical(data1,clinicalD)
})
load_tsne_table2<-reactive ({ # Load TSNE Table 2
req(input$pathway2)
clinicalD=load_clinical_data()
cancer_name=paste(strsplit(input$cancer," ")[[1]],collapse="_")
data1=read.csv(paste(c(tsne_table_dir,cancer_name,paste(input$pathway2,".txt",sep="")),collapse = "/"))
load_tsne_table2=merge_clinical(data1,clinicalD)
})
#merges tsnetables
load_tsne_tables<-reactive ({ # Merge
req(input$clusters)
req(input$clusters2)
clinicalD=load_clinical_data()
table1=load_tsne_table1()
table2=load_tsne_table2()
index=filter_patients()
data1=table1[index,]
data2=table2[index,]
Pathway_Names=c(input$pathway1,input$pathway2)
Cluster=input$clusters
Cluster2=input$clusters2
if ("all" %in% Cluster){Clusters1=sort(unique(data1$Cluster))
} else{Clusters1=as.integer(Cluster)}
load_tsne_tables=sequential_filter(data1,data2,Clusters1,reverse = FALSE,Pathway_Names,Cluster2)
})
display_data<- reactive ({ #Not Used but can show Merged Data used in Analysis
req(input$pathway2)
data=load_tsne_tables()[[1]]
n=ncol(data)
display_data=data[,-c(n-1,n)]
})
load_mapping<-reactive({ #Mapping Data for heirarchical clustering link
load_mapping=read.csv(cancer_mapping_file,sep="\t",header=TRUE)})
load_dendrogram_groups<-reactive ({
req(input$cancer)
mapping=load_mapping()
cancer=input$cancer
cancer_name=paste(strsplit(input$cancer," ")[[1]],collapse="_")
cancer_index=which(mapping$Full_Name==cancer_name)
symbol=as.vector(mapping[cancer_index,"Symbol"])
dendg=readRDS(paste(c(dendro_group_dir,"/",symbol,"_dendro_groups.RDS"),collapse=""))
dendro_groups=cbind(names(dendg),unname(dendg))
colnames(dendro_groups)=c("Sample","Group")
load_dendogram_groups=dendro_groups
})
load_tsne_tables_dendro <-reactive ({ #merge t-sne data for pathway 1 with dendrogram grouping for survival curves
req(input$clusters_heatmap)
req(input$group2)
data1=load_tsne_table1()
dendro_groups=load_dendrogram_groups()
Cluster=input$clusters_heatmap
Group2=input$group2
if ("all" %in% Cluster){Clusters1=sort(unique(data1$Cluster))
} else{Clusters1=as.integer(Cluster)}
load_tsne_tables_dendro=sequential_filter_dendro(data1,dendro_groups,Cluster,Group2)
})
#### Count tables from input data
counttable<- reactive({ #Generated table of counts under survivial plots in Tab 2
req(input$pathway2)
req(input$clusters)
data=load_tsne_tables()[[1]]
counttable1=count_table(data)
rownames(counttable1)=sapply(rownames(counttable1),function(x){paste("Pathway 1: Cluster",as.character(x))})
colnames(counttable1)=sapply(colnames(counttable1),function(x){paste("Pathway 2: Cluster",as.character(x))})
counttable=counttable1
})
#### Count Table for Dendro Groupos
counttable_dendro<- reactive({ #Generated table of counts under survivial plots in Tab 2
req(input$group2)
req(input$clusters_heatmap)
table=load_tsne_tables_dendro()[[1]]
index=filter_patients()
data=table[index,]
counttable_dendro1=count_table_dendro(data)
rownames(counttable_dendro1)=sapply(rownames(counttable_dendro1),function(x){paste("Pathway 1: Cluster",as.character(x))})
colnames(counttable_dendro1)=sapply(colnames(counttable_dendro1),function(x){paste("Pathway 2: Cluster",as.character(x))})
counttable_dendro = counttable_dendro1
})
#### Count Table for Pathway 1
count_table_scatter1<-reactive({
req(input$cancer)
req(input$pathway1)
data <- load_tsne_table1()
index=filter_patients()
validate(need(index>0,"No patients with selected filter options"))
if(!is.null(data) && !is.null(index) ){
data=data[index,]
table=table(data$Cluster)
m=matrix(0,1,length(table))
m[1,]=unname(table)
rownames(m)=c("Number of Patients")
colnames(m)=sapply(c(1:length(table)),function(x){return(paste("Cluster",x,sep=" "))})
count_table_scatter1=m
} else {count_table_scatter1=NULL}
})
#### Count Table for Pathway 2
count_table_scatter2<-reactive({
req(input$cancer)
req(input$pathway1)
req(input$pathway2)
data <- load_tsne_table2()
index=filter_patients()
validate(need(index>0,"No patients with selected filter options"))
if(!is.null(data) && !is.null(index)){
data=data[index,]
table=table(data$Cluster)
m=matrix(0,1,length(table))
m[1,]=unname(table)
rownames(m)=c("Number of Patients")
colnames(m)=sapply(c(1:length(table)),function(x){return(paste("Cluster",x,sep=" "))})
count_table_scatter2=m
} else {count_table_scatter2=NULL}
})
#Load Color palette for plots
load_pal <-reactive ({
load_pal=readRDS(pal_file)
load_pal=unname(load_pal)
})
#################### tSNE/UMAP Analysis 2 ####################
load_dendrogram_groups_custom <-reactive ({
req(input$custom_cancer)
mapping=load_mapping()
cancer=input$custom_cancer
cancer_name=paste(strsplit(input$custom_cancer," ")[[1]],collapse="_")
cancer_index=which(mapping$Full_Name==cancer_name)
symbol=as.vector(mapping[cancer_index,"Symbol"])
dendg=readRDS(paste(c(dendro_group_dir,"/",symbol,"_dendro_groups.RDS"),collapse=""))
dendro_groups=cbind(names(dendg),unname(dendg))
colnames(dendro_groups)=c("Sample","Group")
load_dendogram_groups=dendro_groups
})
#Gene/Pathway List
load_gene_pathways <- reactive({
load_gene_pathways=read.csv(paste(data_dir,"data2/merged/merged_pathway_lists.txt",sep=""),sep="\t")
})
load_phenotype_map_custom <- reactive({
phenotype_var_type=read.csv(paste0(data_dir,"data2/merged/phenotype_mapping.txt"),sep="\t")
clinical_data_types=as.vector(phenotype_var_type$var_type)
names(clinical_data_types)=as.vector(phenotype_var_type$Phenotype)
return(clinical_data_types)
})
#Pathway 1
#Step 1
load_custom_analysis_cancer_data <- reactive({
req(input$custom_cancer)
cancer_map=read.csv(paste0(data_dir,"data2/cancer_name_map.txt"),sep="\t")
disease_filt=as.vector(cancer_map[which(cancer_map$Full == h10(input$custom_cancer)),"Abrv"])
#if(input$use_tpm==TRUE){use_tpm=TRUE} else {use_tpm=FALSE}
if(disease_filt=="LAML"){sample_type_filt="Primary Blood Derived Cancer - Peripheral Blood"} else {sample_type_filt="Primary Tumor"}
print("LOADING DATA")
dc_in=load_metadata(data_dir)
dc_in=filter_metadata(dc_in,disease_filt,sample_type_filt)
return(dc_in)
})
#Step 2
filter_custom_p1 <- eventReactive(input$run_custom,{
req(input$custom_pathway1)
req(input$use_tpm)
dc_in=load_custom_analysis_cancer_data()
data=dc_in$metadata_filtered
#input$filter_control=TRUE
phenotypes=c(input$custom_phenotypes)
if(is.null(data)){
filter_index=NULL
} else {
print(input$filter_control_custom)
if(input$filter_control_custom!="No Filter" && !is.null(phenotypes) && !is.null(input$custom_threshold1) && !is.null(input$custom_compare1)){
num_phenotypes=length(phenotypes)
if(num_phenotypes==2){
req(input$threshold2)
req(input$compare2)
}
thresholds=list()
comparisons=list()
for (p in c(1:num_phenotypes)){
if(p==1){
thresholds[[1]]=input$custom_threshold1
comparisons[[1]]=input$custom_compare1
}
if(p==2){
print(input$threshold2)
thresholds[[2]]=input$custom_threshold2
comparisons[[2]]=input$custom_compare2
}
}
thresholds=modify_filter_options(phenotypes,thresholds)
clinical_data_types=load_phenotype_map_custom ()
filter_index=choose_pheno_3D(data,phenotypes,thresholds,comparisons,clinical_data_types)
} else {
print("A")
filter_index=c(1:nrow(data))
}
print(length(filter_index))
return(filter_index)
}
})
#Step 3
custom_analysis_pathway1 <- eventReactive(input$run_custom,{
req(input$custom_pathway1)
req(input$use_tpm)
index=filter_custom_p1()
#print(length(index))
if(input$use_tpm==TRUE){use_tpm=TRUE} else {use_tpm=FALSE}
use_pathway=input$custom_pathway1
dc_in=load_custom_analysis_cancer_data()
if(use_pathway=="Custom Gene Set"){
#req(input$multigene_p1)
custom_control=TRUE
use_pathway=input$multigene_p1
use_pathway=use_pathway
} else { custom_control=FALSE}
dc_in$filtered_metadata_old=dc_in$metadata_filtered
dc_in$metadata_filtered=dc_in$metadata_filtered[index,]
dc_in=load_filter_rename_gen_data2(dc_in,data_dir,use_tpm,use_pathway,custom_control)
dc_in=merge_gen_meta(dc_in)
dc_in=normalize_data(dc_in)
print("Running UMAP")
custom.config <- umap.defaults
#custom.config$n_epochs <- 100 #how much should we run umap for
#custom.config$min_dist <- 10^(-66) #minimum distance for umap
#custom.config$n_neighbors <- 30 #number of neighbors for umap
custom.config$n_components <- 3 #shouldn't be changed by user: how many dimensions we are doing umap into
if(input$use_UMAP=="UMAP"){
use_umap=TRUE
#print(input$n_neighbors)
custom.config$n_epochs=as.numeric(input$n_epochs)
custom.config$min_dist=10^(as.numeric(input$min_dist))
custom.config$n_neighbors=as.numeric(input$n_neighbors)
tsne_perplexity=NULL
tsne_max_iter=NULL
} else {
use_umap=FALSE
tsne_perplexity=as.numeric(input$perplexity)
tsne_max_iter=as.numeric(input$max_iter)
}
dc_out=dim_reduce(dc_in,use_umap,custom.config,tsne_perplexity,tsne_max_iter)
print("TEST")
if (length(dc_out) == 1){
if (dc_out=="FAILED") {return("FAILED")
} else {
dc_in=cluster(dc_in)
print("DONE")
}
}
#temp=dc_in$joined_dr
dc_in=cluster(dc_out)
print("DONE")
#temp=dc_in$joined_dr
print(colnames(dc_in$joined_dr))
custom_analysis_pathway2=dc_in$joined_dr
})
custom_analysis_pathway2 <- eventReactive(input$run_custom,{
req(input$custom_pathway2)
req(input$use_tpm)
index=filter_custom_p1()
print(length(index))
if(input$use_tpm==TRUE){use_tpm=TRUE} else {use_tpm=FALSE}
use_pathway=input$custom_pathway2
dc_in=load_custom_analysis_cancer_data()
if(use_pathway=="Custom Gene Set"){
#req(input$multigene_p1)
custom_control=TRUE
use_pathway=input$multigene_p2
use_pathway=use_pathway
} else {custom_control=FALSE}
dc_in$filtered_metadata_old=dc_in$metadata_filtered
dc_in$metadata_filtered=dc_in$metadata_filtered[index,]
dc_in=load_filter_rename_gen_data2(dc_in,data_dir,use_tpm,use_pathway,custom_control)
dc_in=merge_gen_meta(dc_in)
dc_in=normalize_data(dc_in)
print("Running UMAP")
custom.config <- umap.defaults
#custom.config$n_epochs <- 100 #how much should we run umap for
#custom.config$min_dist <- 10^(-66) #minimum distance for umap
#custom.config$n_neighbors <- 30 #number of neighbors for umap
custom.config$n_components <- 3 #shouldn't be changed by user: how many dimensions we are doing umap into
if(input$use_UMAP=="UMAP"){
use_umap=TRUE
print(input$n_neighbors)
custom.config$n_epochs=as.numeric(input$n_epochs)
custom.config$min_dist=10^(as.numeric(input$min_dist))
custom.config$n_neighbors=as.numeric(input$n_neighbors)
tsne_perplexity=NULL
tsne_max_iter=NULL
} else {
use_umap=FALSE
tsne_perplexity=as.numeric(input$perplexity)
tsne_max_iter=as.numeric(input$max_iter)
}
dc_out=dim_reduce(dc_in,use_umap,custom.config,tsne_perplexity,tsne_max_iter)
print("TEST")
if (length(dc_out) == 1){
if (dc_out=="FAILED") {return("FAILED")
} else {
dc_in=cluster(dc_in)
print("DONE")
}
}
#temp=dc_in$joined_dr
dc_in=cluster(dc_out)
print("DONE")
#temp=dc_in$joined_dr
print(colnames(dc_in$joined_dr))
custom_analysis_pathway2=dc_in$joined_dr
})
#Num Patients Tables for Scatter Plots
count_table_custom_scatter1<-reactive({
req(input$custom_cancer)
req(input$custom_pathway1)
req(input$use_tpm)
data=custom_analysis_pathway1()
validate(
need(data!="FAILED", "")
)
#data1=data[,c(1,c(ncol(data)-3:ncol(data)))]
data1=data[,c("sample","X1","X2","X3","Cluster")]
colnames(data1)=c("Sample","X","Y","Z","Cluster")
table=table(data1$Cluster)
m=matrix(0,1,length(table))
m[1,]=unname(table)
rownames(m)=c("Number of Patients")
colnames(m)=sapply(c(1:length(table)),function(x){return(paste("Cluster",x,sep=" "))})
count_table_custom_scatter1=m
})
count_table_custom_scatter2<-reactive({
req(input$custom_cancer)
req(input$custom_pathway2)
req(input$use_tpm)
data=custom_analysis_pathway2()
validate(
need(data!="FAILED", "")
)
#data1=data[,c(1,c(ncol(data)-3:ncol(data)))]
data1=data[,c("sample","X1","X2","X3","Cluster")]
colnames(data1)=c("Sample","X","Y","Z","Cluster")
table=table(data1$Cluster)
m=matrix(0,1,length(table))
m[1,]=unname(table)
rownames(m)=c("Number of Patients")
colnames(m)=sapply(c(1:length(table)),function(x){return(paste("Cluster",x,sep=" "))})
count_table_custom_scatter1=m
})
custom_warning<- eventReactive(input$run_custom,{
custom_warning="Please wait, analysis may take a few seconds to load "
})
# Analysis ----------------------------------------------------------------
#################### pre-run t-SNE Groups Analysis 1 ####################
######## Individual Pathway Survival Curves
fit_P1<- reactive ({
data=load_tsne_table1()
index=filter_patients()
table=data[index,]
fit_P1=my_sfit_P1(table)
})
fit_P2<- reactive ({
data=load_tsne_table2()
index=filter_patients()
table=data[index,]
fit_P2=my_sfit_P1(table)
})
######## Sequential Analysis Survivial Fit
fit<- reactive ({
table=load_tsne_tables()[[1]]
Clusters=input$clusters
# if ("all" %in% Cluster){Clusters1="all"
# } else{Clusters1=as.integer(Cluster)}
fit=my_sfit(table)
})
######## Dendrogram Analysis Survivial Fit
fit_dendro<-reactive({
data=load_tsne_tables_dendro()[[1]]
index=filter_patients()
table=data[index,]
fit_dendro=my_sfit(table)
})
######## Choose patients by phenotype
filter_patients<-reactive({
req(input$cancer)
req(input$pathway1)
data=load_tsne_table1()
if(is.null(data)){
filter_index=NULL
} else {
if(input$filter_control==TRUE && !is.null(input$phenotype) && !is.null(input$threshold1) && !is.null(input$compare1)){
phenotypes=c(input$phenotype)
num_phenotypes=length(phenotypes)
if(num_phenotypes==2){
req(input$threshold2)
req(input$compare2)
}
thresholds=list()
comparisons=list()
for (p in c(1:num_phenotypes)){
if(p==1){
thresholds[[1]]=input$threshold1
comparisons[[1]]=input$compare1
}
if(p==2){
print(input$threshold2)
thresholds[[2]]=input$threshold2
comparisons[[2]]=input$compare2
}
}
thresholds=modify_filter_options(phenotypes,thresholds)
clinicalD=load_clinical_data1()
clinical_data_types=as.vector(clinicalD[1,])
filter_index=choose_pheno_3D(data,phenotypes,thresholds,comparisons,clinical_data_types)
} else {
filter_index=c(1:nrow(data))
}
print(length(filter_index))
return(filter_index)
}
})
#################### tSNE/UMAP Analysis 2 ####################
fit_P1_custom <- reactive ({
req(input$custom_cancer)
req(input$custom_pathway1)
req(input$use_tpm)
table=custom_analysis_pathway1()
validate(
need(table!="FAILED", "")
)
fit_P1_custom=my_sfit_P1(table)
})
fit_P2_custom <- reactive ({
req(input$custom_cancer)
req(input$custom_pathway2)
req(input$use_tpm)
table=custom_analysis_pathway2()
validate(
need(table!="FAILED", "")
)
fit_P1_custom=my_sfit_P1(table)
})
merge_custom_pathways<- reactive({
req(input$custom_cancer)
req(input$custom_pathway1)
req(input$custom_pathway2)
req(input$use_tpm)
data1=custom_analysis_pathway1()
data2=custom_analysis_pathway2()
Pathway_Names=c(input$custom_pathway1,input$custom_pathway2)
Cluster=input$clusters_custom
Cluster2=input$clusters2_custom
if ("all" %in% Cluster){Clusters1=sort(unique(data1$Cluster))
} else{Clusters1=as.integer(Cluster)}
merge_data=sequential_filter(data1,data2,Clusters1,reverse = FALSE,Pathway_Names,Cluster2)
return(merge_data)
})
fit_custom <- reactive({
merge_data=merge_custom_pathways()
table=merge_data[[1]]
fit=my_sfit(table)
})
filter_patients_custom_p1<-reactive({
req(input$cancer)
req(input$custom_pathway1)
req(input$use_tpm)
data=custom_analysis_pathway1()
#TEST INPUT
input=list()
input$filter_control=TRUE
input$phenotype="OS"
input$threshold1=0
input$compare1="="
if(is.null(data)){
filter_index=NULL
} else {
if(input$filter_control==TRUE && !is.null(input$phenotype) && !is.null(input$threshold1) && !is.null(input$compare1)){
phenotypes=c(input$phenotype)
num_phenotypes=length(phenotypes)
if(num_phenotypes==2){
req(input$threshold2)
req(input$compare2)
}
thresholds=list()
comparisons=list()
for (p in c(1:num_phenotypes)){
if(p==1){
thresholds[[1]]=input$threshold1
comparisons[[1]]=input$compare1
}
if(p==2){
print(input$threshold2)
thresholds[[2]]=input$threshold2
comparisons[[2]]=input$compare2
}
}
thresholds=modify_filter_options(phenotypes,thresholds)
phenotype_var_type=read.csv(paste0(data_dir,"data2/merged/phenotype_mapping.txt"),sep="\t")
clinical_data_types=as.vector(phenotype_var_type$var_type)
names(clinical_data_types)=as.vector(phenotype_var_type$Phenotype)
filter_index=choose_pheno_3D(data,phenotypes,thresholds,comparisons,clinical_data_types)
} else {
filter_index=c(1:nrow(data))
}
print(length(filter_index))
return(filter_index)
}
})
counttable_custom<- reactive({ #Generated table of counts under survivial plots in Tab 2
req(input$custom_pathway2)
req(input$clusters_custom)
req(input$clusters2_custom)
data=merge_custom_pathways()[[1]]
counttable1=count_table(data)
rownames(counttable1)=sapply(rownames(counttable1),function(x){paste("Pathway 1: Cluster",as.character(x))})
colnames(counttable1)=sapply(colnames(counttable1),function(x){paste("Pathway 2: Cluster",as.character(x))})
counttable_custom=counttable1
})
load_tsne_tables_dendro_custom <-reactive ({ #merge t-sne data for pathway 1 with dendrogram grouping for survival curves
req(input$custom_clusters_heatmap)
req(input$custom_group2)
data1=custom_analysis_pathway1()
dendro_groups=load_dendrogram_groups_custom()
Cluster=input$custom_clusters_heatmap
Group2=input$custom_group2
if ("all" %in% Cluster){Clusters1=sort(unique(data1$Cluster))
} else{Clusters1=as.integer(Cluster)}
load_tsne_tables_dendro=sequential_filter_dendro(data1,dendro_groups,Cluster,Group2)
})
fit_custom_dendro<-reactive({
req(input$custom_pathway1)
table=load_tsne_tables_dendro_custom()[[1]]
fit_dendro=my_sfit(table)
})
custom_counttable_dendro<- reactive({ #Generated table of counts under survivial plots in Tab 2
req(input$custom_group2)
req(input$custom_clusters_heatmap)
data=load_tsne_tables_dendro_custom()[[1]]
counttable_dendro1=count_table_dendro(data)
rownames(counttable_dendro1)=sapply(rownames(counttable_dendro1),function(x){paste("Pathway 1: Cluster",as.character(x))})
colnames(counttable_dendro1)=sapply(colnames(counttable_dendro1),function(x){paste("Pathway 2: Cluster",as.character(x))})
counttable_dendro = counttable_dendro1
})
# Plots -------------------------------------------------------------------
#################### pre-run t-SNE Groups Analysis ####################
######## Survival Plots
survivalplot_P1_gen<-reactive({
validate(
need(input$cancer != "", "")
)
validate(
need(input$pathway1 != "", "")
)
req(input$cancer)
req(input$pathway1)
f=fit_P1()
pal=load_pal()
survivalplot_P1_gen1=my_splot_P1_create1(f[[1]],f[[2]],input$pathway1,pal)
splots=list(survivalplot_P1_gen1)
survivalplot_P1_gen=my_splot_generate(splots,input$cancer,pdf=FALSE,filename='test_P1.pdf')
})
survivalplot_dendo_groups_gen<-reactive({
validate(
need(input$cancer != "", "Please select a Cancer")
)
validate(
need(input$cancer != "Acute Myeloid Leukemia Marrow", "Acute Myeloid Leukemia marrow does not have heiarchical clustering data for analysis")
)
validate(
need(input$pathway1 != "", "Please select 1st Pathway")
)
validate(
need(input$clusters_heatmap != "", "Please select clusters in 1st pathway to analyze")
)
validate(
need(input$group2!= "", "Please select groups from dendrogram in heirarchical clustering to analyze")
)
req(input$cancer)
req(input$pathway1)
req(input$clusters_heatmap)
req(input$group2)
data=load_tsne_tables_dendro()[[1]]
index=filter_patients()
table=data[index,]
f=fit_dendro()
Cluster=input$clusters_heatmap
if ("all" %in% Cluster){Clusters1="all"
} else{Clusters1=as.integer(Cluster)}
Group2=input$group2
if ("all" %in% Group2){Group2a="all"
} else{Group2a=as.integer(Group2)}
pal=load_pal()
survivalplot_gen1=my_splot_create1(f[[1]],f[[2]],table[[2]],Clusters1,pal)
splots=list(survivalplot_gen1)
survivalplot_gen=my_splot_generate(splots,input$cancer,FALSE,filename=NULL)
})
survivalplot_P2_gen<-reactive({
req(input$cancer)
req(input$pathway1)
req(input$pathway2)
f=fit_P2()
pal=load_pal()
survivalplot_P2_gen1=my_splot_P1_create1(f[[1]],f[[2]],input$pathway2,pal)
splots=list(survivalplot_P2_gen1)
survivalplot_P2_gen=my_splot_generate(splots,input$cancer,pdf=FALSE,filename='test_P2.pdf')
})
survivalplot_gen<-reactive({
validate(
need(input$cancer != "", "Please select a Cancer")
)
validate(
need(input$pathway1 != "", "Please select 1st Pathway")
)
validate(
need(input$pathway2 != "", "Please select 2nd Pathway")
)
validate(
need(input$clusters != "", "Please select clusters in 1st pathway to analyze")
)
validate(
need(input$clusters2 != "", "Please select clusters in 2nd pathway to analyze")
)
req(input$cancer)
req(input$pathway1)
req(input$pathway2)
req(input$clusters)
table=load_tsne_tables()
f=fit()
Cluster=input$clusters
if ("all" %in% Cluster){Clusters1="all"
} else{Clusters1=as.integer(Cluster)}
Cluster2=input$clusters2
if ("all" %in% Cluster2){Clusters2="all"
} else{Clusters2=as.integer(Cluster2)}
pal=load_pal()
survivalplot_gen1=my_splot_create1(f[[1]],f[[2]],table[[2]],Clusters1,pal)
splots=list(survivalplot_gen1)
survivalplot_gen=my_splot_generate(splots,input$cancer,FALSE,filename=NULL)
})
######## 3D Scatter Plots
scatterplot_gen2_p1<-reactive({
req(input$cancer)
req(input$pathway1)
data <- load_tsne_table1()
index=filter_patients()
table=data[index,]
pal=load_pal()
colors=pal[1:length(unique(table$Cluster))]
table$Cluster=as.character(table$Cluster)
scatterplot_gen2<-plot_ly(table,x=~X,y=~Y,z=~Z,color=~Cluster,type='scatter3d',mode="markers",opacity=.3,colors=colors,marker=list(size=7))
})
scatterplot_gen2_p2<-reactive({
req(input$cancer)
req(input$pathway1)
data <- load_tsne_table2()
index=filter_patients()
table=data[index,]
pal=load_pal()
colors=pal[1:length(unique(table$Cluster))]
table$Cluster=as.character(table$Cluster)
scatterplot_gen2<-plot_ly(table,x=~X,y=~Y,z=~Z,type='scatter3d',mode="markers",color=~Cluster,opacity=.3,colors=colors,marker=list(size=7))
})
######## Heatmaps
heatmap_gen<-eventReactive(input$create_heatmap,{
validate(
need(input$cancer != "", "Please select a Cancer")
)
validate(
need(input$cancer != "Acute Myeloid Leukemia Marrow", "Acute Myeloid Leukemia marrow does not have heiarchical clustering data for analysis")
)
validate(
need(input$pathway1 != "", "Please select 1st Pathway")
)
req(input$cancer)
req(input$pathway1)
mapping=load_mapping()
cancer_name=paste(strsplit(input$cancer," ")[[1]],collapse="_")
cancer_index=which(mapping$Full_Name==cancer_name)
cancer=as.vector(mapping[cancer_index,"Symbol"])
pathway=input$pathway1
base_dir=data_dir
hm <- readRDS(file.path(base_dir,"heatmap_base",paste0(cancer,"_heatmap.RDS")))
base_ano <- readRDS(file.path(base_dir,"dendro_annotations",paste0(cancer,"_annotation.RDS")))
tsne_ano <- readRDS(file.path(base_dir,"tsne_annotations",cancer,paste0(pathway,"#tsneAnnotation.RDS")))
heatmap_gen <- draw(tsne_ano%v% base_ano %v% hm)
})
#################### tSNE/UMAP Analysis 2 ####################
custom_scatterplot_gen2_p1<-eventReactive(input$run_custom,{
req(input$custom_cancer)
req(input$custom_pathway1)
req(input$use_tpm)
data=custom_analysis_pathway1()
validate(
need(data!="FAILED", "Phenotypes choosen not sufficent for analysisT")
)
data1=data[,c("sample","X1","X2","X3","Cluster")]
colnames(data1)=c("Sample","X","Y","Z","Cluster")
table=data1
pal=load_pal()
colors=pal[1:length(unique(table$Cluster))]
#table$Cluster=as.character(table$Cluster)
#print(table$Cluster)
plot_ly(table,x=~X,y=~Y,z=~Z,color=~Cluster,type='scatter3d',mode="markers",opacity=.3,colors=colors,marker=list(size=7))
})
custom_scatterplot_gen2_p2<-eventReactive(input$run_custom,{
req(input$custom_cancer)
req(input$custom_pathway2)
req(input$use_tpm)
data=custom_analysis_pathway2()
validate(
need(data!="FAILED", "Phenotypes choosen not sufficent for analysis")
)
data1=data[,c("sample","X1","X2","X3","Cluster")]
colnames(data1)=c("Sample","X","Y","Z","Cluster")
table=data1
pal=load_pal()
colors=pal[1:length(unique(table$Cluster))]
#table$Cluster=as.character(table$Cluster)
#print(table$Cluster)
plot_ly(table,x=~X,y=~Y,z=~Z,color=~Cluster,type='scatter3d',mode="markers",opacity=.3,colors=colors,marker=list(size=7))
})
custom_survivalplot_P1_gen<-eventReactive(input$run_custom,{
req(input$custom_cancer)
req(input$custom_pathway1)
req(input$use_tpm)
f=fit_P1_custom()
pal=load_pal()
survivalplot_P1_gen1=my_splot_P1_create1(f[[1]],f[[2]],input$pathway1,pal)
splots=list(survivalplot_P1_gen1)
survivalplot_P1_gen=my_splot_generate(splots,input$cancer,pdf=FALSE,filename='test_P1.pdf')
})
custom_survivalplot_P2_gen<-eventReactive(input$run_custom,{
req(input$custom_cancer)
req(input$custom_pathway2)
req(input$use_tpm)
f=fit_P2_custom()
pal=load_pal()
survivalplot_P2_gen1=my_splot_P1_create1(f[[1]],f[[2]],input$pathway1,pal)
splots=list(survivalplot_P2_gen1)
survivalplot_P2_gen=my_splot_generate(splots,input$cancer,pdf=FALSE,filename='test_P1.pdf')
})
custom_survivalplot_gen<-reactive({
req(input$custom_cancer)
req(input$custom_pathway1)
validate(
need(input$custom_pathway2 != "", "Please select 2nd Pathway for sequential analysis")
)
req(input$custom_pathway2)
validate(
need(input$clusters_custom != "", "Please select clusters in 1st pathway to analyze")
)
validate(
need(input$clusters2_custom != "", "Please select clusters in 2nd pathway to analyze")
)
req(input$clusters_custom)
req(input$clusters2_custom)
table=merge_custom_pathways()
f=fit_custom()
Cluster=input$clusters_custom
if ("all" %in% Cluster){Clusters1="all"
} else{Clusters1=as.integer(Cluster)}
Cluster2=input$clusters2_custom
if ("all" %in% Cluster2){Clusters2="all"
} else{Clusters2=as.integer(Cluster2)}
pal=load_pal()
survivalplot_gen1=my_splot_create1(f[[1]],f[[2]],table[[2]],Clusters1,pal)
splots=list(survivalplot_gen1)
survivalplot_gen=my_splot_generate(splots,input$cancer,FALSE,filename=NULL)
})
custom_survivalplot_dendo_groups_gen<-reactive({
validate(
need(input$custom_cancer != "", "Please select a Cancer")
)
validate(
need(input$custom_cancer != "Acute Myeloid Leukemia Marrow", "Acute Myeloid Leukemia marrow does not have heiarchical clustering data for analysis")
)
validate(
need(input$custom_pathway1 != "", "Please select 1st Pathway")
)
validate(
need(input$custom_clusters_heatmap != "", "Please select clusters in 1st pathway to analyze")
)
validate(
need(input$custom_group2!= "", "Please select groups from dendrogram in heirarchical clustering to analyze")
)
req(input$custom_cancer)
req(input$custom_pathway1)
req(input$custom_clusters_heatmap)
req(input$custom_group2)
print("TEST2")
table=load_tsne_tables_dendro_custom()[[1]]
f=fit_custom_dendro()
Cluster=input$custom_clusters_heatmap
if ("all" %in% Cluster){Clusters1="all"
} else{Clusters1=as.integer(Cluster)}
Group2=input$group2
if ("all" %in% Group2){Group2a="all"
} else{Group2a=as.integer(Group2)}
pal=load_pal()
survivalplot_gen1=my_splot_create1(f[[1]],f[[2]],table[[2]],Clusters1,pal)
splots=list(survivalplot_gen1)
survivalplot_gen=my_splot_generate(splots,input$cancer,FALSE,filename=NULL)
})
custom_heatmap_gen<-eventReactive(input$custom_create_heatmap,{
validate(
need(input$custom_cancer != "", "Please select a Cancer")
)
validate(
need(input$custom_cancer != "Acute Myeloid Leukemia Marrow", "Acute Myeloid Leukemia marrow does not have heiarchical clustering data for analysis")
)
validate(
need(input$custom_pathway1 != "", "Please select 1st Pathway")
)
req(input$custom_cancer)
req(input$custom_pathway1)
mapping=load_mapping()
cancer_name=paste(strsplit(input$custom_cancer," ")[[1]],collapse="_")
cancer_index=which(mapping$Full_Name==cancer_name)
cancer=as.vector(mapping[cancer_index,"Symbol"])
pathway=input$custom_pathway1
base_dir=data_dir
hm <- readRDS(file.path(base_dir,"heatmap_base",paste0(cancer,"_heatmap.RDS")))
base_ano <- readRDS(file.path(base_dir,"dendro_annotations",paste0(cancer,"_annotation.RDS")))
#tsne_ano <- readRDS(file.path(base_dir,"tsne_annotations",cancer,paste0(pathway,"#tsneAnnotation.RDS")))
custom_heatmap_gen <- draw(base_ano %v% hm)
})
# User Input Buttons ------------------------------------------------------
#################### pre-run t-SNE Groups Analysis 1 ####################
#### Step 0: Select Cancer and Gene Sets (Sidebar)
output$cancerSelector <- renderUI({
Cancers=list.dirs(tsne_table_dir,full.names = FALSE,recursive = TRUE) #Get list of cancers from tsne_table_dir
Cancers=unname(sapply(Cancers,function(x){return(paste(strsplit(x,"_")[[1]],collapse=" "))}))
Cancers=c("",Cancers)
selectizeInput("cancer",label="Choose a Cancer",selected=Cancers[1],multiple=FALSE,choices=Cancers) #Cancers[1] vs NA
})
output$pathway1Selector <- renderUI({
req(input$cancer)
cancer_name=paste(strsplit(input$cancer," ")[[1]],collapse="_")
Pathways=c("",as.vector(sapply(list.files(paste(tsne_table_dir,cancer_name,sep="/"),full.names = FALSE),h1)))
selectizeInput("pathway1",label="Choose 1st Pathway",selected=Pathways[1],multiple=FALSE,choices=Pathways) #NA Pathways[1]
})
output$pathway2Selector <- renderUI({
req(input$pathway1)
cancer_name=paste(strsplit(input$cancer," ")[[1]],collapse="_")
Pathways2=c("",as.vector(sapply(list.files(paste(tsne_table_dir,cancer_name,sep="/"),full.names = FALSE),h1)))
pathways2a=Pathways2[which(Pathways2!=input$pathway1)]
selectizeInput("pathway2",label="Choose 2nd Pathway",selected=pathways2a[1],multiple=FALSE,choices=pathways2a) #NA pathways2a[1]
})
#### Step 2a: Select Clusters in Gene Sets (Tab 3)
output$clusterSelector <- renderUI({
req(input$pathway2)
tsne_table1=load_tsne_table1()
Clusters=sort(unique(tsne_table1$Cluster))
Clusters1=c("all",Clusters)
selectizeInput("clusters",label="Choose Clusters in Pathway 1",multiple=TRUE,selected=NA,choices=Clusters1)
})
output$clusterSelector2 <- renderUI({
req(input$clusters)
tsne_table1=load_tsne_table2()
Clusters=sort(unique(tsne_table1$Cluster))
Clusters1=c("all",Clusters)
selectizeInput("clusters2",label="Choose Clusters in Pathway 2",multiple=TRUE,selected="all",choices=Clusters1)
})
#### Step 2b: Select Clusters in Dendrogram Groups (Tab 4)
output$clusterSelector_heatmap <- renderUI({
req(input$pathway1)
tsne_table1=load_tsne_table1()