Skip to content

Commit

Permalink
Merge pull request #33 from EBI-Metagenomics/bugfix/bad-responses-fro…
Browse files Browse the repository at this point in the history
…m-ena-browser

Bugfix: handle bad responses from ENA browser API
  • Loading branch information
SandyRogers authored Mar 7, 2024
2 parents 2c706a7 + 9c8bc7b commit 09ab016
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.10.3
current_version = 0.10.4
commit = True
tag = True

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ target/
#IntelliJ project structure files
*.iml
.idea
venv*
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ erp001736 = OriginalMetadata('ERP001736')
erp001736.fetch_metadata()
```


Development setup
=================
Install the package in edit mode, and additional dev requirements (pre-commit hooks and version bumper).
```shell
pip install -e . -r requirements-dev.txt
pre-commit install
```

You can bump the version with e.g. `bump2version patch`.


Contributors
============

Expand Down
2 changes: 1 addition & 1 deletion mg_toolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@

__all__ = ["original_metadata", "sequence_search", "bulk_download"]

__version__ = "0.10.3"
__version__ = "0.10.4"
3 changes: 3 additions & 0 deletions mg_toolkit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@

ENA_SEARCH_API_URL = "https://www.ebi.ac.uk/ena/portal/api/search"
ENA_XML_VIEW_URL = "https://www.ebi.ac.uk/ena/browser/api/xml"

EBI_URL_PREFIX = "https://www.ebi.ac.uk/"
REQUESTS_RETRIES = 3
27 changes: 23 additions & 4 deletions mg_toolkit/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@

import requests
from pandas import DataFrame
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

from .constants import ENA_SEARCH_API_URL, ENA_XML_VIEW_URL
from .constants import (
EBI_URL_PREFIX,
ENA_SEARCH_API_URL,
ENA_XML_VIEW_URL,
REQUESTS_RETRIES,
)

try:
from json.decoder import JSONDecodeError
Expand Down Expand Up @@ -48,18 +56,29 @@ class OriginalMetadata:
"""

accession = None
session = None

def __init__(self, accession, *args, **kwargs):
self.accession = accession

self.session = Session()
retries = Retry(
total=REQUESTS_RETRIES,
backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504],
)
self.session.mount(EBI_URL_PREFIX, HTTPAdapter(max_retries=retries))

def get_metadata(self, sample_accession):
"""Get the sample metadata from ENA API."""
return_meta = {}

response = requests.get(ENA_XML_VIEW_URL + "/" + sample_accession)
response = self.session.get(ENA_XML_VIEW_URL + "/" + sample_accession)

if not response.ok:
logger.error("Metadata fetch failed for accession:" + self.accession)
logger.error(
"Metadata fetch failed for sample accession: " + sample_accession
)
return

metadata_xml = ET.fromstring(response.content)
Expand Down Expand Up @@ -88,7 +107,7 @@ def get_metadata(self, sample_accession):

def fetch_metadata(self):
"""Get metadata from ENA API."""
response = requests.get(
response = self.session.get(
ENA_SEARCH_API_URL,
params={
"result": "read_run",
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# install and create a virtual environment
# run pip install -r requirements

requests>=2.24.0
requests>=2.31.0
pandas==2.0.3
jsonapi-client>=0.9.9
tqdm>=4.49.0

urllib3~=2.2.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
long_description=long_description,
long_description_content_type="text/markdown",
packages=find_packages(exclude=["ez_setup"]),
version="0.10.3",
version="0.10.4",
python_requires=">=3.8",
install_requires=install_requirements,
setup_requires=["pytest-runner"],
Expand Down

0 comments on commit 09ab016

Please sign in to comment.