fast_nnt/
lib.rs

1use std::{env, time::Instant};
2
3use anyhow::{Context, Result};
4use env_logger::Builder;
5use log::{LevelFilter, info};
6use ndarray::Array2;
7
8use crate::{
9    cli::{NeighbourNetArgs, ProgramArgs},
10    neighbour_net::neighbour_net::NeighbourNet,
11    nexus::nexus::Nexus,
12};
13
14pub mod algorithms;
15pub mod cli;
16pub mod data;
17pub mod neighbour_net;
18pub mod nexus;
19pub mod ordering;
20pub mod phylo;
21pub mod splits;
22pub mod utils;
23pub mod weights;
24
25#[macro_use]
26extern crate log;
27
28pub fn set_log_level(matches: &ProgramArgs, is_last: bool, program_name: &str, version: &str) {
29    let mut log_level = LevelFilter::Info;
30    let mut specified = false;
31    if matches.verbose {
32        specified = true;
33        log_level = LevelFilter::Debug;
34    }
35    if matches.quiet {
36        specified = true;
37        log_level = LevelFilter::Error;
38    }
39    if specified || is_last {
40        let mut builder = Builder::new();
41        builder.filter_level(log_level);
42        if env::var("RUST_LOG").is_ok() {
43            builder.parse_filters(&env::var("RUST_LOG").unwrap());
44        }
45        if builder.try_init().is_err() {
46            panic!("Failed to set log level - has it been specified multiple times?")
47        }
48    }
49    if is_last {
50        info!("{} version {}", program_name, version);
51    }
52}
53
54/// The single entry point for bindings.
55///
56/// - `dist`: square distance matrix (n x n)
57/// - `labels`: length n
58/// - `args`: algorithm params (bindings can construct this or you add a smaller ArgsLite)
59pub fn run_fast_nnt_from_memory(
60    dist: Array2<f64>,
61    labels: Vec<String>,
62    args: NeighbourNetArgs,
63) -> Result<Nexus> {
64    let t0 = Instant::now();
65
66    // Validate
67    let neighbour_net = NeighbourNet::from_distance_matrix(dist, labels, args);
68    let nexus = neighbour_net
69        .generate_nexus()
70        .context("Performing neighbour net analysis")?;
71    info!("Finished NeighbourNet in {:?}", t0.elapsed());
72    Ok(nexus)
73}