<commit_before>#!/usr/bin/env python3

## Author(s): <NAME>
## Contact(s): <EMAIL>
## Python version: 3.6.0
## Creation : 12-20-2016

'''
Generation of XML for Galaxy from https://bio.tools based on the Tooldog model using
galaxyxml library.
'''

###########  Import  ###########

# General libraries
import os
import copy
import json
import logging

# External libraries
import galaxyxml.tool as gxt
import galaxyxml.tool.parameters as gxtp
import requests

# Class and Objects
from ontospy import Ontospy

###########  Constant(s)  ###########

LOCAL_DATA = os.path.dirname(__file__) + "/data"

###########  Logger  ###########

LOGGER = logging.getLogger(__name__)

###########  Class(es)  ###########

class GalaxyInfo(object):
    '''
    Class to gather different information about a Galaxy instance.

    By default, if the galaxy_url is None, information is loaded from local files
    located in the `data/` folder.
    '''

    def __init__(self, galaxy_url):
        '''
        :param galaxy_url: URL of the galaxy instance.
        :type galaxy_url: STRING

        :class:`tooldog.galaxy.GalaxyInfo` object is initialized with two empty
        dictionnaries:

        * datatypes_from_formats: unique datatype for one given edam_format.
        * datatypes_from_data: unique datatype for one given edam_data.

        Dictionnaries are then filled in with :meth:`tooldog.galaxy.GalaxyInfo.load_info`.
        '''
        self.galaxy_url = galaxy_url
        if self.galaxy_url is None:
            LOGGER.info("Loading galaxy info from " + LOCAL_DATA)
            with open(LOCAL_DATA + "/edam_formats.json") as json_file:
                api_edam_formats = json.load(json_file)
            with open(LOCAL_DATA + "/edam_data.json") as json_file:
                api_edam_data = json.load(json_file)
            with open(LOCAL_DATA + "/mapping.json") as json_file:
                mapping = json.load(json_file)
        else:
            LOGGER.info("Loading galaxy info from " + galaxy_url +"/api/datatypes")
            api_edam_formats = requests.get(galaxy_url + "/api/datatypes/edam_formats").json()
            api_edam_data = requests.get(galaxy_url + "/api/datatypes/edam_data").json()
            mapping = requests.get(galaxy_url + "/api/datatypes/mapping").json()
        # Reverse EDAMs dictionnaries
        def rev_dict(dictionnary):
            new_dict = {}
            for key, value in dictionnary.items():
                if not value in new_dict:
                    new_dict[value] = []
                new_dict[value].append(key)
            return new_dict
        self.edam_formats = rev_dict(api_edam_formats)
        self.edam_data = rev_dict(api_edam_data)
        # Store hierarchy from mapping and class names
        self.hierarchy = mapping["class_to_direct_parents"]
        self.class_names = mapping["ext_to_class_name"]

    def select_best(self, datatypes):
        '''
        Select the last common datatype to all given datatypes.

        :param datatypes: list of different datatypes.
        :type datatypes: list of STRING
        :return: last common datatype.
        :rtype: STRING
        '''
        return "MULTI MAPPING"


class EdamInfo(object):<commit_msg>add first try for finding best datatype between a list<commit_after>#!/usr/bin/env python3

## Author(s): <NAME>
## Contact(s): <EMAIL>
## Python version: 3.6.0
## Creation : 12-20-2016

'''
Generation of XML for Galaxy from https://bio.tools based on the Tooldog model using
galaxyxml library.
'''

###########  Import  ###########

# General libraries
import os
import copy
import json
import logging

# External libraries
import galaxyxml.tool as gxt
import galaxyxml.tool.parameters as gxtp
import requests

# Class and Objects
from ontospy import Ontospy

###########  Constant(s)  ###########

LOCAL_DATA = os.path.dirname(__file__) + "/data"

###########  Logger  ###########

LOGGER = logging.getLogger(__name__)

###########  Class(es)  ###########

class GalaxyInfo(object):
    '''
    Class to gather different information about a Galaxy instance.

    By default, if the galaxy_url is None, information is loaded from local files
    located in the `data/` folder.
    '''

    def __init__(self, galaxy_url):
        '''
        :param galaxy_url: URL of the galaxy instance.
        :type galaxy_url: STRING

        :class:`tooldog.galaxy.GalaxyInfo` object is initialized with two empty
        dictionnaries:

        * datatypes_from_formats: unique datatype for one given edam_format.
        * datatypes_from_data: unique datatype for one given edam_data.

        Dictionnaries are then filled in with :meth:`tooldog.galaxy.GalaxyInfo.load_info`.
        '''
        self.galaxy_url = galaxy_url
        if self.galaxy_url is None:
            LOGGER.info("Loading galaxy info from " + LOCAL_DATA)
            with open(LOCAL_DATA + "/edam_formats.json") as json_file:
                api_edam_formats = json.load(json_file)
            with open(LOCAL_DATA + "/edam_data.json") as json_file:
                api_edam_data = json.load(json_file)
            with open(LOCAL_DATA + "/mapping.json") as json_file:
                mapping = json.load(json_file)
        else:
            LOGGER.info("Loading galaxy info from " + galaxy_url +"/api/datatypes")
            api_edam_formats = requests.get(galaxy_url + "/api/datatypes/edam_formats").json()
            api_edam_data = requests.get(galaxy_url + "/api/datatypes/edam_data").json()
            mapping = requests.get(galaxy_url + "/api/datatypes/mapping").json()
        # Reverse EDAMs dictionnaries
        def rev_dict(dictionnary):
            new_dict = {}
            for key, value in dictionnary.items():
                if not value in new_dict:
                    new_dict[value] = []
                new_dict[value].append(key)
            return new_dict
        self.edam_formats = rev_dict(api_edam_formats)
        self.edam_data = rev_dict(api_edam_data)
        # Store hierarchy from mapping and class names
        self.hierarchy = mapping["class_to_direct_parents"]
        self.class_names = mapping["ext_to_class_name"]

    def select_best(self, datatypes):
        '''
        Select the last common datatype to all given datatypes.

        :param datatypes: list of different datatypes.
        :type datatypes: list of STRING
        :return: last common datatype.
        :rtype: STRING
        '''
        # Build class to ext dictionnary
        class_to_ext = {}
        for key, value in self.class_names.items():
            if value not in class_to_ext:
                class_to_ext[value] = []
            class_to_ext[value].append(key)
        # Create class list from datatypes and subdict of hierarchy
        selected_class = []
        sub_dict = {}
        for datatype in datatypes:
            if datatype in self.class_names:
                selected_class.append(self.class_names[datatype])
                sub_dict[self.class_names[datatype]] = self.hierarchy[self.class_names[datatype]]
            else:
                LOGGER.warning(datatype + " was not found in the ext to class mapping. skipped")
        # Find best class
        for class_name in sub_dict.keys():
            for key, value in sub_dict.items():
                if not class_name == key:
                    if not class_name in value:
                        selected_class.remove(class_name)
                        break
        if len(selected_class) == 0:
            LOGGER.warning("No best datatype found, return first datatype")
            return datatypes[0]
        elif len(selected_class) > 1:
            LOGGER.warning("More than one best datatype found, return first")
        if len(class_to_ext[selected_class[0]]) > 1:
            LOGGER.warning("More than one extension corresponds to the class, first is kept")
        return class_to_ext[selected_class[0]][0]


class EdamInfo(object):
{'fraction_non_alphanumeric': 0.10852029241240232, 'fraction_numerical': 0.003907234686160827, 'mean_word_length': 2.495594713656388, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3594976', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2348, 'n_words': 623}