-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
182 lines (167 loc) · 5.41 KB
/
cli.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
"""
Command line interface for the getrandompcmol package.
"""
from __future__ import annotations
import argparse as ap
from .evaluate_calc import create_res_dir, eval_calc_ensemble, get_calc_ensemble
from .evaluate_conf import eval_conf_ensemble
from .main import main
from .miscelleanous import bcolors, checkifinpath
DESIREDCHARGE = 1
TAKEONLYWORST = True
def console_entry_point() -> int:
"""
Entry point for the console script.
"""
# parse arguments
parser = ap.ArgumentParser(description="Generate random molecules from PubChem")
parser.add_argument(
"-n",
"--numcomp",
type=int,
default=2,
help="Number of compounds to generate",
required=False,
)
parser.add_argument(
"--version",
action="version",
version="%(prog)s {version}".format(version="0.1.0"),
)
parser.add_argument(
"--opt",
action="store_true",
help="Optimize the generated structures with xTB",
required=False,
default=False,
)
parser.add_argument(
"--seed",
type=int,
default=2009,
help="Seed for the random number generator",
required=False,
)
parser.add_argument(
"--maxcid",
type=int,
default=100000,
help="Maximum CID to generate random numbers",
required=False,
)
parser.add_argument(
"--maxnumat",
type=int,
default=35,
help="Maximum number of atoms in a molecule",
required=False,
)
parser.add_argument(
"--crest",
help="Conformer sampling with CREST. \
Provide a keyword for a run mode: 'normal', 'protonate'. \
The default is 'normal'.",
required=False,
type=str,
choices=["normal", "protonate"],
default=False,
)
parser.add_argument(
"--evalconfonly",
# store two integers as lower and upper limit for the number of conformers
nargs=2,
type=int,
help="ONLY evaluate the conformer ensemble. \
Provide the lower and upper limit for the number of conformers.",
required=False,
default=False,
)
parser.add_argument(
"--evalconf",
# store two integers as lower and upper limit for the number of conformers
nargs=2,
type=int,
help="Evaluate the conformer ensemble. \
Provide the lower and upper limit for the number of conformers.",
required=False,
default=False,
)
parser.add_argument(
"--evalcalconly",
help="Only evaluate the QM calculations. \
This option is only useful if you have already generated the conformer ensemble. \
Give 'wipe' as the second argument to delete the conformer directories.",
required=False,
default=False,
type=str,
choices=["wipe", "keep"],
)
args = parser.parse_args()
# check for inconsistencies between arguments
if (not args.opt) and args.crest:
print(
f"{bcolors.FAIL}You cannot leave --opt out and use --crest.{bcolors.ENDC}"
)
raise SystemExit(1)
if (not args.opt) and args.evalconf:
print(
f"{bcolors.FAIL}You cannot leave --opt out and use --evalconf.{bcolors.ENDC}"
)
raise SystemExit(1)
if (not args.crest) and args.evalconf:
print(
f"{bcolors.FAIL}You cannot leave --crest out and use --evalconf.{bcolors.ENDC}"
)
raise SystemExit(1)
# raise an error if --evalconfonly is used with any other argument
if args.evalconfonly and (args.opt or args.crest or args.evalconf):
print(
f"{bcolors.FAIL}You cannot use --evalconfonly with any other argument.{bcolors.ENDC}"
)
raise SystemExit(1)
# raise an error if --evalcalconly is used with any other argument
if args.evalcalconly and (args.opt or args.crest or args.evalconf):
print(
f"{bcolors.FAIL}You cannot use --evalcalconly with any other argument.{bcolors.ENDC}"
)
raise SystemExit(1)
# check if dependencies are installed
checkifinpath("PubGrep")
checkifinpath("xtb")
if args.opt:
checkifinpath("mctc-convert")
if args.crest:
checkifinpath("crest")
if args.evalconfonly:
# get the list of directories from compounds.txt
dirs = []
try:
with open("compounds.txt", encoding="UTF-8") as f:
lines = f.readlines()
for line in lines:
dirs.append(int(line.strip().split()[0]))
except FileNotFoundError as e:
print(f"{bcolors.FAIL}Error: {e}{bcolors.ENDC}")
print(
f"{bcolors.FAIL}File 'compounds.txt' not found \
and no compound directories provided.{bcolors.ENDC}"
)
raise SystemExit(1) from e
eval_conf_ensemble(args.evalconfonly[0], args.evalconfonly[1], dirs)
# exit the program
return 0
if args.evalcalconly:
calcenergies = get_calc_ensemble(DESIREDCHARGE)
worstcids = eval_calc_ensemble(calcenergies)
wipe = False
if args.evalcalconly == "wipe":
wipe = True
if TAKEONLYWORST:
create_res_dir(calcenergies, wipe, DESIREDCHARGE, worstcids)
else:
create_res_dir(calcenergies, wipe, DESIREDCHARGE)
return 0
main(args)
return 0
if __name__ == "__main__":
console_entry_point()