-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsc_knngraph.m
81 lines (75 loc) · 2.09 KB
/
sc_knngraph.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
function [A, W] = sc_knngraph(s, k, plotit, methodid, parentfig)
%Generate KNN group network from cell embeddings
%
% input: S - cell embedding coordinates
% output: A - sparse adjacency matrix
%
if nargin < 5, parentfig = []; end
if nargin < 4, methodid = 1; end
if nargin < 3, plotit = false; end
if nargin < 2 || isempty(k), k = 4; end
if isempty(parentfig)
ax = gca;
else
ax = findall(parentfig, 'Type', 'axes');
end
switch methodid
case 1
[mIdx] = knnsearch(s, s, 'K', k+1);
Graph = mIdx';
case 2
pw1 = fileparts(mfilename('fullpath'));
pth = fullfile(pw1, '+run', 'thirdparty', 'k-NN-code');
if ~(ismcc || isdeployed), addpath(pth); end
kneighbors = k; % number of neighbors in kNN
S = s';
[dim, N] = size(S);
rrw = S(:);
[~, Graph] = kNNgraphmex(rrw, N, dim, kneighbors, 1);
Graph = reshape(Graph, kneighbors+1, N);
end
if nargout > 0 || plotit
N = size(s, 1);
A = zeros(N, N);
if nargout > 1
W = zeros(N, N);
end
for i = 1:size(Graph, 2)
for j = 1:size(Graph, 1) % k+1
A(i, Graph(j, i)) = 1;
A(Graph(j, i), i) = 1;
if nargout > 1
w = norm(s(i, :)-s(Graph(j, i), :));
W(i, Graph(j, i)) = w;
W(Graph(j, i), i) = w;
end
end
end
% G=0.5*(G+G');
A = A - diag(diag(A));
A = sparse(A);
end
if plotit
hold on
for i = 1:size(Graph, 2)
for j = 1:size(Graph, 1)
% if i~=Graph(j,i)
if A(i, Graph(j, i)) > 0
if size(s, 2) >= 3
line(ax, s([i, Graph(j, i)], 1), ...
s([i, Graph(j, i)], 2), ...
s([i, Graph(j, i)], 3), 'Color', 'red');
else
line(ax, s([i, Graph(j, i)], 1), ...
s([i, Graph(j, i)], 2), 'Color', 'red');
end
end
end
end
hold off
end
end
% G=graph(W);
% d = distances(G);
% [diameter, long_ind] = max(d(:));
% [a,b] = ind2sub(size(d), long_ind)