Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ahheckel committed Jan 1, 2015
1 parent a5567da commit 6ac1ed3
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 27 deletions.
2 changes: 2 additions & 0 deletions 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
Expand Down Expand Up @@ -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:

```
Expand Down
41 changes: 31 additions & 10 deletions rsc/scripts/extractmask.sh
Expand Up @@ -30,17 +30,18 @@ function row2col()

Usage() {
echo ""
echo "Usage: `basename $0` [--vxcount|--extract] <input3D> <mask3D> <text-output> [-bin]"
echo "Usage: `basename $0` [--vxcount|--extract|--std] <input3D> <mask3D> <text-output> [-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 ""
exit 1
}

# 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
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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 "---------------------------"
Expand All @@ -171,22 +173,41 @@ 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...
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."
62 changes: 47 additions & 15 deletions rsc/scripts/extractmask_z.sh
Expand Up @@ -47,17 +47,18 @@ function getMax() # finds maximum in column

Usage() {
echo ""
echo "Usage: `basename $0` [--vxcount|--extract] <input3D> <mask3D> <text-output> [<\"-bin\"|none>](def:none) [<\"fslmeants-opts\"|none>](def:none) [<vertcat penvalue[0|1],subjects[0|1]>](def:0,0)"
echo "Usage: `basename $0` [--vxcount|--extract|--std] <input3D> <mask3D> <text-output> [<\"-bin\"|none>](def:none) [<\"fslmeants-opts\"|none>](def:none) [<vertcat penvalue[0|1],subjects[0|1]>](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 ""
exit 1
}

# 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
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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 "---------------------------"
Expand All @@ -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)"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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.
Expand Down
50 changes: 50 additions & 0 deletions 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` <input4D> [<nii.gz-output4D-basename>]"
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."
2 changes: 1 addition & 1 deletion 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
Expand Down
3 changes: 2 additions & 1 deletion rsc/scripts/thres_z.sh
Expand Up @@ -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
Expand Down

0 comments on commit 6ac1ed3

Please sign in to comment.