-
Notifications
You must be signed in to change notification settings - Fork 10
/
sc_csubtypeanno.m
120 lines (98 loc) · 3.66 KB
/
sc_csubtypeanno.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
function [sce] = sc_csubtypeanno(sce, cell_type_target, formatid, speciestag)
if nargin < 4 || isempty(speciestag), speciestag='human'; end
if nargin < 3 || isempty(formatid), formatid = 0; end
selectedidx = sce.c_cell_type_tx == cell_type_target;
if ~any(selectedidx)
error('SCE.C_CELLTYPE_TXT does not contain the target cell type.');
end
pw1 = fileparts(mfilename('fullpath'));
pth2 = fullfile(pw1, 'resources', 'PanglaoDB', 'cellsubtypes.xlsx');
T = readtable(pth2);
if ~ismember(upper(cell_type_target), upper(string(unique(T.CellType))))
error('Target cell type is not a primary cell type in cellsubtype.xlsx.');
end
idx = upper(string(T.CellType)) == upper(cell_type_target);
if ~any(idx), error('SC_CSUBTYPEANNO: Runtime Error.'); end
T = T(idx,:);
[pmarkerstr] = in_getprimarymarkers(pw1, cell_type_target);
Tm = T(:,2:3);
[Tm] = in_addprimarymarkers(Tm, pmarkerstr);
[Tw] = pkg.e_markerweight(Tm);
% s = upper(string(Tm.PositiveMarkers));
% S = [];
% for k = 1:length(s)
% sk = s(k);
% a = strsplit(sk, ',');
% a = strtrim(a);
% if strlength(a(end)) == 0 || isempty(a(end))
% a = a(1:end-1);
% end
% S = [S, a];
% end
%
% %%
% N = length(S);
% t = tabulate(S);
% f = cell2mat(t(:, 3));
% if max(f) - min(f) < eps
% w = ones(N, 1);
% else
% w = 1 + sqrt((max(f) - f)/(max(f) - min(f)));
% end
% genelist = string(t(:, 1));
% Tw = table(genelist, w);
% Tw.Properties.VariableNames = {'Var1', 'Var2'};
wvalu = Tw.Var2;
wgene = string(Tw.Var1);
celltypev = string(Tm.SubType);
markergenev = string(Tm.PositiveMarkers);
sce2 = sce.selectcells(selectedidx);
sce2 = sce2.embedcells('tsne3d', true, true, 3);
sce2 = sce2.clustercells([], [], true);
[c, cL] = grp2idx(sce2.c_cluster_id);
for ik = 1:max(c)
ptsSelected = c == ik;
[Tct] = pkg.e_determinecelltype(sce2, ptsSelected, wvalu, ...
wgene, celltypev, markergenev);
ctxt = Tct.C1_Cell_Type{1};
cL{ik} = ctxt;
end
sce2.c_cell_type_tx = string(cL(c));
sce.c_cell_type_tx(selectedidx) = in_formatsubtype(sce2.c_cell_type_tx, ...
cell_type_target, formatid);
end
function [a]=in_formatsubtype(a, b, formatid)
switch formatid
case 0
return;
case 1
for k = 1:length(a)
a(k) = sprintf('%s_{%s}',b,a(k));
end
case 2
for k = 1:length(a)
a(k) = sprintf('%s (%s)',b,a(k));
end
end
end
function [primarymarkerstr] = in_getprimarymarkers(pw1, cell_type_target)
pth1 = fullfile(pw1, '+run', 'thirdparty', 'alona_panglaodb','marker_hs.mat');
load(pth1,'Tm');
if ~ismember(upper(cell_type_target), upper(string(Tm.Var1)))
error('Target cell type is not an available primary cell type.');
end
idx = upper(string(Tm.Var1)) == upper(cell_type_target);
primarymarkerstr = Tm.Var2{idx};
primarymarkerstr = strtrim(primarymarkerstr);
primarymarkerstr = erase(primarymarkerstr," ");
primarymarkerstr = strip(primarymarkerstr,'right',',');
end
function [Tm] = in_addprimarymarkers(Tm, pmarkerstr)
for k=1:size(Tm,1)
a = string(Tm.PositiveMarkers{k});
a = strtrim(a);
a = erase(a," ");
a = strip(a,'right',',');
Tm.PositiveMarkers{k} = char(string(a)+","+pmarkerstr);
end
end