opencobra / cobrapy

COBRApy is a package for constraint-based modeling of metabolic networks.
http://opencobra.github.io/cobrapy/
GNU General Public License v2.0
455 stars 211 forks source link

[BUG] Biomodels web io access broekn due to redirects #1367

Closed cdiener closed 8 months ago

cdiener commented 8 months ago

Is there an existing issue for this?

Problem description

The tests for the Biomodels repo will fail now since the download API endpoint now triggers a redirect to the Biomodels FTP.

Code sample

Code run:

from cobra.io import load_model

load_model("BIOMD0000000633")

Traceback:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/code/cobrapy/src/cobra/io/web/load.py:127, in _cached_load(model_id, repositories)
    126 try:
--> 127     return cache[model_id]
    128 except KeyError:

File ~/code/envs/cobrapy/lib/python3.11/site-packages/diskcache/core.py:1234, in Cache.__getitem__(self, key)
   1233 if value is ENOVAL:
-> 1234     raise KeyError(key)
   1235 return value

KeyError: 'BIOMD0000000633'

During handling of the above exception, another exception occurred:

HTTPStatusError                           Traceback (most recent call last)
File ~/code/cobrapy/src/cobra/io/web/load.py:160, in _fetch_model(model_id, repositories)
    159 try:
--> 160     return repository.get_sbml(model_id=model_id)
    161 except OSError:

File ~/code/cobrapy/src/cobra/io/web/biomodels_repository.py:93, in BioModels.get_sbml(self, model_id)
     88 with self._progress, httpx.stream(
     89     method="GET",
     90     url=self._url.join(f"download/{model_id}"),
     91     params={"filename": model.name},
     92 ) as response:
---> 93     response.raise_for_status()
     94     task_id = self._progress.add_task(
     95         description="download",
     96         total=model.size,
     97         model_id=model_id,
     98     )

File ~/code/envs/cobrapy/lib/python3.11/site-packages/httpx/_models.py:749, in Response.raise_for_status(self)
    748 message = message.format(self, error_type=error_type)
--> 749 raise HTTPStatusError(message, request=request, response=self)

HTTPStatusError: Redirect response '302 Found' for url 'https://www.ebi.ac.uk/biomodels/model/download/BIOMD0000000633?filename=BIOMD0000000633_url.xml'
Redirect location: 'https://ftp.ebi.ac.uk/pub/databases/biomodels/repository/aaa/MODEL1603030000/3/BIOMD0000000633_url.xml'
For more information check: https://httpstatuses.com/302

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
Cell In[2], line 1
----> 1 load_model("BIOMD0000000633")

File ~/code/cobrapy/src/cobra/io/web/load.py:89, in load_model(model_id, repositories, cache)
     40 """
     41 Download an SBML model from a remote repository.
     42 
   (...)
     86 
     87 """
     88 if cache:
---> 89     data = _cached_load(
     90         model_id=model_id,
     91         repositories=repositories,
     92     )
     93 else:
     94     data = _fetch_model(model_id=model_id, repositories=repositories)

File ~/code/cobrapy/src/cobra/io/web/load.py:129, in _cached_load(model_id, repositories)
    127     return cache[model_id]
    128 except KeyError:
--> 129     data = _fetch_model(model_id=model_id, repositories=repositories)
    130     cache.set(key=model_id, value=data, expire=configuration.cache_expiration)
    131     return data

File ~/code/cobrapy/src/cobra/io/web/load.py:172, in _fetch_model(model_id, repositories)
    168         logger.debug(
    169             f"Model '{model_id}' not found in the {repository.name} repository."
    170         )
    171         continue
--> 172     raise RuntimeError(
    173         f"The connection to the {repository.name} repository failed."
    174     ) from error
    175 except httpx.RequestError as error:
    176     raise RuntimeError(
    177         f"The connection to the {repository.name} repository failed."
    178     ) from error

RuntimeError: The connection to the BioModels repository failed.

Environment

### Package Information | Package | Version | |:--------|--------:| | cobra | 0.28.0 | ### Dependency Information | Package | Version | |:--------------------|------------:| | appdirs | 1.4.4 | | black | 23.1.0 | | bumpversion | **missing** | | depinfo | 2.2.0 | | diskcache | 5.4.0 | | future | 0.18.3 | | httpx | 0.24.1 | | importlib-resources | 5.10.2 | | isort | 5.12.0 | | numpy | 1.23.5 | | optlang | 1.7.0 | | pandas | 1.5.3 | | pydantic | 1.10.4 | | python-libsbml | 5.19.7 | | rich | 13.4.1 | | ruamel.yaml | 0.17.21 | | scipy | 1.10.0 | | swiglpk | 5.0.8 | | tox | **missing** | ### Build Tools Information | Package | Version | |:-----------|--------:| | pip | 23.0 | | setuptools | 66.1.1 | | wheel | 0.38.4 | ### Platform Information | | | |:--------|---------------------:| | Linux | 6.5.0-2-amd64-x86_64 | | CPython | 3.11.6 |

Anything else?

Fix incoming.