Debian Codegraph @

bin/tab2graph

raw
1 #!perl
2
3 use strict;
4 use warnings;
5 use version;
6 use English qw( -no_match_vars );
7 use File::Basename;
8 use Getopt::Long;
9 use GraphViz;
10 use List::Util qw( max );
11 use Pod::Usage;
12 use Readonly;
13 use Scalar::Util qw( openhandle );
14 use Text::RecordParser;
15
16 Readonly our $VERSION => 1.01;
17
18 my $add_color = 0;
19 my $fields = '';
20 my $fs = qq{\t};
21 my $is_directed = 0;
22 my $layout = 'circo';
23 my $out_file = '';
24 my $out_format = 'png';
25 my $rs = qq{\n};
26 my $show_numbers = 0;
27
28 my ( $help, $man_page, $show_version );
29
30 GetOptions(
31 'c|color' => \$add_color,
32 'd|directed' => \$is_directed,
33 'format:s' => \$out_format,
34 'fs:s' => \$fs,
35 'f|fields:s' => \$fields,
36 'help' => \$help,
37 'l|layout:s' => \$layout,
38 'man' => \$man_page,
39 'n|numbers' => \$show_numbers,
40 'o|out:s' => \$out_file,
41 'rs:s' => \$rs,
42 'version' => \$show_version,
43 ) or pod2usage;
44
45 if ( $help || $man_page ) {
46 pod2usage({
47 -exitval => 0,
48 -verbose => $man_page ? 2 : 1
49 });
50 };
51
52 if ( $show_version ) {
53 my $prog = basename( $PROGRAM_NAME );
54 print "$prog $VERSION\n";
55 exit 0;
56 }
57
58 if ( !@ARGV ) {
59 pod2usage('No input files');
60 }
61 elsif ( @ARGV > 1 ) {
62 pod2usage('Too many input files');
63 }
64
65 my %field_filter = map { $_, 1 } split( /\s*,\s*/, $fields );
66
67 my $file = shift @ARGV;
68 my $p = Text::RecordParser->new(
69 filename => $file,
70 field_separator => $fs,
71 record_separator => $rs,
72 trim => 1,
73 );
74
75 my $g = GraphViz->new(
76 directed => $is_directed,
77 layout => $layout,
78 bgcolor => $add_color ? 'lightgoldenrodyellow' : 'white',
79 no_overlap => 1,
80 node => {
81 style => 'filled',
82 fillcolor => 'white',
83 }
84 );
85
86 my @cols = $p->field_list;
87 my %col_pos;
88 for my $i ( 1..$#cols ) { # skip first col
89 my $col_name = $cols[ $i ];
90
91 if ( %field_filter ) {
92 next unless $field_filter{ $col_name };
93 }
94
95 $g->add_node( $col_name );
96
97 $col_pos{ $i } = $col_name;
98 }
99
100 my $records = $p->fetchall_arrayref;
101 my @edges;
102 for my $data ( @$records ) {
103 my $node_name = $data->[0];
104 $g->add_node( $node_name );
105
106 for my $i ( 1..$#cols ) {
107 my $val = $data->[ $i ] or next;
108 my $col = $col_pos{ $i } or next;
109
110 if ( %field_filter ) {
111 next unless defined $field_filter{ $col };
112 }
113
114 if ( $val =~ /^\d+$/ && $val > 0 ) {
115 push @edges, [ $cols[ $i ], $node_name, $val ];
116 }
117 }
118 }
119
120 my $max_val = max( map { $_->[-1] } @edges );
121 for my $edge ( @edges ) {
122 my $val = $edge->[-1];
123 my $weight = ( $val / $max_val ) * .5;
124
125 $g->add_edge(
126 $edge->[0], $edge->[1],
127 $show_numbers ? ( label => $val ) : (),
128 weight => $weight,
129 );
130 }
131
132 my $method = join '', 'as_', lc $out_format;
133 if ( $out_file ) {
134 open my $fh, '>', $out_file or die "Can't write '$out_file': $!\n";
135 binmode $fh;
136 print $fh $g->$method;
137 close $fh;
138 my $basename = basename( $out_file );
139 print STDERR qq[Image created "$basename."\n];
140 }
141 else {
142 print $g->$method;
143 }
144
145 __END__
146
147 # -------------------------------------------------------------------
148
149 =pod
150
151 =head1 NAME
152
153 tab2graph - turn tabular data into a graph
154
155 =head1 SYNOPSIS
156
157 tab2graph [options] file.tab
158
159 Options:
160
161 -c|--color Add some color to the output (default is white)
162 -d|--directed Make graph directed (default is not)
163 -l|--layout GraphViz layout; choose from dot, neato, twopi,
164 circo (default), and fdp
165 -f|--fields Restrict to set of fields in first row
166 -n|--numbers Show the numbers (default is not)
167 -o|--out Name of output file (default is STDOUT)
168 --format Output format (default is PNG)
169 --fs=x Use "x" as the field separator (default is tab)
170 --rs=x Use "x" as the record separator (default is newline)
171
172 --help Show brief help and quit
173 --man Show full documentation
174
175 =head1 DESCRIPTION
176
177 Turns tabular data into a graph using GraphViz. This may or may not
178 be useful.
179
180 =head1 SEE ALSO
181
182 =over 4
183
184 =item * Text::RecordParser
185
186 =item * GraphViz
187
188 =back
189
190 =head1 AUTHOR
191
192 Ken Youens-Clark E<lt>[email protected]<gt>.
193
194 =head1 LICENSE AND COPYRIGHT
195
196 Copyright (C) 2009-10 Ken Youens-Clark. All rights reserved.
197
198 This program is free software; you can redistribute it and/or modify
199 it under the terms of the GNU General Public License as published by
200 the Free Software Foundation; version 2.
201
202 This program is distributed in the hope that it will be useful, but
203 WITHOUT ANY WARRANTY; without even the implied warranty of
204 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
205 General Public License for more details.
206
207 =cut