-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprotonate.py
83 lines (66 loc) · 2.94 KB
/
deprotonate.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
# SPDX-FileCopyrightText: 2024 Matthew J. Milner <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause
import argparse
import json
import logging
import sys
from support import easyxtb
from protonate import cleanup_after_taut
logger = logging.getLogger(__name__)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true")
parser.add_argument("--print-options", action="store_true")
parser.add_argument("--run-command", action="store_true")
parser.add_argument("--display-name", action="store_true")
parser.add_argument("--lang", nargs="?", default="en")
parser.add_argument("--menu-path", action="store_true")
args = parser.parse_args()
# Disable if xtb or crest missing
if easyxtb.XTB_BIN is None or easyxtb.CREST_BIN is None:
quit()
if args.print_options:
options = {"inputMoleculeFormat": "xyz"}
print(json.dumps(options))
if args.display_name:
print("Deprotonate")
if args.menu_path:
print("Extensions|Semi-empirical (xtb){730}")
if args.run_command:
# Read input from Avogadro
avo_input = json.loads(sys.stdin.read())
# Extract the coords
geom = easyxtb.Geometry.from_xyz(avo_input["xyz"].split("\n"))
# Run calculation; returns set of tautomers as well as Calculation object
tautomers, calc = easyxtb.calc.deprotonate(
geom,
charge=avo_input["charge"],
multiplicity=avo_input["spin"],
solvation=easyxtb.config["solvent"],
method=2,
return_calc=True,
)
best_cjson = calc.output_geometry.to_cjson()
tautomer_cjson = easyxtb.convert.taut_to_cjson(tautomers)
# Get energy for Avogadro
energies = easyxtb.convert.convert_energy(calc.energy, "hartree")
# Format everything appropriately for Avogadro
# Start by passing back a cleaned version of original cjson
output = {
"moleculeFormat": "cjson",
# Remove anything that is now unphysical after the optimization
"cjson": cleanup_after_taut(avo_input["cjson"]),
}
# Add data from calculation
output["cjson"]["atoms"] = best_cjson["atoms"]
output["cjson"]["properties"]["totalEnergy"] = round(energies["eV"], 7)
output["cjson"]["atoms"]["coords"]["3dSets"] = tautomer_cjson["atoms"]["coords"]["3dSets"]
output["cjson"]["properties"]["energies"] = tautomer_cjson["properties"]["energies"]
# Make sure to adjust new charge
output["cjson"]["properties"]["totalCharge"] -= 1
# Save result
with open(easyxtb.TEMP_DIR / "result.cjson", "w", encoding="utf-8") as save_file:
json.dump(output["cjson"], save_file, indent=2)
# Pass back to Avogadro
print(json.dumps(output))
logger.debug(f"The following dictionary was passed back to Avogadro: {output}")