Skip to content

Commit

Permalink
Merge pull request #576 from FZJ-INM1-BDA/fix_allen_query_w_genelist
Browse files Browse the repository at this point in the history
fix: allen gene exp query w a list of genes
  • Loading branch information
AhmetNSimsek authored Mar 6, 2024
2 parents 1aaa979 + e594a7e commit cd6466e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
9 changes: 9 additions & 0 deletions e2e/features/molecular/test_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ def test_gene_exp():

# Using higher threshold should result in less gene expresssion measures
assert len(features_grandparent_struct[0].data) > len(features_leaf_struct[0].data)


def test_query_w_genelist():
genes = ["GABRA1", "GABRA2", "GABRQ"]
p = siibra.parcellations['julich 3']
regions = [p.get_region(spec) for spec in ["ifg 44 left", "hoc1 left"]]
for region in regions:
fts = siibra.features.get(region, "GeneExpressions", gene=genes)
assert len(fts) > 0
56 changes: 35 additions & 21 deletions siibra/livequeries/allen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ..retrieval import HttpRequest
from ..vocabularies import GENE_NAMES

from typing import Iterable, List
from typing import Iterable
from xml.etree import ElementTree
import numpy as np
import json
Expand Down Expand Up @@ -120,7 +120,7 @@ def parse_gene(spec):

self.genes = parse_gene(gene)

def query(self, concept: structure.BrainStructure) -> List[GeneExpressions]:
def query(self, concept: structure.BrainStructure) -> Iterable[GeneExpressions]:

mnispace = _space.Space.registry().get('mni152')

Expand Down Expand Up @@ -182,16 +182,37 @@ def __iter__(self):
print(GeneExpressions.ALLEN_ATLAS_NOTIFICATION)
self.__class__._notification_shown = True

assert isinstance(self.genes, list)
if len(self.genes) == 1:
logger.debug(f"Retrieving probe ids for gene {self.genes[0]['symbol']}")
probe_ids = self._retrieve_probe_ids(self.genes)

# get specimen information
if AllenBrainAtlasQuery._specimen is None:
AllenBrainAtlasQuery._specimen = {
spcid: AllenBrainAtlasQuery._retrieve_specimen(spcid) for spcid in self._SPECIMEN_IDS
}

if AllenBrainAtlasQuery.factors is None:
self._retrieve_factors()

# get expression levels and z_scores for the gene
if len(probe_ids) > 0:
for donor_id in self._DONOR_IDS:
for item in self._retrieve_microarray(donor_id, probe_ids):
yield item

@staticmethod
def _retrieve_probe_ids(genes: list):
assert isinstance(genes, list)
if len(genes) == 1:
logger.debug(f"Retrieving probe ids for gene {genes[0]['symbol']}")
else:
logger.debug(f"Retrieving probe ids for genes {', '.join(g['symbol'] for g in self.genes)}")
logger.debug(f"Retrieving probe ids for genes {', '.join(g['symbol'] for g in genes)}")
start_row = 0
num_rows = 50
probe_ids = []
while True:
url = self._QUERY["multiple_gene_probe"].format(start_row=start_row, num_rows=num_rows, genes=','.join([f"'{g['symbol']}'" for g in self.genes]))
url = AllenBrainAtlasQuery._QUERY["multiple_gene_probe"].format(
start_row=start_row, num_rows=num_rows, genes=','.join([f"'{g['symbol']}'" for g in genes])
)
response = HttpRequest(url).get()
if "site unavailable" in response.decode().lower():
# When the Allen site is not available, they still send a status code 200.
Expand All @@ -207,17 +228,16 @@ def __iter__(self):
break
# retrieve another page
start_row += num_rows
return probe_ids

# get specimen information
if AllenBrainAtlasQuery._specimen is None:
AllenBrainAtlasQuery._specimen = {
spcid: AllenBrainAtlasQuery._retrieve_specimen(spcid) for spcid in self._SPECIMEN_IDS
}

if AllenBrainAtlasQuery.factors is None:
@staticmethod
def _retrieve_factors():
start_row = 0
num_rows = 50
if AllenBrainAtlasQuery.factors is None or len(AllenBrainAtlasQuery.factors) == 0:
AllenBrainAtlasQuery.factors = {}
while True:
factors_url = self._QUERY["factors"].format(start_row=start_row, num_rows=num_rows)
factors_url = AllenBrainAtlasQuery._QUERY["factors"].format(start_row=start_row, num_rows=num_rows)
response = HttpRequest(factors_url).get()
AllenBrainAtlasQuery.factors.update({
item["id"]: {
Expand All @@ -233,12 +253,6 @@ def __iter__(self):
# retrieve another page
start_row += num_rows

# get expression levels and z_scores for the gene
if len(probe_ids) > 0:
for donor_id in self._DONOR_IDS:
for item in self._retrieve_microarray(donor_id, probe_ids):
yield item

@staticmethod
def _retrieve_specimen(specimen_id: str):
"""
Expand Down

0 comments on commit cd6466e

Please sign in to comment.