Skip to main content

prodigal_rs/api/
training.rs

1use super::types::ProdigalError;
2use crate::types::Training;
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 {
20            std::slice::from_raw_parts_mut(&mut *inner as *mut Training as *mut u8, size)
21        };
22        file.read_exact(buf)?;
23        Ok(TrainingData { inner })
24    }
25
26    /// Save training data to a file.
27    pub fn save(&self, path: impl AsRef<Path>) -> Result<(), ProdigalError> {
28        let mut file = std::fs::File::create(path)?;
29        let size = std::mem::size_of::<Training>();
30        let buf = unsafe {
31            std::slice::from_raw_parts(&*self.inner as *const Training as *const u8, size)
32        };
33        file.write_all(buf)?;
34        Ok(())
35    }
36
37    /// GC content this model was trained on.
38    pub fn gc(&self) -> f64 {
39        self.inner.gc
40    }
41
42    /// Translation table.
43    pub fn translation_table(&self) -> i32 {
44        self.inner.trans_table
45    }
46
47    /// Whether this model uses Shine-Dalgarno RBS scoring.
48    pub fn uses_sd(&self) -> bool {
49        self.inner.uses_sd != 0
50    }
51}
52
53impl Clone for TrainingData {
54    fn clone(&self) -> Self {
55        TrainingData {
56            inner: self.inner.clone(),
57        }
58    }
59}