forked from francopestilli/life_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_fe_mt_ips_tract_hcp.m
310 lines (266 loc) · 11.2 KB
/
s_fe_mt_ips_tract_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
function s_fe_mt_ips_tract_hcp(hemisphere)
%
% This script performs a test of conenctivity of MT+ (Zilles t al ROI from
% freesurfer) with the Superior parietal Cortex (Aparc Freesurfer segementation).
% The following are the steps we perform:
% - It loads a whole-brain tractography solution.
% - It loads the MTand Parietal ROI.
% - It finds the tract connecting the two ROIs.
% - It builds a connectome model in the ROI defined by the tract
% - It performs a virtual lesion on the tract connecting MT and parietal.
%
% Copyright by Franco Pestilli Stanford University, 2014
dataDir = {'2t1','2t1'};
subj_count = 0;
for iDd = 1:length(dataDir)
% Get the base directory for the data
[~,hostname] = system('hostname');
hostname = deblank(hostname);
switch dataDir{iDd}
case {'2t2'}
switch hostname
case {'marcovaldo'}
datapath = '/home/frk/2t2/HCP/';
otherwise
datapath = '/marcovaldo/frk/2t2/HCP/';
end
subjects = {...
'118730', ...
'115320', ...
'117122', ...
};
case {'2t1'}
switch hostname
case {'marcovaldo'}
datapath = '/home/frk/2t1/HCP/';
otherwise
datapath = '/marcovaldo/frk/2t1/HCP/';
end
subjects = {...
'111312', ...
'113619', ...
'105115', ...
'110411', ...
};
otherwise
keyboard
end
if notDefined('saveDir'), savedir = fullfile('/marcovaldo/frk/Dropbox','pestilli_etal_revision',mfilename);end
if notDefined('trackingType'), trackingType = 'lmax10';end
if notDefined('hemisphere'), hemisphere = {'left','right'};end
if notDefined('plotAnatomy'), plotAnatomy = 1;end
anatomyPath = '/dev/data/anatomy/';
parietalRoiName = 'lh_MT_label_smooth3mm.nii.gz';
mtRoiName = 'rh_MT_label_smooth3mm.nii.gz';
parietalRoiName = 'lh_superiorparietal_label_smooth3mm.nii.gz';
mtRoiName = 'rh_superiorparietal_label_smooth3mm.nii.gz';
for iSbj = 1:length(subjects)
% Load the FE structure
saveDir = fullfile(savedir,subjects{iSbj});
fibergroupPath = fullfile(datapath,subjects{iSbj},'fibers');
fgFileToLoad = dir(fullfile(fibergroupPath,sprintf('*%s*2000*.pdb',trackingType)));
fname = fgFileToLoad(1).name;
fgFileToLoad = fullfile(fibergroupPath,fname);
fgFileToLoad1 = dir(fullfile(fibergroupPath,sprintf('*%s*2000*.pdb','lmax8')));
fname1 = fgFileToLoad1(1).name;
fgFileToLoad1 = fullfile(fibergroupPath,fname1);
fgFileToLoad2 = dir(fullfile(fibergroupPath,sprintf('*%s*2000*.pdb','lmax2')));
fname2 = fgFileToLoad2(1).name;
fgFileToLoad2 = fullfile(fibergroupPath,fname2);
subj_count = subj_count+1;
for ih = 1:length(hemisphere)
% Handle parallel computing
if matlabpool('size') == 0
c = parcluster;
c.NumWorkers = 12;
matlabpool(c);
end
fprintf('[%s] Loading: \n%s\n ======================================== \n\n',mfilename,fgFileToLoad)
fg = fgRead(fgFileToLoad);
fg = fgMerge(fg,fgRead(fgFileToLoad1));
fg = fgMerge(fg,fgRead(fgFileToLoad2));
% Set all the variables that depend on the hemisphere
switch hemisphere{ih}
case {'left'}
parietalRoiName = 'lh_superiorparietal_label_smooth3mm.nii.gz';
mtRoiName = 'lh_MT_label_smooth3mm.nii.gz';
axisLims = [-67 -18 -110 -40 -18 80];
vw = [-75,30];
slices = {[-18 0 0],[0 -40 0],[0 0 -14 ]};
lght = 'left';
SLaxLims = [-55 2 -120 120 -20 40 ];
histcolor{1} = [0.4 0.4 0.4];
histcolor{2} = [.6 0.4 0.4];
case {'right'}
parietalRoiName = 'rh_superiorparietal_label_smooth3mm.nii.gz';
mtRoiName = 'rh_MT_label_smooth3mm.nii.gz';
axisLims = [18 67 -110 -40 -18 80];
vw = [75,30];
slices = {[18 0 0],[0 -40 0],[0 0 -14 ]};
lght = 'right';
SLaxLims = [-2 55 -120 120 -20 40 ];
histcolor{1} = [0 0 0];
histcolor{2} = [.8 0.4 0.4];
otherwise
keyboard
end
% Load the ROIs
FS_SUBJECT = matchSubject2FSSUBJ(subjects{iSbj});
roiDir = fullfile(anatomyPath,FS_SUBJECT,'label');
mtFileName = fullfile(roiDir,mtRoiName);
parietalFileName = fullfile(roiDir,parietalRoiName);
% Find the fascicles in the connectome that touch both MT+ and parietal.
mt = dtiImportRoiFromNifti(mtFileName,[parietalFileName(1:end-7),'_ROI.mat']);
parietal = dtiImportRoiFromNifti(parietalFileName,[parietalFileName(1:end-7),'_ROI.mat']);
tic, fprintf('\n[%s] Segmenting tract from connectome... \n',mfilename)
[mt2parietalTract, ~] = feSegmentFascicleFromConnectome(fg, {mt,parietal}, {'endpoints','endpoints'}, 'mt_parietal');
% Clean the fibers by length, fibers that too long are likely to go far
% frontal and not just touch MT+ and parietal.
[~, keep] = mbaComputeFibersOutliers(mt2parietalTract,2,2);
mt2parietalTract = fgExtract(mt2parietalTract,find(keep),'keep');
toc
% Find the Coordinates of the mt-parietal tract
tic, fprintf('\n[%s] Create ROI from MT-Parietal tract... \n',mfilename)
tractRoi = dtiCreateRoiFromFibers(mt2parietalTract);toc
tic, fprintf('\n[%s] Removing fibers not going throught the tractROI... \n',mfilename)
[fg,~, ~, ~] = dtiIntersectFibersWithRoi([],'and',2,tractRoi,fg);
fg = feClipFibersToVolume(fg,tractRoi.coords,1);toc
% Build LiFE model only in this volume, fit, cull
dwiPath = fullfile(datapath,subjects{iSbj},'diffusion_data');
dwiFiles = dir(fullfile(dwiPath,sprintf('*2000_aligned_trilin*.gz')));
dwiFile = fullfile(dwiPath,dwiFiles(1).name);
dwiFileRepeat = fullfile(dwiPath,dwiFiles(1).name);
t1File = fullfile(datapath,subjects{iSbj},'anatomy','T1w_acpc_dc_restore_1p25.nii.gz');
% Directory where to save the fe structures
saveDirC = fullfile(datapath,subjects{iSbj},'connectomes');
feFileName = [parietal.name, '_', fname(1:40), '.mat'];
fe = feConnectomeInit(dwiFile,fg,feFileName,saveDirC,dwiFileRepeat,t1File);
M = feGet(fe,'mfiber');
dSig = feGet(fe,'dsigdemeaned');
fit = feFitModel(M,dSig,'bbnnls');
fe = feSet(fe,'fit',fit);clear fit
fg = feGet(fe,'fibers acpc');
[~, keepFascicles] = feSegmentFascicleFromConnectome(fg, {mt,parietal}, {'endpoints','endpoints'}, 'mt_parietal');
% Perform a virtual lesion: MT+ and parietal.
display.tract = true;
display.distributions = true;
display.evidence = true;
[SE(subj_count,ih), fig] = feVirtualLesion(fe,keepFascicles,display);
clear fe
saveFig(fig(1).fh,fullfile(saveDir,[fig(1).name, '_',hemisphere{ih}]),'eps')
saveFig(fig(2).fh,fullfile(saveDir,[fig(2).name(1:end-4), '_',hemisphere{ih}]),'eps')
if plotAnatomy
% Load the T1 file for display
t1 = niftiRead(t1File);
for ifs = 5:-1:3
% Show te new fiber group
figure(fig(ifs).fh); hold on
h = mbaDisplayBrainSlice(t1, slices{1});
h = mbaDisplayBrainSlice(t1, slices{2});
h = mbaDisplayBrainSlice(t1, slices{3});
view(vw(1),vw(2)); axis(axisLims);
%set(gcf,'Position',[0.0148 0.0148 .35 .87])
saveFig(fig(ifs).fh,fullfile(saveDir,[fig(ifs).name, '_',hemisphere{ih}]),'jpg')
close(fig(ifs).fh); drawnow
end
end
close all
matlabpool close force local
end
end
end
tic, fprintf('\n[%s] Saving results of virtual lesion... \n',mfilename)
save(fullfile(savedir,'strength_of_evidence.mat'),'SE'); toc
end % Main function
%%%%%%%%%%%%%%%%%%%%%%%
function FS_SUBJECT = matchSubject2FSSUBJ(subject)
switch subject
case {'FP_96dirs_b2000_1p5iso'}
FS_SUBJECT = 'pestilli_test';
case {'KW_96dirs_b2000_1p5iso'}
FS_SUBJECT = 'weiner';
case {'MP_96dirs_b2000_1p5iso'}
FS_SUBJECT = 'lmperry';
case {'HT_96dirs_b2000_1p5iso'}
FS_SUBJECT = 'takemura';
case {'JW_96dirs_b2000_1p5iso'}
FS_SUBJECT = 'winawer';
case {'KK_96dirs_b2000_1p5iso'}
FS_SUBJECT = 'knk';
otherwise
FS_SUBJECT = subject;
end
end
function [fh,sh] = makeBrainMap(fe,t1,slice,axLims,figName,saveDir)
% Make a map of the RMSE WITH and WITHOUT the fascicle:
coords = feGet(fe,'roi coords') + 1;
xform = feGet(fe,'xform img 2 acpc');
% Cross-validate RMSE
rmse = feGetRep(fe, 'vox rmse');
img = feReplaceImageValues(nan(feGet(fe,'map size')),rmse,coords);
maxr = 50;
% Make anifti file from the rmse
ni = niftiCreate('data',mbaNormalize(img,[0,1]), ...
'qto_xyz',xform, ...
'fname','rmse', ...
'data_type',class(img));
% Open a figure
fh = mrvNewGraphWin(figName);
% Show the anatomy with the overlay
sh = mbaDisplayOverlay(t1, ni, slice, [], 'hot');
axis(axLims)
saveMap(fh,figName,saveDir,nanmean(img(:)),nanmedian(img(:)),nanstd(img(:)),maxr)
end
%---------------------------------%
function saveMap(fh,figName,saveDir,M,m,SD,maxfd)
% This helper function saves two figures for each map and eps with onlythe
% axis and a jpg with only the brain slice.
% The two can then be combined in illustrator.
%
% First we save only the slice as jpeg.
set(gca,'fontsize',16,'ztick',[-20 0 20 40], ...
'xtick',[-50 -25 0 25 50], ...
'tickdir','out','ticklength',[0.025 0])
axis off
saveFig(fh,fullfile(saveDir,'maps',figName),'tiff')
saveFig(fh,fullfile(saveDir,'maps',figName),'png')
% Then we save the slice with the axis as
% eps. This will only generate the axis
% that can be then combined in illustrator.
axis on
grid off
title(sprintf('mean %2.2f | median %2.2f | SD %2.2f', ...
M,m,SD),'fontsize',16)
zlabel('Z (mm)','fontsize',16)
xlabel('X (mm)','fontsize',16)
cmap = colormap(hot(255));
colorbar('ytick',linspace(0,1,5),'yticklabel', ...
{linspace(0,1,5)*50}, ...
'tickdir','out','ticklength',[0.025 0],'fontsize',16)
saveFig(fh,fullfile(saveDir,'maps',figName),1)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function saveFig(h,figName,type)
% MAke sure the folder to save the figure exists
[p,f,e] = fileparts(figName);
[success,message] = mkdir(p);
if ~isempty(message), disp(sprintf('%s.',message));end
% Find out which type of figure and geenerate the proper printing command.
switch type
case {0,'jpeg','jpg'}
printCommand = (sprintf('print(%s, ''-djpeg90'',''-r500'' , ''-noui'', ''-opengl'', ''%s'')', num2str(h),figName));
case {1,'eps'}
printCommand = (sprintf('print(%s, ''-cmyk'', ''-depsc2'',''-tiff'',''-r500'' , ''-noui'', ''%s'')', num2str(h),figName));
case 'png'
printCommand = (sprintf('print(%s, ''-dpng'',''-r500'', ''%s'')', num2str(h),figName));
case 'tiff'
printCommand = (sprintf('print(%s, ''-dtiff'',''-r500'', ''%s'')', num2str(h),figName));
case 'bmp'
printCommand = (sprintf('print(%s, ''-dbmp256'',''-r500'', ''%s'')', num2str(h),figName));
otherwise
keyboard
end
% do the printing here:
fprintf('[%s] saving figure... \n%s\n',mfilename,figName);
eval(printCommand);
end