-
Notifications
You must be signed in to change notification settings - Fork 2
/
TMFC.m
3098 lines (2699 loc) · 133 KB
/
TMFC.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 TMFC
% ========= Task-Modulated Functional Connectivity (TMFC) toolbox =========
%
% Opens the main GUI window.
%
% The tmfc structure contains the following fields:
%
% tmfc.defaults.parallel - 0 or 1 (sequential or parallel computing)
% tmfc.defaults.maxmem - e.g. 2^32 = 4GB (how much RAM can be used at
% the same time during GLM estimation)
% tmfc.defaults.resmem - true or false (store temporaty files during
% GLM estimation in RAM or on disk)
% tmfc.defaults.analysis - 1 (Seed-to-voxel and ROI-to-ROI analyses)
% - 2 (ROI-to-ROI analysis only)
% - 3 (Seed-to-voxel analysis only)
%
% tmfc.project_path - The path where all results will be saved
%
% tmfc.subjects.path - Paths to individual subject SPM.mat files
% tmfc.subjects.FIR - 1 or 0 (completed or not)
% tmfc.subjects.LSS - 1 or 0 (completed or not)
% tmfc.subjects.LSS_after_FIR - 1 or 0 (completed or not)
%
% tmfc.ROI_set: - information about the selected ROI set
% and completed TMFC procedures
%
% tmfc.FIR.window - FIR window length [seconds]
% tmfc.FIR.bins - Number of FIR time bins
%
% tmfc.LSS.conditions - Conditions of interest for LSS
% regression without FIR regression
% (based on original time series)
% tmfc.LSS_after_FIR.conditions - Conditions of interest for LSS
% regression after FIR regression
% (based on residual time series)
%
% tmfc.ROI_set(i).gPPI.conditions - Conditions of interest for gPPI and
% gPPI-FIR regression
%
% =========================================================================
%
% Copyright (C) 2024 Ruslan Masharipov
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <https://www.gnu.org/licenses/>.
%
% Contact email: [email protected]
%% ==================[ Set up GUI and TMFC structure ]=====================
if isempty(findobj('Tag', 'TMFC_GUI')) == 1
% Set up TMFC structure
tmfc.defaults.parallel = 0;
tmfc.defaults.maxmem = 2^32;
tmfc.defaults.resmem = true;
tmfc.defaults.analysis = 1;
% Main TMFC GUI
main_GUI.TMFC_GUI = figure('Name','TMFC Toolbox','MenuBar', 'none', 'ToolBar', 'none','NumberTitle', 'off', 'Units', 'norm', 'Position', [0.63 0.0875 0.250 0.850], 'color', 'w', 'Tag', 'TMFC_GUI');
% Box Panels
main_GUI.MP1 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.03 0.85 0.94 0.13],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.MP2 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.03 0.65 0.94 0.19],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.MP3 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.03 0.511 0.94 0.13],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.MP4 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.03 0.37 0.94 0.13],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.MP5 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.03 0.23 0.94 0.13],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.MP6 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.03 0.01 0.94 0.13],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.SP1 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.54 0.922 0.40 0.0473],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.SP2 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.54 0.863 0.40 0.0473],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.SP3 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.54 0.782 0.40 0.0473],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.SP4 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.54 0.722 0.40 0.0473],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.SP6 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.54 0.582 0.40 0.0473],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.SP8 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.54 0.442 0.40 0.0473],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
main_GUI.SP9 = uipanel(main_GUI.TMFC_GUI,'Units', 'normalized','Position',[0.54 0.302 0.40 0.0473],'HighLightColor',[0.78 0.78 0.78],'BackgroundColor','w','BorderType', 'line');
% Buttons
main_GUI.TMFC_GUI_B1 = uicontrol('Style', 'pushbutton', 'String', 'Subjects', 'Units', 'normalized', 'Position', [0.06 0.92 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B2 = uicontrol('Style', 'pushbutton', 'String', 'ROI set', 'Units', 'normalized', 'Position', [0.06 0.86 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B3 = uicontrol('Style', 'pushbutton', 'String', 'VOIs', 'Units', 'normalized', 'Position', [0.06 0.78 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B4 = uicontrol('Style', 'pushbutton', 'String', 'PPIs', 'Units', 'normalized', 'Position', [0.06 0.72 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B5a = uicontrol('Style', 'pushbutton', 'String', 'gPPI', 'Units', 'normalized', 'Position', [0.06 0.66 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B5b = uicontrol('Style', 'pushbutton', 'String', 'gPPI FIR', 'Units', 'normalized', 'Position', [0.54 0.66 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B6 = uicontrol('Style', 'pushbutton', 'String', 'LSS GLM', 'Units', 'normalized', 'Position', [0.06 0.58 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B7 = uicontrol('Style', 'pushbutton', 'String', 'BSC LSS', 'Units', 'normalized', 'Position', [0.06 0.52 0.884 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B8 = uicontrol('Style', 'pushbutton', 'String', 'FIR task regression', 'Units', 'normalized', 'Position', [0.06 0.44 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B9 = uicontrol('Style', 'pushbutton', 'String', 'Background connectivity', 'Units', 'normalized', 'Position', [0.06 0.38 0.884 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B10 = uicontrol('Style', 'pushbutton', 'String', 'LSS GLM after FIR', 'Units', 'normalized', 'Position', [0.06 0.30 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B11 = uicontrol('Style', 'pushbutton', 'String', 'BSC LSS after FIR', 'Units', 'normalized', 'Position', [0.06 0.24 0.884 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B12a = uicontrol('Style', 'pushbutton', 'String', 'Statistics', 'Units', 'normalized', 'Position', [0.06 0.16 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B12b = uicontrol('Style', 'pushbutton', 'String', 'Results', 'Units', 'normalized', 'Position', [0.54 0.16 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B13a = uicontrol('Style', 'pushbutton', 'String', 'Open project', 'Units', 'normalized', 'Position', [0.06 0.08 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B13b = uicontrol('Style', 'pushbutton', 'String', 'Save project', 'Units', 'normalized', 'Position', [0.54 0.08 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B14a = uicontrol('Style', 'pushbutton', 'String', 'Change paths', 'Units', 'normalized', 'Position', [0.06 0.02 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
main_GUI.TMFC_GUI_B14b = uicontrol('Style', 'pushbutton', 'String', 'Settings', 'Units', 'normalized', 'Position', [0.54 0.02 0.40 0.05],'FontUnits','normalized','FontSize',0.33);
% String display
main_GUI.TMFC_GUI_S1 = uicontrol('Style', 'text', 'String', 'Not selected', 'ForegroundColor', [1, 0, 0], 'Units', 'norm', 'Position',[0.555 0.926 0.38 0.03],'FontUnits','normalized','FontSize',0.56,'backgroundcolor', 'w');
main_GUI.TMFC_GUI_S2 = uicontrol('Style', 'text', 'String', 'Not selected', 'ForegroundColor', [1, 0, 0], 'Units', 'norm', 'Position',[0.555 0.867 0.38 0.03],'FontUnits','normalized','FontSize',0.56,'backgroundcolor', 'w');
main_GUI.TMFC_GUI_S3 = uicontrol('Style', 'text', 'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067], 'Units', 'norm', 'Position',[0.555 0.787 0.38 0.03],'FontUnits','normalized','FontSize',0.56,'backgroundcolor', 'w');
main_GUI.TMFC_GUI_S4 = uicontrol('Style', 'text', 'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067], 'Units', 'norm', 'Position',[0.555 0.727 0.38 0.03],'FontUnits','normalized','FontSize',0.56,'backgroundcolor', 'w');
main_GUI.TMFC_GUI_S6 = uicontrol('Style', 'text', 'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067], 'Units', 'norm', 'Position',[0.555 0.587 0.38 0.03],'FontUnits','normalized','FontSize',0.56,'backgroundcolor', 'w');
main_GUI.TMFC_GUI_S8 = uicontrol('Style', 'text', 'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067], 'Units', 'norm', 'Position',[0.555 0.447 0.38 0.03],'FontUnits','normalized','FontSize',0.56,'backgroundcolor', 'w');
main_GUI.TMFC_GUI_S10 = uicontrol('Style', 'text', 'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067], 'Units', 'norm', 'Position',[0.555 0.307 0.38 0.03],'FontUnits','normalized','FontSize',0.56,'backgroundcolor', 'w');
% CallBack functions corresponding to each button
set(main_GUI.TMFC_GUI, 'CloseRequestFcn', {@close_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B1, 'callback', {@select_subjects_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B2, 'callback', {@ROI_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B3, 'callback', {@VOI_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B4, 'callback', {@PPI_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B5a, 'callback', {@gPPI_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B5b, 'callback', {@gPPI_FIR_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B6, 'callback', {@LSS_GLM_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B7, 'callback', {@BSC_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B8, 'callback', {@FIR_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B9, 'callback', {@BGFC_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B10, 'callback', {@LSS_FIR_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B11, 'callback', {@BSC_after_FIR_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B12a, 'callback', {@statistics_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B12b, 'callback', {@results_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B13a, 'callback', {@load_project_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B13b, 'callback', {@save_project_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B14a, 'callback', {@change_paths_GUI, main_GUI.TMFC_GUI});
set(main_GUI.TMFC_GUI_B14b, 'callback', {@settings_GUI, main_GUI.TMFC_GUI});
warning('off','backtrace');
else
figure(findobj('Tag', 'TMFC_GUI'));
warning('TMFC toolbox is already running.');
return;
end
%% ========================[ Select subjects ]=============================
function select_subjects_GUI(~,~,~)
% Freeze main TMFC GUI
freeze_GUI(1);
% Select subjects and check SPM.mat files
subject_paths = tmfc_select_subjects_GUI(1);
% If subjects are not selected - unfreeze
if isempty(subject_paths)
freeze_GUI(0);
disp('TMFC: Subjects not selected.');
else
% Clear TMFC structure and resfresh main TMFC GUI
tmfc = major_reset(tmfc);
% Add subject paths to TMFC structure
for iSub = 1:size(subject_paths,1)
tmfc.subjects(iSub).path = char(subject_paths(iSub));
end
% Select TMFC project folder
disp('TMFC: Please select a folder for the new TMFC project.');
tmfc_select_project_path(size(subject_paths,1)); % Dialog window
project_path = spm_select(1,'dir','Select a folder for the new TMFC project',{},pwd);
% Check if project folder is selected
if strcmp(project_path, '')
warning('TMFC: Project folder not selected. Subjects not saved.');
freeze_GUI(0);
return;
else
% Add project path to TMFC structure
fprintf('TMFC: %d subject(s) selected.\n', size(subject_paths,1));
set(main_GUI.TMFC_GUI_S1,'String', strcat(num2str(size(subject_paths,1)),' selected'),'ForegroundColor',[0.219, 0.341, 0.137]);
tmfc.project_path = project_path;
freeze_GUI(0);
cd(tmfc.project_path);
end
end
end
%% ============================[ ROI set ]=================================
% Select ROIs and apply group-mean binary mask to them
function ROI_GUI(~,~,~)
% Initial checks
if ~isfield(tmfc,'subjects')
warning('Select subjects.'); return;
elseif strcmp(tmfc.subjects(1).path, '')
warning('Select subjects.'); return;
elseif ~exist(tmfc.subjects(1).path,'file')
error('SPM.mat file for the first subject does not exist.')
elseif ~isfield(tmfc,'project_path')
error('Select TMFC project folder.');
end
% Freeze main TMFC GUI
cd(tmfc.project_path);
freeze_GUI(1);
% Check if ROI sets already exist
if ~isfield(tmfc,'ROI_set')
% Select ROIs
ROI_set = tmfc_select_ROIs_GUI(tmfc);
% Add ROI set info to TMFC structure & update main TMFC GUI
if isstruct(ROI_set)
tmfc.ROI_set_number = 1;
tmfc.ROI_set(1) = ROI_set;
set(main_GUI.TMFC_GUI_S2,'String', horzcat(tmfc.ROI_set(1).set_name, ' (',num2str(length(tmfc.ROI_set(1).ROIs)),' ROIs)'),'ForegroundColor',[0.219, 0.341, 0.137]);
tmfc = ROI_set_initializer(tmfc);
end
else
% List of previously defined ROI sets
ROI_set_list = {};
for iSet = 1:length(tmfc.ROI_set)
ROI_set_tmp = {iSet,horzcat(tmfc.ROI_set(iSet).set_name, ' (',num2str(length(tmfc.ROI_set(iSet).ROIs)),' ROIs)')};
ROI_set_list = vertcat(ROI_set_list, ROI_set_tmp);
end
% Switch between previously defined ROI sets or add a new ROI set
[ROI_set_check, ROI_set_number] = ROI_set_switcher(ROI_set_list);
nSet = size(ROI_set_list,1);
% Add new ROI set
if ROI_set_check == 1
% Select new ROIs
new_ROI_set = tmfc_select_ROIs_GUI(tmfc);
% Add a new ROI set info to TMFC structure & update main TMFC GUI
if isstruct(new_ROI_set)
tmfc.ROI_set(nSet+1).set_name = new_ROI_set.set_name;
tmfc.ROI_set(nSet+1).ROIs = new_ROI_set.ROIs;
tmfc.ROI_set_number = nSet+1;
disp('New ROI set selected.');
set(main_GUI.TMFC_GUI_S2,'String', horzcat(tmfc.ROI_set(nSet+1).set_name, ' (',num2str(length(tmfc.ROI_set(nSet+1).ROIs)),' ROIs)'),'ForegroundColor',[0.219, 0.341, 0.137]);
tmfc = ROI_set_initializer(tmfc);
set(main_GUI.TMFC_GUI_S3,'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067]);
set(main_GUI.TMFC_GUI_S4,'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067]);
end
% Switch to selected ROI set
elseif ROI_set_check == 0 && ROI_set_number > 0
fprintf('\nSelected ROI set: %s. \n', char(ROI_set_list(ROI_set_number,2)));
tmfc.ROI_set_number = ROI_set_number;
set(main_GUI.TMFC_GUI_S2,'String', horzcat(tmfc.ROI_set(ROI_set_number).set_name, ' (',num2str(length(tmfc.ROI_set(ROI_set_number).ROIs)),' ROIs)'),'ForegroundColor',[0.219, 0.341, 0.137]);
tmfc = update_gPPI(tmfc);
% If user cancels operation
else
disp('No new ROI set selected.');
end
end
% Unfreeze main TMFC GUI
freeze_GUI(0);
end
%% =============================[ VOIs ]===================================
% Perform volume of interest (VOI) computation for selected ROI set
function VOI_GUI(~,~,~)
% Initial checks
if ~isfield(tmfc,'subjects')
warning('Select subjects.'); return;
elseif strcmp(tmfc.subjects(1).path, '')
warning('Select subjects.'); return;
elseif ~exist(tmfc.subjects(1).path,'file')
error('SPM.mat file for the first subject does not exist.')
elseif ~isfield(tmfc,'project_path')
error('Select TMFC project folder.');
elseif ~isfield(tmfc,'ROI_set')
warning('Select ROIs.'); return;
end
ROI_check = check_ROI_masks(tmfc,tmfc.ROI_set_number);
if ROI_check == 0
error('One or more ROI masks are missing. Check the ROI_sets subfolder.')
end
% Freeze main TMFC GUI
cd(tmfc.project_path);
freeze_GUI(1);
nSub = length(tmfc.subjects);
nROI = length(tmfc.ROI_set(tmfc.ROI_set_number).ROIs);
try
cond_list = tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions;
sess = []; sess_num = []; nSess = [];
for iCond = 1:length(cond_list)
sess(iCond) = cond_list(iCond).sess;
end
sess_num = unique(sess);
nSess = length(sess_num);
end
% Update TMFC structure
try
w = waitbar(0,'Please wait...','Name','Updating VOI progress');
for iSub = 1:nSub
check_VOI = zeros(nROI,nSess);
for jROI = 1:nROI
for kSess = 1:nSess
if exist(fullfile(tmfc.project_path,'ROI_sets',tmfc.ROI_set(tmfc.ROI_set_number).set_name,'VOIs',['Subject_' num2str(iSub,'%04.f')], ...
['VOI_' tmfc.ROI_set(tmfc.ROI_set_number).ROIs(jROI).name '_' num2str(kSess) '.mat']), 'file')
check_VOI(jROI,kSess) = 1;
end
end
end
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).VOI = double(~any(check_VOI(:) == 0));
try
waitbar(iSub/nSub,w,['Subject No ' num2str(iSub,'%.f')]);
end
end
end
try
delete(w)
end
% Update main TMFC GUI
track_VOI = 0;
for iSub = 1:nSub
if tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).VOI == 0
track_VOI = iSub;
break;
end
end
if track_VOI == 0
set(main_GUI.TMFC_GUI_S3,'String', strcat(num2str(nSub), '/', num2str(nSub), ' done'),'ForegroundColor',[0.219, 0.341, 0.137]);
elseif track_VOI == 1
set(main_GUI.TMFC_GUI_S3,'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067]);
else
set(main_GUI.TMFC_GUI_S3,'String', strcat(num2str(track_VOI-1), '/', num2str(nSub), ' done'),'ForegroundColor',[0.219, 0.341, 0.137]);
end
restart_VOI = -1;
continue_VOI = -1;
% VOI was not calculated
if ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).VOI] == 1)
start_sub = 1;
define_gPPI_conditions = 1;
% VOI was calculated for all subjects
elseif ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).VOI] == 0)
% Ask user to restart VOI computation
restart_VOI = tmfc_restart_GUI(4);
% Reset VOI, PPI, gPPI and gPPI-FIR progress and delete old files
if restart_VOI == 1
start_sub = 1;
define_gPPI_conditions = 1;
else
disp('VOI computation not initiated.');
freeze_GUI(0);
return;
end
% VOI was calculated for some subjects
else
% Ask user to continue or restart VOI computation
continue_VOI = tmfc_continue_GUI(track_VOI,4);
% Continue VOI computation
if continue_VOI == 1
start_sub = track_VOI;
define_gPPI_conditions = 0;
% Restart VOI computation
elseif continue_VOI == 0
start_sub = 1;
define_gPPI_conditions = 1;
else
disp('VOI computation not initiated.');
freeze_GUI(0);
return;
end
end
% Select gPPI conditions
if define_gPPI_conditions == 1
gPPI_conditions = tmfc_gPPI_GUI(tmfc.subjects(1).path);
if isstruct(gPPI_conditions)
if restart_VOI == 1 || continue_VOI == 0
tmfc = reset_gPPI(tmfc);
end
tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions = gPPI_conditions;
disp('Conditions of interest selected.');
else
disp('Conditions of interest not selected.');
freeze_GUI(0);
return;
end
end
% Compute VOIs
disp('Initiating VOI computation...');
try
sub_check = tmfc_VOI(tmfc,tmfc.ROI_set_number,start_sub);
for iSub = start_sub:nSub
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).VOI = sub_check(iSub);
end
disp('VOI computation completed.');
catch
freeze_GUI(0);
error('Error: Calculate VOIs for all subjects.');
end
% Unfreeze main TMFC GUI
freeze_GUI(0);
end
%% =============================[ PPIs ]===================================
% Perform PPI computation for selected ROI set
function PPI_GUI(~,~,~)
% Initial checks
if ~isfield(tmfc,'subjects')
warning('Select subjects.'); return;
elseif strcmp(tmfc.subjects(1).path, '')
warning('Select subjects.'); return;
elseif ~exist(tmfc.subjects(1).path,'file')
error('SPM.mat file for the first subject does not exist.')
elseif ~isfield(tmfc,'project_path')
error('Select TMFC project folder.');
elseif ~isfield(tmfc,'ROI_set')
warning('Select ROIs.'); return;
elseif any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).VOI] == 0)
warning('Calculate VOIs for all subjects.'); return;
elseif ~isfield(tmfc.ROI_set(tmfc.ROI_set_number),'gPPI')
error('Select conditions of interest.');
elseif ~isfield(tmfc.ROI_set(tmfc.ROI_set_number).gPPI,'conditions')
error('Select conditions of interest. Calculate VOIs for all subjects.');
end
% Freeze main TMFC GUI
cd(tmfc.project_path);
freeze_GUI(1);
nSub = length(tmfc.subjects);
nROI = length(tmfc.ROI_set(tmfc.ROI_set_number).ROIs);
cond_list = tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions;
nCond = length(cond_list);
% Update TMFC structure
w = waitbar(0,'Please wait...','Name','Updating PPI progress');
for iSub = 1:nSub
SPM = load(tmfc.subjects(iSub).path);
check_PPI = zeros(nROI,nCond);
for jROI = 1:nROI
for kCond = 1:nCond
if exist(fullfile(tmfc.project_path,'ROI_sets',tmfc.ROI_set(tmfc.ROI_set_number).set_name,'PPIs',['Subject_' num2str(iSub,'%04.f')], ...
['PPI_[' regexprep(tmfc.ROI_set(tmfc.ROI_set_number).ROIs(jROI).name,' ','_') ']_' cond_list(kCond).file_name '.mat']), 'file')
check_PPI(jROI,kCond) = 1;
end
end
end
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).PPI = double(~any(check_PPI(:) == 0));
clear SPM
try
waitbar(iSub/nSub,w,['Subject No ' num2str(iSub,'%.f')]);
end
end
try
delete(w)
end
% Update main TMFC GUI
track_PPI = 0;
for iSub = 1:nSub
if tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).PPI == 0
track_PPI = iSub;
break;
end
end
if track_PPI == 0
set(main_GUI.TMFC_GUI_S4,'String', strcat(num2str(nSub), '/', num2str(nSub), ' done'),'ForegroundColor',[0.219, 0.341, 0.137]);
elseif track_PPI == 1
set(main_GUI.TMFC_GUI_S4,'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067]);
else
set(main_GUI.TMFC_GUI_S4,'String', strcat(num2str(track_PPI-1), '/', num2str(nSub), ' done'),'ForegroundColor',[0.219, 0.341, 0.137]);
end
% PPI was not calculated
if ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).PPI] == 1)
calculate_PPI = 1;
start_sub = 1;
% PPI was calculated for all subjects
elseif ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).PPI] == 0)
% Dialog window: Help info for PPI recomputation
calculate_PPI = 0;
PPI_recompute();
freeze_GUI(0);
disp('Recompute VOIs to change conditions of interest for gPPI analysis.');
% PPI was calculated for some subjects
else
% Ask user to continue PPI computation
continue_PPI = tmfc_continue_GUI(track_PPI,5);
if continue_PPI == 1
calculate_PPI = 1;
start_sub = track_PPI;
else
disp('PPI computation not initiated.');
freeze_GUI(0);
return;
end
end
% Compute PPIs
if calculate_PPI == 1
disp('Initiating PPI computation...');
try
sub_check = tmfc_PPI(tmfc,tmfc.ROI_set_number,start_sub);
for iSub = start_sub:nSub
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).PPI = sub_check(iSub);
end
disp('PPI computation completed.');
catch
freeze_GUI(0);
error('Error: Calculate PPIs for all subjects.');
end
end
% Unfreeze main TMFC GUI
freeze_GUI(0);
end
%% =============================[ gPPI ]===================================
% Perform gPPI analysis for selected ROI set
function gPPI_GUI(~,~,~)
% Initial checks
if ~isfield(tmfc,'subjects')
warning('Select subjects.'); return;
elseif strcmp(tmfc.subjects(1).path, '')
warning('Select subjects.'); return;
elseif ~exist(tmfc.subjects(1).path,'file')
error('SPM.mat file for the first subject does not exist.')
elseif ~isfield(tmfc,'project_path')
error('Select TMFC project folder.');
elseif ~isfield(tmfc,'ROI_set')
warning('Select ROIs.'); return;
elseif any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).VOI] == 0)
warning('Calculate VOIs for all subjects.'); return;
elseif any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).PPI] == 0)
warning('Calculate PPIs for all subjects.'); return;
elseif ~isfield(tmfc.ROI_set(tmfc.ROI_set_number),'gPPI')
error('Select conditions of interest. Calculate VOIs for all subjects.');
elseif ~isfield(tmfc.ROI_set(tmfc.ROI_set_number).gPPI,'conditions')
error('Select conditions of interest. Calculate VOIs for all subjects.');
end
% Freeze main TMFC GUI
cd(tmfc.project_path);
freeze_GUI(1);
nSub = length(tmfc.subjects);
nROI = length(tmfc.ROI_set(tmfc.ROI_set_number).ROIs);
cond_list = tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions;
nCond = length(cond_list);
% Update TMFC structure
w = waitbar(0,'Please wait...','Name','Updating gPPI progress');
for iSub = 1:nSub
check_gPPI = ones(1,nCond);
for jCond = 1:nCond
% Check ROI-to-ROI files
if tmfc.defaults.analysis == 1 || tmfc.defaults.analysis == 2
if ~exist(fullfile(tmfc.project_path,'ROI_sets',tmfc.ROI_set(tmfc.ROI_set_number).set_name,'gPPI','ROI_to_ROI','symmetrical', ...
['Subject_' num2str(iSub,'%04.f') '_Contrast_' num2str(jCond,'%04.f') '_' cond_list(jCond).file_name '.mat']),'file')
check_gPPI(jCond) = 0;
end
end
% Check seed-to-voxel files
if tmfc.defaults.analysis == 1 || tmfc.defaults.analysis == 3
if ~exist(fullfile(tmfc.project_path,'ROI_sets',tmfc.ROI_set(tmfc.ROI_set_number).set_name,'gPPI','Seed_to_voxel',tmfc.ROI_set(tmfc.ROI_set_number).ROIs(nROI).name, ...
['Subject_' num2str(iSub,'%04.f') '_Contrast_' num2str(jCond,'%04.f') '_' cond_list(jCond).file_name '.nii']),'file')
check_gPPI(jCond) = 0;
end
end
end
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).gPPI = double(~any(check_gPPI(:) == 0));
try
waitbar(iSub/nSub,w,['Subject No ' num2str(iSub,'%.f')]);
end
end
try
delete(w)
end
% Update main TMFC GUI
track_gPPI = 0;
for iSub = 1:nSub
if tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).gPPI == 0
track_gPPI = iSub;
break;
end
end
% gPPI was not calculated
if ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).gPPI] == 1)
calculate_gPPI = 1;
start_sub = 1;
% gPPI was calculated for all subjects
elseif ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).gPPI] == 0)
calculate_gPPI = 0;
fprintf('\ngPPI was calculated for all subjects, %d Session(s) and %d Condition(s). \n', ...
max([tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions.sess]), size(tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions,2));
disp('To calculate gPPI for different conditions, recompute VOIs and PPIs with desired conditions.');
% Number of previously calculated contrasts
nCon = length(tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI);
try
% Specify new contrasts
tmfc = tmfc_specify_contrasts_GUI(tmfc,tmfc.ROI_set_number,1);
% Calculate new contrasts
if nCon ~= length(tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI)
for iCon = nCon+1:length(tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI)
seed2vox_or_ROI2ROI(tmfc,iCon,1);
end
end
catch
freeze_GUI(0);
error('Error: Calculate new contrasts.');
end
% gPPI was calculated for some subjects
else
% Ask user to continue gPPI computation
continue_gPPI = tmfc_continue_GUI(track_gPPI,6);
if continue_gPPI == 1
calculate_gPPI = 1;
start_sub = track_gPPI;
else
disp('gPPI computation not initiated.');
freeze_GUI(0);
return;
end
end
% Compute gPPI
if calculate_gPPI == 1
disp('Initiating gPPI computation...');
try
[sub_check, contrasts] = tmfc_gPPI(tmfc,tmfc.ROI_set_number,start_sub);
for iSub = start_sub:nSub
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).gPPI = sub_check(iSub);
end
for iCon = 1:length(contrasts)
tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI(iCon).title = contrasts(iCon).title;
tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI(iCon).weights = contrasts(iCon).weights;
end
disp('gPPI computation completed.');
catch
freeze_GUI(0);
error('Error: Calculate gPPI for all subjects.');
end
end
% Unfreeze main TMFC GUI
freeze_GUI(0);
end
%% ===========================[ gPPI FIR ]=================================
% Perform gPPI-FIR analysis for selected ROI set
function gPPI_FIR_GUI(~,~,~)
% Initial checks
if ~isfield(tmfc,'subjects')
warning('Select subjects.'); return;
elseif strcmp(tmfc.subjects(1).path, '')
warning('Select subjects.'); return;
elseif ~exist(tmfc.subjects(1).path,'file')
error('SPM.mat file for the first subject does not exist.')
elseif ~isfield(tmfc,'project_path')
error('Select TMFC project folder.');
elseif ~isfield(tmfc,'ROI_set')
warning('Select ROIs.'); return;
elseif any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).VOI] == 0)
warning('Calculate VOIs for all subjects.'); return;
elseif any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).PPI] == 0)
warning('Calculate PPIs for all subjects.'); return;
elseif ~isfield(tmfc.ROI_set(tmfc.ROI_set_number),'gPPI')
error('Select conditions of interest. Calculate VOIs for all subjects.');
elseif ~isfield(tmfc.ROI_set(tmfc.ROI_set_number).gPPI,'conditions')
error('Select conditions of interest. Calculate VOIs for all subjects.');
end
% Freeze main TMFC GUI
cd(tmfc.project_path);
freeze_GUI(1);
nSub = length(tmfc.subjects);
nROI = length(tmfc.ROI_set(tmfc.ROI_set_number).ROIs);
cond_list = tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions;
nCond = length(cond_list);
% Update TMFC structure
w = waitbar(0,'Please wait...','Name','Updating gPPI-FIR progress');
for iSub = 1:nSub
check_gPPI_FIR = ones(1,nCond);
for jCond = 1:nCond
% Check ROI-to-ROI files
if tmfc.defaults.analysis == 1 || tmfc.defaults.analysis == 2
if ~exist(fullfile(tmfc.project_path,'ROI_sets',tmfc.ROI_set(tmfc.ROI_set_number).set_name,'gPPI_FIR','ROI_to_ROI','symmetrical', ...
['Subject_' num2str(iSub,'%04.f') '_Contrast_' num2str(jCond,'%04.f') '_' cond_list(jCond).file_name '.mat']),'file')
check_gPPI_FIR(jCond) = 0;
end
end
% Check seed-to-voxel files
if tmfc.defaults.analysis == 1 || tmfc.defaults.analysis == 3
if ~exist(fullfile(tmfc.project_path,'ROI_sets',tmfc.ROI_set(tmfc.ROI_set_number).set_name,'gPPI_FIR','Seed_to_voxel',tmfc.ROI_set(tmfc.ROI_set_number).ROIs(nROI).name, ...
['Subject_' num2str(iSub,'%04.f') '_Contrast_' num2str(jCond,'%04.f') '_' cond_list(jCond).file_name '.nii']),'file')
check_gPPI_FIR(jCond) = 0;
end
end
end
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).gPPI_FIR = double(~any(check_gPPI_FIR(:) == 0));
try
waitbar(iSub/nSub,w,['Subject No ' num2str(iSub,'%.f')]);
end
end
try
delete(w)
end
% Update main TMFC GUI
track_gPPI_FIR = 0;
for iSub = 1:nSub
if tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).gPPI_FIR == 0
track_gPPI_FIR = iSub;
break;
end
end
% gPPI-FIR was not calculated
if ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).gPPI_FIR] == 1)
calculate_gPPI_FIR = 1;
start_sub = 1;
% Define FIR parameters
[FIR_window,FIR_bins] = tmfc_FIR_GUI(1);
if ~isnan(FIR_window) || ~isnan(FIR_bins)
tmfc.ROI_set(tmfc.ROI_set_number).gPPI_FIR.window = FIR_window;
tmfc.ROI_set(tmfc.ROI_set_number).gPPI_FIR.bins = FIR_bins;
else
warning('Define the window length and number of time bins for gPPI-FIR regression.');
freeze_GUI(0);
return;
end
% gPPI-FIR was calculated for all subjects
elseif ~any([tmfc.ROI_set(tmfc.ROI_set_number).subjects(:).gPPI_FIR] == 0)
calculate_gPPI_FIR = 0;
fprintf('\ngPPI-FIR was calculated for all subjects, %d Session(s) and %d Condition(s). \n', ...
max([tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions.sess]), size(tmfc.ROI_set(tmfc.ROI_set_number).gPPI.conditions,2));
disp('To calculate gPPI-FIR for different conditions, recompute VOIs and PPIs with desired conditions.');
% Number of previously calculated contrasts
nCon = length(tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI_FIR);
try
% Specify new contrasts
tmfc = tmfc_specify_contrasts_GUI(tmfc,tmfc.ROI_set_number,2);
% Calculate new contrasts
if nCon ~= length(tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI_FIR)
for iCon = nCon+1:length(tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI_FIR)
seed2vox_or_ROI2ROI(tmfc,iCon,2);
end
end
catch
freeze_GUI(0);
error('Error: Calculate new contrasts.');
end
% gPPI-FIR was calculated for some subjects
else
% Ask user to continue gPPI-FIR computation
continue_gPPI_FIR = tmfc_continue_GUI(track_gPPI_FIR,7);
if continue_gPPI_FIR == 1
calculate_gPPI_FIR = 1;
start_sub = track_gPPI_FIR;
else
disp('gPPI-FIR computation not initiated.');
freeze_GUI(0);
return;
end
end
% Compute gPPI-FIR
if calculate_gPPI_FIR == 1
disp('Initiating gPPI-FIR computation...');
try
[sub_check, contrasts] = tmfc_gPPI_FIR(tmfc,tmfc.ROI_set_number,start_sub);
for iSub = start_sub:nSub
tmfc.ROI_set(tmfc.ROI_set_number).subjects(iSub).gPPI_FIR = sub_check(iSub);
end
for iCon = 1:length(contrasts)
tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI_FIR(iCon).title = contrasts(iCon).title;
tmfc.ROI_set(tmfc.ROI_set_number).contrasts.gPPI_FIR(iCon).weights = contrasts(iCon).weights;
end
disp('gPPI-FIR computation completed.');
catch
freeze_GUI(0);
error('Error: Calculate gPPI-FIR for all subjects.');
end
end
% Unfreeze main TMFC GUI
freeze_GUI(0);
end
%% ============================[ LSS GLM ]=================================
% Estimate LSS GLMs
function LSS_GLM_GUI(~,~,~)
% Initial checks
if ~isfield(tmfc,'subjects')
warning('Select subjects.'); return;
elseif strcmp(tmfc.subjects(1).path, '')
warning('Select subjects.'); return;
elseif ~exist(tmfc.subjects(1).path,'file')
error('SPM.mat file for the first subject does not exist.')
elseif ~isfield(tmfc,'project_path')
error('Select TMFC project folder.');
end
% Freeze main TMFC GUI
cd(tmfc.project_path);
freeze_GUI(1);
try
cond_list = tmfc.LSS.conditions;
nCond = length(cond_list);
sess = []; sess_num = []; nSess = [];
for iCond = 1:nCond
sess(iCond) = cond_list(iCond).sess;
end
sess_num = unique(sess);
nSess = length(sess_num);
end
nSub = length(tmfc.subjects);
% Update TMFC structure
try
w = waitbar(0,'Please wait...','Name','Updating LSS progress');
for iSub = 1:nSub
SPM = load(tmfc.subjects(iSub).path);
for jSess = 1:nSess
% Trials of interest
nTrial = 0;
trial.cond = [];
trial.number = [];
for kCond = 1:nCond
if cond_list(kCond).sess == sess_num(jSess)
nTrial = nTrial + length(SPM.SPM.Sess(sess_num(jSess)).U(cond_list(kCond).number).ons);
trial.cond = [trial.cond; repmat(cond_list(kCond).number,length(SPM.SPM.Sess(sess_num(jSess)).U(cond_list(kCond).number).ons),1)];
trial.number = [trial.number; (1:length(SPM.SPM.Sess(sess_num(jSess)).U(cond_list(kCond).number).ons))'];
end
end
% Check individual trial GLMs
for kTrial = 1:nTrial
if exist(fullfile(tmfc.project_path,'LSS_regression',['Subject_' num2str(iSub,'%04.f')],'GLM_batches', ...
['GLM_[Sess_' num2str(sess_num(jSess)) ']_[Cond_' num2str(trial.cond(kTrial)) ']_[' ...
regexprep(char(SPM.SPM.Sess(sess_num(jSess)).U(trial.cond(kTrial)).name),' ','_') ']_[Trial_' ...
num2str(trial.number(kTrial)) '].mat']), 'file')
condition(trial.cond(kTrial)).trials(trial.number(kTrial)) = 1;
else
condition(trial.cond(kTrial)).trials(trial.number(kTrial)) = 0;
end
end
tmfc.subjects(iSub).LSS.session(sess_num(jSess)).condition = condition;
clear condition
end
try
waitbar(iSub/nSub,w,['Subject No ' num2str(iSub,'%.f')]);
end
clear SPM trial nTrial
end
try
delete(w)
end
end
% Update main TMFC GUI
track_LSS = 0;
flag = 0;
if exist('cond_list','var')
for iSub = 1:nSub
for jCond = 1:nCond
if any(tmfc.subjects(iSub).LSS.session(cond_list(jCond).sess).condition(cond_list(jCond).number).trials == 0)
track_LSS = iSub;
flag = 1;
break;
end
end
if flag == 1
break;
end
end
else
track_LSS = 1;
end
if track_LSS == 0
set(main_GUI.TMFC_GUI_S6,'String', strcat(num2str(nSub), '/', num2str(nSub), ' done'),'ForegroundColor',[0.219, 0.341, 0.137]);
elseif track_LSS == 1
set(main_GUI.TMFC_GUI_S6,'String', 'Not done', 'ForegroundColor', [0.773, 0.353, 0.067]);
else
set(main_GUI.TMFC_GUI_S6,'String', strcat(num2str(track_LSS-1), '/', num2str(nSub), ' done'),'ForegroundColor',[0.219, 0.341, 0.137]);
end
restart_LSS = -1;
continue_LSS = -1;
% LSS was not calculated
if track_LSS == 1
start_sub = 1;
define_LSS_conditions = 1;
% LSS was calculated for all subjects
elseif track_LSS == 0
% Ask user to restart LSS computation
restart_LSS = tmfc_restart_GUI(2);
if restart_LSS == 1
start_sub = 1;
define_LSS_conditions = 1;
else
disp('LSS computation not initiated.');
freeze_GUI(0);
return;
end
% LSS was calculated for some subjects
else
% Ask user to continue or restart LSS computation
continue_LSS = tmfc_continue_GUI(track_LSS,2);
% Continue LSS computation
if continue_LSS == 1
start_sub = track_LSS;
define_LSS_conditions = 0;
% Restart LSS computation
elseif continue_LSS == 0
start_sub = 1;
define_LSS_conditions = 1;
else
disp('LSS computation not initiated.');
freeze_GUI(0);
return;
end
end
% Select LSS conditions
if define_LSS_conditions == 1
LSS_conditions = tmfc_LSS_GUI(tmfc.subjects(1).path);
if isstruct(LSS_conditions)
if restart_LSS == 1 || continue_LSS == 0
tmfc = reset_LSS(tmfc);
end
tmfc.LSS.conditions = LSS_conditions;
disp('Conditions of interest selected.');
else
warning('Conditions of interest not selected.');
freeze_GUI(0);
return;
end
end
% Compute LSS
disp('Initiating LSS computation...');
try
sub_check = tmfc_LSS(tmfc,start_sub);
for iSub = start_sub:nSub
tmfc.subjects(iSub).LSS = sub_check(iSub);
end