From 6ac1ed3d6241edac563834953370e595b37f36ab Mon Sep 17 00:00:00 2001 From: ahheckel Date: Thu, 1 Jan 2015 20:31:06 +0100 Subject: [PATCH] modified: README.md modified: rsc/scripts/extractmask.sh modified: rsc/scripts/extractmask_z.sh new file: rsc/scripts/mkniigz.sh modified: rsc/scripts/nautilus-scripts/FSL_fslview modified: rsc/scripts/thres_z.sh --- README.md | 2 + rsc/scripts/extractmask.sh | 41 ++++++++++++---- rsc/scripts/extractmask_z.sh | 62 ++++++++++++++++++------ rsc/scripts/mkniigz.sh | 50 +++++++++++++++++++ rsc/scripts/nautilus-scripts/FSL_fslview | 2 +- rsc/scripts/thres_z.sh | 3 +- 6 files changed, 133 insertions(+), 27 deletions(-) create mode 100755 rsc/scripts/mkniigz.sh diff --git a/README.md b/README.md index 21e028b..e005cf6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ ## FSL-scripts Script-framework using FSL, Freesurfer and AFNI functionality for automatization and high-throughput processing of MRI data in clustered environments. +This work was supported by the PostDoc Programme of the Medical Faculty (University of Heidelberg). ### Features * Fully automated cascades up to 2nd-level GLM stats for cross-sectional and longitudinal designs @@ -51,6 +52,7 @@ FSL/Freesurfer installations differ from what is expected by this framework. and/or acquisition parameters vary across subjects/sessions). * Run ```./run_script.sh [bg]``` to start processing. + Example directory structure: ``` diff --git a/rsc/scripts/extractmask.sh b/rsc/scripts/extractmask.sh index b81fb2f..e42b920 100755 --- a/rsc/scripts/extractmask.sh +++ b/rsc/scripts/extractmask.sh @@ -30,7 +30,7 @@ function row2col() Usage() { echo "" - echo "Usage: `basename $0` [--vxcount|--extract] [-bin]" + echo "Usage: `basename $0` [--vxcount|--extract|--std] [-bin]" echo "Example: `basename $0` --extract \"FA-1 FA-2 FA-3\" \"FA-mask-1 FA-mask-2 FA-mask-3\" FA_table.txt -bin" echo " `basename $0` --vxcount FA-list.txt FA-mask-list.txt FA_table.txt" echo "" @@ -38,9 +38,10 @@ Usage() { } # parse options -dovx=1 ; doex=1 -if [ "$1" = "--vxcount" ] ; then dovx=1 ; doex=0 ; shift ; fi -if [ "$1" = "--extract" ] ; then dovx=0 ; doex=1 ; shift ; fi +dovx=1 ; doex=1 ; dostd=1 +if [ "$1" = "--vxcount" ] ; then dovx=1 ; doex=0 ; dostd=0 ; shift ; fi +if [ "$1" = "--extract" ] ; then dovx=0 ; doex=1 ; dostd=0 ; shift ; fi +if [ "$1" = "--std" ] ; then dovx=0 ; doex=0 ; dostd=1 ; shift ; fi # assign input arguments [ "$3" = "" ] && Usage @@ -107,14 +108,14 @@ trap "rm -f $tmpdir/* ; rmdir $tmpdir ; exit" EXIT # checks echo $masks | row2col > $tmpdir/masks.txt echo $inputs | row2col > $tmpdir/inputs.txt -n_lines1=$(cat $tmpdir/masks.txt | wc -l) -n_lines2=$(cat $tmpdir/inputs.txt | wc -l) +n_lines1=$(cat $tmpdir/masks.txt | grep -v ^# | awk '{print $1}' | wc -l) +n_lines2=$(cat $tmpdir/inputs.txt| grep -v ^# | wc -l) if [ $n_lines1 -ne $n_lines2 ] ; then echo "`basename $0`: ERROR : Number of inputs ('$n_lines2') and number of masks ($n_lines1) do not match. Exiting" ; exit 1 fi # looping... -outs_tmp="" ; header="" ; vxouts_tmp="" +outs_tmp="" ; header="" ; vxouts_tmp="" ; stdouts_tmp="" for counter in `seq 1 $n_lines2` ; do mask="$(cat $tmpdir/masks.txt | sed -n ${counter}p)" input="$(cat $tmpdir/inputs.txt | sed -n ${counter}p)" @@ -146,6 +147,7 @@ for counter in `seq 1 $n_lines2` ; do echo "`basename $0` : markers: 1 - ${n0max}" echo "`basename $0` : do-extract: $doex" echo "`basename $0` : do-vxcount: $dovx" + echo "`basename $0` : do-std: $dostd" echo "`basename $0` : txt-out: $out" echo "---------------------------" @@ -171,6 +173,19 @@ for counter in `seq 1 $n_lines2` ; do echo $vxcount > $tmpdir/vxout_$(basename $input)_$(basename $mask) vxouts_tmp=$vxouts_tmp" "$tmpdir/vxout_$(basename $input)_$(basename $mask) fi + + # collect std + if [ $dostd -eq 1 ] ; then + std="" + for n in `seq 1 $n0max` ; do # for each "color" + cmd="$(dirname $0)/seg_mask.sh $mask $n $tmpdir/$(basename $mask)_$(zeropad $n 3)" + $cmd 1 > /dev/null + cmd="fslstats $input -k $tmpdir/$(basename $mask)_$(zeropad $n 3) -s" + echo $cmd ; std=$std" "`$cmd` + done + echo $std > $tmpdir/stdout_$(basename $input)_$(basename $mask) + stdouts_tmp=$stdouts_tmp" "$tmpdir/stdout_$(basename $input)_$(basename $mask) + fi done # horz-cat... @@ -178,15 +193,21 @@ echo $header | row2col > $tmpdir/header # ...extraction if [ $doex -eq 1 ] ; then cat $outs_tmp > $tmpdir/out - echo "paste -d \" \" $tmpdir/header $tmpdir/out > ${out}" - paste -d " " $tmpdir/header $tmpdir/out > ${out} + echo "paste -d \" \" $tmpdir/header $tmpdir/out > $(dirname $out)/meants_$(basename $out)" # info + paste -d " " $tmpdir/header $tmpdir/out > $(dirname $out)/meants_$(basename $out) fi # ...voxelcount if [ $dovx -eq 1 ] ; then cat $vxouts_tmp > $tmpdir/vxout - echo "paste -d \" \" $tmpdir/header $tmpdir/vxout > $(dirname $out)/vxcount_$(basename $out)" + echo "paste -d \" \" $tmpdir/header $tmpdir/vxout > $(dirname $out)/vxcount_$(basename $out)" # info paste -d " " $tmpdir/header $tmpdir/vxout > $(dirname $out)/vxcount_$(basename $out) fi +# ...std +if [ $dostd -eq 1 ] ; then + cat $stdouts_tmp > $tmpdir/stdout + echo "paste -d \" \" $tmpdir/header $tmpdir/stdout > $(dirname $out)/stdout_$(basename $out)" # info + paste -d " " $tmpdir/header $tmpdir/stdout > $(dirname $out)/stdout_$(basename $out) +fi # done. echo "`basename $0` : done." diff --git a/rsc/scripts/extractmask_z.sh b/rsc/scripts/extractmask_z.sh index 1dcded0..d971073 100755 --- a/rsc/scripts/extractmask_z.sh +++ b/rsc/scripts/extractmask_z.sh @@ -47,7 +47,7 @@ function getMax() # finds maximum in column Usage() { echo "" - echo "Usage: `basename $0` [--vxcount|--extract] [<\"-bin\"|none>](def:none) [<\"fslmeants-opts\"|none>](def:none) [](def:0,0)" + echo "Usage: `basename $0` [--vxcount|--extract|--std] [<\"-bin\"|none>](def:none) [<\"fslmeants-opts\"|none>](def:none) [](def:0,0)" echo "Example: `basename $0` --extract \"FA-1 FA-2 FA-3\" \"FA-mask-1 FA-mask-2 FA-mask-3\" FA_vals.txt \"-bin\" \"--showall\" 0,1" echo " `basename $0` --vxcount FA-list.txt FA-mask-list.txt FA_vals.txt" echo "" @@ -55,9 +55,10 @@ Usage() { } # parse options -dovx=1 ; doex=1 -if [ "$1" = "--vxcount" ] ; then dovx=1 ; doex=0 ; shift ; fi -if [ "$1" = "--extract" ] ; then dovx=0 ; doex=1 ; shift ; fi +dovx=1 ; doex=1 ; dostd=1 +if [ "$1" = "--vxcount" ] ; then dovx=1 ; doex=0 ; dostd=0 ; shift ; fi +if [ "$1" = "--extract" ] ; then dovx=0 ; doex=1 ; dostd=0 ; shift ; fi +if [ "$1" = "--std" ] ; then dovx=0 ; doex=0 ; dostd=1 ; shift ; fi # assign input arguments [ "$3" = "" ] && Usage @@ -153,7 +154,7 @@ if [ $n_lines1 -ne $n_lines3 -a x"$shiftvector" != "x0" ] ; then fi # looping... -n_mask=0 ; mask_tmp="" ; header="" ; vxmask_tmp="" +n_mask=0 ; mask_tmp="" ; header="" ; vxmask_tmp="" ; stdmask_tmp="" for counter in `seq 1 $n_lines2` ; do mask="$(cat $tmpdir/masks.txt | sed -n ${counter}p)" input="$(cat $tmpdir/inputs.txt | sed -n ${counter}p)" @@ -197,6 +198,7 @@ for counter in `seq 1 $n_lines2` ; do echo "`basename $0` : vert-cat subj: $vertcat1" echo "`basename $0` : do-extract: $doex" echo "`basename $0` : do-vxcount: $dovx" + echo "`basename $0` : do-std: $dostd" echo "`basename $0` : txt-out: $out" echo "---------------------------" @@ -207,9 +209,9 @@ for counter in `seq 1 $n_lines2` ; do $(dirname $0)/split4D.sh z $mask [0:1:end] $tmpdir/$(basename $mask) # looping... - rm -f $tmpdir/meants_??? ; outs_tmp="" ; vxouts_tmp="" + rm -f $tmpdir/meants_??? ; outs_tmp="" ; vxouts_tmp="" ; stdouts_tmp="" for n in `seq 1 $n0max` ; do # for each "color" - meantsfiles="" ; vxfiles="" + meantsfiles="" ; vxfiles="" ; stdfiles="" for i in `seq 0 $[$Z-1]` ; do # for each slice # segment cmd="$(dirname $0)/seg_mask.sh $tmpdir/$(basename $mask)_slice_$(zeropad $i 4) $n $tmpdir/$(basename $mask)_slice_$(zeropad $i 4)_$(zeropad $n 3)" @@ -237,14 +239,22 @@ for counter in `seq 1 $n_lines2` ; do echo $cmd ; $cmd | cut -d " " -f 1 > $tmpdir/vx_$(zeropad $i 4)_$(zeropad $n 3) fi + # get std + if [ $dostd -eq 1 ] ; then + cmd="fslstats $tmpdir/$(basename $input)_slice_$(zeropad $i 4) -k $tmpdir/$(basename $mask)_slice_$(zeropad $i 4)_$(zeropad $n 3) -s" + echo $cmd ; $cmd > $tmpdir/std_$(zeropad $i 4)_$(zeropad $n 3) + fi + # collect per-slice outputs if [ $doex -eq 1 ] ; then meantsfiles=$meantsfiles" "$tmpdir/meants_$(zeropad $i 4)_$(zeropad $n 3) ; fi if [ $dovx -eq 1 ] ; then vxfiles=$vxfiles" "$tmpdir/vx_$(zeropad $i 4)_$(zeropad $n 3) ; fi + if [ $dostd -eq 1 ] ; then stdfiles=$stdfiles" "$tmpdir/std_$(zeropad $i 4)_$(zeropad $n 3) ; fi done # vert-cat per-slice outputs if [ $doex -eq 1 ] ; then cat $meantsfiles > $tmpdir/out_$(zeropad $n 4) ; fi if [ $dovx -eq 1 ] ; then cat $vxfiles > $tmpdir/vxout_$(zeropad $n 4) ; fi + if [ $dostd -eq 1 ] ; then cat $stdfiles > $tmpdir/stdout_$(zeropad $n 4) ; fi # shift slices if applicable if [ x"$shiftvector" != "x0" ] ; then @@ -255,7 +265,7 @@ for counter in `seq 1 $n_lines2` ; do cat $tmpdir/__out_$(zeropad $n 4) >> $tmpdir/out_$(zeropad $n 4) for sl in `seq 1 $[$addlow-$_shift]` ; do echo 0 ; done >> $tmpdir/out_$(zeropad $n 4) rm $tmpdir/__out_$(zeropad $n 4) - #cat $tmpdir/vxout_$(zeropad $n 4) + #cat $tmpdir/out_$(zeropad $n 4) fi # shift voxel count/volume if [ $dovx -eq 1 ] ; then @@ -266,16 +276,27 @@ for counter in `seq 1 $n_lines2` ; do rm $tmpdir/__vxout_$(zeropad $n 4) #cat $tmpdir/vxout_$(zeropad $n 4) fi + # shift std + if [ $dostd -eq 1 ] ; then + mv $tmpdir/stdout_$(zeropad $n 4) $tmpdir/__stdout_$(zeropad $n 4) + for sl in `seq 1 $[$addhigh+$_shift]` ; do echo 0 ; done > $tmpdir/stdout_$(zeropad $n 4) + cat $tmpdir/__stdout_$(zeropad $n 4) >> $tmpdir/stdout_$(zeropad $n 4) + for sl in `seq 1 $[$addlow-$_shift]` ; do echo 0 ; done >> $tmpdir/stdout_$(zeropad $n 4) + rm $tmpdir/__stdout_$(zeropad $n 4) + #cat $tmpdir/stdout_$(zeropad $n 4) + fi fi # do heading header=$(basename $out)__$(basename $input)__$(basename $mask)_$(zeropad $n 3) if [ $doex -eq 1 ] ; then sed -i "1i $header" $tmpdir/out_$(zeropad $n 4) ; fi if [ $dovx -eq 1 ] ; then sed -i "1i $header" $tmpdir/vxout_$(zeropad $n 4) ; fi + if [ $dostd -eq 1 ] ; then sed -i "1i $header" $tmpdir/stdout_$(zeropad $n 4) ; fi # collect n outputs (n=number of pen-values) if [ $doex -eq 1 ] ; then outs_tmp=$outs_tmp" "$tmpdir/out_$(zeropad $n 4) ; fi if [ $dovx -eq 1 ] ; then vxouts_tmp=$vxouts_tmp" "$tmpdir/vxout_$(zeropad $n 4) ; fi + if [ $dostd -eq 1 ] ; then stdouts_tmp=$stdouts_tmp" "$tmpdir/stdout_$(zeropad $n 4) ; fi echo "" done @@ -284,14 +305,17 @@ for counter in `seq 1 $n_lines2` ; do if [ $vertcat0 -eq 1 ] ; then if [ $doex -eq 1 ] ; then cat $outs_tmp > $tmpdir/$(basename $out)_$(zeropad $n_mask 3) ; fi if [ $dovx -eq 1 ] ; then cat $vxouts_tmp > $tmpdir/vx_$(basename $out)_$(zeropad $n_mask 3) ; fi + if [ $dostd -eq 1 ] ; then cat $stdouts_tmp > $tmpdir/std_$(basename $out)_$(zeropad $n_mask 3) ; fi else if [ $doex -eq 1 ] ; then paste -d " " $outs_tmp > $tmpdir/$(basename $out)_$(zeropad $n_mask 3) ; fi if [ $dovx -eq 1 ] ; then paste -d " " $vxouts_tmp > $tmpdir/vx_$(basename $out)_$(zeropad $n_mask 3) ; fi + if [ $dostd -eq 1 ] ; then paste -d " " $stdouts_tmp > $tmpdir/std_$(basename $out)_$(zeropad $n_mask 3) ; fi fi - + # collect subjects if [ $doex -eq 1 ] ; then mask_tmp=$mask_tmp" "$tmpdir/$(basename $out)_$(zeropad $n_mask 3) ; fi if [ $dovx -eq 1 ] ; then vxmask_tmp=$vxmask_tmp" "$tmpdir/vx_$(basename $out)_$(zeropad $n_mask 3) ; fi + if [ $dostd -eq 1 ] ; then stdmask_tmp=$stdmask_tmp" "$tmpdir/std_$(basename $out)_$(zeropad $n_mask 3) ; fi # increment n_mask=$[$n_mask+1] @@ -300,22 +324,30 @@ done # horz-cat or vert-cat subjects ? if [ $vertcat1 -eq 1 ] ; then if [ $doex -eq 1 ] ; then - echo "cat $mask_tmp > $out" - cat $mask_tmp > $out + echo "cat $mask_tmp > $(dirname $out)/meants_$(basename $out)" # info + cat $mask_tmp > $(dirname $out)/meants_$(basename $out) fi if [ $dovx -eq 1 ] ; then - echo "cat $vxmask_tmp > $(dirname $out)/vxcount_$(basename $out)" + echo "cat $vxmask_tmp > $(dirname $out)/vxcount_$(basename $out)" # info cat $vxmask_tmp > $(dirname $out)/vxcount_$(basename $out) fi + if [ $dostd -eq 1 ] ; then + echo "$stdmask_tmp > $(dirname $out)/stdout_$(basename $out)" # info + cat $stdmask_tmp > $(dirname $out)/stdout_$(basename $out) + fi else if [ $doex -eq 1 ] ; then - echo "paste -d \" \" $mask_tmp > $out" - paste -d " " $mask_tmp > $out + echo "paste -d \" \" $mask_tmp > $(dirname $out)/meants_$(basename $out)" # info + paste -d " " $mask_tmp > $(dirname $out)/meants_$(basename $out) fi if [ $dovx -eq 1 ] ; then - echo "paste -d \" \" $vxmask_tmp > $(dirname $out)/vxcount_$(basename $out)" + echo "paste -d \" \" $vxmask_tmp > $(dirname $out)/vxcount_$(basename $out)" # info paste -d " " $vxmask_tmp > $(dirname $out)/vxcount_$(basename $out) fi + if [ $dostd -eq 1 ] ; then + echo "paste -d \" \" $stdmask_tmp > $(dirname $out)/std_$(basename $out)" # info + paste -d " " $stdmask_tmp > $(dirname $out)/stdout_$(basename $out) + fi fi # done. diff --git a/rsc/scripts/mkniigz.sh b/rsc/scripts/mkniigz.sh new file mode 100755 index 0000000..7210622 --- /dev/null +++ b/rsc/scripts/mkniigz.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# Converts .nii/.img/.hdr to .nii.gz and deletes original file. + +# Written by Andreas Heckel +# University of Heidelberg +# heckelandreas@googlemail.com +# https://github.com/ahheckel +# 28/10/2014 + +trap 'echo "$0 : An ERROR has occured."' ERR + +set -e + +Usage() { + echo "" + echo "Usage: `basename $0` []" + echo "Example: `basename $0` input.hdr output.nii.gz" + echo " `basename $0` input.hdr input" + echo " `basename $0` input.img" + echo " `basename $0` input.nii" + echo "" + exit 1 +} + +[ "$1" = "" ] && Usage + +# define vars +input="$1" ; in_ext=${input##*.} +input=`remove_ext "$1"` +output=`remove_ext "$2"` +if [ x"$output" = "x" ] ; then output="$input" ; fi + +# check +if [ $(imtest $input) -eq 0 ] ; then echo "`basename $0` : ERROR : cannot read '$input' - exiting." ; exit ; fi + +# execute +cmd="fslmaths $input ${output}.nii.gz" +echo "`basename $0` : $cmd" ; $cmd +if [ "${in_ext}" = "hdr" -o "${in_ext}" = "img" ] ; then + cmd="rm -f ${input}.hdr" + echo "`basename $0` : $cmd" ; $cmd + cmd="rm -f ${input}.img" + echo "`basename $0` : $cmd" ; $cmd +else + cmd="rm -f ${input}.${in_ext}" + echo "`basename $0` : $cmd" ; $cmd +fi + +# done +echo "`basename $0` : done." diff --git a/rsc/scripts/nautilus-scripts/FSL_fslview b/rsc/scripts/nautilus-scripts/FSL_fslview index 20c6cbc..c81b5fb 100755 --- a/rsc/scripts/nautilus-scripts/FSL_fslview +++ b/rsc/scripts/nautilus-scripts/FSL_fslview @@ -1,5 +1,5 @@ #!/bin/bash -# Fslview wrapper for Nautilus. Also converts and displays Freesurfer volumes. +# Fslview wrapper for Nautilus. Also converts and displays Freesurfer volumes. No blanks allowed. # INSTALL: # mkdir -p ~/.gnome2/nautilus-scripts diff --git a/rsc/scripts/thres_z.sh b/rsc/scripts/thres_z.sh index ebe168d..644466c 100755 --- a/rsc/scripts/thres_z.sh +++ b/rsc/scripts/thres_z.sh @@ -69,7 +69,8 @@ if [ "$mask" != "none" ] ; then fi # bet image -cmd="bet $t2 $tmpdir/t2_bet -f 0 -m" +cmd="fslmaths $t2 -bin $tmpdir/t2_bet_mask" +#cmd="bet $t2 $tmpdir/t2_bet -f 0 -m" echo " $cmd" ; $cmd # erode