|
| 1 | + |
| 2 | +#!/usr/bin/env Rscript |
| 3 | + |
| 4 | +# Usage: Rscript new_gg.R --project_dir "/Shared/vosslabhpc/Projects/BOOST/InterventionStudy/3-experiment/data/act-int-test/" --deriv_dir "derivatives/GGIR-3.2.6-test/" |
| 5 | +library(tidyr) |
| 6 | +library(plyr) |
| 7 | +library(optparse) |
| 8 | +library(GGIR) |
| 9 | + |
| 10 | +main <- function() { |
| 11 | + # Define the option list |
| 12 | + option_list <- list( |
| 13 | + make_option(c("-p", "--project_dir"), type = "character", |
| 14 | + default = "/mnt/nfs/lss/vosslabhpc/Projects/BOOST/InterventionStudy/3-Experiment/data/act-int-test/", |
| 15 | + help = "Path to the project directory", metavar = "character"), |
| 16 | + make_option(c("-d", "--deriv_dir"), type = "character", |
| 17 | + default = "/derivatives/GGIR-3.2.6-test/", |
| 18 | + help = "Path to the derivatives directory", metavar = "character") |
| 19 | + ) |
| 20 | + |
| 21 | + # Parse the options |
| 22 | + opt_parser <- OptionParser(option_list = option_list) |
| 23 | + opt <- parse_args(opt_parser) |
| 24 | + |
| 25 | + # Assign variables |
| 26 | + ProjectDir <- opt$project_dir |
| 27 | + ProjectDerivDir <- opt$deriv_dir |
| 28 | + |
| 29 | + # Print values to verify |
| 30 | + print(paste("Project Directory:", ProjectDir)) |
| 31 | + print(paste("Derivatives Directory:", ProjectDerivDir)) |
| 32 | + |
| 33 | + # Helper functions |
| 34 | + SubjectGGIRDeriv <- function(x) { |
| 35 | + a <- dirname(x) |
| 36 | + paste0(ProjectDir, ProjectDerivDir, a) |
| 37 | + } |
| 38 | + |
| 39 | + datadirname <- function(x) { |
| 40 | + b <- dirname(x) |
| 41 | + paste0(ProjectDir, b) |
| 42 | + } |
| 43 | + |
| 44 | + # Gather subject directories |
| 45 | + directories <- list.dirs(ProjectDir, recursive = FALSE) |
| 46 | + subdirs <- directories[grepl("sub-*", directories)] |
| 47 | + print(paste("subdirs: ", subdirs)) |
| 48 | + |
| 49 | + # Create project-specific derivatives GGIR folder if it doesn't exist |
| 50 | + if (!dir.exists(paste0(ProjectDir, ProjectDerivDir))) { |
| 51 | + dir.create(paste0(ProjectDir, ProjectDerivDir)) |
| 52 | + } |
| 53 | + |
| 54 | + # List accel.csv files |
| 55 | + filepattern <- "*accel.csv" |
| 56 | + GGIRfiles <- list.files(subdirs, pattern = filepattern, recursive = TRUE, |
| 57 | + include.dirs = TRUE, full.names = TRUE, no.. = TRUE) |
| 58 | + print(paste("GGIR Files before splitting: ", GGIRfiles)) |
| 59 | + |
| 60 | + # Adjust path formatting |
| 61 | + GGIRfiles <- sapply(strsplit(GGIRfiles, "//", fixed = TRUE), function(x) paste(x[2])) |
| 62 | + print(paste("GGIR Files after splitting: ", GGIRfiles)) |
| 63 | + |
| 64 | + # Ensure directory structure exists |
| 65 | + for (i in GGIRfiles) { |
| 66 | + if (!dir.exists(SubjectGGIRDeriv(i))) { |
| 67 | + dir.create(SubjectGGIRDeriv(i), recursive = TRUE) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + # Run GGIR loop |
| 72 | + for (r in GGIRfiles) { |
| 73 | + if (dir.exists(paste0(SubjectGGIRDeriv(r), "/output_beh"))) { |
| 74 | + next |
| 75 | + } else { |
| 76 | + datadir <- normalizePath(datadirname(r), mustWork = FALSE) |
| 77 | + outputdir <- SubjectGGIRDeriv(r) |
| 78 | + print(paste("datadir: ", datadir)) |
| 79 | + print(paste("outputdir: ", outputdir)) |
| 80 | + if (!dir.exists(datadir)) { |
| 81 | + stop(paste("Error: datadir does not exist ->", datadir)) |
| 82 | + } |
| 83 | + |
| 84 | + assign("datadir", datadir, envir = .GlobalEnv) |
| 85 | + assign("outputdir", outputdir, envir = .GlobalEnv) |
| 86 | + |
| 87 | + try({ |
| 88 | + GGIR( |
| 89 | + mode = 1:6, |
| 90 | + datadir = datadir, |
| 91 | + outputdir = outputdir, |
| 92 | + studyname = "boost", |
| 93 | + overwrite = FALSE, |
| 94 | + do.report = c(2, 4, 5, 6), |
| 95 | + visualreport = TRUE, |
| 96 | + old_visualreport = FALSE, |
| 97 | + windowsizes = c(5, 900, 3600), |
| 98 | + desiredtz = "America/Chicago", |
| 99 | + print.filename = TRUE, |
| 100 | + dayborder = 0, |
| 101 | + idloc = 2, |
| 102 | + epochvalues2csv = TRUE, |
| 103 | + ignorenonwear = TRUE, |
| 104 | + do.ENMO = TRUE, |
| 105 | + acc.metric = "ENMO", |
| 106 | + hrs.del.start = 4, |
| 107 | + hrs.del.end = 3, |
| 108 | + maxdur = 9, |
| 109 | + loglocation = "/Shared/vosslabhpc/Projects/BOOST/InterventionStudy/3-experiment/data/act-int-test/sleep.csv", |
| 110 | + colid = 1, |
| 111 | + coln1 = 2, |
| 112 | + sleepwindowType = "SPT", |
| 113 | + timewindow = c("WW", "MM", "OO"), |
| 114 | + part6CR = TRUE |
| 115 | + ) |
| 116 | + }) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +# Run main if executed as script |
| 122 | +if (!interactive()) { |
| 123 | + main() |
| 124 | +} |
0 commit comments