Skip to content

Commit

Permalink
Use local version of helper progs where installed in same directory. …
Browse files Browse the repository at this point in the history
…Helps with bundling and running multiple version
  • Loading branch information
mcraig-ibme committed Apr 3, 2017
1 parent 42217ec commit 87ee95e
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 136 deletions.
33 changes: 15 additions & 18 deletions asl_calib.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#
# SHCOPYRIGHT

# Make script use local copies of helper scripts/programs in the same
# directory, if present. This allows for multiple versions of the scripts
# to be used, possibly with bundled dependencies
PATH=`dirname $0`:${FSLDIR}/bin:${PATH}

#deal with options

Usage() {
Expand Down Expand Up @@ -182,15 +187,7 @@ done
echo "ASL_CALIB"

# set the version of fabber to use
fabber=fabber
#fabber=$FSLDIR/bin/fabber
if [ ! -z $devel ]; then
fabber=~chappell/cproject/fabber/fabber
if [ $FSLMACHTYPE = linux_64-gcc4.1 ]; then #assumption that the architecuture tell me that we are on a jalapeno node
echo "We are on jalapeno"
fabber=~chappell/cproject/jalapeno/fabber/fabber
fi
fi
fabber=fabber_asl

#check for mandatory inputs
if [ -z $calib ]; then
Expand Down Expand Up @@ -227,7 +224,7 @@ if [ ! -d $outdir ]; then
fi

# make a temporary directory to work in - delete at end
tmpbase=`$FSLDIR/bin/tmpnam`
tmpbase=`tmpnam`
temp_calib=${tmpbase}_asl_calib
mkdir $temp_calib

Expand Down Expand Up @@ -311,7 +308,7 @@ echo "Tissue reference : $tissref" >> $log

# command line override of default T1 and T2
if [ ! -z $T1rin ]; then
if [ `${FSLDIR}/bin/imtest $T1rin` ]; then
if [ `imtest $T1rin` ]; then
# we have been supplied with a T1 image - record that here to process later
T1rim=$T1rin
echo "Loading T1 image for reference region: T1im" >> $log
Expand All @@ -321,7 +318,7 @@ if [ ! -z $T1rin ]; then
fi
fi
if [ ! -z $T2rin ]; then
if [ `${FSLDIR}/bin/imtest $T2rin` ]; then
if [ `imtest $T2rin` ]; then
# we have been supplied with a T2 image - record that here to process later
T2rim=$T2rin
echo "Loading T2 image for reference region: T2im" >> $log
Expand Down Expand Up @@ -395,7 +392,7 @@ fi
if [ $tissref = "none" ]; then
# whole brain M0
# in this case use the brain mask
${FSLDIR}/bin/imcp $bmask $temp_calib/refmask
imcp $bmask $temp_calib/refmask
maskflag=1
echo "Brain mask is being used as the reference tissue (beware!)" >> $log
fi
Expand Down Expand Up @@ -502,8 +499,8 @@ if [ -z $maskflag ]; then

if [ ! -z $fasthasrun ] && [ -z $senson ]; then
# also extract the bias field and convert to sensitivity image (as long as we have already been supplied by a sensivity iamge or reference)
${FSLDIR}/bin/applywarp --ref=$calib --in=$temp_calib/seg_bias --out=$temp_calib/biasfield --premat=$temp_calib/high2low.mat --super --interp=spline --superlevel=4
${FSLDIR}/bin/fslmaths $temp_calib/biasfield -recip $temp_calib/sens
applywarp --ref=$calib --in=$temp_calib/seg_bias --out=$temp_calib/biasfield --premat=$temp_calib/high2low.mat --super --interp=spline --superlevel=4
fslmaths $temp_calib/biasfield -recip $temp_calib/sens
senson=1
echo "Using bias field from structural image for sensitivity correction" >> $log
fi
Expand All @@ -521,7 +518,7 @@ if [ -z $maskflag ]; then

else
#use supplied tissue reference mask
${FSLDIR}/bin/imcp $mask $temp_calib/refmask
imcp $mask $temp_calib/refmask
echo "Using supplied reference tissue mask: $mask" >> $log
fi

Expand Down Expand Up @@ -636,10 +633,10 @@ elif [ $mode = satrecov ]; then
if [ ! -z $senson ]; then
echo "Apply sensitivity image to data for reference tisse M0 estimation" >> $log
# apply sensitivity map to calibration image - ONLY for the reference tissue calculations
${FSLDIR}/bin/fslmaths $calib -div $temp_calib/sens $temp_calib/calib_senscorr
fslmaths $calib -div $temp_calib/sens $temp_calib/calib_senscorr
else
# no sensitivity correction required, but copy image over ready for next command
${FSLDIR}/bin/imcp $calib $temp_calib/calib_senscorr
imcp $calib $temp_calib/calib_senscorr
fi
echo "FABBER within reference tissue mask" >> $log
$fabber --data=$temp_calib/calib_senscorr --mask=$mask --output=$temp_calib/satrecov --data-order=singlefile --model=satrecov --noise=white --method=vb $tislist $llopts $sropts --t1=$T1r
Expand Down
9 changes: 7 additions & 2 deletions asl_preproc.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#
# SHCOPYRIGHT

# Make script use local copies of helper scripts/programs in the same
# directory, if present. This allows for multiple versions of the scripts
# to be used, possibly with bundled dependencies
PATH=`dirname $0`:${FSLDIR}/bin:${PATH}

Usage() {
echo "ASL_PREPROC"
echo "Version: ${GIT_SHA1} ${GIT_DATE}"
Expand Down Expand Up @@ -47,7 +52,7 @@ if [ -z $1 ]; then
exit 1
fi

perfusion_subtract=${FSLDIR}/bin/perfusion_subtract
perfusion_subtract=perfusion_subtract

until [ -z $1 ]; do
case $1 in
Expand Down Expand Up @@ -92,7 +97,7 @@ echo "ASL_PREPROC"
oldpwd=`pwd`


tmpbase=`$FSLDIR/bin/tmpnam`
tmpbase=`tmpnam`
if [ -z $debug ]; then
tempdir=${tmpbase}_asl_pre
else
Expand Down
51 changes: 28 additions & 23 deletions asl_reg.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#
# SHCOPYRIGHT

# Make script use local copies of helper scripts/programs in the same
# directory, if present. This allows for multiple versions of the scripts
# to be used, possibly with bundled dependencies
PATH=`dirname $0`:${FSLDIR}/bin:${PATH}

Usage() {
echo "ASL_REG"
echo "Version: ${GIT_SHA1} ${GIT_DATE}"
Expand Down Expand Up @@ -182,7 +187,7 @@ if [ ! -d $outdir ]; then
fi

# make a temporary directory to work in - delete at end
tmpbase=`$FSLDIR/bin/tmpnam`
tmpbase=`tmpnam`
if [ -z $debug ]; then
tempdir=${tmpbase}_asl_reg
else
Expand Down Expand Up @@ -219,7 +224,7 @@ fi
# BET the structural image if required
if [ -z $sbet ]; then
echo "Running BET on structural image"
$FSLDIR/bin/bet $struc $tempdir/struc_brain
bet $struc $tempdir/struc_brain
sbet=$tempdir/struc_brain
fi

Expand All @@ -228,20 +233,20 @@ if [ -z $finalonly ]; then
echo "Registration MAIN stage (FLIRT)"
if [ -z $lowstrucflag ]; then
#Step1: 3DOF translation only transformation
$FSLDIR/bin/flirt -in $cfile -ref $sbet -schedule $FSLDIR/etc/flirtsch/xyztrans.sch -omat $tempdir/low2high1.mat -out $tempdir/low2hig1 $inittext $weigthinstr
flirt -in $cfile -ref $sbet -schedule $FSLDIR/etc/flirtsch/xyztrans.sch -omat $tempdir/low2high1.mat -out $tempdir/low2hig1 $inittext $weigthinstr
#step2: 6DOF transformation with small search region
$FSLDIR/bin/flirt -in $cfile -ref $sbet -dof $DOF -omat $tempdir/low2high.mat -init $tempdir/low2high1.mat -schedule $flirtsch -out $tempdir/low2high $weigthinstr
flirt -in $cfile -ref $sbet -dof $DOF -omat $tempdir/low2high.mat -init $tempdir/low2high1.mat -schedule $flirtsch -out $tempdir/low2high $weigthinstr
else
#as we have a structural image in perfusion space use it to improve registration
echo "Using structral image in perfusion space ($lowstruc)"
#Step1: 3DOF translation only transformation perfusion->lowstruc
$FSLDIR/bin/flirt -in $cfile -ref $lowstruc -schedule $FSLDIR/etc/flirtsch/xyztrans.sch -omat $tempdir/low2low1.mat $inittext $weigthinstr
flirt -in $cfile -ref $lowstruc -schedule $FSLDIR/etc/flirtsch/xyztrans.sch -omat $tempdir/low2low1.mat $inittext $weigthinstr
#Step2: 6DOF limited transformation in perfusion space
$FSLDIR/bin/flirt -in $cfile -ref $lowstruc -dof $DOF -schedule $flirtsch -init $tempdir/low2low1.mat -omat $tempdir/low2low.mat $weigthinstr
flirt -in $cfile -ref $lowstruc -dof $DOF -schedule $flirtsch -init $tempdir/low2low1.mat -omat $tempdir/low2low.mat $weigthinstr
#step3: 6DOF transformation of lowstruc to struc
$FSLDIR/bin/flirt -in $lowstruc -ref $sbet -omat $tempdir/str2str.mat
flirt -in $lowstruc -ref $sbet -omat $tempdir/str2str.mat
#step4: combine the two transformations
$FSLDIR/bin/convert_xfm -omat $tempdir/low2high.mat -concat $tempdir/str2str.mat $tempdir/low2low.mat
convert_xfm -omat $tempdir/low2high.mat -concat $tempdir/str2str.mat $tempdir/low2low.mat
fi
# update the init text ready for the 'final' step to start with the result of the MAIN registration
epi_inittext="--init=$tempdir/low2high.mat"
Expand All @@ -255,9 +260,9 @@ if [ -z $mainonly ]; then
echo "Registration FINAL stage (BBR)"
if [ -z $tissseg ]; then
# Running FAST segmentation
$FSLDIR/bin/fast -o $tempdir/struct_fast $sbet
fast -o $tempdir/struct_fast $sbet
# WM segmentation
$FSLDIR/bin/fslmaths $tempdir/struct_fast_pve_2 -thr 0.5 -bin ${tempdir}/fast_tissseg
fslmaths $tempdir/struct_fast_pve_2 -thr 0.5 -bin ${tempdir}/fast_tissseg
tissseg=${tempdir}/fast_tissseg
fi

Expand All @@ -266,47 +271,47 @@ echo "Registration FINAL stage (BBR)"
convert_xfm -omat $tempdir/high2low.mat -inverse $tempdir/low2high.mat

fslmaths $sbet -thr 0 -bin $tempdir/struct_brain_mask
${FSLDIR}/bin/flirt -in $tempdir/struct_brain_mask -ref $infile -applyxfm -init $tempdir/high2low.mat -out $tempdir/mask -interp trilinear
${FSLDIR}/bin/fslmaths $tempdir/mask -thr 0.25 -bin -fillh $tempdir/mask
${FSLDIR}/bin/fslcpgeom $infile $tempdir/mask
flirt -in $tempdir/struct_brain_mask -ref $infile -applyxfm -init $tempdir/high2low.mat -out $tempdir/mask -interp trilinear
fslmaths $tempdir/mask -thr 0.25 -bin -fillh $tempdir/mask
fslcpgeom $infile $tempdir/mask
mask=$tempdir/mask
fi
# apply mask to asldata
$FSLDIR/bin/fslmaths $infile -mas $mask $tempdir/asldata_brain
fslmaths $infile -mas $mask $tempdir/asldata_brain
# copy mask to output for future reference
${FSLDIR}/bin/imcp $mask $outdir/mask
imcp $mask $outdir/mask

# do a final refinement of the registration using the perfusion and the white matter segmentation - using epi_reg to get BBR (and allow for fielmap correction in future)

if [ -z $fmap ]; then
# Without fieldmap
$FSLDIR/bin/epi_reg --epi=$tempdir/asldata_brain --t1=$struc --t1brain=$sbet $epi_inittext --out=$tempdir/low2high_final --wmseg=$tissseg $weigthinstr
epi_reg --epi=$tempdir/asldata_brain --t1=$struc --t1brain=$sbet $epi_inittext --out=$tempdir/low2high_final --wmseg=$tissseg $weigthinstr
else
# With fieldmap
fmapregstr=""
if [ ! -z $nofmapreg ]; then
fmapregstr="--nofmapreg"
fi
$FSLDIR/bin/epi_reg --epi=$tempdir/asldata_brain --t1=$struc --t1brain=$sbet $epi_inittext --out=$tempdir/low2high_final --wmseg=$tissseg $weigthinstr --fmap=$fmap --fmapmag=$fmapmag --fmapmagbrain=$fmapmagbrain --pedir=$pedir --echospacing=$echospacing $fmapregstr
epi_reg --epi=$tempdir/asldata_brain --t1=$struc --t1brain=$sbet $epi_inittext --out=$tempdir/low2high_final --wmseg=$tissseg $weigthinstr --fmap=$fmap --fmapmag=$fmapmag --fmapmagbrain=$fmapmagbrain --pedir=$pedir --echospacing=$echospacing $fmapregstr
fi

# $FSLDIR/bin/flirt -ref $sbet -in $infile -dof 6 -cost bbr -wmseg $wmseg -init $tempdir/low2high.mat -omat $tempdir/low2high.mat -out $tempdir/low2high_final -schedule ${FSLDIR}/etc/flirtsch/bbr.sch
# flirt -ref $sbet -in $infile -dof 6 -cost bbr -wmseg $wmseg -init $tempdir/low2high.mat -omat $tempdir/low2high.mat -out $tempdir/low2high_final -schedule ${FSLDIR}/etc/flirtsch/bbr.sch

echo "BBR end"

#OUTPUT
echo "Saving FINAL output"
cp $outdir/asl2struct.mat $outdir/asl2struct_init.mat # save the initial transformation matrix to allow chekcing if this part failed
cp $tempdir/low2high_final.mat $outdir/asl2struct.mat #the transformation matrix from epi_reg - this overwrites the version from MAIN registration
${FSLDIR}/bin/convert_xfm -omat $outdir/struct2asl.mat -inverse $outdir/asl2struct.mat #often useful to have the inverse transform, so calcuate it
convert_xfm -omat $outdir/struct2asl.mat -inverse $outdir/asl2struct.mat #often useful to have the inverse transform, so calcuate it
if [ ! -z $fmap ]; then
${FSLDIR}/bin/imcp $tempdir/low2high_final_warp $outdir/asl2struct_warp #the warp from epi_reg
imcp $tempdir/low2high_final_warp $outdir/asl2struct_warp #the warp from epi_reg
fi
${FSLDIR}/bin/imcp $tempdir/low2high_final $outdir/asl2struct # save the transformed image to check on the registration
imcp $tempdir/low2high_final $outdir/asl2struct # save the transformed image to check on the registration

# copy the edge image from epi_reg output as that is good for visualisation
${FSLDIR}/bin/imcp $tissseg $outdir/tissseg
${FSLDIR}/bin/imcp $tempdir/low2high_final_fast_wmedge $outdir/tissedge
imcp $tissseg $outdir/tissseg
imcp $tempdir/low2high_final_fast_wmedge $outdir/tissedge
fi

## ASL-->standard transformation (if specified)
Expand Down
Loading

0 comments on commit 87ee95e

Please sign in to comment.