use rustc_hash::{FxHashMap, FxHashSet};
use chematic_core::{AtomIdx, BondIdx, BondOrder, Molecule, implicit_hcount};
use crate::sssr::find_sssr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RingAromaticity {
Aromatic,
Antiaromatic,
NonAromatic,
}
#[derive(Debug, Clone)]
pub struct AromaticityModel {
aromatic_atoms: FxHashSet<AtomIdx>,
aromatic_bonds: FxHashSet<BondIdx>,
antiaromatic_rings: Vec<Vec<AtomIdx>>,
ring_classifications: Vec<(Vec<AtomIdx>, RingAromaticity, u32)>,
}
impl AromaticityModel {
pub fn is_atom_aromatic(&self, idx: AtomIdx) -> bool {
self.aromatic_atoms.contains(&idx)
}
pub fn is_bond_aromatic(&self, idx: BondIdx) -> bool {
self.aromatic_bonds.contains(&idx)
}
pub fn aromatic_atom_count(&self) -> usize {
self.aromatic_atoms.len()
}
pub fn ring_classifications(&self) -> &[(Vec<AtomIdx>, RingAromaticity, u32)] {
&self.ring_classifications
}
pub fn antiaromatic_rings(&self) -> &[Vec<AtomIdx>] {
&self.antiaromatic_rings
}
pub fn has_antiaromaticity(&self) -> bool {
!self.antiaromatic_rings.is_empty()
}
}
#[allow(clippy::manual_is_multiple_of)]
fn classify_ring_aromaticity(pi_electrons: u32) -> (RingAromaticity, u32) {
if pi_electrons >= 2 && (pi_electrons - 2) % 4 == 0 {
(RingAromaticity::Aromatic, pi_electrons)
} else if pi_electrons > 0 && pi_electrons % 4 == 0 {
(RingAromaticity::Antiaromatic, pi_electrons)
} else {
(RingAromaticity::NonAromatic, pi_electrons)
}
}
fn mark_ring_aromatic(
mol: &Molecule,
ring: &[AtomIdx],
aromatic_atoms: &mut FxHashSet<AtomIdx>,
aromatic_bonds: &mut FxHashSet<BondIdx>,
) {
for &atom in ring {
aromatic_atoms.insert(atom);
}
for i in 0..ring.len() {
let a = ring[i];
let b = ring[(i + 1) % ring.len()];
if let Some((bidx, _)) = mol.bond_between(a, b) {
aromatic_bonds.insert(bidx);
}
}
}
pub fn assign_aromaticity(mol: &Molecule) -> AromaticityModel {
let ring_set = find_sssr(mol);
let sssr_rings = ring_set.rings();
let rings: Vec<Vec<AtomIdx>> = augmented_ring_set(mol, sssr_rings);
let mut aromatic_atoms: FxHashSet<AtomIdx> = FxHashSet::default();
let mut aromatic_bonds: FxHashSet<BondIdx> = FxHashSet::default();
let mut antiaromatic_rings: Vec<Vec<AtomIdx>> = Vec::new();
let mut classifications: Vec<Option<(RingAromaticity, u32)>> = vec![None; rings.len()];
let mut pass2_candidates: Vec<usize> = Vec::new();
let empty_context = FxHashSet::default();
for (ring_idx, ring) in rings.iter().enumerate() {
match ring_pi_electrons(mol, ring, &empty_context) {
Some(pi) => {
let (cls, count) = classify_ring_aromaticity(pi);
classifications[ring_idx] = Some((cls, count));
match cls {
RingAromaticity::Aromatic => {
mark_ring_aromatic(mol, ring, &mut aromatic_atoms, &mut aromatic_bonds);
}
RingAromaticity::Antiaromatic => {
antiaromatic_rings.push(ring.to_vec());
}
RingAromaticity::NonAromatic => {
pass2_candidates.push(ring_idx);
}
}
}
None => {
pass2_candidates.push(ring_idx);
}
}
}
loop {
let mut any_new = false;
let mut still_pending: Vec<usize> = Vec::new();
for ring_idx in pass2_candidates {
let ring = &rings[ring_idx];
if !ring.iter().any(|a| aromatic_atoms.contains(a)) {
still_pending.push(ring_idx);
continue;
}
match ring_pi_electrons(mol, ring, &aromatic_atoms) {
Some(pi) => {
let (cls, count) = classify_ring_aromaticity(pi);
classifications[ring_idx] = Some((cls, count));
if matches!(cls, RingAromaticity::Aromatic) {
mark_ring_aromatic(mol, ring, &mut aromatic_atoms, &mut aromatic_bonds);
any_new = true;
}
}
None => {
still_pending.push(ring_idx);
}
}
}
pass2_candidates = still_pending;
if !any_new {
break;
}
}
let ring_classifications: Vec<(Vec<AtomIdx>, RingAromaticity, u32)> = rings
.iter()
.take(sssr_rings.len()) .enumerate()
.filter_map(|(i, ring)| classifications[i].map(|(cls, count)| (ring.to_vec(), cls, count)))
.collect();
AromaticityModel {
aromatic_atoms,
aromatic_bonds,
antiaromatic_rings,
ring_classifications,
}
}
pub fn apply_aromaticity(mol: &Molecule) -> Molecule {
use chematic_core::{BondOrder, MoleculeBuilder};
let model = assign_aromaticity(mol);
let mut builder = MoleculeBuilder::new();
for (idx, atom) in mol.atoms() {
let mut a = atom.clone();
if model.is_atom_aromatic(idx) {
a.aromatic = true;
}
builder.add_atom(a);
}
for (bidx, bond) in mol.bonds() {
let order = if model.is_bond_aromatic(bidx) {
BondOrder::Aromatic
} else {
bond.order
};
let _ = builder.add_bond(bond.atom1, bond.atom2, order);
}
builder.build()
}
fn ring_bond_set(mol: &Molecule, ring: &[AtomIdx]) -> Vec<BondIdx> {
let n = ring.len();
let mut bonds: Vec<BondIdx> = (0..n)
.filter_map(|i| {
let a = ring[i];
let b = ring[(i + 1) % n];
mol.bond_between(a, b).map(|(bidx, _)| bidx)
})
.collect();
bonds.sort();
bonds
}
fn bond_sym_diff(a: &[BondIdx], b: &[BondIdx]) -> Vec<BondIdx> {
let mut result: Vec<BondIdx> = Vec::new();
let mut i = 0;
let mut j = 0;
while i < a.len() && j < b.len() {
match a[i].cmp(&b[j]) {
std::cmp::Ordering::Less => {
result.push(a[i]);
i += 1;
}
std::cmp::Ordering::Greater => {
result.push(b[j]);
j += 1;
}
std::cmp::Ordering::Equal => {
i += 1;
j += 1;
}
}
}
result.extend_from_slice(&a[i..]);
result.extend_from_slice(&b[j..]);
result
}
fn ring_atoms_from_bond_set(mol: &Molecule, bonds: &[BondIdx]) -> Option<Vec<AtomIdx>> {
if bonds.is_empty() {
return None;
}
let mut adj: FxHashMap<AtomIdx, [Option<AtomIdx>; 2]> = FxHashMap::default();
for &bidx in bonds {
let bond = mol.bond(bidx);
for (a, b) in [(bond.atom1, bond.atom2), (bond.atom2, bond.atom1)] {
let e = adj.entry(a).or_insert([None; 2]);
if e[0].is_none() {
e[0] = Some(b);
} else if e[1].is_none() {
e[1] = Some(b);
} else {
return None; }
}
}
if adj.values().any(|e| e[1].is_none()) {
return None;
}
let start = *adj.keys().next()?;
let mut path = vec![start];
let mut prev = start;
let mut current = adj[&start][0]?;
while current != start {
path.push(current);
let [n0, n1] = adj[¤t];
let next = if n0 == Some(prev) { n1? } else { n0? };
prev = current;
current = next;
}
if path.len() != bonds.len() {
return None;
}
Some(path)
}
pub fn augmented_ring_set(mol: &Molecule, sssr_rings: &[Vec<AtomIdx>]) -> Vec<Vec<AtomIdx>> {
let mut rings: Vec<Vec<AtomIdx>> = sssr_rings.to_vec();
let mut known: FxHashSet<Vec<AtomIdx>> = sssr_rings
.iter()
.map(|r| {
let mut s = r.clone();
s.sort();
s
})
.collect();
loop {
let mut changed = false;
let n = rings.len();
let bond_sets: Vec<Vec<BondIdx>> = rings.iter().map(|r| ring_bond_set(mol, r)).collect();
for i in 0..n {
for j in (i + 1)..n {
let shares_atom = rings[i].iter().any(|a| rings[j].contains(a));
if !shares_atom {
continue;
}
let xor_bonds = bond_sym_diff(&bond_sets[i], &bond_sets[j]);
if xor_bonds.is_empty() {
continue;
}
if xor_bonds.len() >= rings[i].len().max(rings[j].len()) {
continue;
}
if let Some(new_ring) = ring_atoms_from_bond_set(mol, &xor_bonds) {
let mut key = new_ring.clone();
key.sort();
if known.insert(key) {
rings.push(new_ring);
changed = true;
}
}
}
}
if !changed {
break;
}
}
rings
}
pub fn count_aromatic_rings(mol: &Molecule) -> usize {
let mol_with_arom;
let mol = if mol.atoms().any(|(_, a)| a.aromatic) {
mol } else {
mol_with_arom = apply_aromaticity(mol);
&mol_with_arom
};
let sssr = crate::sssr::find_sssr(mol);
let aug = augmented_ring_set(mol, sssr.rings());
let aromatic: Vec<Vec<AtomIdx>> = aug
.into_iter()
.filter(|ring| ring.iter().all(|&idx| mol.atom(idx).aromatic))
.collect();
if aromatic.len() <= 1 {
return aromatic.len();
}
let bond_sets: Vec<Vec<BondIdx>> = aromatic.iter().map(|r| ring_bond_set(mol, r)).collect();
let n = aromatic.len();
let mut is_envelope = vec![false; n];
for i in 0..n {
let si = aromatic[i].len();
'jk: for j in 0..n {
if j == i || aromatic[j].len() >= si {
continue;
}
for k in (j + 1)..n {
if k == i || aromatic[k].len() >= si {
continue;
}
let xor = bond_sym_diff(&bond_sets[j], &bond_sets[k]);
if xor == bond_sets[i] {
is_envelope[i] = true;
break 'jk;
}
}
}
if !is_envelope[i] {
'jkl: for j in 0..n {
if j == i || aromatic[j].len() >= si {
continue;
}
for k in (j + 1)..n {
if k == i || aromatic[k].len() >= si {
continue;
}
let xor_jk = bond_sym_diff(&bond_sets[j], &bond_sets[k]);
for l in (k + 1)..n {
if l == i || aromatic[l].len() >= si {
continue;
}
let xor_jkl = bond_sym_diff(&xor_jk, &bond_sets[l]);
if xor_jkl == bond_sets[i] {
is_envelope[i] = true;
break 'jkl;
}
}
}
}
}
if !is_envelope[i] {
'jklm: for j in 0..n {
if j == i || aromatic[j].len() >= si {
continue;
}
for k in (j + 1)..n {
if k == i || aromatic[k].len() >= si {
continue;
}
let xor_jk = bond_sym_diff(&bond_sets[j], &bond_sets[k]);
for l in (k + 1)..n {
if l == i || aromatic[l].len() >= si {
continue;
}
let xor_jkl = bond_sym_diff(&xor_jk, &bond_sets[l]);
for m in (l + 1)..n {
if m == i || aromatic[m].len() >= si {
continue;
}
let xor_jklm = bond_sym_diff(&xor_jkl, &bond_sets[m]);
if xor_jklm == bond_sets[i] {
is_envelope[i] = true;
break 'jklm;
}
}
}
}
}
}
}
is_envelope.iter().filter(|&&e| !e).count()
}
fn ring_pi_electrons(
mol: &Molecule,
ring: &[AtomIdx],
aromatic_context: &FxHashSet<AtomIdx>,
) -> Option<u32> {
let ring_atom_set: FxHashSet<AtomIdx> = ring.iter().copied().collect();
let mut total_pi: u32 = 0;
for &atom_idx in ring {
if aromatic_context.contains(&atom_idx) {
total_pi += 1;
continue;
}
let atom = mol.atom(atom_idx);
let an = atom.element.atomic_number();
let ring_degree = mol
.neighbors(atom_idx)
.filter(|(nb, _)| ring_atom_set.contains(nb))
.count();
let total_degree = mol.degree(atom_idx);
let has_explicit_double = mol
.neighbors(atom_idx)
.any(|(_, bidx)| mol.bond(bidx).order == BondOrder::Double);
let has_double_any = has_explicit_double
|| mol
.neighbors(atom_idx)
.any(|(_, bidx)| mol.bond(bidx).order == BondOrder::Aromatic);
let has_aromatic_in_ring = mol
.neighbors(atom_idx)
.filter(|(nb, _)| ring_atom_set.contains(nb))
.any(|(_, bidx)| mol.bond(bidx).order == BondOrder::Aromatic);
let pi = match an {
6 => {
if !has_double_any {
return None; }
1
}
7 => {
if implicit_hcount(mol, atom_idx) > 0 {
2
} else if has_explicit_double {
1
} else if total_degree == 3 && ring_degree < total_degree {
2
} else if has_aromatic_in_ring {
1
} else {
return None;
}
}
8 | 16 => {
if ring_degree != 2 {
return None;
}
2
}
_ => return None,
};
total_pi += pi;
}
Some(total_pi)
}
#[cfg(test)]
mod tests {
use super::*;
use chematic_core::{Atom, BondOrder, Element, MoleculeBuilder};
fn benzene_kekule() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let atoms: Vec<_> = (0..6).map(|_| b.add_atom(Atom::new(Element::C))).collect();
for i in 0..6 {
let order = if i % 2 == 0 {
BondOrder::Double
} else {
BondOrder::Single
};
b.add_bond(atoms[i], atoms[(i + 1) % 6], order).unwrap();
}
b.build()
}
fn cyclohexane() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let atoms: Vec<_> = (0..6).map(|_| b.add_atom(Atom::new(Element::C))).collect();
for i in 0..6 {
b.add_bond(atoms[i], atoms[(i + 1) % 6], BondOrder::Single)
.unwrap();
}
b.build()
}
fn pyridine_kekule() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let n = b.add_atom(Atom::new(Element::N));
let atoms_c: Vec<_> = (0..5).map(|_| b.add_atom(Atom::new(Element::C))).collect();
let ring = [
n, atoms_c[0], atoms_c[1], atoms_c[2], atoms_c[3], atoms_c[4],
];
for i in 0..6 {
let order = if i % 2 == 0 {
BondOrder::Double
} else {
BondOrder::Single
};
b.add_bond(ring[i], ring[(i + 1) % 6], order).unwrap();
}
b.build()
}
fn furan_kekule() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let o = b.add_atom(Atom::new(Element::O));
let c1 = b.add_atom(Atom::new(Element::C));
let c2 = b.add_atom(Atom::new(Element::C));
let c3 = b.add_atom(Atom::new(Element::C));
let c4 = b.add_atom(Atom::new(Element::C));
let ring = [o, c1, c2, c3, c4];
b.add_bond(ring[0], ring[1], BondOrder::Single).unwrap();
b.add_bond(ring[1], ring[2], BondOrder::Double).unwrap();
b.add_bond(ring[2], ring[3], BondOrder::Single).unwrap();
b.add_bond(ring[3], ring[4], BondOrder::Double).unwrap();
b.add_bond(ring[4], ring[0], BondOrder::Single).unwrap();
b.build()
}
fn pyrrole_kekule() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let mut n_atom = Atom::new(Element::N);
n_atom.hydrogen_count = Some(1);
let n = b.add_atom(n_atom);
let c1 = b.add_atom(Atom::new(Element::C));
let c2 = b.add_atom(Atom::new(Element::C));
let c3 = b.add_atom(Atom::new(Element::C));
let c4 = b.add_atom(Atom::new(Element::C));
let ring = [n, c1, c2, c3, c4];
b.add_bond(ring[0], ring[1], BondOrder::Single).unwrap();
b.add_bond(ring[1], ring[2], BondOrder::Double).unwrap();
b.add_bond(ring[2], ring[3], BondOrder::Single).unwrap();
b.add_bond(ring[3], ring[4], BondOrder::Double).unwrap();
b.add_bond(ring[4], ring[0], BondOrder::Single).unwrap();
b.build()
}
fn naphthalene_kekule() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let atoms: Vec<_> = (0..10).map(|_| b.add_atom(Atom::new(Element::C))).collect();
let ring1 = [0usize, 1, 2, 3, 4, 9];
let orders1 = [
BondOrder::Double,
BondOrder::Single,
BondOrder::Double,
BondOrder::Single,
BondOrder::Double,
BondOrder::Single,
];
for i in 0..6 {
b.add_bond(atoms[ring1[i]], atoms[ring1[(i + 1) % 6]], orders1[i])
.unwrap();
}
let ring2_extra = [(4, 5), (5, 6), (6, 7), (7, 8), (8, 9)];
let orders2 = [
BondOrder::Single,
BondOrder::Double,
BondOrder::Single,
BondOrder::Double,
BondOrder::Single,
];
for (i, &(a, bb)) in ring2_extra.iter().enumerate() {
b.add_bond(atoms[a], atoms[bb], orders2[i]).unwrap();
}
b.build()
}
fn cyclobutadiene_kekule() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let atoms: Vec<_> = (0..4).map(|_| b.add_atom(Atom::new(Element::C))).collect();
for i in 0..4 {
let order = if i % 2 == 0 {
BondOrder::Double
} else {
BondOrder::Single
};
b.add_bond(atoms[i], atoms[(i + 1) % 4], order).unwrap();
}
b.build()
}
fn cyclooctatetraene_kekule() -> chematic_core::Molecule {
let mut b = MoleculeBuilder::new();
let atoms: Vec<_> = (0..8).map(|_| b.add_atom(Atom::new(Element::C))).collect();
for i in 0..8 {
let order = if i % 2 == 0 {
BondOrder::Double
} else {
BondOrder::Single
};
b.add_bond(atoms[i], atoms[(i + 1) % 8], order).unwrap();
}
b.build()
}
#[cfg(test)]
fn mol_aromatic(smiles: &str) -> chematic_core::Molecule {
chematic_smiles::parse(smiles).expect("valid SMILES")
}
#[cfg(test)]
fn mol_kekulized(smiles: &str) -> chematic_core::Molecule {
let mol = chematic_smiles::parse(smiles).expect("valid SMILES");
let k = chematic_core::kekulize(&mol).expect("kekulizable");
chematic_core::apply_kekule(&mol, &k)
}
#[test]
fn test_benzene_is_aromatic() {
let mol = benzene_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
6,
"all 6 benzene atoms aromatic"
);
for i in 0..6u32 {
assert!(model.is_atom_aromatic(AtomIdx(i)));
}
}
#[test]
fn test_cyclohexane_not_aromatic() {
let mol = cyclohexane();
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 0, "cyclohexane not aromatic");
}
#[test]
fn test_pyridine_is_aromatic() {
let mol = pyridine_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 6);
}
#[test]
fn test_furan_is_aromatic() {
let mol = furan_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 5);
}
#[test]
fn test_pyrrole_is_aromatic() {
let mol = pyrrole_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 5);
}
#[test]
fn test_naphthalene_both_rings_aromatic() {
let mol = naphthalene_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
10,
"all 10 naphthalene atoms aromatic"
);
}
#[test]
fn test_bond_aromaticity_benzene() {
let mol = benzene_kekule();
let model = assign_aromaticity(&mol);
let count = mol
.bonds()
.filter(|(b, _)| model.is_bond_aromatic(*b))
.count();
assert_eq!(count, 6);
}
#[test]
fn test_apply_aromaticity_benzene() {
let mol = benzene_kekule();
let aromatic = apply_aromaticity(&mol);
for (_, atom) in aromatic.atoms() {
assert!(atom.aromatic, "every benzene carbon should be aromatic");
}
let aromatic_bond_count = aromatic
.bonds()
.filter(|(_, b)| b.order == BondOrder::Aromatic)
.count();
assert_eq!(aromatic_bond_count, 6);
}
#[test]
fn test_apply_aromaticity_cyclohexane_unchanged() {
let mol = cyclohexane();
let result = apply_aromaticity(&mol);
for (_, atom) in result.atoms() {
assert!(!atom.aromatic);
}
for (_, bond) in result.bonds() {
assert_ne!(bond.order, BondOrder::Aromatic);
}
}
#[test]
fn test_cyclobutadiene_antiaromatic() {
let mol = cyclobutadiene_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
0,
"cyclobutadiene not aromatic"
);
assert!(model.has_antiaromaticity(), "cyclobutadiene antiaromatic");
assert_eq!(model.antiaromatic_rings().len(), 1);
let classifications = model.ring_classifications();
assert_eq!(classifications.len(), 1);
assert_eq!(classifications[0].1, RingAromaticity::Antiaromatic);
assert_eq!(classifications[0].2, 4);
}
#[test]
fn test_cyclooctatetraene_antiaromatic() {
let mol = cyclooctatetraene_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 0, "COT not aromatic");
assert!(model.has_antiaromaticity(), "COT antiaromatic");
assert_eq!(model.antiaromatic_rings().len(), 1);
let cls = &model.ring_classifications()[0];
assert_eq!(cls.1, RingAromaticity::Antiaromatic);
assert_eq!(cls.2, 8);
}
#[test]
fn test_ring_classifications_benzene() {
let mol = benzene_kekule();
let model = assign_aromaticity(&mol);
let classifications = model.ring_classifications();
assert_eq!(classifications.len(), 1);
assert_eq!(classifications[0].1, RingAromaticity::Aromatic);
assert_eq!(classifications[0].2, 6);
}
#[test]
fn test_ring_classifications_naphthalene() {
let mol = naphthalene_kekule();
let model = assign_aromaticity(&mol);
let classifications = model.ring_classifications();
assert_eq!(classifications.len(), 2, "naphthalene has two rings");
for (_, classification, count) in classifications {
assert_eq!(*classification, RingAromaticity::Aromatic);
assert_eq!(*count, 6);
}
}
#[test]
fn test_non_aromatic_cyclohexane() {
let mol = cyclohexane();
let model = assign_aromaticity(&mol);
for (_, classification, _) in model.ring_classifications() {
assert_ne!(*classification, RingAromaticity::Aromatic);
assert_ne!(*classification, RingAromaticity::Antiaromatic);
}
}
#[test]
fn test_thiophene_aromatic() {
let mut b = MoleculeBuilder::new();
let s = b.add_atom(Atom::new(Element::S));
let c1 = b.add_atom(Atom::new(Element::C));
let c2 = b.add_atom(Atom::new(Element::C));
let c3 = b.add_atom(Atom::new(Element::C));
let c4 = b.add_atom(Atom::new(Element::C));
let ring = [s, c1, c2, c3, c4];
b.add_bond(ring[0], ring[1], BondOrder::Single).unwrap();
b.add_bond(ring[1], ring[2], BondOrder::Double).unwrap();
b.add_bond(ring[2], ring[3], BondOrder::Single).unwrap();
b.add_bond(ring[3], ring[4], BondOrder::Double).unwrap();
b.add_bond(ring[4], ring[0], BondOrder::Single).unwrap();
let mol = b.build();
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 5);
assert_eq!(model.ring_classifications()[0].2, 6);
}
#[test]
fn test_electron_distribution_tracking() {
let mol = benzene_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(model.ring_classifications()[0].2, 6, "benzene: 6 × 1π = 6");
let mol = pyrrole_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(
model.ring_classifications()[0].2,
6,
"pyrrole: N(2π) + 4C(1π) = 6"
);
let mol = furan_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(
model.ring_classifications()[0].2,
6,
"furan: O(2π) + 4C(1π) = 6"
);
}
#[test]
fn test_benzene_aromatic_smiles() {
let mol = mol_aromatic("c1ccccc1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
6,
"benzene from aromatic SMILES"
);
}
#[test]
fn test_naphthalene_aromatic_smiles() {
let mol = mol_aromatic("c1ccc2ccccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
10,
"naphthalene from aromatic SMILES"
);
}
#[test]
fn test_pyridine_aromatic_smiles() {
let mol = mol_aromatic("c1ccncc1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
6,
"pyridine from aromatic SMILES"
);
}
#[test]
fn test_furan_aromatic_smiles() {
let mol = mol_aromatic("c1ccoc1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 5, "furan from aromatic SMILES");
}
#[test]
fn test_pyrrole_aromatic_smiles() {
let mol = mol_aromatic("c1cc[nH]c1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
5,
"pyrrole from aromatic SMILES"
);
}
#[test]
fn test_thiophene_aromatic_smiles() {
let mol = mol_aromatic("c1ccsc1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
5,
"thiophene from aromatic SMILES"
);
}
#[test]
fn test_indole_aromatic() {
let mol = mol_kekulized("c1ccc2[nH]ccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
9,
"all 9 indole atoms aromatic"
);
}
#[test]
fn test_benzimidazole_aromatic() {
let mol = mol_kekulized("c1ccc2[nH]cnc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 9, "all 9 benzimidazole atoms");
}
#[test]
fn test_quinoline_aromatic() {
let mol = mol_kekulized("c1ccc2ncccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 10, "all 10 quinoline atoms");
}
#[test]
fn test_acridine_aromatic() {
let mol = mol_kekulized("c1ccc2nc3ccccc3cc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 14, "all 14 acridine atoms");
}
#[test]
fn test_indolizine_aromatic() {
let mol = mol_aromatic("c1ccn2cccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
9,
"all 9 indolizine atoms aromatic"
);
let has_aromatic_ring = model
.ring_classifications()
.iter()
.any(|(_, cls, _)| *cls == RingAromaticity::Aromatic);
assert!(has_aromatic_ring, "at least one SSSR ring aromatic");
}
#[test]
fn test_purine_aromatic() {
let mol = mol_kekulized("c1cnc2[nH]cnc2n1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
9,
"all 9 purine atoms aromatic"
);
}
#[test]
fn test_purine_aromatic_from_aromatic_smiles() {
let mol = mol_aromatic("c1cnc2[nH]cnc2n1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
9,
"purine from aromatic SMILES"
);
}
#[test]
fn test_2_pyridinone_aromatic() {
let mol = mol_aromatic("O=c1ccncc1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
6,
"all 6 ring atoms of 2-pyridinone aromatic"
);
}
#[test]
fn test_quinolone_aromatic() {
let mol = mol_aromatic("O=c1ccc2ncccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
10,
"all 10 quinolone ring atoms aromatic"
);
assert_eq!(
model.ring_classifications().len(),
2,
"two rings classified"
);
}
#[test]
fn test_indole_aromatic_smiles() {
let mol = mol_aromatic("c1ccc2[nH]ccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
9,
"indole from aromatic SMILES"
);
}
#[test]
fn test_bridgehead_n_contributes_lone_pair() {
let mol = mol_aromatic("c1ccn2cccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 9);
assert!(
model.is_atom_aromatic(AtomIdx(3)),
"bridgehead N must be aromatic"
);
}
#[test]
fn test_non_bridgehead_n_no_false_positive() {
let mol = mol_aromatic("c1ccncn1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 6, "pyrimidine is aromatic");
}
#[test]
fn test_imidazole_aromatic() {
let mol = mol_aromatic("c1cn[nH]c1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 5, "imidazole is aromatic");
}
#[test]
fn test_pass2_needed_for_indolizine_6ring() {
let mol = mol_aromatic("c1ccn2cccc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
9,
"all 9 indolizine atoms aromatic"
);
assert!(
model.is_atom_aromatic(AtomIdx(3)),
"bridgehead N is aromatic"
);
let aromatic_count = model
.ring_classifications()
.iter()
.filter(|(_, cls, _)| *cls == RingAromaticity::Aromatic)
.count();
assert!(aromatic_count >= 1, "at least one SSSR ring is aromatic");
}
#[test]
fn test_no_pass2_needed_for_naphthalene() {
let mol = naphthalene_kekule();
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 10);
let classes = model.ring_classifications();
assert_eq!(classes.len(), 2);
for (_, cls, _) in classes {
assert_eq!(*cls, RingAromaticity::Aromatic);
}
}
#[test]
fn test_anthracene_aromatic() {
let mol = mol_kekulized("c1ccc2cc3ccccc3cc2c1");
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 14, "all 14 anthracene atoms");
}
#[test]
fn test_kekulized_path_unaffected_by_aromatic_bond_changes() {
let mol = benzene_kekule();
for (_, bond) in mol.bonds() {
assert_ne!(bond.order, BondOrder::Aromatic, "input must be kekulized");
}
let model = assign_aromaticity(&mol);
assert_eq!(model.aromatic_atom_count(), 6);
let aromatic_bonds = mol
.bonds()
.filter(|(b, _)| model.is_bond_aromatic(*b))
.count();
assert_eq!(aromatic_bonds, 6);
}
#[test]
fn test_keto_pyridinone_not_huckel_aromatic() {
let mol = mol_kekulized("O=C1NC=CC=C1");
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
0,
"keto pyridinone is not Hückel aromatic (7π ≠ 4n+2)"
);
}
#[test]
fn test_fluorescein_dianion_aromatic() {
let smi = "C1=CC=C(C(=C1)C2=C3C=CC(=O)C=C3OC4=C2C=CC(=C4)[O-])C(=O)[O-]";
let mol = chematic_smiles::parse(smi).expect("fluorescein dianion should parse");
let arc = count_aromatic_rings(&mol);
assert!(
arc >= 2,
"fluorescein dianion: expected ≥2 aromatic rings, got {arc} \
(RDKit #9271: charged aromatics may be misclassified)"
);
}
#[test]
fn test_rhodamine_zwitterion_parses() {
let smi = "CCN(CC)c1ccc2c(-c3ccccc3C(=O)O)c3ccc(=[N+](CC)CC)cc-3oc2c1";
let mol = chematic_smiles::parse(smi).expect("rhodamine zwitterion should parse");
let arc = count_aromatic_rings(&mol);
assert!(arc >= 3, "rhodamine: expected ≥3 aromatic rings, got {arc}");
}
#[test]
fn test_cyclopentadienyl_not_aromatic_kekulized() {
let mut b = MoleculeBuilder::new();
let c0 = b.add_atom(Atom::new(Element::C)); let c1 = b.add_atom(Atom::new(Element::C));
let c2 = b.add_atom(Atom::new(Element::C));
let c3 = b.add_atom(Atom::new(Element::C));
let c4 = b.add_atom(Atom::new(Element::C));
b.add_bond(c0, c1, BondOrder::Single).unwrap();
b.add_bond(c1, c2, BondOrder::Double).unwrap();
b.add_bond(c2, c3, BondOrder::Single).unwrap();
b.add_bond(c3, c4, BondOrder::Double).unwrap();
b.add_bond(c4, c0, BondOrder::Single).unwrap();
let mol = b.build();
let model = assign_aromaticity(&mol);
assert_eq!(
model.aromatic_atom_count(),
0,
"cyclopentadiene not aromatic"
);
}
}