ds003604/code/dwi_qa/eddy_qa_output.sh
2021-04-08 18:48:11 +00:00

84 lines
3.2 KiB (Stored with Git Annex)
Shell

#! bin/bash
# you must module load afni (. collection_moduleload.sh in the $BDL folder)
root=/dors/gpc/JamesBooth/JBooth-Lab/BDL/ELP/bids_dwi_preproc3
cd $root
subjects=$(ls | grep "sub-5*")
ses=ses-9
writefile='dwi_qa_ses-9.txt'
if [ -f "$writefile" ]; then rm $writefile; fi
touch $writefile
echo sub shortnm acq_date num_vol abs_rms_mean rlt_rms_mean outlier_pct b0_snr_mean dwi_cnr_mean > $writefile
# loop through subjects and folders
for sub in $subjects
do
echo $sub
cd $root/${sub}/${ses}
folder=$(ls | grep "dwi")
cd $root/${sub}/${ses}/$folder
files=$(ls | grep "dwi.nii$")
rms_files=$(ls | grep "eddy_movement_rms")
cnr_files=$(ls | grep "eddy_cnr")
outlier_files=$(ls | grep "eddy_outlier_map")
bval_files=$(ls | grep "bval")
json_files=$(ls | grep "json")
for f in $files
do
shortnm=$(basename $f .nii)
echo $shortnm
#get the acq_date for each run
for json_f in $json_files
do
if [[ "$json_f" == *"$shortnm"* ]]; then json_thisrun=$json_f; fi
done
line=$(cat $json_thisrun | grep 'Shifted')
acq_date=$(echo $line | tr -d '"ShiftedAcquisitionDate":' | sed 's/000000$//')
echo "acq_date" $acq_date
#get the number ofvolumes using bval file
for bval_f in $bval_files
do
if [[ "$bval_f" == *"$shortnm"* ]]; then bval_thisrun=$bval_f; fi
done
num_vol=$(echo $(cat $bval_thisrun | grep -o ' ' | wc -l) + 1 | bc)
echo "num_vol" $num_vol
#get the rms values and calculate the mean
for rms_f in $rms_files
do
if [[ "$rms_f" == *"$shortnm"* ]]; then rms_thisrun=$rms_f; fi
done
abs_rms_mean=$(awk '{ total += $1; count++ } END { print total/count}' $rms_thisrun)
rlt_rms_mean=$(awk '{ total += $2; count++ } END { print total/count}' $rms_thisrun)
echo "rms" $abs_rms_mean $rlt_rms_mean
#get the outlier map and calculate the percentage of ourlier slices
for outlier_f in $outlier_files
do
if [[ "$outlier_f" == *"$shortnm"* ]]; then outlier_thisrun=$outlier_f; fi
done
sed '1d' $outlier_thisrun > outlier_num.txt
outlier_count1=$(tr -cd 1 < outlier_num.txt | wc -m)
outlier_count0=$(tr -cd 0 < outlier_num.txt | wc -m)
outlier_total=$(expr $outlier_count1 + $outlier_count0)
outlier_pct=$(echo $outlier_count1/$outlier_total | bc -l)
echo "outliers" $outlier_count1 $outlier_count0 $outlier_total
printf "%0.10f\n" $outlier_pct
rm outlier_num.txt
#get the cnr maps and calculate the b0_snr and dwi_cnr average
for cnr_f in $cnr_files
do
if [[ "$cnr_f" == *"$shortnm"* ]]; then cnr_thisrun=$cnr_f; fi
done
$FSLDIR/bin/fslsplit $cnr_thisrun
3dmaskdump -mask ${shortnm}_bet_brain_mask.nii vol0000.nii > ${shortnm}_b0_snr_brain.txt
b0_snr_mean=$(awk '{ total += $4; count++ } END { print total/count }' ${shortnm}_b0_snr_brain.txt)
3dmaskdump -mask ${shortnm}_bet_brain_mask.nii vol0001.nii > ${shortnm}_dwi_cnr_brain.txt
dwi_cnr_mean=$(awk '{ total += $4; count++ } END { print total/count }' ${shortnm}_dwi_cnr_brain.txt)
rm vol0000.nii.gz vol0001.nii.gz ${shortnm}_b0_snr_brain.txt ${shortnm}_dwi_cnr_brain.txt
echo "snrcnr" $b0_snr_mean $dwi_cnr_mean
echo $sub $shortnm $acq_date $num_vol $abs_rms_mean $rlt_rms_mean $outlier_pct $b0_snr_mean $dwi_cnr_mean >> ${root}/$writefile
done # for files
done #for subjects