-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormalize_Pipeline.m
98 lines (90 loc) · 4.34 KB
/
Normalize_Pipeline.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
function Normalize_Pipeline(smri_directory,varargin)
%Notes CYF 03132018
%Normalization pipeline that uses VBM8 to calculate transformation matrices
%from native to MNI space, then applies to multiple images co-registered to
%native T1
warning off all;
try
tarfile=gunzip(spm_select('FPList',smri_directory,'.*tar.gz$'));
untar(spm_select('FPList',smri_directory,'.*.tar$'),smri_directory);
for x=1:size(varargin,2)
cfolder=char(deblank(varargin(x)));
cd(cfolder);
disp(sprintf('Currently processing folder %s\n',cfolder));
files=spm_select('FPList',cfolder,'.*');
doscmd=sprintf('gdcm gdcminfo %s',files(1,:));[status,cmdout]=dos(doscmd);
%Convert enhanced dicom to standard dicom
if strfind(cmdout,'Enhanced')
display('Enhanced DICOM detected. Converting to standard DICOMS...\n');
fullfolder=fullfile(cfolder,'Standard');
if exist(fullfolder,'dir')~=7
mkdir(fullfolder);
end
doscmd=sprintf('emf2sf --out-dir %s %s',fullfolder,files(1,:));
dos(doscmd);
cfolder=fullfolder;
end
%Get header info & convert to NIFTI
disp(sprintf('Converting DICOMs to NIFTI in %s...\n',cfolder));
files=spm_select('FPList',cfolder,'.*');
display('Reading DICOM headers...\n');
hdr=spm_dicom_headers(files);
display('Done!\n');
spm_dicom_convertP50(hdr,'all','flat','nii');
files=spm_select('FPList',cfolder,'.*.nii');
V=spm_vol(deblank(files(1,:)));
imreso=round(abs(diag(V.mat)));imreso=imreso(1:3);
if size(files,1)>1
for a=1:size(files,1)
seplocs=strfind(files(a,:),'-');
fnum=files(a,seplocs(end):end);
fname=sprintf('%s_04%d_%s%s',deblank(hdr{1,1}.PatientID),hdr{1,1}.SeriesNumber,strrep(hdr{1,1}.SeriesDescription,' ','_'),fnum);
fname=strrep(fname,'(','-');fname=strrep(fname,')','');
movefile(files(a,:),fullfile(cfolder,fname),'f');
end
else
fname=sprintf('%s_04%d_%s.nii',deblank(hdr{1,1}.PatientID),hdr{1,1}.SeriesNumber,strrep(hdr{1,1}.SeriesDescription,' ','_'));
fname=strrep(fname,'(','-');fname=strrep(fname,')','');
movefile(files,fullfile(cfolder,fname),'f');
end
load(which('Normpipecoreg.mat'));
files=spm_select('FPList',cfolder,'.*.nii');
matlabbatch{1,1}.spm.spatial.coreg.estwrite.ref{1}=spm_select('FPList',smri_directory,'^head.nii$');
matlabbatch{1,1}.spm.spatial.coreg.estwrite.source{1}=files(1,:);
if size(files,1)>1,matlabbatch{1,1}.spm.spatial.coreg.estwrite.other=cellstr(files(2:end,:));
else matlabbatch{1,1}.spm.spatial.coreg.estwrite.other{1}=[];
end
try
spm_jobman('initcfg');
spm_jobman('run',matlabbatch);
end
warpimages=spm_select('FPList',cfolder,'^r.*.nii');
mkdir(fullfile(cfolder,'Normalized'));
outputfolder=fullfile(cfolder,'Normalized');
load(which('NormpipeApplyMat.mat'));
matlabbatch{1,1}.spm.tools.vbm8.tools.defs.field1{1}=spm_select('FPList',smri_directory,'^anat2tpl.warp.field.nii');
matlabbatch{1,1}.spm.tools.vbm8.tools.defs.images=cellstr(warpimages);
try
spm_jobman('initcfg');
spm_jobman('run',matlabbatch);
end
files=spm_select('FPList',cfolder,'^wr.*.nii');
for a=1:size(files,1)
movefile(deblank(files(a,:)),outputfolder,'f');end
if ~isempty(spm_select('FPList',smri_directory,'^wrp1.*nii$'))
[pth,nm,ext]=fileparts(spm_select('FPList',smri_directory,'^wrp1.*nii$'));
if ~isequal(ones(3,1),imreso)
spm_smooth(spm_select('FPList',smri_directory,'^wrp1.*nii$'),fullfile(outputfolder,['s',nm,ext]),imreso');
else
copyfile(spm_select('FPList',smri_directory,'^wrp1.*nii$'),outputfolder,'f');end
end
end
catch err
disp(err.message);
disp(err.identifier);
for k=1:length(err.stack)
fprintf('In %s at %d\n',err.stack(k).file,err.stack(k).line);
end
%exit;
end
return;