-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
221 lines (189 loc) · 6.54 KB
/
main.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
# Folder where these scripts will be installed in the server
kWorkingDirectory <- "/home/maramirez/Proyectos/ICARIA/r_icaria_etl/"
setwd(kWorkingDirectory)
source("tokens.R")
source("etl.R")
# READ MODEL (ENTITIES) --------------------------------------------------------
participant <- read.csv(
file = "participant.csv",
stringsAsFactors = F,
strip.white = T,
comment.char = "#"
)
azi <- read.csv(
file = "azi.csv",
stringsAsFactors = F,
strip.white = T,
comment.char = "#"
)
withdrawal <- read.csv(
file = "withdrawal.csv",
stringsAsFactors = F,
strip.white = T,
comment.char = "#"
)
death <- read.csv(
file = "death.csv",
stringsAsFactors = F,
strip.white = T,
comment.char = "#"
)
sae <- read.csv(
file = "sae.csv",
stringsAsFactors = F,
strip.white = T,
comment.char = "#"
)
household <- read.csv(
file = "household.csv",
stringsAsFactors = F,
strip.white = T,
comment.char = "#"
)
screening.log <- read.csv(
file = "screening_log.csv",
stringsAsFactors = F,
strip.white = T,
comment.char = "#"
)
model.trial <- rbind(participant, azi, withdrawal, death, sae, household)
model.log <- rbind(screening.log)
# EXTRACT DATA -----------------------------------------------------------------
# Extract data from all databases (one per ICARIA HF) accroding to the data
# model to be produced
db.filter <- which(model.trial$source == "db")
db.variables <- model.trial$type[db.filter]
names(db.variables) <- model.trial$variable[db.filter]
data <- ExportDataAllHealthFacilities(
redcap.api.url = kRedcapAPIURL,
redcap.tokens = kRedcapTokens,
variables = names(db.variables),
# TODO: Force interpretation of column types to improve efficiency while
# reading and bad type casting
types = NA
)
# Extract data from the screening log database according to the data model to be
# produced
db.filter <- which(model.log$source == "db")
db.variables <- model.log$type[db.filter]
names(db.variables) <- model.log$variable[db.filter]
log <- ExportDataScreeningLog(
redcap.api.url = kRedcapAPIURL,
redcap.token = kRedcapTokens[["profile"]],
variables = names(db.variables),
# TODO: Force interpretation of column types to improve efficiency while
# reading and bad type casting
types = NA
)
# TRANSFORM DATA ---------------------------------------------------------------
data <- TransformRemoveEmptyRows(data)
# TODO: This transformation won't be required once we export data forcing types
# as this variables will be exported as a character with the corresponding
# leading zeros.
data <- TransformAddLeadingZeros(data, "screening_number", 5)
data <- TransformAddICD10Description(
data = data,
icd.10.column = "sae_icd_10",
new.desc.column = "sae_icd_10_desc",
bioportal.api.url = kBioportalAPIURL,
bioportal.api.key = kBioportalAPIKey
)
log <- TransformCollapseColumns(
data = log,
columns = c("hf_bombali", "hf_tonkolili", "hf_port_loko"),
new.column = "hf"
)
log <- TransformAddLeadingZeros(log, "hf", 2)
log <- TransformAddPrefix(log, "hf", "HF")
# ORGANIZE DATA (TABLES): PARTICIPANTS AND SAES --------------------------------
# Get screening variables for all participants (recruited + screening failures)
participants <- data[which(!is.na(data$screening_number)), participant$variable]
# Merge withdrawal and death data to the participants table
withdrawals <- data[which(!is.na(data$wdrawal_reported_date)),
c("hf", "record_id", withdrawal$variable)]
participants <- merge(
x = participants,
y = withdrawals,
by = c("hf", "record_id"),
all.x = T
)
deaths <- data[which(!is.na(data$death_reported_date)),
c("hf", "record_id", death$variable)]
participants <- merge(
x = participants,
y = deaths,
by = c("hf", "record_id"),
all.x = T
)
# Get all AZi/Pbo doses by participant in a one-row-per-participant fashion and
# merge these data to the participants table
azi.doses <- TransformPivotAZiVars(data)
participants <- merge(
x = participants,
y = azi.doses,
by = c("hf", "record_id"),
all.x = T
)
# Merge last household visit at the end of the follow up,
# i.e. event HH-At 18th month of age
end.fu.visits <- data[
which(data$redcap_event_name == "hhat_18th_month_of_arm_1"),
c("hf", "record_id", household$variable)]
participants <- merge(
x = participants,
y = end.fu.visits,
by = c("hf", "record_id"),
all.x = T
)
# Order participants columns and rows
participants$record_id <- as.integer(participants$record_id)
participants <- participants[order(participants$hf, participants$record_id), ]
# Get SAE information to create the SAEs table
saes <- data[which(data$sae_complete == 2), c("hf", "record_id", sae$variable)]
saes$study_number <- substr(saes$sae_number, 1, 9)
# Order saes columns and rows
saes$record_id <- as.integer(saes$record_id)
saes <- saes[order(saes$hf, saes$record_id),
c("hf", "record_id", "study_number", sae$variable)]
# Get Screening Log information to create the Screening Log table
logs <- log[which(!is.na(log$screening_date)),
screening.log$variable[which(screening.log$load == 1)]]
# LOAD DATA --------------------------------------------------------------------
kVersionFormat <- "%Y%m%d"
kCSVExtension <- ".csv"
kParticipantsFile <- "participants"
kSAEsFile <- "saes"
kLogFile <- "screening_log"
kDriveDataPath <- "Data"
data.date <- Sys.time()
# Create data files
participants.filename <- paste0(
kParticipantsFile,
"_",
format(data.date, format = kVersionFormat),
kCSVExtension
)
write.csv(participants, file = participants.filename, row.names = F)
saes.filename <- paste0(
kSAEsFile,
"_",
format(data.date, format = kVersionFormat),
kCSVExtension
)
write.csv(saes, file = saes.filename, row.names = F)
log.filename <- paste0(
kLogFile,
"_",
format(data.date, format = kVersionFormat),
kCSVExtension
)
write.csv(logs, file = log.filename, row.names = F)
# Load data files
LoadAuthorize(kGoogleServiceAccountToken)
LoadDataFile(data.date, kDriveDataPath, kParticipantsFile, participants.filename)
LoadDataFile(data.date, kDriveDataPath, kSAEsFile, saes.filename)
LoadDataFile(data.date, kDriveDataPath, kLogFile, log.filename)
# Remove local data files as they were already uploaded to Drive
file.remove(participants.filename)
file.remove(saes.filename)
file.remove(log.filename)