Skip to content

Concise tutorial and bash code on how to use BEaST to do skull strip

conge edited this page Oct 26, 2014 · 5 revisions

Before running BEaST, you need to make sure

  1. MINC Tool Kit is installed and can be called from command line (see here for instructions);
  2. You have BEaST libraries installed (see here for instructions) and you know the path to them.
  3. You have the MNI ICBM152 template, which is 193x229x193. You can download the template here. I am using the 2009c version. Write down the path to them on your computer.
  4. The anatomical image that you want to do skull-stripping on.

See the following bash script. I have tried about 40 T1 images with the script. The typical run time for processing 1 data set is about an hour.

#!/usr/bin/env bash
# 
# BEaSTSkullStrip.sh
# Using BEaST to do SkullStriping
# [see here](https://github.com/FCP-INDI/C-PAC/wiki/Concise-Installation-Guide-for-BEaST) for instructions for BEaST.
# 
# Qingyang Li
# 2013-07-29
#  
# The script requires AFNI, BEaST, MINC toolkit.

# initialize MINC toolkit, setting path to minc lib and Template images
MincPATH='/opt/minc'
source $MincPATH/minc-toolkit-config.sh

MincLibPATH="$MincPATH/share/beast-library-1.1/"

MNItemplatePATH='/home2/data/PublicProgram/Atlas/mni_icbm152_t1_tal_nlin_sym_09c'

cwd=`pwd`

## how to call the script, arguments
if [ $# -lt 1  ]
then 
  echo " USAGE ::  "
  echo "  BEaSTSkullStrip.sh <input> [output prefix] " 
  echo "   input: anatomical image with skull, in nifti format " 
  echo "   output: The probram will output two nifti files " 
  echo "      1) a skull stripped brain image; "  
  echo "      2) a skull stripped brain mask. "
  echo "   Option: output prefix: the filename of the output files without extention"
  echo " Example: BEaSTSkullStrip.sh ~/data/head.nii.gz ~/brain " 
  exit
fi

## deal with files in nii and nii.gz format (buggy, need to fix)
if [ $# -eq 1 ]
then
  inputDir=$(dirname $1)
  if [ $inputDir == "." ]; then
    inputDir=$cwd
  fi
  
  filename=$(basename $1)
  inputFile=$inputDir/$filename

  extension="${filename##*.}"
  if [ $extension == "gz" ]; then
    filename="${filename%.*}"
  fi
  filename="${filename%.*}"
  
  out=$inputDir/$filename
  
else
  outputDir=$(dirname $2)
  if [ $outputDir == "." ]; then
  outputDir=$cwd
  out=$outputDir/$2
  else
    mkdir -p $outputDir
    out=$2
  fi
  
fi

## change the format input file from nii to mnc
workingDir=`mktemp -d`
echo " ++ working directory is $workingDir"
cd $workingDir
3dcopy $inputFile head.nii
nii2mnc head.nii head.mnc

flist=head.mnc
## Normalize the input
beast_normalize head.mnc head_mni.mnc anat2mni.xfm -modeldir $MNItemplatePATH
flist="$flist head_mni.mnc anat2mni.xfm"

# Run BEaST to do SkullStripping
mincbeast -fill -median -conf $MincLibPATH/default.1mm.conf $MincLibPATH head_mni.mnc brain_mask_mni.mnc
flist="$flist brain_mask_mni.mnc"

# Trasform brain mask to it's original space
mincresample -invert_transformation -like head.mnc -transformation anat2mni.xfm brain_mask_mni.mnc brain_mask.mnc
flist="$flist brain_mask.mnc"

# Convert image from MNC to NII format.

mnc2nii brain_mask.mnc brain_mask_tmp.nii
3dresample -orient RPI -inset brain_mask_tmp.nii -prefix brain_mask_orig.nii
flist="$flist rain_mask_tmp.nii brain_mask_orig.nii"

# Generate and output brain image and brain mask
3dcalc -a brain_mask_orig.nii -b head.nii -expr "a*b" -prefix head_brain.nii.gz

flist="$flist head_brain.nii"

# out put files

# fix the AFNI won't overwrite problem.
rm -rf ${out}_brainmask.nii.gz ${out}_brain.nii.gz

3dcopy -inset brain_mask_orig.nii ${out}_brainmask.nii.gz
3dcopy -orient RPI -inset head_brain.nii.gz ${out}_brain.nii.gz

# delete all intermediate files
#rm -rf $flist
echo "  ++ working directory is $workingDir"
cd $cwd
Clone this wiki locally