bixverse-rs 0.6.3

A Rust library with various utilities for computational biology and single cell.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Specific R wrappers over structures or functions within the `core` module

use extendr_api::*;
use std::collections::{BTreeMap, HashMap};

use crate::core::mat_struct::NamedMatrix;
use crate::core::math::pca_missing::{BpcaParams, PpcaParams};
use crate::core::synthetic_data::{
    BulkDataGenerator, DropoutStrategy, HubModularConfig, LoadingLogNormal, ModularConfig,
    NonGaussianFactorConfig, NonNegativeFactorConfig, SparsityParams, SyntheticRnaSeqParams,
    parse_dropout_strategy,
};
use crate::prelude::*;

///////////////////////////////
// NamedMatrix from R matrix //
///////////////////////////////

impl<'a, T> NamedMatrix<'a, T>
where
    T: BixverseFloat,
{
    /// Generate a NamedMatrix from an RMatrix.
    pub fn from_r_matrix(x: &'a RMatrix<f64>) -> NamedMatrix<'a, f64> {
        let col_names: BTreeMap<String, usize> = x
            .get_colnames()
            .unwrap()
            .iter()
            .enumerate()
            .map(|(i, s)| (s.to_string(), i))
            .collect();
        let row_names: BTreeMap<String, usize> = x
            .get_rownames()
            .unwrap()
            .iter()
            .enumerate()
            .map(|(i, s)| (s.to_string(), i))
            .collect();
        let mat = r_matrix_to_faer(x);
        NamedMatrix {
            col_names,
            row_names,
            values: mat,
        }
    }
}

///////////////////////////////////
// Synthetic data - loading base //
///////////////////////////////////

impl<T: BixverseFloat> LoadingLogNormal<T> {
    /// Read the LogNormal loading sub-config from a flat R list.
    ///
    /// Keys: `loading_mu`, `loading_sigma`, `hub_percentile`. Missing keys
    /// fall back to [`LoadingLogNormal::default()`].
    ///
    /// ### Params
    ///
    /// * `params` - Parsed R list contents (already flattened to a HashMap).
    ///
    /// ### Returns
    ///
    /// A [`LoadingLogNormal`] populated with the provided or default values.
    pub fn from_r_map(params: &HashMap<&str, Robj>) -> LoadingLogNormal<T> {
        let defaults: LoadingLogNormal<T> = LoadingLogNormal::default();

        let mu = params
            .get("loading_mu")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.mu);

        let sigma = params
            .get("loading_sigma")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.sigma);

        let hub_percentile = params
            .get("hub_percentile")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.hub_percentile);

        LoadingLogNormal {
            mu,
            sigma,
            hub_percentile,
        }
    }
}

/////////////////////////////////
// Synthetic data - generators //
/////////////////////////////////

impl<T: BixverseFloat> ModularConfig<T> {
    /// Read [`ModularConfig`] from an R list.
    ///
    /// Keys: `factor_std`. Missing keys fall back to [`ModularConfig::default`].
    ///
    /// ### Params
    ///
    /// * `r_list` - The R list containing the parameters.
    ///
    /// ### Returns
    ///
    /// The [`ModularConfig`] populated with provided or default values.
    pub fn from_r_list(r_list: List) -> Result<ModularConfig<T>> {
        let params: HashMap<&str, Robj> = r_list_to_map(r_list)?;
        let defaults: ModularConfig<T> = ModularConfig::default();

        let factor_std = params
            .get("factor_std")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.factor_std);

        Ok(ModularConfig { factor_std })
    }
}

impl<T: BixverseFloat> HubModularConfig<T> {
    /// Read [`HubModularConfig`] from an R list.
    ///
    /// Keys: `factor_std`, `loading_mu`, `loading_sigma`, `hub_percentile`.
    /// Missing keys fall back to [`HubModularConfig::default`].
    ///
    /// ### Params
    ///
    /// * `r_list` - The R list containing the parameters.
    ///
    /// ### Returns
    ///
    /// The [`HubModularConfig`] populated with provided or default values.
    pub fn from_r_list(r_list: List) -> Result<HubModularConfig<T>> {
        let params: HashMap<&str, Robj> = r_list_to_map(r_list)?;
        let defaults: HubModularConfig<T> = HubModularConfig::default();

        let factor_std = params
            .get("factor_std")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.factor_std);

        let loading = LoadingLogNormal::<T>::from_r_map(&params);

        Ok(HubModularConfig {
            factor_std,
            loading,
        })
    }
}

impl<T: BixverseFloat> NonNegativeFactorConfig<T> {
    /// Read [`NonNegativeFactorConfig`] from an R list.
    ///
    /// Keys: `factor_shape`, `factor_scale`, `loading_mu`, `loading_sigma`,
    /// `hub_percentile`. Missing keys fall back to
    /// [`NonNegativeFactorConfig::default`].
    ///
    /// ### Params
    ///
    /// * `r_list` - The R list containing the parameters.
    ///
    /// ### Returns
    ///
    /// The [`NonNegativeFactorConfig`] populated with provided or default values.
    pub fn from_r_list(r_list: List) -> Result<NonNegativeFactorConfig<T>> {
        let params: HashMap<&str, Robj> = r_list_to_map(r_list)?;
        let defaults: NonNegativeFactorConfig<T> = NonNegativeFactorConfig::default();

        let factor_shape = params
            .get("factor_shape")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.factor_shape);

        let factor_scale = params
            .get("factor_scale")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.factor_scale);

        let loading = LoadingLogNormal::<T>::from_r_map(&params);

        Ok(NonNegativeFactorConfig {
            factor_shape,
            factor_scale,
            loading,
        })
    }
}

impl<T: BixverseFloat> NonGaussianFactorConfig<T> {
    /// Read [`NonGaussianFactorConfig`] from an R list.
    ///
    /// Keys: `factor_scale`, `loading_mu`, `loading_sigma`, `hub_percentile`.
    /// Missing keys fall back to [`NonGaussianFactorConfig::default`].
    ///
    /// ### Params
    ///
    /// * `r_list` - The R list containing the parameters.
    ///
    /// ### Returns
    ///
    /// The [`NonGaussianFactorConfig`] populated with provided or default values.
    pub fn from_r_list(r_list: List) -> Result<NonGaussianFactorConfig<T>> {
        let params: HashMap<&str, Robj> = r_list_to_map(r_list)?;
        let defaults: NonGaussianFactorConfig<T> = NonGaussianFactorConfig::default();

        let factor_scale = params
            .get("factor_scale")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.factor_scale);

        let loading = LoadingLogNormal::<T>::from_r_map(&params);

        Ok(NonGaussianFactorConfig {
            factor_scale,
            loading,
        })
    }
}

//////////////////////////////////////
// Synthetic data - top-level params //
//////////////////////////////////////

impl<T: BixverseFloat> SyntheticRnaSeqParams<T> {
    /// Generate [`SyntheticRnaSeqParams`] from an R list.
    ///
    /// The list is expected to contain all top-level synthetic-data parameters
    /// plus the fields required by the chosen generator variant. Generator
    /// configuration parameters are read from the same flat list, following
    /// the SCENIC convention.
    ///
    /// The `generator` string picks the variant: `"modular"`, `"hub_modular"`,
    /// `"non_negative_factor"` (aliases: `"nonnegative"`, `"nmf"`), or
    /// `"non_gaussian_factor"` (aliases: `"nongaussian"`, `"ica"`). Missing
    /// values fall back to [`SyntheticRnaSeqParams::default`].
    ///
    /// ### Params
    ///
    /// * `r_list` - The R list with the synthetic-data parameters.
    ///
    /// ### Returns
    ///
    /// The [`SyntheticRnaSeqParams`] with all parameters set.
    pub fn from_r_list(r_list: List) -> Result<SyntheticRnaSeqParams<T>> {
        let defaults: SyntheticRnaSeqParams<T> = SyntheticRnaSeqParams::default();
        let params: HashMap<&str, Robj> = r_list_to_map(r_list.clone())?;

        let num_samples = params
            .get("num_samples")
            .and_then(|v| v.as_integer())
            .map(|v| v as usize)
            .unwrap_or(defaults.num_samples);

        let num_genes = params
            .get("num_genes")
            .and_then(|v| v.as_integer())
            .map(|v| v as usize)
            .unwrap_or(defaults.num_genes);

        let seed = params
            .get("seed")
            .and_then(|v| v.as_integer())
            .map(|v| v as u64)
            .unwrap_or(defaults.seed);

        let module_sizes = params
            .get("module_sizes")
            .and_then(|v| v.as_integer_slice())
            .map(|s| s.iter().map(|&x| x as usize).collect::<Vec<_>>())
            .unwrap_or(defaults.module_sizes);

        let mean_exp_gamma_shape = params
            .get("mean_exp_gamma_shape")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.mean_exp_gamma_shape);

        let mean_exp_gamma_scale = params
            .get("mean_exp_gamma_scale")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.mean_exp_gamma_scale);

        let disp_intercept = params
            .get("disp_intercept")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.disp_intercept);

        let disp_slope = params
            .get("disp_slope")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.disp_slope);

        let noise_std = params
            .get("noise_std")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.noise_std);

        let generator_kind = params
            .get("generator")
            .and_then(|v| v.as_str())
            .unwrap_or("hub_modular");

        let generator = match generator_kind.to_lowercase().as_str() {
            "modular" => BulkDataGenerator::Modular(ModularConfig::from_r_list(r_list.clone())?),
            "non_negative_factor" | "nonnegative" | "nmf" => BulkDataGenerator::NonNegativeFactor(
                NonNegativeFactorConfig::from_r_list(r_list.clone())?,
            ),
            "non_gaussian_factor" | "nongaussian" | "ica" => BulkDataGenerator::NonGaussianFactor(
                NonGaussianFactorConfig::from_r_list(r_list.clone())?,
            ),
            _ => BulkDataGenerator::HubModular(HubModularConfig::from_r_list(r_list.clone())?),
        };

        Ok(SyntheticRnaSeqParams {
            num_samples,
            num_genes,
            seed,
            module_sizes,
            generator,
            mean_exp_gamma_shape,
            mean_exp_gamma_scale,
            disp_intercept,
            disp_slope,
            noise_std,
        })
    }
}

////////////////////////////////////////
// Synthetic data - sparsity / dropout //
////////////////////////////////////////

impl<T: BixverseFloat> SparsityParams<T> {
    /// Generate [`SparsityParams`] from an R list.
    ///
    /// Keys: `strategy` (currently only `"seq_depth"` /
    /// `"sequencing_depth"` — falls back to
    /// [`DropoutStrategy::SequencingDepth`]), `target_library_size`,
    /// `capture_efficiency_sigma`, `seed`. Missing values fall back to
    /// [`SparsityParams::default`].
    ///
    /// ### Params
    ///
    /// * `r_list` - The R list with the sparsity parameters.
    ///
    /// ### Returns
    ///
    /// The [`SparsityParams`] with all parameters set.
    pub fn from_r_list(r_list: List) -> Result<SparsityParams<T>> {
        let defaults: SparsityParams<T> = SparsityParams::default();
        let params: HashMap<&str, Robj> = r_list_to_map(r_list)?;

        let strategy = params
            .get("strategy")
            .and_then(|v| v.as_str())
            .and_then(parse_dropout_strategy)
            .unwrap_or(DropoutStrategy::SequencingDepth);

        let target_library_size = params
            .get("target_library_size")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.target_library_size);

        let capture_efficiency_sigma = params
            .get("capture_efficiency_sigma")
            .and_then(|v| v.as_real())
            .map(|v| T::from_f64(v).unwrap())
            .unwrap_or(defaults.capture_efficiency_sigma);

        let seed = params
            .get("seed")
            .and_then(|v| v.as_integer())
            .map(|v| v as u64)
            .unwrap_or(defaults.seed);

        Ok(SparsityParams {
            strategy,
            target_library_size,
            capture_efficiency_sigma,
            seed,
        })
    }
}

/////////////////////////
// Lanczos eigensolver //
/////////////////////////

impl LanczosParams {
    /// Generate LanczosParams from a flattened R list
    ///
    /// Takes the already-flattened map rather than a `List`, so it can be nested
    /// inside the parameter block of whichever method owns the eigensolver.
    /// Anything missing falls back to [LanczosParams::default], so the R and
    /// Rust defaults cannot drift apart.
    ///
    /// ### Params
    ///
    /// * `params` - Parsed R list contents, already flattened to a HashMap.
    ///
    /// ### Returns
    ///
    /// A [LanczosParams] populated with the provided or default values, or an
    /// error when a count is present but unusable.
    pub fn from_r_list(params: &HashMap<&str, Robj>) -> Result<LanczosParams> {
        let defaults = LanczosParams::default();

        // `None` derives the basis from the requested component count.
        // `r_list_count` is load bearing here: `NA_integer_` is `i32::MIN` and a
        // plain `as usize` turns it, or any negative, into ~1.8e19, which for
        // the restart budget is an unbounded loop rather than an error.
        let basis_size = r_list_count(params, "lanczos_basis_size")?;

        let max_restarts =
            r_list_count(params, "lanczos_max_restarts")?.unwrap_or(defaults.max_restarts);

        let tol = params
            .get("lanczos_tol")
            .and_then(|v| v.as_real())
            .unwrap_or(defaults.tol);

        Ok(LanczosParams {
            basis_size,
            max_restarts,
            tol,
        })
    }
}

/////////////////////////////
// PCA with missing values //
/////////////////////////////

impl PpcaParams {
    /// Generate PpcaParams from an R list
    ///
    /// Keys: `n_pcs`, `max_iter`, `tol`, `seed`, `centre`, `scale`. Missing
    /// keys fall back to [PpcaParams::default].
    ///
    /// ### Params
    ///
    /// * `r_list` - The list with the PPCA parameters.
    ///
    /// ### Returns
    ///
    /// The [PpcaParams], or an error when a count is present but unusable.
    pub fn from_r_list(r_list: List) -> Result<Self> {
        let params = r_list_to_map(r_list)?;
        let defaults = Self::default();

        Ok(Self {
            n_pcs: r_list_count(&params, "n_pcs")?.unwrap_or(defaults.n_pcs),
            max_iter: r_list_count(&params, "max_iter")?.unwrap_or(defaults.max_iter),
            tol: params
                .get("tol")
                .and_then(|v| v.as_real())
                .unwrap_or(defaults.tol),
            seed: r_list_count_allow_zero(&params, "seed")?
                .map(|v| v as u64)
                .unwrap_or(defaults.seed),
            centre: params
                .get("centre")
                .and_then(|v| v.as_bool())
                .unwrap_or(defaults.centre),
            scale: params
                .get("scale")
                .and_then(|v| v.as_bool())
                .unwrap_or(defaults.scale),
        })
    }
}

impl BpcaParams {
    /// Generate BpcaParams from an R list
    ///
    /// Keys: `n_pcs`, `max_iter`, `tol`, `centre`, `scale`. Missing keys fall
    /// back to [BpcaParams::default].
    ///
    /// ### Params
    ///
    /// * `r_list` - The list with the BPCA parameters.
    ///
    /// ### Returns
    ///
    /// The [BpcaParams], or an error when a count is present but unusable.
    pub fn from_r_list(r_list: List) -> Result<Self> {
        let params = r_list_to_map(r_list)?;
        let defaults = Self::default();

        Ok(Self {
            n_pcs: r_list_count(&params, "n_pcs")?.unwrap_or(defaults.n_pcs),
            max_iter: r_list_count(&params, "max_iter")?.unwrap_or(defaults.max_iter),
            tol: params
                .get("tol")
                .and_then(|v| v.as_real())
                .unwrap_or(defaults.tol),
            centre: params
                .get("centre")
                .and_then(|v| v.as_bool())
                .unwrap_or(defaults.centre),
            scale: params
                .get("scale")
                .and_then(|v| v.as_bool())
                .unwrap_or(defaults.scale),
        })
    }
}