omicsx 1.0.2

omicsx: SIMD-accelerated sequence alignment and bioinformatics analysis for petabyte-scale genomic data
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
//! CLI Buffered File I/O for processing large genomic databases
//! Efficient streaming processing of FASTA, FASTQ, BAM, and custom formats

use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::Path;
use crate::error::{Error, Result};

/// Sequence record from input file
#[derive(Debug, Clone)]
pub struct SeqRecord {
    /// Sequence identifier
    pub id: String,
    /// Optional description
    pub description: Option<String>,
    /// Sequence data
    pub sequence: String,
    /// Quality scores (FASTQ only)
    pub quality: Option<String>,
}

impl SeqRecord {
    /// Get full header line
    pub fn header(&self) -> String {
        match &self.description {
            Some(desc) => format!("{} {}", self.id, desc),
            None => self.id.clone(),
        }
    }

    /// Get length of sequence
    pub fn len(&self) -> usize {
        self.sequence.len()
    }

    /// Check if record is empty
    pub fn is_empty(&self) -> bool {
        self.sequence.is_empty()
    }
}

/// Input file format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileFormat {
    /// FASTA format (>id description \n sequence)
    Fasta,
    /// FASTQ format (@ quality)
    Fastq,
    /// Tab-separated values (id \t sequence)
    Tsv,
}

impl FileFormat {
    /// Detect format from file extension
    pub fn from_path<P: AsRef<Path>>(path: P) -> Self {
        let path = path.as_ref();
        match path.extension().and_then(|s| s.to_str()) {
            Some("fasta") | Some("fa") | Some("faa") => FileFormat::Fasta,
            Some("fastq") | Some("fq") => FileFormat::Fastq,
            Some("tsv") | Some("txt") => FileFormat::Tsv,
            _ => FileFormat::Fasta, // Default
        }
    }
}

/// Buffered sequence file reader
pub struct SeqFileReader {
    reader: BufReader<File>,
    format: FileFormat,
    buffer: String,
    line_count: u64,
}

impl SeqFileReader {
    /// Open file with automatic format detection
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        let file = File::open(path)
            .map_err(|e| Error::AlignmentError(format!("Failed to open file: {}", e)))?;

        let format = FileFormat::from_path(path);
        Ok(SeqFileReader {
            reader: BufReader::new(file),
            format,
            buffer: String::new(),
            line_count: 0,
        })
    }

    /// Open file with explicit format
    pub fn open_with_format<P: AsRef<Path>>(path: P, format: FileFormat) -> Result<Self> {
        let file = File::open(path.as_ref())
            .map_err(|e| Error::AlignmentError(format!("Failed to open file: {}", e)))?;

        Ok(SeqFileReader {
            reader: BufReader::new(file),
            format,
            buffer: String::new(),
            line_count: 0,
        })
    }

    /// Read next sequence record
    pub fn next_record(&mut self) -> Result<Option<SeqRecord>> {
        match self.format {
            FileFormat::Fasta => self.read_fasta_record(),
            FileFormat::Fastq => self.read_fastq_record(),
            FileFormat::Tsv => self.read_tsv_record(),
        }
    }

    /// Read FASTA record
    fn read_fasta_record(&mut self) -> Result<Option<SeqRecord>> {
        self.buffer.clear();

        // Skip until header line
        loop {
            let n = self
                .reader
                .read_line(&mut self.buffer)
                .map_err(|e| Error::AlignmentError(format!("Read error: {}", e)))?;

            if n == 0 {
                return Ok(None); // EOF
            }

            self.line_count += 1;

            if self.buffer.starts_with('>') {
                break;
            }
            self.buffer.clear();
        }

        let header = self.buffer.trim_end().to_string();
        let (id, description) = parse_fasta_header(&header);

        let mut sequence = String::new();
        self.buffer.clear();

        // Read sequence lines until next header or EOF
        loop {
            let n = self
                .reader
                .read_line(&mut self.buffer)
                .map_err(|e| Error::AlignmentError(format!("Read error: {}", e)))?;

            if n == 0 || self.buffer.starts_with('>') {
                if self.buffer.starts_with('>') {
                    // Put back the header by seeking?
                    // For now, this becomes the next record's header
                }
                break;
            }

            self.line_count += 1;
            sequence.push_str(self.buffer.trim_end());
            self.buffer.clear();
        }

        Ok(Some(SeqRecord {
            id,
            description,
            sequence,
            quality: None,
        }))
    }

    /// Read FASTQ record
    fn read_fastq_record(&mut self) -> Result<Option<SeqRecord>> {
        let mut lines = [String::new(), String::new(), String::new(), String::new()];

        for line in &mut lines {
            let n = self
                .reader
                .read_line(line)
                .map_err(|e| Error::AlignmentError(format!("Read error: {}", e)))?;

            if n == 0 {
                return Ok(None);
            }
            self.line_count += 1;
            *line = line.trim_end().to_string();
        }

        let (id, description) = parse_fastq_header(&lines[0]);
        let sequence = lines[1].clone();
        let quality = Some(lines[3].clone());

        if lines[2] != "+" {
            return Err(Error::AlignmentError(
                format!("Invalid FASTQ format at line {}", self.line_count - 1),
            ));
        }

        Ok(Some(SeqRecord {
            id,
            description,
            sequence,
            quality,
        }))
    }

    /// Read TSV record
    fn read_tsv_record(&mut self) -> Result<Option<SeqRecord>> {
        self.buffer.clear();
        let n = self
            .reader
            .read_line(&mut self.buffer)
            .map_err(|e| Error::AlignmentError(format!("Read error: {}", e)))?;

        if n == 0 {
            return Ok(None);
        }

        self.line_count += 1;
        let parts: Vec<&str> = self.buffer.trim().split('\t').collect();

        if parts.len() < 2 {
            return Err(Error::AlignmentError("TSV record must have at least 2 columns".to_string()));
        }

        Ok(Some(SeqRecord {
            id: parts[0].to_string(),
            description: parts.get(2).map(|s| s.to_string()),
            sequence: parts[1].to_string(),
            quality: parts.get(3).map(|s| s.to_string()),
        }))
    }

    /// Get current line number
    pub fn line_number(&self) -> u64 {
        self.line_count
    }
}

/// Buffered sequence file writer
pub struct SeqFileWriter {
    writer: BufWriter<File>,
    format: FileFormat,
    record_count: u64,
}

impl SeqFileWriter {
    /// Create new file for writing
    pub fn create<P: AsRef<Path>>(path: P, format: FileFormat) -> Result<Self> {
        let file = File::create(path.as_ref())
            .map_err(|e| Error::AlignmentError(format!("Failed to create file: {}", e)))?;

        Ok(SeqFileWriter {
            writer: BufWriter::new(file),
            format,
            record_count: 0,
        })
    }

    /// Write sequence record
    pub fn write_record(&mut self, record: &SeqRecord) -> Result<()> {
        match self.format {
            FileFormat::Fasta => {
                writeln!(self.writer, ">{}", record.header())
                    .map_err(|e| Error::AlignmentError(e.to_string()))?;
                writeln!(self.writer, "{}", record.sequence)
                    .map_err(|e| Error::AlignmentError(e.to_string()))?;
            }
            FileFormat::Fastq => {
                if let Some(ref quality) = record.quality {
                    writeln!(self.writer, "@{}", record.header())
                        .map_err(|e| Error::AlignmentError(e.to_string()))?;
                    writeln!(self.writer, "{}", record.sequence)
                        .map_err(|e| Error::AlignmentError(e.to_string()))?;
                    writeln!(self.writer, "+")
                        .map_err(|e| Error::AlignmentError(e.to_string()))?;
                    writeln!(self.writer, "{}", quality)
                        .map_err(|e| Error::AlignmentError(e.to_string()))?;
                } else {
                    return Err(Error::AlignmentError("FASTQ record requires quality scores".to_string()));
                }
            }
            FileFormat::Tsv => {
                write!(self.writer, "{}\t{}", record.id, record.sequence)
                    .map_err(|e| Error::AlignmentError(e.to_string()))?;
                if let Some(ref desc) = record.description {
                    write!(self.writer, "\t{}", desc)
                        .map_err(|e| Error::AlignmentError(e.to_string()))?;
                }
                if let Some(ref qual) = record.quality {
                    write!(self.writer, "\t{}", qual)
                        .map_err(|e| Error::AlignmentError(e.to_string()))?;
                }
                writeln!(self.writer).map_err(|e| Error::AlignmentError(e.to_string()))?;
            }
        }

        self.record_count += 1;
        Ok(())
    }

    /// Write multiple records
    pub fn write_batch(&mut self, records: &[SeqRecord]) -> Result<()> {
        for record in records {
            self.write_record(record)?;
        }
        Ok(())
    }

    /// Flush buffered writes to disk
    pub fn flush(&mut self) -> Result<()> {
        self.writer
            .flush()
            .map_err(|e| Error::AlignmentError(e.to_string()))
    }

    /// Get number of records written
    pub fn record_count(&self) -> u64 {
        self.record_count
    }
}

/// Parse FASTA header (>id description)
fn parse_fasta_header(header: &str) -> (String, Option<String>) {
    let header = header.trim_start_matches('>');
    let parts: Vec<&str> = header.splitn(2, ' ').collect();

    let id = parts[0].to_string();
    let description = parts.get(1).map(|s| s.to_string());

    (id, description)
}

/// Parse FASTQ header (@id description)
fn parse_fastq_header(header: &str) -> (String, Option<String>) {
    let header = header.trim_start_matches('@');
    let parts: Vec<&str> = header.splitn(2, ' ').collect();

    let id = parts[0].to_string();
    let description = parts.get(1).map(|s| s.to_string());

    (id, description)
}

/// Batch processor for sequence analysis
pub struct BatchProcessor {
    batch_size: usize,
    filter_min_len: usize,
}

impl BatchProcessor {
    /// Create new batch processor
    pub fn new(batch_size: usize) -> Self {
        BatchProcessor {
            batch_size,
            filter_min_len: 0,
        }
    }

    /// Set minimum sequence length filter
    pub fn with_min_length(mut self, len: usize) -> Self {
        self.filter_min_len = len;
        self
    }

    /// Process file in batches
    pub fn process_file<P: AsRef<Path>, F>(
        &self,
        path: P,
        mut callback: F,
    ) -> Result<u64>
    where
        F: FnMut(&[SeqRecord]) -> Result<()>,
    {
        let mut reader = SeqFileReader::open(path)?;
        let mut batch = Vec::with_capacity(self.batch_size);
        let mut total = 0u64;

        loop {
            match reader.next_record()? {
                Some(record) => {
                    if record.len() >= self.filter_min_len {
                        batch.push(record);

                        if batch.len() >= self.batch_size {
                            callback(&batch)?;
                            total += batch.len() as u64;
                            batch.clear();
                        }
                    }
                }
                None => {
                    // Process remaining records
                    if !batch.is_empty() {
                        callback(&batch)?;
                        total += batch.len() as u64;
                    }
                    break;
                }
            }
        }

        Ok(total)
    }
}

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

    #[test]
    fn test_file_format_detection() {
        assert_eq!(FileFormat::from_path("test.fasta"), FileFormat::Fasta);
        assert_eq!(FileFormat::from_path("test.fastq"), FileFormat::Fastq);
        assert_eq!(FileFormat::from_path("test.tsv"), FileFormat::Tsv);
    }

    #[test]
    fn test_parse_fasta_header() {
        let (id, desc) = parse_fasta_header(">seq1 This is a description");
        assert_eq!(id, "seq1");
        assert_eq!(desc, Some("This is a description".to_string()));
    }

    #[test]
    fn test_parse_fastq_header() {
        let (id, desc) = parse_fastq_header("@seq1 description goes here");
        assert_eq!(id, "seq1");
        assert_eq!(desc, Some("description goes here".to_string()));
    }

    #[test]
    fn test_seq_record_creation() {
        let record = SeqRecord {
            id: "test".to_string(),
            description: Some("desc".to_string()),
            sequence: "ACGT".to_string(),
            quality: None,
        };

        assert_eq!(record.id, "test");
        assert_eq!(record.len(), 4);
        assert!(!record.is_empty());
    }

    #[test]
    fn test_seq_record_header() {
        let record = SeqRecord {
            id: "seq1".to_string(),
            description: Some("with desc".to_string()),
            sequence: "ACGT".to_string(),
            quality: None,
        };

        assert_eq!(record.header(), "seq1 with desc");
    }

    #[test]
    fn test_batch_processor_creation() {
        let proc = BatchProcessor::new(100);
        assert_eq!(proc.batch_size, 100);
    }

    #[test]
    fn test_batch_processor_with_filter() {
        let proc = BatchProcessor::new(50).with_min_length(10);
        assert_eq!(proc.filter_min_len, 10);
    }
}