#!/usr/bin/env bash # v 1.0 DEBUG=1 # sub_file=/data/project/brainage_underrepresented/data/ds004107/sub-mind010/ses-01/anat/sub-mind010_ses-01_T1w.nii.gz sub_path=$1 # check that this is a file if [ -e "$sub_path" ] || [ -L "$sub_path" ]; then echo "Input file exists or is a symbolic link." else echo "Input file does not exist, exiting." exit 1 fi # dir_sub dir_path=$(dirname "${sub_path}") # sub_file=sub-mind010_ses-01_T1w.nii.gz (or .nii, or .mgz) sub_file=$(basename "${sub_path}") # sub_name=sub-mind010_ses-01_T1w sub_name="${sub_file%%.*}" # out_dir=derivatives/brainageR/sub-01490270a/ses-BLT11 out_dir=$2 # Container path container_path=$3 # Save or no the intermidiate files save=$4 # Raw file ? raw=$5 if [ $DEBUG -eq 1 ]; then echo "sub_file = $sub_path" echo "dir_path = $dir_path" echo "sub_file = $sub_file" echo "sub_name = $sub_name" echo "out_dir = $out_dir" echo "save = $save" echo "raw = $raw" fi # create result folder and move the results in there if [ -e "$out_dir/${sub_name}_brainageR.csv" ]; then echo "output file exists, exiting" exit else mkdir -p $out_dir fi cd "${dir_path}" datalad get "${sub_file}" # define working dir, create a 'data' folder in it and copy the subject in. work_dir=$(mktemp -d) mkdir ${work_dir}/data cp "${sub_path}" "${work_dir}/data" datalad drop "${sub_path}" cd ${work_dir} # unzip as SPM/CAT work with .nii files. Remove the nii.gz file and rename sub # gunzip only if gz file and then delete .gz file if [[ "$sub_file" == *.gz ]]; then echo "The file is a .gz file." gunzip data/${sub_file} rm -f data/${sub_file} sub_file=${sub_file%".gz"} else echo "The file is not a .gz file." fi # make sure permissions are ok chmod +rwx ${work_dir}/data/$sub_file # run singularity with brainageR code # ---------------------------------------------------------- echo "Running the container" #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 if [ ${raw} -ne 0 ]; then singularity run -B ${work_dir}/data,$(pwd) --cleanenv ${container_path} -b /code/cat_standalone_segment_enigma.m ${sub_path} fi singularity run --fakeroot -B /data/project/BrainAge4AD/tools/brainage_enigma_container/CAT_stacking_container,${work_dir} ${simg_pred} ${dir_path} ${work_dir} # Check the exit status of the previous command if [ $? -ne 0 ]; then echo "Singularity container failed, exiting." >&2 exit 1 fi # ---------------------------------------------------------- # Moving the prediction into the output dir mv $work_dir/${sub_name}_brain_predicted.age.csv $out_dir/${sub_name}_brainageR.csv echo "Prediction file sub${sub_name} saved in $out_dir" # If user wants to save intermidiate files, moving everything into output dir if [ $save == "yes" ]; then mkdir -p $out_dir/${sub_name}_brainageR mv -r $work_dir/* $out_dir/${sub_name}_brainageR/ echo "All files of ${sub_name} saved in $out_dir/${sub_name}_brainageR" fi echo "Cleaning tmp files" rm -rf $work_dir exit