Commit 3843ffa4 authored by NOEL Philippe's avatar NOEL Philippe

commit du pc portable

parent 465dc74f
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'pdbparser'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=pdbparser"
],
"filter": {
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'pdbparser'",
"cargo": {
"args": [
"build",
"--bin=pdbparser",
"--package=pdbparser"
],
"filter": {
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'pdbparser'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=pdbparser",
"--package=pdbparser"
],
"filter": {
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ name = "pdbparser"
version = "0.1.0"
authors = ["NOEL Philippe <philippe.noel@inria.fr>"]
edition = "2018"
include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md"]
[[bin]]
name = "pdbparser"
......
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::process;
mod pdb;
use self::pdb::protein::Protein;
/// Parse the string to return a f32. The `trim` is used to remove
/// /n and spaces.
///
/// # Errors
/// Will return 0.0 if the String cannot be convert and print the error
///
fn parse_float(s: &String) -> f32 {
match s.trim().parse::<f32>() {
Ok(n) => n,
Err(e) => {
println!("{}", e);
0.0
}
}
}
/// Parse the string to return a i64. The `trim` is used to remove
/// /n and spaces.
///
/// # Errors
/// Will return 0 if the String cannot be convert and print the error
///
fn parse_int(s: &String) -> i64 {
match s.trim().parse::<i64>() {
Ok(n) => n,
Err(e) => {
println!("{}", e);
0
}
}
}
pub fn open_pdb_file(f: &str) -> Protein {
let f = match File::open(f) {
Ok(f) => f,
Err(e) => {
println!("Could not open the file \"{}\"\nError: {}", f, e);
process::exit(1);
}
};
let reader = BufReader::new(f);
let mut protein = Protein::new();
for line in reader.lines() {
let l = line.unwrap();
if l.starts_with("HETAM") || l.starts_with("ATOM") {
let _name = &l[12..17].trim().to_string();
let residue_name = &l[17..20].trim().to_string();
let atom_number = parse_int(&l[6..11].to_string());
let residue_number = parse_int(&l[22..26].to_string());
let x = parse_float(&l[30..38].to_string());
let y = parse_float(&l[38..46].to_string());
let z = parse_float(&l[46..54].to_string());
println!("{} {} {} {:?}", residue_name, atom_number, residue_number, [x, y, z]);
protein.add_info(residue_name.clone(), atom_number, residue_number, [x, y, z]);
}
}
protein
}
pub use self::pdb::read_pdb::parse_pdb as parse_pdb;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
pub use self::pdb::protein::Protein;
pub use self::pdb::atom::Atom;
pub use self::pdb::residue::Residue;
pub use self::pdb::chain::Chain;
extern crate pdbparser;
use pdbparser::open_pdb_file;
use pdbparser::*;
fn main() {
//let tutu = open_pdb_file("src/tests/f2_adn.pdb");
let _tutu = open_pdb_file("src/tests/hardcore_protein.pdb");
let tutu = parse_pdb("src/tests/f2.pdb");
//println!("Nb res : {}", tutu.get_res_number());
}
#[derive(Debug)]
pub struct Atom<'a> {
name: &'a str,
number: i64,
pub struct Atom {
name: String,
number: u64,
coord: [f32; 3],
}
impl<'a> Atom<'a> {
pub fn new(name: &str, number: i64, coord: [f32; 3]) -> Atom {
impl Atom {
pub fn new(name: String, number: u64, coord: [f32; 3]) -> Atom {
Atom {
name,
number,
coord,
}
}
}
use super::residue::Residue;
#[derive(Debug)]
pub struct Chain {
name: char,
lst_res: Vec<Residue>,
}
impl Chain {
pub fn new(name: char) -> Chain {
Chain {
name,
lst_res: Vec::new(),
}
}
pub fn add_res(&mut self, r: Residue) {
self.lst_res.push(r);
}
pub fn is_residue(&self, r: u64) -> bool {
for ii in &self.lst_res {
if ii.get_res_num() == r {
return true
}
}
return false
}
pub fn get_name(&self) -> char {
self.name
}
}
\ No newline at end of file
mod atom;
pub mod atom;
pub mod protein;
mod residue;
pub mod residue;
pub mod read_pdb;
pub mod chain;
use super::residue::Residue;
use super::chain::Chain;
#[derive(Debug)]
pub struct Protein<'a> {
name: &'a str,
lst_res: Vec<Residue<'a>>,
last_residue: i64, // Le PDB est ordonné dans l'ordre par numéro de résidu donc si le résidu précédent est différent alors c'est un nouveau
pub struct Protein {
name: String,
lst_chain: Vec<Chain>,
last_chain: char,
}
impl<'a> Protein<'a> {
pub fn new() -> Protein<'a> {
impl<'a> Protein {
pub fn new(n : String) -> Protein {
Protein {
name: "",
lst_res: Vec::new(),
last_residue: 0,
name: n,
lst_chain: Vec::new(),
last_chain: ' ',
}
}
fn set_last_res(&mut self, n: i64) {
self.last_residue = n;
}
pub fn add_info(&mut self, res_name: String, atom_num: i64, res_num: i64, coord: [f32; 3]) {
// Si c'est un atome avec un nouveau residu on le cree sinon on ajoute l'atome au residu
if self.last_residue != res_num {
self.add_residue(res_num, atom_num, coord, res_name);
self.set_last_res(res_num);
} else {
self.update_residue(res_num.clone(), atom_num, coord);
pub fn is_chain(&self, c: char) -> bool {
for ii in &self.lst_chain {
if ii.get_name() == c {
return true
}
}
return false
}
fn add_residue(&mut self, res_number: i64, atom_number: i64, coord: [f32; 3], res_name: String) {
self.lst_res.push(Residue::new(res_name,"tutu"));
self.lst_res[res_number as usize].add_atom(atom_number, coord);
}
fn update_residue(&mut self, res_number: i64, atom_number: i64, coord: [f32; 3]) {
self.lst_res[res_number as usize].add_atom(atom_number, coord);
pub fn get_num_of_chain(&self) -> u32 {
self.lst_chain.len() as u32
}
}
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::process;
use super::protein::Protein;
use super::chain::Chain;
use super::residue::Residue;
use super::atom::Atom;
/// Parse the string to return a f32. The `trim` is used to remove
/// /n and spaces.
///
/// # Errors
/// Will return 0.0 if the String cannot be convert and print the error
///
fn parse_float(s: &String) -> f32 {
match s.trim().parse::<f32>() {
Ok(n) => n,
Err(e) => {
println!("{}", e);
0.0
}
}
}
/// Parse the string to return a i64. The `trim` is used to remove
/// /n and spaces.
///
/// # Errors
/// Will return 0 if the String cannot be convert and print the error
///
fn parse_int(s: &String) -> i64 {
match s.trim().parse::<i64>() {
Ok(n) => n,
Err(e) => {
println!("{}", e);
0
}
}
}
pub fn parse_pdb(f: &str) -> Protein {
// Check if the file exist and/or can be read
let f = match File::open(f) {
Ok(f) => f,
Err(e) => {
println!("Could not open the file \"{}\"\nError: {}", f, e);
process::exit(1);
}
};
let reader = BufReader::new(f);
let mut protein = Protein::new("4CIJ".to_string());
for line in reader.lines() {
let l = line.unwrap();
if l.starts_with("HETAM") || l.starts_with("ATOM") {
let atom_name = &l[12..17].trim().to_string();
let residue_name = &l[17..20].trim().to_string();
let chain = l[21..22].chars().next().unwrap();
let atom_number = parse_int(&l[6..11].to_string());
let residue_number = parse_int(&l[22..26].to_string());
let x = parse_float(&l[30..38].to_string());
let y = parse_float(&l[38..46].to_string());
let z = parse_float(&l[46..54].to_string());
//protein.add_info(chain.clone(), residue_name.clone(), residue_number, atom_name.clone(), atom_number as u64, [x, y, z]);
}
}
protein
}
\ No newline at end of file
use super::atom::Atom;
#[derive(Debug)]
pub struct Residue<'a> {
pub struct Residue {
name: String,
lst_atom: Vec<Atom<'a>>,
chain: &'a str,
res_num: u64,
lst_atom: Vec<Atom>,
chain: char,
}
impl<'a> Residue<'a> {
pub fn new(name: &'a str, chain: &'a str) -> Residue<'a> {
impl Residue {
pub fn new(name: String, res_num: u64, chain: char) -> Residue {
Residue {
name,
res_num,
chain,
lst_atom: Vec::new(),
}
}
pub fn add_atom(&mut self, atom_number: i64, coord: [f32; 3]) {
self.lst_atom.push(Atom::new("toto", atom_number, coord));
pub fn get_res_num(&self) -> u64 {
self.res_num
}
}
pub fn add_atom(&mut self, atom_name: String, atom_number: u64, coord: [f32; 3]) {
self.lst_atom.push(Atom::new(atom_name, atom_number, coord));
}
pub fn get_atom_number(&self) -> u64 {
self.lst_atom.len() as u64
}
}
\ No newline at end of file
extern crate pdbparser;
use pdbparser::Atom;
#[test]
fn test_atom() {
Atom::new();
assert_eq!("rr", "rr");
}
\ No newline at end of file
This diff is collapsed.
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