-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessNiiData.m
More file actions
90 lines (75 loc) · 4.1 KB
/
preprocessNiiData.m
File metadata and controls
90 lines (75 loc) · 4.1 KB
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
% Translational Neuromodeling Project, ETH Zurich
% 'Decoding moral judgements from neurotypical individuals compared to
% individuals with ASD'
%--------------------------------------------------------------------------
% authors: Stephan Boner, Alexander Hess, Nina Stumpf
% date: 2019-05-30
% version: 1.0
%--------------------------------------------------------------------------
% Customized script to preprocess fMRI data given in .nii files
% Input: String data_path specifying the location of the folders 'func' and
% 'anat' containing the functional resp. anatomical fMRI data.
%
% Mostly copied from the SPM example documented in
% http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#Chap:data:auditory
% Copyright (C) 2014 Wellcome Trust Centre for Neuroimaging, Guillaume Flandin
%==========================================================================
function preprocessNiiData(data_path)
savePath = fullfile(data_path,'preprocessed');
savePathFunc = fullfile(savePath, 'func');
savePathAnat = fullfile(savePath, 'anat');
mkdir(savePath);
mkdir(savePathFunc);
mkdir(savePathAnat);
existingFiles = [dir(fullfile(data_path,'anat'))',dir(fullfile(data_path,'func'))'];
% Initialise SPM
%--------------------------------------------------------------------------
spm('Defaults','fMRI');
spm_jobman('initcfg');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SPATIAL PREPROCESSING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = spm_select('FPList', fullfile(data_path,'func'), '^sub.*(dis).*\.nii');
a = spm_select('FPList', fullfile(data_path,'anat'), '^sub.*\.nii$');
clear matlabbatch
% Realign
%--------------------------------------------------------------------------
matlabbatch{1}.spm.spatial.realign.estwrite.data = {cellstr(f)};
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.which = [0 1];
% Coregister
%--------------------------------------------------------------------------
matlabbatch{2}.spm.spatial.coreg.estimate.ref = cellstr(spm_file(f(1,:),'prefix','mean'));
matlabbatch{2}.spm.spatial.coreg.estimate.source = cellstr(a);
% Segment
%--------------------------------------------------------------------------
matlabbatch{3}.spm.spatial.preproc.channel.vols = cellstr(a);
matlabbatch{3}.spm.spatial.preproc.channel.write = [0 1];
matlabbatch{3}.spm.spatial.preproc.warp.write = [0 1];
% Normalise: Write
%--------------------------------------------------------------------------
matlabbatch{4}.spm.spatial.normalise.write.subj.def = cellstr(spm_file(a,'prefix','y_','ext','nii'));
matlabbatch{4}.spm.spatial.normalise.write.subj.resample = cellstr(f);
matlabbatch{4}.spm.spatial.normalise.write.woptions.vox = [3 3 3];
matlabbatch{5}.spm.spatial.normalise.write.subj.def = cellstr(spm_file(a,'prefix','y_','ext','nii'));
matlabbatch{5}.spm.spatial.normalise.write.subj.resample = cellstr(spm_file(a,'prefix','m','ext','nii'));
matlabbatch{5}.spm.spatial.normalise.write.woptions.vox = [1 1 3];
% Smooth
%--------------------------------------------------------------------------
matlabbatch{6}.spm.spatial.smooth.data = cellstr(spm_file(f,'prefix','w'));
matlabbatch{6}.spm.spatial.smooth.fwhm = [6 6 6];
% Run all the configured steps
spm_jobman('run',matlabbatch);
% Move preprocessed files to separate folder
currentlyExistingFilesAnat = dir(fullfile(data_path,'anat'))';
for n=1:numel(currentlyExistingFilesAnat)
if ~contains([existingFiles.name],currentlyExistingFilesAnat(n).name)
movefile(fullfile(currentlyExistingFilesAnat(n).folder,currentlyExistingFilesAnat(n).name),savePathAnat);
end
end
currentlyExistingFilesFunc = dir(fullfile(data_path,'func'))';
for n=1:numel(currentlyExistingFilesFunc)
if ~contains([existingFiles.name],currentlyExistingFilesFunc(n).name)
movefile(fullfile(currentlyExistingFilesFunc(n).folder,currentlyExistingFilesFunc(n).name),savePathFunc);
end
end
end