-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_reaction_smiles_using_indigo.py
216 lines (177 loc) · 6.37 KB
/
map_reaction_smiles_using_indigo.py
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
""" The ``scripts`` directory ``map_reaction_smiles_using_indigo`` script. """
from argparse import ArgumentParser, Namespace
from logging import Formatter, Logger, StreamHandler, getLogger
from pandas import DataFrame, concat, read_csv
from atom_to_atom_mapping.utility import IndigoAtomToAtomMappingUtility
def get_script_arguments() -> Namespace:
"""
Get the script arguments.
:returns: The script arguments.
"""
argument_parser = ArgumentParser()
argument_parser.add_argument(
"-rs",
"--reaction_smiles",
default=None,
type=str,
help="The chemical reaction SMILES string."
)
argument_parser.add_argument(
"-icfp",
"--input_csv_file_path",
default=None,
type=str,
help="The path to the input .csv file."
)
argument_parser.add_argument(
"-rscn",
"--reaction_smiles_column_name",
default=None,
type=str,
help="The name of the chemical reaction SMILES column in the input .csv file."
)
argument_parser.add_argument(
"-ocfp",
"--output_csv_file_path",
default=None,
type=str,
help="The path to the output .csv file."
)
argument_parser.add_argument(
"-tp",
"--timeout_period",
default=10000,
type=int,
help="The timeout period in milliseconds."
)
argument_parser.add_argument(
"-heamn",
"--handle_existing_atom_map_numbers",
default="discard",
type=str,
choices=[
"alter",
"clear",
"discard",
"keep",
],
help="The indicator of how the existing chemical reaction compound atom map numbers should be handled."
)
argument_parser.add_argument(
"-iac",
"--ignore_atom_charges",
action="store_true",
help="The indicator of whether the chemical reaction compound atom charges should be ignored."
)
argument_parser.add_argument(
"-iai",
"--ignore_atom_isotopes",
action="store_true",
help="The indicator of whether the chemical reaction compound atom isotopes should be ignored."
)
argument_parser.add_argument(
"-iav",
"--ignore_atom_valences",
action="store_true",
help="The indicator of whether the chemical reaction compound atom valences should be ignored."
)
argument_parser.add_argument(
"-iar",
"--ignore_atom_radicals",
action="store_true",
help="The indicator of whether the chemical reaction compound atom radicals should be ignored."
)
argument_parser.add_argument(
"-crs",
"--canonicalize_reaction_smiles",
action="store_true",
help="The indicator of whether the chemical reaction SMILES string should be canonicalized."
)
argument_parser.add_argument(
"-np",
"--number_of_processes",
default=1,
type=int,
help="The number of processes."
)
return argument_parser.parse_args()
def get_script_logger() -> Logger:
"""
Get the script logger.
:returns: The script logger.
"""
logger = getLogger(
name="script_logger"
)
logger.setLevel(
level="DEBUG"
)
formatter = Formatter(
fmt="[{name:s} @ {asctime:s}] {levelname:s}: \"{message:s}\"",
style="{"
)
stream_handler = StreamHandler()
stream_handler.setLevel(
level="DEBUG"
)
stream_handler.setFormatter(
fmt=formatter
)
logger.addHandler(
hdlr=stream_handler
)
return logger
if __name__ == "__main__":
script_logger = get_script_logger()
try:
script_arguments = get_script_arguments()
if script_arguments.reaction_smiles is not None:
print({
"reaction_smiles": script_arguments.reaction_smiles,
})
print(IndigoAtomToAtomMappingUtility.map_reaction_smiles(
reaction_smiles=script_arguments.reaction_smiles,
timeout_period=script_arguments.timeout_period,
handle_existing_atom_map_numbers=script_arguments.handle_existing_atom_map_numbers,
ignore_atom_charges=script_arguments.ignore_atom_charges,
ignore_atom_isotopes=script_arguments.ignore_atom_isotopes,
ignore_atom_valences=script_arguments.ignore_atom_valences,
ignore_atom_radicals=script_arguments.ignore_atom_radicals,
canonicalize_reaction_smiles=script_arguments.canonicalize_reaction_smiles,
logger=script_logger
))
if (
script_arguments.input_csv_file_path is not None and
script_arguments.reaction_smiles_column_name is not None and
script_arguments.output_csv_file_path is not None
):
input_csv_file_data = read_csv(
filepath_or_buffer=script_arguments.input_csv_file_path
)
concat(
objs=[
input_csv_file_data,
DataFrame(
data=IndigoAtomToAtomMappingUtility.map_reaction_smiles_strings(
reaction_smiles_strings=input_csv_file_data[
script_arguments.reaction_smiles_column_name
].values.tolist(),
number_of_processes=script_arguments.number_of_processes,
timeout_period=script_arguments.timeout_period,
handle_existing_atom_map_numbers=script_arguments.handle_existing_atom_map_numbers,
ignore_atom_charges=script_arguments.ignore_atom_charges,
ignore_atom_isotopes=script_arguments.ignore_atom_isotopes,
ignore_atom_valences=script_arguments.ignore_atom_valences,
ignore_atom_radicals=script_arguments.ignore_atom_radicals,
canonicalize_reaction_smiles=script_arguments.canonicalize_reaction_smiles,
logger=script_logger
)
),
],
axis=1
).to_csv(
path_or_buf=script_arguments.output_csv_file_path,
index=False
)
except:
raise