File size: 5,949 Bytes
910db9a | 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | '''Parser for gromacs edr'''
from .base import IOHandler
import numpy as np
import xdrlib
import difflib
class QuantityNotAvailable(Exception):
pass
class EdrIO(IOHandler):
'''EDR files store per-frame information for gromacs
trajectories. Examples of properties obtainable from EDR files are::
- temperature
- pressure
- density
- potential energy
- total energy
- etc.
To know which quantities are available in a certain edr file you
can access the feature 'avail quantity'::
>>> datafile('ener.edr').read('avail quantities')
['Temperature', 'Pressure', 'Potential', ...]
To get the frame information for a certain quantity you may use
the "quantity" property passing the quantity as additional
argument, this will return two arrays, the first is an array of
times in ps and the second are the corrisponding quantities::
>>> time, temp = datafile('ener.edr').read('quantity', 'Temperature')
**Features**
.. method:: read("quantity", quant)
Return an array of times in ps and the corresponding quantities
at that times.
.. method:: read("avail quantities")
Return the available quantities in the file.
.. method:: read("units")
Return a dictionary where the keys are the quantities and
the value are the units in which that quantity is expressed.
.. method:: read("frames")
Return a dictionary where the keys are the quantities and
the value are the units in which that quantity is expressed.
'''
can_read = ['quantity', 'units', 'avail quantities']
can_write = []
def __init__(self, fd):
super(EdrIO, self).__init__(fd)
self.processed = False
def read(self, feature, *args):
self.check_feature(feature, 'read')
if not self.processed:
self.frames = frames = self.process_frames()
self.processed = True
else:
frames = self.frames
if feature == 'quantity':
if not args[0]:
raise Exception('the method read("quantity", arg) requires a quantity to get')
quant = args[0]
if quant not in self.props:
close = difflib.get_close_matches(quant, self.props)
raise QuantityNotAvailable('Quantity %s not available. Close matches: %s'%
(str(quant), str(close)))
i = self.props.index(quant)
ret = []
for f in frames:
ret.append(f[i][0])
return np.array(self.times), np.array(ret)
if feature == 'units':
quant = args[0]
i = self.props.index(quant)
return self.units[i]
if feature == 'avail quantities':
return self.props
def process_frames(self):
f = self.fd.read()
self.up = xdrlib.Unpacker(f)
self.times = []
self.dts = []
self.frames = []
self._unpack_start()
fr = self._unpack_frame()
self.frames.append(fr)
while True:
try:
fr = self._unpack_frame()
except EOFError:
break
self.frames.append(fr)
return self.frames
def _unpack_start(self):
up = self.up
magic = up.unpack_int()
if (magic != -55555):
raise Exception('Format not supported: magic number -55555 not matching')
self.version = up.unpack_int()
# Number of properties
self.nre = up.unpack_int()
self.props = props = []
self.units = units = []
# Strings and units of quantities
for i in range(self.nre):
prop = up.unpack_string()
unit = up.unpack_string()
props.append(prop.decode('utf-8'))
units.append(unit.decode('utf-8'))
def _unpack_eheader(self):
up = self.up
first_real_to_check = -2e10
# Checking the first real for format
first_real = up.unpack_double()
if (first_real != first_real_to_check):
raise Exception('Format not supported, first real not matching.')
magic = up.unpack_int()
if (magic != -7777777):
raise Exception('Format not supported, magic number not matching -7777777')
version = up.unpack_int()
time = up.unpack_double()
self.times.append(time)
# This two should give us large int that represent the step number
min = up.unpack_int()
maj = up.unpack_int()
self.nsum = up.unpack_int()
# NSTEPS (again?)
min = up.unpack_int()
maj = up.unpack_int()
# For version 5
dt = up.unpack_double()
self.dts.append(dt)
# Number of properties?
self.nre = up.unpack_int()
dum = up.unpack_int()
nblock = up.unpack_int() + 1
# Block headers:
id = up.unpack_int()
nsubblocks = up.unpack_int()
e_size = up.unpack_int()
#dum = up.unpack_int()
#dum = up.unpack_int()
#up.unpack_int()
def _unpack_frame(self):
# Energies, averages and rmsd
self._unpack_eheader()
frame = []
for i in range(self.nre):
en = self.up.unpack_double()
if self.nsum > 0:
avg = self.up.unpack_double()
rmsd = self.up.unpack_double()
frame.append([en, avg, rmsd])
else:
frame.append([en, en, 0.0])
return frame |