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
// Chemfiles, a modern library for chemistry file reading and writing
// Copyright (C) 2015-2018 Guillaume Fraux -- BSD licensed
use std::ops::{Drop, Deref};
use std::marker::PhantomData;
use std::ptr;

use chemfiles_sys::*;
use errors::{check_not_null, check_success, Error};
use property::{Property, RawProperty, PropertiesIter};
use strings;

/// A `Residue` is a group of atoms belonging to the same logical unit. They
/// can be small molecules, amino-acids in a protein, monomers in polymers,
/// *etc.*
pub struct Residue {
    handle: *mut CHFL_RESIDUE,
}

/// An analog to a reference to a residue (`&Residue`)
pub struct ResidueRef<'a> {
    inner: Residue,
    marker: PhantomData<&'a Residue>
}

impl<'a> Deref for ResidueRef<'a> {
    type Target = Residue;
    fn deref(&self) -> &Residue {
        &self.inner
    }
}

impl Clone for Residue {
    fn clone(&self) -> Residue {
        unsafe {
            let new_handle = chfl_residue_copy(self.as_ptr());
            Residue::from_ptr(new_handle)
        }
    }
}

impl Residue {
    /// Create a `Residue` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the pointer.
    #[inline]
    pub(crate) unsafe fn from_ptr(ptr: *mut CHFL_RESIDUE) -> Residue {
        check_not_null(ptr);
        Residue {
            handle: ptr
        }
    }

    /// Create a borrowed `Residue` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the
    /// pointer, except for it being non-null, and the caller is responsible
    /// for setting the right lifetime
    #[inline]
    pub(crate) unsafe fn ref_from_ptr<'a>(ptr: *const CHFL_RESIDUE) -> ResidueRef<'a> {
        ResidueRef {
            inner: Residue::from_ptr(ptr as *mut CHFL_RESIDUE),
            marker: PhantomData,
        }
    }

    /// Get the underlying C pointer as a const pointer.
    #[inline]
    pub(crate) fn as_ptr(&self) -> *const CHFL_RESIDUE {
        self.handle
    }

    /// Get the underlying C pointer as a mutable pointer.
    #[inline]
    pub(crate) fn as_mut_ptr(&mut self) -> *mut CHFL_RESIDUE {
        self.handle
    }

    /// Create a new residue with the given `name`
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let residue = Residue::new("ALA");
    /// assert_eq!(residue.name(), "ALA");
    /// assert_eq!(residue.id(), None);
    /// ```
    pub fn new<'a>(name: impl Into<&'a str>) -> Residue {
        let buffer = strings::to_c(name.into());
        unsafe {
            let handle = chfl_residue(buffer.as_ptr());
            Residue::from_ptr(handle)
        }
    }

    /// Create a new residue with the given `name` and `id` as identifier.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let residue = Residue::with_id("ALA", 67);
    /// assert_eq!(residue.name(), "ALA");
    /// assert_eq!(residue.id(), Some(67));
    /// ```
    pub fn with_id<'a>(name: impl Into<&'a str>, id: u64) -> Residue {
        let buffer = strings::to_c(name.into());
        unsafe {
            let handle = chfl_residue_with_id(buffer.as_ptr(), id);
            Residue::from_ptr(handle)
        }
    }

    /// Get the number of atoms in this residue.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let mut residue = Residue::new("water");
    /// assert_eq!(residue.size(), 0);
    ///
    /// residue.add_atom(0);
    /// residue.add_atom(1);
    /// residue.add_atom(2);
    /// assert_eq!(residue.size(), 3);
    /// ```
    pub fn size(&self) -> usize {
        let mut size = 0;
        unsafe {
            check_success(chfl_residue_atoms_count(self.as_ptr(), &mut size));
        }
        #[allow(clippy::cast_possible_truncation)]
        return size as usize;
    }

    /// Get the identifier of this residue in the initial topology file.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let residue = Residue::with_id("", 42);
    /// assert_eq!(residue.id(), Some(42));
    /// ```
    pub fn id(&self) -> Option<u64> {
        let mut resid = 0;
        let status = unsafe {
            chfl_residue_id(self.as_ptr(), &mut resid)
        };

        if status == chfl_status::CHFL_SUCCESS {
            return Some(resid);
        } else if status == chfl_status::CHFL_GENERIC_ERROR {
            return None;
        } else {
            panic!("unexpected failure: {}", Error::last_error());
        }
    }

    /// Get the name of this residue.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let residue = Residue::new("water");
    /// assert_eq!(residue.name(), "water");
    /// ```
    pub fn name(&self) -> String {
        let get_name = |ptr, len| unsafe { chfl_residue_name(self.as_ptr(), ptr, len) };
        let name = strings::call_autogrow_buffer(64, get_name).expect("getting residue name failed");
        return strings::from_c(name.as_ptr());
    }

    /// Add the atom at index `atom` in this residue.
    ///
    /// This will fail if the atom is already in the residue.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let mut residue = Residue::new("water");
    /// assert_eq!(residue.size(), 0);
    /// assert_eq!(residue.contains(56), false);
    ///
    /// residue.add_atom(56);
    /// assert_eq!(residue.size(), 1);
    /// assert_eq!(residue.contains(56), true);
    ///
    /// // Adding the same atom twice is fine
    /// residue.add_atom(56);
    /// assert_eq!(residue.size(), 1);
    /// ```
    pub fn add_atom(&mut self, atom: usize) {
        unsafe {
            check_success(chfl_residue_add_atom(self.as_mut_ptr(), atom as u64));
        }
    }

    /// Check if the atom at index `i` is in this residue
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let mut residue = Residue::new("water");
    /// assert_eq!(residue.contains(56), false);
    ///
    /// residue.add_atom(56);
    /// assert_eq!(residue.contains(56), true);
    /// ```
    pub fn contains(&self, atom: usize) -> bool {
        let mut inside = 0;
        unsafe {
            check_success(chfl_residue_contains(self.as_ptr(), atom as u64, &mut inside));
        }
        return inside != 0;
    }

    /// Get the list of atoms of this residue.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Residue;
    /// let mut residue = Residue::new("water");
    /// assert_eq!(residue.atoms(), vec![]);
    ///
    /// residue.add_atom(56);
    /// assert_eq!(residue.atoms(), vec![56]);
    /// ```
    pub fn atoms(&self) -> Vec<usize> {
        let size = self.size();
        let count = size as u64;
        let mut indices = vec![u64::max_value(); size];
        unsafe {
            check_success(chfl_residue_atoms(
                self.as_ptr(),
                indices.as_mut_ptr(),
                count,
            ));
        }
        #[allow(clippy::cast_possible_truncation)]
        return indices.into_iter().map(|idx| idx as usize).collect();
    }

    /// Add a new `property` with the given `name` to this residue.
    ///
    /// If a property with the same name already exists, this function override
    /// the existing property with the new one.
    ///
    /// # Examples
    /// ```
    /// # use chemfiles::{Residue, Property};
    /// let mut residue = Residue::new("ALA");
    /// residue.set("a string", "hello");
    /// residue.set("a double", 3.2);
    ///
    /// assert_eq!(residue.get("a string"), Some(Property::String("hello".into())));
    /// assert_eq!(residue.get("a double"), Some(Property::Double(3.2)));
    /// ```
    pub fn set(&mut self, name: &str, property: impl Into<Property>) {
        let buffer = strings::to_c(name);
        let property = property.into().as_raw();
        unsafe {
            check_success(chfl_residue_set_property(
                self.as_mut_ptr(), buffer.as_ptr(), property.as_ptr()
            ));
        }
    }

    /// Get a property with the given `name` in this frame, if it exist.
    ///
    /// # Examples
    /// ```
    /// # use chemfiles::{Residue, Property};
    /// let mut residue = Residue::new("ALA");
    /// residue.set("foo", Property::Double(22.2));
    ///
    /// assert_eq!(residue.get("foo"), Some(Property::Double(22.2)));
    /// assert_eq!(residue.get("Bar"), None);
    /// ```
    pub fn get(&self, name: &str) -> Option<Property> {
        let buffer = strings::to_c(name);
        unsafe {
            let handle = chfl_residue_get_property(self.as_ptr(), buffer.as_ptr());
            if handle.is_null() {
                None
            } else {
                let raw = RawProperty::from_ptr(handle);
                Some(Property::from_raw(raw))
            }
        }
    }

    /// Get an iterator over all (name, property) pairs for this frame
    ///
    /// # Examples
    /// ```
    /// # use chemfiles::{Residue, Property};
    /// let mut residue = Residue::new("ALA");
    /// residue.set("foo", Property::Double(22.2));
    /// residue.set("bar", Property::Bool(false));
    ///
    /// for (name, property) in residue.properties() {
    ///     if name == "foo" {
    ///         assert_eq!(property, Property::Double(22.2));
    ///     } else if name == "bar" {
    ///         assert_eq!(property, Property::Bool(false));
    ///     }
    /// }
    /// ```
    pub fn properties(&self) -> PropertiesIter {
        let mut count = 0;
        unsafe {
            check_success(chfl_residue_properties_count(self.as_ptr(), &mut count));
        }

        #[allow(clippy::cast_possible_truncation)]
        let size = count as usize;
        let mut c_names = vec![ptr::null_mut(); size];
        unsafe {
            check_success(chfl_residue_list_properties(self.as_ptr(), c_names.as_mut_ptr(), count));
        }

        let mut names = Vec::new();
        for ptr in c_names {
            names.push(strings::from_c(ptr));
        }

        PropertiesIter {
            names: names.into_iter(),
            getter: Box::new(move |name| self.get(&*name).expect("failed to get property"))
        }
    }
}

impl Drop for Residue {
    fn drop(&mut self) {
        unsafe {
            let _ = chfl_free(self.as_ptr() as *const _);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clone() {
        let mut residue = Residue::new("A");
        assert_eq!(residue.size(), 0);

        let copy = residue.clone();
        assert_eq!(copy.size(), 0);

        residue.add_atom(3);
        residue.add_atom(7);
        assert_eq!(residue.size(), 2);
        assert_eq!(copy.size(), 0);
    }

    #[test]
    fn name() {
        let residue = Residue::new("A");
        assert_eq!(residue.name(), "A");
    }

    #[test]
    fn id() {
        let residue = Residue::new("A");
        assert_eq!(residue.id(), None);

        let residue = Residue::with_id("A", 42);
        assert_eq!(residue.id(), Some(42));
    }

    #[test]
    fn atoms() {
        let mut residue = Residue::new("A");
        assert_eq!(residue.size(), 0);

        residue.add_atom(0);
        residue.add_atom(3);
        residue.add_atom(45);
        assert_eq!(residue.size(), 3);

        assert_eq!(residue.contains(3), true);
        assert_eq!(residue.contains(5), false);

        assert_eq!(residue.atoms(), vec![0, 3, 45]);
    }

    #[test]
    fn property() {
        let mut residue = Residue::new("ALA");

        residue.set("foo", -22.0);
        assert_eq!(residue.get("foo"), Some(Property::Double(-22.0)));
        assert_eq!(residue.get("bar"), None);

        residue.set("bar", Property::String("here".into()));
        for (name, property) in residue.properties() {
            if name == "foo" {
                assert_eq!(property, Property::Double(-22.0));
            } else if name == "bar" {
                assert_eq!(property, Property::String("here".into()));
            }
        }
    }
}