run_ENIGMA_stacking.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env bash
  2. # v 1.0
  3. DEBUG=1
  4. # sub_file=/data/project/brainage_underrepresented/data/ds004107/sub-mind010/ses-01/anat/sub-mind010_ses-01_T1w.nii.gz
  5. sub_path=$1
  6. # check that this is a file
  7. if [ -e "$sub_path" ] || [ -L "$sub_path" ]; then
  8. echo "Input file exists or is a symbolic link."
  9. else
  10. echo "Input file does not exist, exiting."
  11. exit 1
  12. fi
  13. # dir_sub
  14. dir_path=$(dirname "${sub_path}")
  15. # sub_file=sub-mind010_ses-01_T1w.nii.gz (or .nii, or .mgz)
  16. sub_file=$(basename "${sub_path}")
  17. # sub_name=sub-mind010_ses-01_T1w
  18. sub_name="${sub_file%%.*}"
  19. # out_dir=derivatives/brainageR/sub-01490270a/ses-BLT11
  20. out_dir=$2
  21. # Container path
  22. container_path=$3
  23. # Save or no the intermidiate files
  24. save=$4
  25. # Raw file ?
  26. raw=$5
  27. if [ $DEBUG -eq 1 ]; then
  28. echo "sub_file = $sub_path"
  29. echo "dir_path = $dir_path"
  30. echo "sub_file = $sub_file"
  31. echo "sub_name = $sub_name"
  32. echo "out_dir = $out_dir"
  33. echo "save = $save"
  34. echo "raw = $raw"
  35. fi
  36. # create result folder and move the results in there
  37. if [ -e "$out_dir/${sub_name}_brainageR.csv" ]; then
  38. echo "output file exists, exiting"
  39. exit
  40. else
  41. mkdir -p $out_dir
  42. fi
  43. cd "${dir_path}"
  44. datalad get "${sub_file}"
  45. # define working dir, create a 'data' folder in it and copy the subject in.
  46. work_dir=$(mktemp -d)
  47. mkdir ${work_dir}/data
  48. cp "${sub_path}" "${work_dir}/data"
  49. datalad drop "${sub_path}"
  50. cd ${work_dir}
  51. # unzip as SPM/CAT work with .nii files. Remove the nii.gz file and rename sub
  52. # gunzip only if gz file and then delete .gz file
  53. if [[ "$sub_file" == *.gz ]]; then
  54. echo "The file is a .gz file."
  55. gunzip data/${sub_file}
  56. rm -f data/${sub_file}
  57. sub_file=${sub_file%".gz"}
  58. else
  59. echo "The file is not a .gz file."
  60. fi
  61. # make sure permissions are ok
  62. chmod +rwx ${work_dir}/data/$sub_file
  63. # run singularity with brainageR code
  64. # ----------------------------------------------------------
  65. echo "Running the container"
  66. #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
  67. if [ ${raw} -ne 0 ]; then
  68. singularity run -B ${work_dir}/data,$(pwd) --cleanenv ${container_path} -b /code/cat_standalone_segment_enigma.m ${sub_path}
  69. fi
  70. singularity run --fakeroot -B /data/project/BrainAge4AD/tools/brainage_enigma_container/CAT_stacking_container,${work_dir} ${simg_pred} ${dir_path} ${work_dir}
  71. # Check the exit status of the previous command
  72. if [ $? -ne 0 ]; then
  73. echo "Singularity container failed, exiting." >&2
  74. exit 1
  75. fi
  76. # ----------------------------------------------------------
  77. # Moving the prediction into the output dir
  78. mv $work_dir/${sub_name}_brain_predicted.age.csv $out_dir/${sub_name}_brainageR.csv
  79. echo "Prediction file sub${sub_name} saved in $out_dir"
  80. # If user wants to save intermidiate files, moving everything into output dir
  81. if [ $save == "yes" ]; then
  82. mkdir -p $out_dir/${sub_name}_brainageR
  83. mv -r $work_dir/* $out_dir/${sub_name}_brainageR/
  84. echo "All files of ${sub_name} saved in $out_dir/${sub_name}_brainageR"
  85. fi
  86. echo "Cleaning tmp files"
  87. rm -rf $work_dir
  88. exit