rsomics-sc-combat 0.1.0

ComBat empirical-Bayes batch-effect correction of a single-cell matrix — matches scanpy pp.combat (parametric EB, ddof conventions)
Documentation
use std::path::PathBuf;

use clap::Parser;
use rsomics_common::{CommonFlags, Result, Tool, ToolMeta};
use rsomics_help::{Example, FlagSpec, HelpSpec, Origin, Section};

use rsomics_sc_combat::{open_output, run};

pub const META: ToolMeta = ToolMeta {
    name: env!("CARGO_PKG_NAME"),
    version: env!("CARGO_PKG_VERSION"),
};

#[derive(Parser, Debug)]
#[command(name = "rsomics-sc-combat", version, about, long_about = None, disable_help_flag = true)]
pub struct Cli {
    /// 10x MTX directory (matrix.mtx[.gz] + barcodes.tsv[.gz], genes×cells).
    pub input: PathBuf,

    /// Barcode → batch-label TSV: column 1 is the barcode, the label column is
    /// column 2 (or the `--key` column when a header is present).
    #[arg(short = 'b', long)]
    batch: PathBuf,

    /// Name of the label column in the batch TSV header.
    #[arg(long)]
    key: Option<String>,

    #[arg(short = 'o', long, default_value = "-")]
    output: String,

    #[command(flatten)]
    pub common: CommonFlags,
}

impl Tool for Cli {
    fn meta() -> ToolMeta {
        META
    }
    fn common(&self) -> &CommonFlags {
        &self.common
    }

    fn execute(self) -> Result<()> {
        self.common.install_rayon_pool()?;
        let out = open_output(&self.output)?;
        let (genes, cells, batches) = run(&self.input, &self.batch, self.key.as_deref(), out)?;
        if !self.common.quiet {
            eprintln!("ComBat-corrected {cells} cells × {genes} genes over {batches} batches");
        }
        Ok(())
    }
}

pub static HELP: HelpSpec = HelpSpec {
    name: env!("CARGO_PKG_NAME"),
    version: env!("CARGO_PKG_VERSION"),
    tagline: "ComBat empirical-Bayes batch-effect correction of a single-cell matrix.",
    origin: Some(Origin {
        upstream: "scanpy sc.pp.combat",
        upstream_license: "BSD-3-Clause",
        our_license: "MIT OR Apache-2.0",
        paper_doi: Some("10.1093/biostatistics/kxj037"),
    }),
    usage_lines: &["<10x-mtx-dir> -b batch.tsv [--key colname] [-o out.mtx]"],
    sections: &[Section {
        title: "OPTIONS",
        flags: &[
            FlagSpec {
                short: Some('b'),
                long: "batch",
                aliases: &[],
                value: Some("<path>"),
                type_hint: Some("PathBuf"),
                required: true,
                default: None,
                description: "Barcode → batch-label TSV (column 1 barcode, column 2 label).",
                why_default: None,
            },
            FlagSpec {
                short: None,
                long: "key",
                aliases: &[],
                value: Some("<name>"),
                type_hint: Some("String"),
                required: false,
                default: None,
                description: "Label column name when the batch TSV carries a header.",
                why_default: Some("Defaults to the second column."),
            },
            FlagSpec {
                short: Some('o'),
                long: "output",
                aliases: &[],
                value: Some("<path>"),
                type_hint: Some("String"),
                required: false,
                default: Some("-"),
                description: "Output dense MTX path (genes×cells array); '-' for stdout.",
                why_default: Some("Streams to stdout for pipeline composition."),
            },
        ],
    }],
    examples: &[Example {
        description: "correct batch effects with a two-column barcode→batch TSV",
        command: "rsomics-sc-combat filtered_feature_bc_matrix/ -b batches.tsv -o corrected.mtx",
    }],
    json_result_schema_doc: None,
};

#[cfg(test)]
mod tests {
    use super::*;
    use clap::CommandFactory;

    #[test]
    fn cli_debug_assert() {
        Cli::command().debug_assert();
    }
}