chaintools 0.0.12

work with .chain files in Rust
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
// Copyright (c) 2026 Alejandro Gonzales-Irribarren <alejandrxgzi@gmail.com>
// Distributed under the terms of the Apache License, Version 2.0.

#[cfg(feature = "gzip")]
use std::io::BufReader;
#[cfg(any(feature = "gzip", not(feature = "mmap")))]
use std::io::Read;
use std::marker::PhantomData;
use std::path::Path;
use std::sync::Arc;

#[cfg(not(feature = "gzip"))]
use crate::io::storage::gzip_feature_error;
use crate::io::storage::{ByteSlice, SharedBytes, is_gz_path};
use crate::model::block::{Block, BlockSlice};
use crate::model::chain::Chain;
use crate::model::error::ChainError;
use crate::parser::parse_chains_sequential;

#[cfg(feature = "parallel")]
use crate::parser::parse_chains_parallel;

#[cfg(feature = "gzip")]
use flate2::read::MultiGzDecoder;

#[cfg(feature = "mmap")]
use memmap2::MmapOptions;

/// Reader for chain files.
///
/// The generic parameter allows the API shape `Reader::<Chain>::from_path(...)`.
/// Provides zero-copy parsing with optional memory mapping and parallel processing.
///
/// # Fields
///
/// * `_bytes` - Shared byte storage for the entire file
/// * `_blocks` - Shared storage for all alignment blocks
/// * `chains` - Parsed chain records with zero-copy references
/// * `_marker` - PhantomData for the generic parameter
///
/// # Examples
///
/// ```ignore
/// use chaintools::Reader;
///
/// // Load a chain file (uses mmap when available)
/// let reader = Reader::<chaintools::Chain>::from_path("example.chain")?;
///
/// // Iterate over all chains
/// for chain in reader.chains() {
///     println!("Chain score: {}", chain.score);
/// }
///
/// // Get chain count
/// println!("Total chains: {}", reader.len());
/// ```
#[derive(Debug)]
pub struct Reader<T = Chain> {
    _bytes: SharedBytes,
    _blocks: Arc<Vec<Block>>,
    chains: Vec<Chain>,
    _marker: PhantomData<T>,
}

/// Zero-copy iterator over chain metadata/comment lines.
///
/// Items exclude trailing `\n` and optional `\r` bytes and retain the leading
/// `#`. Metadata is yielded in file order.
#[derive(Debug, Clone)]
pub struct MetadataIter<'a> {
    bytes: &'a [u8],
    position: usize,
}

impl<'a> Iterator for MetadataIter<'a> {
    type Item = &'a [u8];

    fn next(&mut self) -> Option<Self::Item> {
        while self.position < self.bytes.len() {
            let line_start = self.position;
            let relative_end = memchr::memchr(b'\n', &self.bytes[line_start..]);
            let line_end = relative_end
                .map(|offset| line_start + offset)
                .unwrap_or(self.bytes.len());
            self.position = if line_end < self.bytes.len() {
                line_end + 1
            } else {
                line_end
            };
            let mut line = &self.bytes[line_start..line_end];
            if line.last() == Some(&b'\r') {
                line = &line[..line.len() - 1];
            }
            if line.first() == Some(&b'#') {
                return Some(line);
            }
        }
        None
    }
}

impl Reader<Chain> {
    /// Load a chain file from a path. Uses mmap when available, falls back to owned buffer.
    ///
    /// Automatically detects gzip files and decompresses them when the gzip feature
    /// is enabled. Prefers memory mapping for performance when available.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the chain file (plain text or gzip compressed)
    ///
    /// # Output
    ///
    /// Returns `Ok(Reader<Chain>)` containing all parsed chains, or `Err(ChainError)`
    /// if the file cannot be read or parsed
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// // Load a plain text chain file
    /// let reader = Reader::<chaintools::Chain>::from_path("example.chain")?;
    ///
    /// // Load a gzip compressed chain file
    /// let gz_reader = Reader::<chaintools::Chain>::from_path("example.chain.gz")?;
    ///
    /// println!("Loaded {} chains", reader.len());
    /// ```
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, ChainError> {
        let path = path.as_ref();
        if is_gz_path(path) {
            #[cfg(feature = "gzip")]
            {
                let file = std::fs::File::open(path)?;
                let reader = BufReader::new(file);
                let mut decoder = MultiGzDecoder::new(reader);
                let mut buffer = Vec::new();
                decoder.read_to_end(&mut buffer)?;
                return Self::from_owned_bytes(buffer);
            }
            #[cfg(not(feature = "gzip"))]
            {
                return Err(gzip_feature_error());
            }
        }

        #[cfg(feature = "mmap")]
        {
            Self::from_mmap(path)
        }

        #[cfg(not(feature = "mmap"))]
        {
            let mut data = Vec::new();
            std::fs::File::open(path)?.read_to_end(&mut data)?;
            Self::from_owned_bytes(data)
        }
    }

    /// Load a chain file using memory mapping (requires `mmap` feature).
    ///
    /// Maps the file directly into memory for zero-copy parsing without loading
    /// the entire file into RAM. Only works with uncompressed files.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the uncompressed chain file
    ///
    /// # Output
    ///
    /// Returns `Ok(Reader<Chain>)` with memory-mapped storage, or `Err(ChainError)`
    /// if the file cannot be mapped or parsed
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// // Load with memory mapping for large files
    /// let reader = Reader::<chaintools::Chain>::from_mmap("large.chain")?;
    ///
    /// for chain in reader.chains() {
    ///     println!("Processing chain: {}", chain.id);
    /// }
    /// ```
    #[cfg(feature = "mmap")]
    pub fn from_mmap<P: AsRef<Path>>(path: P) -> Result<Self, ChainError> {
        let file = std::fs::File::open(path)?;
        let mmap = unsafe { MmapOptions::new().map(&file)? };
        Self::build(SharedBytes::from_mmap(mmap), ParseStrategy::Sequential)
    }

    /// Same as [`from_path`] but always parses in parallel (requires `parallel` feature).
    ///
    /// Uses multiple threads to parse large files faster. Automatically detects
    /// gzip files and decompresses them when the gzip feature is enabled.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the chain file (plain text or gzip compressed)
    ///
    /// # Output
    ///
    /// Returns `Ok(Reader<Chain>)` containing all parsed chains, or `Err(ChainError)`
    /// if the file cannot be read or parsed
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// // Load a large file with parallel parsing
    /// let reader = Reader::<chaintools::Chain>::from_path_parallel("huge.chain")?;
    ///
    /// println!("Parsed {} chains in parallel", reader.len());
    /// ```
    #[cfg(feature = "parallel")]
    pub fn from_path_parallel<P: AsRef<Path>>(path: P) -> Result<Self, ChainError> {
        let path = path.as_ref();
        if is_gz_path(path) {
            #[cfg(feature = "gzip")]
            {
                let file = std::fs::File::open(path)?;
                let reader = BufReader::new(file);
                let mut decoder = MultiGzDecoder::new(reader);
                let mut buffer = Vec::new();
                decoder.read_to_end(&mut buffer)?;
                return Self::from_owned_bytes_parallel(buffer);
            }
            #[cfg(not(feature = "gzip"))]
            {
                return Err(gzip_feature_error());
            }
        }

        #[cfg(feature = "mmap")]
        {
            let file = std::fs::File::open(path)?;
            let mmap = unsafe { MmapOptions::new().map(&file)? };
            Self::build(SharedBytes::from_mmap(mmap), ParseStrategy::Parallel)
        }

        #[cfg(not(feature = "mmap"))]
        {
            let mut data = Vec::new();
            std::fs::File::open(path)?.read_to_end(&mut data)?;
            Self::from_owned_bytes_parallel(data)
        }
    }

    /// Construct from an owned buffer (no mmap).
    ///
    /// Parses chain data from an in-memory byte vector using sequential parsing.
    /// Useful when you already have the file contents loaded or working with
    /// dynamically generated chain data.
    ///
    /// # Arguments
    ///
    /// * `data` - Byte vector containing chain file contents
    ///
    /// # Output
    ///
    /// Returns `Ok(Reader<Chain>)` containing all parsed chains, or `Err(ChainError)`
    /// if the data cannot be parsed
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// let chain_data = b"chain 1 chr1 1000 + 0 1000 chr2 1000 + 0 1000 1\n10\n10\n";
    /// let reader = Reader::<chaintools::Chain>::from_owned_bytes(chain_data.to_vec())?;
    ///
    /// println!("Parsed {} chains from memory", reader.len());
    /// ```
    pub fn from_owned_bytes(data: Vec<u8>) -> Result<Self, ChainError> {
        Self::build(SharedBytes::from_owned(data), ParseStrategy::Sequential)
    }

    /// Construct from an owned buffer using parallel parsing (requires `parallel` feature).
    ///
    /// Parses chain data from an in-memory byte vector using multiple threads.
    /// Best for large in-memory datasets where parallel processing can provide
    /// significant speedup over sequential parsing.
    ///
    /// # Arguments
    ///
    /// * `data` - Byte vector containing chain file contents
    ///
    /// # Output
    ///
    /// Returns `Ok(Reader<Chain>)` containing all parsed chains, or `Err(ChainError)`
    /// if the data cannot be parsed
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// let chain_data = b"chain 1 chr1 1000 + 0 1000 chr2 1000 + 0 1000 1\n10\n10\n";
    /// let reader = Reader::<chaintools::Chain>::from_owned_bytes_parallel(chain_data.to_vec())?;
    ///
    /// println!("Parsed {} chains in parallel from memory", reader.len());
    /// ```
    #[cfg(feature = "parallel")]
    pub fn from_owned_bytes_parallel(data: Vec<u8>) -> Result<Self, ChainError> {
        Self::build(SharedBytes::from_owned(data), ParseStrategy::Parallel)
    }

    fn build(bytes: SharedBytes, strategy: ParseStrategy) -> Result<Self, ChainError> {
        let buf = bytes.as_slice();
        let (metas, blocks) = match strategy {
            ParseStrategy::Sequential => parse_chains_sequential(buf)?,
            #[cfg(feature = "parallel")]
            ParseStrategy::Parallel => parse_chains_parallel(buf)?,
        };
        let blocks_arc: Arc<Vec<Block>> = Arc::new(blocks);
        let chains = metas
            .into_iter()
            .map(|meta| Chain {
                score: meta.score,
                reference_name: ByteSlice::new(bytes.clone(), meta.reference_name),
                reference_size: meta.reference_size,
                reference_strand: meta.reference_strand,
                reference_start: meta.reference_start,
                reference_end: meta.reference_end,
                query_name: ByteSlice::new(bytes.clone(), meta.query_name),
                query_size: meta.query_size,
                query_strand: meta.query_strand,
                query_start: meta.query_start,
                query_end: meta.query_end,
                id: meta.id,
                blocks: BlockSlice::new(blocks_arc.clone(), meta.blocks),
            })
            .collect();
        Ok(Reader {
            _bytes: bytes,
            _blocks: blocks_arc,
            chains,
            _marker: PhantomData,
        })
    }

    /// Returns an iterator over all chains in the reader.
    ///
    /// # Arguments
    ///
    /// * `&self` - Reference to the reader
    ///
    /// # Output
    ///
    /// Returns an iterator yielding `&Chain` references
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// let reader = Reader::<chaintools::Chain>::from_path("example.chain")?;
    ///
    /// for chain in reader.chains() {
    ///     println!("Chain {}: score={}", chain.id, chain.score);
    /// }
    /// ```
    pub fn chains(&self) -> impl Iterator<Item = &Chain> {
        self.chains.iter()
    }

    /// Iterate metadata/comment lines from the original chain input.
    ///
    /// The iterator scans the reader's shared mmap/owned buffer without
    /// allocating and yields lines beginning with `#` in input order. Returned
    /// slices borrow this reader and omit newline terminators.
    pub fn metadata_lines(&self) -> MetadataIter<'_> {
        MetadataIter {
            bytes: self._bytes.as_slice(),
            position: 0,
        }
    }

    /// Returns the number of chains in the reader.
    ///
    /// # Arguments
    ///
    /// * `&self` - Reference to the reader
    ///
    /// # Output
    ///
    /// Returns the count of chains as `usize`
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// let reader = Reader::<chaintools::Chain>::from_path("example.chain")?;
    /// println!("Found {} chains", reader.len());
    /// ```
    pub fn len(&self) -> usize {
        self.chains.len()
    }

    /// Returns true if the reader contains no chains.
    ///
    /// # Arguments
    ///
    /// * `&self` - Reference to the reader
    ///
    /// # Output
    ///
    /// Returns `true` if empty, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use chaintools::Reader;
    ///
    /// let reader = Reader::<chaintools::Chain>::from_path("empty.chain")?;
    ///
    /// if reader.is_empty() {
    ///     println!("No chains found in file");
    /// }
    /// ```
    pub fn is_empty(&self) -> bool {
        self.chains.is_empty()
    }
}

#[derive(Clone, Copy)]
enum ParseStrategy {
    Sequential,
    #[cfg(feature = "parallel")]
    Parallel,
}