<commit_before>"""
Convenience wrapper for golr_query.py

In general you should use the `GolrAssociationQuery` object directly. This module provide convenience non-OO functions that typically compose the creation of a GolrAssociationQuery object followed by the `exec` method.

"""
import logging

import pysolr
import json
import logging
import time
from ontobio.golr.golr_query import *

import math ### required for IC calculation

MAX_ROWS = 100000

def get_association(id, **kwargs):
    """
    Fetch an association object by ID
    """
    results = search_associations(id=id, **kwargs)
    return results['associations'][0]

def search_associations(**kwargs):

    """
    Fetch a set of association objects based on a query.
    """
    logging.info("CREATING_GOLR_QUERY {}".format(kwargs))
    q = GolrAssociationQuery(**kwargs)
    return q.exec()

def get_objects_for_subject(subject=None,
                            object_category=None,
                            relation=None,
                            **kwargs):
    """
    Convenience method: Given a subject (e.g. gene, disease, variant), return all associated objects (phenotypes, functions, interacting genes, etc)
    """
    searchresult = search_associations(subject=subject,
                                       fetch_objects=True,
                                       rows=0,
                                       object_category=object_category,
                                       relation=relation,
                                       **kwargs
    )
    objs = searchresult['objects']
    return objs

def get_subjects_for_object(object=None,
                            subject_category=None,
                            subject_taxon=None,
                            relation=None,
                            **kwargs):
    """
    Convenience method: Given a object (e.g. ontology term like phenotype or GO; interacting gene; disease; pathway etc), return all associated subjects (genes, variants, pubs, etc)
    """
    searchresult = search_associations(object=object,
                                       fetch_subjects=True,
                                       rows=0,
                                       subject_category=subject_category,
                                       subject_taxon=subject_taxon,
                                       relation=relation,
                                       **kwargs
    )
    subjs = searchresult['subjects']
    return subjs

def search_associations_compact(**kwargs):
    """
    Convenience method: as for search associations, use compact
    """
    searchresult = search_associations(use_compact_associations=True,
                                       facet_fields=[],
                                       **kwargs
    )
    return searchresult['compact_associations']

def map2slim(subjects, slim, **kwargs):
    """
    Maps a set of subjects (e.g. genes) to a set of slims

    Result is a list of unique subject-class pairs, with
    a list of source assocations
    """
    logging.info("SLIM SUBJECTS:{} SLIM:{} CAT:{}".format(subjects,slim,kwargs.get('category')))
    searchresult = search_associations(subjects=subjects,
                                       slim=slim,
                                       facet_fields=[],
                                       **kwargs
    )
    pmap = {}
    for a in searchresult['associations']:
        subj = a['subject']['id']
        slimmed_terms = a['slim']
        for t in slimmed_terms:
            k = (subj,t)<commit_msg>Decreasing logging<commit_after>"""
Convenience wrapper for golr_query.py

In general you should use the `GolrAssociationQuery` object directly. This module provide convenience non-OO functions that typically compose the creation of a GolrAssociationQuery object followed by the `exec` method.

"""
import logging

import pysolr
import json
import logging
import time
from ontobio.golr.golr_query import *

import math ### required for IC calculation

MAX_ROWS = 100000

def get_association(id, **kwargs):
    """
    Fetch an association object by ID
    """
    results = search_associations(id=id, **kwargs)
    return results['associations'][0]

def search_associations(**kwargs):

    """
    Fetch a set of association objects based on a query.
    """
    logging.info("CREATING_GOLR_QUERY {}".format(kwargs))
    q = GolrAssociationQuery(**kwargs)
    return q.exec()

def get_objects_for_subject(subject=None,
                            object_category=None,
                            relation=None,
                            **kwargs):
    """
    Convenience method: Given a subject (e.g. gene, disease, variant), return all associated objects (phenotypes, functions, interacting genes, etc)
    """
    searchresult = search_associations(subject=subject,
                                       fetch_objects=True,
                                       rows=0,
                                       object_category=object_category,
                                       relation=relation,
                                       **kwargs
    )
    objs = searchresult['objects']
    return objs

def get_subjects_for_object(object=None,
                            subject_category=None,
                            subject_taxon=None,
                            relation=None,
                            **kwargs):
    """
    Convenience method: Given a object (e.g. ontology term like phenotype or GO; interacting gene; disease; pathway etc), return all associated subjects (genes, variants, pubs, etc)
    """
    searchresult = search_associations(object=object,
                                       fetch_subjects=True,
                                       rows=0,
                                       subject_category=subject_category,
                                       subject_taxon=subject_taxon,
                                       relation=relation,
                                       **kwargs
    )
    subjs = searchresult['subjects']
    return subjs

def search_associations_compact(**kwargs):
    """
    Convenience method: as for search associations, use compact
    """
    searchresult = search_associations(use_compact_associations=True,
                                       facet_fields=[],
                                       **kwargs
    )
    return searchresult['compact_associations']

def map2slim(subjects, slim, **kwargs):
    """
    Maps a set of subjects (e.g. genes) to a set of slims

    Result is a list of unique subject-class pairs, with
    a list of source assocations
    """
    logging.info("SLIM SUBJECTS:{} SLIM:{} CAT:{}".format(subjects,slim,kwargs.get('category')))
    searchresult = search_associations(subjects=subjects,
                                       slim=slim,
                                       facet_fields=[],
                                       **kwargs
    )
    pmap = {}
    for a in searchresult['associations']:
        subj = a['subject']['id']
        slimmed_terms = a['slim']
        #logging.info('SLIM: {} {}'.format(subj,slimmed_terms))
        for t in slimmed_terms:
            k = (subj,t)
{'fraction_non_alphanumeric': 0.08434406803290116, 'fraction_numerical': 0.0027882336539802035, 'mean_word_length': 1.7041085563513003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6064890', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1750, 'n_words': 470}