make_branch_glossary.pl 8.22 KB
Newer Older
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
#!/usr/bin/env perl

use strict;
use warnings;

use File::Temp qw/tempfile/;
use Getopt::Long;
use HTML::Entities;
use List::Util qw(min);

#use Bio::EnsEMBL::Hive::Version;
use Bio::EnsEMBL::Hive::Utils::Config;

    # Finding out own path in order to reference own components (including own modules):
use Cwd            ();
use File::Basename ();
BEGIN {
    $ENV{'EHIVE_ROOT_DIR'} ||= File::Basename::dirname( File::Basename::dirname( Cwd::realpath($0) ) );
    unshift @INC, $ENV{'EHIVE_ROOT_DIR'}.'/modules';
}

my $ehrd        = $ENV{'EHIVE_ROOT_DIR'}        or die "Environment variable 'EHIVE_ROOT_DIR' not defined, please check your setup";
chdir $ehrd.'/docs';

my $pipeconfig_template = q{
package %s;

use strict;
use warnings;

use base ('Bio::EnsEMBL::Hive::PipeConfig::HiveGeneric_conf');

sub pipeline_analyses {
    my ($self) = @_;
    my $all_analyses = [%s];
    map {$_->{-module} = 'Bio::EnsEMBL::Hive::RunnableDB::Dummy'} @$all_analyses;
    return $all_analyses;
}

1;

};

my $display_config_json = q{
{
46
    "Graph": {"Pad": 0, "DisplayStats": 0, "DisplayDBIDs": 0, "DisplayDetails": 0 }
47
48
49
}
};

50
51
die "Usage: $0 <base_filename> <title>\n" unless scalar(@ARGV)==2;
main(@ARGV);
52
53
54
55
56



sub main {

57
58
59
60
    my ($base_filename, $title) = @_;

    my $diagrams = generate_diagrams($base_filename);
    my $data     = {'title' => $title, 'diagrams' => $diagrams};
61
62

    # The HTML file
63
64
    open(my $fh, '>', $base_filename.'.html') or die "Couldn't open the output file";
    generate_html($fh, $data);
65
66
67
    close($fh);

    # The Markdown file
68
69
    open($fh, '>', $base_filename.'.md') or die "Couldn't open the output file";
    generate_markdown($fh, $data);
70
    close($fh);
Matthieu Muffato's avatar
Matthieu Muffato committed
71
72

    # The RST file
73
74
    open($fh, '>', $base_filename.'.rst') or die "Couldn't open the output file";
    generate_rst($fh, $data);
Matthieu Muffato's avatar
Matthieu Muffato committed
75
    close($fh);
76
77
78
79
}

sub generate_diagrams {

80
81
    my $base_filename = shift;

82
83
84
85
86
    # Creates a temporary JSON config file
    my ($fh, $json_filename) = tempfile(UNLINK => 1);
    print STDERR "json: $json_filename\n";
    print $fh $display_config_json;
    close($fh);
87
    my @confs = Bio::EnsEMBL::Hive::Utils::Config->default_config_files();
88
89
90
91
92
93
94
95
96
97
    push @confs, $json_filename;

    # Creates a temporary pipe-config file
    my ($pipe_fh, $pipe_filename) = tempfile('tXXXXXXX', UNLINK => 1, DIR => '.', SUFFIX => '.pm');
    my $package_name = File::Basename::basename($pipe_filename);
    $package_name =~ s/\.pm//;
    print STDERR "pipeconfig: $pipe_filename\n";
    close($pipe_fh);

    # The example files
98
    my @example_files = glob($base_filename.'/*.txt');
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    my @data = sort {$a->[0] <=> $b->[0]} map {[parse_one_file($_)]} @example_files;
    my @diagrams;

    foreach my $record (@data) {
        unless ($record->[3]) {
            push @diagrams, [@$record];
            next;
        }

        # Write the pipeconfig file
        open(my $pipe_fh, '>', $pipe_filename);
        print $pipe_fh sprintf($pipeconfig_template, $package_name, $record->[3]);
        close($pipe_fh);

        # The diagram
114
        warn $record->[0];
115
        my $img = sprintf('%s/%d.png', $base_filename, $record->[0]);
116
        system($ehrd.'/scripts/generate_graph.pl', -pipeconfig => $pipe_filename, -output => $img, map {-config_file => $_} @confs);
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
        push @diagrams, [@$record, $img];
    }
    return \@diagrams;
}

sub parse_one_file {
    my $file_name = shift;
    open(my $fh, '<', $file_name) or die "Couldnt open file [$file_name]";
    my $data;
    {
        local $/ = undef;
        $data = <$fh>;
    }
    close($fh);
    my ($title, $description, $code) = split( /^--*-$/m,  $data);
    chomp $title;
    $title =~ s/^\R*//;
    chomp $description;
    $description =~ s/^\R*//;
    die "Wrong format in '$data'" unless $title and $description;
    if ($code) {
        chomp $code;
        $code =~ s/^\R*//;
    }
    my $id = $file_name;
    $id =~ s/[^0-9]//g;
    return ($id, $title, $description, $code);
}

sub generate_html {
    my ($output_fh, $data) = @_;

149
    print $output_fh "<html><body><h1>".$data->{'title'}."</h1>\n";
150
151
    print $output_fh "<ol>\n";
    my $in_group = 0;
152
    foreach my $record (@{$data->{'diagrams'}}) {
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        if ($record->[3]) {
            print $output_fh sprintf(q{<li><a href="#%d">%s</a></li>}, $record->[0], $record->[1]), "\n";
        } else {
            if ($in_group) {
                print $output_fh q{</ol></li>}, "\n";
            }
            print $output_fh sprintf(q{<li><a href="#%d">%s</a><ol>}, $record->[0], $record->[1]), "\n";
            $in_group = 1;
        }
    }
    if ($in_group) {
        print $output_fh q{</ol></li>}, "\n";
    }

    print $output_fh "</ol><table>\n";
168
    foreach my $record (@{$data->{'diagrams'}}) {
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
        if ($record->[3]) {
            print $output_fh sprintf(q{<tr id="%d"><td><h3>%s</h3><p>%s</p><pre>%s</pre></td><td><img src='%s'></td></tr>},
                $record->[0],
                $record->[1],
                $record->[2],
                encode_entities($record->[3]),
                $record->[4],
            ), "\n";
        } else {
            print $output_fh sprintf(q{<tr id="%d"><td><h2>%s</h2><p>%s</p></td><td></td></tr>},
                $record->[0],
                $record->[1],
                $record->[2],
            ), "\n";
        }
    }
    print $output_fh "</table></body></html>\n";
}

sub generate_markdown {
    my ($output_fh, $data) = @_;

191
    print $output_fh "# ".$data->{'title'}."\n\n";
192
193
194
    print $output_fh "## Index\n\n";
    my $i = 1;
    my $j = 1;
195
    foreach my $record (@{$data->{'diagrams'}}) {
196
197
198
199
200
201
202
203
204
205
        if ($record->[3]) {
            print $output_fh sprintf("  %s. [%s](#%s)  \n", chr(96+$j), $record->[1], $record->[0]);
            $j++;
        } else {
            print $output_fh sprintf("%d. [%s](#%s)  \n", $i, $record->[1], $record->[0]);
            $j = 1;
            $i++;
        }
    }
    print $output_fh "\n";
206
    foreach my $record (@{$data->{'diagrams'}}) {
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
        if ($record->[3]) {
            print $output_fh sprintf("### <a name='%s'></a>%s\n\n%s\n\n```\n%s\n```\n![diagram](%s)\n", 
                $record->[0],
                $record->[1],
                $record->[2],
                $record->[3],
                $record->[4],
            ), "\n";
        } else {
            print $output_fh sprintf("## <a name='%s'></a>%s\n\n%s\n\n", 
                $record->[0],
                $record->[1],
                $record->[2],
            ), "\n";
        }
    }
}

Matthieu Muffato's avatar
Matthieu Muffato committed
225
226
227
228
229
230
231
232
sub _rst_underline {
    my ($str, $char) = @_;
    return $str . "\n" . ($char x length($str)) . "\n\n";
}

sub generate_rst {
    my ($output_fh, $data) = @_;

233
    print $output_fh _rst_underline($data->{'title'}, '=');
234
    print $output_fh ".. contents::\n\n";
235
    foreach my $record (@{$data->{'diagrams'}}) {
Matthieu Muffato's avatar
Matthieu Muffato committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        if ($record->[3]) {
            print $output_fh _rst_underline($record->[1], '~');
            print $output_fh $record->[2], "\n\n";
            my $s = $record->[3];
            $s =~ s/^/    /gms;
            print $output_fh "::\n\n", $s, "\n\n";
            print $output_fh ".. figure:: ", $record->[4], "\n\n";
        } else {
            print $output_fh _rst_underline($record->[1], '-');
            print $output_fh $record->[2], "\n\n";
        }
    }
}

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

__DATA__

=pod

=head1 NAME

scripts/make_branch_glossary.pl

=head1 DESCRIPTION

    An internal eHive script for regenerating the document that lists all (most ?) of the dataflow patterns.

=head1 LICENSE

265
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
266
    Copyright [2016-2017] EMBL-European Bioinformatics Institute
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

=head1 CONTACT

    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates

=cut