|
| 1 | +% This script will run through the ROIs made for each subject fo a ROI |
| 2 | +% based anaylisis using MarsBar to get a time course of the activations and |
| 3 | +% a percent signal change for each ROI and subject |
| 4 | + |
| 5 | +clc; |
| 6 | + |
| 7 | +if ~exist('machine_id', 'var') |
| 8 | + machine_id = 2; % 0: container ; 1: Remi ; 2: Beast |
| 9 | +end |
| 10 | + |
| 11 | +% 'MNI' or 'T1w' (native) |
| 12 | +if ~exist('space', 'var') |
| 13 | + space = 'T1w'; |
| 14 | +end |
| 15 | + |
| 16 | +if ~exist('randomize', 'var') |
| 17 | + randomize = 0; |
| 18 | +end |
| 19 | + |
| 20 | +% event specification for getting fitted event time-courses |
| 21 | +contrast_idx = 1; |
| 22 | +event_session_no = repmat(1:4, 1, 4); |
| 23 | +event_type_no = repmat([1:4]', 1, 4)'; |
| 24 | +event_spec = [event_session_no; event_type_no(:)']; |
| 25 | +event_duration = 16; % default SPM event duration |
| 26 | + |
| 27 | +% FOR INFO |
| 28 | +% contrast_ls = { |
| 29 | +% 'Euc-Left + Alm-Left + Euc-Right + Alm-Right > 0' |
| 30 | +% 'Euc-Left + Alm-Left + Euc-Right + Alm-Right < 0' |
| 31 | +% 'Alm-Left + Alm-Right > 0' |
| 32 | +% 'Alm-Left + Alm-Right < 0' |
| 33 | +% 'Euc-Left + Euc-Right > 0' |
| 34 | +% 'Euc-Left + Euc-Right < 0' |
| 35 | +% 'Euc-Right + Alm-Right > 0' |
| 36 | +% 'Euc-Right + Alm-Right < 0' |
| 37 | +% 'Euc-Left + Alm-Left > 0' |
| 38 | +% 'Euc-Left + Alm-Left < 0' |
| 39 | +% 'Euc-Left > 0' |
| 40 | +% 'Euc-Left < 0' |
| 41 | +% 'Alm-Left > 0' |
| 42 | +% 'Alm-Left < 0' |
| 43 | +% 'Euc-Right > 0' |
| 44 | +% 'Euc-Right < 0' |
| 45 | +% 'Alm-Right > 0' |
| 46 | +% 'Alm-Right < 0' |
| 47 | +% 'resp-03 + resp-12 > 0' |
| 48 | +% 'resp-03 + resp-12 < 0'}; |
| 49 | + |
| 50 | +%% |
| 51 | +% setting up directories |
| 52 | +[data_dir, code_dir, output_dir, fMRIprep_DIR] = set_dir(machine_id); |
| 53 | + |
| 54 | +% Set up the SPM defaults, just in case |
| 55 | +addpath(fullfile(spm('dir'), 'toolbox', 'marsbar')); |
| 56 | +% Start marsbar to make sure spm_get works |
| 57 | +marsbar('on'); |
| 58 | + |
| 59 | +% get data info |
| 60 | +bids = spm_BIDS(fullfile(data_dir, 'raw')); |
| 61 | + |
| 62 | +% get subjects |
| 63 | +folder_subj = get_subj_list(output_dir); |
| 64 | +folder_subj = cellstr(char({folder_subj.name}')); % turn subject folders into a cellstr |
| 65 | +[~, ~, folder_subj] = rm_subjects([], [], folder_subj, true); |
| 66 | +nb_subjects = numel(folder_subj); |
| 67 | +group_id = ~cellfun(@isempty, strfind(folder_subj, 'ctrl')); %#ok<*STRCLFH> |
| 68 | + |
| 69 | +% see what GLM to run |
| 70 | +opt = struct(); |
| 71 | +[sets] = get_cfg_GLMS_to_run(); |
| 72 | +[opt, all_GLMs] = set_all_GLMS(opt, sets); |
| 73 | + |
| 74 | +if randomize |
| 75 | + shuffle_subjs = randperm(length(group_id)); |
| 76 | +end |
| 77 | + |
| 78 | +%% for each subject |
| 79 | + |
| 80 | +time_course = {}; |
| 81 | +percent_signal_change = {}; |
| 82 | + |
| 83 | +for i_subj = 1:nb_subjects |
| 84 | + |
| 85 | + fprintf('running %s\n', folder_subj{i_subj}); |
| 86 | + |
| 87 | + subj_dir = fullfile(output_dir, [folder_subj{i_subj}]); |
| 88 | + |
| 89 | + roi_src_folder = fullfile(data_dir, 'derivatives', 'ANTs', folder_subj{i_subj}, 'roi'); |
| 90 | + if strcmp(space, 'MNI') |
| 91 | + roi_src_folder = fullfile(code_dir, 'inputs'); |
| 92 | + end |
| 93 | + |
| 94 | + roi_tgt_folder = fullfile(subj_dir, 'roi'); |
| 95 | + mkdir(roi_tgt_folder); |
| 96 | + |
| 97 | + marsbar_save_folder = fullfile(output_dir, '..', 'marsbar', folder_subj{i_subj}); |
| 98 | + mkdir(marsbar_save_folder); |
| 99 | + |
| 100 | + % list ROIs |
| 101 | + roi_ls = spm_select('FPList', ... |
| 102 | + roi_src_folder, ... |
| 103 | + ['^ROI-.*_space-' space '.nii$']); |
| 104 | + roi_ls = cellstr(roi_ls); |
| 105 | + |
| 106 | + % go through all the models specified and get for each ROI the percetn |
| 107 | + % signal change and time course |
| 108 | + fprintf(' running GLMs\n'); |
| 109 | + for i_GLM = 1:size(all_GLMs) |
| 110 | + |
| 111 | + cfg = get_configuration(all_GLMs, opt, i_GLM); |
| 112 | + |
| 113 | + cfg_list{i_GLM} = cfg; |
| 114 | + |
| 115 | + % directory for this specific analysis |
| 116 | + analysis_dir = name_analysis_dir(cfg, space); |
| 117 | + analysis_dir = fullfile ( ... |
| 118 | + output_dir, ... |
| 119 | + folder_subj{i_subj}, 'stats', analysis_dir); |
| 120 | + |
| 121 | + SPM = load(fullfile(analysis_dir, 'SPM.mat')); |
| 122 | + |
| 123 | + for i_roi = 1:size(roi_ls, 1) |
| 124 | + |
| 125 | + roi = roi_ls{i_roi}; |
| 126 | + |
| 127 | + [path, file] = spm_fileparts(roi); |
| 128 | + |
| 129 | + img = spm_read_vols(spm_vol(roi)); |
| 130 | + |
| 131 | + disp(sum(img(:) > 0)); |
| 132 | + |
| 133 | + % create ROI object for Marsbar and convert to matrix format to avoid delicacies of image format |
| 134 | + roi_obj = maroi_image(struct('vol', spm_vol(roi), 'binarize', 1, ... |
| 135 | + 'func', [])); |
| 136 | + roi_obj = maroi_matrix(roi_obj); |
| 137 | + |
| 138 | + % give it a label |
| 139 | + label(roi_obj, strrep(file, 'ROI-', '')); |
| 140 | + saveroi(roi_obj, fullfile(roi_tgt_folder, [file '_roi.mat'])); |
| 141 | + |
| 142 | + D = mardo(SPM); |
| 143 | + |
| 144 | + try |
| 145 | + |
| 146 | + % Extract data |
| 147 | + Y = get_marsy(roi_obj, D, 'mean'); |
| 148 | + |
| 149 | + % MarsBaR estimation |
| 150 | + E = estimate(D, Y); |
| 151 | + |
| 152 | + % Get, store statistics |
| 153 | + stat_struct = compute_contrasts(E, contrast_idx); |
| 154 | + |
| 155 | + % And fitted time courses |
| 156 | + [tc, dt] = event_fitted(E, event_spec, event_duration); |
| 157 | + |
| 158 | + % Get percent signal change |
| 159 | + psc = event_signal(E, event_spec, event_duration, 'abs max'); |
| 160 | + |
| 161 | + % Make fitted time course into ~% signal change |
| 162 | + block_means(E); |
| 163 | + tc = tc / mean(block_means(E)) * 100; |
| 164 | + |
| 165 | + % Store values |
| 166 | + time_course{i_roi, i_GLM}(i_subj, :) = tc; %#ok<SAGROW> |
| 167 | + percent_signal_change{i_GLM}(i_subj, i_roi) = psc; |
| 168 | + |
| 169 | + % sqve for this subject, ROI, GLM |
| 170 | + name_save_file = fullfile(marsbar_save_folder, ... |
| 171 | + [file '_' name_analysis_dir(cfg, space) '.mat']); |
| 172 | + save(name_save_file, 'tc', 'psc', 'cfg', 'file', 'dt'); |
| 173 | + |
| 174 | + catch |
| 175 | + |
| 176 | + warning('\n\nSomething went wrong: %s - %s\n\n', ... |
| 177 | + folder_subj{i_subj}, file); |
| 178 | + |
| 179 | + end |
| 180 | + |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + if randomize |
| 185 | + time_course{i_roi, i_GLM} = time_course{i_roi, i_GLM}(shuffle_subjs, :); |
| 186 | + percent_signal_change{i_GLM} = percent_signal_change{i_GLM}(shuffle_subjs); |
| 187 | + end |
| 188 | + |
| 189 | +end |
0 commit comments