Struct chemfiles::Topology

source ·
pub struct Topology { /* private fields */ }
Expand description

A Topology contains the definition of all the atoms in the system, and the liaisons between the atoms (bonds, angles, dihedrals, …). It will also contain all the residues information if it is available.

Implementations§

source§

impl Topology

source

pub fn new() -> Topology

Create a new empty topology.

Example
let topology = Topology::new();
assert_eq!(topology.size(), 0);
source

pub fn atom(&self, index: usize) -> AtomRef<'_>

Get a reference of the atom at the given index in this topology.

Panics

If index is out of bounds.

Example
let mut topology = Topology::new();
topology.resize(6);

let atom = topology.atom(4);
assert_eq!(atom.name(), "");
source

pub fn atom_mut(&mut self, index: usize) -> AtomMut<'_>

Get a mutable reference to the atom at the given index in this topology.

Panics

If index is out of bounds.

Example
let mut topology = Topology::new();
topology.resize(6);

assert_eq!(topology.atom(4).name(), "");

topology.atom_mut(4).set_name("Fe");
assert_eq!(topology.atom(4).name(), "Fe");
source

pub fn size(&self) -> usize

Get the current number of atoms in this topology.

Example
let mut topology = Topology::new();
assert_eq!(topology.size(), 0);

topology.resize(6);
assert_eq!(topology.size(), 6);
source

pub fn resize(&mut self, natoms: usize)

Resize this topology to hold natoms atoms, inserting dummy atoms if the new size if bigger than the old one.

Example
let mut topology = Topology::new();
assert_eq!(topology.size(), 0);

topology.resize(6);
assert_eq!(topology.size(), 6);
source

pub fn add_atom(&mut self, atom: &Atom)

Add an Atom at the end of this topology

Example
let mut topology = Topology::new();
topology.add_atom(&Atom::new("Mg"));

let atom = topology.atom(0);
assert_eq!(atom.name(), "Mg");
source

pub fn remove(&mut self, index: usize)

Remove an Atom from this topology by index. This modify all the other atoms indexes.

Panics

If the index is out of bounds

Example
let mut topology = Topology::new();
topology.resize(9);
assert_eq!(topology.size(), 9);

topology.remove(7);
assert_eq!(topology.size(), 8);
source

pub fn bonds_count(&self) -> usize

Get the number of bonds in the topology.

Example
let mut topology = Topology::new();
assert_eq!(topology.bonds_count(), 0);
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(2, 1);
topology.add_bond(2, 3);
assert_eq!(topology.bonds_count(), 3);
source

pub fn angles_count(&self) -> usize

Get the number of angles in the topology.

Example
let mut topology = Topology::new();
assert_eq!(topology.angles_count(), 0);
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(2, 1);
topology.add_bond(2, 3);
assert_eq!(topology.angles_count(), 2);
source

pub fn dihedrals_count(&self) -> usize

Get the number of dihedral angles in the topology.

Example
let mut topology = Topology::new();
assert_eq!(topology.dihedrals_count(), 0);
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(2, 1);
topology.add_bond(2, 3);
assert_eq!(topology.dihedrals_count(), 1);
source

pub fn impropers_count(&self) -> usize

Get the number of improper dihedral angles in the topology.

Example
let mut topology = Topology::new();
assert_eq!(topology.dihedrals_count(), 0);
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(0, 2);
topology.add_bond(0, 3);
assert_eq!(topology.impropers_count(), 1);
source

pub fn bonds(&self) -> Vec<[usize; 2]>

Get the list of bonds in the topology.

Example
let mut topology = Topology::new();
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(2, 1);
topology.add_bond(2, 3);
assert_eq!(topology.bonds(), vec![[0, 1], [1, 2], [2, 3]]);
source

pub fn angles(&self) -> Vec<[usize; 3]>

Get the list of angles in the topology.

Example
let mut topology = Topology::new();
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(2, 1);
topology.add_bond(2, 3);
assert_eq!(topology.angles(), vec![[0, 1, 2], [1, 2, 3]]);
source

pub fn dihedrals(&self) -> Vec<[usize; 4]>

Get the list of dihedral angles in the topology.

Example
let mut topology = Topology::new();
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(2, 1);
topology.add_bond(2, 3);

assert_eq!(topology.dihedrals(), vec![[0, 1, 2, 3]]);
source

pub fn impropers(&self) -> Vec<[usize; 4]>

Get the list of improper dihedral angles in the topology.

Example
let mut topology = Topology::new();
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(0, 2);
topology.add_bond(0, 3);

assert_eq!(topology.impropers(), vec![[1, 0, 2, 3]]);
source

pub fn clear_bonds(&mut self)

Remove all existing bonds, angles, dihedral angles and improper dihedral angles in the topology.

Example
let mut topology = Topology::new();
assert_eq!(topology.bonds_count(), 0);
topology.resize(4);
topology.add_bond(0, 1);
topology.add_bond(0, 2);
assert_eq!(topology.bonds_count(), 2);
assert_eq!(topology.angles().len(), 1);

topology.clear_bonds();
assert!(topology.bonds().is_empty());
assert!(topology.angles().is_empty());
source

pub fn add_bond(&mut self, i: usize, j: usize)

Add a bond between the atoms at indexes i and j in the topology.

The bond order is set to BondOrder::Unknown.

Example
let mut topology = Topology::new();
assert_eq!(topology.bonds_count(), 0);
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(0, 2);
assert_eq!(topology.bonds_count(), 2);

assert_eq!(topology.bond_order(0, 1), BondOrder::Unknown);
source

pub fn add_bond_with_order(&mut self, i: usize, j: usize, order: BondOrder)

Add a bond between the atoms at indexes i and j in the topology with the given bond order.

Example
let mut topology = Topology::new();
assert_eq!(topology.bonds_count(), 0);
topology.resize(2);

topology.add_bond_with_order(0, 1, BondOrder::Double);
assert_eq!(topology.bond_order(0, 1), BondOrder::Double);
source

pub fn bond_order(&self, i: usize, j: usize) -> BondOrder

Get the bond order for the bond between the atoms at indexes i and j.

Example
let mut topology = Topology::new();
assert_eq!(topology.bonds_count(), 0);
topology.resize(2);

topology.add_bond_with_order(0, 1, BondOrder::Double);
assert_eq!(topology.bond_order(0, 1), BondOrder::Double);
source

pub fn bond_orders(&self) -> Vec<BondOrder>

Get the bond order for all the bonds in the topology

Example
let mut topology = Topology::new();
assert_eq!(topology.bonds_count(), 0);
topology.resize(3);

topology.add_bond_with_order(0, 1, BondOrder::Double);
topology.add_bond_with_order(0, 2, BondOrder::Single);

assert_eq!(topology.bond_orders(), &[BondOrder::Double, BondOrder::Single]);
source

pub fn remove_bond(&mut self, i: usize, j: usize)

Remove any existing bond between the atoms at indexes i and j in this topology.

This function does nothing if there is no bond between i and j.

Example
let mut topology = Topology::new();
assert_eq!(topology.bonds_count(), 0);
topology.resize(4);

topology.add_bond(0, 1);
topology.add_bond(1, 2);
assert_eq!(topology.bonds_count(), 2);

topology.remove_bond(0, 1);
assert_eq!(topology.bonds_count(), 1);

// Removing a bond that does not exists is fine
topology.remove_bond(0, 2);
assert_eq!(topology.bonds_count(), 1);
source

pub fn residue(&self, index: usize) -> Option<ResidueRef<'_>>

Get a reference to the residue at index index from this topology.

The residue index in the topology is not always the same as the residue id.

Example
let mut topology = Topology::new();
topology.add_residue(&Residue::new("water")).unwrap();

let residue = topology.residue(0).unwrap();
assert_eq!(residue.name(), "water");
source

pub fn residue_for_atom(&self, index: usize) -> Option<ResidueRef<'_>>

Get a copy of the residue containing the atom at index index in this topology, if any.

Example
let mut topology = Topology::new();
topology.resize(8);

let mut residue = Residue::new("water");
residue.add_atom(0);
residue.add_atom(1);
residue.add_atom(2);
topology.add_residue(&residue).unwrap();

let residue = topology.residue_for_atom(0).unwrap();
assert_eq!(residue.name(), "water");

assert!(topology.residue_for_atom(6).is_none());
source

pub fn residues_count(&self) -> u64

Get the number of residues in this topology.

Example
let mut topology = Topology::new();
assert_eq!(topology.residues_count(), 0);

topology.add_residue(&Residue::with_id("water", 0)).unwrap();
topology.add_residue(&Residue::with_id("protein", 1)).unwrap();
assert_eq!(topology.residues_count(), 2);
source

pub fn add_residue(&mut self, residue: &Residue) -> Result<(), Error>

Add a residue to this topology.

Errors

This function fails is the residue id is not already in the topology, or if the residue contains atoms that are already in another residue.

Example
let mut topology = Topology::new();
topology.add_residue(&Residue::new("water")).unwrap();

let residue = topology.residue(0).unwrap();
assert_eq!(residue.name(), "water");
source

pub fn are_linked(&self, first: &Residue, second: &Residue) -> bool

Check if the two residues first and second from the topology are linked together, i.e. if there is a bond between one atom in the first residue and one atom in the second one.

Example
let mut topology = Topology::new();

topology.add_residue(&Residue::with_id("water", 0)).unwrap();
topology.add_residue(&Residue::with_id("protein", 1)).unwrap();

let first = topology.residue(0).unwrap();
let second = topology.residue(1).unwrap();
assert_eq!(topology.are_linked(&first, &second), false);

Trait Implementations§

source§

impl Clone for Topology

source§

fn clone(&self) -> Topology

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Topology

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Topology

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.