-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.R
2554 lines (1952 loc) · 97.5 KB
/
app.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
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
#----Source Dependencies----#
source('dependencies.R')
source('global.R') #Do not need to source if splitting into server.R and ui.R
#----Dashboard Page ----#
ui <- dashboardPage(
skin="purple",
title = "nDSPA",
#----HEADER----#
dashboardHeader(title = span(img(src = "logo.png", height = 50), "nDSPA")
),
#----SIDEBAR----#
dashboardSidebar(
sidebarMenu(id = "dashmenu",
menuItem("QC and Normalization", tabName = "Import", icon = icon("file-import")),
#menuItem("Import and Normalize Data", icon = icon("file-import"),
#menuSubItem("Import Data", tabName = "Import"),
#menuSubItem("Data Plots",tabName = "Data_Plots")),
#menuItem("Expression Map",tabName = "Map",icon = icon("image")),
#menuItem("Run-To-Run Comparisons",tabName = "Run2run",icon = icon("clipboard-check")),
#####_Currently Disabled_#####
menuItem("Statistical Analysis",tabName = "StatAnalysis", icon = icon("chart-bar")),
hr(),
menuItem("About",tabName = "about",icon = icon("id-card"))
)
),
#----BODY----#
dashboardBody(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "body_style.css")
),
useShinyjs(),
tabItems(
#----SINGLE DATA MENU----#
tabItem(tabName = "Import",
fluidRow(
column(width = 12,
bsButton(inputId = "import_btn",
label = "Import Data",
style = "success"
),
bsButton(inputId = "plot_btn",
label = "Data Plots",
style = "default",
disabled = TRUE
), #On data import, update button to disabled=FALSE
bsButton(inputId = "map_btn",
label = "Expression Map",
style = "default",
disabled = TRUE
)#,
# bsButton(inputId = "run2run_btn",
# label = "Run-to-Run Correlation",
# style = "default",
# disabled = TRUE
# )
)
),
#----FluidRow for Import opts----#
fluidRow(
#----DIV for Import Options----#
div(id="Import_opts",
box(title = "nDSP tsv data", width = 12,
#----Inputer with help for Data----#
fileInput(inputId ="inputfile",
label = "Choose the raw data file in the scale of choice",
accept=c(".csv",".xlsx",".txt")
) %>% helper(type = "markdown",
content = "Import"
),
#----Material Switch----#
materialSwitch(inputId = "QCorNot",
label = "QC & Filter",
value = FALSE,
right = TRUE),
materialSwitch(inputId = "SCALENORM_flag",
label = "Scale & Normalize",
value=FALSE,
right = TRUE),
fileInput(inputId = "inputROIclass",
label = "Input Metadata Table",
accept = c(".txt")
)
#htmlOutput("select_roiClass")
),
#Matrix plots with tabs
div(id = "QCbox",
column(width = 12,
tabBox(selected = "QC Filters",
width = 12,
tabPanel(
title = "QC Filter Options",
value = "QC_Filter_box",
fluidRow(
column(width = 4,
uiOutput("Scan_Sel_prefilt1"),
uiOutput("QCopt"),
textOutput("filtered_n")
),
column(width = 7,
dataTableOutput("filtered_samples")
)
)
),
tabPanel(
title = "Filtered Data",
value = "QC_Filtered_Data",
style = "overflow-x: scroll;",
#dataTableOutput(outputId = "anno_filtered")
reactableOutput("anno_filtered")
)
)
)
),
div(id ="SCALENORMbox",
box(
title = "Scale & Normalization Options",
uiOutput("NORMopt"),width = 12
)
)
)#</div Import_opts>
), #</fluidrow Import Opts>
#----Fluid Row Basic Plots----#
fluidRow(
shinyjs::hidden(
#===DIV Basic Plots----#
#--Notes for sizing
#https://stackoverflow.com/questions/53519783/set-minimum-maximum-width-for-box-with-shinydashboard-r
#https://github.com/rstudio/shinydashboard/issues/270
div(id = "Data_plots",
fluidRow(
div(class = "col-sm-12 col-md-12 col-lg-6",
tabBox(width = '100%',height = '100%',
id = "matplots",
selected = "PCA Probes",
tabPanel("PCA Probes",
plotlyOutput(outputId = "pca_ind")
),
tabPanel("PCA Samples",
plotlyOutput(outputId = "pca_var")
),
tabPanel("Density",
#plotOutput(outputId = "density")
plotlyOutput(outputId = "density")
),
tabPanel("Heatmap",
plotlyOutput(outputId = "mainHeat")
),
tabPanel("BG v HK",
plotlyOutput(outputId = "IsovHK_plt")
),
tabPanel("HK Corr",
plotlyOutput(outputId = "HK_Sct_mt")
),
tabPanel("SNR Levels",
plotlyOutput(outputId = "SNR_boxplot")
)
)
)
),
fluidRow(
tabBox(
selected = "Annotations",
width = 12,
tabPanel("Annotations",
style = "overflow-x: scroll;",
dataTableOutput(outputId = "anno")
),
tabPanel("All Values",
style = "overflow-x: scroll;",
dataTableOutput(outputId = "val_all")
),
tabPanel("Data Matrix",
style = "overflow-x: scroll;",
dataTableOutput(outputId = "val_Endo")
),
tabPanel("Probes",
style = "overflow-x: scroll;",
dataTableOutput(outputId = "probes")
)
)
)
) #</DIV data plots>
) #</hidden>
), #</fluidRow for data plots>
fluidRow(
shinyjs::hidden(
div(id="EXPR_map",
fluidRow(
dropdownButton(icon = icon("gear"),
h3("Expression Map Selector"),
fileInput("impImage", "Choose a Scan Image", accept = c('image/png', 'image/jpeg')) %>%
helper(type = "markdown",
content = "Imgmap"
),
htmlOutput("select_ImgScan"),
htmlOutput("select_ImgProbes"),
selectInput(inputId = "Img_Anno_Filt",
label = "Analysis Set",
choices = c("All ROI"="Immap_ROI_All",
"Filtered ROI Only" = "Immap_ROI_Filt"),
selected = "Immap_ROI_All"
)
),
uiOutput("map.ui")
),
#h2("Expression Map Contents")
)#</div for im map>
)#</hidden>
), #</fluidRow for Image Map>
#----Run 2 Run fluidpage----#
fluidRow(
shinyjs::hidden(
div(id = "Run2run",
) #</DIV data plots>
) #</hidden>
)
), #</tabItem Import>
#---Statistical Analysis----#
tabItem(tabName = "StatAnalysis",
fluidRow(
div(id = "Stat_imports",
box(title = "Import Data File", width = 12,
#----Inputer with help for Data----#
fileInput(inputId ="statgroupimport",
label = "Import grouping file",
accept=c(".csv",".xlsx",".txt")
), #%>% helper(type = "markdown",
#content = "Import"
#),
#----Material Switch----#
# materialSwitch(inputId = "Lmem_low_exp_flag",
# label = "Filter Low Expressed Probes?",
# value = FALSE,
# right = TRUE),
selectInput(inputId = "Lmem_model",
label = "Select Model Method",
choices = c("lmer"="lmer"),
selected = "lmer",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
selectInput(inputId = "Lmem_test_method",
label = "Test for Contrasts",
choices = c("t Test"="t.test",
"Z Test"="z.test"),
selected = "t.test",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
# selectInput(inputId = "Lmem_p_filt",
# label = "P.Value filtering",
# choices = c("Raw P Value"="p.value",
# "FDR"="fdr"),
# selected = "p.value",
# multiple = FALSE,
# selectize = TRUE,
# width = NULL,
# size = NULL
# ),
selectInput(inputId = "Lmem_FE",
label = "Select Fixed Effect",
choices = c("Subject ID"="PID",
"group (classification variable)"="group"),
selected = "group",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
selectInput(inputId = "Lmem_RE",
label = "Select Random Effect",
choices = c("Subject ID"="PID",
"group (classification variable)"="group"),
selected = "PID",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
htmlOutput("stat_seg_select"),
htmlOutput("stat_g1_select"),
htmlOutput("stat_g2_select"),
actionButton("stat_go", "Run Stat Calculations")
#htmlOutput("select_roiClass")
),
column(width = 12,
box(style = "overflow-x: scroll;", width = 12,
dataTableOutput(outputId = "stat_df")
)
),
column(width = 12,
box(style = "overflow-x: scroll;", width = 12,
dataTableOutput(outputId = "data_stats")
)
)
)
)
),
#----About----#
tabItem(tabName = "about",
fluidRow(
box(width = 12,
column(12,verbatimTextOutput("cwd") ),
column(12,verbatimTextOutput("fileload") ),
column(12, verbatimTextOutput("session_info")) )
),
fluidRow(includeHTML("docs/about.html"))
)
) #</tabItems Closed>
) #</Dashboard Body>
) #</Dashboard Page>
server <- function(input, output, session) {
#----Observe Helpers sets dir for help .md files and session to observe---#
observe_helpers(session = session)
#Insert Function or package to test types of input. Wrap in condition based on type.
#Add another function to read lines and determine if it is a DSP data file
#----Data Import Reactive Value----#
rawdata <- reactive({
if( is.null(input$inputfile) ) return(NULL)
read_tsv(file=input$inputfile$datapath, trim_ws=TRUE)
})
class_data <- reactive({
req(input$inputROIclass)
df <- read_tsv(file=input$inputROIclass$datapath, trim_ws=TRUE)
if (t.flag) View(df)
return(df)
})
#----Enable buttons if data is loaded----#
observe({
if (!is.null(rawdata() )){
updateButton(session = session,
inputId = "plot_btn",
disabled = FALSE)
updateButton(session = session,
inputId = "map_btn",
disabled = FALSE)
updateButton(session = session,
inputId = "run2run_btn",
disabled = FALSE)
shinyjs::show(id = "QCorNot")
shinyjs::show(id = "SCALENORM_flag")
shinyjs::show(id = "inputROIclass")
}else{
updateButton(session = session,
inputId = "plot_btn",
disabled = TRUE)
updateButton(session = session,
inputId = "map_btn",
disabled = TRUE)
updateButton(session = session,
inputId = "run2run_btn",
disabled = TRUE)
shinyjs::hide(id = "QCorNot")
shinyjs::hide(id = "SCALENORM_flag")
shinyjs::hide(id = "inputROIclass")
}
})
#----Rendering for About Page data
#----==Troubleshooting var for showing rundir of application----#
# output$cwd <- renderText({
# print(paste0(getwd()))
# cat(paste("Current Working Directory: \n",getwd()))
# })
output$cwd <- renderPrint({
cat(paste("Current Working Directory: \n",getwd()))
})
output$session_info <- renderPrint(sessionInfo())
#Add ability on part 2 to modify names of runs
# anno <- rawdata() %>% slice(1:17) %>% t() %>% `colnames<-`(.[1,]) %>% .[-c(1:4),] %>%
# as.data.frame() %>%
# mutate(ID=gsub(" ", "",paste(Run,ROI_ID,`Segment tags`,sep = "|"))) %>% `rownames<-`(.[,"ID"])
#
# probes <- rawdata() %>% .[-c(1:17),c(1:4)] %>% `colnames<-`(gsub("#","",.[1,])) %>% .[-1,] %>% as.data.frame() %>% `rownames<-`(.[,"ProbeName (display name)"])
# values.all <- rawdata() %>% .[-c(1:18),-c(1:4)] %>% `colnames<-`(anno$ID) %>%
# as.data.frame() %>% `rownames<-`(probes$`ProbeName (display name)`)
# values.Endo <- values.all[rownames(values.all) %in% probes$`ProbeName (display name)`[probes$CodeClass == "Endogenous"],]
#
#----Shows path of loaded data in single file run----#
output$fileload <- renderText({
if( is.null(rawdata()) ){
print("No data loaded...")
}else{ print(paste("Input File Path: \n",input$inputfile$datapath))}
})
observeEvent(input$QCorNot, {
print(paste0("Value of QCorNot: ", input$QCorNot))
if (input$QCorNot){
shinyjs::show(id = "QCbox")
}else{
shinyjs::hide(id = "QCbox")
}
})
observeEvent(input$SCALENORM_flag, {
print(paste0("Value of SCALENORM_flag: ", input$SCALENORM_flag))
if (input$SCALENORM_flag){
shinyjs::show(id = "SCALENORMbox")
}else{
shinyjs::hide(id = "SCALENORMbox")
}
})
#----Toggle Buttons Analysis Menu----#
#---==Button Show Import Opts----#
observeEvent(input$import_btn,{
#Update Button State
# observeEvent(btn) -> Toggle True/False on var
# reactive if var true then
# hidedivs/showdivs
# changebuttoncolor
updateButton(session = session,
inputId = "import_btn",
style = "success")
updateButton(session = session,
inputId = "plot_btn",
style = "default")
updateButton(session = session,
inputId = "map_btn",
style = "default")
shinyjs::hide(id = "Data_plots",anim = TRUE)
shinyjs::hide(id = "EXPR_map",anim = TRUE)
shinyjs::hide(id = "Run2run",anim = TRUE)
shinyjs::show(id = "Import_opts",anim = TRUE)
})
observeEvent(input$plot_btn,{
#Update Button State
# observeEvent(btn) -> Toggle True/False on var
# reactive if var true then
# hidedivs/showdivs
# changebuttoncolor
updateButton(session = session,
inputId = "import_btn",
style = "default")
updateButton(session = session,
inputId = "plot_btn",
style = "success")
updateButton(session = session,
inputId = "map_btn",
style = "default")
shinyjs::hide(id = "EXPR_map",anim = TRUE)
shinyjs::hide(id = "Import_opts",anim = TRUE)
shinyjs::hide(id = "Run2run",anim = TRUE)
shinyjs::show(id = "Data_plots",anim = TRUE)
})
observeEvent(input$map_btn,{
#Update Button State
# observeEvent(btn) -> Toggle True/False on var
# reactive if var true then
# hidedivs/showdivs
# changebuttoncolor
updateButton(session = session,
inputId = "import_btn",
style = "default")
updateButton(session = session,
inputId = "plot_btn",
style = "default")
updateButton(session = session,
inputId = "map_btn",
style = "success")
shinyjs::hide(id = "Data_plots",anim = TRUE)
shinyjs::hide(id = "Import_opts",anim = TRUE)
shinyjs::hide(id = "Run2run",anim = TRUE)
shinyjs::show(id = "EXPR_map",anim = TRUE)
})
observeEvent(input$run2run_btn,{
#Update Button State
# observeEvent(btn) -> Toggle True/False on var
# reactive if var true then
# hidedivs/showdivs
# changebuttoncolor
updateButton(session = session,
inputId = "import_btn",
style = "default")
updateButton(session = session,
inputId = "plot_btn",
style = "default")
updateButton(session = session,
inputId = "map_btn",
style = "success")
shinyjs::hide(id = "Data_plots",anim = TRUE)
shinyjs::hide(id = "Import_opts",anim = TRUE)
shinyjs::hide(id = "EXPR_map",anim = TRUE)
shinyjs::show(id = "Run2run",anim = TRUE)
})
# shinyjs::onclick(id = "import_btn",
# #Change Button State to active!!!!
#
# shinyjs::hide(id = "Data_plots",anim = TRUE)
# shinyjs::hide(id = "EXPR_map",anim = TRUE)
# shinyjs::show(id = "Import_opts",
# anim = TRUE)
# #shinyjs::hide()
#
# )
output$Scan_Sel_prefilt1 <- renderUI({
req(rawdata())
Scans <- rawdata() %>%
slice(1:17) %>%
t() %>%
`colnames<-`(.[1,]) %>%
.[-c(1:4),] %>%
as.data.frame() %>%
select(Scan_ID) %>%
unlist() %>%
unique()
str(unique(Scans))
selectInput(inputId = "Scans_to_prefilt",
label = "Select Scans to Process",
choices = Scans,
selected = Scans,
multiple = TRUE
)
})
observeEvent(input$Scans_to_prefilt, {
#cat(paste("Prefilt Scans:",input$Scans_to_prefilt,"\n", sep = " "))
str(input$Scans_to_prefilt)
})
#----QC OPTIONS UI----#
output$QCopt <- renderUI({
req(rawdata())
if (input$QCorNot){
tagList(
h4("Filter Data"),
materialSwitch(
inputId = "Filt_switch",
value = TRUE
) %>% helper(type = "markdown",
content = "QC"
),
div(id="QC_sliders",
br(),
br(),
numericInput(inputId = "FOV_Flag",
label = "Minimum FOV",
value = 75,
min = 0, max = 280, step = 1,
width = NULL),
sliderInput(inputId = "BD_Flag",
label = "Binding Density Range",
value = c(0.1,2.25),
min = 0, max = 3, step = 0.01,
width = NULL),
sliderInput(inputId = "SF_Flag",
label = "Scaling Factor Range",
value = c(0.3,3),
min = 0, max = 5, step = 0.01,
width = NULL),
#Check if nuclei data available before offering option
sliderInput(inputId = "Min_Nuc",
label = "Minimum Nucleus Count in AOI",
value = 200,
min = 0, max = 500, step = 10,
width = NULL),
sliderInput(inputId = "Min_Area",
label = "Minimum Area of AOI",
value = 16000,
min = 0, max = 20000, step = 500,
width = NULL)
)
)
}
})
observe({
if (!is.null(input$Filt_switch)){
if (input$Filt_switch) {
shinyjs::show(id = "QC_sliders")
} else {
shinyjs::hide(id = "QC_sliders")
}
}
})
#----NORMALIZATION OPTIONS UI----#
output$NORMopt <- renderUI({
req(rawdata())
req(probes())
if (input$SCALENORM_flag){
HK <- probes() %>%
filter(`Analyte type` == "RNA" & CodeClass == "Control") %>%
select(`ProbeName (display name)`) %>% unlist() %>% unname()
Iso <- probes() %>%
filter(`Analyte type` == "RNA" & CodeClass == "Negative") %>%
select(`ProbeName (display name)`) %>% unlist() %>% unname()
tagList(
selectInput(inputId = "Scale_method",
label = "Scaling by area or nuclei?",
choices = c("Nuclei"="Nuclei",
"Area"="Area",
"None"="None"),
selected = "None",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
) %>% helper(type = "markdown",
content = "Normalization"
),
#Change to only ask if method is Nuclei or Area
selectInput(inputId = "Scale_calc",
label = "Calculation Method for Scaling",
choices = c("Geometric Mean"="geomean",
"Mean"="mean"),
selected = NULL,
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
br(),
selectInput(inputId = "Norm_SNR_select",
label = "Normalization Method",
choices = c("HouseKeeping Normalization"="HKnorm",
"Signal-Noise-Ratio (SNR)"="SNR",
"No Normalization"="none"),
selected = "none",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
),
br(),
div(id = "HKNORMopts",
selectInput(inputId = "HK_For_Norm",
label = "Select Housekeeping Probes for Normalization",
choices = HK,
selected = HK,
multiple = TRUE
),
selectInput(inputId = "Norm_calc",
label = "Calculation Method for HK Normalization",
choices = c("Geometric Mean"="geomean",
"Mean"="mean",
"No Normalization"="none"),
selected = "none",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
)
),
div(id = "SNRopts",
selectInput(inputId = "Iso_For_Norm",
label = "Select Background Negative Controls for SNR",
choices = Iso,
selected = Iso,
multiple = TRUE
),
selectInput(inputId = "SNR_calc",
label = "Calculation Method for SNR",
choices = c("Geometric Mean"="geomean",
"Mean"="mean",
"No Normalization"="none"),
selected = "none",
multiple = FALSE,
selectize = TRUE,
width = NULL,
size = NULL
)
)
)
}
})
#----Observer to hide Normalization options not being utilized----#
observe({
if (input$SCALENORM_flag){
if (!is.null(input$Norm_SNR_select)) {
if (input$Norm_SNR_select == "none"){
shinyjs::hide(id = "HKNORMopts")
shinyjs::hide(id = "SNRopts")
}else if (input$Norm_SNR_select == "HKnorm"){
shinyjs::show(id = "HKNORMopts")
shinyjs::hide(id = "SNRopts")
}else{
shinyjs::hide(id = "HKNORMopts")
shinyjs::show(id = "SNRopts")
}
}
}
})
# output$FOV_flag <- renderText({
# print(input$FOV_Flag[1])
# })
observeEvent(input$FOV_Flag, {
cat(paste("FOV_Flag: ",input$FOV_Flag,"\n"))
})
#reactive({print(paste0("FOV_Flag: ",input$FOV_Flag))})
#-----BUILDING DATA FOR REST OF APP FROM RAW DATA----#
#Dynamically request Clasifiction Table
#-Change to dynamic UI that asks for table or dynamically make in UI
##Annotation before any filtering
anno_pre <- reactive({
req(rawdata())
anno <- rawdata() %>%
slice(1:17) %>%
t() %>%
`colnames<-`(.[1,]) %>%
.[-c(1:4),] %>%
as.data.frame() %>%
mutate(ID=gsub(" ", "",paste(Scan_ID,ROI_ID,`Segment tags`,sep = "|")) )
anno
})
##Probes before any filtering
probes <- reactive({
req(rawdata())
probes <- rawdata() %>%
.[-c(1:17),c(1:4)] %>%
`colnames<-`(gsub("#","",.[1,])) %>%
.[-1,] %>% as.data.frame() %>%
`rownames<-`(.[,"ProbeName (display name)"])
probes
})
##Only Endogenous probes. Change to use Probes() to filter
probes_Endo <- reactive({
req(rawdata())
probes <- rawdata() %>%
.[-c(1:17),c(1:4)] %>%
`colnames<-`(gsub("#","",.[1,])) %>%
.[-1,] %>% as.data.frame() %>%
`rownames<-`(.[,"ProbeName (display name)"]) %>%
filter(CodeClass == "Endogenous")
probes
})
##RAW VALUE MATRIX.
val_all <- reactive({
anno <- anno_pre()
probes <- probes()
values.all <- rawdata() %>%
.[-c(1:18),-c(1:4)] %>%
`colnames<-`(anno$ID) %>%
as.data.frame() %>%
`rownames<-`(probes$`ProbeName (display name)`) %>%
data.matrix()
values.all
})
##VALUES of all data after QC##
val_all_QC <- reactive({
#Can Add this transformation back into val_all instead and remove redundant data if we don't care about displaying raw values
val_all <- val_all()
anno <- anno_pre()
probes <- probes()
# DSP_QC <- function(anno, val_all_df, scalefactor, thresh_filt=FALSE, PCF_filt=FALSE){
# if (!isFALSE(thresh_filt)){
# anno <- anno %>% filter(ID %in% thresh_filt)
# }
# if (!isFALSE(PCF_filt)){
# anno <- anno %>% filter(ID %in% PCF_filt)
# }
#
# sf <- scalefactor[anno$ID]
# vadf <- val_all_df[,anno$ID]
# if (all.equal(names(scalefactor), colnames(val_all_df))){
# QCdf <- t(t(vadf)*sf)
# }else{
# cat("Scale values not in filtered data: /n")
# print(names(scalefactor[!names(scalefactor) %in% anno$ID]))
#
# QCdf <- t(t(vadf)*sf)
# }
# return(QCdf) ##<< this is the DF not the anno
# }
#
# ERCC_Scale_factor <- function(df_probe, df_val_all){
# ERCC_Probes <- df_probe$`ProbeName (display name)`[df_probe$CodeClass == "Positive" & df_probe$`Analyte type` == "SpikeIn"]
#
# #check before here that input values are data.matrix with numeric data
# PosCtrl_mat <- df_val_all[ERCC_Probes,]
#
#
# #Need trycatch handling
# if (is.null(dim(PosCtrl_mat))){
# if(length(PosCtrl_mat)==0){
# print("There are no ERCC_Probes") #Change to error condition
# }else if (is.numeric(PosCtrl_mat)){
# normfactors <- PosCtrl_mat
# }else{
# mode(PosCtrl_mat) <- "numeric"
# normfactors <- PosCtrl_mat
# }
# }else{
# normfactors <- PosCtrl_mat %>%
# apply(2, as.numeric) %>%
# apply(2, log) %>%
# apply(2, mean) %>%
# exp()
# }
#
# scalefactor <- mean(normfactors)/normfactors
#
# return(scalefactor)
# }
val_all_QC <- DSP_QC(anno = anno, val_all_df = val_all, scalefactor = ERCC_Scale_factor(df_probe = probes, df_val_all = val_all))
val_all_QC
})
##Scale factor: Used for filtering by data out of range of Positive Control Factor in QC
ScaleFactor <- reactive({
req(probes())
req(val_all())
# ERCC_Scale_factor <- function(df_probe, df_val_all){
# ERCC_Probes <- df_probe$`ProbeName (display name)`[df_probe$CodeClass == "Positive" & df_probe$`Analyte type` == "SpikeIn"]