-
Notifications
You must be signed in to change notification settings - Fork 0
/
BioChem_data_generation.R
261 lines (204 loc) · 9.25 KB
/
BioChem_data_generation.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
# Translate Davis Strait data to BioChem BCD/BCS format
library(librarian)
shelf(tidyverse, readr, readxl, here, oce )
# read in dictionary tables
bcd_dict <- readxl::read_xlsx(here('extdata', 'biochem_dictionary.xlsx'),
sheet = 'BCD')
expos <- read_csv('extdata/expocodes.csv', show_col_types = FALSE)
flags <- read_xlsx(here('extdata', 'flag_dictionary.xlsx'))
flag_definitions <- read_csv('extdata/flags.csv', show_col_types = FALSE)
data_type_seq_table <- read_csv('extdata/biochem_data_types.csv', show_col_types = FALSE)
# read in original data
data_fns <- choose.files(default = '../data', caption = "Select data files: ", multi = TRUE)
#data_fns <- list.files('../data/', pattern = '_EO.csv', full.names = TRUE)
# BCD ----
# loop for all data files
for (i in 1:length(data_fns)) {
data <- read_csv(data_fns[i], show_col_types = FALSE)
# TODO check for empty columns before translating
# translate columns ----
bcd_dict_y <- bcd_dict[bcd_dict$original %in% names(data),]
# Create a named vector from the dictionary
name_changes <- setNames(bcd_dict_y$biochem, bcd_dict_y$original)
# Check which columns in df are not in the dictionary
missing_cols <- setdiff(colnames(data), bcd_dict_y$original)
# If there are any such columns, remove them and print a warning
if (length(missing_cols) > 0) {
warning(paste("The following columns were not found in the dictionary and have been removed:",
paste(missing_cols, collapse = ", ")))
data <- data[, !(colnames(data) %in% missing_cols)]
}
# Use rename_with to rename the columns
data <- data %>% rename_with(~ name_changes[.x], everything())
# remove empty rows
if (anyNA(data$MISSION_NAME)){
data <- data[-which(is.na(data$MISSION_NAME)), ]
}
# gather metadata columns
bcd_meta <- data[names(data) %in% bcd_dict$biochem[bcd_dict$tag == 'metadata']]
# generate necessary columns ----
# DIS_SAMPLE_KEY_VALUE
# (mission_event_sampleID)
# check mission name - adjust to expocode
if (unique(bcd_meta$MISSION_NAME) %in% expos$cruise_name) {
bcd_meta$MISSION_NAME <- expos$biochem_name[expos$cruise_name == unique(bcd_meta$MISSION_NAME)]
bcd_meta$MISSION_DESCRIPTOR <- expos$MEDS[expos$biochem_name == unique(bcd_meta$MISSION_NAME)]
} else{
stop('EXPOCODE not found, MISSION_NAME was left in original condition! \n
No Mission Descriptor provided! \n
Please add an entry to extdata/expocodes.csv')
}
# pad event number to three digits
bcd_meta$EVENT_COLLECTOR_EVENT_ID <- str_pad(bcd_meta$EVENT_COLLECTOR_EVENT_ID,
width = 3,
pad = '0')
# generate dis_sample_key_value
bcd_meta <- bcd_meta %>%
dplyr::mutate(., DIS_SAMPLE_KEY_VALUE = paste0(
MISSION_NAME, '_', EVENT_COLLECTOR_EVENT_ID, '_', DIS_DETAIL_COLLECTOR_SAMP_ID
))
# EVENT_SDATE
bcd_meta$DIS_HEADER_SDATE <- make_date(bcd_meta$year, bcd_meta$month, bcd_meta$day)
# Convert the date to the format DD-MON-YY
bcd_meta$DIS_HEADER_SDATE <- format(bcd_meta$DIS_HEADER_SDATE, "%d-%b-%y")
# format time TODO Check format
# manual time reformat CAUTION
bcd_meta$DIS_HEADER_STIME <- as.numeric(str_sub(gsub(bcd_meta$DIS_HEADER_STIME, pattern = ':', replacement =''), 1, 4))
# bcd_meta$DIS_HEADER_STIME <- format(strptime(bcd_meta$DIS_HEADER_STIME, format = '%H:%M:%s', tz = 'UTC'), '%H%M')
# remove year month day columns
bcd_meta <- bcd_meta[-grep(names(bcd_meta), pattern = 'year')]
bcd_meta <- bcd_meta[-grep(names(bcd_meta), pattern = 'month')]
bcd_meta <- bcd_meta[-grep(names(bcd_meta), pattern = 'day')]
# remove mission_name column
bcd_meta <- bcd_meta[-grep(names(bcd_meta), pattern = 'MISSION_NAME')]
# CREATED_DATE
bcd_meta$CREATED_DATE <- format(Sys.Date(), "%d-%b-%y")
# fill in standard values
# # MISSION_INSTITUTE = DFO BIO
# bcd_meta$MISSION_INSTITUTE <- 'DFO-BIO'
# CREATED_BY = EMILY OGRADY
bcd_meta$CREATED_BY <- "Emily O'Grady"
# DATA_CENTER_CODE = 20
bcd_meta$DATA_CENTER_CODE <- '20'
# PROCESS_FLAG = NR
bcd_meta$PROCESS_FLAG <- 'NR'
# batch_seq = 1
bcd_meta$BATCH_SEQ <- '1'
# reformat to long data (with flags) ----
bcd_data <- data[names(data) %in% bcd_dict$biochem[bcd_dict$tag != 'metadata']]
bcd_wide <- cbind(bcd_meta, bcd_data)
# separate column types
data_cols <- names(bcd_wide)[
which(names(bcd_wide) %in% bcd_dict$biochem[bcd_dict$tag %in% c('btl', 'ctd')])
]
qc_cols <- names(bcd_wide)[
which(names(bcd_wide) %in% bcd_dict$biochem[bcd_dict$tag == 'qc'])
]
metadata_cols <- names(bcd_wide)[
-which(names(bcd_wide) %in% bcd_dict$biochem[bcd_dict$tag %in% c('ctd', 'btl', 'qc')])
]
# make all data columns numeric
bcd_wide <- bcd_wide %>%
mutate_at(vars(data_cols), as.numeric)
# Separate data and qc columns into two dataframes
data_df <- bcd_wide %>% select(all_of(c(metadata_cols, data_cols)))
qc_df <- bcd_wide %>% select(all_of(c(metadata_cols, qc_cols)))
# Pivot both dataframes to long format
data_long <- data_df %>% pivot_longer(cols = all_of(data_cols),
names_to = "DATA_TYPE_METHOD",
values_to = "DIS_DETAIL_DATA_VALUE")
qc_long <- qc_df %>% pivot_longer(cols = all_of(qc_cols),
names_to = "DATA_TYPE_METHOD",
values_to = "DIS_DETAIL_DATA_QC_CODE")
# Remove the '_qc' suffix from the METHOD column in the qc_long dataframe
qc_long$DATA_TYPE_METHOD <- str_remove(qc_long$DATA_TYPE_METHOD, "_qc")
# Join the data and qc dataframes
bcd_long <- left_join(data_long, qc_long, by = c("DATA_TYPE_METHOD",
#"DIS_SAMPLE_KEY_VALUE"
as.character(metadata_cols)
))
# remove any NA data rows
bcd_long <- bcd_long %>%
dplyr::filter(!is.na(DIS_DETAIL_DATA_VALUE))
# Add more metadata in long format ----
# Depth
# calculate from pressure
pressure_data <- bcd_long %>%
filter(., DATA_TYPE_METHOD == 'Pressure') %>%
select(., DIS_SAMPLE_KEY_VALUE, DIS_DETAIL_DATA_VALUE)
pressure_data$DIS_HEADER_START_DEPTH <- oce::swDepth(pressure_data$DIS_DETAIL_DATA_VALUE,
latitude = bcd_long$DIS_HEADER_SLON[1])
pressure_data$DIS_HEADER_END_DEPTH <- oce::swDepth(pressure_data$DIS_DETAIL_DATA_VALUE,
latitude = bcd_long$DIS_HEADER_SLON[1])
pressure_data <- pressure_data %>%
select(-DIS_DETAIL_DATA_VALUE) %>%
distinct(., DIS_SAMPLE_KEY_VALUE, .keep_all = TRUE) %>%
mutate(DIS_HEADER_START_DEPTH = round(DIS_HEADER_START_DEPTH, 0)) %>%
mutate(DIS_HEADER_END_DEPTH = round(DIS_HEADER_END_DEPTH, 0))
if (min(pressure_data$DIS_HEADER_START_DEPTH, na.rm = TRUE) < 0) {
# zero out any erroneously negative depth values
pressure_data$DIS_HEADER_START_DEPTH[pressure_data$DIS_HEADER_START_DEPTH < 0] <- 0
pressure_data$DIS_HEADER_END_DEPTH[pressure_data$DIS_HEADER_END_DEPTH < 0] <- 0
}
# join depth data
bcd_final <- bcd_long %>%
dplyr::left_join(pressure_data)
# clean up
remove(pressure_data, data_long, qc_long, data_df, qc_df)
# get dis_data_type_seq
for (ii in 1:length(bcd_final$DATA_TYPE_METHOD)) {
bcd_final$DIS_DETAIL_DATA_TYPE_SEQ[ii] <- as.character(data_type_seq_table$DATA_TYPE_SEQ[data_type_seq_table$METHOD == bcd_final$DATA_TYPE_METHOD[ii]])
}
# add DIS_DATA_NUM
bcd_final$DIS_DATA_NUM <- seq(1:nrow(bcd_final))
# STOP
# translate flags ----
for (ii in 1:length(bcd_final$DIS_DETAIL_DATA_QC_CODE)) {
lab_qc <- bcd_final$DIS_DETAIL_DATA_QC_CODE[ii]
method <- bcd_final$DATA_TYPE_METHOD[ii]
if (!is.na(lab_qc) && nchar(lab_qc) < 4) { # grab biochem flag from dictionary if lab flag exists
bc_qc <- flags$BioChem[grep(flags$lab, pattern = str_sub(lab_qc, 1, 2))]
} else {
if (is.na(lab_qc)) { # if no flag then assume acceptable
# find method - ctd should stay with a 0 QC, btl can get a 1
if (method %in% bcd_dict_y$biochem[bcd_dict_y$tag == 'btl']){
bc_qc <- 1
} else {
bc_qc <- 0
}
} else if (nchar(lab_qc) > 4) { # if there is text ask for user translation
bc_qc <- menu(choices = flag_definitions$biochem_definition[flag_definitions$flag > 0 ],
title = paste("Translate this QC to BioChem flag: \n", lab_qc))
}
}
bcd_final$DIS_DETAIL_DATA_QC_CODE[ii] <- bc_qc
}
# order columns
bcd_columns <- c('DIS_DATA_NUM',
'MISSION_DESCRIPTOR',
'EVENT_COLLECTOR_EVENT_ID',
'EVENT_COLLECTOR_STN_NAME',
'DIS_HEADER_START_DEPTH',
'DIS_HEADER_END_DEPTH',
'DIS_HEADER_SLAT',
'DIS_HEADER_SLON',
'DIS_HEADER_SDATE',
'DIS_HEADER_STIME',
'DIS_DETAIL_DATA_TYPE_SEQ',
'DATA_TYPE_METHOD',
'DIS_DETAIL_DATA_VALUE',
'DIS_DETAIL_DATA_QC_CODE',
'DIS_DETAIL_COLLECTOR_SAMP_ID',
'CREATED_BY',
'CREATED_DATE',
'DATA_CENTER_CODE',
'PROCESS_FLAG',
'BATCH_SEQ',
'DIS_SAMPLE_KEY_VALUE'
)
bcd_export <- bcd_final %>%
select(all_of(bcd_columns))
# export data file
bcd_name <- file.path('../data/BioChem/', paste0(unique(bcd_export$MISSION_DESCRIPTOR), '_BCD.csv'))
write.csv(bcd_export, bcd_name, row.names = FALSE)
}