Package gromacs :: Module formats :: Class NDX
[hide private]
[frames] | no frames]

Class NDX

source code

     object --+        
              |        
           dict --+    
                  |    
        odict.odict --+
                      |
         object --+   |
                  |   |
utilities.FileUtils --+
                      |
                     NDX
Known Subclasses:

Gromacs index file.

Represented as a ordered dict where the keys are index group names and
values are numpy arrays of atom numbers.

Use the :meth:`NDX.read` and :meth:`NDX.write` methods for
I/O. Access groups by name via the :meth:`NDX.get` and
:meth:`NDX.set` methods.

Alternatively, simply treat the :class:`NDX` instance as a
dictionary. Setting a key automatically transforms the new value
into a integer 1D numpy array (*not* a set, as would be the
:program:`make_ndx` behaviour).

.. Note:: The index entries themselves are ordered and can contain 
          duplicates so that output from NDX can be easily used for 
          :program:`g_dih` and friends. If you need set-like behaviour
          you will have do use :class:`gromacs.formats.uniqueNDX` or
          :class:`gromacs.cbook.IndexBuilder` (which uses
          :program:`make_ndx` throughout).

**Example**

  Read index file, make new group and write to disk::

    ndx = NDX()
    ndx.read('system.ndx')
    print ndx['Protein']       
    ndx['my_group'] = [2, 4, 1, 5]   # add new group
    ndx.write('new.ndx')

  Or quicker (replacing the input file ``system.ndx``)::

    ndx = NDX('system')          # suffix .ndx is automatically added
    ndx['chi1'] = [2, 7, 8, 10]
    ndx.write()

Instance Methods [hide private]
new empty dictionary

__init__(self, filename=None, **kwargs)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
read(self, filename=None)
Read and parse index file filename.
source code
 
write(self, filename=None, ncol=15, format='%6d')
Write index file to filename (or overwrite the file that the index was read from)
source code
D[k] if k in D, else d
get(self, name)
Return index array for index group name.
source code
 
set(self, name, value)
Set or add group name as a 1D numpy array.
source code
 
size(self, name)
Return number of entries for group name.
source code
 
_getarray(self, name)
Helper getter that is used in write().
source code
 
_transform(self, v)
Transform input to the stored representation.
source code
 
__setitem__(self, k, v)
x[i]=y
source code
D.get(k,d), also set D[k]=d if k not in D
setdefault(*args, **kwargs) source code

Inherited from odict.odict: __cmp__, __copy__, __deepcopy__, __delitem__, __eq__, __getstate__, __iter__, __ne__, __repr__, __reversed__, __setstate__, byindex, clear, copy, index, items, iteritems, iterkeys, itervalues, keys, pop, popitem, reverse, sort, update, values

Inherited from dict: __contains__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __le__, __len__, __lt__, __new__, has_key

Inherited from utilities.FileUtils: check_file_exists, filename, infix_filename

Inherited from utilities.FileUtils (private): _init_filename

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__, __str__

Class Methods [hide private]

Inherited from odict.odict: fromkeys

Class Variables [hide private]
  default_extension = 'ndx'
Default extension for files read/written by this class.
  SECTION = re.compile(r'\s*\[\s*(?P<name>\S.*\S)\s*\]\s*')
  ncol = 15
standard ndx file format: 15 columns
  format = '%6d'
standard ndx file format: '%6d'
Instance Variables [hide private]

Inherited from utilities.FileUtils: real_filename

Properties [hide private]
  groups
Return a list of all groups.
  sizes
Return a dict with group names and number of entries,
  ndxlist
Return a list of groups in the same format as :func:`gromacs.cbook.get_ndx_groups`.

Inherited from object: __class__

Method Details [hide private]

__init__(self, filename=None, **kwargs)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

get(self, name)

source code 
Return index array for index group name.
Returns: D[k] if k in D, else d
Overrides: dict.get

_getarray(self, name)

source code 
Helper getter that is used in write(). Override when using a _transform that stores something that cannot be indexed, e.g. when using set()s.

_transform(self, v)

source code 

Transform input to the stored representation.

Override eg with return set(v) for index lists as sets.

__setitem__(self, k, v)
(Index assignment operator)

source code 

x[i]=y

Overrides: dict.__setitem__
(inherited documentation)

setdefault(*args, **kwargs)

source code 
Returns: D.get(k,d), also set D[k]=d if k not in D
Overrides: dict.setdefault
(inherited documentation)

Property Details [hide private]

groups

Return a list of all groups.
Get Method:
unreachable.groups(self) - Return a list of all groups.

sizes

Return a dict with group names and number of entries,
Get Method:
unreachable.sizes(self) - Return a dict with group names and number of entries,

ndxlist

Return a list of groups in the same format as  :func:`gromacs.cbook.get_ndx_groups`.

Format:
   [ {'name': group_name, 'natoms': number_atoms, 'nr':  # group_number}, ....]

Get Method:
unreachable.ndxlist(self) - Return a list of groups in the same format as :func:`gromacs.cbook.get_ndx_groups`.