-
Notifications
You must be signed in to change notification settings - Fork 59
/
ff2xml
executable file
·109 lines (95 loc) · 3.78 KB
/
ff2xml
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
#!/usr/bin/env python
import argparse
import xml.etree.ElementTree as ET
class forcefield(object):
'''force field parameter database'''
def __init__(self, filename):
self.ftree = ET.ElementTree(ET.Element('ForceField'))
root = self.ftree.getroot()
for line in open(filename, 'r'):
if line.startswith('#') or line.strip() == '':
continue
if line.lower().startswith('atom'):
section = 'atoms'
atoms = ET.SubElement(root, 'Atoms')
continue
elif line.lower().startswith('bond'):
section = 'bonds'
bonds = ET.SubElement(root, 'Bonds')
continue
elif line.lower().startswith('angl'):
section = 'angles'
angles = ET.SubElement(root, 'Angles')
continue
elif line.lower().startswith('dihe'):
section = 'dihedrals'
if not root.find('Torsions'):
torsions = ET.SubElement(root, 'Torsions')
continue
elif line.lower().startswith('impro'):
section = 'improper'
if not root.find('Torsions'):
torsions = ET.SubElement(root, 'Torsions')
continue
tok = line.strip().split()
if section == 'atoms':
at = ET.SubElement(atoms, 'Atom')
at.set('type', tok[0])
at.set('class', tok[1])
at.set('mass', tok[2])
at.set('charge', tok[3])
at.set('potential', tok[4])
at.set('sigma', tok[5])
at.set('epsilon', tok[6])
elif section == 'bonds':
bd = ET.SubElement(bonds, 'Bond')
bd.set('class1', tok[0])
bd.set('class2', tok[1])
bd.set('potential', tok[2])
bd.set('length', tok[3])
bd.set('k', tok[4])
elif section == 'angles':
an = ET.SubElement(angles, 'Angle')
an.set('class1', tok[0])
an.set('class2', tok[1])
an.set('class3', tok[2])
an.set('potential', tok[3])
an.set('angle', tok[4])
an.set('k', tok[5])
elif section == 'dihedrals':
di = ET.SubElement(torsions, 'Proper')
di.set('class1', tok[0])
di.set('class2', tok[1])
di.set('class3', tok[2])
di.set('class4', tok[3])
di.set('potential', tok[4])
di.set('v1', tok[5])
di.set('v2', tok[6])
di.set('v3', tok[7])
di.set('v4', tok[8])
elif section == 'improper':
im = ET.SubElement(torsions, 'Improper')
im.set('class1', tok[0])
im.set('class2', tok[1])
im.set('class3', tok[2])
im.set('class4', tok[3])
im.set('potential', tok[4])
im.set('v1', tok[5])
im.set('v2', tok[6])
im.set('v3', tok[7])
im.set('v4', tok[8])
def write(self, outfile):
'''write force field to xml file'''
ET.indent(self.ftree.getroot(), space=' ')
self.ftree.write(outfile)
def main():
parser = argparse.ArgumentParser(description =
'convert force field file to xml',
formatter_class = argparse.RawTextHelpFormatter)
parser.add_argument('infile', help = 'force field file')
parser.add_argument('outfile', help = 'output OpenMM xml file')
args = parser.parse_args()
ff = forcefield(args.infile)
ff.write(args.outfile)
if __name__ == '__main__':
main()