forked from francopestilli/life_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_fe_make_all_freesurfer_labels_into_rois.m
178 lines (148 loc) · 5.81 KB
/
s_fe_make_all_freesurfer_labels_into_rois.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
function s_fe_make_all_freesurfer_labels_into_rois(hemisphere)
%
% This script creates all the freesurfer lables from parcellation files and
% creates corresponding nifti and mat ROI compatible with mrDiffusion.
%
% Copyright by Franco Pestilli Stanford University 2014
% Get the base directory for the data
subjects = {...
'KK_96dirs_b2000_1p5iso' ...
'JW_96dirs_b2000_1p5iso', ...
'KW_96dirs_b2000_1p5iso', ...
'MP_96dirs_b2000_1p5iso', ...
'HT_96dirs_b2000_1p5iso', ...
'FP_96dirs_b2000_1p5iso', ...
};
if notDefined('annotationFileName')
annotationFileName = {'aparc','aparc.a2009s'};
end
fsSubjectsDir = getenv('SUBJECTS_DIR');
clobber = false;
for iSbj = 1:length(subjects)
if ~isdir(fullfile(fsSubjectsDir,subjects{iSbj}))
fs_subject = matchSubject2FSSUBJ(subjects{iSbj});
else fs_subject = subjects{iSbj};
end
fsSubjectDir = fullfile(fsSubjectsDir,fs_subject);
% Create all the necessary label files
for ia = 1:length(annotationFileName)
fs_annotationToLabelFiles(fs_subject,annotationFileName{ia},[],fsSubjectsDir);
end
% File all the label ROIs for this subject
labelFileNames = dir(fullfile(fsSubjectDir,'label','*.label'));
labelRoiName = cell(length(labelFileNames),1);
niftiRoiFullPath = cell(length(labelFileNames),1);
matRoiFullPath = cell(length(labelFileNames),1);
for il = 1:length(labelFileNames)
labelRoiName{il} = labelFileNames(il).name;
niftiRoiName = labelRoiName{il};
niftiRoiName(niftiRoiName=='.') = '_';
niftiRoiFullPath{il} = fullfile(fsSubjectDir,'label', niftiRoiName);
matRoiFullPath{il} = [fullfile(fsSubjectDir,'label', niftiRoiName),'_smooth3mm_ROI.mat'];
end
for il = 1:length(labelFileNames)
if ~(exist([niftiRoiFullPath{il},'_smooth3mm.nii.gz'],'file')==2) || clobber
fs_labelFileToNiftiRoi(fs_subject,labelRoiName{il},niftiRoiFullPath{il},labelFileNames(il).name(1:2),[],[],fsSubjectsDir);
else
fprintf('[%s] Found ROI, skipping: \n%s\n',mfilename,niftiRoiFullPath{il})
end
if ~(exist([matRoiFullPath{il}],'file')==2) || clobber
dtiImportRoiFromNifti([niftiRoiFullPath{il},'_smooth3mm.nii.gz'], matRoiFullPath{il});
else
fprintf('[%s] Found ROI, skipping: \n%s\n',mfilename,niftiRoiFullPath{il})
end
end
end
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, ''-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