Skip to content

Commit f37570b

Browse files
committed
Fixed #96 and small other bug
1 parent 4292928 commit f37570b

File tree

6 files changed

+51247
-17
lines changed

6 files changed

+51247
-17
lines changed

example-pdbs/2btv.pdb

Lines changed: 51201 additions & 0 deletions
Large diffs are not rendered by default.

src/error/pdberror.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ impl PDBError {
5555
&self.short_description
5656
}
5757

58+
/// Gives the long description for this error
59+
pub fn long_description(&self) -> &str {
60+
&self.long_description
61+
}
62+
5863
/// Gives the context for this error
5964
pub const fn context(&self) -> &Context {
6065
&self.context

src/read/pdb/parser.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ where
102102
errors.extend(line_errors);
103103
match result {
104104
LexItem::Header(_, _, identifier) => pdb.identifier = Some(identifier),
105-
LexItem::Remark(num, text) => pdb.add_remark(num, text.to_string()),
105+
LexItem::Remark(num, text) => {
106+
let _ = pdb.add_remark(num, text.to_string()); // Better error messages are created downstream
107+
}
106108
LexItem::Atom(
107109
hetero,
108110
serial_number,

src/reference_tables.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ pub fn valid_remark_type_number(number: usize) -> bool {
6464
}
6565

6666
/// The valid remark type numbers as of PDB v3.30
67-
const REMARK_TYPES: [usize; 41] = [
67+
const REMARK_TYPES: [usize; 42] = [
6868
0, 1, 2, 3, 4, 5, 100, 200, 205, 210, 215, 217, 230, 240, 245, 247, 250, 265, 280, 285, 290,
69-
300, 350, 375, 450, 465, 470, 475, 480, 500, 525, 600, 610, 615, 620, 630, 650, 700, 800, 900,
70-
999,
69+
300, 350, 375, 400, 450, 465, 470, 475, 480, 500, 525, 600, 610, 615, 620, 630, 650, 700, 800,
70+
900, 999,
7171
];
7272

7373
/// All amino acids. Includes Amber-specific naming conventions for (de-)protonated versions, CYS involved in

src/save/pdb.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ pub fn save_pdb_raw<T: Write>(pdb: &PDB, mut sink: BufWriter<T>, level: Strictne
416416
let m = mtrix.transformation.matrix();
417417
print_line(vec![
418418
(0, "MTRIX1"),
419+
(0, " "),
419420
(3, mtrix.serial_number.to_string().as_str()),
420421
(10, &format!("{:10.6}", m[0][0])),
421422
(10, &format!("{:10.6}", m[0][1])),
@@ -427,6 +428,7 @@ pub fn save_pdb_raw<T: Write>(pdb: &PDB, mut sink: BufWriter<T>, level: Strictne
427428
]);
428429
print_line(vec![
429430
(0, "MTRIX2"),
431+
(0, " "),
430432
(3, mtrix.serial_number.to_string().as_str()),
431433
(10, &format!("{:10.6}", m[1][0])),
432434
(10, &format!("{:10.6}", m[1][1])),
@@ -438,6 +440,7 @@ pub fn save_pdb_raw<T: Write>(pdb: &PDB, mut sink: BufWriter<T>, level: Strictne
438440
]);
439441
print_line(vec![
440442
(0, "MTRIX3"),
443+
(0, " "),
441444
(3, mtrix.serial_number.to_string().as_str()),
442445
(10, &format!("{:10.6}", m[2][0])),
443446
(10, &format!("{:10.6}", m[2][1])),

src/structs/pdb.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![allow(dead_code)]
2-
use crate::reference_tables;
32
use crate::structs::hierarchy::*;
4-
use crate::structs::*;
53
use crate::transformation::TransformationMatrix;
4+
use crate::{reference_tables, PDBError};
5+
use crate::{structs::*, Context};
66
use doc_cfg::doc_cfg;
77
#[cfg(feature = "rayon")]
88
use rayon::prelude::*;
@@ -108,20 +108,39 @@ impl PDB {
108108
/// * `remark_type` - the remark-type-number
109109
/// * `remark_text` - the free line of text, containing the actual remark
110110
///
111-
/// ## Panics
112-
/// It panics if the text if too long, the text contains invalid characters or the remark-type-number is not valid (wwPDB v3.30).
113-
pub fn add_remark(&mut self, remark_type: usize, remark_text: String) {
114-
assert!(reference_tables::valid_remark_type_number(remark_type), "The given remark-type-number is not valid, see wwPDB v3.30 for valid remark-type-numbers");
115-
assert!(
116-
valid_text(&remark_text),
117-
"The given remark text contains invalid characters."
118-
);
119-
// As the text can only contain ASCII len() on strings is fine (it returns the length in bytes)
120-
if remark_text.len() > 70 {
121-
println!("WARNING: The given remark text is too long, the maximal length is 68 characters, the given string is {} characters.", remark_text.len());
111+
/// ## Fails
112+
/// It fails if the text if too long, the text contains invalid characters or the remark-type-number is not valid (wwPDB v3.30).
113+
pub fn add_remark(&mut self, remark_type: usize, remark_text: String) -> Result<(), PDBError> {
114+
let context = Context::show(format!("REMARK {:3} {}", remark_type, remark_text));
115+
if !reference_tables::valid_remark_type_number(remark_type) {
116+
return Err(PDBError::new(
117+
crate::ErrorLevel::InvalidatingError,
118+
"Remark-type-number invalid",
119+
"The given remark-type-number is not valid, see wwPDB v3.30 for valid remark-type-numbers",
120+
context));
122121
}
122+
if !valid_text(&remark_text) {
123+
return Err(PDBError::new(
124+
crate::ErrorLevel::InvalidatingError,
125+
"Remark text invalid",
126+
"The given remark text contains invalid characters.",
127+
context,
128+
));
129+
}
130+
131+
// As the text can only contain ASCII len() on strings is fine (it returns the length in bytes)
132+
let res = if remark_text.len() > 70 {
133+
Err(PDBError::new(
134+
crate::ErrorLevel::LooseWarning,
135+
"Remark text too long",
136+
format!("The given remark text is too long, the maximal length is 68 characters, the given string is {} characters.", remark_text.len()),
137+
context))
138+
} else {
139+
Ok(())
140+
};
123141

124142
self.remarks.push((remark_type, remark_text));
143+
res
125144
}
126145

127146
/// Delete the remarks matching the given predicate.

0 commit comments

Comments
 (0)