annotate toolboxes/FullBNT-1.0.7/GraphViz/Old/dot_to_graph.m @ 0:cc4b1211e677 tip

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