import os
import pickle
import pandas as pd

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))


def return_samples():
    """
    Returns sample dictionary which maps TCGA and GTEx samples to a tissue.
    Synapse ID: syn10296681

    :return: Tissues are keys are list of samples are values
    :rtype: dict[list]
    """
    return pickle.load(open(os.path.join(__location__, 'data/samples.pickle'), 'rb'))


def get_gene_map():
    """
    Dictionary mapping gene ID to gene name

    :return: Gene map
    :rtype: dict
    """
    return pickle.load(open(os.path.join(__location__, 'data/gene_map.pickle'), 'rb'))


def map_genes(df):
    gene_map = get_gene_map()
    genes = [gene_map[x.split('.')[0]] for x in df.index]
    df.index = genes
    return df


def get_mab_targets():
    """
    Returns sorted list of MAB cancer drug targets

    :return: Sorted gene list
    :rtype: list
    """
    path = os.path.join(__location__, 'data/cancer-MAB-gene-targets.txt')
    return sorted([x.strip() for x in open(path, 'r').readlines()])


def get_ucsf_genes():
    """
    Returns sorted list of UCSF genes

    :return: Sorted gene list
    :rtype: list
    """
    path = os.path.join(__location__, 'data/UCSF-genes.csv')
    return sorted([x.strip() for x in open(path, 'r').readlines()])


def get_civic_genes():
    """
    Returns sorted list of genes from CIViC

    :return: Sorted gene list
    :rtype: list
    """
    path = os.path.join(__location__, 'data/civic-genes.txt')
    return sorted([x.strip() for x in open(path, 'r').readlines()])


def get_ucsf_subset(df):
    """
    Subset UCSF dataframe and return.

    :param pd.DataFrame df: Input Dataframe in the format of "Genes by Samples"
    :return: Subset of Dataframe that only includes UCSF genes
    :rtype: pd.DataFrame
    """
    df = map_genes(df)

    ucsf_genes = get_ucsf_genes()
    ucsf_genes = [x for x in ucsf_genes if x in df.index]

    return df.loc[ucsf_genes]

@@ -21,27 +21,93 @@
 kle%0A
-import pandas as pd
+%0Aimport numpy as np%0Aimport pandas as pd%0A%0Afrom rnaseq_lib.plotting import plot_boxplot
 %0A%0A__
@@ -2055,8 +2055,1238 @@
 _genes%5D%0A
+%0A%0Adef get_tumor_samples(tissue):%0A    %22%22%22%0A    Returns TCGA tumor samples for a tissue%0A    :param str tissue:%0A    :return: List of tumor samples%0A    :rtype: list%0A    %22%22%22%0A    samples = return_samples()%0A    return %5Bx for x in samples%5Btissue%5D if x.endswith('-01')%5D%0A%0A%0Adef get_gtex_samples(tissue):%0A    %22%22%22%0A    Returns GTEx samples for a tissue%0A%0A    :param str tissue:%0A    :return: List of GTEx samples%0A    :rtype: list%0A    %22%22%22%0A    samples = return_samples()%0A    return %5Bx for x in samples%5Btissue%5D if not x.startswith('TCGA')%5D%0A%0A%0Adef get_normal_samples(tissue):%0A    %22%22%22%0A    Returns TCGA normal samples for a tissue%0A    :param str tissue:%0A    :return: List of TCGA normal samples%0A    :rtype: list%0A    %22%22%22%0A    samples = return_samples()%0A    return %5Bx for x in samples%5Btissue%5D if x.endswith('-11')%5D%0A%0A%0Adef plot_gene_boxplot(df, tissue, gene):%0A    %22%22%22%0A    Returns a holoviews box and whisker object of a gene for a tissue%0A%0A    :param pd.DataFrame df:%0A    :param str tissue:%0A    :param str gene:%0A    :return: Boxplot of gene for tissue%0A    :rtype: hv.BoxWhisker%0A    %22%22%22%0A    tumor = get_tumor_samples(tissue)%0A    gtex = get_gtex_samples(tissue)%0A    return plot_boxplot(df, gtex, tumor, 'GTEx', 'Tumor', gene, norm_func=lambda x: np.log2(x + 1))%0A

Add convenience functions