analysis_diagram.py 4.71 KB
Newer Older
1

2
# Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
3
# Copyright [2016-2021] EMBL-European Bioinformatics Institute
4 5 6 7 8 9 10 11 12 13 14 15 16
# 
# 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.

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
####################################################################
## Sphinx extension to generate analysis diagrams from pipeconfigs #
####################################################################

# Use like this in your RestructuredText document
#
# .. hive_diagram::
#
#     {   -logic_name => 'A',
#         -flow_into  => {
#            1 => [ 'B' ],
#         },
#     },
#     {   -logic_name => 'B',
#     },
#
33 34
# The directive will show side-by-side the pipeconfig code and the
# diagram it models
35

36 37 38
# For python2 compatibility
from __future__ import print_function

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
import json
import os.path
import subprocess
import sys
import tempfile

from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.parsers.rst.directives import tables

from sphinx.ext.graphviz import graphviz


class HiveDiagramDirective(tables.ListTable):

    # defines the parameter the directive expects
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = False
    has_content = True
    add_index = True

    def run(self):

        # The PipeConfig sample is shown in a literal block
        content = '\n'.join(self.content)
        code_block_node = nodes.literal_block(text=content)

        # We reuse the graphviz node (from the graphviz extension) as it deals better with image formats vs builders
        graphviz_node = graphviz()
        graphviz_node['code'] = generate_dot_diagram(content)
        graphviz_node['options'] = {}

72
        table = [[[code_block_node], [graphviz_node]]]
73
        table_node = self.build_table_from_list(table, [50,50], 0, 0)
74
        return [table_node]
75 76 77 78 79 80 81 82


pipeconfig_template = """
package %s;

use strict;
use warnings;

83
use Bio::EnsEMBL::Hive::PipeConfig::HiveGeneric_conf;  # For INPUT_PLUS, WHEN and ELSE
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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;
"""

display_config_json = json.dumps( {
    "Graph": {
        "Pad": 0,
        "DisplayStats": 0,
        "DisplayDBIDs": 0,
        "DisplayDetails": 0,
    }
} )


106 107 108
json_filename = None
pipeconfig_filename = None

109 110 111
def generate_dot_diagram(pipeconfig_content):

    # A temporary file for the JSON config
112 113
    global json_filename
    if json_filename is None:
114 115
        json_fh = tempfile.NamedTemporaryFile(mode='w+', dir="_build", delete=False)
        print(display_config_json, file=json_fh)
116 117
        json_fh.close()
        json_filename = json_fh.name
118 119 120 121 122

    # eHive's default configuration file
    default_config_file = os.environ["EHIVE_ROOT_DIR"] + os.path.sep + "hive_config.json"

    # A temporary file for the sample PipeConfig
123 124
    global pipeconfig_filename
    if pipeconfig_filename is None:
125
        pipeconfig_fh = tempfile.NamedTemporaryFile(mode='w+', suffix='.pm', dir="_build", delete=False)
126 127 128 129 130
        pipeconfig_filename = pipeconfig_fh.name
    else:
        pipeconfig_fh = open(pipeconfig_filename, "w")

    package_name = "_build::" + os.path.basename(pipeconfig_fh.name)[:-3]
131
    print(pipeconfig_template % (package_name, pipeconfig_content), file=pipeconfig_fh)
132 133
    pipeconfig_fh.close()

134
    # Run generate_graph and read the content of the dot file
135
    graph_path = os.path.join(os.environ["EHIVE_ROOT_DIR"], "scripts", "generate_graph.pl")
136 137
    if os.environ["PERL5LIB"][-2:] != ":.":
        os.environ["PERL5LIB"] = os.environ["PERL5LIB"] + ":."
138
    dotcontent = subprocess.check_output([graph_path, "-pipeconfig", pipeconfig_fh.name, "--format", "dot", "-config_file", default_config_file, "-config_file", json_filename], stderr=sys.stderr)
139

140
    return dotcontent.decode()
141 142


143 144 145 146 147 148 149
def cleanup_tmp_files(app, exception):
    if json_filename is not None:
        os.remove(json_filename)
    if pipeconfig_filename is not None:
        os.remove(pipeconfig_filename)


150 151 152
## Register the extension
def setup(app):
    app.add_directive('hive_diagram', HiveDiagramDirective)
153
    app.connect('build-finished', cleanup_tmp_files)
154