Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ow/pdb load options #120

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixes doctest
OWissett committed Apr 15, 2024
commit a51df2fd2067b0bee056d34d3a7d879e7cdf1ba1
21 changes: 9 additions & 12 deletions src/read/general.rs
Original file line number Diff line number Diff line change
@@ -26,19 +26,19 @@ pub type ReadResult = std::result::Result<(PDB, Vec<PDBError>), Vec<PDBError>>;
/// # Related
/// If you want to open a file from memory see [`open_raw`]. There are also function to open a specified file type directly
/// see [`crate::open_pdb`] and [`crate::open_mmcif`] respectively.
pub fn open(
filename: impl AsRef<str>,
level: StrictnessLevel,
) -> ReadResult {
open_with_options(filename, &ReadOptions::new().level(level))
pub fn open(filename: impl AsRef<str>, level: StrictnessLevel) -> ReadResult {
open_with_options(filename, &ReadOptions::new().set_level(level))
}

/// Opens a files based on the given options.
pub(in crate::read) fn open_with_options(filename: impl AsRef<str>, options: &ReadOptions) -> ReadResult {
pub(in crate::read) fn open_with_options(
filename: impl AsRef<str>,
options: &ReadOptions,
) -> ReadResult {
if check_extension(&filename, "pdb") {
open_pdb(filename, level)
open_pdb(filename, options.level)
} else if check_extension(&filename, "cif") {
open_mmcif(filename, level)
open_mmcif(filename, options.level)
} else {
Err(vec![PDBError::new(
ErrorLevel::BreakingError,
@@ -60,10 +60,7 @@ pub(in crate::read) fn open_with_options(filename: impl AsRef<str>, options: &Re
/// These functions are useful if you are using a non-standard compression algorithm or way of
/// storing the data.
#[cfg(feature = "compression")]
pub fn open_gz(
filename: impl AsRef<str>,
level: StrictnessLevel,
) -> ReadResult {
pub fn open_gz(filename: impl AsRef<str>, level: StrictnessLevel) -> ReadResult {
let filename = filename.as_ref();

if check_extension(filename, "gz") {
42 changes: 19 additions & 23 deletions src/read/read_options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::StrictnessLevel;

use super::general::{ReadResult, open_with_options};
use super::general::{open_with_options, ReadResult};

/// Used to set which format to read the file in.
#[derive(Debug, Clone, Copy, Default)]
@@ -29,11 +29,9 @@ impl From<&str> for Format {
///
/// This builder exposes the ability to configure how a [`PDB`] is loaded.
///
/// Generally speaking, when using `ReaderOptions`, you'll first call
/// [`OpenOptions::new`], then chain calls to methods to set each option, then
/// call [`OpenOptions::open`], passing the path of the file you're trying to
/// open. This will give you a [`io::Result`] with a [`File`] inside that you
/// can further operate on.
/// Generally speaking, when using `ReadOptions`, you'll first call
/// [`ReadOptions::new`], then chain calls to methods to set each option, then
/// call [`ReadOptions::read`].
///
/// # Examples
///
@@ -43,32 +41,32 @@ impl From<&str> for Format {
/// use pdbtbx::*;
///
/// let pdb = ReadOptions::new()
/// .format(Format::Pdb)
/// .level(StrictnessLevel::Loose)
/// .discard_hydrogens(true)
/// .set_format(Format::Auto)
/// .set_level(StrictnessLevel::Loose)
/// .set_discard_hydrogens(true)
/// .read("1CRN.pdb");
//
/// ```
#[derive(Debug, Default)]
pub struct ReadOptions {
/// The format to read the file in.
format: Format,
pub(crate) format: Format,

/// The strictness level to use when reading the file.
level: StrictnessLevel,
pub(crate) level: StrictnessLevel,

/// Controls whether to capitalise the chains in the structure.
capitalise_chains: bool,
pub(crate) capitalise_chains: bool,

/// Decompress
#[cfg(feature = "compression")]
decompress: bool,
pub(crate) decompress: bool,

/// Discard hydrogens
discard_hydrogens: bool,
pub(crate) discard_hydrogens: bool,

/// Only read the first model
only_first_model: bool,
pub(crate) only_first_model: bool,
}

impl ReadOptions {
@@ -78,38 +76,38 @@ impl ReadOptions {
}

/// Sets the format to read the file in.
pub fn format(&mut self, format: Format) -> &mut Self{
pub fn set_format(&mut self, format: Format) -> &mut Self {
self.format = format;
self
}

/// Sets the strictness level to use when reading the file.
pub fn level(&mut self, level: StrictnessLevel) -> &mut Self {
pub fn set_level(&mut self, level: StrictnessLevel) -> &mut Self {
self.level = level;
self
}

/// Sets whether to capitalise the chains in the structure.
pub fn capitalise_chains(&mut self, capitalise_chains: bool) -> &mut Self{
pub fn set_capitalise_chains(&mut self, capitalise_chains: bool) -> &mut Self {
self.capitalise_chains = capitalise_chains;
self
}

/// Sets whether to decompress the file.
#[cfg(feature = "compression")]
pub fn decompress(&mut self, decompress: bool) -> &mut Self{
pub fn set_decompress(&mut self, decompress: bool) -> &mut Self {
self.decompress = decompress;
self
}

/// Sets whether to discard hydrogens.
pub fn discard_hydrogens(&mut self, discard_hydrogens: bool) -> &mut Self {
pub fn set_discard_hydrogens(&mut self, discard_hydrogens: bool) -> &mut Self {
self.discard_hydrogens = discard_hydrogens;
self
}

/// Sets whether to only keep the first model.
pub fn only_first_model(&mut self, only_first_model: bool) -> &mut Self {
pub fn set_only_first_model(&mut self, only_first_model: bool) -> &mut Self {
self.only_first_model = only_first_model;
self
}
@@ -119,5 +117,3 @@ impl ReadOptions {
open_with_options(path, self)
}
}