-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCMSpyDFT.py
303 lines (241 loc) · 10.4 KB
/
GCMSpyDFT.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import argparse
import logging
import os
import pathlib
import sys
import warnings
from io import TextIOWrapper
from datamolecule import make_structure, DataMolecule
def command_line():
parser = argparse.ArgumentParser(
prog="GC-MS -> DFT",
description="Program to convert an output of a GC-MS run into computational chemistry input files.")
group = parser.add_mutually_exclusive_group()
group.add_argument("-v",
"--verbosity",
action="store_true",
help="make verbose")
group.add_argument("-q",
"--quiet",
action="store_true",
help="silence output")
parser.add_argument('infile',
# nargs='?',
type=argparse.FileType('r'),
# default=sys.stdin,
help="Input file name.")
parser.add_argument('-V',
'--version',
action='version',
version='%(prog)s v0.1')
parser.add_argument('-o',
'--output',
type=pathlib.Path,
help="Output directory.")
parser.add_argument('-c',
'--cores',
type=int,
help="Number of cpu cores.")
parser.add_argument('-m',
'--memory',
type=int,
help="Maximum allowed memory.")
parser.add_argument('-C',
'--checkpoint',
action='store_true',
help="Create checkpoint.")
parser.add_argument('-t',
'--theory',
type=str,
help="Functional method to use for all input files.")
parser.add_argument('-b',
'--basis',
type=str,
help="Basis set to use for all input files.")
parser.add_argument('-T',
'--type',
choices=["SP", "Opt", "Freq"],
nargs='*',
help="Calculation type to preform. eg. 'SP' = 'Single point'")
parser.add_argument('--charge',
type=int,
help="Total Charge to assign each molecules. Overrides predicted charge.")
parser.add_argument('--spin',
type=int,
help="Multiplicity spin to assign each molecule. Overrides predicted spin.")
parser.add_argument('--modred',
type=str,
nargs='*',
help="Modify redundant internal coordinate definitions to be include at "
"the end of each input file.")
return parser.parse_args()
def log_settings(arguments):
logger_setting = logging.getLogger('GCMSpyDFT')
# Logging setup
logger_setup = logging.getLogger('GCMSpyDFT')
logger_setup.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('GCMSpyDFT.log', mode='w')
fh.setLevel(level=logging.DEBUG)
# create formatter and add it to the handlers
fh_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(fh_formatter)
# add the handlers to the logger
logger_setup.addHandler(fh)
# create console handler
ch = logging.StreamHandler()
# create formatter and add it to the handlers
ch_formatter = logging.Formatter('%(levelname)s: %(message)s')
ch.setFormatter(ch_formatter)
if arguments.verbosity:
# create console handler with a higher log level
ch.setLevel(logging.INFO)
logger_setting.addHandler(ch)
logger_setting.debug("Cavitation")
elif arguments.quiet:
# create console handler with no logging
ch.setLevel(logging.CRITICAL + 1)
logger_setting.addHandler(ch)
logger_setting.debug("Quite running")
else:
# create console handler with a default log level
ch.setLevel(logging.WARNING)
logger_setting.addHandler(ch)
logger_setting.debug("Plain bagel")
def configuration(arguments):
from config import cfg
configuration_logger = logging.getLogger('GCMSpyDFT')
# checks if configuration file exist
if os.path.isfile(cfg.config_path):
configuration_logger.info("Config file found, loading settings...")
cfg.read_config()
# if configuration file does not exist
else:
configuration_logger.info("Config file not found, writing settings...")
cfg.make_config(cfg.config_path)
cfg.read_config()
def settings(arguments):
from config import cfg
if args.output:
cfg.output = args.output
elif args.cores:
cfg.cores = args.cores
elif args.memory:
cfg.memory = args.memory
elif args.checkpoint:
cfg.memory = args.checkpoint
elif args.theory:
cfg.theory = args.theory
elif args.basis:
cfg.basis = args.basis
elif args.type:
cfg.calc_type = args.type
elif args.charge:
cfg.charge = args.charge
elif args.spin:
cfg.spin = args.spin
elif args.modred:
cfg.modred = args.modred
def read_input_file(file: TextIOWrapper) -> list[list[str]]:
file_reader_logger = logging.getLogger('GCMSpyDFT')
import interpreter as gi
lines = gi.read_to_list(file)
if header_line := gi.content_finder(lines) != -1:
file_reader_logger.debug(f'Found header for file {file} at line {header_line}.')
trimmed_lines = gi.trim_header(lines, header_line)
else:
file_reader_logger.warning(f'Assuming there is no header in file {file}')
trimmed_lines = gi.trim_header(lines, 0)
return gi.peak_agg(trimmed_lines)
def parse_peaks(peak_lines: list[list[str]]) -> list[object]:
from peaks import Peak
list_of_peaks: list[object] = []
for lines in peak_lines:
current_peak = Peak(lines)
current_peak.peak_header()
current_peak.left_align()
current_peak.add_separator()
current_peak.combine_lines()
current_peak.parse_lines()
list_of_peaks.append(current_peak)
return list_of_peaks
def create_molecules(names: list[str], structures: list[str]) -> list[DataMolecule]:
# TODO: added charge and spin maybe?
from datamolecule import DataMolecule
molecule_list: list[DataMolecule] = []
for name, structure in zip(names, structures):
current_mol = DataMolecule(name, structure)
current_mol.mol.addh()
current_mol.mol.make2D()
current_mol.mol.make3D()
current_mol.mol.OBMol.Center()
molecule_list.append(current_mol)
return molecule_list
def run() -> list[str]:
from config import cfg
warnings.simplefilter('error')
peak_blocks: list[list[str]] = read_input_file(args.infile) # collection of lines organized by peak
peak_list: list[object] = parse_peaks(peak_blocks) # collection of peak objects
failed_names: list[str] = []
for peak in peak_list:
mol_names: list = getattr(peak, 'ID')
mol_structures: list[str] = []
success_names: list[str] = []
data_molecule: list = [] # FIXME: rename
for i, mol_name in enumerate(mol_names):
try:
structure = make_structure(mol_name)
mol_structures.append(structure)
success_names.append(mol_name)
logger.info(f'{cfg.OKCYAN} Success! {mol_name} is now a structure! {cfg.ENDC}')
# print(structure)
data_molecule.append((mol_name, structure, i))
except Warning as w:
failed_names.append(mol_name)
logger.warning(f'{cfg.FAIL} FAIL! {cfg.UNDERLINE}{mol_name}{cfg.SUNDERLINE} threw an error!\n'
f'{cfg.WARNING} {str(w).strip()} {cfg.ENDC}')
# mol_structures = make_structure(mol_names)
peak_molecules: list[DataMolecule] = create_molecules(success_names, mol_structures)
# peak_molecules: list[DataMolecule] = create_molecules(*data_molecule)
for i, peak_molecule in enumerate(peak_molecules):
folder_path = (f'{cfg.output}/'
f'{getattr(peak, "peak_num")}'
f'-{getattr(peak, "retention_time")}'
f'-{getattr(peak, "percent_area")}/'
f'{getattr(peak, "reference_nums")[i]}'
f'-{getattr(peak, "cas_nums")[i]}'
f'-{getattr(peak, "qualities")[i]}/')
if not os.path.isdir(folder_path):
os.makedirs(folder_path)
# output/PK#-RT-area%/ref-cas-qual/name.gau
file_path = (f'{cfg.output}/'
f'{getattr(peak, "peak_num")}'
f'-{getattr(peak, "retention_time")}'
f'-{getattr(peak, "percent_area")}/'
f'{getattr(peak, "reference_nums")[i]}'
f'-{getattr(peak, "cas_nums")[i]}'
f'-{getattr(peak, "qualities")[i]}/'
f'{getattr(peak_molecule, "name_no_space")}'
f'.inp')
# %cores=1 \n %mem=50 \n %check=name_[calc_type]
header = (f'%NProcShared={cfg.cores}\n'
f'%mem={cfg.memory}\n'
f'%chk={getattr(peak_molecule, "name")}_{",".join(cfg.calc_type).lower()}.chk\n')
# #p theory/basis calc_type
keywords = f'\n#p {cfg.theory}/{cfg.basis} ({",".join(cfg.calc_type)})'
peak_molecule.mol.title = peak_molecule.name + ' ' + '/'.join(cfg.calc_type).strip() + ' GCMSpyDFT'
peak_molecule.mol.write(format='gau', filename=file_path, opt={'k': header + keywords}, overwrite=True)
warnings.resetwarnings()
return failed_names
if __name__ == '__main__':
# run things
args = command_line()
log_settings(args)
logger = logging.getLogger('GCMSpyDFT')
logger.info("Starting configuration")
configuration(args)
logger.info("Starting settings")
settings(args)
logger.info("Starting run")
if failed := run():
logger.error(f'List of molecules that failed to form structures. {failed}')