forked from francopestilli/life_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_fe_mt_ips_tract.m
288 lines (246 loc) · 10.7 KB
/
s_fe_mt_ips_tract.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
function s_fe_mt_ips_tract(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
% Handle parallel computing
if matlabpool('size') == 0
c = parcluster;
c.NumWorkers = 12;
matlabpool(c);
end
% Get the base directory for the data
datapath = '/marcovaldo/frk/2t1/predator/';
subjects = {...
'FP_96dirs_b2000_1p5iso', ...
'MP_96dirs_b2000_1p5iso', ...
'KK_96dirs_b2000_1p5iso', ...
'JW_96dirs_b2000_1p5iso', ...
'HT_96dirs_b2000_1p5iso', ...
'KW_96dirs_b2000_1p5iso', ...
};
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 = true;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*.pdb',trackingType)));
fname = fgFileToLoad(1).name;
fgFileToLoad = fullfile(fibergroupPath,fname);
fgFileToLoad1 = dir(fullfile(fibergroupPath,sprintf('*%s*.pdb','lmax8')));
fname1 = fgFileToLoad1(1).name;
fgFileToLoad1 = fullfile(fibergroupPath,fname1);
fgFileToLoad2 = dir(fullfile(fibergroupPath,sprintf('*%s*.pdb','lmax2')));
fname2 = fgFileToLoad2(1).name;
fgFileToLoad2 = fullfile(fibergroupPath,fname2);
for ih = 1:length(hemisphere)
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,3,3);
fprintf('\n[%s] Found a tract with %i fibers... \n',mfilename,sum(keep))
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);
%tractRoi = dtiRoiClean(tractRoi, 12, 'dilate');toc % We smooth the ROI to enlarge the pathneighborhod fibers for visualization
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('run*.gz')));
dwiFile = fullfile(dwiPath,dwiFiles(1).name);
dwiFileRepeat = fullfile(dwiPath,dwiFiles(2).name);
t1File = fullfile(datapath,subjects{iSbj},'anatomy','t1.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(iSbj,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
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
keyboard
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, ''-djpeg80'',''-r300'' , ''-noui'', ''-opengl'', ''%s'')', num2str(h),figName));
case {1,'eps'}
printCommand = (sprintf('print(%s, ''-cmyk'', ''-depsc2'',''-tiff'',''-r300'' , ''-noui'', ''%s'')', num2str(h),figName));
case 'png'
printCommand = (sprintf('print(%s, ''-dpng'',''-r300'', ''%s'')', num2str(h),figName));
case 'tiff'
printCommand = (sprintf('print(%s, ''-dtiff'',''-r300'', ''%s'')', num2str(h),figName));
case 'bmp'
printCommand = (sprintf('print(%s, ''-dbmp256'',''-r300'', ''%s'')', num2str(h),figName));
otherwise
keyboard
end
% do the printing here:
fprintf('[%s] saving figure... \n%s\n',mfilename,figName);
eval(printCommand);
end