Struct chemfiles::MemoryTrajectoryReader
source · pub struct MemoryTrajectoryReader<'data> { /* private fields */ }
Expand description
MemoryTrajectoryReader
is a handle for a Trajectory
in memory.
Implementations§
source§impl<'data> MemoryTrajectoryReader<'data>
impl<'data> MemoryTrajectoryReader<'data>
sourcepub fn new<Data, Format>(
data: Data,
format: Format
) -> Result<MemoryTrajectoryReader<'data>, Error>where
Data: Into<&'data [u8]>,
Format: AsRef<str>,
pub fn new<Data, Format>( data: Data, format: Format ) -> Result<MemoryTrajectoryReader<'data>, Error>where Data: Into<&'data [u8]>, Format: AsRef<str>,
Read a memory buffer as though it was a formatted file.
The memory buffer used to store the file is given using the data
argument. The format
parameter is required and should follow the same
rules as in the main Trajectory
constructor.
Errors
This function fails if the data is incorrectly formatted for the corresponding format, or if the format do not support in-memory readers.
Example
let aromatics = "c1ccccc1\nc1ccco1\nc1ccccn1\n";
let mut trajectory = MemoryTrajectoryReader::new(aromatics.as_bytes(), "SMI").unwrap();
let mut frame = Frame::new();
trajectory.read(&mut frame).unwrap();
assert_eq!(frame.size(), 6);
Methods from Deref<Target = Trajectory>§
sourcepub fn read(&mut self, frame: &mut Frame) -> Result<(), Error>
pub fn read(&mut self, frame: &mut Frame) -> Result<(), Error>
Read the next step of this trajectory into a frame
.
If the number of atoms in frame does not correspond to the number of atom in the next step, the frame is resized.
Errors
This function fails if the data is incorrectly formatted for the corresponding format, or in case of I/O errors from the OS.
Example
let mut trajectory = Trajectory::open("water.xyz", 'r').unwrap();
let mut frame = Frame::new();
trajectory.read(&mut frame).unwrap();
sourcepub fn read_step(&mut self, step: usize, frame: &mut Frame) -> Result<(), Error>
pub fn read_step(&mut self, step: usize, frame: &mut Frame) -> Result<(), Error>
Read a specific step
of this trajectory into a frame
.
If the number of atoms in frame does not correspond to the number of atom at this step, the frame is resized.
Errors
This function fails if the data is incorrectly formatted for the corresponding format.
Example
let mut trajectory = Trajectory::open("water.xyz", 'r').unwrap();
let mut frame = Frame::new();
trajectory.read_step(10, &mut frame).unwrap();
sourcepub fn set_topology(&mut self, topology: &Topology)
pub fn set_topology(&mut self, topology: &Topology)
Set the topology
associated with this trajectory. This topology will
be used when reading and writing the files, replacing any topology in
the frames or files.
Example
let mut topology = Topology::new();
topology.add_atom(&Atom::new("H"));
topology.add_atom(&Atom::new("O"));
topology.add_atom(&Atom::new("H"));
topology.add_bond(0, 1);
topology.add_bond(1, 2);
let mut trajectory = Trajectory::open("water.xyz", 'r').unwrap();
trajectory.set_topology(&topology);
sourcepub fn set_topology_file<P>(&mut self, path: P) -> Result<(), Error>where
P: AsRef<Path>,
pub fn set_topology_file<P>(&mut self, path: P) -> Result<(), Error>where P: AsRef<Path>,
Set the topology associated with this trajectory by reading the first
frame of the file at the given path
using the file format in format
;
and extracting the topology of this frame.
Errors
This function fails if the topology file is incorrectly formatted for the corresponding format, or in case of I/O errors from the OS.
Example
let mut trajectory = Trajectory::open("water.nc", 'r').unwrap();
trajectory.set_topology_file("topology.pdb").unwrap();
sourcepub fn set_topology_with_format<'a, P, S>(
&mut self,
path: P,
format: S
) -> Result<(), Error>where
P: AsRef<Path>,
S: Into<&'a str>,
pub fn set_topology_with_format<'a, P, S>( &mut self, path: P, format: S ) -> Result<(), Error>where P: AsRef<Path>, S: Into<&'a str>,
Set the topology associated with this trajectory by reading the first
frame of the file at the given path
using the file format in
format
; and extracting the topology of this frame.
If format
is an empty string, the format will be guessed from the
path
extension.
Errors
This function fails if the topology file is incorrectly formatted for the corresponding format, or in case of I/O errors from the OS.
Example
let mut trajectory = Trajectory::open("water.nc", 'r').unwrap();
trajectory.set_topology_with_format("topology.mol", "PDB").unwrap();
sourcepub fn set_cell(&mut self, cell: &UnitCell)
pub fn set_cell(&mut self, cell: &UnitCell)
Set the unit cell
associated with a trajectory. This cell will be
used when reading and writing the files, replacing any unit cell in the
frames or files.
Example
let mut trajectory = Trajectory::open("water.xyz", 'r').unwrap();
trajectory.set_cell(&UnitCell::new([10.0, 11.0, 12.5]));
sourcepub fn nsteps(&mut self) -> usize
pub fn nsteps(&mut self) -> usize
Get the number of steps (the number of frames) in a trajectory.
Example
let mut trajectory = Trajectory::open("water.xyz", 'r').unwrap();
println!("This trajectory contains {} steps", trajectory.nsteps());
sourcepub fn memory_buffer(&self) -> Result<&str, Error>
pub fn memory_buffer(&self) -> Result<&str, Error>
Obtain the memory buffer written to by the trajectory.
Errors
This fails if the trajectory was not opened with
Trajectory::memory_writer
.
Example
let mut trajectory_memory = Trajectory::memory_writer("SMI").unwrap();
let mut frame = Frame::new();
frame.add_atom(&Atom::new("C"), [0.0, 0.0, 0.0], None);
frame.add_atom(&Atom::new("C"), [0.0, 0.0, 0.0], None);
frame.add_bond_with_order(0, 1, BondOrder::Single);
trajectory_memory.write(&frame).unwrap();
let result = trajectory_memory.memory_buffer();
assert_eq!(result.unwrap(), "CC\n");