-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnpointrun
executable file
·109 lines (76 loc) · 3.54 KB
/
npointrun
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
#!/usr/bin/env Rscript
#### Make sure packages are installed and load them
# Rniftilib is not in this location
#if (!suppressPackageStartupMessages(require(Rniftilib))) install.packages("Rniftilib", repos="http://R-Forge.R-project.org")
if (!suppressPackageStartupMessages(require(argparse))) install.packages("argparse")
if (!suppressPackageStartupMessages(require(doParallel))) install.packages("doParallel")
suppressPackageStartupMessages(library(RNifti))
suppressPackageStartupMessages(library(argparse))
suppressPackageStartupMessages(library(doParallel))
suppressPackageStartupMessages(library(neuropointillist))
# Source helper functions
parser <- ArgumentParser()
parser$add_argument("-m", "--mask", nargs=1, help="Maskfile to process", required=TRUE)
parser$add_argument("-d", "--designmat", nargs=1, help="Design matrix in RDS format", required=TRUE)
parser$add_argument("-p", "--permutationfile", nargs=1, help="File name of the permutation output that includes permutation number", required=FALSE)
parser$add_argument("--model", nargs=1, help="R code that defines the voxelwise-model and any initialization", required=TRUE)
args <- parser$parse_args()
###############################################################################
#### Check for mask and read it. It is mandatory and must exist.
maskfile <- args$mask
tryCatch({
mask <- readNifti(maskfile);
}, error=function(e) {
cat("Could not read mask file: ", maskfile, "\n")
stop(e)
})
# reduce to vector and obtain list of nonzero vertices
mask.vector <- as.vector(mask)
mask.vertices <- which(mask.vector > 0)
nvertices <- length(mask.vertices)
###############################################################################
#### read model code
if (!is.null(args$model)) {
modelfile <- args$model
if (!file.exists(modelfile)) {
stop("model file ", modelfile, " does not exist!")
}
result <- tryCatch({
source(modelfile)
}, error=function(e) {
cat("There were errors in the model file: ", modelfile, "\n")
stop(e)
})
}
if( ! exists('processVoxel')) {
stop("The model file did not define the processVoxel function")
}
###############################################################################
#### Read in rds data
designmat <- readRDS(args$designmat)
attach(designmat)
voxelfile <- gsub(".nii.gz", ".rds", args$mask)
voxeldat <- readRDS(voxelfile)
stopifnot(dim(voxeldat)[1] == dim(designmat)[1])
###############################################################################
#### Do the processing
# if permutation testing, we set the number of the permutation. This can be used as a seed or for other purposes
# we also drop the suffix (must be .nii.gz)
if (!is.null(args$permutationfile)) {
permutationNumberString <- gsub("[^[:digit:]]", "", args$permutationfile)
permutationNumber <- as.numeric(permutationNumberString)
}
cat("Starting sequential job\n")
system.time(results <-sapply(1:nvertices, processVoxel))
prefix <- gsub(".nii.gz$","", args$mask)
# Use the permutation file name provided to modify permutation filenames if we are doing that
if (!is.null(args$permutationfile)) {
# test to see if more than one name was returned
# paste the permutation number to the array of names
if(is.array(results)) {
rownames(results) <- paste(attributes(results[,1])$names, permutationNumberString, sep=".")
} else {
names(results) <- rep(paste(attributes(results)$names[1], permutationNumberString, sep="."), length(results))
}
}
npointWriteOutputFiles(prefix,results,mask)