This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2021-12-29_tom_mg_checks.R
622 lines (514 loc) · 22.1 KB
/
2021-12-29_tom_mg_checks.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
# Description: Additional functions for main GENIE validator to front-load
# checks currently caught by the post-processing dashboard.
# Implemented check functions:
# - check_assay_yaml_validity
# - check_seq_assay_id
# - check_pat_sam_id
# - check_contact_info
# - check_death_info
# Author: Haley Hunter-Zinck
# Date: 2021-12-29
# pre-setup ---------------------------
library(optparse)
waitifnot <- function(cond, msg) {
if (!cond) {
for (str in msg) {
message(str)
}
message("Press control-C to exit and try again.")
while(T) {}
}
}
# user input ----------------------------
option_list <- list(
make_option(c("-i", "--synid_project"), type = "character", default = "syn3380222",
help="Synapse ID of project folder (default: syn3380222)"),
make_option(c("-c", "--center"), type = "character", default = "Cancer Research UK",
help="Center abbreviation (default: 'Cancer Research UK')"),
make_option(c("-o", "--synid_folder_output"), type = "character", default = NULL,
help="Synapse ID of output folder (optional)"),
make_option(c("-v", "--verbose"), action="store_true", default = FALSE,
help="Output script messages to the user (default = FALSE).")
)
opt <- parse_args(OptionParser(option_list=option_list))
synid_project <- opt$synid_project
center <- opt$center
synid_folder_output <- opt$synid_folder_output
verbose <- opt$verbose
# setup ----------------------------
tic = as.double(Sys.time())
library(glue)
library(dplyr)
library(yaml)
library(synapser)
synLogin()
# print parameters
if (verbose) {
print(glue("Parameters"))
print(glue("- Project: '{synGet(synid_project, downloadFile = F)$properties$name}' ({synid_project})"))
if (is.null(synid_folder_output)) {
print(glue("- Output folder: {getwd()}"))
} else {
print(glue("- Output folder: '{synGet(synid_folder_output, downloadFile = F)$properties$name}' {synid_folder_output}"))
}
print(glue("- Center: '{center}'"))
print(glue("- Verbose: {verbose}"))
print(glue("--------"))
}
# generic functions ----------------------------
#' Download and load data stored in csv or other delimited format on Synapse
#' into an R data frame.
#'
#' @param synapse_id Synapse ID
#' @version Version of the Synapse entity to download. NA will load current
#' version
#' @param set Delimiter for file
#' @param na.strings Vector of strings to be read in as NA values
#' @param header TRUE if the file contains a header row; FALSE otherwise.
#' @param check_names TRUE if column names should be modified for compatibility
#' with R upon reading; FALSE otherwise.
#' @return data frame
get_synapse_entity_data_in_csv <- function(synapse_id,
version = NA,
sep = ",",
na.strings = c("NA"),
header = T,
check_names = F,
comment.char = "#") {
if (is.na(version)) {
entity <- synGet(synapse_id)
} else {
entity <- synGet(synapse_id, version = version)
}
data <- read.csv(entity$path, stringsAsFactors = F,
na.strings = na.strings, sep = sep, check.names = check_names,
header = header, comment.char = comment.char)
return(data)
}
#' Store a file on Synapse with options to define provenance.
#'
#' @param path Path to the file on the local machine.
#' @param parent_id Synapse ID of the folder or project to which to load the file.
#' @param file_name Name of the Synapse entity once loaded
#' @param prov_name Provenance short description title
#' @param prov_desc Provenance long description
#' @param prov_used Vector of Synapse IDs of data used to create the current
#' file to be loaded.
#' @param prov_exec String representing URL to script used to create the file.
#' @return Synapse ID of entity representing file
save_to_synapse <- function(path,
parent_id,
file_name = NA,
prov_name = NA,
prov_desc = NA,
prov_used = NA,
prov_exec = NA) {
if (is.na(file_name)) {
file_name = path
}
file <- File(path = path, parentId = parent_id, name = file_name)
if (!is.na(prov_name) || !is.na(prov_desc) || !is.na(prov_used) || !is.na(prov_exec)) {
act <- Activity(name = prov_name,
description = prov_desc,
used = prov_used,
executed = prov_exec)
file <- synStore(file, activity = act)
} else {
file <- synStore(file)
}
return(file$properties$id)
}
#' Get a synapse ID by following a traditional file path from a root synapse folder entity.
#'
#' @param synid_folder_root Synapse ID of the root folder
#' @param path Folder path starting in the first subfolder ending in desired folder and delimited with '/'
#' @return Synapse ID of the final subfolder in the path
#' @example get_folder_synid_from_path("syn12345", "first/second/final")
get_folder_synid_from_path <- function(synid_folder_root, path) {
synid_folder_current <- synid_folder_root
subfolders <- strsplit(path, split = "/")[[1]]
for (i in 1:length(subfolders)) {
synid_folder_children <- get_synapse_folder_children(synid_folder_current,
include_types = list("folder"))
if (!is.element(subfolders[i], names(synid_folder_children))) {
return(NA)
}
synid_folder_current <- as.character(synid_folder_children[subfolders[i]])
}
return(synid_folder_current)
}
#' Get all child entities of a synapse folder.
#'
#' @param synapse_id Synapse ID of the folder
#' @param include_types Types of child entities to return
#' @return Vector with values as Synapse IDs and names as entity names.
get_synapse_folder_children <- function(synapse_id,
include_types=list("folder", "file", "table", "link", "entityview", "dockerrepo")) {
ent <- as.list(synGetChildren(synapse_id, includeTypes = include_types))
children <- c()
if (length(ent) > 0) {
for (i in 1:length(ent)) {
children[ent[[i]]$name] <- ent[[i]]$id
}
}
return(children)
}
#' Depth first search for tree traversal of root synapse ID
#' of Synapse folder or project.
#'
#' @param synid_root Synapse ID of the root folder/project.
#' @return Vector of Synapse IDs of all descendants of the root
#' @example
#' traverse_synapse("syn12345")
#' Depth first search for tree traversal of root synapse ID
#' of Synapse folder or project.
#'
#' @param synid_root Synapse ID of the root folder/project.
#' @param exclude Pattern match of entity names to exclude from the traverse.
#' @return Vector of Synapse IDs of all descendants of the root
#' @example
#' traverse_synapse("syn12345")
traverse_synapse <- function(synid_root, exclude = NULL) {
synid_folders <- c()
synid_children <- as.list(synGetChildren(synid_root,
includeTypes = list("folder", "file")))
if(!length(synid_children)) {
return(synid_root)
}
for (synid_child in synid_children) {
if (is.null(exclude) || !grepl(synid_child$name, pattern = exclude, ignore.case = T)) {
synid_folders <- append(synid_folders, traverse_synapse(synid_child$id, exclude = exclude))
}
}
return(c(synid_root, synid_folders))
}
#' Get the name of a Synapse entity.
#'
#' @param synapse_id Synapse ID string
#' @return String representing entity name
#' @example get_synapse_entity_name("syn12345")
get_synapse_entity_name <- function(synapse_id) {
return(synGet(synapse_id, downloadFile = F)$properties$name)
}
#' Get the name of a Synapse entities.
#'
#' @param synapse_ids Vector of Synapse IDs
#' @return Vector of entity names
#' @example get_synapse_entity_name(c("syn54321", syn12345"))
get_synapse_entity_names <- function(synapse_ids) {
synapse_names <- c()
for (synapse_id in synapse_ids) {
synapse_names <- append(synapse_names, get_synapse_entity_name(synapse_id))
}
return(synapse_names)
}
# helper functions for main genie ----------------------
#' Get Synapse ID of center's input files.
#'
#' @param synid_project Synapse ID of relevant project
#' @param center Name of center corresponding to umbrella folder
#' @return Synapse ID
get_synid_folder_center <- function(synid_project, center) {
path_folder <- glue("Centers/{center}/Input")
synid_folder <- get_folder_synid_from_path(synid_folder_root = synid_project,
path = path_folder)
return(synid_folder)
}
#' Get a specific main genie file by descriptor.
#'
#' @param synid_folder_data Synapse ID of folder containing all relevant files
#' @param file_type Type of file to obtain
#' @return Synapse ID
get_synid_file_mg <- function(synid_folder_data,
file_type = c("assay", "sample", "clinical"),
exclude = "vcf") {
file_name <- NA
synid_folder_children <- traverse_synapse(synid_root = synid_folder_data, exclude = exclude)
synid_folder_children <- setNames(synid_folder_children, get_synapse_entity_names(synid_folder_children))
if (file_type == "assay") {
return(as.character(synid_folder_children["assay_information.yaml"]))
}
if (file_type == "sample" || file_type == "patient") {
file_name <- grep(pattern = "data_clinical_supp", x = names(synid_folder_children), value = )
if (length(file_name) == 1) {
return(as.character(synid_folder_children[file_name]))
}
if (length(file_name) > 1 ) {
file_name <- grep(pattern = glue("data_clinical_supp_{file_type}"), x = names(synid_folder_children), value = )
return(as.character(synid_folder_children[file_name]))
}
}
return(file_name)
}
#' Format column names of clinical files for standard processing.
#' - convert to all upper case
#' - replace any duplicate column names with unique column name
#'
#' @param raw Vector of strings representing raw column names
#' @return Vector of strings
clean_column_names <- function(raw) {
mod <- toupper(raw)
mod[which(duplicated(raw))] <- glue("{mod[which(duplicated(raw))]}_2")
return(mod)
}
#' Format sequencing assay IDs for standard processing.
#' - convert to all lower case
#' - replace any underscores with hyphens
#'
#' @param raw Vector of strings representing raw column names
#' @return Vector of strings
clean_seq_ids <- function(raw) {
mod <- tolower(raw)
mod <- gsub(pattern = "_", replacement = "-", x = mod)
return(mod)
}
# check functions for main genie -----------------------
check_assay_yaml_validity_strict <- function(synid_file_yaml) {
data <- tryCatch({
data_seq <- read_yaml(synGet(synid_file_yaml)$path)
}, error = function(cond) {
error_invalid <- T
return(NULL)
}, warning = function(cond) {
return(NULL)
})
return(!is.null(data))
}
check_assay_yaml_validity_loose <- function(synid_file_yaml) {
data <- tryCatch({
data_seq <- read_yaml(synGet(synid_file_yaml)$path)
}, error = function(cond) {
error_invalid <- T
return(NULL)
})
return(!is.null(data))
}
#' Check that assay_information.yaml has a valid format.
#'
#' @param synid_project Synapse ID of project
#' @param center Name of center corresponding to center folder
#' @param warning_invalid Consider YAML files generating warnings in addition to any errors as invalid;
#' otherwise, only errors indicate invalid
#' @return TRUE if valid, otherwise FALSE
check_assay_yaml_validity <- function(synid_project, center, synid_file_seq = NULL, strict = T) {
if (is.null(synid_file_seq)) {
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_seq <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "assay")
}
res <- NA
if (strict) {
res <- check_assay_yaml_validity_strict(synid_file_seq)
} else {
res <- check_assay_yaml_validity_loose(synid_file_seq)
}
return(res)
}
#' Check for sequencing assay IDs listed in clinical file but not in the metadata file.
#'
#' @param synid_project Synapse ID of project
#' @param center Name of center corresponding to center folder
#' @param fuzzy boolean indicating whether fuzzy matching should be used for comparing seq_assay_ids;
#' otherwise, exact match used
#' @return Vector of seq_assay_id values in sample file but not metadata or NULL if none found
check_seq_assay_id <- function(synid_project, center, synid_file_seq = NULL, synid_file_sam = NULL, fuzzy = F) {
if (is.null(synid_file_seq)) {
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_seq <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "assay")
}
if (is.null(synid_file_sam)) {
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_sam <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "sample")
}
data_seq <- read_yaml(synGet(synid_file_seq)$path)
data_sam <- get_synapse_entity_data_in_csv(synid_file_sam, sep = "\t")
colnames(data_sam) <- clean_column_names(colnames(data_sam))
assay_seq <- unlist(lapply(data_seq, function(x) {return(x$assay_specific_info[[1]]$SEQ_ASSAY_ID)}))
assay_sam_raw <- unlist(data_sam %>% select(SEQ_ASSAY_ID) %>% distinct())
assay_sam <- assay_sam_raw
if (fuzzy) {
assay_seq <- clean_seq_ids(assay_seq)
assay_sam <- clean_seq_ids(assay_sam)
}
res <- assay_sam_raw[match(setdiff(assay_sam, assay_seq), assay_sam)]
if (length(res)) {
return(res)
}
return(NULL)
}
#' List any patient IDs in the sample file but not in the patient file,
#' if the files are uploaded as separate files
#'
#' @param synid_project Synapse ID of project
#' @param center Name of center corresponding to center folder
#' @return Vector of patient IDs in sample but not patient file or NULL if none found
check_pat_sam_id <- function(synid_project, center, synid_file_pat = NULL, synid_file_sam = NULL) {
if (is.null(synid_file_pat)) {
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_pat <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "patient")
}
if (is.null(synid_file_sam)) {
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_sam <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "sample")
}
if (synid_file_pat == synid_file_sam) {
return(NULL)
}
data_pat <- get_synapse_entity_data_in_csv(synid_file_pat, sep = "\t")
data_sam <- get_synapse_entity_data_in_csv(synid_file_sam, sep = "\t")
colnames(data_pat) <- clean_column_names(colnames(data_pat))
colnames(data_sam) <- clean_column_names(colnames(data_sam))
id_pat <- unlist(data_pat %>% select(PATIENT_ID) %>% distinct())
id_sam <- unlist(data_sam %>% select(PATIENT_ID) %>% distinct())
res <- setdiff(id_sam, id_pat)
if (length(res)) {
return(res)
}
return(NULL)
}
#' Check for patient contact information inconsistencies.
#' Specifically, check whether the contact information is missing for the
#' year but not the interval or vice versa for a given patient.
#'
#' @param synid_project Synapse ID of project
#' @param center Name of center corresponding to center folder
#' @return Vector of patient IDs with inconsistent contact info or NULL
check_contact_info <- function(synid_project, center, synid_file_pat = NULL,
missing = c("Not Collected", "Unknown", "Not Applicable", NA)) {
if (is.null(synid_file_pat)) {
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_pat <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "patient")
}
data_pat <- get_synapse_entity_data_in_csv(synid_file_pat, sep = "\t", na.strings = c("NA", ""))
colnames(data_pat) <- clean_column_names(colnames(data_pat))
if (length(setdiff(c("YEAR_CONTACT", "INT_CONTACT"), colnames(data_pat)))) {
return(NULL)
}
results <- unlist(data_pat %>%
filter(is.element(YEAR_CONTACT, missing) & !is.element(INT_CONTACT, missing) |
!is.element(YEAR_CONTACT, missing) & is.element(INT_CONTACT, missing)) %>%
select(PATIENT_ID))
if (length(results)) {
return(results)
}
return(NULL)
}
#' Check for patient death data inconsistencies.
#' Specifically, check whether the death information is missing
#' for the year but not the interval or vice versa for a given patient.
#'
#' @param synid_project Synapse ID of project
#' @param center Name of center corresponding to center folder
#' @return Vector of patient IDs with inconsistent contact info or NULL
check_death_info <- function(synid_project, center, synid_file_pat = NULL,
missing = c("Not Collected", "Unknown", "Not Applicable", NA)) {
if (is.null(synid_file_pat)) {
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_pat <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "patient")
}
data_pat <- get_synapse_entity_data_in_csv(synid_file_pat, sep = "\t", na.strings = c("NA", ""))
colnames(data_pat) <- clean_column_names(colnames(data_pat))
if (length(setdiff(c("YEAR_DEATH", "INT_DOD", "DEAD"), colnames(data_pat)))) {
return(NULL)
}
results <- unlist(data_pat %>%
filter(is.element(YEAR_DEATH, missing) & !is.element(INT_DOD, missing) |
!is.element(YEAR_DEATH, missing) & is.element(INT_DOD, missing) |
((!is.element(YEAR_DEATH, missing) | !is.element(INT_DOD, missing)) & !as.logical(DEAD))) %>%
select(PATIENT_ID))
if (length(results)) {
return(results)
}
return(NULL)
}
# main ----------------------------
res_all <- matrix(NA, nrow = 0, ncol = 3, dimnames = list(c(), c("issue_no","flagged_value", "description")))
synid_folder_data <- get_synid_folder_center(synid_project, center)
synid_file_seq <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "assay")
synid_file_pat <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "patient")
synid_file_sam <- get_synid_file_mg(synid_folder_data = synid_folder_data,
file_type = "sample")
res_yaml_error <- check_assay_yaml_validity(synid_project, center, synid_file_seq = synid_file_seq, strict = F)
res_yaml_warn <- check_assay_yaml_validity(synid_project, center, synid_file_seq = synid_file_seq, strict = T)
if (verbose) {
print(glue("assay_information.yaml valid (no errors): {res_yaml_error}"))
print(glue("assay_information.yaml valid (no errors or warnings): {res_yaml_warn}"))
}
if (res_yaml_error) {
res_seq <- check_seq_assay_id(synid_project, center, synid_file_seq = synid_file_seq,
synid_file_sam = synid_file_sam, fuzzy = T)
if (verbose) {
print(glue("number of seq_assay_ids absent form metadata: {length(res_seq)}"))
}
} else {
res_seq <- NULL
if (verbose) {
print(glue("yaml file invalid so not checking seq_assay_id metadata"))
}
}
res_pat <- check_pat_sam_id(synid_project, center, synid_file_pat = synid_file_pat, synid_file_sam = synid_file_sam)
if (verbose) {
print(glue("number of patient ids in sample not patient file: {length(res_pat)}"))
}
res_contact <- check_contact_info(synid_project, center, synid_file_pat = synid_file_pat)
if (verbose) {
print(glue("number of patients with inconsistent contact info: {length(res_contact)}"))
}
res_death <- check_death_info(synid_project, center, synid_file_pat = synid_file_pat)
if (verbose) {
print(glue("number of patients with inconsistent contact info: {length(res_death)}"))
}
if (!res_yaml_error) {
res_all <- rbind(res_all, cbind(NA, synid_file_seq, "yaml generates errors"))
}
if (res_yaml_error && !res_yaml_warn) {
res_all <- rbind(res_all, cbind(NA, synid_file_seq, "yaml generates warnings"))
}
if (!is.null(res_seq)) {
res_all <- rbind(res_all, cbind(NA, res_seq, "seq_assay_id in sample file but not assay_information.yaml"))
}
if (!is.null(res_pat)) {
res_all <- rbind(res_all, cbind(NA, res_pat, "patient IDs in sample but not patient file"))
}
if (!is.null(res_contact)) {
res_all <- rbind(res_all, cbind(NA, res_contact, "contact year and interval are inconsistently missing"))
}
if (!is.null(res_death)) {
res_all <- rbind(res_all, cbind(NA, res_death, "death year or interval or status are inconsistent"))
}
res_all[,"issue_no"] <- c(1:nrow(res_all))
# write --------------------------------
Sys.setenv(TZ='America/Los_Angeles')
file_output <- glue("{Sys.Date()}_{tolower(gsub(center, pattern = ' ', replacement = '_'))}.csv")
write.csv(res_all, file = file_output, row.names = F)
if (!is.null(synid_folder_output)) {
synid_file_output = save_to_synapse(path = file_output,
parent_id = synid_folder_output,
prov_name = "extra main genie checks",
prov_desc = "additional checks to perform on input files for main genie",
prov_used = c(synid_file_seq, synid_file_sam, synid_file_pat),
prov_exec = "https://github.com/hhunterzinck/genie_requests/blob/main/2021-12-29_tom_mg_checks.R")
file.remove(file_output)
}
# close out ----------------------------
if (verbose) {
print(glue("--------"))
print(glue("total number of issues flagged: {nrow(res_all)}"))
if (is.null(synid_folder_output)) {
print(glue("Results written locally as '{file_output}'."))
} else {
print(glue("Results uploaded to Synapse as '{synGet(synid_file_output, downloadFile = F)$properties$name}' (synid_file_output)."))
}
}
if (verbose) {
toc = as.double(Sys.time())
print(glue("Runtime: {round(toc - tic)} s"))
}