Create1stLevelMat.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. % Create1stLevelMat.m
  2. %
  3. % This function sets up the .mat file and executes it for the GLM analysis of
  4. % the 1st level
  5. %
  6. % input:
  7. % outputDir - directory to save 1st level matrix as well as results from estimation
  8. % scansDir - directory that holds scans
  9. % intsDir - directory where regressor intervals are stored
  10. % subjId - ID of the subject
  11. function Create1stLevelMat(outputDir, scansDir, intsDir, subjId)
  12. % constant of fMRI scanner for this experiment
  13. % repetition time
  14. rt = 2; % included in .json file
  15. numOfVolumes = 542;
  16. c_runIds = [1 2 3 4 5 6 7 8];
  17. c_spExt = '_sp.txt';
  18. c_saccExt = '_sacc.txt';
  19. % assure that output dir exists
  20. if (~exist(outputDir))
  21. mkdir(outputDir)
  22. end
  23. % make sure subject is string
  24. assert(ischar(subjId), 'Subject id should be string');
  25. assert(length(subjId)==2, 'Subject id should have length 2');
  26. % create jobs structure
  27. matlabbatch{1,1}.spm.stats.fmri_spec.dir = cellstr(outputDir);
  28. matlabbatch{1,1}.spm.stats.fmri_spec.timing.units = 'secs';
  29. matlabbatch{1,1}.spm.stats.fmri_spec.timing.RT = rt;
  30. matlabbatch{1,1}.spm.stats.fmri_spec.timing.fmri_t = 16;
  31. matlabbatch{1,1}.spm.stats.fmri_spec.timing.fmri_t0 = 8;
  32. % start of multisession part
  33. for sessId=1:length(c_runIds)
  34. runId = c_runIds(sessId);
  35. % get scans for the session
  36. filter = ['sws*run-' num2str(runId) '*'];
  37. boldFile = glob([scansDir '/' filter]);
  38. assert(size(boldFile,1)==1, 'More than one or no files were found. Regexp shoulf be refined');
  39. [dir, basename, ext] = fileparts(boldFile{1});
  40. files = spm_select('ExtFPList', scansDir, basename, 1:numOfVolumes);
  41. matlabbatch{1,1}.spm.stats.fmri_spec.sess(1,sessId).scans = cellstr(files);
  42. nscans = size(files,1);
  43. % create sp condition struct
  44. spTimingFile = glob([intsDir '/sub-' subjId '*run-' num2str(runId) '*' c_spExt]);
  45. assert(size(spTimingFile,1)==1, 'Exactly one SP timing file should be found');
  46. sp = importdata(spTimingFile{1}, ',');
  47. spOnsets = sp(:,1)./1000000; % convert to seconds
  48. spDurations = zeros(size(spOnsets)); % make it like impulse response
  49. spValues = sp(:,3);
  50. spMod = struct('name', {'sp_modulation'}, 'param', {spValues}, 'poly', {1});
  51. structSP = struct('name', {'SP'}, 'onset', {spOnsets}, 'duration', {spDurations}, 'tmod', {0}, 'pmod', {spMod}, 'orth', {1});
  52. % create saccade condition timing
  53. saccTimingFile = glob([intsDir '/sub-' subjId '*run-' num2str(runId) '*' c_saccExt]);
  54. assert(size(saccTimingFile,1)==1, 'Exactly one saccade timing file should be found');
  55. sacc = importdata(saccTimingFile{1}, ',');
  56. saccOnsets = sacc(:,1)./1000000; %convert to seconds
  57. saccDurations = zeros(size(saccOnsets)); % convert to impulse response
  58. saccValues = sacc(:,3);
  59. saccMod = struct('name', {'saccade_modulation'}, 'param', {saccValues}, 'poly', {1});
  60. structSacc = struct('name', {'saccade'}, 'onset', {saccOnsets}, 'duration', {saccDurations}, 'tmod', {0}, 'pmod', {saccMod}, 'orth', {1});
  61. matlabbatch{1,1}.spm.stats.fmri_spec.sess(1,sessId).cond = [structSP, structSacc];
  62. matlabbatch{1,1}.spm.stats.fmri_spec.sess(1,sessId).multi = {''};
  63. matlabbatch{1,1}.spm.stats.fmri_spec.sess(1,sessId).regress = struct([]);
  64. alignFile = glob([scansDir '/rp*run-' num2str(runId) '*.txt']);
  65. assert(size(alignFile,1)==1, 'Too many or too few rp_.. files');
  66. matlabbatch{1,1}.spm.stats.fmri_spec.sess(1,sessId).multi_reg = cellstr(alignFile);
  67. matlabbatch{1,1}.spm.stats.fmri_spec.sess(1,sessId).hpf = 128;
  68. end
  69. % end of multisession part
  70. matlabbatch{1,1}.spm.stats.fmri_spec.fact = struct([]);
  71. matlabbatch{1,1}.spm.stats.fmri_spec.bases.hrf = struct('derivs', {[1,1]}); % time and dispersion derivatives
  72. matlabbatch{1,1}.spm.stats.fmri_spec.volt = 1;
  73. matlabbatch{1,1}.spm.stats.fmri_spec.global = 'None';
  74. matlabbatch{1,1}.spm.stats.fmri_spec.mthresh = 0.8;
  75. matlabbatch{1,1}.spm.stats.fmri_spec.mask = {''};
  76. matlabbatch{1,1}.spm.stats.fmri_spec.cvi = 'AR(1)';
  77. save([ outputDir '/1st_level_file.mat'], 'matlabbatch');
  78. % configure matlab batch for estimation (saves SPM.mat)
  79. spm_jobman('run', matlabbatch);
  80. % estimate GLM (creates beta.nii for conditions and ResMS.nii for residual)
  81. initialPath = pwd;
  82. cd(outputDir); % spm_spm works if we are at the same directory as SPM.mat
  83. load SPM;
  84. spm_spm(SPM);
  85. % move to initial path
  86. cd(initialPath);
  87. end