-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcat_install_atlases.m
110 lines (95 loc) · 3.32 KB
/
cat_install_atlases.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
function cat_install_atlases
% Convert CAT12 atlas files (csv) and add Dartel atlas labels to spm12
% atlas folder (xml)
%
% ______________________________________________________________________
%
% Christian Gaser, Robert Dahnke
% Structural Brain Mapping Group (https://neuro-jena.github.io)
% Departments of Neurology and Psychiatry
% Jena University Hospital
% ______________________________________________________________________
% $Id$
spm_dir = spm('dir');
atlas_dir = fullfile(spm_dir,'atlas');
[ST, RS] = mkdir(atlas_dir);
if ST
[csv_files, n] = cat_vol_findfiles(cat_get_defaults('extopts.pth_templates'), '*.csv');
for i = 1:n
csv_file = deblank(csv_files{i});
csv = cat_io_csv(csv_file,'','',struct('delimiter',';'));
[pth,nam] = spm_fileparts(csv_file);
xml_file = fullfile(atlas_dir, ['labels_cat12_' nam '.xml']);
old_xml_file = fullfile(atlas_dir, ['labels_dartel_' nam '.xml']);
create_spm_atlas_xml(xml_file, csv);
atlas_file = fullfile(pth,[nam '.nii']);
new_atlas_name = ['cat12_' nam '.nii'];
try
copyfile(atlas_file,fullfile(atlas_dir,new_atlas_name),'f');
if exist(old_xml_file,'file'), delete(old_xml_file); end
fprintf('Install %s\n',xml_file);
catch
disp('Writing error: Please check file permissions.');
end
end
else
error(RS);
end
% this is maybe not enough, to update the file in SPM functions
% you may need to remove the old files and finish SPM, update and restart SPM
spm_atlas('list','installed','-refresh');
fprintf('\nUse atlas function in SPM Results or context menu in orthogonal view (via right mouse button): Display|Labels\n');
function create_spm_atlas_xml(fname,csvx,opt)
% create an spm12 compatible xml version of the csv data
if ~exist('opt','var'), opt = struct(); end
[pp,ff,ee] = spm_fileparts(fname);
% remove prepending name part
if ~isempty(strfind(ff,'labels_cat12_'))
ff = ff(length('labels_cat12_')+1:end);
end
def.name = ff;
def.desc = '';
def.url = '';
def.ver = cat_version;
def.lic = 'CC BY-NC';
def.cor = 'MNI152 NLin 2009c Asym';
def.type = 'Label';
def.images = ['cat12_' ff '.nii'];
opt = cat_io_checkinopt(opt,def);
xml.header = [...
'<?xml version="1.0" encoding="ISO-8859-1"?>\n' ...
'<!-- This is a temporary file format -->\n' ...
' <atlas version="' opt.ver '">\n' ...
' <header>\n' ...
' <name>' opt.name '</name>\n' ...
' <version>' opt.ver '</version>\n' ...
' <description>' opt.desc '</description>\n' ...
' <url>' opt. url '</url>\n' ...
' <licence>' opt.lic '</licence>\n' ...
' <coordinate_system>' opt.cor '</coordinate_system>\n' ...
' <type>' opt.type '</type>\n' ...
' <images>\n' ...
' <imagefile>' opt.images '</imagefile>\n' ...
' </images>\n' ...
' </header>\n' ...
' <data>\n' ...
];
xml.data = '';
% find ROIname in header
ind_name = find(strcmp(csvx(1,:),'ROIname'));
for di = 2:size(csvx,1)
xml.data = sprintf('%s%s\n',xml.data,sprintf([' <label><index>%d</index>'...
'<name>%s</name></label>'],...
csvx{di,1}, csvx{di,ind_name}));
end
xml.footer = [ ...
' </data>\n' ...
'</atlas>\n' ...
];
fid = fopen(fname,'w');
if fid >= 0
fprintf(fid,[xml.header,xml.data,xml.footer]);
fclose(fid);
else
fprintf('Error while writing %s. Check file permissions.\n',fname);
end