-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsonify.pl
executable file
·48 lines (37 loc) · 961 Bytes
/
jsonify.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl -w
# This script converts a tree (in the form of a list of links) into .json
# data that could be read in by D3.js for example.
# For example:
# ./jsonify.pl Caniformia < PRUNED.txt
$root = $ARGV[0];
while ($line = <STDIN>) {
$line =~ /^(.*) -> (.*)$/;
$head = $1;
$tail = $2;
push @{$tails{$head}}, $tail;
}
sub jsonify {
my $node = $_[0];
my $depth = $_[1];
$tab = "";
for (0 .. $depth) { $tab .= " "; }
print "$tab {\"name\": \"$node\"";
if (exists $tails{$node}) {
my $length = @{$tails{$node}};
my $i = 0;
for $daughter ( @{$tails{$node}} ) {
if ($i == 0) { print ", \"children\": [\n"; }
&jsonify ($daughter, $depth+1);
if ($i == $length-1) { print " ]\n"; }
else { print ",\n"; }
$i++;
}
} else {
# $size = 100 * (8-$depth);
# if ($size < 200) { $size = 200; }
$size = 300;
print ",\"size\": $size";
}
print " }";
}
&jsonify($root, 0);