edge-rs 0.3.1

Negative binomial differential expression for bulk and single-cell RNA-seq: the edgeR numerical stack and NEBULA mixed models.
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
//! Single-group negative binomial fits.
//!
//! edgeR's `mglmOneGroup`: one coefficient per gene, fitted by Fisher scoring.
//! Score and information collapse to sums over samples, so each Newton step is
//! a division. `aveLogCPM` calls this for every gene and `mglmOneWay` once per
//! group, so it sits under both `estimateDisp` and the fast path of `glmFit`.

use rayon::prelude::*;

use crate::core::expression::check_dispersion;
use crate::glm::{EMPTY_GENE_COEF, ETA_CLAMP, MIN_POSITIVE};
use crate::prelude::*;

////////////
// Consts //
////////////

/// Information below which a step is treated as undefined.
const MIN_INFORMATION: f64 = 1e-300;

////////////////////
// OneGroupParams //
////////////////////

/// Tuning knobs for [`mglm_one_group`].
#[derive(Clone, Copy, Debug)]
pub struct OneGroupParams {
    /// Iteration budget per gene.
    pub max_iter: usize,
    /// Convergence tolerance on the coefficient step.
    ///
    /// A gene stops when the step is smaller than `tol * (|coef| + 0.1)`.
    /// Tighter than the Levenberg fit's, as a step here is cheap.
    pub tol: f64,
}

impl Default for OneGroupParams {
    /// edgeR's defaults.
    fn default() -> Self {
        Self {
            max_iter: 50,
            tol: 1e-10,
        }
    }
}

///////////////
// Front end //
///////////////

/// Fits an intercept-only negative binomial GLM to every gene.
///
/// ### Params
///
/// * `counts` - Counts, row-major `n_genes * n_samples`
/// * `n_genes` - Number of genes
/// * `n_samples` - Number of samples
/// * `dispersion` - Dispersion, recycled over genes and samples
/// * `offset` - Log-scale offsets, recycled over genes and samples
/// * `weights` - Optional observation weights
/// * `coef_start` - Optional starting coefficient per gene. A non-finite entry
///   asks for that gene to be initialised from the data, matching edgeR's use
///   of `NA` as a per-gene opt-out.
/// * `params` - Tuning knobs, or [`OneGroupParams::default`]
///
/// ### Returns
///
/// One coefficient per gene on the log scale, or [`EdgeErrors`] if the shapes
/// disagree.
#[allow(clippy::too_many_arguments)]
pub fn mglm_one_group<T: EdgeFloat>(
    counts: &[T],
    n_genes: usize,
    n_samples: usize,
    dispersion: &Recycled<f64>,
    offset: &Recycled<f64>,
    weights: Option<&Recycled<f64>>,
    coef_start: Option<&[f64]>,
    params: Option<OneGroupParams>,
) -> Result<Vec<f64>, EdgeErrors> {
    let params = params.unwrap_or_default();

    if n_genes == 0 || n_samples == 0 {
        return Err(EdgeErrors::EmptyCounts { n_genes, n_samples });
    }
    if counts.len() != n_genes * n_samples {
        return Err(EdgeErrors::LengthMismatch {
            name: "counts",
            expected: n_genes * n_samples,
            got: counts.len(),
        });
    }
    dispersion.validate(n_genes, n_samples)?;
    check_dispersion(dispersion)?;
    offset.validate(n_genes, n_samples)?;
    if let Some(w) = weights {
        w.validate(n_genes, n_samples)?;
    }
    if let Some(start) = coef_start
        && start.len() != n_genes
    {
        return Err(EdgeErrors::LengthMismatch {
            name: "coef_start",
            expected: n_genes,
            got: start.len(),
        });
    }

    let mut coefficients = vec![0.0; n_genes];
    let all_samples: Vec<usize> = (0..n_samples).collect();

    coefficients
        .par_iter_mut()
        .enumerate()
        .for_each(|(gene, coef)| {
            let disp_row = dispersion.row(gene, n_samples);
            let offset_row = offset.row(gene, n_samples);
            let weight_row = weights.map(|w| w.row(gene, n_samples));
            let y = &counts[gene * n_samples..(gene + 1) * n_samples];

            let start = coef_start
                .map(|s| s[gene])
                .filter(|v| v.is_finite())
                .unwrap_or_else(|| initial_coefficient(y, &all_samples, offset_row, weight_row));

            *coef = fit_one_gene(
                y,
                &all_samples,
                disp_row,
                offset_row,
                weight_row,
                start,
                &params,
            );
        });

    Ok(coefficients)
}

/// Starting coefficient from the weighted total count over the weighted total
/// library size.
///
/// ### Params
///
/// * `y` - Counts for this gene, indexed by the full sample range
/// * `samples` - Which samples take part. [`mglm_one_way`] passes one group's
///   columns; [`mglm_one_group`] passes all of them.
/// * `offset` - Offset row
/// * `weights` - Optional weight row
///
/// ### Returns
///
/// The log of the crude rate, or [`EMPTY_GENE_COEF`] if the gene or the library
/// is empty.
///
/// [`mglm_one_way`]: crate::glm::one_way::mglm_one_way
pub(crate) fn initial_coefficient<T: EdgeFloat>(
    y: &[T],
    samples: &[usize],
    offset: RecycledRow<'_, f64>,
    weights: Option<RecycledRow<'_, f64>>,
) -> f64 {
    let mut total_counts = 0.0;
    let mut total_library = 0.0;
    for &j in samples {
        let w = weights.map_or(1.0, |w| w.get(j));
        total_counts += w * y[j].to_f64().unwrap_or(0.0);
        total_library += w * offset.get(j).exp();
    }
    if total_counts > 0.0 && total_library > 0.0 {
        (total_counts / total_library).ln()
    } else {
        EMPTY_GENE_COEF
    }
}

/// Runs Fisher scoring for one gene.
///
/// The score is `sum_j w_j (y_j - mu_j) / (1 + phi_j mu_j)` and the information
/// is `sum_j w_j mu_j / (1 + phi_j mu_j)`, both accumulated in one pass since
/// they share the denominator.
///
/// ### Params
///
/// * `y` - Counts for this gene, indexed by the full sample range
/// * `samples` - Which samples take part
/// * `dispersion` - Dispersion row
/// * `offset` - Offset row
/// * `weights` - Optional weight row
/// * `start` - Starting coefficient
/// * `params` - Tuning knobs
///
/// ### Returns
///
/// The fitted coefficient.
pub(crate) fn fit_one_gene<T: EdgeFloat>(
    y: &[T],
    samples: &[usize],
    dispersion: RecycledRow<'_, f64>,
    offset: RecycledRow<'_, f64>,
    weights: Option<RecycledRow<'_, f64>>,
    start: f64,
    params: &OneGroupParams,
) -> f64 {
    let mut beta = start;

    for _ in 0..params.max_iter {
        let mut score = 0.0;
        let mut information = 0.0;

        for &j in samples {
            let eta = (beta + offset.get(j)).clamp(-ETA_CLAMP, ETA_CLAMP);
            let mu = eta.exp().max(MIN_POSITIVE);
            let denom = 1.0 + dispersion.get(j) * mu;
            let w = weights.map_or(1.0, |w| w.get(j));
            score += w * (y[j].to_f64().unwrap_or(0.0) - mu) / denom;
            information += w * mu / denom;
        }

        if information <= MIN_INFORMATION {
            break;
        }

        let step = score / information;
        beta += step;
        if step.abs() < params.tol * (beta.abs() + 0.1) {
            break;
        }
    }

    beta
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;

    /// Zero dispersion, no offset: the Poisson MLE, the log sample mean.
    #[test]
    fn test_poisson_fit_is_the_log_sample_mean() {
        let counts = vec![10.0, 12.0, 14.0, 5.0, 5.0, 5.0];
        let coef = mglm_one_group(
            &counts,
            2,
            3,
            &Recycled::scalar(0.0),
            &Recycled::scalar(0.0),
            None,
            None,
            None,
        )
        .unwrap();
        assert_relative_eq!(coef[0], 12.0_f64.ln(), max_relative = 1e-10);
        assert_relative_eq!(coef[1], 5.0_f64.ln(), max_relative = 1e-10);
    }

    /// With a shared offset the dispersion cancels out of the score.
    #[test]
    fn test_dispersion_does_not_move_a_balanced_fit() {
        let counts = vec![10.0, 12.0, 14.0];
        for phi in [0.0, 0.01, 0.5, 10.0] {
            let coef = mglm_one_group(
                &counts,
                1,
                3,
                &Recycled::scalar(phi),
                &Recycled::scalar(0.0),
                None,
                None,
                None,
            )
            .unwrap();
            assert_relative_eq!(coef[0], 12.0_f64.ln(), max_relative = 1e-8);
        }
    }

    /// Parity with edgeR 4.8.2:
    /// ```r
    /// y <- matrix(c(10,12,11,40,44,38, 50,48,52,49,51,50, 2,0,5,1,3,0),
    ///             nrow = 3, byrow = TRUE)
    /// mglmOneGroup(y, dispersion = 0.1, offset = log(c(1e6,1.2e6,0.9e6,1.1e6,1e6,1.3e6)))
    /// ```
    #[test]
    fn test_matches_edger_mglm_one_group() {
        let counts = vec![
            10.0, 12.0, 11.0, 40.0, 44.0, 38.0, //
            50.0, 48.0, 52.0, 49.0, 51.0, 50.0, //
            2.0, 0.0, 5.0, 1.0, 3.0, 0.0, //
        ];
        let libraries: [f64; 6] = [1e6, 1.2e6, 0.9e6, 1.1e6, 1e6, 1.3e6];
        let offset = Recycled::by_sample(libraries.iter().map(|v| v.ln()).collect());

        let coef = mglm_one_group(
            &counts,
            3,
            6,
            &Recycled::scalar(0.1),
            &offset,
            None,
            None,
            None,
        )
        .unwrap();

        let expected = [
            -10.649_844_006_464_7,
            -9.968_996_644_248_97,
            -13.269_736_410_245_08,
        ];
        for (got, want) in coef.iter().zip(expected.iter()) {
            assert_relative_eq!(got, want, epsilon = 1e-9, max_relative = 1e-8);
        }
    }

    #[test]
    fn test_offsets_shift_the_coefficient() {
        let counts = vec![10.0, 12.0, 14.0];
        let plain = mglm_one_group(
            &counts,
            1,
            3,
            &Recycled::scalar(0.1),
            &Recycled::scalar(0.0),
            None,
            None,
            None,
        )
        .unwrap();
        let shifted = mglm_one_group(
            &counts,
            1,
            3,
            &Recycled::scalar(0.1),
            &Recycled::scalar(3.0),
            None,
            None,
            None,
        )
        .unwrap();
        assert_relative_eq!(shifted[0], plain[0] - 3.0, max_relative = 1e-8);
    }

    #[test]
    fn test_zero_weight_removes_a_sample() {
        let counts = vec![10.0, 12.0, 1000.0];
        let weights = Recycled::by_sample(vec![1.0, 1.0, 0.0]);
        let coef = mglm_one_group(
            &counts,
            1,
            3,
            &Recycled::scalar(0.0),
            &Recycled::scalar(0.0),
            Some(&weights),
            None,
            None,
        )
        .unwrap();
        assert_relative_eq!(coef[0], 11.0_f64.ln(), max_relative = 1e-10);
    }

    #[test]
    fn test_empty_gene_gets_the_finite_fallback() {
        let counts = vec![0.0, 0.0, 0.0];
        let coef = mglm_one_group(
            &counts,
            1,
            3,
            &Recycled::scalar(0.1),
            &Recycled::scalar(0.0),
            None,
            None,
            None,
        )
        .unwrap();
        assert!(coef[0].is_finite());
        assert!(coef[0] <= EMPTY_GENE_COEF);
    }

    /// A non-finite start initialises that gene from the data (edgeR's per-gene `NA`).
    #[test]
    fn test_non_finite_start_falls_back_to_the_data() {
        let counts = vec![10.0, 12.0, 14.0, 5.0, 5.0, 5.0];
        let start = vec![f64::NAN, 5.0_f64.ln()];
        let coef = mglm_one_group(
            &counts,
            2,
            3,
            &Recycled::scalar(0.0),
            &Recycled::scalar(0.0),
            None,
            Some(&start),
            None,
        )
        .unwrap();
        assert_relative_eq!(coef[0], 12.0_f64.ln(), max_relative = 1e-10);
        assert_relative_eq!(coef[1], 5.0_f64.ln(), max_relative = 1e-10);
    }

    #[test]
    fn test_rejects_a_shape_mismatch() {
        let err = mglm_one_group(
            &[1.0_f64; 5],
            2,
            3,
            &Recycled::scalar(0.0),
            &Recycled::scalar(0.0),
            None,
            None,
            None,
        )
        .unwrap_err();
        assert!(matches!(
            err,
            EdgeErrors::LengthMismatch { name: "counts", .. }
        ));
    }
}