-
Notifications
You must be signed in to change notification settings - Fork 0
/
art_mtncorr.m
170 lines (150 loc) · 6.74 KB
/
art_mtncorr.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
function art_mtncorr(subjs, raw_dir, settings, swFiles)
%#######################################################################
%Loads variables into a configure file that can be passed on to Art
%May 2013 BMM
% Modifications made through 2017
%Enter variables per the example.cfg in /opt/bin/spm/spm8/toolbox/art
% Originally scripted in tcsh...
%
%Common errors: "?" padding for file definitions; two or more rp files that
%are not the same length as the processed files
%#######################################################################
if isempty(which('art_bmm'))
rmpath(which('art'));
art_home = fileparts(which('art_mtncorr')); %art_mtn_corr should be in ~/tools/fmri_processing_utilities
addpath(fullfile(art_home,'art'));
end
if ~exist('settings','var')
overwrite = 0;
else
overwrite = settings;
end
switch nargin
case 1
[cwd,pth_subjdirs] = file_selector(subjs); %cwd is the root of the study
case 0
[cwd,pth_subjdirs] = file_selector;
otherwise
rawDirPieces = textscan(raw_dir,'%s','Delimiter','/');
cwd = fullfile(filesep,rawDirPieces{1,1}{1:end-1});
pth_subjdirs = {cwd};
end
switch exist ('raw_dir','var')
case 1
tmp = textscan(raw_dir,'%s','Delimiter','/');
taskArray = tmp{1,1}(end);
[pth_taskdirs, taskArray] = file_selector_task(pth_subjdirs, taskArray); %I think this is duplicating a bunch of efforts
otherwise
[pth_taskdirs, taskArray] = file_selector_task(pth_subjdirs);
end
projName = textscan(cwd,'%s','Delimiter','/');
if exist('subjs','var') && strcmp(subjs,projName{1,1}{end})
projName = projName{1,1}{end-1};
else
projName = projName{1,1}{end};
end
%% Setup basics of the first-level
for iSubj = 1:length(pth_subjdirs)
pth = textscan(pth_subjdirs{iSubj},'%s','Delimiter','/');
spIxN = strfind(pth{:},projName);
spIx = find(~cellfun('isempty',spIxN)==1);
if eq(spIx, length(spIxN))
pth = pth{1,1}(2:spIx);
else
pth = pth{1,1}(2:spIx+1);
end
[proj_dir subj unk] = fileparts(strtrim(sprintf('/%s',pth{:})));
%for iTask = 1:length(taskArray);
task = pth_taskdirs.task; %stored from file_selector_task
rawDirName = pth_taskdirs.rawDir; % if "raw" exists in file structure
if isempty(glob(char(strcat(pth_subjdirs{iSubj}, filesep,task,filesep,'*_art_graphs*')))) || eq(overwrite,1); % has the ART correction already been applied?
fprintf('Processing %s\n', subj);
tmp = strfind(pth,subj); % Scan all the parts of the path to find which pieces should be put together for the raw directory path
ix = find(cellfun(@(x) ~isempty(x),tmp)); %Trying to locate the path correctly
if ix == length(pth);
raw_dir = strcat(strtrim(sprintf('/%s',pth{:})),filesep,task);
else
raw_dir = strcat(strtrim(sprintf('/%s',pth{:})),filesep,task, filesep, rawDirName);
end
rp_file = rdir([raw_dir, filesep, 'rp_','*txt']);
rf = fopen(rp_file(1,1).name);
ln = textscan(rf,'%s', 'Delimiter','\n');
fclose(rf);
find_files = rdir(strcat(raw_dir,filesep,'w','*.nii'));
findShort = cellfun(@(x) numel(x),{find_files.name}); %compare the length of all the nii's
find_files = find_files(findShort==min(findShort));
if numel(find_files) > 1
cmp_len = numel(find_files);
else
cmp_len = length(spm_vol(find_files.name));
end
if ~eq(cmp_len,length(ln{1,1}));
try
eq(cmp_len,length(ln{1,1})+4);
fprintf('Note: Dummies were manually discarded in these processed files.\n')
start_ix = 5
catch
fprintf('***** The number of rps and the length of the w files do not match... *********\nExiting.\n')
return
end
else
start_ix = 1
end
[ ~, ~, ext ] = fileparts(find_files(1).name);
if strcmp (ext, '.nii')
ftmp = {};
for jj = start_ix : cmp_len;
swFiles{1,jj} = strcat( find_files(1).name, ',', int2str(jj));
end
elseif length(find_files) < 2
fprintf('Found %d files. Incomplete scan?\n',length(find_files));
return
else
for iSwF = 1: cmp_len-4
swFiles{1,iSwF} = fullfile(find_files(iSwF).name);
end
end
if numel(swFiles) > 0
%% Load parameters
fileName = (strcat(raw_dir,filesep,subj,'_art.cfg'));
fid = fopen(fileName,'w+');
fprintf(fid, 'sessions: 1\n' );
fprintf(fid, 'global_mean: 1\n');
% global mean type (1: Standard 2: User-defined mask)
fprintf(fid, 'global_threshold: 6.0\n');
% threhsolds for outlier detection
fprintf(fid, 'motion_threshold: 2.0\n' );
fprintf(fid, 'motion_file_type: 0\n' );
% motion file type (0: SPM .txt file 1: FSL .par file 2:Siemens .txt file)
fprintf(fid, 'motion_fname_from_image_fname: 0\n' );
% 1/0: derive motion filename from data filename
fprintf(fid, 'use_diff_motion: 1\n' );
%set spm_file_out = `ls ${long_name}/results_unwarp/SPM.mat`
% location of SPM.mat file (comment this line if you do not wish to estimate number of outliers per condition)
%fprintf(fid, 'spm_file: ${spm_file_out} ' >> ${basename}_art.cfg
fprintf(fid, 'subj_dir: %s\n', strcat(filesep,fullfile(pth{:})));
fprintf(fid, 'output_dir: %s\n', raw_dir );
fprintf(fid, 'image_dir: %s\n', raw_dir ); % functional and movement data folders (comment these lines if functional/movement filenames below contain full path information)
fprintf(fid, 'motion_dir: %s\n', raw_dir );
rpIx = textscan(rp_file(1,1).name,'%s','Delimiter', '/');
rp_file = rpIx{1,1}{end};
if ~isempty(rp_file)
fprintf(fid, 'end\n\n'); %needed halfway through for ART script
%The following hoops are necessary because you need flexibility for the 3D nii's, but you also can't have too many ?'s and have any sort of search efficiency
w_file = strcat('w',rp_file(4:end-7),'???.nii'); %parsing based on "rp_" and "???.txt" holding constant
fprintf(fid, 'session 1 image %s\n', w_file); %CHECK THE NUMBER OF ???'s if you are having trouble.
fprintf(fid, 'session 1 motion %s\n\n', rp_file );
%fprintf(fid, 'output_dir: %s\n', raw_dir ); %Don't send to cfg file
% fprintf(fid, 'stats_dir: ${long_name}'
fprintf(fid, 'end\n');
end
fclose('all');
art_bmm(char(fileName)); %Almost always, errors in loading files here are due to how many question marks there are within the config file path definitions. Cannot be relative path.
close('all');
end
else
fprintf('Already evaluated: %s \n',subj);
end
end
end
%end