Datasets:
Upload scripts/download_alphafold_structures.py with huggingface_hub
Browse files
scripts/download_alphafold_structures.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Download AlphaFold structures from AlphaFold Database.
|
| 4 |
+
Part of APED - African Protein Engineering Dataset
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import argparse
|
| 8 |
+
import os
|
| 9 |
+
import time
|
| 10 |
+
import requests
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
ALPHAFOLD_DB_URL = "https://alphafold.ebi.ac.uk/files/AF-{uniprot_id}-F1-model_v4.pdb"
|
| 15 |
+
UNIPROT_API_URL = "https://rest.uniprot.org/uniprotkb/search"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def get_uniprot_ids_by_taxonomy(taxonomy_ids: list, limit: int = 500) -> list:
|
| 19 |
+
"""Fetch UniProt IDs for given taxonomy IDs."""
|
| 20 |
+
all_ids = []
|
| 21 |
+
|
| 22 |
+
for tax_id in taxonomy_ids:
|
| 23 |
+
print(f"Fetching proteins for taxonomy ID: {tax_id}")
|
| 24 |
+
|
| 25 |
+
query = f"(taxonomy_id:{tax_id}) AND (reviewed:true)"
|
| 26 |
+
params = {
|
| 27 |
+
"query": query,
|
| 28 |
+
"format": "list",
|
| 29 |
+
"size": limit
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
response = requests.get(UNIPROT_API_URL, params=params, timeout=30)
|
| 34 |
+
response.raise_for_status()
|
| 35 |
+
ids = response.text.strip().split('\n')
|
| 36 |
+
ids = [i for i in ids if i]
|
| 37 |
+
all_ids.extend(ids)
|
| 38 |
+
print(f" Found {len(ids)} proteins")
|
| 39 |
+
except requests.RequestException as e:
|
| 40 |
+
print(f" Error fetching taxonomy {tax_id}: {e}")
|
| 41 |
+
|
| 42 |
+
time.sleep(0.5) # Rate limiting
|
| 43 |
+
|
| 44 |
+
return list(set(all_ids))
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def download_alphafold_structure(uniprot_id: str, output_dir: Path) -> bool:
|
| 48 |
+
"""Download a single AlphaFold structure."""
|
| 49 |
+
output_file = output_dir / f"AF-{uniprot_id}-F1-model_v4.pdb"
|
| 50 |
+
|
| 51 |
+
if output_file.exists():
|
| 52 |
+
return True
|
| 53 |
+
|
| 54 |
+
url = ALPHAFOLD_DB_URL.format(uniprot_id=uniprot_id)
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
response = requests.get(url, timeout=30)
|
| 58 |
+
response.raise_for_status()
|
| 59 |
+
|
| 60 |
+
with open(output_file, 'w') as f:
|
| 61 |
+
f.write(response.text)
|
| 62 |
+
return True
|
| 63 |
+
|
| 64 |
+
except requests.RequestException:
|
| 65 |
+
return False
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def main():
|
| 69 |
+
parser = argparse.ArgumentParser(
|
| 70 |
+
description="Download AlphaFold structures for thermophilic organisms"
|
| 71 |
+
)
|
| 72 |
+
parser.add_argument(
|
| 73 |
+
"--taxonomy-ids",
|
| 74 |
+
type=str,
|
| 75 |
+
required=True,
|
| 76 |
+
help="Comma-separated taxonomy IDs (e.g., 274,69014,2287)"
|
| 77 |
+
)
|
| 78 |
+
parser.add_argument(
|
| 79 |
+
"--output-dir",
|
| 80 |
+
type=str,
|
| 81 |
+
default="data/structures/alphafold_db/",
|
| 82 |
+
help="Output directory for PDB files"
|
| 83 |
+
)
|
| 84 |
+
parser.add_argument(
|
| 85 |
+
"--limit",
|
| 86 |
+
type=int,
|
| 87 |
+
default=500,
|
| 88 |
+
help="Max proteins per organism"
|
| 89 |
+
)
|
| 90 |
+
parser.add_argument(
|
| 91 |
+
"--uniprot-ids-file",
|
| 92 |
+
type=str,
|
| 93 |
+
help="Optional: File with UniProt IDs (one per line)"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
args = parser.parse_args()
|
| 97 |
+
|
| 98 |
+
output_dir = Path(args.output_dir)
|
| 99 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 100 |
+
|
| 101 |
+
# Get UniProt IDs
|
| 102 |
+
if args.uniprot_ids_file:
|
| 103 |
+
with open(args.uniprot_ids_file) as f:
|
| 104 |
+
uniprot_ids = [line.strip() for line in f if line.strip()]
|
| 105 |
+
else:
|
| 106 |
+
taxonomy_ids = [int(t.strip()) for t in args.taxonomy_ids.split(',')]
|
| 107 |
+
uniprot_ids = get_uniprot_ids_by_taxonomy(taxonomy_ids, args.limit)
|
| 108 |
+
|
| 109 |
+
print(f"\nDownloading {len(uniprot_ids)} structures...")
|
| 110 |
+
|
| 111 |
+
success = 0
|
| 112 |
+
failed = 0
|
| 113 |
+
|
| 114 |
+
for i, uid in enumerate(uniprot_ids):
|
| 115 |
+
if download_alphafold_structure(uid, output_dir):
|
| 116 |
+
success += 1
|
| 117 |
+
else:
|
| 118 |
+
failed += 1
|
| 119 |
+
|
| 120 |
+
if (i + 1) % 50 == 0:
|
| 121 |
+
print(f" Progress: {i + 1}/{len(uniprot_ids)} (success: {success}, failed: {failed})")
|
| 122 |
+
|
| 123 |
+
time.sleep(0.1) # Rate limiting
|
| 124 |
+
|
| 125 |
+
print(f"\nComplete!")
|
| 126 |
+
print(f" Downloaded: {success}")
|
| 127 |
+
print(f" Failed: {failed}")
|
| 128 |
+
print(f" Output: {output_dir}")
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
main()
|