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']}

[((21, 19, 21, 49), 'installed_clients.KBaseReportClient.KBaseReport', 'KBaseReport', ({(21, 31, 21, 48): 'self.callback_url'}, {}), '(self.callback_url)', False, 'from installed_clients.KBaseReportClient import KBaseReport\n'), ((22, 24, 22, 49), 'installed_clients.annotation_ontology_apiServiceClient.annotation_ontology_api', 'annotation_ontology_api', ({}, {}), '()', False, 'from installed_clients.annotation_ontology_apiServiceClient import annotation_ontology_api\n'), ((23, 25, 23, 59), 'installed_clients.WorkspaceClient.Workspace', 'Workspace', ({(23, 35, 23, 58): "config['workspace-url']"}, {}), "(config['workspace-url'])", True, 'from installed_clients.WorkspaceClient import Workspace as Workspace\n'), ((31, 28, 32, 61), 'MergeMetabolicAnnotations.utils.functions.filter_selected_ontologies', 'f.filter_selected_ontologies', (), '', True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), ((39, 8, 39, 34), 'os.mkdir', 'os.mkdir', ({(39, 17, 39, 33): 'output_directory'}, {}), '(output_directory)', False, 'import os\n'), ((41, 24, 41, 60), 'MergeMetabolicAnnotations.utils.functions.get_event_lists', 'f.get_event_lists', ({(41, 42, 41, 59): 'ontology_selected'}, {}), '(ontology_selected)', True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), ((42, 23, 42, 92), 'MergeMetabolicAnnotations.utils.functions.compare_report_stack', 'f.compare_report_stack', ({(42, 46, 42, 58): 'html_reports', (42, 60, 42, 73): 'event_summary', (42, 75, 42, 91): 'output_directory'}, {}), '(html_reports, event_summary, output_directory)', True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), ((34, 12, 34, 59), 'json.dump', 'json.dump', (), '', False, 'import json\n'), ((18, 25, 18, 48), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((33, 18, 33, 70), 'os.path.join', 'os.path.join', ({(33, 31, 33, 43): 'self.scratch', (33, 45, 33, 69): '"""get_ontology_dump.json"""'}, {}), "(self.scratch, 'get_ontology_dump.json')", False, 'import os\n'), ((38, 58, 38, 70), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((50, 57, 50, 69), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n')]