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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
% Script to generate GLMs with the matched case info and volume statistics
% from both MRI and photo segmentations

%% load data

PHOTO_RECON_HOME='/home/acasamitjana/Results';%getenv('PHOTO_RECON_HOME');

figuresDir = fullfile(PHOTO_RECON_HOME,'figures');

% load(fullfile(figuresDir,'AdjustedCaseStats.mat'),'segVolumeInfo',...
%     'matchedInfo')

savedir = fullfile(figuresDir,'GLM');

if ~exist(savedir,'dir')
    mkdir(savedir)
end


%% sort out cases to remove
% removalLevel 1 - missing volumes and Hemmorhage removed
% removalLevel 2 - poorly segmented ventricles 
% removalLevel 3 - poor segmentations and defformation 
% removalLevel 4 - biasing ventricle volumes

removalLevel = 1;

removalFlag = (0<[segVolumeInfo.removaltype]) &...
    ([segVolumeInfo.removaltype] <=removalLevel);

segVolumeInfo_kept=segVolumeInfo(~removalFlag);

savepath = fullfile(savedir,['GLMresults_removalLevel_',...
    num2str(removalLevel)]);

%% get target demographics

flag_mrs = strcmp({segVolumeInfo_kept.segtype},'MRI');
flag_hrd = strcmp({segVolumeInfo_kept.segtype},'Hard');
flag_sft = strcmp({segVolumeInfo_kept.segtype},'Soft');


flag_female_disease = strcmp(matchedInfo.sex,'Female') & ...
    strcmp(matchedInfo.CognitiveStatus,'Dementia');

fd_demographics = matchedInfo(flag_female_disease,{'caseID','age'});

flag_male_disease = strcmp(matchedInfo.sex,'Male') & ...
    strcmp(matchedInfo.CognitiveStatus,'Dementia');

md_demographics = matchedInfo(flag_male_disease,{'caseID','age'});

flag_male_control = strcmp(matchedInfo.sex,'Male') & ...
    strcmp(matchedInfo.CognitiveStatus,'No dementia');

mc_demographics = matchedInfo(flag_male_control,{'caseID','age'});

%% make info structures
% these structures hold the label volumes, age and caseIDs for the selected
% cases that have not been excluded. They are split into separate
% structures for MRI, Hard reconstructions, Hard reconstructions with
% volume correction and Soft reconstruction.
%
% They are also split by cohort fd - female disease, md - male disease,
% mc - male control.

mri_strct_fd = struct();
mri_strct_fd(size(fd_demographics,1)) = struct();

mri_strct_md = struct();
mri_strct_md(size(md_demographics,1)) = struct();

mri_strct_mc = struct();
mri_strct_mc(size(mc_demographics,1)) = struct();


hrd_strct_fd = struct();
hrd_strct_fd(size(fd_demographics,1)) = struct();

hrd_strct_md = struct();
hrd_strct_md(size(md_demographics,1)) = struct();

hrd_strct_mc = struct();
hrd_strct_mc(size(mc_demographics,1)) = struct();


crct_strct_fd = struct();
crct_strct_fd(size(fd_demographics,1)) = struct();

crct_strct_md = struct();
crct_strct_md(size(md_demographics,1)) = struct();

crct_strct_mc = struct();
crct_strct_mc(size(mc_demographics,1)) = struct();


sft_strct_fd = struct();
sft_strct_fd(size(fd_demographics,1)) = struct();

sft_strct_md = struct();
sft_strct_md(size(md_demographics,1)) = struct();

sft_strct_mc = struct();
sft_strct_mc(size(mc_demographics,1)) = struct();


strfields = fieldnames(segVolumeInfo_kept);

%% get info fd

keep_flag = true(size(fd_demographics,1),1);

for il = 1:size(fd_demographics,1)
    
    selection_flag = strcmp({segVolumeInfo_kept.caseID},...
        fd_demographics.caseID(il));
    
    if any(selection_flag & flag_mrs)
        mri_strct_fd(il).age=fd_demographics.age(il);
        
        hrd_strct_fd(il).age=fd_demographics.age(il);
        
        crct_strct_fd(il).age=fd_demographics.age(il);
        
        sft_strct_fd(il).age=fd_demographics.age(il);
        
        for jl=1:length(strfields)
            mri_strct_fd(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_mrs).(strfields{jl});
            
            hrd_strct_fd(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_hrd).(strfields{jl});
            
            if jl<5
                crct_strct_fd(il).(strfields{jl})=segVolumeInfo_kept(...
                    selection_flag & flag_hrd).(strfields{jl});
            else
                crct_strct_fd(il).(strfields{jl})=segVolumeInfo_kept(...
                    selection_flag & flag_hrd).(strfields{jl})...
                    .*segVolumeInfo_kept(selection_flag &...
                    flag_hrd).volumeAdjustmentFactor;
            end
            
            sft_strct_fd(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_sft).(strfields{jl});
        end
    else
        keep_flag(il)=false;
    end
    
    
    
end

mri_strct_fd=mri_strct_fd(keep_flag);

hrd_strct_fd=hrd_strct_fd(keep_flag);

crct_strct_fd=crct_strct_fd(keep_flag);

sft_strct_fd=sft_strct_fd(keep_flag);

%% get info md

keep_flag = true(size(md_demographics,1),1);

for il = 1:size(md_demographics,1)
    
    selection_flag = strcmp({segVolumeInfo_kept.caseID},...
        md_demographics.caseID(il));
    
    if any(selection_flag & flag_mrs)
        
        mri_strct_md(il).age=md_demographics.age(il);
        
        hrd_strct_md(il).age=md_demographics.age(il);
        
        sft_strct_md(il).age=md_demographics.age(il);
        
        for jl=1:length(strfields)
            mri_strct_md(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_mrs).(strfields{jl});
            
            hrd_strct_md(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_hrd).(strfields{jl});
            
            if jl<5
                crct_strct_md(il).(strfields{jl})=segVolumeInfo_kept(...
                    selection_flag & flag_hrd).(strfields{jl});
            else
                crct_strct_md(il).(strfields{jl})=segVolumeInfo_kept(...
                    selection_flag & flag_hrd).(strfields{jl})...
                    .*segVolumeInfo_kept(selection_flag &...
                    flag_hrd).volumeAdjustmentFactor;
            end
            
            sft_strct_md(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_sft).(strfields{jl});
        end
    else
        keep_flag(il)=false;
    end
    
    
    
end

mri_strct_md=mri_strct_md(keep_flag);

hrd_strct_md=hrd_strct_md(keep_flag);

crct_strct_md=crct_strct_md(keep_flag);

sft_strct_md=sft_strct_md(keep_flag);

%% get info mc

keep_flag = true(size(mc_demographics,1),1);

for il = 1:size(mc_demographics,1)
    
    selection_flag = strcmp({segVolumeInfo_kept.caseID},...
        mc_demographics.caseID(il));
    
    if any(selection_flag & flag_mrs)
        
        mri_strct_mc(il).age=mc_demographics.age(il);
        
        hrd_strct_mc(il).age=mc_demographics.age(il);
        
        sft_strct_mc(il).age=mc_demographics.age(il);
        
        for jl=1:length(strfields)
            
            mri_strct_mc(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_mrs).(strfields{jl});
            
            hrd_strct_mc(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_hrd).(strfields{jl});
            
            if jl<5
                crct_strct_mc(il).(strfields{jl})=segVolumeInfo_kept(...
                    selection_flag & flag_hrd).(strfields{jl});
            else
                crct_strct_mc(il).(strfields{jl})=segVolumeInfo_kept(...
                    selection_flag & flag_hrd).(strfields{jl})...
                    .*segVolumeInfo_kept(selection_flag &...
                    flag_hrd).volumeAdjustmentFactor;
            end
            
            sft_strct_mc(il).(strfields{jl})=segVolumeInfo_kept(...
                selection_flag & flag_sft).(strfields{jl});
            
        end
    else
        keep_flag(il)=false;
    end
    
    
    
end

mri_strct_mc=mri_strct_mc(keep_flag);

hrd_strct_mc=hrd_strct_mc(keep_flag);

crct_strct_mc=crct_strct_mc(keep_flag);

sft_strct_mc=sft_strct_mc(keep_flag);


%% build GLM design matrix
% columns correspond to age, gender and disease respectively. For gender 1
% corresponds to female 0 to male. For disease 1 is dementia 0 is control.
% There is also a column of ones to account for the constant.

% numbers in each cohort
% N_fd - female diseased
N_fd = length(mri_strct_fd);
% N_md - male diseased
N_md = length(mri_strct_md);
% N_mc - male control
N_mc = length(mri_strct_mc);

N_total = N_fd+N_md+N_mc;

% predictors will be age, gender and disease, with a constant term
Design_mat = zeros(N_total,4);
Design_mat(:,4)=1;

% females with disease
for il= 1:N_fd
    
    Design_mat(il,1) = mri_strct_fd(il).age;
    Design_mat(il,2) = 1;
    Design_mat(il,3) = 1;
    
end

% males with disease
for il= 1:N_md
    
    Ientry = il+N_fd;
    
    Design_mat(Ientry,1) = mri_strct_md(il).age;
    Design_mat(Ientry,2) = 0;
    Design_mat(Ientry,3) = 1;
    
end

% males without disease
for il= 1:N_mc
    
    Ientry = il+N_fd+N_md;
    
    Design_mat(Ientry,1) = mri_strct_mc(il).age;
    Design_mat(Ientry,2) = 0;
    Design_mat(Ientry,3) = 0;
    
end

% remove column for gender due to lack of female controls
Design_mat_maleOnly=Design_mat(N_fd+1:end,[1,3,4]);


% Orthogonal designmat
Design_mat = gsog(Design_mat);


% Orthogonal designmat_male
Design_mat_maleOnly = gsog(Design_mat_maleOnly);


%% run model
% not going to use volume corrections at the moment because I'm not sure
% they are working well.

volume_fields = fieldnames(mri_strct_fd);

volume_flag = ~ismember(volume_fields,{'age','caseID','segtype',...
    'volumeAdjustmentFactor','removaltype','removalnotes'}');

volume_fields = volume_fields(volume_flag);


MRI_GLM_resultsStruct.N_femaleDisease = N_fd;
MRI_GLM_resultsStruct.N_maleDisease   = N_md;
MRI_GLM_resultsStruct.N_maleControl   = N_mc;
MRI_GLM_resultsStruct.N_total         = N_total;

Hrd_GLM_resultsStruct.N_femaleDisease = N_fd;
Hrd_GLM_resultsStruct.N_maleDisease   = N_md;
Hrd_GLM_resultsStruct.N_maleControl   = N_mc;
Hrd_GLM_resultsStruct.N_total         = N_total;

Crct_GLM_resultsStruct.N_femaleDisease = N_fd;
Crct_GLM_resultsStruct.N_maleDisease   = N_md;
Crct_GLM_resultsStruct.N_maleControl   = N_mc;
Crct_GLM_resultsStruct.N_total         = N_total;

Sft_GLM_resultsStruct.N_femaleDisease = N_fd;
Sft_GLM_resultsStruct.N_maleDisease   = N_md;
Sft_GLM_resultsStruct.N_maleControl   = N_mc;
Sft_GLM_resultsStruct.N_total         = N_total;


for il=1:length(volume_fields)
    
    %% MRI GLM
    
    Observations = [mri_strct_fd(:).(volume_fields{il}),...
        mri_strct_md(:).(volume_fields{il}),...
        mri_strct_mc(:).(volume_fields{il})]';
    
    [b,dev,stats] = glmfit(Design_mat,Observations,'normal','constant',...
        'off');
    
    MRI_GLM_resultsStruct.full.(volume_fields{il}).b = b;
    MRI_GLM_resultsStruct.full.(volume_fields{il}).dev = dev;
    MRI_GLM_resultsStruct.full.(volume_fields{il}).stats = stats;
    
    
    [b,dev,stats] = glmfit(Design_mat_maleOnly,Observations(N_fd+1:end),...
        'normal','constant','off');
    
    MRI_GLM_resultsStruct.male.(volume_fields{il}).b = b;
    MRI_GLM_resultsStruct.male.(volume_fields{il}).dev = dev;
    MRI_GLM_resultsStruct.male.(volume_fields{il}).stats = stats;
    
    
    %% Hard GLM
    
    Observations = [hrd_strct_fd(:).(volume_fields{il}),...
        hrd_strct_md(:).(volume_fields{il}),...
        hrd_strct_mc(:).(volume_fields{il})]';
    
    [b,dev,stats] = glmfit(Design_mat,Observations,'normal','constant',...
        'off');
    
    Hrd_GLM_resultsStruct.full.(volume_fields{il}).b = b;
    Hrd_GLM_resultsStruct.full.(volume_fields{il}).dev = dev;
    Hrd_GLM_resultsStruct.full.(volume_fields{il}).stats = stats;
    
    
    [b,dev,stats] = glmfit(Design_mat_maleOnly,Observations(N_fd+1:end),...
        'normal','constant','off');
    
    Hrd_GLM_resultsStruct.male.(volume_fields{il}).b = b;
    Hrd_GLM_resultsStruct.male.(volume_fields{il}).dev = dev;
    Hrd_GLM_resultsStruct.male.(volume_fields{il}).stats = stats;
    
    
    %% volume corrected Hard GLM
    
    Observations = [crct_strct_fd(:).(volume_fields{il}),...
        crct_strct_md(:).(volume_fields{il}),...
        crct_strct_mc(:).(volume_fields{il})]';
    
    [b,dev,stats] = glmfit(Design_mat,Observations,'normal','constant',...
        'off');
    
    Crct_GLM_resultsStruct.full.(volume_fields{il}).b = b;
    Crct_GLM_resultsStruct.full.(volume_fields{il}).dev = dev;
    Crct_GLM_resultsStruct.full.(volume_fields{il}).stats = stats;
    
    
    [b,dev,stats] = glmfit(Design_mat_maleOnly,Observations(N_fd+1:end),...
        'normal','constant','off');
    
    Crct_GLM_resultsStruct.male.(volume_fields{il}).b = b;
    Crct_GLM_resultsStruct.male.(volume_fields{il}).dev = dev;
    Crct_GLM_resultsStruct.male.(volume_fields{il}).stats = stats;
    
    
    %% Soft GLM
    
    Observations = [sft_strct_fd(:).(volume_fields{il}),...
        sft_strct_md(:).(volume_fields{il}),...
        sft_strct_mc(:).(volume_fields{il})]';
    
    [b,dev,stats] = glmfit(Design_mat,Observations,'normal','constant',...
        'off');
    
    Sft_GLM_resultsStruct.full.(volume_fields{il}).b = b;
    Sft_GLM_resultsStruct.full.(volume_fields{il}).dev = dev;
    Sft_GLM_resultsStruct.full.(volume_fields{il}).stats = stats;
    
    
    [b,dev,stats] = glmfit(Design_mat_maleOnly,Observations(N_fd+1:end),...
        'normal','constant','off');
    
    Sft_GLM_resultsStruct.male.(volume_fields{il}).b = b;
    Sft_GLM_resultsStruct.male.(volume_fields{il}).dev = dev;
    Sft_GLM_resultsStruct.male.(volume_fields{il}).stats = stats;
    
    
end

%% Save results

save(savepath,'segVolumeInfo_kept','*_strct_*','Design_mat*','*resultsStruct')