annotate toolboxes/graph_visualisation/graphViz4Matlab/util/adj2pajek2.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function adj2pajek2(adj,filename, varargin)
wolffd@0 2 % ADJ2PAJEK2 Converts an adjacency matrix representation to a Pajek .net read format
wolffd@0 3 % adj2pajek2(adj, filename-stem, 'argname1', argval1, ...)
wolffd@0 4 %
wolffd@0 5 % Set A(i,j)=-1 to get a dotted line
wolffd@0 6 %
wolffd@0 7 % Optional arguments
wolffd@0 8 %
wolffd@0 9 % nodeNames - cell array, defaults to {'v1','v2,...}
wolffd@0 10 % shapes - cell array, defaults to {'ellipse','ellipse',...}
wolffd@0 11 % Choices are 'ellipse', 'box', 'diamond', 'triangle', 'cross', 'empty'
wolffd@0 12 % partition - vector of integers, defaults to [1 1 ... 1]
wolffd@0 13 % This will automatically color-code the vertices by their partition
wolffd@0 14 %
wolffd@0 15 % Run pajek (available from http://vlado.fmf.uni-lj.si/pub/networks/pajek/)
wolffd@0 16 % Choose File->Network->Read from the menu
wolffd@0 17 % Then press ctrl-G (Draw->Draw)
wolffd@0 18 % Optional: additionally load the partition file then press ctrl-P (Draw->partition)
wolffd@0 19 %
wolffd@0 20 % Examples
wolffd@0 21 % A=zeros(5,5);A(1,2)=-1;A(2,1)=-1;A(1,[3 4])=1;A(2,5)=1;
wolffd@0 22 % adj2pajek2(A,'foo') % makes foo.net
wolffd@0 23 %
wolffd@0 24 % adj2pajek2(A,'foo','partition',[1 1 2 2 2]) % makes foo.net and foo.clu
wolffd@0 25 %
wolffd@0 26 % adj2pajek2(A,'foo',...
wolffd@0 27 % 'nodeNames',{'TF1','TF2','G1','G2','G3'},...
wolffd@0 28 % 'shapes',{'box','box','ellipse','ellipse','ellipse'});
wolffd@0 29 %
wolffd@0 30 %
wolffd@0 31 % The file format is documented on p68 of the pajek manual
wolffd@0 32 % and good examples are on p58, p72
wolffd@0 33 %
wolffd@0 34 % Written by Kevin Murphy, 30 May 2007
wolffd@0 35 % Based on adj2pajek by Gergana Bounova
wolffd@0 36 % http://stuff.mit.edu/people/gerganaa/www/matlab/routines.html
wolffd@0 37 % Fixes a small bug (opens files as 'wt' instead of 'w' so it works in windows)
wolffd@0 38 % Also, simplified her code and added some features.
wolffd@0 39
wolffd@0 40 N = length(adj);
wolffd@0 41 for i=1:N
wolffd@0 42 nodeNames{i} = strcat('"v',num2str(i),'"');
wolffd@0 43 shapes{i} = 'ellipse';
wolffd@0 44 end
wolffd@0 45
wolffd@0 46 [nodeNames, shapes, partition] = process_options(varargin, ...
wolffd@0 47 'nodeNames', nodeNames, 'shapes', shapes, 'partition', []);
wolffd@0 48
wolffd@0 49 if ~isempty(partition)
wolffd@0 50 fid = fopen(sprintf('%s.clu', filename),'wt','native');
wolffd@0 51 fprintf(fid,'*Vertices %6i\n',N);
wolffd@0 52 for i=1:N
wolffd@0 53 fprintf(fid, '%d\n', partition(i));
wolffd@0 54 end
wolffd@0 55 fclose(fid);
wolffd@0 56 end
wolffd@0 57
wolffd@0 58 fid = fopen(sprintf('%s.net', filename),'wt','native');
wolffd@0 59
wolffd@0 60 fprintf(fid,'*Vertices %6i\n',N);
wolffd@0 61 for i=1:N
wolffd@0 62 fprintf(fid,'%3i %s %s\n', i, nodeNames{i}, shapes{i});
wolffd@0 63 end
wolffd@0 64
wolffd@0 65 %fprintf(fid,'*Edges\n');
wolffd@0 66 fprintf(fid,'*Arcs\n'); % directed
wolffd@0 67 for i=1:N
wolffd@0 68 for j=1:N
wolffd@0 69 if adj(i,j) ~= 0
wolffd@0 70 fprintf(fid,' %4i %4i %2i\n',i,j,adj(i,j));
wolffd@0 71 end
wolffd@0 72 end
wolffd@0 73 end
wolffd@0 74 fclose(fid)
wolffd@0 75
wolffd@0 76
wolffd@0 77 if 0
wolffd@0 78 adj2pajek2(A,'foo',...
wolffd@0 79 'nodeNames',{'TF1','TF2','G1','G2','G3'},...
wolffd@0 80 'shapes',{'box','box','ellipse','ellipse','ellipse'});
wolffd@0 81
wolffd@0 82 N = 100; part = ones(1,N); part(intersect(reg.tfidxTest,1:N))=2;
wolffd@0 83 G = reg.Atest(1:N, 1:N)';
wolffd@0 84 adj2pajek2(G, 'Ecoli100', 'partition', part)
wolffd@0 85 end
wolffd@0 86 end