pdbrust 0.7.0

A comprehensive Rust library for parsing and analyzing Protein Data Bank (PDB) 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
//! Geometric analysis and structure superposition.
//!
//! This module provides RMSD calculation, LDDT scoring, and structure alignment
//! using the Kabsch algorithm. It requires the `geometry` feature flag.
//!
//! # Feature Flag
//!
//! Enable this module in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! pdbrust = { version = "0.7", features = ["geometry"] }
//! ```
//!
//! # Overview
//!
//! The main functionality includes:
//!
//! - **RMSD Calculation**: Compute root mean square deviation between structures
//! - **LDDT Calculation**: Compute Local Distance Difference Test (superposition-free)
//! - **Structure Alignment**: Optimal superposition using Kabsch algorithm
//! - **Per-Residue Analysis**: Identify flexible or poorly modeled regions
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use pdbrust::{parse_pdb_file, PdbStructure};
//! use pdbrust::geometry::{AtomSelection, LddtOptions};
//!
//! // Parse two structures
//! let model = parse_pdb_file("model.pdb")?;
//! let reference = parse_pdb_file("reference.pdb")?;
//!
//! // Calculate RMSD (without alignment)
//! let rmsd = model.rmsd_to(&reference)?;
//! println!("Direct RMSD: {:.2} Angstroms", rmsd);
//!
//! // Calculate LDDT (superposition-free)
//! let lddt = model.lddt_to(&reference)?;
//! println!("LDDT: {:.4}", lddt.score);
//!
//! // Align structures and get aligned RMSD
//! let (aligned, result) = model.align_to(&reference)?;
//! println!("Aligned RMSD: {:.4} Angstroms", result.rmsd);
//!
//! // Get per-residue RMSD for flexibility analysis
//! let per_res = model.per_residue_rmsd_to(&reference)?;
//! for r in &per_res {
//!     if r.rmsd > 2.0 {
//!         println!("Flexible: {}{} - {:.2} A", r.residue_id.0, r.residue_id.1, r.rmsd);
//!     }
//! }
//!
//! // Get per-residue LDDT for quality analysis
//! let per_res_lddt = model.per_residue_lddt_to(&reference)?;
//! for r in per_res_lddt.iter().filter(|r| r.score < 0.7) {
//!     println!("Low LDDT: {}{} = {:.2}", r.residue_id.0, r.residue_id.1, r.score);
//! }
//! ```
//!
//! # Atom Selection
//!
//! By default, RMSD, LDDT, and alignment use CA (alpha-carbon) atoms only.
//! You can customize this with [`AtomSelection`]:
//!
//! ```rust,ignore
//! use pdbrust::geometry::AtomSelection;
//!
//! // Use backbone atoms (N, CA, C, O)
//! let rmsd = model.rmsd_to_with_selection(&reference, AtomSelection::Backbone)?;
//!
//! // Use all atoms (requires exact correspondence)
//! let rmsd = model.rmsd_to_with_selection(&reference, AtomSelection::AllAtoms)?;
//!
//! // Use custom atom set
//! let selection = AtomSelection::Custom(vec!["CA".into(), "CB".into()]);
//! let rmsd = model.rmsd_to_with_selection(&reference, selection)?;
//! ```
//!
//! # LDDT vs RMSD
//!
//! LDDT (Local Distance Difference Test) complements RMSD:
//!
//! - **LDDT** is superposition-free and focuses on local structure quality
//! - **RMSD** requires alignment and measures global deviation
//! - LDDT is used in AlphaFold (pLDDT) and CASP evaluations
//! - For comparing predicted structures, LDDT is often preferred

mod lddt;
mod rmsd;
mod superposition;
mod transform;

// Re-export public types
pub use lddt::{LddtOptions, LddtResult, PerResidueLddt, calculate_lddt, per_residue_lddt};
pub use rmsd::{calculate_rmsd, calculate_rmsd_chain, rmsd_from_coords};
pub(crate) use superposition::superpose_coords;
pub use superposition::{
    AlignmentResult, PerResidueRmsd, align_structures, calculate_alignment, per_residue_rmsd,
};
pub use transform::{
    AtomSelection, apply_transform, apply_transform_to_coords, center_coords, compute_centroid,
    extract_coords_by_selection, extract_coords_with_residue_info, translate_structure,
};

use crate::core::PdbStructure;
use crate::error::PdbError;

/// Extension methods for PdbStructure providing RMSD and alignment functionality.
impl PdbStructure {
    /// Calculate RMSD to another structure using CA atoms.
    ///
    /// This is a convenience method that uses the default CA-only atom selection.
    /// For other atom selections, use [`rmsd_to_with_selection`](Self::rmsd_to_with_selection).
    ///
    /// **Note**: This calculates RMSD without alignment. For aligned RMSD, use
    /// [`align_to`](Self::align_to) which returns both the aligned structure and RMSD.
    ///
    /// # Arguments
    /// * `other` - The structure to compare against
    ///
    /// # Returns
    /// RMSD in Angstroms
    ///
    /// # Errors
    /// - `AtomCountMismatch` if structures have different numbers of CA atoms
    /// - `NoAtomsSelected` if either structure has no CA atoms
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let rmsd = structure1.rmsd_to(&structure2)?;
    /// println!("RMSD: {:.2} Angstroms", rmsd);
    /// ```
    pub fn rmsd_to(&self, other: &PdbStructure) -> Result<f64, PdbError> {
        calculate_rmsd(self, other, AtomSelection::CaOnly)
    }

    /// Calculate RMSD with a custom atom selection.
    ///
    /// # Arguments
    /// * `other` - The structure to compare against
    /// * `selection` - Atom selection criteria
    ///
    /// # Returns
    /// RMSD in Angstroms
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pdbrust::geometry::AtomSelection;
    ///
    /// // RMSD using backbone atoms
    /// let rmsd = structure1.rmsd_to_with_selection(&structure2, AtomSelection::Backbone)?;
    /// ```
    pub fn rmsd_to_with_selection(
        &self,
        other: &PdbStructure,
        selection: AtomSelection,
    ) -> Result<f64, PdbError> {
        calculate_rmsd(self, other, selection)
    }

    /// Align this structure onto a target structure.
    ///
    /// Uses the Kabsch algorithm to find the optimal rotation and translation
    /// that minimizes RMSD between the structures. Returns both the aligned
    /// structure and an [`AlignmentResult`] containing the RMSD and transformation.
    ///
    /// # Arguments
    /// * `target` - The reference structure to align onto
    ///
    /// # Returns
    /// Tuple of (aligned_structure, AlignmentResult)
    ///
    /// # Errors
    /// - `AtomCountMismatch` if structures have different numbers of CA atoms
    /// - `NoAtomsSelected` if either structure has no CA atoms
    /// - `InsufficientAtoms` if fewer than 3 CA atoms are present
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let (aligned, result) = mobile.align_to(&target)?;
    /// println!("RMSD after alignment: {:.4} Angstroms", result.rmsd);
    /// println!("Aligned {} atoms", result.num_atoms);
    ///
    /// // Save the aligned structure
    /// aligned.to_file("aligned.pdb")?;
    /// ```
    pub fn align_to(&self, target: &PdbStructure) -> Result<(Self, AlignmentResult), PdbError> {
        align_structures(self, target, AtomSelection::CaOnly)
    }

    /// Align with a custom atom selection.
    ///
    /// # Arguments
    /// * `target` - The reference structure to align onto
    /// * `selection` - Atom selection criteria for alignment
    ///
    /// # Returns
    /// Tuple of (aligned_structure, AlignmentResult)
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pdbrust::geometry::AtomSelection;
    ///
    /// // Align using backbone atoms
    /// let (aligned, result) = mobile.align_to_with_selection(
    ///     &target,
    ///     AtomSelection::Backbone
    /// )?;
    /// ```
    pub fn align_to_with_selection(
        &self,
        target: &PdbStructure,
        selection: AtomSelection,
    ) -> Result<(Self, AlignmentResult), PdbError> {
        align_structures(self, target, selection)
    }

    /// Get per-residue RMSD after alignment to target.
    ///
    /// This method first aligns the structures optimally, then computes
    /// RMSD for each residue individually. Useful for identifying flexible
    /// regions in protein structures.
    ///
    /// # Arguments
    /// * `target` - The reference structure
    ///
    /// # Returns
    /// Vector of [`PerResidueRmsd`] containing RMSD for each residue
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let per_res = mobile.per_residue_rmsd_to(&target)?;
    ///
    /// // Find flexible regions (RMSD > 2.0 Angstroms)
    /// for r in &per_res {
    ///     if r.rmsd > 2.0 {
    ///         println!("{}{} {}: {:.2} A",
    ///             r.residue_id.0,
    ///             r.residue_id.1,
    ///             r.residue_name,
    ///             r.rmsd
    ///         );
    ///     }
    /// }
    /// ```
    pub fn per_residue_rmsd_to(
        &self,
        target: &PdbStructure,
    ) -> Result<Vec<PerResidueRmsd>, PdbError> {
        per_residue_rmsd(self, target, AtomSelection::CaOnly)
    }

    /// Get per-residue RMSD with a custom atom selection.
    ///
    /// # Arguments
    /// * `target` - The reference structure
    /// * `selection` - Atom selection criteria
    ///
    /// # Returns
    /// Vector of [`PerResidueRmsd`] containing RMSD for each residue
    pub fn per_residue_rmsd_to_with_selection(
        &self,
        target: &PdbStructure,
        selection: AtomSelection,
    ) -> Result<Vec<PerResidueRmsd>, PdbError> {
        per_residue_rmsd(self, target, selection)
    }

    // ==================== LDDT Methods ====================

    /// Calculate LDDT (Local Distance Difference Test) to a reference structure.
    ///
    /// LDDT is a superposition-free metric that measures the fraction of
    /// inter-atomic distances that are preserved within specified thresholds.
    /// It ranges from 0.0 (poor) to 1.0 (perfect).
    ///
    /// This is the same metric used by AlphaFold (pLDDT) and CASP evaluations.
    ///
    /// # Arguments
    /// * `reference` - The reference structure (ground truth)
    ///
    /// # Returns
    /// [`LddtResult`] containing the global score and per-threshold scores
    ///
    /// # Errors
    /// - `AtomCountMismatch` if structures have different numbers of CA atoms
    /// - `NoAtomsSelected` if either structure has no CA atoms
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let result = model.lddt_to(&reference)?;
    /// println!("LDDT: {:.4}", result.score);
    /// println!("Evaluated {} distance pairs", result.num_pairs);
    /// ```
    pub fn lddt_to(&self, reference: &PdbStructure) -> Result<LddtResult, PdbError> {
        calculate_lddt(
            self,
            reference,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        )
    }

    /// Calculate LDDT with custom atom selection and options.
    ///
    /// # Arguments
    /// * `reference` - The reference structure (ground truth)
    /// * `selection` - Atom selection criteria (e.g., CA only, backbone)
    /// * `options` - LDDT options (inclusion radius, thresholds)
    ///
    /// # Returns
    /// [`LddtResult`] containing the global score and per-threshold scores
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pdbrust::geometry::{AtomSelection, LddtOptions};
    ///
    /// let options = LddtOptions::default()
    ///     .with_inclusion_radius(10.0)
    ///     .with_thresholds(vec![0.5, 1.0, 2.0, 4.0]);
    ///
    /// let result = model.lddt_to_with_options(
    ///     &reference,
    ///     AtomSelection::Backbone,
    ///     options
    /// )?;
    /// ```
    pub fn lddt_to_with_options(
        &self,
        reference: &PdbStructure,
        selection: AtomSelection,
        options: LddtOptions,
    ) -> Result<LddtResult, PdbError> {
        calculate_lddt(self, reference, selection, options)
    }

    /// Get per-residue LDDT scores.
    ///
    /// Returns LDDT scores for each residue individually, useful for
    /// identifying poorly modeled regions in predicted structures.
    ///
    /// # Arguments
    /// * `reference` - The reference structure (ground truth)
    ///
    /// # Returns
    /// Vector of [`PerResidueLddt`] containing LDDT for each residue
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let per_res = model.per_residue_lddt_to(&reference)?;
    ///
    /// // Find poorly modeled regions (LDDT < 0.7)
    /// for r in per_res.iter().filter(|r| r.score < 0.7) {
    ///     println!("{}{} {}: LDDT = {:.2}",
    ///         r.residue_id.0,
    ///         r.residue_id.1,
    ///         r.residue_name,
    ///         r.score
    ///     );
    /// }
    /// ```
    pub fn per_residue_lddt_to(
        &self,
        reference: &PdbStructure,
    ) -> Result<Vec<PerResidueLddt>, PdbError> {
        per_residue_lddt(
            self,
            reference,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        )
    }

    /// Get per-residue LDDT with custom atom selection and options.
    ///
    /// # Arguments
    /// * `reference` - The reference structure (ground truth)
    /// * `selection` - Atom selection criteria
    /// * `options` - LDDT options (inclusion radius, thresholds)
    ///
    /// # Returns
    /// Vector of [`PerResidueLddt`] containing LDDT for each residue
    pub fn per_residue_lddt_to_with_options(
        &self,
        reference: &PdbStructure,
        selection: AtomSelection,
        options: LddtOptions,
    ) -> Result<Vec<PerResidueLddt>, PdbError> {
        per_residue_lddt(self, reference, selection, options)
    }
}

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

    fn create_atom(x: f64, y: f64, z: f64, residue_seq: i32) -> Atom {
        Atom {
            serial: residue_seq,
            name: "CA".to_string(),
            alt_loc: None,
            residue_name: "ALA".to_string(),
            chain_id: "A".to_string(),
            residue_seq,
            x,
            y,
            z,
            occupancy: 1.0,
            temp_factor: 0.0,
            element: "C".to_string(),
            ins_code: None,
            is_hetatm: false,
        }
    }

    fn create_test_structure() -> PdbStructure {
        let mut structure = PdbStructure::new();
        structure.atoms = vec![
            create_atom(0.0, 0.0, 0.0, 1),
            create_atom(3.8, 0.0, 0.0, 2),
            create_atom(7.6, 0.0, 0.0, 3),
            create_atom(11.4, 0.0, 0.0, 4),
        ];
        structure
    }

    #[test]
    fn test_pdbstructure_rmsd_to_self() {
        let structure = create_test_structure();
        let rmsd = structure.rmsd_to(&structure).unwrap();
        assert!(rmsd < 1e-10);
    }

    #[test]
    fn test_pdbstructure_align_to_self() {
        let structure = create_test_structure();
        let (aligned, result) = structure.align_to(&structure).unwrap();

        assert!(result.rmsd < 1e-10);
        assert_eq!(result.num_atoms, 4);
        assert_eq!(aligned.atoms.len(), structure.atoms.len());
    }

    #[test]
    fn test_pdbstructure_align_translated() {
        let target = create_test_structure();
        let mut mobile = target.clone();
        for atom in &mut mobile.atoms {
            atom.x += 100.0;
            atom.y += 100.0;
            atom.z += 100.0;
        }

        let (aligned, result) = mobile.align_to(&target).unwrap();

        // RMSD should be ~0 after alignment
        assert!(result.rmsd < 1e-6);

        // Aligned atoms should match target
        for (a, t) in aligned.atoms.iter().zip(target.atoms.iter()) {
            assert!((a.x - t.x).abs() < 1e-6);
            assert!((a.y - t.y).abs() < 1e-6);
            assert!((a.z - t.z).abs() < 1e-6);
        }
    }

    #[test]
    fn test_pdbstructure_per_residue_rmsd() {
        let target = create_test_structure();
        let mut mobile = target.clone();
        for atom in &mut mobile.atoms {
            atom.x += 10.0;
        }

        let per_res = mobile.per_residue_rmsd_to(&target).unwrap();

        assert_eq!(per_res.len(), 4);
        for r in &per_res {
            // After alignment, all per-residue RMSDs should be ~0
            assert!(r.rmsd < 1e-6);
        }
    }

    // ==================== LDDT Tests ====================

    #[test]
    fn test_pdbstructure_lddt_to_self() {
        let structure = create_test_structure();
        let result = structure.lddt_to(&structure).unwrap();

        // Self-comparison should have perfect LDDT
        assert!(
            (result.score - 1.0).abs() < 1e-10,
            "Self-LDDT should be 1.0, got {}",
            result.score
        );
    }

    #[test]
    fn test_pdbstructure_lddt_translation_invariant() {
        let reference = create_test_structure();
        let mut model = reference.clone();

        // Translate the model
        for atom in &mut model.atoms {
            atom.x += 100.0;
            atom.y += 50.0;
            atom.z += 25.0;
        }

        let result = model.lddt_to(&reference).unwrap();

        // LDDT should be invariant to translation
        assert!(
            (result.score - 1.0).abs() < 1e-10,
            "LDDT should be translation invariant, got {}",
            result.score
        );
    }

    #[test]
    fn test_pdbstructure_lddt_rotation_invariant() {
        let reference = create_test_structure();
        let mut model = reference.clone();

        // Rotate 90 degrees around z-axis
        for atom in &mut model.atoms {
            let x = atom.x;
            let y = atom.y;
            atom.x = -y;
            atom.y = x;
        }

        let result = model.lddt_to(&reference).unwrap();

        // LDDT should be invariant to rotation
        assert!(
            (result.score - 1.0).abs() < 1e-10,
            "LDDT should be rotation invariant, got {}",
            result.score
        );
    }

    #[test]
    fn test_pdbstructure_lddt_perturbed() {
        let reference = create_test_structure();
        let mut model = reference.clone();

        // Perturb one atom
        model.atoms[1].y += 5.0;

        let result = model.lddt_to(&reference).unwrap();

        // Perturbed structure should have LDDT < 1.0
        assert!(
            result.score < 1.0,
            "Perturbed LDDT should be < 1.0, got {}",
            result.score
        );
        assert!(
            result.score > 0.0,
            "Perturbed LDDT should be > 0.0, got {}",
            result.score
        );
    }

    #[test]
    fn test_pdbstructure_per_residue_lddt() {
        let reference = create_test_structure();
        let model = reference.clone();

        let per_res = model.per_residue_lddt_to(&reference).unwrap();

        assert_eq!(per_res.len(), 4, "Should have 4 residues");
        for r in &per_res {
            assert!(
                (r.score - 1.0).abs() < 1e-10,
                "Self per-residue LDDT should be 1.0"
            );
        }
    }
}