import os
import datetime
import logging
import json
import uuid

from installed_clients.WorkspaceClient import Workspace as Workspace
from installed_clients.KBaseReportClient import KBaseReport
from installed_clients.annotation_ontology_apiServiceClient import annotation_ontology_api

import MergeMetabolicAnnotations.utils.functions as f


class CompareAnnotationsUtil:

    def __init__(self, config):
        self.config = config
        self.timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
        self.callback_url = config['SDK_CALLBACK_URL']
        self.scratch = config['scratch']
        self.kbr = KBaseReport(self.callback_url)
        self.anno_api = annotation_ontology_api()
        self.ws_client = Workspace(config["workspace-url"])

    def run(self, ctx, params):
        get_ontology_results = self.anno_api.get_annotation_ontology_events({
            "input_ref": params['genome'],
            "workspace-url": self.config["workspace-url"]
        })

        ontology_selected = f.filter_selected_ontologies(
            get_ontology_results, params, workflow="compare")
        with open(os.path.join(self.scratch, "get_ontology_dump.json"), 'w') as outfile:
            json.dump(ontology_selected, outfile, indent=2)

        # make reports
        html_reports = []
        output_directory = os.path.join(self.scratch, str(uuid.uuid4()))
        os.mkdir(output_directory)

        event_summary = f.get_event_lists(ontology_selected)
        html_reports = f.compare_report_stack(html_reports, event_summary, output_directory)

        # finalize html reports
        report_params = {
            'message': '',
            'html_links': html_reports,
            'direct_html_link_index': 0,
            'workspace_name': params['workspace_name'],
            'report_object_name': f'compare_annotations_{uuid.uuid4()}'}

        report_output = self.kbr.create_extended_report(report_params)

        return {'report_name': report_output['name'],
                'report_ref': report_output['ref']}

[((629, 659), 'installed_clients.KBaseReportClient.KBaseReport', 'KBaseReport', (['self.callback_url'], {}), '(self.callback_url)\n', (640, 659), False, 'from installed_clients.KBaseReportClient import KBaseReport\n'), ((684, 709), 'installed_clients.annotation_ontology_apiServiceClient.annotation_ontology_api', 'annotation_ontology_api', ([], {}), '()\n', (707, 709), False, 'from installed_clients.annotation_ontology_apiServiceClient import annotation_ontology_api\n'), ((735, 769), 'installed_clients.WorkspaceClient.Workspace', 'Workspace', (["config['workspace-url']"], {}), "(config['workspace-url'])\n", (744, 769), True, 'from installed_clients.WorkspaceClient import Workspace as Workspace\n'), ((1022, 1100), 'MergeMetabolicAnnotations.utils.functions.filter_selected_ontologies', 'f.filter_selected_ontologies', (['get_ontology_results', 'params'], {'workflow': '"""compare"""'}), "(get_ontology_results, params, workflow='compare')\n", (1050, 1100), True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), ((1394, 1420), 'os.mkdir', 'os.mkdir', (['output_directory'], {}), '(output_directory)\n', (1402, 1420), False, 'import os\n'), ((1446, 1482), 'MergeMetabolicAnnotations.utils.functions.get_event_lists', 'f.get_event_lists', (['ontology_selected'], {}), '(ontology_selected)\n', (1463, 1482), True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), ((1506, 1575), 'MergeMetabolicAnnotations.utils.functions.compare_report_stack', 'f.compare_report_stack', (['html_reports', 'event_summary', 'output_directory'], {}), '(html_reports, event_summary, output_directory)\n', (1528, 1575), True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), ((1215, 1262), 'json.dump', 'json.dump', (['ontology_selected', 'outfile'], {'indent': '(2)'}), '(ontology_selected, outfile, indent=2)\n', (1224, 1262), False, 'import json\n'), ((460, 483), 'datetime.datetime.now', 'datetime.datetime.now', ([], {}), '()\n', (481, 483), False, 'import datetime\n'), ((1132, 1184), 'os.path.join', 'os.path.join', (['self.scratch', '"""get_ontology_dump.json"""'], {}), "(self.scratch, 'get_ontology_dump.json')\n", (1144, 1184), False, 'import os\n'), ((1371, 1383), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (1381, 1383), False, 'import uuid\n'), ((1856, 1868), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (1866, 1868), False, 'import uuid\n')]