-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate_data_NIFTI.R
More file actions
289 lines (243 loc) · 8.99 KB
/
simulate_data_NIFTI.R
File metadata and controls
289 lines (243 loc) · 8.99 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
args <- commandArgs(trailingOnly=TRUE)
args <- grep('--args', unlist(strsplit(args, ' ')), value = TRUE, invert = TRUE)
if(length(args) < 5){
cat("No arguments supplied. Positional arguments: \n
[sample_nifti] : sample NIFTI file to use as template \n
[BS_dir] : BrainSuite directory \n
[ROI_mask] : ROI mask volume \n
[output_dir] : output directory \n
[datatype] : type of data (var2_roi1_interaction, var3_roi1_all, var3_roi2_nointeraction, var3_roi1_nosig) \n
"
)
quit()
}else{
print(args)
}
sample_nii <- args[1]
BS_dir <- args[2]
ROI_mask <- args[3]
odir <- args[4]
datatype <- args[5]
.libPaths(c( .libPaths(), "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"))
#require(SyncRNG)
require(oro.nifti)
#require(data.frame)
# load sample file
sample <- oro.nifti::readNIfTI(sample_nii)
# load brain mask file
brainmask <- oro.nifti::readNIfTI(paste(BS_dir,'/svreg/BrainSuiteAtlas1/mri.mask.nii.gz', sep=""))
brainmask.idx <- which(brainmask == 255, arr.ind = T)
nonbrainmask.idx <- which(brainmask != 255, arr.ind = T)
#extract set sig roi
roilabel <- oro.nifti::readNIfTI(ROI_mask)
roilabel.idx <- which(roilabel == 1, arr.ind = T)
roilabel.idx2 <- which(roilabel == 2, arr.ind = T)
####### make data for 1 significant roi, testing interaction effect (group*continuous)
if(datatype == "var2_roi1_interaction"){
dir.create(paste(odir, '/Simulated_Data_var2_roi1_interaction', sep=""))
setwd(paste(odir, '/Simulated_Data_var2_roi1_interaction', sep=""))
seed <- 22
### make demographics file
set.seed(seed)
x1 <- sort(rbinom(100, 1, 0.5))
set.seed(seed)
x2 <- jitter(c(seq(1,50), seq(50,1)), factor=2)
id <- paste("SIMvar2_roi1_", seq(1,100), sep="")
filepaths <- paste(odir, '/Simulated_Data_var2_roi1_interaction/SIMvar2_roi1_', seq(1,100), '/SIMvar2_roi1_', seq(1,100),
".nii.gz", sep="")
demog <- data.frame(cbind(id,x1,x2, filepaths))
colnames(demog) <- c("subjID", "Sex", "Score", "File_tbm")
write.table(demog, 'simulated_data.csv', sep=',', row.names=F)
#initialize outcome list
yvalues <- list()
yvalues <- lapply(seq(1,(length(roilabel.idx)/3)), function(seed){
set.seed(seed)
e <- rnorm(100,0,5)
b0 <- 1
b1 <- 0.3
b2 <- 0.4
b3 <- -1.0
y <- b0 + b1*x1 + b2*x2 + b3*x1*x2 + e
return(y)
})
# reformat outcome values
sigroi <- data.frame(do.call('cbind', yvalues))
sigroi <- data.matrix(sigroi)
# create nifti files
voxels <- list()
voxels <- lapply(seq(1,100), function(seed){
set.seed(seed)
values <- jitter(rnorm(length(brainmask.idx),0,5), factor=1)
sample@.Data[brainmask.idx] <- values
sample@.Data[nonbrainmask.idx] <- 0
sample@.Data[roilabel.idx] <- sigroi[seed,]
dir.create(sprintf('SIMvar2_roi1_%s', seed))
oro.nifti::writeNIfTI(sample, sprintf('SIMvar2_roi1_%s/SIMvar2_roi1_%s', seed, seed), verbose=T)
return(seed)
})
}
####### make data for 1 significant roi, 3 variables, testing for interaction and main effects (group*continous, group, continous)
if(datatype == "var3_roi1_all"){
dir.create(paste(odir, '/Simulated_Data_var3_roi1_all', sep=""))
setwd(paste(odir, '/Simulated_Data_var3_roi1_all', sep=""))
seed <- 1
### make demographics file
set.seed(seed)
x1 <- sort(rbinom(100, 1, 0.5))
set.seed(seed)
x2 <- jitter(c(seq(1,50), seq(50,1)), factor=2)
set.seed(seed)
x3 <- jitter(c(seq(101,150), rnorm(50,0,2)), factor=2)
id <- paste("SIMvar3_roi1_", seq(1,100), sep="")
id <- paste("SIMvar3_roi1_", seq(1,100), sep="")
filepaths <- paste(odir, '/Simulated_Data_var3_roi1_all/SIMvar3_roi1_', seq(1,100), '/SIMvar3_roi1_', seq(1,100),
".nii.gz", sep="")
demog <- data.frame(cbind(id,x1,x2,x3, filepaths))
colnames(demog) <- c("subjID", "Sex", "Score", "VLD", "File_tbm")
write.table(demog, 'simulated_data_multi.csv', sep=',', row.names=F)
#initialize outcome list
yvalues <- list()
yvalues <- lapply(seq(1,(length(roilabel.idx)/3)), function(seed){
set.seed(seed)
e <- rnorm(100,0,5)
b0 <- 1
b1 <- 0.3
b2 <- 0.4
b3 <- -1.0
b4 <- 0.2
y <- b0 + b1*x1 + b2*x2 + b3*x1*x2 + b4*x3 + e
return(y)
})
# reformat outcome values
sigroi <- data.frame(do.call('cbind', yvalues))
sigroi <- data.matrix(sigroi)
# create nifti files
voxels <- list()
voxels <- lapply(seq(1,100), function(seed){
set.seed(seed)
values <- jitter(rnorm(length(brainmask.idx),0,5), factor=1)
sample@.Data[brainmask.idx] <- values
sample@.Data[nonbrainmask.idx] <- 0
sample@.Data[roilabel.idx] <- sigroi[seed,]
dir.create(sprintf('SIMvar3_roi1_%s', seed))
oro.nifti::writeNIfTI(sample, sprintf('SIMvar3_roi1_%s/SIMvar3_roi1_%s', seed, seed), verbose=T)
return(seed)
})
}
####### make data for 1 non-significant roi, 3 variables, testing for no significance
if(datatype == "var3_roi1_nosig"){
dir.create(paste(odir, '/Simulated_Data_var3_roi1_nosig', sep=""))
setwd(paste(odir, '/Simulated_Data_var3_roi1_nosig', sep=""))
seed <- 1
### make demographics file
set.seed(seed)
x1 <- sort(rbinom(100, 1, 0.5))
set.seed(seed)
x2 <- jitter(c(seq(1,50), seq(50,1)), factor=2)
set.seed(seed)
x3 <- jitter(c(seq(101,150), rnorm(50,0,2)), factor=2)
id <- paste("SIMvar3_roi1_nosig", seq(1,100), sep="")
filepaths <- paste(odir, '/Simulated_Data_var3_roi1_nosig/SIMvar3_roi1_nosig', seq(1,100), '/SIMvar3_roi1_nosig', seq(1,100),
".nii.gz", sep="")
demog <- data.frame(cbind(id,x1,x2,x3,filepaths))
colnames(demog) <- c("subjID", "Sex", "Score", "VLD", "File_tbm")
write.table(demog, 'simulated_data_nosig.csv', sep=',', row.names=F)
#initialize outcome list
yvalues <- list()
yvalues <- lapply(seq(1,(length(roilabel.idx)/3)), function(seed){
set.seed(seed)
e <- rnorm(100,0,5)
b0 <- 1
b1 <- 0.3
b2 <- 0.4
b3 <- -1.0
b4 <- 0.2
y <- b0 + b1*x1 + b2*x2 + b3*x1*x2 + b4*x3 + e
return(y)
})
# reformat outcome values
sigroi <- data.frame(do.call('cbind', yvalues))
sigroi <- data.matrix(sigroi)
# create nifti files
voxels <- list()
voxels <- lapply(seq(1,100), function(seed){
set.seed(seed)
values <- jitter(rnorm(length(brainmask.idx),0,5), factor=1)
sample@.Data[brainmask.idx] <- values
sample@.Data[nonbrainmask.idx] <- 0
#sample@.Data[roilabel.idx] <- sigroi[seed,]
dir.create(sprintf('SIM_var3_roi1_nosig%s', seed))
oro.nifti::writeNIfTI(sample, sprintf('SIM_var3_roi1_nosig%s/SIM_var3_roi1_nosig%s', seed, seed), verbose=T)
return(seed)
})
}
####### make data for 2 significant rois, 3 variables, testing for multiple clusters
if(datatype == "var3_roi2_nointeraction"){
dir.create(paste(odir, '/Simulated_Data_var3_roi2_nointeraction', sep=""))
setwd(paste(odir, '/Simulated_Data_var3_roi2_nointeraction', sep=""))
seed <- 1
### make demographics file
set.seed(seed)
x1 <- sort(rbinom(100, 1, 0.5))
set.seed(seed)
x2 <- jitter(c(seq(1,50), seq(50,1)), factor=2)
set.seed(seed)
x3 <- jitter(c(seq(101,150), rnorm(50,0,2)), factor=2)
id <- paste("SIMvar3_roi2_", seq(1,100), sep="")
filepaths <- paste(odir, '/Simulated_Data_var3_roi2_nointeraction/SIMvar3_roi2_', seq(1,100), '/SIMvar3_roi2_', seq(1,100),
".nii.gz", sep="")
demog <- data.frame(cbind(id,x1,x2,x3,filepaths))
colnames(demog) <- c("subjID", "Sex", "Score", "VLD", "File_tbm")
write.table(demog, 'simulated_data_rois.csv', sep=',', row.names=F)
#initialize outcome list
yvalues <- list()
yvalues <- lapply(seq(1,(length(roilabel.idx)/3)), function(seed){
set.seed(seed)
e <- rnorm(100,0,5)
b0 <- 1
b1 <- 0.3
b2 <- 0.4
b3 <- -1.0
b4 <- 0.2
y <- b0 + b1*x1 + b2*x2 + b3*x1*x2 + b4*x3 + e
return(y)
})
#initialize second outcome list
yvalues2 <- list()
yvalues2 <- lapply(seq(1,(length(roilabel.idx2)/3)), function(seed){
set.seed(seed)
e <- rnorm(100,0,5)
b0 <- 1
b1 <- 0.3
b2 <- 0.4
b3 <- 1.0
b4 <- -0.2
y <- b0 + b1*x1 + b2*x2 + b3*x1*x2 + b4*x3 + e
return(y)
})
# reformat outcome values
sigroi <- data.frame(do.call('cbind', yvalues))
sigroi <- data.matrix(sigroi)
# reformat second outcome values
sigroi2 <- data.frame(do.call('cbind', yvalues2))
sigroi2 <- data.matrix(sigroi2)
# create nifti files
voxels <- list()
voxels <- lapply(seq(1,100), function(seed){
set.seed(seed)
values <- jitter(rnorm(length(brainmask.idx),0,5), factor=1)
sample@.Data[brainmask.idx] <- values
sample@.Data[nonbrainmask.idx] <- 0
sample@.Data[roilabel.idx] <- sigroi[seed,]
sample@.Data[roilabel.idx2] <- sigroi2[seed,]
dir.create(sprintf('SIMvar3_roi2_%s', seed))
oro.nifti::writeNIfTI(sample, sprintf('SIMvar3_roi2_%s/SIMvar3_roi2_%s', seed, seed), verbose=T)
return(seed)
})
}
#
#
# dat <- data.frame(cbind(x1, x2, x3, y))
# fit <- lm(y ~ x1 + x2 + x3, data = dat)
# summary(fit)
# quartz();ggplot(dat, aes(x2, y, colour=as.factor(x1))) + stat_smooth(method='lm') + geom_point()