-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgamm4_voxelwise.R
executable file
·587 lines (438 loc) · 21.1 KB
/
gamm4_voxelwise.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
##############################################################################
################ ###############
################ GAMM Voxelwise Wrapper ###############
################ Angel Garcia de la Garza ###############
################ [email protected] ###############
################ 12/20/2016 ###############
##############################################################################
suppressMessages(require(optparse))
##############################################################################
################ Option List ###############
##############################################################################
option_list = list(
make_option(c("-c", "--covariates"), action="store", default=NA, type='character',
help="Full path to RDS covariate file."),
make_option(c("-o", "--output"), action="store", default=NA, type='character',
help="Full path to output directory"),
make_option(c("-p", "--imagepaths"), action="store", default=NA, type='character',
help="Name of the variable in the covariate file that contains the path to the images to be analyzed"),
make_option(c("-m", "--mask"), action="store", default=NA, type='character',
help="Full path to mask"),
make_option(c("-s", "--smoothing"), action="store", default=NA, type='numeric',
help="The smoothing in sigmas required for the fourd image. Please write in 0 if no smoothing is wanted"),
make_option(c("-i", "--inclusion"), action="store", default=NA, type='character',
help="Name of inclusion variable on dataset. By default 1 means include. This will subset your rds file"),
make_option(c("-u", "--subjId"), action="store", default=NA, type='character',
help="subjID name on the covariates dataset"),
make_option(c("-f", "--formula"), action="store", default=NA, type='character',
help="Formula for covariates to be used, should only include the right hand side of the formula.
Example: ~ stai_stai_tr+sex+s(age)+s(age,by=sex)"),
make_option(c("-e", "--random"), action="store", default=NULL, type='character',
help="Formula for random effects to be used, should only include the right hand side of the formula.
Example: ~(1|bblid)"),
make_option(c("-a", "--padjust"), action="store", default="none", type='character',
help="method used to adjust pvalues, default is `none`"),
make_option(c("-k", "--splits"), action="store", default=10, type='numeric',
help="number of splits to divide the data in, default is 10. To minimize data usage"),
make_option(c("-n", "--numbercores"), action="store", default=10, type='numeric',
help="Number of cores to be used, default is 10"),
make_option(c("-d", "--skipfourD"), action="store", default=FALSE, type='logical',
help="Option to skip creation of fourdD image and look for it in the Analysis Directory.
4D image must be labeled as 'fourd.nii.gz'. Will also skip smoothing step.
Default (FALSE) means to not skip"),
make_option(c("-r", "--residual"), action="store", default=FALSE, type='logical',
help="Option to output residual 4D image.
Default (FALSE) means to not generate residual maps")
)
opt = parse_args(OptionParser(option_list=option_list))
for (i in 1:length(opt)){
if (is.na(opt)[i] == T) {
cat('User did not specify all arguments.\n')
cat('Use gamm4_voxelwise.R -h for an expanded usage menu.\n')
quit()
}
}
print("##############################################################################")
print("################ Generalized Additive Mixed Effects Model ###############")
print("################ Voxelwise Script ###############")
print("################ Angel Garcia de la Garza ###############")
print("################ [email protected] ###############")
print("################ Version 4.1.2 ###############")
print("##############################################################################")
##############################################################################
################ Load Libraries ###############
##############################################################################
print("Loading Libraries")
suppressMessages(require(ggplot2))
suppressMessages(require(base))
suppressMessages(require(reshape2))
suppressMessages(require(nlme))
suppressMessages(require(lme4))
suppressMessages(require(gamm4))
suppressMessages(require(stats))
suppressMessages(require(knitr))
suppressMessages(require(mgcv))
suppressMessages(require(plyr))
suppressMessages(require(oro.nifti))
suppressMessages(require(parallel))
suppressMessages(require(optparse))
suppressMessages(require(fslr))
suppressMessages(require(voxel))
##############################################################################
################ Declare Variables ###############
##############################################################################
print("Reading Arguments")
subjDataName <- opt$covariates
OutDirRoot <- opt$output
namePaths <- opt$imagepaths
maskName <- opt$mask
smooth <- opt$smoothing
inclusionName <- opt$inclusion
subjID <- opt$subjId
covsFormula <- opt$formula
randomFormula <- opt$random
pAdjustMethod <- opt$padjust
splits <- opt$splits
ncores <- opt$numbercores
skipFourD <- opt$skipfourD
residualMap <- opt$residual
methods <- c("holm", "hochberg", "hommel", "bonferroni", "BH", "BY","fdr", "none")
if (!any(pAdjustMethod == methods)) {
print("p.adjust.method is not a valid one, reverting back to 'none'")
pAdjustMethod <- "none"
}
##############################################################################
################ Load subject data ###############
##############################################################################
print("Loading covariates file")
subjData<-readRDS(subjDataName) ##Read Data
subset <- which(subjData[inclusionName] == 1) ##Find subset for analysis
subjData <- subjData[subset, ] #subset data
##############################################################################
################ Create Analysis Directory ###############
##############################################################################
print("Creating Analysis Directory")
OutDir <- paste0(OutDirRoot, "/n",dim(subjData)[1],"_",namePaths,"_",inclusionName,"_smooth",as.character(smooth))
dir.create(OutDir)
setwd(OutDir)
##############################################################################
################ Create and output fourd image ###############
##############################################################################
if (!skipFourD) {
print("Merging images and saving out a fourd image")
subjList <- as.character(subjData[,grep(namePaths, names(subjData))])
length.subj <- length(subjList)
k <- splits
break.subj <- ceiling(length.subj / k)
if (break.subj == 1) {
k = 1
break.subj <- ceiling(dim(imageMat)[2] / k )
} else if (break.subj < k ) {
k = break.subj - 1
break.subj <- ceiling(dim(imageMat)[2] / k )
}
subMergeNames <- "foo"
for (i in 1:k) {
if (i == k) {
out <- paste0("fourd_",i,".nii.gz")
fslmerge(subjList[(1 + (i-1)*break.subj):length.subj], direction="t", outfile=out, drop_dim=F)
subMergeNames <- c(subMergeNames, out)
} else {
out <- paste0("fourd_",i,".nii.gz")
fslmerge(subjList[(1 + (i-1)*break.subj):((i)*break.subj)], direction="t", outfile=out, drop_dim=F)
subMergeNames <- c(subMergeNames, out)
}
}
subMergeNames <- subMergeNames[-1]
fslmerge(subMergeNames, direction="t", outfile="fourd.nii.gz")
system('rm -f fourd_*.nii.gz')
if (smooth > 0) {
fslsmooth("fourd.nii.gz", sigma = smooth, outfile="fourd.nii.gz")
} else {
print("No smoothing done")
}
} else {
print("Skipping fourd image creation; Script will looking for file names fourd.nii.gz under first level directory")
}
system(paste0("scp ", maskName," ",OutDir, "/mask.nii.gz"), wait=T)
print("mask succesfully copied")
##############################################################################
################ Output summary files ###############
##############################################################################
write.table(subjData[, namePaths], paste0(namePaths,".csv"), row.names = F, col.names=FALSE)
write.table(subjData[, subjID], paste0(subjID,".csv"), row.names = F, col.names=FALSE)
print("Succesfully wrote paths and id files")
##############################################################################
################ Make Output Directory ###############
##############################################################################
print("Creating output directory")
outName <- gsub("~", "", covsFormula)
outName <- gsub(" ", "", outName)
outName <- gsub("\\+","_",outName)
outName <- gsub("\\(","",outName)
outName <- gsub("\\)","",outName)
outName <- gsub(",","",outName)
outName <- gsub("\\.","",outName)
outName <- gsub("=","",outName)
outName <- gsub("\\*","and",outName)
outName <- gsub(":","and",outName)
random <- gsub("~", "", randomFormula)
random <- gsub("\\(", "", random)
random <- gsub("\\)", "", random)
random <- gsub("\\|", "", random)
outsubDir <- paste0("n",dim(subjData)[1],"gamm_Cov_",outName,"_Random_",random)
outsubDir<-paste(OutDir,outsubDir,sep="/")
logDir<-paste(OutDir,"logs",sep="/")
#Will return a warning if logDir and outsubDir already exist
dir.create(logDir)
dir.create(outsubDir)
system(paste('rm -f', file.path(outsubDir, '*')))
##############################################################################
################ Echo Arguments ###############
##############################################################################
system( paste0("echo Arguments are: >> ", outsubDir,"/logs.txt"))
system( paste0("echo Covariates file is: ", subjDataName,">> ", outsubDir,"/logs.txt"))
system( paste0("echo Output directory is: ", OutDir,">> ", outsubDir,"/logs.txt"))
system( paste0("echo Path name variable in covarites file is: ", namePaths,">> ", outsubDir,"/logs.txt"))
system( paste0("echo Mask path is: ", maskName,">> ", outsubDir,"/logs.txt"))
system( paste0("echo Smoothing is: ", smooth," >> ", outsubDir,"/logs.txt"))
system( paste0("echo Inclusion variable name is: ", inclusionName,">> ", outsubDir,"/logs.txt"))
system( paste0("echo ID variable name is: ", subjID,">> ", outsubDir,"/logs.txt"))
system( paste0("echo Formula for fixed effects is: ", outName,">> ", outsubDir,"/logs.txt"))
system( paste0("echo Formula for random effects is: ", random,">> ", outsubDir,"/logs.txt"))
system( paste0("echo Number of cores is: ", ncores," >> ", outsubDir,"/logs.txt"))
###cleanup logdir
system(paste('rm -f', file.path(logDir, '*')))
##############################################################################
################ Load data ###############
##############################################################################
maskName <- paste0(OutDir,"/mask.nii.gz")
imageName <- paste0(OutDir,"/fourd.nii.gz")
mask<-readNIfTI(maskName)
imageIn<-readNIfTI(imageName)
###Time Series to Matrix Using oro.nifti
ts2matrix <- function(image, mask) {
label <- sort(as.numeric(unique(matrix([email protected]))))
if (length(label) == 2 && label[1] == 0 && label[2] == 1) {
if (length(dim([email protected])) == 3 | dim([email protected])[4] == 1) {
vector <- [email protected][[email protected] == 1]
gc()
return(vector)
} else {
temp <- matrix([email protected])[[email protected] == 1]
temp <- t(temp)
temp <- as.data.frame(temp)
names <- base::lapply(1:dim(temp)[2], function(x) { return(paste0("voxel",x))})
names(temp) <- names
gc()
return(temp)
}
} else {
gc()
stop("Mask Image is not Binary")
}
}
imageMat<-ts2matrix(imageIn,mask)
print("Fourd image and mask has been loaded")
##############################################################################
################ Preallocate output ###############
##############################################################################
pOut<-matrix(NA,nrow=dim(imageMat)[2],ncol=1)
pAdjustedOut<-matrix(NA,nrow=dim(imageMat)[2],ncol=1)
tOut<-matrix(NA,nrow=dim(imageMat)[2],ncol=1)
zOut<-matrix(NA,nrow=dim(imageMat)[2],ncol=1)
print("Preallocate output done")
##############################################################################
################ Run model using parallel ###############
##############################################################################
timeOn<-proc.time()
k <- 0
rm(k)
length.voxel <- ceiling(dim(imageMat)[2] / splits)
if (length.voxel == 1) {
splits = 1
length.voxel <- ceiling(dim(imageMat)[2] / splits)
} else if (length.voxel < splits) {
splits = length.voxel - 1
length.voxel <- ceiling(dim(imageMat)[2] / splits)
}
setwd(outsubDir)
#If statement to create or not create residual 4D image.
if (!residualMap) {
# We create a list of formulas for each voxel in our data.
# Each element in the list will have formula with a different voxel as the dependent variable
print("Running Test Model")
m <- mclapply(1:5, function(x) {as.formula(paste(paste0("imageMat[,",x,"]"), covsFormula, sep=""))}, mc.cores = ncores)
test <- gamm4(formula = m[[1]], data=subjData, REML=T, random = as.formula(randomFormula))
model <- mclapply(m, function(x) {
foo <- summary(gamm4(formula = x, data=subjData, REML=T, random = as.formula(randomFormula))$gam)
return(rbind(foo$p.table,foo$s.table))
}, mc.cores = ncores)
print("Test Models Done; Running Parallel Models")
for (k in 1:(splits)) {
if (k == splits) {
if ((6 + (k-1)*length.voxel) < dim(imageMat)[2]) {
m <- mclapply((6 + (k-1)*length.voxel):dim(imageMat)[2], function(x) {as.formula(paste(paste0("imageMat[,",x,"]"), covsFormula, sep=""))}, mc.cores = ncores)
}
} else {
m <- mclapply((6 + (k-1)*length.voxel):(5 + (k)*length.voxel), function(x) {as.formula(paste(paste0("imageMat[,",x,"]"), covsFormula, sep=""))}, mc.cores = ncores)
}
model.temp <- mclapply(m, function(x) {
foo <- summary(gamm4(formula = x, data=subjData, REML=T, random = as.formula(randomFormula))$gam)
return(rbind(foo$p.table,foo$s.table))
}, mc.cores = ncores)
model <- c(model, model.temp)
percent <- (k / splits) * 100
print(paste0(percent, "% of voxels done"))
}
loopTime<-proc.time()-timeOn
print("Models are done")
print(loopTime/60)
} else {
# We create a list of formulas for each voxel in our data.
# Each element in the list will have formula with a different voxel as the dependent variable
print("Working on test models; will generate residual timeseries")
m <- mclapply(1:5, function(x) {as.formula(paste(paste0("imageMat[,",x,"]"), covsFormula, sep=""))}, mc.cores = ncores)
test <- gamm4(formula = m[[1]], data=subjData, REML=T, random = as.formula(randomFormula))
model <- mclapply(m, function(x) {
foo <- summary(gamm4(formula = x, data=subjData, REML=T, random = as.formula(randomFormula))$gam)
residualVector <- (gamm4(formula = x, data=subjData, REML=T, random = as.formula(randomFormula))$gam)$residuals
return(list(rbind(foo$p.table,foo$s.table), residualVector))
}, mc.cores = ncores)
print("Test Models Done; Running Parallel Models")
for (k in 1:(splits)) {
if (k == splits) {
if ((6 + (k-1)*length.voxel) < dim(imageMat)[2]) {
m <- mclapply((6 + (k-1)*length.voxel):dim(imageMat)[2], function(x) {as.formula(paste(paste0("imageMat[,",x,"]"), covsFormula, sep=""))}, mc.cores = ncores)
}
} else {
m <- mclapply((6 + (k-1)*length.voxel):(5 + (k)*length.voxel), function(x) {as.formula(paste(paste0("imageMat[,",x,"]"), covsFormula, sep=""))}, mc.cores = ncores)
}
model.temp <- mclapply(m, function(x) {
foo <- summary(gamm4(formula = x, data=subjData, REML=T, random = as.formula(randomFormula))$gam)
residualVector <- (gamm4(formula = x, data=subjData, REML=T, random = as.formula(randomFormula))$gam)$residuals
return(list(rbind(foo$p.table,foo$s.table), residualVector))
}, mc.cores = ncores)
model <- c(model, model.temp)
percent <- (k / splits) * 100
print(paste0(percent, "% of voxels done"))
}
##Remove tsmatrix
dimMat <- dim(imageMat)
rm(imageMat)
gc()
#Generate tsresiduals
residualList <- mclapply(model, function(x) {
return(x[[2]])
}, mc.cores = ncores)
#Generate tsresiduals
residualMat <- mcmapply(function(x) {
return(x)
}, residualList, mc.cores = ncores, SIMPLIFY = TRUE)
rm(residualList)
gc()
#Save only parameter tables under models
model <- mclapply(model, function(x) {
return(x[[1]])
}, mc.cores = ncores)
loopTime<-proc.time()-timeOn
print("Models are done")
print(loopTime/60)
print("Generating Residual timeseries")
### Create output
residualMask <- mask
residualMask <- [email protected]
#remove image in for memorize optimization purposes
dataTypeIn <- datatype(imageIn)
dimPixIn <- pixdim(imageIn)
rm(imageIn)
gc()
Residualnames <- "temp"
subj.split <- ceiling(dim(residualMat)[1] / splits)
for (k in 1:(splits)) {
##Output Percentages
if (k == splits) {
seq <- (1 + (k-1)*subj.split):(dim(residualMat)[1])
print(paste0(seq[length(seq)]*100/dim(residualMat)[1],"%"))
} else {
seq <- (1 + (k-1)*subj.split):(k*subj.split)
print(paste0(seq[length(seq)]*100/dim(residualMat)[1],"%"))
}
#generate 4d residual image
residuals <- mcmapply(function(x) {
residualMask[[email protected]==1] <- residualMat[x,]
return(residualMask)
}, seq, SIMPLIFY = "array", mc.cores = ncores, mc.preschedule=F)
#Write it out
residualNii <- nifti(residuals, datatype=dataTypeIn, pixdim=dimPixIn)
rm(residuals)
gc()
writeNIfTI2(residualNii,paste0("gamm4_residualMap_", k))
Residualnames <- c(Residualnames, paste0("gamm4_residualMap_", k,".nii.gz"))
}
Residualnames <- Residualnames[-1]
ls()
fslmerge(Residualnames, direction="t", outfile="gamm4_residualMap.nii.gz")
for (i in Residualnames) {
system(paste0("rm -f ",i))
}
print("DONE: Residual timeseries")
}
##############################################################################
################ Allocate out t-map and z-map ###############
##############################################################################
for (j in 1:dim(model[[1]])[1]) {
variable <- rownames(model[[1]])[j]
if (grepl("s(", variable, fixed=T)) {
for(i in 1:length(model)){
pOut[i,1]<- model[[i]][which(rownames(model[[i]]) == variable),4]
zOut[i,1]<- qnorm((model[[i]][which(rownames(model[[i]]) == variable),4] / 2), lower.tail=F)
}
pOutImage<-mask
zOutImage<-mask
pAdjustedOutImage<-mask
pAdjustedOut <- stats::p.adjust(pOut, method=pAdjustMethod)
[email protected][[email protected]]<-pAdjustedOut
var <- gsub("\\(", "", variable)
var <- gsub("\\)", "", var)
var <- gsub(",", "", var)
var <- gsub("=", "", var)
var <- gsub("\\*","and",var)
var <- gsub(":","and",var)
writeNIfTI(pOutImage,paste0("gammP_",var))
writeNIfTI(zOutImage,paste0("gammZ_",var))
if (pAdjustMethod != "none") {
writeNIfTI(pAdjustedOutImage,paste0("gammPadjusted_",pAdjustMethod, "_",var))
}
}
else {
for(i in 1:length(model)){
pOut[i,1]<-model[[i]][which(rownames(model[[i]]) == variable),4]
zOut[i,1]<-sign(model[[i]][which(rownames(model[[i]]) == variable),3])*qnorm((model[[i]][which(rownames(model[[i]]) == variable),4] / 2), lower.tail=F)
tOut[i,1]<-model[[i]][which(rownames(model[[i]]) == variable),3]
}
pOutImage<-mask
zOutImage<-mask
tOutImage<-mask
pAdjustedOutImage<-mask
pAdjustedOut <- stats::p.adjust(pOut, method=pAdjustMethod)
[email protected][[email protected]]<-pAdjustedOut
var <- gsub("\\(", "", variable)
var <- gsub("\\)", "", var)
var <- gsub("\\*","and",var)
var <- gsub(":","and",var)
writeNIfTI(pOutImage,paste0("gammP_",var))
writeNIfTI(zOutImage,paste0("gammZ_",var))
writeNIfTI(tOutImage,paste0("gammT_",var))
if (pAdjustMethod != "none") {
writeNIfTI(pAdjustedOutImage,paste0("gammPadjusted_",pAdjustMethod, "_",var))
}
}
}
print("Write t-maps and p-maps is done")