Skip to content

Commit 736cca0

Browse files
committed
Updated Tests to comply with pdb.gz
1 parent ef1f73e commit 736cca0

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

package/MDAnalysis/web/downloaders.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ def __init__(self,
1919

2020
super().__init__(message)
2121

22+
# Dict to convert file name arguments for Universe(topology_format="")
23+
TOPOLOGY_FORMAT_CONVERTER = {
24+
"physical_file_extension": tuple([["pdb.gz", "pdb"]],
25+
) ,
26+
"topology_format": tuple(["PDB",],
27+
)
28+
}
29+
30+
def _file_format_to_topology_string(file_extension):
31+
"""Converts file names to a string usable by Universe(topology_format=)"""
32+
for valid_file_extensions, valid_topology_string in zip(*TOPOLOGY_FORMAT_CONVERTER.values(), strict=True):
33+
if file_extension in valid_file_extensions:
34+
return valid_topology_string
2235

2336
class BaseDownloader(ABC):
2437
"""Abstract Base Class for all Downloaders. Not meant to be directly initalized!"""
@@ -51,14 +64,17 @@ def convert_to_universe(self, **kwargs):
5164
raise RuntimeError("File not set. Run download() to set file before convert_to_universe()")
5265

5366
try:
54-
return Universe(self._file.name, topology_format=self.file_format.upper(), **kwargs)
67+
return Universe(self._file.name,
68+
topology_format=_file_format_to_topology_string(self.file_format),
69+
**kwargs)
5570
finally:
5671
self._file.close()
5772

5873
class PdbDownloader(BaseDownloader):
5974
"""Class to handle download PDBs from the RCSB"""
6075

61-
def __init__(self, PDB_ID, file_format="pdb"):
76+
def __init__(self, PDB_ID, file_format="pdb.gz"):
77+
6278
super().__init__(PDB_ID, file_format)
6379

6480
self._download = False
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from .downloaders import PdbDownloader
22

3-
4-
def fetch_pdb(PDB_ID, download_path=None, timeout=None, **kwargs):
3+
def fetch_pdb(PDB_ID, file_format="pdb.gz", download_path=None, timeout=None, **kwargs):
54
"""Fetchs PDB from RCSB"""
65

7-
downloader = PdbDownloader(PDB_ID).download(download_path,
8-
timeout=timeout)
6+
downloader = PdbDownloader(PDB_ID, file_format).download(download_path, timeout=timeout)
97

108
return downloader.convert_to_universe(**kwargs)

testsuite/MDAnalysisTests/web/test_web.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import requests
66

77
working_PDB_ID = '1DPX' # egg white lysozyme
8-
file_format = "pdb"
8+
file_format = "pdb.gz"
99

1010

1111
@pytest.fixture(scope="function")
@@ -16,12 +16,18 @@ def shared_cache_directory(tmp_path_factory):
1616

1717
class Test_Pdb_Downloader_BaseFunctionality():
1818
"""Test Public API of Pdb_Downloader()"""
19-
20-
def test_base_functionality(self):
21-
"""Test keywords in convert_to_universe()"""
22-
downloader = mda.web.PdbDownloader(PDB_ID=working_PDB_ID).download()
2319

24-
assert(isinstance(downloader.convert_to_universe(in_memory=True), mda.Universe))
20+
valid_file_formats = ("pdb.gz", "pdb")
21+
22+
def test_base_functionality(self):
23+
"""Test file_formats and keywords in convert_to_universe()"""
24+
25+
universe_list = []
26+
for file_format in self.valid_file_formats:
27+
downloader = mda.web.PdbDownloader(PDB_ID=working_PDB_ID, file_format=file_format).download()
28+
universe_list.append(downloader.convert_to_universe(in_memory=True))
29+
30+
assert all(isinstance(universe, mda.Universe) for universe in universe_list)
2531

2632
def test_timeout(self):
2733
"""Checks requests timeout Exception for fetch_pdb and PdbDownloader"""
@@ -32,7 +38,7 @@ def test_invalid_id(self):
3238
"""Test invalid id for PdbDownloader"""
3339

3440
with pytest.raises(mda.web.downloaders.FileDownloadPDBError):
35-
mda.web.PdbDownloader(PDB_ID='BananaBoat').download()
41+
mda.web.PdbDownloader(PDB_ID='BananaBoat').download()
3642

3743
class Test_Pdb_Downloader_Cache():
3844
### Cache Test underneath

0 commit comments

Comments
 (0)