data-beans 0.12.0

Sparse genomics data backends, QC, algorithms, and simulation
Documentation
//! `convert` writes the same matrix in another container or format, and
//! never over the file it reads.

use super::super::tests::input_in;
use super::*;

/// The shared 3 x 4 input, with a producer in its metadata.
fn fixture(dir: &std::path::Path) -> String {
    let path = input_in(dir);
    let mut data = open_sparse_matrix(&path, &SparseIoBackend::Zarr).unwrap();
    data.set_meta(meta::PRODUCER, "p1").unwrap();
    path
}

#[test]
fn an_output_that_is_the_input_is_refused_and_the_input_kept() {
    let dir = tempfile::tempdir().unwrap();
    let input = fixture(dir.path());
    // `-o in --no-zip` is in.zarr itself, cleared before it is read.
    let err = run_convert(&ConvertArgs {
        data_file: input.as_str().into(),
        backend: SparseIoBackend::Zarr,
        output: input.strip_suffix(".zarr").unwrap().into(),
        zip: false,
    })
    .unwrap_err();
    assert!(format!("{err:#}").contains("is the input"), "{err:#}");
    let kept = open_sparse_matrix(&input, &SparseIoBackend::Zarr).unwrap();
    assert_eq!(kept.num_non_zeros(), Some(4));
}

/// Into the other format and back: the matrix, its names and its metadata.
#[cfg(feature = "hdf5")]
#[test]
fn the_other_format_holds_the_same_matrix() {
    let dir = tempfile::tempdir().unwrap();
    let input = fixture(dir.path());
    let h5 = dir.path().join("out.h5");
    let h5 = h5.to_str().unwrap();
    let back = dir.path().join("back.zarr");
    let back = back.to_str().unwrap();
    for (from, to, backend) in [
        (input.as_str(), h5, SparseIoBackend::HDF5),
        (h5, back, SparseIoBackend::Zarr),
    ] {
        run_convert(&ConvertArgs {
            data_file: from.into(),
            backend,
            output: to.into(),
            zip: false,
        })
        .unwrap();
    }
    let read = |path: &str| {
        let data = open_sparse_matrix_by_path(path).unwrap();
        let ncol = data.num_columns().unwrap();
        (
            data.read_triplets_by_columns((0..ncol).collect()).unwrap(),
            data.read_rows_dmatrix((0..data.num_rows().unwrap()).collect())
                .unwrap(),
            data.row_names().unwrap(),
            data.column_names().unwrap(),
            data.meta(meta::PRODUCER),
        )
    };
    let want = read(&input);
    assert_eq!(read(h5), want);
    assert_eq!(read(back), want);
}