jinysun commited on
Commit
ab4f034
·
verified ·
1 Parent(s): e397834

Update tool/PCE.py

Browse files
Files changed (1) hide show
  1. tool/PCE.py +206 -146
tool/PCE.py CHANGED
@@ -1,146 +1,206 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- """
4
- Created on Wed Sep 11 10:27:20 2024
5
-
6
- @author: BM109X32G-10GPU-02
7
- """
8
-
9
- from langchain.tools import BaseTool
10
- from rdkit import Chem
11
- from rdkit.Chem import rdMolDescriptors
12
- from rdkit.Chem import Descriptors
13
- from .deepacceptor import RF
14
- from .deepdonor import sm, pm
15
- from .dap import run, screen
16
- import pandas as pd
17
-
18
- class acceptor_predictor(BaseTool):
19
- name:str = "acceptor_predictor"
20
- description:str = (
21
- "Input acceptor SMILES , returns the score of the acceptor."
22
- )
23
-
24
- def __init__(self):
25
- super().__init__()
26
- def _run(self, smiles: str) -> str:
27
- mol = Chem.MolFromSmiles(smiles)
28
- if mol is None:
29
- return "Invalid SMILES string"
30
- smiles = Chem.MolToSmiles(mol)
31
- pce = RF.main( str(smiles) )
32
- return f'The power conversion efficiency (PCE) is predicted to be {pce} (predicted by DeepAcceptor) '
33
-
34
- async def _arun(self, smiles: str) -> str:
35
- """Use the tool asynchronously."""
36
- raise NotImplementedError()
37
-
38
- class donor_predictor(BaseTool):
39
- name:str = "donor_predictor"
40
- description:str = (
41
- "Input donor SMILES , returns the score of the donor."
42
- )
43
-
44
- def __init__(self):
45
- super().__init__()
46
- def _run(self, smiles: str) -> str:
47
- mol = Chem.MolFromSmiles(smiles)
48
- if mol is None:
49
- return "Invalid SMILES string"
50
-
51
- mol = Chem.MolFromSmiles(smiles)
52
- if mol is None:
53
- return "Invalid SMILES string"
54
- sdpce = sm.main( str(smiles) )
55
- pdpce = pm.main( str(smiles) )
56
- return f'The power conversion efficiency (PCE) of the given molecule is predicted to be {sdpce} as a small molecule donor , and {pdpce} as a polymer donor(predicted by DeepDonor) '
57
-
58
- async def _arun(self, smiles: str) -> str:
59
- """Use the tool asynchronously."""
60
- raise NotImplementedError()
61
-
62
-
63
-
64
- class dap_predictor(BaseTool):
65
- name:str = "dap_predictor"
66
- description :str = (
67
- "Input SMILES of D/A pairs(separated by '.') , returns the performance of the D/A pairs ."
68
- )
69
-
70
-
71
- def __init__(self):
72
- super().__init__()
73
-
74
- def _run(self, smiles_pair: str) -> str:
75
- smi_list = smiles_pair.split(".")
76
- if len(smi_list) != 2:
77
-
78
- return "Input error, please input two smiles strings separated by '.'"
79
-
80
- else:
81
- smiles1, smiles2 = smi_list
82
-
83
-
84
- pce = run.smiles_aas_test( str(smiles1 ), str(smiles2) )
85
-
86
- return pce
87
-
88
- async def _arun(self, smiles_pair: str) -> str:
89
- """Use the tool asynchronously."""
90
- raise NotImplementedError()
91
-
92
-
93
-
94
- class dap_screen(BaseTool):
95
- name:str = "dap_screen"
96
- description :str = (
97
- "Input dataset path containing D/A pairs, returns the files of prediction results."
98
- )
99
- return_direct: bool = True
100
- def __init__(self):
101
- super().__init__()
102
-
103
- def _run(self, file_path: str) -> str:
104
- smi_list = screen.smiles_aas_test(file_path)
105
-
106
- return smi_list
107
-
108
- async def _arun(self, smiles_pair: str) -> str:
109
- """Use the tool asynchronously."""
110
- raise NotImplementedError()
111
-
112
-
113
- from .comget import generator
114
-
115
- class molgen(BaseTool):
116
- name: str = "donorgen"
117
- description: str = (
118
-
119
- "Useful to generate polymer donor molecules with required PCE. "
120
- "Input the values of PCE , return the SMILES"
121
- )
122
-
123
-
124
- def __init__(self
125
- ):
126
- super().__init__( )
127
-
128
-
129
- def _run(self, value ) -> str:
130
- try:
131
- results = generator.generation(value)
132
- for i in results['smiles']:
133
- pdpce = pm.main( str(i) )
134
- if abs(pdpce-float(value))<1.0:
135
- return f"The SMILES of generated donor is {i}, its predicted PCE is {pdpce}."
136
- break
137
-
138
-
139
-
140
- except Exception as e:
141
- return str(e)
142
-
143
-
144
- async def _arun(self, query) -> str:
145
- """Use the tool asynchronously."""
146
- raise NotImplementedError("this tool does not support async")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ Created on Wed Sep 11 10:27:20 2024
5
+
6
+ @author: BM109X32G-10GPU-02
7
+ """
8
+
9
+ from langchain.tools import BaseTool
10
+ from rdkit import Chem
11
+ from rdkit.Chem import rdMolDescriptors
12
+ from rdkit.Chem import Descriptors
13
+ from .deepacceptor import RF
14
+ from .deepdonor import sm, pm
15
+ from .dap import run, screen
16
+ import pandas as pd
17
+
18
+ class acceptor_predictor(BaseTool):
19
+ name:str = "acceptor_predictor"
20
+ description:str = (
21
+ "Input acceptor SMILES , returns the score of the acceptor."
22
+ )
23
+
24
+ def __init__(self):
25
+ super().__init__()
26
+ def _run(self, smiles: str) -> str:
27
+ mol = Chem.MolFromSmiles(smiles)
28
+ if mol is None:
29
+ return "Invalid SMILES string"
30
+ smiles = Chem.MolToSmiles(mol)
31
+ pce = RF.main( str(smiles) )
32
+ return f'The power conversion efficiency (PCE) is predicted to be {pce} (predicted by DeepAcceptor) '
33
+
34
+ async def _arun(self, smiles: str) -> str:
35
+ """Use the tool asynchronously."""
36
+ raise NotImplementedError()
37
+
38
+ class donor_predictor(BaseTool):
39
+ name:str = "donor_predictor"
40
+ description:str = (
41
+ "Input donor SMILES , returns the score of the donor."
42
+ )
43
+
44
+ def __init__(self):
45
+ super().__init__()
46
+ def _run(self, smiles: str) -> str:
47
+ mol = Chem.MolFromSmiles(smiles)
48
+ if mol is None:
49
+ return "Invalid SMILES string"
50
+
51
+ mol = Chem.MolFromSmiles(smiles)
52
+ if mol is None:
53
+ return "Invalid SMILES string"
54
+ sdpce = sm.main( str(smiles) )
55
+ pdpce = pm.main( str(smiles) )
56
+ return f'The power conversion efficiency (PCE) of the given molecule is predicted to be {sdpce} as a small molecule donor , and {pdpce} as a polymer donor(predicted by DeepDonor) '
57
+
58
+ async def _arun(self, smiles: str) -> str:
59
+ """Use the tool asynchronously."""
60
+ raise NotImplementedError()
61
+
62
+
63
+
64
+ class dap_predictor(BaseTool):
65
+ name:str = "dap_predictor"
66
+ description :str = (
67
+ "Input SMILES of D/A pairs(separated by '.') , returns the performance of the D/A pairs ."
68
+ )
69
+
70
+
71
+ def __init__(self):
72
+ super().__init__()
73
+
74
+ def _run(self, smiles_pair: str) -> str:
75
+ smi_list = smiles_pair.split(".")
76
+ if len(smi_list) != 2:
77
+
78
+ return "Input error, please input two smiles strings separated by '.'"
79
+
80
+ else:
81
+ smiles1, smiles2 = smi_list
82
+
83
+
84
+ pce = run.smiles_aas_test( str(smiles1 ), str(smiles2) )
85
+
86
+ return pce
87
+
88
+ async def _arun(self, smiles_pair: str) -> str:
89
+ """Use the tool asynchronously."""
90
+ raise NotImplementedError()
91
+
92
+
93
+
94
+ class dap_screen(BaseTool):
95
+ name:str = "dap_screen"
96
+ description :str = (
97
+ "Input dataset path containing D/A pairs, returns the files of prediction results."
98
+ )
99
+ return_direct: bool = True
100
+ def __init__(self):
101
+ super().__init__()
102
+
103
+ def _run(self, file_path: str) -> str:
104
+ smi_list = screen.smiles_aas_test(file_path)
105
+
106
+ return smi_list
107
+
108
+ async def _arun(self, smiles_pair: str) -> str:
109
+ """Use the tool asynchronously."""
110
+ raise NotImplementedError()
111
+
112
+
113
+ from .comget import generator
114
+
115
+ class molgen(BaseTool):
116
+ name: str = "donorgen"
117
+ description: str = (
118
+
119
+ "Useful to generate polymer donor molecules with required PCE. "
120
+ "Input the values of PCE , return the SMILES"
121
+ )
122
+
123
+
124
+ def __init__(self
125
+ ):
126
+ super().__init__( )
127
+
128
+
129
+ def _run(self, value ) -> str:
130
+ try:
131
+ results = generator.generation(value)
132
+ for i in results['smiles']:
133
+ pdpce = pm.main( str(i) )
134
+ if abs(pdpce-float(value))<1.0:
135
+ return f"The SMILES of generated donor is {i}, its predicted PCE is {pdpce}."
136
+ break
137
+
138
+
139
+
140
+ except Exception as e:
141
+ return str(e)
142
+
143
+
144
+ async def _arun(self, query) -> str:
145
+ """Use the tool asynchronously."""
146
+ raise NotImplementedError("this tool does not support async")
147
+ from rdkit import Chem
148
+ from rdkit.Chem import BRICS
149
+ from typing import List
150
+
151
+ def connect_A_B_A(a_smiles: str, b_smiles: str) -> str:
152
+ mol_A = Chem.MolFromSmiles(a_smiles)
153
+ mol_B = Chem.MolFromSmiles(b_smiles)
154
+ final_product_smiles = set()
155
+ ab_intermediates_gen = BRICS.BRICSBuild([mol_A, mol_B])
156
+ for intermediate_mol in ab_intermediates_gen:
157
+ Chem.SanitizeMol(intermediate_mol)
158
+ aba_products_gen = BRICS.BRICSBuild([intermediate_mol, mol_A])
159
+ for final_mol in aba_products_gen:
160
+ try:
161
+ Chem.SanitizeMol(final_mol)
162
+ final_product_smiles.add(Chem.MolToSmiles(final_mol))
163
+ except Exception as e:
164
+ print(f" {e}")
165
+
166
+ final_mols = [Chem.MolFromSmiles(s) for s in sorted(list(final_product_smiles))]
167
+ smiles = Chem.MolToSmiles(final_mols[0])
168
+ return smiles
169
+
170
+
171
+ class QMGen(BaseTool):
172
+ name: str = "QMGen"
173
+ description: str = (
174
+ "Useful to generateQuasi-macromolecule . "
175
+ "Input two molecule SMILES (separated by '.') , return the SMILES"
176
+ )
177
+ def __init__(self):
178
+ super().__init__( )
179
+ def _run(self, smiles_pair: str) -> str:
180
+ smi_list = smiles_pair.split(".")
181
+ if len(smi_list) != 2:
182
+ return "Input error, please input two smiles strings separated by '.'"
183
+ else:
184
+ smiles1, smiles2 = smi_list
185
+
186
+ frag_1dummy_s = [smiles for smiles in smi_list if smiles.count('*')==1]
187
+ frag_2dummy_s = [smiles for smiles in smi_list if smiles.count('*')==2]
188
+ if len(frag_1dummy_s) != len(frag_1dummy_s):
189
+ return "Input error, please input acceptor (A) unit smiles with one '[*]' and Π bridge unit with two '[*]'."
190
+ smiles1 = frag_1dummy_s[0].replace('[*]','[16*]').replace('C[16*]','[16*]').replace('[16*]C','[16*]')
191
+ smiles2= frag_2dummy_s[0].replace('[*]','[14*]')
192
+
193
+ if len(frag_1dummy_s) != 1:
194
+ return "Input error, please input smiles with '[*]' as the the key to be connected e.g.[14*]c1ccc([14*])s1."
195
+ try:
196
+ products = connect_A_B_A(smiles1, smiles2)
197
+ return products
198
+
199
+
200
+ except Exception as e:
201
+ return str(e)
202
+
203
+
204
+ async def _arun(self, query) -> str:
205
+ """Use the tool asynchronously."""
206
+ raise NotImplementedError("this tool does not support async")