Download libs/docking/comparison.py from QPromaQ/Docking_project: direct link, hf CLI and curl.
- Browser
- Download file 4.88 kB
-
https://huggingface.co/datasets/QPromaQ/Docking_project/resolve/main/libs/docking/comparison.py
- Command line
-
hf download hf://datasets/QPromaQ/Docking_project/libs/docking/comparison.py
-
curl -L -o comparison.py https://huggingface.co/datasets/QPromaQ/Docking_project/resolve/main/libs/docking/comparison.py
4.88 kB
| from __future__ import annotations | |
| from typing import Dict | |
| import numpy as np | |
| import pandas as pd | |
| def backend_score_semantics(backend_name: str) -> str: | |
| name = str(backend_name).strip().lower() | |
| if name == "rdock": | |
| return "rdock_docking_score_lower_better" | |
| if name == "haddock": | |
| return "haddock_emscoring_energy_proxy_lower_better" | |
| return "unknown_score_semantics" | |
| def build_backend_comparison_table(df: pd.DataFrame) -> pd.DataFrame: | |
| """Build backend-comparison rows with explicit semantics and normalized scores.""" | |
| if df.empty: | |
| return pd.DataFrame( | |
| columns=[ | |
| "ligand_id", | |
| "backend_name", | |
| "backend_mode", | |
| "raw_score", | |
| "normalized_score", | |
| "zscore_within_backend", | |
| "score_semantics", | |
| "direct_comparison_justified", | |
| "provenance_path", | |
| "parsed_from", | |
| ] | |
| ) | |
| work = df.copy() | |
| if "docking_score" in work.columns and "raw_score" not in work.columns: | |
| work["raw_score"] = pd.to_numeric(work["docking_score"], errors="coerce") | |
| work["backend_name"] = work["backend_name"].astype(str) | |
| work["backend_mode"] = work.get("backend_mode", "").astype(str) | |
| work["score_semantics"] = work["backend_name"].map(backend_score_semantics) | |
| work["provenance_path"] = work.get("raw_output_file", "").astype(str) | |
| work["parsed_from"] = work.get("parsed_from", "").astype(str) | |
| norm = np.full(work.shape[0], np.nan, dtype=float) | |
| z = np.full(work.shape[0], np.nan, dtype=float) | |
| for backend, idx in work.groupby("backend_name").groups.items(): | |
| vals = pd.to_numeric(work.loc[idx, "raw_score"], errors="coerce").to_numpy(dtype=float) | |
| valid = np.isfinite(vals) | |
| if valid.any(): | |
| v = vals[valid] | |
| ranks = pd.Series(v).rank(method="average", pct=True).to_numpy(dtype=float) | |
| # lower score is better -> invert percentile for "higher is better normalized" | |
| nv = 1.0 - ranks | |
| mu = float(np.mean(v)) | |
| sd = float(np.std(v)) | |
| zv = (v - mu) / (sd if sd > 1e-9 else 1.0) | |
| # map back | |
| norm_vals = np.full(vals.shape[0], np.nan, dtype=float) | |
| z_vals = np.full(vals.shape[0], np.nan, dtype=float) | |
| norm_vals[valid] = nv | |
| z_vals[valid] = zv | |
| norm[list(idx)] = norm_vals | |
| z[list(idx)] = z_vals | |
| work["normalized_score"] = norm | |
| work["zscore_within_backend"] = z | |
| unique_sem = set(work["score_semantics"].astype(str).tolist()) | |
| direct = len(unique_sem) == 1 | |
| work["direct_comparison_justified"] = bool(direct) | |
| if not direct: | |
| work["direct_comparison_justified"] = False | |
| cols = [ | |
| "ligand_id", | |
| "backend_name", | |
| "backend_mode", | |
| "raw_score", | |
| "normalized_score", | |
| "zscore_within_backend", | |
| "score_semantics", | |
| "direct_comparison_justified", | |
| "provenance_path", | |
| "parsed_from", | |
| ] | |
| for c in cols: | |
| if c not in work.columns: | |
| work[c] = np.nan | |
| return work[cols].copy() | |
| def summarize_backend_table(df: pd.DataFrame) -> pd.DataFrame: | |
| if df.empty: | |
| return pd.DataFrame( | |
| columns=[ | |
| "backend_name", | |
| "score_semantics", | |
| "n_scores", | |
| "mean_raw_score", | |
| "std_raw_score", | |
| "mean_normalized_score", | |
| "direct_comparison_justified", | |
| ] | |
| ) | |
| rows = [] | |
| for backend, sub in df.groupby("backend_name"): | |
| rows.append( | |
| { | |
| "backend_name": str(backend), | |
| "score_semantics": str(sub["score_semantics"].iloc[0]), | |
| "n_scores": int(sub.shape[0]), | |
| "mean_raw_score": float(pd.to_numeric(sub["raw_score"], errors="coerce").mean()), | |
| "std_raw_score": float(pd.to_numeric(sub["raw_score"], errors="coerce").std()), | |
| "mean_normalized_score": float(pd.to_numeric(sub["normalized_score"], errors="coerce").mean()), | |
| "direct_comparison_justified": bool(sub["direct_comparison_justified"].all()), | |
| } | |
| ) | |
| return pd.DataFrame(rows) | |
| def semantic_compatibility_notes() -> Dict[str, str]: | |
| return { | |
| "rdock": "Pose generation + docking score; lower score is better.", | |
| "haddock": ( | |
| "HADDOCK emscoring on provided complexes/poses; energy proxy; " | |
| "lower score is better; in this repository treated as rescoring semantics." | |
| ), | |
| "cross_backend": ( | |
| "Direct raw-score comparison between rDock and HADDOCK is approximate; " | |
| "prefer within-backend normalization / rank statistics." | |
| ), | |
| } | |