comparison toolboxes/FullBNT-1.0.7/GraphViz/Old/dot_to_graph.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [Adj, labels, x, y] = dot_to_graph(filename)
2
3 % [Adj, labels, x, y] = dot_to_graph(filename)
4 % Extract a matrix representation, node labels, and node position coordinates
5 % from a file in GraphViz format http://www.research.att.com/sw/tools/graphviz
6 %
7 % INPUTS:
8 % 'filename' - the file in DOT format containing the graph layout.
9 % OUTPUT:
10 % 'Adj' - an adjacency matrix representation of the graph in 'filename';
11 % 'labels' - a character array with the names of the nodes of the graph;
12 % 'x' - a row vector with the x-coordinates of the nodes in 'filename';
13 % 'y' - a row vector with the y-coordinates of the nodes in 'filename'.
14 %
15 % WARNINGS: not guaranted to parse ANY GraphViz file. Debugged on undirected
16 % sample graphs from GraphViz(Heawood, Petersen, ER, ngk10_4, process).
17 % Complaines about RecursionLimit set only to 500 on huge graphs.
18 % Ignores singletons (disjoint nodes).
19 % Sample DOT code "ABC.dot", read by [Adj, labels, x, y] = dot_to_graph('ABC.dot')
20 % digraph G {
21 % A [pos="28,31"];
22 % B [pos="74,87"];
23 % A -- B [pos="e,61,71 41,47 46,53 50,58 55,64"];
24 % }
25 % last modified: Jan 2004
26 % by Alexi Savov: asavov @wustl.edu | http://artsci.wustl.edu/~azsavov
27 % Leon Peshkin: pesha @ai.mit.edu | http://www.ai.mit.edu/~pesha
28
29 if ~exist(filename) % Checks whether the specified file exists.
30 error('* * * File does not exist or could not be found. * * *'); return;
31 end;
32
33 lines = textread(filename,'%s','delimiter','\n','commentstyle','c'); % Read file into cell array
34 dot_lines = strvcat(lines); % of lines, ignoring C-style comments
35
36 if findstr(dot_lines(1,:), 'graph ') == [] % Is this a DOT file ?
37 error('* * * File does not appear to be in valid DOT format. * * *'); return;
38 end;
39
40 Nlns = size(dot_lines,1); % The number of lines;
41 labels = {};
42 unread = 1:Nlns; % 'unread' list of lines which has not been examined yet
43 edge_id = 1;
44 for line_ndx = 1:Nlns % This section sets the adjacency matrix A(Lnode,Rnode) = edge_id.
45 line = dot_lines(line_ndx,:);
46 Ddash_pos = strfind(line, ' -- ') + 1; % double dash positions
47 arrow_pos = strfind(line, ' -> ') + 1; % arrow dash positions
48 tokens = strread(line,'%s','delimiter',' "');
49 left_bound = 1;
50 for dash_pos = [Ddash_pos arrow_pos]; % if empty - not a POS line
51 Lnode = sscanf(line(left_bound:dash_pos -2), '%s');
52 Rnode = sscanf(line(dash_pos +3 : length(line)-1),'%s',1);
53 Lndx = strmatch(Lnode, labels, 'exact');
54 Rndx = strmatch(Rnode, labels, 'exact');
55 if isempty(Lndx) % extend our list of labels
56 labels{end+1} = Lnode;
57 Lndx = length(labels);
58 end
59 if isempty(Rndx)
60 labels{end+1} = Rnode;
61 Rndx = length(labels);
62 end
63 Adj(Lndx, Rndx) = edge_id;;
64 if ismember(dash_pos, Ddash_pos) % The edge is undirected, A(Rndx,LndxL) is also set to 1;
65 Adj(Rndx, Lndx) = edge_id;
66 end
67 edge_id = edge_id + 1;
68 left_bound = dash_pos + 3;
69 unread = setdiff(unread, line_ndx);
70 end
71 end
72 Nvrt = length(labels); % number of vertices we found [Do we ever have singleton vertices ???]
73 % labels = strvcat(labels); % convert to the searchable array
74 x = zeros(1, Nvrt);
75 y = zeros(1, Nvrt);
76 lst_node = 0;
77 % Find node's position coordinates if they are contained in 'filename'.
78 for line_ndx = unread % Look for node's coordiantes among the 'unread' lines.
79 line = dot_lines(line_ndx,:);
80 bra_pos = strfind(line, '['); % has to have "[" if it has the lable
81 pos_pos = strfind(line, 'pos'); % position of the "pos"
82 for node = 1:Nvrt % look through the list of labels
83 % THE NEXT STATEMENT we assume no label is substring of any other label
84 lbl_pos = strfind(line, labels{node});
85 if (~isempty(lbl_pos) & ~isempty(bra_pos) & (x(node) == 0)) % make sure we have not seen it
86 if (lbl_pos(1) < bra_pos(1)) % label has to be to the left of braket
87 lst_node = node;
88 end
89 end
90 end
91 if (~isempty(pos_pos) & lst_node) % this line contains SOME position
92 [node_pos] = sscanf(line(pos_pos:length(line)), ' pos = "%d,%d"')';
93 x(lst_node) = node_pos(1);
94 y(lst_node) = node_pos(2);
95 lst_node = 0; % not to assign position several times
96 end
97 end
98
99 if (isempty(find(x)) & (nargout > 2)) % If coordinates were requested, but not found in 'filename'.
100 warning('File does not contain node coordinates.');
101 end;
102 if ~(size(Adj,1)==size(Adj,2)) % Make sure Adj is a square matrix. ?
103 Adj = eye(max(size(Adj)),size(Adj,1))*Adj*eye(size(Adj,2),max(size(Adj)));
104 end;
105 x = .9*(x-min(x))/range(x)+.05; % normalise and push off margins
106 y = .9*(y-min(y))/range(y)+.05;
107