dreid-typer 0.2.1

A pure Rust library for DREIDING atom typing and molecular topology perception.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//! Evaluates fused ring systems to determine whether the atoms are aromatic, anti-aromatic, or neither.
//!
//! The module groups rings, builds localized models that count π-electrons under planarity
//! assumptions, and sets per-atom flags that later perception stages consume.

use super::model::{AnnotatedAtom, AnnotatedMolecule, Ring};
use crate::core::error::PerceptionError;
use crate::core::properties::BondOrder;
use std::collections::{HashMap, HashSet};

/// Runs aromaticity perception over all ring systems present in the molecule.
///
/// The procedure clusters rings that share atoms, evaluates each cluster as a whole, falls back to
/// ring-by-ring evaluation when mixed behavior occurs, and annotates atoms as aromatic or
/// anti-aromatic accordingly.
///
/// # Arguments
///
/// * `molecule` - Annotated molecule whose atom flags should be updated.
///
/// # Returns
///
/// `Ok(())` once every ring system has been processed or when no rings exist.
pub fn perceive(molecule: &mut AnnotatedMolecule) -> Result<(), PerceptionError> {
    if molecule.rings.is_empty() {
        return Ok(());
    }

    let ring_systems_indices = find_ring_systems(&molecule.rings);

    for system_indices in ring_systems_indices {
        let system_atoms: HashSet<usize> = system_indices
            .iter()
            .flat_map(|&i| molecule.rings[i].iter())
            .copied()
            .collect();

        let model = AromaticityModel::new(molecule, &system_atoms);

        if model.is_aromatic() {
            for &atom_id in &system_atoms {
                molecule.atoms[atom_id].is_aromatic = true;
            }
        } else if model.is_anti_aromatic() {
            for &atom_id in &system_atoms {
                molecule.atoms[atom_id].is_anti_aromatic = true;
            }
        } else {
            evaluate_rings_individually(molecule, &system_indices);
        }
    }

    Ok(())
}

/// Evaluates each ring independently when a fused system lacks uniform behavior.
///
/// # Arguments
///
/// * `molecule` - Annotated molecule to mutate.
/// * `system_indices` - Indices of rings belonging to the fused system.
fn evaluate_rings_individually(molecule: &mut AnnotatedMolecule, system_indices: &[usize]) {
    for &ring_idx in system_indices {
        let ring_atoms: HashSet<_> = molecule.rings[ring_idx].iter().copied().collect();
        let ring_model = AromaticityModel::new(molecule, &ring_atoms);

        if ring_model.is_aromatic() {
            for &atom_id in &ring_atoms {
                molecule.atoms[atom_id].is_aromatic = true;
            }
        } else if ring_model.is_anti_aromatic() {
            for &atom_id in &ring_atoms {
                molecule.atoms[atom_id].is_anti_aromatic = true;
            }
        }
    }
}

/// Local model capturing the atoms and π-electron count for a ring system.
struct AromaticityModel<'a> {
    /// Annotated molecule providing adjacency information.
    molecule: &'a AnnotatedMolecule,
    /// Atom IDs forming the current system under evaluation.
    atoms: HashSet<usize>,
    /// Computed π-electron count, if evaluation succeeded.
    pi_electrons: Option<u32>,
    /// Flag describing whether the atoms satisfy the planarity heuristic.
    is_potentially_planar: bool,
}

impl<'a> AromaticityModel<'a> {
    /// Constructs the model and immediately evaluates planarity and π-electrons.
    ///
    /// # Arguments
    ///
    /// * `molecule` - Annotated molecule backing the model.
    /// * `system_atoms` - Atom IDs representing a ring system.
    fn new(molecule: &'a AnnotatedMolecule, system_atoms: &HashSet<usize>) -> Self {
        let mut model = Self {
            molecule,
            atoms: system_atoms.clone(),
            pi_electrons: None,
            is_potentially_planar: false,
        };
        model.evaluate();
        model
    }

    /// Returns `true` when the Huckel 4n+2 rule is satisfied.
    fn is_aromatic(&self) -> bool {
        if !self.is_potentially_planar {
            return false;
        }
        matches!(self.pi_electrons, Some(pi) if pi > 0 && (pi - 2) % 4 == 0)
    }

    /// Returns `true` when the Huckel 4n rule indicates anti-aromaticity.
    fn is_anti_aromatic(&self) -> bool {
        if !self.is_potentially_planar || self.has_cross_conjugation() {
            return false;
        }
        matches!(self.pi_electrons, Some(pi) if pi > 0 && pi % 4 == 0)
    }

    /// Computes planarity and π-electron counts, caching the results on the struct.
    fn evaluate(&mut self) {
        if !self
            .atoms
            .iter()
            .all(|&id| is_potentially_planar(&self.molecule.atoms[id]))
        {
            self.is_potentially_planar = false;
            return;
        }
        self.is_potentially_planar = true;

        let mut pi_count = 0;
        for &atom_id in &self.atoms {
            if let Some(contribution) = self.count_pi_contribution(atom_id) {
                pi_count += contribution;
            } else {
                self.is_potentially_planar = false;
                self.pi_electrons = None;
                return;
            }
        }
        self.pi_electrons = Some(pi_count);
    }

    /// Checks if an atom participates in a double bond within the ring system.
    fn atom_has_endocyclic_double(&self, atom_id: usize) -> bool {
        self.molecule.adjacency[atom_id]
            .iter()
            .any(|&(n_id, order)| order == BondOrder::Double && self.atoms.contains(&n_id))
    }

    /// Checks if an atom carries a double bond outside the ring system.
    fn atom_has_exocyclic_double(&self, atom_id: usize) -> bool {
        self.molecule.adjacency[atom_id]
            .iter()
            .any(|&(n_id, order)| order == BondOrder::Double && !self.atoms.contains(&n_id))
    }

    /// Detects cross-conjugation, which prevents anti-aromatic classification.
    fn has_cross_conjugation(&self) -> bool {
        self.atoms
            .iter()
            .any(|&atom_id| self.atom_has_exocyclic_double(atom_id))
    }

    /// Computes each atom's π contribution using bond, lone-pair, and resonance flags.
    fn count_pi_contribution(&self, atom_id: usize) -> Option<u32> {
        let atom = &self.molecule.atoms[atom_id];
        let has_endocyclic_double_bond = self.atom_has_endocyclic_double(atom_id);
        let has_exocyclic_double_bond = self.atom_has_exocyclic_double(atom_id);

        if has_endocyclic_double_bond {
            return Some(1);
        }

        if !has_exocyclic_double_bond && atom.lone_pairs > 0 {
            return Some(2);
        }

        if atom.formal_charge == -1 {
            return Some(2);
        }
        if atom.formal_charge == 1 {
            return Some(0);
        }

        if has_exocyclic_double_bond {
            return Some(1);
        }

        if atom.is_resonant && atom.is_in_ring {
            return Some(1);
        }

        None
    }
}

/// Heuristic planarity test derived from steric number rules.
fn is_potentially_planar(atom: &AnnotatedAtom) -> bool {
    let steric_number = atom.degree + atom.lone_pairs;
    match steric_number {
        0..=3 => true,
        4 => atom.lone_pairs > 0,
        _ => false,
    }
}

/// Groups rings into fused systems via shared atoms.
fn find_ring_systems(rings: &[Ring]) -> Vec<Vec<usize>> {
    if rings.is_empty() {
        return vec![];
    }

    let ring_adj = build_ring_adjacency(rings);
    let mut systems = Vec::new();
    let mut visited = vec![false; rings.len()];

    for i in 0..rings.len() {
        if !visited[i] {
            let mut current_system_indices = Vec::new();
            let mut stack = vec![i];
            visited[i] = true;

            while let Some(ring_idx) = stack.pop() {
                current_system_indices.push(ring_idx);
                for &neighbor_idx in &ring_adj[ring_idx] {
                    if !visited[neighbor_idx] {
                        visited[neighbor_idx] = true;
                        stack.push(neighbor_idx);
                    }
                }
            }
            systems.push(current_system_indices);
        }
    }
    systems
}

/// Builds an adjacency list between rings that share at least one atom.
fn build_ring_adjacency(rings: &[Ring]) -> Vec<Vec<usize>> {
    let mut atom_to_rings: HashMap<usize, Vec<usize>> = HashMap::new();
    for (ring_idx, ring) in rings.iter().enumerate() {
        for &atom_id in ring {
            atom_to_rings.entry(atom_id).or_default().push(ring_idx);
        }
    }

    let mut adj = vec![vec![]; rings.len()];
    for ring_indices in atom_to_rings.values() {
        if ring_indices.len() > 1 {
            for i in 0..ring_indices.len() {
                for j in (i + 1)..ring_indices.len() {
                    let r1 = ring_indices[i];
                    let r2 = ring_indices[j];
                    adj[r1].push(r2);
                    adj[r2].push(r1);
                }
            }
        }
    }

    for neighbors in adj.iter_mut() {
        neighbors.sort_unstable();
        neighbors.dedup();
    }

    adj
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::graph::MolecularGraph;
    use crate::core::properties::Element;

    #[derive(Clone, Copy)]
    struct AtomSpec {
        element: Element,
        formal_charge: i8,
        lone_pairs: u8,
        degree_override: Option<u8>,
    }

    impl AtomSpec {
        fn new(element: Element) -> Self {
            Self {
                element,
                formal_charge: 0,
                lone_pairs: 0,
                degree_override: None,
            }
        }

        fn with_charge(mut self, charge: i8) -> Self {
            self.formal_charge = charge;
            self
        }

        fn with_lone_pairs(mut self, lone_pairs: u8) -> Self {
            self.lone_pairs = lone_pairs;
            self
        }

        fn with_degree(mut self, degree: u8) -> Self {
            self.degree_override = Some(degree);
            self
        }
    }

    fn build_test_molecule(
        atom_specs: &[AtomSpec],
        bonds: &[(usize, usize, BondOrder)],
        rings: &[&[usize]],
    ) -> AnnotatedMolecule {
        let mut graph = MolecularGraph::new();
        for spec in atom_specs {
            graph.add_atom(spec.element);
        }
        for &(a, b, order) in bonds {
            graph.add_bond(a, b, order).expect("valid bond definition");
        }

        let mut molecule = AnnotatedMolecule::new(&graph).expect("graph must be valid");
        molecule.rings = rings.iter().map(|ring| ring.to_vec()).collect();
        annotate_ring_flags(&mut molecule);
        apply_atom_specs(&mut molecule, atom_specs);
        molecule
    }

    fn annotate_ring_flags(molecule: &mut AnnotatedMolecule) {
        for ring in &molecule.rings {
            for &atom_id in ring {
                let atom = &mut molecule.atoms[atom_id];
                atom.is_in_ring = true;
            }
        }
    }

    fn apply_atom_specs(molecule: &mut AnnotatedMolecule, specs: &[AtomSpec]) {
        for (i, spec) in specs.iter().enumerate() {
            let atom = &mut molecule.atoms[i];
            atom.formal_charge = spec.formal_charge;
            atom.lone_pairs = spec.lone_pairs;
            if let Some(override_degree) = spec.degree_override {
                atom.degree = override_degree;
            }
            atom.steric_number = atom.degree + atom.lone_pairs;
        }
    }

    fn perceive_aromaticity(mut molecule: AnnotatedMolecule) -> AnnotatedMolecule {
        perceive(&mut molecule).expect("aromaticity perception should succeed");
        molecule
    }

    fn assert_flag_sets(
        molecule: &AnnotatedMolecule,
        expected_aromatic: &[usize],
        expected_anti: &[usize],
    ) {
        use std::collections::HashSet;
        let aromatic: HashSet<_> = molecule
            .atoms
            .iter()
            .enumerate()
            .filter_map(|(idx, atom)| atom.is_aromatic.then_some(idx))
            .collect();
        let anti: HashSet<_> = molecule
            .atoms
            .iter()
            .enumerate()
            .filter_map(|(idx, atom)| atom.is_anti_aromatic.then_some(idx))
            .collect();

        assert_eq!(
            aromatic,
            expected_aromatic.iter().copied().collect(),
            "unexpected aromatic atom assignment"
        );
        assert_eq!(
            anti,
            expected_anti.iter().copied().collect(),
            "unexpected anti-aromatic atom assignment"
        );

        for (idx, atom) in molecule.atoms.iter().enumerate() {
            assert!(
                !(atom.is_aromatic && atom.is_anti_aromatic),
                "atom {idx} cannot be aromatic and anti-aromatic simultaneously"
            );
        }
    }

    fn h() -> AtomSpec {
        AtomSpec::new(Element::H)
    }
    fn c() -> AtomSpec {
        AtomSpec::new(Element::C)
    }
    fn n_pyridine() -> AtomSpec {
        AtomSpec::new(Element::N).with_lone_pairs(1)
    }
    fn n_pyrrole() -> AtomSpec {
        AtomSpec::new(Element::N).with_lone_pairs(1)
    }
    fn o_furan() -> AtomSpec {
        AtomSpec::new(Element::O).with_lone_pairs(2)
    }
    fn o_carbonyl() -> AtomSpec {
        AtomSpec::new(Element::O).with_lone_pairs(2)
    }
    fn b_anion() -> AtomSpec {
        AtomSpec::new(Element::B).with_charge(-1)
    }

    fn benzene() -> AnnotatedMolecule {
        let atoms = vec![c(), c(), c(), c(), c(), c(), h(), h(), h(), h(), h(), h()];
        let bonds = vec![
            (0, 1, BondOrder::Double),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 5, BondOrder::Double),
            (5, 0, BondOrder::Single),
            (0, 6, BondOrder::Single),
            (1, 7, BondOrder::Single),
            (2, 8, BondOrder::Single),
            (3, 9, BondOrder::Single),
            (4, 10, BondOrder::Single),
            (5, 11, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4, 5]])
    }

    fn pyrrole() -> AnnotatedMolecule {
        let atoms = vec![n_pyrrole(), c(), c(), c(), c(), h(), h(), h(), h(), h()];
        let bonds = vec![
            (0, 1, BondOrder::Single),
            (1, 2, BondOrder::Double),
            (2, 3, BondOrder::Single),
            (3, 4, BondOrder::Double),
            (4, 0, BondOrder::Single),
            (0, 5, BondOrder::Single),
            (1, 6, BondOrder::Single),
            (2, 7, BondOrder::Single),
            (3, 8, BondOrder::Single),
            (4, 9, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4]])
    }

    fn borabenzene_anion() -> AnnotatedMolecule {
        let atoms = vec![b_anion(), c(), c(), c(), c(), c(), h(), h(), h(), h(), h()];
        let bonds = vec![
            (0, 1, BondOrder::Double),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 5, BondOrder::Double),
            (5, 0, BondOrder::Single),
            (1, 6, BondOrder::Single),
            (2, 7, BondOrder::Single),
            (3, 8, BondOrder::Single),
            (4, 9, BondOrder::Single),
            (5, 10, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4, 5]])
    }

    fn cyclobutadiene() -> AnnotatedMolecule {
        let atoms = vec![c(), c(), c(), c(), h(), h(), h(), h()];
        let bonds = vec![
            (0, 1, BondOrder::Double),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 0, BondOrder::Single),
            (0, 4, BondOrder::Single),
            (1, 5, BondOrder::Single),
            (2, 6, BondOrder::Single),
            (3, 7, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3]])
    }

    fn cyclooctatetraene_nonplanar() -> AnnotatedMolecule {
        let atoms: Vec<_> = (0..8)
            .map(|idx| {
                if idx % 2 == 0 {
                    c().with_degree(4)
                } else {
                    c()
                }
            })
            .chain((8..16).map(|_| h()))
            .collect();
        let bonds = vec![
            (0, 1, BondOrder::Double),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 5, BondOrder::Double),
            (5, 6, BondOrder::Single),
            (6, 7, BondOrder::Double),
            (7, 0, BondOrder::Single),
            (0, 8, BondOrder::Single),
            (1, 9, BondOrder::Single),
            (2, 10, BondOrder::Single),
            (3, 11, BondOrder::Single),
            (4, 12, BondOrder::Single),
            (5, 13, BondOrder::Single),
            (6, 14, BondOrder::Single),
            (7, 15, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4, 5, 6, 7]])
    }

    fn cyclohexane() -> AnnotatedMolecule {
        let atoms = (0..18)
            .map(|i| if i < 6 { c() } else { h() })
            .collect::<Vec<_>>();
        let bonds = vec![
            (0, 1, BondOrder::Single),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Single),
            (3, 4, BondOrder::Single),
            (4, 5, BondOrder::Single),
            (5, 0, BondOrder::Single),
            (0, 6, BondOrder::Single),
            (0, 7, BondOrder::Single),
            (1, 8, BondOrder::Single),
            (1, 9, BondOrder::Single),
            (2, 10, BondOrder::Single),
            (2, 11, BondOrder::Single),
            (3, 12, BondOrder::Single),
            (3, 13, BondOrder::Single),
            (4, 14, BondOrder::Single),
            (4, 15, BondOrder::Single),
            (5, 16, BondOrder::Single),
            (5, 17, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4, 5]])
    }

    fn naphthalene() -> AnnotatedMolecule {
        let atoms = (0..18)
            .map(|i| if i < 10 { c() } else { h() })
            .collect::<Vec<_>>();
        let bonds = vec![
            (0, 1, BondOrder::Double),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 9, BondOrder::Single),
            (9, 8, BondOrder::Double),
            (8, 7, BondOrder::Single),
            (7, 6, BondOrder::Double),
            (6, 5, BondOrder::Single),
            (5, 0, BondOrder::Single),
            (4, 5, BondOrder::Double),
            (0, 10, BondOrder::Single),
            (1, 11, BondOrder::Single),
            (2, 12, BondOrder::Single),
            (3, 13, BondOrder::Single),
            (6, 14, BondOrder::Single),
            (7, 15, BondOrder::Single),
            (8, 16, BondOrder::Single),
            (9, 17, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4, 5], &[4, 5, 6, 7, 8, 9]])
    }

    fn anthracene() -> AnnotatedMolecule {
        let atoms = (0..24)
            .map(|i| if i < 14 { c() } else { h() })
            .collect::<Vec<_>>();
        let bonds = vec![
            (0, 1, BondOrder::Double),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 13, BondOrder::Single),
            (13, 12, BondOrder::Double),
            (12, 11, BondOrder::Single),
            (11, 10, BondOrder::Double),
            (10, 5, BondOrder::Single),
            (5, 0, BondOrder::Single),
            (4, 5, BondOrder::Double),
            (10, 9, BondOrder::Single),
            (9, 8, BondOrder::Double),
            (8, 7, BondOrder::Single),
            (7, 6, BondOrder::Double),
            (6, 11, BondOrder::Single),
            (0, 14, BondOrder::Single),
            (1, 15, BondOrder::Single),
            (2, 16, BondOrder::Single),
            (3, 17, BondOrder::Single),
            (6, 18, BondOrder::Single),
            (7, 19, BondOrder::Single),
            (8, 20, BondOrder::Single),
            (9, 21, BondOrder::Single),
            (12, 22, BondOrder::Single),
            (13, 23, BondOrder::Single),
        ];
        build_test_molecule(
            &atoms,
            &bonds,
            &[
                &[0, 1, 2, 3, 4, 5],
                &[4, 5, 10, 11, 6, 7, 8, 9, 13, 12],
                &[6, 7, 8, 9, 10, 11],
            ],
        )
    }

    fn purine() -> AnnotatedMolecule {
        let atoms = vec![
            n_pyridine(),
            c(),
            n_pyridine(),
            c(),
            c(),
            n_pyrrole(),
            c(),
            n_pyridine(),
            c(),
            h(),
            h(),
            h(),
            h(),
        ];
        let bonds = vec![
            (0, 1, BondOrder::Double),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 5, BondOrder::Single),
            (5, 0, BondOrder::Single),
            (3, 8, BondOrder::Single),
            (8, 7, BondOrder::Double),
            (7, 6, BondOrder::Single),
            (6, 4, BondOrder::Double),
            (1, 9, BondOrder::Single),
            (5, 10, BondOrder::Single),
            (6, 11, BondOrder::Single),
            (8, 12, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4, 5], &[3, 4, 6, 7, 8]])
    }

    fn alpha_pyrone() -> AnnotatedMolecule {
        let atoms = vec![
            c(),
            o_furan(),
            c(),
            c(),
            c(),
            c(),
            o_carbonyl(),
            h(),
            h(),
            h(),
            h(),
        ];
        let bonds = vec![
            (0, 1, BondOrder::Single),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 5, BondOrder::Double),
            (5, 0, BondOrder::Single),
            (0, 6, BondOrder::Double),
            (2, 7, BondOrder::Single),
            (3, 8, BondOrder::Single),
            (4, 9, BondOrder::Single),
            (5, 10, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4, 5]])
    }

    fn pyrazole() -> AnnotatedMolecule {
        let atoms = vec![c(), n_pyrrole(), n_pyridine(), c(), c(), h(), h(), h(), h()];
        let bonds = vec![
            (0, 1, BondOrder::Single),
            (1, 2, BondOrder::Single),
            (2, 3, BondOrder::Double),
            (3, 4, BondOrder::Single),
            (4, 0, BondOrder::Double),
            (0, 5, BondOrder::Single),
            (1, 6, BondOrder::Single),
            (3, 7, BondOrder::Single),
            (4, 8, BondOrder::Single),
        ];
        build_test_molecule(&atoms, &bonds, &[&[0, 1, 2, 3, 4]])
    }

    #[test]
    fn benzene_ring_is_aromatic() {
        let molecule = perceive_aromaticity(benzene());
        assert_flag_sets(&molecule, &[0, 1, 2, 3, 4, 5], &[]);
    }

    #[test]
    fn pyrrole_lone_pair_contributes_to_aromaticity() {
        let molecule = perceive_aromaticity(pyrrole());
        assert_flag_sets(&molecule, &[0, 1, 2, 3, 4], &[]);
    }

    #[test]
    fn borabenzene_anion_is_aromatic() {
        let molecule = perceive_aromaticity(borabenzene_anion());
        assert_flag_sets(&molecule, &[0, 1, 2, 3, 4, 5], &[]);
    }

    #[test]
    fn cyclobutadiene_detected_as_antiaromatic() {
        let molecule = perceive_aromaticity(cyclobutadiene());
        assert_flag_sets(&molecule, &[], &[0, 1, 2, 3]);
    }

    #[test]
    fn cyclooctatetraene_rejected_due_to_non_planarity() {
        let molecule = perceive_aromaticity(cyclooctatetraene_nonplanar());
        assert_flag_sets(&molecule, &[], &[]);
    }

    #[test]
    fn cyclohexane_is_non_aromatic() {
        let molecule = perceive_aromaticity(cyclohexane());
        assert_flag_sets(&molecule, &[], &[]);
    }

    #[test]
    fn naphthalene_fused_rings_are_aromatic() {
        let molecule = perceive_aromaticity(naphthalene());
        assert_flag_sets(&molecule, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], &[]);
    }

    #[test]
    fn anthracene_three_ring_system_is_aromatic() {
        let molecule = perceive_aromaticity(anthracene());
        assert_flag_sets(&molecule, &(0..14).collect::<Vec<_>>(), &[]);
    }

    #[test]
    fn purine_dual_ring_system_is_aromatic() {
        let molecule = perceive_aromaticity(purine());
        assert_flag_sets(&molecule, &[0, 1, 2, 3, 4, 5, 6, 7, 8], &[]);
    }

    #[test]
    fn alpha_pyrone_is_non_aromatic() {
        let molecule = perceive_aromaticity(alpha_pyrone());
        assert_flag_sets(&molecule, &[], &[]);
    }

    #[test]
    fn pyrazole_is_aromatic() {
        let molecule = perceive_aromaticity(pyrazole());
        assert_flag_sets(&molecule, &[0, 1, 2, 3, 4], &[]);
    }
}