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
impl Molecule
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty molecule.
use inchi::Molecule;
let mol = Molecule::new();
assert_eq!(mol.atom_count(), 0);Sourcepub fn add_atom(&mut self, atom: Atom) -> usize
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);Sourcepub fn add_bond(&mut self, a: usize, b: usize, order: BondOrder) -> Result<()>
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());Sourcepub fn add_stereo(&mut self, stereo: Stereo)
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);Sourcepub fn add_polymer_unit(&mut self, unit: PolymerUnit)
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"));Sourcepub fn atom_count(&self) -> usize
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);Sourcepub fn bond_count(&self) -> usize
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);Sourcepub fn stereo_count(&self) -> usize
pub fn stereo_count(&self) -> usize
The number of 0D stereo descriptors.
assert_eq!(Molecule::new().stereo_count(), 0);Sourcepub fn to_inchi(&self, options: impl Into<Options>) -> Result<InchiOutput>
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");