-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsc_mergesces.m
143 lines (126 loc) · 4.54 KB
/
sc_mergesces.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
function [sce] = sc_mergesces(sces, method, keepbatchid)
%Merges two SCE objects
%Usage: [sce]=sc_mergesces({sce1,sce2},'intersect');
%See also: SC_MERGEDATA
if nargin < 3, keepbatchid = true; end
if nargin < 2 || isempty(method), method = 'intersect'; end
validMethods = ["intersect", "union"];
method = validatestring(method, validMethods);
if ~iscell(sces), error('SCES is not a cell array.'); end
if length(sces) < 2, error('At least two SCE required.'); end
for k = 1:length(sces)
if ~isa(sces{k}, 'SingleCellExperiment')
error('sces{%d} is not a SingleCellExperiment object.', k);
end
end
needappendix=false;
sce = sces{1};
c = ones(sce.NumCells, 1);
for k = 2:length(sces)
c = [c; k * ones(sces{k}.NumCells, 1)];
[sce, hasidoverlapx] = i_merge2sces(sce, sces{k}, method);
if hasidoverlapx
needappendix=true;
end
end
if ~keepbatchid || isscalar(unique(sce.c_batch_id))
sce.c_batch_id = c;
end
if needappendix
sce.c_batch_id = strcat(string(sce.c_batch_id), "_", string(c));
warning('A suffix is added to SCE.C_BATCH_ID to distinguish cells'' original batch IDs.');
end
end
function [sce, hasidoverlap] = i_merge2sces(sce1, sce2, method)
hasidoverlap = false;
if nargin < 3, method = 'intersect'; end
[X, g, ~] = sc_mergedata(sce1.X, sce2.X, ...
sce1.g, sce2.g, method);
sce = SingleCellExperiment(X, g);
sce.metadata = [sce1.metadata; sce2.metadata];
sce.c = [sce1.c; sce2.c];
try
sce.s = [sce1.s; sce2.s];
catch
sce.s = randn(size(X, 2), 3);
end
% sce.c_batch_id=c;
if ~isempty(sce1.c_batch_id) && ~isempty(sce2.c_batch_id)
if ~isstring(sce1.c_batch_id)
sce1.c_batch_id = string(sce1.c_batch_id);
end
if ~isstring(sce2.c_batch_id)
sce2.c_batch_id = string(sce2.c_batch_id);
end
sce.c_batch_id = [sce1.c_batch_id; sce2.c_batch_id];
% intersect(sce1.c_batch_id, sce2.c_batch_id)
% pause
hasidoverlap = ~isempty(intersect(sce1.c_batch_id, sce2.c_batch_id));
end
if ~isempty(sce1.c_cell_cycle_tx) && ~isempty(sce2.c_cell_cycle_tx)
sce.c_cell_cycle_tx = [sce1.c_cell_cycle_tx; sce2.c_cell_cycle_tx];
end
if ~isempty(sce1.c_cell_type_tx) && ~isempty(sce2.c_cell_type_tx)
if ~isstring(sce1.c_cell_type_tx)
sce1.c_cell_type_tx = string(sce1.c_cell_type_tx);
end
if ~isstring(sce2.c_cell_type_tx)
sce2.c_cell_type_tx = string(sce2.c_cell_type_tx);
end
sce.c_cell_type_tx = [i_remove_affix(sce1.c_cell_type_tx); ...
i_remove_affix(sce2.c_cell_type_tx)];
end
if ~isempty(sce1.c_cluster_id) && ~isempty(sce2.c_cluster_id)
sce.c_cluster_id = [sce1.c_cluster_id; sce2.c_cluster_id];
end
if ~isempty(sce1.c_cell_id) && ~isempty(sce2.c_cell_id)
sce.c_cell_id = [sce1.c_cell_id; sce2.c_cell_id];
end
try
if ~isempty(sce1.list_cell_attributes) && ~isempty(sce2.list_cell_attributes)
for k = 1:2:min([length(sce1.list_cell_attributes), length(sce2.list_cell_attributes)])
if strcmp(sce1.list_cell_attributes{k}, ...
sce2.list_cell_attributes{k})
sce.list_cell_attributes{k} = sce1.list_cell_attributes{k};
sce.list_cell_attributes{k+1} = [sce1.list_cell_attributes{k+1}; ...
sce2.list_cell_attributes{k+1}];
end
end
end
catch
warning('SCE.LIST_CELL_ATTRIBUTES not merged.')
end
try
a = fields(sce.struct_cell_clusterings);
for k = 1:length(a)
c1 = sce1.struct_cell_clusterings.(a{k});
c2 = sce2.struct_cell_clusterings.(a{k});
if ~isempty(c1) && ~isempty(c2)
sce.struct_cell_clusterings.(a{k}) = [c1; c2];
end
end
catch
warning('SCE.STRUCT_CELL_CLUSTERINGS not merged.')
end
try
a = fields(sce.struct_cell_embeddings);
for k = 1:length(a)
c1 = sce1.struct_cell_embeddings.(a{k});
c2 = sce2.struct_cell_embeddings.(a{k});
if ~isempty(c1) && ~isempty(c2)
sce.struct_cell_embeddings.(a{k}) = [c1; c2];
end
end
catch
% warning('SCE.STRUCT_CELL_EMBEDDINGS not merged.')
end
end
function b = i_remove_affix(a)
b = a;
for k = 1:length(a)
idx = strfind(a(k), '_{');
if idx > 0
b(k) = extractBefore(a(k), idx);
end
end
end