-
Notifications
You must be signed in to change notification settings - Fork 20
/
DOTHUB_runHomerPrepro.m
244 lines (225 loc) · 8.54 KB
/
DOTHUB_runHomerPrepro.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function [prepro, preproFileName] = DOTHUB_runHomerPrepro(nirsFileName,cfgFileName,variableToReconstruct)
% Runs Homer processing stream defined in cfgFileName on nirs file
% nirsFileName, and outputs relevant results into a prepro file. The output
% variable to be reconstructed is specified by 'variableToReconstruct'
% this script was hacked out of Homer2 functions, written by Jay Dubb at
% MGH. See Homer2 https://www.nitrc.org/projects/homer2/
% ####################### INPUTS ##########################################
%
% nirsFileName : Path to .nirs file.
%
% cfgFileName : Path to .cfg file.
%
% variableToReconstruct : 'full' or 'HRF' (default 'HRF'). If 'full',
% the dod output from the .cfg file processing
% stream (i.e. the full time course after
% filter/motion correction or whatever) is
% saved into prepro.dod (which is the variable
% actually reconstructed downstream in the
% DOTHUB pipeline). If 'HRF' is selected, the
% dcAvg variable is converted back to dod and
% this is saved into prepro.dod. dcAvg and
% dcStd are also saved
%
% ####################### OUTPUTS #########################################
%
% prepro : The prepro stucture
%
% preproFileName : The full pathname of the resulting prepro file
%
% ####################### Dependencies ####################################
% #########################################################################
% RJC, UCL, April 2020
fprintf('################# Running DOTHUB_runHomerPrepro ##################\n');
% MANAGE VARIABLES
% #########################################################################
[pathstr, name, ext] = fileparts(nirsFileName);
if isempty(ext) || ~strcmpi(ext,'.nirs')
ext = '.nirs';
end
if isempty(pathstr) %Enforce full pathname
pathstr = pwd;
end
nirsFileName = fullfile(pathstr,[name ext]);
[pathstr, name, ext] = fileparts(cfgFileName);
if isempty(ext) || ~strcmpi(ext,'.cfg')
ext = '.cfg';
end
if isempty(pathstr) %Enforce full pathname
pathstr = pwd;
end
cfgFileName = fullfile(pathstr,[name ext]);
if exist('variableToReconstruct','var')
ind = find(strcmpi(variableToReconstruct,{'full','hrf'}));
if isempty(ind)
error('The variableToReconstruct input must be ''full'' or ''hrf''.');
end
else
variableToReconstruct = 'HRF';
end
if strcmpi(variableToReconstruct,'full')
fullFlag = 1;
else
fullFlag = 0;
end
% LOAD DATA
% #########################################################################
% Load .nirs
hmr = load(nirsFileName,'-mat');
% Overwrite hmr.SD with hmr.SD3D to match downstream Homer2 function calls
SD2D = hmr.SD;
hmr.SD = hmr.SD3D;
% Load .cfg
fid = fopen(cfgFileName,'r');
[procInput, ~] = parseProcessOpt(fid);
fclose(fid);
% RUN STREAM
% #########################################################################
paramOut = {};
fcallList = {};
hwait = waitbar(0, 'Processing...' );
flagNoRun = 0;
for iFunc = 1:procInput.procFunc.nFunc
wb = waitbar( iFunc/procInput.procFunc.nFunc, hwait, sprintf('Processing... %s',procInput.procFunc.funcName{iFunc}) );
% Extract input arguments from hmr
argIn = parseProcessFuncArgsIn(procInput.procFunc.funcArgIn{iFunc});
for ii = 1:length(argIn)
if ~exist(argIn{ii},'var')
if isfield(hmr,argIn{ii})
eval(sprintf('%s = hmr.%s;',argIn{ii},argIn{ii}));
else
if strcmpi(argIn{ii},'tIncMan')
tIncMan = ones(length(hmr.t),1); %RJC addition
else
eval(sprintf('%s = [];',argIn{ii})); % if variable doesn't exist and not in hmr then make it empty DAB 11/8/11
end
end
end
end
% parse input parameters
p = [];
sargin = '';
sarginVal = '';
for iP = 1:procInput.procFunc.nFuncParam(iFunc)
if ~procInput.procFunc.nFuncParamVar(iFunc)
p{iP} = procInput.procFunc.funcParamVal{iFunc}{iP};
else
p{iP}.name = procInput.procFunc.funcParam{iFunc}{iP};
p{iP}.val = procInput.procFunc.funcParamVal{iFunc}{iP};
end
if length(procInput.procFunc.funcArgIn{iFunc})==1 & iP==1
sargin = sprintf('%sp{%d}',sargin,iP);
if isnumeric(p{iP})
if length(p{iP})==1
sarginVal = sprintf('%s%s',sarginVal,num2str(p{iP}));
else
sarginVal = sprintf('%s[%s]',sarginVal,num2str(p{iP}));
end
elseif ~isstruct(p{iP})
sarginVal = sprintf('%s,%s',sarginVal,p{iP});
else
sarginVal = sprintf('%s,[XXX]',sarginVal);
end
else
sargin = sprintf('%s,p{%d}',sargin,iP);
if isnumeric(p{iP})
if length(p{iP})==1
sarginVal = sprintf('%s,%s',sarginVal,num2str(p{iP}));
else
sarginVal = sprintf('%s,[%s]',sarginVal,num2str(p{iP}));
end
elseif ~isstruct(p{iP})
sarginVal = sprintf('%s,%s',sarginVal,p{iP});
else
sarginVal = sprintf('%s,[XXX]',sarginVal);
end
end
end
% set up output format
sargout = procInput.procFunc.funcArgOut{iFunc};
for ii=1:length(procInput.procFunc.funcArgOut{iFunc})
if sargout(ii)=='#'
sargout(ii) = ' ';
end
end
% call function
fcall = sprintf( '%s = %s%s%s);', sargout, ...
procInput.procFunc.funcName{iFunc}, ...
procInput.procFunc.funcArgIn{iFunc}, sargin );
if flagNoRun==0
try
eval( fcall );
catch ME
msg = sprintf('Function %s generated ERROR at line %d: %s', procInput.procFunc.funcName{iFunc}, ME.stack(1).line, ME.message);
menu(msg,'OK');
close(hwait);
assert(logical(0), msg);
end
end
fcallList{end+1} = sprintf( '%s = %s%s%s);', sargout, ...
procInput.procFunc.funcName{iFunc}, ...
procInput.procFunc.funcArgIn{iFunc}, sarginVal );
% parse output parameters
foos = procInput.procFunc.funcArgOut{iFunc};
% remove '[', ']', and ','
for ii=1:length(foos)
if foos(ii)=='[' | foos(ii)==']' | foos(ii)==',' | foos(ii)=='#'
foos(ii) = ' ';
end
end
% get parameters for Output to hmr.procResult
lst = strfind(foos,' ');
lst = [0 lst length(foos)+1];
param = [];
for ii=1:length(lst)-1
foo2 = foos(lst(ii)+1:lst(ii+1)-1);
lst2 = strmatch( foo2, paramOut, 'exact' );
idx = strfind(foo2,'foo');
if isempty(lst2) & (isempty(idx) || idx>1) & ~isempty(foo2)
paramOut{end+1} = foo2;
end
end
end
delete(wb);
% Output variables for reconstruction ###################################
%Force MeasListAct to be the same across wavelengths
SD3D = DOTHUB_balanceMeasListAct(hmr.SD3D);
if fullFlag %Full timecourse is to be reconstructed
dodRecon = dod;
tRecon = t;
if ~exist(dcAvg,'var') %User may have calculated HRFs but want to recon
%full timecourse. Still save HRF is exists, else
%parse empty (so I only need one writePREPRO
%line
dcAvg = [];
end
if ~exist(dcAvgStd,'var')
dcAvgStd = [];
end
if ~exist(tHRF,'var')
tHRF = [];
end
else %HRF is to be reconstructed
%Find the DPF values used in cfg
funcInd = find(strcmpi(procInput.procFunc.funcName,'hmrOD2Conc'));
DPFs = procInput.procFunc.funcParamVal{funcInd}{:};
dodRecon = DOTHUB_hmrConc2OD(dcAvg,SD3D,DPFs);
tRecon = tHRF;
if isfield(hmr,'CondNames')
condNames = hmr.CondNames;
else
condNames = {};
end
end
% USE CODE SNIPPET FROM DOTHUB_writePREPRO to define filename and logData
[pathstr, name, ~] = fileparts(nirsFileName);
ds = datestr(now,'yyyymmDDHHMMSS');
preproFileName = fullfile(pathstr,[name '.prepro']);
logData(1,:) = {'Created on: '; ds};
logData(2,:) = {'Derived from data: ', nirsFileName};
logData(3,:) = {'Pre-processed using:', cfgFileName};
%(preproFileName,logData,dod,tDOD,SD3D,s,dcAvg,dcAvgStd,tHRF)
%Convert to uM
dcAvg = 1e6*dcAvg;
dcAvgStd = 1e6*dcAvgStd;
[prepro, preproFileName] = DOTHUB_writePREPRO(preproFileName,logData,dodRecon,tRecon,SD3D,hmr.s,dcAvg,dcAvgStd,tHRF,condNames,SD2D);