-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTidy ROTEM Sigma script.R
151 lines (107 loc) · 7.59 KB
/
Tidy ROTEM Sigma script.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
## Matthew Miller (C) 2019
##
## This program is WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. It has been written for research/academic non-commercial use.
#if you do not have pacman installed run :install.packages("pacman")
library(pacman)
p_load(tidyverse, checkmate, stringr, lubridate, gdata, readxl, gmodels, naniar, openxlsx, ggpubr, janitor, skimr, REDCapR, hablar, mefa)
# STEP 1 ------------------------------------------------------------------
# load txt file, clean names, remove * ------------------------------------
path_to_file = file.path("input the path to your ROTEM sigma .txt file") # to enter the filepath correctly
# you dont need to enter the \ , just enter the folders like ("C:", "ROTEM" , "Backups" , "filename.txt")
rotem <- read_delim(path_to_file, "\t", escape_double = FALSE, trim_ws = TRUE)%>%
clean_names()%>%
select(-image_filename, -sex, -birth_date, -te_mogram_link, -starter, -stopper,-lot, -comment, -saved_by,- channel, -error_code, -flags, -serial_nr_cartridge, -profile)%>%
mutate_all(funs(str_replace(., "[*]", "")))%>%
convert(num(ct,a5,a10,a20,a30,cft,mcf,li30,li45,li60,ml))%>%
# the text in brackets below lets you filter out names of test or calibration ROTEMS
filter(patient_name != "training, training", patient_name != "ABC, DEF", patient_name != "demo, demo")%>%
mutate(start_time = ymd_hms(start_time))%>%
arrange(start_time)%>%
mutate(uni_id = case_when(
test_name == "fibtem c" & lag(test_name == "aptem c") ~ str_c(patient_id, hour(start_time), minute(start_time), sep = "-"),
test_name == "extem c" & lag(test_name == "fibtem c") ~ str_c(lag(patient_id), lag(hour(start_time)), lag(minute(start_time)), sep = "-"),
test_name == "intem c" & lag(test_name == "extem c") ~ str_c(lag(n=2, patient_id), lag(n=2,hour(start_time)), lag(n=2,minute(start_time)), sep = "-"),
test_name == "aptem c" & lag(test_name == "intem c") ~ str_c(lag(n=3, patient_id), lag(n=3,hour(start_time)), lag(n=3,minute(start_time)), sep = "-"),
test_name == "heptem c" & lag(test_name == "intem c") ~ str_c(lag(n=3, patient_id), lag(n=3,hour(start_time)), lag(n=3,minute(start_time)), sep = "-"))) %>%
fill(uni_id, .direction = "up") %>%
mutate(rotem_start_time = if_else(test_name == "fibtem c", str_c(start_time), NA_character_, NA_character_))%>%
mutate(rotem_run_time = if_else(test_name == "fibtem c", str_c(run_time), NA_character_, NA_character_))%>%
mutate(rotem_start_time = fill.na(rotem_start_time))%>%
mutate(rotem_run_time = fill.na(rotem_run_time))
# STEP 2 ------------------------------------------------------------------
#make it wide, this essentially creates the tidy data
wide_rotem <- rotem%>%
group_by(uni_id)%>%
pivot_wider(names_from = test_name, values_from = c(start_time, run_time, ct,a5,a10,a20,a30,cft,mcf,li30,li45,li60,ml))
# STEP 3 ------------------------------------------------------------------
#define as likely cardiac or not
# only run this if you want this as an extra column for later
rotem_final <- wide_rotem%>%
mutate(possible_cardiac = if_else(is.na(`start_time_heptem c`), "no", "yes", "no"))%>%
mutate(patient_name = "")%>%
clean_names()
# STEP 4 ------------------------------------------------------------------
# upload to redcap --------------------------------------------------------
uri <- "input your REDCAP URI"
api_key <- "input your RECAP project API key"
#create new record
redcap_write(ds=rotem_final, redcap_uri=uri, token=api_key)
# STEP 5 ------------------------------------------------------------------
# upload images -----------------------------------------------------------
image_upload <- rotem_final%>%
select(uni_id, patient_id, rotem_start_time, start_time_fibtem_c, start_time_extem_c, start_time_intem_c,
start_time_heptem_c, start_time_aptem_c)%>%
mutate(patient_id = str_replace(patient_id, ".STG", "STG"))%>%#this is specific to my hospital, used to get rid of the '.' in the MRN.
mutate(patient_id = str_replace(patient_id, ".stg", "stg"))%>%
mutate(file.year = str_sub(year(rotem_start_time), -2),
file.month = str_pad(month(rotem_start_time), width = 2, side = ("left"), pad = "0"),
file.day = str_pad(day(rotem_start_time), width = 2, side = ("left"), pad = "0")) %>%
mutate(heptem.fn = str_c(patient_id, "_heptem c_", file.year, file.month, file.day,
str_pad(hour(start_time_heptem_c),width = 2, side = ("left"), pad = "0"),
str_pad(minute(start_time_heptem_c),width = 2, side = ("left"), pad = "0"),
str_pad(second(start_time_heptem_c),width = 2, side = ("left"), pad = "0"),
"000", ".jpg"),
intem.fn = str_c(patient_id, "_intem c_", file.year, file.month, file.day,
str_pad(hour(start_time_intem_c),width = 2, side = ("left"), pad = "0"),
str_pad(minute(start_time_intem_c),width = 2, side = ("left"), pad = "0"),
str_pad(second(start_time_intem_c),width = 2, side = ("left"), pad = "0"),
"000",".jpg"),
extem.fn = str_c(patient_id, "_extem c_", file.year, file.month, file.day,
str_pad(hour(start_time_extem_c),width = 2, side = ("left"), pad = "0"),
str_pad(minute(start_time_extem_c),width = 2, side = ("left"), pad = "0"),
str_pad(second(start_time_extem_c),width = 2, side = ("left"), pad = "0"),
"000",".jpg"),
fibtem.fn = str_c(patient_id, "_fibtem c_", file.year, file.month, file.day,
str_pad(hour(start_time_fibtem_c),width = 2, side = ("left"), pad = "0"),
str_pad(minute(start_time_fibtem_c),width = 2, side = ("left"), pad = "0"),
str_pad(second(start_time_fibtem_c),width = 2, side = ("left"), pad = "0"),
"000",".jpg"),
aptem.fn = str_c(patient_id, "_aptem c_", file.year, file.month, file.day,
str_pad(hour(start_time_aptem_c),width = 2, side = ("left"), pad = "0"),
str_pad(minute(start_time_aptem_c),width = 2, side = ("left"), pad = "0"),
str_pad(second(start_time_aptem_c),width = 2, side = ("left"), pad = "0"),
"000",".jpg")) %>%
select(uni_id, heptem.fn, intem.fn, extem.fn, fibtem.fn, aptem.fn) %>%
pivot_longer(-uni_id, names_to= "test", values_to = "filename") %>%
na.omit()
fn <- image_upload$filename
rid <- image_upload$uni_id
rotemImageUpload <- function(x, y) {
file.name <- x
record.id <- y
file_path <- file.path("path to the folder with the image files", file.name)
field <- if_else(str_detect(file.name,"fibtem"), "fibtem_image",
if_else(str_detect(file.name,"aptem"), "aptem_image",
if_else(str_detect(file.name,"intem"), "intem_image",
if_else(str_detect(file.name,"extem"), "extem_image",
if_else(str_detect(file.name,"heptem"), "heptem_image", NA_character_ , NA_character_)))))
images_in_folder <- list.files(file.path("path to the folder with the image files"))
if (any(grepl(file.name,images_in_folder)) == TRUE) {
redcap_upload_file_oneshot(file_name = file_path, record = record.id, field = field,
redcap_uri = uri, token = api_key)
file.move.path <- file.path("path to the folder with the image files", file.name)
file.rename(file_path, file.move.path)
}
}
map2(.x=fn, .y=rid, .f=rotemImageUpload)