Skip to main content

prodigal_rs/api/
training.rs

1use crate::types::Training;
2use super::types::ProdigalError;
3use std::io::{Read, Write};
4use std::path::Path;
5
6/// Opaque wrapper around learned training parameters.
7///
8/// Can be saved/loaded to reuse training across runs.
9pub struct TrainingData {
10    pub(crate) inner: Box<Training>,
11}
12
13impl TrainingData {
14    /// Load training data from a Prodigal binary training file.
15    pub fn load(path: impl AsRef<Path>) -> Result<Self, ProdigalError> {
16        let mut file = std::fs::File::open(path)?;
17        let mut inner = Box::new(unsafe { std::mem::zeroed::<Training>() });
18        let size = std::mem::size_of::<Training>();
19        let buf = unsafe { std::slice::from_raw_parts_mut(&mut *inner as *mut Training as *mut u8, size) };
20        file.read_exact(buf)?;
21        Ok(TrainingData { inner })
22    }
23
24    /// Save training data to a file.
25    pub fn save(&self, path: impl AsRef<Path>) -> Result<(), ProdigalError> {
26        let mut file = std::fs::File::create(path)?;
27        let size = std::mem::size_of::<Training>();
28        let buf = unsafe { std::slice::from_raw_parts(&*self.inner as *const Training as *const u8, size) };
29        file.write_all(buf)?;
30        Ok(())
31    }
32
33    /// GC content this model was trained on.
34    pub fn gc(&self) -> f64 {
35        self.inner.gc
36    }
37
38    /// Translation table.
39    pub fn translation_table(&self) -> i32 {
40        self.inner.trans_table
41    }
42
43    /// Whether this model uses Shine-Dalgarno RBS scoring.
44    pub fn uses_sd(&self) -> bool {
45        self.inner.uses_sd != 0
46    }
47}
48
49impl Clone for TrainingData {
50    fn clone(&self) -> Self {
51        TrainingData {
52            inner: self.inner.clone(),
53        }
54    }
55}