-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc1_sc2_HCP.m
1211 lines (1015 loc) · 50.4 KB
/
sc1_sc2_HCP.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 varargout=sc1_sc2_HCP(what,varargin)
% Directories
% baseDir_orig = '/Users/maedbhking/Documents/Cerebellum_Cognition';
baseDir_orig = '/Users/maedbhking/Remote/Documents2/Cerebellum_Cognition';
baseDir = '/Volumes/Seagate Backup Plus Drive';
% baseDir = '/Volumes/MotorControl/data/super_cerebellum_new';
% baseDir = '/Users/jdiedrichsen/Data/super_cerebellum_new';
% atlasDir='/Users/maedbhking/Documents/Atlas_templates/';
atlasDir='/Users/maedbhking/Documents/Atlas_templates/';
studyDir{1} =fullfile(baseDir_orig,'sc1');
studyDir{2} =fullfile(baseDir_orig,'sc2');
HCPDir =fullfile(baseDir,'hcp');
caretDir ='/surfaceCaret';
studyStr = {'SC1','SC2','SC12'};
behavDir ='/data';
suitDir ='/suit';
regDir ='/RegionOfInterest/';
encodeDir ='/encoding';
contrastDir ='/contrasts';
anatDir ='/anatomicals';
HCP_badSubjs=[122620];
% ijk coordinates
loc_AC = {[-94,-131,-114],... %s100307
[-97,-131,-111],... %s100408
[-93,-133,-114],... %s101107
[-93,-128,-114],... %s101309
[-99,-136,-121],...%s101915
[-94,-131,-112],... %s103111
[-100,-133,-115],...%s103414
[-97,-142,-119],... %s103818
[-94,-127,-109],... %s105014
[-94,-149,-132],... %s105115
[-94,-141,-115],... %s106016
[-101,-143,-126],... %s108828
[-99,-131,-113],... %s110411
[-99,-140,-124],... %s111312
[-90,-127,-104],... %s111716
[-95,-125,-105],... %s113619
[-94,-126,-109],... %s113922
[-92,-141,-124],... %s114419
[-100,-147,-129],... %s115320
[-100,-137,-115],... %s116524
[-92,-143,-124],... %s117122
[-94,-133,-114],... %s118528
[-99,-129,-111],... %s118730
[-99,-136,-107],...%s118932
[-98,-132,-115],... %s120111
[-94,-130,-106],... %s122317
[-94,-128,-111],... %s122620
[-91,-137,-115],... %s123117
[-100,-130,-113],... %s123925
[-94,-153,-127],... %s124422
[-96,-137,-119],... %s125525
[-99,-130,-108],... %s126325
[-94,-137,-113],... %s127630
[-91,-139,-123],... %s127933
[-98,-140,-122],... %s128127
[-100,-136,-114],... %s128632
[-98,-133,-107],...%s129028
[-92,-140,-125],... %s130013
[-97,-133,-111],... %s130316
[-102,-131,-115],...%s131217
[-96,-145,-120],... %s131722
[-100,-135,-120],... %s133019
[-92,-130,-112],... %s133928
[-105,-140,-123],...%s135225
[-94,-126,-110],... %s135932
[-103,-159,-137],... %s136833
[-96,-139,-120],... %s138534
[-98,-136,-115],... %s139637
[-95,-147,-124],... %s140925
[-94,-130,-104],... %s144832
[-97,-131,-109],... %s146432
[-104,-142,-116],...%s147737
[-101,-132,-116],... %s148335
[-101,-141,-113],...%s148840
[-97,-133,-116],... %s149337
[-91,-128,-108],... %s149539
[-96,-144,-122],... %s149741
[-96,-135,-117],... %s151223
[-100,-149,-127],... %s151526
[-98,-131,-111],... %s151627
[-100,-133,-114],...%s153025
[-95,-134,-112],... %s154734
[-99,-143,-127],... %s156637
[-96,-134,-116],... %s159340
[-99,-135,-111],... %s160123
[-92,-135,-117],... %s161731
[-97,-145,-124],... %s162733
[-101,-136,-125],... %s163129
[-102,-132,-109],...%s176542
[-95,-144,-127],... %s178950
[-95,-138,-113],... %s188347
[-93,-143,-122],... %s189450
[-99,-129,-114],... %s190031
[-95,-138,-116],... %s192540
[-101,-128,-110],... %s196750
[-100,-132,-114],... %s198451
[-101,-131,-116],...%s199655
[-97,-133,-111],... %s201111
[-94,-129,-109],... %s208226
[-95,-142,-117],... %s211417
[-94,-136,-122],... %s211720
[-103,-134,-112],...%s212318
[-93,-140,-123],... %s214423
[-99,-140,-120],... %s221319
[-97,-127,-108],... %s239944
[-96,-139,-118],... %s245333
[-93,-137,-110],... %s280739
[-100,-135,-113],... %s298051
[-95,-130,-109],... %s366446
[-95,-138,-121],... %s397760
[-98,-133,-117],... %s414229
[-98,-133,-112],... %s499566
[-101,-140,-123],... %s654754
[-95,-131,-108],... %s672756
[-92,-125,-108],... %s751348
[-91,-135,-111],... %s756055
[-100,-138,-122],... %s792564
[-102,-130,-113],... %s856766
[-100,-127,-111],... %s857263
[-99,-130,-108],... %s899885
[],... %
};
loc_AC_newSubj={[-94 -129 -67]};
taskNames_all={'EMOTION','GAMBLING','LANGUAGE','MOTOR','RELATIONAL','SOCIAL','WM'};
taskNames_all_abbrev={'EM','GA','LA','MO','RE','SO','WM'};
taskNames_new = {'EM_FACES','EM_SHAPES','GA_PUNISH','GA_REWARD','LA_MATH','LA_STORY','MO_CUE',...
'MO_LF','MO_LH','MO_RF','MO_RH','MO_T','WM_TOOL','WM_PLACE','WM_0BK','WM_2BK',...
'WM_BODY','WM_FACE','RE_MATCH','RE_REL','SO_RANDOM','SO_TOM','rest'};
taskNames={'EM_FACES','EM_SHAPES','GA_PUNISH','GA_REWARD','LA_MATH','LA_STORY','MO_CUE',...
'MO_LF','MO_LH','MO_RF','MO_RH','MO_T','WM_TOOL','WM_PLACE','WM_0BK','WM_2BK',...
'WM_BODY','WM_FACE','RE_MATCH','RE_REL','SO_RANDOM','SO_TOM'};
wbDir='/Users/maedbhking/Documents/MATLAB/workbench/bin_macosx64/wb_command';
switch what
case 'LOAD:formatFiles'
whichT=varargin{1}; % 'anat' or 'task'
% get subj ID
HCP_subjs=load(fullfile(HCPDir,'HCP_subjs_new'));
% loop over subjs
for ii=1:length(HCP_subjs),
switch whichT,
case 'anat'
fprintf('%d:grabbing anatomical \n',HCP_subjs(ii))
cd(fullfile(HCPDir,'new_subjects'))
%********* DO ANATOMICAL FIRST **********
% check if folder exists and unzip
if exist(sprintf('%d_3T_Structural_preproc.zip',HCP_subjs(ii)))>0,
unzip(sprintf('%d_3T_Structural_preproc.zip',HCP_subjs(ii)))
% get nifti anat
cd(fullfile(sprintf('%d',HCP_subjs(ii)),'MNINonLinear'));
gunzip('T1w.nii.gz')
% move to anat folder (new name)
movefile('T1w.nii',fullfile(HCPDir,'anatomicals',sprintf('s%d',HCP_subjs(ii)),'anatomical_raw.nii'))
% delete unzipped folder
rmdir(fullfile(HCPDir,'new_subjects',sprintf('%d',HCP_subjs(ii))),'s')
else
fprintf('ANATOMICAL missing for %d \n',HCP_subjs(ii))
end
case 'task'
cd(fullfile(HCPDir,'new_subjects'))
%********** NOW DO TASKS *****************
% loop through all task names
for c=1:length(taskNames_all),
fprintf('%d:grabbing %s \n',HCP_subjs(ii),taskNames_all{c})
% check if folder exists and unzip folder
if exist(sprintf('%d_3T_tfMRI_%s_analysis_s2.zip',HCP_subjs(ii),taskNames_all{c}))>0,
unzip(fullfile(sprintf('%d_3T_tfMRI_%s_analysis_s2.zip',HCP_subjs(ii),taskNames_all{c})))
% get directory to cifti file
taskDir=fullfile(sprintf('%d',HCP_subjs(ii)),'MNINonLinear','Results',sprintf('tfMRI_%s',taskNames_all{c}),...
sprintf('tfMRI_%s_hp200_s2_level2.feat',taskNames_all{c}));
% get name of file to convert
fileToConvert=fullfile(taskDir,sprintf('%d_tfMRI_%s_level2_hp200_s2.dscalar.nii',HCP_subjs(ii),taskNames_all{c}));
if exist(fileToConvert)>0,
% convert cifti file to nifti file
[oldName,newName]=sc1_sc2_HCP('LOAD:CIFTI2NIFTI',fullfile(HCPDir,'new_subjects',fileToConvert));
% move nifti file to contrasts folder
for i=1:length(oldName),
movefile(fullfile(HCPDir,'new_subjects',taskDir,sprintf('/%s.nii',oldName{i})),...
fullfile(HCPDir,'contrasts',sprintf('%s_%d.nii',newName{i},HCP_subjs(ii))))
end
end
% delete unzipped folder
rmdir(fullfile(HCPDir,'new_subjects',sprintf('%d',HCP_subjs(ii))),'s')
else
fprintf('%s missing for %d \n',taskNames_all{c},HCP_subjs(ii))
end
end
end
end
case 'LOAD:CIFTI2NIFTI'
image=varargin{1};
% find taskName
for tn=1:length(taskNames_all),
if sum(strfind(image,taskNames_all(tn)))>0,
taskN=lower(taskNames_all{tn});
taskNum=tn;
else
end
end
% Read CIFTI
cii=ft_read_cifti(image);
% get images (in fieldnames)
names=fieldnames(cii);
% find directory
[dirN,~]=spm_fileparts(image);
indx=1;
% loop over images and save out as MNI
for n=1:length(names),
% only grab the relevant functional images
if sum(strfind(names{n},taskN))>0;
conName{1}=names{n};
weights=cii.(conName{1});
condIndx=strfind(conName{1},'_'); % find condN
taskName{1}=conName{1}(condIndx(2)+1:condIndx(end-1)-1);
% get rid of cortex
pos=cii.pos;
pos(cii.brainstructure==1 | cii.brainstructure==2,:,:)=[];
weights(cii.brainstructure==1 | cii.brainstructure==2)=[];
numVox=size(weights,1);
% Conversion from mm to vox
for v=1:numVox,
tmp=inv(cii.transform)*[pos(v,:),1]';
vox(v,:)=tmp';
end
% Write Labels
DATA=zeros(cii.dim);
for i=1:numVox,
DATA(vox(i,1),vox(i,2),vox(i,3))=weights(i);
end
% MNI 2mm data structure
V = struct('fname',[dirN '/' taskName{1} '.nii'],...
'dim',cii.dim,...
'dt',[4 0],...
'mat',[-2 0 0 92;0 2 0 -128;0 0 2 -74;0 0 0 1],...
'descrip','MNI_2mm');
V=spm_create_vol(V);
spm_write_vol(V,DATA);
clear V DATA
% rename
oldName{indx}=taskName{1};
tmp=char(taskName);
a=strfind(tmp,'level2'); % taskCond always comes after 'level2'
newName{indx}=sprintf('%s_%s',taskNames_all_abbrev{taskNum},upper(tmp(a+7:end)));
indx=indx+1;
else
% do nothing
end
end
varargout={oldName,newName};
case 'ANAT:resample'
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
subjs=length(HCP_subjs);
for s=1:subjs,
if exist(fullfile(HCPDir,'anatomicals',sprintf('s%d',HCP_subjs(s)),['anatomical_raw','.nii']))>0,
% Downsample anat images to 1 mm ^3
source = fullfile(HCPDir,'anatomicals',sprintf('s%d',HCP_subjs(s)),['anatomical_raw','.nii']);
newFile = fullfile(HCPDir,'anatomicals',sprintf('s%d',HCP_subjs(s)),['anatomical_resample_raw','.nii']);
spmj_resample(source,newFile,.7);
end
end
case 'ANAT:reslice_LPI' % STEP 1.2: Reslice anatomical image within LPI coordinate systems
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
subjs=length(HCP_subjs);
for s=1:subjs,
% (1) Reslice anatomical image to set it within LPI co-ordinate frames
source = fullfile(HCPDir,'anatomicals',sprintf('s%d',HCP_subjs(s)),['anatomical_resample_raw','.nii']);
dest = fullfile(HCPDir,'anatomicals',sprintf('s%d',HCP_subjs(s)),['anatomical','.nii']);
if exist(source)>0,
spmj_reslice_LPI(source,'name', dest);
% (2) In the resliced image, set translation to zero
V = spm_vol(dest);
dat = spm_read_vols(V);
V.mat(1:3,4) = [0 0 0];
spm_write_vol(V,dat);
display 'Manually retrieve the location of the anterior commissure (x,y,z) before continuing'
end
end
case 'ANAT:centre_AC' % STEP 1.3: Re-centre AC
% Set origin of anatomical to anterior commissure (must provide
% coordinates in section (4)).
% example: sc1_imana('ANAT:centre_AC',1)
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
for s=1:length(HCP_subjs),
img = fullfile(HCPDir,anatDir,sprintf('s%d',HCP_subjs(s)),['anatomical','.nii']);
if exist(img)>0,
V = spm_vol(img);
dat = spm_read_vols(V);
if strcmp(subjs,'new'),
V.mat(1:3,4)=loc_AC_newSubj{1};
else
V.mat(1:3,4) = loc_AC{s};
end
spm_write_vol(V,dat);
fprintf('Done for s%d \n',HCP_subjs(s))
end
end
case 'SUIT:run_all'
sn=varargin{1}; % 'new' or 'old'
sc1_sc2_HCP('SUIT:isolate_segment',sn)
sc1_sc2_HCP('SUIT:make_maskImage',sn)
sc1_sc2_HCP('SUIT:corr_cereb_cortex_mask',sn)
sc1_sc2_HCP('SUIT:normalise_dartel',sn,'grey')
% sc1_sc2_HCP('SUIT:make_mask',sn,'grey')
% sc1_sc2_HCP('SUIT:reslice',sn)
case 'SUIT:isolate_segment' % STEP 9.2:Segment cerebellum into grey and white matter
spm fmri
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
for s=1:length(HCP_subjs),
source=fullfile(HCPDir,anatDir,sprintf('s%d',HCP_subjs(s)),'anatomical.nii');
if exist(source)>0,
suitSubjDir = fullfile(HCPDir,suitDir,'anatomicals',sprintf('s%d',HCP_subjs(s)));dircheck(suitSubjDir);
dest=fullfile(suitSubjDir,'anatomical.nii');
copyfile(source,dest);
cd(fullfile(suitSubjDir));
suit_isolate_seg({fullfile(suitSubjDir,'anatomical.nii')},'keeptempfiles',1);
end
end
case 'SUIT:corr_cereb_cortex_mask' % STEP 9.4:
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
suitAnatDir=fullfile(HCPDir,'suit','anatomicals');
for s=1:length(HCP_subjs),
% check if the file exists
if exist(fullfile(suitAnatDir,sprintf('s%d',HCP_subjs(s))))>0,
cortexGrey= fullfile(suitAnatDir,sprintf('s%d',HCP_subjs(s)),'c3anatomical.nii');
cerebGrey = fullfile(suitAnatDir,sprintf('s%d',HCP_subjs(s)),'c1anatomical.nii');
bufferVox = fullfile(suitAnatDir,sprintf('s%d',HCP_subjs(s)),'buffer_voxels.nii');
% isolate overlapping voxels
spm_imcalc({cortexGrey,cerebGrey},bufferVox,'(i1.*i2)')
% mask buffer
spm_imcalc({bufferVox},bufferVox,'i1>0')
cerebGrey2 = fullfile(suitAnatDir,sprintf('s%d',HCP_subjs(s)),'cereb_prob_corr_grey.nii');
cortexGrey2= fullfile(suitAnatDir,sprintf('s%d',HCP_subjs(s)),'cortical_mask_grey_corr.nii');
% remove buffer from cerebellum
spm_imcalc({cerebGrey,bufferVox},cerebGrey2,'i1-i2')
% remove buffer from cortex
spm_imcalc({cortexGrey,bufferVox},cortexGrey2,'i1-i2')
end
end
case 'SUIT:normalise_dartel' % STEP 9.5: Normalise the cerebellum into the SUIT template.
% Normalise an individual cerebellum into the SUIT atlas template
% Dartel normalises the tissue segmentation maps produced by suit_isolate
% to the SUIT template
% !! Make sure that you're choosing the correct isolation mask
% (corr OR corr1 OR corr2 etc)!!
% if you are running multiple subjs - change to 'job.subjND(s)."'
% example: sc1_sc2_imana('SUIT:normalise_dartel',1,'grey')
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
for s=1:length(HCP_subjs),
if exist(fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s))))>0;
cd(fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s))));
job.subjND.gray = {'c_anatomical_seg1.nii'};
job.subjND.white = {'c_anatomical_seg2.nii'};
job.subjND.isolation= {'cereb_prob_corr_grey.nii'};
suit_normalize_dartel(job);
end
end
% 'spm_dartel_warp' code was changed to look in the working
% directory for 'u_a_anatomical_segment1.nii' file - previously it
% was giving a 'file2mat' error because it mistakenly believed that
% this file had been created
case 'SUIT:make_mask' % STEP 9.7: Make cerebellar mask using SUIT
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
for s=1:length(HCP_subjs),
if exist(fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s))))>0,
switch subjs,
case 'old'
mask = fullfile(HCPDir,'contrasts',sprintf('mask_EM_%d.nii',HCP_subjs(s))); % mask for functional image
case 'new'
toMask=fullfile(HCPDir,'contrasts',sprintf('EM_FACES_%d.nii',HCP_subjs(s)));
if exist(toMask)>0,
mask=fullfile(fullfile(HCPDir,'contrasts',sprintf('mask_grey_%d.nii',HCP_subjs(s))));
spm_imcalc(toMask,mask,'i1~=0'); % mask for functional image
end
end
suit = fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'cereb_prob_corr_grey.nii'); % cerebellar mask grey (corrected)
omask = fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'maskbrainSUITGrey.nii'); % output mask image - grey matter
cd(fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s))));
if exist(mask)>0,
spm_imcalc({mask,suit},omask,'i1>0 & i2>0.7',{});
end
end
end
case 'SUIT:reslice' % STEP 9.8: Reslice the contrast images from first-level GLM
% Reslices the functional data (betas, contrast images or ResMS)
% from the first-level GLM using deformation from
% 'suit_normalise_dartel'.
% example: sc1_sc2_imana('SUIT:reslice',1,1,4,'betas','cereb_prob_corr_grey')
% make sure that you reslice into 2mm^3 resolution
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
T=[];
for s=1:length(HCP_subjs),
for t=1:length(taskNames),
source=fullfile(HCPDir,'contrasts',sprintf('%s_%d.nii',taskNames{t},HCP_subjs(s))); % images to be resliced
if exist(source),
job.subj.affineTr = {fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'Affine_c_anatomical_seg1.mat')};
job.subj.flowfield= {fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'u_a_c_anatomical_seg1.nii')};
job.subj.mask = {fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'cereb_prob_corr_grey.nii')};
job.vox = [2 2 2];
cd(fullfile(HCPDir,'contrasts'))
job.subj.resample = {source};
dircheck(fullfile(HCPDir,'suit','contrasts'));
% cd(fullfile(HCPDir,'suit','contrasts',HCP_subjs{sn(s)}));
suit_reslice_dartel(job);
temp=dir('*wd*');
for f=1:length(temp),
movefile(fullfile(HCPDir,'contrasts',temp(f).name),fullfile(HCPDir,'suit','contrasts',temp(f).name));
end
else
fprintf('%s does not exist for %d',taskNames{t},sprintf('s%d',HCP_subjs(s)));
T.subj=HCP_subjs(s);
T.missingCon=taskNames{t};
end
fprintf('%s contrast has been resliced into suit space for %s \n\n',taskNames{t},sprintf('s%d',HCP_subjs(s)))
end
end
varargout={T};
keyboard;
case 'PREP:HCP_info'
sn=varargin{1};
% organise across subjs
S=[];
for s=1:length(sn),
conName=dir(fullfile(HCPDir,'contrasts',HCP_subjs{sn(s)},'*sess*'));
for c=1:length(conName),
condName=conName(c).name(8:end-4);
T.condNum(c,1)=D.condNum(strcmp(D.condNames,condName));
T.condNames{c,1}=condName;
tmp=str2double(conName(c).name(5:6));
T.sessNum(c,1)=tmp;
end
T.SN=repmat(sn(s),length(T.condNames),1);
S=addstruct(S,T);
clear T conName tmp
end
varargout={S};
case 'PREP:avrgMask_cereb' % STEP 11.3:
step=varargin{1}; % 'reslice' or 'mask'
% don't include s01 and s04 in the 'mask' step
HCP_subjs=sc1_sc2_HCP('PREP:HCP_subjs');
switch step,
case 'reslice'
for s=1:length(HCP_subjs),
cd(fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s))))
% normalise cerebellar grey into suit
job.subj.affineTr = {fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'Affine_c_anatomical_seg1.mat')};
job.subj.flowfield= {fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'u_a_c_anatomical_seg1.nii')};
job.subj.mask = {fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'cereb_prob_corr_grey.nii')};
job.vox = [2 2 2];
job.subj.resample = {'c1anatomical.nii'};
suit_reslice_dartel(job);
end
case 'mask'
for s=1:length(HCP_subjs),
nam{s}=fullfile(HCPDir,'suit','anatomicals',sprintf('s%d',HCP_subjs(s)),'wdc1anatomical.nii');
end
opt.dmtx = 1;
cd(fullfile(HCPDir,'suit','anatomicals'));
spm_imcalc(nam,'cerebellarGreySUIT.nii','mean(X)',opt);
fprintf('averaged cerebellar grey mask in SUIT space has been computed \n')
end
case 'PREP:cereb:voxels' % STEP 11.6: Get UW cerebellar data (voxels)
subjs=varargin{1}; % 'old' or 'new' ?
HCP_subjs=load(fullfile(HCPDir,sprintf('HCP_subjs_%s',subjs)));
% Load over all grey matter mask
V=spm_vol(fullfile(HCPDir,'suit','anatomicals','cerebellarGreySUIT.nii'));
X=spm_read_vols(V);
grey_threshold = 0.1; % grey matter threshold
volIndx=find(X>grey_threshold);
[i,j,k]= ind2sub(size(X),volIndx');
P=size(volIndx,1); % number of voxels
T=[];
for s=1:length(HCP_subjs),
for c=1:length(taskNames),
nam{1}=fullfile(HCPDir,'suit','contrasts',sprintf('wd%s_%d.nii',taskNames{c},HCP_subjs(s)));
if exist(nam{1}),
Vi=spm_vol(nam{1});
C1=spm_sample_vol(Vi,i,j,k,0);
S.data(c,:)=C1;
S.condName{c,1}=taskNames{c};
S.condNum(c,1)=c;
S.SN(c,1)=HCP_subjs(s);
else
S.data(c,:)=nan(1,P);
S.condName{c,1}=taskNames{c};
S.condNum(c,1)=c;
S.SN(c,1)=HCP_subjs(s);
end
fprintf('subj %d done for %s contrast \n',HCP_subjs(s),taskNames{c})
end
% add intercept (rest)
S.data=[S.data; zeros(1,P)];
S.condName=[S.condName;'rest'];
S.condNum=[S.condNum;c+1];
S.SN=[S.SN;HCP_subjs(s)];
T=addstruct(T,S);
clear S
end
outName=fullfile(HCPDir,'suit','contrasts',sprintf('cereb_avrgDataStruct_%sSubjs.mat',subjs));
save(outName,'T','volIndx','V');
case 'ACTIVITY:SNR'
load(fullfile(HCPDir,'suit','contrasts','cereb_avrgDataStruct.mat'));
outDir=fullfile(studyDir{2},caretDir,'suit_flat','glm4');
CN=unique(T.condNum(T.condNum~=0)); % task conditions
condNames=unique(T.condName(T.condNum~=0));
P=size(T.data,2); % number of voxels
% Tet up volume info
Yy=zeros(length(CN),V.dim(1)*V.dim(2)*V.dim(3));
C{1}.dim=V.dim;
C{1}.mat=V.mat;
% loop over tasks
% calculate SSQ, variance, sd
for c=1:length(CN),
A=getrow(T,T.condNum==CN(c));
A_avrg=nanmean(A.data,1);
SSQ(c,:)=bsxfun(@minus,A_avrg,nanmean(A_avrg));
end
Yy(:,volIndx)=SSQ;
outName='HCP_SSQ';
% map vol2surf
indices=reshape(Yy,[size(Yy,1) V.dim(1),V.dim(2),V.dim(3)]);
for i=1:size(indices,1),
data=reshape(indices(i,:,:,:),[C{1}.dim]);
C{i}.dat=data;
end
P=caret_suit_map2surf(C,'space','SUIT','stats','nanmean','column_names',condNames'); % MK created caret_suit_map2surf to allow for output to be used as input to caret_save
% save out metric
caret_save(fullfile(outDir,sprintf('%s.metric',outName)),P);
case 'ACTIVITY:patterns'
outDir=fullfile(studyDir{2},caretDir,'suit_flat','glm4');
load(fullfile(HCPDir,'suit','contrasts','cereb_avrgDataStruct.mat'));
CN=unique(T.condNum(T.condNum~=0));
SN=unique(T.SN(T.condNum~=0));
% Set up volume info
Yy=zeros(length(CN),V.dim(1)*V.dim(2)*V.dim(3));
C{1}.dim=V.dim;
C{1}.mat=V.mat;
% do normalisation (subtract avrg taskCond)
for c=1:length(CN),
A=getrow(T,T.condNum==CN(c));
B(c,:,:)=A.data;
end
% subtract baseline (avrg of all taskConds from each task)
B_avrg=nanmean(B,1);
Bb=bsxfun(@minus,B,B_avrg);
% get group avrg
B=permute(Bb,[1,3,2]);
B_group=nanmean(B,3);
Yy(:,volIndx)=B_group;
% map vol2surf
indices=reshape(Yy,[size(Yy,1) V.dim(1),V.dim(2),V.dim(3)]);
for i=1:size(indices,1),
data=reshape(indices(i,:,:,:),[C{1}.dim]);
C{i}.dat=data;
end
P=caret_suit_map2surf(C,'space','SUIT','stats','nanmean','column_names',taskNames_new(CN)); % MK created caret_suit_map2surf to allow for output to be used as input to caret_save
% save out metric
outName='HCP_contrasts';
caret_save(fullfile(outDir,sprintf('%s.metric',outName)),P);
case 'ACTIVITY:vol2surf'
taskContrast=varargin{1};
% this function takes any labelled volume (already in SUIT space)
% and plots to the surface
HCP_subjs=sc1_sc2_HCP('PREP:HCP_subjs');
someSubj=randi(100,1);
mapDir=fullfile(HCPDir,'suit','contrasts',sprintf('wd%s_%d.nii',taskContrast,HCP_subjs(someSubj)));
Vo=spm_vol(fullfile(mapDir));
Vi=spm_read_vols(Vo);
Vv{1}.dat=Vi;
Vv{1}.dim=Vo.dim;
Vv{1}.mat=Vo.mat;
M=caret_suit_map2surf(Vv,'space','SUIT');
suit_plotflatmap(M.data)
case 'ACTIVITY:indiv'
outDir=fullfile(studyDir{2},caretDir,'suit_flat','glm4');
load(fullfile(HCPDir,'suit','contrasts','cereb_avrgDataStruct.mat'));
CN=unique(T.condNum(T.condNum~=0));
SN=unique(T.SN(T.SN~=0));
P=size(T.data,2);
B=zeros(length(SN),P,length(CN));
% Set up volume info
Yy=zeros(length(CN),V.dim(1)*V.dim(2)*V.dim(3),length(SN));
C{1}.dim=V.dim;
C{1}.mat=V.mat;
% fix baseline here
for c=1:length(CN),
A=getrow(T,T.condNum==CN(c));
A_avrg=nanmean(A.data,1);
B(:,:,c)=bsxfun(@minus,A.data,A_avrg);
end
B=permute(B,[3 2 1]);
Yy(:,volIndx,:)=B;
% map vol2surf
idx=1;
indices=reshape(Yy,[size(Yy,1) V.dim(1),V.dim(2),V.dim(3) size(Yy,3)]);
for i=1:size(indices,1), % loop over taskConds
for ii=1:size(indices,5), % loop over subjs
data=reshape(indices(i,:,:,:,ii),[C{1}.dim]);
C{idx}.dat=data;
colNames{idx}=sprintf('%s-subj%d',taskNames_new{i},ii);
idx=idx+1;
fprintf('%s-subj%d done \n',taskNames_new{CN(i)},ii)
end
end
P=caret_suit_map2surf(C,'space','SUIT','stats','nanmean','column_names',colNames); % MK created caret_suit_map2surf to allow for output to be used as input to caret_save
% save out metric
outName='HCP_contrasts_allSubjs';
caret_save(fullfile(outDir,sprintf('%s.metric',outName)),P);
case 'EVAL:HCP' % evaluate group map on individual HCP data
mapType=varargin{1}; % options are 'lob10','lob26','Buckner_17Networks','Buckner_7Networks', 'Cole_10Networks'
condType=varargin{2}; % which subset of tasks are we choosing 'subset1', 'subset2' ...
groupSize=varargin{3}; % how many subjs are we grouping together ? 'old' subjs=100; 'new' subjs=214
subjs=varargin{4}; % 'new' or 'old'
% load in func data to test (e.g. if map is sc1; func data should
% be sc2)
load(fullfile(HCPDir,'suit','contrasts',sprintf('cereb_avrgDataStruct_%sSubjs.mat',subjs)));
CN=unique(T.condNum(T.condNum~=0));
condNames=T.condName(CN);
SN=unique(T.SN(T.SN~=0));
P=size(T.data,2);
switch condType
case 'subset1' % includes all taskConds
idx=CN';
case 'subset2' % doesn't include cue, lf, rf, tongue, la-story
idx=[1:5,9,11,13,14,16:23];
case 'subset3' % doesn't include cue, lf, rf, tongue
idx=[1:6,9,11,13,14,16:23];
end
% do normalisation (subtract avrg taskCond)
for c=1:length(idx),
A=getrow(T,T.condNum==idx(c));
B(c,:,:)=A.data;
end
% subtract baseline (avrg of all taskConds from each task)
B_avrg=nanmean(B,1);
Bb=bsxfun(@minus,B,B_avrg);
% group subjs into diff groups
groups=[0:groupSize:length(SN)];
for s=1:length(groups)-1,
tmp=Bb(:,groups(s)+1:groups(s+1),:);
Bb_group(:,s,:)=nanmean(tmp,2);
end
% load in group map
mapName=fullfile(studyDir{2},encodeDir,'glm4',sprintf('groupEval_%s',mapType),'map.nii');
outName=fullfile(studyDir{2},encodeDir,'glm4',sprintf('groupEval_%s',mapType),sprintf('spatialBoundfunc_HCP_%s_groupSize%d_%sSubjs.mat',condType,groupSize,subjs));
volIndx=volIndx';
% Now get the parcellation sampled into the same space
[i,j,k]=ind2sub(V.dim,volIndx);
[x,y,z]=spmj_affine_transform(i,j,k,V.mat);
VA= spm_vol(mapName);
[i1,j1,k1]=spmj_affine_transform(x,y,z,inv(VA.mat));
Parcel = spm_sample_vol(VA,i1,j1,k1,0);
% Divide the voxel pairs into all the spatial bins that we want
fprintf('parcels\n');
voxIn = Parcel>0;
XYZ= [x;y;z];
RR=[];
[BIN,R]=mva_spatialCorrBin(XYZ(:,voxIn),'Parcel',Parcel(1,voxIn));
clear XYZ i k l x y z i1 j1 k1 VA Parcel; % Free memory
% Now calculate the estimation of the correlation for each subject
for s=1:length(groups)-1,
D=Bb_group(:,s,voxIn);
D=permute(D,[1,3,2]); % conditions x voxels
fprintf('%d cross\n',s);
R.SN = ones(length(R.N),1)*s;
R.corr=mva_spatialCorr(D,BIN);
R.crossval = zeros(length(R.corr),1);
RR = addstruct(RR,R);
end;
save(outName,'-struct','RR');
case 'EVAL:PLOT:CURVES'
mapType=varargin{1}; % options are 'lob10','lob26','bucknerRest','SC<studyNum>_<num>cluster', or 'SC<studyNum>_POV<num>'
condType=varargin{2}; % 'subset1', 'subset2' etc
subjs=varargin{3}; % {'new','old'}, or {'new'}
vararginoptions({varargin{4:end}},{'CAT','sn'}); % option if doing individual map analysis
T=[];
for ss=1:length(subjs),
S=load(fullfile(studyDir{2},encodeDir,'glm4',sprintf('groupEval_%s',mapType),sprintf('spatialBoundfunc_HCP_%s_%sSubjs.mat',condType,subjs{ss})));
if ss==2,
S.SN=S.SN*101;
end
T=addstruct(T,S);
clear S
end
% distances are diff across evals so need to get dist per bin:
for b=1:length(unique(T.bin)),
dist=mode(round(T.dist(T.bin==b)));
idx=find(T.bin==b);
T.dist(idx,1)=dist;
end
if exist('CAT'),
xyplot(T.dist,T.corr,T.dist,'split',T.bwParcel,'subset',T.crossval==0 & T.dist<=35 & ~isnan(T.corr),'CAT',CAT,'leg',{'within','between'},'leglocation','SouthEast');
else
xyplot(T.dist,T.corr,T.dist,'split',T.bwParcel,'subset',T.crossval==0 & T.dist<=35 & ~isnan(T.corr),'leg',{'within','between'},'leglocation','SouthEast');
end
case 'EVAL:STATS:CURVES'
mapType=varargin{1}; % options are 'lob10','lob26','bucknerRest','SC<studyNum>_<num>cluster', or 'SC<studyNum>_POV<num>'
condType=varargin{2};
subjs=varargin{3}; % {'new','old'}, or {'new'}
crossval=0;
T=[];
for ss=1:length(subjs),
S=load(fullfile(studyDir{2},encodeDir,'glm4',sprintf('groupEval_%s',mapType),sprintf('spatialBoundfunc_HCP_%s_%sSubjs.mat',condType,subjs{ss})));
if ss==2,
S.SN=S.SN*101;
end
T=addstruct(T,S);
clear S
end
% do stats (over all bins) for group only
C=getrow(T,T.crossval==crossval & T.dist<=35); % only crossval and dist<35
S=tapply(C,{'bwParcel','SN'},{'corr'});
fprintf('overall \n')
ttest(S.corr(S.bwParcel==0), S.corr(S.bwParcel==1),2,'paired');
% calculate effect size
Group1=S.corr(S.bwParcel==0);
Group2=S.corr(S.bwParcel==1);
num=((Group1-1)*std(Group1)^2 + (Group2-1)*std(Group2)^2);
denom=Group1+Group2-2;
pooledSTD= sqrt(mean(num)/mean(denom));
ES_pooled=(mean(Group1)-mean(Group2))/pooledSTD;
fprintf('Effect size for within and between for %s is %2.2f when denom is pooled std \n',mapType,ES_pooled);
% summary stats
x1=nanmean(S.corr(S.bwParcel==0));x2=nanmean(S.corr(S.bwParcel==1));
SEM1=std(S.corr(S.bwParcel==0))/sqrt(length(T.SN));SEM2=std(S.bwParcel==1)/sqrt(length(T.SN));
fprintf('average within corr is %2.2f; CI:%2.2f-%2.2f \n average between corr is %2.2f; CI:%2.2f-%2.2f \n',...
nanmean(S.corr(S.bwParcel==0)),x1-(1.96*SEM1),x1+(1.96*SEM1),nanmean(S.corr(S.bwParcel==1)),...
x2-(1.96*SEM2),x2+(1.96*SEM2));
case 'EVAL:PLOT:DIFF'
mapType=varargin{1}; % options are 'lob10','lob26','bucknerRest','SC<studyNum>_<num>cluster', or 'SC<studyNum>_POV<num>'
condType=varargin{2}; % 'subset1', 'subset2' etc
subjs=varargin{3}; % {'new','old'}, or {'new'}
vararginoptions({varargin{4:end}},{'CAT','sn'}); % option if plotting individual map analysis
T=[];
for ss=1:length(subjs),
S=load(fullfile(studyDir{2},'encoding','glm4',sprintf('groupEval_%s',mapType),sprintf('spatialBoundfunc_HCP_%s_%sSubjs.mat',condType,subjs{ss}))); %sprintf('spatialBoundfunc_HCP_%s.mat',condType)
if ss==2,
S.SN=S.SN*101;
end
T=addstruct(T,S);
clear S
end
% distances are diff across evals so need to get dist per bin:
for b=1:length(unique(T.bin))
dist=mode(round(T.dist(T.bin==b)));
idx=find(T.bin==b);
T.dist(idx,1)=dist;
end
% plot boxplot of different clusters
W=getrow(T,T.bwParcel==0); % within
B=getrow(T,T.bwParcel==1); % between
W.diff=W.corr-B.corr;
W=rmfield(W,{'bwParcel','crossval','corr'});
if exist('CAT'),
lineplot(W.dist,W.diff,'subset',W.dist<=35 & ~isnan(W.diff),'CAT',CAT,'leg','auto');
else
lineplot(W.dist,W.diff,'subset',W.dist<=35 & ~isnan(W.diff));
end
case 'EVAL:STATS:DIFF'
mapType=varargin{1}; % options are 'lob10','lob26','bucknerRest','SC<studyNum>_<algorithm>_<numCluster>'
condType=varargin{2}; % 'subset3_groupSize25'
subjs=varargin{3}; % {'new','old'}, or {'new'}
crossval=0;
% do stats
P=[];
for m=1:length(mapType)
T=[];
for ss=1:length(subjs)
S=load(fullfile(studyDir{2},encodeDir,'glm4',sprintf('groupEval_%s',mapType{m}),sprintf('spatialBoundfunc_HCP_%s_%sSubjs.mat',condType,subjs{ss})));
if ss==2,
S.SN=S.SN*101;
end
T=addstruct(T,S);
clear S
end
A=getrow(T,T.crossval==crossval);
A.type=repmat({sprintf('%d.%s',m,mapType{m})},length(A.bin),1);
A.m=repmat(m,length(A.bin),1);
P=addstruct(P,A);
clear A
end
W=getrow(P,P.bwParcel==0); % within
B=getrow(P,P.bwParcel==1); % between
W.diff=W.corr-B.corr;
% do stats (integrate over spatial bins)
W=rmfield(W,{'bwParcel','crossval','corr'});
C=getrow(W,W.dist<=35);
S=tapply(C,{'m','SN','type'},{'diff'});
% do F test (or t test if just two groups)
if length(unique(S.m))>2,
X=[S.diff(S.m==1),S.diff(S.m==2),S.diff(S.m==3)];
results=anovaMixed(S.diff,S.SN,'between',S.m,'betweenNames',{'a','b','c'});
else
ttest(S.diff(S.m==1), S.diff(S.m==2),2,'paired');
% calculate effect size
Group1=S.diff(S.m==1);
Group2=S.diff(S.m==2);
ES_group1=(mean(Group1)-mean(Group2))/std(Group1); % uses the std of one of the groups
ES_group2=(mean(Group1)-mean(Group2))/std(Group2); % uses the std of one of the groups
% this is biased as the effect size changes depending on which
% group you choose. Therefore, pooled estimate is better.
num=((Group1-1)*std(Group1)^2 + (Group2-1)*std(Group2)^2);
denom=Group1+Group2-2;
pooledSTD= sqrt(mean(num)/mean(denom));
ES_pooled=(mean(Group1)-mean(Group2))/pooledSTD;
fprintf('Effect size between %s and %s is %2.2f when denom is std(Group1) \n',mapType{1},mapType{2},ES_group1);
fprintf('Effect size between %s and %s is %2.2f when denom is std(Group2) \n',mapType{1},mapType{2},ES_group2);
fprintf('Effect size between %s and %s is %2.2f when denom is pooled std \n',mapType{1},mapType{2},ES_pooled);
end
case 'STRENGTH:eval_bound'
mapType = varargin{1}; % 'SC12_10cluster','Buckner_7Networks'
condType = varargin{2}; % 'subset1' etc
groupSize=varargin{3}; % 1,10,25 etc
spatialBins = [0:3:35];
EvalDir = fullfile(studyDir{2},'encoding','glm4',sprintf('groupEval_%s',mapType));
% load boundaries
load(fullfile(EvalDir,'boundaries.mat'));
numBins = length(spatialBins)-1;
% Get the condition numbers
P=dload(fullfile(baseDir,'sc1_sc2_taskConds.txt'));
CN=unique(P.condNum(P.condNum~=0));
% load in func data to test
load(fullfile(HCPDir,'suit','contrasts','cereb_avrgDataStruct.mat'));
switch condType,
case 'subset1' % includes all taskConds
idx=CN';