-
Notifications
You must be signed in to change notification settings - Fork 1
/
Error Checking.R
536 lines (427 loc) · 20.2 KB
/
Error Checking.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
## These are all of the functions that check to make sure the input data is formatted correctly and has other consistencies.
#################
## Standards Mix
#################
standards_read_check <- function(metadata_file_path){
try_result <- try(CompoundNamesAndFormulasSorted <- read.xlsx_or_csv(metadata_file_path, header = TRUE, stringsAsFactors = FALSE, skip = 20, sheet = "ICMS_StdComp_RefDB_Submission"))
## Check to see if there were any errors when reading in.
if(class(try_result) == "try-error"){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Standards Mixture Read Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("Error when reading in the \"ICMS_StdComp_RefDB_Submission\" sheet in the meta data file:\n\n", try_result[1]),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
return(CompoundNamesAndFormulasSorted)
}
standards_empty_check <- function(CompoundNamesAndFormulasSorted){
## Check to see if there is data in the file.
if(nrow(CompoundNamesAndFormulasSorted) == 0){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Standards Mixture Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_StdComp_RefDB_Submission\" sheet in the meta data file is empty. \nPlease supply a non-empty sheet.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
standards_column_check <- function(CompoundNamesAndFormulasSorted){
## Make sure it has the correct columns. If it doesn't have all of the columns then give the user a message box and quit the program.
if(!all(c("CompoundName", "Concentration_uM") %in% colnames(CompoundNamesAndFormulasSorted))){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Standards Mixture Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_StdComp_RefDB_Submission\" sheet in the meta data file did not have the correct column names. \nThere should be column names for \"CompoundName\" and \"Concentration_uM\". \nThese names are case sensitive.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
CompoundNamesAndFormulasSorted <- CompoundNamesAndFormulasSorted[!is.na(CompoundNamesAndFormulasSorted$CompoundName),]
CompoundNamesAndFormulasSorted <- CompoundNamesAndFormulasSorted[,c("CompoundName", "Concentration_uM")]
return(CompoundNamesAndFormulasSorted)
}
## Check to make sure that every CompoundName has data in every column. This function assumes that there are no NA values for the
## CompoundName column since they are removed with an earlier function. It also assumes that the data only has the columns we need
## since an earlier function also strips off any extra columns.
standards_values_check <- function(CompoundNamesAndFormulasSorted){
if(any(is.na(CompoundNamesAndFormulasSorted))){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Standards Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_StdComp_RefDB_Submission\" sheet in the meta data file does not have data for every CompoundName. \nMake sure every column has values for every CompoundName.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
##################
## Meta Data
##################
metadata_read_check <- function(metadata_file_path){
try_result <- try(meta_data <- read.xlsx_or_csv(metadata_file_path, col_names = TRUE, stringsAsFactors = FALSE, sheet = "ICMS_MetaData_Submission", skip = 20))
## Check to see if there were any errors when reading in.
if(class(try_result) == "try-error"){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Meta Data Read Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("Error when reading in the \"ICMS_MetaData_Submission\" sheet in the meta data file:\n\n", try_result[1]),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
return(meta_data)
}
metadata_empty_check <- function(meta_data){
## Check to see if there is data in the file.
if(nrow(meta_data) == 0){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Meta-Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_MetaData_Submission\" sheet in the meta data file is empty. \nPlease supply a non-empty sheet.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
metadata_column_check <- function(meta_data){
## Make sure meta data has the correct columns. If it doesn't have all of the columns then give the user a message box and quit the program.
if(!all(c("SampleID", "Protein_mg", "ICMS_split_ratio", "ReconstitutionVolume_uL", "InjectionVolume_uL") %in% colnames(meta_data))){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Meta-Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_MetaData_Submission\" sheet in the meta data file did not have the correct column names. \nThere should be column names for \"SampleID\", \"Protein_mg\", \"ICMS_split_ratio\", \"ReconstitutionVolume_uL\", and \"InjectionVolume_uL\". \nThese names are case sensitive.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
meta_data <- meta_data[!is.na(meta_data$SampleID),]
## Keep only the columns we need.
meta_data <- meta_data[,c("SampleID", "Protein_mg", "ICMS_split_ratio", "ReconstitutionVolume_uL", "InjectionVolume_uL")]
meta_data$SampleID <- gsub(" ", "", meta_data$SampleID)
return(meta_data)
}
## Check to make sure that every SampleID has data in every column. This function assumes that there are no NA values for the
## SampleID column since they are removed with an earlier function. It also assumes that the data only has the columns we need
## since an earlier function also strips off any extra columns.
metadata_values_check <- function(meta_data){
if(any(is.na(meta_data))){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Meta-Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_MetaData_Submission\" sheet in the meta data file does not have data for every SampleID. \nMake sure every column has values for every SampleID.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
#########################
## Sequence Data
#########################
sequence_read_check <- function(metadata_file_path){
try_result <- try(sequence_data <- read.xlsx_or_csv(metadata_file_path, col_names = TRUE, stringsAsFactors = FALSE, sheet = "ICMS_SequenceData_Submission", skip = 20))
## Check to see if there were any errors when reading in.
if(class(try_result) == "try-error"){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Sequence Data Read Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("Error when reading in the \"ICMS_SequenceData_Submission\" sheet in the meta data file:\n\n", try_result[1]),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
colnames(sequence_data) <- gsub(" ", "", colnames(sequence_data))
return(sequence_data)
}
sequence_empty_check <- function(sequence_data){
## Check to see if there is data in the file.
if(nrow(sequence_data) == 0){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Sequence Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_SequenceData_Submission\" sheet in the meta data file is empty. \nPlease supply a non-empty sheet.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
sequence_column_check <- function(sequence_data){
## Make sure sequence data has the correct columns. If it doesn't have all of the columns then give the user a message box and quit the program.
if(!("FileName" %in% colnames(sequence_data))){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Sequence Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "The \"ICMS_SequenceData_Submission\" sheet in the meta data file did not have the correct column names. \nThere should be column names for \"File Name\". \nThese names are case sensitive.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
sequence_list <- sequence_data$FileName[!is.na(sequence_data$FileName)]
sequence_data <- data.frame(x = sequence_list, stringsAsFactors = FALSE)
colnames(sequence_data) <- "File Name"
return(sequence_data)
}
##########################
## TraceFinder Reports for Every Sample in MetaData
##########################
metadata_in_TF_list <- function(meta_data, SampleNames){
## Get the sample names that are in the meta data sheet, but there are not TraceFinder reports for.
meta_diff <- setdiff(meta_data$SampleID, SampleNames)
## Check to make sure that every meta data sample ID has a matching tracefinder report.
if(length(meta_diff) != 0 ){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Meta Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("There is not a matching TraceFinder report for every sample in the \"ICMS_MetaData_Submission\" sheet in the meta data file.\nReports for:\n\n", paste(meta_diff, collapse = "\n"), "\n\ncould not be found.", sep =""),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
##########################
## Every Sample in MetaData has a TF Report
##########################
TF_list_in_metadata <- function(meta_data, SampleNamesNoStds){
## Get the sample names that there are TF reports for but are not in the meta data sheet.
TF_diff <- setdiff(SampleNamesNoStds, meta_data$SampleID)
## Check that every TraceFinder report has an entry for meta data.
if(length(TF_diff) != 0 ){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Meta Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("There is not a matching entry in the \"ICMS_MetaData_Submission\" sheet in the meta data file for every TraceFinder report.\nEntries for:\n\n", paste(TF_diff, collapse = "\n"), "\n\ncould not be found in the meta data.", sep = ""),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
##########################
## Every Sample in Sequence Data has a TF Report
##########################
TF_list_in_sequence_data <- function(sequence_data, SampleNames){
## Find the sample names that there are TraceFinder reports for but aren't in the sequence data.
sequence_diff <- setdiff(SampleNames, sequence_data$id)
if(length(sequence_diff) != 0){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Sequence Data Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("There is not a matching entry in the \"ICMS_SequenceData_Submission\" sheet in the meta data file for every TraceFinder report.\nEntries for:\n\n", paste(sequence_diff, collapse = "\n"), "\n\ncould not be found in the sequence data.", sep = ""),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
##########################
## Every Standard Compound is in the TF Reports
##########################
standards_in_TF_reports <- function(CompoundNamesAndFormulasSorted, TF_compounds){
## Check that every compound in the standards mix is in the TraceFinder reports.
standard_diff <- setdiff(CompoundNamesAndFormulasSorted$CompoundName, unique(TF_compounds))
if(length(standard_diff) != 0){
abort_flag <- FALSE
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Standard Mix Warning")
tkgrid(ttklabel(tt, image = warning_icon),
ttklabel(tt, text = paste("The following standard compounds in the \"ICMS_StdComp_RefDB_Submission\" \nsheet of the meta data file were not found in the TraceFinder reports:\n\n", paste(standard_diff, collapse = "\n"), "\n\nContinue quantification?", sep = ""),
font = message_font), padx = 20, pady = 20)
continue <- function(){
tkdestroy(tt)
}
abort <- function(){
assign("abort_flag", TRUE, envir = .GlobalEnv)
tkdestroy(tt)
}
tt1 <- tkframe(tt)
tkgrid(tkbutton(tt1, text='Continue', command = continue, background = "SlateGray1"), tkbutton(tt1, text='Abort', command = abort, background = "SlateGray1"), padx = 20, pady = 15)
tkgrid(tt1, columnspan = 2)
tkwait.window(tt)
if(abort_flag){
stop()
}
}
}
###########################
## TraceFinder reports checks
###########################
report_read_check <- function(TF_File){
try_result <- try(TempMatrix <- read.xlsx2(TF_File, 1, startRow=45))
## Check to see if there were any errors when reading in.
if(class(try_result) == "try-error"){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "TraceFinder Report Read Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("Error when reading in the TraceFinder report ", TF_File, " .\n\n", try_result[1]),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
return(TempMatrix)
}
report_empty_check <- function(TempMatrix, TF_File){
## Check to see if there is data in the file.
if(nrow(TempMatrix) == 0){
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "TraceFinder Report Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("The TraceFinder report ", TF_File, " is empty. \nPlease supply a non-empty report."),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
report_column_check <- function(TempMatrix, TF_File){
## Make sure it has the correct columns. If it doesn't have all of the columns then give the user a message box and quit the program.
if(!all(c("Target.Compounds", "Formula", "Peak.Area") %in% colnames(TempMatrix))){
## Create a message box.
tt <- tktoplevel()
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "TraceFinder Report Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("The TraceFinder report ", TF_File, " did not have the correct column names, or the column names are not on row 45. \nThere should be column names for \"Target Compounds\", \"Formula\" and \"Peak Area\". \nThese names are case sensitive."),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
TempMatrix <- TempMatrix[!is.na(TempMatrix$Target.Compound),]
TempMatrix <- TempMatrix[!TempMatrix$Target.Compounds == "",]
return(TempMatrix)
}
##########################
## TraceFinder Reports All Have the Same Type of Labeling
##########################
TF_labeling_check <- function(TF_labeling_type){
## Make sure all TF files have the same labeling and set labeling to that type.
if(any(TF_labeling_type$Labeling != TF_labeling_type$Labeling[1])){
tt <- tktoplevel()
tkfocus(tt)
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Labeling Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = "Not all TraceFinder files have the same type of labeling.\nPlease submit TraceFinder files with the same type of labeling.",
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}
############################
## Check that every unique compound has a formula.
############################
formulas_check <- function(TF_Report, CompoundListNoLabelling){
Temp <- TF_Report
Temp$NoLabelling <- CompoundListNoLabelling
Temp <- Temp[!duplicated(Temp$NoLabelling),]
if (any(Temp$Formula == "")){
compounds <- Temp$Target.Compound[Temp$Formula == ""]
tt <- tktoplevel()
tkfocus(tt)
message_font <- tkfont.create(family = "Times New Roman", size = 14)
tkwm.title(tt, "Formula Error")
tkgrid(ttklabel(tt, image = error_icon),
ttklabel(tt, text = paste("Not all compounds in TraceFinder files have a formula.\nWithout the correct formula the natural abundance stripping will be incorrect. \nThe compounds are:\n", paste(compounds, collapse = "\n")),
font = message_font), padx = 20, pady = 20)
close_box <- function(){
tkdestroy(tt)
}
tkgrid(tkbutton(tt, text='Okay', command = close_box), columnspan = 2)
tkwait.window(tt)
stop()
}
}