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-10-20_shawn_drug_masking_blanket_all_cohorts.R
199 lines (153 loc) · 5.79 KB
/
2021-10-20_shawn_drug_masking_blanket_all_cohorts.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
# Description: For regimens marked as being a part of a clincial trial, what number
# of unique drug names are unmasked?
# Author: Haley Hunter-Zinck
# Date: 2021-10-20
# setup ----------------------------
tic = as.double(Sys.time())
library(glue)
library(dplyr)
library(synapser)
synLogin()
# synapse
synid_folder_output <- "syn26380175"
synid_folders <- c()
synid_folders["BrCa"]<- "syn26195593"
synid_folders["CRC"]<- "syn26132942"
synid_folders["Prostate"] <- "syn26256984"
synid_folders["NSCLC"] <- "syn26380225"
# functions ----------------------------
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)
}
#' 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) {
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)
return(data)
}
get_synapse_entity_name <- function(synapse_id) {
return(synGet(synapse_id, downloadFile = F)$properties$name)
}
get_site <- function(x) {
if (length(grep(pattern = "DFCI", x = x))) {
return ("DFCI")
}
if (length(grep(pattern = "MSK", x = x))) {
return ("MSK")
}
if (length(grep(pattern = "UHN", x = x))) {
return ("UHN")
}
return("VICC")
}
get_number_ct_unmasked <- function(data) {
reg_ct <- as.character(unlist(data %>%
filter(drugs_ct_yn == "Yes") %>%
select(drugs_in_regimen)))
drugs_ct <- unlist(strsplit(reg_ct, split = ", "))
return(table(drugs_ct))
}
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(T)
}
# read ----------------------------
datas <- list()
for (i in 1:length(synid_folders)) {
synid_folder_children <- get_synapse_folder_children(as.character(synid_folders[i]),
include_types = list("file"))
idx_synid_files_csv <- grep(pattern = "\\.csv$", x = names(synid_folder_children),
value = F)
datas[[names(synid_folders)[i]]] <- list()
for (idx in idx_synid_files_csv) {
site <- get_site(names(synid_folder_children)[idx])
datas[[names(synid_folders)[i]]][[site]] <- get_synapse_entity_data_in_csv(as.character(synid_folder_children[idx]))
}
}
# main ----------------------------
cohorts <- sort(names(datas))
sites <- sort(unique(unlist(lapply(datas, names))))
res <- list()
for (site in sites) {
res[[site]] <- list()
res_cohort <- list()
for (cohort in cohorts) {
data <- datas[[cohort]][[site]]
if (!is.null(data)) {
res_cohort[[cohort]] <- get_number_ct_unmasked(data)
}
}
drugs <- sort(unique(unlist(lapply(res_cohort, names))))
res[[site]] <- matrix(0, nrow = length(drugs), ncol = length(cohorts),
dimnames = list(drugs, cohorts))
for (cohort in cohorts) {
if (is.null(datas[[cohort]][[site]])) {
res[[site]][,cohort] <- NA
} else {
res[[site]][match(names(res_cohort[[cohort]]), drugs), cohort] <- res_cohort[[cohort]]
}
}
}
# write --------------------------------
for (site in names(res)) {
file_output <- glue("{tolower(site)}_unmasked_drug_count_in_ct_regimen.csv")
write.csv(res[[site]], file = file_output)
save_to_synapse(path = file_output,
parent_id = synid_folder_output,
prov_name = "count unmasked drugs",
prov_desc = "Number of unmasked drug name instances in regimens marked as in a clinical trial",
prov_used = as.character(synid_folders),
prov_exec = "https://github.com/hhunterzinck/genie_requests/blob/main/2021-10-20_shawn_drug_masking_blanket_all_cohorts.R")
file.remove(file_output)
}
# close out ----------------------------
toc = as.double(Sys.time())
print(glue("Runtime: {round(toc - tic)} s"))