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
//! DockQ v2 interface quality assessment for protein-protein complexes.
//!
//! This module implements the DockQ scoring method for evaluating the quality
//! of modeled protein-protein interfaces. DockQ is the standard metric used
//! in CAPRI and CASP-multimer evaluations.
//!
//! # Feature Flag
//!
//! Enable this module in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! pdbrust = { version = "0.7", features = ["dockq"] }
//! ```
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use pdbrust::{parse_pdb_file, PdbStructure};
//!
//! let model = parse_pdb_file("model.pdb")?;
//! let native = parse_pdb_file("native.pdb")?;
//!
//! // Compute DockQ with automatic chain mapping
//! let result = model.dockq_to(&native)?;
//! println!("DockQ: {:.4}", result.total_dockq);
//!
//! for iface in &result.interfaces {
//!     println!("Interface {}-{}: DockQ={:.3} fnat={:.3} iRMSD={:.2} LRMSD={:.2}",
//!         iface.native_chains.0, iface.native_chains.1,
//!         iface.dockq, iface.fnat, iface.irmsd, iface.lrmsd);
//! }
//! ```
//!
//! # DockQ Score Components
//!
//! DockQ combines three metrics:
//!
//! - **fnat**: Fraction of native contacts preserved in the model
//! - **iRMSD**: Interface RMSD (backbone atoms at the interface)
//! - **LRMSD**: Ligand RMSD (backbone RMSD of the smaller chain after
//!   receptor alignment)
//!
//! The combined score: `DockQ = (fnat + 1/(1+(iRMSD/1.5)^2) + 1/(1+(LRMSD/8.5)^2)) / 3`
//!
//! # Quality Classification
//!
//! | Quality | DockQ Range |
//! |---------|-------------|
//! | Incorrect | < 0.23 |
//! | Acceptable | 0.23 - 0.49 |
//! | Medium | 0.49 - 0.80 |
//! | High | >= 0.80 |

mod chain_mapping;
mod contacts;
mod metrics;
pub mod sequence_align;

use std::collections::HashMap;

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

pub use chain_mapping::find_chain_mapping;
pub use contacts::ResidueKey;
pub use sequence_align::{AlignmentParams, SequenceAlignment, align_sequences, sequence_identity};

/// Options for DockQ calculation.
#[derive(Debug, Clone)]
pub struct DockQOptions {
    /// Distance threshold for fnat contact detection (default: 5.0 A).
    pub contact_threshold: f64,
    /// Distance threshold for identifying interface residues (default: 10.0 A).
    pub interface_threshold: f64,
    /// Atom selection for fnat contact detection (default: AllAtoms for heavy atoms).
    pub fnat_atom_selection: AtomSelection,
    /// Atom selection for iRMSD/LRMSD (default: Backbone).
    pub rmsd_atom_selection: AtomSelection,
    /// Strategy for establishing chain correspondence.
    pub chain_mapping: ChainMappingStrategy,
}

impl Default for DockQOptions {
    fn default() -> Self {
        Self {
            contact_threshold: 5.0,
            interface_threshold: 10.0,
            fnat_atom_selection: AtomSelection::AllAtoms,
            rmsd_atom_selection: AtomSelection::Backbone,
            chain_mapping: ChainMappingStrategy::Auto,
        }
    }
}

/// Strategy for establishing chain correspondence between model and native.
#[derive(Debug, Clone)]
pub enum ChainMappingStrategy {
    /// Automatic: use sequence alignment to determine chain mapping.
    Auto,
    /// Explicit mapping: vec of (model_chain, native_chain) pairs.
    Explicit(Vec<(String, String)>),
}

/// Result for the entire complex comparison.
#[derive(Debug, Clone)]
pub struct DockQResult {
    /// Per-interface results.
    pub interfaces: Vec<InterfaceResult>,
    /// Average DockQ over all native interfaces (equal weighting).
    pub total_dockq: f64,
    /// Final model-to-native chain mapping used.
    pub chain_mapping: Vec<(String, String)>,
    /// Number of interfaces evaluated.
    pub num_interfaces: usize,
}

/// Result for a single chain-pair interface.
#[derive(Debug, Clone)]
pub struct InterfaceResult {
    /// Chain pair in the native structure (receptor, ligand).
    pub native_chains: (String, String),
    /// Corresponding chain pair in the model.
    pub model_chains: (String, String),
    /// Fraction of native contacts preserved [0, 1].
    pub fnat: f64,
    /// Fraction of non-native contacts in the model.
    pub fnonnat: f64,
    /// Interface RMSD in Angstroms.
    pub irmsd: f64,
    /// Ligand RMSD in Angstroms.
    pub lrmsd: f64,
    /// DockQ score [0, 1].
    pub dockq: f64,
    /// Quality classification.
    pub quality: DockQQuality,
    /// F1 score for contacts.
    pub f1: f64,
    /// Number of contacts in native.
    pub num_native_contacts: usize,
    /// Number of contacts in model.
    pub num_model_contacts: usize,
    /// Number of atomic clashes (< 2.0 A).
    pub num_clashes: usize,
}

/// DockQ quality classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DockQQuality {
    /// DockQ < 0.23
    Incorrect,
    /// 0.23 <= DockQ < 0.49
    Acceptable,
    /// 0.49 <= DockQ < 0.80
    Medium,
    /// DockQ >= 0.80
    High,
}

impl DockQQuality {
    /// Classify a DockQ score.
    pub fn from_score(dockq: f64) -> Self {
        if dockq >= 0.80 {
            DockQQuality::High
        } else if dockq >= 0.49 {
            DockQQuality::Medium
        } else if dockq >= 0.23 {
            DockQQuality::Acceptable
        } else {
            DockQQuality::Incorrect
        }
    }
}

impl std::fmt::Display for DockQQuality {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DockQQuality::Incorrect => write!(f, "Incorrect"),
            DockQQuality::Acceptable => write!(f, "Acceptable"),
            DockQQuality::Medium => write!(f, "Medium"),
            DockQQuality::High => write!(f, "High"),
        }
    }
}

/// Compute DockQ for a specific interface (two chain pairs).
pub fn calculate_interface_dockq(
    model: &PdbStructure,
    native: &PdbStructure,
    model_chains: (&str, &str),
    native_chains: (&str, &str),
    options: &DockQOptions,
) -> Result<InterfaceResult, PdbError> {
    // Build residue mapping for both chain pairs
    let mut residue_mapping: HashMap<ResidueKey, ResidueKey> = HashMap::new();
    let map_a =
        chain_mapping::build_residue_mapping(model, native, model_chains.0, native_chains.0);
    let map_b =
        chain_mapping::build_residue_mapping(model, native, model_chains.1, native_chains.1);
    residue_mapping.extend(map_a);
    residue_mapping.extend(map_b);

    // Compute fnat
    let native_contacts = contacts::find_interface_contacts(
        native,
        native_chains.0,
        native_chains.1,
        options.contact_threshold,
    );
    let model_contacts = contacts::find_interface_contacts(
        model,
        model_chains.0,
        model_chains.1,
        options.contact_threshold,
    );

    if native_contacts.is_empty() {
        return Err(PdbError::NoInterfaceContacts(format!(
            "No contacts between chains {} and {} in native (threshold: {} A)",
            native_chains.0, native_chains.1, options.contact_threshold
        )));
    }

    let contact_result =
        contacts::compare_contacts(&native_contacts, &model_contacts, &residue_mapping);

    // Count clashes
    let num_clashes = contacts::count_clashes(model, model_chains.0, model_chains.1);

    // Compute iRMSD
    let irmsd = metrics::compute_irmsd(
        model,
        native,
        native_chains.0,
        native_chains.1,
        model_chains.0,
        model_chains.1,
        &residue_mapping,
        options.interface_threshold,
    )
    .unwrap_or(f64::INFINITY);

    // Compute LRMSD
    let lrmsd = metrics::compute_lrmsd(
        model,
        native,
        native_chains.0,
        native_chains.1,
        model_chains.0,
        model_chains.1,
    )
    .unwrap_or(f64::INFINITY);

    let dockq = metrics::compute_dockq(contact_result.fnat, irmsd, lrmsd);
    let quality = DockQQuality::from_score(dockq);

    Ok(InterfaceResult {
        native_chains: (native_chains.0.to_string(), native_chains.1.to_string()),
        model_chains: (model_chains.0.to_string(), model_chains.1.to_string()),
        fnat: contact_result.fnat,
        fnonnat: contact_result.fnonnat,
        irmsd,
        lrmsd,
        dockq,
        quality,
        f1: contact_result.f1,
        num_native_contacts: contact_result.num_native_contacts,
        num_model_contacts: contact_result.num_model_contacts,
        num_clashes,
    })
}

/// Compute DockQ for all interfaces in a complex.
fn compute_all_interfaces(
    model: &PdbStructure,
    native: &PdbStructure,
    mapping: &[(String, String)],
    options: &DockQOptions,
) -> Result<DockQResult, PdbError> {
    // Find all native chain pairs that have interface contacts
    let native_chain_ids: Vec<&str> = mapping.iter().map(|(_, n)| n.as_str()).collect();

    let mut interfaces = Vec::new();

    for i in 0..native_chain_ids.len() {
        for j in (i + 1)..native_chain_ids.len() {
            let nc_a = native_chain_ids[i];
            let nc_b = native_chain_ids[j];

            // Check if there are contacts at this interface
            let native_contacts =
                contacts::find_interface_contacts(native, nc_a, nc_b, options.contact_threshold);

            if native_contacts.is_empty() {
                continue;
            }

            // Find corresponding model chains
            let mc_a = mapping
                .iter()
                .find(|(_, n)| n == nc_a)
                .map(|(m, _)| m.as_str());
            let mc_b = mapping
                .iter()
                .find(|(_, n)| n == nc_b)
                .map(|(m, _)| m.as_str());

            if let (Some(mc_a), Some(mc_b)) = (mc_a, mc_b) {
                match calculate_interface_dockq(model, native, (mc_a, mc_b), (nc_a, nc_b), options)
                {
                    Ok(iface_result) => interfaces.push(iface_result),
                    Err(_) => continue, // Skip interfaces that can't be computed
                }
            }
        }
    }

    if interfaces.is_empty() {
        return Err(PdbError::NoInterfaceContacts(
            "No interfaces with contacts found between mapped chains".to_string(),
        ));
    }

    // Average DockQ over all native interfaces (equal weighting, matching DockQ v2)
    let total_dockq = interfaces.iter().map(|i| i.dockq).sum::<f64>() / interfaces.len() as f64;

    let num_interfaces = interfaces.len();

    Ok(DockQResult {
        interfaces,
        total_dockq,
        chain_mapping: mapping.to_vec(),
        num_interfaces,
    })
}

/// Extension methods for PdbStructure providing DockQ functionality.
impl PdbStructure {
    /// Compute DockQ between this (model) and a reference (native) complex.
    ///
    /// Automatically detects chain mapping using sequence alignment and
    /// evaluates all interfaces with contacts.
    ///
    /// # Arguments
    /// * `native` - The reference (native/crystal) structure
    ///
    /// # Returns
    /// [`DockQResult`] containing per-interface scores and overall DockQ
    ///
    /// # Errors
    /// - `NoChainMapping` if no chain correspondence can be found
    /// - `NoInterfaceContacts` if no interfaces with contacts are found
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let result = model.dockq_to(&native)?;
    /// println!("DockQ: {:.4} ({})", result.total_dockq,
    ///     result.interfaces[0].quality);
    /// ```
    pub fn dockq_to(&self, native: &PdbStructure) -> Result<DockQResult, PdbError> {
        self.dockq_to_with_options(native, DockQOptions::default())
    }

    /// Compute DockQ with custom options.
    ///
    /// # Arguments
    /// * `native` - The reference (native/crystal) structure
    /// * `options` - Custom options for thresholds, atom selection, and chain mapping
    ///
    /// # Returns
    /// [`DockQResult`] containing per-interface scores and overall DockQ
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use pdbrust::dockq::{DockQOptions, ChainMappingStrategy};
    ///
    /// let options = DockQOptions {
    ///     chain_mapping: ChainMappingStrategy::Explicit(vec![
    ///         ("A".to_string(), "A".to_string()),
    ///         ("B".to_string(), "B".to_string()),
    ///     ]),
    ///     ..Default::default()
    /// };
    ///
    /// let result = model.dockq_to_with_options(&native, options)?;
    /// ```
    pub fn dockq_to_with_options(
        &self,
        native: &PdbStructure,
        options: DockQOptions,
    ) -> Result<DockQResult, PdbError> {
        let mapping = match &options.chain_mapping {
            ChainMappingStrategy::Auto => find_chain_mapping(self, native)?,
            ChainMappingStrategy::Explicit(m) => m.clone(),
        };

        compute_all_interfaces(self, native, &mapping, &options)
    }
}

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

    #[allow(clippy::too_many_arguments)]
    fn create_atom(
        serial: i32,
        name: &str,
        residue_name: &str,
        chain_id: &str,
        residue_seq: i32,
        x: f64,
        y: f64,
        z: f64,
        element: &str,
    ) -> Atom {
        Atom {
            serial,
            name: name.to_string(),
            alt_loc: None,
            residue_name: residue_name.to_string(),
            chain_id: chain_id.to_string(),
            residue_seq,
            ins_code: None,
            is_hetatm: false,
            x,
            y,
            z,
            occupancy: 1.0,
            temp_factor: 20.0,
            element: element.to_string(),
        }
    }

    fn create_two_chain_complex() -> PdbStructure {
        let mut structure = PdbStructure::new();
        // Chain A: 4 residues along x-axis
        // Chain B: 4 residues along x-axis, offset in y by 4.0 A (within contact distance)
        let mut serial = 1;
        for i in 0..4 {
            let x = i as f64 * 3.8;
            let resname = match i {
                0 => "ALA",
                1 => "GLY",
                2 => "VAL",
                _ => "LEU",
            };
            // Chain A backbone
            structure.atoms.push(create_atom(
                serial,
                "N",
                resname,
                "A",
                i + 1,
                x - 0.5,
                0.0,
                0.0,
                "N",
            ));
            serial += 1;
            structure.atoms.push(create_atom(
                serial,
                "CA",
                resname,
                "A",
                i + 1,
                x,
                0.0,
                0.0,
                "C",
            ));
            serial += 1;
            structure.atoms.push(create_atom(
                serial,
                "C",
                resname,
                "A",
                i + 1,
                x + 0.5,
                0.0,
                0.0,
                "C",
            ));
            serial += 1;
            structure.atoms.push(create_atom(
                serial,
                "O",
                resname,
                "A",
                i + 1,
                x + 0.5,
                0.5,
                0.0,
                "O",
            ));
            serial += 1;
        }
        for i in 0..4 {
            let x = i as f64 * 3.8;
            let resname = match i {
                0 => "ILE",
                1 => "PHE",
                2 => "TRP",
                _ => "TYR",
            };
            // Chain B backbone, 4.0 A above chain A in y
            structure.atoms.push(create_atom(
                serial,
                "N",
                resname,
                "B",
                i + 1,
                x - 0.5,
                4.0,
                0.0,
                "N",
            ));
            serial += 1;
            structure.atoms.push(create_atom(
                serial,
                "CA",
                resname,
                "B",
                i + 1,
                x,
                4.0,
                0.0,
                "C",
            ));
            serial += 1;
            structure.atoms.push(create_atom(
                serial,
                "C",
                resname,
                "B",
                i + 1,
                x + 0.5,
                4.0,
                0.0,
                "C",
            ));
            serial += 1;
            structure.atoms.push(create_atom(
                serial,
                "O",
                resname,
                "B",
                i + 1,
                x + 0.5,
                4.5,
                0.0,
                "O",
            ));
            serial += 1;
        }
        structure
    }

    #[test]
    fn test_dockq_self_comparison() {
        let structure = create_two_chain_complex();
        let result = structure.dockq_to(&structure).unwrap();

        assert!(
            (result.total_dockq - 1.0).abs() < 1e-6,
            "Self-comparison DockQ should be 1.0, got {}",
            result.total_dockq
        );

        for iface in &result.interfaces {
            assert!(
                (iface.fnat - 1.0).abs() < 1e-6,
                "Self-comparison fnat should be 1.0"
            );
            assert!(iface.irmsd < 1e-6, "Self-comparison iRMSD should be ~0");
            assert!(iface.lrmsd < 1e-6, "Self-comparison LRMSD should be ~0");
            assert_eq!(iface.quality, DockQQuality::High);
        }
    }

    #[test]
    fn test_dockq_quality_classification() {
        assert_eq!(DockQQuality::from_score(0.0), DockQQuality::Incorrect);
        assert_eq!(DockQQuality::from_score(0.22), DockQQuality::Incorrect);
        assert_eq!(DockQQuality::from_score(0.23), DockQQuality::Acceptable);
        assert_eq!(DockQQuality::from_score(0.48), DockQQuality::Acceptable);
        assert_eq!(DockQQuality::from_score(0.49), DockQQuality::Medium);
        assert_eq!(DockQQuality::from_score(0.79), DockQQuality::Medium);
        assert_eq!(DockQQuality::from_score(0.80), DockQQuality::High);
        assert_eq!(DockQQuality::from_score(1.0), DockQQuality::High);
    }

    #[test]
    fn test_dockq_explicit_mapping() {
        let structure = create_two_chain_complex();
        let options = DockQOptions {
            chain_mapping: ChainMappingStrategy::Explicit(vec![
                ("A".to_string(), "A".to_string()),
                ("B".to_string(), "B".to_string()),
            ]),
            ..Default::default()
        };

        let result = structure
            .dockq_to_with_options(&structure, options)
            .unwrap();
        assert!((result.total_dockq - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_dockq_translated_model() {
        let native = create_two_chain_complex();
        let mut model = native.clone();

        // Translate all atoms - contacts should be preserved (fnat=1)
        // but iRMSD and LRMSD should be > 0
        for atom in &mut model.atoms {
            atom.x += 2.0;
            atom.y += 1.0;
        }

        let options = DockQOptions {
            chain_mapping: ChainMappingStrategy::Explicit(vec![
                ("A".to_string(), "A".to_string()),
                ("B".to_string(), "B".to_string()),
            ]),
            ..Default::default()
        };

        let result = model.dockq_to_with_options(&native, options).unwrap();

        // Contacts should be preserved since both chains moved together
        for iface in &result.interfaces {
            assert!(
                (iface.fnat - 1.0).abs() < 1e-6,
                "fnat should be 1.0 for rigid body translation, got {}",
                iface.fnat
            );
        }
    }
}