<commit_before>import utils.python.util as _utils
import numpy as np
import sqlite3
import pandas as pd
import pandas.io.sql as psql
import plot_data
import logging

logger = logging.getLogger(__name__)

def retrieve_gene_features(conn, opts):
    """Retrieve gene information from the gene_features table.

    See the gene_features module to understand the gene_features
    database table.

    **Parameters**

    conn : mysql/sqlite connection
        connection to db with gene_features table
    options : dict
        options for getting info

    **Returns**

    df : pd.dataframe
        dataframe of gene lengths
    """
    logger.info('Retrieving features of genes . . .')

    selected_cols = ['gene']

    # retrieve more features if specified by command line
    if opts['gene_length']:
        selected_cols.append('gene_length')
    if opts['mutation_rate']:
        selected_cols.append('noncoding_mutation_rate')
    if opts['replication_time']:
        selected_cols.append('replication_time')
    if opts['expression']:
        selected_cols.append('expression_CCLE as expression')
    if opts['betweeness']:
        selected_cols.append('gene_betweeness')
    if opts['degree']:
        selected_cols.append('gene_degree')
    if opts['cnv_gain']:
        selected_cols.append('cnv_gain')
    if opts['cnv_loss']:
        selected_cols.append('cnv_loss')

    # get info from gene_features table
    logger.info('Retrieving gene feature information from gene_features table . . . ')
    sql = "SELECT %s FROM gene_features" % ', '.join(selected_cols)
    df = psql.frame_query(sql, conn)
    df = df.set_index('gene')
    df['gene'] = df.index
    logger.info('Finished retrieving gene features from gene_features table.')

    # fill graph stats with zeros if gene not in Biogrid
    if 'gene_betweeness' in df.columns:
        df['gene_betweeness'] = df['gene_betweeness'].fillna(0)
    if 'gene_degree' in df.columns:
        df['gene_degree'] = df['gene_degree'].fillna(0)

    # add ratio of CNV gain to CNV loss
    if opts['cnv_ratio']:
        df['cnv_ratio'] = (df['cnv_gain'] + 1).astype(float).div(df['cnv_loss']+1)

    # get position entropy features
    entropy_cfg = _utils.get_output_config('position_entropy')
    mutation_pos_entropy = pd.read_csv(_utils.result_dir + entropy_cfg['mutation_pos_entropy'],
                                       sep='\t', index_col=0)
    missense_pos_entropy = pd.read_csv(_utils.result_dir + entropy_cfg['missense_pos_entropy'],
                                       sep='\t', index_col=0)
    df['mutation position entropy'] = mutation_pos_entropy['mutation position entropy']
    df['pct of uniform mutation entropy'] = mutation_pos_entropy['pct of uniform mutation entropy']
    df['missense position entropy'] = missense_pos_entropy['missense position entropy']
    df['pct of uniform missense entropy'] = missense_pos_entropy['pct of uniform missense entropy']

    return df


def label_gene(gene,
               oncogene=True,
               tsg=True,
               kind='vogelstein'):
    """Label a gene according to Vogelstein's list of oncogenes
    and tsg."""
    # set integer representation of classes
    other_num = 0
    if oncogene: onco_num = 1
    if tsg: tsg_num = 2 if oncogene else 1
    smg_num = 1

    # classify genes
    if kind == 'vogelstein':
        if gene in _utils.oncogene_set:
            return onco_num
        elif gene in _utils.tsg_set:
            return tsg_num
        else:
            return other_num
    elif kind == 'smg':
        if gene in _utils.smg_list:
            return smg_num
        else:
            return other_num<commit_msg>use class integers in util module instead of hardcoding class representation in features module<commit_after>import utils.python.util as _utils
import numpy as np
import sqlite3
import pandas as pd
import pandas.io.sql as psql
import plot_data
import logging

logger = logging.getLogger(__name__)

def retrieve_gene_features(conn, opts):
    """Retrieve gene information from the gene_features table.

    See the gene_features module to understand the gene_features
    database table.

    **Parameters**

    conn : mysql/sqlite connection
        connection to db with gene_features table
    options : dict
        options for getting info

    **Returns**

    df : pd.dataframe
        dataframe of gene lengths
    """
    logger.info('Retrieving features of genes . . .')

    selected_cols = ['gene']

    # retrieve more features if specified by command line
    if opts['gene_length']:
        selected_cols.append('gene_length')
    if opts['mutation_rate']:
        selected_cols.append('noncoding_mutation_rate')
    if opts['replication_time']:
        selected_cols.append('replication_time')
    if opts['expression']:
        selected_cols.append('expression_CCLE as expression')
    if opts['betweeness']:
        selected_cols.append('gene_betweeness')
    if opts['degree']:
        selected_cols.append('gene_degree')
    if opts['cnv_gain']:
        selected_cols.append('cnv_gain')
    if opts['cnv_loss']:
        selected_cols.append('cnv_loss')

    # get info from gene_features table
    logger.info('Retrieving gene feature information from gene_features table . . . ')
    sql = "SELECT %s FROM gene_features" % ', '.join(selected_cols)
    df = psql.frame_query(sql, conn)
    df = df.set_index('gene')
    df['gene'] = df.index
    logger.info('Finished retrieving gene features from gene_features table.')

    # fill graph stats with zeros if gene not in Biogrid
    if 'gene_betweeness' in df.columns:
        df['gene_betweeness'] = df['gene_betweeness'].fillna(0)
    if 'gene_degree' in df.columns:
        df['gene_degree'] = df['gene_degree'].fillna(0)

    # add ratio of CNV gain to CNV loss
    if opts['cnv_ratio']:
        df['cnv_ratio'] = (df['cnv_gain'] + 1).astype(float).div(df['cnv_loss']+1)

    # get position entropy features
    entropy_cfg = _utils.get_output_config('position_entropy')
    mutation_pos_entropy = pd.read_csv(_utils.result_dir + entropy_cfg['mutation_pos_entropy'],
                                       sep='\t', index_col=0)
    missense_pos_entropy = pd.read_csv(_utils.result_dir + entropy_cfg['missense_pos_entropy'],
                                       sep='\t', index_col=0)
    df['mutation position entropy'] = mutation_pos_entropy['mutation position entropy']
    df['pct of uniform mutation entropy'] = mutation_pos_entropy['pct of uniform mutation entropy']
    df['missense position entropy'] = missense_pos_entropy['missense position entropy']
    df['pct of uniform missense entropy'] = missense_pos_entropy['pct of uniform missense entropy']

    return df


def label_gene(gene,
               oncogene=True,
               tsg=True,
               kind='onco_tsg'):
    """Label a gene according to list of oncogenes
    and tsg."""
    # set integer representation of classes
    other_num = _utils.other_label
    if oncogene: onco_num = _utils.onco_label
    if tsg: tsg_num = _utils.tsg_label if oncogene else _utils.onco_label
    smg_num = 1

    # classify genes
    if kind == 'onco_tsg':
        if gene in _utils.oncogene_set:
            return onco_num
        elif gene in _utils.tsg_set:
            return tsg_num
        else:
            return other_num
    elif kind == 'smg':
        if gene in _utils.smg_list:
            return smg_num
        else:
            return other_num
{'fraction_non_alphanumeric': 0.09602249899558056, 'fraction_numerical': 0.0026784518548279095, 'mean_word_length': 2.9325961032122168, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2855437', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2197, 'n_words': 597}