-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsanitize.py
executable file
·71 lines (56 loc) · 2.28 KB
/
sanitize.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
#!/usr/bin/env python3
import argparse
EXPECTED_TOKS = {
"bonds": 5,
"angles": 6,
"dihedrals": 8,
"constraints": 4,
"pairs": -1
}
def main(args):
with open(args.input) as infile, open(args.output, "w") as outfile:
section = ""
has_constr = False
constr_lines = []
for line in infile:
line = line.strip("\n")
if line.startswith("["):
section = line.strip("[ ]")
if args.constraint_threshold is not None and section == "constraints":
print(line, file=outfile)
has_constr = True
for c_line in constr_lines:
print(c_line, file=outfile)
continue
if section == "system" and not has_constr and constr_lines:
print("[ constraints ]", file=outfile)
for c_line in constr_lines:
print(c_line, file=outfile)
print(file=outfile)
has_constr = True
print(line, file=outfile)
continue
if line.startswith("#") or line.startswith(";") or line == "":
print(line, file=outfile)
continue
if len(line.split()) < EXPECTED_TOKS.get(section, 0):
continue
if EXPECTED_TOKS.get(section, 0) < 0:
continue
# Convert high force constants to constraints
if (args.constraint_threshold is not None and section == "bonds"
and float(line.split()[4]) >= args.constraint_threshold):
constr_lines.append(line[:line.rindex(line.split()[4]) - 1])
continue
print(line, file=outfile)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Sanitize PDB2GMX output topology.")
parser.add_argument('input', type=str, help="Input topology file")
parser.add_argument('output', type=str, help="Output topology file")
parser.add_argument('-c',
'--constraint-threshold',
type=float,
default=None,
help="Convert high force constants to constraints")
main(parser.parse_args())