-
Notifications
You must be signed in to change notification settings - Fork 0
/
NINGA_miscread.m
308 lines (268 loc) · 11.9 KB
/
NINGA_miscread.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
function X = NINGA_miscread(filename,useniiclass,tmppath)
% Read various scalar data formats based on the file extension.
%
% X = NINGA_miscread(filename,useniiclass,tmppath);
%
% filename : File to be read.
% useniiclass : True/False. Use the NIFTI class when reading
% NIFTI files. It requires less memory.
% tmppath : For some file types, indicate whether to store
% temporary data.
%
% X is a struct that contains the fields:
% X.filename : Contains the name of the file.
% X.readwith : This tells which program or function was used
% to read the data. This is useful when saving the
% data back, to use a compatible function.
% X.data : Array with the actual data. The size can vary
% according to what was read.
% X.extra : Contain extra information, depending on the kind
% of data that was read and the function or
% program used for reading.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Aug/2013 (first version)
% Dec/2015 (this version)
% http://brainder.org
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PALM -- Permutation Analysis of Linear Models
% Copyright (C) 2015 Anderson M. Winkler
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Check if the file actually exists before doing anything else
if ~ exist(filename,'file')
error('File not found: %s',filename);
end
% Store the filename, in case there is a need to overwrite this later
X.filename = filename;
% Take the file extension and try to load accordingly
[~,fnam,fext] = fileparts(X.filename);
fext = strdotsplit(strcat(fnam,fext));
% Some formats use external i/o functions that use random numbers. Save
% current state of the random number generator, then restore at the end.
state = rng;
switch lower(fext{end}),
case 'txt',
% Read a generic text file
X.readwith = 'textscan';
fid = fopen(X.filename);
X.data = textscan(fid,'%s');
X.data = X.data{1};
fclose(fid);
X.extra = [];
case 'csv',
% Read a CSV file. It has to contain numeric values only.
% The command 'csvwrite' is a frontend to 'dlmwrite', which calls
% 'textscan', which on its turn has a limitation of 100k columns.
% Using 'load' bypass this issue.
X.readwith = 'load';
X.data = load(X.filename);
X.extra = [];
case {'mat','con','fts','grp'},
% Read an FSL "VEST" file.
X.readwith = 'vestread';
[X.data,X.extra.PPH] = NINGA_vestread(X.filename);
case 'mset',
% Set of matrices
X.readwith = 'mset';
X.data = NINGA_msetread(X.filename);
case 'gz',
% Handle (or not) a gzipped NIFTI or CIFTI file.
if strcmpi(fext{end-1},'nii'),
if any(strcmpi(fext{end-2},{'dscalar','dtseries','dconn','dlabel','ptseries','merge'})),
% Until CIFTI migrates to HDF5, users will have to uncompress manually.
error('CIFTI files must be uncompressed before they can be read. Use gunzip and try again.');
else
% Read as NIFTI proper (not CIFTI)
extern = NINGA_checkprogs;
if useniiclass,
error([
'Reading of gzipped NIFTI files (.nii.gz) is currently disabled\n' ...
'If you are sure that your gzipped files, once uncompressed, are not\n' ...
'too large to exceed memory limits, you can include the option ''-noniiclass''\n' ...
'in the command line. Otherwise, uncompress manually and try again using\n' ...
'as input the .nii files instead.\n' ...
'File: %s'],X.filename);
else
if extern.fs, % Read with FreeSurfer
X.readwith = 'fs_load_nifti';
X.extra.hdr = load_nifti(X.filename);
X.data = X.extra.hdr.vol;
X.extra.hdr.vol = [];
elseif extern.fsl, % Read with FSL
X.readwith = 'fsl_read_avw';
[X.data,X.extra.dims,X.extra.voxsize, ...
X.extra.bpp,X.extra.endian] = read_avw(X.filename);
else
error([
'Neither FreeSurfer or FSL were found.\n' ...
'To use this data you must do one of:\n' ...
'- Make sure FreeSurfer is correctly installed and configured,\n' ...
' and that your ''FREESURFER_HOME'' environmental variable is\n' ...
' properly set;\n' ...
'- Make sure FSL is correctly installed and configured,\n' ...
' and that your ''FSLDIR'' environmental variable is\n' ...
' properly set;\n' ...
'- If you do not have FSL or FS, uncompress the .gz file\n' ...
' and try again.\n' ...
'File: %s'],X.filename);
end
end
end
else
error('Unrecognised format with extension %s%s',fext0,fext);
end
case {'nii','hdr','img'},
% Check for external loaders
extern = NINGA_checkprogs;
% Handle NIFTI and CIFTI files.
if strcmpi(fext{end},'nii') && ...
any(strcmpi(fext{end-1},{'dscalar','dtseries','dconn','dlabel','ptseries','merge','pscalar'})),
% Read a CIFTI file.
if extern.wb_command,
X.readwith = 'wb_command';
[X.data,X.extra] = NINGA_ciftiread(X.filename,pwd);
X.data = X.data';
X.extra.cifti_file_extension = fext{end-1};
else
error('Currently cannot read/write CIFTI files without the HCP Workbench.')
end
else
% Read a NIFTI file. Note that this will should not
% be used for ANALYZE.
if useniiclass,
X.readwith = 'nifticlass';
X.extra = nifti(X.filename);
X.data = X.extra.dat;
else
if extern.fs, % Read with FreeSurfer
X.readwith = 'fs_load_nifti';
X.extra.hdr = load_nifti(X.filename);
X.data = X.extra.hdr.vol;
X.extra.hdr.vol = [];
elseif extern.spm, % Read with SPM
X.readwith = 'spm_spm_vol';
X.extra = spm_vol(X.filename);
X.data = spm_read_vols(X.extra);
elseif extern.fsl, % Read with FSL
X.readwith = 'fsl_read_avw';
[X.data,X.extra.dims,X.extra.voxsize, ...
X.extra.bpp,X.extra.endian] = read_avw(X.filename);
elseif extern.nii, % Read with the NIFTI toolbox
X.readwith = 'nii_load_nii';
X.extra = load_nii(X.filename);
X.data = X.extra.img;
X.extra.img = [];
else
error([
'No FSL, FreeSurfer or SPM were found.\n' ...
'To use this data you must have one of these\n' ...
'installed and correctly configured.\n' ...
'File: %s\n'],X.filename);
end
end
end
case {'dpv','dpf','dpx'},
% Read a DPV/DPF file, in ASCII
X.readwith = 'dpxread';
[X.data,X.extra.crd,X.extra.idx] = NINGA_dpxread(X.filename);
case 'srf',
% Read a SRF file, in ASCII
X.readwith = 'srfread';
[X.data.vtx,X.data.fac] = NINGA_srfread(X.filename);
case {'area','avg_curv','crv','curv', ...
'h','k','jacobian_white','mid', ...
'sulc','thickness','volume'},
% Read a FreeSurfer curvature file
extern = NINGA_checkprogs;
if extern.fs,
X.readwith = 'fs_read_curv';
[X.data,X.extra.fnum] = read_curv(X.filename);
else
error([
'FreeSurfer was not found. To use this data, make sure\n' ...
'that FreeSurfer is correctly installed and configured, and\n' ...
'that your ''FREESURFER_HOME'' environmental variable is\n' ...
'properly set.\n' ...
'File: %s\n'],X.filename);
end
case {'inflated','nofix','orig','pial', ...
'smoothwm','sphere','reg','white','white_reg'},
% Read a FreeSurfer surface file
extern = NINGA_checkprogs;
if extern.fs,
X.readwith = 'fs_read_surf';
[X.data.vtx,X.data.fac] = read_surf(X.filename);
X.data.fac = X.data.fac + 1;
else
error([
'FreeSurfer was not found. To use this data, make sure\n' ...
'that FreeSurfer is correctly installed and configured, and\n' ...
'that your ''FREESURFER_HOME'' environmental variable is\n' ...
'properly set.\n' ...
'File: %s\n'],X.filename);
end
case {'mgh','mgz'},
% Read a FreeSurfer MGH/MGZ file
extern = NINGA_checkprogs;
if extern.fs,
X.readwith = 'fs_load_mgh';
[X.data,X.extra.M,X.extra.mr_parms,X.extra.volsz] = load_mgh(X.filename);
else
error([
'FreeSurfer was not found. To use this data, make sure\n' ...
'that FreeSurfer is correctly installed and configured, and\n' ...
'that your ''FREESURFER_HOME'' environmental variable is\n' ...
'properly set.\n' ...
'File: %s\n'],X.filename);
end
case 'gii',
% Read a GIFTI file (no mapped file arrays)
NINGA_checkprogs; % ensure GIFTI toolbox in the path
X.readwith = 'gifti';
gii = gifti(X.filename);
if isfield(gii,'cdata'),
X.data = gii.cdata';
end
if isfield(gii,'vertices') && isfield(gii,'faces'),
X.data.vtx = gii.vertices;
X.data.fac = gii.faces;
if isfield(gii,'mat'),
vtx = [X.data.vtx ones(size(X.data.vtx,1),1)];
vtx = vtx * gii.mat;
X.data.vtx = vtx(:,1:3);
end
end
for d = numel(gii.private.data):-1:1,
gii.private.data{d}.data = [];
end
X.extra = gii.private;
otherwise
error('File extension %s not known. Data cannot be loaded\n',fext{end});
end
% Restore the state of the random number generator.
rng(state);
% ==============================================================
function spl = strdotsplit(str)
% Split a string at the dots (.).
idx = find(str == '.');
idxb = [1 idx+1];
idxe = [idx-1 numel(str)];
spl = cell(numel(idxb),1);
for s = 1:numel(idxb),
spl{s} = str(idxb(s):idxe(s));
end