File size: 1,563 Bytes
80dde7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# predict.py — Kidney Arch3 LazyPredict-best deployment model
# (BaggingClassifier, the full-pool sweep winner). Predicts the
# probability of the kidney-toxic (nephrotoxicity-concern) class for
# one molecule.
#
# Usage:
#   python3 predict.py "CCO"            -> prints the probability
#   from predict import predict; predict("CCO")
#   from predict import predict_many; predict_many([smi1, smi2])
#
# Requires: scikit-learn, rdkit, joblib, numpy, and
# deployment_common.py (shipped in this repo) on the import path — the
# shipped preprocessing object (DescPrep) unpickles against that
# module.
#
# Featurization is the deployment pipeline's own code: featurize /
# prep_matrix from deployment_common.py; prediction replicates
# screen_leads.py's arch3_prob exactly (fp_desc block).
import os, sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

import numpy as np
import joblib
from deployment_common import featurize, prep_matrix

_bundle = joblib.load(os.path.join(HERE, "model.joblib"))
_model, _prep = _bundle["model"], _bundle["prep"]


def predict_many(smiles_list):
    """Probabilities of the toxic class; None where unparseable."""
    blocks, valid = featurize(list(smiles_list))
    X = prep_matrix(blocks, _prep, "fp_desc")
    p = _model.predict_proba(X)[:, 1]
    return [float(v) if ok else None for v, ok in zip(p, valid)]


def predict(smiles):
    """Probability of the toxic class; None if unparseable."""
    return predict_many([smiles])[0]


if __name__ == "__main__":
    print(predict(sys.argv[1]))