get_brainage.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. DEBUG=1
  3. EXTENSIONS=('nii.gz' 'nii' 'mgz')
  4. # Initialize variables with default values
  5. INPUT_DIR=""
  6. OUTPUT_DIR=""
  7. BIDS="yes"
  8. CONDOR="yes"
  9. RAW="yes"
  10. RUN_SINGULARITY_FILE=""
  11. SAVE="no"
  12. # Condor specific arguments
  13. CPUS='1'
  14. RAM='8G'
  15. DISK='1G'
  16. # Parse command-line arguments
  17. while [[ "$#" -gt 0 ]]; do
  18. case $1 in
  19. -input) INPUT_DIR="$2"; shift ;;
  20. -output) OUTPUT_DIR="$2"; shift ;;
  21. -BIDS) BIDS="$2"; shift ;;
  22. -CONDOR) CONDOR="$2"; shift ;;
  23. -RAW) RAW="$2"; shift ;;
  24. -RUN_FILE) RUN_SINGULARITY_FILE="$2"; shift ;;
  25. -SAVE_FILE) SAVE="$2"; shift ;;
  26. -CONTAINER_PATH) CONTAINER_PATH="$2"; shift ;;
  27. -help)
  28. echo "Usage: $0 -input <input_dir> -output <output_dir> -BIDS <yes/no> -CONDOR <yes/no> -RAW <yes/no> -RUN_FILE <path> -SAVE_FILE <yes/no> -CONTAINER_PATH <path>"
  29. exit 0
  30. ;;
  31. *) echo "Unknown parameter passed: $1"; exit 1 ;;
  32. esac
  33. shift
  34. done
  35. # Debugging or verbose mode can be added here if needed.
  36. if [ "$DEBUG" -eq 1 ]; then
  37. echo "Input directory: $INPUT_DIR" >&2
  38. echo "Output directory: $OUTPUT_DIR" >&2
  39. echo "BIDS: $BIDS" >&2
  40. echo "CONDOR: $CONDOR" >&2
  41. echo "RAW: $RAW" >&2
  42. echo "RUN_FILE: $RUN_SINGULARITY_FILE" >&2
  43. echo "SAVE_ALL: $SAVE" >&2
  44. echo "CONTAINER_PATH: $CONTAINER_PATH" >&2
  45. fi
  46. # Check if INPUT_DIR exists and is a directory (even if one file)
  47. if [ ! -d "$INPUT_DIR" ]; then
  48. echo "Error: $INPUT_DIR is not a directory or does not exist." >&2
  49. exit 1
  50. fi
  51. if [ -z "$( ls -A $INPUT_DIR )" ]; then
  52. echo "$INPUT_DIR Empty" >&2
  53. exit 1
  54. fi
  55. # Check if OUTPUT_DIR exists; if not, create it
  56. if [ ! -d "$OUTPUT_DIR" ]; then
  57. echo "Warning: $OUTPUT_DIR does not exist. Creating it now." >&2
  58. mkdir -p "$OUTPUT_DIR"
  59. fi
  60. if [ ! -z "$( ls -A $OUTPUT_DIR )" ]; then
  61. echo "Warning $OUTPUT_DIR is not Empty, files might be overwritten" >&2
  62. fi
  63. LOGS_DIR=${OUTPUT_DIR}/logs
  64. # create the logs dir if it doesn't exist
  65. if [ ! -d "${LOGS_DIR}" ]; then
  66. echo "Creating logs diretory ${LOGS_DIR}" >&2
  67. mkdir -p "${LOGS_DIR}"
  68. fi
  69. # # print the .submit header
  70. if [ "$CONDOR" == "yes" ]; then
  71. printf "# The environmentls
  72. universe = vanilla
  73. getenv = True
  74. request_cpus = ${CPUS}
  75. request_memory = ${RAM}
  76. request_disk = ${DISK}
  77. Requirements = HasSingularity
  78. # Execution
  79. executable = ${RUN_SINGULARITY_FILE}
  80. notification = Error
  81. \n"
  82. fi
  83. # Loop over subjects in INPUT_DIR no BIDS
  84. for type in "${EXTENSIONS[@]}"; do
  85. if [ "$BIDS" == "no" ]; then
  86. # no bids
  87. subj_list=($( find "$INPUT_DIR" -maxdepth 1 -name "*.${type}" \( -type f -o -type l \) ))
  88. else
  89. # bids
  90. subj_list=($( find "${INPUT_DIR}"/sub-*/ses-*/anat -maxdepth 1 -name "*T1*.${type}" \( -type f -o -type l \) ))
  91. fi
  92. for subj in "${subj_list[@]}"; do
  93. #printf "Processing subject: $subj \n" >&2
  94. if [ "$CONDOR" == "yes" ]; then
  95. sub=$(basename $subj)
  96. printf "arguments = $subj \ \n $OUTPUT_DIR \ \n $CONTAINER_PATH \ \n $SAVE $RAW \ \n"
  97. printf "log = ${LOGS_DIR}/\$(Cluster).\$(Process).${sub::-11}.log\n"
  98. printf "output = ${LOGS_DIR}/\$(Cluster).\$(Process).${sub::-11}.out\n"
  99. printf "error = ${LOGS_DIR}/\$(Cluster).\$(Process).${sub::-11}.err\n"
  100. printf "Queue\n\n"
  101. else
  102. printf "${RUN_SINGULARITY_FILE} $subj $OUTPUT_DIR $CONTAINER_PATH $SAVE $RAW \n"
  103. fi
  104. done
  105. done