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 Parse Amber restart files.
27 """
28
29 import re
30 import numpy.oldnumeric as N
31 import os.path
32
33 from AmberCrdParser import ParseError
34 from PDBModel import PDBModel
35 import tools as T
36
38 """Convert an Amber restart file to array, PDBModel or a Amber crd file.
39
40 Note: AmberRstParser is currently ignoring both the velocity and
41 boxinfo record (although this could be easily changed).
42 """
43
45 """
46 @param frst: input restart file
47 @type frst: str
48 """
49 self.frst = T.absfile( frst )
50 self.crd = open( self.frst )
51
52 self.n = 0
53 self.lines_per_frame = 0
54 self.xyz = None
55 self.box = None
56
57
58 xnumber = "-*\d+\.\d+"
59 xspace = ' *'
60 self.xnumbers = re.compile('('+xspace+xnumber+')')
61
62
64 try:
65 self.crd.close()
66 except:
67 pass
68
69
71 """Extract next line of coordinates from crd file
72
73 @return: coordinates
74 @rtype: [float]
75 """
76 l = self.crd.readline()
77 if l == '':
78 raise EOFError('EOF')
79
80 match = self.xnumbers.findall( l )
81 return [ round( float(strCrd),7) for strCrd in match ]
82
83
85 """Collect next complete coordinate frame
86
87 @return: coordinate frame
88 @rtype: array
89 """
90 self.xyz = [ self.__nextLine() for i in range(self.lines_per_frame) ]
91
92 return N.reshape(self.xyz, ( self.n, 3 ) ).astype(N.Float32)
93
94
96 """Get coordinate array.
97
98 @return: coordinates, N.array( N x 3, 'f')
99 @rtype: array
100
101 @raise ParseError: if can't interprete second line
102 """
103 if not self.xyz:
104
105
106 self.crd.readline()
107
108 try:
109 self.n, self.time = self.crd.readline().split()
110 self.n = int( self.n )
111 self.time = float( self.time )
112 except:
113 raise ParseError("Can't interprete second line of "+self.frst)
114
115
116 self.lines_per_frame = self.n / 2
117 if self.n % 2 != 0:
118 self.lines_per_frame += 1
119
120 self.xyz = self.__frame()
121
122 return self.xyz
123
124
126 """
127 Get model.
128
129 @param ref: reference with same number and order of atoms
130 @type ref: PDBModel
131 @param rnAmber: rename Amber to standard residues (HIE, HID, HIP, CYX)
132 @type rnAmber: 1|0
133
134 @return: PDBModel
135 @rtype: PDBModel
136 """
137 if not self.xyz:
138 self.getXyz()
139
140 result = ref.clone()
141 result.setXyz( self.xyz )
142 if rnAmber:
143 result.renameAmberRes()
144
145 return result
146
147
149 """
150 Return the first line of Amber crd.
151
152 @return: first line of Amber crd formatted coordinate block
153 @rtype: str
154 """
155 if not self.xyz:
156 self.getXyz()
157
158 result = ""
159 for x in N.ravel( self.xyz )[:10]:
160 result += "%8.3f" % x
161
162 return result + "\n"
163
164
165 - def writeCrd( self, fcrd, append=1, lastAtom=None ):
166 """
167 Write/Append Amber-formatted block of coordinates to a file.
168 If a file handle is given, the file will not be closed.
169
170 @param fcrd: file to write to
171 @type fcrd: str or file object
172 @param append: append to existing file (default: 1)
173 @type append: 0|1
174 @param lastAtom: skip all atoms beyond this one (default: None)
175 @type lastAtom: int
176 """
177 if not self.xyz:
178 self.getXyz()
179
180 if type( fcrd ) == file:
181
182 f = fcrd
183 else:
184
185 mode = 'w'
186 if append:
187 mode = 'a'
188 f = open( T.absfile( fcrd ), mode )
189
190 newf = (mode=='w' or not os.path.exists( T.absfile(fcrd) ))
191 if newf:
192 f.write("\n")
193
194 i = 0
195 for x in N.ravel( self.xyz ):
196 i = i + 1
197
198 f.write( "%8.3f" % x )
199
200 if (i % 10) == 0:
201 f.write("\n")
202
203 if lastAtom and i / 3.0 == lastAtom:
204 break
205
206 if ((i % 10) != 0):
207 f.write("\n")
208
209 if type( fcrd ) != file:
210
211 f.close()
212
213
214
215 import Biskit.test as BT
216
217 -class Test(BT.BiskitTest):
218 """Test AmberRstParser"""
219
225
227 """AmberRstParser.getXyz test"""
228 self.xyz = self.p.getXyz()
229 self.assertEqual( N.shape(self.xyz), (11200,3) )
230
232 """long AmberRstParser test"""
233 TAGS = [BT.LONG]
234
236 """AmberRstParser.getModel test"""
237 self.ref = PDBModel( self.fref )
238 self.model = self.p.getModel( self.ref )
239 self.assertEqual( len(self.model), 11200 )
240
241
242 if __name__ == '__main__':
243
244
245 BT.localTest( )
246