-
Notifications
You must be signed in to change notification settings - Fork 0
/
roi_extraction.m
1411 lines (1158 loc) · 46.6 KB
/
roi_extraction.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function roi_extraction(varargin)
% ROI_EXTRACTION toolbox v0.1
%
% SYNTAX
% roi_extraction()
%
%
% DESCRIPTION
%
% This toolbox allows a semi-automated roi definition procedure.
% The graphics user interface requires that you input:
%
% 1 - the folder in which your subjects are located (e.g. /home/myproject/mridata)
% 2 - the SPM.mat of the first subject for the functional localiser from which you want to expand ROIs (e.g. /home/myproject/mridata/subject1/functionallocaliserstats/SPM.mat)
% 3 - the native space anatomical of the first subject of the study (e.g. /home/myproject/mridata/subject1/structurals/sMPRAGE.nii)
% 4 - whether you want to use contiguous expansion of the ROIs or discontiguous
% (discontiguous is not recommended for now and an option to add an anatomically defined inclusion mask will be ported in future releases)
% 5 - the Number of voxels that you wish to save in your binary mask (this also handles matlab regular expression e.g. linspace(20,400,20) or vectors e.g. [20 40 60 80 100])
% 6 - the activation contrast that you entered in SPM at the first level to define this ROI
% 7 - A name that you want to use for your ROI masks (e.g. FFA). The name will be used in twofolds:
% a: a folder will be created to store binary .img masks with this name (e.g. /home/myproject/mridata/subject1/masks/FFA/)
% b: the name will be used as a suffix for the number of voxels chosen (e.g. /home/myproject/mridata/subject1/masks/FFA/leftFFA_20.img
% /home/myproject/mridata/subject1/masks/FFA/leftFFA_40.img ...)
%
% As hinted above, the toolbox will proceed with left and right hemisphere definitions independently, one after the other.
% The toolbox will recursively select the subjects contained in your mridata folder so it is important that your folder architecture
% remain the same across all the subjects.
%
% REQUIREMENTS:
%
% - SPM 8 OR ABOVE NEEDS TO BE DEFINED IN MATLAB PATH
%
% Medical Research Council - Cognition and Brain Sciences Unit
% Ian Charest - August 2014
%
% Last Modified by Ian Charest 03-Aug-2014 10:45:20
warnstate = warning;
warning off;
%% Check for SPM
try
spmdir = spm('dir');
%spm('defaults', 'fmri');
catch
disp('Please add spm path.');
warning(warnstate(1).state);
return
end
%% define GUI and handle the case of a GUI callback
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @roi_extraction_OpeningFcn, ...
'gui_OutputFcn', @roi_extraction_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
gui_mainfcn(gui_State, varargin{:});
function roi_extraction_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to roi_extraction (see VARARGIN)
% Choose default command line output for roi_extraction
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes roi_extraction wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function pushbuttonfolder_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonfolder (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.studydir = uigetdir('Please select your study folder');
try
d = dir(handles.studydir);
isub = [d(:).isdir]; % returns logical vector
handles.subjects = {d(isub).name}';
handles.subjects(ismember(handles.subjects,{'.','..'})) = [];
handles.subjects(cellfun(@isempty,strfind(handles.subjects,'CBU'))) = [];
end
guidata(hObject, handles);
% --- Executes on button press in pushbuttonspm
function pushbuttonspm_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonfolder (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles,'studydir')
FilterSpec = fullfile(handles.studydir,'*SPM.mat');
else
FilterSpec = fullfile('*SPM.mat');
end
[jnk,PathName] = uigetfile(FilterSpec,'Please select the first subject''s SPM.mat');
try
[name, subject, studydir] = getpathinfo(PathName);
if ~(strcmp(studydir,handles.studydir))
warndlg({'Warining, that SPM.mat seems to come from a different study';'Things could go berserk from here!'});
end
if ~(ismember(subject,handles.subjects))
warndlg({'Warining, either you have not yet defined the study folder or';'that subject (SPM.mat) is not in study dir'});
end
handles.statsdir = name;
end
guidata(hObject, handles);
% --- Executes on button press in pushbuttonanatomy.
function pushbuttonanatomy_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonanatomy (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles,'studydir')
FilterSpec = fullfile(handles.studydir,'*.nii');
else
FilterSpec = pwd;
end
[jnk,PathName] = uigetfile(FilterSpec,'Please select the first subject''s native space anatomical image');
try
[name, subject, studydir] = getpathinfo(PathName);
if ~(strcmp(studydir,handles.studydir))
warndlg({'Warining, that anatomy seems to come from a different study';'Things could go berserk from here!'});
end
if ~(ismember(subject,handles.subjects))
warndlg({'Warining, either you have not yet defined the study folder or';'that subject anatomy is not in study dir'});
end
handles.anatomydir = name;
end
guidata(hObject, handles);
% --- Executes on button press in pushbuttonanatomymask.
function pushbuttonanatomymask_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonanatomymask (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles,'studydir')
FilterSpec = fullfile(handles.studydir,'*.nii');
else
FilterSpec = pwd;
end
[jnk,PathName] = uigetfile(FilterSpec,'Please select the first subject''s anatomical mask to constrain the search');
try
[name, maskfolder, studydir] = getpathinfo(PathName);
handles.anatomymaskdir = fullfile(maskfolder,name);
end
guidata(hObject, handles);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
tref = str2double(get(hObject,'String')) ;
handles.tmap = tref;
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
try
nvoxels = eval(get(hObject,'String'));
catch
nvoxels = str2double(get(hObject,'String'));
end
handles.nvoxels = nvoxels;
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function MyCheckboxCallback(hObject, eventdata, handles)
other = setdiff(get(handles.uipanel1,'Children'),hObject);
for ii = 1:length(other)
set(other(ii),'Value',get(other(ii),'Min'));
end
function checkbox1_Callback(hObject, eventdata, handles)
MyCheckboxCallback(hObject, eventdata, handles)
handles.extendmethod = 'contiguous';
guidata(hObject, handles);
function checkbox2_Callback(hObject, eventdata, handles)
MyCheckboxCallback(hObject, eventdata, handles)
handles.extendmethod = 'discontiguous';
guidata(hObject, handles);
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.roiname = get(hObject,'String');
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbuttonstart.
function pushbuttonstart_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonstart (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if allfieldscomplete(handles)==1
extractROI(handles)
else
warndlg('You need to fill in each fields')
end
%% --------------------------------------------------------------------------
% --- Outputs from this function are returned to the command line.
function varargout = roi_extraction_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = hObject;
varargout{2} = eventdata;
varargout{3} = handles;
function [name, subject, studydir] = getpathinfo(filepath)
delimiters = strfind(filepath,filesep);
name = deblank(filepath(delimiters(end-1)+1:delimiters(end)-1));
subject = filepath(delimiters(end-2)+1:delimiters(end-1)-1);
studydir = filepath(1:delimiters(end-2)-1);
function answer=allfieldscomplete(handles)
% the toolbox need to have
% studydir
% subjects
% statsdir
% contrast
% anatomydir
% nvoxels
if isfield(handles,'studydir') && isfield(handles,'subjects') ...
&& isfield(handles,'statsdir') && isfield(handles,'tmap') && isfield(handles,'extendmethod') ...
&& isfield(handles,'nvoxels') && isfield(handles,'anatomydir') && isfield(handles,'roiname')
answer=1;
else
answer=0;
end
function extractROI(options)
% roiExtraction version 0.0
%
%
% http://www.mrc-cbu.cam.ac.uk/people/ian.charest
%
% by Ian Charest
mridataPath = fullfile(options.studydir);
subjects = options.subjects;
extendmethod = options.extendmethod;
nSubjects = numel(subjects);
if isfield(options,'anatomymaskdir')
anatomymask = options.anatomymaskdir;
else
anatomymask = 0;
end
% which ROIs do you want to extract?
roiIdentifier = options.roiname;
% define the spm t-maps related to the ROIs you want to extract
tIdentifier = options.tmap;
if anatomymask~=0
hemiIdentifiers = {...
'lh'...
'rh'...
'bilateral'...
};
else
hemiIdentifiers = {...
'lh'...
'rh'...
};
end
%% define the number of voxels (I start with 20 voxels and grow ROIs until I reach 1000 voxels. Modify this according to your taste and your ROIs.
nHemi = length(hemiIdentifiers);
nVoxels=options.nvoxels;
% flags for reslicing the anatomical
flags.mean=0;
flags.which=1;
flags.wrap=[1 1 0];
options.lessIsMore=0;
options.showNeg=0;
options.threshold=0;
options.transparency=0.3;
options.title='';
%% loop over subjects, hemispheres, and ROIs
for subI = 1:nSubjects
% define thisSubject
thisSubject.name = subjects{subI};
thisSubject.root = fullfile(mridataPath,thisSubject.name);
thisSubject.masks = fullfile(thisSubject.root,'masks',roiIdentifier);
thisSubject.stats = fullfile(thisSubject.root,options.statsdir);
thisSubject.roi = roiIdentifier;
% this will create the directory if not existent
gotoDir(thisSubject.masks)
% get headers of a stats volume (this is used to reslice anat to dims
% of stats map
V = spm_vol(fullfile(thisSubject.stats,'spmT_0001.img'));
% find the fname
thisSubject.tmap=V.fname;
% define the subject's structural
thisSubject.structural = get_files(fullfile(thisSubject.root,options.anatomydir),'s*.nii');
thisSubjectWitness = fullfile(thisSubject.masks,['right' roiIdentifier '_allpages.ps']);
if (~exist(thisSubjectWitness,'file'))
fprintf('***\t now working on %s for %s \t***\n',thisSubject.name,thisSubject.roi)
% reslice the anat to fit the T-map
P=char(thisSubject.tmap,thisSubject.structural);
spm_reslice(P,flags)
% now we have a structural image that fits the t-maps (64x64x32)
thisSubject.reslicedStructural = get_files(fullfile(thisSubject.root,'structurals'),'rs*.nii');
thisSubject.anatMap = spm_read_vols(spm_vol(thisSubject.reslicedStructural));
thisSubject.extendmethod = extendmethod;
% perform the roi extraction
brainVol = cell(1,length(hemiIdentifiers));
for hemiI = 1:nHemi
if anatomymask~=0
thisSubject.anatomicalMask = fullfile(thisSubject.root,anatomymask,[hemiIdentifiers{hemiI} '.' roiIdentifier '.nii']);
end
thisSubject.hemisphere = hemiIdentifiers{hemiI};
thisSubject.tMap = tIdentifier;
roiExtraction(thisSubject,nVoxels);
end % hemiI
else
fprintf('***\t work already done for %s for %s \t***\n',thisSubject.name,thisSubject.roi)
end
end% subI
return
% local FUNCTION: brainVol = roiExtraction(userOptions,nVoxels)
% This is the core function of the toolbox.
% % USAGE
% brainVol = roiExtraction(userOptions,nVoxels)
%
% FUNCTION
% this function will write down a given number of binary
% maps (defined by nVoxels) to the location of your choice
% as defined by userOptions.masks
% It works with some automatised functionality as well as visual
% inspection of the t-maps that define your ROIs.
% It allows navigation using the spm visualisation tools on the
% subject's native space anatomical map.
%
%
% ARGUMENTS
% userOptions.root the root folder location for that given subject
% (absoulte path passed as a string)
%
% userOptions.masks the absolute folder location where you want the
% masks for this subject to be written
%
% userOptions.stats the absolution path and filename of the t-map
% which you want to display on the subject's anatomical map
% for the roi extraction
%
% userOptions.structural the absolution path and filename of the native
% space anatomical map
%
% userOptions.roi for each ROI that you want to define, this program
% first tells you what to look for (i.e. which
% functional ROI)
%
% userOptions.hemisphere the program also specifies which hemisphere it is
% currently working on
%
% nVoxels a vector of scalars defining the number of voxels
% you want to include in the resulting binary mask.
%
% Written by Ian Charest - MRC-CBSU - Updated - May 2012
%
% Thanks to the developers of SPM, a neuroimaging software that is needed
% for this tool to work. Some of the code was also inspired from the SPM
% code.
% Please visit
% www.fil.ion.ucl.ac.uk/spm/software/spm8/
%
% Also thanks to Xu Cui for developing xjview which inspired some of this
% code
%
% Please visit
% www.alivelearn.net/xjview8/
function outputmasks = roiExtraction(userOptions,nVoxels)
spmdir = spm('dir');
try
spm('defaults', 'fmri');
catch
[];
end
thisSubjectMaskLocation = userOptions.masks;
gotoDir(thisSubjectMaskLocation)
anatMap = userOptions.anatMap;
thisLocStats = userOptions.stats;
thisStruct = userOptions.structural;
thisHemisphere = userOptions.hemisphere;
thisRoi = userOptions.roi;
structimg = thisStruct;
extendmethod = userOptions.extendmethod;
if isfield(userOptions,'anatomicalMask')
anatomymask = userOptions.anatomicalMask;
else
anatomymask = 0;
end
if strcmp(extendmethod,'contiguous');
contiguous=1;
else
contiguous=0;
end
cd(thisLocStats)
message = {['navigate to ' thisHemisphere,' ',thisRoi];'you can use spm right-click controls in glass brain';'press pause when you are on the spot to start growing rois'};
uiwait(warndlg(message,'Instructions'));
[Finter,Fgraph,CmdLine] = spm('FnUIsetup','Stats: Results');
xSPM = getxSPM(userOptions);
[SPM,xSPM]=spm_getSPM(xSPM);
M = SPM.xVol.M;
DIM = SPM.xVol.DIM;
options.figI = Finter;
%-Space units
%----------------------------------------------------------------------
try
try
units = SPM.xVol.units;
catch
units = xSPM.units;
end
catch
try
if strcmp(spm('CheckModality'),'EEG')
datatype = {...
'Volumetric (2D/3D)',...
'Scalp-Time',...
'Scalp-Frequency',...
'Time-Frequency',...
'Frequency-Frequency'};
selected = spm_input('Data Type: ','+1','m',datatype);
datatype = datatype{selected};
else
datatype = 'Volumetric (2D/3D)';
end
catch
datatype = 'Volumetric (2D/3D)';
end
switch datatype
case 'Volumetric (2D/3D)'
units = {'mm' 'mm' 'mm'};
case 'Scalp-Time'
units = {'mm' 'mm' 'ms'};
case 'Scalp-Frequency'
units = {'mm' 'mm' 'Hz'};
case 'Time-Frequency'
units = {'Hz' 'ms' ''};
case 'Frequency-Frequency'
units = {'Hz' 'Hz' ''};
otherwise
error('Unknown data type.');
end
end
if DIM(3) == 1, units{3} = ''; end
xSPM.units = units;
SPM.xVol.units = units;
spm_clf(Finter);
spm('FigName',['SPM{',xSPM.STAT,'}: Results'],Finter,CmdLine);
hReg = spm_results_ui('SetupGUI',M,DIM,xSPM,Finter);
hMIPax = axes('Parent',Fgraph,'Position',[0.05 0.60 0.55 0.36],'Visible','off');
hMIPax = spm_mip_ui(xSPM.Z,xSPM.XYZmm,M,DIM,hMIPax,units);
spm_XYZreg('XReg',hReg,hMIPax,'spm_mip_ui')
hold on
spm_sections_noClear(xSPM,hReg,structimg)
% this will wait for space bar input
pause
% get the xyz vox location
xyz_vox = mm_to_vox(hReg,xSPM);
if userOptions.tMap<10
spmT_map=fullfile(thisLocStats,['spmT_000' num2str(userOptions.tMap)]);
elseif userOptions.tMap<100
spmT_map=fullfile(thisLocStats,['spmT_00' num2str(userOptions.tMap)]);
end
roiName = [thisHemisphere,thisRoi];
%% flow chart
filename = fullfile([thisSubjectMaskLocation,filesep,roiName,'_allpages']);
V = spm_vol([spmT_map,'.img']);
Y = spm_read_vols(V);
if strcmp(thisHemisphere,'bilateral')
nVoxels = [nVoxels nVoxels(end)+nVoxels(2) - nVoxels(1):nVoxels(2)-nVoxels(1):nVoxels(end)*2];
end
nRoisToCreate = numel(nVoxels);
spm_progress_bar('Init',nRoisToCreate,'Expanding Rois','Rois Complete');
for roiI = 1:nRoisToCreate
% call this for contiguous voxels:
if anatomymask~=0
maskV = spm_vol(anatomymask);
maskData = spm_read_vols(maskV);
if contiguous==1
newRoi = resizeRoi(single(xyz_vox)',Y, nVoxels(roiI),maskData);
else
% call this for discontiguous voxels:
newRoi = discontiguousRoi(Y,nVoxels(roiI),maskData);
end
else
if contiguous==1
newRoi = resizeRoi(single(xyz_vox)',Y, nVoxels(roiI));
else
% call this for discontiguous voxels:
newRoi = discontiguousRoi(Y,nVoxels(roiI));
end
end
newRoimask = roi2mask(newRoi,size(Y));
% define options
options.title = [roiName,'_',num2str(nVoxels(roiI)),'_voxels'];
options.nVoxels = nVoxels(roiI);
Fgraph = spm_figure('GetWin','Graphics');
options.figI = Fgraph;
showRoiOnAnatomy(newRoimask.*Y,map2vol(anatMap),options)
spm_print(filename)
% update metaData structure
newRoiMapMetadataStruct = V;
newRoiMapMetadataStruct.fname = fullfile([thisSubjectMaskLocation,filesep,roiName,'_',num2str(nVoxels(roiI)),'.img']);
newRoiMapMetadataStruct.descrip = ['binary ROI mask for ' roiName];
newRoiMapMetadataStruct.dim = size(newRoimask);
spm_write_vol(newRoiMapMetadataStruct, newRoimask);
spm_progress_bar('Set',roiI); % update the progress bar
end
return
% local FUNCTION: getxSPM(userOptions)
% Thanks to Alex Woolgar for providing this very useful function.
function xSPM = getxSPM(userOptions)
% set up xSPM for visual localisation of various visual regions in SPM (to
% save on GUI interaction)
% Alex Woolgar 22 Feb 2012
%
% Ian Charest Modified it to use the last digit of the passed t-map
% identifier
xSPM.swd = userOptions.stats; % dir with SPM.mat
xSPM.title = [userOptions.hemisphere ' ' userOptions.roi];
xSPM.Ic = userOptions.tMap;
xSPM.n = 1;
xSPM.Im = []; %no masking contrast
xSPM.pm = [];
xSPM.Ex = [];
xSPM.u = 1.69; %approx p < 0.01 (uncor)
xSPM.k = 0;
xSPM.thresDesc = 'none';
return
% local FUNCTION: gotoDir(varargin)
% Thanks to Nikolaus Kriegeskorte and Cai Wingfield for providing this very useful function.
function gotoDir(varargin)
% gotoDir(path, dir)
% Goes to path; then goes to dir, making it if necessary.
%
% gotoDir(path)
% Goes to path, making all required directories on the way.
switch nargin
case 2
path = varargin{1};
dir = varargin{2};
try
cd(path)
catch
error('gotoDir:NonexistantPath', ['The path "' path '" does not refer!']);
end%try
try
cd(dir);
catch
fprintf(['The directory "' dir '" doesn''t exist at "' path '"; making it.\n']);
mkdir(dir);
cd(dir);
end%try
case 1
path = varargin{1};
sIndices = strfind(path, filesep);
for i = 1:numel(sIndices)
if i == 1 && sIndices(i) == 1
continue;
end%if
try
cd(path(1:sIndices(i)-1));
catch
fprintf(['The directory "' path(1:sIndices(i)-1) '" doesn''t exist... making it.\n']);
mkdir(path(1:sIndices(i)-1));
cd(path(1:sIndices(i)-1));
end%try
end%for:i
% cleanup final directory!
try
cd(path);
catch
fprintf(['The directory "' path '" doesn''t exist... making it.\n']);
mkdir(path);
end%try
cd(path);
otherwise
error('gotoDir:BadNargin', 'Only 1 or 2 arguments allowed.');
end%switch:nargin
return
% local FUNCTION: files = get_files(direc, filt)
% Thanks to Russell Thompson's get_files function from which this is a
% modified version. modified by Ian Charest
function files = get_files(direc, filt)
% =========================================================================
% return a list of files
% filt = filter string
% direc = cell array of directory names
% revised 07-2011 Ian Charest
if nargin~=2, error('get_files:missing inputs, Please input folder(s) and file filter.'); end%if
files = [];
if ischar(direc) % if direc is already a character array
currDir = direc;
tmp = dir(fullfile(currDir,filt)); % find all files matching f*.nii
tmp = [repmat([currDir filesep],size(tmp,1),1) char(tmp.name)]; % build the full path name for these files
files = char(files,tmp);
else % if direc is a cell array
if size(direc,1)>size(direc,2)
nRuns=size(direc,1);
else
nRuns=size(direc,2);
end
for runI=1:nRuns % loop through each EPI session
currDir = char(direc{runI});
tmp = dir(fullfile(currDir,filt)); % find all files matching f*.nii
tmp = [repmat([currDir filesep],size(tmp,1),1) char(tmp.name)]; % build the full path name for these files
files = char(files,tmp);
end
end
files = files(~all(files'==' ')',:);
return
% local FUNCTION: spm_sections_noClear(xSPM,hReg,img)
% Thanks to John Ashburner and all the genius people at the FIL.
function spm_sections_noClear(xSPM,hReg,img)
%
% modified version of spm_sections that inhibs the clearing of the Fgraph.
% Rendering of regional effects [SPM{.}] on orthogonal sections
% FORMAT spm_sections(xSPM,hReg,img)
%
% xSPM - structure containing details of excursion set (see spm_getSPM)
% hReg - handle of MIP register
% img - filename of background image
%__________________________________________________________________________
%
% spm_sections is called by spm_results_ui and uses variable img to
% create three orthogonal sections through a background image.
% Regional foci from the selected xSPM are rendered on this image.
%__________________________________________________________________________
% Copyright (C) 1994-2011 Wellcome Trust Centre for Neuroimaging
% John Ashburner
% $Id: spm_sections.m 4199 2011-02-10 20:07:17Z guillaume $
%
% modified by
% Ian Charest - MRC-CBSU - July 2011 -
% so that the glass brain and anatomical
% is not cleared with the call to spm_results_ui('Clear',Fgraph)
if ~nargin, [SPM,xSPM] = spm_getSPM; end
if nargin < 2, hReg = []; end
if nargin < 3 || isempty(img)
[img, sts] = spm_select(1,'image','Select image for rendering on');
if ~sts, return; end
end
Fgraph = spm_figure('GetWin','Graphics');
%spm_results_ui('Clear',Fgraph);
spm_orthviews('Reset');
global st prevsect
st.Space = spm_matrix([0 0 0 0 0 -pi/2]) * st.Space;
prevsect = img;
h = spm_orthviews('Image', img, [0.05 0.05 0.9 0.45]);
spm_orthviews('AddContext', h);
spm_orthviews('MaxBB');
if ~isempty(hReg), spm_orthviews('Register', hReg); end
spm_orthviews('AddBlobs', h, xSPM.XYZ, xSPM.Z, xSPM.M);
spm_orthviews('Redraw');
return
% local FUNCTION: newRoi=resizeRoi(roi, map, nVox, mask)
% Thanks to Nikolaus Kriegeskorte for providing this very useful function.
function newRoi=resizeRoi(roi, map, nVox, mask)
% USAGE
% newRoi=resizeRoi(roi, map, nVox[, mask])
%
% FUNCTION
% redefine a roi to make it conform to the size
% given by nVox. the new roi is defined by a region
% growing process, which (1) is seeded at the voxel
% that has the maximal statistical parameter within
% the passed statistical map and (2) is prioritized
% by the map's values.
%
% ARGUMENTS
% roi (r)egion (o)f (i)nterest
% a matrix of voxel positions
% each row contains ONE-BASED coordinates (x, y, z) of a voxel.
%
% map a 3D statistical-parameter map
% the map must match the volume, relative to which
% the roi-voxel coords are specified in roi.
%
% nVox number of voxels the resized roi is to have
%
% [mask] optional binary mask. if present, the region growing is
% restricted to the nonzero entries of it.
% PARAMETERS
%maxNlayers=2; %defines how many complete layers are maximally added
if ~exist('mask','var') || (exist('mask','var') && isempty(mask))
mask=ones(size(map));
end
map(~mask)=min(map(:));
% DEFINE THE VOLUME
vol=zeros(size(map));
% FIND THE SEED (A MAXIMAL MAP VALUE IN ROI&mask)
mapINDs=sub2ind(size(vol),roi(:,1),roi(:,2),roi(:,3)); %single indices to MAP specifying voxels in the roi
roimap=map(mapINDs); %column vector of statistical-map subset for the roi
[roimax,roimax_roimapIND]=max(roimap); %the maximal statistical map value in the roi and its index within roimap
seed_mapIND=mapINDs(roimax_roimapIND); %seed index within map
newRoi=[];
if nVox==0; return; end;
if mask(seed_mapIND)
vol(seed_mapIND)=1;
[x,y,z]=ind2sub(size(vol),seed_mapIND);
newRoi=[newRoi;[x,y,z]];
end
if nVox==1 || isempty(newRoi)
return;
end
% GROW THE REGION
for i=2:nVox
% DEFINE THE FRINGE
cFringe=vol;
[ivolx,ivoly,ivolz]=ind2sub(size(vol),find(vol));
superset=[ivolx-1,ivoly,ivolz;
ivolx+1,ivoly,ivolz;
ivolx,ivoly-1,ivolz;
ivolx,ivoly+1,ivolz;
ivolx,ivoly,ivolz-1;
ivolx,ivoly,ivolz+1];
% exclude out-of-volume voxels
outgrowths = superset(:,1)<1 | superset(:,2)<1 | superset(:,3)<1 | ...
superset(:,1)>size(vol,1) | superset(:,2)>size(vol,2) | superset(:,3)>size(vol,3);
superset(find(outgrowths),:)=[];
% draw the layer (excluding multiply defined voxels)
cFringe(sub2ind(size(vol),superset(:,1),superset(:,2),superset(:,3)))=1;
cFringe=cFringe&mask;
cFringe=cFringe-vol;
if size(find(cFringe),1)==0
break; % exit the loop (possible cause of empty fringe: the whole volume is full)
end
% FIND A MAXIMAL-MAP-VALUE FRINGE VOXEL...
mapINDs=find(cFringe); %single indices to MAP specifying voxels in the fringe
fringemap=map(mapINDs); %column vector of statistical-map subset for the fringe
[fringemax,fringemax_fringemapIND]=max(fringemap); %the maximal statistical map value in the roi and its index within roimap
fringemax_mapIND=mapINDs(fringemax_fringemapIND); %seed index within map
% ...INCLUDE IT
vol(fringemax_mapIND)=1;
[x,y,z]=ind2sub(size(vol),fringemax_mapIND);
newRoi=[newRoi;[x,y,z]];
end
return
% local FUNCTION: newRoi=discontiguousRoi(prioritizationMap,nVox,mask)
% Thanks to Nikolaus Kriegeskorte for providing this very useful function.
function roi=discontiguousRoi(prioritizationMap,nVox,mask)
mask(mask~=0)=1; % set all nonzero entries to one (mask with continuous values are correctly handled (only 0 counts as vacant)
if nVox>sum(mask(:))
disp('Function discontiguousRoi, WARNING : the mask passed does not allow an ROI of the requested size. All mask voxels are returned as the ROI.')
roi=mask2roi(mask);
else % mask has at least nVox voxels
prioritizationMap(~mask)=min(prioritizationMap(:)); % sets voxels not covered by mask to zero.
% retrieve the threshold value for the nVox maximum entries inside mask.
threshold=autothresholdMap(prioritizationMap,nVox);
roiMask=prioritizationMap>=threshold & mask;
roi=mask2roi(roiMask);
end
return
% local FUNCTION: mask=roi2mask(roi,volSize_vox)
% Thanks to Nikolaus Kriegeskorte for providing this very useful function.
% roi2mask is a function with two arguments:
% ARGUMENTS:
% roi: this is a 3xn matrix where each row contains the coordinates for
% a point inside the roi
% volSize_vox: this is a 1x3 vector containing the dimensions of the
% scanned volume. E.g., [32 64 64]
% RETURNS:
% mask: a volume of size volSize_vox which is all 0s, except for the
% points indicated by roi, which are 1s.
function mask=roi2mask(roi,volSize_vox)
roi_INDs=sub2ind(volSize_vox,roi(:,1),roi(:,2),roi(:,3)); %single indices to MAP specifying voxels in the roi
mask=false(volSize_vox);
mask(roi_INDs)=true;
return
% local FUNCTION: mask=mask2roi(mask)
% Thanks to Nikolaus Kriegeskorte for providing this very useful function.
function roi=mask2roi(mask)
[x,y,z]=ind2sub(size(mask),find(mask));
roi=[x,y,z];
return
% local FUNCTION: xyz_vox = mm_to_vox(hReg,xSPM)
% Thanks to Johan Carlin for providing this very useful function.
% Returns the voxel coordinates for the current position in the
% SPM results viewer.
% J Carlin 10/6/2011
%function xyz_vox = mm_to_vox(hReg,xSPM)
function xyz_vox = mm_to_vox(hReg,xSPM)
%hReg = findobj('Tag','hReg'); % get results figure handle
xyz_mm = spm_XYZreg('GetCoords',hReg); % get mm coordinates for current locationassignin('base','xyz_mm',xyz_mm); % puts xyz_mm in base workspace
xyz_vox = xSPM.iM*[xyz_mm;1]; %evalin('base','xSPM.iM*[xyz_mm;1]'); % avoid passing xSPM explicitly
xyz_vox = xyz_vox(1:3);
return
% local FUNCTION: vol=map2vol(map)
% Thanks to Nikolaus Kriegeskorte for providing this very useful function.
function vol=map2vol(map)
if strcmp(class(map),'single') || strcmp(class(map),'double')
% scale into RGB range [0,1] for display
map=map-min(map(:)); % move min to zero
map=map/max(map(:)); % scale max to one
vol=double(permute(repmat(map,[1 1 1 3]),[1 2 4 3]));
else
% make vol same class as map (e.g. logical)
vol=permute(repmat(map,[1 1 1 3]),[1 2 4 3]);
end
return
% local FUNCTION: showRoiOnAnatomy(roiMap,anatVol,options)
% Inspired from Nikolaus Kriegeskorte's function showMapOnAnatomy,
% thanks for providing this very useful function.