def load_gene_expression_profile(gene_list_file_name, gene_expression_file_name, gene_filter_file_name=None, gene_list_path=None, gene_expression_path=None, gene_filter_path=None ,by_gene=False, list_mode = "FROM_DISK"): stopwatch = Stopwatch() stopwatch.start() if list_mode == "ON_THE_FLY": gene_list = gene_list_file_name else: gene_list = load_gene_list(gene_list_file_name=gene_list_file_name, gene_list_path=gene_list_path) gene_list = [l.split(".")[0] for i, l in enumerate(gene_list)] print stopwatch.stop("done loading gene list") # random.shuffle(gene_list) # gene_list = gene_list[:400] if gene_filter_file_name: stopwatch.start() filter_gene_list = load_gene_list(gene_list_file_name=gene_filter_file_name, gene_list_path=gene_filter_path) gene_list = [cur for cur in gene_list if cur in filter_gene_list] print stopwatch.stop("done filter gene list") if gene_expression_path == None: gene_expression_path = os.path.join(constants.TCGA_DATA_DIR, gene_expression_file_name) stopwatch.start() f = open(gene_expression_path,'r') expression_profiles_filtered = [l.strip().split() for i, l in enumerate(f) if i==0 or l[:l.strip().find('\t')].split(".")[0] in gene_list] # or l.strip()[0:l.strip().find('\t')] in gene_list or l.strip()[0:l.strip().find('\t')].split(".")[0] in gene_list f.close() print stopwatch.stop("done filter gene expression") if not by_gene: stopwatch.start() expression_profiles_filtered = np.flip(np.rot90(expression_profiles_filtered, k=1, axes=(1,0)),1) print stopwatch.stop("done rotate gene expression") return expression_profiles_filtered
async def _eval(ctx, *, code: str): if not dev_check(ctx.author.id): return await ctx.send( f"Sorry, but you can't run this command because you ain't a developer! {bot.get_emoji(555121740465045516)}" ) env = { "bot": bot, "ctx": ctx, "channel": ctx.channel, "author": ctx.author, "guild": ctx.guild, "message": ctx.message, "msg": ctx.message, "_": bot._last_result, "source": inspect.getsource, "src": inspect.getsource, "session": bot.session, "docs": lambda x: print(x.__doc__) } env.update(globals()) body = cleanup_code(code) stdout = io.StringIO() err = out = None to_compile = f"async def func():\n{textwrap.indent(body, ' ')}" stopwatch = Stopwatch().start() try: exec(to_compile, env) except Exception as e: stopwatch.stop() err = await ctx.send( f"**Error**```py\n{e.__class__.__name__}: {e}\n```\n**Type**```ts\n{Type(e)}```\n⏱ {stopwatch}" ) return await ctx.message.add_reaction(bot.get_emoji(522530579627900938) ) func = env["func"] stopwatch.restart() try: with redirect_stdout(stdout): ret = await func() stopwatch.stop() except Exception as e: stopwatch.stop() value = stdout.getvalue() err = await ctx.send( f"**Error**```py\n{value}{traceback.format_exc()}\n```\n**Type**```ts\n{Type(err)}```\n⏱ {stopwatch}" ) else: value = stdout.getvalue() if ret is None: if value: try: out = await ctx.send( f"**Output**```py\n{value}```\n⏱ {stopwatch}") except: paginated_text = paginate(value) for page in paginated_text: if page == paginated_text[-1]: out = await ctx.send(f"```py\n{page}\n```", edit=False) break await ctx.send(f"```py\n{page}\n```", edit=False) await ctx.send(f"⏱ {stopwatch}", edit=False) else: bot._last_result = ret try: out = await ctx.send( f"**Output**```py\n{value}{ret}```\n**Type**```ts\n{Type(ret)}```\n⏱ {stopwatch}" ) except: paginated_text = paginate(f"{value}{ret}") for page in paginated_text: if page == paginated_text[-1]: out = await ctx.send(f"```py\n{page}```", edit=False) break await ctx.send(f"```py\n{page}```", edit=False) await ctx.send(f"**Type**```ts\n{Type(ret)}```\n⏱ {stopwatch}", edit=False) if out: await ctx.message.add_reaction(bot.get_emoji(522530578860605442)) elif err: await ctx.message.add_reaction(bot.get_emoji(522530579627900938)) else: await ctx.message.add_reaction("\u2705")
def check_group_enrichment_goatools(tested_gene_file_name, total_gene_file_name, th=1): if len(tested_gene_file_name) == 0 or len(total_gene_file_name) == 0: return [] if type(total_gene_file_name) == str: total_gene_list = load_gene_list(total_gene_file_name) else: total_gene_list = total_gene_file_name if type(tested_gene_file_name) == str: tested_gene_list = load_gene_list(tested_gene_file_name) else: tested_gene_list = tested_gene_file_name if not os.path.exists( os.path.join(constants.GO_DIR, constants.GO_FILE_NAME)): download(constants.GO_OBO_URL, constants.GO_DIR) obo_dag = GODag(os.path.join(constants.GO_DIR, constants.GO_FILE_NAME)) if not os.path.exists( os.path.join(constants.GO_DIR, constants.GO_ASSOCIATION_FILE_NAME)): download(constants.GO_ASSOCIATION_GENE2GEO_URL, constants.GO_DIR) with gzip.open( os.path.join( constants.GO_DIR, os.path.basename(constants.GO_ASSOCIATION_GENE2GEO_URL)), 'rb') as f_in: with open( os.path.join(constants.GO_DIR, constants.GO_ASSOCIATION_FILE_NAME), 'wb') as f_out: shutil.copyfileobj(f_in, f_out) assoc = read_ncbi_gene2go(os.path.join(constants.GO_DIR, constants.GO_ASSOCIATION_FILE_NAME), no_top=True) sw = Stopwatch() sw.start() g = GOEnrichmentStudy( [int(cur) for cur in ensembl2entrez_convertor(total_gene_list)], assoc, obo_dag, methods=[], log=None) # "bonferroni", "fdr_bh" g_res = g.run_study( [int(cur) for cur in ensembl2entrez_convertor(tested_gene_list)]) print sw.stop("done GO analysis in ") # GO_results = [(cur.NS, cur.GO, cur.goterm.name, cur.pop_count, cur.p_uncorrected, cur.p_fdr_bh) for cur in g_res if # cur.p_fdr_bh <= 0.05] GO_results = [(cur.NS, cur.GO, cur.goterm.name, cur.pop_count, cur.p_uncorrected) for cur in g_res if cur.p_uncorrected <= th] hg_report = [{ HG_GO_ROOT: cur[0], HG_GO_ID: cur[1], HG_GO_NAME: cur[2], HG_VALUE: cur[3], HG_PVAL: cur[4], HG_QVAL: 1 } for cur in GO_results] # , HG_QVAL : cur[5] # hg_report.sort(key=lambda x: x[HG_QVAL]) hg_report.sort(key=lambda x: x[HG_PVAL]) if len(GO_results) > 0: go_ns, go_terms, go_names, go_hg_value, uncorrectd_pvals = zip( *GO_results) # , FDRs else: go_terms = [] uncorrectd_pvals = [] FDRs = [] go_names = [] go_ns = [] # output_rows = [("\r\n".join(e2g_convertor(tested_gene_list)), "\r\n".join(go_ns), # "\r\n".join(go_terms), "\r\n".join(go_names), "\r\n".join(map(str, uncorrectd_pvals)), # "\r\n".join(map(str, FDRs)))] # print_to_excel(output_rows, str(tested_gene_file_name)[:10], str(total_gene_file_name)[:10]) return hg_report