-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_intendedfor_bold
executable file
·60 lines (48 loc) · 1.94 KB
/
add_intendedfor_bold
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
#!/usr/bin/env Rscript
library(jsonlite)
#read in command line arguments.
args <- commandArgs(trailingOnly = FALSE)
scriptpath <- dirname(sub("--file=", "", grep("--file=", args, fixed=TRUE, value=TRUE), fixed=TRUE))
argpos <- grep("--args", args, fixed=TRUE)
if (length(argpos) > 0L) {
args <- args[(argpos+1):length(args)]
} else {
args <- c()
}
if (length(args) == 0L) { stop("Expect at least one top-level subject directory") }
#needs to be a path to top of subject directory (can also be multiple paths)
fpath <- args
#fpath <- "/gpfs/group/mnh5174/default/Michael/fmriprep_pipeline_test/bids/sub-048"
if (!all(fex <- file.exists(fpath))) { stop("Cannot locate dir: ", fpath[!fex]) }
cur_user <- Sys.info()[["user"]] #to verify paths
make_writable <- function(file, user) {
ff <- file.info(file)
mode <- as.numeric(ff[["mode"]])
if (file.access(file, 2) == -1) {
if (ff[["uname"]] != user) {
#add group write permission
Sys.chmod(file, mode+20)
} else {
#add user write permission
Sys.chmod(file, mode+200)
}
return(mode) #for resetting
} else {
return(NULL)
}
}
for (fp in fpath) {
bold_files <- list.files(path=file.path(fp, "func"), pattern=".*_bold\\.nii\\.gz", full.names=TRUE)
fmap_files <- list.files(path=file.path(fp, "fmap"), pattern=".*_epi\\.nii\\.gz", full.names=TRUE)
bold_rel <- sub(".*/sub-\\d+/(.*)$", "\\1", bold_files, perl=TRUE) #IntendedFor must be relative to subject path, starting with func/
for (f in fmap_files) {
jfile <- sub(".nii.gz", ".json", f, fixed=TRUE)
j <- read_json(path=jfile)
j$IntendedFor <- bold_rel
message("In ", jfile, ", adding IntendedFor: ", paste(bold_files, collapse=", "))
#json file must be writeable; if not, chmod it
orig_mode <- make_writable(jfile, cur_user)
write_json(j, path=jfile, pretty=TRUE, auto_unbox=TRUE, digits=NA)
if (!is.null(orig_mode)) { Sys.chmod(jfile, orig_mode) } #restore original permission
}
}