Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes LAMMPSDataConverter #2569

Merged
merged 3 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Enhancements
* Improve the distance search in water bridge analysis with capped_distance (PR #2480)

Changes
* Deprecated :class:`LAMMPSDataConverter` has now been removed (Issue #2564)
* Removed AtomGroup stubs (PR #1070, Issue #2443).
* encore.hes() doesn't accept the details keyword anymore, it always returns
the relevant details instead, consistently with ces() and dres(), in the
Expand Down
190 changes: 5 additions & 185 deletions package/MDAnalysis/topology/LAMMPSParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
.. _data: DATA file format: :http://lammps.sandia.gov/doc/2001/data_format.html
.. _dump: http://lammps.sandia.gov/doc/dump.html

.. versionchanged:: 1.0.0
Deprecated :class:`LAMMPSDataConverter` has now been removed.


.. _atom_style_kwarg:

Expand Down Expand Up @@ -70,11 +73,6 @@
.. autoclass:: LammpsDumpParser
:members:

Deprecated classes
------------------

.. autoclass:: LAMMPSDataConverter
:members:

"""
from __future__ import absolute_import, print_function
Expand Down Expand Up @@ -673,183 +671,5 @@ def __getattr__(self, attr):

def __iter__(self):
pos = self.pos
return iter((self.index + 1, self.chainid, self.type, self.charge, self.mass, pos[0], pos[1], pos[2]))


class LAMMPSDataConverter(object): # pragma: no cover
"""Class to parse a LAMMPS_ DATA file and convert it to PSF/PDB.

The DATA file contains both topology and coordinate information.

The :class:`LAMMPSDataConverter` class can extract topology information and
coordinates from a LAMMPS_ data file. For instance, in order to
produce a PSF file of the topology and a PDB file of the coordinates
from a data file "lammps.data" you can use::

from MDAnalysis.topology.LAMMPSParser import LAMPPSData
d = LAMMPSDataConverter("lammps.data")
d.writePSF("lammps.psf")
d.writePDB("lammps.pdb")

You can then read a trajectory (e.g. a LAMMPS DCD, see
:class:`MDAnalysis.coordinates.LAMMPS.DCDReader`) with ::

u = MDAnalysis.Unverse("lammps.psf", "lammps.dcd", format="LAMMPS")

.. deprecated:: 0.9.0

.. versionchanged:: 0.9.0
Renamed from ``LAMMPSData`` to ``LAMMPSDataConverter``.
"""
header_keywords = [
"atoms", "bonds", "angles", "dihedrals", "impropers",
"atom types", "bond types", "angle types",
"dihedral types", "improper types",
"xlo xhi", "ylo yhi", "zlo zhi"]

connections = dict([
["Bonds", ("bonds", 3)],
["Angles", ("angles", 3)],
["Dihedrals", ("dihedrals", 4)],
["Impropers", ("impropers", 2)]])

coeff = dict([
["Masses", ("atom types", 1)],
["Velocities", ("atoms", 3)],
["Pair Coeffs", ("atom types", 4)],
["Bond Coeffs", ("bond types", 2)],
["Angle Coeffs", ("angle types", 4)],
["Dihedral Coeffs", ("dihedral types", 3)],
["Improper Coeffs", ("improper types", 2)]])

def __init__(self, filename=None):
self.names = {}
self.headers = {}
self.sections = {}
if filename is None:
self.title = "LAMMPS data file"
else:
# Open and check validity
with openany(filename) as file:
file_iter = file.xreadlines()
self.title = file_iter.next()
# Parse headers
headers = self.headers
for l in file_iter:
line = l.strip()
if len(line) == 0:
continue
found = False
for keyword in self.header_keywords:
if line.find(keyword) >= 0:
found = True
values = line.split()
if keyword in ("xlo xhi", "ylo yhi", "zlo zhi"):
headers[keyword] = (float(values[0]), float(values[1]))
else:
headers[keyword] = int(values[0])
if found is False:
break

# Parse sections
# XXX This is a crappy way to do it
with openany(filename) as file:
file_iter = file.xreadlines()
# Create coordinate array
positions = np.zeros((headers['atoms'], 3), np.float64)
sections = self.sections
for l in file_iter:
line = l.strip()
if len(line) == 0:
continue
if line in self.coeff:
h, numcoeff = self.coeff[line]
# skip line
file_iter.next()
data = []
for i in range(headers[h]):
fields = file_iter.next().strip().split()
data.append(tuple([conv_float(el) for el in fields[1:]]))
sections[line] = data
elif line in self.connections:
h, numfields = self.connections[line]
# skip line
file_iter.next()
data = []
for i in range(headers[h]):
fields = file_iter.next().strip().split()
data.append(tuple(np.int64(fields[1:])))
sections[line] = data
elif line == "Atoms":
file_iter.next()
data = []
for i in range(headers["atoms"]):
fields = file_iter.next().strip().split()
index = int(fields[0]) - 1
a = LAMMPSAtom(index=index, name=fields[2], type=int(fields[2]), chain_id=int(fields[1]),
charge=float(fields[3]))
a._positions = positions
data.append(a)
positions[index] = np.array([float(fields[4]), float(fields[5]), float(fields[6])])
sections[line] = data
elif line == "Masses":
file_iter.next()
data = []
for i in range(headers["atom type"]):
fields = file_iter.next().strip().split()
print("help")
self.positions = positions

def writePSF(self, filename, names=None):
"""Export topology information to a simple PSF file."""
# Naveen formatted -- works with MDAnalysis verison 52
#psf_atom_format = " %5d %-4s %-4d %-4s %-4s %-4s %10.6f %7.4f %1d\n"
# Liz formatted -- works with MDAnalysis verison 59
#psf_atom_format = "%8d %4.4s %-4.4s %-4.4s %-4.4s %-4.4s %16.8e %1s %-7.4f %7.7s %s\n"
# Oli formatted -- works with MDAnalysis verison 81
psf_atom_format = "%8d %4s %-4s %4s %-4s% 4s %-14.6f%-14.6f%8s\n"
with openany(filename, 'w') as file:
file.write("PSF\n\n")
file.write(string.rjust('0', 8) + ' !NTITLE\n\n')
file.write(string.rjust(str(len(self.sections["Atoms"])), 8) + ' !NATOM\n')
#print self.sections["Masses"]
for i, atom in enumerate(self.sections["Atoms"]):
if names is not None:
resname, atomname = names[i]
else:
resname, atomname = 'TEMP', 'XXXX'
for j, liz in enumerate(self.sections["Masses"]):
liz = liz[0]
#print j+1, atom.type, liz
if j + 1 == atom.type:
line = [
i + 1, 'TEMP',
str(atom.chainid), resname, atomname, str(atom.type + 1), atom.charge,
float(liz), 0.]
else:
continue
#print line
file.write(psf_atom_format % tuple(line))

file.write("\n")
num_bonds = len(self.sections["Bonds"])
bond_list = self.sections["Bonds"]
file.write(string.rjust(str(num_bonds), 8) + ' !NBOND\n')
for index in range(0, num_bonds, 4):
try:
bonds = bond_list[index:index + 4]
except IndexError:
bonds = bond_list[index:-1]
bond_line = [string.rjust(str(bond[1]), 8) + string.rjust(str(bond[2]), 8) for bond in bonds]
file.write(''.join(bond_line) + '\n')

def writePDB(self, filename):
"""Export coordinates to a simple PDB file."""
atom_format = "%6s%.5s %4s %4s %.4s %8.3f%8.3f%8.3f%6.2f%6.2f %2s \n"
p = self.positions
with openany(filename, 'w') as file:
for i, atom in enumerate(self.sections["Atoms"]):
line = [
"ATOM ", str(i + 1), 'XXXX', 'TEMP', str(atom.type + 1), p[i, 0], p[i, 1], p[i, 2], 0.0, 0.0,
str(atom.type)]
file.write(atom_format % tuple(line))
return iter((self.index + 1, self.chainid, self.type, self.charge,
self.mass, pos[0], pos[1], pos[2]))