Commit 3cc6a186 authored by peastman's avatar peastman Committed by GitHub
Browse files

Merge pull request #1536 from peastman/filewarning

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