Skip to content

Commit fd86ce6

Browse files
authored
refactor to handle any order of chains (#28)
1 parent ea53978 commit fd86ce6

File tree

2 files changed

+1018
-5
lines changed

2 files changed

+1018
-5
lines changed

src/main.rs

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn gen_tbl(input_file: &str) {
184184
///
185185
/// # Returns
186186
///
187-
/// A `Result<(), Box<dyn Error>>` which is Ok(()) if the function completes successfully, or an Error if something goes wrong.
187+
/// A `Result<String, Box<dyn Error>>` which is Ok(String) if the function completes successfully, or an Error if something goes wrong.
188188
///
189189
/// # Functionality
190190
///
@@ -214,7 +214,7 @@ fn gen_tbl(input_file: &str) {
214214
/// - `structure::get_true_interface` and `structure::get_chains_in_contact` for interface analysis.
215215
/// - `Interactor` struct for representing protein chains and their interactions.
216216
/// - `Air` struct for generating the AIR table.
217-
fn true_interface(input_file: &str, cutoff: &f64) -> Result<(), Box<dyn Error>> {
217+
fn true_interface(input_file: &str, cutoff: &f64) -> Result<String, Box<dyn Error>> {
218218
// Read PDB file
219219
let pdb = match structure::load_pdb(input_file) {
220220
Ok(pdb) => pdb,
@@ -230,9 +230,17 @@ fn true_interface(input_file: &str, cutoff: &f64) -> Result<(), Box<dyn Error>>
230230
let mut true_interface: Vec<_> = true_interface.iter().collect();
231231
true_interface.sort_by(|a, b| a.0.cmp(b.0));
232232

233+
// NOTE: Here the IDs of the interactors are their position in the PDB file; this is so that
234+
// we can handle any order of chains.
233235
let mut interactors: Vec<Interactor> = Vec::new();
234-
for (index, (chain_id, residues)) in true_interface.iter().enumerate() {
235-
let mut interactor = Interactor::new(index as u16);
236+
for (chain_id, residues) in true_interface.iter() {
237+
// Get what is the position of this chain in the PDB, this will be its ID
238+
let target_id = pdb
239+
.chains()
240+
.position(|chain| chain.id() == *chain_id)
241+
.unwrap();
242+
243+
let mut interactor = Interactor::new(target_id as u16);
236244
interactor.set_chain(chain_id);
237245
interactor.set_active(residues.iter().map(|&residue| residue as i16).collect());
238246

@@ -257,7 +265,7 @@ fn true_interface(input_file: &str, cutoff: &f64) -> Result<(), Box<dyn Error>>
257265

258266
println!("{}", tbl);
259267

260-
Ok(())
268+
Ok(tbl)
261269
}
262270

263271
/// Generates distance restraints between non-contiguous bodies in a protein structure.
@@ -543,3 +551,84 @@ fn generate_z_restraints(
543551

544552
Ok(())
545553
}
554+
555+
#[cfg(test)]
556+
mod tests {
557+
558+
use super::*;
559+
560+
#[test]
561+
fn test_true_interface() {
562+
let expected_tbl = r#"assign ( resid 933 and segid A )
563+
(
564+
( resid 46 and segid B )
565+
or
566+
( resid 47 and segid B )
567+
) 2.0 2.0 0.0
568+
569+
assign ( resid 950 and segid A )
570+
(
571+
( resid 46 and segid B )
572+
or
573+
( resid 47 and segid B )
574+
) 2.0 2.0 0.0
575+
576+
assign ( resid 46 and segid B )
577+
(
578+
( resid 933 and segid A )
579+
or
580+
( resid 950 and segid A )
581+
) 2.0 2.0 0.0
582+
583+
assign ( resid 47 and segid B )
584+
(
585+
( resid 933 and segid A )
586+
or
587+
( resid 950 and segid A )
588+
) 2.0 2.0 0.0
589+
590+
"#;
591+
592+
match true_interface("tests/data/complex.pdb", &3.0) {
593+
Ok(tbl) => assert_eq!(tbl, expected_tbl),
594+
Err(_e) => (),
595+
};
596+
}
597+
#[test]
598+
fn test_true_interface_ba() {
599+
let expected_tbl = r#"assign ( resid 933 and segid A )
600+
(
601+
( resid 46 and segid B )
602+
or
603+
( resid 47 and segid B )
604+
) 2.0 2.0 0.0
605+
606+
assign ( resid 950 and segid A )
607+
(
608+
( resid 46 and segid B )
609+
or
610+
( resid 47 and segid B )
611+
) 2.0 2.0 0.0
612+
613+
assign ( resid 46 and segid B )
614+
(
615+
( resid 933 and segid A )
616+
or
617+
( resid 950 and segid A )
618+
) 2.0 2.0 0.0
619+
620+
assign ( resid 47 and segid B )
621+
(
622+
( resid 933 and segid A )
623+
or
624+
( resid 950 and segid A )
625+
) 2.0 2.0 0.0
626+
627+
"#;
628+
629+
match true_interface("tests/data/complex_BA.pdb", &3.0) {
630+
Ok(tbl) => assert_eq!(tbl, expected_tbl),
631+
Err(_e) => (),
632+
};
633+
}
634+
}

0 commit comments

Comments
 (0)