standardPreproc.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. function output = standardPreproc(functional4D_fn, structural_fn, fwhm, spm_dir)
  2. % Function to complete preprocessing of structural and functional data from
  3. % a single subject for use in any other Matlab/SPM12 script.
  4. % Steps include coregistering structural image to first functional image,
  5. % segmenting the coregistered structural image into tissue types, and
  6. % reslicing the segments to the functional resolution image grid.
  7. % Makes use of spm12 batch routines. If spm12 batch parameters are not
  8. % explicitly set, defaults are assumed.
  9. %
  10. % INPUT:
  11. % funcional4D_fn - filename of pre-real-time functional scan
  12. % structural_fn - filename of T1-weighted structural scan
  13. % fwhm - kernel size for smoothing operations
  14. % spm_dir - SPM12 directory location
  15. %
  16. % OUTPUT:
  17. % output - structure with filenames and data
  18. %__________________________________________________________________________
  19. % Copyright (C) Stephan Heunis 2018
  20. % Load data
  21. f4D_img = spm_read_vols(spm_vol(functional4D_fn));
  22. [Ni, Nj, Nk, Nt] = size(f4D_img);
  23. % Declare output structure
  24. output = struct;
  25. % STEP 1 -- Realign (estimate and reslice) all functionals to first functional
  26. disp('Step 1 -- Realign all volumes to first functional volume');
  27. realign_estimate_reslice = struct;
  28. % Data
  29. fnms={};
  30. for i = 1:Nt
  31. fnms{i} = [functional4D_fn ',' num2str(i) ];
  32. end
  33. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.data={fnms'};
  34. % Eoptions
  35. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
  36. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.sep = 4;
  37. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
  38. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
  39. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.interp = 2;
  40. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
  41. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.weight = '';
  42. % Roptions
  43. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.which = [2 1];
  44. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.interp = 4;
  45. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
  46. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.mask = 1;
  47. realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
  48. % Run
  49. cfg_util('run',realign_estimate_reslice.matlabbatch);
  50. [d, f, e] = fileparts(functional4D_fn);
  51. output.rfunctional_fn = [d filesep 'r' f e];
  52. output.mp_fn = [d filesep 'rp_' f '.txt'];
  53. output.MP = load(output.mp_fn);
  54. disp('Step 1 - Done!');
  55. % STEP 2 -- Coregister structural image to first dynamic image (estimate only)
  56. disp('Step 2 -- Coregister structural image to first dynamic image');
  57. coreg_estimate = struct;
  58. % Ref
  59. coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.ref = {[functional4D_fn ',1']};
  60. % Source
  61. coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.source = {structural_fn};
  62. % Eoptions
  63. coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
  64. coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
  65. coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.tol = [0.02 0.02 0.02 0.001 0.001 0.001 0.01 0.01 0.01 0.001 0.001 0.001];
  66. coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
  67. % Run
  68. cfg_util('run',coreg_estimate.matlabbatch);
  69. disp('Step 2 - Done!');
  70. % STEP 3 -- Segmentation of coregistered structural image into GM, WM, CSF, etc
  71. % (with implicit warping to MNI space, saving forward and inverse transformations)
  72. disp('Step 3 -- Segmentation');
  73. segmentation = struct;
  74. % Channel
  75. segmentation.matlabbatch{1}.spm.spatial.preproc.channel.biasreg = 0.001;
  76. segmentation.matlabbatch{1}.spm.spatial.preproc.channel.biasfwhm = 60;
  77. segmentation.matlabbatch{1}.spm.spatial.preproc.channel.write = [0 1];
  78. segmentation.matlabbatch{1}.spm.spatial.preproc.channel.vols = {structural_fn};
  79. % Tissue
  80. for t = 1:6
  81. segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).tpm = {[spm_dir filesep 'tpm' filesep 'TPM.nii,' num2str(t)]};
  82. segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).ngaus = t-1;
  83. segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).native = [1 0];
  84. segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).warped = [0 0];
  85. end
  86. segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(1).ngaus = 1;
  87. segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(6).ngaus = 2;
  88. % Warp
  89. segmentation.matlabbatch{1}.spm.spatial.preproc.warp.mrf = 1;
  90. segmentation.matlabbatch{1}.spm.spatial.preproc.warp.cleanup = 1;
  91. segmentation.matlabbatch{1}.spm.spatial.preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
  92. segmentation.matlabbatch{1}.spm.spatial.preproc.warp.affreg = 'mni';
  93. segmentation.matlabbatch{1}.spm.spatial.preproc.warp.fwhm = 0;
  94. segmentation.matlabbatch{1}.spm.spatial.preproc.warp.samp = 3;
  95. segmentation.matlabbatch{1}.spm.spatial.preproc.warp.write=[1 1];
  96. % Run
  97. cfg_util('run',segmentation.matlabbatch);
  98. % Save filenames
  99. [d, f, e] = fileparts(structural_fn);
  100. output.forward_transformation = [d filesep 'y_' f e];
  101. output.inverse_transformation = [d filesep 'iy_' f e];
  102. output.gm_fn = [d filesep 'c1' f e];
  103. output.wm_fn = [d filesep 'c2' f e];
  104. output.csf_fn = [d filesep 'c3' f e];
  105. output.bone_fn = [d filesep 'c4' f e];
  106. output.soft_fn = [d filesep 'c5' f e];
  107. output.air_fn = [d filesep 'c6' f e];
  108. disp('Step 3 - done!');
  109. % STEP 4 -- Reslice all to functional-resolution image grid
  110. disp('Step 4 -- Reslice all to functional-resolution image grid');
  111. reslice = struct;
  112. % Ref
  113. reslice.matlabbatch{1}.spm.spatial.coreg.write.ref = {[functional4D_fn ',1']};
  114. % Source
  115. source_fns = {};
  116. for i = 1:6
  117. source_fns{i} = [d filesep 'c' num2str(i) f e];
  118. end
  119. source_fns{7} = structural_fn;
  120. reslice.matlabbatch{1}.spm.spatial.coreg.write.source = source_fns';
  121. % Roptions
  122. reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.interp = 4;
  123. reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.wrap = [0 0 0];
  124. reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.mask = 0;
  125. reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.prefix = 'r';
  126. % Run
  127. cfg_util('run',reslice.matlabbatch);
  128. % Save filenames
  129. [d, f, e] = fileparts(structural_fn);
  130. output.rstructural_fn = [d filesep 'r' f e];
  131. output.rgm_fn = [d filesep 'rc1' f e];
  132. output.rwm_fn = [d filesep 'rc2' f e];
  133. output.rcsf_fn = [d filesep 'rc3' f e];
  134. output.rbone_fn = [d filesep 'rc4' f e];
  135. output.rsoft_fn = [d filesep 'rc5' f e];
  136. output.rair_fn = [d filesep 'rc6' f e];
  137. disp('Step 4 - done!');
  138. % STEP 5 -- Gaussian kernel smoothing of realigned data
  139. disp('STEP 5 -- Gaussian kernel smoothing of realigned data');
  140. smooth = struct;
  141. % Data
  142. fns={};
  143. for i = 1:Nt
  144. fns{i} = [output.rfunctional_fn ',' num2str(i) ];
  145. end
  146. smooth.matlabbatch{1}.spm.spatial.smooth.data = fns';
  147. % Other
  148. smooth.matlabbatch{1}.spm.spatial.smooth.fwhm = [fwhm fwhm fwhm];
  149. smooth.matlabbatch{1}.spm.spatial.smooth.dtype = 0;
  150. smooth.matlabbatch{1}.spm.spatial.smooth.im = 0;
  151. smooth.matlabbatch{1}.spm.spatial.smooth.prefix = 's';
  152. % Run
  153. cfg_util('run',smooth.matlabbatch);
  154. [d, f, e] = fileparts(output.rfunctional_fn);
  155. output.srfunctional_fn = [d filesep 's' f e];
  156. disp('Step 5 - done!');
  157. % STEP 6 -- Gaussian kernel smoothing of unprocessed data
  158. disp('Step 6 -- Gaussian kernel smoothing of unprocessed data');
  159. smooth = struct;
  160. % Data
  161. fns={};
  162. for i = 1:Nt
  163. fns{i} = [functional4D_fn ',' num2str(i) ];
  164. end
  165. smooth.matlabbatch{1}.spm.spatial.smooth.data = fns';
  166. % Other
  167. smooth.matlabbatch{1}.spm.spatial.smooth.fwhm = [fwhm fwhm fwhm];
  168. smooth.matlabbatch{1}.spm.spatial.smooth.dtype = 0;
  169. smooth.matlabbatch{1}.spm.spatial.smooth.im = 0;
  170. smooth.matlabbatch{1}.spm.spatial.smooth.prefix = 's';
  171. % Run
  172. cfg_util('run',smooth.matlabbatch);
  173. [d, f, e] = fileparts(functional4D_fn);
  174. output.sfunctional_fn = [d filesep 's' f e];
  175. disp('Step 6 - done!');