use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use crate::chem_env::{ChemEnv, default_rules};
use crate::search::{SearchConfig, find_routes};
#[pyfunction]
#[pyo3(name = "find_routes", signature = (target, depth=5, max_routes=5, beam_width=0, building_blocks=None))]
pub fn find_routes_py(
target: &str,
depth: u32,
max_routes: usize,
beam_width: usize,
building_blocks: Option<Vec<String>>,
) -> PyResult<String> {
let env = match building_blocks {
Some(ref bbs) => {
let refs: Vec<&str> = bbs.iter().map(|s| s.as_str()).collect();
ChemEnv::in_memory(&refs)
}
None => ChemEnv::load("data/building_blocks.smi")
.unwrap_or_else(|_| ChemEnv::in_memory(crate::DEFAULT_BUILDING_BLOCKS)),
};
let rules = default_rules();
let config = SearchConfig {
max_depth: depth,
max_routes,
beam_width,
..Default::default()
};
let routes = find_routes(target, &env, &rules, &config)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
let output = serde_json::json!({
"target": target,
"routes_found": routes.len(),
"routes": routes,
});
serde_json::to_string(&output).map_err(|e| PyValueError::new_err(e.to_string()))
}
#[pymodule]
pub fn renkin(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(find_routes_py, m)?)?;
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
Ok(())
}