feat(scripts): add UNIFAC subgroup audit tool for registry compounds
Browse files- scripts/audit_unifac_groups.py +140 -0
scripts/audit_unifac_groups.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import json
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from typing import Any
|
| 8 |
+
|
| 9 |
+
from rdkit import Chem
|
| 10 |
+
|
| 11 |
+
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
|
| 12 |
+
|
| 13 |
+
from pino.thermo.unifac import DOUFSG_AVAILABLE_GROUPS, get_dortmund_subgroups
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def audit_registry_thermodynamics(registry_path: str | Path) -> dict[str, Any]:
|
| 17 |
+
"""Scan a JSON-Lines registry and flag compounds that cannot be represented in DOUFSG."""
|
| 18 |
+
print("=== Beginning Comprehensive Dortmund UNIFAC (DOUFSG) Group Audit ===")
|
| 19 |
+
|
| 20 |
+
with Path(registry_path).open("r", encoding="utf-8") as f:
|
| 21 |
+
compounds = [json.loads(line) for line in f]
|
| 22 |
+
|
| 23 |
+
unifac_failures = []
|
| 24 |
+
total = len(compounds)
|
| 25 |
+
inspected = 0
|
| 26 |
+
|
| 27 |
+
for c in compounds:
|
| 28 |
+
cas = c.get("cas", "")
|
| 29 |
+
smiles = c.get("smiles", "")
|
| 30 |
+
name = c.get("name", "Unknown")
|
| 31 |
+
|
| 32 |
+
if not smiles:
|
| 33 |
+
unifac_failures.append({
|
| 34 |
+
"cas": cas,
|
| 35 |
+
"name": name,
|
| 36 |
+
"smiles": smiles,
|
| 37 |
+
"reason": "Missing SMILES string",
|
| 38 |
+
"missing_subgroup_ids": [],
|
| 39 |
+
})
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 44 |
+
if mol is None:
|
| 45 |
+
raise ValueError("Invalid SMILES structure")
|
| 46 |
+
|
| 47 |
+
molecule_subgroups = get_dortmund_subgroups(smiles)
|
| 48 |
+
inspected += 1
|
| 49 |
+
|
| 50 |
+
# Check if all discovered structural fragments exist in the DOUFSG parameter set
|
| 51 |
+
missing_in_doufsg = [
|
| 52 |
+
int(gid) for gid in molecule_subgroups.keys()
|
| 53 |
+
if int(gid) not in DOUFSG_AVAILABLE_GROUPS
|
| 54 |
+
]
|
| 55 |
+
if missing_in_doufsg:
|
| 56 |
+
unifac_failures.append({
|
| 57 |
+
"cas": cas,
|
| 58 |
+
"name": name,
|
| 59 |
+
"smiles": smiles,
|
| 60 |
+
"reason": f"Subgroup IDs {missing_in_doufsg} not present in active DOUFSG definitions",
|
| 61 |
+
"missing_subgroup_ids": missing_in_doufsg,
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
if not molecule_subgroups:
|
| 65 |
+
# Either fragmentation failed or every subgroup was missing.
|
| 66 |
+
# Re-check by trying a raw ugropy call to report the missing subgroup names.
|
| 67 |
+
reason = "UNIFAC fragmentation returned no usable DOUFSG subgroups"
|
| 68 |
+
try:
|
| 69 |
+
import ugropy
|
| 70 |
+
raw_groups = ugropy.Groups(smiles, identifier_type="smiles").dortmund.subgroups
|
| 71 |
+
unknown = [
|
| 72 |
+
name for name in raw_groups.keys()
|
| 73 |
+
if int(getattr(name, "id", 0)) not in DOUFSG_AVAILABLE_GROUPS
|
| 74 |
+
]
|
| 75 |
+
if unknown:
|
| 76 |
+
reason = f"ugropy groups {unknown} have no matching DOUFSG parameter"
|
| 77 |
+
except Exception:
|
| 78 |
+
pass
|
| 79 |
+
unifac_failures.append({
|
| 80 |
+
"cas": cas,
|
| 81 |
+
"name": name,
|
| 82 |
+
"smiles": smiles,
|
| 83 |
+
"reason": reason,
|
| 84 |
+
"missing_subgroup_ids": [],
|
| 85 |
+
})
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
unifac_failures.append({
|
| 89 |
+
"cas": cas,
|
| 90 |
+
"name": name,
|
| 91 |
+
"smiles": smiles,
|
| 92 |
+
"reason": f"UNIFAC fragmentation error: {str(e)}",
|
| 93 |
+
"missing_subgroup_ids": [],
|
| 94 |
+
})
|
| 95 |
+
|
| 96 |
+
print(f"\nAudit complete. Inspected {total} materials.")
|
| 97 |
+
print(f"Successfully derived DOUFSG subgroups for {inspected} materials.")
|
| 98 |
+
|
| 99 |
+
if unifac_failures:
|
| 100 |
+
print(f"⚠️ Found {len(unifac_failures)} thermodynamic integration blockers:")
|
| 101 |
+
for fail in unifac_failures[:10]:
|
| 102 |
+
print(f" - CAS [{fail['cas']}] ({fail['name']}): {fail['reason']}")
|
| 103 |
+
if len(unifac_failures) > 10:
|
| 104 |
+
print(f" ... and {len(unifac_failures) - 10} more")
|
| 105 |
+
else:
|
| 106 |
+
print("✅ SUCCESS: 100% of registry compounds have valid, un-fragmented DOUFSG definitions.")
|
| 107 |
+
|
| 108 |
+
return {
|
| 109 |
+
"total": total,
|
| 110 |
+
"inspected": inspected,
|
| 111 |
+
"failures": unifac_failures,
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def main() -> int:
|
| 116 |
+
parser = argparse.ArgumentParser(description="Audit registry compounds against DOUFSG")
|
| 117 |
+
parser.add_argument(
|
| 118 |
+
"--registry",
|
| 119 |
+
default="data/aroma_chemicals.jsonl",
|
| 120 |
+
help="Path to the JSON-Lines registry file",
|
| 121 |
+
)
|
| 122 |
+
parser.add_argument(
|
| 123 |
+
"--output",
|
| 124 |
+
default="data/unifac_missing_groups.json",
|
| 125 |
+
help="Path to write the failure report",
|
| 126 |
+
)
|
| 127 |
+
args = parser.parse_args()
|
| 128 |
+
|
| 129 |
+
result = audit_registry_thermodynamics(args.registry)
|
| 130 |
+
|
| 131 |
+
output_path = Path(args.output)
|
| 132 |
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 133 |
+
output_path.write_text(json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8")
|
| 134 |
+
print(f"\nBlockers exported to {output_path}.")
|
| 135 |
+
|
| 136 |
+
return 0 if not result["failures"] else 1
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
raise SystemExit(main())
|