lightmotif 0.10.1

A lightweight platform-accelerated library for biological motif scanning using position weight matrices.
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Digital encoding for biological sequences using an alphabet.

use std::fmt::Debug;
use std::ops::Index;

use generic_array::ArrayLength;
use generic_array::GenericArray;
use typenum::consts::U21;
use typenum::consts::U5;
use typenum::marker_traits::NonZero;
use typenum::marker_traits::Unsigned;

use super::err::InvalidData;
use super::err::InvalidSymbol;
use super::seq::SymbolCount;

// --- Symbol ------------------------------------------------------------------

/// A symbol from a biological alphabet.
pub trait Symbol: Default + Sized + Copy + Eq {
    /// View this symbol as a zero-based index.
    fn as_index(&self) -> usize;
    /// View this symbol as a string character.
    fn as_char(&self) -> char {
        self.as_ascii() as char
    }
    /// Parse a string character into a symbol.
    fn from_char(c: char) -> Result<Self, InvalidSymbol> {
        if c.is_ascii() {
            Self::from_ascii(c as u8)
        } else {
            Err(InvalidSymbol(c))
        }
    }
    /// View this symbol as an ASCII charater.
    fn as_ascii(&self) -> u8;
    /// Parse an ASCII character into a symbol.
    fn from_ascii(c: u8) -> Result<Self, InvalidSymbol>;
}

/// A symbol that can be complemented.
pub trait ComplementableSymbol: Symbol {
    /// Get the complement of this symbol.
    fn complement(&self) -> Self;
}

// --- Alphabet ----------------------------------------------------------------

/// A biological alphabet with associated metadata.
pub trait Alphabet: Debug + Copy + Default + 'static {
    type Symbol: Symbol + Debug;
    type K: Unsigned + NonZero + ArrayLength + Debug;

    /// Get the default symbol for this alphabet.
    #[inline]
    fn default_symbol() -> Self::Symbol {
        Default::default()
    }

    /// Get all the symbols of this alphabet.
    fn symbols() -> &'static [Self::Symbol];

    /// Get a string with all symbols from this alphabet.
    fn as_str() -> &'static str;
}

// --- ComplementableAlphabet --------------------------------------------------

/// An alphabet that defines the complement operation.
pub trait ComplementableAlphabet: Alphabet {
    /// Get the complement of this symbol.
    fn complement(s: Self::Symbol) -> Self::Symbol;
}

impl<A: Alphabet> ComplementableAlphabet for A
where
    <A as Alphabet>::Symbol: ComplementableSymbol,
{
    #[inline]
    fn complement(s: Self::Symbol) -> Self::Symbol {
        s.complement()
    }
}

// --- DNA ---------------------------------------------------------------------

/// The standard DNA alphabet composed of 4 deoxyribonucleotides and a wildcard.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dna;

impl Alphabet for Dna {
    type Symbol = Nucleotide;
    type K = U5;

    #[inline]
    fn symbols() -> &'static [Nucleotide] {
        &[
            Nucleotide::A,
            Nucleotide::C,
            Nucleotide::T,
            Nucleotide::G,
            Nucleotide::N,
        ]
    }

    #[inline]
    fn as_str() -> &'static str {
        "ACTGN"
    }
}

/// A deoxyribonucleotide.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(u8)]
pub enum Nucleotide {
    /// Adenine.
    ///
    /// ![adenine.png](https://www.ebi.ac.uk/chebi/displayImage.do?defaultImage=true&imageIndex=0&chebiId=16708)
    A = 0,
    /// Cytosine.
    ///
    /// ![cytosine.png](https://www.ebi.ac.uk/chebi/displayImage.do?defaultImage=true&imageIndex=0&chebiId=16040)
    C = 1,
    /// Thymine.
    ///
    /// ![thymine.png](https://www.ebi.ac.uk/chebi/displayImage.do?defaultImage=true&imageIndex=0&chebiId=17821)
    T = 2,
    /// Guanine.
    ///
    /// ![guanine.png](https://www.ebi.ac.uk/chebi/displayImage.do?defaultImage=true&imageIndex=0&chebiId=16235)
    G = 3,
    /// Unknown base.
    #[default]
    N = 4,
}

impl From<Nucleotide> for char {
    #[inline]
    fn from(n: Nucleotide) -> char {
        n.as_char()
    }
}

impl Symbol for Nucleotide {
    #[inline]
    fn as_index(&self) -> usize {
        *self as usize
    }

    #[inline]
    fn as_ascii(&self) -> u8 {
        match self {
            Nucleotide::A => b'A',
            Nucleotide::C => b'C',
            Nucleotide::T => b'T',
            Nucleotide::G => b'G',
            Nucleotide::N => b'N',
        }
    }

    #[inline]
    fn from_ascii(c: u8) -> Result<Self, InvalidSymbol> {
        match c {
            b'A' => Ok(Nucleotide::A),
            b'C' => Ok(Nucleotide::C),
            b'T' => Ok(Nucleotide::T),
            b'G' => Ok(Nucleotide::G),
            b'N' => Ok(Nucleotide::N),
            _ => Err(InvalidSymbol(c as char)),
        }
    }
}

impl ComplementableSymbol for Nucleotide {
    #[inline]
    fn complement(&self) -> Self {
        match *self {
            Nucleotide::A => Nucleotide::T,
            Nucleotide::T => Nucleotide::A,
            Nucleotide::G => Nucleotide::C,
            Nucleotide::C => Nucleotide::G,
            Nucleotide::N => Nucleotide::N,
        }
    }
}

// --- Protein -----------------------------------------------------------------

/// The standard protein alphabet composed of 20 residues and a wildcard.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Protein;

impl Alphabet for Protein {
    type Symbol = AminoAcid;
    type K = U21;

    #[inline]
    fn symbols() -> &'static [AminoAcid] {
        &[
            AminoAcid::A,
            AminoAcid::C,
            AminoAcid::D,
            AminoAcid::E,
            AminoAcid::F,
            AminoAcid::G,
            AminoAcid::H,
            AminoAcid::I,
            AminoAcid::K,
            AminoAcid::L,
            AminoAcid::M,
            AminoAcid::N,
            AminoAcid::P,
            AminoAcid::Q,
            AminoAcid::R,
            AminoAcid::S,
            AminoAcid::T,
            AminoAcid::V,
            AminoAcid::W,
            AminoAcid::Y,
            AminoAcid::X,
        ]
    }

    #[inline]
    fn as_str() -> &'static str {
        "ACDEFGHIKLMNPQRSTVWYX"
    }
}

/// A proteinogenic amino acid.
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum AminoAcid {
    A = 0,
    C = 1,
    D = 2,
    E = 3,
    F = 4,
    G = 5,
    H = 6,
    I = 7,
    K = 8,
    L = 9,
    M = 10,
    N = 11,
    P = 12,
    Q = 13,
    R = 14,
    S = 15,
    T = 16,
    V = 17,
    W = 18,
    Y = 19,
    #[default]
    X = 20,
}

impl From<AminoAcid> for char {
    #[inline]
    fn from(aa: AminoAcid) -> char {
        aa.as_char()
    }
}

impl Symbol for AminoAcid {
    #[inline]
    fn as_index(&self) -> usize {
        *self as usize
    }

    #[inline]
    fn as_ascii(&self) -> u8 {
        match self {
            AminoAcid::A => b'A',
            AminoAcid::C => b'C',
            AminoAcid::D => b'D',
            AminoAcid::E => b'E',
            AminoAcid::F => b'F',
            AminoAcid::G => b'G',
            AminoAcid::H => b'H',
            AminoAcid::I => b'I',
            AminoAcid::K => b'K',
            AminoAcid::L => b'L',
            AminoAcid::M => b'M',
            AminoAcid::N => b'N',
            AminoAcid::P => b'P',
            AminoAcid::Q => b'Q',
            AminoAcid::R => b'R',
            AminoAcid::S => b'S',
            AminoAcid::T => b'T',
            AminoAcid::V => b'V',
            AminoAcid::W => b'W',
            AminoAcid::Y => b'Y',
            AminoAcid::X => b'X',
        }
    }

    #[inline]
    fn from_ascii(c: u8) -> Result<Self, InvalidSymbol> {
        match c {
            b'A' => Ok(AminoAcid::A),
            b'C' => Ok(AminoAcid::C),
            b'D' => Ok(AminoAcid::D),
            b'E' => Ok(AminoAcid::E),
            b'F' => Ok(AminoAcid::F),
            b'G' => Ok(AminoAcid::G),
            b'H' => Ok(AminoAcid::H),
            b'I' => Ok(AminoAcid::I),
            b'K' => Ok(AminoAcid::K),
            b'L' => Ok(AminoAcid::L),
            b'M' => Ok(AminoAcid::M),
            b'N' => Ok(AminoAcid::N),
            b'P' => Ok(AminoAcid::P),
            b'Q' => Ok(AminoAcid::Q),
            b'R' => Ok(AminoAcid::R),
            b'S' => Ok(AminoAcid::S),
            b'T' => Ok(AminoAcid::T),
            b'V' => Ok(AminoAcid::V),
            b'W' => Ok(AminoAcid::W),
            b'Y' => Ok(AminoAcid::Y),
            b'X' => Ok(AminoAcid::X),
            _ => Err(InvalidSymbol(c as char)),
        }
    }
}

// --- Background --------------------------------------------------------------

/// The background frequencies for an alphabet.
#[derive(Clone, Debug, PartialEq)]
pub struct Background<A: Alphabet> {
    frequencies: GenericArray<f32, A::K>,
    alphabet: std::marker::PhantomData<A>,
}

impl<A: Alphabet> Background<A> {
    /// Create a new background with the given frequencies.
    ///
    /// The array must contain valid frequencies, i.e. real numbers between
    /// zero and one that sum to one.
    pub fn new<F>(frequencies: F) -> Result<Self, InvalidData>
    where
        F: Into<GenericArray<f32, A::K>>,
    {
        let frequencies = frequencies.into();
        let mut sum = 0.0;
        for &f in frequencies.iter() {
            if !(0.0..=1.0).contains(&f) {
                return Err(InvalidData);
            }
            sum += f;
        }
        if sum != 1.0 {
            return Err(InvalidData);
        }
        Ok(Self {
            frequencies,
            alphabet: std::marker::PhantomData,
        })
    }

    /// Create a new background without checking frequencies.
    #[doc(hidden)]
    pub fn new_unchecked<F>(frequencies: F) -> Self
    where
        F: Into<GenericArray<f32, A::K>>,
    {
        Self {
            frequencies: frequencies.into(),
            alphabet: std::marker::PhantomData,
        }
    }

    /// Create a new background from the given symbol counts.
    ///
    /// # Example
    /// ```rust
    /// # use generic_array::GenericArray;
    /// # use lightmotif::abc::Background;
    /// # use lightmotif::abc::Dna;
    /// # use lightmotif::abc::Nucleotide::*;
    /// let counts = GenericArray::from([ 2, 2, 5, 1, 0 ]);
    /// let background = Background::<Dna>::from_counts(&counts).unwrap();
    /// assert_eq!(background[A], 0.2);
    /// assert_eq!(background[C], 0.2);
    /// assert_eq!(background[T], 0.5);
    /// assert_eq!(background[G], 0.1);
    /// ```
    pub fn from_counts(counts: &GenericArray<usize, A::K>) -> Result<Self, InvalidData> {
        let total = counts.iter().sum::<usize>();
        if total == 0 {
            return Err(InvalidData);
        }
        let mut frequencies = GenericArray::<f32, A::K>::default();
        for c in A::symbols() {
            frequencies[c.as_index()] = counts[c.as_index()] as f32 / total as f32;
        }
        Ok(Self {
            frequencies,
            alphabet: std::marker::PhantomData,
        })
    }

    /// Create a new background by counting symbol occurences in the given sequence.
    ///
    /// Pass `true` as the `unknown` argument to count the unknown symbol when
    /// computing frequencies, or `false` to only count frequencies of known
    /// symbols.
    ///
    /// # Example
    /// ```rust
    /// # use lightmotif::abc::Background;
    /// # use lightmotif::abc::Dna;
    /// # use lightmotif::abc::Nucleotide::*;
    /// let sequence = [ T, T, A, T, G, T, T, A, C, C ];
    /// let background = Background::<Dna>::from_sequence(&sequence[..], false).unwrap();
    /// assert_eq!(background[A], 0.2);
    /// assert_eq!(background[C], 0.2);
    /// assert_eq!(background[T], 0.5);
    /// assert_eq!(background[G], 0.1);
    /// ```
    pub fn from_sequence<S>(sequence: S, unknown: bool) -> Result<Self, InvalidData>
    where
        S: SymbolCount<A>,
    {
        let n = A::Symbol::default();
        let mut base_counts = GenericArray::<usize, A::K>::default();
        for &c in A::symbols() {
            if unknown || c != n {
                base_counts[c.as_index()] += sequence.count_symbol(c);
            }
        }
        Self::from_counts(&base_counts)
    }

    /// Create a new background by counting symbol occurences in the given sequences.
    ///
    /// Pass `true` as the `unknown` argument to count the unknown symbol when
    /// computing frequencies, or `false` to only count frequencies of known
    /// symbols.
    pub fn from_sequences<I>(sequences: I, unknown: bool) -> Result<Self, InvalidData>
    where
        I: IntoIterator,
        <I as IntoIterator>::Item: SymbolCount<A>,
    {
        let n = A::Symbol::default();
        let mut base_counts = GenericArray::<usize, A::K>::default();
        for seq in sequences {
            for &c in A::symbols() {
                if unknown || c != n {
                    base_counts[c.as_index()] += seq.count_symbol(c);
                }
            }
        }
        Self::from_counts(&base_counts)
    }

    /// Create a new background with uniform frequencies.
    ///
    /// The non-default symbols from the alphabet `A` will be initialized
    /// with a frequency of `1/(K-1)`, while the default symbol will remain
    /// with a zero frequency.
    ///
    /// # Note
    /// The `Default` implementation for `Background` uses uniform frequencies.
    ///
    /// # Example
    /// ```
    /// # use lightmotif::abc::*;
    /// let bg = Background::<Dna>::uniform();
    /// assert_eq!(bg.frequencies(), &[0.25, 0.25, 0.25, 0.25, 0.0]);
    /// ```
    pub fn uniform() -> Self {
        let frequencies = (0..A::K::USIZE)
            .map(|i| {
                if i != A::Symbol::default().as_index() {
                    1.0 / ((A::K::USIZE - 1) as f32)
                } else {
                    0.0
                }
            })
            .collect();
        Self {
            frequencies,
            alphabet: std::marker::PhantomData,
        }
    }

    /// A reference to the raw background frequencies.
    #[inline]
    pub fn frequencies(&self) -> &[f32] {
        &self.frequencies
    }
}

impl<A: Alphabet> AsRef<[f32]> for Background<A> {
    #[inline]
    fn as_ref(&self) -> &[f32] {
        self.frequencies()
    }
}

impl<A: Alphabet> AsRef<GenericArray<f32, A::K>> for Background<A> {
    #[inline]
    fn as_ref(&self) -> &GenericArray<f32, A::K> {
        &self.frequencies
    }
}

impl<A: Alphabet> Default for Background<A> {
    #[inline]
    fn default() -> Self {
        Self::uniform()
    }
}

impl<A: Alphabet> Index<A::Symbol> for Background<A> {
    type Output = f32;
    #[inline]
    fn index(&self, index: A::Symbol) -> &Self::Output {
        &self.frequencies()[index.as_index()]
    }
}

// --- Pseudocounts ------------------------------------------------------------

/// A structure for storing the pseudocounts over an alphabet.
#[derive(Clone, Debug, PartialEq)]
pub struct Pseudocounts<A: Alphabet> {
    counts: GenericArray<f32, A::K>,
    alphabet: std::marker::PhantomData<A>,
}

impl<A: Alphabet> Pseudocounts<A> {
    #[inline]
    pub fn counts(&self) -> &GenericArray<f32, A::K> {
        &self.counts
    }
}

impl<A: Alphabet> Default for Pseudocounts<A> {
    #[inline]
    fn default() -> Self {
        Self::from(0.0)
    }
}

impl<A: Alphabet> From<GenericArray<f32, A::K>> for Pseudocounts<A> {
    #[inline]
    fn from(counts: GenericArray<f32, A::K>) -> Self {
        Self {
            alphabet: std::marker::PhantomData,
            counts,
        }
    }
}

impl<A: Alphabet> From<f32> for Pseudocounts<A> {
    fn from(count: f32) -> Self {
        let counts = (0..A::K::USIZE)
            .map(|i| {
                if i != A::Symbol::default().as_index() {
                    count
                } else {
                    0.0
                }
            })
            .collect();
        Self {
            counts,
            alphabet: std::marker::PhantomData,
        }
    }
}

impl<A: Alphabet> AsRef<[f32]> for Pseudocounts<A> {
    #[inline]
    fn as_ref(&self) -> &[f32] {
        &self.counts
    }
}

impl<A: Alphabet> AsMut<[f32]> for Pseudocounts<A> {
    #[inline]
    fn as_mut(&mut self) -> &mut [f32] {
        &mut self.counts
    }
}

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

    #[test]
    fn background_new() {
        assert!(Background::<Dna>::new([0.3, 0.2, 0.2, 0.3, 0.0]).is_ok());
        assert!(Background::<Dna>::new([0.1, 0.1, 0.1, 0.1, 0.0]).is_err());
    }
}