forked from barionleg/3D-Barcoding-Spots-Code-GIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolocalizeBarcodeV2.m
424 lines (396 loc) · 13.8 KB
/
colocalizeBarcodeV2.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
function [ pts1, pts2, called1, called2, pair ] = colocalizeBarcodeV2( color1, color2, points, hybnum,hybnum2, channels,radius,debug )
%COLOCALIZE Summary of this function goes here
% Detailed explanation goes here
%if length(dots(channels(1)).channels)>length(dots(channels(2)).channels)
%conc1 = dots(channels(2)).channels;
%conc2 = dots(channels(1)).channels;
%else
conc1 = points(hybnum).dots(channels(1)).channels;
conc2 = points(hybnum2).dots(channels(2)).channels;
%end
[idx,D]= rangesearch(conc1,conc2,sqrt(radius)+.00001);
[idx2,D2] = rangesearch(conc2,conc1,sqrt(radius)+.00001);
pair = [];
if ~isempty(idx) & ~isempty(idx2)
%Match Unique Points symmetrically
idx2temp = idx2;
D2temp = D2;
for i = 1:length(idx)
if length(idx{i}) == 1
if length(idx2{idx{i}}) == 1;
idxtemp{i,1} = idx{i};
Dtemp{i,1} = D{i};
else
dummy = idx2{idx{i}};
dummyD = D2{idx{i}};
matches = cellfun(@length,idx(dummy))';
keepers = matches == 1;
idxtemp{i,1} = idx{i};
Dtemp{i,1} = D{i};
idx2temp{idx{i}} = dummy(keepers);
D2temp{idx{i}} = dummyD(keepers);
end
else
idxtemp{i,1} = idx{i};
Dtemp{i,1} = D{i};
end
end
idxunique = idxtemp;
Dunique = Dtemp;
for i = 1:length(idx2temp)
if length(idx2temp{i}) == 1
if length(idxtemp{idx2temp{i}}) == 1;
idx2unique{i,1} = idx2temp{i};
D2unique{i,1} = D2temp{i};
else
dummy = idxtemp{idx2temp{i}};
dummyD = Dtemp{idx2temp{i}};
matches = cellfun(@length,idx2temp(dummy))';
keepers = matches == 1;
idx2unique{i,1} = idx2temp{i};
D2unique{i,1} = D2temp{i};
idxunique{idx2temp{i}} = dummy(keepers);
Dunique{idx2temp{i}} = dummyD(keepers);
end
else
idx2unique{i,1} = idx2temp{i};
D2unique{i,1} = D2temp{i};
end
end
% Place unique matches in curated points list
% Remove points from not uniquely matched list
[idxcurated{1:length(idx), 1}] = deal(zeros(1,1));
[idx2curated{1:length(idx2), 1}] = deal(zeros(1,1));
counter = 1;
if length(idx2unique)>length(idxunique)
for i = 1:length(idx2unique)
if length(idx2unique{i})==1 && logical(sum(idxunique{idx2unique{i}} == i)) && length(idxunique{idx2unique{i}}) == 1
idx2curated{i} = idx2unique{i};
idxcurated{idx2unique{i}} = idxunique{idx2unique{i}};
D2unique{i} = [];
Dunique{idx2unique{i}} = [];
idxunique{idx2unique{i}} = [];
idx2unique{i} = [];
pair(counter,2) = idx2curated{i};
pair(counter,1) = i;
counter = counter +1;
end
end
else
for i = 1:length(idxunique)
if length(idxunique{i})==1 && logical(sum(idx2unique{idxunique{i}} == i)) && length(idx2unique{idxunique{i}}) == 1
idxcurated{i} = idxunique{i};
idx2curated{idxunique{i}} = idx2unique{idxunique{i}};
Dunique{i} = [];
D2unique{idxunique{i}} = [];
idx2unique{idxunique{i}} = [];
idxunique{i} = [];
pair(counter,1) = idxcurated{i}; %conc1
pair(counter,2) = i; %conc2
counter = counter + 1;
end
end
end
% pts1 = conc1(pair(:,1),:);
% pts2 = conc2(pair(:,2),:);
% figure; showMatchedFeatures(max(m{1},[],3), max(m{2},[],3), pts1(:,1:2), pts2(:,1:2));
%Distance Minimization
idxmin = idxunique;
Dmin = Dunique;
for i = 1:length(idxunique);
if length(Dunique{i}) > 1;
smallest = Dunique{i} == min(Dunique{i});
if sum(smallest) == 1
idxmin{i,1} = idxunique{i}(smallest);
Dmin{i,1} = Dunique{i}(smallest);
end
end
end
idx2min = idx2unique;
D2min = D2unique;
for i = 1:length(idx2unique);
if length(D2unique{i}) > 1;
smallest = D2unique{i} == min(D2unique{i});
if sum(smallest) == 1
idx2min{i,1} = idx2unique{i}(smallest);
D2min{i,1} = D2unique{i}(smallest);
end
end
end
% counter = 1;
% clear pair;
% clear pts1;
% clear pts2;
if length(idx2min)>length(idxmin)
for i = 1:length(idx2min)
if length(idx2min{i})==1 && length(idxmin{idx2min{i}}) == 1
if idxmin{idx2min{i}} == i
idx2curated{i} = idx2min{i};
idxcurated{idx2min{i}} = idxmin{idx2min{i}};
D2min{i} = [];
Dmin{idx2min{i}} = [];
idxmin{idx2min{i}} = [];
idx2min{i} = [];
pair(counter,2) = idx2curated{i};
pair(counter,1) = i;
counter = counter +1;
else
idx2min{i} = [];
D2min{i} = [];
end
elseif length(idx2min{i})==1 && isempty(idxmin{idx2min{i}})
idxmin{i} = [];
Dmin{i} = [];
end
end
else
for i = 1:length(idxmin)
if length(idxmin{i})==1 && length(idx2min{idxmin{i}}) == 1
if idx2min{idxmin{i}} == i
idxcurated{i} = idxmin{i};
idx2curated{idxmin{i}} = idx2min{idxmin{i}};
Dmin{i} = [];
D2min{idxmin{i}} = [];
idx2min{idxmin{i}} = [];
idxmin{i} = [];
pair(counter,2) = i;
pair(counter,1) = idxcurated{i};
counter = counter +1;
else
idxmin{i} = [];
Dmin{i} = [];
end
elseif length(idxmin{i})==1 && isempty(idx2min{idxmin{i}})
idxmin{i} = [];
Dmin = [];
end
end
end
% pts1 = conc1(pair(:,1),:);
% pts2 = conc2(pair(:,2),:);
% figure; showMatchedFeatures(max(m{1},[],3), max(m{2},[],3), pts1(:,1:2), pts2(:,1:2));
% same z check based on distance
%idxmin = idxunique;
idxz = idxmin;
%Dz = Dfinal;
for i = 1:length(idxz)
if length(idxz{i}) > 1
zref = conc2(i,3);
zdots = conc1(idxmin{i}, 3);
keepers = zdots == zref;
dummy = idxz{i};
%dummyD = Dfinal{i};
idxz{i} = dummy(keepers);
%Dz{i} = dummyD(keepers);
end
end
%idx2min = idx2unique;
idx2z = idx2min;
%Dz = Dfinal;
for i = 1:length(idx2z)
if length(idx2z{i}) > 1
zref = conc1(i,3);
zdots = conc2(idx2min{i}, 3);
keepers = zdots == zref;
dummy = idx2min{i};
%dummyD = Dfinal{i};
idx2z{i} = dummy(keepers);
%Dz{i} = dummyD(keepers);
end
end
% counter = 1;
% clear pair;
% clear pts1;
% clear pts2;
if length(idx2z)>length(idxz)
for i = 1:length(idx2z)
if length(idx2z{i})==1 && length(idxz{idx2z{i}}) == 1
if idxz{idx2z{i}} == i
idx2curated{i} = idx2z{i};
idxcurated{idx2z{i}} = idxz{idx2z{i}};
idxz{idx2z{i}} = [];
idx2z{i} = [];
pair(counter,2) = idx2curated{i};
pair(counter,1) = i;
counter = counter +1;
else
idx2z{i} = [];
end
elseif length(idx2z{i})==1 && isempty(idxz{idx2z{i}})
idx2z{i} = [];
elseif length(idx2z{i})>1
len = cellfun(@length,idxz(idx2z{i}));
if sum(len) == 1
keepers = len' == 1;
vals = idx2z{i}(keepers);
idx2curated{i} = vals;
idxcurated{vals} = idxz{vals};
idx2z{i} = [];
idxz{vals} = [];
pair(counter,2) = idx2curated{i};
pair(counter,1) = i;
counter = counter +1;
end
end
end
else
for i = 1:length(idxz)
if length(idxz{i})==1 && length(idx2z{idxz{i}}) == 1
if idx2z{idxz{i}} == i
idxcurated{i} = idxz{i};
idx2curated{idxz{i}} = idx2z{idxz{i}};
idx2z{idxz{i}} = [];
idxz{i} = [];
pair(counter,2) = i;
pair(counter,1) = idxcurated{i};
counter = counter +1;
else
idxz{i} = [];
end
elseif length(idxz{i})==1 && isempty(idx2z{idxz{i}})
idxz{i} = [];
elseif length(idxz{i})>1
len = cellfun(@length,idx2z(idxz{i}));
if sum(len) == 1
keepers = len' == 1;
vals = idxz{i}(keepers);
idxcurated{i} = vals;
idx2curated{vals} = idx2z{vals};
idxz{i} = [];
idx2z{vals} = [];
pair(counter,2) = i;
pair(counter,1) = idxcurated{i};
counter = counter +1;
end
end
end
end
%%%% added to remove multiple matching
for i = 1:length(idxz)
if length(idxz{i})>1
a = idx2z(idxz{i});
for j = length(a):-1:1
if length(a{j}) == 1 & a{j} ~= i
idxz{i}(j) = [];
elseif isempty(a{j})
idxz{i}(j) = [];
end
end
end
end
for i = 1:length(idx2z)
if length(idx2z{i})>1
a = idxz(idx2z{i});
for j = length(a):-1:1
if length(a{j}) == 1 & a{j} ~= i
idx2z{i}(j) = [];
elseif isempty(a{j})
idx2z{i}(j) = [];
end
end
end
end
%%%%%
if length(idxz)>length(idx2z)
for i = 1:length(idxz)
if ~isempty(idxz{i}) & length(idx2z(idxz{i})) == 1 & length(idxz{i}) ==1 & idx2z{idxz{i}} == i
pair(counter,2) = i;
pair(counter,1) = idxz{i};
idx2z{idxz{i}} = [];
idxz{i} = [];
counter = counter + 1;
end
end
else
for i = 1:length(idx2z)
if ~isempty(idx2z{i}) & length(idxz(idx2z{i})) == 1 & length(idx2z{i}) ==1 & idxz{idx2z{i}} == i
pair(counter,2) = idx2z{i};
pair(counter,1) = i;
idxz{idx2z{i}} = [];
idx2z{i} = [];
counter = counter + 1;
end
end
end
for i = 1:length(idxz)
if length(idxz{i}) > 1
bart = points(hybnum).dots(channels(1)).intensity(idxz{i}) == max(points(hybnum).dots(channels(1)).intensity(idxz{i}));
if sum(bart) > 1
bart = false(1,length(bart));
bart(1) = 1;
end
pair(counter,2) = i;
pair(counter,1) = idxz{i}(bart');
idxz{i} = [];
counter = counter + 1;
end
end
for i = 1:length(idx2z)
if length(idx2z{i})==1 && isempty(idxz{idx2z{i}})
idx2z{i} = [];
elseif length(idx2z{i}) > 1
bart = points(hybnum2).dots(channels(2)).intensity(idx2z{i}) == max(points(hybnum2).dots(channels(2)).intensity(idx2z{i}));
if sum(bart) > 1
bart = false(1,length(bart));
bart(1) = 1;
end
pair(counter,2) = idx2z{i}(bart');
pair(counter,1) = i;
idx2z{i} = [];
counter = counter + 1;
end
end
%remove this for barcoding
if ~isempty(pair)
called1 = zeros(size(conc1,1),1);
called1(pair(:,1)) = 1;
called1 = logical(called1);
called2 = zeros(size(conc2,1),1);
called2(pair(:,2)) = 1;
called2 = logical(called2);
%2D gaussian mixture model scaled by intensity
%Fit a known barcode
pts1 = conc1(pair(:,1),:);
pts2 = conc2(pair(:,2),:);
end
else
called1 = zeros(size(conc1,1),1);
called2 = zeros(size(conc2,1),1);
pair = [];
pts1 = [];
pts2 = [];
end
if isempty(pair)
called1 = zeros(size(conc1,1),1);
called2 = zeros(size(conc2,1),1);
pair = [];
pts1 = [];
pts2 = [];
end
%image ouput of colocalization
if debug == 1
figure;
g(1:length(pts1), 1) = 1;
g(length(pts1)+1:2*length(pts1), 1) = 2;
vec = pts2 - pts1;
allpts = [pts1;pts2];
z = allpts(:,3);
% call GSCATTER and capture output argument (handles to lines)
h = gscatter(allpts(:,1), allpts(:,2), g,[],'ox+*sdv^<>ph.',5);
% for each unique group in 'g', set the ZData property appropriately
gu = unique(g);
for k = 1:numel(gu)
set(h(k), 'ZData', z( g == gu(k) ));
end
view(3)
hold on; quiver3(pts1(:,1),pts1(:,2),pts1(:,3),vec(:,1),vec(:,2),vec(:,3),0,'MaxHeadSize',0.5)
figure; ax = axes; showMatchedFeatures(max(color1,[],3), max(color2,[],3), pts1(:,1:2), pts2(:,1:2));
legend(ax,'dots 1','dots 2');
hold on
for i = 1:length(pts1)
text(pts1(i,1),pts1(i,2),num2str(i));
end
for i = 1:length(pts2)
text(pts2(i,1)+.2,pts2(i,2)+.2,num2str(i));
end
end