-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHelperChecks.R
1073 lines (979 loc) · 57.3 KB
/
HelperChecks.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
## ---------------------------
##
## Script name: HelperFunctions
##
## Purpose of script: General helper functions to check function input and save results
##
## Author: Christina Schmidt
##
## Date Created: 2023-06-14
##
## Copyright (c) Christina Schmidt
## Email:
##
## ---------------------------
##
## Notes:
##
##
## ---------------------------
################################################################################################
### ### ### Helper function: Internal Function to check function input ### ### ###
################################################################################################
#' Check input general parameters
#'
#' @param InputData Passed to MetaProViz functions. Usually DF which contains unique sample identifiers as row names and metabolite numerical values in columns with metabolite identifiers as column names. But can also be differential expression results or other InputData.
#' @param InputData_Num \emph{Optional: } If InputData must be numeric \strong{Default = TRUE}
#' @param SettingsFile_Sample \emph{Optional: } DF which contains information about the samples, which will be combined with the input data based on the unique sample identifiers used as rownames. If not avaliable can be set to NULL. \strong{Default = NULL}
#' @param SettingsFile_Metab \emph{Optional: } DF which contains information about the features. If not avaliable can be set to NULL. \strong{Default = NULL}
#' @param SettingsInfo \emph{Optional: } Passed to MetaProViz functions. Usually named vector containing the information about the names of the experimental parameters in SettingsFile_Sample, SettingsFile_Metab or InputData. \strong{Default = NULL}
#' @param SaveAs_Plot \emph{Optional: } Select the file type of output plots. Options are svg, png, pdf. If set to NULL, plots are not saved.\strong{Default = NULL}
#' @param SaveAs_Table \emph{Optional: } Select the file type of output table. Options are "csv", "xlsx", "txt". If set to NULL, plots are not saved. \strong{Default = NULL}
#' @param CoRe \emph{Optional: } If TRUE, a consumption-release experiment has been performed. If not avaliable can be set to NULL. \strong{Default = FALSE}
#' @param PrintPlot \emph{Optional: } If TRUE prints an overview of resulting plots. If not avaliable can be set to NULL. \strong{Default = FALSE}
#' @param Theme \emph{Optional: } Can be set for VizX functions. If not avaliable can be set to NULL. \strong{Default = NULL}
#' @param PlotSettings \emph{Optional: } Needs to be set for VizX functions. Options are "Sample", "Feature", Both". This refers to SettingsInfo color, shape, individual as for some plots we have both feature and sample settings. \strong{Default = NULL}
#'
#' @return returns warnings and errors if input is not correct
#'
#' @keywords Input check
#'
#' @importFrom logger log_trace
#'
#' @noRd
#'
CheckInput <- function(InputData,
InputData_Num=TRUE,
SettingsFile_Sample=NULL,
SettingsFile_Metab=NULL,
SettingsInfo=NULL,
SaveAs_Plot=NULL,
SaveAs_Table=NULL,
CoRe=FALSE,
PrintPlot=FALSE,
Theme=NULL,
PlotSettings=NULL){
############## Parameters valid for multiple MetaProViz functions
#-------------InputData
if(is.data.frame(InputData)==FALSE){
message <- paste0("InputData should be a data.frame. It's currently a ", paste(class(InputData)), ".", sep = "")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(any(duplicated(row.names(InputData)))==TRUE){
message <- paste0("Duplicated row.names of InputData, whilst row.names must be unique")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(InputData_Num==TRUE){
Test_num <- apply(InputData, 2, function(x) is.numeric(x))
if((any(Test_num) == FALSE) == TRUE){
message <- paste0("InputData needs to be of class numeric")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
if(sum(duplicated(colnames(InputData))) > 0){
message <- paste0("InputData contained duplicates column names, whilst col.names must be unique.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
#-------------SettingsFile
if(is.null(SettingsFile_Sample)==FALSE){
Test_match <- merge(SettingsFile_Sample, InputData, by = "row.names", all = FALSE)
if(nrow(Test_match) == 0){
message <- paste0("row.names InputData need to match row.names SettingsFile_Sample.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
if(is.null(SettingsFile_Metab)==FALSE){
Test_match <- merge(SettingsFile_Metab, as.data.frame(t(InputData)), by = "row.names", all = FALSE)
if(nrow(Test_match) == 0){
stop("col.names InputData need to match row.names SettingsFile_Metab.")
}
}
#-------------SettingsInfo
if(is.vector(SettingsInfo)==FALSE & is.null(SettingsInfo)==FALSE){
message <- paste0("SettingsInfo should be NULL or a vector. It's currently a ", paste(class(SettingsInfo), ".", sep = ""))
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.null(SettingsInfo)==FALSE){
#Conditions
if("Conditions" %in% names(SettingsInfo)){
if(SettingsInfo[["Conditions"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ", SettingsInfo[["Conditions"]], " column selected as Conditions in SettingsInfo was not found in SettingsFile. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Biological replicates
if("Biological_Replicates" %in% names(SettingsInfo)){
if(SettingsInfo[["Biological_Replicates"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ", SettingsInfo[["Biological_Replicates"]], " column selected as Biological_Replicates in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Numerator
if("Numerator" %in% names(SettingsInfo)==TRUE){
if(SettingsInfo[["Numerator"]] %in% SettingsFile_Sample[[SettingsInfo[["Conditions"]]]]==FALSE){
message <- paste0("The ",SettingsInfo[["Numerator"]], " column selected as numerator in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Denominator
if("Denominator" %in% names(SettingsInfo)==TRUE){
if(SettingsInfo[["Denominator"]] %in% SettingsFile_Sample[[SettingsInfo[["Conditions"]]]]==FALSE){
message <- paste0("The ",SettingsInfo[["Denominator"]], " column selected as denominator in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Denominator & Numerator
if("Denominator" %in% names(SettingsInfo)==FALSE & "Numerator" %in% names(SettingsInfo) ==TRUE){
message <- paste0("Check input. The selected denominator option is empty while ",paste(SettingsInfo[["Numerator"]])," has been selected as a numerator. Please add a denominator for 1-vs-1 comparison or remove the numerator for all-vs-all comparison.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
#Superplot
if("Superplot" %in% names(SettingsInfo)){
if(SettingsInfo[["Superplot"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ",SettingsInfo[["Superplot"]], " column selected as Superplot column in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
if(is.null(PlotSettings)==FALSE){
if(PlotSettings== "Sample"){
#Plot colour
if("color" %in% names(SettingsInfo)){
if(SettingsInfo[["color"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ",SettingsInfo[["color"]], " column selected as color in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Plot shape
if("shape" %in% names(SettingsInfo)){
if(SettingsInfo[["shape"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ",SettingsInfo[["shape"]], " column selected as shape in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Plot individual
if("individual" %in% names(SettingsInfo)){
if(SettingsInfo[["individual"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ",SettingsInfo[["individual"]], " column selected as individual in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
}else if(PlotSettings== "Feature"){
if("color" %in% names(SettingsInfo)){
if(SettingsInfo[["color"]] %in% colnames(SettingsFile_Metab)== FALSE){
message <- paste0("The ",SettingsInfo[["color"]], " column selected as color in SettingsInfo was not found in SettingsFile_Metab. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Plot shape
if("shape" %in% names(SettingsInfo)){
if(SettingsInfo[["shape"]] %in% colnames(SettingsFile_Metab)== FALSE){
message <- paste0("The ",SettingsInfo[["shape"]], " column selected as shape in SettingsInfo was not found in SettingsFile_Metab. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Plot individual
if("individual" %in% names(SettingsInfo)){
if(SettingsInfo[["individual"]] %in% colnames(SettingsFile_Metab)== FALSE){
message <- paste0("The ",SettingsInfo[["individual"]], " column selected as individual in SettingsInfo was not found in SettingsFile_Metab. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
}else if(PlotSettings== "Both"){
#Plot colour sample
if("color_Sample" %in% names(SettingsInfo)){
if(SettingsInfo[["color_Sample"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ",SettingsInfo[["color_Sample"]], " column selected as color_Sample in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Plot colour Metab
if("color_Metab" %in% names(SettingsInfo)){
if(SettingsInfo[["color_Metab"]] %in% colnames(SettingsFile_Metab)== FALSE){
message <- paste0("The ",SettingsInfo[["color_Metab"]], " column selected as color_Metab in SettingsInfo was not found in SettingsFile_Metab. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(sum(colnames(InputData) %in% SettingsFile_Metab$Metabolite) < length(InputData) ){
message <- paste0("The InputData contains metabolites not found in SettingsFile_Metab.")
logger::log_trace(paste("Warning ", message, sep=""))
warning(message)
}
}
# Plot shape_metab
if("shape_Metab" %in% names(SettingsInfo)){
if(SettingsInfo[["shape_Metab"]] %in% colnames(SettingsFile_Metab)== FALSE){
message <- paste0("The ",SettingsInfo[["shape_Metab"]], " column selected as shape_Metab in SettingsInfo was not found in SettingsFile_Metab. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
# Plot shape_metab
if("shape_Sample" %in% names(SettingsInfo)){
if(SettingsInfo[["shape_Sample"]] %in% colnames(SettingsFile_Metab)== FALSE){
message <- paste0("The ",SettingsInfo[["shape_Sample"]], " column selected as shape_Metab in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Plot individual_Metab
if("individual_Metab" %in% names(SettingsInfo)){
if(SettingsInfo[["individual_Metab"]] %in% colnames(SettingsFile_Metab)== FALSE){
message <- paste0("The ",SettingsInfo[["individual_Metab"]], " column selected as individual_Metab in SettingsInfo was not found in SettingsFile_Metab. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#Plot individual_Sample
if("individual_Sample" %in% names(SettingsInfo)){
if(SettingsInfo[["individual_Sample"]] %in% colnames(SettingsFile_Sample)== FALSE){
message <- paste0("The ",SettingsInfo[["individual_Sample"]], " column selected as individual_Sample in SettingsInfo was not found in SettingsFile_Sample. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
}
}
}
#-------------SaveAs
Save_as_Plot_options <- c("svg","pdf", "png")
if(is.null(SaveAs_Plot)==FALSE){
if(SaveAs_Plot %in% Save_as_Plot_options == FALSE){
message <- paste0("Check input. The selected SaveAs_Plot option is not valid. Please select one of the folowwing: ",paste(Save_as_Plot_options,collapse = ", ")," or set to NULL if no plots should be saved.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
SaveAs_Table_options <- c("txt","csv", "xlsx", "RData")#RData = SummarizedExperiment (?)
if(is.null(SaveAs_Table)==FALSE){
if((SaveAs_Table %in% SaveAs_Table_options == FALSE)| (is.null(SaveAs_Table)==TRUE)){
message <- paste0("Check input. The selected SaveAs_Table option is not valid. Please select one of the folowwing: ",paste(SaveAs_Table_options,collapse = ", ")," or set to NULL if no tables should be saved.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#-------------CoRe
if(is.logical(CoRe) == FALSE){
message <- paste0("Check input. The CoRe value should be either =TRUE for preprocessing of Consuption/Release experiment or =FALSE if not.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
#-------------Theme
if(is.null(Theme)==FALSE){
Theme_options <- c("theme_grey()", "theme_gray()", "theme_bw()", "theme_linedraw()", "theme_light()", "theme_dark()", "theme_minimal()", "theme_classic()", "theme_void()", "theme_test()")
if (Theme %in% Theme_options == FALSE){
message <- paste0("Check input. Theme option is incorrect. You can check for complete themes here: https://ggplot2.tidyverse.org/reference/ggtheme.html. Options are the following: ",paste(Theme_options, collapse = ", "),"." )
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#------------- general
if(is.logical(PrintPlot) == FALSE){
message <- paste0("Check input. PrintPlot should be either =TRUE or =FALSE.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
################################################################################################
### ### ### PreProcessing helper function: Internal Function to check function input ### ### ###
################################################################################################
#' Check specific input parameters for PreProcessing()
#'
#' @param SettingsFile_Sample DF which contains information about the samples, which will be combined with the input data based on the unique sample identifiers used as rownames.
#' @param SettingsInfo Named vector containing the information about the names of the experimental parameters. c(Conditions="ColumnName_Plot_SettingsFile", Biological_Replicates="ColumnName_Plot_SettingsFile"). For CoRe = TRUE a CoRe_norm_factor = "Columnname_Input_SettingsFile" and CoRe_media = "Columnname_Input_SettingsFile", have to also be added.
#' @param \emph{Optional: }CoRe If TRUE, a consumption-release experiment has been performed and the CoRe value will be calculated.\strong{Default = FALSE}
#' @param FeatureFilt \emph{Optional: }If NULL, no feature filtering is performed. If set to "Standard" then it applies the 80%-filtering rule (Bijlsma S. et al., 2006) on the metabolite features on the whole dataset. If is set to "Modified",filtering is done based on the different conditions, thus a column named "Conditions" must be provided in the Input_SettingsFile input file including the individual conditions you want to apply the filtering to (Yang, J et al., 2015). \strong{Default = "Standard"}
#' @param FeatureFilt_Value \emph{Optional: } Percentage of feature filtering. \strong{Default = 0.8}
#' @param TIC \emph{Optional: } If TRUE, Total Ion Count normalization is performed. \strong{Default = TRUE}
#' @param MVI \emph{Optional: } If TRUE, Missing Value Imputation (MVI) based on half minimum is performed \strong{Default = TRUE}
#' @param MVI_Percentage \emph{Optional: } Percentage 0-100 of imputed value based on the minimum value. \strong{Default = 50}
#' @param HotellinsConfidence \emph{Optional: } Defines the Confidence of Outlier identification in HotellingT2 test. Must be numeric.\strong{Default = 0.99}
#'
#' @return returns warnings and errors if input is not correct
#'
#' @keywords Input check for MetaProViz::PreProcessing
#'
#' @importFrom logger log_trace
#'
#' @noRd
#'
CheckInput_PreProcessing <- function(SettingsFile_Sample,
SettingsInfo,
CoRe=FALSE,
FeatureFilt = "Modified",
FeatureFilt_Value = 0.8,
TIC = TRUE,
MVI= TRUE,
MVI_Percentage=50,
HotellinsConfidence = 0.99){
if(is.vector(SettingsInfo)==TRUE){
#-------------SettingsInfo
#CoRe
if(CoRe == TRUE){ # parse CoRe normalisation factor
message <- paste0("For Consumption Release experiment we are using the method from Jain M. REF: Jain et. al, (2012), Science 336(6084):1040-4, doi: 10.1126/science.1218595.")
logger::log_trace(paste("Message ", message, sep=""))
message(message)
if("CoRe_media" %in% names(SettingsInfo)){
if(length(grep(SettingsInfo[["CoRe_media"]], SettingsFile_Sample[[SettingsInfo[["Conditions"]]]])) < 1){ # Check for CoRe_media samples
message <- paste0("No CoRe_media samples were provided in the 'Conditions' in the SettingsFile_Sample. For a CoRe experiment control media samples without cells have to be measured and be added in the 'Conditions'
column labeled as 'CoRe_media' (see @param section). Please make sure that you used the correct labelling or whether you need CoRe = FALSE for your analysis")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
if ("CoRe_norm_factor" %in% names(SettingsInfo)==FALSE){
message <- paste0("No growth rate or growth factor provided for normalising the CoRe result, hence CoRe_norm_factor set to 1 for each sample")
logger::log_trace(paste("Warning ", message, sep=""))
warning(message)
}
}
}
#-------------General parameters
Feature_Filtering_options <- c("Standard","Modified")
if(FeatureFilt %in% Feature_Filtering_options == FALSE & is.null(FeatureFilt)==FALSE){
message <- paste0("Check input. The selected FeatureFilt option is not valid. Please set to NULL or select one of the folowwing: ",paste(Feature_Filtering_options,collapse = ", "),"." )
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.numeric(FeatureFilt_Value) == FALSE |FeatureFilt_Value > 1 | FeatureFilt_Value < 0){
message <- paste0("Check input. The selected FeatureFilt_Value should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.logical(TIC) == FALSE){
message <- paste0("Check input. The TIC value should be either `TRUE` if TIC normalization is to be performed or `FALSE` if no data normalization is to be applied.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.logical(MVI) == FALSE){
message <- paste0("Check input. The MVI value should be either `TRUE` if mising value imputation should be performed or `FALSE` if not.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.numeric(MVI_Percentage)== FALSE |HotellinsConfidence > 100 | HotellinsConfidence < 0){
message <- paste0("Check input. The selected MVI_Percentage value should be numeric and between 0 and 100.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if( is.numeric(HotellinsConfidence)== FALSE |HotellinsConfidence > 1 | HotellinsConfidence < 0){
message <- paste0("Check input. The selected HotellinsConfidence value should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
################################################################################################
### ### ### DMA helper function: Internal Function to check function input ### ### ###
################################################################################################
#' Check input parameters of DMA
#'
#' @param InputData DF with unique sample identifiers as row names and metabolite numerical values in columns with metabolite identifiers as column names.
#' @param SettingsFile_Sample DF which contains metadata information about the samples, which will be combined with your input data based on the unique sample identifiers used as rownames.
#' @param SettingsInfo Named vector including the information about the conditions column information on numerator or denominator c(Conditions="ColumnName_SettingsFile", Numerator = "ColumnName_SettingsFile", Denominator = "ColumnName_SettingsFile"). \strong{Default = c(conditions="Conditions", numerator = NULL, denumerator = NULL)}
#' @param StatPval \emph{Optional: } String which contains an abbreviation of the selected test to calculate p.value. For one-vs-one comparisons choose t.test, wilcox.test, "chisq.test", "cor.test" or lmFit (=limma), for one-vs-all or all-vs-all comparison choose aov (=anova), welch(=welch anova), kruskal.test or lmFit (=limma) \strong{Default = "lmFit"}
#' @param StatPadj \emph{Optional: } String which contains an abbreviation of the selected p.adjusted test for p.value correction for multiple Hypothesis testing. Search: ?p.adjust for more methods:"BH", "fdr", "bonferroni", "holm", etc.\strong{Default = "fdr"}
#' @param VST TRUE or FALSE for whether to use variance stabilizing transformation on the data when linear modeling is used for hypothesis testing. \strong{Default = FALSE}
#' @param PerformShapiro TRUE or FALSE for whether to perform the shapiro.test and get informed about data distribution (normal versus not-normal distribution. \strong{Default = TRUE}
#' @param PerformBartlett TRUE or FALSE for whether to perform the bartlett.test. \strong{Default = TRUE}
#' @param Transform TRUE or FALSE. If TRUE we expect the data to be not log2 transformed and log2 transformation will be performed within the limma function and Log2FC calculation. If FALSE we expect the data to be log2 transformed as this impacts the Log2FC calculation and limma. \strong{Default= TRUE}
#'
#' @return Returns: 1. warnings and errors if input is not correct, 2. Settings
#'
#' @keywords Input check for MetaProViz::DMA
#'
#' @importFrom logger log_trace
#' @importFrom dplyr filter select_if
#' @importFrom magrittr %>%
#' @importFrom utils combn
#'
#' @noRd
#'
CheckInput_DMA <- function(InputData,
SettingsFile_Sample,
SettingsInfo= c(Conditions="Conditions", Numerator = NULL, Denominator = NULL),
StatPval ="lmFit",
StatPadj="fdr",
VST=FALSE,
PerformShapiro =TRUE,
PerformBartlett =TRUE,
Transform=TRUE){
#-------------SettingsInfo
if(is.null(SettingsInfo)==TRUE){
message <- paste0("You have to provide SettingsInfo's for Conditions.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
## ------------ Denominator/numerator ----------- ##
# Denominator and numerator: Define if we compare one_vs_one, one_vs_all or all_vs_all.
if("Denominator" %in% names(SettingsInfo)==FALSE & "Numerator" %in% names(SettingsInfo) ==FALSE){
# all-vs-all: Generate all pairwise combinations
conditions = SettingsFile_Sample[[SettingsInfo[["Conditions"]]]]
denominator <-unique(SettingsFile_Sample[[SettingsInfo[["Conditions"]]]])
numerator <-unique(SettingsFile_Sample[[SettingsInfo[["Conditions"]]]])
comparisons <- utils::combn(unique(conditions), 2) %>% as.matrix()
#Settings:
MultipleComparison = TRUE
all_vs_all = TRUE
}else if("Denominator" %in% names(SettingsInfo)==TRUE & "Numerator" %in% names(SettingsInfo)==FALSE){
#all-vs-one: Generate the pairwise combinations
conditions = SettingsFile_Sample[[SettingsInfo[["Conditions"]]]]
denominator <- SettingsInfo[["Denominator"]]
numerator <-unique(SettingsFile_Sample[[SettingsInfo[["Conditions"]]]])
# Remove denom from num
numerator <- numerator[!numerator %in% denominator]
comparisons <- t(expand.grid(numerator, denominator)) %>% as.data.frame()
#Settings:
MultipleComparison = TRUE
all_vs_all = FALSE
}else if("Denominator" %in% names(SettingsInfo)==TRUE & "Numerator" %in% names(SettingsInfo)==TRUE){
# one-vs-one: Generate the comparisons
denominator <- SettingsInfo[["Denominator"]]
numerator <- SettingsInfo[["Numerator"]]
comparisons <- matrix(c(SettingsInfo[["Denominator"]], SettingsInfo[["Numerator"]]))
#Settings:
MultipleComparison = FALSE
all_vs_all = FALSE
}
## ------------ Test statistics ----------- ##
if(MultipleComparison==FALSE){
STAT_pval_options <- c("t.test", "wilcox.test","chisq.test", "cor.test", "lmFit")
if(StatPval %in% STAT_pval_options == FALSE){
message <- paste0("Check input. The selected StatPval option for Hypothesis testing is not valid for multiple comparison (one-vs-all or all-vs-all). Please select one of the following: ",paste(STAT_pval_options,collapse = ", ")," or specify numerator and denumerator." )
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}else{
STAT_pval_options <- c("aov", "kruskal.test", "welch" ,"lmFit")
if(StatPval %in% STAT_pval_options == FALSE){
message <- paste0("Check input. The selected StatPval option for Hypothesis testing is not valid for one-vs-one comparsion. Multiple comparison is selected. Please select one of the following: ",paste(STAT_pval_options,collapse = ", ")," or change numerator and denumerator." )
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
STAT_padj_options <- c("holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none")
if(StatPadj %in% STAT_padj_options == FALSE){
message <- paste0("Check input. The selected StatPadj option for multiple Hypothesis testing correction is not valid. Please select one of the folowing: ",paste(STAT_padj_options,collapse = ", "),"." )
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
## ------------ Sample Numbers ----------- ##
Num <- InputData %>%#Are sample numbers enough?
dplyr::filter(SettingsFile_Sample[[SettingsInfo[["Conditions"]]]] %in% numerator) %>%
dplyr::select_if(is.numeric)#only keep numeric columns with metabolite values
Denom <- InputData %>%
dplyr::filter(SettingsFile_Sample[[SettingsInfo[["Conditions"]]]] %in% denominator) %>%
dplyr::select_if(is.numeric)
if(nrow(Num)==1){
message <- paste0("There is only one sample available for ", numerator, ", so no statistical test can be performed.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
} else if(nrow(Denom)==1){
message <- paste0("There is only one sample available for ", denominator, ", so no statistical test can be performed.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}else if(nrow(Num)==0){
message <- paste0("There is no sample available for ", numerator, ".")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}else if(nrow(Denom)==0){
message <- paste0("There is no sample available for ", denominator, ".")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
## ------------ Check Missingness ------------- ##
Num_Miss <- replace(Num, Num==0, NA)
Num_Miss <- Num_Miss[, (colSums(is.na(Num_Miss)) > 0), drop = FALSE]
Denom_Miss <- replace(Denom, Denom==0, NA)
Denom_Miss <- Denom_Miss[, (colSums(is.na(Denom_Miss)) > 0), drop = FALSE]
if((ncol(Num_Miss)>0 & ncol(Denom_Miss)==0)){
Metabolites_Miss <- colnames(Num_Miss)
if(ncol(Num_Miss)<=10){
message <- paste0("In `Numerator` ",paste0(toString(numerator)), ", NA/0 values exist in ", ncol(Num_Miss), " Metabolite(s): ", paste0(colnames(Num_Miss), collapse = ", "), ". Those metabolite(s) might return p.val= NA, p.adj.= NA, t.val= NA. The Log2FC = Inf, if all replicates are 0/NA.")
logger::log_info(message)
message(message)
}else{
message <- paste0("In `Numerator` ",paste0(toString(numerator)), ", NA/0 values exist in ", ncol(Num_Miss), " Metabolite(s).", " Those metabolite(s) might return p.val= NA, p.adj.= NA, t.val= NA. The Log2FC = Inf, if all replicates are 0/NA.")
logger::log_info(message)
message(message)
}
} else if(ncol(Num_Miss)==0 & ncol(Denom_Miss)>0){
Metabolites_Miss <- colnames(Denom_Miss)
if(ncol(Num_Miss)<=10){
message <- paste0("In `Denominator` ",paste0(toString(denominator)), ", NA/0 values exist in ", ncol(Denom_Miss), " Metabolite(s): ", paste0(colnames(Denom_Miss), collapse = ", "), ". Those metabolite(s) might return p.val= NA, p.adj.= NA, t.val= NA. The Log2FC = Inf, if all replicates are 0/NA.")
logger::log_info(message)
message(message)
}else{
message <- paste0("In `Denominator` ",paste0(toString(denominator)), ", NA/0 values exist in ", ncol(Denom_Miss), " Metabolite(s).", " Those metabolite(s) might return p.val= NA, p.adj.= NA, t.val= NA. The Log2FC = Inf, if all replicates are 0/NA.")
logger::log_info(message)
message(message)#
}
} else if(ncol(Num_Miss)>0 & ncol(Denom_Miss)>0){
Metabolites_Miss <- c(colnames(Num_Miss), colnames(Denom_Miss))
Metabolites_Miss <- unique(Metabolites_Miss)
message <- paste0("In `Numerator` ",paste0(toString(numerator)), ", NA/0 values exist in ", ncol(Num_Miss), " Metabolite(s).", " and in `denominator`",paste0(toString(denominator)), " ",ncol(Denom_Miss), " Metabolite(s).",
". Those metabolite(s) might return p.val= NA, p.adj.= NA, t.val= NA. The Log2FC = Inf, if all replicates are 0/NA.")
logger::log_info(message)
message(message)
} else{
message <- paste0("There are no NA/0 values")
logger::log_info(message)
message(message)
Metabolites_Miss <- c(colnames(Num_Miss), colnames(Denom_Miss))
Metabolites_Miss <- unique(Metabolites_Miss)
}
#-------------General parameters
if(is.logical(VST) == FALSE){
message <- paste0("Check input. The VST value should be either =TRUE or =FALSE.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.logical(PerformShapiro) == FALSE){
message <- paste0("Check input. The Shapiro value should be either =TRUE or =FALSE.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.logical(PerformBartlett) == FALSE){
message <- paste0("Check input. The Bartlett value should be either =TRUE or =FALSE.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.logical(Transform) == FALSE){
message <- paste0("Check input. `Transform` should be either =TRUE or =FALSE.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
Settings <- list("comparisons"=comparisons, "MultipleComparison"=MultipleComparison, "all_vs_all"=all_vs_all, "Metabolites_Miss"=Metabolites_Miss, "denominator"=denominator, "numerator"=numerator)
return(invisible(Settings))
}
################################################################################################
### ### ### ORA helper function: Internal Function to check function input ### ### ###
################################################################################################
#' Check input parameters of ORA
#'
#' @param InputData DF with metabolite names/metabolite IDs as row names. Metabolite names/IDs need to match the identifier type (e.g. HMDB IDs) in the PathwayFile.
#' @param SettingsInfo \emph{Optional: } Pass ColumnName of the column including parameters to use for pCutoff and PercentageCutoff, ColumnName for PathwayFile. For MetaProViz::ClusterORA also BackgroundColumn. \strong{c(pvalColumn="p.adj", PercentageColumn="t.val", PathwayTerm= "term", PathwayFeature= "Metabolite")}
#' @param pCutoff \emph{Optional: } p-adjusted value cutoff from ORA results. Must be a numeric value. \strong{default: 0.05}
#' @param PercentageCutoff \emph{Optional: } Percentage cutoff of metabolites that should be considered for ORA. Selects Top/Bottom % of selected PercentageColumn, usually t.val or Log2FC \strong{default: 10}
#' @param PathwayFile DF that must include column "term" with the pathway name, column "Metabolite" with the Metabolite name or ID and column "Description" with pathway description that will be depicted on the plots.
#' @param PathwayName \emph{Optional: } Name of the PathwayFile used \strong{default: ""}
#' @param minGSSize \emph{Optional: } minimum group size in ORA \strong{default: 10}
#' @param maxGSSize \emph{Optional: } maximum group size in ORA \strong{default: 1000}
#' @param SaveAs_Table \emph{Optional: } File types for the analysis results are: "csv", "xlsx", "txt" \strong{default: "csv"}
#' @param RemoveBackground For MetaProViz::ClusterORA the Background Settings are passed, for MetaProViz::StandardORA set to FALSE.
#'
#' @return Returns: 1. warnings and errors if input is not correct, 2. Pathway file
#'
#' @keywords Input check MetaProViz::StandardORA and MetaProViz::ClusterORA
#'
#' @importFrom logger log_trace
#' @importFrom dplyr rename
#'
#' @noRd
#'
CheckInput_ORA <- function(InputData,
SettingsInfo=c(pvalColumn="p.adj", PercentageColumn="t.val", PathwayTerm= "term", PathwayFeature= "Metabolite"),
pCutoff=0.05,
PercentageCutoff=10,
PathwayFile,
PathwayName="",
minGSSize=10,
maxGSSize=1000 ,
SaveAs_Table="csv",
RemoveBackground
){
# 1. The input data:
if(class(InputData) != "data.frame"){
message <- paste0("InputData should be a data.frame. It's currently a ", paste(class(InputData), ".",sep = ""))
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(any(duplicated(row.names(InputData)))==TRUE){
message <- paste0("Duplicated row.names of InputData, whilst row.names must be unique")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
# 2. Settings Columns:
if(is.vector(SettingsInfo)==FALSE & is.null(SettingsInfo)==FALSE){
message <- paste0("SettingsInfo should be NULL or a vector. It's currently a ", paste(class(SettingsInfo), ".", sep = ""))
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.null(SettingsInfo)==FALSE){
#"ClusterColumn"
if("ClusterColumn" %in% names(SettingsInfo)){
if(SettingsInfo[["ClusterColumn"]] %in% colnames(InputData)== FALSE){
message <- paste0("The ", SettingsInfo[["ClusterColumn"]], " column selected as ClusterColumn in SettingsInfo was not found in InputData. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#"BackgroundColumn"
if("BackgroundColumn" %in% names(SettingsInfo)){
if(SettingsInfo[["BackgroundColumn"]] %in% colnames(InputData)== FALSE){
message <- paste0("The ", SettingsInfo[["BackgroundColumn"]], " column selected as BackgroundColumn in SettingsInfo was not found in InputData. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#"pvalColumn"
if("pvalColumn" %in% names(SettingsInfo)){
if(SettingsInfo[["pvalColumn"]] %in% colnames(InputData)== FALSE){
message <- paste0("The ", SettingsInfo[["pvalColumn"]], " column selected as pvalColumn in SettingsInfo was not found in InputData. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#"PercentageColumn"
if("PercentageColumn" %in% names(SettingsInfo)){
if(SettingsInfo[["PercentageColumn"]] %in% colnames(InputData)== FALSE){
message <- paste0("The ", SettingsInfo[["PercentageColumn"]], " column selected as PercentageColumn in SettingsInfo was not found in InputData. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#"PathwayTerm"
if("PathwayTerm" %in% names(SettingsInfo)){
if(SettingsInfo[["PathwayTerm"]] %in% colnames(PathwayFile)== FALSE){
message <- paste0("The ", SettingsInfo[["PathwayTerm"]], " column selected as PathwayTerm in SettingsInfo was not found in PathwayFile. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}else{
PathwayFile <- PathwayFile%>%
dplyr::rename("term"=SettingsInfo[["PathwayTerm"]])
PathwayFile$Description <- PathwayFile$term
}
}else{
message <- paste0("SettingsInfo must provide the column name for PathwayTerm in PathwayFile")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
# PathwayFeature
if("PathwayFeature" %in% names(SettingsInfo)){
if(SettingsInfo[["PathwayFeature"]] %in% colnames(PathwayFile)== FALSE){
message <- paste0("The ", SettingsInfo[["PathwayFeature"]], " column selected as PathwayFeature in SettingsInfo was not found in PathwayFile. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}else{
PathwayFile <- PathwayFile%>%
dplyr::rename("gene"=SettingsInfo[["PathwayFeature"]])
}
}else{
message <- paste0("SettingsInfo must provide the column name for PathwayFeature in PathwayFile")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}else{
message <- paste0("You must provide SettingsInfo.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
# 3. General Settings
if(is.character(PathwayName)==FALSE){
message <- paste0("Check input. PathwayName must be a character of syntax 'example'.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.logical(RemoveBackground) == FALSE){
message <- paste0("Check input. RemoveBackground value should be either =TRUE or = FALSE.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.numeric(minGSSize)== FALSE){
message <- paste0("Check input. The selected minGSSize value should be numeric.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.numeric(maxGSSize)== FALSE){
message <- paste0("Check input. The selected maxGSSize value should be numeric.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
SaveAs_Table_options <- c("txt","csv", "xlsx")
if(is.null(SaveAs_Table)==FALSE){
if((SaveAs_Table %in% SaveAs_Table_options == FALSE)| (is.null(SaveAs_Table)==TRUE)){
message <- paste0("Check input. The selected SaveAs_Table option is not valid. Please select one of the folowing: ",paste(SaveAs_Table_options,collapse = ", "),"." )
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
if(is.null(pCutoff)== FALSE){
if(is.numeric(pCutoff)== FALSE | pCutoff > 1 | pCutoff < 0){
message <- paste0("Check input. The selected pCutoff value should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
if(is.null(PercentageCutoff)== FALSE){
if( is.numeric(PercentageCutoff)== FALSE | PercentageCutoff > 100 | PercentageCutoff < 0){
message <- paste0("Check input. The selected PercentageCutoff value should be numeric and between 0 and 100.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
## -------- Return Pathways ---------##
return(invisible(PathwayFile))
}
################################################################################################
### ### ### MCA Helper function: Internal Function to check function input ### ### ###
################################################################################################
#' Check input parameters of MCA
#'
#' @param InputData_Intra For MetaProViz::MCA_CoRe, otherwise NULL. DF for your data (results from e.g. DMA) containing metabolites in rows with corresponding Log2FC and stat (p-value, p.adjusted) value columns.
#' @param InputData_CoRe For MetaProViz::MCA_CoRe, otherwise NULL. DF for your data (results from e.g. DMA) containing metabolites in rows with corresponding Log2FC and stat (p-value, p.adjusted) value columns. Here we additionally require
#' @param SettingsInfo_Intra For MetaProViz::MCA_CoRe, otherwise NULL. Pass ColumnNames and Cutoffs for the intracellular metabolomics including the value column (e.g. Log2FC, Log2Diff, t.val, etc) and the stats column (e.g. p.adj, p.val). This must include: c(ValueCol=ColumnName_InputData_Intra,StatCol=ColumnName_InputData_Intra, StatCutoff= NumericValue, ValueCutoff=NumericValue)
#' @param SettingsInfo_CoRe For MetaProViz::MCA_CoRe, otherwise NULL. Pass ColumnNames and Cutoffs for the consumption-release metabolomics including the direction column, the value column (e.g. Log2Diff, t.val, etc) and the stats column (e.g. p.adj, p.val). This must include: c(DirectionCol= ColumnName_InputData_CoRe,ValueCol=ColumnName_InputData_CoRe,StatCol=ColumnName_InputData_CoRe, StatCutoff= NumericValue, ValueCutoff=NumericValue)
#' @param InputData_C1 For MetaProViz::MCA_2Cond, otherwise NULL. DF for your data (results from e.g. DMA) containing metabolites in rows with corresponding Log2FC and stat (p-value, p.adjusted) value columns.
#' @param InputData_C2 For MetaProViz::MCA_2Cond, otherwise NULL. DF for your data (results from e.g. DMA) containing metabolites in rows with corresponding Log2FC and stat (p-value, p.adjusted) value columns.
#' @param SettingsInfo_C1 For MetaProViz::MCA_2Cond, otherwise NULL. Pass ColumnNames and Cutoffs for condition 1 including the value column (e.g. Log2FC, Log2Diff, t.val, etc) and the stats column (e.g. p.adj, p.val). This must include: c(ValueCol=ColumnName_InputData_C1,StatCol=ColumnName_InputData_C1, StatCutoff= NumericValue, ValueCutoff=NumericValue)
#' @param SettingsInfo_C2 For MetaProViz::MCA_2Cond, otherwise NULL. Pass ColumnNames and Cutoffs for condition 2 includingthe value column (e.g. Log2FC, Log2Diff, t.val, etc) and the stats column (e.g. p.adj, p.val). This must include: c(ValueCol=ColumnName_InputData_C2,StatCol=ColumnName_InputData_C2, StatCutoff= NumericValue, ValueCutoff=NumericValue)
#' @param FeatureID \emph{Optional: } Column name of Column including the Metabolite identifiers. This MUST BE THE SAME in each of your Input files. \strong{Default="Metabolite"}
#' @param BackgroundMethod \emph{Optional: } Background method `Intra|CoRe, Intra&CoRe, CoRe, Intra or * \strong{Default="Intra&CoRe"}
#' @param SaveAs_Table \emph{Optional: } File types for the analysis results are: "csv", "xlsx", "txt" \strong{default: "csv"}
#'
#' @return Returns warnings and errors if input is not correct
#'
#' @keywords Input check MetaProViz::MCA_2Cond and MetaProViz::MCA_CoRe
#'
#' @importFrom logger log_trace
#'
#' @noRd
#'
CheckInput_MCA <- function(InputData_C1,
InputData_C2,
InputData_CoRe,
InputData_Intra,
SettingsInfo_C1,
SettingsInfo_C2,
SettingsInfo_CoRe,
SettingsInfo_Intra,
FeatureID= "Metabolite",
BackgroundMethod="Intra&CoRe",
SaveAs_Table = "csv"
){
## ------------ Create log file ----------- ##
MetaProViz_Init()
#------------- InputData
if(is.null(InputData_C1)==FALSE){
if(class(InputData_C1) != "data.frame"| class(InputData_C2) != "data.frame"){
message <- paste0("InputData_C1 and InputData_C2 should be a data.frame. It's currently a ", paste(class(InputData_C1)), paste(class(InputData_C2)), ".",sep = "")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(length(InputData_C1[duplicated(InputData_C1[[FeatureID]]), FeatureID]) > 0){
message <- paste0("Duplicated FeatureIDs of InputData_C1, whilst features must be unique")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(length(InputData_C2[duplicated(InputData_C2[[FeatureID]]), FeatureID]) > 0){
message <- paste0("Duplicated FeatureIDs of InputData_C2, whilst features must be unique")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}else{
if(class(InputData_Intra) != "data.frame"| class(InputData_CoRe) != "data.frame"){
message <- paste0("InputData_Intra and InputData_CoRe should be a data.frame. It's currently a ", paste(class(InputData_Intra)), paste(class(InputData_CoRe)), ".",sep = "")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(length(InputData_Intra[duplicated(InputData_Intra[[FeatureID]]), FeatureID]) > 0){
message <- paste0("Duplicated FeatureIDs of InputData_Intra, whilst features must be unique")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(length(InputData_CoRe[duplicated(InputData_CoRe[[FeatureID]]), FeatureID]) > 0){
message <- paste0("Duplicated FeatureIDs of InputData_CoRe, whilst features must be unique")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#------------- SettingsInfo
if(is.null(SettingsInfo_C1)==FALSE){
## C1
#ValueCol
if("ValueCol" %in% names(SettingsInfo_C1)){
if(SettingsInfo_C1[["ValueCol"]] %in% colnames(InputData_C1)== FALSE){
message <- paste0("The ", SettingsInfo_C1[["ValueCol"]], " column selected as ValueCol in SettingsInfo_C1 was not found in InputData_C1. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#StatCol
if("StatCol" %in% names(SettingsInfo_C1)){
if(SettingsInfo_C1[["StatCol"]] %in% colnames(InputData_C1)== FALSE){
message <- paste0("The ", SettingsInfo_C1[["StatCol"]], " column selected as StatCol in SettingsInfo_C1 was not found in InputData_C1. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
## C2
#ValueCol
if("ValueCol" %in% names(SettingsInfo_C2)){
if(SettingsInfo_C2[["ValueCol"]] %in% colnames(InputData_C2)== FALSE){
message <- paste0("The ", SettingsInfo_C2[["ValueCol"]], " column selected as ValueCol in SettingsInfo_C2 was not found in InputData_C2. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#StatCol
if("StatCol" %in% names(SettingsInfo_C2)){
if(SettingsInfo_C2[["StatCol"]] %in% colnames(InputData_C2)== FALSE){
message <- paste0("The ", SettingsInfo_C2[["StatCol"]], " column selected as StatCol in SettingsInfo_C2 was not found in InputData_C2. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
}else{
## Intra
#ValueCol
if("ValueCol" %in% names(SettingsInfo_Intra)){
if(SettingsInfo_Intra[["ValueCol"]] %in% colnames(InputData_Intra)== FALSE){
message <- paste0("The ", SettingsInfo_Intra[["ValueCol"]], " column selected as ValueCol in SettingsInfo_Intra was not found in InputData_Intra. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#StatCol
if("StatCol" %in% names(SettingsInfo_Intra)){
if(SettingsInfo_Intra[["StatCol"]] %in% colnames(InputData_Intra)== FALSE){
message <- paste0("The ", SettingsInfo_Intra[["StatCol"]], " column selected as StatCol in SettingsInfo_Intra was not found in InputData_Intra. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
## CoRe
#ValueCol
if("ValueCol" %in% names(SettingsInfo_CoRe)){
if(SettingsInfo_CoRe[["ValueCol"]] %in% colnames(InputData_CoRe)== FALSE){
message <- paste0("The ", SettingsInfo_CoRe[["ValueCol"]], " column selected as ValueCol in SettingsInfo_CoRe was not found in InputData_CoRe. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#StatCol
if("StatCol" %in% names(SettingsInfo_CoRe)){
if(SettingsInfo_CoRe[["StatCol"]] %in% colnames(InputData_CoRe)== FALSE){
message <- paste0("The ", SettingsInfo_CoRe[["StatCol"]], " column selected as StatCol in SettingsInfo_CoRe was not found in InputData_CoRe. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
#StatCol
if("DirectionCol" %in% names(SettingsInfo_CoRe)){
if(SettingsInfo_CoRe[["DirectionCol"]] %in% colnames(InputData_CoRe)== FALSE){
message <- paste0("The ", SettingsInfo_CoRe[["DirectionCol"]], " column selected as DirectionCol in SettingsInfo_CoRe was not found in InputData_CoRe. Please check your input.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}
}
#------------- SettingsInfo Cutoffs:
if(is.null(SettingsInfo_C1)==FALSE){
if(is.na(as.numeric(SettingsInfo_C1[["StatCutoff"]])) == TRUE |as.numeric(SettingsInfo_C1[["StatCutoff"]]) > 1 | as.numeric(SettingsInfo_C1[["StatCutoff"]]) < 0){
message <- paste0("Check input. The selected StatCutoff in SettingsInfo_C1 should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.na(as.numeric(SettingsInfo_C2[["StatCutoff"]])) == TRUE |as.numeric(SettingsInfo_C2[["StatCutoff"]]) > 1 | as.numeric(SettingsInfo_C2[["StatCutoff"]]) < 0){
message <- paste0("Check input. The selected StatCutoff in SettingsInfo_C2 should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.na(as.numeric(SettingsInfo_C1[["ValueCutoff"]])) == TRUE){
message <- paste0("Check input. The selected ValueCutoff in SettingsInfo_C1 should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.na(as.numeric(SettingsInfo_C2[["ValueCutoff"]])) == TRUE){
message <- paste0("Check input. The selected ValueCutoff in SettingsInfo_C2 should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
}else{
if(is.na(as.numeric(SettingsInfo_Intra[["StatCutoff"]])) == TRUE |as.numeric(SettingsInfo_Intra[["StatCutoff"]]) > 1 | as.numeric(SettingsInfo_Intra[["StatCutoff"]]) < 0){
message <- paste0("Check input. The selected StatCutoff in SettingsInfo_Intra should be numeric and between 0 and 1.")
logger::log_trace(paste("Error ", message, sep=""))
stop(message)
}
if(is.na(as.numeric(SettingsInfo_CoRe[["StatCutoff"]])) == TRUE |as.numeric(SettingsInfo_CoRe[["StatCutoff"]]) > 1 | as.numeric(SettingsInfo_CoRe[["StatCutoff"]]) < 0){
message <- paste0("Check input. The selected StatCutoff in SettingsInfo_CoRe should be numeric and between 0 and 1.")