Skip to content

Commit

Permalink
Merge e64e62a into 7802d45
Browse files Browse the repository at this point in the history
  • Loading branch information
ivirshup committed Nov 20, 2018
2 parents 7802d45 + e64e62a commit 932e856
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
28 changes: 26 additions & 2 deletions src/pybiomart/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,21 @@ def display_name(self):
def filters(self):
"""List of filters available for the dataset."""
if self._filters is None:
self._filters, self._attributes = self._fetch_configuration()
self._init_attributes_filters()
return self._filters

@property
def attributes(self):
"""List of attributes available for the dataset (cached)."""
if self._attributes is None:
self._filters, self._attributes = self._fetch_configuration()
self._init_attributes_filters()
return self._attributes

def _init_attributes_filters(self):
_filters = self._fetch_filters()
self._filters, self._attributes = self._fetch_configuration()
self._filters.update(_filters)

@property
def default_attributes(self):
"""List of default attributes for the dataset."""
Expand Down Expand Up @@ -155,6 +160,25 @@ def _fetch_configuration(self):

return filters, attributes

def _fetch_filters(self):
columns = ['name', 'description', 'options', 'fullDescription',
'filters', 'type', 'operation', 'tableConstraint', 'field']
response = self.get(type='filters', dataset=self._name)

# Check response for problems.
if 'Problem retrieving' in response.text:
raise BiomartException(response.text)

with StringIO(response.text) as f:
table = pd.read_table(f, header=None, names=columns)

filters = {}
for record in table.itertuples():
filters[record.name] = Filter(record.name, record.type,
record.description)

return filters

@staticmethod
def _filters_from_xml(xml):
for node in xml.iter('FilterDescription'):
Expand Down
18 changes: 16 additions & 2 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ def test_fetch_configuration(self, mocker, mock_dataset,
assert len(mock_dataset.filters) > 0
assert len(mock_dataset.attributes) > 0

mock_get.assert_called_once_with(
type='configuration', dataset=mock_dataset.name)
mock_get.assert_has_calls([
mocker.call(type="configuration", dataset=mock_dataset.name),
mocker.call(type="filters", dataset=mock_dataset.name)
], any_order=True)

def test_fetch_attribute(self, mocker, mock_dataset,
dataset_config_response):
Expand Down Expand Up @@ -162,3 +164,15 @@ def test_ensembl(self):

assert result.shape[0] > 0
assert result.shape[1] == 2

def test_fetch_filters(self):
"""Fetches up to date filters."""
dataset = Dataset(
name='hsapiens_gene_ensembl',
host='http://www.ensembl.org',
use_cache=False)

filt = dataset.filters['chromosome_name']
assert filt.name == 'chromosome_name'
assert filt.type == 'text'
assert filt.description == 'Chromosome/scaffold name'

0 comments on commit 932e856

Please sign in to comment.