<commit_before>import re
from xbrowse import get_xpos
from utils import xpos_to_pos

SEARCH_LIMIT = 10000

def get_gene(db, gene_id):
    return db.genes.find_one({'gene_id': gene_id}, fields={'_id': False})


def get_gene_by_name(db, gene_name):
    return db.genes.find_one({'gene_name': gene_name}, fields={'_id': False})


def get_transcript(db, transcript_id):
    transcript = db.transcripts.find_one({'transcript_id': transcript_id}, fields={'_id': False})
    if not transcript:
        return None
    transcript['exons'] = get_exons_in_transcript(db, transcript_id)
    return transcript


def get_variant(db, xpos, ref, alt):
    return db.variants.find_one({'xpos': xpos, 'ref': ref, 'alt': alt}, fields={'_id': False})


def get_variants_by_rsid(db, rsid):
    print 'Looking up: ', rsid
    if not rsid.startswith('RS'):
        return None
    return list(db.variants.find({'rsid': rsid}, fields={'_id': False}))


def get_coverage_for_bases(db, xstart, xstop=None):
    """
    Get the coverage for the list of bases given by xstart->xstop, inclusive
    Returns list of coverage dicts
    xstop can be None if just one base, but you'll still get back a list
    """
    if xstop is None:
        xstop = xstart
    coverages = {
        doc['xpos']: doc for doc in db.base_coverage.find(
            {'xpos': {'$gte': xstart, '$lte': xstop}},
            fields={'_id': False}
        )
    }
    ret = []
    for i in range(xstart, xstop+1):
        if i in coverages:
            ret.append(coverages[i])
        else:
            ret.append({'xpos': i})
    for item in ret:
        item['has_coverage'] = 'mean' in item
    return ret


def get_coverage_for_transcript(db, genomic_coord_to_exon, xstart, xstop=None):
    """

    :param db:
    :param genomic_coord_to_exon:
    :param xstart:
    :param xstop:
    :return:
    """
    null_coverage = {
        'mean': 0,
        'median': 0,
        '1': 0,
        '5': 0,
        '10': 0,
        '15': 0,
        '20': 0,
        '25': 0,
        '30': 0,
        '50': 0,
        '100': 0
    }
    coverage_array = get_coverage_for_bases(db, xstart, xstop)
    for item in coverage_array:
        xpos = item['xpos']
        if xpos_to_pos(xpos) in genomic_coord_to_exon:
            item['exon_number'] = genomic_coord_to_exon[xpos_to_pos(item['xpos'])]
        else:
            item['exon_number'] = -1
        if not item['has_coverage']:
            for entry in null_coverage:
                item[entry] = null_coverage[entry]
    return coverage_array


def get_awesomebar_suggestions(db, query):
    """
    This generates autocomplete suggestions when user<commit_msg>Fixing gene page coverage plot, which was causing JS errors<commit_after>import re
from xbrowse import get_xpos
from utils import xpos_to_pos

SEARCH_LIMIT = 10000

def get_gene(db, gene_id):
    return db.genes.find_one({'gene_id': gene_id}, fields={'_id': False})


def get_gene_by_name(db, gene_name):
    return db.genes.find_one({'gene_name': gene_name}, fields={'_id': False})


def get_transcript(db, transcript_id):
    transcript = db.transcripts.find_one({'transcript_id': transcript_id}, fields={'_id': False})
    if not transcript:
        return None
    transcript['exons'] = get_exons_in_transcript(db, transcript_id)
    return transcript


def get_variant(db, xpos, ref, alt):
    return db.variants.find_one({'xpos': xpos, 'ref': ref, 'alt': alt}, fields={'_id': False})


def get_variants_by_rsid(db, rsid):
    print 'Looking up: ', rsid
    if not rsid.startswith('RS'):
        return None
    return list(db.variants.find({'rsid': rsid}, fields={'_id': False}))


def get_coverage_for_bases(db, xstart, xstop=None):
    """
    Get the coverage for the list of bases given by xstart->xstop, inclusive
    Returns list of coverage dicts
    xstop can be None if just one base, but you'll still get back a list
    """
    if xstop is None:
        xstop = xstart
    coverages = {
        doc['xpos']: doc for doc in db.base_coverage.find(
            {'xpos': {'$gte': xstart, '$lte': xstop}},
            fields={'_id': False}
        )
    }
    ret = []
    for i in range(xstart, xstop+1):
        if i in coverages:
            ret.append(coverages[i])
        else:
            ret.append({'xpos': i})
    for item in ret:
        item['has_coverage'] = 'mean' in item
    return ret


def get_coverage_for_transcript(db, genomic_coord_to_exon, xstart, xstop=None):
    """

    :param db:
    :param genomic_coord_to_exon:
    :param xstart:
    :param xstop:
    :return:
    """
    null_coverage = {
        'mean': 0,
        'median': 0,
        '1': 0,
        '5': 0,
        '10': 0,
        '15': 0,
        '20': 0,
        '25': 0,
        '30': 0,
        '50': 0,
        '100': 0
    }
    coverage_array = get_coverage_for_bases(db, xstart, xstop)
    for item in coverage_array:
        xpos = item['xpos']
        if xpos_to_pos(xpos) in genomic_coord_to_exon:
            item['exon_number'] = genomic_coord_to_exon[xpos_to_pos(item['xpos'])]
        else:
            item['exon_number'] = -1
        if not item['has_coverage']:
            for entry in null_coverage:
                item[entry] = null_coverage[entry]
    # only return coverages that have coverage (if that makes any sense?)
    return [c for c in coverage_array if c['has_coverage']]


def get_awesomebar_suggestions(db, query):
    """
    This generates autocomplete suggestions when user
{'fraction_non_alphanumeric': 0.12710314557425018, 'fraction_numerical': 0.012801755669348939, 'mean_word_length': 2.720408163265306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4683236', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1860, 'n_words': 491}