-
Notifications
You must be signed in to change notification settings - Fork 1
diverse feat, perf and fixes #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
418a23c
f52cf43
d34d460
53a7068
7db210e
6c2bb5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,122 @@ | ||
| import requests, time, pathlib, re | ||
| import pathlib | ||
| import re | ||
| import time | ||
|
|
||
| # === Configuration === | ||
| FASTA_FILE = "tests/head_10.fasta" # your input FASTA filename | ||
| OUT_DIR = pathlib.Path("pdbs") # output folder for PDB files | ||
| import requests | ||
|
|
||
| # =========================== | ||
| # Configuration | ||
| # =========================== | ||
| FASTA_FILE = "tests/head_10.fasta" | ||
| OUT_DIR = pathlib.Path("pdbs") | ||
| OUT_DIR.mkdir(exist_ok=True) | ||
| API_URL = "https://alphafold.ebi.ac.uk/api/prediction/{}" | ||
|
|
||
| ALPHAFOLD_API = "https://alphafold.ebi.ac.uk/api/prediction/{}" | ||
| ESM_API = "https://api.esmatlas.com/fetchPredictedStructure/{}" | ||
|
|
||
|
|
||
| def extract_ids(fasta_path): | ||
| """ | ||
| Extract identifiers from FASTA headers. | ||
|
|
||
| def extract_uniprot_ids(fasta_path): | ||
| """Extract UniProt IDs from FASTA headers (>ID|...).""" | ||
| Expected formats: | ||
| >P12345|... | ||
| >A0A023GPI8|... | ||
| >MGYP002537940442|... | ||
| """ | ||
| ids = [] | ||
| pattern = re.compile(r"^>([A-Z0-9]+)\|") | ||
| with open(fasta_path, "r") as f: | ||
|
|
||
| pattern = re.compile(r"^>([^|\s]+)") | ||
|
|
||
| with open(fasta_path) as f: | ||
| for line in f: | ||
| if line.startswith(">"): | ||
| m = pattern.match(line) | ||
| if m: | ||
| ids.append(m.group(1)) | ||
|
|
||
| return ids | ||
|
|
||
| def download_pdb(uniprot_id): | ||
| """Download only the .pdb file for a given UniProt ID from AlphaFold DB.""" | ||
|
|
||
| def download_from_alphafold(uniprot_id): | ||
| """Download a PDB from AlphaFoldDB.""" | ||
|
|
||
| pdb_path = OUT_DIR / f"{uniprot_id}.pdb" | ||
|
|
||
| if pdb_path.exists(): | ||
| print(f"[SKIP] {pdb_path.name} already exists") | ||
| return | ||
|
|
||
| r = requests.get(ALPHAFOLD_API.format(uniprot_id), timeout=15) | ||
| r.raise_for_status() | ||
|
|
||
| data = r.json() | ||
|
|
||
| if not data or "pdbUrl" not in data[0]: | ||
| print(f"[WARN] {uniprot_id}: no AlphaFold model found") | ||
| return | ||
|
|
||
| pdb_url = data[0]["pdbUrl"] | ||
|
|
||
| with requests.get(pdb_url, stream=True, timeout=60) as s: | ||
| s.raise_for_status() | ||
|
|
||
| with open(pdb_path, "wb") as fh: | ||
| for chunk in s.iter_content(8192): | ||
| fh.write(chunk) | ||
|
|
||
| print(f"[AFDB] Saved {pdb_path.name}") | ||
|
|
||
|
|
||
| def download_from_esm(mgyp_id): | ||
| """Download a PDB directly from the ESM Metagenomic Atlas.""" | ||
|
|
||
| pdb_path = OUT_DIR / f"{mgyp_id}.pdb" | ||
|
|
||
| if pdb_path.exists(): | ||
| print(f"[SKIP] {pdb_path.name} already exists") | ||
| return | ||
|
|
||
| url = ESM_API.format(mgyp_id) | ||
|
|
||
| r = requests.get(url, timeout=60) | ||
|
|
||
| if r.status_code == 404: | ||
| print(f"[WARN] {mgyp_id}: no ESM model found") | ||
| return | ||
|
|
||
| r.raise_for_status() | ||
|
|
||
| with open(pdb_path, "w") as fh: | ||
| fh.write(r.text) | ||
|
|
||
| print(f"[ESM ] Saved {pdb_path.name}") | ||
|
|
||
|
|
||
| def download_pdb(identifier): | ||
| """ | ||
| Automatically choose the correct database. | ||
|
|
||
| MGYP* -> ESM Metagenomic Atlas | ||
| others -> AlphaFoldDB | ||
| """ | ||
|
|
||
| try: | ||
| r = requests.get(API_URL.format(uniprot_id), timeout=15) | ||
| r.raise_for_status() | ||
| data = r.json() | ||
| if not data or "pdbUrl" not in data[0]: | ||
| print(f"[WARN] {uniprot_id}: no PDB found") | ||
| return | ||
| pdb_url = data[0]["pdbUrl"] | ||
| pdb_path = OUT_DIR / f"{uniprot_id}.pdb" | ||
| if pdb_path.exists(): | ||
| print(f"[SKIP] {pdb_path.name} already exists") | ||
| return | ||
| with requests.get(pdb_url, stream=True, timeout=60) as s: | ||
| s.raise_for_status() | ||
| with open(pdb_path, "wb") as fh: | ||
| for chunk in s.iter_content(8192): | ||
| fh.write(chunk) | ||
| print(f"[OK] Saved {pdb_path.name}") | ||
| if identifier.upper().startswith("MGYP"): | ||
| download_from_esm(identifier) | ||
| else: | ||
| download_from_alphafold(identifier) | ||
|
|
||
| except Exception as e: | ||
| print(f"[ERR] {uniprot_id}: {e}") | ||
| print(f"[ERR] {identifier}: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| ids = extract_uniprot_ids(FASTA_FILE) | ||
| print(f"Found {len(ids)} UniProt IDs.") | ||
| for uid in ids: | ||
| download_pdb(uid) | ||
|
|
||
| ids = extract_ids(FASTA_FILE) | ||
|
|
||
| print(f"Found {len(ids)} identifiers.") | ||
|
|
||
| for identifier in ids: | ||
| download_pdb(identifier) | ||
| time.sleep(0.2) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,8 +9,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from PredictionServer.data import BatchConverter, FastaBatchedDataset # part of local netsolp install | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from PredictionServer.predict import get_preds_split, sigmoid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from selectzyme.backend.utils import run_time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_preds(df, args): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @run_time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_preds(df, args, pred_frequency=2): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| alphabet_path = os.path.join(args.MODELS_PATH, "ESM1b_alphabet.pkl") # args.MODELS_PATH | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Using This change adds useful flexibility, but
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(alphabet_path, "rb") as f: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -24,19 +27,19 @@ def get_preds(df, args): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "S" in args.PREDICTION_TYPE: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preds_per_split = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in range(5): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in range(pred_frequency): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pred_df = get_preds_split(i, embed_dataloader, args, "Solubility", df) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preds_i = sigmoid(np.stack(pred_df.preds.to_numpy())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preds_per_split.append(preds_i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| avg_pred = sum(preds_per_split) / 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| avg_pred = sum(preds_per_split) / pred_frequency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| df["netSolP_solubility"] = pd.Series(avg_pred) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "U" in args.PREDICTION_TYPE: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preds_per_split = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in range(5): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in range(pred_frequency): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pred_df = get_preds_split(i, embed_dataloader, args, "Usability", df) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preds_i = sigmoid(np.stack(pred_df.preds.to_numpy())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preds_per_split.append(preds_i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| avg_pred = sum(preds_per_split) / 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| avg_pred = sum(preds_per_split) / pred_frequency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| df["netSolP_usability"] = pd.Series(avg_pred) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| df.rename(columns={"sid": "accession", "fasta": "sequence"}, inplace=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): The
deviceargument here appears undefined in this scope, which will raise at runtime.Within
clean_max_sep_predictions,deviceis neither defined nor passed, yet it’s used for.to(device)and forwarded toget_dist_map_test, which will cause aNameErrorat runtime. Please either adddeviceas a parameter toclean_max_sep_predictionsor infer it from existing inputs (e.g.model_emb_inference.device), and confirm the type matches whatget_dist_map_testexpects (string vstorch.device).