Skip to main content

splice_girl/
lib.rs

1//! Library entry point tying the pipeline together: parsed junctions ->
2//! per-(sample, gene) graphs -> Hodge decomposition. `main.rs` is a thin
3//! CLI wrapper around `analyze_junctions`.
4pub mod annotations;
5pub mod audit;
6pub mod baselines;
7pub mod compare;
8pub mod fdr;
9pub mod graph;
10pub mod hodge;
11pub mod manifest;
12pub mod matrix;
13pub mod output;
14pub mod parser;
15pub mod permutation;
16pub mod ranking;
17pub mod report;
18pub mod shared;
19pub mod statistics;
20pub mod visualize;
21
22use std::collections::BTreeMap;
23
24use graph::SpliceGraph;
25use hodge::{Decomposition, decompose};
26use parser::Junction;
27
28/// The graph and decomposition computed for one sample's one gene.
29#[derive(Debug)]
30pub struct GroupResult {
31    pub sample_id: String,
32    pub gene_id: String,
33    pub graph: SpliceGraph,
34    pub decomposition: Decomposition,
35}
36
37/// Groups junction rows by (sample_id, gene_id) — the decomposition is
38/// always computed within a single sample/gene, never pooled across
39/// samples — then builds a graph and runs the decomposition for each group.
40pub fn analyze_junctions(junctions: Vec<Junction>) -> Result<Vec<GroupResult>, String> {
41    let mut groups: BTreeMap<(String, String), Vec<Junction>> = BTreeMap::new();
42
43    for junction in junctions {
44        groups
45            .entry((junction.sample_id.clone(), junction.gene_id.clone()))
46            .or_default()
47            .push(junction);
48    }
49
50    let mut results = Vec::with_capacity(groups.len());
51    for ((sample_id, gene_id), rows) in groups {
52        let graph = SpliceGraph::from_junctions(&rows).map_err(|error| {
53            format!("could not build graph for sample '{sample_id}', gene '{gene_id}': {error}")
54        })?;
55        let decomposition = decompose(&graph).map_err(|error| {
56            format!(
57                "Hodge decomposition failed for sample '{sample_id}', gene '{gene_id}': {error}; inspect this sample/gene graph and its junction counts"
58            )
59        })?;
60        results.push(GroupResult {
61            sample_id,
62            gene_id,
63            graph,
64            decomposition,
65        });
66    }
67
68    Ok(results)
69}