run_brainageR.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env bash
  2. # v 1.0
  3. DEBUG=1
  4. # Initialize variables with default values
  5. sub_path=""
  6. out_dir=""
  7. container_path=""
  8. save="no"
  9. raw="yes"
  10. model="vanilla"
  11. # Parse command-line arguments
  12. while [[ "$#" -gt 0 ]]; do
  13. case $1 in
  14. -input) sub_path="$2"; shift ;;
  15. -output) out_dir="$2"; shift ;;
  16. -RAW) raw="$2"; shift ;;
  17. -SAVE_FILE) save="$2"; shift ;;
  18. -CONTAINER_PATH) container_path="$2"; shift ;;
  19. -MODEL_NAME) model="$2"; shift ;;
  20. -help)
  21. echo "Usage: $0 -input <input_dir> -output <output_dir> -RAW <yes/no> -SAVE_FILE <yes/no> -CONTAINER_PATH <path> -MODEL_NAME <name>"
  22. exit 0
  23. ;;
  24. *) echo "Unknown parameter passed: $1"; exit 1 ;;
  25. esac
  26. shift
  27. done
  28. # check that this is a file
  29. if [ -e "$sub_path" ] || [ -L "$sub_path" ]; then
  30. echo "Input file exists or is a symbolic link."
  31. else
  32. echo "Input file does not exist, exiting."
  33. exit 1
  34. fi
  35. # sub_path=/data/project/brainage_underrepresented/data/ds004107/sub-mind010/ses-01/anat/sub-mind010_ses-01_T1w.nii.gz
  36. # dir_sub
  37. dir_path=$(dirname "${sub_path}")
  38. # sub_file=sub-mind010_ses-01_T1w.nii.gz (or .nii, or .mgz)
  39. sub_file=$(basename "${sub_path}")
  40. # sub_name=sub-mind010_ses-01_T1w
  41. sub_name="${sub_file%%.*}"
  42. # Debugging or verbose mode can be added here if needed.
  43. if [ $DEBUG -eq 1 ]; then
  44. echo "sub_file = $sub_path"
  45. echo "dir_path = $dir_path"
  46. echo "sub_file = $sub_file"
  47. echo "sub_name = $sub_name"
  48. echo "out_dir = $out_dir"
  49. echo "save = $save"
  50. echo "raw = $raw"
  51. echo "container_path = $container_path"
  52. echo "model = $model"
  53. fi
  54. # create result folder and move the results in there
  55. if [ -e "$out_dir/${sub_name}_brainageR.csv" ]; then
  56. echo "output file exists, exiting"
  57. exit
  58. else
  59. mkdir -p $out_dir
  60. fi
  61. cd "${dir_path}"
  62. datalad_dir_path=$(git -C ${dir_path} rev-parse --show-toplevel)
  63. if [ -d ${datalad_dir_path} ]; then
  64. datalad_work_dir=$(mktemp -d)
  65. cd ${datalad_work_dir}
  66. datalad clone ${datalad_dir_path} .
  67. # Trying to get sub-mind010/ses-01/anat/sub-mind010_ses-01_T1w.nii.gz
  68. complement="${sub_path#"$datalad_dir_path"}"
  69. complement_dir_name=$(dirname "$complement" | sed 's|^/||')
  70. cd $complement_dir_name
  71. sub_path=$(pwd)/$sub_file
  72. datalad get "${sub_file}"
  73. # Check the exit status of the previous command
  74. if [ $? -ne 0 ]; then
  75. echo "datalad cloning failed, exiting." >&2
  76. exit 1
  77. fi
  78. fi
  79. # define working dir, create a 'data' folder in it and copy the subject in.
  80. work_dir=$(mktemp -d)
  81. mkdir -p ${work_dir}/data/anat
  82. cp "${sub_path}" "${work_dir}/data/anat"
  83. if [ $? -ne 0 ]; then
  84. echo "datalad cloning failed, exiting." >&2
  85. exit 1
  86. fi
  87. if [ -d ${datalad_dir_path} ]; then
  88. # datalad drop "${sub_path}"
  89. chmod -R a+w $datalad_work_dir
  90. rm -fr $datalad_work_dir
  91. fi
  92. cd ${work_dir}
  93. # unzip as SPM/CAT work with .nii files. Remove the nii.gz file and rename sub
  94. # gunzip only if gz file and then delete .gz file
  95. if [[ "$sub_file" == *.gz ]]; then
  96. echo "The file is a .gz file."
  97. gunzip data/anat/${sub_file}
  98. rm -f data/anat/${sub_file}
  99. sub_file=${sub_file%".gz"}
  100. else
  101. echo "The file is not a .gz file."
  102. fi
  103. # make sure permissions are ok
  104. chmod +rwx ${work_dir}/data/anat/$sub_file
  105. # run singularity with brainageR code
  106. # ----------------------------------------------------------
  107. echo "Running the container"
  108. #singularity exec /data/project/BrainAge4AD/tools/brainageR_dockerfile/brainageR2.1.sif brainageR -f ${work_dir}/data/${sub_file} -o ${sub_name}_brain_predicted.age.csv
  109. singularity exec ${container_path}/brainageR2.1.sif brainageR -f ${work_dir}/data/anat/${sub_file} -o ${sub_name}_brain_predicted.age.csv
  110. # Check the exit status of the previous command
  111. if [ $? -ne 0 ]; then
  112. echo "Singularity container failed, exiting." >&2
  113. exit 1
  114. fi
  115. # ----------------------------------------------------------
  116. # Moving the prediction into the output dir
  117. mv $work_dir/${sub_name}_brain_predicted.age.csv $out_dir/${sub_name}_brainageR.csv
  118. # Check if the last command was successful
  119. if [ $? -eq 0 ]; then
  120. echo "Prediction file ${sub_name} saved in $out_dir"
  121. else
  122. echo "There was a problem"
  123. fi
  124. # If user wants to save intermidiate files, moving everything into output dir
  125. if [ $save == "yes" ]; then
  126. mkdir -p $out_dir/${sub_name}_brainageR
  127. mv $work_dir/* $out_dir/${sub_name}_brainageR/
  128. echo "All files of ${sub_name} saved in $out_dir/${sub_name}_brainageR"
  129. # Check if the last command was successful
  130. if [ $? -eq 0 ]; then
  131. echo "All files of ${sub_name} saved in $out_dir/${sub_name}_brainageR"
  132. else
  133. echo "There was a problem"
  134. fi
  135. fi
  136. echo "Cleaning tmp files"
  137. rm -rf $work_dir
  138. exit