Struct chemfiles::Selection

source ·
pub struct Selection { /* private fields */ }
Expand description

A Selection allow to select atoms in a Frame, from a selection language. The selection language is built by combining basic operations. Each basic operation follows the <selector>[(<variable>)] <operator> <value> structure, where <operator> is a comparison operator in == != < <= > >=.

Implementations§

source§

impl Selection

source

pub fn new<'a, S: Into<&'a str>>(selection: S) -> Result<Selection, Error>

Create a new selection from the given selection string.

Errors

This function fails if the selection string is invalid.

Example
let selection = Selection::new("pairs: name(#1) H and name(#2) O").unwrap();
source

pub fn size(&self) -> usize

Get the size of the selection, i.e. the number of atoms we are selecting together.

This value is 1 for the ‘atom’ context, 2 for the ‘pair’ and ‘bond’ context, 3 for the ‘three’ and ‘angles’ context and 4 for the ‘four’ and ‘dihedral’ context.

Example
let selection = Selection::new("pairs: name(#1) H and name(#2) O").unwrap();
assert_eq!(selection.size(), 2);
source

pub fn string(&self) -> String

Get the selection string used to create this selection.

Example
let selection = Selection::new("name H").unwrap();
assert_eq!(selection.string(), "name H");
source

pub fn evaluate(&mut self, frame: &Frame) -> Vec<Match>

Evaluate a selection for a given frame, and return the corresponding matches.

Example
let mut frame = Frame::new();
frame.add_atom(&Atom::new("H"), [1.0, 0.0, 0.0], None);
frame.add_atom(&Atom::new("O"), [0.0, 0.0, 0.0], None);
frame.add_atom(&Atom::new("H"), [-1.0, 0.0, 0.0], None);

let mut selection = Selection::new("pairs: name(#1) H and name(#2) O").unwrap();
let matches = selection.evaluate(&frame);

assert_eq!(matches.len(), 2);

assert_eq!(matches[0].len(), 2);
assert_eq!(matches[0][0], 0);
assert_eq!(matches[0][1], 1);

assert_eq!(matches[1].len(), 2);
assert_eq!(matches[1][0], 2);
assert_eq!(matches[1][1], 1);
source

pub fn list(&mut self, frame: &Frame) -> Vec<usize>

Evaluates a selection of size 1 on a given frame. This function returns the list of atomic indexes in the frame matching this selection.

Panics

If the selection size is not 1

Example
let mut frame = Frame::new();
frame.add_atom(&Atom::new("H"), [1.0, 0.0, 0.0], None);
frame.add_atom(&Atom::new("O"), [0.0, 0.0, 0.0], None);
frame.add_atom(&Atom::new("H"), [-1.0, 0.0, 0.0], None);

let mut selection = Selection::new("name H").unwrap();
let matches = selection.list(&frame);

assert_eq!(matches.len(), 2);
assert_eq!(matches[0], 0);
assert_eq!(matches[1], 2);

Trait Implementations§

source§

impl Clone for Selection

source§

fn clone(&self) -> Selection

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Selection

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Selection

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.