Skip to main content

Molecule

Struct Molecule 

Source
pub struct Molecule { /* private fields */ }
Expand description

A molecular structure assembled programmatically from atoms, bonds, and 0D stereo descriptors.

Atoms are referenced by the index returned from Molecule::add_atom (also the order in which they are added, starting at 0).

use inchi::{Molecule, Atom, BondOrder};

// Ethanol: C-C-O (implicit hydrogens added automatically).
let mut mol = Molecule::new();
let c1 = mol.add_atom(Atom::new("C"));
let c2 = mol.add_atom(Atom::new("C"));
let o = mol.add_atom(Atom::new("O"));
mol.add_bond(c1, c2, BondOrder::Single)?;
mol.add_bond(c2, o, BondOrder::Single)?;

let out = mol.to_inchi(())?;
assert_eq!(out.inchi(), "InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3");

Implementations§

Source§

impl Molecule

Source

pub fn new() -> Self

Creates an empty molecule.

use inchi::Molecule;
let mol = Molecule::new();
assert_eq!(mol.atom_count(), 0);
Source

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

Adds an atom and returns its index.

use inchi::{Molecule, Atom};
let mut mol = Molecule::new();
assert_eq!(mol.add_atom(Atom::new("C")), 0);
assert_eq!(mol.add_atom(Atom::new("O")), 1);
Source

pub fn add_bond(&mut self, a: usize, b: usize, order: BondOrder) -> Result<()>

Adds a bond between two existing atoms.

Returns InchiError::InvalidStructure if either index is out of range or if a == b.

use inchi::{Molecule, Atom, BondOrder};
let mut mol = Molecule::new();
let a = mol.add_atom(Atom::new("C"));
let b = mol.add_atom(Atom::new("O"));
mol.add_bond(a, b, BondOrder::Double)?;
assert!(mol.add_bond(a, 99, BondOrder::Single).is_err());
Source

pub fn add_stereo(&mut self, stereo: Stereo)

Adds a 0D stereo descriptor. Indices are validated at conversion time.

use inchi::{Molecule, Atom, Stereo, Parity};
let mut mol = Molecule::new();
for el in ["C", "C", "N", "O"] { mol.add_atom(Atom::new(el)); }
mol.add_stereo(Stereo::Tetrahedral { center: 0, neighbors: [1, 2, 3, 0], parity: Parity::Odd });
assert_eq!(mol.stereo_count(), 1);
Source

pub fn add_polymer_unit(&mut self, unit: PolymerUnit)

Adds a polymer structural repeating unit, switching InChI generation to the extended GetINCHIEx entry point.

Polymers require the Options::polymers flag to be set and yield a non-standard, beta-flagged InChI. The unit’s atom indices refer to atoms already added to this molecule; the two chain ends are normally capped with "Zz" star atoms.

use inchi::{Molecule, Atom, BondOrder, Options, Polymers, PolymerUnit};
// A polyethylene repeat unit: *-CH2-CH2-*
let mut mol = Molecule::new();
let s1 = mol.add_atom(Atom::new("Zz"));
let c1 = mol.add_atom(Atom::new("C"));
let c2 = mol.add_atom(Atom::new("C"));
let s2 = mol.add_atom(Atom::new("Zz"));
mol.add_bond(s1, c1, BondOrder::Single)?;
mol.add_bond(c1, c2, BondOrder::Single)?;
mol.add_bond(c2, s2, BondOrder::Single)?;
mol.add_polymer_unit(PolymerUnit::sru([c1, c2], [[s1, c1], [c2, s2]]));
let inchi = mol.to_inchi(Options::new().polymers(Polymers::On))?.into_inchi();
assert!(inchi.contains("/z"));
Source

pub fn atom_count(&self) -> usize

The number of atoms.

let mut mol = Molecule::new();
mol.add_atom(Atom::new("C"));
assert_eq!(mol.atom_count(), 1);
Source

pub fn bond_count(&self) -> usize

The number of bonds.

let mut mol = Molecule::new();
let a = mol.add_atom(Atom::new("C"));
let b = mol.add_atom(Atom::new("C"));
mol.add_bond(a, b, BondOrder::Single)?;
assert_eq!(mol.bond_count(), 1);
Source

pub fn stereo_count(&self) -> usize

The number of 0D stereo descriptors.

assert_eq!(Molecule::new().stereo_count(), 0);
Source

pub fn to_inchi(&self, options: impl Into<Options>) -> Result<InchiOutput>

Generates the InChI for this molecule using the given Options.

use inchi::{Molecule, Atom};
// A lone oxygen atom becomes water once implicit H are added.
let mut mol = Molecule::new();
mol.add_atom(Atom::new("O"));
assert_eq!(mol.to_inchi(())?.inchi(), "InChI=1S/H2O/h1H2");

Trait Implementations§

Source§

impl Clone for Molecule

Source§

fn clone(&self) -> Molecule

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Molecule

Source§

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

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

impl Default for Molecule

Source§

fn default() -> Molecule

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Molecule

Source§

fn eq(&self, other: &Molecule) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Molecule

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.