-
Notifications
You must be signed in to change notification settings - Fork 85
/
types2xyz.py
executable file
·67 lines (56 loc) · 1.69 KB
/
types2xyz.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
#!/usr/bin/env python3
'''convert gninatypes file to xyz file'''
import struct, sys, argparse
from functools import partial
import molgrid
names = molgrid.GninaIndexTyper().get_type_names()
def elem(t):
'''convert type index into element string'''
name = names[t]
if 'Hydrogen' in name:
return 'H'
elif 'Carbon' in name:
return 'C'
elif 'Nitrogen' in name:
return 'N'
elif 'Oxygen' in name:
return 'O'
elif 'Sulfur' in name:
return 'S'
elif 'Phosphorus' == name:
return 'P'
elif 'Fluorine' == name:
return 'F'
elif 'Chlorine' == name:
return 'Cl'
elif 'Bromine' == name:
return 'Br'
elif 'Iodine' == name:
return 'I'
elif 'Magnesium' == name:
return 'Mg'
elif 'Manganese' == name:
return 'Mn'
elif 'Zinc' == name:
return 'Zn'
elif 'Calcium' == name:
return 'Ca'
elif 'Iron' == name:
return 'Fe'
elif 'Boron' == name:
return 'B'
else:
return 'X'
parser = argparse.ArgumentParser()
parser.add_argument('input',type=str,help='gninatypes file')
parser.add_argument('output', default='-',nargs='?',type=argparse.FileType('w'),help='output xyz')
args = parser.parse_args()
struct_fmt = 'fffi'
struct_len = struct.calcsize(struct_fmt)
struct_unpack = struct.Struct(struct_fmt).unpack_from
with open(args.input,'rb') as tfile:
results = [struct_unpack(chunk) for chunk in iter(partial(tfile.read, struct_len), b'')]
args.output.write('%d\n'%len(results)) # number atoms
args.output.write(args.input+'\n') #comment
for x,y,z,t in results:
args.output.write('%s\t%f\t%f\t%f\n'%(elem(t),x,y,z))