mcflirt_2stage 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. # Author: Michael Hanke <michael.hanke@gmail.com>
  3. # License: MIT
  4. set -e
  5. set -u
  6. if [ $# -lt 3 ]; then
  7. cat << EOT
  8. This script implements MCFLIRT-based motion correction using an dedicated
  9. target image. The difference to using MCFLIRT's --reffile option is that
  10. an initial alignment to the target image can be performed with more
  11. flexibility. The actual motion correction is performed using the mean
  12. volume as a reference and a constant subsequent transformation to the
  13. final target is applied in addition. Each volume is transformed separately
  14. with a single interpolation/reslicing step that applies the combined
  15. transformations.
  16. Call this script with at least three arguments
  17. 1. Input bold time series image
  18. 2. Skull-stripped reference image
  19. 3. Output filename for the motion-corrected time series image.
  20. (4) Options to FLIRT for the alignment to the target image. Default: "-dof 6"
  21. Examples:
  22. $0 bold.nii.gz subjtmpl.nii.gz bold_mc.nii.gz "-dof 6 -cost normcorr"
  23. $0 bold.nii.gz subjtmpl.nii.gz bold_mc.nii.gz "-dof 6 -cost normcorr"
  24. EOT
  25. exit 1
  26. fi
  27. # fake test for FSL
  28. ignore=$FSLDIR
  29. inbold=$(readlink -f $1) || true
  30. ref=$(readlink -f $2) || true
  31. outbold=$(readlink -m $3)
  32. refsmooth=${4:-0}
  33. initflirtopts=${5:--dof 6}
  34. if [ -z "$inbold" ]; then
  35. echo "File not found '$1'"
  36. exit 1
  37. fi
  38. if [ -z "$ref" ]; then
  39. echo "File not found '$2'"
  40. exit 1
  41. fi
  42. outbolddir=$(dirname "${outbold}")
  43. mkdir -p "$outbolddir"
  44. wdir=$(mktemp -d --suffix=2stagemc)
  45. trap "rm -rf $wdir" SIGINT SIGTERM
  46. cd "$wdir"
  47. # motion correction reference
  48. $FSLDIR/bin/fslmaths "$inbold" -Tmean bold -odt input
  49. # perform motion correction
  50. $FSLDIR/bin/mcflirt -in "$inbold" -reffile bold -mats -plots -out mc
  51. # mean bold volume after MC
  52. $FSLDIR/bin/fslmaths mc -Tmean meanbold_mc -odt input
  53. $FSLDIR/bin/bet meanbold_mc bold_brain
  54. # smooth reference volume somewhat
  55. $FSLDIR/bin/fslmaths "$ref" -s $(echo "scale=4; $refsmooth / 2.355" | bc) refsmooth
  56. $FSLDIR/bin/flirt ${initflirtopts} -in bold_brain -ref refsmooth -omat bold2brain.mat
  57. cp mc.par "$($FSLDIR/bin/remove_ext "$outbold")_mcparams.txt"
  58. for i in mc.mat/MAT*; do convert_xfm -omat mc.mat/tmpl_$(basename $i) -concat bold2brain.mat $i; done
  59. mkdir bold
  60. $FSLDIR/bin/fslsplit "$inbold" bold/
  61. mkdir mc
  62. for i in bold/*.nii*; do
  63. bname=$(basename $i)
  64. idx=${bname%.nii*}
  65. $FSLDIR/bin/flirt -in "$i" -ref "$ref" -applyxfm -init mc.mat/tmpl_MAT_${idx} -out mc/${idx} -interp sinc; done
  66. $FSLDIR/bin/fslmerge -tr "$outbold" mc/* $(fslval "$inbold" pixdim4)
  67. cd -
  68. rm -rf "$wdir"