-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractValuesFromGLMtxtFile.m
238 lines (192 loc) · 8.42 KB
/
extractValuesFromGLMtxtFile.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
function [BetaVal, TVal,PVal, channelID]=extractValuesFromGLMtxtFile(hdrFileName,txtFileName, filePath)
% this function takes as input the hdr and glm output files from NIRStar and TSI, respectively and
% outputs the estimated coefficients, p-values and channel IDs
% Input:
% hdrFileName: name of .hdr file (extension included)
% txtfileName: name of glm output .txt file (extension included)
% filePath: path where both files are located
% Output:
% BetaVal/TVal/PVal:
% 3x1 structure:
% .HbO - NrChannels x NrCond (excluding rest)
% .HbR - NrChannels x NrCond (excluding rest)
% .HbT - NrChannels x NrCond (excluding rest)
% .Contrast - 4x1 structure:
% .HbO - NrChannels x NrOfContrasts
% .HbR - NrChannels x NrOfContrasts
% .HbT - NrChannels x NrOfContrasts
% .Name- ContrastNames
% Channel ID: cell matrix with dimension NrChannels x 1
% Dependencies:
% HDRFile_extractInfo.m --> extracts protocol information from hdr file
% based on trigger matrix
% Last updated : 22.07.2019
% @ author: A.B., Maastricht University
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen(fullfile(filePath,txtFileName));%[NIRx_foldername '/' hdr_name]);
tmp = textscan(fid,'%s','delimiter','\n');%This just reads every line
GLM_txt = tmp{1};
fclose(fid);
% Load hdr file
[~, ~, ~, ~, ~, ~,~, prt_info, ~, ~,~,NrOfSDD, ~]= HDRFile_extractInfo(filePath, hdrFileName);
% Extract number of tasks
NrOfTasks = size(unique(prt_info(:,2)),1)-1;% remove rest period
% Initialize variables
BetaVal.HbO = [];
BetaVal.HbR = [];
BetaVal.HbT = [];
TVal.HbO = [];
TVal.HbR = [];
TVal.HbT = [];
PVal.HbO = [];
PVal.HbR = [];
PVal.HbT = [];
TVal.Contrast.HbO = [];
TVal.Contrast.HbR = [];
TVal.Contrast.HbT = [];
PVal.Contrast.HbO = [];
PVal.Contrast.HbR = [];
PVal.Contrast.HbT = [];
for hh=1:3 % 1 = HbO; 2 = HbR; 3 = HbT
if hh==1
keyword_start = 'Model fit per channel for oxy-Hb (beta, t, p values)';
keyword_finish = 'Model fit per channel for deoxy-Hb (beta, t, p values)';
elseif hh==2
keyword_start = 'Model fit per channel for deoxy-Hb (beta, t, p values)';
keyword_finish = 'Model fit per channel for total-hb (beta, t, p values)';
else
keyword_start = 'Model fit per channel for total-hb (beta, t, p values)';
keyword_finish = 'GLM CONTRAST RESULTS';
end
% get location of coefficients of interest
tmpStart = strfind(GLM_txt,keyword_start);
indStart = find(~cellfun(@isempty,tmpStart)) + 1;
tmpFinish = strfind(GLM_txt,keyword_finish);
indFinish = find(~cellfun(@isempty,tmpFinish)) -2;
if NrOfSDD>0
% get row indices where short distance detectors indices are located & remove them
indSDD = strfind(GLM_txt(1:indFinish),'D8'); % in our case, we don't have more than 7 short distance detectors
indSDD = [find(~cellfun(@isempty,indSDD)) find(~cellfun(@isempty,indSDD))+ (NrOfSDD-1)];
indx_temp = indStart:indFinish;
toRemove = [];
for int=1:size(indSDD,1)
toRemove = [toRemove; (indSDD(int,1):indSDD(int,2))'];
end
indx = setdiff(indx_temp, toRemove); % remove SDD indices
else
indx = indStart:indFinish;
end
% initialize variables
channelID = cell(numel(indx),1);
temp_BetaVal = zeros(numel(indx),NrOfTasks);
temp_TVal = zeros(numel(indx),NrOfTasks);
temp_PVal = zeros(numel(indx),NrOfTasks);
for cc=1:numel(indx)% for all normal distance channels in the setup
temp= GLM_txt(indx(cc));
% find white spaces in array and use them to find beta coefficients
whiteSpaceInd = isspace(temp{1})';
whiteSpaceInd = whiteSpaceInd(1:end-1)- whiteSpaceInd(2:end) ;
startPeriod = find(whiteSpaceInd==1)+1;
endPeriod = find(whiteSpaceInd==-1);
matrix = [[1;startPeriod] endPeriod];
% channelID is always the first column
channelID{cc} = temp{1}(matrix(1,1):matrix(1,2)-1);
matrix = matrix(2:end,:);
% beta values are located every third value in the matrix
indxB = 1:3:size(matrix,1);
indxT = 2:3:size(matrix,1);
indxP = 3:3:size(matrix,1);
for int=1:NrOfTasks
temp_BetaVal(cc,int) = str2double(temp{1}(matrix(indxB(int),1):matrix(indxB(int),2))); % save beta values
temp_TVal(cc,int) = str2double(temp{1}(matrix(indxT(int),1):matrix(indxT(int),2))); % save beta values
temp_PVal(cc,int) = str2double(temp{1}(matrix(indxP(int),1):matrix(indxP(int),2))); % save beta values
end
end
if hh==1
BetaVal.HbO = temp_BetaVal;
TVal.HbO = temp_TVal;
PVal.HbO = temp_PVal;
elseif hh==2
BetaVal.HbR = temp_BetaVal;
TVal.HbR = temp_TVal;
PVal.HbR = temp_PVal;
else
BetaVal.HbT = temp_BetaVal;
TVal.HbT = temp_TVal;
PVal.HbT = temp_PVal;
end
%% Extract contrast-related information
if hh==1 % get oxy values
keyword_start = 'Contrast results per channel for oxy-Hb (t, p values)';
keyword_finish = 'Contrast results per channel for deoxy-Hb (t, p values)';
elseif hh==2
keyword_start = 'Contrast results per channel for deoxy-Hb (t, p values)';
keyword_finish = 'Contrast results per channel for total-hb (t, p values)';
else
keyword_start = 'Contrast results per channel for total-hb (t, p values)';
keyword_finish = '';
end
% get where coefficients of interest are shown
tmpStart = strfind(GLM_txt,keyword_start);
indStart = find(~cellfun(@isempty,tmpStart)) + 1;
if ~isempty(keyword_finish)
tmpFinish = strfind(GLM_txt,keyword_finish);
indFinish = find(~cellfun(@isempty,tmpFinish)) -2;
else
indFinish = size(GLM_txt,1);
end
if NrOfSDD>0
% get row indices where short distance detectors indices are shown & remove them
indSDD = strfind(GLM_txt(1:indFinish),['D' num2str(min(SDindx))]);
indSDD = [find(~cellfun(@isempty,indSDD)) find(~cellfun(@isempty,indSDD))+ (NrOfSDD-1)];
indx_temp = indStart:indFinish;
toRemove = [];
for int=1:size(indSDD,1)
toRemove = [toRemove; (indSDD(int,1):indSDD(int,2))'];
end
indx = setdiff(indx_temp, toRemove); % remove SDD indices
else
indx = indStart:indFinish;
end
% define #of contrasts
keyword_start = 'GLM CONTRAST RESULTS';
keyword_finish = 'Contrast results per channel for oxy-Hb (t, p values)';
tmpStart = strfind(GLM_txt,keyword_start);
indStart = find(~cellfun(@isempty,tmpStart)) + 1;
tmpFinish = strfind(GLM_txt,keyword_finish);
indFinish = find(~cellfun(@isempty,tmpFinish)) -2;
NrOfContrast = numel(indStart:indFinish);
contrastNames = GLM_txt(indStart:indFinish);
% define variables
temp_TVal = zeros(numel(indx),NrOfContrast);
temp_PVal = zeros(numel(indx),NrOfContrast);
for cc=1:numel(indx)% for all normal distance channels in the setup
temp= GLM_txt(indx(cc));
% find white spaces in array and use them to find beta coefficients
whiteSpaceInd = isspace(temp{1})';
whiteSpaceInd = whiteSpaceInd(1:end-1)- whiteSpaceInd(2:end) ;
startPeriod = find(whiteSpaceInd==1)+1;
endPeriod = find(whiteSpaceInd==-1);
matrix = [[1;startPeriod] endPeriod];
% channelID is always the first column --> not needed
matrix = matrix(2:end,:);
indxT = 1:2:size(matrix,1);
indxP = 2:2:size(matrix,1);
for int=1:NrOfContrast
temp_TVal(cc,int) = str2double(temp{1}(matrix(indxT(int),1):matrix(indxT(int),2))); % save beta values
temp_PVal(cc,int) = str2double(temp{1}(matrix(indxP(int),1):matrix(indxP(int),2))); % save beta values
end
end
if hh==1
TVal.Contrast.HbO = temp_TVal;
PVal.Contrast.HbO = temp_PVal;
elseif hh==2
TVal.Contrast.HbR = temp_TVal;
PVal.Contrast.HbR = temp_PVal;
else
TVal.Contrast.HbT = temp_TVal;
PVal.Contrast.HbT = temp_PVal;
end
TVal.Contrast.Name = contrastNames;
PVal.Contrast.Name = contrastNames;
end