compact_genome/implementation/
handle_sequence_store.rs

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
//! An "anti" sequence store that stores the sequences in the handles.
//!
//! This is useful to use methods that require a sequence store when only plain sequences are available.

use std::marker::PhantomData;

use crate::interface::{
    alphabet::{Alphabet, AlphabetError},
    sequence::{GenomeSequence, OwnedGenomeSequence},
    sequence_store::SequenceStore,
};

/// A handle-based sequence store.
///
/// The sequence store stores nothing, all data is in the handles.
#[derive(Default, Clone, Eq, PartialEq, Debug)]
pub struct HandleSequenceStore<AlphabetType, SequenceType, SubsequenceType: ?Sized> {
    phantom_data: PhantomData<(AlphabetType, SequenceType, SubsequenceType)>,
}

impl<AlphabetType, SequenceType, SubsequenceType: ?Sized>
    HandleSequenceStore<AlphabetType, SequenceType, SubsequenceType>
{
    /// Creates a new instance.
    pub fn new() -> Self {
        Self {
            phantom_data: Default::default(),
        }
    }
}

impl<
        AlphabetType: Alphabet,
        SequenceType: OwnedGenomeSequence<AlphabetType, SubsequenceType>,
        SubsequenceType: GenomeSequence<AlphabetType, SubsequenceType> + ?Sized,
    > SequenceStore<AlphabetType>
    for HandleSequenceStore<AlphabetType, SequenceType, SubsequenceType>
{
    type Handle = SequenceType;
    type SequenceRef = SubsequenceType;

    fn add<
        Sequence: GenomeSequence<AlphabetType, Subsequence> + ?Sized,
        Subsequence: GenomeSequence<AlphabetType, Subsequence> + ?Sized,
    >(
        &mut self,
        s: &Sequence,
    ) -> Self::Handle {
        Self::Handle::from_iter(s.iter().cloned())
    }

    fn add_from_iter(
        &mut self,
        iter: impl IntoIterator<Item = <AlphabetType as Alphabet>::CharacterType>,
    ) -> Self::Handle {
        Self::Handle::from_iter(iter)
    }

    fn add_from_iter_u8<IteratorType: IntoIterator<Item = u8>>(
        &mut self,
        iter: IteratorType,
    ) -> Result<Self::Handle, AlphabetError> {
        Self::Handle::from_iter_u8(iter)
    }

    fn get<'this: 'result, 'handle: 'result, 'result>(
        &'this self,
        handle: &'handle Self::Handle,
    ) -> &'result Self::SequenceRef {
        handle.as_genome_subsequence()
    }
}