-
Notifications
You must be signed in to change notification settings - Fork 5
/
optimizing_dotfinding.m~
executable file
·374 lines (268 loc) · 11.6 KB
/
optimizing_dotfinding.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
%% optimizing _dotfinding.m
% Alistair Boettiger Date Begun: 01/31/11
% Levine Lab
% Just a testing script to optimize dot finding algorithm and parameters.
% This is a faster testbed then running the whole stack analysis as in
% imsinglemolecule.fig
%
%
% Modified to require images from projected files. This is more memory
% efficient than reading in directly the whole lsm file.
clear all;
dispdata = 0; % display dots around a chosen z section for visual cross-check.
max_size = 200; % Biggest linear dimension at which to attempt 3D projection;
tic;
disp('loading data...');
%folder = '/Volumes/Data/Lab Data/Raw_Data/02-17-11/';
%fname = 'MP05_22C_sna_y'; emb = '02';
%fname = 'MP09_22C_hb_y_d'; emb = '01';
rawfolder = '/home/alistair/Documents/Research/Raw_Data_Temp/2011_06_22/';
folder = '/home/alistair/Documents/Research/Projects/Chromatin/Data/';
rawfolder = folder; % '/Volumes/Data/Lab Data/Raw_Data/2011-05-22/s12_cntrl_2label/';
stackfolder = 's12_cntrl_2label/';
fname = 's12_cntrl_2label_2'; emb ='01';
% stackfolder = 's14_comp_cntrl/';
% fname = 's14_comp_cntrl_1'; emb ='01';
filename = [folder,'/',fname];
handles.Im = lsm_read_mod([filename,'.mat'],str2double(emb),1.9E4);
% fname = 's14_comp_cntrl'; xp1 = 1700; yp1 = 1500;
fname = 's12_cntrl_2label'; emb ='02'; xp1 = 50; yp1 = 900;
handles.mRNAchn1 = 1;
toc
%fdata = '/Users/alistair/Documents/Berkeley/Levine_Lab/ImageProcessing/';
%save([fdata,'opt_data'],'handles');
%load([fdata,'opt_data']);
%---- Dot Finding Parameters ----- %
sigmaE = 3;% IMPORTANT
sigmaI = 4; % IMPORTANT
min_int = 0.04; % 5 ;% .05 % not necessary Fix at Zero
FiltSize = 30;%
min_size = 30;%
% Build the Gaussian Filter
Ex = fspecial('gaussian',FiltSize,sigmaE); % excitatory gaussian
Ix = fspecial('gaussian',FiltSize,sigmaI); % inhibitory gaussian
Filt = Ex -Ix;
%---------------------------------%
%% Set-up
% zoom in on specific region
m = .9; % 1/2048; % .7; % 1/2048; % .9; % .85;
Zs = length(handles.Im);
[h,w] = size(handles.Im{1,1}{1});
%
% xp1= floor(h/2*m)+1;
% xp2 = floor(h/2*(2-m))+1;
% yp1 = floor(w/2*m)+1;
% yp2 = floor(w/2*(2-m))+1;
%xp1 = 1000; yp1 = 1700;
xp2 = xp1+200;
yp2 = yp1+200;
Imax = imread([rawfolder,stackfolder,'max_',fname,'_',emb,'.tif']);
Imax_dots = Imax(xp1:xp2,yp1:yp2,1:3);
figure(1); clf; imagesc(Imax(:,:,1:3));
figure(2); clf; imagesc(Imax_dots);
disp(['Coordinates: ', num2str(xp1), ' : ', num2str(xp2), ', ' num2str(yp1), ' : ', num2str(yp2) ] );
% Build the Gaussian Filter
Ex = fspecial('gaussian',FiltSize,sigmaE); % excitatory gaussian
Ix = fspecial('gaussian',FiltSize,sigmaI); % inhibitory gaussian
Filt = Ex -Ix;
% figure(1); clf; imagesc(Filt); colorbar; colormap jet;
% set(gcf,'color','k'); set(gca,'FontSize',14);
%% Quick view around layer z
if dispdata == 1
tic
z = 20;
disp(['running sample filter on layer', num2str(z),'...']);
I0 = handles.Im{1,z-1}{handles.mRNAchn1}( xp1:xp2,yp1:yp2 );
D0 = dotfinder(I0,Ex,Ix,min_int,min_size);
I1 = handles.Im{1,z}{handles.mRNAchn1}( xp1:xp2,yp1:yp2 );
D1 = dotfinder(I1,Ex,Ix,min_int,min_size);
I2 = handles.Im{1,z+1}{handles.mRNAchn1}( xp1:xp2,yp1:yp2 );
D2 = dotfinder(I2,Ex,Ix,min_int,min_size);
I3 = handles.Im{1,z+2}{handles.mRNAchn1}( xp1:xp2,yp1:yp2 );
D3 = dotfinder(I3,Ex,Ix,min_int,min_size);
[h,w] = size(I2);
% plotting
Iz = uint16(zeros(h,w,3));
Iz(:,:,1) = 3*I1 + I3 + .5*I0;
Iz(:,:,2) = 1*I2 + 2*I3 + 2*I0;
Iz(:,:,3) = 3*I2 + I3 + .4*I0;
figure(6); clf;
imagesc(Iz); hold on;
plot(D1(:,1),D1(:,2),'y+');
plot(D2(:,1),D2(:,2),'co');
plot(D3(:,1),D3(:,2),'+','color',[1,1,1]);
plot(D0(:,1),D0(:,2),'+','color',[1,.2,.2]);
clear D0 D1 D2 D3 I1 I2 I3 I0;
toc
end
%% Find all dots
[hs,ws] = size( handles.Im{1,1}{handles.mRNAchn1}( xp1:xp2,yp1:yp2 ));
%im_folder{z} = [rawfolder,stackfolder,fname,'_',emb,'_z',num2str(z),'.tif'];
% Iin_z = imread(im_folder{z});
if hs< max_size
% NEEDS to be Double
Isect1 = zeros(hs,ws,Zs);
% Isect2 = zeros(hs,ws,Zs);
% Inuc = zeros(hs,ws,Zs);
Alldots = uint16(zeros(hs,ws,Zs));
end
plotdata = 0 ;% don't show
im_folder = cell(1,Zs);
DotLabels1 = cell(1,Zs);
DotData1 = cell(1,Zs);
Inds1 = cell(1,Zs);
Ints1 = cell(1,Zs);
DotLabels2 = cell(1,Zs);
DotData2 = cell(1,Zs);
Inds2 = cell(1,Zs);
Ints2 = cell(1,Zs);
tic; disp('finding dots...');
for z = 1:Zs % z = 20
% Dot finding for channel 1
im_folder{z} = [rawfolder,stackfolder,fname,'_',emb,'_z',num2str(z),'.tif'];
Iin_z = imread(im_folder{z});
mRNAchn = 1; min_int =.06; % .05; %
[DotLabels1{z},DotData1{z},Inds1{z},Ints1{z}] = dotfinder(Iin_z(xp1:xp2,yp1:yp2,mRNAchn),Ex,Ix,min_int,min_size);
% figure(2); clf; imagesc(Alldots(:,:,z));
% if hs< max_size
% Alldots(:,:,z) = Iin_z; % 2
% bnd1 = imdilate(bw1,strel('disk',2)) -bw1;
% % figure(2); clf; imshow(bnd2);
% mask = double(2*bw1)+bnd1;
% mask(mask==0)=NaN;
% mask(mask==1) = 0;
% Isect1(:,:,z) = double(I1).*double(mask);
% % figure(2); clf; imagesc(mask);
% % figure(2); clf; imagesc(Isect1(:,:,z) ); colormap hot;
% end
mRNAchn = 2; min_int = .05; % .05; %
[DotLabels2{z},DotData2{z},Inds2{z},Ints2{z}] = dotfinder(Iin_z(xp1:xp2,yp1:yp2,mRNAchn),Ex,Ix,min_int,min_size);
end
toc;
%%
consec_layers = 2;
ovlap = 4;
intype = class(Iin_z);
show_projected = 1; % show max-project with all dots and linked dots.
plotZdata = 1 ;% show z-map of data
getpreciseZ = 1;
ck_dots = tic;
NewDotC1 = CheckDotUpDown(DotLabels1,DotData1,Inds1,Ints1,plotZdata,getpreciseZ,consec_layers,ovlap,xp1,xp2,yp1,yp2,intype);
NewDotC2 = CheckDotUpDown(DotLabels2,DotData2,Inds2,Ints2,plotZdata,getpreciseZ,consec_layers,ovlap,xp1,xp2,yp1,yp2,intype);
toc(ck_dots)
%% Project all layers
D1 = cell2mat(DotData1');
D2 = cell2mat(DotData2');
Imax = imread([rawfolder,stackfolder,'max_',fname,'_',emb,'.tif']);
Imax_dots = 1.5*Imax(xp1:xp2,yp1:yp2,1:3);
Imax_dots(:,:,3) = .1*Imax_dots(:,:,3);
figure(2); clf; imagesc(Imax_dots);
colordef black; set(gcf,'color','k');
%colormap hot;
hold on;
plot( NewDotC1(:,1),NewDotC1(:,2),'mo','MarkerSize',5 );
plot( NewDotC2(:,1),NewDotC2(:,2),'co','MarkerSize',5 );
plot( D1(:,1),D1(:,2),'m+','MarkerSize',4);
plot( D2(:,1),D2(:,2),'c+','MarkerSize',4);
% saveas(Iout,[folder,fname,'_',emb,'_chn',num2str(mRNAchn),'.fig']);
figure(4); clf; imagesc(Imax_dots(:,:,1)+Imax_dots(:,:,2));
hold on; set(gcf,'color','k');
plot( NewDotC1(:,1),NewDotC1(:,2),'b+','MarkerSize',5);
plot( NewDotC2(:,1),NewDotC2(:,2),'go','MarkerSize',5);
colormap hot; colorbar;
figure(3); clf; subplot(2,1,1); set(gcf,'color','k');
Imax_r = Imax_dots; Imax_r(:,:,2) = 0*Imax_r(:,:,2);
imagesc(Imax_r); hold on;
plot( NewDotC1(:,1),NewDotC1(:,2),'m+','MarkerSize',5 );
subplot(2,1,2);
Imax_g = Imax_dots; Imax_g(:,:,1) = 0*Imax_g(:,:,1);
imagesc(Imax_g); hold on;
plot( NewDotC2(:,1),NewDotC2(:,2),'go','MarkerSize',5 );
% figure(2);
% for z=1:Zs
% text(DotData2{z}(:,1),DotData2{z}(:,2),[num2str(z)],'color','w','FontSize',8);
% text(DotData1{z}(:,1),DotData1{z}(:,2),[num2str(z)],'color','m','FontSize',8);
% end
%
figure(2); clf; set(gcf,'color','k');
Imax_r = Imax_dots; Imax_r(:,:,2) = 0*Imax_r(:,:,2);
imagesc(Imax_r);
Imax_g = Imax_dots; Imax_g(:,:,1) = 0*Imax_g(:,:,1);
imagesc(Imax_g); hold on;
minS = 8;
d2 = 10*zeros(1,length(NewDotC2));
jn = zeros(1,length(NewDotC2));
for n = 1:length(NewDotC2)
[d2(n),jn(n)] = min(sqrt( (NewDotC2(n,1) - NewDotC1(:,1)).^2 + (NewDotC2(n,2) - NewDotC1(:,2)).^2 + (NewDotC2(n,3) - NewDotC1(:,3)).^2) );
end
figure(10); clf; hist(d2);
d2miss = sum(d2< minS)/length(d2);
d1 = 10*zeros(1,length(NewDotC1));
for n = 1:length(NewDotC1)
d1(n) = min(sqrt( (NewDotC1(n,1) - NewDotC2(:,1)).^2 + (NewDotC1(n,2) - NewDotC2(:,2)).^2 + (NewDotC1(n,3) - NewDotC2(:,3)).^2) );
end
figure(10); clf; hist(d1);
d1miss = sum(d1< minS)/length(d1);
figure(4); hold on; plot(NewDotC1((d1< minS),1),NewDotC1((d1< minS),2),'c*')
figure(3); subplot(2,1,1); hold on; plot(NewDotC2((d2> minS),1),NewDotC2((d2> minS),2),'y*')
title(['miss rate = ',num2str(d2miss,3)]);
figure(3); subplot(2,1,2); hold on; plot(NewDotC1((d1> minS),1),NewDotC1((d1> minS),2),'y*')
title(['miss rate = ',num2str(d1miss,3)]);
%% Project all layers
first = 1; last = Zs;
depth_code = jet(last-first+1);
Alldots_proj = max(Alldots(:,:,first:last),[],3); % perform max project
figure(2); clf; colormap hot; imagesc(Alldots_proj); hold on;
plot( NewDotC(:,1),NewDotC(:,2),'bo','MarkerSize',4 );
%% 3-D visualization
if hs<max_size
[X,Y] = meshgrid((1:ws)*50,(1:hs)*50);
Istack = zeros(hs,ws,Zs);
%nmax = round(max(Inuc(:)));
c1max = round(max(Isect1(:)));
%c2max = round(max(Isect2(:)));
figure(1); clf;
colordef black; set(gca,'color','k'); set(gcf,'color','k');
% % Plot nuclei data
% for z=first:last
% In = Inuc(:,:,z) + c1max + c2max;
% Z = (Zs - z*ones(hs,ws))*340;
% surf(X,Y,Z,In); hold on;
% end
% shading interp;
% alpha(.45); % make nuclei transparent
figure(1); clf;
for z=first:last
figure(1); hold on;
I1 = Isect1(:,:,z) ;
Z = (Zs - z*ones(hs,ws))*340;
surf(X,Y,Z,I1); hold on;
%plot3(D2u_Z{z}(:,1)*50,D2u_Z{z}(:,2)*50,((Zs-z)*340)*ones(1,length(D2u_Z{z})),'o','MarkerSize',10,'Color',depth_code(z,:) ); hold on;
end
plot3(NewDotC(:,1)*50,NewDotC(:,2)*50,340*(Zs-NewDotC(:,3)) ,'.','MarkerSize',20);
shading interp;
% figure(1); hold on;
% for z=first:last
% I2 = Isect2(:,:,z) + c1max+1;
% Z = (Zs - z*ones(hs,ws))*340;
% surf(X,Y,Z,I2); hold on;
% end
% shading interp;
%
% C2 = zeros(c2max,3);
% for cc = 1:c2max
% C2(cc,:) = [((cc-1)/c2max)^3,((cc-1)/c2max)^.5,((cc-1)/c2max)^2];
% end
C1 = hot(c1max);
%C1 = flipud(1-hot(c1max));
%CN = cool(nmax);
% colormap([0,0,0;C1;C2;CN]); colorbar; caxis([0,nmax+c1max+c2max]);
colormap(C1); colorbar; caxis([0,c1max]);
%view(-109,50);
view(40,-30); axis on;
set(gca,'FontSize',12);
zlim([(Zs-last)*340,(Zs-first+1)*340]);
xlabel('nanometers'); ylabel('nanometers'); zlabel('nanometers');
end
%% Can we extract the intensities inside the sphere and rplot just the
% sphere with the intensity of the original dot?