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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Concrete implementations of the sequence scoring pipeline.

use std::ops::AddAssign;
use std::ops::Range;

use crate::abc::Alphabet;
use crate::abc::Dna;
use crate::abc::Symbol;
use crate::dense::DenseMatrix;
use crate::dense::MatrixCoordinates;
use crate::dense::MatrixElement;
use crate::err::InvalidSymbol;
use crate::err::UnsupportedBackend;
use crate::num::MultipleOf;
use crate::num::PositiveLength;
use crate::num::U16;
use crate::scores::StripedScores;
use crate::seq::EncodedSequence;
use crate::seq::StripedSequence;

use self::dispatch::Dispatch;
use self::platform::Avx2;
use self::platform::Backend;
use self::platform::Generic;
use self::platform::Neon;
use self::platform::Sse2;

pub mod dispatch;
pub mod platform;

// --- Traits ------------------------------------------------------------------

/// Used for encoding a sequence into rank-based encoding.
pub trait Encode<A: Alphabet> {
    /// Encode the given sequence into a vector of symbols.
    #[allow(clippy::uninit_vec)]
    fn encode_raw<S: AsRef<[u8]>>(&self, seq: S) -> Result<Vec<A::Symbol>, InvalidSymbol> {
        let s = seq.as_ref();
        let mut buffer = Vec::with_capacity(s.len());
        unsafe { buffer.set_len(s.len()) };
        match self.encode_into(s, &mut buffer) {
            Ok(_) => Ok(buffer),
            Err(e) => Err(e),
        }
    }

    /// Encode the given sequence into an `EncodedSequence`.
    fn encode<S: AsRef<[u8]>>(&self, seq: S) -> Result<EncodedSequence<A>, InvalidSymbol> {
        self.encode_raw(seq).map(EncodedSequence::new)
    }

    /// Encode the given sequence into a buffer of symbols.
    ///
    /// The destination buffer is expected to be large enough to store the
    /// entire sequence.
    fn encode_into<S: AsRef<[u8]>>(
        &self,
        seq: S,
        dst: &mut [A::Symbol],
    ) -> Result<(), InvalidSymbol> {
        assert_eq!(seq.as_ref().len(), dst.len());
        for (i, c) in seq.as_ref().iter().enumerate() {
            dst[i] = A::Symbol::from_ascii(*c)?;
        }
        Ok(())
    }
}

/// Used computing sequence scores with a PSSM.
pub trait Score<T: MatrixElement + AddAssign, A: Alphabet, C: PositiveLength> {
    /// Compute the PSSM scores into the given striped score matrix.
    fn score_rows_into<S, M>(
        &self,
        pssm: M,
        seq: S,
        rows: Range<usize>,
        scores: &mut StripedScores<T, C>,
    ) where
        S: AsRef<StripedSequence<A, C>>,
        M: AsRef<DenseMatrix<T, A::K>>,
    {
        let seq = seq.as_ref();
        let pssm = pssm.as_ref();

        if seq.len() < pssm.rows() || rows.is_empty() {
            scores.resize(0, 0);
            return;
        }

        // FIXME?
        scores.resize(rows.len(), (seq.len() + 1).saturating_sub(pssm.rows()));

        let result = scores.matrix_mut();
        let matrix = pssm;

        for (res_row, seq_row) in rows.enumerate() {
            for col in 0..C::USIZE {
                let mut score = T::default();
                for (j, pssm_row) in matrix.iter().enumerate() {
                    let symbol = seq.matrix()[seq_row + j][col];
                    score += pssm_row[symbol.as_index()];
                }
                result[res_row][col] = score;
            }
        }
    }

    /// Compute the PSSM scores into the given striped score matrix.
    fn score_into<S, M>(&self, pssm: M, seq: S, scores: &mut StripedScores<T, C>)
    where
        S: AsRef<StripedSequence<A, C>>,
        M: AsRef<DenseMatrix<T, A::K>>,
    {
        let s = seq.as_ref();
        let rows = s.matrix().rows() - s.wrap();
        self.score_rows_into(pssm, s, 0..rows, scores)
    }

    /// Compute the PSSM scores for every sequence positions.
    fn score<S, M>(&self, pssm: M, seq: S) -> StripedScores<T, C>
    where
        S: AsRef<StripedSequence<A, C>>,
        M: AsRef<DenseMatrix<T, A::K>>,
    {
        let seq = seq.as_ref();
        let mut scores = StripedScores::empty();
        self.score_into(pssm, seq, &mut scores);
        scores
    }
}

/// Used for finding the highest scoring site in a striped score matrix.
pub trait Maximum<T: MatrixElement + PartialOrd, C: PositiveLength> {
    /// Find the matrix coordinates with the highest score.
    fn argmax(&self, scores: &StripedScores<T, C>) -> Option<MatrixCoordinates> {
        if scores.is_empty() {
            return None;
        }

        let mut best_row = 0;
        let mut best_col = 0;
        let mut best_score = scores[0];

        for (i, row) in scores.matrix().iter().enumerate() {
            for (j, &x) in row.iter().enumerate() {
                if x >= best_score {
                    best_row = i;
                    best_col = j;
                    best_score = x;
                }
            }
        }

        Some(MatrixCoordinates::new(best_row, best_col))
    }

    /// Find the highest score.
    fn max(&self, scores: &StripedScores<T, C>) -> Option<T> {
        self.argmax(scores).map(|c| scores.matrix()[c])
    }
}

/// Used for converting an encoded sequence into a striped sequence.
pub trait Stripe<A: Alphabet, C: PositiveLength> {
    /// Stripe a sequence into a striped, column-major order matrix.
    fn stripe<S: AsRef<[A::Symbol]>>(&self, seq: S) -> StripedSequence<A, C> {
        let s = seq.as_ref();
        let length = s.len();
        let rows = (length / C::USIZE) + ((length % C::USIZE > 0) as usize);
        let capacity = rows + crate::seq::DEFAULT_EXTRA_ROWS;
        let mut striped =
            StripedSequence::new(DenseMatrix::with_capacity(rows, capacity), length).unwrap();
        self.stripe_into(s, &mut striped);
        striped
    }

    /// Stripe a sequence into the given striped matrix.
    fn stripe_into<S: AsRef<[A::Symbol]>>(&self, seq: S, striped: &mut StripedSequence<A, C>) {
        // compute length of striped matrix
        let s = seq.as_ref();
        let length = s.len();
        let rows = length.div_ceil(C::USIZE);
        let capacity = rows + crate::seq::DEFAULT_EXTRA_ROWS;

        // get the data out of the given buffer
        let mut data = std::mem::take(striped).into_matrix();
        data.reserve(capacity);
        data.resize(rows);

        // stripe the sequence
        for (i, &x) in s.iter().enumerate() {
            data[i % rows][i / rows] = x;
        }
        for i in s.len()..data.rows() * data.columns() {
            data[i % rows][i / rows] = A::Symbol::default();
        }

        // replace the original matrix with a new one
        *striped = StripedSequence::new(data, length).unwrap();
    }
}

/// Used for finding positions above a score threshold in a striped score matrix.
pub trait Threshold<T: MatrixElement + PartialOrd, C: PositiveLength> {
    /// Return the coordinates of positions with score equal to or greater than the threshold.
    ///
    /// # Note
    /// The indices are not be sorted, and the actual order depends on the
    /// implementation.
    fn threshold(&self, scores: &StripedScores<T, C>, threshold: T) -> Vec<MatrixCoordinates> {
        let mut positions = Vec::new();
        for (i, row) in scores.matrix().iter().enumerate() {
            for (j, &x) in row.iter().enumerate() {
                // assert!(!row[col].is_nan());
                if x >= threshold {
                    positions.push(MatrixCoordinates::new(i, j));
                }
            }
        }
        positions
    }
}

// --- Pipeline ----------------------------------------------------------------

/// Wrapper implementing score computation for different platforms.
#[derive(Debug, Default, Clone)]
pub struct Pipeline<A: Alphabet, B: Backend> {
    alphabet: std::marker::PhantomData<A>,
    backend: B,
}

// --- Generic pipeline --------------------------------------------------------

impl<A: Alphabet> Pipeline<A, Generic> {
    /// Create a new generic pipeline.
    pub const fn generic() -> Self {
        Self {
            alphabet: std::marker::PhantomData,
            backend: Generic,
        }
    }
}

impl<A: Alphabet> Encode<A> for Pipeline<A, Generic> {}

impl<T: MatrixElement + AddAssign, A: Alphabet, C: PositiveLength> Score<T, A, C>
    for Pipeline<A, Generic>
{
}

impl<T: MatrixElement + PartialOrd, A: Alphabet, C: PositiveLength> Maximum<T, C>
    for Pipeline<A, Generic>
{
}

impl<A: Alphabet, C: PositiveLength> Stripe<A, C> for Pipeline<A, Generic> {}

impl<T: MatrixElement + PartialOrd, A: Alphabet, C: PositiveLength> Threshold<T, C>
    for Pipeline<A, Generic>
{
}

// --- Dynamic dispatch --------------------------------------------------------

impl<A: Alphabet> Pipeline<A, Dispatch> {
    /// Create a new dynamic dispatch pipeline.
    #[allow(unreachable_code)]
    pub fn dispatch() -> Self {
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        if std::is_x86_feature_detected!("avx2") {
            return Self {
                backend: Dispatch::Avx2,
                alphabet: std::marker::PhantomData,
            };
        }
        #[cfg(target_arch = "x86")]
        if std::is_x86_feature_detected!("sse2") {
            return Self {
                backend: Dispatch::Sse2,
                alphabet: std::marker::PhantomData,
            };
        }
        #[cfg(target_arch = "x86_64")]
        return Self {
            backend: Dispatch::Sse2,
            alphabet: std::marker::PhantomData,
        };
        #[cfg(target_arch = "arm")]
        if std::arch::is_arm_feature_detected!("neon") {
            return Self {
                backend: Dispatch::Neon,
                alphabet: std::marker::PhantomData,
            };
        }
        #[cfg(target_arch = "aarch64")]
        if std::arch::is_aarch64_feature_detected!("neon") {
            return Self {
                backend: Dispatch::Neon,
                alphabet: std::marker::PhantomData,
            };
        }
        Self {
            backend: Dispatch::Generic,
            alphabet: std::marker::PhantomData,
        }
    }
}

// --- SSE2 pipeline -----------------------------------------------------------

impl<A: Alphabet> Pipeline<A, Sse2> {
    /// Attempt to create a new SSE2-accelerated pipeline.
    #[inline]
    pub fn sse2() -> Result<Self, UnsupportedBackend> {
        #[cfg(target_arch = "x86")]
        if std::is_x86_feature_detected!("sse2") {
            return Ok(Self::default());
        }
        #[cfg(target_arch = "x86_64")]
        return Ok(Self::default());
        #[allow(unreachable_code)]
        Err(UnsupportedBackend)
    }
}

impl<A: Alphabet> Encode<A> for Pipeline<A, Sse2> {
    #[inline]
    fn encode_into<S: AsRef<[u8]>>(
        &self,
        seq: S,
        dst: &mut [A::Symbol],
    ) -> Result<(), InvalidSymbol> {
        Sse2::encode_into::<A>(seq.as_ref(), dst)
    }
}

impl<A, C> Score<f32, A, C> for Pipeline<A, Sse2>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
    #[inline]
    fn score_rows_into<S, M>(
        &self,
        pssm: M,
        seq: S,
        rows: Range<usize>,
        scores: &mut StripedScores<f32, C>,
    ) where
        S: AsRef<StripedSequence<A, C>>,
        M: AsRef<DenseMatrix<f32, A::K>>,
    {
        Sse2::score_rows_into(pssm, seq, rows, scores)
    }
}

impl<A, C> Score<u8, A, C> for Pipeline<A, Sse2>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

impl<A, C> Maximum<f32, C> for Pipeline<A, Sse2>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
    #[inline]
    fn argmax(&self, scores: &StripedScores<f32, C>) -> Option<MatrixCoordinates> {
        Sse2::argmax(scores)
    }
}

impl<A, C> Maximum<u8, C> for Pipeline<A, Sse2>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

impl<A, C> Threshold<f32, C> for Pipeline<A, Sse2>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

impl<A, C> Threshold<u8, C> for Pipeline<A, Sse2>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

// --- AVX2 pipeline -----------------------------------------------------------

impl<A: Alphabet> Pipeline<A, Avx2> {
    /// Attempt to create a new AVX2-accelerated pipeline.
    pub fn avx2() -> Result<Self, UnsupportedBackend> {
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        if std::is_x86_feature_detected!("avx2") {
            return Ok(Self::default());
        }
        Err(UnsupportedBackend)
    }
}

impl<A: Alphabet> Encode<A> for Pipeline<A, Avx2> {
    #[inline]
    fn encode_into<S: AsRef<[u8]>>(
        &self,
        seq: S,
        dst: &mut [A::Symbol],
    ) -> Result<(), InvalidSymbol> {
        Avx2::encode_into::<A>(seq.as_ref(), dst)
    }
}

impl<A: Alphabet> Score<f32, A, <Avx2 as Backend>::Lanes> for Pipeline<A, Avx2> {
    #[inline]
    fn score_rows_into<S, M>(
        &self,
        pssm: M,
        seq: S,
        rows: Range<usize>,
        scores: &mut StripedScores<f32, <Avx2 as Backend>::Lanes>,
    ) where
        S: AsRef<StripedSequence<A, <Avx2 as Backend>::Lanes>>,
        M: AsRef<DenseMatrix<f32, <A as Alphabet>::K>>,
    {
        Avx2::score_f32_rows_into(pssm, seq, rows, scores)
    }
}

impl Score<u8, Dna, <Avx2 as Backend>::Lanes> for Pipeline<Dna, Avx2> {
    #[inline]
    fn score_rows_into<S, M>(
        &self,
        pssm: M,
        seq: S,
        rows: Range<usize>,
        scores: &mut StripedScores<u8, <Avx2 as Backend>::Lanes>,
    ) where
        S: AsRef<StripedSequence<Dna, <Avx2 as Backend>::Lanes>>,
        M: AsRef<DenseMatrix<u8, <Dna as Alphabet>::K>>,
    {
        Avx2::score_u8_rows_into_shuffle(pssm, seq, rows, scores)
    }
}

impl<A: Alphabet> Stripe<A, <Avx2 as Backend>::Lanes> for Pipeline<A, Avx2> {
    /// Stripe a sequence into the given striped matrix.
    #[inline]
    fn stripe_into<S: AsRef<[A::Symbol]>>(
        &self,
        seq: S,
        matrix: &mut StripedSequence<A, <Avx2 as Backend>::Lanes>,
    ) {
        Avx2::stripe_into(seq, matrix)
    }
}

impl<A: Alphabet> Maximum<f32, <Avx2 as Backend>::Lanes> for Pipeline<A, Avx2> {
    fn argmax(
        &self,
        scores: &StripedScores<f32, <Avx2 as Backend>::Lanes>,
    ) -> Option<MatrixCoordinates> {
        Avx2::argmax_f32(scores)
    }

    fn max(&self, scores: &StripedScores<f32, <Avx2 as Backend>::Lanes>) -> Option<f32> {
        Avx2::max_f32(scores)
    }
}

impl<A: Alphabet> Maximum<u8, <Avx2 as Backend>::Lanes> for Pipeline<A, Avx2> {
    fn argmax(
        &self,
        scores: &StripedScores<u8, <Avx2 as Backend>::Lanes>,
    ) -> Option<MatrixCoordinates> {
        Avx2::argmax_u8(scores)
    }

    fn max(&self, scores: &StripedScores<u8, <Avx2 as Backend>::Lanes>) -> Option<u8> {
        Avx2::max_u8(scores)
    }
}

impl<A: Alphabet> Threshold<f32, <Avx2 as Backend>::Lanes> for Pipeline<A, Avx2> {}

impl<A: Alphabet> Threshold<u8, <Avx2 as Backend>::Lanes> for Pipeline<A, Avx2> {}

// --- NEON pipeline -----------------------------------------------------------

impl<A: Alphabet> Pipeline<A, Neon> {
    /// Attempt to create a new AVX2-accelerated pipeline.
    #[inline]
    pub fn neon() -> Result<Self, UnsupportedBackend> {
        #[cfg(target_arch = "arm")]
        if std::arch::is_arm_feature_detected!("neon") {
            return Ok(Self::default());
        }
        #[cfg(target_arch = "aarch64")]
        if std::arch::is_aarch64_feature_detected!("neon") {
            return Ok(Self::default());
        }
        Err(UnsupportedBackend)
    }
}

impl<A: Alphabet> Encode<A> for Pipeline<A, Neon> {
    #[inline]
    fn encode_into<S: AsRef<[u8]>>(
        &self,
        seq: S,
        dst: &mut [A::Symbol],
    ) -> Result<(), InvalidSymbol> {
        Neon::encode_into::<A>(seq.as_ref(), dst)
    }
}

impl<A, C> Score<f32, A, C> for Pipeline<A, Neon>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
    #[inline]
    fn score_rows_into<S, M>(
        &self,
        pssm: M,
        seq: S,
        rows: Range<usize>,
        scores: &mut StripedScores<f32, C>,
    ) where
        S: AsRef<StripedSequence<A, C>>,
        M: AsRef<DenseMatrix<f32, <A as Alphabet>::K>>,
    {
        Neon::score_f32_rows_into(pssm, seq, rows, scores);
    }
}

impl<C> Score<u8, Dna, C> for Pipeline<Dna, Neon>
where
    C: PositiveLength + MultipleOf<U16>,
{
    #[inline]
    fn score_rows_into<S, M>(
        &self,
        pssm: M,
        seq: S,
        rows: Range<usize>,
        scores: &mut StripedScores<u8, C>,
    ) where
        S: AsRef<StripedSequence<Dna, C>>,
        M: AsRef<DenseMatrix<u8, <Dna as Alphabet>::K>>,
    {
        Neon::score_u8_rows_into(pssm, seq, rows, scores);
    }
}

impl<A, C> Maximum<f32, C> for Pipeline<A, Neon>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

impl<A, C> Maximum<u8, C> for Pipeline<A, Neon>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

impl<A, C> Threshold<f32, C> for Pipeline<A, Neon>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

impl<A, C> Threshold<u8, C> for Pipeline<A, Neon>
where
    A: Alphabet,
    C: PositiveLength + MultipleOf<U16>,
{
}

// -- Tests --------------------------------------------------------------------

#[cfg(test)]
mod test {
    use std::str::FromStr;
    use typenum::consts::U4;

    use super::*;

    use crate::abc::Dna;
    use crate::pwm::CountMatrix;

    #[test]
    fn score_rows_into_empty() {
        let pli = Pipeline::generic();

        let seq = EncodedSequence::<Dna>::from_str("ATGCA").unwrap();
        let mut striped = <Pipeline<_, _> as Stripe<Dna, U4>>::stripe(&pli, seq);

        let cm = CountMatrix::<Dna>::from_sequences(
            ["ATTA", "ATTC"]
                .iter()
                .map(|x| EncodedSequence::encode(x).unwrap()),
        )
        .unwrap();
        let pbm = cm.to_freq(0.1);
        let pwm = pbm.to_weight(None);
        let pssm = pwm.to_scoring();

        striped.configure(&pssm);
        let mut scores = StripedScores::empty();
        pli.score_rows_into(pssm, striped, 1..1, &mut scores);
    }
}