File size: 9,029 Bytes
da8eac4 0538e73 da8eac4 899f17e 5a434bd 899f17e 5a434bd 899f17e 5a434bd 899f17e da8eac4 | 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | """
CASCADE worker process
"""
import os
import sys
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
import json
import math
import pickle
import datetime
from io import StringIO
import redis
import numpy as np
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import keras
from keras.models import load_model
from NMR_Prediction.apply import (
preprocess_C, preprocess_H,
evaluate_C, evaluate_H,
RBFSequence,
)
from nfp.layers import (
MessageLayer, GRUStep, Squeeze, EdgeNetwork,
ReduceBondToPro, ReduceBondToAtom,
GatherAtomToBond, ReduceAtomToPro,
)
from nfp.models import GraphModel
import pandas as pd
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import SDWriter
from NMR_Prediction.genConf import genConf
MODEL_PATH_C = os.path.join("NMR_Prediction", "schnet_edgeupdate", "best_model.hdf5")
MODEL_PATH_H = os.path.join("NMR_Prediction", "schnet_edgeupdate_H", "best_model.hdf5")
PREPROCESSOR_PATH = os.path.join("NMR_Prediction", "preprocessor.p")
custom_objects = {
"MessageLayer": MessageLayer,
"GRUStep": GRUStep,
"Squeeze": Squeeze,
"EdgeNetwork": EdgeNetwork,
"ReduceBondToPro": ReduceBondToPro,
"ReduceBondToAtom": ReduceBondToAtom,
"GatherAtomToBond": GatherAtomToBond,
"ReduceAtomToPro": ReduceAtomToPro,
"GraphModel": GraphModel,
}
print("Loading 13C model...", flush=True)
model_C = load_model(MODEL_PATH_C, custom_objects=custom_objects)
print("Loading 1H model...", flush=True)
model_H = load_model(MODEL_PATH_H, custom_objects=custom_objects)
print("Both models loaded.", flush=True)
with open(PREPROCESSOR_PATH, "rb") as f:
preprocessor = pickle.load(f)["preprocessor"]
redis_client = redis.StrictRedis(
host="localhost", port=6379, db=0, decode_responses=True
)
# ββ Analytics logging to HF Dataset ββββββββββββββββββββββββββββββββββββββββββ
_HF_TOKEN = os.environ.get("HF_TOKEN", "")
_ANALYTICS_REPO = "patonlab/analytics"
_ANALYTICS_FILE = "visits.csv"
def _log_prediction():
"""Append one row to the existing patonlab/analytics data.csv.
Format matches the alfabet log: space,timestamp
"""
if not _HF_TOKEN:
return
try:
from huggingface_hub import HfApi, hf_hub_download
import tempfile
api = HfApi(token=_HF_TOKEN)
timestamp = datetime.datetime.utcnow().isoformat()
with tempfile.TemporaryDirectory() as tmpdir:
local_path = hf_hub_download(
repo_id=_ANALYTICS_REPO,
filename=_ANALYTICS_FILE,
repo_type="dataset",
token=_HF_TOKEN,
local_dir=tmpdir,
)
with open(local_path, "a") as f:
f.write(f"patonlab/cascade,{timestamp}\n")
api.upload_file(
path_or_fileobj=local_path,
path_in_repo=_ANALYTICS_FILE,
repo_id=_ANALYTICS_REPO,
repo_type="dataset",
commit_message=f"log: cascade prediction {timestamp[:10]}",
)
except Exception as e:
print(f"Analytics logging failed (non-fatal): {e}", flush=True)
def _mol_to_sdf(mol, conf_id=0):
sio = StringIO()
w = SDWriter(sio)
w.write(mol, confId=conf_id)
w.close()
return sio.getvalue()
def _build_sdfs_from_genconf(mol_with_confs, ids):
sdfs = []
energy_order = []
for energy, conf_id in ids:
try:
sdf = _mol_to_sdf(mol_with_confs, conf_id=int(conf_id))
if sdf.strip():
sdfs.append(sdf)
energy_order.append(int(conf_id))
except Exception as e:
print(f"SDF error for conf_id={conf_id}: {e}", flush=True)
return sdfs, energy_order
def _boltzmann_average(spread_df):
spread_df["b_weight"] = spread_df["relative_E"].apply(
lambda x: math.exp(-x / (0.001987 * 298.15))
)
df_group = spread_df.set_index(["mol_id", "atom_index", "cf_id"]).groupby(level=[0, 1])
final = []
for (m_id, a_id), df in df_group:
ws = (df["b_weight"] * df["predicted"]).sum() / df["b_weight"].sum()
final.append([m_id, a_id, ws])
final = pd.DataFrame(final, columns=["mol_id", "atom_index", "Shift"])
final["atom_index"] = final["atom_index"].apply(lambda x: x + 1)
return final.round(2).astype(dtype={"atom_index": "int"})
def _fmt_weighted(final_df):
return "".join(f"{int(r['atom_index'])},{r['Shift']:.2f};" for _, r in final_df.iterrows())
def _fmt_conf_shifts(spread_df, energy_order):
parts = []
for cf_id in energy_order:
sub = spread_df[spread_df["cf_id"] == cf_id]
if len(sub) == 0:
continue
parts.append("".join(f"{int(r['atom_index'])},{r['predicted']:.2f};" for _, r in sub.iterrows()))
return "!".join(parts)
def _fmt_relative_E(spread_df, energy_order):
total_bw = spread_df.groupby("cf_id")["b_weight"].first().sum()
parts = []
for cf_id in energy_order:
sub = spread_df[spread_df["cf_id"] == cf_id]
if len(sub) == 0:
continue
e = round(sub["relative_E"].iloc[0], 2)
bw = round(sub["b_weight"].iloc[0] / total_bw, 4)
parts.append(f"{e},{bw},")
return "!".join(parts)
def run_job(task_id, smiles, type_):
result_key = f"task_result_{task_id}"
try:
mol = Chem.MolFromSmiles(smiles)
AllChem.EmbedMolecule(mol, useRandomCoords=True)
mol_with_h = Chem.AddHs(mol, addCoords=True)
mol_with_confs, ids, nr = genConf(mol_with_h, rms=-1, nc=200, efilter=10.0, rmspost=0.5)
print(f"genConf: {len(ids)} conformers", flush=True)
conf_sdfs, energy_order = _build_sdfs_from_genconf(mol_with_confs, ids)
mols = [Chem.MolFromSmiles(smiles)]
for m in mols:
AllChem.EmbedMolecule(m, useRandomCoords=True)
mols = [Chem.AddHs(m, addCoords=True) for m in mols]
# Suppress duplicate genConf stdout during preprocess
_stdout, _stderr = sys.stdout, sys.stderr
sys.stdout = sys.stderr = open(os.devnull, 'w')
try:
if type_ == "C":
inputs, df, conf_mols = preprocess_C(mols, preprocessor, keep_all_cf=True)
else:
inputs, df, conf_mols = preprocess_H(mols, preprocessor, keep_all_cf=True)
finally:
sys.stdout.close()
sys.stdout, sys.stderr = _stdout, _stderr
if type_ == "C":
predicted = evaluate_C(inputs, preprocessor, model_C)
else:
predicted = evaluate_H(inputs, preprocessor, model_H)
if len(inputs) == 0:
raise RuntimeError("No conformers generated")
spread_df = pd.DataFrame(columns=["mol_id", "atom_index", "relative_E", "cf_id"])
for _, r in df.iterrows():
n = len(r["atom_index"])
tmp = pd.DataFrame({
"mol_id": [r["mol_id"]] * n,
"atom_index": r["atom_index"],
"relative_E": [r["relative_E"]] * n,
"cf_id": [r["cf_id"]] * n,
})
spread_df = pd.concat([spread_df, tmp], sort=True)
spread_df["predicted"] = predicted
spread_df["b_weight"] = spread_df["relative_E"].apply(
lambda x: math.exp(-x / (0.001987 * 298.15))
)
spread_df["atom_index"] = spread_df["atom_index"].apply(lambda x: x + 1)
spread_df = spread_df.round(2)
final_df = _boltzmann_average(
spread_df.copy().assign(
atom_index=spread_df["atom_index"].apply(lambda x: x - 1)
)
)
result = {
"smiles": smiles,
"type_": type_,
"conf_sdfs": conf_sdfs,
"weightedShiftTxt": _fmt_weighted(final_df),
"confShiftTxt": _fmt_conf_shifts(spread_df, energy_order),
"relative_E": _fmt_relative_E(spread_df, energy_order),
}
redis_client.set(result_key, json.dumps(result), ex=3600)
print(f"Task {task_id} complete β {len(conf_sdfs)} conformers", flush=True)
# Log to analytics dataset (non-blocking)
_log_prediction()
except Exception as e:
import traceback; traceback.print_exc()
redis_client.set(result_key, json.dumps({"errMessage": str(e)}), ex=3600)
print("Worker ready, waiting for jobs...", flush=True)
while True:
item = redis_client.blpop("task_queue", timeout=5)
if item is None:
continue
_, task_id = item
detail = redis_client.get(f"task_detail_{task_id}")
if not detail:
continue
detail = json.loads(detail)
print(f"Processing task {task_id} smiles={detail['smiles']} type={detail['type_']}", flush=True)
run_job(task_id, detail["smiles"], detail["type_"])
|