Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/biotite/database/afdb/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def fetch(ids, format, target_path=None, overwrite=False, verbose=False):
target_path.mkdir(parents=True, exist_ok=True)

files = []
session = requests.Session()
for i, id in enumerate(ids):
# Verbose output
if verbose:
Expand All @@ -98,7 +99,7 @@ def fetch(ids, format, target_path=None, overwrite=False, verbose=False):
# 'file = None' -> store content in a file-like object
file = None
if file is None or not file.is_file() or file.stat().st_size == 0 or overwrite:
file_response = requests.get(_get_file_url(id, format))
file_response = session.get(_get_file_url(session, id, format))
_assert_valid_file(file_response, id)
if format in _BINARY_FORMATS:
content = file_response.content
Expand Down Expand Up @@ -128,12 +129,14 @@ def fetch(ids, format, target_path=None, overwrite=False, verbose=False):
return files


def _get_file_url(id, format):
def _get_file_url(session, id, format):
"""
Get the actual file URL for the given ID from the ``prediction`` API endpoint.

Parameters
----------
session : requests.Session
The session to use for the request.
id : str
The ID of the file to be downloaded.
format : str
Expand All @@ -146,7 +149,7 @@ def _get_file_url(id, format):
"""
uniprot_id = _extract_id(id)
try:
metadata = requests.get(f"{_METADATA_URL}/{uniprot_id}").json()
metadata = session.get(f"{_METADATA_URL}/{uniprot_id}").json()
except requests.exceptions.JSONDecodeError:
raise RequestError("Received malformed JSON response")
if len(metadata) == 0:
Expand Down
3 changes: 2 additions & 1 deletion src/biotite/database/entrez/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def fetch(
if target_path is not None and not isdir(target_path):
os.makedirs(target_path)
files = []
session = requests.Session()
for i, id in enumerate(uids):
# Verbose output
if verbose:
Expand All @@ -127,7 +128,7 @@ def fetch(
api_key = get_api_key()
if api_key is not None:
param_dict["api_key"] = api_key
r = requests.get(_fetch_url, params=param_dict)
r = session.get(_fetch_url, params=param_dict)
check_for_errors(r)
content = r.text
if file is None:
Expand Down
3 changes: 2 additions & 1 deletion src/biotite/database/pubchem/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def fetch(
os.makedirs(target_path)

files = []
session = requests.Session()
for i, cid in enumerate(cids):
# Prevent IDs as strings, this could be a common error, as other
# database interfaces of Biotite use string IDs
Expand All @@ -125,7 +126,7 @@ def fetch(

if file is None or not isfile(file) or getsize(file) == 0 or overwrite:
record_type = "2d" if as_structural_formula else "3d"
r = requests.get(
r = session.get(
_base_url + f"compound/cid/{cid}/{format.upper()}",
params={"record_type": record_type},
)
Expand Down
9 changes: 5 additions & 4 deletions src/biotite/database/rcsb/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def fetch(
gz_suffix = ""

files = []
session = requests.Session()
for i, id in enumerate(pdb_ids):
# Verbose output
if verbose:
Expand All @@ -116,25 +117,25 @@ def fetch(

if file is None or not isfile(file) or getsize(file) == 0 or overwrite:
if format == "pdb":
r = requests.get(_standard_url + id + ".pdb" + gz_suffix)
r = session.get(_standard_url + id + ".pdb" + gz_suffix)
_assert_valid_file(r, id)
if gzip:
content = r.content
else:
content = r.text
elif format in ["cif", "mmcif", "pdbx"]:
r = requests.get(_standard_url + id + ".cif" + gz_suffix)
r = session.get(_standard_url + id + ".cif" + gz_suffix)
_assert_valid_file(r, id)
if gzip:
content = r.content
else:
content = r.text
elif format in ["bcif"]:
r = requests.get(_bcif_url + id + ".bcif" + gz_suffix)
r = session.get(_bcif_url + id + ".bcif" + gz_suffix)
_assert_valid_file(r, id)
content = r.content
elif format == "fasta":
r = requests.get(_fasta_url + id)
r = session.get(_fasta_url + id)
_assert_valid_file(r, id)
content = r.text
else:
Expand Down
3 changes: 2 additions & 1 deletion src/biotite/database/uniprot/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def fetch(ids, format, target_path=None, overwrite=False, verbose=False):
if target_path is not None and not isdir(target_path):
os.makedirs(target_path)
files = []
session = requests.Session()
for i, id in enumerate(ids):
db_name = _get_database_name(id)
# Verbose output
Expand All @@ -106,7 +107,7 @@ def fetch(ids, format, target_path=None, overwrite=False, verbose=False):
file = None
if file is None or not isfile(file) or getsize(file) == 0 or overwrite:
if format in ["fasta", "gff", "txt", "xml", "rdf", "tab"]:
r = requests.get(_fetch_url + db_name + "/" + id + "." + format)
r = session.get(_fetch_url + db_name + "/" + id + "." + format)
content = r.text
assert_valid_response(r)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/database/test_afdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_fetch_invalid(monkeypatch, format, invalid_id, bypass_metadata):
monkeypatch.setattr(
module,
"_get_file_url",
lambda id, f: f"https://alphafold.ebi.ac.uk/files/AF-{id}-F1-model_v4.{f}",
lambda session, id, format: f"{AFDB_URL}files/AF-{id}-F1-model_v4.{format}",
)
with pytest.raises((RequestError, ValueError)):
afdb.fetch(invalid_id, format)
Loading