<commit_before># -*- coding: utf-8 -*-
# Copyright (C) 2009-2010, <NAME> <<EMAIL>>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
# License: MIT. See COPYING.MIT file in the Waldo distribution

from __future__ import division
from sqlalchemy import and_
from waldo import backend
from waldo.locate.models import Entry
from waldo.translations.services import translate
from waldo.go.go import id_to_term

def from_ensembl_gene_id(ensembl_gene_id, session=None):
    '''
    locate_id = from_ensembl_gene_id(ensembl_gene_id, session={backend.create_session()})

    Convert ensembl_gene_id to LOCATE uid.

    Parameters
    ----------
      ensembl_gene_id : Ensembl gene ID
      session : SQLAlchemy session to use (default: call backend.create_session())
    Returns
    -------
      locate_id : LOCATE protein identifier
    '''
    return translate(ensembl_gene_id, 'ensembl:gene_id', 'locate:id', session)

def from_ensembl_peptide_id(ensembl_peptide_id, session=None):
    '''
    locate_id = from_ensembl_peptide_id(ensembl_peptide_id, session={backend.create_session()})

    Convert ensembl_peptide_id to LOCATE uid.

    Parameters
    ----------
    ensembl_peptide_id : str
        Ensembl peptide ID
    session : SQLAlchemy session object
        session to use (default: call backend.create_session())

    Returns
    -------
    locate_id : str
        LOCATE protein identifier
    '''
    return translate(ensembl_peptide_id, 'ensembl:peptide_id', 'locate:id', session)

def _splitGO(goids):
    goids = goids.split(';')
    return goids

def retrieve_go_annotations(id, session=None):
    '''
    go_ids = retrieve_go_annotations(name, session={backend.create_session()})

    Retrieve GO ids by LOCATE identifier.

    Parameters
    ----------
    id : str
        LOCATE protein uid
    session : SQLAlchemy session object, optional
        session to use (default: call backend.create_session())

    Returns
    -------
    go_ids : list of str
        go terms (of the form "GO:00...")
    '''
    if session is None: session = backend.create_session()
    entry = session.query(Entry).filter(Entry.id == id).first()

    locations = set()
    for location in entry.locations:
        locations.update(_splitGO(location.go_id))

    for predict in entry.predictions:
        locations.update(_splitGO(predict.go_id))

    for reference in entry.references:
        for location in reference.locations:
            locations.update(_splitGO(location.go_id))

    for annotation in entry.go_annotations:
        for location in annotation.locations:
            locations.update(_splitGO(location.go_id))

    return list(locations)

def retrieve_entry(id, session=None):
    '''
    entry = retrieve_entry(id, session={backend.create_session()})

    Retrieve an Entry object based on its ID

    Parameters
    ----------
      id : LOCATE database ID<commit_msg>RFCT+MIN Inline one-liner<commit_after># -*- coding: utf-8 -*-
# Copyright (C) 2009-2012, <NAME> <<EMAIL>> and <NAME> <<EMAIL>>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
# License: MIT. See COPYING.MIT file in the Waldo distribution

from __future__ import division
from sqlalchemy import and_
from waldo import backend
from waldo.locate.models import Entry
from waldo.translations.services import translate
from waldo.go.go import id_to_term

def from_ensembl_gene_id(ensembl_gene_id, session=None):
    '''
    locate_id = from_ensembl_gene_id(ensembl_gene_id, session={backend.create_session()})

    Convert ensembl_gene_id to LOCATE uid.

    Parameters
    ----------
      ensembl_gene_id : Ensembl gene ID
      session : SQLAlchemy session to use (default: call backend.create_session())
    Returns
    -------
      locate_id : LOCATE protein identifier
    '''
    return translate(ensembl_gene_id, 'ensembl:gene_id', 'locate:id', session)

def from_ensembl_peptide_id(ensembl_peptide_id, session=None):
    '''
    locate_id = from_ensembl_peptide_id(ensembl_peptide_id, session={backend.create_session()})

    Convert ensembl_peptide_id to LOCATE uid.

    Parameters
    ----------
    ensembl_peptide_id : str
        Ensembl peptide ID
    session : SQLAlchemy session object
        session to use (default: call backend.create_session())

    Returns
    -------
    locate_id : str
        LOCATE protein identifier
    '''
    return translate(ensembl_peptide_id, 'ensembl:peptide_id', 'locate:id', session)


def retrieve_go_annotations(id, session=None):
    '''
    go_ids = retrieve_go_annotations(name, session={backend.create_session()})

    Retrieve GO ids by LOCATE identifier.

    Parameters
    ----------
    id : str
        LOCATE protein uid
    session : SQLAlchemy session object, optional
        session to use (default: call backend.create_session())

    Returns
    -------
    go_ids : list of str
        go terms (of the form "GO:00...")
    '''
    if session is None: session = backend.create_session()
    entry = session.query(Entry).filter(Entry.id == id).first()
    locations = set()
    for location in entry.locations:
        locations.update(location.go_id.split(';'))

    for predict in entry.predictions:
        locations.update(predict.go_id.split(';'))

    for reference in entry.references:
        for location in reference.locations:
            locations.update(location.go_id.split(';')

    for annotation in entry.go_annotations:
        for location in annotation.locations:
            locations.update(location.go_id.split(';'))

    return list(locations)

def retrieve_entry(id, session=None):
    '''
    entry = retrieve_entry(id, session={backend.create_session()})

    Retrieve an Entry object based on its ID

    Parameters
    ----------
      id : LOCATE database ID
{'fraction_non_alphanumeric': 0.11070559610705596, 'fraction_numerical': 0.004866180048661801, 'mean_word_length': 3.0815602836879434, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5142982', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1695, 'n_words': 450}