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
//! For identifying Tautomers
use std::collections::HashSet;
use bio_files::BondType::*;
use na_seq::Element::*;
use crate::molecules::{Atom, Bond, common::MoleculeCommon};
const MAX_PATH_LEN_BONDS: usize = 8;
const MAX_TAUTOMERS: usize = 512;
impl MoleculeCommon {
/// Find tautomers of this molecule. These are structural isomers that readily interconvert.
///It shifts around single vs double bonds, and which corresponding atoms have hydrogens bound.
///
/// Notes:
/// - This version enumerates *explicit-H* prototropic tautomers (it requires H atoms in `atoms`).
/// - It moves H along alternating single/double bond paths and flips bond orders along that path.
pub fn find_tautomers(&self) -> Vec<MoleculeCommon> {
// Map (min,max) -> bond index for fast lookups.
let mut bond_ix = std::collections::HashMap::<(usize, usize), usize>::new();
for (i, b) in self.bonds.iter().enumerate() {
bond_ix.insert(edge_key(b.atom_0, b.atom_1), i);
}
// Identify explicit H atoms that are bound to a hetero atom donor.
let mut h_donors: Vec<(usize, usize, usize)> = Vec::new(); // (h, donor, bond_idx)
for h in 0..self.atoms.len() {
if self.atoms[h].element != Hydrogen {
continue;
}
if self.adjacency_list[h].len() != 1 {
continue;
}
let donor = self.adjacency_list[h][0];
if !is_hetero(&self.atoms[donor]) {
continue;
}
if let Some(&bidx) = bond_ix.get(&edge_key(h, donor)) {
// Only allow H-single bonds.
if bond_order(&self.bonds[bidx]) == Some(1) {
h_donors.push((h, donor, bidx));
}
}
}
let mut out = Vec::<MoleculeCommon>::new();
let mut seen = HashSet::<Vec<(usize, usize, u8)>>::new();
for (h, donor, hbond_idx) in h_donors {
let paths = enumerate_paths(self, &bond_ix, donor, MAX_PATH_LEN_BONDS);
for p in paths {
if out.len() >= MAX_TAUTOMERS {
return out;
}
let acceptor = *p.last().unwrap();
if acceptor == donor {
continue;
}
// Create tautomer by:
// - moving the H from donor to acceptor
// - flipping single<->double along each heavy bond on the donor..acceptor path
let mut m2 = self.clone();
// Move H bond endpoint to acceptor (keep it single).
{
let b = &mut m2.bonds[hbond_idx];
if b.atom_0 == h {
b.atom_1 = acceptor;
} else if b.atom_1 == h {
b.atom_0 = acceptor;
} else {
// If the bond structure differs, adjust this block.
continue;
}
// Ensure single H-bond.
// (If your BondType supports it, this keeps it single.)
// If you want to preserve existing type, remove this.
// (No-op if already single.)
// set_bond_order(b, 1);
}
// Flip bond orders along the path (donor=a0, ..., acceptor=ak)
let mut ok = true;
for w in p.windows(2) {
let a = w[0];
let c = w[1];
let Some(&bidx) = bond_ix.get(&edge_key(a, c)) else {
ok = false;
break;
};
let Some(ord) = bond_order(&m2.bonds[bidx]) else {
ok = false;
break;
};
let new_ord = if ord == 1 { 2 } else { 1 };
set_bond_order(&mut m2.bonds[bidx], new_ord);
}
if !ok {
continue;
}
// Rebuild adjacency_list to match bonds after edits.
m2.build_adjacency_list();
let sig = molecule_signature(&m2);
if seen.insert(sig) {
out.push(m2);
}
}
}
out
}
}
fn is_hetero(atom: &Atom) -> bool {
matches!(atom.element, Nitrogen | Oxygen | Sulfur)
}
// Doesn't take into account aromatic bonds. Is that ok?
fn bond_order(b: &Bond) -> Option<u8> {
match b.bond_type {
Single => Some(1),
Double => Some(2),
Triple => Some(3),
_ => None,
}
}
fn set_bond_order(b: &mut Bond, order: u8) {
b.bond_type = match order {
1 => Single,
2 => Double,
3 => Triple,
_ => b.bond_type,
};
}
fn molecule_signature(m: &MoleculeCommon) -> Vec<(usize, usize, u8)> {
let mut v: Vec<(usize, usize, u8)> = m
.bonds
.iter()
.map(|b| {
let (a, c) = edge_key(b.atom_0, b.atom_1);
let ord = bond_order(b).unwrap_or(0);
(a, c, ord)
})
.collect();
v.sort_unstable();
v
}
fn edge_key(a: usize, b: usize) -> (usize, usize) {
if a < b { (a, b) } else { (b, a) }
}
fn rec(
mol: &MoleculeCommon,
bond_ix: &std::collections::HashMap<(usize, usize), usize>,
cur: usize,
start: usize,
last_order: Option<u8>,
max_len_bonds: usize,
visited: &mut [bool],
path: &mut Vec<usize>,
out: &mut Vec<Vec<usize>>,
) {
// path contains atoms, bonds count is path.len()-1
let bonds_len = path.len().saturating_sub(1);
if bonds_len > max_len_bonds {
return;
}
if cur != start && is_hetero(&mol.atoms[cur]) && bonds_len >= 2 {
out.push(path.clone());
}
for &nbr in &mol.adjacency_list[cur] {
// Skip H atoms; we only walk the heavy-atom graph for tautomeric paths.
if matches!(mol.atoms[nbr].element, Hydrogen) {
continue;
}
if visited[nbr] {
continue;
}
let Some(&bidx) = bond_ix.get(&edge_key(cur, nbr)) else {
continue;
};
let Some(ord) = bond_order(&mol.bonds[bidx]) else {
continue;
};
if let Some(last) = last_order
&& ord == last
{
continue; // enforce alternation
}
visited[nbr] = true;
path.push(nbr);
rec(
mol,
bond_ix,
nbr,
start,
Some(ord),
max_len_bonds,
visited,
path,
out,
);
path.pop();
visited[nbr] = false;
}
}
// DFS that enumerates alternating single/double paths from donor to hetero acceptors.
fn enumerate_paths(
mol: &MoleculeCommon,
bond_ix: &std::collections::HashMap<(usize, usize), usize>,
start: usize,
max_len_bonds: usize,
) -> Vec<Vec<usize>> {
let mut out = Vec::new();
let mut visited = vec![false; mol.atoms.len()];
let mut path = Vec::<usize>::new();
visited[start] = true;
path.push(start);
rec(
mol,
bond_ix,
start,
start,
None,
max_len_bonds,
&mut visited,
&mut path,
&mut out,
);
out
}