-
Notifications
You must be signed in to change notification settings - Fork 0
/
NINGA_ciftiwrite.m
116 lines (106 loc) · 3.91 KB
/
NINGA_ciftiwrite.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
function NINGA_ciftiwrite(varargin)
% Provides limited support for writing CIFTI files
% (surface only) using the wb_command as the backend.
%
% C = palm_ciftiwrite(filename,data,extra,wb_command)
%
% Inputs:
% - filename : Filename of the CIFTI (surface only, dtseries).
% - data : Actual data that will be saved.
% - extra : Metadata for the CIFTI.
% - wb_command : (Optional) Full path to the executable wb_command.
% - toscalar : (Optional)
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Apr/2015
% 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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Input arguments
narginchk(1,5);
filename = varargin{1};
data = varargin{2};
extra = varargin{3};
if nargin == 4 && ~ isempty(varargin{4}),
wb_command = varargin{4};
else
wb_command = 'wb_command';
end
if nargin == 5,
toscalar = varargin{5};
else
toscalar = false;
end
% Test the wb_command
try %#ok
[status,wb_output] = system(wb_command);
end
if status ~= 0,
disp(wb_output);
error('Test failed for your architecture. CIFTI files will not be written.');
end
% Convert to a GIFTI object
C = gifti([]);
F = fieldnames(extra);
for f = 1:numel(F),
if ~ strcmpi(F{f},'cifti_file_extension')
C.private.(F{f}) = extra.(F{f});
end
end
C.cdata = data;
% Adjust the size in the XML-tree
xt = xmltree(C.private.data{1}.metadata.value);
uid = find(xt,'/CIFTI/Matrix/MatrixIndicesMap');
for u = 1:numel(uid),
attr = attributes(xt,'get',uid(u));
for a = 1:numel(attr),
if strcmp(attr{a}.key,'NumberOfSeriesPoints'),
xt = attributes(xt,'set',uid(u),a,'NumberOfSeriesPoints',num2str(size(C.cdata,2)));
break;
end
end
end
C.private.data{1}.metadata.value = save(xt);
% Save the GIFTI. This will create a .gii and a .gii.dat.
save(C,strcat(filename,'.gii'),'ExternalFileBinary');
% Include the reset flags to the wb_command (thanks Matt Glasser!)
resetstr = '';
if any(strcmpi(extra.cifti_file_extension,{'dscalar','pscalar'})),
resetstr = '-reset-scalars';
elseif any(strcmpi(extra.cifti_file_extension,{'dtseries','ptseries'})),
resetstr = '-reset-timepoints 1 0';
end
% Use the Workbench to convert the GIFTI to CIFTI.
[~] = system(sprintf('%s -cifti-convert -from-gifti-ext %s %s %s',...
wb_command,strcat(filename,'.gii'),...
sprintf('%s.%s.nii',filename,extra.cifti_file_extension),resetstr));
% If the CIFTI this is to be converted from timeseries to a scalar
if toscalar && any(strcmpi(extra.cifti_file_extension,{'dtseries','ptseries'})),
[~] = system(sprintf('%s -cifti-convert-to-scalar %s ROW %s',...
wb_command,...
sprintf('%s.%s.nii',filename,extra.cifti_file_extension),...
sprintf('%s.%s.nii',filename,strrep(extra.cifti_file_extension,'tseries','scalar'))));
delete(sprintf('%s.%s.nii',filename,extra.cifti_file_extension));
end
% Delete the temporary GIFTI file.
% Note that here the extension is "dat" because it comes from the GIFTI
% I/O. In the writing function, it's "data", because it comes from the
% Workbench.
delete(strcat(filename,'.gii'));
delete(strcat(filename,'.dat'));