-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCluster.m
585 lines (469 loc) · 17.9 KB
/
Cluster.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
function varargout = Cluster(varargin)
% CLUSTER MATLAB code for Cluster.fig
% Diinamic package for clustering analysis
% GUI for selecting files
% Marianne Renner oct22
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Last Modified by GUIDE v2.5 29-Sep-2022 16:11:34
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Cluster_OpeningFcn, ...
'gui_OutputFcn', @Cluster_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Cluster_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
set(handles.selectfilepushbutton,'userdata',[]);
set(handles.previouspushbutton,'Enable','off')
set(handles.nextpushbutton,'Enable','off')
h3 = findobj('Type','figure');
txtHand = findall(h3, '-property', 'FontUnits');
set(txtHand, 'FontUnits', 'normalized');
guidata(hObject, handles);
%--------------------------------------------------------------------------
function varargout = Cluster_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function selectfilepushbutton_Callback(hObject, eventdata, handles)
% load data simplified version
szpx = str2num(get(handles.szpx,'String'));
mu=get(handles.muradiobutton,'Value');
d = dir('*.mat'); % movie
st={d.name};
cuenta=1;
if isempty(st)==1
msgbox('No files!!','Select files','error');
return
else
for jj=1:size(st,2)
k1 = strfind(st{jj},'roidata');
k2 = strfind(st{jj},'Irend');
k3 = strfind(st{jj},'auxdens');
k4 = strfind(st{jj},'mask');
if isempty(k1)==1 && isempty(k2)==1 && isempty(k3)==1 && isempty(k4)==1
stok{cuenta}=st{jj};
cuenta=cuenta+1;
end
end
end
[files,v] = listdlg('PromptString','Select files:','SelectionMode','multiple','ListString',stok);
if v==0
return
end
for i=1:size(files,2)
listafiles{i}=stok{files(i)};
end
filename=listafiles{1}; %first file
set (handles.filename, 'Userdata',filename);
if size(files,2)>1 %batch
set (handles.filename, 'string',['Batch: File ',filename,' (1/',num2str(size(files,2)),')']) ;
set(handles.nextpushbutton,'Enable','on')
else
set (handles.filename, 'string',filename) ;
end
[namefile,~]=strtok(filename,'.'); %!!!!!!!!!!!!!!!!!!!!!!!!!!!!
set(handles.namefile,'String',namefile);
set(handles.analyzepushbutton,'Enable','on');
set(handles.analyzepresentpushbutton,'Enable','on');
set(handles.refreshpushbutton,'Enable','on');
set(handles.textlistafiles,'userdata',listafiles);
set(handles.textnrofiles,'userdata',1);
%load data, in µm
disp(' ')
disp('Reading data, please wait...')
if mu==0
handles=loadselectPALMdata(filename,szpx,handles);
else
handles=loadselectPALMdata(filename,1,handles); %data already in microns
end
set(handles.selectfilepushbutton,'enable','on')
set(handles.alpha1,'string','0')
set(handles.alpha2,'string','100')
% pointillistic
showimagesauto = get(handles.autoimradiobutton,'value');
if showimagesauto==1
pointillistic(handles)
end
disp('Done')
disp(' ')
% Update handles structure
guidata(hObject, handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function refreshpushbutton_Callback(hObject, eventdata, handles)
pointillistic(handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function analyzepresentpushbutton_Callback(hObject, eventdata, handles)
% Values from window
dx = str2num(get(handles.PALMszpx,'String'));
szpx = str2num(get(handles.szpx,'String'));
alphamin = str2num(get(handles.alpha1,'String'));
alphamax = str2num(get(handles.alpha2,'String'));
mu=get(handles.muradiobutton,'Value');
nrofile=get(handles.textnrofiles,'userdata');
listafiles=get(handles.textlistafiles,'userdata');
%check for previous parameters
if length(dir('auxdens.mat'))>0
load('auxdens.mat','-mat')
else
Densparam.maxdiamcluster=0;
end
%load data
filename=listafiles{nrofile};
handles.file=filename;
[namefile,~]=strtok(filename,'.') ; %sin extension
set(handles.namefile,'String', namefile);
% control existance of ROIs
if exist([namefile,'.rgn'],'file')
names{1}=filename; % for report
if mu==0
handles=loadselectPALMdata(filename,szpx,handles);
else
handles=loadselectPALMdata(filename,1,handles); %data already in microns
end
X_mu=handles.x-min(handles.x);
Y_mu=handles.y-min(handles.y);
alpha = handles.alpha(:);
alpha(1:10);
xdim=ceil(max(X_mu)/dx);
ydim=ceil(max(Y_mu)/dx);
% remove by intensities
vect2remove=selectremovealpha2(handles,X_mu,Y_mu,alpha);
if ~isempty(vect2remove)
[X_mu,Y_mu,alpha,fr]=removealpha(vect2remove,X_mu,Y_mu,alpha,handles.fr);
end
% Create rendered image
[handles.I,xxi,yyi] = PALM_rendering3(X_mu,Y_mu,alpha,dx*2,dx,0,xdim, ydim, 1);
% figure
% imshow(handles.I,'InitialMagnification','fit')
% clustering analysis
typeanalysis=1; % only one file
varargin{1} = handles.I;
varargin{2} = []; %roifile;
varargin{3} = handles.fr;
varargin{4} = handles.alpha;
varargin{5} = filename;
varargin{6} = dx;
varargin{7} = szpx;
varargin{8} = Densparam;
varargin{9} = typeanalysis;
if length(dir('auxiliar.mat'))>0
delete auxiliar.mat %clears previous results
end
varargout=DetectClusters(varargin);
uiwait;
%read results (in auxiliar.mat) for each file (all rois)
control=0;
aux='auxiliar.mat';
if length(dir(aux))>0
load(aux,'-mat')
load('auxdens.mat','-mat')
%abort signal?
if abortsignal==1
return
else
savenameout=[namefile,'-allclust.txt'];
savenamenano=[namefile,'-nanoclust.txt'];
savenamedist=[namefile,'-clustperdist.txt'];
resultsclu=allresult.clu;
resultsnano=allresult.nano;
resdistance=allresult.dist;
if isempty(resultsclu)==0
save(savenameout,'resultsclu','-ascii')
end
if isempty(resdistance)==0 % distance data
save(savenamedist,'resdistance','-ascii')
end
if isempty(resultsnano)==0
save(savenamenano,'resultsnano','-ascii')
end
delete 'auxiliar.mat'
%save report
reportclustering(Densparam,names);
end
else
return %something wrong happened
end % empty results (aux)
else % no ROI for this file
disp('No ROI found for this file : no analysis done')
disp(' ')
end % control existance ROI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function analyzepushbutton_Callback(hObject, eventdata, handles)
% Values from window
dx = str2num(get(handles.PALMszpx,'String'));
szpx = str2num(get(handles.szpx,'String'));
alphamin = str2num(get(handles.alpha1,'String'));
alphamax = str2num(get(handles.alpha2,'String'));
mu=get(handles.muradiobutton,'Value');
batch=get(handles.batchradiobutton,'Value');
currentfolder=cd;
listafiles=get(handles.textlistafiles,'userdata');
for i=1:size(listafiles,2)
newlistafiles(i).name=listafiles{i};
end
if batch==1 %batch mode
%check for previous parameters
if length(dir('auxdens.mat'))>0
load('auxdens.mat','-mat')
else
Densparam.maxdiamcluster=0;
end
varargin{1} = newlistafiles;
varargin{2} = dx;
varargin{3} = szpx;
varargin{4} = mu;
varargin{5} = alphamin;
varargin{6} = alphamax;
varargin{7} = Densparam;
%DetectClustersBatchOptim(varargin); % detects clusters and saves results in batch mode (parameters set at the begining)
DetectClustersBatch(varargin); % detects clusters and saves results in batch mode (parameters set at the begining)
uiwait;
else
set(handles.textnrofiles,'userdata',0); %starts from file 1
names={};
count=1;
logical=1;
while logical % loop files
%check for previous parameters
if length(dir('auxdens.mat'))>0
load('auxdens.mat','-mat')
else
Densparam.maxdiamcluster=0;
end
%load data
cd(currentfolder)
nrofile=get(handles.textnrofiles,'userdata');
next=nrofile+1;
if next>size(listafiles,2)
logical=0;
break
end
set(handles.textnrofiles,'userdata',next);
filename=listafiles{next};
handles.file=filename;
[namefile,~]=strtok(filename,'.') ; %sin extension
set(handles.namefile,'String', namefile);
% control existance of ROIs
if exist([namefile,'.rgn'],'file')
names{count}=filename; % for report
set(handles.filename,'string',[filename,' (',num2str(next),' of ',num2str(size(listafiles,2)),')']);
if mu==0
handles=loadselectPALMdata(filename,szpx,handles);
else
handles=loadselectPALMdata(filename,1,handles); %data already in microns
end
X_mu=handles.x-min(handles.x);
Y_mu=handles.y-min(handles.y);
alpha = handles.alpha(:);
alpha(1:10);
xdim=ceil(max(X_mu)/dx);
ydim=ceil(max(Y_mu)/dx);
% remove by intensities
vect2remove=selectremovealpha2(handles,X_mu,Y_mu,alpha);
if ~isempty(vect2remove)
[X_mu,Y_mu,alpha,fr]=removealpha(vect2remove,X_mu,Y_mu,alpha,handles.fr);
end
% Create rendered image
[handles.I,xxi,yyi] = PALM_rendering3(X_mu,Y_mu,alpha,dx*2,dx,0,xdim, ydim, 1);
% clustering analysis
typeanalysis=2; % loop aver all files
varargin{1} = handles.I;
varargin{2} = []; %roifile;
varargin{3} = handles.fr;
varargin{4} = handles.alpha;
varargin{5} = filename;
varargin{6} = dx;
varargin{7} = szpx;
varargin{8} = Densparam;
varargin{9} = typeanalysis;
if length(dir('auxiliar.mat'))>0
delete auxiliar.mat %clears previous results
end
varargout=DetectClusters(varargin);
uiwait;
%read results (in auxiliar.mat) for each file (all rois)
control=0;
aux='auxiliar.mat';
if length(dir(aux))>0
load(aux,'-mat')
load('auxdens.mat','-mat')
if abortsignal==1
logical=0;
break
else
savenameout=[namefile,'-allclust.txt'];
savenamenano=[namefile,'-nanoclust.txt'];
savenamedist=[namefile,'-clustperdist.txt'];
resultsclu=allresult.clu;
resultsnano=allresult.nano;
resdistance=allresult.dist;
if isempty(resultsclu)==0
save(savenameout,'resultsclu','-ascii')
end
if isempty(resdistance)==0 % distance data
save(savenamedist,'resdistance','-ascii')
end
if isempty(resultsnano)==0
save(savenamenano,'resultsnano','-ascii')
end
delete 'auxiliar.mat'
%save report
reportclustering(Densparam,names);
end
else
break %something wrong happened
end % empty results (aux)
else % no ROI for this file
disp('No ROI found for this file : no analysis done')
disp(' ')
end % control existance ROI
count=count+1; %number file
end %while loop files
end %batch mode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function previouspushbutton_Callback(hObject, eventdata, handles)
szpx= str2num(get(handles.szpx,'String'));
mu=get(handles.muradiobutton,'Value');
PALMszpx= str2num(get(handles.PALMszpx,'String'));
listafiles=get(handles.textlistafiles,'userdata');
nrofile=get(handles.textnrofiles,'userdata');
next=nrofile-1;
% go for previous file
if next>0
set(handles.textnrofiles,'userdata',next);
filename=listafiles{next}
[namefile,~]=strtok(filename,'.'); %sin extension
set(handles.namefile,'String',namefile);
set(handles.filename,'string',[filename,' (',num2str(next),' of ',num2str(size(listafiles,2)),')']);
set(handles.textlistafiles,'userdata',listafiles);
set(handles.textnrofiles,'userdata',next);
set(handles.filename,'userdata',filename);
if mu==0
handles=loadselectPALMdata(filename,szpx,handles);
else
handles=loadselectPALMdata(filename,1,handles); %data already in microns
end
% pointillistic
showimagesauto = get(handles.autoimradiobutton,'value');
if showimagesauto==1
pointillistic(handles)
end
set(handles.nextpushbutton,'Enable','on')
if next==1
set(handles.previouspushbutton,'Enable','off')
end
else
set(handles.previouspushbutton,'Enable','off')
end %there is previous
clear file listafiles
% Update handles structure
guidata(gcbo, handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function nextpushbutton_Callback(hObject, eventdata, handles)
szpx= str2num(get(handles.szpx,'String'));
mu=get(handles.muradiobutton,'Value');
PALMszpx= str2num(get(handles.PALMszpx,'String'));
listafiles=get(handles.textlistafiles,'userdata');
nrofile=get(handles.textnrofiles,'userdata');
next=nrofile+1;
% go for next file
if size(listafiles,2)<next
set(handles.nextpushbutton,'Enable','off')
else
set(handles.textnrofiles,'userdata',next);
filename=listafiles{next}
[namefile,~]=strtok(filename,'.'); %sin extension
set(handles.namefile,'String',namefile);
set(handles.filename,'string',[filename,' (',num2str(next),' of ',num2str(size(listafiles,2)),')']);
set(handles.textlistafiles,'userdata',listafiles);
set(handles.textnrofiles,'userdata',next);
set(handles.filename,'userdata',filename);
if mu==0
handles=loadselectPALMdata(filename,szpx,handles);
else
handles=loadselectPALMdata(filename,1,handles); %data already in microns
end
% pointillistic
showimagesauto = get(handles.autoimradiobutton,'value');
if showimagesauto==1
pointillistic(handles)
end
set(handles.previouspushbutton,'Enable','on')
if next==size(listafiles,2)
set(handles.nextpushbutton,'Enable','off')
end
end %there is next
clear file listafiles
% Update handles structure
guidata(gcbo, handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function optim_Callback(hObject, eventdata, handles)
function menuoptim_Callback(hObject, eventdata, handles)
% Values from window
dx = str2num(get(handles.PALMszpx,'String'));
szpx = str2num(get(handles.szpx,'String'));
alphamin = str2num(get(handles.alpha1,'String'));
alphamax = str2num(get(handles.alpha2,'String'));
mu=get(handles.muradiobutton,'Value');
batch=get(handles.batchradiobutton,'Value');
currentfolder=cd;
listafiles=get(handles.textlistafiles,'userdata');
for i=1:size(listafiles,2)
newlistafiles(i).name=listafiles{i};
end
%check for previous parameters
if length(dir('auxdens.mat'))>0
load('auxdens.mat','-mat')
else
Densparam.maxdiamcluster=0;
end
% varargin{1} = newlistafiles; %not needed
varargin{2} = dx;
varargin{3} = szpx;
varargin{4} = mu;
varargin{5} = alphamin;
varargin{6} = alphamax;
varargin{7} = Densparam;
OptimDetectClusters(varargin); % detects clusters and saves results in batch mode (parameters set at the begining)
uiwait;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function namefile_Callback(hObject, eventdata, handles)
function namefile_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function alpha1_Callback(hObject, eventdata, handles)
function alpha1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function alpha2_Callback(hObject, eventdata, handles)
function alpha2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function szpx_Callback(hObject, eventdata, handles)
function szpx_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function PALMszpx_Callback(hObject, eventdata, handles)
function PALMszpx_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function muradiobutton_Callback(hObject, eventdata, handles)
function autoimradiobutton_Callback(hObject, eventdata, handles)
function batchradiobutton_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%