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
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
//! LDDT (Local Distance Difference Test) calculation.
//!
//! LDDT is a superposition-free metric for comparing protein structures,
//! widely used in AlphaFold (pLDDT) and CASP evaluations. It measures the
//! fraction of inter-atomic distances that are preserved within specified
//! thresholds.
//!
//! # Algorithm
//!
//! 1. For each atom in the reference structure, find all atoms within the
//!    inclusion radius (default: 15Å)
//! 2. For each pair (i,j) in reference, compute distance d_ref
//! 3. Find the same pair in the model and compute d_model
//! 4. Check if |d_ref - d_model| < threshold for each threshold
//! 5. Score = average fraction of preserved distances across all thresholds
//!
//! The default thresholds are 0.5Å, 1.0Å, 2.0Å, and 4.0Å.
//!
//! # Key Properties
//!
//! - **Superposition-free**: LDDT is invariant to rotation and translation
//! - **Local**: Focuses on local structure quality, not global fold
//! - **Range**: 0.0 (poor) to 1.0 (perfect)
//!
//! # Example
//!
//! ```rust,ignore
//! use pdbrust::geometry::{calculate_lddt, LddtOptions, AtomSelection};
//!
//! let model = parse_pdb_file("model.pdb")?;
//! let reference = parse_pdb_file("reference.pdb")?;
//!
//! // Calculate LDDT with default options (CA atoms, 15Å radius)
//! let result = model.lddt_to(&reference)?;
//! println!("LDDT: {:.4}", result.score);
//!
//! // With custom options
//! let options = LddtOptions::default().with_inclusion_radius(10.0);
//! let result = model.lddt_to_with_options(&reference, AtomSelection::Backbone, options)?;
//! ```
//!
//! # References
//!
//! - Mariani V, et al. (2013) "lDDT: a local superposition-free score for
//!   comparing protein structures and models using distance difference tests"
//!   Bioinformatics 29(21):2722-2728

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

use super::transform::{AtomSelection, CoordWithResidue, extract_coords_with_residue_info};

/// Options for LDDT calculation.
///
/// # Example
///
/// ```rust
/// use pdbrust::geometry::LddtOptions;
///
/// // Default options
/// let options = LddtOptions::default();
/// assert_eq!(options.inclusion_radius, 15.0);
///
/// // Custom options
/// let options = LddtOptions::default()
///     .with_inclusion_radius(10.0)
///     .with_thresholds(vec![0.5, 1.0, 2.0, 4.0, 8.0]);
/// ```
#[derive(Debug, Clone)]
pub struct LddtOptions {
    /// Maximum distance to consider for local comparisons (default: 15.0 Å).
    ///
    /// Pairs of atoms with reference distance > inclusion_radius are ignored.
    pub inclusion_radius: f64,

    /// Distance difference thresholds (default: [0.5, 1.0, 2.0, 4.0] Å).
    ///
    /// For each threshold, count how many distance pairs are preserved
    /// (i.e., |d_ref - d_model| < threshold). The final score is the
    /// average across all thresholds.
    pub thresholds: Vec<f64>,
}

impl Default for LddtOptions {
    fn default() -> Self {
        Self {
            inclusion_radius: 15.0,
            thresholds: vec![0.5, 1.0, 2.0, 4.0],
        }
    }
}

impl LddtOptions {
    /// Create new options with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the inclusion radius.
    ///
    /// Only pairs of atoms with distance <= inclusion_radius in the
    /// reference structure are considered.
    pub fn with_inclusion_radius(mut self, radius: f64) -> Self {
        self.inclusion_radius = radius;
        self
    }

    /// Set custom thresholds.
    ///
    /// The LDDT score is the average fraction of preserved distances
    /// across all thresholds.
    pub fn with_thresholds(mut self, thresholds: Vec<f64>) -> Self {
        self.thresholds = thresholds;
        self
    }
}

/// Result of LDDT calculation.
#[derive(Debug, Clone)]
pub struct LddtResult {
    /// Global LDDT score (0.0 to 1.0).
    ///
    /// This is the average fraction of preserved distances across all
    /// thresholds, averaged over all residues.
    pub score: f64,

    /// Number of distance pairs evaluated.
    pub num_pairs: usize,

    /// Score for each threshold.
    ///
    /// Each value is the fraction of pairs preserved at that threshold.
    pub per_threshold_scores: Vec<f64>,

    /// Number of residues evaluated.
    pub num_residues: usize,
}

/// Per-residue LDDT information.
#[derive(Debug, Clone)]
pub struct PerResidueLddt {
    /// Residue identifier as (chain_id, residue_seq).
    pub residue_id: (String, i32),

    /// Residue name (e.g., "ALA", "GLY").
    pub residue_name: String,

    /// LDDT score for this residue (0.0 to 1.0).
    pub score: f64,

    /// Number of distance pairs involving this residue.
    pub num_pairs: usize,
}

/// Calculate LDDT between two structures.
///
/// This function computes the Local Distance Difference Test score between
/// a model structure and a reference structure. The score measures how well
/// local inter-atomic distances are preserved.
///
/// # Arguments
///
/// * `model` - The model structure to evaluate
/// * `reference` - The reference structure (ground truth)
/// * `selection` - Atom selection criteria (e.g., CA only, backbone)
/// * `options` - LDDT calculation options (thresholds, inclusion radius)
///
/// # Returns
///
/// `LddtResult` containing the global score, per-threshold scores, and counts.
///
/// # Errors
///
/// - `NoAtomsSelected` if no atoms match the selection
/// - `AtomCountMismatch` if structures have different numbers of selected atoms
///
/// # Example
///
/// ```rust,ignore
/// use pdbrust::geometry::{calculate_lddt, LddtOptions, AtomSelection};
///
/// let result = calculate_lddt(&model, &reference, AtomSelection::CaOnly, LddtOptions::default())?;
/// println!("LDDT: {:.4}", result.score);
/// ```
pub fn calculate_lddt(
    model: &PdbStructure,
    reference: &PdbStructure,
    selection: AtomSelection,
    options: LddtOptions,
) -> Result<LddtResult, PdbError> {
    // Extract coordinates with residue info
    let model_coords = extract_coords_with_residue_info(model, &selection, None);
    let ref_coords = extract_coords_with_residue_info(reference, &selection, None);

    if model_coords.is_empty() {
        return Err(PdbError::NoAtomsSelected(format!(
            "No atoms matching {:?} selection in model structure",
            selection
        )));
    }

    if ref_coords.is_empty() {
        return Err(PdbError::NoAtomsSelected(format!(
            "No atoms matching {:?} selection in reference structure",
            selection
        )));
    }

    if model_coords.len() != ref_coords.len() {
        return Err(PdbError::AtomCountMismatch {
            expected: ref_coords.len(),
            found: model_coords.len(),
        });
    }

    calculate_lddt_from_coords(&model_coords, &ref_coords, &options)
}

/// Calculate per-residue LDDT scores.
///
/// Returns LDDT scores for each residue individually, useful for
/// identifying poorly modeled regions.
///
/// # Arguments
///
/// * `model` - The model structure to evaluate
/// * `reference` - The reference structure (ground truth)
/// * `selection` - Atom selection criteria
/// * `options` - LDDT calculation options
///
/// # Returns
///
/// Vector of `PerResidueLddt` for each residue.
///
/// # Example
///
/// ```rust,ignore
/// let per_res = per_residue_lddt(&model, &reference, AtomSelection::CaOnly, LddtOptions::default())?;
/// for r in per_res.iter().filter(|r| r.score < 0.7) {
///     println!("Low LDDT: {}{} = {:.2}", r.residue_id.0, r.residue_id.1, r.score);
/// }
/// ```
pub fn per_residue_lddt(
    model: &PdbStructure,
    reference: &PdbStructure,
    selection: AtomSelection,
    options: LddtOptions,
) -> Result<Vec<PerResidueLddt>, PdbError> {
    // Extract coordinates with residue info
    let model_coords = extract_coords_with_residue_info(model, &selection, None);
    let ref_coords = extract_coords_with_residue_info(reference, &selection, None);

    if model_coords.is_empty() {
        return Err(PdbError::NoAtomsSelected(format!(
            "No atoms matching {:?} selection in model structure",
            selection
        )));
    }

    if ref_coords.is_empty() {
        return Err(PdbError::NoAtomsSelected(format!(
            "No atoms matching {:?} selection in reference structure",
            selection
        )));
    }

    if model_coords.len() != ref_coords.len() {
        return Err(PdbError::AtomCountMismatch {
            expected: ref_coords.len(),
            found: model_coords.len(),
        });
    }

    per_residue_lddt_from_coords(&model_coords, &ref_coords, &options)
}

/// Calculate Euclidean distance between two 3D points.
#[inline]
fn distance(p1: &(f64, f64, f64), p2: &(f64, f64, f64)) -> f64 {
    let dx = p1.0 - p2.0;
    let dy = p1.1 - p2.1;
    let dz = p1.2 - p2.2;
    (dx * dx + dy * dy + dz * dz).sqrt()
}

/// Calculate LDDT from coordinate arrays with residue information.
fn calculate_lddt_from_coords(
    model_coords: &[CoordWithResidue],
    ref_coords: &[CoordWithResidue],
    options: &LddtOptions,
) -> Result<LddtResult, PdbError> {
    let n = ref_coords.len();
    let inclusion_radius_sq = options.inclusion_radius * options.inclusion_radius;

    // Count preserved distances for each threshold
    let mut preserved_counts: Vec<usize> = vec![0; options.thresholds.len()];
    let mut total_pairs: usize = 0;

    // Count unique residues
    let mut unique_residues = std::collections::HashSet::new();

    // For each pair of atoms (i, j) where i < j
    for i in 0..n {
        let ref_i = &ref_coords[i];
        let model_i = &model_coords[i];

        unique_residues.insert((ref_i.0.0.clone(), ref_i.0.1));

        for j in (i + 1)..n {
            let ref_j = &ref_coords[j];
            let model_j = &model_coords[j];

            // Calculate reference distance
            let d_ref = distance(&ref_i.1, &ref_j.1);

            // Skip pairs outside inclusion radius
            if d_ref * d_ref > inclusion_radius_sq {
                continue;
            }

            // Calculate model distance
            let d_model = distance(&model_i.1, &model_j.1);

            // Check each threshold
            let diff = (d_ref - d_model).abs();
            for (k, threshold) in options.thresholds.iter().enumerate() {
                if diff < *threshold {
                    preserved_counts[k] += 1;
                }
            }

            total_pairs += 1;
        }
    }

    // Calculate per-threshold scores
    let per_threshold_scores: Vec<f64> = if total_pairs > 0 {
        preserved_counts
            .iter()
            .map(|&count| count as f64 / total_pairs as f64)
            .collect()
    } else {
        vec![1.0; options.thresholds.len()] // Perfect score if no pairs
    };

    // Global LDDT is average of per-threshold scores
    let score = if per_threshold_scores.is_empty() {
        1.0
    } else {
        per_threshold_scores.iter().sum::<f64>() / per_threshold_scores.len() as f64
    };

    Ok(LddtResult {
        score,
        num_pairs: total_pairs,
        per_threshold_scores,
        num_residues: unique_residues.len(),
    })
}

/// Calculate per-residue LDDT from coordinate arrays.
fn per_residue_lddt_from_coords(
    model_coords: &[CoordWithResidue],
    ref_coords: &[CoordWithResidue],
    options: &LddtOptions,
) -> Result<Vec<PerResidueLddt>, PdbError> {
    use std::collections::HashMap;

    let n = ref_coords.len();
    let inclusion_radius_sq = options.inclusion_radius * options.inclusion_radius;
    let num_thresholds = options.thresholds.len();

    // Track per-residue statistics: (chain_id, residue_seq) -> (preserved_counts, total_pairs, residue_name)
    let mut residue_stats: HashMap<(String, i32), (Vec<usize>, usize, String)> = HashMap::new();

    // Initialize residue entries
    for ref_coord in ref_coords {
        let key = (ref_coord.0.0.clone(), ref_coord.0.1);
        residue_stats
            .entry(key)
            .or_insert_with(|| (vec![0; num_thresholds], 0, ref_coord.0.2.clone()));
    }

    // For each pair of atoms (i, j)
    for i in 0..n {
        let ref_i = &ref_coords[i];
        let model_i = &model_coords[i];

        for j in (i + 1)..n {
            let ref_j = &ref_coords[j];
            let model_j = &model_coords[j];

            // Calculate reference distance
            let d_ref = distance(&ref_i.1, &ref_j.1);

            // Skip pairs outside inclusion radius
            if d_ref * d_ref > inclusion_radius_sq {
                continue;
            }

            // Calculate model distance
            let d_model = distance(&model_i.1, &model_j.1);
            let diff = (d_ref - d_model).abs();

            // Update statistics for residue i
            let key_i = (ref_i.0.0.clone(), ref_i.0.1);
            if let Some((preserved, total, _)) = residue_stats.get_mut(&key_i) {
                for (k, threshold) in options.thresholds.iter().enumerate() {
                    if diff < *threshold {
                        preserved[k] += 1;
                    }
                }
                *total += 1;
            }

            // Update statistics for residue j
            let key_j = (ref_j.0.0.clone(), ref_j.0.1);
            if let Some((preserved, total, _)) = residue_stats.get_mut(&key_j) {
                for (k, threshold) in options.thresholds.iter().enumerate() {
                    if diff < *threshold {
                        preserved[k] += 1;
                    }
                }
                *total += 1;
            }
        }
    }

    // Convert to PerResidueLddt
    let mut results: Vec<PerResidueLddt> = residue_stats
        .into_iter()
        .map(|(key, (preserved, total, residue_name))| {
            let score = if total > 0 {
                let per_threshold: Vec<f64> =
                    preserved.iter().map(|&p| p as f64 / total as f64).collect();
                per_threshold.iter().sum::<f64>() / num_thresholds as f64
            } else {
                1.0 // Perfect score if no pairs (isolated atom)
            };

            PerResidueLddt {
                residue_id: key,
                residue_name,
                score,
                num_pairs: total,
            }
        })
        .collect();

    // Sort by chain_id then residue_seq
    results.sort_by(|a, b| {
        a.residue_id
            .0
            .cmp(&b.residue_id.0)
            .then(a.residue_id.1.cmp(&b.residue_id.1))
    });

    Ok(results)
}

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

    fn create_atom(x: f64, y: f64, z: f64, residue_seq: i32, chain_id: &str) -> Atom {
        Atom {
            serial: residue_seq,
            name: "CA".to_string(),
            alt_loc: None,
            residue_name: "ALA".to_string(),
            chain_id: chain_id.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_linear_structure(spacing: f64) -> PdbStructure {
        let mut structure = PdbStructure::new();
        structure.atoms = vec![
            create_atom(0.0, 0.0, 0.0, 1, "A"),
            create_atom(spacing, 0.0, 0.0, 2, "A"),
            create_atom(spacing * 2.0, 0.0, 0.0, 3, "A"),
            create_atom(spacing * 3.0, 0.0, 0.0, 4, "A"),
            create_atom(spacing * 4.0, 0.0, 0.0, 5, "A"),
        ];
        structure
    }

    #[test]
    fn test_lddt_self_comparison() {
        let structure = create_linear_structure(3.8);
        let result = calculate_lddt(
            &structure,
            &structure,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        )
        .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_lddt_translation_invariance() {
        let reference = create_linear_structure(3.8);
        let mut model = create_linear_structure(3.8);

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

        let result = calculate_lddt(
            &model,
            &reference,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        )
        .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_lddt_rotation_invariance() {
        let reference = create_linear_structure(3.8);
        let mut model = create_linear_structure(3.8);

        // 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 = calculate_lddt(
            &model,
            &reference,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        )
        .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_lddt_perturbed_structure() {
        let reference = create_linear_structure(3.8);
        let mut model = create_linear_structure(3.8);

        // Perturb one atom significantly
        model.atoms[2].y += 5.0; // Move middle atom by 5 Angstroms

        let result = calculate_lddt(
            &model,
            &reference,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        )
        .unwrap();

        // LDDT should be less than 1.0 for perturbed structure
        assert!(
            result.score < 1.0,
            "LDDT should be < 1.0 for perturbed structure, got {}",
            result.score
        );
        assert!(
            result.score > 0.0,
            "LDDT should be > 0.0 for perturbed structure"
        );
    }

    #[test]
    fn test_lddt_custom_options() {
        let reference = create_linear_structure(3.8);
        let mut model = create_linear_structure(3.8);

        // Larger perturbation that will fail strict thresholds but pass lenient ones
        model.atoms[2].y += 1.5; // 1.5 Angstrom perturbation

        let options_lenient = LddtOptions::default().with_thresholds(vec![2.0, 4.0]);
        let options_strict = LddtOptions::default().with_thresholds(vec![0.5, 1.0]);

        let result_lenient =
            calculate_lddt(&model, &reference, AtomSelection::CaOnly, options_lenient).unwrap();
        let result_strict =
            calculate_lddt(&model, &reference, AtomSelection::CaOnly, options_strict).unwrap();

        // Stricter thresholds should give lower or equal score
        assert!(
            result_strict.score <= result_lenient.score,
            "Stricter thresholds should give lower or equal LDDT: {} vs {}",
            result_strict.score,
            result_lenient.score
        );
    }

    #[test]
    fn test_lddt_inclusion_radius() {
        let reference = create_linear_structure(10.0); // Larger spacing
        let mut model = create_linear_structure(10.0);

        // Perturb distant atom
        model.atoms[4].y += 2.0;

        let options_small_radius = LddtOptions::default().with_inclusion_radius(5.0);
        let options_large_radius = LddtOptions::default().with_inclusion_radius(50.0);

        let result_small = calculate_lddt(
            &model,
            &reference,
            AtomSelection::CaOnly,
            options_small_radius,
        )
        .unwrap();
        let result_large = calculate_lddt(
            &model,
            &reference,
            AtomSelection::CaOnly,
            options_large_radius,
        )
        .unwrap();

        // Smaller radius should have fewer pairs and potentially different score
        assert!(result_small.num_pairs <= result_large.num_pairs);
    }

    #[test]
    fn test_per_residue_lddt() {
        let reference = create_linear_structure(3.8);
        let mut model = create_linear_structure(3.8);

        // Perturb middle residue
        model.atoms[2].y += 5.0;

        let per_res = per_residue_lddt(
            &model,
            &reference,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        )
        .unwrap();

        assert_eq!(per_res.len(), 5, "Should have 5 residues");

        // Find the perturbed residue (residue 3)
        let perturbed = per_res.iter().find(|r| r.residue_id.1 == 3).unwrap();

        // The perturbed residue should have lower LDDT
        let others: Vec<_> = per_res.iter().filter(|r| r.residue_id.1 != 3).collect();
        let avg_others = others.iter().map(|r| r.score).sum::<f64>() / others.len() as f64;

        assert!(
            perturbed.score < avg_others,
            "Perturbed residue should have lower LDDT: {} vs {}",
            perturbed.score,
            avg_others
        );
    }

    #[test]
    fn test_lddt_empty_structure() {
        let structure = PdbStructure::new();

        let result = calculate_lddt(
            &structure,
            &structure,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        );

        assert!(matches!(result, Err(PdbError::NoAtomsSelected(_))));
    }

    #[test]
    fn test_lddt_mismatched_structures() {
        let structure1 = create_linear_structure(3.8);
        let mut structure2 = create_linear_structure(3.8);
        structure2.atoms.pop(); // Remove one atom

        let result = calculate_lddt(
            &structure1,
            &structure2,
            AtomSelection::CaOnly,
            LddtOptions::default(),
        );

        assert!(matches!(result, Err(PdbError::AtomCountMismatch { .. })));
    }

    #[test]
    fn test_lddt_options_builder() {
        let options = LddtOptions::new()
            .with_inclusion_radius(10.0)
            .with_thresholds(vec![0.25, 0.5, 1.0]);

        assert_eq!(options.inclusion_radius, 10.0);
        assert_eq!(options.thresholds, vec![0.25, 0.5, 1.0]);
    }

    #[test]
    fn test_distance_function() {
        let p1 = (0.0, 0.0, 0.0);
        let p2 = (3.0, 4.0, 0.0);
        let d = distance(&p1, &p2);
        assert!((d - 5.0).abs() < 1e-10, "Distance should be 5.0, got {}", d);
    }
}