|
| 1 | +use std::error::Error; |
| 2 | + |
| 3 | +use crate::*; |
| 4 | + |
| 5 | +/// Generates distance restraints between non-contiguous bodies in a protein structure. |
| 6 | +/// |
| 7 | +/// This function analyzes a PDB file to identify separate bodies within the protein structure, |
| 8 | +/// calculates gaps between these bodies, and generates Ambiguous Interaction Restraints (AIRs) |
| 9 | +/// to maintain the relative positions of these bodies. |
| 10 | +/// |
| 11 | +/// # Arguments |
| 12 | +/// |
| 13 | +/// * `input_file` - A string slice that holds the path to the input PDB file. |
| 14 | +/// |
| 15 | +/// # Returns |
| 16 | +/// |
| 17 | +/// A `Result<(), Box<dyn Error>>` which is Ok(()) if the function completes successfully, |
| 18 | +/// or an Error if something goes wrong. |
| 19 | +/// |
| 20 | +/// # Functionality |
| 21 | +/// |
| 22 | +/// 1. Opens and parses the PDB file using a loose strictness level. |
| 23 | +/// 2. Identifies separate bodies within the protein structure. |
| 24 | +/// 3. Calculates gaps between these bodies. |
| 25 | +/// 4. For each gap: |
| 26 | +/// a. Creates two interactors, one for each side of the gap. |
| 27 | +/// b. Sets up the interactors with appropriate chain, residue, and atom information. |
| 28 | +/// c. Configures distance restraints based on the gap distance. |
| 29 | +/// 5. Generates AIRs using the created interactors. |
| 30 | +/// 6. Prints the generated AIR table to stdout. |
| 31 | +/// |
| 32 | +pub fn restraint_bodies(input_file: &str, pml: &Option<String>) -> Result<String, Box<dyn Error>> { |
| 33 | + // Read PDB file |
| 34 | + let pdb = match load_pdb(input_file) { |
| 35 | + Ok(pdb) => pdb, |
| 36 | + Err(e) => { |
| 37 | + panic!("Error opening PDB file: {:?}", e); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + // Find in-contiguous chains |
| 42 | + let bodies = find_bodies(&pdb); |
| 43 | + let mut gaps = create_iter_body_gaps(&bodies); |
| 44 | + |
| 45 | + // Find same-chain ligands |
| 46 | + let ligand_gaps = gaps_around_ligand(&pdb); |
| 47 | + |
| 48 | + // NOTE: One restraint per atom of the ligand might be too much, apply some filtering |
| 49 | + // - no duplicated atoms in the protein should be used |
| 50 | + // - select every-other pair |
| 51 | + let filtered_gaps = filter_unique_by_atom_j(ligand_gaps); |
| 52 | + let every_other_gaps: Vec<Gap> = filtered_gaps |
| 53 | + .into_iter() |
| 54 | + .enumerate() |
| 55 | + .filter_map(|(idx, g)| { |
| 56 | + if idx % 2 == 0 { |
| 57 | + // select even |
| 58 | + Some(g) |
| 59 | + } else { |
| 60 | + None |
| 61 | + } |
| 62 | + }) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + gaps.extend(every_other_gaps); |
| 66 | + |
| 67 | + // Create the interactors |
| 68 | + let mut interactors: Vec<Interactor> = Vec::new(); |
| 69 | + let mut counter = 0; |
| 70 | + gaps.iter().for_each(|g| { |
| 71 | + // let (chain, res_i, res_j) = g; |
| 72 | + let mut interactor_i = Interactor::new(counter); |
| 73 | + counter += 1; |
| 74 | + let mut interactor_j = Interactor::new(counter); |
| 75 | + interactor_j.add_target(counter - 1); |
| 76 | + interactor_i.add_target(counter); |
| 77 | + counter += 1; |
| 78 | + |
| 79 | + interactor_i.set_chain(g.chain.as_str()); |
| 80 | + interactor_i.set_active(vec![g.res_i as i16]); |
| 81 | + interactor_i.set_active_atoms(vec![g.atom_i.clone()]); |
| 82 | + interactor_i.set_target_distance(g.distance); |
| 83 | + interactor_i.set_lower_margin(0.0); |
| 84 | + interactor_i.set_upper_margin(0.0); |
| 85 | + |
| 86 | + interactor_j.set_chain(g.chain.as_str()); |
| 87 | + interactor_j.set_passive(vec![g.res_j as i16]); |
| 88 | + interactor_j.set_passive_atoms(vec![g.atom_j.clone()]); |
| 89 | + |
| 90 | + interactors.push(interactor_i); |
| 91 | + interactors.push(interactor_j); |
| 92 | + }); |
| 93 | + |
| 94 | + let air = Air::new(interactors); |
| 95 | + let tbl = air.gen_tbl().unwrap(); |
| 96 | + |
| 97 | + println!("{}", tbl); |
| 98 | + |
| 99 | + if let Some(output_f) = pml { |
| 100 | + air.gen_pml(output_f) |
| 101 | + }; |
| 102 | + Ok(tbl) |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + |
| 108 | + use super::*; |
| 109 | + #[test] |
| 110 | + fn test_restraint_bodies() { |
| 111 | + let expected_tbl = r"assign ( resid 1 and segid A and name CA ) ( resid 4 and segid A and name CA ) 10.2 0.0 0.0 |
| 112 | +
|
| 113 | +assign ( resid 2 and segid A and name CA ) ( resid 8 and segid A and name CA ) 14.2 0.0 0.0 |
| 114 | +
|
| 115 | +"; |
| 116 | + |
| 117 | + let opt: Option<String> = None; |
| 118 | + match restraint_bodies("tests/data/gaps.pdb", &opt) { |
| 119 | + Ok(tbl) => assert_eq!(tbl, expected_tbl), |
| 120 | + Err(_e) => (), |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments