Topology¶
-
class
chemfiles::Topology¶ A topology contains the definition of all the atoms in the system, as well as the liaisons between the particles (bonds, angles, dihedrals, …) and the residues.
Only the atoms and the bonds are stored, the angles, dihedrals and impropers are automaticaly deduced from the bonds.
It is also possible to iterate over a
Topology, yielding all the atoms in the system.auto topology = Topology(); topology.add_atom(Atom("Fe")); topology.add_atom(Atom("Fe")); topology.add_atom(Atom("Fe")); for (Atom& atom: topology) { assert(atom.name() == "Fe"); }
Public Functions
-
Topology() = default¶ Construct a new empty topology
auto topology = Topology(); assert(topology.size() == 0); assert(topology.bonds().size() == 0);
-
Atom &
operator[](size_t index)¶ Get a reference to the atom at the position
index.auto topology = Topology(); topology.add_atom(Atom("Co")); topology.add_atom(Atom("V")); topology.add_atom(Atom("Fe")); topology.add_atom(Atom("Fe")); assert(topology[0].name() == "Co"); assert(topology[1].name() == "V"); topology[2].set_mass(45); assert(topology[2].mass() == 45);
- Parameters
index: the atomic index
- Exceptions
OutOfBounds: ifindexis greater thansize()
-
const Atom &
operator[](size_t index) const¶ Get a const reference to the atom at the position
index.auto topology = Topology(); topology.add_atom(Atom("Co")); topology.add_atom(Atom("V")); topology.add_atom(Atom("Fe")); topology.add_atom(Atom("Fe")); assert(topology[0].name() == "Co"); assert(topology[1].name() == "V"); topology[2].set_mass(45); assert(topology[2].mass() == 45);
- Parameters
index: the atomic index
- Exceptions
OutOfBounds: ifindexis greater thansize()
-
void
add_atom(Atom atom)¶ Add an
atomat the end of this topology.auto topology = Topology(); topology.add_atom(Atom("Zn")); assert(topology.size() == 1); assert(topology[0].name() == "Zn");
- Parameters
atom: the new atom to add
-
void
remove(size_t i)¶ Delete the atom at index
iin this topology, as well as all the bonds involving this atom.This function modify the index of all the atoms after
i, and modify the bond list accordingly.auto topology = Topology(); topology.add_atom(Atom("Zn")); topology.add_atom(Atom("Fe")); topology.add_atom(Atom("Rd")); assert(topology.size() == 3); assert(topology[1].name() == "Fe"); topology.remove(0); // atomic indexes are shifted by remove assert(topology[1].name() == "Rd");
- Parameters
i: the index of the atom to remove
- Exceptions
OutOfBounds: ifiis greater than size()
-
void
add_bond(size_t atom_i, size_t atom_j, Bond::BondOrder bond_order = Bond::UNKNOWN)¶ Add a bond in the system, between the atoms at index
atom_iandatom_j.auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_bond(0, 1); topology.add_bond(1, 2); assert(topology.bonds() == std::vector<Bond>({{0, 1}, {1, 2}})); // angles are automaticaly computed too assert(topology.angles() == std::vector<Angle>({{0, 1, 2}}));
- Parameters
atom_i: the index of the first atom in the bondatom_j: the index of the second atom in the bondbond_order: the bond order for the bond added
- Exceptions
OutOfBounds: ifatom_ioratom_jare greater thansize()Error: ifatom_i == atom_j, as this is an invalid bond
-
void
remove_bond(size_t atom_i, size_t atom_j)¶ Remove a bond in the system, between the atoms at index
atom_iandatom_j.If the bond does not exist, this does nothing.
auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_bond(0, 1); topology.add_bond(1, 2); assert(topology.bonds() == std::vector<Bond>({{0, 1}, {1, 2}})); topology.remove_bond(1, 0); assert(topology.bonds() == std::vector<Bond>({{1, 2}})); // This does nothing topology.remove_bond(0, 2); assert(topology.bonds() == std::vector<Bond>({{1, 2}}));
- Parameters
atom_i: the index of the first atom in the bondatom_j: the index of the second atom in the bond
- Exceptions
OutOfBounds: ifatom_ioratom_jare greater thansize()
-
Bond::BondOrder
bond_order(size_t atom_i, size_t atom_j) const¶ Get the bond order for the given bond
If the bond does not exist, this will thrown an Error.
auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("O")); topology.add_bond(0, 1, Bond::SINGLE); topology.add_bond(3, 4, Bond::DOUBLE); topology.add_bond(1, 2, Bond::SINGLE); // Lookup by the bond index assert(topology.bond_orders()[0] == Bond::SINGLE); assert(topology.bond_orders()[1] == Bond::SINGLE); assert(topology.bond_orders()[2] == Bond::DOUBLE); // Lookup by the atom indexes assert(topology.bond_order(0, 1) == Bond::SINGLE); assert(topology.bond_order(3, 4) == Bond::DOUBLE);
- Parameters
atom_i: the index of the first atom in the bondatom_j: the index of the second atom in the bond
- Exceptions
OutOfBounds: ifatom_ioratom_jare greater thansize()Error: if no bond betweenatom_iandatom_jexists.
-
size_t
size() const¶ Get the number of atoms in the topology
auto topology = Topology(); assert(topology.size() == 0); topology.resize(22); assert(topology.size() == 22); topology.add_atom(Atom("H")); assert(topology.size() == 23);
-
void
resize(size_t size)¶ Resize the topology to hold
sizeatoms, adding new atoms as needed.If the new number of atoms is bigger than the old one, pre-existing atoms are conserved.
If the new size if smaller than the old one, all atoms and connectivity elements after the new size are removed.
auto topology = Topology(); assert(topology.size() == 0); topology.resize(22); assert(topology.size() == 22);
- Parameters
size: the new size of the topology
-
void
reserve(size_t size)¶ Allocate memory in the frame to be able to store data for
sizeatoms.This function does not change the actual number of atoms in the topology, and should be used as an optimisation.
auto topology = Topology(); assert(topology.size() == 0); topology.resize(10); assert(topology.size() == 10); // reserve allocate memory, but does not change the size topology.reserve(100); assert(topology.size() == 10);
- Parameters
size: the number of elements to reserve memory for
-
const std::vector<Bond> &
bonds() const¶ Get the bonds in the system
The bonds are sorted according to
operator<(const Bond&, const Bond&), which mean it is possible to look for a bond in the list using a binary search (std::lower_bound).auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_bond(0, 1); topology.add_bond(1, 2); assert(topology.bonds() == std::vector<Bond>({{0, 1}, {1, 2}})); auto bonds = topology.bonds(); // perform a binary search in the bonds auto it = std::lower_bound(bonds.begin(), bonds.end(), Bond(1, 2)); assert(it != bonds.end()); assert(*it == Bond(1, 2));
-
const std::vector<Bond::BondOrder> &
bond_orders() const¶ Get the bond orders in the system.
The bond orders are sorted so that the index of each bond is the same as its index in the array returned by
Topology::bonds. This means that the bond order forTopology::bonds()[index]would be given bybond_orders()[index].auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("O")); topology.add_bond(0, 1, Bond::SINGLE); topology.add_bond(3, 4, Bond::DOUBLE); topology.add_bond(1, 2, Bond::SINGLE); // Lookup by the bond index assert(topology.bond_orders()[0] == Bond::SINGLE); assert(topology.bond_orders()[1] == Bond::SINGLE); assert(topology.bond_orders()[2] == Bond::DOUBLE); // Lookup by the atom indexes assert(topology.bond_order(0, 1) == Bond::SINGLE); assert(topology.bond_order(3, 4) == Bond::DOUBLE);
-
const std::vector<Angle> &
angles() const¶ Get the angles in the system
The angles are sorted according to
operator<(const Angle&, const Angle&), which mean it is possible to look for an angle in the list using a binary search (std::lower_bound).auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_bond(0, 1); topology.add_bond(1, 2); topology.add_bond(2, 3); assert(topology.angles() == std::vector<Angle>({{0, 1, 2}, {1, 2, 3}})); auto angles = topology.angles(); // perform a binary search in the angles auto it = std::lower_bound(angles.begin(), angles.end(), Angle(1, 2, 3)); assert(it != angles.end()); assert(*it == Angle(1, 2, 3));
-
const std::vector<Dihedral> &
dihedrals() const¶ Get the dihedral angles in the system
The dihedrals are sorted according to
operator<(const Dihedral&, const Dihedral&), which mean it is possible to look for a dihedral in the list using a binary search (std::lower_bound).auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_bond(0, 1); topology.add_bond(1, 2); topology.add_bond(2, 3); assert(topology.dihedrals() == std::vector<Dihedral>({{0, 1, 2, 3}})); auto dihedrals = topology.dihedrals(); // perform a binary search in the dihedrals auto it = std::lower_bound( dihedrals.begin(), dihedrals.end(), Dihedral(0, 1, 2, 3) ); assert(it != dihedrals.end()); assert(*it == Dihedral(0, 1, 2, 3));
-
const std::vector<Improper> &
impropers() const¶ Get the improper dihedral angles in the system
The impropers are sorted according to
operator<(const Improper&, const Improper&), which mean it is possible to look for an improper in the list using a binary search (std::lower_bound).auto topology = Topology(); topology.add_atom(Atom("C")); topology.add_atom(Atom("H")); topology.add_atom(Atom("H")); topology.add_atom(Atom("H")); topology.add_bond(0, 1); topology.add_bond(0, 2); topology.add_bond(0, 3); assert(topology.impropers() == std::vector<Improper>({{1, 0, 2, 3}})); auto impropers = topology.impropers(); // perform a binary search in the impropers auto it = std::lower_bound( impropers.begin(), impropers.end(), Improper(1, 0, 2, 3) ); assert(it != impropers.end()); assert(*it == Improper(1, 0, 2, 3));
-
void
clear_bonds()¶ Remove all bonding information in the topology (bonds, angles and dihedrals)
auto topology = Topology(); topology.add_atom(Atom("H")); topology.add_atom(Atom("O")); topology.add_atom(Atom("H")); topology.add_bond(0, 1); topology.add_bond(1, 2); assert(topology.bonds().size() == 2); assert(topology.angles().size() == 1); topology.clear_bonds(); assert(topology.bonds().size() == 0); assert(topology.angles().size() == 0);
-
void
add_residue(Residue residue)¶ Add a
residueto this topology.auto topology = Topology(); topology.add_atom(Atom("Zn")); topology.add_atom(Atom("Fe")); assert(topology.residues().size() == 0); auto residue = Residue("first"); residue.add_atom(0); topology.add_residue(residue); assert(topology.residues().size() == 1);
- Parameters
residue: the residue to add to this topology
- Exceptions
chemfiles::Error: if any atom in theresidueis already in another residue in this topology. In that case, the topology is not modified.
-
bool
are_linked(const Residue &first, const Residue &second) const¶ Check if two residues are linked together, i.e. if there is a bond between one atom in the
firstresidue and one atom in thesecondone. Both residues should be in this topology.The two residues are the same (
first == second), this function returnstrue.auto topology = Topology(); topology.add_atom(Atom("Zn")); topology.add_atom(Atom("Fe")); auto first = Residue("first"); first.add_atom(0); topology.add_residue(first); auto second = Residue("second"); second.add_atom(1); topology.add_residue(second); assert(!topology.are_linked(first, second)); topology.add_bond(0, 1); assert(topology.are_linked(first, second));
-
optional<const Residue&>
residue_for_atom(size_t index) const¶ Get the residue containing the atom at the given
index.If no residue contains this atom, this function returns
nullopt.auto topology = Topology(); topology.add_atom(Atom("Zn")); topology.add_atom(Atom("Fe")); auto first = Residue("first"); first.add_atom(0); topology.add_residue(first); assert(topology.residue_for_atom(0)); assert(topology.residue_for_atom(0)->name() == "first"); assert(!topology.residue_for_atom(1)); assert(topology.residue_for_atom(1) == nullopt);
-
const Residue &
residue(size_t index) const¶ Get the residue at the given
indexin this topologyThere is no guarantee that this index matches the residue id.
auto topology = Topology(); topology.add_atom(Atom("Zn")); topology.add_atom(Atom("Fe")); auto first = Residue("first"); first.add_atom(0); topology.add_residue(first); assert(topology.residue(0).name() == "first");
-
const std::vector<Residue> &
residues() const¶ Get all the residues in the topology as a vector
auto topology = Topology(); topology.add_residue(Residue("first")); topology.add_residue(Residue("second")); auto residues = topology.residues(); assert(residues.size() == 2); assert(residues[0].name() == "first"); assert(residues[1].name() == "second");
-
Connectivity elements¶
-
class
chemfiles::Bond¶ The
Bondclass ensure a canonical representation of a bond two atoms.This class implements all the comparison operators, as well as indexing.
auto bond = Bond(55, 23); // indexing assert(bond[0] == 23); assert(bond[1] == 55); // equality assert(bond == Bond(23, 55)); assert(bond != Bond(23, 24)); // lexicographic comparison assert(bond < Bond(44, 55)); assert(bond >= Bond(12, 33));
Public Types
-
enum
BondOrder¶ Stores the type of bond.
Values:
-
enumerator
SINGLE¶ Single bond.
-
enumerator
DOUBLE¶ Double bond.
-
enumerator
TRIPLE¶ Triple bond.
-
enumerator
QUADRUPLE¶ Quadruplet bond.
-
enumerator
QUINTUPLET¶ Quintuplet bond.
-
enumerator
DOWN¶ Single bond direction from first atom to second is ‘down’. Used for cis-trans isomers.
-
enumerator
UP¶ Single bond direction from first atom to second is ‘up’. Used for cis-trans isomers.
-
enumerator
DATIVE_R¶ Dative bond where the electrons are localized to the first atom.
-
enumerator
DATIVE_L¶ Dative bond where the electrons are localized to the second atom.
-
enumerator
AMIDE¶ Amide bond (C(=O)-NH)
-
enumerator
AROMATIC¶ Aromatic bond (for example the ring bonds in benzene)
-
enumerator
Public Functions
-
Bond(size_t i, size_t j)¶ Create a new
Bondcontaining the atomsiandj.- Exceptions
Error: ifi == j
-
size_t
operator[](size_t i) const¶ Get the index of the
ith atom (i == 0ori == 1) in the bond.- Exceptions
OutOfBounds: ifiis not 0 or 1
-
enum
-
class
chemfiles::Angle¶ The
Angleclass ensure a canonical representation of an angle between three atoms.An angle is formed by two consecutive bonds:
This class implements all the comparison operators, as well as indexing.| i k | | \ / | | j |
auto angle = Angle(55, 23, 12); // indexing assert(angle[0] == 12); assert(angle[1] == 23); assert(angle[2] == 55); // equality assert(angle == Angle(12, 23, 55)); assert(angle != Angle(12, 23, 24)); // lexicographic comparison assert(angle < Angle(44, 23, 55)); assert(angle >= Angle(11, 33, 14));
Public Functions
-
Angle(size_t i, size_t j, size_t k)¶ Create a new
Anglecontaining the atomsi,jandk.- Exceptions
Error: ifi == j,j == kori == k
-
size_t
operator[](size_t i) const¶ Get the index of the
ith atom (i == 0,i == 1ori == 2) in the angle.- Exceptions
OutOfBounds: ifiis not 0, 1 or 2
-
-
class
chemfiles::Dihedral¶ The
Dihedralclass ensure a canonical representation of a dihedral angle between four atoms.A dihedral angle is formed by three consecutive bonds:
This class implements all the comparison operators, as well as indexing.| i k | | \ / \ | | j m |
auto dihedral = Dihedral(2, 55, 23, 12); // indexing assert(dihedral[0] == 12); assert(dihedral[1] == 23); assert(dihedral[2] == 55); assert(dihedral[3] == 2); // equality assert(dihedral == Dihedral(12, 23, 55, 2)); assert(dihedral != Dihedral(12, 23, 24, 2)); // lexicographic comparison assert(dihedral < Dihedral(44, 23, 55, 1)); assert(dihedral >= Dihedral(11, 33, 14, 4));
Public Functions
-
Dihedral(size_t i, size_t j, size_t k, size_t m)¶ Create a new
Dihedralcontaining the atomsi,j,kandm.- Exceptions
Error: if any ofi,j,k,mhas the same value as another
-
size_t
operator[](size_t i) const¶ Get the index of the
ith atom (ican be 0, 1, 2 or 3) in the dihedral.- Exceptions
OutOfBounds: ifiis not 0, 1, 2 or 3.
-
-
class
chemfiles::Improper¶ The
Improperclass ensure a canonical representation of an improper dihedral angle between four atoms.An improper dihedral angle is formed by three bonds around a central atom:
This class implements all the comparison operators, as well as indexing.| i k | | \ / | | j | | | | | m |
The second atom of the improper is always the central atom.
auto improper = Improper(2, 55, 23, 12); // indexing assert(improper[0] == 2); assert(improper[1] == 55); assert(improper[2] == 12); assert(improper[3] == 23); // equality assert(improper == Improper(12, 55, 2, 23)); assert(improper != Improper(12, 55, 2, 21)); // lexicographic comparison assert(improper < Improper(44, 23, 55, 8)); assert(improper >= Improper(11, 33, 14, 1));
Public Functions
-
Improper(size_t i, size_t j, size_t k, size_t m)¶ Create a new
Impropercontaining the atomsi,j,kandm.jmust be the central atom of the improper.- Exceptions
Error: if any ofi,j,k,mhas the same value as another
-
size_t
operator[](size_t i) const¶ Get the index of the
ith atom (ican be 0, 1, 2 or 3) in the improper.- Exceptions
OutOfBounds: ifiis not 0, 1, 2 or 3.
-