-
Notifications
You must be signed in to change notification settings - Fork 9
/
IMACytE.m
795 lines (633 loc) · 28.5 KB
/
IMACytE.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
function varargout = IMACytE(varargin)
% Copyright 2019 Antonios Somarakis (LUMC) ImaCytE toolbox
% IMACYTE MATLAB code for IMACytE.fig
% IMACYTE, by itself, creates a new IMACYTE or raises the existing
% singleton*.
%
% H = IMACYTE returns the handle to a new IMACYTE or the handle to
% the existing singleton*.
%
% IMACYTE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IMACYTE.M with the given input arguments.
%
% IMACYTE('Property','Value',...) creates a new IMACYTE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before IMACytE_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to IMACytE_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 IMACytE
% Last Modified by GUIDE v2.5 31-Oct-2019 14:38:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @IMACytE_OpeningFcn, ...
'gui_OutputFcn', @IMACytE_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 IMACytE is made visible.
function IMACytE_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 IMACytE (see VARARGIN)
% clear variables that may are mis-initialized from previous run
clear global
cla(handles.uipanel4)
cla(handles.uipanel1)
cla(handles.uipanel2)
setappdata(handles.figure1,'clustMembsCell',[])
set(handles.Markerlist,'String',[]);
set(handles.uipanel1,'Visible','off')
set(handles.uipanel4,'Visible','off')
set(handles.uipanel2,'Visible','off')
set(handles.slider2,'Visible','off')
set(handles.arcsin,'Value',0);
set(handles.arcsin,'Visible','off');
set(handles.Compute_map,'Visible','off');
setappdata(handles.figure1,'count_interaction',0);
setappdata(handles.figure1,'segment_value',0);
% Choose default command line output for untitled
handles.output = hObject;
%% Initialize and add more instances in the options menu
f_scatter=handles.uipanel1; % where scatter plot is gonna be illustrated in the tool
set(f_scatter,'Tag','tsne_plot');
setappdata(handles.figure1,'Scatter_Figure',f_scatter);
d = uicontextmenu(get(f_scatter,'Parent')); %contect menu for saving_as the samples
Sava_as_interaction1=uimenu('Parent',d,'Label','Save_as','Callback',{@Save_as_Context_Menu, f_scatter});
set(f_scatter,'UIContextMenu',d);
f_image=handles.uipanel2; % where images of samples are gonna be illustrated in the tool
set(f_image,'Tag','Images_panel');
setappdata(handles.figure1,'Tissue_Figure',f_image);
d = uicontextmenu(get(f_image,'Parent')); %contect menu for saving_as the samples
Sava_as_interaction2=uimenu('Parent',d,'Label','Save_as','Callback',{@Save_as_Context_Menu, f_image});
set(f_image,'UIContextMenu',d);
f_heatmap=handles.uipanel4; % where heatmap is gonna be illustrated in the tool
set(f_heatmap,'Tag','heatmap')
setappdata(handles.figure1,'Heatmap_Figure',f_heatmap);
d = uicontextmenu(get(f_heatmap,'Parent')); %contect menu for saving_as the samples
Sava_as_interaction3=uimenu('Parent',d,'Label','Save_as','Callback',{@Save_as_Context_Menu, f_heatmap});
set(f_heatmap,'UIContextMenu',d);
% Cohort Comparison
mitem = uimenu(handles.figure1,'Text','Cohort Comparison');
set(mitem,'MenuSelectedFcn',{@Cohort_comparison_Load,handles})
% Load Raw Images
mitem = uimenu(handles.Load_data,'Text','Load Raw Images');
flag1=[0 1];
set(mitem,'MenuSelectedFcn',{@Load_Data_,flag1,handles})
% Load Thresholded Values
mitem = uimenu(handles.Load_data,'Text','Load Thresholded Values');
flag2=[1 1];
set(mitem,'MenuSelectedFcn',{@Load_Data_,flag2,handles})
% Load normalized data
mitem = uimenu(handles.Load_data,'Text','Load Normalized Data');
flag3=[1 0];
set(mitem,'MenuSelectedFcn',{@Load_Data_,flag3,handles})
% % Include Batch effect removal button Under Options Menu
% mitem = uimenu(handles.Options.Children(1),'Text','Batch effect removal');
% set(mitem,'MenuSelectedFcn',{@Batch_effect_Removal_Callback,handles})
% Enable samples clustering Under Options Menu
mitem = uimenu(handles.Options.Children(1),'Text','Cluster samples based on Phenotypes');
set(mitem,'MenuSelectedFcn',{@Cluster_per_sample,handles})
% Enable overlay in scatter plot Under Options Menu
mitem = uimenu(handles.Options.Children(1),'Text','Overlay Metadata to tSNE');
set(mitem,'MenuSelectedFcn',{@Scatter_overlay,handles})
% Initializes the panels of the tool that depict the main figures
gg=get(handles.uipanel2,'Children');
delete(gg);
set(handles.uipanel2,'Visible','on')
set(handles.arcsin,'Visible','on');
set(handles.Compute_map,'Visible','on');
uicontrol(handles.uipanel2,'Style', 'pushbutton', 'String', 'Samples','Units','normalized','position', [0.1 0.96 0.2 0.025 ], 'callback', {@ Samples_Callbacki, handles});
uicontrol(handles.uipanel2,'Style', 'pushbutton', 'String', 'Markers','Units','normalized','position', [0.7 0.96 0.2 0.025 ], 'callback', {@ Markers_Callbacki, handles});
%% Update handles structure
guidata(hObject, handles);
setappdata(0,'mygui',gcf);
set(handles.figure1,'windowbuttonmotionfcn',[]);
set( findall( gcf, '-property', 'Units' ), 'Units', 'Normalized' ) %in order to make window resizable
% UIWAIT makes IMACytE wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = IMACytE_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;
% set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
jFrame = get(handle(gcf),'JavaFrame');
jFrame.setMaximized(true);
% --- Executes on selection change in Markerlist.
function Markerlist_Callback(hObject, eventdata, handles)
% hObject handle to Markerlist (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 Markerlist contents as cell array
% contents{get(hObject,'Value')} returns selected item from Markerlist
%% This callback saves the selected features which would be used afterwars for the tsne map computation
selected_markers = get(hObject,'Value') ;
setappdata(handles.figure1,'selected_markers',selected_markers);
% --- Executes during object creation, after setting all properties.
function Markerlist_CreateFcn(hObject, eventdata, handles)
% hObject handle to Markerlist (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 Compute_map.
function Compute_map_Callback(hObject, eventdata, handles)
% hObject handle to Compute_map (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% This callback implements the tsne creation map
global n_data
global tsne_map
global cell4
global tsne_idx
tsne_idx=0;
%% Select which samples you want to include in your analysis
samples={cell4(:).name};
selected_samples=listdlg('PromptString','Select samples to utilize:','ListString',samples);
cell4=cell4(selected_samples);
setappdata(handles.figure1,'selection_samples',[]); % you currently deselct all the illustrated images
%% You redifine the tsne_idx and n_data in case less samples were selected
prev=0;
for i=1:length(cell4)
tsne_idx(prev+1:prev+length(cell4(i).idx))=i;
prev=prev+ length(cell4(i).idx);
end
n_data=double(vertcat(cell4(:).data));
%% Select which features to use for tsne map creation
markerlist=getappdata(handles.figure1,'selected_markers');
if isempty(markerlist); warndlg('Please select features for tSNE'); return; end
used_data=n_data(:,markerlist);
%% Select the tsne version you want to use
% tsne_choice=listdlg('PromptString','Select tSNE method to utilize:','ListString',{'tSNE', 'A-tSNE','Texture A-tSNE'},'SelectionMode','single');
tsne_choice=1;
f = waitbar(0,'Please wait...');
switch tsne_choice
case 1
try
tsne_map=tsne(my_normalize(used_data,'column'));
catch ME
rethrow(ME)
end
case 2
try
tsne_map=fast_atsne(my_normalize(used_data,'column'));
catch ME
rethrow(ME)
end
case 3
try
tsne_map=my_texture_tsne(my_normalize(used_data,'column'));
catch ME
rethrow(ME)
end
end
waitbar(1,f,'Finished');
%% Create the scatter plot which gonna present the tsne map
f_scatter=getappdata(handles.figure1,'Scatter_Figure');
delete(get(f_scatter,'Children'));
x=axes(f_scatter);
scatter(x,tsne_map(:,1),tsne_map(:,2),'filled');
set(x,'Tag','Scatter_axes');
set(x,'Position',[0.05 0.05 0.9 0.9]);
%% Create the controls of the bandwidth range and min/max values they can get
handles.Slider = uicontrol(handles.figure1,'style', 'Slider', 'Min', 0, 'Max', 20, 'Value',10, 'Units','normalized','position', [0 0.9 0.067 0.025 ], 'callback', {@ slider2_Callback, handles});
handles.Text_val = uicontrol(handles.figure1,'Style','text','String','Value(#Clusters)','Units','normalized','position', [0.01 0.94 0.03 0.02 ]);
% handles.Edit_val = uicontrol(handles.figure1,'Style','text','String',num2str(get(handles.Slider,'Value')),'Units','normalized','position', [0.025 0.925 0.015 0.02 ]);
handles.Text_max = uicontrol(handles.figure1,'Style','text','String','Max','Units','normalized','position', [0.048 0.94 0.02 0.02 ]);
handles.Edit_max = uicontrol(handles.figure1,'Style','edit','String',num2str(get(handles.Slider,'max')),'Units','normalized','position', [0.05 0.926 0.015 0.02], 'Callback',{@ MaxCallback, handles});
guidata(handles.figure1,handles)
set(handles.uipanel1,'Visible','on')
function MinCallback(hObject, ~, handles)
value_min=str2double(get(hObject, 'String'));
if value_min > str2double(get(handles.Slider, 'Value'))
set(handles.Slider, 'Value', value_min+1);
end
set(handles.Slider, 'Min', value_min);
guidata(handles.figure1,handles)
function MaxCallback(hObject, ~, handles)
value_max=str2double(get(hObject, 'String'));
if value_max < get(handles.Slider, 'Value')
set(handles.Slider, 'Value', value_max -0.5);
end
set(handles.Slider, 'Max', value_max);
guidata(handles.figure1,handles)
% --- Executes on button press in arcsin.
function arcsin_Callback(hObject, eventdata, handles)
% hObject handle to arcsin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of arcsin
%% This callback applies arcsin transformation to the data if has not been applied so far
global n_data
global cell4
setappdata(handles.figure1,'Arcsinh',get(hObject,'Value'))
if getappdata(handles.figure1,'Arcsinh')
n_data=asinh(double(vertcat(cell4(:).data))/5);
else
n_data=double(vertcat(cell4(:).data));
end
% --------------------------------------------------------------------
function Options_Callback(hObject, eventdata, handles)
% hObject handle to Options (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Save_Callback(hObject, eventdata, handles)
% hObject handle to Save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Load_data_Callback(hObject, eventdata, handles)
% hObject handle to Load_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Interaction_Analysis_Callback(hObject, eventdata, handles)
% hObject handle to Interaction_Analysis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%% This callback initializes the microenvironemnt exploration of the data which is performed from the Interactions_Motifs functions
global cell4
global tsne_idx
persistent origin_data
count=getappdata(handles.figure1,'count_interaction');
count=count+1;
if count==1
origin_data=cell4;
end
setappdata(handles.figure1,'count_interaction',count);
cell4=origin_data;
samples={cell4(:).name};
[selection_samples,~] = listdlg('PromptString','Select samples to utilize:','ListString',samples);
cell4=origin_data(selection_samples);
% indexes_left=find(ismember(tsne_idx,selection_samples));
tsne_idx=[];
for i=1:length(cell4)
tsne_idx=[ tsne_idx ones(1,length(cell4(i).data))*i];
end
handles=cell2clust(handles);
cmap=getappdata(handles.figure1,'cmap');
clustMembsCell=getappdata(handles.figure1, 'clustMembsCell');
cluster_names=getappdata(handles.figure1, 'cluster_names');
markerlist=getappdata(handles.figure1,'selected_markers');
% uisave({'clustMembsCell','cmap','cluster_names'},'For_interaction.mat')
Interactions_Motifs(clustMembsCell,cmap,1,cluster_names,markerlist);
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
%% This callback is executed any time the slider is moved
global tsne_map
global heatmap_selection
bandwidth=get(hObject,'Value');
[~,~,clustMembsCell] = HGMeanShiftCluster(tsne_map',bandwidth,'gaussian'); % Clustering in the tsne map is perfromed
handles.Edit_val = uicontrol(handles.figure1,'Style','text','String',[num2str(round(bandwidth,2)) '(' num2str(length(clustMembsCell)) ')' ],'Units','normalized','position', [0.02 0.925 0.03 0.02 ]);
try
clustMembsCell(cellfun(@isempty,clustMembsCell))=[];
setappdata(handles.figure1, 'clustMembsCell',clustMembsCell); %Save the clusters
clust2cell(handles)
cluster_names=cell(1,length(clustMembsCell)); % Assign random names to clusters
for i=1:length(clustMembsCell)
cluster_names{i}=['Cluster' num2str(i)];
end
setappdata(handles.figure1,'cluster_names',cluster_names);
setappdata(handles.figure1,'selection_markers',1); %Chekc if a unique markers has been selected or the clustering one
color_assignment( handles) % Assign the colors for each cluster according to the alogrithm described in the paper
Update_Scatter(handles); % Update the colors of the tsne map
Update_Tissue(handles); % Update the colors of the tissue samples
heatmap_data(handles) % Updates the heatmap to correspond with the colors and points assigned to each cluster
set(handles.uipanel4,'Visible','on')
set(handles.figure1,'windowbuttonmotionfcn',@mousemove);
heatmap_selection=[];
catch ME
warndlg(ME.message)
errordlg('Adjust the bandwidth accordingly')
end
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
set( hObject, 'Min', 1, 'Max', 30, 'Value', 15 )
% --- Executes on button press in Markers.
function Markers_Callbacki(~, ~, handles)
Markers_Callback([], [], handles)
function Samples_Callbacki(~, ~, handles)
Samples_Callback([],[], handles)
% --------------------------------------------------------------------
function Find_Selection_Callback(hObject, eventdata, handles)
% hObject handle to Find_Selection (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global find_selection
temp=listdlg('PromptString','Select samples to utilize:','ListString',{'Show previous selected samples'; 'Show only samples that have been selected'},'SelectionMode','single','InitialValue',find_selection + 1);
find_selection=temp -1;
% --------------------------------------------------------------------
function Export_data_Callback(hObject, eventdata, handles)
% hObject handle to Export_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Save_session_Callback(hObject, eventdata, handles)
% hObject handle to Save_session (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global tsne_map
global tsne_idx
markerlist=getappdata(handles.figure1,'selected_markers');
cluster_names=getappdata(handles.figure1, 'cluster_names');
clustMembsCell=getappdata(handles.figure1, 'clustMembsCell');
cmap=getappdata(handles.figure1, 'cmap');
t_scatter=tsne_map;
T=getappdata(handles.figure1,'h_cluster');
tsne_idx=tsne_idx;
uisave({'clustMembsCell','cmap','t_scatter','cluster_names','T','tsne_idx','markerlist'},'clustered_data.mat')
% --------------------------------------------------------------------
function Save_csv_Callback(hObject, eventdata, handles)
% hObject handle to Save_csv (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global cell4
r=uigetdir;
answer = questdlg('Do you want to save the phenotype with a unique number of with the Given phenotype name', ...
'', ...
'Number','Text','Text');
switch answer
case 'Number'
for i=1:length(cell4)
temo=cell4(i).clusters';
my_csvwrite([r '\' cell4(i).name '.csv'],temo,[]);
end
case 'Text'
for i=1:length(cell4)
cluster_names=getappdata(handles.figure1,'cluster_names');
temo=cluster_names(cell4(i).clusters);
writetable(cell2table(temo'),[r '\' cell4(i).name '.csv'],'WriteVariableNames',false);
end
end
% --------------------------------------------------------------------
function Load_per_sample_Callback(hObject, eventdata, handles)
% hObject handle to Load_per_sample (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global cell4
global heatmap_selection
[file,path] = uigetfile('*.csv','Select One or More Files', 'MultiSelect', 'on');
if ischar(file)
file2={};
file2{1}=file;
file=file2;
end
if length(cell4)~= length(file)
warndlg('Not all samples have phneotypes')
end
temp=cellfun(@(x) strsplit(x,'.csv'),file,'uni',0);
temp=cellfun(@(x) x{1},temp,'uni',0);
zero_clust=0;
for i=1:length(file)
ind=ismember(temp,{cell4(i).name});
if isempty(find(ind, 1))
warndlg(['Sample ' cell4(i).name ' has not assigned phenotype. All their cells will be assigned with a unique one'])
cell4(i).clusters=zeros(1,size(cell4(i).data,1));
zero_clust=[zero_clust i];
else
[number,txt]=xlsread([ path '\' file{ind}]);
if length(txt)<2
if isequal(number(1),'Var1')
cell4(i).clusters=number(2:end)';
else
cell4(i).clusters=number(1:end)';
end
else
if isequal(txt{1},'Var1')
cell4(i).clusters=txt(2:end)';
else
cell4(i).clusters=txt(1:end)';
end
end
if ~isequal(length(cell4.clusters),length(cell4.idx))
errordlg(['Different number of cells among clusters and mask for ' temp.name ' sample'])
end
end
end
prev=0;
for i=1:length(cell4)
tsne_idx(prev+1:prev+length(cell4(i).idx))=i;
prev=prev+ length(cell4(i).idx);
end
if iscell([cell4(:).clusters])
[cluster_names,~,cluster_ids]=unique([cell4(:).clusters]);
for i=1:length(cell4)
temp=cluster_ids(tsne_idx==i)';
cell4(i).clusters=temp;
end
else
numClust=max(horzcat(cell4(:).clusters));
for i=1:numClust
cluster_names{i}=['Cluster' num2str(i)];
end
end
for i=1:zero_clust
cell4(i).clusters=ones(1,size(cell4(i).data,1))*(max(horzcat(cell4(:).clusters))+1);
end
handles=cell2clust(handles);
markers=getappdata(handles.figure1,'markers');
[selection_markers,~] = listdlg('PromptString','Select markers to utilize:','ListString',markers);
setappdata(handles.figure1,'selected_markers',selection_markers);
setappdata(handles.figure1,'cluster_names',cluster_names);
setappdata(handles.figure1,'selection_markers',1);
color_assignment( handles)
try
Update_Scatter(handles);
catch
end
Update_Tissue(handles);
heatmap_data(handles)
set(handles.uipanel4,'Visible','on')
set(handles.figure1,'windowbuttonmotionfcn',@mousemove);
heatmap_selection=[];
% --------------------------------------------------------------------
function Load_per_phenotype_Callback(hObject, eventdata, handles)
% hObject handle to Load_per_phenotype (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global heatmap_selection
global tsne_idx
[file,path] = uigetfile('*.fcs','Select One or More Files', 'MultiSelect', 'on');
for i=1:length(file)
[a,~]=fca_readfcs([path '\' file{i}]);
clustMembsCell{i}=a(:,1)';
temp=strsplit(file{i},'.fcs');
cluster_names{i}=temp{1};
end
% cluster_names=cell(1,length(clustMembsCell));
% for i=1:length(clustMembsCell)
% cluster_names{i}=['Cluster' num2str(i)];
% end
if ~isequal(length(horzcat(clustMembsCell{:})),length(tsne_idx))
warndlg('Not all cells have an assigned phneotype')
try
clustMembsCell{end+1}=setdiff([1:length(tsne_idx)],vertcat(clustMembsCell{:}));
catch
clustMembsCell{end+1}=setdiff([1:length(tsne_idx)],horzcat(clustMembsCell{:}));
end
cluster_names{end+1}='Unclustered cells';
end
setappdata(handles.figure1, 'clustMembsCell',clustMembsCell);
clust2cell(handles);
markers=getappdata(handles.figure1,'markers');
[selection_markers,~] = listdlg('PromptString','Select markers to utilize:','ListString',markers);
setappdata(handles.figure1,'selected_markers',selection_markers);
setappdata(handles.figure1,'cluster_names',cluster_names);
setappdata(handles.figure1,'selection_markers',1);
color_assignment( handles)
try
Update_Scatter(handles);
catch
end
Update_Tissue(handles);
heatmap_data(handles)
set(handles.uipanel4,'Visible','on')
set(handles.figure1,'windowbuttonmotionfcn',@mousemove);
heatmap_selection=[];
% --------------------------------------------------------------------
function Export_fcs_file_Callback(hObject, eventdata, handles)
% hObject handle to Export_fcs_file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global cell4
prev=0;
r=uigetdir;
for i=1:length(cell4)
temo=1:size(cell4(i).data,1);
temo=temo+prev;
temo=[temo' ones(size(cell4(i).data,1),1)*(i) cell4(i).data];
temp_markers=[ 'Cell_id' 'Image_id' cell4(1).cell_markers];
fca_writefcs([r '\' cell4(i).name '_mean_aggregated.fcs'],temo,temp_markers,temp_markers);
prev=prev+ size(cell4(i).data,1);
end
% --------------------------------------------------------------------
function Export_csv_file_Callback(hObject, eventdata, handles)
% hObject handle to Export_csv_file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global cell4
prev=0;
r=uigetdir;
for i=1:length(cell4)
temo=1:size(cell4(i).data,1);
temo=temo+prev;
temo=[temo' ones(size(cell4(i).data,1),1)*(i) cell4(i).data];
temp_markers=[ 'Cell_id' 'Image_id' cell4(1).cell_markers];
my_csvwrite([r '\' cell4(i).name '_mean_aggregated.csv'],temo,temp_markers);
prev=prev+ size(cell4(i).data,1);
end
% --------------------------------------------------------------------
function Load_session_Callback(hObject, eventdata, handles)
% hObject handle to Load_session (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%% This callback loads a saved clustering
global heatmap_selection
global tsne_map
[file,path] = uigetfile('*.mat');
temp=load([path '\' file]);
clustMembsCell=temp.clustMembsCell;
setappdata(handles.figure1,'clustMembsCell',clustMembsCell)
clust2cell(handles)
if ~isempty(temp.cluster_names)
cluster_names=temp.cluster_names;
else
cluster_names=cell(1,length(clustMembsCell)); % Assign random names to clusters
for i=1:length(clustMembsCell)
cluster_names{i}=['Cluster' num2str(i)];
end
end
setappdata(handles.figure1,'cluster_names',cluster_names);
setappdata(handles.figure1,'selection_markers',1);
if ~isempty(temp.markerlist)
markerlist=temp.markerlist;
else
markers=getappdata(handles.figure1,'markers');
[markerlist,~] = listdlg('PromptString','Select markers to utilize:','ListString',markers);
end
setappdata(handles.figure1,'selected_markers',markerlist);
if ~isempty(temp.cmap)
cmap=temp.cmap;
setappdata(handles.figure1, 'cmap',cmap);
else
color_assignment( handles)
end
if ~isempty(temp.t_scatter) ; tsne_map=temp.t_scatter; end
try
Update_Scatter(handles);
catch
end
Update_Tissue(handles);
Update_Scatter(handles);
heatmap_data(handles)
set(handles.uipanel4,'Visible','on')
set(handles.uipanel1,'Visible','on')
set(handles.figure1,'windowbuttonmotionfcn',@mousemove);
heatmap_selection=[];
function Batch_effect_Removal_Callback(hObject, eventdata, handles)
% hObject handle to Batch_effect_Removal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global cell4
global tsne_map
global tsne_idx
tsne_idx=[];
tsne_map=[];
f = waitbar(0,'Please wait...');
cell4=debatching(cell4);
markers=cell4(1).cell_markers;
setappdata(handles.figure1,'markers',markers);
prev=0;
for i=1:length(cell4)
tsne_idx(prev+1:prev+length(cell4(i).idx))=i;
prev=prev+ length(cell4(i).idx);
end
waitbar(1,f,'Finished');
set(handles.Markerlist,'String',markers);
set(handles.Markerlist,'Value',[]);
set(handles.uipanel1,'Visible','off')
set(handles.uipanel4,'Visible','off')
set(handles.arcsin,'Visible','off')
delete(get(handles.uipanel2,'Children'));
uicontrol(handles.uipanel2,'Style', 'pushbutton', 'String', 'Samples','Units','normalized','position', [0.1 0.96 0.2 0.025 ], 'callback', {@ Samples_Callback, handles});
uicontrol(handles.uipanel2,'Style', 'pushbutton', 'String', 'Markers','Units','normalized','position', [0.7 0.96 0.2 0.025 ], 'callback', {@ Markers_Callback, handles});
function Scatter_overlay(hObject, eventdata, handles)
scatter_overlay(handles)
function Cluster_per_sample(hObject, eventdata, handles)
cluster_per_sample(handles)