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
//! We use this module to identify which bonded parameters are present in a small organic molecule,
//! but are absent from GAFF2.dat.
use std::{collections::HashSet, io};
use bio_files::{AtomGeneric, md_params::ForceFieldParams};
use itertools::Itertools;
/// These values are atom forcefield types.
#[derive(Default)]
pub struct MissingParams {
pub bond: Vec<(String, String)>,
pub angle: Vec<(String, String, String)>,
pub dihedral: Vec<(String, String, String, String)>,
pub improper: Vec<(String, String, String, String)>,
}
impl MissingParams {
/// Find proper and improper dihedral angles that this molecule has, but are not included in gaff2.dat.
/// Overrides are required for these. `params` passed should be from Gaff2.
///
/// Returns (dihedral, improper). Force field combinations present in the molecule, but not
/// gaff2.dat.
///
/// Note: Repetition here with that in `prep.rs`, but this is simplified. See that for reference.
/// todo: Missing valance and bond params too A/R
pub(in crate::param_inference) fn new(
atoms: &[AtomGeneric],
adj_list: &[Vec<usize>],
gaff_params: &ForceFieldParams,
) -> io::Result<Self> {
let mut result = Self::default();
for (i0, neighbors) in adj_list.iter().enumerate() {
for &i1 in neighbors {
if i0 >= i1 {
continue; // Only add each bond once.
}
if atoms[i0].force_field_type.is_none() || atoms[i1].force_field_type.is_none() {
eprintln!(
"Error finding missing bond params for param inference: Missing FF type {} - {}",
atoms[i0].serial_number, atoms[i1].serial_number
);
return Err(io::Error::other(format!(
"Missing FF type: {} - {}",
atoms[i0].serial_number, atoms[i1].serial_number
)));
}
let type_0 = atoms[i0].force_field_type.as_ref().unwrap();
let type_1 = atoms[i1].force_field_type.as_ref().unwrap();
let key = (type_0.clone(), type_1.clone());
if gaff_params.get_bond(&key, true).is_none() {
result.bond.push(key);
}
}
}
// Valence angles: Every connection between 3 atoms bonded linearly.
for (ctr, neighbors) in adj_list.iter().enumerate() {
if neighbors.len() < 2 {
continue;
}
for (&n0, &n1) in neighbors.iter().tuple_combinations() {
if atoms[n0].force_field_type.is_none()
|| atoms[ctr].force_field_type.is_none()
|| atoms[n1].force_field_type.is_none()
{
eprintln!(
"Error finding missing valence angles for param inference: Missing FF type."
);
return Err(io::Error::other("Missing FF type"));
}
let type_n0 = atoms[n0].force_field_type.as_ref().unwrap();
let type_ctr = atoms[ctr].force_field_type.as_ref().unwrap();
let type_n1 = atoms[n1].force_field_type.as_ref().unwrap();
let key = (type_n0.clone(), type_ctr.clone(), type_n1.clone());
if gaff_params.get_valence_angle(&key, true).is_none() {
result.angle.push(key);
}
}
}
// Proper and improper dihedral angles.
let mut seen = HashSet::<(usize, usize, usize, usize)>::new();
// Proper dihedrals: Atoms 1-2-3-4 bonded linearly
for (i1, nbr_j) in adj_list.iter().enumerate() {
for &i2 in nbr_j {
if i1 >= i2 {
continue;
} // handle each j-k bond once
for &i0 in adj_list[i1].iter().filter(|&&x| x != i2) {
for &i3 in adj_list[i2].iter().filter(|&&x| x != i1) {
if i0 == i3 {
continue;
}
// Canonicalise so (i1, i2) is always (min, max)
let idx_key = if i1 < i2 {
(i0, i1, i2, i3)
} else {
(i3, i2, i1, i0)
};
if !seen.insert(idx_key) {
continue;
}
if atoms[i0].force_field_type.is_none()
|| atoms[i1].force_field_type.is_none()
|| atoms[i2].force_field_type.is_none()
|| atoms[i3].force_field_type.is_none()
{
eprintln!(
"Error finding missing dihedrals for param inference: Missing FF type."
);
return Err(io::Error::other("Missing FF type"));
}
let type_0 = atoms[i0].force_field_type.as_ref().unwrap();
let type_1 = atoms[i1].force_field_type.as_ref().unwrap();
let type_2 = atoms[i2].force_field_type.as_ref().unwrap();
let type_3 = atoms[i3].force_field_type.as_ref().unwrap();
let key = (
type_0.clone(),
type_1.clone(),
type_2.clone(),
type_3.clone(),
);
if gaff_params.get_dihedral(&key, true, true).is_none() {
result.dihedral.push(key);
}
}
}
}
}
// Improper dihedrals 2-1-3-4. Atom 3 is the hub, with the other 3 atoms bonded to it.
// The order of the others in the angle calculation affects the sign of the result.
// Generally only for planar configs.
//
// Note: The sattelites are expected to be in alphabetical order, re their FF types.
// So, for the hub of "ca" with sattelites of "ca", "ca", and "os", the correct combination
// to look for in the params is "ca-ca-ca-os"
for (ctr, satellites) in adj_list.iter().enumerate() {
if satellites.len() < 3 {
continue;
}
// Unique unordered triples of neighbours
for a in 0..satellites.len() - 2 {
for b in a + 1..satellites.len() - 1 {
for d in b + 1..satellites.len() {
let (sat0, sat1, sat2) = (satellites[a], satellites[b], satellites[d]);
let idx_key = (sat0, sat1, ctr, sat2); // order is fixed → no swap
if !seen.insert(idx_key) {
continue;
}
if atoms[sat0].force_field_type.is_none()
|| atoms[sat1].force_field_type.is_none()
|| atoms[ctr].force_field_type.is_none()
|| atoms[sat2].force_field_type.is_none()
{
eprintln!(
"Error finding missing improper dihedrals for param inference: Missing FF type."
);
return Err(io::Error::other("Missing FF type"));
}
let type_0 = atoms[sat0].force_field_type.as_ref().unwrap();
let type_1 = atoms[sat1].force_field_type.as_ref().unwrap();
let type_ctr = atoms[ctr].force_field_type.as_ref().unwrap();
let type_2 = atoms[sat2].force_field_type.as_ref().unwrap();
// Sort satellites alphabetically; required to ensure we don't miss combinations.
let mut sat_types = [type_0.clone(), type_1.clone(), type_2.clone()];
sat_types.sort();
let key = (
sat_types[0].clone(),
sat_types[1].clone(),
type_ctr.clone(),
sat_types[2].clone(),
);
// todo: Re this note: it may be tough to determine which impropers we need.
// In the case of improper, unlike all other param types, we are allowed to
// have missing values. Impropers areonly, by Amber convention, for planar
// hub and spoke setups, so non-planar ones will be omitted. These may occur,
// for example, at ring intersections.
if gaff_params.get_dihedral(&key, false, true).is_none() {
result.improper.push(key);
}
}
}
}
}
Ok(result)
}
}