Introduction to Bash scripting for Neuroimaging - tannerjared/MRI_Guide GitHub Wiki

For example, let’s look at a portion of a processing stream for preparing diffusion weighted MRI scans for fiber tracking.

Images DICOMS >> NiFTI >> motion (eddy current) correction >> skull stripping >> co-registration with FreeSurfer processed T1 scans >> and so forth. If you have a lot of time or a lot of trained undergraduates you can move through this whole processing stream (and that was just a small portion of my processing stream!) step by step and manually for each research participant. This would be a massive waste of time and effort! Neuroimaging requires a lot of time and organization but you can only do so much before your brain fizzles. Scripting is a useful way to keep the process organized and cut down on redundant tasks.

Every Bash script must start with the following:

#!/bin/sh

After that you can do whatever you need to do.

Placing a “#” (without the parentheses) at the beginning of a line comments out the whole line (makes it non-functional). This is a good way to explain what you are doing in the script so if you go and edit the script or if someone else does, it is obvious what you were doing. The best scripts have descriptive comments.

Let’s have a sample script now that will convert a bunch of brains from FreeSurfer into Niftis, skull strip them, and then register them to preexisting diffusion scans. Note: this is not an example of best practices for bash scripting. My scripts are about minimizing time spent on repetitive tasks and reproducibility of research results. Scripts written for broader (external) use can have improved structure.

#!/bin/sh

#Created by Jared Tanner on 08/03/2010

#Some other comment

cd /Applications/freesurfer/subjects

ls >> blindnums.txt

open -a TextEdit blindnums.txt

read -p “Now opening TextEdit to verify the subjects you want to convert. Press Enter to continue once you have finished verifying and editing the list.” shlock ;

echo Now making target directories and starting the conversion to NIfTI…

echo

for blind in `cat blindnums.txt` ; do

mkdir /Users/Shared/mri_images/${blind} ;

mri_convert ./${blind}/rawavg.mgz /Users/Shared/mri_images/${blind}/${blind}_rawavg.nii.gz ;

done

read -p “Now done converting to NIfTIs. Press Enter to continue…” shlock

cd /Users/Shared/mri_images

ls >> blindnums.txt

for blind in `cat blindnums.txt` ; do

echo Now converting ${blind}

echo

bet ./${blind}/${blind}_rawavg ./${blind}/${blind}_rawavg_brain -m -R -f .30 ;

done

echo Now FLIRTing…

for blind in `cat blindnums.txt` ; do

cp /Users/Shared/diffusion/${blind}/${blind}_nodif_brain.nii.gz ./${blind}/${blind}_nodif_brain.nii.gz

echo FLIRTing ${blind}…

echo

flirt -in $./${blind}/${blind}_rawavg_brain.nii.gz -ref ./${blind}/${blind}_nodif_brain.nii.gz -out ./${blind}/${blind}_rawavg_brain_nodif_trilinear.nii.gz -omat ./${blind}/${blind}_rawavg_brain_nodif_trilinear.mat -bins 256 -cost corratio -dof 12 ;

done

echo All done! Exiting script.

exit 0

That’s a very simple script written in about 10 minutes. It is not great scripting but it gets the job done and makes all your work reproducible. I’ll explain a few of the commands.

cp = copy [initial location] [target location]

cd = change directories

ls = list (contents of directory)

>> outputs text to a text file. An alternative is to do something like this: ls | tee -a blindnums.txt

echo = print to screen.

mkdir = make directory

for … ; do …. ; done – for loops are very useful to run the same subprocess on a number of inputs.

The script I wrote above is simple. It doesn’t have any error checks in it. It is a good idea to include things that will either not duplicate past efforts (say you already skull stripped some brains but the script processing was interrupted and there was more script to do, you could write an if-then command to check for a file and if it exists to go on to the next step, else it will create the missing file) or allow you to loop back and redo something if it didn’t work.

I’ll go into more about scripting later but I believe that neuroimaging work suffers without scripting knowledge. The best way to learn scripting is to read resources online and then just give it a try.

I also recommend you do the following things (or at least keep them in mind):

  1. Always name files and folders consistently; if you don’t it’s a pain to deal with image processing
  2. Store files in easy to reach locations (/home/images/ is way better than /My \Stuff/Snacks/Chips/Brains/images/Freds \work/penguins/flamingfireballs) – keep things simple
  3. Search the web for help – the solution to your problem is probably out there