2020-10-27 22:13:16 +01:00
|
|
|
source("renv/activate.R")
|
|
|
|
|
2021-11-30 17:08:07 +01:00
|
|
|
#library(RMariaDB)
|
2020-10-27 22:13:16 +01:00
|
|
|
library(stringr)
|
|
|
|
library(purrr)
|
|
|
|
library(readr)
|
|
|
|
library("dplyr", warn.conflicts = F)
|
|
|
|
|
|
|
|
config <- snakemake@params[["config"]]
|
|
|
|
group <- config$SOURCE$DATABASE_GROUP
|
|
|
|
timezone <- config$SOURCE$TIMEZONE
|
2021-03-25 16:39:31 +01:00
|
|
|
phone_device_id_column = "device_id"
|
|
|
|
fitbit_device_id_column = "fitbit_id"
|
|
|
|
empatica_device_id_column = "empatica_id"
|
2021-01-06 17:14:15 +01:00
|
|
|
add_phone_section = config$PHONE_SECTION$ADD
|
|
|
|
add_fitbit_section = config$FITBIT_SECTION$ADD
|
2021-02-21 23:30:30 +01:00
|
|
|
add_empatica_section = config$EMPATICA_SECTION$ADD
|
2020-10-27 22:13:16 +01:00
|
|
|
phone_ignored = config$PHONE_SECTION$IGNORED_DEVICE_IDS
|
|
|
|
fitbit_ignored = config$FITBIT_SECTION$IGNORED_DEVICE_IDS
|
2021-03-11 20:30:42 +01:00
|
|
|
empatica_ignored = config$EMPATICA_SECTION$IGNORED_DEVICE_IDS
|
2020-10-27 22:13:16 +01:00
|
|
|
|
|
|
|
rmysql.settingsfile <- "./.env"
|
|
|
|
|
2021-03-11 20:30:42 +01:00
|
|
|
participants <- read_csv(config$CSV_FILE_PATH, col_types=cols_only(device_id="c",pid="c",label="c",platform="c",
|
2021-03-25 16:39:31 +01:00
|
|
|
start_date=col_datetime(),end_date=col_datetime(),fitbit_id="c",empatica_id="c")) %>%
|
2021-03-11 20:30:42 +01:00
|
|
|
mutate(start_date = as.character(start_date), end_date = as.character(end_date)) # we read as date to validate format
|
|
|
|
participants <- participants %>%
|
2021-05-11 22:42:20 +02:00
|
|
|
mutate(!!phone_device_id_column := str_replace_all(!!rlang::sym(phone_device_id_column), ";",","),
|
|
|
|
!!fitbit_device_id_column := str_replace_all(!!rlang::sym(fitbit_device_id_column), ";",","),
|
|
|
|
!!empatica_device_id_column := str_replace_all(!!rlang::sym(empatica_device_id_column), ";",","),
|
|
|
|
platform = str_replace_all(platform, ";",","),
|
2021-03-11 20:30:42 +01:00
|
|
|
!!phone_device_id_column := if_else(!!rlang::sym(phone_device_id_column) %in% phone_ignored, NA_character_, !!rlang::sym(phone_device_id_column)),
|
|
|
|
!!empatica_device_id_column := if_else(!!rlang::sym(empatica_device_id_column) %in% empatica_ignored, NA_character_, !!rlang::sym(empatica_device_id_column)),
|
|
|
|
!!fitbit_device_id_column := if_else(!!rlang::sym(fitbit_device_id_column) %in% fitbit_ignored, NA_character_, !!rlang::sym(fitbit_device_id_column)))
|
2020-10-27 22:13:16 +01:00
|
|
|
|
2020-11-17 23:27:48 +01:00
|
|
|
dir.create(file.path("./data/external/participant_files/"))
|
|
|
|
|
2020-10-27 22:13:16 +01:00
|
|
|
participants %>%
|
|
|
|
pwalk(function(add_phone_section, add_fitbit_section, phone_device_id_column, fitbit_device_id_column, ...) {
|
|
|
|
empty_phone <- c("PHONE:", " DEVICE_IDS:", " PLATFORMS:"," LABEL:", " START_DATE:", " END_DATE:")
|
|
|
|
empty_fitbit <- c("FITBIT:", " DEVICE_IDS:", " LABEL:", " START_DATE:", " END_DATE:")
|
2021-02-21 23:30:30 +01:00
|
|
|
empty_empatica <- c("EMPATICA:", " LABEL:", " START_DATE:", " END_DATE:")
|
2020-10-27 22:13:16 +01:00
|
|
|
row <- tibble(...)
|
|
|
|
lines <- c()
|
2021-01-06 17:14:15 +01:00
|
|
|
start_date = if_else(is.na(row$start_date), "", row$start_date)
|
|
|
|
end_date = if_else(is.na(row$end_date), "", row$end_date)
|
2020-10-27 22:13:16 +01:00
|
|
|
|
|
|
|
if(add_phone_section == TRUE && !is.na(row[phone_device_id_column])){
|
|
|
|
lines <- append(lines, c("PHONE:", paste0(" DEVICE_IDS: [",row[phone_device_id_column],"]"), paste0(" PLATFORMS: [",row$platform,"]"),
|
2021-01-06 17:14:15 +01:00
|
|
|
paste(" LABEL:",row$label), paste(" START_DATE:", start_date), paste(" END_DATE:", end_date)))
|
2020-10-27 22:13:16 +01:00
|
|
|
}else
|
|
|
|
lines <- append(lines, empty_phone)
|
|
|
|
|
|
|
|
if(add_fitbit_section == TRUE && !is.na(row[fitbit_device_id_column])){
|
|
|
|
lines <- append(lines, c("FITBIT:", paste0(" DEVICE_IDS: [",row[fitbit_device_id_column],"]"),
|
2021-01-06 17:14:15 +01:00
|
|
|
paste(" LABEL:",row$label), paste(" START_DATE:", start_date), paste(" END_DATE:", end_date)))
|
2020-10-27 22:13:16 +01:00
|
|
|
} else
|
|
|
|
lines <- append(lines, empty_fitbit)
|
2021-02-21 23:30:30 +01:00
|
|
|
|
2021-03-11 20:30:42 +01:00
|
|
|
if(add_empatica_section == TRUE && !is.na(row[empatica_device_id_column])){
|
|
|
|
lines <- append(lines, c("EMPATICA:", paste0(" DEVICE_IDS: [",row[empatica_device_id_column],"]"),
|
2021-02-21 23:30:30 +01:00
|
|
|
paste(" LABEL:",row$label), paste(" START_DATE:", start_date), paste(" END_DATE:", end_date)))
|
|
|
|
} else
|
|
|
|
lines <- append(lines, empty_empatica)
|
2021-05-11 22:42:20 +02:00
|
|
|
lines <- append(lines, "\n")
|
|
|
|
|
2020-10-27 22:13:16 +01:00
|
|
|
file_connection <- file(paste0("./data/external/participant_files/", row$pid, ".yaml"))
|
|
|
|
writeLines(lines, file_connection)
|
|
|
|
close(file_connection)
|
|
|
|
|
2021-03-11 20:30:42 +01:00
|
|
|
}, add_phone_section, add_fitbit_section, phone_device_id_column, fitbit_device_id_column, empatica_device_id_column)
|
2020-10-28 19:11:41 +01:00
|
|
|
|
|
|
|
file_lines <-readLines("./config.yaml")
|
|
|
|
for (i in 1:length(file_lines)){
|
|
|
|
if(startsWith(file_lines[i], "PIDS:")){
|
2021-06-22 22:37:22 +02:00
|
|
|
file_lines[i] <- paste0("PIDS: ['", paste(participants$pid, collapse = "', '"), "']")
|
2020-10-28 19:11:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writeLines(file_lines, con = "./config.yaml")
|