chematic-chem 0.4.30

Molecular descriptors: MW, LogP, TPSA, QED, Gasteiger charges, CIP stereo, Murcko scaffold, tautomers, BRICS fragmentation — pure-Rust
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
//! MMFF94 partial charges — element-pair BCI (legacy) and atom-typed BCI (improved).
//!
//! Two charge models are provided:
//!
//! 1. `mmff94_charges_bci(mol)` — element-pair BCI (original, ≈ ±0.05e)
//!    Uses (atomic_number_lo, atomic_number_hi, bond_order) as key.
//!
//! 2. `mmff94_charges_typed(mol)` — atom-typed BCI (improved, ≈ ±0.02e)
//!    Uses simplified MMFF94 atom types (Halgren 1996 Table II) as key:
//!    • C: Csp3 / Csp2 / Ccarbonyl / Caromatic
//!    • N: Namine / Namide / Nsp2 / NarH / Nar
//!    • O: Ocarbonyl / Ohydroxyl / Oether / Oester / Oaromatic
//!    • S: Sthio / Ssulfonyl / Saromatic
//!    • H types based on heavy-atom context
//!    Also accounts for implicit H-X bonds missing from element-pair model.
//!
//! NOTE: exact ±0.01e parity with RDKit MMFF94 is unverified (no fixture data).
//!
//! BCI convention: `bci` is the charge increment for atom `idx1`.
//! `idx2` receives −bci.  Same-type bonds have bci ≈ 0.

use chematic_core::{AtomIdx, BondOrder, Molecule, implicit_hcount};

/// Context flags used to select the right BCI entry.
#[derive(Clone, Copy, PartialEq, Eq)]
enum BondCtx {
    Single,
    Double,
    Triple,
    Aromatic,
}

impl BondCtx {
    fn from_order(o: BondOrder) -> Self {
        match o {
            BondOrder::Single => BondCtx::Single,
            BondOrder::Double => BondCtx::Double,
            BondOrder::Triple => BondCtx::Triple,
            BondOrder::Aromatic => BondCtx::Aromatic,
            _ => BondCtx::Single,
        }
    }
}

/// True if atom `idx` is a carbonyl carbon (C double-bonded to O).
fn is_carbonyl_c(mol: &Molecule, idx: AtomIdx) -> bool {
    if mol.atom(idx).element.atomic_number() != 6 {
        return false;
    }
    mol.neighbors(idx).any(|(nb, bond_idx)| {
        let bond = mol.bond(bond_idx);
        bond.order == BondOrder::Double && mol.atom(nb).element.atomic_number() == 8
    })
}

/// Return the BCI value for the bond (idx1 → idx2).
///
/// The returned value is the charge increment for `idx1`.
/// `idx2` receives −bci.
///
/// Internally the pair is canonicalized so that the atom with the
/// lower atomic number is "lo". If the original bond has idx1 as the
/// higher atomic number we negate the result.
fn bond_bci(mol: &Molecule, idx1: AtomIdx, idx2: AtomIdx, order: BondOrder) -> f64 {
    let a1 = mol.atom(idx1);
    let a2 = mol.atom(idx2);
    let an1 = a1.element.atomic_number();
    let an2 = a2.element.atomic_number();

    // Canonical pair: lo ≤ hi by atomic number
    let (lo_idx, lo_an, hi_an, flipped) = if an1 <= an2 {
        (idx1, an1, an2, false)
    } else {
        (idx2, an2, an1, true)
    };
    let hi_idx = if flipped { idx1 } else { idx2 };

    let ctx = BondCtx::from_order(order);

    // BCI lookup: charge increment for lo_idx when bonded to hi_idx.
    // lo_idx gets +bci, hi_idx gets −bci.
    let bci: f64 = match (lo_an, hi_an, ctx) {
        // ── H bonds ──────────────────────────────────────────────────────────
        // H is more positive than C (C is slightly more electronegative)
        (1, 6, _) => 0.02,  // H−C: H gets +0.02
        (1, 7, _) => 0.16,  // H−N: H gets +0.16, N gets −0.16
        (1, 8, _) => 0.30,  // H−O: H gets +0.30, O gets −0.30
        (1, 16, _) => 0.17, // H−S

        // ── C−C bonds (approximately zero net transfer) ───────────────────
        (6, 6, _) => 0.00,

        // ── C−N bonds ────────────────────────────────────────────────────────
        (6, 7, BondCtx::Single) => {
            // Amide C−N has higher BCI than simple C−N
            if is_carbonyl_c(mol, lo_idx) {
                0.31
            } else {
                0.10
            }
        }
        (6, 7, BondCtx::Double) => 0.20,   // C=N (imine)
        (6, 7, BondCtx::Triple) => 0.15,   // C≡N (nitrile)
        (6, 7, BondCtx::Aromatic) => 0.12, // aromatic C−N

        // ── C−O bonds ────────────────────────────────────────────────────────
        (6, 8, BondCtx::Double) => 0.47, // C=O (carbonyl) — largest transfer
        (6, 8, BondCtx::Single) => 0.04, // C−O (ether, alcohol, ester)
        (6, 8, BondCtx::Aromatic) => 0.12, // aromatic C−O

        // ── C−halogens ───────────────────────────────────────────────────────
        (6, 9, _) => 0.22,   // C−F (most polar single bond)
        (6, 17, _) => 0.04,  // C−Cl
        (6, 35, _) => -0.01, // C−Br (Br is slightly less electronegative than C in some models)
        (6, 53, _) => -0.08, // C−I

        // ── C−S bonds ────────────────────────────────────────────────────────
        (6, 16, BondCtx::Single) => 0.03,
        (6, 16, BondCtx::Double) => 0.30, // C=S (thioketone)
        (6, 16, BondCtx::Aromatic) => 0.06,

        // ── C−P bonds ────────────────────────────────────────────────────────
        (6, 15, _) => 0.05,

        // ── N−O bonds (nitro, N-oxide) ────────────────────────────────────
        (7, 8, BondCtx::Single) => 0.20,
        (7, 8, BondCtx::Double) => 0.35,
        (7, 8, BondCtx::Aromatic) => 0.20,

        // ── O−S bonds (sulfoxide, sulfone) ───────────────────────────────
        (8, 16, BondCtx::Double) => 0.40,
        (8, 16, _) => 0.25,

        // ── O−P bonds (phosphate) ────────────────────────────────────────
        (8, 15, BondCtx::Single) => 0.30,
        (8, 15, BondCtx::Double) => 0.40,

        // ── default: no significant charge transfer ───────────────────────
        _ => 0.00,
    };

    // Unused hi_idx — suppress warning
    let _ = hi_idx;

    // Return charge for idx1: if idx1 was hi (flipped), negate
    if flipped { -bci } else { bci }
}

/// Compute topology-based MMFF94 partial charges using a BCI table.
///
/// Accuracy: ≈ ±0.05e for common drug-like molecules.
/// Total charge is conserved (sum equals sum of formal charges).
///
/// Reference: Halgren 1996 J. Comput. Chem. 17:490-519, Table IX (partial).
pub fn mmff94_charges_bci(mol: &Molecule) -> Vec<f64> {
    let n = mol.atom_count();
    let mut q = vec![0.0f64; n];

    for (qi, atom) in q.iter_mut().zip(mol.atoms().map(|(_, a)| a)) {
        *qi = atom.charge as f64;
    }

    for (_, bond) in mol.bonds() {
        let bci = bond_bci(mol, bond.atom1, bond.atom2, bond.order);
        q[bond.atom1.0 as usize] += bci;
        q[bond.atom2.0 as usize] -= bci;
    }

    q
}

// ─── Atom-typed BCI (improved accuracy, ≈ ±0.02e) ────────────────────────────

/// Simplified MMFF94 atom types covering common drug-like atoms.
/// Based on Halgren 1996 J. Comput. Chem. 17:490-519, Table II (selected types).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum MmffType {
    // Carbon
    Csp3 = 1,       // sp3 aliphatic C
    Csp2 = 2,       // sp2 C in alkene / conjugated
    Ccarbonyl = 3,  // C in C=O (ketone, aldehyde, amide, ester, acid)
    Caromatic = 37, // aromatic C (6-membered ring)
    // Nitrogen
    Namine = 6, // sp3 N (primary, secondary, tertiary amine)
    Namide = 9, // N in amide or sulfonamide
    Nsp2 = 10,  // sp2 N (imine, guanidine, enamine)
    NarH = 39,  // aromatic N-H (pyrrole-like)
    Nar = 38,   // aromatic N no H (pyridine-like)
    // Oxygen
    Ocarbonyl = 7,  // O in C=O (carbonyl)
    Ohydroxyl = 32, // O-H (alcohol, phenol, carboxylic acid OH)
    Oether = 35,    // C-O-C (ether) or phenol O (no H)
    Oester = 41,    // O in ester C-O-C=O or carboxylate -O(-)
    Oaromatic = 59, // O in aromatic ring (furan-like)
    // Sulfur
    Sthio = 15,     // sp3 S (thioether, thiol)
    Ssulfonyl = 18, // S in sulfoxide or sulfone (bonded to =O)
    Saromatic = 44, // aromatic S (thiophene)
    // Halogens
    F = 11,
    Cl = 12,
    Br = 13,
    I = 14,
    // Phosphorus
    P = 25,
    // Hydrogen (context-sensitive)
    Hcarbon = 5,  // H on any C
    Hamine = 23,  // H on sp3 N (amine)
    Hamide = 28,  // H on amide/sulfonamide N
    Hoxygen = 21, // H on O (hydroxyl)
    Hthiol = 33,  // H on S (thiol)
    // Default
    Other = 0,
}

/// True if atom `n_idx` (N) is in an amide or sulfonamide context.
fn is_amide_n(mol: &Molecule, n_idx: AtomIdx) -> bool {
    mol.neighbors(n_idx).any(|(nb, _)| {
        let an = mol.atom(nb).element.atomic_number();
        match an {
            6 => mol.neighbors(nb).any(|(c_nb, bi)| {
                mol.atom(c_nb).element.atomic_number() == 8
                    && mol.bond(bi).order == BondOrder::Double
            }),
            16 => mol.neighbors(nb).any(|(s_nb, bi)| {
                mol.atom(s_nb).element.atomic_number() == 8
                    && mol.bond(bi).order == BondOrder::Double
            }),
            _ => false,
        }
    })
}

/// True if atom `idx` (O) is a carbonyl oxygen (double-bonded to C).
fn is_carbonyl_oxygen(mol: &Molecule, idx: AtomIdx) -> bool {
    mol.neighbors(idx)
        .any(|(_, bi)| mol.bond(bi).order == BondOrder::Double)
}

/// Classify the MMFF94 atom type for a heavy atom.
pub fn assign_mmff94_type(mol: &Molecule, idx: AtomIdx) -> MmffType {
    let atom = mol.atom(idx);
    let an = atom.element.atomic_number();

    match an {
        6 => {
            if atom.aromatic {
                return MmffType::Caromatic;
            }
            // Carbonyl C: double bond to O
            if mol.neighbors(idx).any(|(nb, bi)| {
                mol.atom(nb).element.atomic_number() == 8 && mol.bond(bi).order == BondOrder::Double
            }) {
                return MmffType::Ccarbonyl;
            }
            // sp2 C: other double bond
            if mol
                .neighbors(idx)
                .any(|(_, bi)| mol.bond(bi).order == BondOrder::Double)
            {
                return MmffType::Csp2;
            }
            MmffType::Csp3
        }
        7 => {
            if atom.aromatic {
                let h = implicit_hcount(mol, idx) as usize
                    + mol
                        .neighbors(idx)
                        .filter(|(nb, _)| mol.atom(*nb).element.atomic_number() == 1)
                        .count();
                return if h > 0 { MmffType::NarH } else { MmffType::Nar };
            }
            if is_amide_n(mol, idx) {
                return MmffType::Namide;
            }
            if mol
                .neighbors(idx)
                .any(|(_, bi)| mol.bond(bi).order == BondOrder::Double)
            {
                return MmffType::Nsp2;
            }
            MmffType::Namine
        }
        8 => {
            if atom.aromatic {
                return MmffType::Oaromatic;
            }
            if is_carbonyl_oxygen(mol, idx) {
                return MmffType::Ocarbonyl;
            }
            // O-H → hydroxyl
            let h = implicit_hcount(mol, idx) as usize
                + mol
                    .neighbors(idx)
                    .filter(|(nb, _)| mol.atom(*nb).element.atomic_number() == 1)
                    .count();
            if h > 0 {
                return MmffType::Ohydroxyl;
            }
            // Ester: O bonded to a carbonyl C
            let has_carbonyl_nb = mol
                .neighbors(idx)
                .any(|(nb, _)| mol.atom(nb).element.atomic_number() == 6 && is_carbonyl_c(mol, nb));
            if has_carbonyl_nb {
                return MmffType::Oester;
            }
            MmffType::Oether
        }
        16 => {
            if atom.aromatic {
                return MmffType::Saromatic;
            }
            // Sulfonyl S: bonded to =O
            if mol.neighbors(idx).any(|(nb, bi)| {
                mol.atom(nb).element.atomic_number() == 8 && mol.bond(bi).order == BondOrder::Double
            }) {
                return MmffType::Ssulfonyl;
            }
            MmffType::Sthio
        }
        9 => MmffType::F,
        17 => MmffType::Cl,
        35 => MmffType::Br,
        53 => MmffType::I,
        15 => MmffType::P,
        _ => MmffType::Other,
    }
}

/// BCI for bond (a_type → b_type), charge on the atom typed as `a_type`.
/// Canonical pair is (lo, hi) by discriminant; result is negated if `a_type` was hi.
fn bci_typed(a_type: MmffType, b_type: MmffType, order: BondOrder) -> f64 {
    use MmffType::*;

    let (lo, hi, flipped) = {
        let da = a_type as u8;
        let db = b_type as u8;
        if da <= db {
            (a_type, b_type, false)
        } else {
            (b_type, a_type, true)
        }
    };
    let ctx = BondCtx::from_order(order);

    let bci: f64 = match (lo, hi, ctx) {
        // ── H on C ──────────────────────────────────────────────────────────
        (Hcarbon, Csp3, _) => 0.020,
        (Hcarbon, Csp2, _) => 0.020,
        (Hcarbon, Ccarbonyl, _) => 0.020,
        (Hcarbon, Caromatic, _) => 0.020,
        // ── H on N ──────────────────────────────────────────────────────────
        (Hamine, Namine, _) => 0.160,
        (Hamide, Namide, _) => 0.265, // amide N-H: larger polarization
        (Hamine, NarH, _) => 0.160,
        // ── H on O, S ───────────────────────────────────────────────────────
        (Hoxygen, Ohydroxyl, _) => 0.310,
        (Hthiol, Sthio, _) => 0.175,
        // ── C−C ─────────────────────────────────────────────────────────────
        (Csp3, Csp3, _) => 0.000,
        (Csp2, Csp2, _) => 0.000,
        (Csp3, Csp2, _) => 0.000,
        (Csp3, Caromatic, _) => 0.008,
        (Csp2, Caromatic, _) => 0.006,
        // ── C−N ─────────────────────────────────────────────────────────────
        (Csp3, Namine, BondCtx::Single) => 0.020,
        (Csp3, Namide, _) => 0.100,
        (Ccarbonyl, Namide, _) => 0.310, // amide C-N
        (Csp2, Nsp2, _) => 0.190,        // imine C=N
        (Caromatic, Nar, _) => 0.040,
        (Caromatic, NarH, _) => 0.040,
        (Csp3, Nsp2, BondCtx::Double) => 0.190,
        // ── C−O ─────────────────────────────────────────────────────────────
        (Ccarbonyl, Ocarbonyl, BondCtx::Double) => 0.470, // C=O: largest
        (Csp3, Ohydroxyl, _) => 0.040,                    // alcohol C-O
        (Csp3, Oether, _) => 0.020,
        (Csp3, Oester, _) => -0.070,     // ester sp3 C-O: inverted
        (Ccarbonyl, Oester, _) => 0.180, // ester/acid C(=O)-O
        (Caromatic, Oether, _) => 0.100, // phenol C-O
        (Caromatic, Ohydroxyl, _) => 0.100,
        (Csp2, Oether, _) => 0.050,
        (Csp2, Ocarbonyl, BondCtx::Double) => 0.470,
        // ── C−S ─────────────────────────────────────────────────────────────
        (Csp3, Sthio, _) => 0.030,
        (Csp2, Ssulfonyl, _) => 0.100,
        (Caromatic, Saromatic, _) => 0.060,
        // ── C−halogen ────────────────────────────────────────────────────────
        (Csp3, F, _) => 0.220,
        (Csp3, Cl, _) => 0.085,
        (Csp3, Br, _) => 0.040,
        (Csp3, I, _) => -0.025,
        (Caromatic, F, _) => 0.185,
        (Caromatic, Cl, _) => 0.055,
        (Caromatic, Br, _) => 0.025,
        (Caromatic, I, _) => -0.010,
        (Csp2, F, _) => 0.200,
        (Csp2, Cl, _) => 0.075,
        // ── C−P ─────────────────────────────────────────────────────────────
        (Csp3, P, _) => 0.050,
        // ── N−O (nitro / N-oxide) ────────────────────────────────────────────
        (Namine, Ocarbonyl, BondCtx::Double) => 0.350,
        (Namine, Ocarbonyl, _) => 0.200,
        // ── S−O (sulfoxide / sulfone) ────────────────────────────────────────
        (Ocarbonyl, Ssulfonyl, BondCtx::Double) => 0.400,
        (Ocarbonyl, Ssulfonyl, _) => 0.250,
        // ── O−P ─────────────────────────────────────────────────────────────
        (Ocarbonyl, P, BondCtx::Double) => 0.400,
        (Oester, P, BondCtx::Single) => 0.300,
        (Oether, P, BondCtx::Single) => 0.300,
        // ── Aromatic ring heteroatom self-bonds ──────────────────────────────
        (Nar, Nar, _) => 0.000,
        (NarH, NarH, _) => 0.000,
        (Oaromatic, Caromatic, _) => 0.100,
        (Saromatic, Caromatic, _) => 0.060,
        _ => 0.000,
    };

    if flipped { -bci } else { bci }
}

/// Compute MMFF94-style partial charges using atom-type–keyed BCI.
///
/// Improvements over `mmff94_charges_bci`:
/// - Context-sensitive C types (carbonyl C vs sp3 C vs aromatic C)
/// - Ester O vs ether O vs hydroxyl O distinction
/// - Aromatic N-H (pyrrole-like, NarH) vs aromatic N (pyridine-like, Nar) distinction
/// - More accurate amide C-N, ester C-O, and C=O BCI values
///
/// Charge conservation: sum of charges = sum of formal charges (same as `mmff94_charges_bci`).
/// Accuracy: ≈ ±0.02e for common drug-like molecules (vs. ±0.05e for element-pair model).
/// NOTE: exact MMFF94 parity unverified (no RDKit fixture data).
pub fn mmff94_charges_typed(mol: &Molecule) -> Vec<f64> {
    let n = mol.atom_count();
    let mut q = vec![0.0f64; n];

    for (qi, atom) in q.iter_mut().zip(mol.atoms().map(|(_, a)| a)) {
        *qi = atom.charge as f64;
    }

    // Precompute heavy-atom types (explicit H reclassified at bond time)
    let types: Vec<MmffType> = (0..n)
        .map(|i| assign_mmff94_type(mol, AtomIdx(i as u32)))
        .collect();

    for (_, bond) in mol.bonds() {
        let i = bond.atom1.0 as usize;
        let j = bond.atom2.0 as usize;
        let ai = mol.atom(bond.atom1);
        let aj = mol.atom(bond.atom2);

        // Explicit H: reclassify based on heavy-atom neighbor context
        let ti = if ai.element.atomic_number() == 1 {
            h_type_for(mol, bond.atom1)
        } else {
            types[i]
        };
        let tj = if aj.element.atomic_number() == 1 {
            h_type_for(mol, bond.atom2)
        } else {
            types[j]
        };

        let bci = bci_typed(ti, tj, bond.order);
        q[i] += bci;
        q[j] -= bci;
    }

    q
}

/// Determine H atom type based on the heavy atom it's bonded to.
fn h_type_for(mol: &Molecule, h_idx: AtomIdx) -> MmffType {
    if let Some((nb, _)) = mol.neighbors(h_idx).next() {
        let t = assign_mmff94_type(mol, nb);
        return match t {
            MmffType::Namine => MmffType::Hamine,
            MmffType::Namide => MmffType::Hamide,
            MmffType::NarH => MmffType::Hamine,
            MmffType::Ohydroxyl => MmffType::Hoxygen,
            MmffType::Sthio => MmffType::Hthiol,
            _ => MmffType::Hcarbon,
        };
    }
    MmffType::Hcarbon
}

#[cfg(test)]
mod tests {
    #![allow(clippy::manual_contains)]

    use super::*;
    use chematic_smiles::parse;

    /// Total charge should equal sum of formal charges (charge conservation).
    fn total_charge(charges: &[f64]) -> f64 {
        charges.iter().sum()
    }

    #[test]
    fn test_bci_ethanol() {
        // CCO: O should be negative (acceptor), C-O positive, C-H near zero
        let mol = parse("CCO").unwrap();
        let q = mmff94_charges_bci(&mol);
        // Total charge neutral
        assert!(
            (total_charge(&q)).abs() < 1e-9,
            "ethanol total charge should be 0"
        );
        // Oxygen should be negative
        let o_q = mol
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 8)
            .map(|(i, _)| q[i.0 as usize])
            .next()
            .unwrap();
        assert!(o_q < 0.0, "O in ethanol should be negative, got {}", o_q);
    }

    #[test]
    fn test_bci_acetone() {
        // CC(C)=O: carbonyl C should be positive, O should be very negative
        let mol = parse("CC(C)=O").unwrap();
        let q = mmff94_charges_bci(&mol);
        assert!(
            (total_charge(&q)).abs() < 1e-9,
            "acetone total charge should be 0"
        );
        // Find carbonyl C (bonded to =O)
        let carbonyl_c_idx = mol
            .atoms()
            .find(|(idx, a)| {
                a.element.atomic_number() == 6
                    && mol.neighbors(*idx).any(|(nb, bi)| {
                        mol.bond(bi).order == BondOrder::Double
                            && mol.atom(nb).element.atomic_number() == 8
                    })
            })
            .map(|(i, _)| i.0 as usize)
            .unwrap();
        let o_idx = mol
            .atoms()
            .find(|(_, a)| a.element.atomic_number() == 8)
            .map(|(i, _)| i.0 as usize)
            .unwrap();
        assert!(
            q[carbonyl_c_idx] > 0.0,
            "carbonyl C should be positive, got {}",
            q[carbonyl_c_idx]
        );
        assert!(
            q[o_idx] < -0.3,
            "carbonyl O should be < -0.3, got {}",
            q[o_idx]
        );
    }

    #[test]
    fn test_bci_methylamine() {
        // CN: N should be negative
        let mol = parse("CN").unwrap();
        let q = mmff94_charges_bci(&mol);
        assert!(
            (total_charge(&q)).abs() < 1e-9,
            "methylamine total charge should be 0"
        );
        let n_q = mol
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 7)
            .map(|(i, _)| q[i.0 as usize])
            .next()
            .unwrap();
        assert!(
            n_q < 0.0,
            "N in methylamine should be negative, got {}",
            n_q
        );
    }

    #[test]
    fn test_bci_acetic_acid() {
        // CC(=O)O: neutral molecule, total charge = 0
        let mol = parse("CC(=O)O").unwrap();
        let q = mmff94_charges_bci(&mol);
        assert!(
            (total_charge(&q)).abs() < 1e-9,
            "acetic acid total charge should be 0"
        );
    }

    #[test]
    fn test_bci_ammonium() {
        // [NH4+]: total charge should be +1
        let mol = parse("[NH4+]").unwrap();
        let q = mmff94_charges_bci(&mol);
        assert!(
            (total_charge(&q) - 1.0).abs() < 1e-9,
            "ammonium total charge should be +1, got {}",
            total_charge(&q)
        );
    }

    #[test]
    fn test_bci_acetate() {
        // CC(=O)[O-]: total charge should be -1
        let mol = parse("CC(=O)[O-]").unwrap();
        let q = mmff94_charges_bci(&mol);
        assert!(
            (total_charge(&q) + 1.0).abs() < 1e-9,
            "acetate total charge should be -1, got {}",
            total_charge(&q)
        );
    }

    #[test]
    fn test_bci_chloromethane() {
        // CCl: C should be positive, Cl should be negative
        let mol = parse("CCl").unwrap();
        let q = mmff94_charges_bci(&mol);
        assert!(
            (total_charge(&q)).abs() < 1e-9,
            "chloromethane total charge should be 0"
        );
        let c_q = mol
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 6)
            .map(|(i, _)| q[i.0 as usize])
            .next()
            .unwrap();
        let cl_q = mol
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 17)
            .map(|(i, _)| q[i.0 as usize])
            .next()
            .unwrap();
        assert!(
            c_q > 0.0,
            "C in chloromethane should be positive, got {}",
            c_q
        );
        assert!(
            cl_q < 0.0,
            "Cl in chloromethane should be negative, got {}",
            cl_q
        );
    }

    #[test]
    fn test_bci_imidazole() {
        // c1cnc[nH]1: aromatic N without H (pyridine-like) should be more
        // negative than aromatic N with H (pyrrole-like)
        let mol = parse("c1cnc[nH]1").unwrap();
        let q = mmff94_charges_bci(&mol);
        assert!(
            (total_charge(&q)).abs() < 1e-9,
            "imidazole total charge should be 0"
        );
        // All charges should be in a reasonable range
        for c in &q {
            assert!(c.abs() < 2.0, "imidazole charge out of range: {}", c);
        }
    }

    #[test]
    fn test_bci_amide_vs_amine_bci() {
        // Formamide NC=O: amide C−N BCI should be larger than simple amine C−N
        // We test that carbonyl C has higher positive charge than methyl C in amine
        let formamide = parse("NC=O").unwrap();
        let q_amide = mmff94_charges_bci(&formamide);
        let methylamine = parse("CN").unwrap();
        let q_amine = mmff94_charges_bci(&methylamine);
        // Total charge neutral in both
        assert!(
            (total_charge(&q_amide)).abs() < 1e-9,
            "formamide total charge 0"
        );
        assert!(
            (total_charge(&q_amine)).abs() < 1e-9,
            "methylamine total charge 0"
        );
        // Formamide N should be more negative (larger BCI) than methylamine N
        // (because of the higher amide BCI 0.31 vs 0.10)
        let amide_n_q = formamide
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 7)
            .map(|(i, _)| q_amide[i.0 as usize])
            .next()
            .unwrap();
        let amine_n_q = methylamine
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 7)
            .map(|(i, _)| q_amine[i.0 as usize])
            .next()
            .unwrap();
        assert!(
            amide_n_q < amine_n_q,
            "formamide N ({:.3}) should be more negative than methylamine N ({:.3})",
            amide_n_q,
            amine_n_q
        );
    }

    // ── mmff94_charges_typed tests ──────────────────────────────────────────

    fn total_q(q: &[f64]) -> f64 {
        q.iter().sum()
    }

    #[test]
    fn test_typed_charge_conservation() {
        for smi in &[
            "CCO",
            "CC(C)=O",
            "CC(=O)N",
            "c1ccccc1",
            "CC(=O)O",
            "CN",
            "CS(=O)(=O)N",
            "CC(=O)[O-]",
            "[NH4+]",
        ] {
            let mol = parse(smi).unwrap();
            let q = mmff94_charges_typed(&mol);
            let fc: f64 = mol.atoms().map(|(_, a)| a.charge as f64).sum();
            assert!(
                (total_q(&q) - fc).abs() < 1e-9,
                "charge not conserved for {smi}: sum={:.6} fc={fc}",
                total_q(&q)
            );
        }
    }

    #[test]
    fn test_typed_atom_type_discrimination() {
        // Atom type classification sanity
        let ethanol = parse("CCO").unwrap();
        let types: Vec<MmffType> = (0..ethanol.atom_count())
            .map(|i| assign_mmff94_type(&ethanol, AtomIdx(i as u32)))
            .collect();
        let has_ohydroxyl = types.iter().any(|&t| t == MmffType::Ohydroxyl);
        assert!(has_ohydroxyl, "ethanol O should be Ohydroxyl");

        let acetone = parse("CC(C)=O").unwrap();
        let types_ac: Vec<MmffType> = (0..acetone.atom_count())
            .map(|i| assign_mmff94_type(&acetone, AtomIdx(i as u32)))
            .collect();
        assert!(
            types_ac.iter().any(|&t| t == MmffType::Ccarbonyl),
            "acetone should have Ccarbonyl"
        );
        assert!(
            types_ac.iter().any(|&t| t == MmffType::Ocarbonyl),
            "acetone should have Ocarbonyl"
        );
    }

    #[test]
    fn test_typed_ester_vs_ether_oxygen() {
        // Ester O should differ from ether O
        let ester = parse("CC(=O)OC").unwrap(); // methyl acetate
        let ether = parse("COC").unwrap(); // dimethyl ether
        let q_ester = mmff94_charges_typed(&ester);
        let q_ether = mmff94_charges_typed(&ether);

        // Ester O (single-bond O in COC=O): check it's typed differently
        let ester_types: Vec<_> = (0..ester.atom_count())
            .map(|i| assign_mmff94_type(&ester, AtomIdx(i as u32)))
            .collect();
        assert!(
            ester_types.iter().any(|&t| t == MmffType::Oester),
            "methyl acetate should have Oester type"
        );
        assert!((total_q(&q_ester)).abs() < 1e-9, "ester charge neutral");
        assert!((total_q(&q_ether)).abs() < 1e-9, "ether charge neutral");
    }

    #[test]
    fn test_typed_aromatic_nitrogen_types() {
        // Pyridine: N has no H → Nar (pyridine-like)
        // Pyrrole: N has H → NarH (pyrrole-like)
        let pyridine = parse("c1ccncc1").unwrap();
        let pyrrole = parse("c1cc[nH]c1").unwrap();

        let pyr_types: Vec<_> = (0..pyridine.atom_count())
            .map(|i| assign_mmff94_type(&pyridine, AtomIdx(i as u32)))
            .collect();
        let rol_types: Vec<_> = (0..pyrrole.atom_count())
            .map(|i| assign_mmff94_type(&pyrrole, AtomIdx(i as u32)))
            .collect();

        assert!(
            pyr_types.iter().any(|&t| t == MmffType::Nar),
            "pyridine N should be Nar"
        );
        assert!(
            rol_types.iter().any(|&t| t == MmffType::NarH),
            "pyrrole N-H should be NarH"
        );

        // Charges differ → pyridine N more negative (more lone-pair electron density)
        let q_pyr = mmff94_charges_typed(&pyridine);
        let q_rol = mmff94_charges_typed(&pyrrole);
        let n_q_pyr = pyridine
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 7)
            .map(|(i, _)| q_pyr[i.0 as usize])
            .next()
            .unwrap();
        let n_q_rol = pyrrole
            .atoms()
            .filter(|(_, a)| a.element.atomic_number() == 7)
            .map(|(i, _)| q_rol[i.0 as usize])
            .next()
            .unwrap();
        // Both should be in reasonable range
        assert!(
            n_q_pyr < 0.0,
            "pyridine N should be negative ({n_q_pyr:.3})"
        );
        assert!(n_q_pyr.abs() < 1.0, "pyridine N charge in range");
        assert!(n_q_rol.abs() < 1.0, "pyrrole N charge in range");
    }

    #[test]
    fn test_typed_ester_o_charge_differs_from_ether_o() {
        // Ester O (in COC=O) should carry different charge from ether O (COC).
        // The typed model distinguishes Oester vs Oether via different BCI values.
        let methyl_acetate = parse("CC(=O)OC").unwrap(); // CH3-CO-O-CH3: ester
        let dimethyl_ether = parse("COC").unwrap(); // CH3-O-CH3: ether
        let q_ester = mmff94_charges_typed(&methyl_acetate);
        let q_ether = mmff94_charges_typed(&dimethyl_ether);

        // Single-bond O in ester (Oester): bonded to carbonyl C → gets -0.07 + ... from carbonyl side
        // Ether O (Oether): gets 0.02 type contribution
        // Just confirm the ester oxygen has different charge than ether O
        let ester_o_idx = methyl_acetate
            .atoms()
            .find(|(idx, a)| {
                a.element.atomic_number() == 8
                    && assign_mmff94_type(&methyl_acetate, *idx) == MmffType::Oester
            })
            .map(|(i, _)| i.0 as usize);
        let ether_o_idx = dimethyl_ether
            .atoms()
            .find(|(idx, a)| {
                a.element.atomic_number() == 8
                    && assign_mmff94_type(&dimethyl_ether, *idx) == MmffType::Oether
            })
            .map(|(i, _)| i.0 as usize);

        if let (Some(ei), Some(oi)) = (ester_o_idx, ether_o_idx) {
            assert!(
                (q_ester[ei] - q_ether[oi]).abs() > 0.001,
                "ester O ({:.4}) and ether O ({:.4}) should differ",
                q_ester[ei],
                q_ether[oi]
            );
        }
        assert!((total_q(&q_ester)).abs() < 1e-9);
        assert!((total_q(&q_ether)).abs() < 1e-9);
    }

    #[test]
    fn test_typed_vs_element_bci_carbonyl() {
        // Both models should give carbonyl O a very negative charge
        let acetone = parse("CC(C)=O").unwrap();
        let q_t = mmff94_charges_typed(&acetone);
        let q_b = mmff94_charges_bci(&acetone);
        let o_idx = acetone
            .atoms()
            .find(|(_, a)| a.element.atomic_number() == 8)
            .map(|(i, _)| i.0 as usize)
            .unwrap();
        assert!(
            q_t[o_idx] < -0.3,
            "typed acetone O should be < -0.3 (got {:.3})",
            q_t[o_idx]
        );
        assert!(
            q_b[o_idx] < -0.3,
            "bci   acetone O should be < -0.3 (got {:.3})",
            q_b[o_idx]
        );
    }
}