xdrfile 0.3.0

Wrapper around the gromacs libxdrfile library. Can be used to read and write gromacs trajectories in xtc and trr format.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
//! # xdrfile
//! Read and write xdr trajectory files in .xtc and .trr file format
//!
//! This crate is mainly intended to be a wrapper around the GROMACS libxdrfile
//! XTC library and provides basic functionality to read and write xtc and trr
//! files with a safe api.
//!
//! # Basic usage example
//! ```rust
//! use xdrfile::*;
//!
//! fn main() -> Result<()> {
//!     // get a handle to the file
//!     let mut trj = XTCTrajectory::open_read("tests/1l2y.xtc")?;
//!
//!     // find number of atoms in the file
//!     let num_atoms = trj.get_num_atoms()?;
//!
//!     // a frame object is used to get to read or write from a trajectory
//!     // without instantiating data arrays for every step
//!     let mut frame = Frame::with_len(num_atoms);
//!
//!     // read the first frame of the trajectory
//!     trj.read(&mut frame)?;
//!
//!     assert_eq!(frame.step, 1);
//!     assert_eq!(frame.len(), num_atoms);
//!
//!     let first_atom_coords = frame.coords[0];
//!     assert_eq!(first_atom_coords, [-0.8901, 0.4127, -0.055499997]);
//!
//!     Ok(())
//! }
//! ```
//!
//! # Frame iteration
//! For convenience, the trajectory implementations provide "into_iter" to
//! be turned into an iterator that yields Rc<Frame>. If a frame is not kept
//! during iteration, the Iterator reuses it for better performance (and hence,
//! Rc is required)
//!
//! ```rust
//! use xdrfile::*;
//!
//! fn main() -> Result<()> {
//!     // get a handle to the file
//!     let trj = XTCTrajectory::open_read("tests/1l2y.xtc")?;
//!
//!     // iterate over all frames
//!     for (idx, result) in trj.into_iter().enumerate() {
//!         let frame = result?;
//!         println!("{}", frame.time);
//!         assert_eq!(idx+1, frame.step);
//!     }
//!     Ok(())
//! }
//! ```

#[cfg(test)]
#[macro_use]
extern crate assert_approx_eq;
extern crate lazy_init;

pub mod c_abi;
mod errors;
mod frame;
mod iterator;
pub use errors::*;
pub use frame::Frame;
pub use iterator::*;

use c_abi::xdr_seek;
use c_abi::xdrfile;
use c_abi::xdrfile::XDRFILE;
use c_abi::xdrfile_trr;
use c_abi::xdrfile_xtc;

use lazy_init::Lazy;
use std::cell::Cell;
use std::convert::{TryFrom, TryInto};
use std::ffi::CString;
use std::io;
use std::io::SeekFrom;
use std::os::raw::{c_float, c_int};
use std::path::{Path, PathBuf};

/// File Mode for accessing trajectories.
#[derive(Debug, Clone, PartialEq)]
pub enum FileMode {
    Write,
    Append,
    Read,
}

impl FileMode {
    /// Get a CStr slice corresponding to the file mode
    fn to_cstr(&self) -> &'static std::ffi::CStr {
        let bytes: &[u8; 2] = match *self {
            FileMode::Write => b"w\0",
            FileMode::Append => b"a\0",
            FileMode::Read => b"r\0",
        };

        std::ffi::CStr::from_bytes_with_nul(bytes).expect("CStr::from_bytes_with_nul failed")
    }
}

fn path_to_cstring(path: impl AsRef<Path>) -> Result<CString> {
    if let Some(s) = path.as_ref().to_str() {
        CString::new(s).map_err(|e| Error::InvalidOsStr(Some(e)))
    } else {
        Err(Error::InvalidOsStr(None))
    }
}

fn to<I, O>(value: I, task: ErrorTask, name: &'static str) -> Result<O>
where
    I: TryInto<O> + std::fmt::Display + Copy,
{
    value.try_into().map_err(|_| Error::OutOfRange {
        name,
        value: format!("{}", &value),
        target: std::any::type_name::<O>(),
        task,
    })
}

macro_rules! to {
    ($value:expr, $task:expr) => {
        to($value, $task, stringify!($value))
    };
}

/// Convert an error code from a C call to an Error
///
/// `code` should be an integer return code returned from the C API.
/// If `code` indicates the function returned successfully, None is returned;
/// otherwise, the code is converted into the appropriate `Error`.
fn check_code(code: impl Into<ErrorCode>, task: ErrorTask) -> Option<Error> {
    let code: ErrorCode = code.into();
    if let ErrorCode::ExdrOk = code {
        None
    } else {
        Some(Error::from((code, task)))
    }
}

/// A safe wrapper around the c implementation of an XDRFile
struct XDRFile {
    xdrfile: *mut XDRFILE,
    #[allow(dead_code)]
    filemode: FileMode,
    path: PathBuf,
}

impl XDRFile {
    pub fn open(path: impl AsRef<Path>, filemode: FileMode) -> Result<XDRFile> {
        let path = path.as_ref();
        unsafe {
            let path_p = path_to_cstring(path)?.into_raw();
            // SAFETY: mode_p must not be mutated by the C code
            let mode_p = filemode.to_cstr().as_ptr();

            let xdrfile = xdrfile::xdrfile_open(path_p, mode_p);

            // Reconstitute the CString so it is deallocated correctly
            let _ = CString::from_raw(path_p);

            if !xdrfile.is_null() {
                let path = path.to_owned();
                Ok(XDRFile {
                    xdrfile,
                    filemode,
                    path,
                })
            } else {
                // Something went wrong. But the C api does not tell us what
                Err((path, filemode))?
            }
        }
    }

    /// Get the current position in the file
    pub fn tell(&self) -> u64 {
        unsafe {
            xdr_seek::xdr_tell(self.xdrfile)
                .try_into()
                .expect("i64 could not be converted to u64")
        }
    }
}

impl io::Seek for XDRFile {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        let (whence, pos) = match pos {
            SeekFrom::Start(u) => (
                0,
                i64::try_from(u).expect("Seek position did not fit in i64"),
            ),
            SeekFrom::Current(i) => (1, i),
            SeekFrom::End(i) => (2, i),
        };
        unsafe {
            let code = xdr_seek::xdr_seek(self.xdrfile, pos, whence);
            match check_code(code, ErrorTask::Seek) {
                None => Ok(self.tell()),
                Some(err) => Err(io::Error::new(io::ErrorKind::Other, err)),
            }
        }
    }
}

impl Drop for XDRFile {
    /// Close the underlying xdr file on drop
    fn drop(&mut self) {
        unsafe {
            xdrfile::xdrfile_close(self.xdrfile);
        }
    }
}

/// The trajectory trait defines shared methods for xtc and trr trajectories
pub trait Trajectory {
    /// Read the next step of the trajectory into the frame object
    fn read(&mut self, frame: &mut Frame) -> Result<()>;

    /// Write the frame to the trajectory file
    fn write(&mut self, frame: &Frame) -> Result<()>;

    /// Flush the trajectory file
    fn flush(&mut self) -> Result<()>;

    /// Get the number of atoms from the give trajectory
    fn get_num_atoms(&mut self) -> Result<usize>;
}

/// Handle to Read/Write XTC Trajectories
pub struct XTCTrajectory {
    handle: XDRFile,
    precision: Cell<c_float>, // internal mutability required for read method
    num_atoms: Lazy<Result<usize>>,
}

impl XTCTrajectory {
    pub fn open(path: impl AsRef<Path>, filemode: FileMode) -> Result<XTCTrajectory> {
        let xdr = XDRFile::open(path, filemode)?;
        Ok(XTCTrajectory {
            handle: xdr,
            precision: Cell::new(1000.0),
            num_atoms: Lazy::new(),
        })
    }

    /// Open a file in read mode
    pub fn open_read(path: impl AsRef<Path>) -> Result<Self> {
        Self::open(path, FileMode::Read)
    }

    /// Open a file in append mode
    pub fn open_append(path: impl AsRef<Path>) -> Result<Self> {
        Self::open(path, FileMode::Append)
    }

    /// Open a file in write mode
    pub fn open_write(path: impl AsRef<Path>) -> Result<Self> {
        Self::open(path, FileMode::Write)
    }
}

impl Trajectory for XTCTrajectory {
    fn read(&mut self, frame: &mut Frame) -> Result<()> {
        let mut step: c_int = 0;

        let num_atoms = self
            .get_num_atoms()
            .map_err(|e| Error::CouldNotCheckNAtoms(Box::new(e)))?;
        if num_atoms != frame.coords.len() {
            Err((&*frame, num_atoms))?;
        };

        unsafe {
            let code = xdrfile_xtc::read_xtc(
                self.handle.xdrfile,
                to!(num_atoms, ErrorTask::Read)?,
                &mut step,
                &mut frame.time,
                &mut frame.box_vector,
                frame.coords.as_mut_ptr(),
                &mut self.precision.get(),
            );
            if let Some(err) = check_code(code, ErrorTask::Read) {
                return Err(err);
            }
            frame.step = to!(step, ErrorTask::Read)?;
            Ok(())
        }
    }

    fn write(&mut self, frame: &Frame) -> Result<()> {
        unsafe {
            let code = xdrfile_xtc::write_xtc(
                self.handle.xdrfile,
                to!(frame.num_atoms(), ErrorTask::Write)?,
                to!(frame.step, ErrorTask::Write)?,
                frame.time,
                &frame.box_vector,
                frame.coords.as_ptr(),
                1000.0,
            );
            if let Some(err) = check_code(code, ErrorTask::Write) {
                Err(err)
            } else {
                Ok(())
            }
        }
    }

    fn flush(&mut self) -> Result<()> {
        unsafe {
            let code = xdr_seek::xdr_flush(self.handle.xdrfile);
            if let Some(err) = check_code(code, ErrorTask::Flush) {
                Err(err)
            } else {
                Ok(())
            }
        }
    }

    fn get_num_atoms(&mut self) -> Result<usize> {
        self.num_atoms
            .get_or_create(|| {
                let mut num_atoms: c_int = 0;

                unsafe {
                    let path = path_to_cstring(&self.handle.path)?;
                    let path_p = path.into_raw();
                    let code = xdrfile_xtc::read_xtc_natoms(path_p, &mut num_atoms);
                    // Reconstitute the CString so it is deallocated correctly
                    let _ = CString::from_raw(path_p);

                    if let Some(err) = check_code(code, ErrorTask::ReadNumAtoms) {
                        Err(err)
                    } else {
                        to!(num_atoms, ErrorTask::ReadNumAtoms)
                    }
                }
            })
            .clone()
    }
}

impl XTCTrajectory {
    /// Get the current position in the file
    pub fn tell(&self) -> u64 {
        self.handle.tell()
    }
}

impl io::Seek for XTCTrajectory {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        self.handle.seek(pos)
    }
}

/// Handle to Read/Write TRR Trajectories
pub struct TRRTrajectory {
    handle: XDRFile,
    num_atoms: Lazy<Result<usize>>,
}

impl TRRTrajectory {
    pub fn open(path: impl AsRef<Path>, filemode: FileMode) -> Result<TRRTrajectory> {
        let xdr = XDRFile::open(path, filemode)?;
        Ok(TRRTrajectory {
            handle: xdr,
            num_atoms: Lazy::new(),
        })
    }

    /// Open a file in read mode
    pub fn open_read(path: impl AsRef<Path>) -> Result<Self> {
        Self::open(path, FileMode::Read)
    }

    /// Open a file in append mode
    pub fn open_append(path: impl AsRef<Path>) -> Result<Self> {
        Self::open(path, FileMode::Append)
    }

    /// Open a file in write mode
    pub fn open_write(path: impl AsRef<Path>) -> Result<Self> {
        Self::open(path, FileMode::Write)
    }
}

impl Trajectory for TRRTrajectory {
    fn read(&mut self, frame: &mut Frame) -> Result<()> {
        let mut step: c_int = 0;
        let mut lambda: c_float = 0.0;

        let num_atoms = self
            .get_num_atoms()
            .map_err(|e| Error::CouldNotCheckNAtoms(Box::new(e)))?;
        if num_atoms != frame.coords.len() {
            Err((&*frame, num_atoms))?;
        }

        unsafe {
            let code = xdrfile_trr::read_trr(
                self.handle.xdrfile,
                to!(num_atoms, ErrorTask::Read)?,
                &mut step,
                &mut frame.time,
                &mut lambda,
                &mut frame.box_vector,
                frame.coords.as_mut_ptr(),
                std::ptr::null_mut(),
                std::ptr::null_mut(),
            );
            if let Some(err) = check_code(code, ErrorTask::Read) {
                return Err(err);
            }
            frame.step = to!(step, ErrorTask::Read)?;
            Ok(())
        }
    }

    fn write(&mut self, frame: &Frame) -> Result<()> {
        unsafe {
            let code = xdrfile_trr::write_trr(
                self.handle.xdrfile,
                to!(frame.len(), ErrorTask::Write)?,
                to!(frame.step, ErrorTask::Write)?,
                frame.time,
                0.0,
                &frame.box_vector,
                frame.coords[..].as_ptr(),
                std::ptr::null_mut(),
                std::ptr::null_mut(),
            );
            if let Some(err) = check_code(code, ErrorTask::Write) {
                Err(err)
            } else {
                Ok(())
            }
        }
    }

    fn flush(&mut self) -> Result<()> {
        unsafe {
            let code = xdr_seek::xdr_flush(self.handle.xdrfile);
            if let Some(err) = check_code(code, ErrorTask::Flush) {
                Err(err)
            } else {
                Ok(())
            }
        }
    }

    fn get_num_atoms(&mut self) -> Result<usize> {
        self.num_atoms
            .get_or_create(|| {
                let mut num_atoms: c_int = 0;
                unsafe {
                    let path = path_to_cstring(&self.handle.path)?;
                    let path_p = path.into_raw();
                    let code = xdrfile_trr::read_trr_natoms(path_p, &mut num_atoms);
                    // Reconstitute the CString so it is deallocated correctly
                    let _ = CString::from_raw(path_p);

                    if let Some(err) = check_code(code, ErrorTask::ReadNumAtoms) {
                        Err(err)
                    } else {
                        to!(num_atoms, ErrorTask::ReadNumAtoms)
                    }
                }
            })
            .clone()
    }
}

impl TRRTrajectory {
    /// Get the current position in the file
    pub fn tell(&self) -> u64 {
        self.handle.tell()
    }
}

impl io::Seek for TRRTrajectory {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        self.handle.seek(pos)
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use std::io::Seek;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_read_write_xtc() -> Result<()> {
        let tempfile = NamedTempFile::new().expect("Could not create temporary file");
        let tmp_path = tempfile.path();

        let natoms = 2;
        let frame = Frame {
            step: 5,
            time: 2.0,
            box_vector: [[1.0, 2.0, 3.0], [2.0, 1.0, 3.0], [3.0, 2.0, 1.0]],
            coords: vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
        };
        let mut f = XTCTrajectory::open_write(&tmp_path)?;
        let write_status = f.write(&frame);
        match write_status {
            Err(_) => panic!("Failed"),
            Ok(()) => {}
        }
        f.flush()?;

        let mut new_frame = Frame::with_len(natoms);
        let mut f = XTCTrajectory::open_read(tmp_path)?;
        let num_atoms = f.get_num_atoms()?;
        assert_eq!(num_atoms, natoms);

        let read_status = f.read(&mut new_frame);
        match read_status {
            Err(e) => assert!(false, "{:?}", e),
            Ok(()) => {}
        }

        assert_eq!(new_frame.len(), frame.len());
        assert_eq!(new_frame.step, frame.step);
        assert_approx_eq!(new_frame.time, frame.time);
        assert_eq!(new_frame.box_vector, frame.box_vector);
        assert_eq!(new_frame.coords, frame.coords);
        Ok(())
    }

    #[test]
    fn test_read_write_trr() -> Result<()> {
        let tempfile = NamedTempFile::new().expect("Could not create temporary file");
        let tmp_path = tempfile.path();

        let natoms = 2;
        let frame = Frame {
            step: 5,
            time: 2.0,
            box_vector: [[1.0, 2.0, 3.0], [2.0, 1.0, 3.0], [3.0, 2.0, 1.0]],
            coords: vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
        };
        let mut f = TRRTrajectory::open_write(tmp_path)?;
        let write_status = f.write(&frame);
        match write_status {
            Err(_) => panic!("Failed"),
            Ok(()) => {}
        }
        f.flush()?;

        let mut new_frame = Frame::with_len(natoms);
        let mut f = TRRTrajectory::open_read(tmp_path)?;
        // let num_atoms = f.get_num_atoms()?;
        // assert_eq!(num_atoms, natoms);

        let read_status = f.read(&mut new_frame);
        match read_status {
            Err(e) => assert!(false, "{:?}", e),
            Ok(()) => {}
        }

        assert_eq!(new_frame.len(), frame.len());
        assert_eq!(new_frame.step, frame.step);
        assert_eq!(new_frame.time, frame.time);
        assert_eq!(new_frame.box_vector, frame.box_vector);
        assert_eq!(new_frame.coords, frame.coords);
        Ok(())
    }

    #[test]
    pub fn test_manual_loop() -> Result<(), Box<dyn std::error::Error>> {
        let mut xtc_frames = Vec::new();
        let mut xtc_traj = XTCTrajectory::open_read("tests/1l2y.xtc")?;
        let mut frame = Frame::with_len(xtc_traj.get_num_atoms()?);

        while let Ok(()) = xtc_traj.read(&mut frame) {
            xtc_frames.push(frame.clone());
        }

        let mut trr_frames = Vec::new();
        let mut trr_traj = TRRTrajectory::open_read("tests/1l2y.trr")?;

        while let Ok(()) = trr_traj.read(&mut frame) {
            trr_frames.push(frame.clone());
        }

        for (xtc, trr) in xtc_frames.into_iter().zip(trr_frames) {
            assert_eq!(xtc.len(), trr.len());
            assert_eq!(xtc.step, trr.step);
            assert_eq!(xtc.time, trr.time);
            assert_eq!(xtc.box_vector, trr.box_vector);
            for (xtc_xyz, trr_xyz) in xtc.coords.into_iter().zip(trr.coords) {
                assert!(xtc_xyz[0] - trr_xyz[0] <= 1e-5);
                assert!(xtc_xyz[1] - trr_xyz[1] <= 1e-5);
                assert!(xtc_xyz[2] - trr_xyz[2] <= 1e-5);
            }
        }
        Ok(())
    }

    #[test]
    pub fn test_wrong_size_frame() -> Result<(), Box<dyn std::error::Error>> {
        let mut xtc_traj = XTCTrajectory::open_read("tests/1l2y.xtc")?;
        let mut frame = Frame::new();

        let result = xtc_traj.read(&mut frame);
        if let Err(e) = result {
            assert!(matches!(e, Error::WrongSizeFrame { .. }));
        } else {
            panic!("A read with an incorrectly sized frame should not succeed")
        }
        Ok(())
    }

    #[test]
    fn test_path_to_cstring() -> Result<(), Box<dyn std::error::Error>> {
        // A valid string should convert to CString successfully
        let valid_result = path_to_cstring(PathBuf::from("test"));
        match valid_result {
            Ok(s) => {
                assert_eq!(s, CString::new("test")?);
            }
            Err(_) => panic!("Valid Path failed to convert to CString.")
        }

        // \0 in path should result in an InvalidOsStr(Some(NulError)) 
        let result = path_to_cstring(PathBuf::from("invalid/\0path"));
        match result {
            Ok(_) => panic!("Cstring conversion did not fail"),
            Err(e) => {
                match e {
                    Error::InvalidOsStr(opt) => {
                        assert!(opt.is_some())
                    }
                    _ => panic!("Wrong error type. (This should never happend).")
                }
            }
        }
        Ok(())
    }

    #[test]
    fn test_tell() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let tempfile = NamedTempFile::new()?;
        let tmp_path = tempfile.path();

        let natoms: usize = 2;
        let frame = Frame {
            step: 5,
            time: 2.0,
            box_vector: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
            coords: vec![[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]],
        };
        let mut f = TRRTrajectory::open_write(tmp_path)?;
        assert_eq!(f.tell(), 0);
        f.write(&frame)?;
        assert_eq!(f.tell(), 144);
        f.flush()?;

        let mut new_frame = Frame::with_len(natoms);
        let mut f = TRRTrajectory::open_read(tmp_path)?;
        assert_eq!(f.tell(), 0);

        f.read(&mut new_frame)?;
        assert_eq!(f.tell(), 144);

        Ok(())
    }

    #[test]
    fn test_seek() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let tempfile = NamedTempFile::new()?;
        let tmp_path = tempfile.path();

        let natoms: usize = 2;
        let mut frame = Frame {
            step: 0,
            time: 0.0,
            box_vector: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
            coords: vec![[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]],
        };
        let mut f = TRRTrajectory::open_write(tmp_path)?;
        f.write(&frame)?;
        let after_first_frame = f.tell();
        frame.step += 1;
        frame.time += 10.0;
        f.write(&frame)?;
        let after_second_frame = f.tell();
        f.flush()?;

        let mut new_frame = Frame::with_len(natoms);
        let mut f = TRRTrajectory::open_read(tmp_path)?;
        let pos = f.seek(std::io::SeekFrom::Current(144))?;
        assert_eq!(pos, after_first_frame);

        f.read(&mut new_frame)?;
        assert_eq!(f.tell(), after_second_frame);

        assert_eq!(new_frame.len(), frame.len());
        assert_eq!(new_frame.step, frame.step);
        assert_eq!(new_frame.time, frame.time);
        assert_eq!(new_frame.box_vector, frame.box_vector);
        assert_eq!(new_frame.coords, frame.coords);

        Ok(())
    }

    #[test]
    fn test_err_could_not_open() {
        let file_name = "non-existent.xtc";

        let path = Path::new(&file_name);
        if let Err(e) = XDRFile::open(file_name, FileMode::Read) {
            if let Error::CouldNotOpen {
                path: err_path,
                mode: err_mode,
            } = e
            {
                assert_eq!(path, err_path);
                assert_eq!(FileMode::Read, err_mode)
            } else {
                panic!("Wrong Error type")
            }
        }
    }

    #[test]
    fn test_err_could_not_read_atom_nr() -> Result<()> {
        let file_name = "README.md"; // not a trajectory
        let mut trr = TRRTrajectory::open_read(file_name)?;
        if let Err(e) = trr.get_num_atoms() {
            assert_eq!(Some(ErrorCode::ExdrMagic), e.code());
        } else {
            panic!("Should not be able to read number of atoms from readme");
        }
        Ok(())
    }

    #[test]
    fn test_err_could_not_read() -> Result<()> {
        let file_name = "README.md"; // not a trajectory
        let mut frame = Frame::with_len(1);
        let mut trr = TRRTrajectory::open_read(file_name)?;
        if let Err(e) = trr.read(&mut frame) {
            assert_eq!(Some(ErrorCode::ExdrMagic), e.code());
        } else {
            panic!("Should not be able to read number of atoms from readme");
        }
        Ok(())
    }

    #[test]
    fn test_err_file_eof() -> Result<(), Box<dyn std::error::Error>> {
        let tempfile = NamedTempFile::new()?;
        let tmp_path = tempfile.path();

        let natoms = 2;
        let frame = Frame {
            step: 5,
            time: 2.0,
            box_vector: [[1.0, 2.0, 3.0], [2.0, 1.0, 3.0], [3.0, 2.0, 1.0]],
            coords: vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
        };
        let mut f = XTCTrajectory::open_write(&tmp_path)?;
        f.write(&frame)?;
        f.flush()?;

        let mut new_frame = Frame::with_len(natoms);
        let mut f = XTCTrajectory::open_read(tmp_path)?;

        f.read(&mut new_frame)?;

        let result = f.read(&mut new_frame); // Should be eof as we only wrote one frame
        if let Err(e) = result {
            assert!(e.is_eof());
        } else {
            panic!("read two frames after writing one");
        }

        let mut file = std::fs::File::create(tmp_path)?;
        file.write_all(&[0; 999])?;
        file.flush()?;

        let mut f = XTCTrajectory::open_read(tmp_path)?;
        let result = f.read(&mut new_frame); // Should be an invalid XTC file
        if let Err(e) = result {
            assert!(!e.is_eof());
        } else {
            panic!("999 zero bytes was read as a valid XTC file");
        }

        Ok(())
    }

    #[test]
    fn test_check_code() {
        let code: ErrorCode = 0.into();
        assert!(!check_code(code, ErrorTask::Read).is_some());

        for i in vec![1, 10, 100, 1000] {
            let code: ErrorCode = i.into();
            assert!(check_code(code, ErrorTask::Read).is_some());
        }
    }

    #[test]
    fn test_to() -> Result<()> {
        assert_eq!(24234_i32, to!(24234_usize, ErrorTask::Write)?);

        let big_number = 3_294_967_295_usize;
        let expected: Result<i32> = Err(Error::OutOfRange {
            name: "big_number",
            task: ErrorTask::Write,
            value: "3294967295".to_string(),
            target: "i32",
        });
        assert_eq!(expected, to!(big_number, ErrorTask::Write));

        let num_atoms: usize = 304;
        let res: Result<u8, _> = to!(num_atoms, ErrorTask::Write);
        assert_eq!(
            format!("{}", res.unwrap_err()),
            "Illegal num_atoms while writing trajectory: Failed to cast 304 to u8"
        );

        Ok(())
    }

    #[test]
    fn test_write_outofrange_step() -> Result<(), Box<dyn std::error::Error>> {
        let tempfile = NamedTempFile::new()?;
        let tmp_path = tempfile.path();
        let mut traj = XTCTrajectory::open_write(tmp_path)?;

        let frame = Frame {
            step: usize::MAX,
            time: 0.0,
            box_vector: [[0.0; 3]; 3],
            coords: vec![[1.0; 3]],
        };
        let expected = Error::OutOfRange {
            name: "frame.step",
            value: usize::MAX.to_string(),
            target: "i32",
            task: ErrorTask::Write,
        };

        if let Err(e) = traj.write(&frame) {
            print!("{:?}", e);
            assert_eq!(expected, e);
        } else {
            panic!("Writing frame with step=usize::MAX should not succeed.")
        }

        Ok(())
    }
}