forked from CNG-LAB/affect_cognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffect_cogn.m
executable file
·2145 lines (1766 loc) · 83.6 KB
/
affect_cogn.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
%% reproducibility script of cognition-affect project
% Lina Schaare, May 2021
%% load utils
dir = '/datadir/'; % adapt data directory
P = [dir 'utils/surfstat/'];
addpath(genpath(P));
addpath([P, 'surfstat_tutorial/surfstat']);
addpath([P, 'FreeSurfer5.3']);
addpath(genpath([dir 'utils/BrainSpace-0.1.1/matlab/']))
addpath(genpath([dir 'utils/BrainSpace-0.1.1/gifti-master/']))
addpath(genpath([dir 'utils/cifti-matlab-master']))
addpath([dir 'utils/cbrewer/'])
addpath([dir 'utils/misc/'])
DPATH = [dir 'data/HCP/'];
MASKPATH = [P, 'FreeSurfer5.3/fsaverage5/label/'];
RPATH = [dir '/affect_cognition/FIG/']; % adapt output directory
% freesurfer surfaces
S = SurfStatAvSurf({[P 'surfstat_tutorial/fsaverage5/lh.inflated'],[P 'surfstat_tutorial/fsaverage5/rh.inflated']});
SP = SurfStatAvSurf({[P 'surfstat_tutorial/fsaverage5/lh.pial'],[P 'surfstat_tutorial/fsaverage5/rh.pial']});
SW = SurfStatAvSurf({[P 'surfstat_tutorial/fsaverage5/lh.white'],[P 'surfstat_tutorial/fsaverage5/rh.white']});
SM.coord = (SP.coord + SW.coord)./2;
SM.tri = SP.tri;
SInf = SW;
SInf.coord = 0.2 *SW.coord + 0.8* S.coord;
SN.coord = 0.4*SInf.coord + 0.6*SM.coord;
SN.tri = SM.tri;
% table specifications
import mlreportgen.dom.*
fontFamily = FontFamily('TimesNewRoman');
fontFamily.BackupFamilyNames = {'Times'};
fontSize = FontSize('10pt');
t.Style = {fontFamily, fontSize};
%% load HCP data
HCP=readtable([DPATH, 'unrestricted_hlschaare_5_21_2020_5_11_56.csv']);
HCP_r=readtable([DPATH, 'RESTRICTED_hlschaare_5_21_2020_5_14_38.csv']);
% behavioural measures of interest
list_of_vars = [HCP.CogTotalComp_Unadj, HCP.CogFluidComp_Unadj, HCP.CogCrystalComp_Unadj,...
HCP.LifeSatisf_Unadj,HCP.MeanPurp_Unadj,HCP.PosAffect_Unadj,...
HCP.AngAffect_Unadj,HCP.FearAffect_Unadj,HCP.Sadness_Unadj];
%% load brain measures
for standard200_7 = 1
load_new = 0; % 0 = load pre-saved data, 1 = load all data from HCP repository
if load_new == 0
HCP200_CT = csvread([dir 'data/HCP_fromSofie/CT_200_7_nevi.csv']);
area200 = csvread([dir 'data/HCP_fromSofie/SA_200_7_nevi.csv']);
% annotate parcels
for parcels200 = 1
[vertices, label, colortablel] = ...
fs_read_annotation([MASKPATH 'lh.Schaefer2018_200Parcels_7Networks_order.annot']);
parcel_left = label;
label_left = label;
for i = 1:size(colortablel.table, 1)
mycode = colortablel.table(i,5);
parcel_left(find(parcel_left == mycode)) = i;
end
[vertices, label, colortabler] = ...
fs_read_annotation([MASKPATH 'rh.Schaefer2018_200Parcels_7Networks_order.annot']);
parcel_right = label;
label_right = label;
for i = 1:size(colortabler.table, 1)
mycode = colortabler.table(i,5);
parcel_right(find(parcel_right == mycode)) = i;
end
parcels200 = [parcel_left; parcel_right+1000];
parcels200 = parcels200';
names200 = [colortablel.struct_names(2:end);colortabler.struct_names(2:end)];
end
else
% load cortical thickness
HCP200 = ft_read_cifti([dir 'utils/misc/Schaefer2018_200Parcels_7Networks_order.dlabel.nii'],'mapname','array');
CTX_fs32k = zeros(length(HCP_r.Subject),64984);
for i = 1:length(HCP_r.Subject)
i
try
L = gifti(['/HCP/', num2str(HCP_r.Subject(i)),'/MNINonLinear/fsaverage_LR32k/', num2str(HCP_r.Subject(i)),'.L.thickness.32k_fs_LR.shape.gii']);
R = gifti(['/HCP/', num2str(HCP_r.Subject(i)),'/MNINonLinear/fsaverage_LR32k/', num2str(HCP_r.Subject(i)),'.R.thickness.32k_fs_LR.shape.gii']);
CTX_fs32k(i,:) = [L.cdata;R.cdata];
catch
end
end
% parcellate CT data
HCP200_CT= zeros(length(HCP_r.Subject),200);
for i = 1:200
HCP200_CT(:,i) = trimmean(CTX_fs32k(:,find(HCP200.dlabel==i))',10)';
end
% annotate parcels
for parcels200 = 1
[vertices, label, colortablel] = ...
fs_read_annotation([MASKPATH 'lh.Schaefer2018_200Parcels_7Networks_order.annot']);
parcel_left = label;
label_left = label;
for i = 1:size(colortablel.table, 1)
mycode = colortablel.table(i,5);
parcel_left(find(parcel_left == mycode)) = i;
end
[vertices, label, colortabler] = ...
fs_read_annotation([MASKPATH 'rh.Schaefer2018_200Parcels_7Networks_order.annot']);
parcel_right = label;
label_right = label;
for i = 1:size(colortabler.table, 1)
mycode = colortabler.table(i,5);
parcel_right(find(parcel_right == mycode)) = i;
end
parcels200 = [parcel_left; parcel_right+1000];
parcels200 = parcels200';
names200 = [colortablel.struct_names(2:end);colortabler.struct_names(2:end)]
end
% load surface area and parcellate
% use the unsmoothed data for the parcels
isthere_ct = zeros(size(HCP_r.Subject));
namesct_left = strcat('/FS_surf/', num2str(HCP_r.Subject), '_lh2areaj_fsaverage_1.mgh');
namesct_right = strcat('/FS_surf/', num2str(HCP_r.Subject), '_rh2areaj_fsaverage_1.mgh');
AREA = zeros(length(HCP_r.Subject),size(SW.coord,2));
for i = 1:length(HCP_r.Subject)
try
AREA(i,1:10242) = SurfStatReadData1(namesct_left(i,:));
AREA(i,10243:20484) = SurfStatReadData1(namesct_right(i,:));
isthere_ct(i) = 1;
catch
disp([namesct_left(i,:) ' not there'])
end
sum(isthere_ct)/length(isthere_ct)
end
area200 = []
for i = 1:100
area200(i,:) = sum(AREA(:,find(parcels200==i+1)),2);
end
for i = 1:100
area200(i+100,:) = sum(AREA(:,find(parcels200==i+1001)),2);
end
% save data
csvwrite([DPATH 'CT_200_7.csv'], HCP200_CT);
csvwrite([DPATH 'SA_200_7.csv'], area200');
fid = fopen(['DPATH labels_200_7.csv','w'])
fprintf(fid,'%s\n',names200{:,1})
fclose(fid)
end
% load subcortical volumes
subcort_fs(:,1) = table(HCP.FS_L_AccumbensArea_Vol);
subcort_fs(:,2) = table(HCP.FS_R_AccumbensArea_Vol);
subcort_fs(:,3) = table(HCP.FS_L_Amygdala_Vol);
subcort_fs(:,4) = table(HCP.FS_R_Amygdala_Vol);
subcort_fs(:,5) = table(HCP.FS_L_Caudate_Vol);
subcort_fs(:,6) = table(HCP.FS_R_Caudate_Vol);
subcort_fs(:,7) = table(HCP.FS_L_Hippo_Vol);
subcort_fs(:,8) = table(HCP.FS_R_Hippo_Vol);
subcort_fs(:,9) = table(HCP.FS_L_Pallidum_Vol);
subcort_fs(:,10) = table(HCP.FS_R_Pallidum_Vol);
subcort_fs(:,11) = table(HCP.FS_L_Putamen_Vol);
subcort_fs(:,12) = table(HCP.FS_R_Putamen_Vol);
subcort_fs(:,13) = table(HCP.FS_L_ThalamusProper_Vol);
subcort_fs(:,14) = table(HCP.FS_R_ThalamusProper_Vol);
subcort_fs(:,15) = table(HCP.FS_L_VentDC_Vol);
subcort_fs(:,16) = table(HCP.FS_R_VentDC_Vol);
for i = 1:size(subcort_fs, 2)
subcort_fs{:,i} = subcort_fs{:,i}./1000;
end
subcort_fs.Properties.VariableNames = {'accumb_l','accumb_r',...
'amy_l','amy_r','caud_l','caud_r','hipp_l','hipp_r','pall_l',...
'pall_r','put_l','put_r','thal_l','thal_r','ventDC_l','ventDC_r'};
end
%% find missing data and outliers
for out = 1
studykeep = [1:size(HCP,1)]';
% how many missing values in each modality?
sum(sum(isnan(list_of_vars),2)>0)
sum(sum(isnan(subcort_fs{:,:}),2)>0)
sum(mean(HCP200_CT,2)==0)
sum(mean(area200,2)==0)
% find participants with missing data
studykeep(sum(isnan(list_of_vars),2)>0) = 0;
studykeep(sum(isnan(subcort_fs{:,:}),2)>0) = 0;
studykeep(mean(HCP200_CT,2)==0) = 0;
studykeep(mean(area200,2)==0) = 0;
% find participants with outlier data
group_a = mean(area200(mean(area200,2)>0,:));
group_c = mean(HCP200_CT(mean(HCP200_CT,2)>0,:));
group_s = nanmean(subcort_fs{:,:});
outlier = ones(4,size(HCP,1));
for c = 1:size(HCP,1)
r_a = corrcoef(area200(c,:), group_a);
r_c = corrcoef(HCP200_CT(c,:), group_c);
r_s = corrcoef(subcort_fs{c,:}, group_s);
if r_a(2) < 0.8 || isnan(r_a(2))
outlier(1,c) = c;
outlier(2,c) = r_a(2);
elseif r_c(2) < 0.8 || isnan(r_c(2))
outlier(1,c) = c;
outlier(3,c) = r_c(2);
elseif r_s(2) < 0.8 || isnan(r_s(2))
outlier(1,c) = c;
outlier(4,c) = r_c(2);
else
continue;
end
end
%outlier in sa
out_sa = find(outlier(2,:)<1)
%outlier in ctx
out_ct = find(outlier(3,:)<1)
%outlier in subc
out_subc = find(outlier(4,:)<1)
% check outliers visually
for o = 1:length(out_sa)
ROnSurf = zeros(1,20484);
for i = 1:100
ROnSurf(:,find(parcels200==i+1)) = area200(out_sa(o),i);
end
for i = 1:100
ROnSurf(:,find(parcels200==i+1001)) = area200(out_sa(o),i+100);
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
exportfigbo(f,[RPATH, 'outliers.sa_' num2str(o) '.png'],'png', 10)
close(f)
end
for o = 1:length(out_ct)
ROnSurf = zeros(1,20484);
for i = 1:100
ROnSurf(:,find(parcels200==i+1)) = HCP200_CT(out_ct(o),i);
end
for i = 1:100
ROnSurf(:,find(parcels200==i+1001)) = HCP200_CT(out_ct(o),i+100);
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
exportfigbo(f,[RPATH, 'outliers.ctx_' num2str(o) '.png'],'png', 10)
close(f)
end
% exclude outliers
studykeep(find(ismember(studykeep, [out_ct, out_sa, out_subc]))) = 0;
studykeep(studykeep == 0) = [];
end
%% sample descriptives
sum(strcmp(HCP.Gender, 'F')) %656
sum(strcmp(HCP_r.ZygositySR, 'MZ')) %292
sum(strcmp(HCP_r.ZygositySR, 'NotMZ')) %323
sum(strcmp(HCP_r.ZygositySR, 'NotTwin')) %586 %5 missing values
sum(strcmp(HCP.Gender(studykeep), 'F')) %592
sum(strcmp(HCP.Gender(studykeep), 'M')) %499
sum(strcmp(HCP_r.ZygositySR(studykeep), 'MZ')) %274
sum(strcmp(HCP_r.ZygositySR(studykeep), 'NotMZ')) %288
sum(strcmp(HCP_r.ZygositySR(studykeep), 'NotTwin')) %525 %4 missing values
mean(HCP_r.Age_in_Yrs(studykeep)) %28.8139
std(HCP_r.Age_in_Yrs(studykeep)) %3.6977
min(HCP_r.Age_in_Yrs(studykeep)) %22
max(HCP_r.Age_in_Yrs(studykeep)) %37
%% Phenotypic correlation, heritability and genetic correlation of cognition and affect
for figure1 = 1
% NIH positive affect composite
nihPA = zeros(1,1206);
nihPA(studykeep) = mean([HCP.LifeSatisf_Unadj(studykeep), HCP.MeanPurp_Unadj(studykeep), HCP.PosAffect_Unadj(studykeep)],2);
% NIH negative affect composite
nihNA = zeros(1,1206);
nihNA(studykeep) = mean([HCP.AngAffect_Unadj(studykeep), HCP.AngHostil_Unadj(studykeep), HCP.Sadness_Unadj(studykeep),...
HCP.FearAffect_Unadj(studykeep),HCP.PercStress_Unadj(studykeep)],2);
% mean affect
aff = zeros(1,1206);
aff(studykeep) = mean([nihPA(studykeep)', -nihNA(studykeep)'],2);
list_of_vars = [HCP.CogTotalComp_Unadj, HCP.CogFluidComp_Unadj, HCP.CogCrystalComp_Unadj, nihPA', nihNA', aff'];
comptitlevar = {'Total Cognition', 'Fluid', 'Crystallized', 'Positive Affect', 'Negative Affect', 'Mean Affect'};
T = table(mean(list_of_vars(studykeep,:))',std(list_of_vars(studykeep,:))',...
min(list_of_vars(studykeep,:))',max(list_of_vars(studykeep,:))',...
'VariableNames', {'Mean','SD','Min','Max'}, 'RowNames',comptitlevar')
writetable(T,[RPATH 'Table1.csv']);
% write phenotype tables for genetic analyses
% CT
T = table(HCP.Subject(studykeep), HCP_r.Age_in_Yrs(studykeep), HCP.Gender(studykeep),...
list_of_vars(studykeep,:), mean(HCP200_CT(studykeep,:),2), HCP200_CT(studykeep,:),...
'VariableNames', {'id','age','sex','bhv','gb','brain'});
T = splitvars(T, 'bhv', 'NewVariableNames', {'totalCog','fluid','crystal','nihPA','nihNA','aff'});
T = splitvars(T, 'brain', 'NewVariableNames', names200);
writetable(T, [dir '/affect_cognition/solar_CT/cog_affect_CT.csv']);
% SA
T = table(HCP.Subject(studykeep), HCP_r.Age_in_Yrs(studykeep), HCP.Gender(studykeep),...
list_of_vars(studykeep,:), HCP.FS_IntraCranial_Vol(studykeep,:)./1000, area200(studykeep,:),...
'VariableNames', {'id','age','sex','bhv','icv','brain'});
T = splitvars(T, 'bhv', 'NewVariableNames', {'totalCog','fluid','crystal','nihPA','nihNA','aff'});
T = splitvars(T, 'brain', 'NewVariableNames', names200);
writetable(T, [dir '/affect_cognition/solar_SA/cog_affect_SA.csv']);
% SUBC
T = [table(HCP.Subject(studykeep), HCP_r.Age_in_Yrs(studykeep), HCP.Gender(studykeep),...
list_of_vars(studykeep,:), HCP.FS_IntraCranial_Vol(studykeep,:)./1000,...
'VariableNames', {'id','age','sex','bhv','icv'}), subcort_fs(studykeep,:)];
T = splitvars(T, 'bhv', 'NewVariableNames', {'totalCog','fluid','crystal','nihPA','nihNA','aff'});
writetable(T, [dir '/affect_cognition/solar_subc/cog_affect_subcortical.csv']);
% phenotypic behavioral analyses
f = figure,
subplot(1,6,1),
hist(list_of_vars(studykeep,1))
subplot(1,6,2),
hist(list_of_vars(studykeep,2))
subplot(1,6,3),
hist(list_of_vars(studykeep,3))
subplot(1,6,4),
hist((list_of_vars(studykeep,4)))
subplot(1,6,5),
hist(list_of_vars(studykeep,5))
subplot(1,6,6),
hist(list_of_vars(studykeep,6))
exportfigbo(f,[RPATH, 'F1A.personality.hist.png'],'png', 6)
close(f)
for i = 1:size(list_of_vars,2)
vary = list_of_vars(:,i);
keep = studykeep;
vark = zscore(vary(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
sex_num = grp2idx(sexk);
M = 1 + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ term(vark); %((term(agek) * term(agek)) * term(sexk)) +
slm = SurfStatLinMod(zscore(list_of_vars(keep,:)),M);
slm = SurfStatT(slm, vark);
t_var(i,:) = slm.t;
beta_var(i,:) = slm.coef(end,:);
pp_var(i,:) = 1- tcdf(slm.t,slm.df);
pn_var(i,:) = 1-pp_var(i,:);
end
t_var(eye(6)==1)=1;
beta_var(eye(6)==1)=1;
pp_var(eye(6)==1)=1;
pn_var(eye(6)==1)=1;
fdr_bh([pp_var(1,2),pp_var(1,3),pp_var(1,4),pp_var(1,5),pp_var(1,6),...
pp_var(2,3),pp_var(2,4),pp_var(2,5),pp_var(2,6),...
pp_var(3,4),pp_var(3,5),pp_var(3,6),...
pp_var(4,5),pp_var(4,6),...
pp_var(5,6)],0.025)
ppn = squareform(ans);
fdr_bh([pn_var(1,2),pn_var(1,3),pn_var(1,4),pn_var(1,5),pn_var(1,6),...
pn_var(2,3),pn_var(2,4),pn_var(2,5),pn_var(2,6),...
pn_var(3,4),pn_var(3,5),pn_var(3,6),...
pn_var(4,5),pn_var(4,6),...
pn_var(5,6)],0.025)
pnn = squareform(ans);
ppp = ppn+pnn;
ppp(eye(6)==1) =1;
for explo = 1
psignr = ppp == 1;
%rleft = t_var.*psignr;
rleft = beta_var.*psignr;
mat = rleft;
f = figure;
imagesc(mat)
colorbar;
caxis([-1,1]);
textStrings = num2str(mat(:), '%0.2f'); % Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings)); % Remove any space padding
textStrings = replace(textStrings,'0.00','-');
[x, y] = meshgrid(1:6); % Create x and y coordinates for the strings
hStrings = text(x(:), y(:), textStrings(:), ... % Plot the strings
'HorizontalAlignment', 'center');
midValue = mean(get(gca, 'CLim')); % Get the middle value of the color range
textColors = repmat(abs(mat(:)) > 0.5, 1, 3); % Choose white or black for the text color of the strings so they can be easily seen over the background color
set(hStrings, {'Color'}, num2cell(textColors, 2)); % Change the text colors
set(gca, 'XTick', 1:(length(list_of_vars)));
set(gca, 'XTickLabel', comptitlevar)
xtickangle(-45)
colormap(flipud(cbrewer('div','RdBu',121)));
colorbar;
set(gca, 'YTick', 1:(length(list_of_vars)));
set(gca, 'YTickLabel', comptitlevar)
caxis([-1,1]);
title('Phenotypic correlation: betas, FDRq<0.05')
print(f,[RPATH 'Corr_comp_explo_n'],'-dpng')
close(f)
end
%plot total cognition and mean affect
f=figure;
scatter(list_of_vars(:,1), list_of_vars(:,6),'.','k')
l = lsline;
l.Color = 'k';
l.LineWidth = 2;
xlabel('Total Cognition')
ylabel('Mean Affect')
print(f,[RPATH 'Scatter_cog_affect'],'-dpng')
close(f)
%heritability:
F1b = readtable([dir 'affect_cognition/solar_subc/cog_affect_heritability.csv']);
%rearrange order of traits, so it matches with order above
ord = [6 3 2 5 4 1];
F1b = F1b(ord,:);
f = figure;
bar(F1b.H2r)
set(gca, 'XTickLabel', comptitlevar)
xtickangle(-45)
print(f,[RPATH 'Heri_1B'],'-dpng')
close(f)
%genetic correlation
F1c = readtable([dir 'affect_cognition/solar_subc/cog_aff_gencorr.csv']);
gc_bv = eye(6);
gc_bv(gc_bv==0)=F1c.rG;
gc_bv_p = eye(6);
gc_bv_p(gc_bv_p==0)=F1c.p;
%rearrange order of traits, so it matches with order above
ord = [6 3 2 5 4 1];
gc_bv = gc_bv(ord,ord);
gc_bv_p = gc_bv_p(ord,ord);
%FDR correction
pp_var = gc_bv_p;
fdr_bh([pp_var(1,2),pp_var(1,3),pp_var(1,4),pp_var(1,5),pp_var(1,6),...
pp_var(2,3),pp_var(2,4),pp_var(2,5),pp_var(2,6),...
pp_var(3,4),pp_var(3,5),pp_var(3,6),...
pp_var(4,5),pp_var(4,6),...
pp_var(5,6)],0.025)
ppn = squareform(ans);
for explo = 1
psignr = ppn;
rleft = gc_bv.*psignr;
mat = rleft;
f = figure;
imagesc(mat)
colorbar;
caxis([-1,1]);
textStrings = num2str(mat(:), '%0.2f'); % Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings)); % Remove any space padding
textStrings = replace(textStrings,'0.00','-');
[x, y] = meshgrid(1:6); % Create x and y coordinates for the strings
hStrings = text(x(:), y(:), textStrings(:), ... % Plot the strings
'HorizontalAlignment', 'center');
midValue = mean(get(gca, 'CLim')); % Get the middle value of the color range
textColors = repmat(mat(:) > 0.5, 1, 3); % Choose white or black for the text color of the strings so they can be easily seen over the background color
set(hStrings, {'Color'}, num2cell(textColors, 2)); % Change the text colors
set(gca, 'XTick', 1:(length(list_of_vars)));
set(gca, 'XTickLabel', comptitlevar)
xtickangle(-45)
colormap(flipud(cbrewer('div','RdBu',121)));
colorbar;
set(gca, 'YTick', 1:(length(list_of_vars)));
set(gca, 'YTickLabel', comptitlevar)
caxis([-1,1]);
title('Genetic correlation: rhoG, FDRq<0.05')
print(f,[RPATH 'F1C'],'-dpng')
close(f)
end
end
%% Phenotypic correlation of cognition and brain structure
%cognition: total cognition, fluid, crystallized
% cortical thickness
for ctx = 1
% total
for k = 1
total = HCP.CogTotalComp_Unadj;
keep = studykeep;
vark = zscore(total(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
gb = zscore(mean(HCP200_CT(keep,:),2));
M = 1 + term(gb) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek)) + ((term(agek) * term(agek)) * term(sexk)) + term(vark);
slm = SurfStatLinMod(zscore(HCP200_CT(keep,:)),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp, 0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn, 0.025);
h = h1+h2;
var_ctx_t(1,:) = slm.ef; %slm.t; % standardized betas or t-values
var_ctx_sd(1,:) = slm.sd;
var_ctx_p(1,:) = p_all;
var_ctx_FDR(1,:) = h;
%CohD = (slm.t)./sqrt(slm.df);
if max(h) == 1
p_val = h;
ROnSurf = zeros(1,20484);
for i = 1:100
%ROnSurf(:,find(parcels200==i+1)) = (slm.t(i)).*(p_val(i));
ROnSurf(:,find(parcels200==i+1)) = (slm.ef(i)).*(p_val(i));
%ROnSurf(:,find(parcels200==i+1)) = (CohD(i)).*(p_val(i));
end
for i = 1:100
%ROnSurf(:,find(parcels200==i+1001)) = (slm.t(i+100)).*(p_val(i+100));
ROnSurf(:,find(parcels200==i+1001)) = (slm.ef(i+100)).*(p_val(i+100));
%ROnSurf(:,find(parcels200==i+1001)) = (CohD(i+100)).*(p_val(i+100));
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
colormap(flipud(cbrewer('div','RdBu',11)));
SurfStatColLim([-0.2 0.2])
exportfigbo(f,[RPATH, 'F2pheno.total.ctx.png'],'png', 10)
close(f)
end
end
% fluid
for k = 1
fluid = HCP.CogFluidComp_Unadj;
keep = studykeep;
vark = zscore(fluid(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
gb = zscore(mean(HCP200_CT(keep,:),2));
M = 1 + term(gb) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek)) + ((term(agek) * term(agek)) * term(sexk)) + term(vark);
slm = SurfStatLinMod(zscore(HCP200_CT(keep,:)),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_ctx_t(2,:) = slm.ef; %slm.t; % standardized betas or t-values
var_ctx_sd(2,:) = slm.sd;
var_ctx_p(2,:) = p_all;
var_ctx_FDR(2,:) = h;
if max(h) == 1
p_val = h;
ROnSurf = zeros(1,20484);
for i = 1:100
%ROnSurf(:,find(parcels200==i+1)) = (slm.t(i)).*(p_val(i));
ROnSurf(:,find(parcels200==i+1)) = (slm.ef(i)).*(p_val(i));
end
for i = 1:100
%ROnSurf(:,find(parcels200==i+1001)) = (slm.t(i+100)).*(p_val(i+100));
ROnSurf(:,find(parcels200==i+1001)) = (slm.ef(i+100)).*(p_val(i+100));
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
colormap(flipud(cbrewer('div','RdBu',11)));
SurfStatColLim([-0.2 0.2])
exportfigbo(f,[RPATH, 'F2pheno.fluid.ctx.png'],'png', 10)
close(f)
end
end
% crystallized
for k = 1
crys = HCP.CogCrystalComp_Unadj;
keep = studykeep;
vark = zscore(crys(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
gb = zscore(mean(HCP200_CT(keep,:),2));
M = 1 + term(gb) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ ((term(agek) * term(agek)) * term(sexk)) + term(vark);
slm = SurfStatLinMod(zscore(HCP200_CT(keep,:)),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_ctx_t(3,:) = slm.ef; %slm.t; % standardized betas or t-values
var_ctx_sd(3,:) = slm.sd;
var_ctx_p(3,:) = p_all;
var_ctx_FDR(3,:) = h;
if max(h) == 1
p_val = h;
ROnSurf = zeros(1,20484);
for i = 1:100
%ROnSurf(:,find(parcels200==i+1)) = (slm.t(i)).*(p_val(i));
ROnSurf(:,find(parcels200==i+1)) = (slm.ef(i)).*(p_val(i));
end
for i = 1:100
%ROnSurf(:,find(parcels200==i+1001)) = (slm.t(i+100)).*(p_val(i+100));
ROnSurf(:,find(parcels200==i+1001)) = (slm.ef(i+100)).*(p_val(i+100));
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
colormap(flipud(cbrewer('div','RdBu',11)));
SurfStatColLim([-0.2 0.2])
exportfigbo(f,[RPATH, 'F2pheno.crys.ctx.png'],'png', 10)
close(f)
end
end
end
% surface area
for area = 1
% total
for k = 1
total = HCP.CogTotalComp_Unadj;
keep = studykeep;
vark = zscore(total(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
icv = zscore(HCP.FS_IntraCranial_Vol(keep));
M = 1 + term(icv) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ ((term(agek) * term(agek)) * term(sexk)) + term(vark);
slm = SurfStatLinMod(zscore(area200(keep,:)),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_area_t(1,:) = slm.ef; %slm.t;
var_area_sd(1,:) = slm.sd;
var_area_p(1,:) = p_all;
var_area_FDR(1,:) = h;
if max(h) == 1
p_val = h;
ROnSurf = zeros(1,20484);
for i = 1:100
%ROnSurf(:,find(parcels200==i+1)) = (slm.t(i)).*(p_val(i));
ROnSurf(:,find(parcels200==i+1)) = (slm.ef(i)).*(p_val(i));
end
for i = 1:100
%ROnSurf(:,find(parcels200==i+1001)) = (slm.t(i+100)).*(p_val(i+100));
ROnSurf(:,find(parcels200==i+1001)) = (slm.ef(i+100)).*(p_val(i+100));
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
colormap(flipud(cbrewer('div','RdBu',11)));
SurfStatColLim([-0.2 0.2])
exportfigbo(f,[RPATH, 'F2pheno.total.area.png'],'png', 10)
close(f)
end
end
% fluid
for k = 1
fluid = HCP.CogFluidComp_Unadj;
keep = studykeep;
vark = zscore(fluid(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
icv = zscore(HCP.FS_IntraCranial_Vol(keep));
M = 1 + term(icv) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ ((term(agek) * term(agek)) * term(sexk)) + term(vark) ;
slm = SurfStatLinMod(zscore(area200(keep,:)),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_area_t(2,:) = slm.ef; %slm.t;
var_area_sd(2,:) = slm.sd;
var_area_p(2,:) = p_all;
var_area_FDR(2,:) = h;
if max(h) == 1
p_val = h;
ROnSurf = zeros(1,20484);
for i = 1:100
%ROnSurf(:,find(parcels200==i+1)) = (slm.t(i)).*(p_val(i));
ROnSurf(:,find(parcels200==i+1)) = (slm.ef(i)).*(p_val(i));
end
for i = 1:100
%ROnSurf(:,find(parcels200==i+1001)) = (slm.t(i+100)).*(p_val(i+100));
ROnSurf(:,find(parcels200==i+1001)) = (slm.ef(i+100)).*(p_val(i+100));
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
colormap(flipud(cbrewer('div','RdBu',11)));
SurfStatColLim([-0.2 0.2])
exportfigbo(f,[RPATH, 'F2pheno.fluid.area.png'],'png', 10)
close(f)
end
end
% crystallized
for k = 1
crys = HCP.CogCrystalComp_Unadj;
keep = studykeep;
vark = zscore(crys(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
icv = zscore(HCP.FS_IntraCranial_Vol(keep));
M = 1 + term(icv) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ ((term(agek) * term(agek)) * term(sexk)) + term(vark) ;
slm = SurfStatLinMod(zscore(area200(keep,:)),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_area_t(3,:) = slm.ef; %slm.t;
var_area_sd(3,:) = slm.sd;
var_area_p(3,:) = p_all;
var_area_FDR(3,:) = h;
if max(h) == 1
p_val = h;
ROnSurf = zeros(1,20484);
for i = 1:100
%ROnSurf(:,find(parcels200==i+1)) = (slm.t(i)).*(p_val(i));
ROnSurf(:,find(parcels200==i+1)) = (slm.ef(i)).*(p_val(i));
end
for i = 1:100
%ROnSurf(:,find(parcels200==i+1001)) = (slm.t(i+100)).*(p_val(i+100));
ROnSurf(:,find(parcels200==i+1001)) = (slm.ef(i+100)).*(p_val(i+100));
end
f = figure;
BoSurfStatViewData(ROnSurf,SN,'')
colormap(flipud(cbrewer('div','RdBu',11)));
SurfStatColLim([-0.2 0.2])
exportfigbo(f,[RPATH, 'F2pheno.crys.area.png'],'png', 10)
close(f)
end
end
end
% subcortical voumes
for subcort = 1
% total
for k = 1
total = HCP.CogTotalComp_Unadj;
keep = studykeep;
vark = zscore(total(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
icv = zscore(HCP.FS_IntraCranial_Vol(keep));
M = 1 + term(icv) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ ((term(agek) * term(agek)) * term(sexk)) + term(vark);
slm = SurfStatLinMod(zscore(subcort_fs{keep,:}),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_sub_t(1,:) = slm.ef; %slm.t;
var_sub_sd(1,:) = slm.sd;
var_sub_p(1,:) = p_all;
var_sub_FDR(1,:) = h;
if max(h) == 1
f = figure;
bar(slm.ef)
set(gca, 'XTick', 1:(length(subcort_fs.Properties.VariableNames)));
set(gca, 'XTickLabel', subcort_fs.Properties.VariableNames)
xtickangle(-45)
exportfigbo(f,[RPATH, 'F2pheno.total.sub.png'],'png', 10)
close(f)
end
end
% fluid
for k = 1
fluid = HCP.CogFluidComp_Unadj;
keep = studykeep;
vark = zscore(fluid(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
icv = zscore(HCP.FS_IntraCranial_Vol(keep));
M = 1 + term(icv) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ ((term(agek) * term(agek)) * term(sexk)) + term(vark);
slm = SurfStatLinMod(zscore(subcort_fs{keep,:}),M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_sub_t(2,:) = slm.ef; %slm.t;
var_sub_sd(2,:) = slm.sd;
var_sub_p(2,:) = p_all;
var_sub_FDR(2,:) = h;
if max(h) == 1
p_val = h;
f = figure;
bar(slm.ef)
set(gca, 'XTick', 1:(length(subcort_fs.Properties.VariableNames)));
set(gca, 'XTickLabel', subcort_fs.Properties.VariableNames)
xtickangle(-45)
exportfigbo(f,[RPATH, 'F2pheno.fluid.sub.png'],'png', 10)
close(f)
end
end
% crystallized
for k = 1
crys = HCP.CogCrystalComp_Unadj;
keep = studykeep;
vark = zscore(crys(keep));
agek = zscore(HCP_r.Age_in_Yrs(keep));
sexk = cellstr(HCP.Gender(keep));
icv = zscore(HCP.FS_IntraCranial_Vol(keep));
M = 1 + term(icv) + term(agek) + term(sexk) + (term(agek) * term(sexk)) + (term(agek) * term(agek))+ ((term(agek) * term(agek)) * term(sexk)) + term(vark);
slm = SurfStatLinMod(subcort_fs{keep,:},M);
slm = SurfStatT(slm, vark);
pp = 1 - tcdf(slm.t, slm.df);
pn = 1 - tcdf(-slm.t, slm.df);
p= zeros(size(pp));
p = pp<pn;
p_all= zeros(size(p));
p_all(p==1) = pp(p==1);
p_all(p==0) = pn(p==0);
[h1, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pp,0.025);
[h2, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pn,0.025);
h = h1+h2;
var_sub_t(3,:) = slm.ef; %slm.t;
var_sub_sd(3,:) = slm.sd;
var_sub_p(3,:) = p_all;
var_sub_FDR(3,:) = h;
if max(h) == 1
p_val = h;
f = figure;
bar(slm.ef)
set(gca, 'XTick', 1:(length(subcort_fs.Properties.VariableNames)));
set(gca, 'XTickLabel', subcort_fs.Properties.VariableNames)
xtickangle(-45)
exportfigbo(f,[RPATH, 'F2pheno.crys.sub.png'],'png', 10)
close(f)
end
end
end
%% Phenotypic correlation of affect and brain structure
% affect: positive affect, negative affect, mean affect