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
use std::{io::{BufRead, BufReader}, fs::File};
use flate2::read::MultiGzDecoder;
use anyhow::Result;

use super::{
    FastxRead,
    FastaReader,
    FastqReader,
    Record
};

const BUFFER_SIZE: usize = 4096 * 68;

/// Handles the algebraic enumerations of possible expected file types.
/// This is either a Fasta/Fastq and whether or not it is Gzipped.
#[derive(Debug)]
enum FileType {
    Fasta,
    Fastq,
    FastaGz,
    FastqGz
}
impl FileType {
    
    /// Handles creation of enum from path name
    pub fn from_path(path: &str) -> Option<Self> {
        if path.ends_with(".fastq.gz") | path.ends_with(".fq.gz") {
            Some(Self::FastqGz)
        } else if path.ends_with(".fastq") | path.ends_with(".fq") {
            Some(Self::Fastq)
        } else if path.ends_with(".fasta.gz") | path.ends_with(".fa.gz") {
            Some(Self::FastaGz)
        } else if path.ends_with(".fasta") | path.ends_with(".fa") {
            Some(Self::Fasta)
        } else {
            None
        }
    }
}

fn initialize_generic_buffer(
        path: &str, 
        is_gzip: bool) -> Result<Box<dyn BufRead>> 
{
    if is_gzip {
        let file = File::open(path)?;
        let gzip = MultiGzDecoder::new(file);
        let buffer = BufReader::with_capacity(BUFFER_SIZE, gzip);
        Ok(Box::new(buffer))
    }
    else {
        let file = File::open(path)?;
        let buffer = BufReader::with_capacity(BUFFER_SIZE, file);
        Ok(Box::new(buffer))
    }
}

fn initialize_generic_reader(
        buffer: Box<dyn BufRead>, 
        is_fasta: bool) -> Box<dyn FastxRead<Item = Record>> 
{
    if is_fasta { Box::new(FastaReader::new(buffer)) } else { Box::new(FastqReader::new(buffer)) }
}

/// # Initializing a reader dependent on the file path extensions.
/// ## Recognized Extensions
/// This recognizes `FASTA` formats from `*.fa` and `*.fasta` and
/// `FASTQ` formats from `*.fq` and `*.fastq`. These will then be
/// opened with gzip buffers or standard depending on if the 
/// `*.gz` extension is found at the end of the pathname.
/// 
/// ## From Fasta
/// This example shows the creation of a reader from a fasta formatted
/// plaintext file.
/// ```
/// use fxread::initialize_reader;
/// let path = "example/sequences.fa";
/// let reader = initialize_reader(path).unwrap();
/// reader
///     .for_each(|record| println!("{:?}", record));
/// ```
///
/// ## From Gzip Fasta
/// A common problem for the above however is that your file
/// may actually be gzipped. Here you can use the same function
/// and it will handle the initialization of the reader depending
/// on the extension.
/// ```
/// use fxread::initialize_reader;
/// let path = "example/sequences.fa.gz";
/// let reader = initialize_reader(path).unwrap();
/// reader
///     .for_each(|record| println!("{:?}", record));
/// ```
///
/// ## From Fastq
/// Here is another example for a fastq-formatted file that 
/// is plaintext
/// ```
/// use fxread::initialize_reader;
/// let path = "example/sequences.fq";
/// let reader = initialize_reader(path).unwrap();
/// reader
///     .for_each(|record| println!("{:?}", record));
///
/// ```
/// ## From Gzip Fastq
/// Here is another example for a fastq-formatted file that 
/// is gzipped
/// ```
/// use fxread::initialize_reader;
/// let path = "example/sequences.fq.gz";
/// let reader = initialize_reader(path).unwrap();
/// reader
///     .for_each(|record| println!("{:?}", record));
/// ```
pub fn initialize_reader(path: &str) -> Result<Box<dyn FastxRead<Item = Record>>>{
    let (is_gzip, is_fasta) = match FileType::from_path(path) {
        Some(s) => {
            match s {
                FileType::Fasta => Ok((false, true)),
                FileType::Fastq => Ok((false, false)),
                FileType::FastaGz => Ok((true, true)),
                FileType::FastqGz => Ok((true, false))
            }
        }
        None => Err(anyhow::anyhow!("Cannot accurately parse filetype from path: {}", path))
    }?;
    let buffer = initialize_generic_buffer(path, is_gzip)?;
    let reader = initialize_generic_reader(buffer, is_fasta);
    Ok(reader)
}


#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn assignment_fastq_short() {
        let path = "sequence.fq";
        match FileType::from_path(path) {
            Some(FileType::Fastq) => assert!(true),
            _ => assert!(false)
        }
    }

    #[test]
    fn assignment_fastq_long() {
        let path = "sequence.fastq";
        match FileType::from_path(path) {
            Some(FileType::Fastq) => assert!(true),
            _ => assert!(false)
        }
    }

    #[test]
    fn assignment_fasta_short() {
        let path = "sequence.fa";
        match FileType::from_path(path) {
            Some(FileType::Fasta) => assert!(true),
            _ => assert!(false)
        }
    }

    #[test]
    fn assignment_fasta_long() {
        let path = "sequence.fasta";
        match FileType::from_path(path) {
            Some(FileType::Fasta) => assert!(true),
            _ => assert!(false)
        }
    }


    #[test]
    fn assignment_gz_fastq_short() {
        let path = "sequence.fq.gz";
        match FileType::from_path(path) {
            Some(FileType::FastqGz) => assert!(true),
            _ => assert!(false)
        }
    }

    #[test]
    fn assignment_gz_fastq_long() {
        let path = "sequence.fastq.gz";
        match FileType::from_path(path) {
            Some(FileType::FastqGz) => assert!(true),
            _ => assert!(false)
        }
    }

    #[test]
    fn assignment_gz_fasta_short() {
        let path = "sequence.fa.gz";
        match FileType::from_path(path) {
            Some(FileType::FastaGz) => assert!(true),
            _ => assert!(false)
        }
    }

    #[test]
    fn assignment_gz_fasta_long() {
        let path = "sequence.fasta.gz";
        match FileType::from_path(path) {
            Some(FileType::FastaGz) => assert!(true),
            _ => assert!(false)
        }
    }

    #[test]
    fn assign_fasta() {
        let path = "example/sequences.fa";
        let reader = initialize_reader(path).expect("invalid path");
        let num_records = reader
            .into_iter()
            .map(|x| assert!(!x.empty()))
            .count();
        assert_eq!(num_records, 10);
    }

    #[test]
    fn assign_gzfasta() {
        let path = "example/sequences.fa.gz";
        let reader = initialize_reader(path).expect("invalid path");
        let num_records = reader
            .into_iter()
            .map(|x| assert!(!x.empty()))
            .count();
        assert_eq!(num_records, 10);
    }

    #[test]
    fn assign_fastq() {
        let path = "example/sequences.fq";
        let reader = initialize_reader(path).expect("invalid path");
        let num_records = reader
            .into_iter()
            .map(|x| assert!(!x.empty()))
            .count();
        assert_eq!(num_records, 10);
    }

    #[test]
    fn assign_gzfastq() {
        let path = "example/sequences.fq.gz";
        let reader = initialize_reader(path).expect("invalid path");
        let num_records = reader
            .into_iter()
            .map(|x| assert!(!x.empty()))
            .count();
        assert_eq!(num_records, 10);
    }
}