-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowGUI.m
2772 lines (2242 loc) · 92.1 KB
/
FlowGUI.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = FlowGUI(varargin)
% FLOWGUI MATLAB code for FlowGUI.fig
% FLOWGUI, by itself, creates a new FLOWGUI or raises the existing
% singleton*.
%
% H = FLOWGUI returns the handle to a new FLOWGUI or the handle to
% the existing singleton*.
%
% FLOWGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FLOWGUI.M with the given input arguments.
%
% FLOWGUI('Property','Value',...) creates a new FLOWGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FlowGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FlowGUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help FlowGUI
% Last Modified by GUIDE v2.5 21-Feb-2018 14:10:29
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FlowGUI_OpeningFcn, ...
'gui_OutputFcn', @FlowGUI_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
% End initialization code - DO NOT EDIT
% --- Executes just before FlowGUI is made visible.
function FlowGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to FlowGUI (see VARARGIN)
warning('off','all');
% Choose default command line output for FlowGUI
handles.output = hObject;
handles.clustermethodbox.String={'Hard KMEANS (on t-SNE)','Hard KMEANS (on HD Data)','DBSCAN','Hierarchical Clustering','Network Graph-Based','Self Organized Map','GMM - Expectation Minimization','Variational Bayesian Inference for GMM'};
handles.Distance_Measure.String={'euclidean','seuclidean','cityblock','chebychev','minkowski','mahalanobis','cosine','correlation','spearman','hamming','jaccard'};
handles.Perplexity.String='30';
addpath('Functions/');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FlowGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = FlowGUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function fileread1_Callback(hObject, eventdata, handles)
% hObject handle to fileread1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of fileread1 as text
% str2double(get(hObject,'String')) returns contents of fileread1 as a double
if strcmp(handles.fileread1.String(end-3:end),'.csv')
[num,ChannelsOut]= ReadCSVFile(handles.fileread1.String);
handles.num=num;
handles.ChannelsAll=ChannelsOut;
handles.channelselect.String=ChannelsOut;
handles.xaxis.String=ChannelsOut;
handles.yaxis.String=ChannelsOut;
elseif strcmp(lower(handles.fileread1.String(end-3:end)),'.fcs')
%[fcsdat, fcshdr, fcsdatscaled, fcsdatcomp] = fca_readfcs(handles.fileread1.String);
try
[data, marker_names, channel_names, scaled_data, compensated_data, fcshdr] = readfcs_v2(handles.fileread1.String);
data=transpose(data);
compensated_data=transpose(compensated_data);
handles.num=compensated_data;
handles.num2=data;
header=marker_names;
catch
[data, fcshdr, fcsdatscaled, compensated_data] = fca_readfcs(handles.fileread1.String);
data=double(data);
headerdata=fcshdr.par;
for i=1:size(headerdata,2);
if ~isempty(headerdata(i).name2)
header{i}=headerdata(i).name2;
else
header{i}=headerdata(i).name;
end
end
handles.num=data;
handles.num2=data;
end
% mindata=min(fcsdat);
% for i=1:size(mindata,2);
% if mindata(i)<0
% mindataapp=repmat(mindata(i),size(fcsdat,1),1);
% fcsdat(:,i)=fcsdat(:,i)+abs(mindataapp);
% end
% end
% mindata=repmat(mindata,size(fcsdat,1),1);
% fcsdat=fcsdat+abs(mindata);
handles.ChannelsAll=header;
handles.channelselect.String=header;
handles.xaxis.String=header;
handles.yaxis.String=header;
handles.heatmaptsne.String=header;
end
guidata(hObject,handles);
msgbox('Data Imported');
% --- Executes during object creation, after setting all properties.
function fileread1_CreateFcn(hObject, eventdata, handles)
% hObject handle to fileread1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in SelectFileButton.
function SelectFileButton_Callback(hObject, eventdata, handles)
% hObject handle to SelectFileButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=ResetGUI(handles);
[filename,folder]=uigetfile({'*.fcs'},'Select file','MultiSelect','on');
handles.files.String=filename;
if ~iscell(filename)
filename=cellstr(filename);
end
if strcmp(handles.eventperfile.String,'Events Per File') || isempty(handles.eventperfile.String)
subsample=0;
else
subsample=1;
sampleamnt=str2num(handles.eventperfile.String);
end
num=[];
num2=[];
for i=1:size(filename,2)
filenamequery=filename(i);
filenamequery=fullfile(folder,filenamequery);
try
[data, marker_names, channel_names, scaled_data, compensated_data, fcshdr] = readfcs_v2(filenamequery{1});
data=single(transpose(data+1));
compensated_data=single(transpose(compensated_data+1));
if subsample==1
if size(data,1)>sampleamnt || size(compensated_data,1)>sampleamnt
data=datasample(data,sampleamnt);
compensated_data=datasample(compensated_data,sampleamnt);
end
%data=asinh(data/5);
%compensated_data=asinh(data/5);
end
num=[num;compensated_data];
num2=[num2;data;];
% handles.num=compensated_data;
% handles.num2=data;
header=marker_names;
catch
[data, fcshdr, fcsdatscaled, compensated_data] = fca_readfcs(filenamequery{1});
data=single(double(data+1));
if subsample==1
if size(data,1)>sampleamnt
data=datasample(data,sampleamnt);
end
%data=asinh(data/5);
end
headerdata=fcshdr.par;
for j=1:size(headerdata,2);
if ~isempty(headerdata(j).name2)
header{j}=headerdata(j).name2;
else
header{j}=headerdata(j).name;
end
end
channel_names = header
% handles.num=data;
% handles.num2=data;
num=[num;data];
num2=[num2;data];
end
end
handles.num=num;
handles.num2=num2;
handles.ChannelsAll=header;
handles.channel_colors = channel_names;
handles.channelselect.String=header;
handles.xaxis.String=header;
handles.yaxis.String=header;
handles.heatmaptsne.String=header;
guidata(hObject,handles);
msgbox('Data Imported');
%[filename,folder]=uigetfile({'*.fcs'},'Select file');
%filename=fullfile(folder, filename);
%set(handles.fileread1,'string',filename);
%fileread1_Callback(hObject,eventdata,handles);
function handles=ResetGUI(handles);
fieldremove={'num','num2','ChannelsAll','num_samples','channel_select','y2','ChannelsOut','y','Y','tsne_xlim','tsne_ylim'};
for i=1:size(fieldremove,2);
if isfield(handles,fieldremove{i})
handles=rmfield(handles,fieldremove{i});
end
end
handles.numsamples.String='';
handles.perc_file.String='';
handles.channelselect.Value=[];
handles.channelselect.String={};
handles.heatmaptsne.Value=1;
handles.heatmaptsne.String={'Channel'};
handles.popupmenu1.Value=1;
handles.popupmenu1.String={'Channel'};
handles.popupmenu2.Value=1;
handles.popupmenu2.String={'Channel'};
% --- Executes on button press in tnsebutton.
function tnsebutton_Callback(hObject, eventdata, handles)
% hObject handle to tnsebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num=handles.num;
if ~isfield(handles,'num_samples')
msgbox('Number of Samples Not Entered','Error','error');
end
if ~isfield(handles,'y')
y=datasample(num,handles.num_samples);
handles.y2=y;
if handles.inst_type.Value==1
y=asinh(y/150);
handles.transy2=y;
elseif handles.inst_type.Value==2;
y=asinh(y/5);
handles.transy2=y;
end
else
if handles.num_samples>size(handles.y2,1)
y=datasample(num,handles.num_samples);
handles.y2=y;
if handles.inst_type.Value==1
y=asinh(y/150);
handles.transy2=y;
elseif handles.inst_type.Value==2;
y=asinh(y/5);
handles.transy2=y;
end
else
y=handles.y2;
y=datasample(y,handles.num_samples);
handles.y2=y;
if handles.inst_type.Value==1
y=asinh(y/150);
handles.transy2=y;
elseif handles.inst_type.Value==2;
y=asinh(y/5);
handles.transy2=y;
end
end
end
if ~isfield(handles,'channel_select')
msgbox('Select Channels For Analysis','Error','error');
end
y=y(:,handles.channel_select);
handles.y=handles.y2(:,handles.channel_select);
handles.transy=y;
handles.ChannelsOut=handles.ChannelsAll(handles.channel_select);
handles.popupmenu1.String=handles.ChannelsAll;
handles.popupmenu2.String=handles.ChannelsAll;
normalizetsne=1;
hbox=msgbox('Running t-SNE Analysis');
Y=tsne(y,'Standardize',normalizetsne,'Distance',handles.Distance_Measure.String{handles.Distance_Measure.Value},'Perplexity',str2num(handles.Perplexity.String));
close(hbox);
handles.Y=Y;
scatter(handles.axes1,Y(:,1),Y(:,2),'filled');
handles.axes1.XTickLabel={};
handles.axes1.YTickLabel={};
handles.tsne_xlim=handles.axes1.XLim;
handles.tsne_ylim=handles.axes1.YLim;
guidata(hObject,handles);
function clusterparameter_Callback(hObject, eventdata, handles)
% hObject handle to clusterparameter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of clusterparameter as text
% str2double(get(hObject,'String')) returns contents of clusterparameter as a double
key = get(gcf,'CurrentKey');
if(strcmp (key , 'return'))
clusterbutton_Callback(hObject, eventdata, handles)
end
% --- Executes during object creation, after setting all properties.
function clusterparameter_CreateFcn(hObject, eventdata, handles)
% hObject handle to clusterparameter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in clustermethodbox.
function clustermethodbox_Callback(hObject, eventdata, handles)
% hObject handle to clustermethodbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns clustermethodbox contents as cell array
% contents{get(hObject,'Value')} returns selected item from clustermethodbox
sel=handles.clustermethodbox.Value;
if ismember(sel,[1 2 6 7 8])
set(handles.clusterparameter,'String','# of Clusters');
elseif ismember(sel,[3 4])
set(handles.clusterparameter,'String','Distance Factor');
elseif ismember(sel,[5])
set(handles.clusterparameter,'String','k-nearest neighbors');
end
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function clustermethodbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to clustermethodbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function numsamples_Callback(hObject, eventdata, handles)
% hObject handle to numsamples (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of numsamples as text
% str2double(get(hObject,'String')) returns contents of numsamples as a double
handles.num_samples=str2num(handles.numsamples.String);
handles.perc_file.String=num2str(100*handles.num_samples/size(handles.num,1));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function numsamples_CreateFcn(hObject, eventdata, handles)
% hObject handle to numsamples (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function perc_file_Callback(hObject, eventdata, handles)
% hObject handle to perc_file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of perc_file as text
% str2double(get(hObject,'String')) returns contents of perc_file as a double
handles.num_samples=round((str2num(handles.perc_file.String)/100)*size(handles.num,1));
handles.numsamples.String=num2str(handles.num_samples);
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function perc_file_CreateFcn(hObject, eventdata, handles)
% hObject handle to perc_file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in channelselect.
function channelselect_Callback(hObject, eventdata, handles)
% hObject handle to channelselect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns channelselect contents as cell array
% contents{get(hObject,'Value')} returns selected item from channelselect
handles.channel_select=handles.channelselect.Value;
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function channelselect_CreateFcn(hObject, eventdata, handles)
% hObject handle to channelselect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function uipanel3_CreateFcn(hObject, eventdata, handles)
% hObject handle to uipanel3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in clusterbutton.
function clusterbutton_Callback(hObject, eventdata, handles)
% hObject handle to clusterbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fieldremove={'ManualClusterCount','colorspec1','idx','ClusterMethod','ClusterContrib','num_clusters','HeatMapData','RowLabels','SizeCluster','I','Imod','Ifinal','line','thresholdcurrent','thresholdbook','threshold_count','graphclustermethod'};
for i=1:size(fieldremove,2);
if isfield(handles,fieldremove{i})
handles=rmfield(handles,fieldremove{i});
end
end
handles.threshlist.String={};
handles.cluster_sel.String={};
handles.cluster_sel.Value=[];
handles.clusterplot.String={};
handles.clusterplot.Value=[];
handles.clusterfreq.String='';
handles.popupmenu1.Value=1;
handles.popupmenu2.Value=1;
Y=handles.Y;
ClusterMethod=handles.clustermethodbox.Value;
clusterparameter=str2num(handles.clusterparameter.String);
switch ClusterMethod
case 1
hbox=msgbox('Clustering Events...');
num_clusters=clusterparameter;
if floor(num_clusters) ~= num_clusters;
msgbox('Number of Clusters must be an integer value!','Error','error');
return
end
idx=kmeans(Y,num_clusters,'Start','uniform');
case 2
hbox=msgbox('Clustering Events...');
num_clusters=clusterparameter;
if floor(num_clusters) ~= num_clusters;
msgbox('Number of Clusters must be an integer value!','Error','error');
return
end
idx=kmeans(handles.transy,num_clusters,'Start','uniform');
% elseif ClusterMethod==3
%
% hbox=msgbox('Clustering Events...');
% [centers,U] = fcm(Y,clusterparameter);
% thresh=0.75;
% parfor i=1:size(U,2);
% probdist=U(:,i);
% probdist=probdist>thresh
% idxi=find(probdist);
% if sum(idxi)==0
% idx(i)=clusterparameter+1;
% else
% idx(i)=idxi;
% end
% end
% num_clusters=clusterparameter+1;
% idx=transpose(idx);
% elseif ClusterMethod==4
% hbox=msgbox('Clustering Events...');
% [centers,U] = fcm(handles.y,clusterparameter);
% thresh=0.75;
% parfor i=1:size(U,2);
% probdist=U(:,i);
% probdist=probdist>thresh
% idxi=find(probdist);
% if sum(idxi)==0
% idx(i)=clusterparameter+1;
% else
% idx(i)=idxi;
% end
% end
% num_clusters=clusterparameter+1;
% idx=transpose(idx);
case 3
hbox=msgbox('Clustering Events...');
epsilonf=clusterparameter/100;
D=pdist(Y);
epsilon=(epsilonf)*median(D); %.02 default
MinPoints=1;%(0.0001)*size(Y,1); %.0001 default
[idx,isnoise]=DBSCAN(Y,epsilon,MinPoints);
num_clusters=max(idx);
case 4
hbox=msgbox('Clustering Events...');
dm=pdist(handles.transy);
z=linkage(dm);
idx=cluster(z,'cutoff',clusterparameter);
num_clusters=max(idx);
case 5
NetworkGui(handles);
waitfor(findobj('Tag','networkgui'));
hbox=msgbox('Creating Graph...');
[G,GGraph]=CreateGraph(handles.transy,clusterparameter);
close(hbox);
handles=guidata(findobj('Tag','clusterbutton'));
switch handles.graphclustermethod
case 1
hbox=msgbox('Clustering Events...');
N=length(G);
W=PermMat(N); % permute the graph node labels
A=W*G*W';
%%RG Version
% [row,col,val]=find(A);
% input=table(row,col,val);
% writetable(input,'G.csv');
% RG_clust('G.csv')
[COMTY ending] = cluster_jl_cppJW(A,1);
J=size(COMTY.COM,2);
VV=COMTY.COM{J}';
idx=W'*VV;
case 2
hbox=msgbox('Clustering Events...');
idx=GCModulMax2(G);
case 3
hbox=msgbox('Clustering Events...');
idx=GCModulMax3(G);
case 4
hbox=msgbox('Clustering Events...');
idx=GCDanon(G);
case 5
clusterparameter2=inputdlg('Enter # of Clusters');
hbox=msgbox('Clustering Events...');
clusterparameter2=str2num(clusterparameter2{1});
idx=GCSpectralClust1(G,clusterparameter2);
idx=idx(:,clusterparameter2);
end
num_clusters=max(idx);
case 6
num_clusters = clusterparameter;
if floor(num_clusters) ~= num_clusters;
msgbox('Number of Clusters must be an integer value!','Error','error');
return
end
hbox=msgbox('Clustering Events...');
net=selforgmap([round(sqrt(clusterparameter)),round(sqrt(clusterparameter))]);
net.trainParam.showWindow = false;
net=train(net,transpose(handles.transy));
idx=transpose(vec2ind(net(transpose(handles.transy))));
num_clusters=max(idx);
case 7
num_clusters = clusterparameter;
if floor(num_clusters) ~= num_clusters;
msgbox('Number of Clusters must be an integer value!','Error','error');
return
end
hbox=msgbox('Clustering Events...');
try
idx=transpose(mixGaussEm(transpose(handles.transy),clusterparameter));
num_clusters=max(idx);
catch
msgbox('Enter smaller # of Clusters');
end
case 8
num_clusters = clusterparameter;
if floor(num_clusters) ~= num_clusters;
msgbox('Number of Clusters must be an integer value!','Error','error');
return
end
hbox=msgbox('Clustering Events...');
try
idx=transpose(mixGaussVb(transpose(handles.transy),clusterparameter));
num_clusters=max(idx);
catch
msgbox('Enter smaller # of Clusters');
end
end
close(hbox);
[colorspec1,colorspec2]=CreateColorTemplate(num_clusters);
handles.colorspec1=colorspec1;
clear colorscheme
for i=1:size(idx,1);
colorscheme(i,:)=colorspec1(idx(i)).spec;
end
scatter(handles.axes1,Y(:,1),Y(:,2),[],colorscheme,'filled');
handles.axes1.XTickLabel={};
handles.axes1.YTickLabel={};
y=handles.transy;
ClusterContrib=tabulate(idx);
for i=1:num_clusters
ClusterNames(i)=strcat('Cluster ',num2str(i),{' - '},num2str(ClusterContrib(i,3)),{'%'});
end
[HeatMapData,RowLabels,SizeCluster]=GetHeatMapData(num_clusters,idx,y,ClusterMethod,ClusterContrib);
handles.cluster_sel.String=ClusterNames;
handles.idx=idx;
handles.ClusterContrib=ClusterContrib;
handles.num_clusters=num_clusters;
handles.HeatMapData=HeatMapData;
handles.RowLabels=RowLabels;
handles.SizeCluster=SizeCluster;
handles.I=[1:num_clusters];
handles.Imod=handles.I;
guidata(hObject,handles);
datacursormode on
dcm_obj=datacursormode(handles.axes1.Parent);
set(dcm_obj,'UpdateFcn',{@myupdatefcn,idx,Y})
function NetworkGui(handles)
fh = figure('units','pixels',...
'units','normalized',...
'position',[0.25 0.25 .175 .125],...
'menubar','none',...
'name','Select Graph Clustering Method',...
'tag','networkgui',...
'numbertitle','off',...
'resize','off');
guidata(fh,handles);
clusteroptions=uicontrol('Style','listbox',...
'String',{'Modularity Max - Louvain';'Modularity Max - Fast Greedy';'Modularity Max - Newman';...
'Danon Method';'Spectral Clustering'},...
'units','normalized',...
'Position',[.1,.3,0.8,0.6],...
'Tag','graphclusteropt');
selectbutton=uicontrol('Style','pushbutton',...
'String','Select',...
'units','normalized',...
'Position',[0.75,.1,.2,0.2],...
'Tag','selectgraphcluster',...
'Callback',@selectgraphcluster);
function selectgraphcluster(hObject, eventdata)
temp=findobj('Tag','graphclusteropt');
clustergraphmethod=temp.Value;
handles=guidata(hObject);
handles.graphclustermethod=clustergraphmethod;
guidata(findobj('Tag','clusterbutton'),handles);
closereq;
% --- Executes on button press in selcluster.
function selcluster_Callback(hObject, eventdata, handles)
% hObject handle to selcluster (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dcm_obj=datacursormode(handles.axes1.Parent);
set(dcm_obj,'Enable','off');
if ~isfield(handles,'ManualClusterCount')
if isfield(handles,'idx')
handles=rmfield(handles,'idx');
handles=rmfield(handles,'colorspec1');
end
[colorspec1,colorspec2]=CreateColorTemplate(100);
handles.colorspec1=colorspec1;
handles.cluster_sel.String={};
Y=handles.Y;
scatter(handles.axes1,Y(:,1),Y(:,2),'filled');
handles.axes1.XTickLabel={};
handles.axes1.YTickLabel={};
handles.axes1.Title.String='TSNE';
hold(handles.axes1)
handles.ManualClusterCount=1;
I=1;
else
hold(handles.axes1)
handles.ManualClusterCount=handles.ManualClusterCount+1;
colorspec1=handles.colorspec1;
idx=handles.idx;
I=handles.I;
I=[I,I(end)+1];
handles.I=I;
handles.Imod=I;
ClusterContrib=handles.ClusterContrib;
end
sel=selectdata('SelectionMode','Lasso','Verify','on');
if handles.ManualClusterCount~=1
sel=sel{handles.ManualClusterCount};
end
in=zeros(size(handles.Y,1),1);
in(sel)=1;
in=logical(in);
%[x,y]=ginput;
%in=inpolygon(handles.Y(:,1),handles.Y(:,2),x,y);
Yplot=handles.Y(in,:);
hold(handles.axes1);
scatter(handles.axes1,Yplot(:,1),Yplot(:,2),[],colorspec1(handles.ManualClusterCount).spec,'filled');
handles.axes1.XLim=handles.tsne_xlim;
handles.axes2.YLim=handles.tsne_ylim;
hold(handles.axes1);
ClusterContrib(handles.ManualClusterCount,1)=handles.ManualClusterCount;
ClusterContrib(handles.ManualClusterCount,2)=sum(in);
ClusterContrib(handles.ManualClusterCount,3)=100*(sum(in)/size(in,1));
handles.ClusterContrib=ClusterContrib;
sortedlist=cell(1,size(I,2));
for i=1:size(I,2);
sortedlist(i)=strcat({'Cluster '},num2str(I(i)),{' - '},num2str(ClusterContrib(I(i),3)),{'%'});
end
if handles.ManualClusterCount==1;
handles.cluster_sel.Value=[];
handles.cluster_sel.String=sortedlist;
handles.idx=double(in);
handles.I=I;
handles.Imod=I;
else
p=handles.ManualClusterCount;
idx=handles.idx+p*double(in);
handles.idx=double(idx);
handles.cluster_sel.Value=[];
handles.cluster_sel.String=sortedlist;
handles=HeatMap_CallbackManual(hObject, eventdata, handles);
end
guidata(hObject,handles);
% --- Executes on selection change in cluster_sel.
function cluster_sel_Callback(hObject, eventdata, handles)
% hObject handle to cluster_sel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns cluster_sel contents as cell array
% contents{get(hObject,'Value')} returns selected item from cluster_sel
% --- Executes during object creation, after setting all properties.
function cluster_sel_CreateFcn(hObject, eventdata, handles)
% hObject handle to cluster_sel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in clearclusters.
function clearclusters_Callback(hObject, eventdata, handles)
% hObject handle to clearclusters (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fieldremove={'ManualClusterCount','colorspec1','idx','ClusterMethod','ClusterContrib','num_clusters','HeatMapData','RowLabels','SizeCluster','I','Imod','Ifinal','line','thresholdcurrent','thresholdbook','threshold_count'};
for i=1:size(fieldremove,2);
if isfield(handles,fieldremove{i})
handles=rmfield(handles,fieldremove{i});
end
end
handles.threshlist.String={};
handles.cluster_sel.String={};
handles.cluster_sel.Value=[];
handles.clusterplot.String={};
handles.clusterplot.Value=[];
handles.clusterfreq.String='';
handles.popupmenu1.Value=1;
handles.popupmenu2.Value=1;
Y=handles.Y;
scatter(handles.axes1,Y(:,1),Y(:,2),'filled');
handles.axes1.XTickLabel={};
handles.axes1.YTickLabel={};
guidata(hObject,handles);
% --- Executes on selection change in popupmenu1.
function handles=popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
handles=SortClusters(handles);
handles=ApplyCurrentThresh(handles);
handles=ClusterCut(handles);
PlotSelectClusters(handles);
guidata(hObject,handles);
function handles=SortClusters(handles)
HeatMapData=handles.HeatMapData;
ListC=[1:size(HeatMapData,1)];
clear HeatMapData
for j=ListC;
clusterselect=handles.idx==j;
SizeCluster(j)=sum(clusterselect);
clusterselect2=handles.transy2(clusterselect,:);
if size(clusterselect2,1)==1
HeatMapData(j,:)=clusterselect2;
else
HeatMapData(j,:)=median(clusterselect2);
end
end
channelsel=handles.popupmenu1.Value;
ClusterContrib=handles.ClusterContrib;
channelselstring=handles.ChannelsAll{channelsel};
valsort=strmatch(channelselstring,handles.ChannelsAll,'exact');
I=handles.I;
if handles.sortbutton.Value==0
[B,I2]=sortrows(HeatMapData,-valsort);
else
[B,I2]=sortrows(HeatMapData,valsort);
end
I2=transpose(intersect(I2,I,'stable'));
I=I2;
sortedlist=cell(1,size(I,2));
for i=1:size(I,2);
sortedlist(i)=strcat({'Cluster '},num2str(I(i)),{' - '},num2str(ClusterContrib(I(i),3)),{'%'});
end
handles.cluster_sel.Value=[];
handles.cluster_sel.String=sortedlist;
handles.Imod=I;
function handles=ApplyCurrentThresh(handles)
if isfield(handles,'thresholdbook')
thresholdbook=handles.thresholdbook;
HeatMapData=handles.HeatMapData;
for i=1:size(thresholdbook,2);
threshold_indx(i)=strmatch(thresholdbook(i).Channel,handles.ChannelsAll,'exact');
threshold_dir{i}=thresholdbook(i).direction;
threshold_val(i)=thresholdbook(i).threshold;
end
ListC=[1:size(HeatMapData,1)];
clear HeatMapData
thresh_cut_ind=ListC;
for j=ListC;
clusterselect=handles.idx==j;
SizeCluster(j)=sum(clusterselect);
clusterselect2=handles.transy2(clusterselect,:);
if size(clusterselect2,1)==1
HeatMapData(j,:)=clusterselect2;
else
HeatMapData(j,:)=median(clusterselect2);
end
end
for i=1:size(threshold_indx,2);
thresh_cut=HeatMapData(:,threshold_indx(i));
eval(['thresh_cut=thresh_cut' threshold_dir{i} 'threshold_val(i);'])
thresh_cut_ind=intersect(ListC(thresh_cut),thresh_cut_ind);
end
I=handles.Imod;
I=intersect(I,thresh_cut_ind,'stable');
sortedlist=cell(1,size(I,2));
for i=1:size(I,2);
sortedlist(i)=strcat({'Cluster '},num2str(I(i)),{' - '},num2str(handles.ClusterContrib(I(i),3)),{'%'});
end
handles.cluster_sel.Value=[];
handles.cluster_sel.String=sortedlist;
handles.Imod=I;
end
function handles=ClusterCut(handles)
SizeCluster=handles.SizeCluster;
ClusterContrib=handles.ClusterContrib;
I=handles.Imod;
cut=str2num(handles.clusterfreq.String)/100;
if isempty(cut);
cut=0;
end
FreqCluster=SizeCluster./size(handles.y,1);
Keep=(FreqCluster>cut).*[1:size(FreqCluster,2)];
Keep(Keep==0)=[];
I=intersect(I,Keep,'stable');
handles.Imod=I;
if ~isempty(I)
for i=1:size(I,2);
sortedlist(i)=strcat({'Cluster '},num2str(I(i)),{' - '},num2str(ClusterContrib(I(i),3)),{'%'});
end
end