data-beans 0.11.122

Sparse genomics data backends, QC, algorithms, and simulation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::handlers::explore::{off_screen, open_session, pick_entries};
use crate::hdf5_io::*;
use crate::interactive::list_tui::{browse_in, ListView};
use crate::interactive::picks;
use crate::interactive::stat_tui::Side;
use crate::interactive::ui::Tui;
use crate::sparse_io::*;
use crate::utilities::io_helpers::{
    parse_index_spec_within, read_col_names, read_row_names, MAX_COLUMN_NAME_IDX, MAX_ROW_NAME_IDX,
};
use crate::utilities::name_matching::match_by_substring;
use clap::Args;
use legume_numeric::matrix::common_io::*;
use legume_numeric::matrix::traits::IoOps;
use std::io::Write;

/// A quick information of the underlying matrix of a backend file.
#[derive(Args, Debug)]
pub struct InfoArgs {
    /// data file -- .zarr or .h5 file
    pub data_file: Box<str>,

    /// file header for {output}.{rows.gz,columns.gz}
    #[arg(short, long, default_value = "")]
    pub output: Box<str>,
}

#[derive(Args, Debug)]
pub struct TakeColumnsArgs {
    /// data file -- either `.zarr` or `.h5`
    pub data_file: Box<str>,

    /// column indices to take: singles and inclusive ranges, e.g. `0,1,2,3`, `1-10`, or `1-20,50-55`
    #[arg(short = 'i', long)]
    pub column_indices: Option<Box<str>>,

    /// column name file where each line is a column name
    #[arg(short = 'f', long)]
    pub name_file: Option<Box<str>>,

    /// column names to take: e.g., `col1,col2,col3` (supports substring matching)
    #[arg(short = 'n', long, value_delimiter = ',')]
    pub column_names: Option<Vec<Box<str>>>,

    /// output `parquet` file
    #[arg(short, long, default_value = "stdout")]
    pub output: Box<str>,

    /// never open the explorer: what to take is given on the command line
    #[arg(long, default_value_t = false)]
    pub batch_process: bool,

    #[command(flatten)]
    pub session: legume_tui::cli::SessionArgs,
}

#[derive(Args, Debug)]
pub struct TakeRowsArgs {
    /// data file -- either `.zarr` or `.h5`
    pub data_file: Box<str>,

    /// row indices to take: singles and inclusive ranges, e.g. `0,1,2,3`, `1-10`, or `1-20,50-55`
    #[arg(short = 'i', long)]
    pub row_indices: Option<Box<str>>,

    /// row name file where each line is a row name
    #[arg(short = 'f', long)]
    pub name_file: Option<Box<str>>,

    /// row names to take: e.g., `gene1,gene2,gene3` (supports substring matching)
    #[arg(short = 'n', long, value_delimiter = ',')]
    pub row_names: Option<Vec<Box<str>>>,

    /// output `parquet` file
    #[arg(short, long, default_value = "stdout")]
    pub output: Box<str>,

    /// never open the explorer: what to take is given on the command line
    #[arg(long, default_value_t = false)]
    pub batch_process: bool,

    #[command(flatten)]
    pub session: legume_tui::cli::SessionArgs,
}

#[derive(Args, Debug)]
pub struct TakeColumnNamesArgs {
    /// data file -- either `.zarr` or `.h5`
    pub data_file: Box<str>,

    /// output file
    #[arg(short, long, default_value = "stdout")]
    pub output: Box<str>,

    #[arg(
        long,
        default_value_t = false,
        help = "Print the names and stop: no browser, even on a terminal",
        long_help = "On a terminal, the names open in a browser instead of being printed: `/`\n\
                     filters, Space marks, `w` writes the marked (or shown) names, `y` copies\n\
                     them. A file --output is still written. --batch-process (or no terminal)\n\
                     prints or writes them and stops."
    )]
    pub batch_process: bool,

    #[command(flatten)]
    pub session: legume_tui::cli::SessionArgs,
}

#[derive(Args, Debug)]
pub struct TakeRowNamesArgs {
    /// data file -- either `.zarr` or `.h5`
    pub data_file: Box<str>,

    /// output file
    #[arg(short, long, default_value = "stdout")]
    pub output: Box<str>,

    #[arg(
        long,
        default_value_t = false,
        help = "Print the names and stop: no browser, even on a terminal",
        long_help = "On a terminal, the names open in a browser instead of being printed: `/`\n\
                     filters, Space marks, `w` writes the marked (or shown) names, `y` copies\n\
                     them. A file --output is still written. --batch-process (or no terminal)\n\
                     prints or writes them and stops."
    )]
    pub batch_process: bool,

    #[command(flatten)]
    pub session: legume_tui::cli::SessionArgs,
}

/// Write a matrix as TSV with optional row/column names.
/// Arguments mirror `to_parquet_with_names`:
/// - `row_names`: `(Option<&[Box<str>]>, Option<&str>)` — names and label for the row dimension
/// - `column_names`: `Option<&[Box<str>]>` — column headers
fn write_named_tsv(
    output: &str,
    data: &DMatrix<f32>,
    row_names: (Option<&[Box<str>]>, Option<&str>),
    column_names: Option<&[Box<str>]>,
) -> anyhow::Result<()> {
    let nrows = data.nrows();
    let row_label = row_names.1.unwrap_or("row");
    let row_names: Vec<Box<str>> = match row_names.0 {
        Some(names) => names.to_vec(),
        None => (0..nrows).map(|i| format!("{}", i).into()).collect(),
    };
    let mut w = open_buf_writer(output)?;
    // Header
    write!(w, "{}", row_label)?;
    if let Some(col_names) = column_names {
        for name in col_names {
            write!(w, "\t{}", name)?;
        }
    } else {
        for j in 0..data.ncols() {
            write!(w, "\t{}", j)?;
        }
    }
    writeln!(w)?;
    // Data rows
    for (i, row) in data.row_iter().enumerate() {
        write!(w, "{}", row_names[i])?;
        for val in row.iter() {
            write!(w, "\t{}", val)?;
        }
        writeln!(w)?;
    }
    Ok(())
}

pub fn take_column_names(cmd_args: &TakeColumnNamesArgs) -> anyhow::Result<()> {
    let names = Names {
        command: "column-names",
        what: "columns",
        data_file: &cmd_args.data_file,
        output: &cmd_args.output,
        batch: cmd_args.batch_process,
        session: &cmd_args.session,
    };
    names.run(|data| data.column_names())
}

pub fn take_row_names(cmd_args: &TakeRowNamesArgs) -> anyhow::Result<()> {
    let names = Names {
        command: "row-names",
        what: "rows",
        data_file: &cmd_args.data_file,
        output: &cmd_args.output,
        batch: cmd_args.batch_process,
        session: &cmd_args.session,
    };
    names.run(|data| data.row_names())
}

/// `row-names` or `column-names`: the names to `output`, then, on a
/// terminal, in a browser (which replaces printing them to stdout).
struct Names<'a> {
    command: &'static str,
    what: &'static str,
    data_file: &'a str,
    output: &'a str,
    batch: bool,
    session: &'a legume_tui::cli::SessionArgs,
}

impl Names<'_> {
    fn run(
        &self,
        names_of: impl FnOnce(&dyn SparseIo<IndexIter = Vec<usize>>) -> anyhow::Result<Vec<Box<str>>>,
    ) -> anyhow::Result<()> {
        let view = crate::interactive::opens_view(self.command, self.batch);
        // The session first: hosted, the names are read there.
        let session = view
            .then(|| open_session(self.command, self.session))
            .transpose()?;
        let (backend, input) = resolve_backend_file(self.data_file, None)?;
        let data = open_sparse_matrix(&input, &backend)?;
        let names = names_of(data.as_ref())?;
        let to_stdout = self.output.eq_ignore_ascii_case("stdout");
        if !(view && to_stdout) {
            write_lines(&names, self.output)?;
        }
        let Some(session) = session else {
            return Ok(());
        };
        let stem = basename(self.data_file).unwrap_or_else(|_| self.data_file.into());
        let write_to = format!("{stem}.{}.txt", self.what);
        let title = format!("{} · {}", self.data_file, self.what);
        let mut tui = Tui::new(&session);
        browse_in(
            &mut tui,
            ListView::names("names", &title, &names, &write_to),
        )
    }
}

pub fn show_info(cmd_args: &InfoArgs) -> anyhow::Result<()> {
    let output = cmd_args.output.clone();

    let (backend, input) = resolve_backend_file(&cmd_args.data_file, None)?;

    let data = open_sparse_matrix(&input, &backend)?;

    if let (Some(nrow), Some(ncol), Some(nnz)) =
        (data.num_rows(), data.num_columns(), data.num_non_zeros())
    {
        println!("number_of_rows:\t{}", nrow);
        println!("number_of_columns:\t{}", ncol);
        println!("number_of_nonzeros:\t{}", nnz);
    }
    for (key, value) in data.metadata() {
        println!("meta.{key}:\t{value}");
    }

    if !output.is_empty() {
        // `output` is a file prefix: its folder is made, not one of its name.
        mkdir_parent(&output)?;
        let row_names = data.row_names()?;
        let col_names = data.column_names()?;
        write_lines(&row_names, &(output.to_string() + ".rows.gz"))?;
        write_lines(&col_names, &(output.to_string() + ".columns.gz"))?;
    }

    Ok(())
}

pub fn take_columns(args: &TakeColumnsArgs) -> anyhow::Result<()> {
    // The picker's session first: hosted, the rest runs in the session.
    // Nothing selected: picked in the explorer.
    let selected =
        args.column_indices.is_some() || args.name_file.is_some() || args.column_names.is_some();
    let options = "--column-indices, --name-file or --column-names";
    let session = picks("columns", args.batch_process, selected, options)?
        .then(|| open_session("columns", &args.session))
        .transpose()?;
    let mut tui = session.as_ref().map(Tui::new);
    let columns = args.column_indices.clone();
    let column_name_file = args.name_file.clone();
    let column_names_arg = args.column_names.clone();

    let (backend, data_file) = resolve_backend_file(&args.data_file, None)?;

    let output = args.output.clone();

    let picked = match tui.as_mut() {
        Some(tui) => match pick_entries(tui, &args.data_file, Side::Columns, "take")? {
            Some(picked) => Some(picked),
            None => return Ok(()),
        },
        None => None,
    };

    // Written off the screen: a `kill` meanwhile lets it finish.
    off_screen(tui.as_mut(), || {
        let data = open_sparse_matrix(&data_file, &backend)?;
        let row_names = data.row_names()?;

        let (data, column_names) = if let Some(columns) = picked {
            let names = data.column_names()?;
            let column_names: Vec<Box<str>> = columns.iter().map(|&i| names[i].clone()).collect();
            (data.read_columns_dmatrix(columns)?, column_names)
        } else if let Some(columns) = columns {
            let columns = parse_index_spec_within(&columns, data.num_columns().unwrap_or(0))?;

            if columns.is_empty() {
                return Err(anyhow::anyhow!("invalid indexes"));
            }

            let _names = data.column_names()?;
            let column_names: Vec<Box<str>> = columns.iter().map(|&i| _names[i].clone()).collect();

            (data.read_columns_dmatrix(columns)?, column_names)
        } else if let Some(column_file) = column_name_file {
            let col_names_to_match = read_col_names(column_file, MAX_COLUMN_NAME_IDX)?;
            let all_names = data.column_names()?;
            let (matched_indices, column_names) =
                match_by_substring(&all_names, &col_names_to_match, "column")?;
            (data.read_columns_dmatrix(matched_indices)?, column_names)
        } else if let Some(col_names_to_match) = column_names_arg {
            let all_names = data.column_names()?;
            let (matched_indices, column_names) =
                match_by_substring(&all_names, &col_names_to_match, "column")?;
            (data.read_columns_dmatrix(matched_indices)?, column_names)
        } else {
            return Err(anyhow::anyhow!(
                "either `column-indices`, `name-file` or `column-names` must be provided"
            ));
        };

        if let Ok(ext) = file_ext(&output) {
            if ext.as_ref() == "parquet" {
                data.to_parquet_with_names(
                    &output,
                    (Some(&row_names), Some("row_name")),
                    Some(&column_names),
                )?;
                return Ok(());
            }
        }

        write_named_tsv(
            &output,
            &data,
            (Some(&row_names), Some("row_name")),
            Some(&column_names),
        )?;

        Ok(())
    })
}

pub fn take_rows(args: &TakeRowsArgs) -> anyhow::Result<()> {
    // The picker's session first: hosted, the rest runs in the session.
    // Nothing selected: picked in the explorer.
    let selected =
        args.row_indices.is_some() || args.name_file.is_some() || args.row_names.is_some();
    let options = "--row-indices, --name-file or --row-names";
    let session = picks("rows", args.batch_process, selected, options)?
        .then(|| open_session("rows", &args.session))
        .transpose()?;
    let mut tui = session.as_ref().map(Tui::new);
    let rows = args.row_indices.clone();
    let row_name_file = args.name_file.clone();
    let row_names_arg = args.row_names.clone();

    let (backend, data_file) = resolve_backend_file(&args.data_file, None)?;

    let output = args.output.clone();

    let picked = match tui.as_mut() {
        Some(tui) => match pick_entries(tui, &args.data_file, Side::Rows, "take")? {
            Some(picked) => Some(picked),
            None => return Ok(()),
        },
        None => None,
    };

    // Written off the screen: a `kill` meanwhile lets it finish.
    off_screen(tui.as_mut(), || {
        let data_backend = open_sparse_matrix(&data_file, &backend)?;

        let (data, row_names) = if let Some(rows) = picked {
            let names = data_backend.row_names()?;
            let row_names: Vec<Box<str>> = rows.iter().map(|&i| names[i].clone()).collect();
            (data_backend.read_rows_dmatrix(rows)?, row_names)
        } else if let Some(rows) = rows {
            let rows = parse_index_spec_within(&rows, data_backend.num_rows().unwrap_or(0))?;

            if rows.is_empty() {
                return Err(anyhow::anyhow!("invalid indexes"));
            }

            let _names = data_backend.row_names()?;
            let row_names: Vec<Box<str>> = rows.iter().map(|&i| _names[i].clone()).collect();

            (data_backend.read_rows_dmatrix(rows)?, row_names)
        } else if let Some(row_name_file) = row_name_file {
            let row_names_to_match = read_row_names(row_name_file, MAX_ROW_NAME_IDX)?;
            let all_names = data_backend.row_names()?;
            let (matched_indices, row_names) =
                match_by_substring(&all_names, &row_names_to_match, "row")?;
            (data_backend.read_rows_dmatrix(matched_indices)?, row_names)
        } else if let Some(row_names_to_match) = row_names_arg {
            let all_names = data_backend.row_names()?;
            let (matched_indices, row_names) =
                match_by_substring(&all_names, &row_names_to_match, "row")?;
            (data_backend.read_rows_dmatrix(matched_indices)?, row_names)
        } else {
            return Err(anyhow::anyhow!(
                "either `row-indices`, `name-file` or `row-names` must be provided"
            ));
        };

        let data_t = data.transpose();
        let column_names = data_backend.column_names()?;

        if let Ok(ext) = file_ext(&output) {
            if ext.as_ref() == "parquet" {
                data_t.to_parquet_with_names(
                    &output,
                    (Some(&column_names), Some("column_name")),
                    Some(&row_names),
                )?;
                return Ok(());
            }
        }

        write_named_tsv(
            &output,
            &data_t,
            (Some(&column_names), Some("column_name")),
            Some(&row_names),
        )?;
        Ok(())
    })
}

#[cfg(test)]
#[path = "inspection_tests.rs"]
mod tests;