preproc_vbm.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. % preproc_vbm - preprocessing for voxel-based morphometry
  2. %
  3. % Need to specify:
  4. % * Parent directory for directory of Template, average and mask images
  5. % * Structural images for all subjects
  6. % * A filename for the spreadsheet (CSV) of total tissue volumes
  7. %
  8. % PLEASE NOTE:
  9. % There is a minor bug in this version: the file Template_6_2mni.mat will
  10. % be left in the directory of the first selected structural image; not
  11. % moved to the Templates_etc directory along with the Template image files.
  12. %__________________________________________________________________________
  13. % Copyright (C) 2014 Wellcome Trust Centre for Neuroimaging
  14. % Ged Ridgway
  15. % $Id: preproc_vbm.m 6177 2014-09-16 10:44:55Z guillaume $
  16. if exist('preproc_vbm', 'file') && ~isdeployed
  17. help preproc_vbm
  18. end
  19. matlabbatch = {};
  20. %% Make directory to hold Dartel Templates, average image, mask
  21. iDir = length(matlabbatch) + 1;
  22. matlabbatch{iDir}.cfg_basicio.file_dir.dir_ops.cfg_mkdir.parent = '<UNDEFINED>';
  23. matlabbatch{iDir}.cfg_basicio.file_dir.dir_ops.cfg_mkdir.name = 'Templates_etc';
  24. %% Segmentation
  25. iSeg = length(matlabbatch) + 1;
  26. matlabbatch{iSeg}.spm.spatial.preproc.channel.write = [0 1];
  27. ngaus = [1 1 2 3 4 2];
  28. native = [
  29. 1 0 0 0 0 0
  30. 1 1 0 0 0 0];
  31. warped = [
  32. 0 0 0 0 0 0
  33. 1 1 1 0 0 0];
  34. for c = 1:6 % tissue class c
  35. matlabbatch{iSeg}.spm.spatial.preproc.tissue(c).tpm = {
  36. fullfile(spm('dir'), 'tpm', sprintf('TPM.nii,%d', c))};
  37. matlabbatch{iSeg}.spm.spatial.preproc.tissue(c).ngaus = ngaus(c);
  38. matlabbatch{iSeg}.spm.spatial.preproc.tissue(c).native = native(:, c)';
  39. matlabbatch{iSeg}.spm.spatial.preproc.tissue(c).warped = warped(:, c)';
  40. end
  41. %% Dartel - create template
  42. iDar = length(matlabbatch) + 1;
  43. matlabbatch{iDar}.spm.tools.dartel.warp.images{1}(1) = ...
  44. cfg_dep('Segment: rc1 Images', substruct('.','val', '{}',{iSeg}, ...
  45. '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  46. substruct('.','tiss', '()',{1}, '.','rc', '()',{':'}));
  47. matlabbatch{iDar}.spm.tools.dartel.warp.images{2}(1) = ...
  48. cfg_dep('Segment: rc2 Images', substruct('.','val', '{}',{iSeg}, ...
  49. '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  50. substruct('.','tiss', '()',{2}, '.','rc', '()',{':'}));
  51. %% Normalise segmentations to MNI space
  52. iNS = length(matlabbatch) + 1;
  53. matlabbatch{iNS}.spm.tools.dartel.mni_norm.template(1) = ...
  54. cfg_dep('Run Dartel (create Templates): Template (Iteration 6)', ...
  55. substruct('.','val', '{}',{iDar}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  56. substruct('.','template', '()',{7}));
  57. matlabbatch{iNS}.spm.tools.dartel.mni_norm.data.subjs.flowfields(1) = ...
  58. cfg_dep('Run Dartel (create Templates): Flow Fields', ...
  59. substruct('.','val', '{}',{iDar}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  60. substruct('.','files', '()',{':'}));
  61. matlabbatch{iNS}.spm.tools.dartel.mni_norm.data.subjs.images{1}(1) = ...
  62. cfg_dep('Segment: c1 Images', ...
  63. substruct('.','val', '{}',{iSeg}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  64. substruct('.','tiss', '()',{1}, '.','c', '()',{':'}));
  65. matlabbatch{iNS}.spm.tools.dartel.mni_norm.preserve = 1;
  66. matlabbatch{iNS}.spm.tools.dartel.mni_norm.fwhm = [8 8 8];
  67. %% Average the smwc1 images
  68. iAS = length(matlabbatch) + 1;
  69. matlabbatch{iAS}.spm.util.imcalc.input(1) = ...
  70. cfg_dep('Normalise to MNI Space: MNI Smo. Warped - Amount (Image 1)', ...
  71. substruct('.','val', '{}',{iNS}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  72. substruct('()',{':', 1}));
  73. matlabbatch{iAS}.spm.util.imcalc.output = 'average_smwc1';
  74. matlabbatch{iAS}.spm.util.imcalc.outdir(1) = ...
  75. cfg_dep('Make Directory: Make Directory ''Templates_etc''', ...
  76. substruct('.','val', '{}',{iDir}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  77. substruct('.','dir'));
  78. matlabbatch{iAS}.spm.util.imcalc.expression = 'median(X)';
  79. matlabbatch{iAS}.spm.util.imcalc.options.dmtx = 1;
  80. %% Threshold the tissue average to get an explicit mask
  81. iEM = length(matlabbatch) + 1;
  82. matlabbatch{iEM}.spm.util.imcalc.input(1) = ...
  83. cfg_dep('Image Calculator: Imcalc Computed Image: average_smwc1', ...
  84. substruct('.','val', '{}',{iAS}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  85. substruct('.','files'));
  86. matlabbatch{iEM}.spm.util.imcalc.output = 'mask_smwc1';
  87. matlabbatch{iEM}.spm.util.imcalc.outdir(1) = cfg_dep('Make Directory: Make Directory ''Templates_etc''', substruct('.','val', '{}',{iDir}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','dir'));
  88. matlabbatch{iEM}.spm.util.imcalc.expression = 'i1>0.2';
  89. matlabbatch{iEM}.spm.util.imcalc.options.dtype = 2;
  90. %% Normalise the bias corrected images to MNI space
  91. iNI = length(matlabbatch) + 1;
  92. matlabbatch{iNI}.spm.tools.dartel.mni_norm.template(1) = ...
  93. cfg_dep('Run Dartel (create Templates): Template (Iteration 6)', ...
  94. substruct('.','val', '{}',{iDar}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  95. substruct('.','template', '()',{7}));
  96. matlabbatch{iNI}.spm.tools.dartel.mni_norm.data.subjs.flowfields(1) = ...
  97. cfg_dep('Run Dartel (create Templates): Flow Fields', ...
  98. substruct('.','val', '{}',{iDar}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  99. substruct('.','files', '()',{':'}));
  100. matlabbatch{iNI}.spm.tools.dartel.mni_norm.data.subjs.images{1}(1) = ...
  101. cfg_dep('Segment: Bias Corrected (1)', ...
  102. substruct('.','val', '{}',{iSeg}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  103. substruct('.','channel', '()',{1}, '.','biascorr', '()',{':'}));
  104. matlabbatch{iNI}.spm.tools.dartel.mni_norm.preserve = 0;
  105. matlabbatch{iNI}.spm.tools.dartel.mni_norm.fwhm = [0 0 0];
  106. %% Average the normalised bias corrected images
  107. iAI = length(matlabbatch) + 1;
  108. matlabbatch{iAI}.spm.util.imcalc.input(1) = ...
  109. cfg_dep('Normalise to MNI Space: MNI Smo. Warped - Concentrations (Image 1)', ...
  110. substruct('.','val', '{}',{7}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  111. substruct('()',{':', 1}));
  112. matlabbatch{iAI}.spm.util.imcalc.output = 'average_image';
  113. matlabbatch{iAI}.spm.util.imcalc.outdir(1) = ...
  114. cfg_dep('Make Directory: Make Directory ''Templates_etc''', ...
  115. substruct('.','val', '{}',{iDir}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  116. substruct('.','dir'));
  117. matlabbatch{iAI}.spm.util.imcalc.expression = 'median(X)';
  118. matlabbatch{iAI}.spm.util.imcalc.options.dmtx = 1;
  119. %% Show the average image and explicit mask in Check Reg
  120. iCR = length(matlabbatch) + 1;
  121. matlabbatch{iCR}.spm.util.checkreg.data(1) = ...
  122. cfg_dep('Image Calculator: Imcalc Computed Image: average_image', ...
  123. substruct('.','val', '{}',{iAI}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  124. substruct('.','files'));
  125. matlabbatch{iCR}.spm.util.checkreg.data(2) = ...
  126. cfg_dep('Image Calculator: Imcalc Computed Image: mask_smwc1', ...
  127. substruct('.','val', '{}',{iEM}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  128. substruct('.','files'));
  129. %% Compute tissue volumes
  130. iTV = length(matlabbatch) + 1;
  131. matlabbatch{iTV}.spm.util.tvol.matfiles(1) = ...
  132. cfg_dep('Segment: Seg Params', ...
  133. substruct('.','val', '{}',{iSeg}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  134. substruct('.','param', '()',{':'}));
  135. matlabbatch{iTV}.spm.util.tvol.outf = '<UNDEFINED>';
  136. %% Move the Dartel templates to the Templates_etc directory
  137. % BUG: The mat file for the affine reg of Template_6 to MNI is not moved.
  138. % This is not too serious, since another will be created if norm-to-MNI is
  139. % repeated later on the relocated template. Probably best to add a new
  140. % option for a "Template directory" to the Dartel create template module.
  141. iMov = length(matlabbatch) + 1;
  142. for it = 0:6
  143. matlabbatch{iMov}.cfg_basicio.file_dir.file_ops.file_move.files(it+1) = ...
  144. cfg_dep(sprintf('Run Dartel (create Templates): Template (Iteration %d)', it), ...
  145. substruct('.','val', '{}',{iDar}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  146. substruct('.','template', '()',{it+1}));
  147. end
  148. matlabbatch{iMov}.cfg_basicio.file_dir.file_ops.file_move.action.moveto(1) = ...
  149. cfg_dep('Make Directory: Make Directory ''Templates_etc''', ...
  150. substruct('.','val', '{}',{iDir}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  151. substruct('.','dir'));
  152. %% Delete (most) unnecessary files
  153. iDel = length(matlabbatch) + 1;
  154. for c = 1:3
  155. matlabbatch{iDel}.cfg_basicio.file_dir.file_ops.file_move.files(c) = ...
  156. cfg_dep(sprintf('Segment: mwc%d Images', c), ...
  157. substruct('.','val', '{}',{iSeg}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  158. substruct('.','tiss', '()',{c}, '.','mwc', '()',{':'}));
  159. end
  160. matlabbatch{iDel}.cfg_basicio.file_dir.file_ops.file_move.files(4) = ...
  161. cfg_dep('Segment: Bias Corrected (1)', ...
  162. substruct('.','val', '{}',{iSeg}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  163. substruct('.','channel', '()',{1}, '.','biascorr', '()',{':'}));
  164. matlabbatch{iDel}.cfg_basicio.file_dir.file_ops.file_move.files(5) = ...
  165. cfg_dep('Normalise to MNI Space: MNI Smo. Warped - Concentrations (Image 1)', ...
  166. substruct('.','val', '{}',{iNI}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), ...
  167. substruct('()',{':', 1}));
  168. matlabbatch{iDel}.cfg_basicio.file_dir.file_ops.file_move.action.delete = false;
  169. %% If run as script, open matlabbatch GUI
  170. % NB mfilename is cfg_load_jobs if loaded from matlabbatch GUI
  171. if strcmp(mfilename, 'preproc_vbm')
  172. spm_jobman('interactive', matlabbatch)
  173. end