Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Commit

Permalink
partial support for DCD files
Browse files Browse the repository at this point in the history
  • Loading branch information
ferchault committed May 27, 2015
1 parent f79f6c9 commit bab4eb3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
41 changes: 40 additions & 1 deletion src/euston/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

# third-party modules
import numpy as np
try:
import MDAnalysis as mda
HAS_MDA = True
except:
HAS_MDA = False

# custom modules
import geometry as geo
Expand Down Expand Up @@ -47,6 +52,13 @@ class HoldsCoordinates(object):
def get_coordinates(self):
pass

class HoldsUnitcell(object):
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def get_h_matrix(self):
pass

def anyopen(filename):
rawname = filename
gzip = '.gz .gzip'.split()
Expand All @@ -57,6 +69,9 @@ def anyopen(filename):
if rawname.endswith('.xyz'):
return XYZ(filename=filename)

if rawname.endswith('.dcd'):
return DCD(filename=filename)

class FileIO(object):
"""Abstract base class for all file objects with support for gzipped input files.."""

Expand Down Expand Up @@ -97,6 +112,30 @@ def _parse(self):
"""Finalises file content parsing."""
self._parsed = True

class DCD(HoldsCoordinates, HoldsUnitcell, FileIO):
@require_loaded
def _parse(self):
if not HAS_MDA:
raise TypeError('MDAnalysis required in order to load a DCD file.')
reader = mda.coordinates.DCD.DCDReader(self._fh.name)
self._hmat = geo.abc_to_hmatrix(*reader.ts.dimensions, degrees=True)
self._coordinates = np.zeros((reader.ts.numatoms, 3))
self._coordinates[:, 0] = reader.ts._x
self._coordinates[:, 1] = reader.ts._y
self._coordinates[:, 2] = reader.ts._z

super(DCD, self)._parse()

@require_loaded
@require_parsed
def get_coordinates(self):
return self._coordinates

@require_loaded
@require_parsed
def get_h_matrix(self):
return self._hmat

class XYZ(HoldsCoordinates, FileIO):
@require_loaded
def count_atoms(self):
Expand Down Expand Up @@ -394,7 +433,7 @@ def get_cell_vectors(self):
return (a, b, c)


class CubeFile(FileIO):
class CubeFile(HoldsUnitcell, FileIO):
def count_atoms(self):
return self._natoms

Expand Down
31 changes: 22 additions & 9 deletions src/tools/es_cellmultiply.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
Supported File Formats
----------------------
====== ====== ====== =========
Format Input Output Extension
====== ====== ====== =========
XYZ Native Native .xyz
====== ====== ====== =========
Most of the file formats require external python packages like MDAnalysis or mdtraj. Only native formats can be
used without any further packages.
====== ===================== ===================== =========
Format Input Output Extension
====== ===================== ===================== =========
XYZ Native Native .xyz
DCD MDAnalysis -- .dcd
====== ===================== ===================== =========
Command Line Interface
----------------------
Expand Down Expand Up @@ -86,10 +90,15 @@ def main(args):
:param args: Arguments as from argparse.ArgumentParser.parse_args
"""

xyz = io.XYZ(args.input)
inputfile = io.anyopen(args.input)
if not isinstance(inputfile, io.HoldsCoordinates):
print 'Input file has to contain atom positions.'

hmat = None
if not (args.sc_in and args.sc_out):
if isinstance(inputfile, io.HoldsUnitcell):
hmat = inputfile.get_h_matrix()

if not (args.sc_in and args.sc_out) and (hmat is None):
if (args.hmat is None and args.abc is None) or (args.hmat is not None and args.abc is not None):
print 'Please specify either the H matrix or lattice constants unless input and output are scaled.'
exit(1)
Expand Down Expand Up @@ -118,11 +127,15 @@ def main(args):
print 'Invalid multiplier setting - has to be at least one.'
exit(4)

multiplied = geo.cell_multiply(xyz.get_coordinates(), args.X, args.Y, args.Z, h_matrix=hmat, scaling_in=args.sc_in, scaling_out=args.sc_out)
multiplied = geo.cell_multiply(inputfile.get_coordinates(), args.X, args.Y, args.Z, h_matrix=hmat, scaling_in=args.sc_in, scaling_out=args.sc_out)
factor = args.X*args.Y*args.Z

output = io.XYZ()
output.set_data(xyz.get_labels()*factor, multiplied)
try:
labels = inputfile.get_labels()
except:
labels = ['X'] * inputfile.get_coordinates().shape[0]
output.set_data(labels*factor, multiplied)
io.write_lines(args.output, output.to_string())

if __name__ == '__main__':
Expand Down
Binary file added tests/data/unscaled.dcd
Binary file not shown.

0 comments on commit bab4eb3

Please sign in to comment.