Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning][].
### Added
- `pl.volcano` now accepts a gene name (`str`) or list of gene names (`list[str]`) for the `top` parameter to annotate specific features on volcano plots

### Changes
- Refactored `ds.ensmbl_to_symbol` to reuse `_download` and fixed mirror fallback to actually switch between Ensembl mirrors

## 2.1.4

### Changes
Expand Down
37 changes: 23 additions & 14 deletions src/decoupler/ds/_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import io

import pandas as pd
import requests

from decoupler._download import _download


def ensmbl_to_symbol(
Expand Down Expand Up @@ -29,7 +30,7 @@ def ensmbl_to_symbol(
dc.ds.ensmbl_to_symbol(genes=["ENSG00000196092", "ENSG00000115415"], organism="hsapiens_gene_ensembl")
"""
url = (
'http://www.ensembl.org/biomart/martservice?query=<?xml version="1.0" encoding="UTF-8"?>'
'http://{mirror}.ensembl.org/biomart/martservice?query=<?xml version="1.0" encoding="UTF-8"?>'
'<!DOCTYPE Query><Query virtualSchemaName = "default" formatter = "TSV" header = "0" un'
'iqueRows = "0" count = "" ><Dataset name = "{organism}" '
'interface = "default" ><Attribute name = "ensembl_gene_id" /><Attribute name ='
Expand All @@ -47,16 +48,24 @@ def ensmbl_to_symbol(
assert isinstance(genes, list), "genes must be list"
assert isinstance(organism, str), "organism must be str"
# Try different mirrors
response = requests.get(url.format(miror="www", organism=organism))
if any(msg in response.text for msg in ["Service unavailable", "Gateway Time-out"]):
response = requests.get(url.format(miror="useast", organism=organism))
if any(msg in response.text for msg in ["Service unavailable", "Gateway Time-out"]):
response = requests.get(url.format(miror="asia", organism=organism))
if not any(msg in response.text for msg in ["Service unavailable", "Gateway Time-out"]):
eids = pd.read_csv(io.StringIO(response.text), sep="\t", header=None, index_col=0)[1].to_dict()
elif organism in ["hsapiens_gene_ensembl", "mmusculus_gene_ensembl"]:
error_msgs = ["Service unavailable", "Gateway Time-out"]
for mirror in ["www", "useast", "uswest", "asia"]:
try:
data = _download(url.format(mirror=mirror, organism=organism))
text = data.read().decode()
if any(msg in text for msg in error_msgs) or not text.strip():
continue
df = pd.read_csv(io.StringIO(text), sep="\t", header=None, index_col=0)
if df.empty or 1 not in df.columns:
continue
eids = df[1].to_dict()
return [eids[g] if g in eids else None for g in genes]
except Exception:
continue
# Zenodo fallback for human and mouse
if organism in ["hsapiens_gene_ensembl", "mmusculus_gene_ensembl"]:
url = f"https://zenodo.org/records/15551885/files/{organism}.csv.gz?download=1"
eids = pd.read_csv(url, index_col=0, compression="gzip")["symbol"].to_dict()
else:
assert AssertionError(), "ensembl servers are down, try again later"
return [eids[g] if g in eids else None for g in genes]
data = _download(url)
eids = pd.read_csv(data, index_col=0, compression="gzip")["symbol"].to_dict()
return [eids[g] if g in eids else None for g in genes]
raise ValueError("ensembl servers are down, try again later")
Loading