Skip to content

Commit 630ae07

Browse files
committed
stuff not working
1 parent 259afe3 commit 630ae07

2 files changed

Lines changed: 53 additions & 37 deletions

File tree

package/MDAnalysis/fetch/pdb.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
---------
4141
4242
.. autofunction:: from_PDB
43+
.. autofunction:: from_DOI
4344
4445
"""
4546

@@ -180,10 +181,10 @@ def from_PDB(
180181
)
181182

182183

183-
def from_DOI(doi, file_name, cache_path=None, remove_prefix=True):
184+
def from_DOI(doi, file_name, remove_prefix=True, cache_path=None):
184185
"""
185-
Download one or more files given the Digital Object Identifier (DOI).
186-
them locally.
186+
Download one or more files given the Digital Object Identifier (DOI)
187+
and store them locally.
187188
188189
Given one DOI, this function downloads one or multiple file names from
189190
the repository and stores them in a local cache directory. If files
@@ -195,51 +196,43 @@ def from_DOI(doi, file_name, cache_path=None, remove_prefix=True):
195196
Parameters
196197
----------
197198
doi: str
198-
the name of a links o
199-
file_name, : str
200-
The file extension/format to download (e.g., "cif", "pdb").
201-
See the Notes section below for a list of all supported file formats.
199+
The Digital Object Identifier (DOI) of the file(s) to download.
200+
file_name : str or list of str
201+
The name(s) of the file(s) to download from the repository.
202+
remove_prefix : bool
203+
If True, remove the prefix (e.g., "https://doi.org/") from the DOI string.
204+
Default is True.
202205
cache_path : str or pathlib.Path
203206
Directory where downloaded file(s) will be cached.
204207
The default ``None`` argument uses the :mod:`pooch` default cache with
205208
project name :data:`DEFAULT_CACHE_NAME_DOWNLOADER`.
206-
remove_prefix : bool
207-
If True, display a progress bar during file downloads. Default
208-
is False.
209+
209210
210211
Returns
211212
-------
212213
:class:`~pathlib.Path` or list of :class:`~pathlib.Path`
213214
The path(s) to the downloaded file(s). Returns a single
214-
:class:`~pathlib.Path` if a single pdb id is given, or a list of
215-
:class:`~pathlib.Path` if multiple pdb ids are provided.
215+
:class:`~pathlib.Path` if a single DOI is given, or a list of
216+
:class:`~pathlib.Path` if multiple DOIs are provided.
216217
217218
Raises
218219
------
219220
ValueError
220-
For an invalid file format. Supported file formats are under Notes.
221+
For an invalid DOI link. See Notes for more information.
221222
222223
:class:`requests.exceptions.HTTPError`
223-
If an invalid PDB code is specified.
224-
224+
If an invalid file_name is specified.
225+
225226
Notes
226227
-----
227-
This function uses the `RCSB File Download Services`_ for directly
228-
downloading structure files via https.
228+
The DOI link, as specified by `doi`, should be in the format of
229+
"https://doi.org/..." or "doi.org/...".
230+
The function will automatically handle the prefix removal
231+
if `remove_prefix` is set to True.
229232
230-
.. _`RCSB File Download Services`:
231-
https://www.rcsb.org/docs/programmatic-access/file-download-services
232-
233-
The RCSB currently provides data in ``'cif'`` , ``'cif.gz'`` , ``'bcif'`` ,
234-
``'bcif.gz'`` , ``'xml'`` , ``'xml.gz'`` , ``'pdb'`` , ``'pdb.gz'``,
235-
``'pdb1'``, ``'pdb1.gz'`` file formats and can therefore be downloaded.
236-
Not all of these formats can be currently read with MDAnalysis.
237-
238-
Caching, controlled by the `cache_path` parameter, is handled internally by
239-
:mod:`pooch`. The default cache name is taken from
240-
:data:`DEFAULT_CACHE_NAME_DOWNLOADER`. To clear cache (and subsequently
241-
force re-fetching), it is required to delete the cache folder
242-
as specified by `cache_path`.
233+
The current backend for downloading files is :class:`pooch.DOIDownloader`
234+
which only handles downloading from Zenodo and Figshare repositories.
235+
243236
244237
Examples
245238
--------
@@ -267,8 +260,8 @@ def from_DOI(doi, file_name, cache_path=None, remove_prefix=True):
267260
.. versionadded:: 2.11.0
268261
"""
269262

270-
# Suppress Figshare warning of getting most current
271-
# repository if none is provided
263+
# Supress warnings from pooch about not specifying
264+
# which version of the repostory to download.
272265
warnings.filterwarnings("ignore", category=UserWarning)
273266

274267
if remove_prefix:
@@ -284,9 +277,12 @@ def from_DOI(doi, file_name, cache_path=None, remove_prefix=True):
284277
doi = doi.replace("doi.org/", "doi:")
285278

286279
fetcher = StaticFetcher(cache_path=cache_path)
287-
return fetcher.fetch(
288-
base_url=doi, file_name=file_name, append_db=True, downloader="doi"
289-
)
290280

291-
292-
##
281+
try:
282+
return fetcher.fetch(
283+
base_url=doi, file_name=file_name, append_db=True, downloader="doi"
284+
)
285+
except IndexError as e:
286+
raise ValueError(
287+
f"DOI link {doi} is invalid. Please check the DOI link."
288+
)

testsuite/MDAnalysisTests/fetch/test_from_DOI.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
#
2323

2424
import pytest
25+
import re
2526
from MDAnalysis.fetch.pdb import from_DOI
2627
import hashlib
2728

29+
from requests.exceptions import HTTPError
2830
from MDAnalysis.fetch.fetchers import HAS_POOCH
2931
from urllib import request
3032

@@ -105,3 +107,21 @@ def test_different_sources(tmp_path, doi, file_name, expected_md5):
105107
assert p1.exists()
106108
assert isinstance(p1, Path)
107109
assert hashlib.md5(p1.read_bytes()).hexdigest() == expected_md5
110+
111+
@pytest.mark.skipif(not HAS_POOCH, reason="Pooch is not installed.")
112+
def test_invalid_doi_link(tmp_path):
113+
114+
with pytest.raises(
115+
HTTPError,
116+
):
117+
from_DOI("invalid", file_name="missing.dat", cache_path=tmp_path)
118+
119+
120+
def test_invalid_file_name(tmp_path):
121+
with pytest.raises(
122+
ValueError,
123+
match=re.escape(
124+
"DOI link doi:invalid is invalid. Please check the DOI link."
125+
),
126+
):
127+
from_DOI("doi.org/invalid", file_name="missing.dat", cache_path=tmp_path)

0 commit comments

Comments
 (0)