Skip to content

Preprocess Rat Brains Structural

Eduardo Garza-Villarreal edited this page Sep 3, 2019 · 14 revisions

Your rat data will need preprocessing, therefore here are the preprocessing steps performed by the script mentioned here:

  • flip
  • reorder dimensions to standard "RMINC" mouse ordering
  • center
  • N4 bias field correct everything
  • Otsu mask
  • re-bias field correct with otsu mask

First, have your rat brains in MINC.

dcm2mnc -usecoordinates -anon -dname rat1 -fname rat1_t1 rat1 rat1_minc_folder

MINC

Preprocessing

Use the tool rat-preprocessing-v2.sh at the end of this page or inside the module minc-toolkit-extras.

LAVIS instructions:

Load the modules:

module load hdf5/1.8.18 netcdf/gcc/64/4.4.1.1 python36/3.6.8 minc-toolkit-v2/1.9.17 minc-stuffs/0.1.24 minc-toolkit-extras/24abr2019

then load ANTs:

module load ants/9Mayo09

Otherwise, it will use the ANTs in minc-toolkit which is old and does not work well.

The ways to use the script are either by file:

rat-preprocessing-v2.sh input output

Example: rat-preprocessing.sh rat1_t1.mnc rat1_t1_preproc.mnc

Or, process in parallel.

for i in data/minc/*/*; do file=$(basename $i); echo rat-preprocessing.sh ${i} preproc/${file%.*}_pp.mnc; done > ratpreproc

qbatch -N fatrat_preproc --chunksize 1 --walltime=12:00:00 ratpreproc

Check the preprocessed files and that's it.

PREPROC

rat-preprocessing.sh

#!/bin/bash
#Script to do optimal preprocessing on in-vivo/ex-vivo structural scans
#Taken using the LANIREM Bruker 7T
#usage:
#rat-preprocessing-v2.sh input.mnc output.mnc

#Operations
# centers brain in space
# denoises
# registers to Ratlas
# gets approximate brain mask from atlas
# expands mask
# Bias field correction with N4

set -euo pipefail

export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=${THREADS_PER_COMMAND:-$(nproc)}

REGTARGET=/opt/quarantine/resources/Ratlas/cropped/rescale_centered.mnc
REGMASK=/opt/quarantine/resources/Ratlas/cropped/rat_mask_centered.mnc

tmpdir=$(mktemp -d)

input=$1
output=$2

set -x

maxval=$(mincstats -max -quiet ${input})

volflip -y $input $tmpdir/flip.mnc
volmash -swap zy $tmpdir/flip.mnc $tmpdir/mash.mnc


clean_and_center_minc.pl $tmpdir/mash.mnc $tmpdir/centered.mnc

#ImageMath 3 $tmpdir/centered.mnc RescaleImage $tmpdir/centered.mnc 0 32767

#minc_anlm --mt ${ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS} $tmpdir/centered.mnc $tmpdir/denoise.mnc

#cp -f $tmpdir/centered.mnc $tmpdir/denoise.mnc

minccalc -byte -unsigned -expression 'A[0]?1:1' $tmpdir/centered.mnc $tmpdir/fullmask.mnc

N4BiasFieldCorrection -d 3 -i $tmpdir/centered.mnc -b [30] -c [200x200x200,0.0] -r 0 -x $tmpdir/fullmask.mnc -o $tmpdir/corrected.mnc -s 4 --verbose

ThresholdImage 3 $tmpdir/corrected.mnc $tmpdir/otsu.mnc  Otsu 1

N4BiasFieldCorrection -d 3 -i $tmpdir/centered.mnc -b [30] -c [200x200x200,0.0] -r 0 -w $tmpdir/otsu.mnc -x $tmpdir/fullmask.mnc -o $tmpdir/corrected.mnc -s 2 --verbose

#antsRegistration --dimensionality 3 --float 0 --collapse-output-transforms 1 --minc \
#--output $tmpdir/trans \
#--use-histogram-matching 0 \
#--transform Rigid[0.1] --metric Mattes[${REGTARGET},$tmpdir/corrected.mnc,1,32,Regular,0.25] --convergence [2000x2000x2000x2000x0,1e-6,10,1] --shrink-factors 8x6x4x2x1 --smoothing-sigmas 4x3x2x1x0 --masks [NULL,NULL] \
#--transform Similarity[0.1] --metric Mattes[${REGTARGET},$tmpdir/corrected.mnc,1,32,Regular,0.5] --convergence [2000x2000x2000,1e-6,10,1] --shrink-factors 6x4x2 --smoothing-sigmas 3x2x1 --masks [NULL,NULL] \
#--transform Affine[0.1]     --metric Mattes[${REGTARGET},$tmpdir/corrected.mnc,1,32,Regular,1] --convergence [2000x2000x0,1e-6,10,1] --shrink-factors 4x2x1 --smoothing-sigmas 2x1x0 --masks [${REGMASK},NULL] --verbose

#antsApplyTransforms -d 3 -i ${REGMASK} -o $tmpdir/mask.mnc -t [$tmpdir/trans0_GenericAffine.xfm,1] -r $tmpdir/denoise.mnc -n NearestNeighbor

#mincmorph -3D26 -successive D $tmpdir/mask.mnc $tmpdir/weight.mnc

#ImageMath 3 $tmpdir/trunc.mnc TruncateImageIntensity $tmpdir/denoise.mnc 0.025 0.9995 1024 $tmpdir/weight.mnc

#N4BiasFieldCorrection -d 3 -i $tmpdir/trunc.mnc -b [30] -c [200x200x200,0.0] -w $tmpdir/weight.mnc -r 0 -x $tmpdir/fullmask.mnc -o $tmpdir/corrected.mnc -s 4 --verbose

#ImageMath 3 $tmpdir/norm.mnc Normalize $tmpdir/corrected.mnc
#ImageMath 3 $output m $tmpdir/norm.mnc ${maxval}
ImageMath 3 $output RescaleImage $tmpdir/corrected.mnc 0 ${maxval}


#cp -f $tmpdir/weight.mnc $(dirname $output)/$(basename $output .mnc)_mask.mnc

rm -rf $tmpdir

Clone this wiki locally