Skip to main content

strided_heatmap/
strided_heatmap.rs

1//! Synthetic assay response from alternate columns of a noncontiguous view.
2
3// ANCHOR: example
4use lichtung_plot_ndarray as nd;
5use nd::{
6    formatting::Labels,
7    scales::{ColorDomain, ContinuousColorScale},
8};
9use ndarray::{array, s};
10
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    let mut arguments = std::env::args_os().skip(1);
13    let directory = match arguments.next() {
14        Some(path) if !path.is_empty() => std::path::PathBuf::from(path),
15        Some(_) => return Err("output directory must not be empty".into()),
16        None => std::env::current_dir()?,
17    };
18    if arguments.next().is_some() {
19        return Err("usage: strided_heatmap [output-directory]".into());
20    }
21    std::fs::create_dir_all(&directory)?;
22    // ANCHOR: strided-values
23    // Interleaved quality-control columns are deliberately excluded by the view.
24    let measurements = array![
25        [0.2, 99.0, 0.8, 99.0, 1.4, 99.0, 1.7, 99.0],
26        [0.1, 99.0, 0.6, 99.0, 1.1, 99.0, 1.3, 99.0],
27        [-0.1, 99.0, -0.5, 99.0, -0.9, 99.0, -1.4, 99.0],
28        [0.0, 99.0, 0.3, 99.0, 0.9, 99.0, 1.2, 99.0],
29    ];
30    let times = ["0 h", "2 h", "6 h", "12 h"];
31    let programs = [
32        "Inflammation",
33        "Stress response",
34        "Cell cycle",
35        "DNA repair",
36    ];
37    let present = array![
38        [true, true, true, true],
39        [true, true, true, true],
40        [true, false, true, true],
41        [true, true, true, true],
42    ];
43    let highlights = array![
44        [false, false, false, true],
45        [false, false, false, false],
46        [false, false, false, true],
47        [false, false, false, false],
48    ];
49    let cell_ids = array![
50        [101u64, 102, 103, 104],
51        [105, 106, 107, 108],
52        [109, 110, 111, 112],
53        [113, 114, 115, 116]
54    ];
55    let heatmap = nd::Heatmap::new((measurements.slice(s![.., ..;2]), present.view()))?
56        .highlight_cells(highlights.view())?
57        .source_rows(cell_ids.view())?;
58    // ANCHOR_END: strided-values
59    // ANCHOR: strided-plot
60    heatmap
61        .labels(Labels::raw())
62        .x_labels(times)?
63        .y_labels(programs)?
64        .x_label("Time after stimulus")
65        .y_label("Response program")
66        .color_label("Response (log2 fold change)")
67        .continuous_color_scale(ContinuousColorScale::diverging(
68            0.0,
69            None,
70            Some(ColorDomain::exact(-2.0, 2.0)?),
71        )?)
72        .size(780, 480)
73        .title("Response programs from a strided assay matrix")
74        .caption("Synthetic illustration · alternate source columns · one missing cell · no inferred clustering")
75        .svg(directory.join("strided_heatmap.svg"))?
76        .svg_preserve_text(directory.join("strided_heatmap_text.svg"))?;
77    // ANCHOR_END: strided-plot
78    Ok(())
79}
80// ANCHOR_END: example