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 path2022-02-03_haley_cbio_null_start_dates.R
147 lines (118 loc) · 4.72 KB
/
2022-02-03_haley_cbio_null_start_dates.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
# Description: Remove rows with empty start dates from cbio treatment
# timelines files and re-upload.
# Author: Haley Hunter-Zinck
# Date: 2022-02-03
# setup ----------------------------
tic = as.double(Sys.time())
library(glue)
library(dplyr)
library(synapser)
synLogin()
# synapse
synid_files <- setNames(c("syn26349077", "syn26475421", "syn26990161"), c("panc","prostate","bladder"))
# parameters
file_name <- "data_timeline_treatment.txt"
# for checking
n_rm <- setNames(c(1,4,3), c("panc","prostate","bladder"))
# 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.
#' @param comment.char character designating comment lines to ignore
#' @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)
}
# read ----------------------------
raw <- list()
for (i in 1:length(synid_files)) {
cohort <- names(synid_files)[i]
raw[[cohort]] <- get_synapse_entity_data_in_csv(as.character(synid_files[cohort]), sep = "\t")
}
# main ----------------------------
mod <- list()
for (i in 1:length(raw)) {
mod[[names(raw)[i]]] <- raw[[i]] %>%
filter(!is.na(START_DATE))
}
# tests -----------------------------
res_check <- c()
for (i in 1:length(mod)) {
if (nrow(raw[[i]]) - nrow(mod[[i]]) == n_rm[names(mod[i])]) {
res_check[names(mod[i])] = T
} else {
res_check[names(mod[i])] = T
}
}
# write -------------------------------
if (all(res_check)) {
for (i in 1:length(mod)) {
cohort <- names(mod)[i]
write.table(mod[[i]], file = file_name, row.names = F, sep = "\t", na = "", quote = F)
synid_folder_output <- synGet(as.character(synid_files[cohort]), downloadFile = F)$properties$parentId
save_to_synapse(path = file_name,
parent_id = synid_folder_output,
prov_name = 'remove na start date',
prov_desc = 'removes any rows in which start date is missing',
prov_used = as.character(synid_files[cohort]),
prov_exec = "https://github.com/hhunterzinck/genie_requests/blob/main/2022-02-03_haley_cbio_null_start_dates.R")
}
}
# close out ----------------------------
toc = as.double(Sys.time())
print(glue("Runtime: {round(toc - tic)} s"))