Skip to content

Commit 7b91c02

Browse files
committed
Convert gninatypes to xyz
1 parent 772cf6b commit 7b91c02

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

types2xyz.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
3+
'''convert gninatypes file to xyz file'''
4+
import struct, sys, argparse
5+
from functools import partial
6+
import molgrid
7+
8+
names = molgrid.GninaIndexTyper().get_type_names()
9+
10+
def elem(t):
11+
'''convert type index into element string'''
12+
name = names[t]
13+
if 'Hydrogen' in name:
14+
return 'H'
15+
elif 'Carbon' in name:
16+
return 'C'
17+
elif 'Nitrogen' in name:
18+
return 'N'
19+
elif 'Oxygen' in name:
20+
return 'O'
21+
elif 'Sulfur' in name:
22+
return 'S'
23+
elif 'Phosphorus' == name:
24+
return 'P'
25+
elif 'Fluorine' == name:
26+
return 'F'
27+
elif 'Chlorine' == name:
28+
return 'Cl'
29+
elif 'Bromine' == name:
30+
return 'Br'
31+
elif 'Iodine' == name:
32+
return 'I'
33+
elif 'Magnesium' == name:
34+
return 'Mg'
35+
elif 'Manganese' == name:
36+
return 'Mn'
37+
elif 'Zinc' == name:
38+
return 'Zn'
39+
elif 'Calcium' == name:
40+
return 'Ca'
41+
elif 'Iron' == name:
42+
return 'Fe'
43+
elif 'Boron' == name:
44+
return 'B'
45+
else:
46+
return 'X'
47+
48+
49+
parser = argparse.ArgumentParser()
50+
parser.add_argument('input',type=str,help='gninatypes file')
51+
parser.add_argument('output', default='-',nargs='?',type=argparse.FileType('w'),help='output xyz')
52+
53+
args = parser.parse_args()
54+
55+
56+
struct_fmt = 'fffi'
57+
struct_len = struct.calcsize(struct_fmt)
58+
struct_unpack = struct.Struct(struct_fmt).unpack_from
59+
60+
with open(args.input,'rb') as tfile:
61+
results = [struct_unpack(chunk) for chunk in iter(partial(tfile.read, struct_len), b'')]
62+
63+
args.output.write('%d\n'%len(results)) # number atoms
64+
args.output.write(args.input+'\n') #comment
65+
for x,y,z,t in results:
66+
args.output.write('%s\t%f\t%f\t%f\n'%(elem(t),x,y,z))
67+

0 commit comments

Comments
 (0)