From e05760c1aa28995c09b604e68a7896a4b70c10b6 Mon Sep 17 00:00:00 2001
From: David Flitney <dave.flitney@ndcn.ox.ac.uk>
Date: Tue, 25 Apr 2006 14:05:01 +0000
Subject: [PATCH] New SGE aware scripts to be called if SGE_ROOT exists

---
 bedpost                  | 136 +++++++++++++++++++++++++++++++++++++++
 bedpost_postproc.sh      |  41 ++++++++++++
 bedpost_preproc.sh       |  20 ++++++
 bedpost_single_slice.sh  |  17 +++++
 bedpostx                 | 136 +++++++++++++++++++++++++++++++++++++++
 bedpostx_postproc.sh     |  11 ++++
 bedpostx_preproc.sh      |  20 ++++++
 bedpostx_single_slice.sh |  18 ++++++
 8 files changed, 399 insertions(+)
 create mode 100755 bedpost
 create mode 100755 bedpost_postproc.sh
 create mode 100755 bedpost_preproc.sh
 create mode 100755 bedpost_single_slice.sh
 create mode 100755 bedpostx
 create mode 100755 bedpostx_postproc.sh
 create mode 100755 bedpostx_preproc.sh
 create mode 100755 bedpostx_single_slice.sh

diff --git a/bedpost b/bedpost
new file mode 100755
index 0000000..610e401
--- /dev/null
+++ b/bedpost
@@ -0,0 +1,136 @@
+#!/bin/sh
+
+#   Copyright (C) 2004 University of Oxford
+#
+#   SHCOPYRIGHT
+
+if [ "x$SGE_ROOT" = "x" ] ; then
+  if [ -f /usr/local/share/sge-6.0/default/common/settings.sh ] ; then
+    . /usr/local/share/sge-6.0/default/common/settings.sh
+  else if [ -f /usr/local/sge-6/default/common/settings.sh ] ; then
+      . /usr/local/sge-6/default/common/settings.sh
+    else
+      echo "bedpost: Fatal error: couldn\'t locate SGE_ROOT\!"
+      exit 255
+    fi
+  fi
+fi
+
+Usage() {
+    echo ""
+    echo "Usage: bedpost <subject directory> "
+    echo ""
+    echo "expects to find bvals and bvecs in subject directory"
+    echo "expects to find data nodif_brain_mask nodif in subject directory"
+    echo ""
+    exit
+}
+
+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
+
+#check that all required files exist
+
+if [ ! -d $subjdir ]; then
+	echo "subject directory $1 not found"
+fi
+
+if [ ! -e ${subjdir}/bvecs ]; then
+	echo "${subjdir}/bvecs not found"
+fi
+
+if [ ! -e ${subjdir}/bvals ]; then
+	echo "${subjdir}/bvals not found"
+fi
+
+if [ `${FSLDIR}/bin/imtest ${subjdir}/data` -eq 0 ]; then
+	echo "${subjdir}/data not found"
+fi
+
+if [ `${FSLDIR}/bin/imtest ${subjdir}/nodif` -eq 0 ]; then
+	echo "${subjdir}/nodif not found"
+fi
+
+if [ `${FSLDIR}/bin/imtest ${subjdir}/nodif_brain_mask` -eq 0 ]; then
+	echo "${subjdir}/nodif_brain_mask not found"
+fi
+
+echo Making bedpost directory structure
+
+mkdir -p ${subjdir}.bedpost/
+mkdir -p ${subjdir}.bedpost/diff_slices
+mkdir -p ${subjdir}.bedpost/logs
+mkdir -p ${subjdir}.bedpost/logs/pid_${$}
+mkdir -p ${subjdir}.bedpost/xfms
+
+mailto=`whoami`@fmrib.ox.ac.uk
+
+echo Queuing preprocessing stages
+preprocid=`qsub -M $mailto -V -q short.q \
+    -o ${subjdir}.bedpost/logs -e ${subjdir}.bedpost/logs \
+    ${FSLDIR}/bin/sge_bedpost_preproc.sh ${subjdir} | \
+    awk '{print $3}'`
+
+echo Queuing parallel processing stage
+nslices=`avwval ${subjdir}/data dim3`
+bedpostid=`qsub -M $mailto -hold_jid $preprocid -V -q long.q -t 1-$nslices \
+    -o ${subjdir}.bedpost/logs -e ${subjdir}.bedpost/logs \
+    ${FSLDIR}/bin/sge_bedpost_single_slice.sh $subjdir | \
+    awk '{print $3}' | awk -F. '{print $1}'`
+
+echo Queuing post processing stage
+mergeid=`qsub -M $mailto -hold_jid $bedpostid -V -q short.q \
+    -o ${subjdir}.bedpost/logs -e ${subjdir}.bedpost/logs \
+    ${FSLDIR}/bin/sge_bedpost_postproc.sh $subjdir | \
+    awk '{print $3}'`
+
+echo
+echo Type ${subjdir}.bedpost/monitor to show progress.
+cat <<EOM > ${subjdir}.bedpost/monitor
+#!/bin/sh
+finished=0
+logdir=${subjdir}.bedpost/logs
+while [ \$finished -eq 0 ] ; do
+    nfin=0
+    if [ -f \$logdir/bedpost.o$bedpostid.1 ] ; then
+      nfin=\`cat \$logdir/bedpost.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}.bedpost/monitor
+
+echo Type ${subjdir}.bedpost/cancel to terminate the task.
+cat <<EOC > ${subjdir}.bedpost/cancel
+#!/bin/sh
+qdel $mergeid $bedpostid $preprocid
+EOC
+chmod +x ${subjdir}.bedpost/cancel
+
+echo
+echo You will get an email at the end of the post-processing stage.
+echo
diff --git a/bedpost_postproc.sh b/bedpost_postproc.sh
new file mode 100755
index 0000000..c4e8e4d
--- /dev/null
+++ b/bedpost_postproc.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+subjdir=$1
+
+#$ -S /bin/sh
+#$ -V
+#$ -N bp_postproc
+#$ -m ae
+
+echo Merging outputs into 4D files
+
+${FSLDIR}/bin/avwmerge -z ${subjdir}.bedpost/merged_thsamples `${FSLDIR}/bin/imglob -oneperimage ${subjdir}.bedpost/diff_slices/data_slice_*/th_samples`
+${FSLDIR}/bin/avwmerge -z ${subjdir}.bedpost/merged_phsamples `${FSLDIR}/bin/imglob -oneperimage ${subjdir}.bedpost/diff_slices/data_slice_*/ph_samples`
+${FSLDIR}/bin/avwmerge -z ${subjdir}.bedpost/merged_fsamples  `${FSLDIR}/bin/imglob -oneperimage ${subjdir}.bedpost/diff_slices/data_slice_*/f_samples`
+${FSLDIR}/bin/avwmaths ${subjdir}.bedpost/merged_thsamples -Tmean ${subjdir}.bedpost/mean_thsamples
+${FSLDIR}/bin/avwmaths ${subjdir}.bedpost/merged_phsamples -Tmean ${subjdir}.bedpost/mean_phsamples
+${FSLDIR}/bin/avwmaths ${subjdir}.bedpost/merged_fsamples -Tmean ${subjdir}.bedpost/mean_fsamples
+
+${FSLDIR}/bin/make_dyadic_vectors ${subjdir}.bedpost/merged_thsamples ${subjdir}.bedpost/merged_phsamples ${subjdir}.bedpost/nodif_brain_mask ${subjdir}.bedpost/dyadic_vectors
+
+echo Removing intermediate files
+
+if [ `imtest ${subjdir}.bedpost/merged_thsamples` -eq 1 ];then
+  if [ `imtest ${subjdir}.bedpost/merged_phsamples` -eq 1 ];then
+    if [ `imtest ${subjdir}.bedpost/merged_fsamples` -eq 1 ];then
+      rm -rf ${subjdir}.bedpost/diff_slices
+      rm -f ${subjdir}/data_slice_*
+      rm -f ${subjdir}/nodif_brain_mask_slice_*
+    fi
+  fi
+fi
+
+echo Creating identity xfm
+
+xfmdir=${subjdir}.bedpost/xfms
+echo 1 0 0 0 > ${xfmdir}/eye.mat
+echo 0 1 0 0 >> ${xfmdir}/eye.mat
+echo 0 0 1 0 >> ${xfmdir}/eye.mat
+echo 0 0 0 1 >> ${xfmdir}/eye.mat
+
+echo Done
\ No newline at end of file
diff --git a/bedpost_preproc.sh b/bedpost_preproc.sh
new file mode 100755
index 0000000..34a527c
--- /dev/null
+++ b/bedpost_preproc.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+subjdir=$1
+
+#$ -S /bin/sh
+#$ -V
+#$ -N bp_preproc
+#$ -m a
+
+echo Copying files to bedpost directory
+cp ${subjdir}/bvecs ${subjdir}/bvals ${subjdir}.bedpost
+${FSLDIR}/bin/imcp ${subjdir}/nodif ${subjdir}/nodif_brain_mask ${subjdir}.bedpost
+${FSLDIR}/bin/avwmaths\
+ ${subjdir}/nodif\
+ -mas ${subjdir}/nodif_brain_mask\
+ ${subjdir}.bedpost/nodif_brain
+
+${FSLDIR}/bin/avwslice ${subjdir}/data
+${FSLDIR}/bin/avwslice ${subjdir}/nodif_brain_mask
+echo Done
\ No newline at end of file
diff --git a/bedpost_single_slice.sh b/bedpost_single_slice.sh
new file mode 100755
index 0000000..a9d27a1
--- /dev/null
+++ b/bedpost_single_slice.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+subjdir=$1
+slice=`expr $SGE_TASK_ID - 1`
+slicezp=`${FSLDIR}/bin/zeropad $slice 4`
+
+#$ -cwd -q long.q
+#$ -S /bin/sh
+#$ -V -N bedpost
+#$ -m a
+
+${FSLDIR}/bin/diff_pvm\
+ --data=$subjdir/data_slice_$slicezp\
+ --mask=$subjdir/nodif_brain_mask_slice_$slicezp\
+ -b $subjdir/bvals -r $subjdir/bvecs\
+ --forcedir --logdir=$subjdir.bedpost/diff_slices/data_slice_$slicezp\
+ --nj=1300 --bi=300 --se=20 > $subjdir.bedpost/logs/log$slicezp && echo Done
diff --git a/bedpostx b/bedpostx
new file mode 100755
index 0000000..50c1cd2
--- /dev/null
+++ b/bedpostx
@@ -0,0 +1,136 @@
+#!/bin/sh
+
+#   Copyright (C) 2004 University of Oxford
+#
+#   SHCOPYRIGHT
+
+if [ "x$SGE_ROOT" = "x" ] ; then
+  if [ -f /usr/local/share/sge-6.0/default/common/settings.sh ] ; then
+    . /usr/local/share/sge-6.0/default/common/settings.sh
+  else if [ -f /usr/local/sge-6/default/common/settings.sh ] ; then
+      . /usr/local/sge-6/default/common/settings.sh
+    else
+      echo "bedpostX: Fatal error: couldn\'t locate SGE_ROOT\!"
+      exit 255
+    fi
+  fi
+fi
+
+Usage() {
+    echo ""
+    echo "Usage: bedpostX <subject directory> "
+    echo ""
+    echo "expects to find bvals and bvecs in subject directory"
+    echo "expects to find data nodif_brain_mask nodif in subject directory"
+    echo ""
+    exit
+}
+
+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
+
+#check that all required files exist
+
+if [ ! -d $subjdir ]; then
+	echo "subject directory $1 not found"
+fi
+
+if [ ! -e ${subjdir}/bvecs ]; then
+	echo "${subjdir}/bvecs not found"
+fi
+
+if [ ! -e ${subjdir}/bvals ]; then
+	echo "${subjdir}/bvals not found"
+fi
+
+if [ `${FSLDIR}/bin/imtest ${subjdir}/data` -eq 0 ]; then
+	echo "${subjdir}/data not found"
+fi
+
+if [ `${FSLDIR}/bin/imtest ${subjdir}/nodif` -eq 0 ]; then
+	echo "${subjdir}/nodif not found"
+fi
+
+if [ `${FSLDIR}/bin/imtest ${subjdir}/nodif_brain_mask` -eq 0 ]; then
+	echo "${subjdir}/nodif_brain_mask not found"
+fi
+
+echo Making bedpost directory structure
+
+mkdir -p ${subjdir}.bedpost/
+mkdir -p ${subjdir}.bedpost/diff_slices
+mkdir -p ${subjdir}.bedpost/logs
+mkdir -p ${subjdir}.bedpost/logs/pid_${$}
+mkdir -p ${subjdir}.bedpost/xfms
+
+mailto=`whoami`@fmrib.ox.ac.uk
+
+echo Queuing preprocessing stages
+preprocid=`qsub -M $mailto -V -q short.q \
+    -o ${subjdir}.bedpost/logs -e ${subjdir}.bedpost/logs \
+    ${FSLDIR}/bin/sge_bedpostX_preproc.sh ${subjdir} | \
+    awk '{print $3}'`
+
+echo Queuing parallel processing stage
+nslices=`avwval ${subjdir}/data dim3`
+bedpostid=`qsub -M $mailto -hold_jid $preprocid -V -q long.q -t 1-$nslices \
+    -o ${subjdir}.bedpost/logs -e ${subjdir}.bedpost/logs \
+    ${FSLDIR}/bin/sge_bedpostX_single_slice.sh $subjdir | \
+    awk '{print $3}' | awk -F. '{print $1}'`
+
+echo Queuing post processing stage
+mergeid=`qsub -M $mailto -hold_jid $bedpostid -V -q short.q \
+    -o ${subjdir}.bedpost/logs -e ${subjdir}.bedpost/logs \
+    ${FSLDIR}/bin/sge_bedpostX_postproc.sh $subjdir | \
+    awk '{print $3}'`
+
+echo
+echo Type ${subjdir}.bedpost/monitor to show progress.
+cat <<EOM > ${subjdir}.bedpost/monitor
+#!/bin/sh
+finished=0
+logdir=${subjdir}.bedpost/logs
+while [ \$finished -eq 0 ] ; do
+    nfin=0
+    if [ -f \$logdir/bedpost.o$bedpostid.1 ] ; then
+      nfin=\`cat \$logdir/bedpost.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}.bedpost/monitor
+
+echo Type ${subjdir}.bedpost/cancel to terminate the task.
+cat <<EOC > ${subjdir}.bedpost/cancel
+#!/bin/sh
+qdel $mergeid $bedpostid $preprocid
+EOC
+chmod +x ${subjdir}.bedpost/cancel
+
+echo
+echo You will get an email at the end of the post-processing stage.
+echo
diff --git a/bedpostx_postproc.sh b/bedpostx_postproc.sh
new file mode 100755
index 0000000..0cf50fb
--- /dev/null
+++ b/bedpostx_postproc.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+subjdir=$1
+
+#$ -S /bin/sh
+#$ -V
+#$ -N bp_postproc
+#$ -m ae
+
+
+echo cock
\ No newline at end of file
diff --git a/bedpostx_preproc.sh b/bedpostx_preproc.sh
new file mode 100755
index 0000000..fc010ae
--- /dev/null
+++ b/bedpostx_preproc.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+subjdir=$1
+
+#$ -S /bin/sh
+#$ -V
+#$ -N bpx_preproc
+#$ -m a
+
+echo Copying files to bedpost directory
+cp ${subjdir}/bvecs ${subjdir}/bvals ${subjdir}.bedpost
+${FSLDIR}/bin/imcp ${subjdir}/nodif ${subjdir}/nodif_brain_mask ${subjdir}.bedpost
+${FSLDIR}/bin/avwmaths\
+ ${subjdir}/nodif\
+ -mas ${subjdir}/nodif_brain_mask\
+ ${subjdir}.bedpost/nodif_brain
+
+${FSLDIR}/bin/avwslice ${subjdir}/data
+${FSLDIR}/bin/avwslice ${subjdir}/nodif_brain_mask
+echo Done
\ No newline at end of file
diff --git a/bedpostx_single_slice.sh b/bedpostx_single_slice.sh
new file mode 100755
index 0000000..6a87b26
--- /dev/null
+++ b/bedpostx_single_slice.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+subjdir=$1
+slice=`expr $SGE_TASK_ID - 1`
+slicezp=`${FSLDIR}/bin/zeropad $slice 4`
+
+#$ -cwd -q long.q
+#$ -S /bin/sh
+#$ -V -N bedpostX
+#$ -m a
+
+${FSLDIR}/bin/xfibres\
+ --data=$subjdir/data_slice_$slicezp\
+ --mask=$subjdir/nodif_brain_mask_slice_$slicezp\
+ -b $subjdir/bvals -r $subjdir/bvecs\
+ --forcedir --logdir=$subjdir.bedpost/diff_slices/data_slice_$slicezp\
+ --nj=1000 --bi=600 --se=20 --upe=24 --nfibres=2 > $subjdir.bedpost/logs/log$slicezp  && echo Done
+
-- 
GitLab