-
Notifications
You must be signed in to change notification settings - Fork 10
/
sc_grnview.m
220 lines (200 loc) · 5.2 KB
/
sc_grnview.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
function sc_grnview(A, nodenames, figname, parentfig)
if nargin < 4, parentfig = []; end
if nargin < 3, figname = ''; end
%GRN network viewer
if isa(A, 'digraph') || isa(A, 'graph')
G = A;
else
if nargin < 2
nodenames = string((1:size(A, 1))');
end
G = pkg.i_makegraph(A, nodenames);
end
if nargin < 3
figname = sprintf('nodes (n=%d, red=TF); edges (blue=positive, red=negative)', ...
G.numnodes);
end
gui.i_singlegraph(G, figname, parentfig);
end
%{
if nargin<3, cutoff=0.75; end
if nargin<2, g=[]; end
if isa(A,'digraph')||isa(A,'graph')
g=string(A.Nodes.Name);
A=adjacency(A,'weighted');
end
n=size(A,1);
if n>200, error('Smaller network is expected'); end
if isempty(g), g=string(1:n); end
% Add the UI components
hs = addcomponents;
[G,p]=drawnetwork(A,g,cutoff);
% Make figure visible after adding components
hs.fig.Visible = 'on';
function hs = addcomponents
% Add components, save handles in a struct
hs.fig = figure('Visible','off',...
'Tag','fig',...
'SizeChangedFcn',@resizeui, 'DockControls', 'off');
hs.btn = uicontrol(hs.fig,'String',...
'Layout',...
'Callback',@ChangeLayout,...
'Tag','button');
hs.btn2 = uicontrol(hs.fig,'String',...
'Weight',...
'Callback',@ChangeWeight,...
'Tag','button');
hs.btn3 = uicontrol(hs.fig,'String',...
'Cutoff',...
'Callback',@ChangeCutoff,...
'Tag','button');
hs.btn4 = uicontrol(hs.fig,'String',...
'Directed',...
'Callback',@ChangeDirected,...
'Tag','button');
hs.btn5 = uicontrol(hs.fig,'String',...
'Font Size',...
'Callback',@ChangeFontSize,...
'Tag','button');
hs.btn6 = uicontrol(hs.fig,'String',...
'Save Adj',...
'Callback',@SaveAdj,...
'Tag','button');
hs.ax = axes('Parent',hs.fig,...
'Units','pixels',...
'Tag','ax');
end
function ChangeLayout(hObject,event)
%plot(hs.ax,theta,y);
a=["layered","subspace","force","circle","force3","subspace3"];
i=randi(length(a));
p.layout(a(i));
if i>4
view(3);
else
view(2);
end
end
function ChangeWeight(hObject,event)
if length(unique(p.LineWidth))>1
p.LineWidth = p.LineWidth./p.LineWidth;
else
a=3:10;
b=a(randi(length(a),1));
G.Edges.LWidths = abs(b*G.Edges.Weight/max(G.Edges.Weight));
p.LineWidth = G.Edges.LWidths;
end
end
function ChangeCutoff(hObject,event)
list = {'0.00 (show all edges)',...
'0.30','0.35','0.40','0.45',...
'0.50','0.55','0.60',...
'0.65','0.70','0.75','0.80','0.85',...
'0.90','0.95 (show 5% of edges)'};
[indx,tf] = listdlg('ListString',list,...
'SelectionMode','single','ListSize', [220, 300]);
if tf
if indx==1
cutoff=0;
elseif indx==length(list)
cutoff=0.95;
else
cutoff=str2double(list(indx));
end
x=p.XData; y=p.YData; z=p.ZData;
if isa(G,'idgraph')
[G,p]=drawnetwork(A,g,cutoff);
else
[G,p]=drawnetwork(0.5*(A+A.'),g,cutoff);
end
p.XData=x; p.YData=y; p.ZData=z;
end
end
function ChangeDirected(hObject,event)
x=p.XData; y=p.YData; z=p.ZData;
if isa(G,'graph')
[G,p]=drawnetwork(A,g,cutoff);
else
[G,p]=drawnetwork(0.5*(A+A.'),g,cutoff);
end
p.XData=x; p.YData=y; p.ZData=z;
end
function ChangeFontSize(hObject,event)
if p.NodeFontSize>=20
p.NodeFontSize=7;
else
p.NodeFontSize=p.NodeFontSize+1;
end
end
function SaveAdj(hObject,event)
labels = {'Save adjacency matrix A to variable named:',...
'Save graph G to variable named:',...
'Save genelist g to variable named:'};
vars = {'A','G','g'}; values = {A,G,g};
msgfig=export2wsdlg(labels,vars,values);
uiwait(msgfig);
end
function resizeui(hObject,event)
% Get figure width and height
figwidth = hs.fig.Position(3);
figheight = hs.fig.Position(4);
% Set button position
bheight = 30;
bwidth = 70;
bbottomedge = figheight - bheight - 50;
bleftedge = 10;
hs.btn.Position = [bleftedge bbottomedge bwidth bheight];
hs.btn2.Position = [bleftedge bbottomedge-40 bwidth bheight];
hs.btn3.Position = [bleftedge bbottomedge-80 bwidth bheight];
hs.btn4.Position = [bleftedge bbottomedge-120 bwidth bheight];
hs.btn5.Position = [bleftedge bbottomedge-160 bwidth bheight];
hs.btn6.Position = [bleftedge bbottomedge-200 bwidth bheight];
% Set axes position
axheight = .85*figheight;
axbottomedge = max(0,figheight - axheight - 30);
axleftedge = bleftedge + bwidth + 10;
axwidth = max(0,figwidth - axleftedge - 30);
hs.ax.Position = [axleftedge axbottomedge axwidth axheight];
end
function [G,p]=drawnetwork(A,g,cutoff)
B=e_transf(A,cutoff);
if issymmetric(B)
G=graph(B,g,'omitselfloops');
else
G=digraph(B,g,'omitselfloops');
end
p=plot(hs.ax,G);
layout(p,'force');
if isa(G,'digraph')
G.Nodes.NodeColors = outdegree(G)-indegree(G);
else
G.Nodes.NodeColors = degree(G);
end
p.NodeCData = G.Nodes.NodeColors;
n=size(G.Edges,1);
cc=repmat([0 0.4470 0.7410],n,1);
cc(G.Edges.Weight<0,:)=repmat([0.8500, 0.3250, 0.0980],...
sum(G.Edges.Weight<0),1);
p.EdgeColor=cc;
p.NodeFontSize=2*p.NodeFontSize;
%view(3)
title('Single-cell Gene Regulatory Netowrk');
end
end
function A=e_transf(A,q)
% A - adjacency matrix
if nargin<2, q=0.95; end
dim=size(A);
if numel(dim)==2
a=max(abs(A(:)));
if a>0
A=A./a;
A=A.*(abs(A)>quantile(abs(A(:)),q));
end
elseif numel(dim)==3
for k=1:dim(3)
A(:,:,k)=e_transf(A(:,:,k),q);
end
end
end
%}