Update tool/orbital.py
Browse files- tool/orbital.py +92 -93
tool/orbital.py
CHANGED
|
@@ -1,94 +1,93 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""
|
| 3 |
-
Created on Wed Oct 30 09:14:55 2024
|
| 4 |
-
|
| 5 |
-
@author: BM109X32G-10GPU-02
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
import
|
| 15 |
-
import
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
from
|
| 19 |
-
from
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
import
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
mol
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
smi =
|
| 51 |
-
|
| 52 |
-
a =
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
lumo
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
mol
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
Y_homo,
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
raise NotImplementedError()
|
| 94 |
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Wed Oct 30 09:14:55 2024
|
| 4 |
+
|
| 5 |
+
@author: BM109X32G-10GPU-02
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
from rdkit.Chem import AllChem
|
| 12 |
+
from sklearn.datasets import make_blobs
|
| 13 |
+
import json
|
| 14 |
+
import numpy as np
|
| 15 |
+
import math
|
| 16 |
+
|
| 17 |
+
from scipy import sparse
|
| 18 |
+
from sklearn.metrics import median_absolute_error,r2_score, mean_absolute_error,mean_squared_error
|
| 19 |
+
from langchain.tools import BaseTool
|
| 20 |
+
import pandas as pd
|
| 21 |
+
|
| 22 |
+
from rdkit import Chem
|
| 23 |
+
import pickle
|
| 24 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def split_string(string):
|
| 28 |
+
|
| 29 |
+
result = []
|
| 30 |
+
|
| 31 |
+
for char in string:
|
| 32 |
+
|
| 33 |
+
result.append(char)
|
| 34 |
+
|
| 35 |
+
return result
|
| 36 |
+
|
| 37 |
+
def main(sm):
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
inchis = list([sm])
|
| 41 |
+
rts = list([0])
|
| 42 |
+
|
| 43 |
+
smiles, targets,features = [], [],[]
|
| 44 |
+
for i, inc in enumerate(inchis):
|
| 45 |
+
mol = Chem.MolFromSmiles(inc)
|
| 46 |
+
if mol is None:
|
| 47 |
+
continue
|
| 48 |
+
else:
|
| 49 |
+
smi =AllChem. GetMorganFingerprintAsBitVect(mol,1024)
|
| 50 |
+
smi = smi.ToBitString()
|
| 51 |
+
a = split_string(smi)
|
| 52 |
+
a = np.array(a)
|
| 53 |
+
#smi = Chem.MolToSmiles(mol)
|
| 54 |
+
features.append(a)
|
| 55 |
+
targets.append(rts[i])
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
features = np.asarray(features)
|
| 60 |
+
targets = np.asarray(targets)
|
| 61 |
+
X_test=features
|
| 62 |
+
Y_test=targets
|
| 63 |
+
n_features=10
|
| 64 |
+
|
| 65 |
+
model = RandomForestRegressor(n_estimators=100)
|
| 66 |
+
load_homo = pickle.load(open(r"tool/orbital/homo.pkl", 'rb'))
|
| 67 |
+
load_lumo = pickle.load(open(r"tool/orbital/lumo.pkl", 'rb'))
|
| 68 |
+
# model = load_model('C:/Users/sunjinyu/Desktop/FingerID Reference/drug-likeness/CNN/single_model.h5')
|
| 69 |
+
Y_homo= load_homo.predict(X_test)
|
| 70 |
+
Y_lumo = load_lumo.predict(X_test)
|
| 71 |
+
homo = float(Y_homo)
|
| 72 |
+
lumo = float(Y_lumo)
|
| 73 |
+
return homo, lumo
|
| 74 |
+
|
| 75 |
+
class homolumo_predictor(BaseTool):
|
| 76 |
+
name: str = "homolumo_predictor"
|
| 77 |
+
description: str = (
|
| 78 |
+
"Input SMILES , returns the HOMO/LUMO (Highest Occupied Molecular Orbital (HOMO) \
|
| 79 |
+
and Lowest Unoccupied Molecular Orbital)."
|
| 80 |
+
)
|
| 81 |
+
def __init__(self):
|
| 82 |
+
super().__init__()
|
| 83 |
+
def _run(self, smiles: str) -> str:
|
| 84 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 85 |
+
if mol is None:
|
| 86 |
+
return "Invalid SMILES string"
|
| 87 |
+
Y_homo, Y_lumo = main( str(smiles) )
|
| 88 |
+
return f"The HOMO is predicted to be {'{:.2f}'.format(Y_homo)} eV , the LUMO is predicted to be {'{:.2f}'.format(Y_lumo)} eV"
|
| 89 |
+
|
| 90 |
+
async def _arun(self, smiles: str) -> str:
|
| 91 |
+
"""Use the tool asynchronously."""
|
| 92 |
+
raise NotImplementedError()
|
|
|
|
| 93 |
|