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