Skip to content

Commit 59a3546

Browse files
committed
ensure metalinksdb gets properly downloaded
1 parent 01381ba commit 59a3546

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Added Python 3.13 support in classifiers. #216
1212
- Added Installation instructions in `installation.md`. #217
1313
- Properly check if a passed (cell type) labels in plotting are a string #220
14+
- Fixed an issue where MetalinksDB download would fail due to User-Agent restrictions.
1415

1516
## 1.6.1 (28.09.2025)
1617

docs/notebooks/basic_usage.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@
17381738
],
17391739
"metadata": {
17401740
"kernelspec": {
1741-
"display_name": "liana311",
1741+
"display_name": "liana_squidpy",
17421742
"language": "python",
17431743
"name": "python3"
17441744
},
@@ -1752,7 +1752,7 @@
17521752
"name": "python",
17531753
"nbconvert_exporter": "python",
17541754
"pygments_lexer": "ipython3",
1755-
"version": "3.11.13"
1755+
"version": "3.10.15"
17561756
}
17571757
},
17581758
"nbformat": 4,

src/liana/resource/get_metalinks.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,47 @@ def _download_metalinksdb(verbose=True):
1919
"""
2020
requests = _check_if_installed("requests")
2121

22-
METALINKS_URL = "https://figshare.com/ndownloader/files/47567597"
22+
METALINKS_URL = "https://figshare.com/ndownloader/files/60861292"
2323

2424
# Define the local filename to save the downloaded database
2525
db_file_name = 'metalinksdb.db'
2626
db_path = os.path.join(os.getcwd(), db_file_name)
2727

28-
# Check if the database file already exists
29-
if not os.path.exists(db_path):
30-
_logg("Downloading database...", verbose=verbose)
31-
response = requests.get(METALINKS_URL)
28+
# Check if the database file already exists and is valid
29+
if os.path.exists(db_path):
30+
# Check if file is empty or corrupted
31+
if os.path.getsize(db_path) == 0:
32+
_logg("Existing database file is empty. Removing and re-downloading...", verbose=verbose)
33+
os.remove(db_path)
34+
else:
35+
return db_path
36+
37+
# Download the database
38+
_logg("Downloading database...", verbose=verbose)
39+
try:
40+
# Figshare requires a browser-like User-Agent to bypass WAF
41+
headers = {
42+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
43+
}
44+
response = requests.get(METALINKS_URL, headers=headers, stream=True, allow_redirects=True)
45+
response.raise_for_status() # Raise an error for bad status codes
46+
3247
with open(db_path, 'wb') as f:
33-
f.write(response.content)
34-
_logg(f"Database downloaded and saved to {db_path}.", verbose=verbose)
48+
for chunk in response.iter_content(chunk_size=8192):
49+
f.write(chunk)
50+
51+
# Validate the downloaded file
52+
file_size = os.path.getsize(db_path)
53+
if file_size == 0:
54+
os.remove(db_path)
55+
raise RuntimeError("Downloaded file is empty. Please check the URL and try again.")
56+
57+
_logg(f"Database downloaded and saved to {db_path} ({file_size} bytes).", verbose=verbose)
58+
except (requests.exceptions.RequestException, OSError, RuntimeError) as e:
59+
# Clean up failed download
60+
if os.path.exists(db_path):
61+
os.remove(db_path)
62+
raise RuntimeError(f"Failed to download database: {e}") from e
3563

3664
return db_path
3765

0 commit comments

Comments
 (0)