pdbtbx 0.12.0

A library to open/edit/save (crystallographic) Protein Data Bank (PDB) and mmCIF files.
Documentation
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//! # Atoms with containing Hierarchy
//!
//! Defines structs to contain a piece of Hierarchy within the PDB structure. It also defines mutable
//! counterparts to these to allow for mutable access to one level in the hierarchy at the same time.
//!
//! Using the traits you can write more generic functions.
//! ```rust
//! use pdbtbx::*;
//! let (mut pdb, _errors) = pdbtbx::open("example-pdbs/1ubq.pdb").unwrap();
//!
//! // Return the X Y coordinates if the conformer name is "HOH"
//! fn find_position(hierarchy: impl ContainsAtomConformer) -> Option<(f64, f64)> {
//!     if hierarchy.conformer().name() == "HOH" {
//!         Some((hierarchy.atom().x(), hierarchy.atom().y()))
//!     } else {
//!         None
//!     }
//! }
//!
//! // Translate the Y position of all atoms and return all HOH X Y coordinates
//! pdb.atoms_with_hierarchy_mut().filter_map(|mut hierarchy| {
//!     let new_y = hierarchy.atom().y() - 150.0;
//!     hierarchy.atom_mut().set_y(new_y);
//!     find_position(hierarchy)
//! });
//!
//! ```
//!
//! ## Crate supporters
//!
//! Each struct implements three important methods: `new`, `from_tuple`, and `extend`. These methods
//! allow for the construction of the structs and are designated `pub(crate)` to ensure that users
//! cannot create structs that do not follow the rule:
//!
//! > All elements of the struct should be a child of all previous levels in the hierarchy.
#![allow(clippy::unwrap_used, dead_code)]
use super::*;
use std::marker::PhantomData;

/// A struct to hold references to an Atom and its containing Conformer.
#[derive(Debug, Clone)]
pub struct AtomConformer<'a> {
    /// This Atom
    atom: &'a Atom,
    /// The Conformer containing this Atom
    conformer: &'a Conformer,
}

/// A struct to hold references to an Atom and its containing Conformer and Residue.
#[derive(Debug, Clone)]
pub struct AtomConformerResidue<'a> {
    /// This Atom
    atom: &'a Atom,
    /// The Conformer containing this Atom
    conformer: &'a Conformer,
    /// The Residue containing this Atom
    residue: &'a Residue,
}

/// A struct to hold references to an Atom and its containing Conformer, Residue, and Chain.
#[derive(Debug, Clone)]
pub struct AtomConformerResidueChain<'a> {
    /// This Atom
    atom: &'a Atom,
    /// The Conformer containing this Atom
    conformer: &'a Conformer,
    /// The Residue containing this Atom
    residue: &'a Residue,
    /// The Chain containing this Atom
    chain: &'a Chain,
}

/// A struct to hold references to an Atom and its containing Conformer, Residue, Chain, and Model.
#[derive(Debug, Clone)]
pub struct AtomConformerResidueChainModel<'a> {
    /// This Atom
    atom: &'a Atom,
    /// The Conformer containing this Atom
    conformer: &'a Conformer,
    /// The Residue containing this Atom
    residue: &'a Residue,
    /// The Chain containing this Atom
    chain: &'a Chain,
    /// The Model containing this Atom
    model: &'a Model,
}

/// A struct to hold mutable references to an Atom and its containing Conformer.
#[derive(Debug, Clone)]
pub struct AtomConformerMut<'a> {
    /// This Atom
    atom: *mut Atom,
    /// The Conformer containing this Atom
    conformer: *mut Conformer,
    phantom: PhantomData<&'a usize>,
}

/// A struct to hold mutable references to an Atom and its containing Conformer and Residue.
#[derive(Debug, Clone)]
pub struct AtomConformerResidueMut<'a> {
    /// This Atom
    atom: *mut Atom,
    /// The Conformer containing this Atom
    conformer: *mut Conformer,
    /// The Residue containing this Atom
    residue: *mut Residue,
    phantom: PhantomData<&'a usize>,
}

/// A struct to hold mutable references to an Atom and its containing Conformer, Residue, and
/// Chain.
#[derive(Debug, Clone)]
pub struct AtomConformerResidueChainMut<'a> {
    /// This Atom
    atom: *mut Atom,
    /// The Conformer containing this Atom
    conformer: *mut Conformer,
    /// The Residue containing this Atom
    residue: *mut Residue,
    /// The Chain containing this Atom
    chain: *mut Chain,
    phantom: PhantomData<&'a usize>,
}

/// A struct to hold mutable references to an Atom and its containing Conformer, Residue, Chain, and Model.
#[derive(Debug, Clone)]
pub struct AtomConformerResidueChainModelMut<'a> {
    /// This Atom
    atom: *mut Atom,
    /// The Conformer containing this Atom
    conformer: *mut Conformer,
    /// The Residue containing this Atom
    residue: *mut Residue,
    /// The Chain containing this Atom
    chain: *mut Chain,
    /// The Model containing this Atom
    model: *mut Model,
    phantom: PhantomData<&'a usize>,
}

/// A trait which defines all functions on a hierarchy which contains Atoms and Conformers.
pub trait ContainsAtomConformer {
    /// Get a reference to the atom
    fn atom(&self) -> &Atom;
    /// Get a reference to the conformer
    fn conformer(&self) -> &Conformer;
    /// Tests if this atom is part of the protein backbone
    fn is_backbone(&self) -> bool {
        self.conformer().is_amino_acid() && self.atom().is_backbone()
    }
    /// Tests if this atom is part of a side chain of an amino acid
    fn is_sidechain(&self) -> bool {
        self.conformer().is_amino_acid() && !self.atom().is_backbone()
    }
}

/// A trait which defines all functions on a hierarchy which contains Atoms, Conformers, and Residues.
pub trait ContainsAtomConformerResidue: ContainsAtomConformer {
    /// Get a reference to the residue
    fn residue(&self) -> &Residue;
}

/// A trait which defines all functions on a hierarchy which contains Atoms, Conformers, Residues, and Chains.
pub trait ContainsAtomConformerResidueChain: ContainsAtomConformerResidue {
    /// Get a reference to the chain
    fn chain(&self) -> &Chain;
}

/// A trait which defines all functions on a hierarchy which contains Atoms, Conformers, Residues, Chains, and Models.
pub trait ContainsAtomConformerResidueChainModel: ContainsAtomConformerResidueChain {
    /// Get a reference to the model
    fn model(&self) -> &Model;
}

/// A trait which defines all functions on a mutable hierarchy which contains Atoms and Conformers.
pub trait ContainsAtomConformerMut: ContainsAtomConformer {
    /// Get a mutable reference to the atom
    fn atom_mut(&mut self) -> &mut Atom;
    /// Get a mutable reference to the conformer
    fn conformer_mut(&mut self) -> &mut Conformer;
}

/// A trait which defines all functions on a mutable hierarchy which contains Atoms, Conformers, and Residues.
pub trait ContainsAtomConformerResidueMut:
    ContainsAtomConformerResidue + ContainsAtomConformerMut
{
    /// Get a mutable reference to the residue
    fn residue_mut(&mut self) -> &mut Residue;
}

/// A trait which defines all functions on a mutable hierarchy which contains Atoms, Conformers, Residues, and Chains.
pub trait ContainsAtomConformerResidueChainMut:
    ContainsAtomConformerResidueChain + ContainsAtomConformerResidueMut
{
    /// Get a mutable reference to the chain
    fn chain_mut(&mut self) -> &mut Chain;
}

/// A trait which defines all functions on a mutable hierarchy which contains Atoms, Conformers, Residues, Chains, and Models.
pub trait ContainsAtomConformerResidueChainModelMut:
    ContainsAtomConformerResidueChainModel + ContainsAtomConformerResidueChainMut
{
    /// Get a mutable reference to the model
    fn model_mut(&mut self) -> &mut Model;
}

#[cfg(feature = "rstar")]
use rstar::{PointDistance, RTreeObject, AABB};

macro_rules! impl_hierarchy {
    ($($type:ty,)*) => {
        $(#[cfg(feature = "rstar")]
        impl<'a> RTreeObject for $type {
            type Envelope = AABB<(f64, f64, f64)>;

            fn envelope(&self) -> Self::Envelope {
                self.atom().envelope()
            }
        }

        #[cfg(feature = "rstar")]
        impl<'a> PointDistance for $type {
            fn distance_2(&self, other: &(f64, f64, f64)) -> f64 {
                self.atom().distance_2(other)
            }
        }

        impl<'a> Eq for $type {}
        impl<'a> PartialEq for $type {
            fn eq(&self, other: &Self) -> bool {
                // By definition the combination of serial number and alternative location should be
                // unique across the whole PDB, this does not account for the fact that there could
                // be multiple models, but that is impossible to check anyway without Model information.
                self.atom().serial_number() == other.atom().serial_number()
                    && self.conformer().alternative_location() == other.conformer().alternative_location()
            }
        })*
    };
}

impl_hierarchy!(
    AtomConformer<'a>,
    AtomConformerMut<'a>,
    AtomConformerResidue<'a>,
    AtomConformerResidueMut<'a>,
    AtomConformerResidueChain<'a>,
    AtomConformerResidueChainMut<'a>,
    AtomConformerResidueChainModel<'a>,
    AtomConformerResidueChainModelMut<'a>,
);

// ______ AtomConformer

impl<'a> AtomConformer<'a> {
    pub(crate) const fn new(atom: &'a Atom, conformer: &'a Conformer) -> AtomConformer<'a> {
        AtomConformer { atom, conformer }
    }
    pub(crate) const fn from_tuple(tuple: (&'a Atom, &'a Conformer)) -> AtomConformer<'a> {
        AtomConformer {
            atom: tuple.0,
            conformer: tuple.1,
        }
    }
    pub(crate) const fn extend(self, residue: &'a Residue) -> AtomConformerResidue<'a> {
        AtomConformerResidue {
            atom: self.atom,
            conformer: self.conformer,
            residue,
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformer<'a> {
    fn atom(&self) -> &Atom {
        self.atom
    }
    fn conformer(&self) -> &Conformer {
        self.conformer
    }
}

// ______ AtomConformerResidue

impl<'a> AtomConformerResidue<'a> {
    pub(crate) const fn new(
        atom: &'a Atom,
        conformer: &'a Conformer,
        residue: &'a Residue,
    ) -> AtomConformerResidue<'a> {
        AtomConformerResidue {
            atom,
            conformer,
            residue,
        }
    }
    pub(crate) const fn from_tuple(
        tuple: (&'a Atom, &'a Conformer, &'a Residue),
    ) -> AtomConformerResidue<'a> {
        AtomConformerResidue {
            atom: tuple.0,
            conformer: tuple.1,
            residue: tuple.2,
        }
    }
    pub(crate) const fn extend(self, chain: &'a Chain) -> AtomConformerResidueChain<'a> {
        AtomConformerResidueChain {
            atom: self.atom,
            conformer: self.conformer,
            residue: self.residue,
            chain,
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformerResidue<'a> {
    fn atom(&self) -> &Atom {
        self.atom
    }
    fn conformer(&self) -> &Conformer {
        self.conformer
    }
}

impl<'a> ContainsAtomConformerResidue for AtomConformerResidue<'a> {
    fn residue(&self) -> &Residue {
        self.residue
    }
}

// ______ AtomConformerResidueChain

impl<'a> AtomConformerResidueChain<'a> {
    pub(crate) const fn new(
        atom: &'a Atom,
        conformer: &'a Conformer,
        residue: &'a Residue,
        chain: &'a Chain,
    ) -> AtomConformerResidueChain<'a> {
        AtomConformerResidueChain {
            atom,
            conformer,
            residue,
            chain,
        }
    }
    pub(crate) const fn from_tuple(
        tuple: (&'a Atom, &'a Conformer, &'a Residue, &'a Chain),
    ) -> AtomConformerResidueChain<'a> {
        AtomConformerResidueChain {
            atom: tuple.0,
            conformer: tuple.1,
            residue: tuple.2,
            chain: tuple.3,
        }
    }
    pub(crate) const fn extend(self, model: &'a Model) -> AtomConformerResidueChainModel<'a> {
        AtomConformerResidueChainModel {
            atom: self.atom,
            conformer: self.conformer,
            residue: self.residue,
            chain: self.chain,
            model,
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformerResidueChain<'a> {
    fn atom(&self) -> &Atom {
        self.atom
    }
    fn conformer(&self) -> &Conformer {
        self.conformer
    }
}

impl<'a> ContainsAtomConformerResidue for AtomConformerResidueChain<'a> {
    fn residue(&self) -> &Residue {
        self.residue
    }
}

impl<'a> ContainsAtomConformerResidueChain for AtomConformerResidueChain<'a> {
    fn chain(&self) -> &Chain {
        self.chain
    }
}

// ______ AtomConformerResidueChainModel

impl<'a> AtomConformerResidueChainModel<'a> {
    pub(crate) const fn new(
        atom: &'a Atom,
        conformer: &'a Conformer,
        residue: &'a Residue,
        chain: &'a Chain,
        model: &'a Model,
    ) -> AtomConformerResidueChainModel<'a> {
        AtomConformerResidueChainModel {
            atom,
            conformer,
            residue,
            chain,
            model,
        }
    }
    pub(crate) const fn from_tuple(
        tuple: (&'a Atom, &'a Conformer, &'a Residue, &'a Chain, &'a Model),
    ) -> AtomConformerResidueChainModel<'a> {
        AtomConformerResidueChainModel {
            atom: tuple.0,
            conformer: tuple.1,
            residue: tuple.2,
            chain: tuple.3,
            model: tuple.4,
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformerResidueChainModel<'a> {
    fn atom(&self) -> &Atom {
        self.atom
    }
    fn conformer(&self) -> &Conformer {
        self.conformer
    }
}

impl<'a> ContainsAtomConformerResidue for AtomConformerResidueChainModel<'a> {
    fn residue(&self) -> &Residue {
        self.residue
    }
}

impl<'a> ContainsAtomConformerResidueChain for AtomConformerResidueChainModel<'a> {
    fn chain(&self) -> &Chain {
        self.chain
    }
}

impl<'a> ContainsAtomConformerResidueChainModel for AtomConformerResidueChainModel<'a> {
    fn model(&self) -> &Model {
        self.model
    }
}

// ______ AtomConformerMut

impl<'a> AtomConformerMut<'a> {
    pub(crate) const fn new(atom: *mut Atom, conformer: *mut Conformer) -> AtomConformerMut<'a> {
        AtomConformerMut {
            atom,
            conformer,
            phantom: PhantomData,
        }
    }
    pub(crate) const fn from_tuple(tuple: (*mut Atom, *mut Conformer)) -> AtomConformerMut<'a> {
        AtomConformerMut {
            atom: tuple.0,
            conformer: tuple.1,
            phantom: PhantomData,
        }
    }
    pub(crate) const fn extend(self, residue: *mut Residue) -> AtomConformerResidueMut<'a> {
        AtomConformerResidueMut {
            atom: self.atom,
            conformer: self.conformer,
            residue,
            phantom: PhantomData,
        }
    }
    /// Change this mutable hierarchy into an immutable hierarchy
    pub fn without_mut(self) -> AtomConformer<'a> {
        unsafe {
            AtomConformer {
                atom: self.atom.as_ref().unwrap(),
                conformer: self.conformer.as_ref().unwrap(),
            }
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformerMut<'a> {
    fn atom(&self) -> &Atom {
        unsafe { self.atom.as_ref().unwrap() }
    }
    fn conformer(&self) -> &Conformer {
        unsafe { self.conformer.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerMut for AtomConformerMut<'a> {
    fn atom_mut(&mut self) -> &mut Atom {
        unsafe { self.atom.as_mut().unwrap() }
    }
    fn conformer_mut(&mut self) -> &mut Conformer {
        unsafe { self.conformer.as_mut().unwrap() }
    }
}

// ______ AtomConformerResidueMut

impl<'a> AtomConformerResidueMut<'a> {
    pub(crate) const fn new(
        atom: *mut Atom,
        conformer: *mut Conformer,
        residue: *mut Residue,
    ) -> AtomConformerResidueMut<'a> {
        AtomConformerResidueMut {
            atom,
            conformer,
            residue,
            phantom: PhantomData,
        }
    }
    pub(crate) const fn from_tuple(
        tuple: (*mut Atom, *mut Conformer, *mut Residue),
    ) -> AtomConformerResidueMut<'a> {
        AtomConformerResidueMut {
            atom: tuple.0,
            conformer: tuple.1,
            residue: tuple.2,
            phantom: PhantomData,
        }
    }
    pub(crate) const fn extend(self, chain: *mut Chain) -> AtomConformerResidueChainMut<'a> {
        AtomConformerResidueChainMut {
            atom: self.atom,
            conformer: self.conformer,
            residue: self.residue,
            chain,
            phantom: PhantomData,
        }
    }
    /// Change this mutable hierarchy into an immutable hierarchy
    pub fn without_mut(self) -> AtomConformerResidue<'a> {
        unsafe {
            AtomConformerResidue {
                atom: self.atom.as_ref().unwrap(),
                conformer: self.conformer.as_ref().unwrap(),
                residue: self.residue.as_ref().unwrap(),
            }
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformerResidueMut<'a> {
    fn atom(&self) -> &Atom {
        unsafe { self.atom.as_ref().unwrap() }
    }
    fn conformer(&self) -> &Conformer {
        unsafe { self.conformer.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerMut for AtomConformerResidueMut<'a> {
    fn atom_mut(&mut self) -> &mut Atom {
        unsafe { self.atom.as_mut().unwrap() }
    }
    fn conformer_mut(&mut self) -> &mut Conformer {
        unsafe { self.conformer.as_mut().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidue for AtomConformerResidueMut<'a> {
    fn residue(&self) -> &Residue {
        unsafe { self.residue.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueMut for AtomConformerResidueMut<'a> {
    fn residue_mut(&mut self) -> &mut Residue {
        unsafe { self.residue.as_mut().unwrap() }
    }
}

// ______ AtomConformerResidueChainMut

impl<'a> AtomConformerResidueChainMut<'a> {
    pub(crate) const fn new(
        atom: *mut Atom,
        conformer: *mut Conformer,
        residue: *mut Residue,
        chain: *mut Chain,
    ) -> AtomConformerResidueChainMut<'a> {
        AtomConformerResidueChainMut {
            atom,
            conformer,
            residue,
            chain,
            phantom: PhantomData,
        }
    }
    pub(crate) const fn from_tuple(
        tuple: (*mut Atom, *mut Conformer, *mut Residue, *mut Chain),
    ) -> AtomConformerResidueChainMut<'a> {
        AtomConformerResidueChainMut {
            atom: tuple.0,
            conformer: tuple.1,
            residue: tuple.2,
            chain: tuple.3,
            phantom: PhantomData,
        }
    }
    pub(crate) const fn extend(self, model: *mut Model) -> AtomConformerResidueChainModelMut<'a> {
        AtomConformerResidueChainModelMut {
            atom: self.atom,
            conformer: self.conformer,
            residue: self.residue,
            chain: self.chain,
            model,
            phantom: PhantomData,
        }
    }
    /// Change this mutable hierarchy into an immutable hierarchy
    pub fn without_mut(self) -> AtomConformerResidueChain<'a> {
        unsafe {
            AtomConformerResidueChain {
                atom: self.atom.as_ref().unwrap(),
                conformer: self.conformer.as_ref().unwrap(),
                residue: self.residue.as_ref().unwrap(),
                chain: self.chain.as_ref().unwrap(),
            }
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformerResidueChainMut<'a> {
    fn atom(&self) -> &Atom {
        unsafe { self.atom.as_ref().unwrap() }
    }
    fn conformer(&self) -> &Conformer {
        unsafe { self.conformer.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerMut for AtomConformerResidueChainMut<'a> {
    fn atom_mut(&mut self) -> &mut Atom {
        unsafe { self.atom.as_mut().unwrap() }
    }
    fn conformer_mut(&mut self) -> &mut Conformer {
        unsafe { self.conformer.as_mut().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidue for AtomConformerResidueChainMut<'a> {
    fn residue(&self) -> &Residue {
        unsafe { self.residue.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueMut for AtomConformerResidueChainMut<'a> {
    fn residue_mut(&mut self) -> &mut Residue {
        unsafe { self.residue.as_mut().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueChain for AtomConformerResidueChainMut<'a> {
    fn chain(&self) -> &Chain {
        unsafe { self.chain.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueChainMut for AtomConformerResidueChainMut<'a> {
    fn chain_mut(&mut self) -> &mut Chain {
        unsafe { self.chain.as_mut().unwrap() }
    }
}

// ______ AtomConformerResidueChainModelMut

impl<'a> AtomConformerResidueChainModelMut<'a> {
    pub(crate) const fn new(
        atom: *mut Atom,
        conformer: *mut Conformer,
        residue: *mut Residue,
        chain: *mut Chain,
        model: *mut Model,
    ) -> AtomConformerResidueChainModelMut<'a> {
        AtomConformerResidueChainModelMut {
            atom,
            conformer,
            residue,
            chain,
            model,
            phantom: PhantomData,
        }
    }
    pub(crate) const fn from_tuple(
        tuple: (
            *mut Atom,
            *mut Conformer,
            *mut Residue,
            *mut Chain,
            *mut Model,
        ),
    ) -> AtomConformerResidueChainModelMut<'a> {
        AtomConformerResidueChainModelMut {
            atom: tuple.0,
            conformer: tuple.1,
            residue: tuple.2,
            chain: tuple.3,
            model: tuple.4,
            phantom: PhantomData,
        }
    }
    /// Change this mutable hierarchy into an immutable hierarchy
    pub fn without_mut(self) -> AtomConformerResidueChainModel<'a> {
        unsafe {
            AtomConformerResidueChainModel {
                atom: self.atom.as_ref().unwrap(),
                conformer: self.conformer.as_ref().unwrap(),
                residue: self.residue.as_ref().unwrap(),
                chain: self.chain.as_ref().unwrap(),
                model: self.model.as_ref().unwrap(),
            }
        }
    }
}

impl<'a> ContainsAtomConformer for AtomConformerResidueChainModelMut<'a> {
    fn atom(&self) -> &Atom {
        unsafe { self.atom.as_ref().unwrap() }
    }
    fn conformer(&self) -> &Conformer {
        unsafe { self.conformer.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerMut for AtomConformerResidueChainModelMut<'a> {
    fn atom_mut(&mut self) -> &mut Atom {
        unsafe { self.atom.as_mut().unwrap() }
    }
    fn conformer_mut(&mut self) -> &mut Conformer {
        unsafe { self.conformer.as_mut().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidue for AtomConformerResidueChainModelMut<'a> {
    fn residue(&self) -> &Residue {
        unsafe { self.residue.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueMut for AtomConformerResidueChainModelMut<'a> {
    fn residue_mut(&mut self) -> &mut Residue {
        unsafe { self.residue.as_mut().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueChain for AtomConformerResidueChainModelMut<'a> {
    fn chain(&self) -> &Chain {
        unsafe { self.chain.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueChainMut for AtomConformerResidueChainModelMut<'a> {
    fn chain_mut(&mut self) -> &mut Chain {
        unsafe { self.chain.as_mut().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueChainModel for AtomConformerResidueChainModelMut<'a> {
    fn model(&self) -> &Model {
        unsafe { self.model.as_ref().unwrap() }
    }
}

impl<'a> ContainsAtomConformerResidueChainModelMut for AtomConformerResidueChainModelMut<'a> {
    fn model_mut(&mut self) -> &mut Model {
        unsafe { self.model.as_mut().unwrap() }
    }
}