Commit 258ef062 authored by peastman's avatar peastman
Browse files

Eliminated warnings about unclosed files

parent 01f9e415
......@@ -81,51 +81,51 @@ class CharmmCrdFile(object):
def _parse(self, fname):
crdfile = open(fname, 'r')
line = crdfile.readline()
while len(line.strip()) == 0: # Skip whitespace, as a precaution
line = crdfile.readline()
intitle = True
while intitle:
self.title.append(line.strip())
with open(fname, 'r') as crdfile:
line = crdfile.readline()
if len(line.strip()) == 0:
intitle = False
elif line.strip()[0] != '*':
intitle = False
else:
intitle = True
while len(line.strip()) == 0: # Skip whitespace
line = crdfile.readline()
try:
self.natom = int(line.strip().split()[0])
for row in range(self.natom):
line = crdfile.readline().strip().split()
self.atomno.append(int(line[0]))
self.resno.append(int(line[1]))
self.resname.append(line[2])
self.attype.append(line[3])
pos = Vec3(float(line[4]), float(line[5]), float(line[6]))
self.positions.append(pos)
self.segid.append(line[7])
self.resid.append(int(line[8]))
self.weighting.append(float(line[9]))
if self.natom != len(self.positions):
raise CharmmFileError("Error parsing CHARMM .crd file: %d "
"atoms requires %d positions (not %d)" %
(self.natom, self.natom,
len(self.positions))
)
except (ValueError, IndexError) as e:
raise CharmmFileError('Error parsing CHARMM coordinate file')
while len(line.strip()) == 0: # Skip whitespace, as a precaution
line = crdfile.readline()
intitle = True
while intitle:
self.title.append(line.strip())
line = crdfile.readline()
if len(line.strip()) == 0:
intitle = False
elif line.strip()[0] != '*':
intitle = False
else:
intitle = True
while len(line.strip()) == 0: # Skip whitespace
line = crdfile.readline()
try:
self.natom = int(line.strip().split()[0])
for row in range(self.natom):
line = crdfile.readline().strip().split()
self.atomno.append(int(line[0]))
self.resno.append(int(line[1]))
self.resname.append(line[2])
self.attype.append(line[3])
pos = Vec3(float(line[4]), float(line[5]), float(line[6]))
self.positions.append(pos)
self.segid.append(line[7])
self.resid.append(int(line[8]))
self.weighting.append(float(line[9]))
if self.natom != len(self.positions):
raise CharmmFileError("Error parsing CHARMM .crd file: %d "
"atoms requires %d positions (not %d)" %
(self.natom, self.natom,
len(self.positions))
)
except (ValueError, IndexError) as e:
raise CharmmFileError('Error parsing CHARMM coordinate file')
# Apply units to the positions now. Do it this way to allow for
# (possible) numpy functionality in the future.
......
......@@ -178,23 +178,23 @@ class CharmmPsfFile(object):
if not os.path.exists(psf_name):
raise IOError('Could not find PSF file %s' % psf_name)
# Open the PSF and read the first line. It must start with "PSF"
psf = open(psf_name, 'r')
line = psf.readline()
if not line.startswith('PSF'):
raise CharmmPSFError('Unrecognized PSF file. First line is %s' %
line.strip())
# Store the flags
psf_flags = line.split()[1:]
# Now get all of the sections and store them in a dict
psf.readline()
# Now get all of the sections
psfsections = _ZeroDict()
while True:
try:
sec, ptr, data = CharmmPsfFile._parse_psf_section(psf)
except CharmmPsfEOF:
break
psfsections[sec] = (ptr, data)
with open(psf_name, 'r') as psf:
line = psf.readline()
if not line.startswith('PSF'):
raise CharmmPSFError('Unrecognized PSF file. First line is %s' %
line.strip())
# Store the flags
psf_flags = line.split()[1:]
# Now get all of the sections and store them in a dict
psf.readline()
# Now get all of the sections
psfsections = _ZeroDict()
while True:
try:
sec, ptr, data = CharmmPsfFile._parse_psf_section(psf)
except CharmmPsfEOF:
break
psfsections[sec] = (ptr, data)
# store the title
title = psfsections['NTITLE'][1]
# Next is the number of atoms
......
......@@ -165,7 +165,8 @@ class TestCharmmFiles(unittest.TestCase):
#out = open('systems/ala-ala-ala-implicit-forces/'+file[i]+'.xml', 'w')
#out.write(XmlSerializer.serialize(state1))
#out.close()
state2 = XmlSerializer.deserialize(open('systems/ala-ala-ala-implicit-forces/'+file[i]+'.xml').read())
with open('systems/ala-ala-ala-implicit-forces/'+file[i]+'.xml') as xml:
state2 = XmlSerializer.deserialize(xml.read())
for f1, f2, in zip(state1.getForces().value_in_unit(kilojoules_per_mole/nanometer), state2.getForces().value_in_unit(kilojoules_per_mole/nanometer)):
diff = norm(f1-f2)
self.assertTrue(diff < 0.1 or diff/norm(f1) < 1e-4)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment