ery_4a_prep_s3_smooth.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. %% LaBGAScore_prep_s2_smooth
  2. %
  3. % This script unzips fMRIprep output images, smooth them, zip the
  4. % smoothed images, and delete all the unzipped images again
  5. %
  6. % USAGE
  7. %
  8. % Script should be run from the root directory of the superdataset, e.g.
  9. % /data/proj_discoverie
  10. % The script is generic, i.e. it does not require study-specific adaptions,
  11. % but you can change some default options if required
  12. %
  13. %
  14. % DEPENDENCIES
  15. %
  16. % 1. LaBGAScore Github repo on Matlab path, with subfolders
  17. % https://github.com/labgas/LaBGAScore
  18. % 2. spm12 on Matlab path, without subfolders
  19. % will be checked by calling LaBGAScore_prep_s0_define_directories
  20. %
  21. %
  22. % INPUTS
  23. %
  24. % preprocessed .nii.gz images outputted by fMRIprep
  25. % variables created by running LaBGAScore_prep_s0_define_directories from
  26. % the root directory of your (super)dataset
  27. %
  28. %
  29. % OUTPUT
  30. %
  31. % smoothed .nii.gz images
  32. %
  33. %
  34. % OPTIONS
  35. %
  36. % 1. fwhm
  37. % smoothing kernel width in mm
  38. % 2. prefix
  39. % string defining prefix of choice for smoothing images
  40. % 3. subjs2smooth
  41. % cell array of subjects in derivdir you want to smooth, empty cell array
  42. % if you want to loop over all subjects
  43. %
  44. %__________________________________________________________________________
  45. %
  46. % author: Lukas Van Oudenhove
  47. % date: November, 2021
  48. %
  49. %__________________________________________________________________________
  50. % @(#)% LaBGAScore_prep_s2_smooth.m v1.1
  51. % last modified: 2022/03/15
  52. %% SET SMOOTHING OPTIONS, AND SUBJECTS
  53. %--------------------------------------------------------------------------
  54. fwhm = 6; % kernel width in mm
  55. prefix = 's6-'; % prefix for name of smoothed images
  56. subjs2smooth = {}; % enter subjects separated by comma if you only want to smooth selected subjects e.g. {'sub-01','sub-02'}
  57. %% DEFINE DIRECTORIES
  58. %--------------------------------------------------------------------------
  59. LaBGAScore_prep_s0_define_directories;
  60. %% UNZIP IMAGES, SMOOTH, ZIP, SMOOTHED IMAGES, AND DELETE ALL UNZIPPED IMAGES
  61. %----------------------------------------------------------------------------
  62. if ~isempty(subjs2smooth)
  63. [C,ia,~] = intersect(derivsubjs,subjs2smooth);
  64. if ~isequal(C,subjs2smooth)
  65. error('\n subject %s defined in subjs2smooth not present in %s, please check before proceeding',subjs2smooth{~ismember(subjs2smooth,C)},derivdir);
  66. else
  67. for sub=ia'
  68. cd([derivsubjdirs{sub,:},'/func']);
  69. % unzip .nii.gz files
  70. gunzip('*preproc_bold*.nii.gz');
  71. % write smoothing spm batch
  72. clear matlabbatch;
  73. matlabbatch = struct([]);
  74. scans=spm_select('ExtFPList',pwd,'.*\.nii$',Inf);
  75. kernel = ones(1,3).*fwhm;
  76. matlabbatch{1}.spm.spatial.smooth.data = cellstr(scans);
  77. matlabbatch{1}.spm.spatial.smooth.fwhm = kernel;
  78. matlabbatch{1}.spm.spatial.smooth.dtype = 0;
  79. matlabbatch{1}.spm.spatial.smooth.im = 0;
  80. matlabbatch{1}.spm.spatial.smooth.prefix = prefix;
  81. % save batch and run
  82. eval(['save ' derivsubjs{sub,:} '_smooth.mat matlabbatch']);
  83. spm_jobman('initcfg');
  84. spm_jobman('run',matlabbatch);
  85. % zip smoothed files
  86. gzip('s6*');
  87. % delete all unzipped files
  88. delete('*.nii');
  89. end % for loop over subjs2smooth
  90. end % if loop checking intersection of subjs2smooth and subjdirs
  91. else
  92. for sub=1:size(derivsubjdirs,1)
  93. cd([derivsubjdirs{sub,:},'/func']);
  94. % unzip .nii.gz files
  95. gunzip('*preproc_bold*.nii.gz');
  96. % write smoothing spm batch
  97. clear matlabbatch;
  98. matlabbatch = struct([]);
  99. scans=spm_select('ExtFPList',pwd,'.*\.nii$',Inf);
  100. kernel = ones(1,3).*fwhm;
  101. matlabbatch{1}.spm.spatial.smooth.data = cellstr(scans);
  102. matlabbatch{1}.spm.spatial.smooth.fwhm = kernel;
  103. matlabbatch{1}.spm.spatial.smooth.dtype = 0;
  104. matlabbatch{1}.spm.spatial.smooth.im = 0;
  105. matlabbatch{1}.spm.spatial.smooth.prefix = prefix;
  106. % save batch and run
  107. eval(['save ' derivsubjs{sub,:} '_smooth.mat matlabbatch']);
  108. spm_jobman('initcfg');
  109. spm_jobman('run',matlabbatch);
  110. % zip smoothed files
  111. gzip('s6*');
  112. % delete all unzipped files
  113. delete('*.nii');
  114. end % for loop over subjdirs
  115. end % if loop checking smoothing option
  116. cd(rootdir);