Run SPM from linux terminal

Hello all,

I’m trying to write a script (.sh) to preprocess some brain MRI (T1 and FLAIR). First and second steps (convert dicom to nifti and N4bias correction) are done, now I want to segment the T1 image using SPM and the white matter hyperintensities of FLAIR images using the Lesion Segmentation Toolbox (LPA) also from SPM, but I don’t know how to do it from the terminal (in other words, how to add this step to my script).

Also, because I have several images in the same folder, the script needs to match just the T1 and FLAIR images I want to segment for every subject

My folders look like:
– subject_1
----- preprocessing
– subject_2
----- preprocessing

So, the match would be something like folder/*/preprocessing/*T1.nii ffor the T1 images and folder/*/preprocessing/*FLAIR.nii for FLAIR images

I hope you can help me.

Hi @Al-yhuwert,

not an SPM person, so my response might not be the most accurate/helpful.

However, IIRC you can call matlab and thus SPM functions via .sh scripts. I think there might be different options to do that:

  1. you create a matlab script/function that runs the commands you want (for an example check this section of Andy Jahn’s great “Brain Book”) and call that via the shell:
    matlab -nodesktop -nosplash -r "segment_T1_FLAIR"
  2. call each matlab/spm script/function via the shell:
    matlab -nodesktop -nosplash -r "function1(argument1,argument2);exit"
    matlab -nodesktop -nosplash -r "function2(argument1);exit"
    (check this stackoverflow thread for details)

I guess the precise implementation depends on your setup (local machine, HPC, etc.) and how you want to define for-loops et al. . If you’re up for it, you could share a toy version of your script so that folks can provide more precise help (I hope other folks with more experience wrt this will reply as well).

HTH, cheers, Peer

1 Like

Thank you @PeerHerholz , I’ll have a look at these recommendations. For now I have nothing in matlab, the first two steps were done with MRtrix. I’ll try a first attempt and see what can I do (I’m a beginner so this have been a bit difficult but exciting hehe). Thanks again.