forked from kdharris101/iss
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfind_spots.m
411 lines (333 loc) · 14.6 KB
/
find_spots.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
function o = find_spots(o) %ADDING t2 BIT BACK IN
% o = o.find_spots;
%
% finds spots in all tiles using the reference channel, removes
% duplicates in overlap regions and returns nSpots x 2 array o.SpotGlobalYX of
% coordinates in global frame
%
% Looks up colors from apporpriate files and makes nSpots x nBP x nRounds
% array o.SpotColors
%
% o.Isolated is a nSpots x 1 binary array giving 1 for
% well-isolated spots
%
% NB spots that can't be read in all rounds are discarded
%
% Kenneth D. Harris, 29/3/17
% GPL 3.0 https://www.gnu.org/licenses/gpl-3.0.en.html
%% variable naming conventions:
% spot subgroups:
% All: Any spot included in any tile (includes duplicates)
% nd: only spots whose anchor coordinate is in its home tile (no duplicates)
% Good: Spots for which could be read for all rounds
% coordinate frames or other info
% LocalYX: relative to home tile on the reference round
% LocalTile: number of home tile on the reference round
% GlobalYX: relative to the stitched image on the reference round
% RoundYX: relative to home tile after registration on each round
% RoundTile: number of home tile after registration on each round
% Isolated: binary number, saying if it is isolated
% SpotColors: the answer:
%% basic variables
rr = o.ReferenceRound;
Tiles = find(~o.EmptyTiles)';
[nY, nX] = size(o.EmptyTiles);
nTiles = nY*nX;
%% loop through all tiles, finding spots in anchor channel on ref round
RawLocalYX = cell(nTiles,1); % cell array, giving spots in local coordinates
RawIsolated = cell(nTiles,1);
SE = fspecial('disk', o.SmoothSize);
for t=Tiles
if mod(t,10)==0; fprintf('Detecting reference spots in tile %d\n', t); end
[y,x] = ind2sub([nY nX], t);
AnchorIm = imread(o.TileFiles{rr,y,x}, o.AnchorChannel);
if o.SmoothSize
AnchorImSm = imfilter(AnchorIm, SE);
else
AnchorImSm = AnchorIm;
end
[RawLocalYX{t}, RawIsolated{t}] = o.detect_spots(AnchorImSm);
end
%% now make array of global coordinates
AllIsolated = logical(vertcat(RawIsolated{:})); % I HATE MATLAB - for converting logical to doubles for no reason
nAll = length(AllIsolated);
AllGlobalYX = zeros(nAll,2);
AllLocalYX = zeros(nAll,2);
ind = 1;
for t=Tiles
MySpots = RawLocalYX{t};
nMySpots = size(MySpots, 1);
AllGlobalYX(ind:ind+nMySpots-1,:) = bsxfun(@plus, MySpots, o.TileOrigin(t,:,rr));
AllLocalYX(ind:ind+nMySpots-1,:) = MySpots;
OriginalTile(ind:ind+nMySpots-1) = t;
ind = ind+nMySpots;
end
if o.Graphics
figure(1001)
plot(AllGlobalYX(:,2), AllGlobalYX(:,1), '.', 'markersize', 1);
title('All global coords including duplicates');
%set(gca, 'YDir', 'reverse');
end
%% now remove duplicates by keeping only spots detected on their home tile
[AllLocalTile, ~] = which_tile(AllGlobalYX, o.TileOrigin(:,:,rr), o.TileSz);
NotDuplicate = (AllLocalTile==OriginalTile');
ndGlobalYX = AllGlobalYX(NotDuplicate,:);
ndLocalYX = AllLocalYX(NotDuplicate,:);
ndIsolated = AllIsolated(NotDuplicate,:);
ndLocalTile = AllLocalTile(NotDuplicate,:);
nnd = sum(NotDuplicate);
if o.Graphics
figure(1002); clf
plot(ndGlobalYX(:,2), ndGlobalYX(:,1), '.', 'markersize', 1);
title('Global coords without duplicates');
drawnow;
%set(gca, 'YDir', 'reverse');
end
%% get spot local coordinates in all colour channels and run PCR
%Specify which rounds/colour channels to use (default is all)
if isempty(o.UseChannels)
o.UseChannels = 1:o.nBP;
end
if isempty(o.UseRounds)
o.UseRounds = 1:o.nRounds;
end
AllBaseLocalYX = cell(nTiles,o.nBP, o.nRounds);
o.D0 = zeros(nTiles,2,o.nRounds);
o.cc = zeros(nTiles,o.nRounds);
% loop through all tiles, finding PCR outputs
fprintf('\nLocating spots in each colour channel of tile ');
for t=1:nTiles
if o.EmptyTiles(t); continue; end
if t<10
fprintf('\b%d',t);
else
fprintf('\b\b%d',t);
end
[y, x] = ind2sub([nY nX], t);
%Reload anchor image to find initial shift
AnchorIm = imread(o.TileFiles{rr,y,x}, o.AnchorChannel);
if o.SmoothSize
AnchorImSm = imfilter(AnchorIm, SE);
else
AnchorImSm = AnchorIm;
end
for r=o.UseRounds
% find spots whose home tile on round r is t
% open file for this tile/round
FileName = o.TileFiles{r,t};
TifObj = Tiff(FileName);
% now read in images for each base
for b=o.UseChannels
TifObj.setDirectory(o.FirstBaseChannel + b - 1);
BaseIm = TifObj.read();
% find spots for base b on tile t - we will use this for point
% cloud registration only, we don't use these detections to
% detect colors, we read the colors off the
% pointcloud-corrected positions of the spots detected in the reference round home tiles
CenteredSpots = o.detect_spots(BaseIm) - [o.TileSz/2,o.TileSz/2];
AllBaseLocalYX(t,b,r) = {CenteredSpots};
if b == o.InitialShiftChannel
%For chosen channel, find initial shift
%o.Graphics = 2;
BaseIm = imfilter(BaseIm, SE);
[o.D0(t,:,r), o.cc(t,r)] = o.ImRegFft2_FindSpots(BaseIm,AnchorImSm, 0, o.RegMinSize);
%o.Graphics = 1;
end
end
TifObj.close();
end
end
fprintf('\n');
o = o.PointCloudRegister(AllBaseLocalYX, RawLocalYX, eye(2), nTiles);
%% decide which tile to read each spot off in each round.
% They are read of home tile if possible (always possible in ref round)
% in other rounds might have to be a NWSE neighbor - but never a diagonal
% neighbor
% ndRoundTile(s,r) stores appropriate tile for spot s on round r
% ndRoundYX(s,:,r) stores YX coord on this tile
o.TileOrigin(:,:,1:o.nRounds) = o.TileOrigin(:,:,rr) - o.D;
ndRoundTile = nan(nnd,o.nRounds);
ndRoundYX = nan(nnd,2,o.nRounds);
PossNeighbs = [-1 -nY 1 nY 0]; % NWSE then same tile - same will have priority by being last
for r=o.UseRounds
fprintf('Finding appropriate tiles for round %d\n', r);
for n = PossNeighbs
% find origins of each tile's neighbor, NaN if not there
NeighbTile = (1:nTiles)+n;
NeighbOK = (NeighbTile>=1 & NeighbTile<=nTiles);
NeighbOrigins = nan(nTiles,2);
NeighbOrigins(NeighbOK,:) = round(o.TileOrigin(NeighbTile(NeighbOK),:,r));
% now for each spot see if it is inside neighbor's tile area
SpotsNeighbOrigin = NeighbOrigins(ndLocalTile,:);
SpotsInNeighbTile = all(ndGlobalYX>=SpotsNeighbOrigin+1+o.ExpectedAberration...
& ndGlobalYX<=SpotsNeighbOrigin+o.TileSz-o.ExpectedAberration, 2);
% for those that were in set this to be its neighbor
ndRoundTile(SpotsInNeighbTile,r) = NeighbTile(ndLocalTile(SpotsInNeighbTile));
end
% compute YX coord
HasTile = isfinite(ndRoundTile(:,r));
ndRoundYX(HasTile,:,r) = ndGlobalYX(HasTile,:) - round(o.TileOrigin(ndRoundTile(HasTile,r),:,r));
end
%% loop through all tiles, finding spot colors
ndSpotColors = nan(nnd, o.nBP, o.nRounds);
ndPointCorrectedLocalYX = nan(nnd, 2, o.nRounds, o.nBP);
for t=1:nTiles
if o.EmptyTiles(t); continue; end
[y, x] = ind2sub([nY nX], t);
for r=o.UseRounds
% find spots whose home tile on round r is t
MySpots = (ndRoundTile(:,r)==t);
if ~any(MySpots); continue; end
% open file for this tile/round
FileName = o.TileFiles{r,t};
TifObj = Tiff(FileName);
% find the home tile for all current spots in the ref round
RefRoundHomeTiles = ndLocalTile(ndRoundTile(:,r)==t);
MyRefTiles = unique(RefRoundHomeTiles);
fprintf('\nRef round home tiles for spots in t%d at (%2d, %2d), r%d: ', t, y, x, r);
for i=MyRefTiles(:)'
fprintf('t%d, %d spots; ', i, sum(RefRoundHomeTiles==i));
end
fprintf('\n');
% now read in images for each base
for b=o.UseChannels %No 0 as trying without using anchor
TifObj.setDirectory(o.FirstBaseChannel + b - 1);
BaseIm = TifObj.read();
if o.SmoothSize
BaseImSm = imfilter(double(BaseIm), fspecial('disk', o.SmoothSize));
else
BaseImSm = BaseIm;
end
for t2 = MyRefTiles(:)'
MyBaseSpots = (ndRoundTile(:,r)==t & ndLocalTile==t2);
CenteredMyLocalYX = ndLocalYX(MyBaseSpots,:) - [o.TileSz/2,o.TileSz/2];
if t == t2
fprintf('Point cloud: ref round tile %d -> tile %d round %d base %d, %d/%d matches, error %f\n', ...
t, t2, r, b, o.nMatches(t,b,r), size(RawLocalYX{t2},1), o.Error(t,b,r));
if o.nMatches(t,b,r)<o.MinPCMatches || isempty(o.nMatches(t,b,r))
continue;
end
CenteredMyPointCorrectedYX = (o.A(:,:,b)*(CenteredMyLocalYX + o.D(t,:,r))')';
MyPointCorrectedYX = round(CenteredMyPointCorrectedYX + [o.TileSz/2,o.TileSz/2]);
ndPointCorrectedLocalYX(MyBaseSpots,:,r,b) = MyPointCorrectedYX;
ndSpotColors(MyBaseSpots,b,r) = IndexArrayNan(BaseImSm, MyPointCorrectedYX');
else
[MyPointCorrectedYX, error, nMatches] = o.different_tile_transform(AllBaseLocalYX,RawLocalYX, ...
CenteredMyLocalYX,t,t2,r,b);
fprintf('Point cloud: ref round tile %d -> tile %d round %d base %d, %d/%d matches, error %f\n', ...
t, t2, r, b, nMatches, size(RawLocalYX{t2},1), error);
if nMatches<o.MinPCMatches || isempty(nMatches)
continue;
end
ndPointCorrectedLocalYX(MyBaseSpots,:,r,b) = MyPointCorrectedYX;
ndSpotColors(MyBaseSpots,b,r) = IndexArrayNan(BaseImSm, MyPointCorrectedYX');
end
end
end
TifObj.close();
end
end
fprintf('\n');
%% now find those that were detected in all tiles
ndSpotColorsToUse = ndSpotColors(:,o.UseChannels,o.UseRounds);
Good = all(isfinite(ndSpotColorsToUse(:,:)),2);
GoodGlobalYX = ndGlobalYX(Good,:);
GoodSpotColors = ndSpotColors(Good,:,:);
GoodLocalTile = ndLocalTile(Good);
GoodIsolated = ndIsolated(Good);
save(fullfile(o.OutputDirectory, 'Intensities_NoAnchor.mat'), 'Good', 'ndGlobalYX', 'ndSpotColors', 'ndLocalTile');
%% plot those that were found and those that weren't
if o.Graphics
PlotScale = 1;
figure(10032); clf; hold on; set(gca, 'color', 'k');
plot(ndGlobalYX(Good,2), ndGlobalYX(Good,1), 'b.', 'markersize', 1);
plot(ndGlobalYX(~Good,2), ndGlobalYX(~Good,1), 'r.', 'markersize', 1);
legend({'resolved', 'unresolved'}, 'color', [.6 .6 .6]);
% now put on edges
SquareX1 = [0, 0, o.TileSz];
SquareY1 = [o.TileSz, 0, 0];
SquareX2 = [o.TileSz, o.TileSz, 0];
SquareY2 = [0, o.TileSz, o.TileSz];
SquareColors = hsv2rgb([(1:o.nRounds)'/o.nRounds, [.5, .6] .*ones(o.nRounds,1)]);
SquareColors(o.ReferenceRound,:)=1.0;
for r=o.UseRounds
for t=Tiles
MyOrigin = o.TileOrigin(t,:,r);
plot(SquareX1 + MyOrigin(2), SquareY1 + MyOrigin(1),...
'--', 'Color', SquareColors(r,:));
plot(SquareX2 + MyOrigin(2), SquareY2 + MyOrigin(1),...
':', 'Color', SquareColors(r,:));
text(MyOrigin(2), MyOrigin(1),...
sprintf('T%d r%d', t, r), 'color', SquareColors(r,:));
end
end
%set(gca, 'YDir', 'reverse');
end
%% sanity check
plsz = 7;
if o.Graphics ==2
GoodRoundYX = ndRoundYX(Good,:,:);
GoodRoundTile = ndRoundTile(Good,:);
GoodCorrectedYX = ndPointCorrectedLocalYX(Good,:,:,:);
roi = o.FindSpotsRoi;
PlotSpots = find(GoodGlobalYX(:,1)>roi(1) & GoodGlobalYX(:,1)<roi(2) & GoodGlobalYX(:,2)>roi(3) & GoodGlobalYX(:,2)<roi(4));
for s=(PlotSpots(:))' %PlotSpots(randperm(length(PlotSpots)))'
figure(91); clf
for r=o.UseRounds
t=GoodRoundTile(s,r);
fprintf('Spot %d, round %d, tile %d: y=%d, x=%d\n', s, r, t, GoodRoundYX(s,1,r), GoodRoundYX(s,2,r));
Ylegends = {'Anchor', o.bpLabels{:}};
for b=o.UseChannels
% if b==0
% y0 = GoodRoundYX(s,1,r);
% x0 = GoodRoundYX(s,2,r);
% else
% y0 = GoodCorrectedYX(s,1,r,b);
% x0 = GoodCorrectedYX(s,2,r,b);
% end
y0 = GoodCorrectedYX(s,1,r,b);
x0 = GoodCorrectedYX(s,2,r,b);
if ~isfinite(x0) || ~isfinite(y0)
continue;
end
y1 = max(1,y0 - plsz);
y2 = min(o.TileSz,y0 + plsz);
x1 = max(1,x0 - plsz);
x2 = min(o.TileSz,x0 + plsz);
BaseIm = imread(o.TileFiles{r,t}, b, 'PixelRegion', {[y1 y2], [x1 x2]});
if o.SmoothSize
BaseImSm = imfilter(double(BaseIm), fspecial('disk', o.SmoothSize));
else
BaseImSm = BaseIm;
end
subplot(o.nBP+1, o.nRounds, (b)*o.nRounds + r)
imagesc([x1 x2], [y1 y2], BaseImSm); hold on
axis([x0-plsz, x0+plsz, y0-plsz, y0+plsz]);
plot(xlim, [y0 y0], 'w'); plot([x0 x0], ylim, 'w');
caxis([0 o.DetectionThresh*2]);
if r==1; ylabel(Ylegends{b+1}); end
colorbar;
title(sprintf('Round %d, Base %d, Tile %d', r, b, t));
drawnow
end
end
fprintf('\n');
figure(92); clf
imagesc(sq(GoodSpotColors(s,:,:)));
set(gca, 'ytick', 1:5); set(gca, 'yticklabel', {'Anchor', o.bpLabels{:}});
%caxis([0 o.DetectionThresh*2]);
% fprintf('local YX = (%f, %f) screen YX = (%f, %f) Called as %s, %s, quality %f\n', ...
% GoodRoundYX(s,1), GoodRoundYX(s,2), GoodGlobalYX(s,1)/4, GoodGlobalYX(s,2)/4, ...
% GoodCodes{s}, GoodGenes{s}, GoodMaxScore(s));
figure(1003); hold on
squarex = [-1 1 1 -1 -1]*plsz; squarey = [-1 -1 1 1 -1]*plsz;
h = plot(GoodGlobalYX(s,2)+squarex, GoodGlobalYX(s,1)+squarey, 'g');
pause;
delete(h);
end
end
%%
o.SpotGlobalYX = GoodGlobalYX;
o.cSpotColors = GoodSpotColors;
%o.cAnchorIntensities = squeeze(GoodSpotColors(:,1,:));
o.cSpotIsolated = GoodIsolated;