-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABCD_Harmonization_controlled.R
246 lines (166 loc) · 7.14 KB
/
ABCD_Harmonization_controlled.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
# Author: Dominik Kraft
# This code performs data loading and handling of the ABCD data and subsequently performs
# Combat, Covbat, and Relief harmonization and saves raw/combat/covbat/relief data files for .py scripts
# This code outputs results from the "controlled setting" - see manuscript for additional information.
rm(list = ls())
# set datapath !
DATAPATH = "/path_to/abcd-data-release-5.0/core"
set.seed(123)
date=format(Sys.time(), "_%Y-%m-%d_%H%M")
# import packages
library(neuroCombat)
library(RELIEF)
library(CovBat)
library(data.table)
library(dplyr)
library(tidyr)
# loading demographics
demo1<-fread(file.path(DATAPATH, "abcd-general/abcd_p_demo.csv"), data.table = F)
demo2<-fread(file.path(DATAPATH, "abcd-general/abcd_y_lt.csv"), data.table = F)
demo3<-fread(file.path(DATAPATH, "imaging/mri_y_adm_info.csv"), data.table = F)
#select subject, age, sex and scanner site / manufacturer
demo1<-demo1[, c(1:2, 9)]
demo2<-demo2[, c(1:2, 9)]
demo3<-demo3[, c(1:2, 5:6)]
# choose only scanners from SIEMENS (prisma,fit) and GE (Discovery) for simulation part
demo3_filtered <- demo3[demo3$mri_info_manufacturersmn %in% list("Prisma", "Prisma_fit", "DISCOVERY MR750"),]
#merge demos
demo<-Reduce(function(x,y) merge(x, y, by= c("src_subject_id", "eventname")),
list( demo1, demo2, demo3_filtered))
# perform some data wrangling, re-naming etc. on demo
demo<-rename(demo, subject = src_subject_id)
demo$eventname <- gsub("(?<=^|_)([a-z])", "\\U\\1", demo$eventname, perl=TRUE)
demo$eventname<- gsub("_", "", demo$eventname)
demo$subject<- gsub("_", "", demo$subject)
demo<-rename(demo, session = eventname)
demo<-rename(demo, age = interview_age)
demo<-rename(demo,sex = demo_sex_v2)
demo<-rename(demo, site = mri_info_deviceserialnumber)
demo<-rename(demo, manufacturer = mri_info_manufacturersmn)
demo<- demo %>%
group_by(subject) %>%
fill(sex, .direction = "downup") %>%
ungroup
## select only baseline data
baseline <- demo[demo$session == "BaselineYear1Arm1", ]
# select only male / female data
baseline <- baseline[baseline$sex %in% list(1,2),]
# define function for loading and processing imaging data
process_data <- function(file_path) {
# Load data
data <- fread(file_path, data.table = FALSE)
# Clean up eventname column
data$eventname <- gsub("(?<=^|_)([a-z])", "\\U\\1", data$eventname, perl=TRUE)
data$eventname <- gsub("_", "", data$eventname)
# Rename columns
data <- rename(data, subject = src_subject_id)
data$subject <- gsub("_", "", data$subject)
data <- rename(data, session = eventname)
# Filter rows for the desired session
data <- data[data$session == "BaselineYear1Arm1", ]
# Ignore columns with "all" for fa and md
data <- data[, !grepl("all", names(data))]
# Omit rows with NaN values
data <- na.omit(data)
return(data)
}
# fa = fractional anisotropy, md = mean diffusivity
fa <- process_data(file.path(DATAPATH, "imaging/mri_y_dti_fa_is_at.csv"))
md <- process_data(file.path(DATAPATH, "imaging/mri_y_dti_md_is_at.csv"))
## merge demo and brain dataframes (fa, md)
df1<-Reduce(function(x,y) merge(x, y, by= c("subject", "session")),
list(baseline,fa))
df2<-Reduce(function(x,y) merge(x, y, by= c("subject", "session")),
list(baseline,md))
# SUBSAMPLING
# 1) select sites with >= 500 subjects
df1_filtered <- subset(df1, table(site)[as.character(site)] > 500)
df2_filtered <- subset(df2, table(site)[as.character(site)] > 500)
# select n=500 subjects from each site (some sites contain > 500 subjects)
# sample from df1
df1_sampled <- df1_filtered %>%
group_split(site) %>%
lapply(function(group) sample_n(group, size = 500, replace=FALSE)) %>%
bind_rows() %>%
arrange(subject)
# choose same subjects from df2
df2_sampled <- df2_filtered %>%
filter(subject %in% df1_sampled$subject) %>%
arrange(subject)
stopifnot(identical(df1_sampled$subject, df2_sampled$subject))
# define function that samples different sample sizes from dataframes
# note: all sampling is always done per scanning site / group
sampling <- function(df1, df2, sizes) {
result_list <- list()
for (size in sizes) {
# Sample from df1
df1_sampled <- df1 %>%
group_split(site) %>%
lapply(function(group) sample_n(group, size = size, replace = FALSE)) %>%
bind_rows() %>%
arrange(subject)
# choose same subjects from df2
df2_sampled <- df2 %>%
filter(subject %in% df1_sampled$subject) %>%
arrange(subject)
# Check if subjects are identical
stopifnot(identical(df1_sampled$subject, df2_sampled$subject))
# Store the results in a list
result_list[[paste0("n_", size)]] <- list(df1_sampled = df1_sampled, df2_sampled = df2_sampled)
}
return(result_list)
}
# Specify sample sizes
sample_sizes <- c(50, 100, 200, 300, 400, 500)
# perform sampling
result <- sampling(df1_sampled, df2_sampled, sample_sizes)
# define harmonization function
harmonization_function <- function(data_frame, output_prefix, size, write_output=TRUE) {
# Convert 'sex' column to factor
#data_frame$sex <- as.factor(data_frame$sex)
# Select covariates
covar <- data_frame[, c(1:6)]
# Extract 'site' for batch
batch <- covar$site
# Extract data for fa/md
data <- data_frame[, 7:43]
# Transform data
data <- t(data)
# Define covariates
modcovar <- model.matrix(~ as.factor(sex) + age, data = covar)
# Combat harmonization
combat_result <- neuroCombat(dat = data, batch = batch, mod = modcovar)
datcombat <- t(as.data.frame(combat_result$dat.combat))
combat_harmonized <- cbind(covar, datcombat)
# Relief harmonization
relief_result <- relief(dat = data, batch = batch, mod = modcovar)
datrelief <- t(as.data.frame(relief_result$dat.relief))
relief_harmonized <- cbind(covar, datrelief)
# CovBat harmonization
covbat_result <- covbat(dat = data, bat = batch, mod = modcovar)
datcovbat <- t(as.data.frame(covbat_result$dat.covbat))
covbat_harmonized <- cbind(covar, datcovbat)
result_list <- list(
combat = combat_harmonized,
covbat = covbat_harmonized,
relief = relief_harmonized)
# Write tables
if (write_output) {
write.table(data_frame, file = paste0("raw_", output_prefix, "_n", size, "_", date, ".csv"), sep = ",", row.names = FALSE)
write.table(combat_harmonized, file = paste0("combat_", output_prefix,"_n", size, "_", date, ".csv"), sep = ",", row.names = FALSE)
write.table(relief_harmonized, file = paste0("relief_", output_prefix, "_n", size, "_",date, ".csv"), sep = ",", row.names = FALSE)
write.table(covbat_harmonized, file = paste0("covbat_", output_prefix, "_n", size, "_",date, ".csv"), sep = ",", row.names = FALSE)
}
return(result_list)
}
# perform harmonization and save files
for (si in sample_sizes) {
print(si)
# Access df1 and df2 for the current sample size
current_result <- result[[paste0("n_", si)]]
fa_sampled <- current_result$df1_sampled
md_sampled <- current_result$df2_sampled
fa_result_sim <- harmonization_function(fa_sampled, "fa", si, write_output = TRUE)
md_result_sim <- harmonization_function(md_sampled, "md", si, write_output = TRUE)
}
print(date) # get date for filename