|
| 1 | +#!/usr/bin/env Rscript |
| 2 | + |
| 3 | +library(tidyr) |
| 4 | +library(plyr) |
| 5 | +library(optparse) |
| 6 | +library(GGIR) |
| 7 | + |
| 8 | +main <- function() { |
| 9 | + # Define the option list |
| 10 | + option_list <- list( |
| 11 | + make_option(c("-p", "--project_dir"), type="character", |
| 12 | + default="/mnt/nfs/lss/vosslabhpc/Projects/BOOST/InterventionStudy/3-Experiment/data/act-int-test/", |
| 13 | + help="Path to the project directory", metavar="character"), |
| 14 | + make_option(c("-d", "--deriv_dir"), type="character", |
| 15 | + default="/derivatives/GGIR-2.8.2-test2/", |
| 16 | + help="Path to the derivatives directory", metavar="character") |
| 17 | + ) |
| 18 | + |
| 19 | + # Parse the options |
| 20 | + opt_parser <- OptionParser(option_list=option_list) |
| 21 | + opt <- parse_args(opt_parser) |
| 22 | + |
| 23 | + # Assign variables |
| 24 | + ProjectDir <- opt$project_dir |
| 25 | + ProjectDerivDir <- opt$deriv_dir |
| 26 | + |
| 27 | + # Print values to verify |
| 28 | + print(paste("Project Directory:", ProjectDir)) |
| 29 | + print(paste("Derivatives Directory:", ProjectDerivDir)) |
| 30 | + |
| 31 | + # Helper functions |
| 32 | + SubjectGGIRDeriv <- function(x) { |
| 33 | + a <- dirname(x) |
| 34 | + paste0(ProjectDir, ProjectDerivDir, a) |
| 35 | + } |
| 36 | + |
| 37 | + datadirname <- function(x) { |
| 38 | + b <- dirname(x) |
| 39 | + paste0(ProjectDir, b) |
| 40 | + } |
| 41 | + |
| 42 | + # Gather subject directories |
| 43 | + directories <- list.dirs(ProjectDir, recursive = FALSE) |
| 44 | + subdirs <- directories[grepl("sub-*", directories)] |
| 45 | + print(paste("subdirs: ", subdirs)) |
| 46 | + |
| 47 | + # Create project-specific derivatives GGIR folder if it doesn't already exist |
| 48 | + if (!dir.exists(paste0(ProjectDir, ProjectDerivDir))) { |
| 49 | + dir.create(paste0(ProjectDir, ProjectDerivDir)) |
| 50 | + } |
| 51 | + |
| 52 | + # List accel.csv files within subject-specific folders |
| 53 | + filepattern <- "*accel.csv" |
| 54 | + GGIRfiles <- list.files(subdirs, pattern = filepattern, recursive = TRUE, |
| 55 | + include.dirs = TRUE, full.names = TRUE, no.. = TRUE) |
| 56 | + print(paste("GGIR Files before splitting: ", GGIRfiles)) |
| 57 | + |
| 58 | + # Split files at the "//" so we only have paths from sub-XXX/ses-..... |
| 59 | + GGIRfiles <- sapply(strsplit(GGIRfiles, "//", fixed = TRUE), function(x) paste(x[2])) |
| 60 | + |
| 61 | + print(paste("GGIR Files after splitting: ", GGIRfiles)) |
| 62 | + |
| 63 | + |
| 64 | + # Ensure directory structure exists for GGIR processing |
| 65 | + for (i in GGIRfiles) { |
| 66 | + if (!dir.exists(SubjectGGIRDeriv(i))) { |
| 67 | + dir.create(SubjectGGIRDeriv(i), recursive = TRUE) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + # Run GGIR loop |
| 73 | + for (r in GGIRfiles) { |
| 74 | + if (dir.exists(paste0(SubjectGGIRDeriv(r), "/output_beh"))) { |
| 75 | + next |
| 76 | + } else { |
| 77 | + datadir <- paste0(normalize(path(datadirname(r))) |
| 78 | + outputdir <- paste0(SubjectGGIRDeriv(r)) |
| 79 | + print(paste("datadir: ", datadir)) |
| 80 | + print(paste("outputdir: ", outputdir)) |
| 81 | + if (!dir.exists(datadir)) { |
| 82 | + stop(paste("Error: datadir does not exist ->", datadir)) |
| 83 | + } |
| 84 | + # Force evaluation before calling GGIR |
| 85 | + assign("datadir", datadir, envir = .GlobalEnv) |
| 86 | + assign("outputdir", outputdir, envir = .GlobalEnv) |
| 87 | + |
| 88 | + try(g.shell.GGIR( |
| 89 | + mode = 1:5, |
| 90 | + datadir = get("datadir", envir = .GlobalEnv), |
| 91 | + outputdir = get("outputdir", envir = .GlobalEnv), |
| 92 | + overwrite = FALSE, |
| 93 | + print.filename = TRUE, |
| 94 | + storefolderstructure = FALSE, |
| 95 | + windowsizes = c(5, 900, 3600), |
| 96 | + desiredtz = "America/Chicago", |
| 97 | + do.enmo = TRUE, do.anglez = TRUE, |
| 98 | + dayborder = 0, |
| 99 | + strategy = 1, hrs.del.start = 0, hrs.del.end = 0, |
| 100 | + maxdur = 0, includedaycrit = 0, |
| 101 | + idloc = 1, |
| 102 | + dynrange = 8, |
| 103 | + chunksize = 1, |
| 104 | + do.cal = TRUE, |
| 105 | + use.temp = FALSE, |
| 106 | + spherecrit = 0.3, |
| 107 | + minloadcrit = 72, |
| 108 | + printsummary = TRUE, |
| 109 | + do.imp = TRUE, |
| 110 | + epochvalues2csv = TRUE, |
| 111 | + L5M5window = c(0,24), |
| 112 | + M5L5res = 10, |
| 113 | + winhr = c(5,10), |
| 114 | + qlevels = c(960/1440, 1320/1440, 1380/1440, 1410/1440, 1430/1440, 1435/1440, 1438/1440), |
| 115 | + ilevels = seq(0,600, by = 25), |
| 116 | + iglevels = c(seq(0,4000, by = 25), 8000), |
| 117 | + bout.metric = 4, |
| 118 | + do.visual = TRUE, |
| 119 | + excludefirstlast = FALSE, |
| 120 | + includenightcrit = 0, |
| 121 | + anglethreshold = 5, |
| 122 | + timethreshold = 5, |
| 123 | + ignorenonwear = TRUE, |
| 124 | + acc.metric = "ENMO", |
| 125 | + do.part3.pdf = TRUE, |
| 126 | + outliers.only = FALSE, |
| 127 | + def.noc.sleep = 1, |
| 128 | + excludefirstlast.part5 = FALSE, |
| 129 | + maxdur = 0, |
| 130 | + threshold.lig = c(45), |
| 131 | + threshold.mod = c(100), |
| 132 | + threshold.vig = c(430), |
| 133 | + boutdur.mvpa = c(1,5,10), |
| 134 | + boutdur.in = c(10,20,30), |
| 135 | + boutdur.lig = c(1,5,10), |
| 136 | + boutcriter.mvpa = 0.8, |
| 137 | + boutcriter.in = 0.9, |
| 138 | + boutcriter.lig = 0.8, |
| 139 | + timewindow = c("MM", "WW"), |
| 140 | + acc.metric = "ENMO", |
| 141 | + do.report = c(2,4,5), |
| 142 | + visualreport = TRUE, |
| 143 | + do.parallel = TRUE |
| 144 | + )) |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +# Run main if executed as a script |
| 150 | +if (!interactive()) { |
| 151 | + main() |
| 152 | +} |
0 commit comments