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