-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate_scoretree_for_d3.pl
executable file
·221 lines (158 loc) · 5 KB
/
translate_scoretree_for_d3.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env perl
=head1 NAME
translate_scoretree_for_d3.pl -- produce a d3.js compatible score tree
=head1 DESCRIPTION
translate_scoretree_for_d3 -- Given the score tree generated by
generate_resource_scores and the components listing, produce a json output fit
for diagraming with d3.js
=head1 SYNOPSIS
./translate_scoretree_for_d3.pl [OPTIONS]
=head1 OPTIONS
=over
=item B<--tree_file>
The file to pull the yaml scoretree from
=item B<--d3_file>
The file to save the json scoretree to
=item B<--components>
File containing the component relationships. Default config/components.yaml
=item B<--verbose>
=back
=head1 EXAMPLES
Translate the nca3 report score
./translate_scoretree_for_d3.pl \
--tree_file ./nca3_tree.yaml \
--d3_file ./nca3_tree_d3.json
=cut
use Getopt::Long qw/GetOptions/;
use Pod::Usage qw/pod2usage/;
use Gcis::Exim qw();
use Gcis::Client;
use YAML::XS;
use JSON;
use Data::Dumper;
use FindBin qw( $RealBin );
use strict;
use v5.14;
my $COMPONENTS;
my $components_map = "config/components.yaml";
GetOptions(
'tree_file=s' => \(my $tree_file),
'd3_file=s' => \(my $d3_file),
'components=s' => \$components_map,
'verbose!' => \(my $verbose),
'help|?' => sub { pod2usage(verbose => 2) },
) or die pod2usage(verbose => 1);
pod2usage(verbose => 1, message => "$0: tree_file option must be specified") unless $tree_file;
pod2usage(verbose => 1, message => "$0: d3_file option must be specified") unless $d3_file;
# Long running script - confirm our output file will work up front.
{
if ( open(TEST, ">", $d3_file) ) {
close TEST;
} else {
close Test;
pod2usage(verbose => 1, message => "$0: d3_file $d3_file can't be opened for writing");
}
}
&main;
sub main {
my $greeting = <<END;
Evaluating Provenance
input file : $tree_file
output file : $d3_file
components file : $components_map
END
say $greeting if $verbose;
my $score_tree_initial = load_data();
# Read the tree and produce the json.
my $score_tree_final = process_subtree($score_tree_initial, 'publication');
output_to_file( $score_tree_final );
return;
}
=head1 FUNCTIONS
=cut
=head2 process_subtree
TODO
=cut
sub process_subtree {
my $tree = shift;
my $type = shift;
my $result_tree = [];
foreach my $key ( keys %$tree ) {
# get name, type, and score
my $node = {};
$node->{name} = $key;
$node->{type} = $type;
$node->{score} = $tree->{$key}->{score};
$node->{children} = get_children($tree->{$key});
next unless $node->{score};
push @$result_tree, $node;
}
return $result_tree;
}
=head2 get_children
TODO
=cut
sub get_children {
my $tree = shift;
my $children = [];
# Handle each component type subtree
foreach my $component_type ( %{$tree->{components}} ) {
my $childs = process_subtree($tree->{components}->{$component_type}, 'publication');
push @$children, @$childs;
}
# Handle the child_pub type subtree
if ( exists $tree->{child_publication}
&& $tree->{child_publication} ) {
my $childs = process_subtree($tree->{child_publication}, 'publication');
push @$children, @$childs;
}
# Handle the contributor type subtree
if ( exists $tree->{connections}->{contributors}
&& $tree->{connections}->{contributors} ) {
my $childs = process_subtree($tree->{connections}->{contributors}, 'contributor');
push @$children, @$childs;
}
# Handle the reference type subtree
if ( exists $tree->{connections}->{references}
&& $tree->{connections}->{references} ) {
my $childs = process_subtree($tree->{connections}->{references}, 'reference');
push @$children, @$childs;
}
# Handle the person type subtree
if ( exists $tree->{person}
&& $tree->{person}) {
my $childs = process_subtree($tree->{person}, 'entity');
push @$children, @$childs;
}
# Handle the organization type subtree
if ( exists $tree->{organization}
&& $tree->{organization}) {
my $childs = process_subtree($tree->{organization}, 'entity');
push @$children, @$childs;
}
return $children;
}
### UTILITY FUNCTIONS
=head2 output_to_file
Print out the json score tree.
=cut
sub output_to_file {
my $score_tree = shift;
my $eval_json = to_json($score_tree);
open( OUTPUT, ">", $d3_file ) or die $!;
print OUTPUT $eval_json;
close OUTPUT;
say "Score tree written to $d3_file." if $verbose;
return;
}
=head2 load_rubric_and_components
Load in the YAML rubrics, components.
=cut
sub load_data {
$COMPONENTS = YAML::XS::LoadFile("$RealBin/$components_map")
or die "Could not load Components file $RealBin/$components_map";
my $score_tree_yaml = YAML::XS::LoadFile("$RealBin/$tree_file")
or die "Could not load Components file $RealBin/$tree_file";
return $score_tree_yaml;
}
1;