-
Notifications
You must be signed in to change notification settings - Fork 8
/
graph_gui.m
executable file
·411 lines (379 loc) · 13.7 KB
/
graph_gui.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
function g=graph_gui(A,xy,drawDef,delDef,vertexChked,edgeChked)
% GRAPH_GUI Draw and Edit Graphs of Vertices and Edges
% GRAPH_GUI(A,XY) Loads a graph into the GUI for editing
%
% Controls:
% Use the radio buttons to select an action
% 1. To DRAW a new VERTEX, select 'Draw' and 'Vertex'. Then click
% inside the axes in the desired location.
% 2. To DELETE a VERTEX, select 'Delete' and 'Vertex'. Then click
% inside the axis near the desired vertex.
% 3. To DRAW an EDGE, select 'Draw' and 'Edge'. Then click and hold
% the mouse button down on the starting vertex and drag the mouse
% to the vertex of the desired connection.
% a. If 'Directed' is selected, a 1-way edge will be created from
% the first vertex to the second.
% b. If 'Undirected' is selected, a 2-way edge between the first vertex
% and the second vertex will be created.
% Note:
% If no vertices exist, no edges will be created.
% 4. To DELETE an EDGE, select 'Delete' and 'Edge'. Then click and
% hold the mouse button down on the starting vertex and drag the mouse
% to the vertex of the desired connection to delete.
% a. If 'Directed' is selected, only the 1-way edge between the
% first vertex and the second will be deleted.
% b. If 'Undirected' is selected, the entire 2-way edge between the
% first vertex and the second vertex will be deleted.
% 5. Click the 'Latch Points' checkbox to toggle between rounded and
% unrounded (x,y) points.
% 6. The number in the edit box specifies a rounding value. For example,
% a value of 0.5 rounds each (x,y) point to the nearest 0.5
% 7. Click the 'Save Graph' button to save the adjacency matrix (A) and the
% coordinates (xy) of the graph to a MAT file.
%
% Note: The display at the bottom of the figure window shows either the (x,y)
% coordinate of the mouse location or the ID of the closest vertex.
%
% Note: The following line styles are used to display the graph
% 1. Undirected (2-way) edges are plotted in solid black lines
% 2. Directed (1-way) edges are plotted in two styles
% a. If the edge connects a larger vertex ID with a smaller ID,
% the edge is plotted as a blue dashed line
% b. If the edge connects a smaller vertex ID with a larger ID,
% the edge is plotted as a red dotted line
% 3. Any vertex that is connected to itself is plotted with a
% black circle around it
%
% Example:
% graph_gui; % sets up a GUI to draw a new graph
%
% Example:
% n = 10; t = 2*pi/n*(0:n-1);
% A = round(rand(n));
% xy = 10*[cos(t); sin(t)]';
% graph_gui(A,xy); % loads a graph into the GUI for editing
%
% See also: gplot, gplotd (FEX #16131)
%
% Author: Joseph Kirk
% Email: [email protected]
% Release: 1.1
% Release Date: 8/28/07
%
% Edited for use with SBEToolbox.
% (C) is for SBEToolbox, not for graph_gui.
%
% Systems Biology and Evolution Toolbox (SBEToolbox).
% Authors: James Cai, Kranti Konganti.
% (C) Texas A&M University.
%
% $LastChangedDate: 2013-02-14 13:31:31 -0600 (Thu, 14 Feb 2013) $
% $LastChangedRevision: 438 $
% $LastChangedBy: konganti $
%
global sbeG sbeNode
[in_G_N, in_G_E] = size(sbeG);
if nargin < 6
edgeChked = 0;
vertexChked = 1;
delDef = 0;
drawDef = 1;
end
% Initially graph is not drawn
setpref('SBEToolbox', 'graph_gui', 0);
if ~ispref('SBEToolbox', 'graph_gui_drawn')
addpref('SBEToolbox', 'graph_gui_drawn', 0);
else
setpref('SBEToolbox', 'graph_gui_drawn', 0);
end
% set up the figure window
g=figure('Name','Network Editor', ...
'Numbertitle','off', ...
'Menubar', 'none', ...
'Toolbar', 'figure', ...
'Position',[300 100 525 550], ...
'WindowButtonDownFcn',@clickButtonDown, ...
'WindowButtonUpFcn',@clickButtonUp, ...
'WindowButtonMotionFcn',@buttonMotion);
%'Menubar','none', ...
%'ToolBar','figure', ...
%'WindowStyle','modal',...
axes('Units','normalized', ...
'Position', [0.1 0.1 0.8 0.75])
% radio buttons for choosing to draw or delete
ddpanel = uipanel('BackgroundColor',[0.9 0.9 0.9], ...
'Position',[0.025 0.875 0.175 0.1]);
draw_radio = uicontrol(ddpanel, ...
'Style','radiobutton', ...
'String','Draw', ...
'Units','normalized', ...
'BackgroundColor',[0.9 0.9 0.9], ...
'Value',drawDef, ...
'Position', [0.05 0.55 0.9 0.35], ...
'Callback',@clickDrawRadio);
delete_radio = uicontrol(ddpanel, ...
'Style','radiobutton', ...
'String','Delete', ...
'Units','normalized', ...
'BackgroundColor',[0.9 0.9 0.9], ...
'Value',delDef, ...
'Position', [0.05 0.1 0.9 0.35], ...
'Callback',@clickDeleteRadio);
% radio buttons for choosing a vertex or edge
vepanel = uipanel('BackgroundColor',[0.9 0.9 0.9], ...
'Position',[0.225 0.875 0.175 0.1]);
vertex_radio = uicontrol(vepanel, ...
'Style','radiobutton', ...
'String','Vertex', ...
'Units','normalized', ...
'BackgroundColor',[0.9 0.9 0.9], ...
'Value',vertexChked, ...
'Position', [0.05 0.55 0.9 0.35], ...
'Callback',@clickVertexRadio);
edge_radio = uicontrol(vepanel, ...
'Style','radiobutton', ...
'String','Edge', ...
'Units','normalized', ...
'BackgroundColor',[0.9 0.9 0.9], ...
'Value',edgeChked, ...
'Position', [0.05 0.1 0.9 0.35], ...
'Callback',@clickEdgeRadio);
% radio buttons for choosing a directed or undirected edge
dupanel = uipanel('BackgroundColor',[0.9 0.9 0.9], ...
'Position',[0.425 0.875 0.175 0.1]);
directed_radio = uicontrol(dupanel, ...
'Style','radiobutton', ...
'String','Directed', ...
'Units','normalized', ...
'BackgroundColor',[0.9 0.9 0.9], ...
'Value',0, ...
'Position', [0.05 0.55 0.9 0.35], ...
'Enable','off', ...
'Callback',@clickDirRadio);
undirected_radio = uicontrol(dupanel, ...
'Style','radiobutton', ...
'String','Undirected', ...
'Units','normalized', ...
'BackgroundColor',[0.9 0.9 0.9], ...
'Value',1, ...
'Position', [0.05 0.1 0.9 0.35], ...
'Enable','off', ...
'Callback',@clickUdirRadio);
% check box and text box for latching x/y points
lpanel = uipanel('BackgroundColor',[0.9 0.9 0.9], ...
'Position',[0.625 0.875 0.175 0.1]);
latch_edt = uicontrol(lpanel, ...
'Style','edit', ...
'String','0.01', ...
'Units','normalized', ...
'Position', [0.05 0.55 0.9 0.35], ...
'BackgroundColor',[1 1 1]);
latch_chk = uicontrol(lpanel, ...
'Style','checkbox', ...
'String','Latch Points', ...
'Value',1, ...
'Units','normalized', ...
'Position', [0.05 0.1 0.9 0.35], ...
'BackgroundColor',[0.9 0.9 0.9]);
% button to save A/xy
uicontrol('Style','pushbutton', ...
'String','Done', ...
'Units','normalized', ...
'Position', [0.825 0.875 0.15 0.1], ...
'Callback',@saveGraph);
% text box to show x/y or vertex id
xy_txt = uicontrol('Style','text', ...
'Units','normalized', ...
'Position',[0 0 1 0.05], ...
'FontSize',11, ...
'FontWeight','b', ...
'BackgroundColor',[0.8 0.8 0.8]);
if nargin < 2
xy = [];
A = [];
else
plotGraph(A,xy);
%zoom on;
%pan on;
end
eid = [];
%----- callback functions ------------------------
function pt = getPoint()
cpt = get(gca,'CurrentPoint');
pt = cpt(1,1:2);
if get(latch_chk,'Value')
latch = str2double(get(latch_edt,'String'));
if ~isnan(latch)
pt = round(pt/latch)*latch;
end
end
end
function plotGraph(A,xy,ax)
cla;
if ~isempty(xy)
plot(xy(:,1),xy(:,2),'.')
if nargin < 3
ax = axis;
else
minx = min([ax(1); xy(:,1)]);
maxx = max([ax(2); xy(:,1)]);
miny = min([ax(3); xy(:,2)]);
maxy = max([ax(4); xy(:,2)]);
ax = [minx maxx miny maxy];
end
dgplot(A,xy);
axis(ax);
end
end
function dgplot(A,xy)
n = size(A,1);
A = max(0,min(1,ceil(abs(double(A)))));
uA = A.*A'.*(1-eye(n));
[ux,uy] = makeXY(uA,xy); % undirected edges (2-way)
[dxu,dyu] = makeXY(triu(A-uA),xy); % directed edges (1-way, sm2lg)
[dxl,dyl] = makeXY(tril(A-uA),xy); % directed edges (1-way, lg2sm)
[ix,iy] = makeXY(A.*eye(n),xy); % self-connecting edges
hold on
plot(ux,uy,'k-','LineSmoothing','On','color',[.6 .6 .6])
plot(dxu,dyu,'r:','LineSmoothing','On','color',[.6 .6 .6])
plot(dxl,dyl,'b--','LineSmoothing','On','color',[.6 .6 .6])
plot(ix,iy,'ko','LineSmoothing','On','color',[.6 .6 .6])
graph=plot(xy(:,1),xy(:,2),'k.');
set(graph, 'MarkerEdgeColor','r',...
'MarkerFaceColor', 'r', 'MarkerSize', 15);
if n > size(sbeNode, 1)
for newN = size(sbeNode, 1) + 1:n
sbeNode{newN} = num2str(newN);
end
end
for k = 1:n
text(xy(k,1),xy(k,2),...
[' ' sbeNode{k}],...
'Color','k','FontSize',12,'FontWeight','b',...
'Clipping', 'On')
end
hold off
function [x,y] = makeXY(A,xy)
x = NaN;
y = NaN;
if any(A(:))
[ii,jj] = find(A);
m = length(ii);
xmat = [xy(ii,1) xy(jj,1)]';
ymat = [xy(ii,2) xy(jj,2)]';
x = [xmat; NaN(1,m)]; x = x(:);
y = [ymat; NaN(1,m)]; y = y(:);
end
end
end
function id = getNearestVertex(pt)
dsq = sum((xy-pt(ones(size(xy,1),1),:)).^2,2);
id = find(dsq==min(dsq));
id = id(1);
end
function clickButtonDown(varargin)
pt = getPoint;
ax = axis;
if pt(1)>=ax(1) && pt(1)<=ax(2) && pt(2)>=ax(3) && pt(2)<=ax(4)
if get(edge_radio,'Value')
if ~isempty(xy)
eid = getNearestVertex(pt);
end
elseif get(draw_radio,'Value')
n = size(xy,1);
xy(n+1,:) = pt;
A(n+1,n+1) = 0;
plotGraph(A,xy,ax);
else
if ~isempty(xy)
id = getNearestVertex(pt);
xy(id,:) = [];
A(id,:) = [];
A(:,id) = [];
plotGraph(A,xy,ax);
end
end
end
end
function clickButtonUp(varargin)
if get(edge_radio,'Value') && ~isempty(xy)
cpt = get(gca,'CurrentPoint');
pt = cpt(1,1:2);
ax = axis;
if pt(1)>=ax(1) && pt(1)<=ax(2) && pt(2)>=ax(3) && pt(2)<=ax(4)
id = getNearestVertex(pt);
if get(draw_radio,'Value')
A(eid,id) = 1;
else
A(eid,id) = 0;
end
if get(undirected_radio,'Value')
if id ~= eid
A(id,eid) = A(eid,id);
else
A(id,eid) = 0;
end
end
plotGraph(A,xy,ax);
end
end
end
function buttonMotion(varargin)
pt = getPoint;
if get(draw_radio,'Value') && get(vertex_radio,'Value')
set(xy_txt,'String',[' x = ' num2str(pt) ' = y ']);
elseif ~isempty(xy)
id = getNearestVertex(pt);
set(xy_txt,'String',[' id = ' num2str(id)]);
else
set(xy_txt,'String','');
end
end
function saveGraph(varargin)
%str = inputdlg('Name of the MAT file','Save',1,{'mygraph'});
%str = inputdlg('Name of the variable','Assign',1,{'sbeG'});
%if ~isempty(str)
%eval(sprintf('save %s A xy',str{1}));
%assignin('caller',sprintf('%s',str{1}),A);
%assignin('base','xy',xy);
%end
sbeG=logical(A);
if (size(sbeG, 1) ~= in_G_N && size(sbeG, 2) ~= in_G_E)
setpref('SBEToolbox', 'graph_gui_drawn', 1);
end
%sbeNode=num2cellstr(1:size(sbeG,1));
%sbeNode=cellfun(@num2str,num2cell(1:size(sbeG,1)),'Uniform',false);
if getpref('SBEToolbox', 'graph_gui_drawn') == 1
setpref('SBEToolbox', 'graph_gui', 1);
end
close
end
function clickDrawRadio(varargin)
set(draw_radio,'Value',1);
set(delete_radio,'Value',0);
end
function clickDeleteRadio(varargin)
set(draw_radio,'Value',0);
set(delete_radio,'Value',1);
end
function clickVertexRadio(varargin)
set(vertex_radio,'Value',1);
set(edge_radio,'Value',0);
set(directed_radio,'Enable','off');
set(undirected_radio,'Enable','off');
end
function clickEdgeRadio(varargin)
set(vertex_radio,'Value',0);
set(edge_radio,'Value',1);
set(directed_radio,'Enable','on');
set(undirected_radio,'Enable','on');
end
function clickDirRadio(varargin)
set(directed_radio,'Value',1);
set(undirected_radio,'Value',0);
end
function clickUdirRadio(varargin)
set(directed_radio,'Value',0);
set(undirected_radio,'Value',1);
end
end