Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HESTData: provide util to map ensemble ID to gene name #71

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/hest/HESTData.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,37 @@ def unify_gene_names(adata: sc.AnnData, species="human", drop=False) -> sc.AnnDa

return adata

def ensembleID_to_gene(st: HESTData, filter_na: bool = False) -> HESTData:
"""
Converts ensemble gene IDs of a HESTData object using Biomart annosations

Args:
st (HESTData): HESTData object

Returns:
HESTData: HESTData object with gene names instead of ensemble gene IDs
"""
import scanpy as sc
species = st.meta['species']
org = "hsapiens" if species == "Homo sapiens" else "mmusculus"

annotations = sc.queries.biomart_annotations(org=org,attrs=['ensembl_gene_id', 'external_gene_name'], use_cache=True)
ensembl_to_gene_name = dict(zip(annotations['ensembl_gene_id'], annotations['external_gene_name']))


st.adata.var['gene_name'] = st.adata.var_names.map(ensembl_to_gene_name, na_action=None)

if filter_na:
st.adata.var_names = st.adata.var['gene_name'].fillna('')
else:
st.adata.var['gene_name'] = st.adata.var['gene_name'].where(st.adata.var['gene_name'].notna(), st.adata.var_names)

valid_genes = st.adata.var['gene_name'].notna()
st.adata = st.adata[:, valid_genes]

return st


def save_spatial_plot(adata: sc.AnnData, save_path: str, name: str='', key='total_counts', pl_kwargs={}):
"""Save the spatial plot from that sc.AnnData

Expand Down
5 changes: 3 additions & 2 deletions src/hest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .utils import tiff_save, find_pixel_size_from_spot_coords, write_10X_h5, get_k_genes, SpotPacking
from .autoalign import autoalign_visium
from .readers import *
from .HESTData import HESTData, read_HESTData, load_hest, iter_hest
from .HESTData import HESTData, read_HESTData, load_hest, iter_hest, ensembleID_to_gene
from .segmentation.cell_segmenters import segment_cellvit

__all__ = [
Expand All @@ -20,5 +20,6 @@
'autoalign_visium',
'write_10X_h5',
'HESTData',
'segment_cellvit'
'segment_cellvit',
'ensembleID_to_gene'
]