Commit 28b22a9b authored by NOEL Philippe's avatar NOEL Philippe

Solve conflict in tools.rs

parents 8f248f06 5cf4f49b
Pipeline #75844 passed with stages
in 2 minutes and 52 seconds
[package] [package]
name = "pdbparser" name = "pdbparser"
version = "1.0.0" version = "1.0.0"
authors = ["NOEL Philippe <philippe.noel@inria.fr>"] authors = ["NOEL Philippe <philippe.noel@inria.fr>", "BRESSO Emmanuel <emmanuel.bresso@loria.fr>"]
edition = "2018" edition = "2018"
include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md"] include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md"]
......
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -352,4 +352,39 @@ impl Structure { ...@@ -352,4 +352,39 @@ impl Structure {
} }
} }
} }
/// Used to remove Hydrogens in the proteins
///
///
/// # Examples
/// ```
/// use pdbparser;
///
/// let my_struct = pdbparser::read_pdb("tests/tests_file/f2.pdb", "f2");
/// let ss_h = my_struct.remove_h().unwrap();
/// assert_eq!(541, ss_h.get_atom_number());
/// ```
pub fn remove_h(&self) -> Option<Structure> {
let mut new_struct = Structure::new(self.name.clone());
for chain in &self.chains {
let c_chain = chain.name;
for residue in &chain.lst_res {
let c_res = residue.res_num;
for atom in &residue.lst_atom {
if !(atom.name().starts_with("H")) {
new_struct.update_structure(
c_chain,
residue.name.clone(),
c_res,
atom.name.clone(),
atom.number,
atom.coord,
);
}
}
}
}
new_struct.refine_atom_numbering();
Some(new_struct)
}
} }
use std::collections::HashMap; use std::collections::HashMap;
use std::env;
use std::fs;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::BufRead;
use std::process;
use super::structure::Structure; use super::structure::Structure;
use super::atom::Atom;
/// Convert the all amino acid to a FASTA sequence (1 residue as 1 char) /// Convert the all amino acid to a FASTA sequence (1 residue as 1 char)
/// Consult the corresponding table to have the code 1 letter <-> 3 letters /// Consult the corresponding table to have the code 1 letter <-> 3 letters
...@@ -56,3 +63,98 @@ pub fn fasta_seq(my_struct: &Structure) -> String { ...@@ -56,3 +63,98 @@ pub fn fasta_seq(my_struct: &Structure) -> String {
} }
fasta fasta
} }
// extract atom mass from Charmm parameter files.
fn atom_mass() -> HashMap<String, f32> {
let mut ref_masses = HashMap::new();
// If CHARMM_FOLDER enviroment variable is defined, then look for .parm files in this folder. Else use the ./datasets folder*.
let key = "CHARMM_FOLDER";
let charmm_folder = match env::var(key) {
Ok(charmm_folder) => charmm_folder,
_ => "./datasets".to_string(),
};
let paths = match fs::read_dir(charmm_folder) {
Ok(paths) => paths,
Err(_e) => {
eprintln!("cannot find your charmm folder");
process::exit(1);
}
};
for path in paths {
let test = path.unwrap().path();
if test.is_file() && test.extension().unwrap().to_str() == Some("prm") {
let charmmf = match File::open(test) {
Ok(charmmf) => charmmf,
Err(e) => {
eprintln!("Error while reading {}", e);
process::exit(1);
}
};
let reader = BufReader::new(charmmf);
for line in reader.lines() {
// MASS -1 H 1.00800 ! polar H
let l = line.unwrap();
if l.starts_with("MASS") {
let split: Vec<&str> = l.split_ascii_whitespace().collect();
let val = match split[3].parse::<f32>() {
Ok(val) => val,
Err(e) => {
eprintln!("Cannot cast {} into f32", e);
process::exit(1);
}
};
ref_masses.insert(split[2].to_string(), val);
}
}
}
}
return ref_masses;
}
/// Calculate the center of mass of a selection of atoms
///
///
/// Atom masses are extracted from CHARMM Force Field .prm files.
/// If CHARMM_FOLDER environement variable is set, .prm files in this folder are used. Else it will be files in datasets folder.
///
/// # Example
/// ```
/// use pdbparser;
///
/// let my_prot = pdbparser::read_pdb("tests/tests_file/5jpq.pdb", "5jqp");
///
/// let atom_number = my_prot.get_atom_number() as usize;
/// let mut atoms: Vec<pdbparser::Atom> = Vec::with_capacity(atom_number);
/// for chain in my_prot.chains {
/// for residue in chain.lst_res {
/// for atom in residue.lst_atom {
/// atoms.push(atom);
/// }
/// }
/// }
/// assert_eq!([237.98595, 241.93814, 231.15921], pdbparser::center_of_mass(atoms))
/// ```
pub fn center_of_mass(atom_list: Vec<Atom>) -> [f32; 3] {
let mut coord: [f32;3] = [0.0, 0.0, 0.0];
let mut mass_tot: f32 = 0.0;
let ref_masses = atom_mass();
for atom in atom_list {
let mass = match ref_masses.get(&atom.name) {
Some(mass) => *mass,
None => 0.0
};
mass_tot += mass;
coord[0] += mass * atom.coord[0];
coord[1] += mass * atom.coord[1];
coord[2] += mass * atom.coord[2];
}
[coord[0] / mass_tot, coord[1] / mass_tot, coord[2] / mass_tot]
}
...@@ -49,3 +49,42 @@ fn f2_res() { ...@@ -49,3 +49,42 @@ fn f2_res() {
.unwrap(); .unwrap();
assert_eq!("HSD", res.name()); assert_eq!("HSD", res.name());
} }
#[test]
fn mass_center_f2() {
let my_struct = pdbparser::read_pdb("tests/tests_file/f2.pdb", "f2");
let mut atoms: Vec<pdbparser::Atom> = Vec::new();
for chain in my_struct.chains {
for residue in chain.lst_res {
for atom in residue.lst_atom {
atoms.push(atom);
}
}
}
assert_eq!([0.011766517, 20.1037, -0.007502083], pdbparser::center_of_mass(atoms))
}
#[test]
fn mass_center_5jpq() {
let my_struct = pdbparser::read_pdb("tests/tests_file/5jpq.pdb", "5jpq");
let mut atoms: Vec<pdbparser::Atom> = Vec::new();
for chain in my_struct.chains {
for residue in chain.lst_res {
for atom in residue.lst_atom {
atoms.push(atom);
}
}
}
assert_eq!([237.98595, 241.93814, 231.15921], pdbparser::center_of_mass(atoms))
}
#[test]
fn remove_hydrogens_f2(){
let my_struct = pdbparser::read_pdb("tests/tests_file/f2.pdb", "f2");
let ss_h = my_struct.remove_h().unwrap();
assert_eq!(541, ss_h.get_atom_number());
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment