Skip to content

Commit 61e927a

Browse files
committed
Share a single GemmaApi instance
1 parent 31a3b7b commit 61e927a

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

rnaseq_pipeline/gemma.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,15 @@ def platforms(self, experiment_id):
7171
def quantitation_types(self, experiment_id):
7272
return self._query_api(join('datasets', experiment_id, 'quantitationTypes'))
7373

74+
gemma_api = GemmaApi()
75+
7476
class GemmaTaskMixin(luigi.Task):
7577
experiment_id = luigi.Parameter()
7678

77-
def __init__(self, *kwargs, **kwds):
78-
super().__init__(*kwargs, **kwds)
79-
self._gemma_api = GemmaApi()
80-
8179
@property
8280
def dataset_info(self):
8381
if not hasattr(self, '_dataset_info'):
84-
data = self._gemma_api.datasets(self.experiment_id)
82+
data = gemma_api.datasets(self.experiment_id)
8583
if not data:
8684
raise RuntimeError('Could not retrieve Gemma dataset with short name {}.'.format(self.experiment_id))
8785
self._dataset_info = data[0]
@@ -112,7 +110,7 @@ def reference_id(self):
112110
try:
113111
if self.assay_type == GemmaAssayType.BULK_RNA_SEQ:
114112
return {'human': cfg.human_reference_id, 'mouse': cfg.mouse_reference_id, 'rat': cfg.rat_reference_id}[
115-
self.taxon]
113+
self.taxon]
116114
elif self.assay_type == GemmaAssayType.SINGLE_CELL_RNA_SEQ:
117115
return {'human': cfg.human_single_cell_reference_id, 'mouse': cfg.mouse_single_cell_reference_id,
118116
'rat': cfg.rat_single_cell_reference_id}[
@@ -150,7 +148,7 @@ def assay_type(self):
150148
'http://www.ebi.ac.uk/efo/EFO_0005684']
151149
fac_sorted_uri = 'http://www.ebi.ac.uk/efo/EFO_0009108'
152150

153-
annotations = self._gemma_api.dataset_annotations(self.experiment_id)
151+
annotations = gemma_api.dataset_annotations(self.experiment_id)
154152
fac_sorted = any(annotation['classUri'] == assay_type_class_uri and annotation['termUri'] == fac_sorted_uri
155153
for annotation in annotations)
156154
for annotation in annotations:

rnaseq_pipeline/sources/gemma.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import logging
22

33
import luigi
4-
54
from bioluigi.tasks.utils import DynamicTaskWithOutputMixin, DynamicWrapperTask
5+
66
from .geo import DownloadGeoSample
77
from .sra import DownloadSraExperiment
88
from ..config import Config
9-
from ..gemma import GemmaApi
9+
from ..gemma import gemma_api
1010

1111
logger = logging.getLogger(__name__)
1212

@@ -21,12 +21,8 @@ class DownloadGemmaExperiment(DynamicTaskWithOutputMixin, DynamicWrapperTask):
2121
"""
2222
experiment_id: str = luigi.Parameter()
2323

24-
def __init__(self, *kwargs, **kwds):
25-
super().__init__(*kwargs, **kwds)
26-
self._gemma_api = GemmaApi()
27-
2824
def run(self):
29-
data = self._gemma_api.samples(self.experiment_id)
25+
data = gemma_api.samples(self.experiment_id)
3026
download_sample_tasks = []
3127
for sample in data:
3228
accession = sample['accession']['accession']
@@ -41,4 +37,4 @@ def run(self):
4137
else:
4238
logger.warning('Downloading %s from %s is not supported.', accession, external_database)
4339
continue
44-
yield download_sample_tasks
40+
yield download_sample_tasks

rnaseq_pipeline/targets.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import luigi
1010

11-
from .gemma import GemmaApi
11+
from .gemma import gemma_api
1212

1313
logger = logging.getLogger(__name__)
1414

@@ -52,14 +52,13 @@ def __init__(self,
5252
self.dataset = dataset
5353
self.quantitation_type = quantitation_type
5454
self.vector_type = vector_type
55-
self._gemma_api = GemmaApi()
5655

5756
def exists(self):
5857
return any(
5958
(quantitation_type['id'] == self.quantitation_type or quantitation_type['name'] == self.quantitation_type
6059
if self.quantitation_type else quantitation_type['isPreferred'])
6160
and (quantitation_type['vectorType'] == self.vector_type.value if self.vector_type else True)
62-
for quantitation_type in self._gemma_api.quantitation_types(self.dataset))
61+
for quantitation_type in gemma_api.quantitation_types(self.dataset))
6362

6463
def __repr__(self):
6564
return f'GemmaDatasetQuantitationType(dataset={self.dataset}, quantitation_type={self.quantitation_type}, vector_type={self.vector_type})'
@@ -71,10 +70,9 @@ class GemmaDatasetHasBatch(luigi.Target):
7170

7271
def __init__(self, dataset_short_name):
7372
self.dataset_short_name = dataset_short_name
74-
self._gemma_api = GemmaApi()
7573

7674
def exists(self):
77-
return self._gemma_api.dataset_has_batch(self.dataset_short_name)
75+
return gemma_api.dataset_has_batch(self.dataset_short_name)
7876

7977
class ExpirableLocalTarget(luigi.LocalTarget):
8078
"""

rnaseq_pipeline/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from luigi.util import requires
2424

2525
from rnaseq_pipeline.config import Config
26-
from .gemma import GemmaAssayType, GemmaTaskMixin, GemmaConfig
26+
from .gemma import GemmaAssayType, GemmaTaskMixin, GemmaConfig, gemma_api
2727
from .gemma import GemmaCliTask
2828
from .sources.arrayexpress import DownloadArrayExpressSample, DownloadArrayExpressExperiment
2929
from .sources.gemma import DownloadGemmaExperiment
@@ -819,7 +819,7 @@ def run(self):
819819
for split_id in range(self.num_splits):
820820
split_id = self.experiment_id + '.' + str(split_id + 1)
821821
logger.info('Reorganizing data for %s.', split_id)
822-
for sample in self._gemma_api.samples(split_id):
822+
for sample in gemma_api.samples(split_id):
823823
sample_id = sample['accession']['accession']
824824
for d in dirs_to_relocate:
825825
for sample_file in iglob(join(d, self.experiment_id, sample_id)):

0 commit comments

Comments
 (0)