|
| 1 | +//! Canonical 37-slot heavy-atom representation and atom-name lookups. |
| 2 | +//! |
| 3 | +//! The atom37 scheme is the standard representation used by ESMFold, AlphaFold2, |
| 4 | +//! and related models: every amino acid is represented with up to 37 possible |
| 5 | +//! heavy-atom positions, each at a fixed slot index defined by [`ATOM37_NAMES`]. |
| 6 | +//! |
| 7 | +//! # Key items |
| 8 | +//! - [`NUM_ATOM37`] / [`NUM_RESIDUES`] — dimension constants |
| 9 | +//! - [`ATOM37_NAMES`] — canonical name→slot mapping (array) |
| 10 | +//! - [`atom_order`] — `HashMap` version of the same mapping |
| 11 | +//! - [`atom37_index`] — slot lookup by atom name |
| 12 | +//! - [`AAAtom`] — typed enum mirroring the slot indices |
| 13 | +//! - [`vdw_radius`] — van der Waals radii for common protein elements |
| 14 | +
|
| 15 | +use std::collections::HashMap; |
| 16 | +use strum::{Display, EnumIter, EnumString}; |
| 17 | + |
| 18 | +// ── Dimension constants ─────────────────────────────────────────────────────── |
| 19 | + |
| 20 | +pub const NUM_ATOM37: usize = 37; |
| 21 | +pub const NUM_RESIDUES: usize = 21; // 20 standard AAs + UNK |
| 22 | + |
| 23 | +// ── Atom37 slot names ───────────────────────────────────────────────────────── |
| 24 | + |
| 25 | +/// Canonical atom37 names in slot order. |
| 26 | +/// |
| 27 | +/// The index of each name is its atom37 slot number, matching [`AAAtom`]. |
| 28 | +#[rustfmt::skip] |
| 29 | +pub const ATOM37_NAMES: [&str; NUM_ATOM37] = [ |
| 30 | + "N", "CA", "C", "CB", "O", |
| 31 | + "CG", "CG1", "CG2", "OG", "OG1", |
| 32 | + "SG", "CD", "CD1", "CD2", "ND1", |
| 33 | + "ND2", "OD1", "OD2", "SD", "CE", |
| 34 | + "CE1", "CE2", "CE3", "NE", "NE1", |
| 35 | + "NE2", "OE1", "OE2", "CH2", "NH1", |
| 36 | + "NH2", "OH", "CZ", "CZ2", "CZ3", |
| 37 | + "NZ", "OXT", |
| 38 | +]; |
| 39 | + |
| 40 | +/// Map from atom name (e.g. `"CA"`) to its atom37 slot index. |
| 41 | +pub fn atom_order() -> HashMap<String, usize> { |
| 42 | + ATOM37_NAMES |
| 43 | + .iter() |
| 44 | + .enumerate() |
| 45 | + .map(|(i, &name)| (name.to_string(), i)) |
| 46 | + .collect() |
| 47 | +} |
| 48 | + |
| 49 | +/// Returns the atom37 slot index for a named atom, or `None` if unknown. |
| 50 | +pub fn atom37_index(name: &str) -> Option<usize> { |
| 51 | + ATOM37_NAMES.iter().position(|&n| n == name) |
| 52 | +} |
| 53 | + |
| 54 | +// ── AAAtom enum ─────────────────────────────────────────────────────────────── |
| 55 | + |
| 56 | +/// Typed atom37 slots; discriminants match [`ATOM37_NAMES`] indices. |
| 57 | +/// |
| 58 | +/// `Unknown = -1` is used as a sentinel for "atom not present in this residue" |
| 59 | +/// in atom14 tables. Callers working with `usize` slot indices should use |
| 60 | +/// [`atom37_index`] instead. |
| 61 | +#[rustfmt::skip] |
| 62 | +#[derive(Debug, Clone, Copy, PartialEq, Display, EnumString, EnumIter)] |
| 63 | +pub enum AAAtom { |
| 64 | + N = 0, CA = 1, C = 2, CB = 3, O = 4, |
| 65 | + CG = 5, CG1 = 6, CG2 = 7, OG = 8, OG1 = 9, |
| 66 | + SG = 10, CD = 11, CD1 = 12, CD2 = 13, ND1 = 14, |
| 67 | + ND2 = 15, OD1 = 16, OD2 = 17, SD = 18, CE = 19, |
| 68 | + CE1 = 20, CE2 = 21, CE3 = 22, NE = 23, NE1 = 24, |
| 69 | + NE2 = 25, OE1 = 26, OE2 = 27, CH2 = 28, NH1 = 29, |
| 70 | + NH2 = 30, OH = 31, CZ = 32, CZ2 = 33, CZ3 = 34, |
| 71 | + NZ = 35, OXT = 36, |
| 72 | + Unknown = -1, |
| 73 | +} |
| 74 | + |
| 75 | +impl AAAtom { |
| 76 | + pub fn to_index(&self) -> usize { |
| 77 | + *self as usize |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// ── Van der Waals radii ─────────────────────────────────────────────────────── |
| 82 | + |
| 83 | +/// Van der Waals radius (Å) for elements commonly found in proteins. |
| 84 | +/// |
| 85 | +/// Source: Bondi (1964) / standard crystallographic values. |
| 86 | +pub fn vdw_radius(element: &str) -> f32 { |
| 87 | + match element { |
| 88 | + "H" => 1.20, |
| 89 | + "C" => 1.70, |
| 90 | + "N" => 1.55, |
| 91 | + "O" => 1.52, |
| 92 | + "S" => 1.80, |
| 93 | + "P" => 1.80, |
| 94 | + "F" => 1.47, |
| 95 | + "CL" | "Cl" => 1.75, |
| 96 | + "BR" | "Br" => 1.85, |
| 97 | + "I" => 1.98, |
| 98 | + "SE" | "Se" => 1.90, |
| 99 | + _ => 1.70, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// ── Tests ───────────────────────────────────────────────────────────────────── |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use super::*; |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn test_atom_order_completeness() { |
| 111 | + let order = atom_order(); |
| 112 | + assert_eq!(order.len(), NUM_ATOM37); |
| 113 | + assert_eq!(order["N"], 0); |
| 114 | + assert_eq!(order["CA"], 1); |
| 115 | + assert_eq!(order["C"], 2); |
| 116 | + assert_eq!(order["CB"], 3); |
| 117 | + assert_eq!(order["O"], 4); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_atom37_index_roundtrip() { |
| 122 | + for (i, &name) in ATOM37_NAMES.iter().enumerate() { |
| 123 | + assert_eq!(atom37_index(name), Some(i)); |
| 124 | + } |
| 125 | + assert_eq!(atom37_index("ZZZ"), None); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_aaatom_discriminants_match_names() { |
| 130 | + assert_eq!(AAAtom::N as i32, 0); |
| 131 | + assert_eq!(AAAtom::CA as i32, 1); |
| 132 | + assert_eq!(AAAtom::OXT as i32, 36); |
| 133 | + assert_eq!(AAAtom::Unknown as i32, -1); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_vdw_radius_known_elements() { |
| 138 | + assert!((vdw_radius("C") - 1.70).abs() < 1e-6); |
| 139 | + assert!((vdw_radius("N") - 1.55).abs() < 1e-6); |
| 140 | + assert!((vdw_radius("O") - 1.52).abs() < 1e-6); |
| 141 | + assert!((vdw_radius("S") - 1.80).abs() < 1e-6); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_vdw_radius_unknown_defaults_to_carbon() { |
| 146 | + assert!((vdw_radius("X") - 1.70).abs() < 1e-6); |
| 147 | + } |
| 148 | +} |
0 commit comments