Skip to content

Commit

Permalink
Add typequote argument to control whether DX array type should be quoted
Browse files Browse the repository at this point in the history
This change works around another example of poorly hard-coded DX parser
(see also issue MDAnalysis#35).

See the code below from the GridForcesGrid.C file in NAMD:
fscanf(poten_fp, "object %*d class array type double rank 0 "
                 "items %*d data follows\n");
which will only work if the quote character (introduced in 115b05a to appease
the PyMol parser) is omitted.
  • Loading branch information
giacomofiorin committed Nov 12, 2018
1 parent 43d398f commit 60998dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
9 changes: 6 additions & 3 deletions gridData/OpenDX.py
Expand Up @@ -301,7 +301,8 @@ class array(DXclass):
# "string" not automatically supported
}

def __init__(self, classid, array=None, type=None, **kwargs):
def __init__(self, classid, array=None, type=None, typequote='"',
**kwargs):
"""
Parameters
----------
Expand Down Expand Up @@ -347,6 +348,7 @@ def __init__(self, classid, array=None, type=None, **kwargs):
"types are: {1}".format(type,
list(self.dx_types.values()))))
self.type = type
self.typequote = typequote

def write(self, file):
"""Write the *class array* section.
Expand All @@ -367,9 +369,10 @@ def write(self, file):
"Supported valus are: {}\n"
"Use the type=<type> keyword argument.").format(
self.type, list(self.dx_types.keys())))
typelabel = (self.typequote+self.type+self.typequote)
DXclass.write(self,file,
'type "{0}" rank 0 items {1} data follows'.format(
self.type, self.array.size))
'type {0} rank 0 items {1} data follows'.format(
typelabel, self.array.size))
# grid data, serialized as a C array (z fastest varying)
# (flat iterator is equivalent to: for x: for y: for z: grid[x,y,z])
# VMD's DX reader requires exactly 3 values per line
Expand Down
20 changes: 15 additions & 5 deletions gridData/core.py
Expand Up @@ -405,7 +405,7 @@ def _load_plt(self, filename):
grid, edges = g.histogramdd()
self.__init__(grid=grid, edges=edges, metadata=self.metadata)

def export(self, filename, file_format=None, type=None):
def export(self, filename, file_format=None, type=None, typequote='"'):
"""export density to file using the given format.
The format can also be deduced from the suffix of the filename
Expand All @@ -424,10 +424,13 @@ def export(self, filename, file_format=None, type=None):
Parameters
----------
filename : str
name of the output file
file_format : {'dx', 'pickle', None} (optional)
output file format, the default is "dx"
type : str (optional)
for DX, set the output DX array type, e.g., "double" or
"float"; note that PyMOL only understands "double" (see
Expand All @@ -437,12 +440,19 @@ def export(self, filename, file_format=None, type=None):
.. versionadded:: 0.4.0
.. _`#35`: https://github.com/MDAnalysis/GridDataFormats/issues/35
typequote : str (optional)
For DX, set the character used to quote the type string;
by default this is a double-quote character, '"'.
Custom parsers like the one from NAMD-GridForces (backend for MDFF)
expect no quotes, and typequote='' may be used to appease them.
.. versionadded:: after 0.4.0
"""
exporter = self._get_exporter(filename, file_format=file_format)
exporter(filename, type=type)
exporter(filename, type=type, typequote=typequote)

# note: the _export_FORMAT() methods all take the filename as a mandatory
# argument. They can process kwargs but they are not required to do
Expand All @@ -461,7 +471,7 @@ def _export_python(self, filename, **kwargs):
with open(filename, 'wb') as f:
cPickle.dump(data, f, cPickle.HIGHEST_PROTOCOL)

def _export_dx(self, filename, type=None, **kwargs):
def _export_dx(self, filename, type=None, typequote='"', **kwargs):
"""Export the density grid to an OpenDX file.
The file format is the simplest regular grid array and it is
Expand Down Expand Up @@ -495,7 +505,7 @@ def _export_dx(self, filename, type=None, **kwargs):
positions=OpenDX.gridpositions(1, self.grid.shape, self.origin,
self.delta),
connections=OpenDX.gridconnections(2, self.grid.shape),
data=OpenDX.array(3, self.grid, type=type),
data=OpenDX.array(3, self.grid, type=type, typequote=typequote),
)
dx = OpenDX.field('density', components=components, comments=comments)
dx.write(filename)
Expand Down

0 comments on commit 60998dd

Please sign in to comment.