-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetstatechain.m
269 lines (234 loc) · 9.67 KB
/
getstatechain.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
function getstatechain(file, msBandwidth, onPeakNo, dbPeakNo, expStates)
% This function will find list of localizations and noise-free video
% mat data to extract a list of temporal barcodes for each localization
%
% file should contain full name of video with its extension (eg. AVI)
%
% msBandwidth is the width for meanshift clustering (eg. 50). Higher
% the value lesser the number of peaks and vice versa
%
% onPeakNo is the minimum number of on peaks required for signal
% consideration (eg. 10).
%
% dbPeakNo is the minimum number of double-blink peaks required for signal
% consideration (eg. 5).
%
% expStates is the number of state expected in the signal
fileName = strsplit(file, '.');
addpath('lib/meanshift/')
% Check if temporal barcode data in .mat format exists
if ~exist(strcat('tmp/brcd/', fileName{1}, '.mat'), 'file')
fprintf('cannot find noise-free video data in tmp folder\n');
return
end
% Check if localizations list in .mat format exists
if ~exist(strcat('tmp/pnts/', fileName{1}, '.mat'), 'file')
fprintf('cannot find localizations lists in tmp folder\n');
return
end
localizationsData = load(strcat('tmp/pnts/', fileName{1}, '.mat'));
localizations = localizationsData.localizationList;
filtLocalization = transpose(mat2cell(localizations, ...
ones(1, size(localizations, 1))));
fileData = load(strcat('tmp/brcd/', fileName{1}, '.mat'));
filtData = fileData.filtBarcodesList;
rawData = fileData.tempBarcodesList;
% apply cell function to generate state chain, remove single state sigs
fprintf('Applying mean-shift clustering algorithm for state chain\n');
threshSig = cellfun(@(x, y) applythreshold(x, y, msBandwidth, expStates), ...
filtData, rawData, 'UniformOutput',false);
filtData(cellfun(@(x) any(isnan(x)),threshSig)) = [];
filtLocalization(cellfun(@(x) any(isnan(x)),threshSig)) = [];
threshSig(cellfun(@(x) any(isnan(x)),threshSig)) = [];
fprintf('Filtered %d/%d signals using mean-shift\n', ...
length(rawData)-length(threshSig), length(rawData));
% filter based on number of signal peaks in the thresholded signal
fprintf('Applying peak based filtering to remove non-specific binding\n');
filt1 = cellfun(@(x, y) applypeakfilter(x, y, onPeakNo, dbPeakNo), ...
threshSig, filtData, 'UniformOutput',false);
filtData(cellfun(@(x) any(isnan(x)),filt1)) = [];
filtLocalization(cellfun(@(x) any(isnan(x)),filt1)) = [];
filt1(cellfun(@(x) any(isnan(x)),filt1)) = [];
fprintf('Filtered %d/%d signals using number of peaks\n', ...
length(threshSig)-length(filt1), length(threshSig));
% FINAL FILTER- human supervision to remove convoluted signals
fprintf('Applying human filter to select final set of %d signals\n',...
length(filt1));
filt2 = cellfun(@(x, y) applyhumanfilter(x, y), ...
filt1, filtData, 'UniformOutput',false);
filtData(cellfun(@(x) any(isnan(x)),filt2)) = [];
filtLocalization(cellfun(@(x) any(isnan(x)),filt2)) = [];
filt2(cellfun(@(x) any(isnan(x)),filt2)) = [];
fprintf('Selected %d signals for final analysis\n', length(filt2));
stateChain = filt2;
temporalBarcode = filtData;
localizations = filtLocalization;
% save filtered signals before generating stats
save(strcat('tmp/st_chn/', fileName{1}), 'stateChain', 'localizations',...
'temporalBarcode', '-v7.3');
end
function thrshBarcode = applythreshold(temporalBarcode, raw, msBandwidth, expStates)
% threshold signal using a bandwidth value and bimodal distribution
[meanOfPeaks, thrshBarcode, ~] = HGMeanShiftCluster(...
temporalBarcode, msBandwidth, 'gaussian', 0);
% remove full-noise data with only 1 peak
if length(meanOfPeaks) ~= expStates
thrshBarcode = nan;
return
end
% since the start point is randomly selected the peaks are not always sorted
[sOutput, origIds] = sort(meanOfPeaks, 'ascend');
sortedThBarcode = zeros(length(thrshBarcode), 1);
for i = 1 : length(meanOfPeaks)
sortedThBarcode(thrshBarcode == find(meanOfPeaks == sOutput(i))) = i;
end
thrshBarcode = sortedThBarcode;
% plot all the thresholding steps to compare with preprocessed signal
% figure(2);
% clf;
% subplot(4, 1, 1);
% plot(temporalBarcode);
% hold on;
% for i = 1:length(meanOfPeaks)
% plot([0 length(temporalBarcode)], [meanOfPeaks(i) meanOfPeaks(i)],...
% ':', 'LineWidth', 2.0)
% end
% subplot(4, 1, 2);
% stairs(thrshBarcode, 'LineWidth', 2.0);
% subplot(4, 1, 3);
% histogram(temporalBarcode);
% set(gca, 'YScale', 'log')
% hold on;
% for i = 1:length(meanOfPeaks)
% line([meanOfPeaks(i) meanOfPeaks(i)], ylim, 'LineWidth', 2, 'Color', 'r');
% end
%
% subplot(4, 1, 4);
% plot(raw);
%
% getpeakcount(thrshBarcode);
end
function output = applypeakfilter(threshSignal, raw, onPeakNo, dbPeakNo)
% filter signals based on the number of peaks found. This helps to
% remove non-specific binding data since their activity is high
%
% threshSignal is the classified signal (eg. 0 0 0 0 1 1 2 1 1 0 0)
%
% threshPeakNos is minimum peaks required for signal consideration (eg.
% 10)
output = threshSignal;
[onPeaks, dbPeaks] = getpeakcount(threshSignal);
if onPeaks < onPeakNo
output = nan;
return
end
if dbPeaks < dbPeakNo
output = nan;
return
end
% figure(3);
% clf;
% subplot(3, 1, 1);
% plot(raw);
% subplot(3, 1, 2);
% stairs(threshSignal, 'LineWidth', 2.0);
% legend(['on peaks: ', num2str(onPeaks), ' db-blink peaks: ', num2str(dbPeaks)])
% subplot(3, 1, 3);
% histogram(raw);
% set(gca, 'YScale', 'log')
end
function [onPeaks, dbPeaks] = getpeakcount(signal)
% this function determines the number of time signals crosses 0. It is
% robust to the number of on states. Signal can have more than 1 on
% states
%
% signal is the thresholded temporal barcode
onPeaks = 0;
dbPeaks = 0;
% classification algorithm gives output from 1 and Inf. Change it to
% start from 0
signal = signal - 1;
for iTime = 2:length(signal)
if signal(iTime) == 0 && signal(iTime-1) == 1
onPeaks = onPeaks + 1;
elseif signal(iTime) == 0 && signal(iTime-1) > 1
onPeaks = onPeaks + 1;
dbPeaks = dbPeaks + 1;
elseif signal(iTime) == 1 && signal(iTime-1) > 1
dbPeaks = dbPeaks + 1;
end
end
end
function output = genOnOffStats2(barcode, expsrTime)
% analyse the state chain to calculate # of on-peaks, on-time per
% peak and off-time per peak. The signals will be analyzed for upto 3
% states
%
% barcode is the state chain between 0 and 1 (eg. 0 0 0 0 1 2 2 1 0 1 0)
% where each point is separated by expsrTime
%
% expsrTime is the capture rate in seconds (eg. 0.1)
barcode = barcode - 1;
offTimes = [];
onTimes = [];
dbBlinkTimes = [];
onPeaks = 0;
onStart = 1;
dbOnStart = 1;
offStart = 1;
for iTime = 2 : length(barcode)
if barcode(iTime) == 1 && barcode(iTime-1) == 0
offTimes = [offTimes; expsrTime * (iTime-offStart)];
onStart = iTime;
onPeaks = onPeaks + 1;
elseif barcode(iTime) == 2 && barcode(iTime-1) == 0
offTimes = [offTimes; expsrTime * (iTime-offStart)];
onStart = iTime;
dbOnStart = iTime;
onPeaks = onPeaks + 1;
elseif barcode(iTime) == 0 && barcode(iTime-1) == 1
onTimes = [onTimes; expsrTime * (iTime-onStart)];
offStart = iTime;
elseif barcode(iTime) == 0 && barcode(iTime-1) == 2
onTimes = [onTimes; expsrTime * (iTime-onStart)];
dbBlinkTimes = [dbBlinkTimes; expsrTime * (iTime-dbOnStart)];
offStart = iTime;
elseif barcode(iTime) == 2 && barcode(iTime-1) == 1
dbOnStart = iTime;
elseif barcode(iTime) == 1 && barcode(iTime-1) == 2
dbBlinkTimes = [dbBlinkTimes; expsrTime * (iTime-dbOnStart)];
end
end
output = cell(4, 1);
output{1} = onTimes;
output{2} = dbBlinkTimes;
output{3} = offTimes;
output{4} = onPeaks;
% figure(1);
% plot(barcode);
fprintf(['\nmean on-time (s): ', num2str(median(onTimes)/log(2)), ' peaks #: '...
num2str(onPeaks), ' median db-blink times(s): ',num2str(median(dbBlinkTimes)/log(2))]);
end
function output = applyhumanfilter(signal, raw)
% filter signals due to high activity, drift, and any other unwanted
% activity that was not removed by filter bank
%
% signal is the classified signal (eg. 0 0 0 0 1 1 2 1 1 0 0)
%
% raw is the intensity signature after simple corrections
figure(4);
clf;
subplot(3, 1, 1);
plot(raw);
subplot(3, 1, 2);
stairs(signal, 'LineWidth', 2.0);
subplot(3, 1, 3);
histogram(raw);
set(gca, 'YScale', 'log')
genOnOffStats2(signal, 0.1);
output = signal;
hInput = input('\nkeep this signal [Y/N]?', 's');
if hInput == 'N' || hInput == 'n'
output = nan;
end
end