gaddlemaps.components package

Module contents

Molecular Simulation Components

This submodule contains useful objects to workaround with components from molecular dynamics simulations (molecules, atoms…). You can load the information of these components just from the coordinates files (.gro) or add information about atom types or how they are bonded loading the topologies.

To start, you can load an object with a .gro file of a simulation system with the class SystemGro. You can iterate through this object accessing the residues in the system which will be instances of Residue class. At the same time, you can iterate through the atoms (AtomGro instances) of a Residue instance.

If you want to include the information from topology files (up to now, just .itp files with gromacs format are compatible) to the system you can initialize an instance of the System class. In this case, the System is formed by Molecule objects (which are combinations of Residue and MoleculeTop objects) and the Molecule is formed by Atom objects (combination of AtomGro and AtomTop).

class gaddlemaps.components.Atom(atom_top, atom_gro)[source]

Bases: object

An atom class that wraps the AtomTop and AtomGro classes.

You can access to the methods and attributes that both AtomGro and AtomItp have. To create the atom object, both input atoms should have the same resname and name attributes. On the other hand, only the attributes from the AtomGro can be changed (e.g. positions, velocities, …) excluding the resname and name.

Parameters
  • atom_top (AtomTop) – The AtomTop object.

  • atom_gro (AtomGro) – The AtomGro object.

Raises
  • IOError – If the atom_gro and atom_top do not correspond to the same atom.

  • TypeError – If the inputs are not instances of the corresponding classes.

Attributes
atom_gro

AtomGro : The input AtomGro object

atom_top

AtomTop : The input AtomTop object

gro_resid

Residue number for the gro part of the atom (atom_gro).

top_resid

Residue number for the part of the atom with the topology (atom_top).

Methods

copy()

Returns a copy of self.

property atom_gro: AtomGro

The input AtomGro object

Type

AtomGro

Return type

AtomGro

property atom_top: AtomTop

The input AtomTop object

Type

AtomTop

Return type

AtomTop

copy()[source]

Returns a copy of self.

Only the gro atom is copied. The atom_top remains the same.

Returns

new_atom – The copied atom.

Return type

Atom

property gro_resid: int

Residue number for the gro part of the atom (atom_gro).

Return type

int

property top_resid: int

Residue number for the part of the atom with the topology (atom_top).

Return type

int

class gaddlemaps.components.AtomGro(parsed_gro_line)[source]

Bases: object

It contains the information of a .gro line corresponding to an atom.

You can add atoms with the same residue name and number to form a Residue instance.

Parameters

parsed_gro_line (list) – A list returned by the GroFile.read_line method when a correct formatted .gro line is input.

resid

Residue number of the atom.

Type

int

resname

Residue name of the atom.

Type

str

name

Name of the atom.

Type

str

atomid

Atom index in the .gro file.

Type

str

position

The coordinates of the atom.

Type

np.ndarray(3)

velocity

The velocity of the atom if it is specified in the .gro line. If not this attribute is set to None.

Type

np.ndarray(3) or None

Attributes
element

string : The element of the atom. It is obtained removing the non

residname

string : An identifier that contains the residue name and number. This

Methods

copy()

Returns a copy of the atom.

gro_line([parsed])

Returns the gro line corresponding to the atom.

copy()[source]

Returns a copy of the atom.

You can safely change the attributes of the returned atom without changing the original one.

Returns

new_atom – The copied atom.

Return type

AtomGro

property element: str

The element of the atom. It is obtained removing the non alphabetic character in the atom name.

Type

string

Return type

str

gro_line(parsed=True)[source]

Returns the gro line corresponding to the atom.

Parameters

parsed (bool, optional) – If True (default), the line is returned as FileGro.read_line method output. Else, line with the correct .gro format is returned.

Returns

gro_line – The corresponding .gro line.

Return type

List of (str, int or float) or str

property residname: str

An identifier that contains the residue name and number. This should be unique for ear Residue object in a simulation system.

Type

string

Return type

str

class gaddlemaps.components.AtomTop(name, resname, resid, index)[source]

Bases: object

Atom with information of its name, residue name and bonds.

It is also needed the index of the atom in the molecule. The bonds are initialized as empty set.

Parameters
  • name (str) – Atom name

  • resname (str) – Residue name of the atom.

  • resid (int) – Residue index of the atom.

  • index (int) – Atom index in the molecule.

bonds

A set with the hash of the atoms that are connected to self.

Type

set of int

Attributes
residname

string: An identifier of the residue (resid+name)

Methods

closest_atoms([natoms])

Returns a list with natoms index of bonded atoms to self.

connect(atom)

Connects self with other atom setting the bond.

copy()

Returns a copy of the current atom.

closest_atoms(natoms=2)[source]

Returns a list with natoms index of bonded atoms to self.

If more than natoms are bonded self, the natoms with lower id_num are returned.

Parameters

natoms (integer) – The number of atoms to return.

Returns

bonded_atoms – The list with the index of natoms atoms bonded self.

Return type

list of int

connect(atom)[source]

Connects self with other atom setting the bond.

Parameters

atom (AtomTop) – Atom to connect.

copy()[source]

Returns a copy of the current atom.

Returns

atom_top – The copy of the atom.

Return type

AtomTop

property residname: str

An identifier of the residue (resid+name)

Type

string

Return type

str

class gaddlemaps.components.Molecule(molecule_top, residues)[source]

Bases: Residue

Loads a molecule combining a MoleculeTop and a list of Residue.

This class wraps all the features of both MoleculeTop and Residues which conform the molecule. When an object is initialized a copy of the input residues is stored (to avoid undesired attribute changes). This class inherits from Residue so they have the same methods and properties (although most of them are reimplemented).

Parameters
  • molecule_top (MoleculeTop) – The object with the bonds information of the molecule.

  • residues (List of Residue) – An list with the residues that constitute the molecule.

Raises
  • TypeError – If the input are instances of wrong type.

  • IOError – If residues do not constitute the molecule_top.

Attributes
atoms

list of Atom: List with the copies of the atoms of the molecule.

atoms_ids

list of int: A list with the ids of the atoms in the residues.

atoms_positions

numpy.ndarray((N, 3)) : An array with the atoms positions.

atoms_velocities

numpy.ndarray((N, 3)) or None : An array with the atoms velocities.

bonds_distance

dict of int to list of tuple(int, float): A complex data structure

distance_to_zero

float : The distance between the geometric_center and (0, 0, 0)

geometric_center

numpy.ndarray(3): Coordinates of the geometric center of the residue.

molecule_top

MoleculeTop : The object with the topology information of the molecule.

resid

int: Residue number of the residue.

residname

string: An identifier of the residue (resid+name)

resids

List of int: A list with the residues ids of the residues constituting the molecule.

residues

List of Residue : a list with the Residue objects that constitute the molecule.

resname

string: Resname of the residue.

resnames

List of string: A list with the names of the residues constituting the molecule.

x

float: The x coordinate of the geometric center of the residue.

y

float: The y coordinate of the geometric center of the residue.

z

float: The z coordinate of the geometric center of the residue.

Methods

copy([new_residues])

Returns a copy of the molecule.

deep_copy([new_residues])

Returns a deep copy of the molecule.

distance_to(residue[, box_vects, inv])

Returns the distance between self and residue.

from_files(fgro, ftop)

Loads the molecule from gro and a compatible topology file.

index(atom)

Returns the index of the atom in the molecule.

move(displacement)

Moves the residue a given displacement vector.

move_to(new_position)

Moves the residue geometric_center to new_position.

remove_atom(atom)

Removes a given atom from the residue.

rotate(rotation_matrix)

Rotate the residue around its center of mass with a given rotation matrix.

update_from_molecule_top(mtop)

Modifies the Residue atoms name to match the mtop.

write_gro(fout)

Writes a .gro file with the residue conformation.

property atoms: List[Atom]

List with the copies of the atoms of the molecule.

Type

list of Atom

Return type

List[Atom]

property atoms_ids: List[int]

A list with the ids of the atoms in the residues.

Type

list of int

Return type

List[int]

property atoms_positions: ndarray

An array with the atoms positions.

Type

numpy.ndarray((N, 3))

Return type

ndarray

property atoms_velocities: Optional[ndarray]

An array with the atoms velocities. If one of the atoms has no velocity this returns None.

Type

numpy.ndarray((N, 3)) or None

Return type

Optional[ndarray]

property bonds_distance: Dict[int, List[Tuple[int, float]]]

A complex data structure that collect the information of the bond distances. The key of the property corresponds to the atom index in the molecule. The value is a list with tuples. For each tuple, the first value corresponds with the index of the bonded atom and the second is the length of the bond. This property is used in the alignment process in gaddle maps.

Type

dict of int to list of tuple(int, float)

Return type

Dict[int, List[Tuple[int, float]]]

copy(new_residues=None)[source]

Returns a copy of the molecule.

If new_molecule_gro is passed, the old residues will be replaced to update the positions. This is used in the extrapolation step.

NOTE: With this method, the molecule_top used for the Molecule initialization remains the same. This means that future changes in copied molecules may affect other parts of you code. If you want a completely independent new molecule use “deep_copy” method.

Parameters

new_residues (Optional[List[Residue]]) – List of residues to replace the original positions.

Returns

molecule – The copy of the molecule.

Return type

Molecule

deep_copy(new_residues=None)[source]

Returns a deep copy of the molecule.

If new_molecule_gro is passed, the old residues will be replaced to update the positions. This is used in the extrapolation step. This method generates a new molecule that is not linked to any attribute of the original one.

Parameters

new_residues (Optional[List[Residue]]) – List of residues to replace the original positions.

Returns

molecule – The deep copy of the molecule.

Return type

Molecule

distance_to(residue, box_vects=None, inv=False)

Returns the distance between self and residue.

residue can be a Residue instance or a 3D vector.

Parameters
  • residue (Residue or numpy.ndarray) – The residue or a point to compute the distance.

  • box_vects (numpy.ndarray) – The box vectors to apply periodic boundary conditions.

  • inv (bool) – If it is True, box_vects are considered as the inverse matrix of the actual box_vects for a better performance.

Returns

distance – The euclidean distance.

Return type

float

property distance_to_zero: float

The distance between the geometric_center and (0, 0, 0) without applying periodic boundary conditions.

Type

float

Return type

float

classmethod from_files(fgro, ftop)[source]

Loads the molecule from gro and a compatible topology file.

Parameters
  • fgro (str) – The file name with the gro.

  • ftop (str) – The file name with the top.

Returns

molecule – The initialized molecule.

Return type

Molecule

property geometric_center: ndarray

Coordinates of the geometric center of the residue.

Type

numpy.ndarray(3)

Return type

ndarray

index(atom)[source]

Returns the index of the atom in the molecule.

Parameters

atom (Atom) – The atom to find the index.

Returns

index – The index of the atom in the molecule.

Return type

int

property molecule_top: MoleculeTop

The object with the topology information of the molecule.

Type

MoleculeTop

Return type

MoleculeTop

move(displacement)

Moves the residue a given displacement vector.

Parameters

displacement (numpy.ndarray(3)) – An array with the displacement vector.

move_to(new_position)

Moves the residue geometric_center to new_position.

Parameters

new_position (numpy.ndarray(3)) – An array with the new position coordinates.

remove_atom(atom)

Removes a given atom from the residue.

Parameters

atom (AtomGro) – The atom you want to remove from the residue.

property resid: int

Residue number of the residue.

Type

int

Return type

int

property residname: str

An identifier of the residue (resid+name)

Type

string

Return type

str

property resids: List[int]
A list with the residues ids of the residues

constituting the molecule.

To set this property, a list of int with the same length as the original must be passed. This will change each residue id. You can also pass just an int and this will set all the residue ids to the same value.

Type

List of int

Return type

List[int]

property residues: List[Residue]

a list with the Residue objects that constitute the molecule.

Type

List of Residue

Return type

List[Residue]

property resname: str

Resname of the residue.

Type

string

Return type

str

property resnames: List[str]
A list with the names of the residues constituting the

molecule.

To set this property, a list of strings with the same length as the original must be passed. This will change each residue name. You can also pass just a string and this will set all the residue names to the same value.

Type

List of string

Return type

List[str]

rotate(rotation_matrix)

Rotate the residue around its center of mass with a given rotation matrix.

Parameters

rotation_matrix (numpy.ndarray) – 3x3 array with the rotation matrix. ADVICE: create the matrix with the help of “rotation_matrix” function placed in the root of the package.

update_from_molecule_top(mtop)

Modifies the Residue atoms name to match the mtop.

This method is very useful when you have a miss-match between the atom names in the topology and gro files. This will modify the Residue atoms names to match the names in the topology. Make sure that all the atoms in the topology are in the Residue.

Parameters

mtop (MoleculeTop) – The molecule to match.

Raises

ValueError – If number of atoms in self and in mtop does not match.

write_gro(fout)

Writes a .gro file with the residue conformation.

Parameters

fout (str) – The path with the file to write the information.

property x: float

The x coordinate of the geometric center of the residue.

Type

float

Return type

float

property y: float

The y coordinate of the geometric center of the residue.

Type

float

Return type

float

property z: float

The z coordinate of the geometric center of the residue.

Type

float

Return type

float

class gaddlemaps.components.MoleculeTop(ftop, file_format=None)[source]

Bases: object

Loads molecules from a topology file.

This class behaves like a list of atoms which has bonds defined. The appropriate parser will be used based on the input file extension. The available parsers are summarized in the class attribute “PARSERS”. In this attribute, the keys are the files extensions and the values the corresponding functions that extracts the information from the files with that extensions. These functions should return:

  • The name of the molecule

  • A list with tuples with the atoms and residues names in order of

appearance in the file. - A list with tuples with atoms index (referred to the atoms_info indexes) that are bonded.

Parameters
  • ftop (string or TextIOWrapper) – The path to the file (or the opened file) with the molecule name and bonds information.

  • file_format (str, Optional) – The file extension of ftop. If it is None this will be taken from ftop.

Raises
  • ValueError – If the file format is not supported.

  • IOError – If the input file misses information.

Attributes
resids

list of str: Residue names of the atoms without consecutive

resname_len_list

list of tuple(str, int) :

resnames

list of str: Residue names of the atoms without consecutive

Methods

copy()

Returns a copy of the molecule_top.

index(atom)

Returns the index of the atom in the molecule.

copy()[source]

Returns a copy of the molecule_top.

The atoms forming the copy are not the same objects as the original molecule so you do not have to worry about linked objects.

Returns

molecule_top – The copy of the molecule.

Return type

MoleculeTop

index(atom)[source]

Returns the index of the atom in the molecule.

Parameters

atom (AtomTop) – The atom to find the index.

Returns

index – The index of the atom in the molecule.

Return type

int

property resids: List[int]
Residue names of the atoms without consecutive

repetitions.

To set this property a list with the same length of residues must be passed.

Type

list of str

Return type

List[int]

property resname_len_list: List[Tuple[str, int]]

list of tuple(str, int): [(resname_1, number_of_atoms_with_resname_1), (resname_2, number_of_atoms_with_resname_2), …]

Return type

List[Tuple[str, int]]

property resnames: List[str]
Residue names of the atoms without consecutive

repetitions.

To set this property a list with the same length of residues must be passed.

Type

list of str

Return type

List[str]

class gaddlemaps.components.Residue(atoms)[source]

Bases: object

A class with the information of a residue in a .gro file.

This class creates objects that are enumerations of AtomGro instances. It has methods to manipulate atoms positions maintaining the shape of the residue. This class have to be initialized with non empty list of atoms.

You can add two residues if the atoms from both residues have the same residue name and number. This can also be done with just one atom and the same check will be done.

Parameters

atoms (list of AtomGro) – A list with the atoms of the residue.

:raises ValueError : If the the input list of atoms is empty or they have different: residue number or name.

Attributes
atoms

list of AtomGro: List with a copy of the atoms of the residue.

atoms_ids

list of int: A list with the ids of the atoms of the residue.

atoms_positions

numpy.ndarray((N, 3)) : An array with the atoms positions.

atoms_velocities

numpy.ndarray((N, 3)) or None : An array with the atoms velocities.

distance_to_zero

float : The distance between the geometric_center and (0, 0, 0)

geometric_center

numpy.ndarray(3): Coordinates of the geometric center of the residue.

resid

int: Residue number of the residue.

residname

string: An identifier of the residue (resid+name)

resname

string: Resname of the residue.

x

float: The x coordinate of the geometric center of the residue.

y

float: The y coordinate of the geometric center of the residue.

z

float: The z coordinate of the geometric center of the residue.

Methods

copy()

Returns a copy of the residue.

distance_to(residue[, box_vects, inv])

Returns the distance between self and residue.

move(displacement)

Moves the residue a given displacement vector.

move_to(new_position)

Moves the residue geometric_center to new_position.

remove_atom(atom)

Removes a given atom from the residue.

rotate(rotation_matrix)

Rotate the residue around its center of mass with a given rotation matrix.

update_from_molecule_top(mtop)

Modifies the Residue atoms name to match the mtop.

write_gro(fout)

Writes a .gro file with the residue conformation.

property atoms: List[AtomGro]

List with a copy of the atoms of the residue.

Type

list of AtomGro

Return type

List[AtomGro]

property atoms_ids: List[int]

A list with the ids of the atoms of the residue.

Type

list of int

Return type

List[int]

property atoms_positions: ndarray

An array with the atoms positions.

Type

numpy.ndarray((N, 3))

Return type

ndarray

property atoms_velocities: Optional[ndarray]

An array with the atoms velocities. If one of the atoms has no velocity this returns None.

Type

numpy.ndarray((N, 3)) or None

Return type

Optional[ndarray]

copy()[source]

Returns a copy of the residue.

Returns

residue – The copy of the residue.

Return type

Residue

distance_to(residue, box_vects=None, inv=False)[source]

Returns the distance between self and residue.

residue can be a Residue instance or a 3D vector.

Parameters
  • residue (Residue or numpy.ndarray) – The residue or a point to compute the distance.

  • box_vects (numpy.ndarray) – The box vectors to apply periodic boundary conditions.

  • inv (bool) – If it is True, box_vects are considered as the inverse matrix of the actual box_vects for a better performance.

Returns

distance – The euclidean distance.

Return type

float

property distance_to_zero: float

The distance between the geometric_center and (0, 0, 0) without applying periodic boundary conditions.

Type

float

Return type

float

property geometric_center: ndarray

Coordinates of the geometric center of the residue.

Type

numpy.ndarray(3)

Return type

ndarray

move(displacement)[source]

Moves the residue a given displacement vector.

Parameters

displacement (numpy.ndarray(3)) – An array with the displacement vector.

move_to(new_position)[source]

Moves the residue geometric_center to new_position.

Parameters

new_position (numpy.ndarray(3)) – An array with the new position coordinates.

remove_atom(atom)[source]

Removes a given atom from the residue.

Parameters

atom (AtomGro) – The atom you want to remove from the residue.

property resid: int

Residue number of the residue.

Type

int

Return type

int

property residname: str

An identifier of the residue (resid+name)

Type

string

Return type

str

property resname: str

Resname of the residue.

Type

string

Return type

str

rotate(rotation_matrix)[source]

Rotate the residue around its center of mass with a given rotation matrix.

Parameters

rotation_matrix (numpy.ndarray) – 3x3 array with the rotation matrix. ADVICE: create the matrix with the help of “rotation_matrix” function placed in the root of the package.

update_from_molecule_top(mtop)[source]

Modifies the Residue atoms name to match the mtop.

This method is very useful when you have a miss-match between the atom names in the topology and gro files. This will modify the Residue atoms names to match the names in the topology. Make sure that all the atoms in the topology are in the Residue.

Parameters

mtop (MoleculeTop) – The molecule to match.

Raises

ValueError – If number of atoms in self and in mtop does not match.

write_gro(fout)[source]

Writes a .gro file with the residue conformation.

Parameters

fout (str) – The path with the file to write the information.

property x: float

The x coordinate of the geometric center of the residue.

Type

float

Return type

float

property y: float

The y coordinate of the geometric center of the residue.

Type

float

Return type

float

property z: float

The z coordinate of the geometric center of the residue.

Type

float

Return type

float

class gaddlemaps.components.System(fgro, *ftops)[source]

Bases: object

Class to manage simulation systems.

A System object is formed by Molecule objects. Only the molecules corresponding to the input ftops will be loaded. The input files can be paths to files or opened files.

Parameters
  • fgro (string or TextIOWrapper) – Gromacs file with the system information.

  • *ftops (string or TextIOWrapper) – Paths with the files with the bonds information to load molecules.

Raises

IOError – If one of the topology files do not match with any molecule in the system.

Attributes
composition

Counter of str: int : For each molecule name (key), how many molecules there are (value).

fgro

Methods

add_ftop(ftop)

Adds and identifies the molecule from the ftop to the system.

add_molecule_top(mol_top)

Adds a molecule to the system and find it in the gro file.

add_ftop(ftop)[source]

Adds and identifies the molecule from the ftop to the system.

Parameters

ftop (str or TextIOWrapper) – Path to the file (or opened file) with the topology of the molecule.

add_molecule_top(mol_top)[source]

Adds a molecule to the system and find it in the gro file.

property composition: Counter[str]

For each molecule name (key), how many molecules there are (value).

Type

Counter of str

Type

int

Return type

Counter[str]

property fgro: str
Return type

str

class gaddlemaps.components.SystemGro(fgro)[source]

Bases: object

Class to work with the information in gro files.

Basically this class acts like a list of Residue objects. Only one Residue instance of each type is loaded to afford memory for large systems. The positions of the rest of the residues are storage and they are generated when requested.

Parameters

fgro (string or TextIOWrapper) – Gromacs file path or open file with the system information.

Attributes
box_matrix

numpy.ndarray (3,3) : the 3 lattice vectors of the box.

comment_line

str: The comment line in the gro file.

composition

Counter of str: int : For each resname (key), how many molecules there are (value).

molecules_info_ordered_all

generator of int: Returns the index of the molecules in the

molecules_resname_len_index

dict of tuple (string, int) to int: The keys of the dictionary are

n_atoms

int : The number of atoms in the system.

property box_matrix: ndarray

the 3 lattice vectors of the box.

Type

numpy.ndarray (3,3)

Return type

ndarray

property comment_line: str

The comment line in the gro file.

Type

str

Return type

str

property composition: Mapping[str, int]

For each resname (key), how many molecules there are (value).

Type

Counter of str

Type

int

Return type

Mapping[str, int]

property molecules_info_ordered_all: Generator[int, None, None]

Returns the index of the molecules in the different_molecules attribute in order of appearance.

Type

generator of int

Return type

Generator[int, None, None]

property molecules_resname_len_index: Dict[Tuple[str, int], int]

The keys of the dictionary are tuples with the residue name and the number of atoms of the different residues in the system and the values are its index in the list of different molecules.

Type

dict of tuple (string, int) to int

Return type

Dict[Tuple[str, int], int]

property n_atoms: int

The number of atoms in the system.

Type

int

Return type

int

gaddlemaps.components.are_connected(atoms)[source]

Check if the input atoms are connected.

Parameters

atoms (List of AtomTop or Atom) – The list of atoms to compute if they are connected.

Returns

connected – True if the atoms are connected, else False.

Return type

bool