{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### h5ad_ID2symbol" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import anndata as ad\n", "import pandas as pd\n", "import numpy as np\n", "from gseapy import Biomart\n", "adata = ad.read_h5ad(\"kidney_tumors.h5ad\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
vst.meanvst.variancevst.variance.expectedvst.variance.standardizedvst.variablefeature_is_filteredfeature_namefeature_referencefeature_biotype
ENSG000002434850.0002160.0002160.0002160.999738001585500
ENSG000002376130.0000000.0000000.0000000.00000000837000
\n", "
" ], "text/plain": [ " vst.mean vst.variance vst.variance.expected \\\n", "ENSG00000243485 0.000216 0.000216 0.000216 \n", "ENSG00000237613 0.000000 0.000000 0.000000 \n", "\n", " vst.variance.standardized vst.variable feature_is_filtered \\\n", "ENSG00000243485 0.999738 0 0 \n", "ENSG00000237613 0.000000 0 0 \n", "\n", " feature_name feature_reference feature_biotype \n", "ENSG00000243485 15855 0 0 \n", "ENSG00000237613 8370 0 0 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adata.var.head(2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "bm = Biomart()" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "var_names = adata.var_names.to_list()\n", "\n", "df = pd.DataFrame()\n", "\n", "for i in range(1,int(len(var_names)/400) +2):\n", " subl = []\n", " if i*400>=len(var_names):\n", " subl = var_names[(i-1)*400:len(var_names)]\n", " else:\n", " subl = var_names[(i-1)*400:i*400]\n", "\n", " queries ={'ensembl_gene_id': subl } # need to be a dict object\n", " results = bm.query(dataset='hsapiens_gene_ensembl',\n", " attributes=['ensembl_gene_id', 'external_gene_name'],\n", " filters=queries)\n", " if df.shape[0]==0:\n", " df = results\n", " else:\n", " df = pd.concat([df, results], axis=0)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(32926, 2)" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shape" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "df.index = df[\"ensembl_gene_id\"]\n", "df = df.drop_duplicates(keep=\"first\")" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "not_conver_index = df[df[\"external_gene_name\"]!= df[\"external_gene_name\"]].index.to_list()\n", "df.loc[not_conver_index,\"external_gene_name\"] = not_conver_index" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(32844, 2)" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shape" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "adata.var[\"external_gene_name\"] = adata.var.index\n", "have_symbol = adata.var.index[adata.var.index.isin(df.index)]\n", "\n", "adata.var.loc[have_symbol, \"external_gene_name\"] = df.loc[have_symbol, \"external_gene_name\"]" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "adata.var.index = adata.var[\"external_gene_name\"]" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['MIR1302-2HG', 'FAM138A', 'OR4F5', 'ENSG00000238009', 'ENSG00000239945',\n", " 'ENSG00000239906', 'ENSG00000241599', 'DDX11L17', 'WASH9P',\n", " 'ENSG00000228463',\n", " ...\n", " 'ENSG00000277196', 'ENSG00000277630', 'ENSG00000278384',\n", " 'ENSG00000278633', 'ENSG00000276345', 'ENSG00000277856',\n", " 'ENSG00000275063', 'ENSG00000271254', 'ENSG00000277475',\n", " 'ENSG00000268674'],\n", " dtype='object', name='external_gene_name', length=32922)" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adata.var_names" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "adata.var_names_make_unique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "index name can not be dup to the column name" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "adata.var.index.name = \"index_name\"" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "adata.write(\"kidney_tumors_convert_symbol.h5ad\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }