File size: 3,660 Bytes
6d1bbc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Download BindingDB full dataset (TSV in ZIP archive).

Source: https://www.bindingdb.org/bind/downloads/
File: BindingDB_All_*.tsv (~525 MB zip -> ~2 GB TSV)
"""

import zipfile
from pathlib import Path

import requests
from tqdm import tqdm

from negbiodb.download import (
    check_disk_space,
    load_config,
    verify_file_exists,
)


def download_bindingdb_zip(servlet_url: str, file_url: str, dest: Path) -> None:
    """Download BindingDB ZIP using session-based two-step approach.

    BindingDB requires visiting a servlet page first to prepare the file,
    then downloading from the direct URL with the same session.
    """
    if dest.exists() and dest.stat().st_size > 0:
        print(f"Already exists: {dest}")
        return

    dest.parent.mkdir(parents=True, exist_ok=True)

    session = requests.Session()

    # Step 1: Hit the servlet to prepare the file and get session cookie
    print("Requesting file preparation from BindingDB servlet...")
    prep = session.get(servlet_url, timeout=60)
    prep.raise_for_status()

    # Step 2: Download the actual file with the session
    print("Downloading ZIP file...")
    resp = session.get(file_url, stream=True, timeout=300)
    resp.raise_for_status()

    total = int(resp.headers.get("content-length", 0))
    try:
        with (
            open(dest, "wb") as f,
            tqdm(total=total or None, unit="B", unit_scale=True, desc="BindingDB ZIP") as pbar,
        ):
            for chunk in resp.iter_content(chunk_size=8192):
                f.write(chunk)
                pbar.update(len(chunk))
    except Exception:
        if dest.exists():
            dest.unlink()
        raise

    print(f"Downloaded: {dest} ({dest.stat().st_size / (1024**2):.1f} MB)")


def main():
    cfg = load_config()
    dl = cfg["downloads"]["bindingdb"]
    servlet_url = dl["servlet_url"]
    file_url = dl["file_url"]
    dest_dir = Path(dl["dest_dir"])
    min_bytes = dl["min_size_bytes"]

    dest_dir.mkdir(parents=True, exist_ok=True)
    zip_path = dest_dir / "bindingdb_all.zip"

    print("=== BindingDB Download ===")
    print(f"Servlet: {servlet_url}")
    print(f"File:    {file_url}")
    print(f"Dest:    {dest_dir}")

    check_disk_space(dest_dir, required_gb=3.0)

    try:
        download_bindingdb_zip(servlet_url, file_url, zip_path)
    except (requests.RequestException, OSError) as e:
        print(f"\nERROR: Download failed: {e}")
        print(
            "BindingDB uses a servlet-based URL that may require manual download."
        )
        print(f"Please download manually from: https://www.bindingdb.org/bind/downloads/")
        print(f"Save the ZIP file as: {zip_path}")
        return

    # Extract TSV from ZIP
    print("Extracting TSV from ZIP...")
    tsv_path = None
    with zipfile.ZipFile(zip_path) as zf:
        for name in zf.namelist():
            if name.endswith(".tsv"):
                zf.extract(name, dest_dir)
                extracted = dest_dir / name
                tsv_path = dest_dir / "BindingDB_All.tsv"
                if extracted != tsv_path:
                    extracted.rename(tsv_path)
                break

    if tsv_path is None:
        print("WARNING: No TSV file found in ZIP archive")
        return

    # Remove ZIP to save space
    zip_path.unlink()
    print(f"Removed ZIP: {zip_path}")

    if not verify_file_exists(tsv_path, min_bytes=min_bytes):
        print(f"WARNING: TSV smaller than expected ({min_bytes / 1e6:.0f} MB)")

    print(f"Extracted: {tsv_path} ({tsv_path.stat().st_size / (1024**2):.1f} MB)")
    print("\nBindingDB download complete.")


if __name__ == "__main__":
    main()