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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*! ![stability-experimental](https://img.shields.io/badge/stability-experimental-orange.svg)

A crate to manipulate multiple sequences alignments in Rust.

Instead of storing aligned sequences as multiple strings, `multi_seq_align` stores bases or residues in [`Alignment`] using a list of characters, like a matrix. This allows easy access to specific rows and columns of the alignment.

# Usage

```rust
# use multi_seq_align::Alignment;
# use std::error::Error;
# fn main() -> Result<(), Box<dyn Error>> {
let mut kappa_casein_fragments_alignment = Alignment::with_sequences(
    &[
        b"PAPISKWQSMP".to_vec(),
        b"HAQIPQRQYLP".to_vec(),
        b"PAQILQWQVLS".to_vec(),
    ],
)?;

// Let's extract a column of this alignment
assert_eq!(
    kappa_casein_fragments_alignment.nth_position(6).unwrap(),
    [&b'W', &b'R', &b'W']
);

// But we also have the aligned sequence for the Platypus
// Let's add it to the original alignment
kappa_casein_fragments_alignment.add(
    b"EHQRP--YVLP".to_vec(),
)?;

// the new aligned sequence has a gap at the 6th position
assert_eq!(
    kappa_casein_fragments_alignment.nth_position(6).unwrap(),
    [&b'W', &b'R', &b'W', &b'-']
);

// We can also loop over each position of the alignment
for aas in kappa_casein_fragments_alignment.iter_positions() {
    println!("{:?}", aas);
    assert_eq!(aas.len(), 4); // 4 sequences
}

# Ok(())
# }
```

Here I instancied an alignment using `u8`, but `Alignment` works on generics like numbers, custom or third-party structs.

# Features

- Create [`Alignment`] from one or multiple aligned sequences at once (see [`add()`] and [`with_sequences()`]).
- Extract columns of the alignment (see [`iter_positions()`] and [`iter_sequences(`]).
This crate is currently in early stage development. I wouldn't recommend using it in production but I am interested in possible ideas to further the developemt of this project. Quite some work needs toi be done to improve the API and make it easy to use in other project.
# Ideas
- Computation of conservation scores
- Identification of conserved sites
- Computation of consensus sequence
- Collapse / trim alignment
- Serialisation / Deserialisation of alignment files
- Extract sub-alignments
    - positions
    - motifs

# Optimisation

My goal is to reduce the footprint of this crate, there is ome work to do to achieve it. The code will eventually be optimised to be faster and to better use memory.

[`Alignment`]: struct.Alignment.html
[`iter_positions()`]: struct.Alignment.html#method.iter_positions
[`iter_sequences(`]: struct.Alignment.html#method.iter_sequences
[`add()`]: struct.Alignment.html#method.add
[`with_sequences()`]: struct.Alignment.html#method.with_sequences
*/
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]

mod errors;
mod utils;

use errors::MultiSeqAlignError;
use std::iter::FromIterator;

#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;

/// An alignment of DNA or amino acids sequences
///
/// Aligned sequences should all have the same length. Each sequence is stored as one vector of `char`s. This allows an easy access to columns and rows of the alignment.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] // Use Rc to implement Copy
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Alignment<T> {
    /// Sequences (as one)
    sequences: Vec<T>,
    /// The number of sequences in the alignment
    n_sequences: usize,
    /// The length of the alignment
    length: usize,
}

impl<T> Default for Alignment<T>
where
    T: Clone,
{
    fn default() -> Self {
        Self {
            sequences: Vec::<T>::default(),
            n_sequences: 0_usize,
            length: 0_usize,
        }
    }
}
struct AlignmentPositionIterator<'a, T> {
    alignment: &'a Alignment<T>,
    index: usize,
    size_hint: usize,
}

impl<'a, T> Iterator for AlignmentPositionIterator<'a, T>
where
    T: Clone,
{
    type Item = Vec<&'a T>;
    fn next(&mut self) -> Option<Vec<&'a T>> {
        if self.index >= self.alignment.length {
            return None;
        }
        match self.alignment.nth_position(self.index) {
            Some(position) => {
                self.index = self.index.saturating_add(1);
                self.size_hint = self.size_hint.saturating_sub(1);
                Some(position)
            }
            None => None,
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.size_hint < usize::max_value() {
            // ?
            (self.size_hint, Some(self.size_hint))
        } else {
            (usize::max_value(), None)
        }
    }
}

impl<'a, T> ExactSizeIterator for AlignmentPositionIterator<'a, T>
where
    T: Clone,
{
    fn len(&self) -> usize {
        let (lower, upper) = self.size_hint();
        // Note: This assertion is overly defensive, but it checks the invariant
        // guaranteed by the trait. If this trait were rust-internal,
        // we could use debug_assert!; assert_eq! will check all Rust user
        // implementations too.
        assert_eq!(upper, Some(lower));
        lower
    }
}

struct AlignmentSequenceIterator<'a, T> {
    alignment: &'a Alignment<T>,
    index: usize,
    size_hint: usize,
}

impl<'a, T> Iterator for AlignmentSequenceIterator<'a, T>
where
    T: Clone,
{
    type Item = Vec<&'a T>;
    fn next(&mut self) -> Option<Vec<&'a T>> {
        if self.index >= self.alignment.n_sequences {
            return None;
        }

        match self.alignment.nth_sequence(self.index) {
            Some(seq) => {
                self.index = self.index.saturating_add(1);
                self.size_hint = self.size_hint.saturating_sub(1);
                Some(seq)
            }
            None => None,
        }
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.alignment.nth_sequence(n)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.size_hint < usize::max_value() {
            // ?
            (self.size_hint, Some(self.size_hint))
        } else {
            (usize::max_value(), None)
        }
    }
}

impl<'a, T> ExactSizeIterator for AlignmentSequenceIterator<'a, T>
where
    T: Clone,
{
    fn len(&self) -> usize {
        let (lower, upper) = self.size_hint();
        // Note: This assertion is overly defensive, but it checks the invariant
        // guaranteed by the trait. If this trait were rust-internal,
        // we could use debug_assert!; assert_eq! will check all Rust user
        // implementations too.
        assert_eq!(upper, Some(lower));
        lower
    }
}

impl<T> Alignment<T> {
    /// Returns the fixed `length` of the Alignment `self`
    #[must_use]
    pub const fn length(&self) -> &usize {
        &self.length
    }

    /// Returns the number of sequences contained in `self`
    #[must_use]
    pub const fn n_sequences(&self) -> &usize {
        &self.n_sequences
    }

    /// Returns an Iterator over the positions of the alignment
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use multi_seq_align::Alignment;
    /// let align = Alignment::with_sequences(
    ///     &[
    ///         b"AVEQTPRK".to_vec(),
    ///         b"SVEQTPRK".to_vec(),
    ///         b"SVEQTPKK".to_vec(),
    ///     ],
    /// )
    /// .unwrap();
    ///
    /// for position in align.iter_positions() {
    ///     assert_eq!(position.len(), 3)
    /// }
    /// ```
    pub fn iter_positions(
        &self,
    ) -> impl Iterator<Item = Vec<&T>> + ExactSizeIterator<Item = Vec<&T>>
    where
        T: Clone,
    {
        AlignmentPositionIterator {
            alignment: self,
            index: 0_usize,
            size_hint: self.length,
        }
    }

    /// Returns an Iterator over the sequences of the alignment
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use multi_seq_align::Alignment;
    /// let align = Alignment::with_sequences(
    ///     &[
    ///         b"AVEQTPRK".to_vec(),
    ///         b"SVEQTPRK".to_vec(),
    ///         b"SVEQTPKK".to_vec(),
    ///     ],
    /// )
    /// .unwrap();
    ///
    /// for sequence in align.iter_sequences() {
    ///     assert_eq!(sequence.len(), 8)
    /// }
    /// ```
    pub fn iter_sequences(
        &self,
    ) -> impl Iterator<Item = Vec<&T>> + ExactSizeIterator<Item = Vec<&T>>
    where
        T: Clone,
    {
        AlignmentSequenceIterator {
            alignment: self,
            index: 0_usize,
            size_hint: self.n_sequences,
        }
    }

    /// Returns an empty `Alignment` of fixed `length`
    ///
    ///
    /// # Examples
    ///
    /// ```rust
    ///  # use multi_seq_align::Alignment;
    ///  let alignment = Alignment::<char>::new(42);
    ///
    ///  assert_eq!(*alignment.length(), 42 as usize);
    ///  assert_eq!(*alignment.n_sequences(), 0 as usize);
    /// ```
    #[must_use]
    pub const fn new(length: usize) -> Self {
        Self {
            sequences: Vec::new(),
            n_sequences: 0_usize,
            length,
        }
    }

    /// Returns `true` if `self` doesn't contains any sequence
    ///
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use multi_seq_align::Alignment;
    /// let alignment = Alignment::<char>::new(42);
    ///
    /// assert!(alignment.is_empty())
    ///```
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.n_sequences == 0_usize
    }

    /// Create an `Alignment` from same length vectors of names, descriptions, sequences
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use multi_seq_align::Alignment;
    /// let align = Alignment::with_sequences(
    ///     &[
    ///         b"AVEQTPRK".to_vec(),
    ///         b"SVEQTPRK".to_vec(),
    ///         b"SVEQTPKK".to_vec(),
    ///     ],
    /// )
    /// .unwrap();
    ///
    /// assert_eq!(*align.length(), 8);
    /// assert_eq!(*align.n_sequences(), 3);
    /// ```
    ///
    /// # Errors
    ///
    /// Will return an error if `names`, `descriptions` and `sequences` have different lengths, and also if the sequences have different lengths (based on the first sequence).
    pub fn with_sequences(sequences: &[Vec<T>]) -> Result<Self, MultiSeqAlignError>
    where
        T: Clone,
    {
        let length = utils::first_sequence_length(sequences);
        utils::check_unequal_lengths(sequences, length)?;

        let n_sequences = sequences.len();

        let sequences_vec = sequences.iter().flat_map(|x| x.to_vec()).collect();

        Ok(Self {
            sequences: sequences_vec,
            n_sequences,
            length,
        })
    }

    /// Add a sequence to `self`
    ///
    /// The new sequence must have the same length than `self.length`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use multi_seq_align::Alignment;
    /// let mut align = Alignment::new(8);
    ///
    ///  assert_eq!(*align.n_sequences(), 0);
    ///
    /// align
    ///     .add(b"AVEQTPRK".to_vec())
    ///     .unwrap();
    ///
    /// assert_eq!(*align.n_sequences(), 1);
    ///
    /// align
    ///     .add(b"SVEQTPRK".to_vec())
    ///     .unwrap();
    ///
    /// assert_eq!(*align.n_sequences(), 2);
    /// ```
    ///
    /// # Errors
    ///
    /// Will return an error if the length of `sequence` is different from the one of the alignment.
    pub fn add<'a>(&'a mut self, sequence: Vec<T>) -> Result<&'a mut Self, MultiSeqAlignError> {
        if sequence.len() != self.length {
            return Err(MultiSeqAlignError::NewSequenceOfDifferentLength {
                expected_length: self.length,
                // sequences_name: name,
                found_length: sequence.len(),
            });
        }

        self.sequences.extend(sequence);

        self.n_sequences += 1;

        Ok(self)
    }

    /// Returns all amino acids / bases at a `position` in the alignment `self`. The returned vector has a length equal of number of sequences in `self`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use multi_seq_align::Alignment;
    /// let align = Alignment::<u8>::with_sequences(
    ///     &[b"ELK".to_vec(), b"ILK".to_vec()],
    /// )
    /// .unwrap();
    ///
    /// assert_eq!(align.nth_position(0).unwrap(), &[&b'E', &b'I']);
    /// ```
    /// # Panics
    ///
    /// Panics if `n` is greater or equal to the `length` of the Alignment.
    #[must_use]
    pub fn nth_position(&self, n: usize) -> Option<Vec<&T>> {
        assert!(n < self.length);
        (0..self.n_sequences)
            .map(|i| self.sequences.get(i * self.length + n))
            .collect::<Vec<Option<&T>>>()
            .into_iter()
            .collect::<Option<Vec<&T>>>()
    }

    /// Returns all amino acids / bases of the sequence at the `index` of the Alignment `self`. The returned vector has a length equal to the length of the Alignment `self`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use multi_seq_align::Alignment;
    /// let align = Alignment::<u8>::with_sequences(
    ///     &[b"ELK".to_vec(), b"ILK".to_vec()],
    /// )
    /// .unwrap();
    ///
    /// assert_eq!(align.nth_sequence(1).unwrap(), &[&b'I', &b'L', &b'K']);
    /// ```
    /// # Panics
    ///
    /// Panics if `index` is greater or equal to the `n_sequences` of the Alignment.
    #[must_use]
    pub fn nth_sequence(&self, index: usize) -> Option<Vec<&T>> {
        debug_assert!(index < self.n_sequences);

        (0..self.length)
            .map(|i| self.sequences.get(self.length * index + i))
            .collect::<Vec<Option<&T>>>()
            .into_iter()
            .collect::<Option<Vec<&T>>>()
    }
}

impl<A> FromIterator<Vec<A>> for Alignment<A>
where
    A: Clone,
{
    /// # Panics
    ///
    /// Panics if sequences are of different lengths
    fn from_iter<I: IntoIterator<Item = Vec<A>>>(iter: I) -> Self {
        let mut length: Option<usize> = None;
        let mut n_sequences = 0_usize;

        let sequences = iter
            .into_iter()
            .flat_map(|x| {
                if length.is_none() {
                    length = Some(x.len());
                } else if Some(x.len()) != length {
                    panic!("sequences of different lengths");
                }

                n_sequences += 1;
                x.to_vec()
            })
            .collect::<Vec<_>>();

        Self {
            sequences,
            n_sequences,
            length: length.unwrap(),
        }
    }
}

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

    #[test]
    fn new_align() {
        let x = Alignment::<char>::new(5_usize);
        assert!(x.sequences.is_empty());
        assert_eq!(x.length, 5_usize);
        assert_eq!(x.n_sequences, 0_usize);
    }

    #[test]
    fn new_alignment_with_desc() {
        let x = Alignment::<u8>::with_sequences(&[b"ELK".to_vec(), b"ILK".to_vec()]).unwrap();

        assert_eq!(x.sequences, vec![b'E', b'L', b'K', b'I', b'L', b'K']);
        assert_eq!(x.length, 3);
        assert_eq!(x.n_sequences, 2);
    }

    #[test]
    fn add_1_sequence() {
        let mut align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();

        align.add(b"ALRYITAT".to_vec()).unwrap();

        assert_eq!(align.n_sequences, 3_usize);
        assert_eq!(align.nth_position(3).unwrap(), vec![&b'H', &b'-', &b'Y'])
    }

    #[test]
    fn add_1_sequence_wrong_length() {
        let mut x = Alignment::new(3_usize);
        let error = x.add(b"ILKAV".to_vec()).err().unwrap();
        let expected = MultiSeqAlignError::NewSequenceOfDifferentLength {
            expected_length: 3_usize,
            found_length: 5_usize,
        };
        assert_eq!(error, expected);
    }

    #[test]
    fn add_to_new() {
        let mut x = Alignment::new(3_usize);

        x.add(b"ELK".to_vec()).unwrap();
        assert_eq!(x.n_sequences, 1_usize);
        assert_eq!(x.length, 3_usize);
        assert_eq!(x.sequences.len(), 3_usize);

        x.add(b"ILK".to_vec()).unwrap();

        assert_eq!(x.n_sequences, 2_usize);
        assert_eq!(x.length, 3_usize);
        assert_eq!(x.sequences.len(), 6_usize);
    }

    #[test]
    fn empty_align() {
        let mut x = Alignment::new(3_usize);
        assert!(x.is_empty());
        x.add(b"ILK".to_vec()).unwrap();
        assert!(!x.is_empty());
    }

    #[test]
    fn nth_residues_3() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();
        assert_eq!(align.nth_position(3).unwrap(), vec![&b'H', &b'-'])
    }

    #[test]
    fn nth_residues_more_seqs() {
        let align = Alignment::with_sequences(&[
            b"ALKHITAN".to_vec(),
            b"VLK-ITAN".to_vec(),
            b"ALKWITAN".to_vec(),
            b"VLKMITAN".to_vec(),
        ])
        .unwrap();
        assert_eq!(
            align.nth_position(3).unwrap(),
            vec![&b'H', &b'-', &b'W', &b'M']
        )
    }

    #[test]
    #[should_panic(expected = "assertion failed: n < self.length")]
    fn nth_residues_out() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();
        let _out_of_bonds = align.nth_position(10);
    }

    #[test]
    fn different_seq_lengths() {
        let error = Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN---".to_vec()])
            .err()
            .unwrap();

        let expected = MultiSeqAlignError::MultipleSequencesOfDifferentLengths {
            expected_length: 8,
            found_lengths: vec![11],
        };
        assert_eq!(error, expected);
    }

    #[test]
    fn for_positions() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();

        let mut x = Vec::new();

        for col in align.iter_positions() {
            x.push(col);
        }

        assert_eq!(x.len(), 8);
        assert_eq!(x.get(0).unwrap(), &[&b'A', &b'V']);
        assert_eq!(x.get(3).unwrap(), &[&b'H', &b'-']);
    }

    #[test]
    #[should_panic]
    fn for_positions_out_of_bonds() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();
        let mut x = Vec::new();

        for col in align.iter_positions() {
            x.push(col);
        }

        let _ = x.get(22).unwrap();
    }

    #[test]
    fn for_positions_exact() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();

        assert_eq!(align.iter_positions().len(), 8);
        assert_eq!(align.iter_positions().next().unwrap().len(), 2);
    }

    #[test]
    fn for_sequences() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();

        let mut x = Vec::new();

        for row in align.iter_sequences() {
            assert_eq!(row.len(), 8);
            x.push(row);
        }

        assert_eq!(x.len(), 2)
    }

    #[test]
    fn for_sequences_exact() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();

        assert_eq!(align.iter_sequences().len(), 2);
        assert_eq!(align.iter_sequences().next().unwrap().len(), 8);
    }

    #[test]
    fn for_sequences_collect() {
        let align =
            Alignment::with_sequences(&[b"ALKHITAN".to_vec(), b"VLK-ITAN".to_vec()]).unwrap();

        assert_eq!(align.iter_sequences().len(), 2);
        assert_eq!(align.iter_sequences().next().unwrap().len(), 8);
    }
}