gaddlemaps.parsers package
Module contents
Simulation Files Parsers
This submodule contains some useful functionalities to read and extract information from files coming from molecular simulation programs. Up to now, only files in the format used by GROMACS are contemplated (.gro and .itp formats).
- class gaddlemaps.parsers.CoordinatesParser(path, mode='r')[source]
Bases:
object
- Attributes
- EXTENSIONS
box_matrix
Return a 3x3 matrix with the 3 lattice vectors
comment
A comment/name of the system
natoms
Number of atoms in the file
Methods
close
()Closes the file
next
()Returns next atomline formatted
seek_atom
(index)Displaces the position of the 'cursor' to an atom line
writeline
(atomlist)Writes a line of atom information
writelines
(list_atomlist)Writes several lines of atoms
- abstract seek_atom(index)[source]
Displaces the position of the ‘cursor’ to an atom line
Displaces the position of the ‘cursor’ to the beginning of the line of the ‘index’ atom, where the first atom index is 0. This function warrants that calling the “next” function the information about atom “index” will be returned.
- Parameters
index (int) – The index of the atom to found
- abstract writeline(atomlist)[source]
Writes a line of atom information
If there was no content written in the file it creates the header and the number of atoms. If the number of atoms was not provided it will kept empty and the number will be written just before closing the file. This possibility is only compatible with a number of atoms smaller than 1000000.
- Parameters
atomlist (list or string) – A list with all the info of the atom, just like the one returned by readline. If it is a string, it will be written directly without parsing.
- class gaddlemaps.parsers.GroFile(path, mode='r')[source]
Bases:
CoordinatesParser
Implements a file object for opening and writing .gro files
Grofile verifies the gromacs format, and autodetects the correct way to read the file. It modifies the methods read and write of a common file with methods that take as input lists with all the info of an atom. The readlines and writelines are modified to take (return) lists with the info of an atom in lists (as returned by the read function).
This class can also be initiated with an already opened file.
Note
The “+” modifier for the open mode is not allowed right now
- Parameters
- Raises
IOError – If the gro file has not the correct format
- Attributes
box_matrix
numpy.ndarray(3,3): the 3 lattice vectors of the box.
comment
string: The comment of the .gro file.
name
str: The filename of the open file
natoms
integer: The number of atoms in the system.
position_format
tuple of int : the number of total figures and decimal places for the
Methods
close
()Closes the file
determine_format
(atomline)Returns the format of the postion coordinates and whether or not the .gro file has velocities
next
()Returns next atomline formatted
parse_atomline
(atomline[, format_dict])Parses an atom line and returns its content in a list.
parse_atomlist
(atomlist[, format_dict])Convert a list of atom info to string with the appropriate format
readline
([parsed])Returns the next line of the gro file.
Returns all the lines of a grofile parsed in lists.
seek_atom
(index)Displaces the position of the 'cursor' to an atom line
validate_string
(string)Validates a string to be valid as resname or as name.
writeline
(atomlist)Writes a line of atom information
writelines
(list_atomlist)Writes several lines of atoms
- COORD_START = 20
- DEFAULT_COMMENT = "Gro file genereted with 'Gromacs Tools' python module."
- DEFAULT_POSTION_FORMAT = (8, 3)
- NUMBER_FIGURES = 9
- property box_matrix: ndarray
the 3 lattice vectors of the box.
- Type
numpy.ndarray(3,3)
- Return type
- close()[source]
Closes the file
If the file is in write mode it will write al the remaining information.
- classmethod determine_format(atomline)[source]
Returns the format of the postion coordinates and whether or not the .gro file has velocities
- Parameters
atomline (str) – An atom line from the .gro file
- Returns
form – Dictionary with 2 keys, “position” and “velocities”. The position key leads to a tuple with 2 ints. THe first in is the number of characters in the float format (C) and the second is the number of decimal places in the float (D), i.e. corresponds to the ‘%C.Df’ format.
- Return type
- classmethod parse_atomline(atomline, format_dict=None)[source]
Parses an atom line and returns its content in a list.
Parses an atom line and returns its content in a list. In its default behaviour the input line is analyzed and the number of decimals are guessed. It is also possible to insert the format in a format dictionary. This is a dictionary with 2 keys, “position” and “velocities”. The position key leads to a tuple with 2 ints. The first in is the number of characters in the float format (C) and the second is the number of decimal places in the float (D), i.e. corresponds to the ‘%C.Df’ format.
- Parameters
atomline (str) – An string with a line from a grofile
format_dict (dict_format (optional)) – None or dictionary with 2 keys, “position” and “velocities”. The position key leads to a tuple with 2 ints. THe first in is the number of characters in the float format (C) and the second is the number of decimal places in the float (D), i.e. corresponds to the ‘%C.Df’ format. If None the format will be automatically guessed. Default None.
- Returns
info –
- A list with the following information:
mol_index(integer)
resname (string)
name (string)
global_index (integer)
x, y, z (floats)
vx, vy ,vz (floats) [optional]
- Return type
- classmethod parse_atomlist(atomlist, format_dict=None)[source]
Convert a list of atom info to string with the appropriate format
Parses a list with the atom information and returns its content in a string with the correct format to write in a .gro file. In its default behaviour the input line is analyzed and the number of decimals are guessed. It is also possible to insert the format in a format dictionary. This is a dictionary with 2 keys, “position” and “velocities”. The position key leads to a tuple with 2 ints. The first is the number of characters in the float format (C) and the second is the number of decimal places in the float (D), i.e. corresponds to the ‘%C.Df’ format.
- Parameters
atomlist (list) –
- A list with the following information:
mol_index(integer)
resname (string)
name (string)
global_index (integer)
x, y, z (floats)
vx, vy ,vz (floats) [optional]]
format_dict (dict (optional)) – None or dictionary with 2 keys, “position” and “velocities”. The position key leads to a tuple with 2 ints. THe first in is the number of characters in the float format (C) and the second is the number of decimal places in the float (D), i.e. corresponds to the ‘%C.Df’ format. If None the format will be automatically guessed. Default None.
- Returns
atomline – An string with a line from a .gro file
- Return type
- property position_format: Tuple[int, int]
the number of total figures and decimal places for the positions.
- Type
tuple of int
- readline(parsed=True)[source]
Returns the next line of the gro file.
If parsed=True (default) the data on the atom line is parsed and returned in a list. If false the line is returned as an string (just like if it were opened as a common file.)
- seek_atom(index)[source]
Displaces the position of the ‘cursor’ to an atom line
Displaces the position of the ‘cursor’ to the beginning of the line of the ‘index’ atom, where the first atom index is 0. If the index is equal to the number of atoms the beginning of the box lattice line is found.
- Parameters
index (int) – The index of the atom to found
- static validate_string(string)[source]
Validates a string to be valid as resname or as name.
If the input name has more than 5 characters, new name is returned cutting the input one.
- writeline(atomlist)[source]
Writes a line of atom information
If there was no content written in the file it creates the header and the number of atoms. If the number of atoms was not provided it will kept empty and the number will be written just before closing the file. This possibility is only compatible with a number of atoms smaller than 1000000.
- Parameters
atomlist (list or string) – A list with all the info of the atom, just like the one returned by readline. If it is a string, it will be written directly without parsing.
- writelines(list_atomlist)[source]
Writes several lines of atoms
If there was no content written in the file it creates the header and the number of atoms. If the number of atoms was not provided it will kept empty and the number will be written just before closing the file. This possibility is only compatible with a number of atoms smaller than 1000000.
- class gaddlemaps.parsers.ParserManager[source]
Bases:
object
Methods
register
- parsers: Dict[str, Type[CoordinatesParser]] = {'GRO': <class 'gaddlemaps.parsers.GroFile'>, 'gro': <class 'gaddlemaps.parsers.GroFile'>}
- class gaddlemaps.parsers.ParserRegistered(name, bases, attrs)[source]
Bases:
ABCMeta
Methods
__call__
(*args, **kwargs)Call self as a function.
mro
(/)Return a type's method resolution order.
register
(subclass)Register a virtual subclass of an ABC.
- mro(/)
Return a type’s method resolution order.
- register(subclass)
Register a virtual subclass of an ABC.
Returns the subclass, to allow usage as a class decorator.
- gaddlemaps.parsers.dump_lattice_gro(vectors)[source]
Extracts final line of a .gro file from the lattice vectors for pbc.
- Parameters
vectors (numpy.ndarray((3,3))) – An array with a lattice vector in each row.
- Returns
line – The final line of a .gro file.
- Return type
string
- gaddlemaps.parsers.extract_lattice_gro(line)[source]
Extracts the lattice vectors for pbc from the final line of a .gro file.
- Parameters
line (string) – The final line of a .gro file.
- Returns
vectors – An array with a lattice vector in each row.
- Return type
numpy.ndarray((3,3))