# -*- coding: utf-8 -*-
#
#  This file is part of Sequana software
#
#  Copyright (c) 2016-2021 - Sequana Dev Team (https://sequana.readthedocs.io)
#
#  File author(s):
#      Thomas Cokelaer <thomas.cokelaer@pasteur.fr>
#
#  Distributed under the terms of the 3-clause BSD license.
#  The full license is in the LICENSE file, distributed with this software.
#
#  Website:       https://github.com/sequana/sequana
#  Documentation: http://sequana.readthedocs.io
#  Contributors:  https://github.com/sequana/sequana/graphs/contributors
##############################################################################



rule summary_pipeline:
    """ Create a JSON file that summarise all information about the pipeline.
    This JSON file is used by the sequana reports. It creates
    the summary report for your samples.

    Required input:
        - __summary_pipeline__inputs = list of the pipeline input.
        - __summary_pipeline__outputs = list of the pipeline outputs.
        - __summary_pipeline__html = list of html pages created by rules.
        - __summary_pipeline__rulegraph = the rulegraph SVG filename.
        - __summary_pipeline__requirements = the requirements filename (conda).
        - __summary_pipeline__snakefile = the snakemake file.
        - __summary_pipeline__config = the snakemake config file.

    Required output:
        - __summary_pipeline__json_output = the json output file.

    Required params:
        - __summary_pipeline__name = pipeline name.

    """
    input:
        inputs = __summary_pipeline__inputs,
        outputs = __summary_pipeline__outputs,
        html = __summary_pipeline__html,
        rulegraph = __summary_pipeline__rulegraph,
        snakefile = __summary_pipeline__snakefile,
        config = __summary_pipeline__config,
        requirements = __summary_pipeline__requirements
    output:
        json = __summary_pipeline__json_output
    params:
        name = __summary_pipeline__name
    run:
        print("summary_pipeline will be removed in the future. Copy the rule in the pipeline if needed")
        import json
        import os.path

        # use real path
        inputs = [os.path.realpath(f) for f in input['inputs']]
        outputs = [os.path.realpath(f) for f in input['outputs']]
        html = [os.path.realpath(f) for f in input['html']]
        # create json
        summary = {'tool': 'sequana_summary',
                   'inputs': inputs,
                   'outputs': outputs,
                   'html': html,
                   'rulegraph': os.path.realpath(input['rulegraph']),
                   'requirements': os.path.realpath(input['requirements']),
                   'snakefile': os.path.realpath(input['snakefile']),
                   'config': os.path.realpath(input['config']),
                   'name': params['name']}
        js = json.dumps(summary, indent=4, sort_keys=True)
        with open(output['json'], 'w') as fp:
            fp.write(js)
