-
Stephen Smith authoredStephen Smith authored
bedpostx 4.52 KiB
#!/bin/sh
# Copyright (C) 2004 University of Oxford
#
# SHCOPYRIGHT
if [ "x$SGE_ROOT" = "x" ] ; then
if [ -f /usr/local/share/sge/default/common/settings.sh ] ; then
. /usr/local/share/sge/default/common/settings.sh
elif [ -f /usr/local/sge/default/common/settings.sh ] ; then
. /usr/local/sge/default/common/settings.sh
fi
fi
Usage() {
echo ""
echo "Usage: bedpostx <subject directory> [options]"
echo ""
echo "expects to find bvals and bvecs in subject directory"
echo "expects to find data and nodif_brain_mask in subject directory"
echo "options:"
echo "-n (number of fibres per voxel, default 2)"
echo "-w (ARD weight, more weight means less fibres killed per voxel, default 1)"
echo "-b (burnin period, default 1000)"
echo "-j (number of jumps, default 1250)"
echo "-s (sample every, default 25)"
echo ""
exit 1
}
make_absolute(){
dir=$1;
if [ -d ${dir} ]; then
OLDWD=`pwd`
cd ${dir}
dir_all=`pwd`
cd $OLDWD
else
dir_all=${dir}
fi
echo ${dir_all}
}
[ "$1" = "" ] && Usage
subjdir=`make_absolute $1`
subjdir=`echo $subjdir | sed 's/\/$/$/g'`
echo subjectdir is $subjdir
#parse option arguments
nfibres=2
fudge=1
burnin=1000
njumps=1250
sampleevery=25
while [ ! -z "$2" ]
do
case "$2" in
-n) nfibres=$3;shift;;
-w) fudge=$3;shift;;
-b) burnin=$3;shift;;
-j) njumps=$3;shift;;
-s) sampleevery=$3;shift;;
*) break;;
esac
shift
done
#check that all required files exist
if [ ! -d $subjdir ]; then
echo "subject directory $1 not found"
exit 1
fi
if [ ! -e ${subjdir}/bvecs ]; then
echo "${subjdir}/bvecs not found"
exit 1
fi
if [ ! -e ${subjdir}/bvals ]; then
echo "${subjdir}/bvals not found"
exit 1
fi
if [ `${FSLDIR}/bin/imtest ${subjdir}/data` -eq 0 ]; then
echo "${subjdir}/data not found"
exit 1
fi
if [ `${FSLDIR}/bin/imtest ${subjdir}/nodif_brain_mask` -eq 0 ]; then
echo "${subjdir}/nodif_brain_mask not found"
exit 1
fi
echo Making bedpostx directory structure
mkdir -p ${subjdir}.bedpostX/
mkdir -p ${subjdir}.bedpostX/diff_slices
mkdir -p ${subjdir}.bedpostX/logs
mkdir -p ${subjdir}.bedpostX/logs/pid_${$}
mkdir -p ${subjdir}.bedpostX/xfms
mailto=`whoami`@fmrib.ox.ac.uk
echo Queuing preprocessing stages
preprocid=`${FSLDIR}/bin/fsl_sub -T 60 -m as -N bpx_preproc -l ${subjdir}.bedpostX/logs ${FSLDIR}/bin/bedpostx_preproc.sh ${subjdir}`
echo Queuing parallel processing stage
nslices=`${FSLDIR}/bin/fslval ${subjdir}/data dim3`
[ -f ${subjdir}.bedpostX/commands.txt ] && rm ${subjdir}.bedpostX/commands.txt
if [ "x$SGE_ROOT" = "x" ]; then
#
# Need to set off a backgrounded monitoring task
#
cat <<EOM > ${subjdir}.bedpostX/monitor
#!/bin/sh
finished=0
logdir=${subjdir}.bedpostX/logs
while [ \$finished -eq 0 ] ; do
nfin=0
if [ -f \$logdir/bedpostx.o*.1 ] ; then
nfin=\`cat \$logdir/bedpostx.o* |\
grep Done | wc -w | awk '{print \$1}'\`
fi
if [ \$nfin -eq $nslices ] ; then
finished=1
fi
echo "\$nfin slices processed"
sleep 600;
done
EOM
chmod +x ${subjdir}.bedpostX/monitor
${subjdir}.bedpostX/monitor&
monitorpid=$!
fi
slice=0
while [ $slice -lt $nslices ]
do
echo "${FSLDIR}/bin/bedpostx_single_slice.sh $subjdir $nfibres $fudge $burnin $njumps $sampleevery $slice" \
>> ${subjdir}.bedpostX/commands.txt
slice=$(($slice + 1))
done
bedpostid=`${FSLDIR}/bin/fsl_sub -j $preprocid -l ${subjdir}.bedpostX/logs -M $mailto -N bedpostx -t ${subjdir}.bedpostX/commands.txt`
[ "x$SGE_ROOT" = "x" ] && kill $monitorpid
echo Queuing post processing stage
mergeid=`${FSLDIR}/bin/fsl_sub -j $bedpostid -T 60 -m as -N bpx_postproc -l ${subjdir}.bedpostX/logs ${FSLDIR}/bin/bedpostx_postproc.sh $subjdir`
if [ "x$SGE_ROOT" != "x" ]; then
echo
echo Type ${subjdir}.bedpostX/monitor to show progress.
cat <<EOM > ${subjdir}.bedpostX/monitor
#!/bin/sh
finished=0
logdir=${subjdir}.bedpostX/logs
while [ \$finished -eq 0 ] ; do
nfin=0
if [ -f \$logdir/bedpostx.o$bedpostid.1 ] ; then
nfin=\`cat \$logdir/bedpostx.o$bedpostid.* |\
grep Done | wc -w | awk '{print \$1}'\`
fi
if [ \$nfin -eq $nslices ] ; then
finished=1
fi
echo "\$nfin slices processed"
sleep 600;
done
EOM
chmod +x ${subjdir}.bedpostX/monitor
echo Type ${subjdir}.bedpostX/cancel to terminate all the queued tasks.
cat <<EOC > ${subjdir}.bedpostX/cancel
#!/bin/sh
qdel $mergeid $bedpostid $preprocid
EOC
chmod +x ${subjdir}.bedpostX/cancel
echo
echo You will get an email at the end of the post-processing stage.
echo
fi