forked from danijak/GeneCloudOmics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABioTrans.R
2706 lines (2506 loc) · 92.5 KB
/
ABioTrans.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
#Sys.setenv("plotly_username"=" your_plotly_username")
#Sys.setenv("plotly_api_key"="your_api_key")
## test repo
print("start loading")
start.load <- Sys.time() ### time
if(length(find.package(package = 'shiny',quiet = T))>0){
library(shiny)
}else{
print("Package shiny not installed")
install.packages("shiny")
print("Package shiny installed")
library(shiny)
}
if(length(find.package(package = 'shinythemes',quiet = T))>0){
library(shinythemes)
}else{
print("Package shinythemes not installed")
install.packages("shinythemes")
print("Package shinythemes installed")
library(shinythemes)
}
if(length(find.package(package = 'rstudioapi',quiet = T))>0){
library(rstudioapi)
}else{
install.packages("rstudioapi")
library(rstudioapi)
}
wd <- dirname(rstudioapi::getActiveDocumentContext()$path) #set wd as the current folder
print(wd == getwd())
print(wd)
print(getwd())
if(! wd == getwd()){
setwd(wd)
}
#
# ## sourcing util files
source(paste0("./www/utils.R"))
# source("ui.R")
#
loadPkg()
species.choices <<- c("Homo sapiens"='org.Hs.eg.db',"Mus musculus"='org.Mm.eg.db',"Rattus norvegicus"='org.Rn.eg.db',"Gallus gallus"='org.Gg.eg.db',"Danio rerio"='org.Dr.eg.db',"Drosophila melanogaster"='org.Dm.eg.db',"Caenorhabditis elegans"='org.Ce.eg.db',"Saccharomyces cereviasiae"='org.Sc.sgd.db',"Arabidopsis thaliana"='org.At.tair.db',"Escherichia coli (strain K12)"='org.EcK12.eg.db',"Escherichia coli (strain Sakai)"='org.EcSakai.eg.db',"Anopheles gambiae"='org.Ag.eg.db',"Bos taurus"='org.Bt.eg.db',"Canis familiaris"='org.Cf.eg.db',"Macaca mulatta"='org.Mmu.eg.db',"Plasmodium falciparum"='org.Pf.plasmo.db',"Pan troglodytes"='org.Pt.eg.db',"Sus scrofa"='org.Ss.eg.db',"Xenopus tropicalis"='org.Xl.eg.db')
DBS <<- list('org.Hs.eg.db'=org.Hs.eg.db,'org.Mm.eg.db'=org.Mm.eg.db,'org.Rn.eg.db'=org.Rn.eg.db,"org.Gg.eg.db"=org.Gg.eg.db,"org.Dr.eg.db"=org.Dr.eg.db,"org.Dm.eg.db"=org.Dm.eg.db,"org.Ce.eg.db"=org.Ce.eg.db,"org.Sc.sgd.db"=org.Sc.sgd.db,"org.At.tair.db"=org.At.tair.db,"org.EcK12.eg.db"=org.EcK12.eg.db,"org.EcSakai.eg.db"=org.EcSakai.eg.db,"org.Ag.eg.db"=org.Ag.eg.db,"org.Bt.eg.db"=org.Bt.eg.db,"org.Cf.eg.db"=org.Cf.eg.db,"org.Mmu.eg.db"=org.Mmu.eg.db,"org.Pf.plasmo.db"=org.Pf.plasmo.db,"org.Pt.eg.db"=org.Pt.eg.db,"org.Ss.eg.db"=org.Ss.eg.db,"org.Xl.eg.db"=org.Xl.eg.db)
enrichRdbs <- as.character(read.csv(paste0(wd,"/www/enrichRdbs.csv"))[,1])
end.load <- Sys.time()
print("loading time")
print(end.load-start.load)
##### UI from here ###########
ui <- navbarPage(id = "navbar",
theme = shinytheme("flatly"),
title = 'GeneCloudOmics',
tabPanel('Home',
# useShinyjs(),
sidebarPanel(
radioButtons('file_type',"Choose File Type",
c('Raw file (read count)'='raw','Normalised file'='norm')),
conditionalPanel(
condition = "input.file_type=='raw'", # raw
p("Example ",a("here", href="https://github.com/buithuytien/GeneCloudOmics/blob/master/Test%20data/Eg_raw.png")), # ADD EXAMPLE
fileInput('file1','Choose Raw Counts'),
# radioButtons('norm_method',"Normalisation method",
# c('RPKM','FPKM','TPM')),
p("Example ",a("here", href = "https://github.com/buithuytien/GeneCloudOmics/blob/master/Test%20data/Eg_gene_length.png")), # ADD EXAMPLE
fileInput('length1','Choose Gene Length'), #gene id + length
p("Example ",a("here", href = "https://github.com/buithuytien/GeneCloudOmics/blob/master/Test%20data/Eg_negative_control_genes.png")), # ADD EXAMPLE
fileInput('spikes1','Choose Negative Control Genes')
# helpText("* Format requirement: CSV file. The first column contains gene names; the read counts of each genotype (conditions: wildtype, mutants, replicates, etc.) are in the following columns.Each genotype column should have a column name. ")
),
conditionalPanel(
condition = "input.file_type=='norm'", # normalized
p("Example ",a("here", href = "https://github.com/buithuytien/GeneCloudOmics/blob/master/Test%20data/Eg_normalised.png")), # ADD EXAMPLE
fileInput('file2','Choose Normalized Expression')
# helpText("* Format requirement: CSV file. Gene names in rows and genotypes in columns, following the usual format of files deposited in the GEO database.")
),
p("Example ",a("here", href="https://github.com/buithuytien/GeneCloudOmics/blob/master/Test%20data/Eg_metadata.png")), # ADD EXAMPLE
fileInput('metafile1','Choose Meta Data File'),
actionButton("submit_input","Submit")
),
mainPanel(
h3('Welcome to GeneCloudOmics --'),
h3('A Biostatistical tool for Transcriptomics Analysis'),
img(src="Abiotrans-logo.png",
width = 570,height = 370)
)
),
tabPanel('Preprocessing',
sidebarPanel(
h4("Filtering"),
splitLayout(
numericInput("min_val","Min. value", min=0.1,step=0.1,value=1.0),
numericInput("min_col","Min. columns", min=1, value=2)
),
conditionalPanel(
condition = "input.file_type=='raw'",
radioButtons('norm_method',"Normalisation method",
c("None (Black)"="None",
'RPKM (Blue)'='RPKM','FPKM (Dark cyan)'='FPKM',
'TPM (Dark green)'='TPM',
"RUV (Brown)"='RUV'))
),
actionButton("submit_preprocessing","Submit"),
conditionalPanel(
condition = "input.preprocessing_tabs == 'Data table' ",
br(),
br(),
downloadButton("download_norm_data", "Download table (csv)")
)
),
mainPanel(
tabsetPanel(type = "tabs",id="preprocessing_tabs",
tabPanel("RLE plot",
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
plotOutput("RLE.plot2")
),
conditionalPanel(
condition = "input.file_type=='raw'",
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
plotOutput("RLE.plot")
)
)
),
tabPanel("Data table",
h3("Normalized data"),
DT::dataTableOutput("norm_table")
),
tabPanel("Description table",
h3("Data description"),
DT::dataTableOutput("meta_table")
)
)
)
),
tabPanel(' Scatter ',
sidebarPanel(
selectInput(inputId = 'scatter.x',label = 'X-axis',choices = ""),
selectInput(inputId = 'scatter.y',label = 'Y-axis',choices = ""),
radioButtons('trans',"Transformation:",
c('None','Natural log','log2','log10')),
downloadButton("downloadscatter", "Download as PDF"),
h6('Download all pairs of samples in one PDF (this may take some time to run) :'),
downloadButton("downloadscatter_collage","Download collage")),
mainPanel(
h3('Heatscatter'),
plotOutput('scatter.plot')
)),
tabPanel('Distribution Fit',
sidebarPanel(
conditionalPanel(
condition= "input.dist_tabs=='Distribution Fit'",
selectInput(inputId = 'dist.var',label = 'Choose a column',choices = colnames('dataset')),
checkboxGroupInput("distributions", "Distributions:",
choices = c("Log-normal","Log-logistic","Pareto","Burr","Weibull","Gamma"),selected = c("Log-normal","Pareto")),
radioButtons('dist_zoom',"Zoom to see fit",c('slider','text input')),
conditionalPanel(
condition = "input.dist_zoom=='slider'",
sliderInput("dist_range", "Range:",
min = 0.1, max = 1000,step=1,
value = c(0.1,1000))
),
conditionalPanel(
condition = "input.dist_zoom=='text input'",
textOutput('dist_range_allowed'),
numericInput('dist_range_min',"min",value=0.1,min=0.1,max=1000),
numericInput('dist_range_max',"max",value=1000,min=0.1,max=1000)
),
downloadButton("downloaddist", "Download as PDF")
),
conditionalPanel(
condition = "input.dist_tabs=='AIC table'",
downloadButton("downloaddistaic", "Download as CSV")
)
),
mainPanel(
tabsetPanel(type = "tabs",id="dist_tabs",
tabPanel("Distribution Fit", plotOutput("dist.plot")),
tabPanel("AIC table",
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
div(tableOutput('dist.aic'), style = "font-size:80%")
))
)
)),
tabPanel(' Correlation ',
sidebarPanel(
radioButtons('cor_method',"Method:",
c('Pearson correlation','Spearman correlation')),
conditionalPanel(
condition = "input.cor_tabs == 'Correlation heatmap'",
downloadButton("downloadcorrplot", "Download as PDF")
),
conditionalPanel(
condition = "input.cor_tabs == 'Correlation plot'",
downloadButton("downloadcorrplot2", "Download as PDF")
),
conditionalPanel(
condition = "input.cor_tabs == 'Correlation matrix'",
downloadButton("downloadcorrmat","Download as CSV")
)
),
mainPanel(
conditionalPanel(
condition = "input.cor_method=='Pearson correlation'",
h3('Pearson correlation')
),
conditionalPanel(
condition = "input.cor_method=='Spearman correlation'",
h3('Spearman correlation')
),
tabsetPanel(type = "tabs",id="cor_tabs",
tabPanel("Correlation heatmap", plotOutput('corr.plot')),
tabPanel("Correlation plot", plotOutput('corr.plot2')),
tabPanel("Correlation matrix", div(tableOutput('corr.matrix'), style = "font-size:80%"))
)
)
),
tabPanel('PCA',
sidebarPanel(
conditionalPanel(
condition = "input.pca_tabs == 'PCA-2D plot'",
selectInput(inputId = 'pca.x',label = 'X-axis',choices = ""),
selectInput(inputId = 'pca.y',label = 'Y-axis',choices = "")
),
selectInput(inputId = 'gene_size',label = 'Gene sample size',choices = ""),
radioButtons('gene_order',"Gene sample order (wrt column 1)",
c('Descending (highest to lowest)'='Descending','Ascending (lowest to highest)'='Ascending','Random')),
conditionalPanel(
condition = "input.pca_tabs == 'PCA-2D plot' || input.pca_tabs == 'PCA-3D plot'",
checkboxInput('pca_cluster',strong('Kmeans clustering on columns'),FALSE),
conditionalPanel(
condition = "input.pca_cluster == true",
sliderInput("pca_cluster_num","Number of clusters:",value=1,min=1,max=1,step=1),
checkboxInput('pca_text',strong('Display sample name'),FALSE)
)
),
conditionalPanel(
condition = "input.gene_order=='Random'",
helpText('* Click multiple times to resample'),
actionButton('pca_refresh',"Resample",style="background-color: #337ab7;border-color:#337ab7"),
br(),br()
),
conditionalPanel(
condition = "input.pca_tabs == 'PCA variance'",
downloadButton("downloadpcavar", "Download as PNG")
),
conditionalPanel(
condition = "input.pca_tabs == 'PCA-2D plot'",
downloadButton("downloadpca2d","Download as PNG")
),
conditionalPanel(
condition = "input.pca_tabs == 'PCA-3D plot'",
downloadButton("downloadpca3d","Download as PNG")
)
),
mainPanel(
tabsetPanel(type = "tabs",id="pca_tabs",
tabPanel("PCA variance", plotlyOutput("pcavar.plot")),
tabPanel("PCA-2D plot", plotlyOutput("pca2d.plot")),
tabPanel("PCA-3D plot",plotlyOutput("pca3d.plot"))
)
)),
tabPanel("DE Analysis",
# useShinyjs(),
sidebarPanel(
radioButtons("n_rep","Replicates?",choices=c("Multiple"=1,"Single"=0)),
conditionalPanel(
condition="input.n_rep=='1'",
radioButtons("de_method1","DE Method",choices=c("EdgeR","DESeq2","NOISeq"))
),
conditionalPanel(
condition="input.n_rep=='0'",
radioButtons("de_method0","DE Method",choices=c("NOISeq"))
),
h5("Choose 2 experiment conditions for DE analysis"),
selectInput("f1","Condition 1",choices = ""),
selectInput("f2","Condition 2",choices = ""),
h5("DE criteria"),
splitLayout(
numericInput("p_val","FDR",min=0.01,max=1,value=0.05,step=0.01),
numericInput("fc","Fold Change",min=1,value=2,step=0.1)
),
fluidRow(
column(4,
actionButton("submit_DE","Submit")
),
column(6,
conditionalPanel(
condition = "input.DE_tabs=='DE genes' ",
downloadButton("download_de_table","Download table (csv)")
),
conditionalPanel(
condition = "input.DE_tabs=='Volcano plot' ",
downloadButton("download_volcano","Download plot (PDF)")
),
conditionalPanel(
condition = "input.DE_tabs=='Dispersion plot' ",
downloadButton("download_dispersion","Download plot (PDF)")
)
# conditionalPanel(
# condition = "input.DE_tabs=='Heatmap plot' ",
# downloadButton("download_heatmap","Download plot")
# )
)
)
),
mainPanel(
tabsetPanel(type = "tabs", id= "DE_tabs",
tabPanel("DE genes",
# h3("Differential Expression Analysis"),
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
DT::dataTableOutput("DE_table")
)
),
tabPanel("Volcano plot", # for DESeq and edgeR
h6("Volcano plot is only available for edgeR and DESeq2 methods"),
conditionalPanel(
condition = "input.n_rep=='1' && input.method1!='NOISeq'",
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
plotOutput("volcano_plot")
)
),
conditionalPanel(
condition = "input.method0=='NOISeq' || input.method1=='NOISeq'",
h6("Volcano Plot is only applicable to DESeq2 and edgeR")
)
),
tabPanel("Dispersion plot", # for edgeR
h6("Dispersion plot is only available for edgeR and DESeq2 methods"),
conditionalPanel(
condition = "input.n_rep=='1' && input.method1!='NOISeq'",
# h3("Dispersion plot"),
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
plotOutput("dispersion_plot")
)
),
conditionalPanel(
condition = "input.method0=='NOISeq' || input.method1=='NOISeq'",
h6("Dispersion Plot is only applicable to DESeq2 and edgeR")
)
)
)
)
),
tabPanel('Heatmap',
sidebarPanel(
conditionalPanel(
condition = "input.heatmap_tabs=='Heatmap'",
radioButtons("heatmap_de_ind",label="Choose data",choices=c("Indenpendent"="ind","DE result"="de")),
numericInput('numOfCluster',"Number of clusters on rows",value=2,min=2,max=30,step=1),
conditionalPanel(
condition = "input.heatmap_de_ind == 'ind' ",
# selectInput('numOfGeno',"Number of genotypes (mutants)",choices=c(1)),
splitLayout(
numericInput('fold',"Fold change",value=2,min=1,step=1),
numericInput("fold_ncol", "min. column",value=2,min=1,step=1)
)
# uiOutput("refGeno"),
# radioButtons('heatmap_value',"Values",
# c('Fold change','Log fold change'))
),
downloadButton("downloadheatmap","Download as PDF"),
actionButton('heatmap_plot',"Plot",width='65px',style="color: #fff; background-color: #337ab7; border-color: #337ab7;float:right")
# conditionalPanel(
# condition = "input.heatmap_de_ind == 'ind' ",
# h5('Specify names of the genotypes'),
# uiOutput("expand_genonames")
# )
),
conditionalPanel(
condition = "input.heatmap_tabs=='Gene clusters'",
uiOutput("heatmap_display"),
conditionalPanel(
condition = "input.display_cluster=='ALL'",
downloadButton("downloadclusters","Download as CSV")
)
)
),
mainPanel(
tabsetPanel(type = "tabs",id="heatmap_tabs",
tabPanel("Heatmap",
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
plotOutput("heatmap.plot")
)),
tabPanel("Gene clusters", dataTableOutput('cluster.info'))
)
)),
######## NOISE ######
#############################################
tabPanel('Noise',
sidebarPanel(
radioButtons('noise_situation',"Select desired noise plot between",choices = c('replicates'='a','genotypes (average of replicates)'='b','genotypes (no replicate)'='c')),
conditionalPanel(
condition = "input.noise_situation=='a' | input.noise_situation=='b' ",
textInput('noise_numOfRep',"Number of replicates",value=1),
helpText("* Please order the sample columns in input file properly. Replicates of the same genotype should be put in adjacent columns.")
),
conditionalPanel(
condition = "input.noise_situation=='b'",
uiOutput("noise_anchor_choices")
),
conditionalPanel(
condition = "input.noise_situation=='c'",
selectInput('noise_anchor_c',"Anchor genotype",choices = "")
),
radioButtons('noise_graph_type',"Graph type:",
c('Bar chart','Line chart')),
downloadButton("downloadnoise","Download as PNG"),
actionButton('noise_plot',"Plot",width='65px',style="color: #fff; background-color: #337ab7; border-color:#337ab7;float:right"),
conditionalPanel(
condition = "input.noise_situation=='a' | input.noise_situation=='b' ",
h5("Specify names of the genotypes"),
uiOutput("expand_genonames_noise")
)
),
mainPanel(
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),h4("Processing ... Please wait"), style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
plotlyOutput('noise.plot')
)
)),
###### ENTROPY #############
#########################################
tabPanel('Entropy',
sidebarPanel(
checkboxInput('tsflag',strong("Time series data"),FALSE),
conditionalPanel(
condition = "input.tsflag==true",
textInput('entropy_timepoints',"Number of time points"),
helpText("* Please order the sample columns in input file properly. Time series data of the same genotype should be put in adjacent columns.")
),
radioButtons('entropy_graph_type',"Graph type:",
c('Bar chart','Line chart')),
downloadButton("downloadentropy","Download as PNG"),
conditionalPanel(
condition = "input.tsflag==true",
h5("Specify names of the genotypes"),
uiOutput("expand_genonames_entropy")
)
),
mainPanel(
h3('Shannon entropy'),
plotlyOutput('entropy.plot')
)
),
################## GO analysis ###################
##################################################
tabPanel("GO Analysis",
# useShinyjs(),
sidebarPanel(
conditionalPanel(
condition = "input.go_tab == 'go_table' || input.go_tab == 'go_pie' ",
helpText("* One-column csv file"),
fileInput("filego","Upload list of DE genes"),
fileInput("filebg","List of background genes"),
selectInput("go_method","Select GO package", choices = c("clusterProfiler","GOstats","enrichR")), #new
conditionalPanel(
condition = "input.go_method=='clusterProfiler' || input.go_method=='GOstats'",
# update if enrichR
selectInput('go_species',"Select species",selected="org.EcK12.eg.db",choices=c("Homo sapiens"='org.Hs.eg.db',"Mus musculus"='org.Mm.eg.db',"Rattus norvegicus"='org.Rn.eg.db',"Gallus gallus"='org.Gg.eg.db',"Danio rerio"='org.Dr.eg.db',"Drosophila melanogaster"='org.Dm.eg.db',"Caenorhabditis elegans"='org.Ce.eg.db',"Saccharomyces cereviasiae"='org.Sc.sgd.db',"Arabidopsis thaliana"='org.At.tair.db',"Escherichia coli (strain K12)"='org.EcK12.eg.db',"Escherichia coli (strain Sakai)"='org.EcSakai.eg.db',"Anopheles gambiae"='org.Ag.eg.db',"Bos taurus"='org.Bt.eg.db',"Canis familiaris"='org.Cf.eg.db',"Macaca mulatta"='org.Mmu.eg.db',"Plasmodium falciparum"='org.Pf.plasmo.db',"Pan troglodytes"='org.Pt.eg.db',"Sus scrofa"='org.Ss.eg.db',"Xenopus tropicalis"='org.Xl.eg.db')), # species.choices
selectInput('go_geneidtype',"Select identifier",choices=NULL),
selectInput('subontology',"Select subontology",selected="BP",choices = c("biological process"="BP","molecular function"="MF","cellular component"="CC")),
numericInput("go_max_p","Adjusted p cutoff",value=0.05,min=0,max=1, step=0.05)
# selectInput('go_level',"Select level",choices = c(2:10))
), conditionalPanel(
condition = "input.go_method == 'enrichR'",
selectInput('enrichR_dbs',"Select database",choices=enrichRdbs)
),
numericInput("go_min_no","Min. number of genes",value=3,min=1,step=1),
br(),
fluidRow(
column(4,
actionButton("submit_go","Submit")
),
conditionalPanel(
condition = "input.go_tab == 'go_table' ",
column(7,
downloadButton("download_go_table","Save CSV")
)
),
conditionalPanel(
condition = "input.go_tab == 'go_pie' ",
column(7,
downloadButton("download_go_pie","Save PNG")
)
)
)
),
conditionalPanel(
condition = "input.go_tab == 'go_graph'",
selectInput("go_term_slect","Select GO terms",choices="",multiple=T),
checkboxInput("show_gene_names","Show gene names", value = F),
actionButton("submit_go_graph","Submit"),
br(),
fluidRow(
column(5,
br(),
radioButtons("download_go_graph_type","File Type",choices=c("png","pdf"))
),
column(6,
br(),
br(),
downloadButton("download_go_graph","Save Graph")
)
)
)
),
mainPanel(
tabsetPanel(type = "tabs", id = "go_tab",
tabPanel("go_table",
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
DT::dataTableOutput("go_table")
)
),
tabPanel("go_pie",
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
h6("Pie chart is only available for clusterProfiler or GOstats method"),
h4("Relative size of GO terms level 2"),
plotlyOutput("go_pie")
)
),
tabPanel("go_graph",
h6("Graph visualization is only available for clusterProfiler method"),
conditionalPanel(condition="$('html').hasClass('shiny-busy')",
div(img(src="load.gif",width=240,height=180),
h4("Processing ... Please wait"),style="text-align: center;")
),
conditionalPanel(condition="!$('html').hasClass('shiny-busy')",
plotOutput("go_graph")
)
)
)
)
)
)
####################################################
server <- function(input,output,session){
########################################
##### get variable names for input #####
########################################
observe({
type <- input$file_type
if(type=='norm'){
DS <- df_norm()
}else if(type=='raw'){
DS <- df_raw()
}
nms <- colnames(DS)
updateSelectInput(session, "scatter.x", choices = nms,selected = nms[1])
updateSelectInput(session, "scatter.y", choices = nms,selected = nms[2])
updateSelectInput(session, "dist.var", choices = nms)
col_num <- ncol(DS)
updateSliderInput(session,"pca_cluster_num",max=col_num-1)
genotype_num <- NULL
if(is.null(DS)==FALSE){
for(i in 2:col_num){
if(col_num %% i == 0)
genotype_num <- c(genotype_num,i)
}
}
updateSelectInput(session,"numOfGeno",choices=genotype_num)
updateSelectInput(session,"noise_anchor_c",choices = nms)
### preprocessing tab
f <- group_names()
f <- unique(as.character(f))
if(is.null(f)){
hideTab(inputId="preprocessing_tabs", target="Description table")
# hideTab(inputId="preprocessing_tabs", target="Description table")
} else {
showTab(inputId="preprocessing_tabs", target="Description table")
updateSelectInput(session,"f1",choices=f,selected =f[1])
updateSelectInput(session,"f2",choices=f,selected =f[2])
}
### gene expression range for distribution fit ###
if(is.null(DS)==FALSE){
DS_dist <- distfit_df()
range_min <- min(DS_dist)
range_max <- max(DS_dist)
updateSliderInput(session,"dist_range",max=round(range_max),value = c(0.1,range_max))
updateNumericInput(session,"dist_range_min",min=0.000001,max=round(range_max),value = 0.1)
updateNumericInput(session,"dist_range_max",min=0.000001,max=round(range_max),value = round(range_max))
}
### gene sample size choices for PCA ###
# print("line 647 check input$submit_preprocessing")
# v=input$submit_preprocessing
if(input$submit_preprocessing > 0){
if(type=='norm'){
DS_filt <- df_shiny()
}else if(type=='raw'){
DS_filt <- df_raw_shiny()
}
} else{
DS_filt <- DS
}
i <- 1
min_size <- 25
samplesize <- NULL
while(i*min_size<length(DS_filt[,1])){
samplesize <- c(samplesize,i*min_size)
i <- i*2
}
if(is.null(samplesize)){
samplesize <- c(samplesize,length(DS_filt[,1]))
}else if(samplesize[length(samplesize)]!=length(DS_filt[,1])){
samplesize <- c(samplesize,length(DS_filt[,1]))
}
updateSelectInput(session,"gene_size", choices = samplesize,selected = samplesize[length(samplesize)])
### pca choices for PCA-2D ###
pcchoices <- NULL
if(is.null(DS)==FALSE)
for (i in 1:ncol(DS)){
pcchoices <- c(pcchoices,paste("PC",i,sep=""))
}
updateSelectInput(session,"pca.x",choices = pcchoices,selected = pcchoices[1])
updateSelectInput(session,"pca.y",choices = pcchoices,selected = pcchoices[2])
### Gene ontology ####
# database update
go_method <- input$go_method
dbs_name <- input$go_species
dbs <- DBS[[dbs_name]]
# slect_id <- input$go_geneidtype
if( ! is.null (dbs)){
id_choices <- AnnotationDbi::keytypes(dbs)
slt <- input$go_geneidtype
if (! slt %in% id_choices ){
slt = id_choices[1]
}
updateSelectInput(session,"go_geneidtype", choices=id_choices, selected = slt)
} else{
id_choices <- NULL
updateSelectInput(session,"go_geneidtype", choices=id_choices)
}
# go terms on GO graph
go_method <- input$go_method
res <- go_res_filt()
if (go_method == "clusterProfiler" & !is.null(res) ){
go_terms <- as.character(res$Description)
} else {
go_terms <- NULL
}
updateSelectInput(session,"go_term_slect",choices=go_terms)
})
observeEvent(input$submit_input, {
type <- input$file_type
if(type=='norm'){
DS <- df_norm()
lengths <- 0
}else if(type=='raw'){
DS <- df_raw()
lengths <- gene_length()
# if( length(intersect(rownames(lengths), rownames(DS))) < 1000 )
# length <- NULL
}
f <- group_names()
spikes <- neg_control()
# if any NULL value, throw error. TO CHANGE TO BE MORE SPECIFIC
input.list <- list(DS, f)
input.null <- sapply(input.list, is.null)
names(input.null) <- c("Expression/Counts","Meta Data")
if( any(input.null) ){
index.null <- which(input.null)
errors <- paste(names(input.null)[index.null],collapse = ', ')
# print(errors)
showModal(modalDialog(
type = "Error",
paste("Please check these input:",errors,"and try again!")
))
} else{
updateNavbarPage(session, inputId="navbar",selected="Preprocessing")
}
# update input
updateNumericInput(session,"min_col",max=ncol(DS)) # update max column nunmber in filtering
if(is.null(spikes)){
updateRadioButtons(session,"norm_method",choices = c("None (Black)"="None",
'RPKM (Blue)'='RPKM','FPKM (Dark cyan)'='FPKM',
'TPM (Dark green)'='TPM',
"Upper Quartile (Brown)"='RUV') )
#c("None",'RPKM','FPKM','TPM',"Upper Quartile"="RUV")
} else {
updateRadioButtons(session,"norm_method",choices = c("None (Black)"="None",
'RPKM (Blue)'='RPKM','FPKM (Dark cyan)'='FPKM',
'TPM (Dark green)'='TPM',
"RUV (Brown)"='RUV'))
}
if(is.null(lengths) & !(is.null(spikes)) ){
updateRadioButtons(session,"norm_method",choices = c("None (Black)"="None","RUV (Brown)"="RUV"))
} else if(is.null(lengths) & (is.null(spikes)) ){
updateRadioButtons(session,"norm_method",choices = c("None (Black)"="None","Upper Quartile (Brown)"="RUV"))
}
if(is.null(f)){
hideTab(inputId="navbar",target="DE Analysis")
} else{
showTab(inputId="navbar",target="DE Analysis")
}
# if(is.null(f)){
# hideTab(inputId="preprocessing_tabs", target="Description table")
# } else {
# showTab(inputId="preprocessing_tabs", target="Description table")
# }
})
# observeEvent(input$submit_preprocessing, {
# type <- input$file_type
# if(type=='norm'){
# DS <- df_shiny()
# }else if(type=='raw'){
# DS <- df_raw_shiny()
# }
# ### gene sample size choices for PCA ###
# i <- 1
# min_size <- 25
# samplesize <- NULL
# while(i*min_size<length(DS[,1])){
# samplesize <- c(samplesize,i*min_size)
# i <- i*2
# }
# if(is.null(samplesize)){
# samplesize <- c(samplesize,length(DS[,1]))
# }else if(samplesize[length(samplesize)]!=length(DS[,1])){
# samplesize <- c(samplesize,length(DS[,1]))
# }
# updateSelectInput(session,"gene_size", choices = samplesize,selected = samplesize[length(samplesize)])
# })
######################################
######### read in / get data #########
######################################
#####################
## get data #########
#####################
# get normalized counts
df_norm <- reactive({ # get normalized counts
if (is.null(input$file2))
return (NULL)
parts <- strsplit(input$file2$datapath,".",fixed=TRUE)
type <- parts[[1]][length(parts[[1]])]
if(type!="csv"){
showModal(modalDialog(
title = "Error",
"Please input a csv file!"
))
return (NULL)
}
ds <- read.csv(input$file2$datapath)
ds <- na.omit(ds)
ds <- ds[!duplicated(ds[,1]),] # remove duplicated gene names
row_names <- ds[,1]
DS <- data.frame(ds)
if(ncol(DS)<=1){
showModal(modalDialog(
title = "Error",
"Please check normalised data file format (Eg_normalised.png) and try again!"
))
return(NULL)
}
DS <- DS[,-1]
row.names(DS) <- row_names
for (i in 1:ncol(DS)){
if(class(DS[,i])!="numeric" & class(DS[,i])!="integer"){
showModal(modalDialog(
title = "Error",
"Please check normalised data file format (Eg_normalised.png) and try again!"
))
return(NULL)
}
}
return(DS)
})
# get raw counts
df_raw <- reactive ({
if(is.null(input$file1))
return(NULL)
parts <- strsplit(input$file1$datapath,".",fixed=TRUE)
type <- parts[[1]][length(parts[[1]])]
if(type!="csv"){
showModal(modalDialog(
title = "Error",
"Please input a csv file!"
))
return (NULL)
}
raw_ds <- read.csv(input$file1$datapath)
raw_ds <- na.omit(raw_ds)
raw_ds <- raw_ds[!duplicated(raw_ds[,1]),] # remove duplicated gene names
# raw_ds <- as.data.frame(raw_ds)
if(ncol(raw_ds)<=1){
showModal(modalDialog(
title = "Error",
"Data file must contain at least 2 columns. Please check raw data format and try again!"
))
return(NULL)
}
row_names <- raw_ds[,1]
rownames(raw_ds) <- row_names
raw_DS <- raw_ds[,-1] # remove the first column, which is gene Id
for (i in 1:ncol(raw_DS)){
if(class(raw_DS[,i])!="numeric" & class(raw_DS[,i])!="integer"){
showModal(modalDialog(
title = "Error",
"Raw counts must be integer. Please check raw data formate and try again!"
))
return(NULL)
}
}
return(raw_DS)
})
# get gene length
gene_length <- reactive({
if (is.null(input$length1))
return (NULL)
lengths_df <- read.csv(input$length1$datapath)
lengths_df2 <- data.frame("len" = lengths_df[,2]);
rownames(lengths_df2) <- as.character(lengths_df[,1])
return(lengths_df2)
})
# get spikes / negative control genes
neg_control <- reactive({
if(is.null(input$spikes1))
return(NULL)
spikes <- read.csv(input$spikes1$datapath,header=F)
spikes <- as.character(spikes[,1])
# print(spikes[1:10])
return(spikes)
})
# get meta data table
group_names <- reactive({
# if no data
if(is.null(input$metafile1))
return(NULL)
# read in group names (metadata)
groups <- read.csv(input$metafile1$datapath)
group_colnames <- as.character(groups[,1])
type <- input$file_type
if(type=='norm'){
DS <- df_norm()
}else if(type=='raw'){
DS <- df_raw()
}
col_names <- colnames(DS) # columm names of DS in order
# check if groups and column names are similar
if ( !all(col_names %in% group_colnames) || ncol(groups) < 2 ){
showNotification(type = "error", "group names and DS column names not similar")
return(NULL)
}
if(ncol(groups)==2){
f <- groups[match(col_names,groups[,1]),] [,2] # arrange f in the same order as col_names
} else {
f <- groups[match(col_names,groups[,1]),] [,2]
for(i in 3:ncol(groups)){
f <- paste0(f,"_",groups[,i])
}
}
f <- as.factor(make.names(f))
# return(as.factor(f))
return(f)
})
### Gene ontology
gene_list <- reactive({
if(is.null(input$filego))
return (NULL)
parts <- strsplit(input$filego$datapath,".",fixed=TRUE)
type <- parts[[1]][length(parts[[1]])]
if(type!="csv"){
showModal(modalDialog(
title = "Error",
"Please input a csv file!"
))
return (NULL)
}
ds <- read.csv(input$filego$datapath,header=FALSE)
if(ncol(ds) >= 2){
col1 <- ds[-1,1]
} else if( ncol(ds) == 1){
col1 <- ds[,1]
} else {
showModal(modalDialog(
title = "Error",
"No data found! Please check required data format and try again!"
))
return (NULL)
}
gene_list <- as.character(col1)
print("gene list from gene_list")
print(head(gene_list))
return(gene_list)
})
bg_list <- reactive({
if(is.null(input$filebg))
return (NULL)
parts <- strsplit(input$filebg$datapath,".",fixed=TRUE)
type <- parts[[1]][length(parts[[1]])]
if(type!="csv"){
showModal(modalDialog(
title = "Error",
"Please input a csv file!"
))
return (NULL)
}
ds <- read.csv(input$filebg$datapath,header=FALSE)
if(ncol(ds) > 1){
col1 <- ds[-1,1]
} else if( ncol(ds) == 1){
col1 <- ds[,1]
} else {
showModal(modalDialog(
title = "Error",
"No data found! Please check required data format and try again!"
))
return (NULL)
}