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
use super::periodic_table::{ELEMENT_MASS, ELEMENT_NAME, ELEMENT_NAME_UPPER, ELEMENT_VDW};
use tinystr::TinyAsciiStr;
/// Stack-allocated ASCII atom string (max 8 bytes, no heap allocation).
pub type AtomStr = TinyAsciiStr<8>;
pub(crate) const ATOM_NAME_EXPECT: &str = "atom name fits in 8 bytes";
pub(crate) const ATOM_RESNAME_EXPECT: &str = "residue name fits in 8 bytes";
pub(crate) const ATOM_TYPE_NAME_EXPECT: &str = "atom type name fits in 8 bytes";
pub trait AtomLike {
/// Atom name.
fn get_name(&self) -> &str;
fn set_name(&mut self, name: &str);
/// Residue name.
fn get_resname(&self) -> &str;
fn set_resname(&mut self, resname: &str);
/// Residue id (aka residue number). This could be negative!
fn get_resid(&self) -> isize;
fn set_resid(&mut self, resid: isize);
/// Residue index. Assigned when reading the topology.
/// Unique for each contiguous span of resid. Starts from zero.
fn get_resindex(&self) -> usize;
fn set_resindex(&mut self, resindex: usize);
/// Atomic number in the periodic table.
fn get_atomic_number(&self) -> u8;
fn set_atomic_number(&mut self, atomic_number: u8);
/// Mass in atomic units
fn get_mass(&self) -> f32;
fn set_mass(&mut self, mass: f32);
/// Charge in electric charges.
fn get_charge(&self) -> f32;
fn set_charge(&mut self, charge: f32);
/// Name of the atom type.
fn get_type_name(&self) -> &str;
fn set_type_name(&mut self, type_name: &str);
/// Unique id of the atom type.
fn get_type_id(&self) -> u32;
fn set_type_id(&mut self, type_id: u32);
/// PDB chain identifier.
fn get_chain(&self) -> char;
fn set_chain(&mut self, chain: char);
/// PDB B-factor.
fn get_bfactor(&self) -> f32;
fn set_bfactor(&mut self, bfactor: f32);
/// PDB occupancy.
fn get_occupancy(&self) -> f32;
fn set_occupancy(&mut self, occupancy: f32);
}
/// Information about the atom except its coordinates.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Atom {
/// Atom name.
pub name: AtomStr,
/// Residue name.
pub resname: AtomStr,
/// Residue id (aka residue number). This could be negative!
pub resid: i32, // Could be negative.
/// Residue index. Assigned when reading the [topology](super::Topology).
/// Unique for each contigous span of resid. Starts from zero.
pub resindex: usize,
/// Atomic number in the periodic table.
pub atomic_number: u8,
/// Mass in atomic units
pub mass: f32,
/// Charge in electroc charges.
pub charge: f32,
/// Name of the atom type.
pub type_name: AtomStr,
/// Unique id of the atom type.
pub type_id: u32,
// PDB chain identifier.
pub chain: char,
// PDB B-factor.
pub bfactor: f32,
/// PDB occupancy.
pub occupancy: f32,
}
impl Default for Atom {
fn default() -> Self {
let empty = AtomStr::try_from_str("").unwrap();
Atom {
name: empty,
resname: empty,
type_name: empty,
resid: 0,
resindex: 0,
atomic_number: 0,
mass: 0.0,
charge: 0.0,
type_id: 0,
chain: '\0',
bfactor: 0.0,
occupancy: 0.0,
}
}
}
impl Atom {
pub fn new() -> Self {
Default::default()
}
pub fn guess_element_from_name(&mut self) {
self.atomic_number = 0;
// Index of the first letter in atom name
if let Some(i) = self.name.find(|c: char| c.is_ascii_alphabetic()) {
// Match special cases when atom name doesn't
// start with the element name at all
match self.name.as_str() {
"SOD" => self.atomic_number = 11, //Sodium
"POT" => self.atomic_number = 19, //Potassium
_ => (),
}
// Find matching element name in periodic table
// Attempt 2-letter matching if possible
if self.atomic_number == 0 && self.name.len() >= 2 {
let c2 = self.name[i..=i + 1].to_ascii_uppercase();
for an in 1..ELEMENT_NAME_UPPER.len() {
let el = ELEMENT_NAME_UPPER[an];
if el.len() == 2 && el == c2 {
// If the first letters are C,N,O,H,P be extra careful
// and only match to two-letter elements if the resname is the
// same as name (like in ions CA and CL).
// Otherwise skip to single-letter matching
match el.chars().next().unwrap() {
'C' | 'N' | 'O' | 'H' | 'P' => {
if self.name == self.resname {
self.atomic_number = an as u8;
}
}
_ => {
self.atomic_number = an as u8;
}
}
}
}
}
// If atomic_number is still 0 try 1-letter matching
if self.atomic_number == 0 {
for an in 1..ELEMENT_NAME.len() {
let el = ELEMENT_NAME[an];
if el.len() == 1 && el == &self.name[i..=i] {
self.atomic_number = an as u8;
}
}
}
}
}
pub fn guess_element_and_mass_from_name(&mut self) {
self.guess_element_from_name();
// Fill mass field
self.mass = ELEMENT_MASS[self.atomic_number as usize];
}
/// Naive guessing of the mass and element from the atom name.
// pub fn guess_element_and_mass_from_name(&mut self) {
// (self.atomic_number, self.mass) = match self
// .name
// .as_str()
// .trim_start_matches(char::is_numeric)
// .chars()
// .next()
// .unwrap()
// {
// 'C' => (6, ELEMENT_MASS[6]),
// 'O' => (8, ELEMENT_MASS[8]),
// 'N' => (7, ELEMENT_MASS[7]),
// 'S' => (16, ELEMENT_MASS[16]),
// 'H' => (1, ELEMENT_MASS[1]),
// 'P' => (15, ELEMENT_MASS[15]),
// 'F' => (9, ELEMENT_MASS[9]),
// 'B' => (5, ELEMENT_MASS[5]),
// _ => (0, 1.0), // Unknown atom
// }
// }
/// Returns atom's Van der Waals radius based on its atomic number.
/// If the element is not recognized returns a default value of 0.15 nm (atomnum '0').
pub fn vdw(&self) -> f32 {
ELEMENT_VDW[self.atomic_number as usize] * 0.1
}
}
/// Returns the uppercase element symbol for the given atomic number (e.g. `"FE"`, `"C"`, `"HE"`).
/// Returns `""` for atomic number 0 (unknown).
pub(crate) fn element_symbol(atomic_number: u8) -> &'static str {
ELEMENT_NAME_UPPER
.get(atomic_number as usize)
.copied()
.unwrap_or("")
}
impl AtomLike for Atom {
// Atom name
fn get_name(&self) -> &str {
self.name.as_str()
}
fn set_name(&mut self, name: &str) {
self.name = AtomStr::try_from_str(name).expect(ATOM_NAME_EXPECT);
}
// Residue name
fn get_resname(&self) -> &str {
self.resname.as_str()
}
fn set_resname(&mut self, resname: &str) {
self.resname = AtomStr::try_from_str(resname).expect(ATOM_RESNAME_EXPECT);
}
// Residue id
fn get_resid(&self) -> isize {
self.resid as isize
}
fn set_resid(&mut self, resid: isize) {
self.resid = resid as i32
}
// Residue index
fn get_resindex(&self) -> usize {
self.resindex
}
fn set_resindex(&mut self, resindex: usize) {
self.resindex = resindex;
}
// Atomic number
fn get_atomic_number(&self) -> u8 {
self.atomic_number
}
fn set_atomic_number(&mut self, atomic_number: u8) {
self.atomic_number = atomic_number;
}
// Mass
fn get_mass(&self) -> f32 {
self.mass
}
fn set_mass(&mut self, mass: f32) {
self.mass = mass;
}
// Charge
fn get_charge(&self) -> f32 {
self.charge
}
fn set_charge(&mut self, charge: f32) {
self.charge = charge;
}
// Type name
fn get_type_name(&self) -> &str {
self.type_name.as_str()
}
fn set_type_name(&mut self, type_name: &str) {
self.type_name = AtomStr::try_from_str(type_name).expect(ATOM_TYPE_NAME_EXPECT);
}
// Type id
fn get_type_id(&self) -> u32 {
self.type_id
}
fn set_type_id(&mut self, type_id: u32) {
self.type_id = type_id;
}
// Chain
fn get_chain(&self) -> char {
self.chain
}
fn set_chain(&mut self, chain: char) {
self.chain = chain;
}
// B-factor
fn get_bfactor(&self) -> f32 {
self.bfactor
}
fn set_bfactor(&mut self, bfactor: f32) {
self.bfactor = bfactor;
}
// Occupancy
fn get_occupancy(&self) -> f32 {
self.occupancy
}
fn set_occupancy(&mut self, occupancy: f32) {
self.occupancy = occupancy;
}
}