chromsize 0.0.34

just get your chrom sizes
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
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
//! chromsize
//! Alejandro Gonzales-Irribarren, 2024
//!
//! `chromsize` is a utility designed to extract chromosome names
//! and their corresponding lengths from FASTA and 2bit files. It supports
//! both plain and gzipped FASTA formats [and 2bit] and offers an option to
//! include only the accession ID from the FASTA headers.

use flate2::read::MultiGzDecoder;
use memmap2::Mmap;
use rayon::prelude::*;
use std::{
    fmt,
    fmt::Debug,
    fs::File,
    io::{self, BufWriter, Read, Write},
    path::Path,
};

const GZIP_MAGIC: [u8; 2] = [0x1f, 0x8b];
const TWOBIT_MAGIC: [u8; 4] = [0x1a, 0x41, 0x27, 0x43];
const TWOBIT_MAGIC_REV: [u8; 4] = [0x43, 0x27, 0x41, 0x1a];

/// Error types for the chromsize application.
///
/// This enum represents all possible error conditions that can occur
/// during sequence processing and chromosome size calculation.
#[derive(Debug)]
pub enum ChromsizeError {
    /// I/O related errors from file operations
    Io(io::Error),
    /// Empty input data (no content to process)
    EmptyInput,
    /// Invalid input format or unsupported content
    InvalidInput(String),
    /// Invalid FASTA format with descriptive message
    InvalidFasta(String),
}

impl fmt::Display for ChromsizeError {
    /// Formats the error for display purposes.
    ///
    /// Provides human-readable error messages for each error variant.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ChromsizeError::Io(err) => write!(f, "I/O error: {}", err),
            ChromsizeError::EmptyInput => write!(f, "Input is empty"),
            ChromsizeError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
            ChromsizeError::InvalidFasta(msg) => write!(f, "Invalid FASTA: {}", msg),
        }
    }
}

impl std::error::Error for ChromsizeError {
    /// Returns the underlying error source for error chaining.
    ///
    /// Only IO errors have an underlying source, other errors are standalone.
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ChromsizeError::Io(err) => Some(err),
            _ => None,
        }
    }
}

impl From<io::Error> for ChromsizeError {
    /// Converts I/O errors to ChromsizeError.
    ///
    /// This enables the `?` operator for I/O operations throughout the codebase.
    fn from(err: io::Error) -> Self {
        ChromsizeError::Io(err)
    }
}

/// Represents different types of input data storage.
///
/// This enum allows handling both memory-mapped files and owned buffers,
/// enabling efficient processing of different input sources.
enum InputData {
    /// Memory-mapped file data (zero-copy for regular files)
    Mmap(Mmap),
    /// Owned byte buffer (for stdin or decompressed data)
    Owned(Vec<u8>),
}

enum InputFormat {
    Fasta,
    TwoBit,
}

impl AsRef<[u8]> for InputData {
    /// Provides read access to the underlying byte data.
    ///
    /// This enables uniform processing regardless of whether the data
    /// comes from a memory-mapped file or an owned buffer.
    fn as_ref(&self) -> &[u8] {
        match self {
            InputData::Mmap(m) => m.as_ref(),
            InputData::Owned(b) => b.as_slice(),
        }
    }
}

/// Extracts chromosome sizes from a sequence file.
///
/// This function processes a sequence file (from file path or stdin) and returns
/// a vector of tuples containing chromosome names and their corresponding sizes.
/// Input format is detected by content (FASTA header or 2bit signature).
///
/// # Arguments
///
/// * `sequence` - Path to a FASTA/2bit file or "-" for stdin
///
/// # Returns
///
/// Vector of (chromosome_name, size) pairs
///
/// # Examples
///
/// ```ignore
/// let sizes = get_sizes("genome.fa")?;
/// // sizes: [("chr1", 248956422), ("chr2", 242193529), ...]
/// ```
pub fn get_sizes<T: AsRef<Path> + Debug>(
    sequence: T,
) -> Result<Vec<(String, u64)>, ChromsizeError> {
    let path = sequence.as_ref();
    let data = if is_stdin(path) {
        from_stdin()?
    } else {
        from_file(path)?
    };

    sizes_from_bytes(data.as_ref())
}

/// Checks if the path represents stdin input.
///
/// Returns true if the path is "-" which conventionally means read from stdin.
/// Checks if the path represents stdin input.
///
/// Returns true if the path is "-" which conventionally means read from stdin.
fn is_stdin(path: &Path) -> bool {
    path == Path::new("-")
}

/// Reads data from standard input with optional gzip decompression.
///
/// This function reads all data from stdin, detects if it's gzip-compressed,
/// and decompresses it if necessary. Returns the data in an owned buffer.
///
/// # Returns
///
/// InputData::Owned containing the raw or decompressed input data
/// Reads data from standard input with optional gzip decompression.
///
/// This function reads all data from stdin, detects if it's gzip-compressed,
/// and decompresses it if necessary. Returns the data in an owned buffer.
///
/// # Returns
///
/// InputData::Owned containing the raw or decompressed input data
fn from_stdin() -> Result<InputData, ChromsizeError> {
    let mut buffer = Vec::with_capacity(1024 * 1024);
    let mut handle = io::stdin().lock();
    handle.read_to_end(&mut buffer)?;

    if buffer.is_empty() {
        return Err(ChromsizeError::EmptyInput);
    }

    if is_gzip(&buffer) {
        let decompressed = decompress_gzip(&buffer)?;

        if decompressed.is_empty() {
            return Err(ChromsizeError::EmptyInput);
        }

        Ok(InputData::Owned(decompressed))
    } else {
        Ok(InputData::Owned(buffer))
    }
}

/// Reads data from a file with memory mapping and optional gzip decompression.
///
/// This function attempts to memory-map the file for efficient access.
/// If the file is gzip-compressed, it decompresses the entire content
/// into an owned buffer instead.
///
/// # Arguments
///
/// * `path` - Path to the input file
///
/// # Returns
///
/// InputData::Mmap for uncompressed files, InputData::Owned for compressed files
/// Reads data from a file with memory mapping and optional gzip decompression.
///
/// This function attempts to memory-map the file for efficient access.
/// If the file is gzip-compressed, it decompresses the entire content
/// into an owned buffer instead.
///
/// # Arguments
///
/// * `path` - Path to the input file
///
/// # Returns
///
/// InputData::Mmap for uncompressed files, InputData::Owned for compressed files
fn from_file(path: &Path) -> Result<InputData, ChromsizeError> {
    let file = File::open(path)?;
    let mmap = unsafe { Mmap::map(&file)? };

    if mmap.is_empty() {
        return Err(ChromsizeError::EmptyInput);
    }

    if is_gzip(&mmap) {
        let decompressed = decompress_gzip(&mmap)?;
        if decompressed.is_empty() {
            return Err(ChromsizeError::EmptyInput);
        }
        Ok(InputData::Owned(decompressed))
    } else {
        Ok(InputData::Mmap(mmap))
    }
}

/// Reads 2bit data and extracts chromosome sizes.
///
/// This internal helper function opens and reads 2bit data using the `twobit` crate,
/// then extracts chromosome names and their corresponding sizes. The function converts
/// chromosome sizes from `usize` to `u64` for consistency with other chromsize functions.
///
/// # Arguments
/// * `twobit` - Raw 2bit data to be processed.
///
/// # Returns
/// A `Result` containing a `Vec` of `(String, u64)` tuples representing chromosome names
/// and their sizes in base pairs, or an error if the data cannot be parsed.
///
fn from_2bit(twobit: &[u8]) -> Result<Vec<(String, u64)>, ChromsizeError> {
    let genome = twobit::TwoBitFile::from_buf(twobit)
        .map_err(|e| ChromsizeError::InvalidInput(format!("Invalid 2bit data: {e}")))?;

    let cs = genome
        .chrom_names()
        .into_iter()
        .zip(genome.chrom_sizes())
        .map(|(chr, size)| (chr, size as u64))
        .collect();

    Ok(cs)
}

/// Processes raw bytes to extract chromosome sizes by detecting format.
///
/// This function acts as a dispatcher that determines the input format
/// (FASTA or 2bit) and routes the data to the appropriate processor.
/// It returns an error if the format is not recognized.
///
/// # Arguments
///
/// * `data` - Raw byte data to be processed
///
/// # Returns
///
/// Vector of (chromosome_name, size) pairs or error for unsupported format
///
/// # Examples
///
/// ```ignore
/// let data = b">chr1\nATGC\n>chr2\nGCTA";
/// let sizes = sizes_from_bytes(data)?;
/// // sizes: [("chr1", 4), ("chr2", 4)]
/// ```
fn sizes_from_bytes(data: &[u8]) -> Result<Vec<(String, u64)>, ChromsizeError> {
    match sniff_format(data) {
        Some(InputFormat::TwoBit) => from_2bit(data),
        Some(InputFormat::Fasta) => chromsize(data),
        None => Err(ChromsizeError::InvalidInput(
            "Input format not recognized (expected FASTA or 2bit)".to_string(),
        )),
    }
}

/// Detects the input format by examining file magic bytes and patterns.
///
/// This function identifies whether the data is in 2bit format (by checking
/// the 4-byte magic signature) or FASTA format (by checking for the '>' header
/// character). Returns None for unrecognized formats.
///
/// # Arguments
///
/// * `data` - Raw byte data to examine for format identification
///
/// # Returns
///
/// Some(InputFormat) if format is recognized, None otherwise
fn sniff_format(data: &[u8]) -> Option<InputFormat> {
    if data.len() >= TWOBIT_MAGIC.len()
        && (data[..TWOBIT_MAGIC.len()] == TWOBIT_MAGIC
            || data[..TWOBIT_MAGIC_REV.len()] == TWOBIT_MAGIC_REV)
    {
        return Some(InputFormat::TwoBit);
    }

    if data.first() == Some(&b'>') {
        return Some(InputFormat::Fasta);
    }

    None
}

/// Detects if data is gzip-compressed by checking magic bytes.
///
/// Gzip files start with the magic bytes 0x1f 0x8b.
///
/// # Arguments
///
/// * `bytes` - Byte slice to check
///
/// # Returns
///
/// true if the data appears to be gzip-compressed
/// Detects if data is gzip-compressed by checking magic bytes.
///
/// Gzip files start with the magic bytes 0x1f 0x8b.
///
/// # Arguments
///
/// * `bytes` - Byte slice to check
///
/// # Returns
///
/// true if the data appears to be gzip-compressed
fn is_gzip(bytes: &[u8]) -> bool {
    bytes.len() >= 2 && bytes[0] == GZIP_MAGIC[0] && bytes[1] == GZIP_MAGIC[1]
}

/// Decompresses gzip data into a byte vector.
///
/// This function handles potentially large gzip files efficiently
/// by using a reasonably sized initial buffer.
///
/// # Arguments
///
/// * `data` - Compressed gzip data
///
/// # Returns
///
/// Decompressed data in a new Vec<u8>
/// Decompresses gzip data into a byte vector.
///
/// This function handles potentially large gzip files efficiently
/// by using a reasonably sized initial buffer.
///
/// # Arguments
///
/// * `data` - Compressed gzip data
///
/// # Returns
///
/// Decompressed data in a new Vec<u8>
fn decompress_gzip(data: &[u8]) -> Result<Vec<u8>, ChromsizeError> {
    let mut decoder = MultiGzDecoder::new(data);
    let mut buffer = Vec::with_capacity(8 * 1024 * 1024);
    decoder.read_to_end(&mut buffer)?;
    Ok(buffer)
}

/// Processes FASTA data to extract chromosome sizes in parallel.
///
/// This is the core processing function that validates FASTA format
/// and uses parallel processing to handle multiple chromosomes efficiently.
///
/// # Arguments
///
/// * `data` - Raw FASTA data as bytes
///
/// # Returns
///
/// Vector of (chromosome_name, size) pairs
/// Processes FASTA data to extract chromosome sizes in parallel.
///
/// This is the core processing function that validates FASTA format
/// and uses parallel processing to handle multiple chromosomes efficiently.
///
/// # Arguments
///
/// * `data` - Raw FASTA data as bytes
///
/// # Returns
///
/// Vector of (chromosome_name, size) pairs
fn chromsize(data: &[u8]) -> Result<Vec<(String, u64)>, ChromsizeError> {
    if data.is_empty() {
        return Err(ChromsizeError::EmptyInput);
    }

    if data[0] != b'>' {
        return Err(ChromsizeError::InvalidFasta(
            "Input does not start with '>'".to_string(),
        ));
    }

    data.par_split(|&c| c == b'>')
        .filter(|chunk| !chunk.is_empty())
        .map(process_record)
        .collect()
}

/// Processes a single FASTA record to extract header and count sequence length.
///
/// This function parses a FASTA record (header + sequence), validates the format,
/// and counts the total number of valid sequence characters while enforcing
/// strict FASTA format compliance.
///
/// # Arguments
///
/// * `chunk` - FASTA record data without the leading '>' character
///
/// # Returns
///
/// Tuple of (header_string, sequence_length)
/// Processes a single FASTA record to extract header and count sequence length.
///
/// This function parses a FASTA record (header + sequence), validates the format,
/// and counts the total number of valid sequence characters while enforcing
/// strict FASTA format compliance.
///
/// # Arguments
///
/// * `chunk` - FASTA record data without the leading '>' character
///
/// # Returns
///
/// Tuple of (header_string, sequence_length)
fn process_record(chunk: &[u8]) -> Result<(String, u64), ChromsizeError> {
    let Some(stop) = memchr::memchr(b'\n', chunk) else {
        return Err(ChromsizeError::InvalidFasta(
            "Record header is not terminated by a newline".to_string(),
        ));
    };

    let header = std::str::from_utf8(&chunk[..stop])
        .map_err(|_| ChromsizeError::InvalidFasta("Record header is not UTF-8".to_string()))?
        .trim();

    if header.is_empty() {
        return Err(ChromsizeError::InvalidFasta(
            "Record has an empty header".to_string(),
        ));
    }

    let data = &chunk[stop + 1..];

    if memchr::memchr2(b' ', b'\t', data).is_some() {
        return Err(ChromsizeError::InvalidFasta(
            "Record contains whitespace inside sequence data".to_string(),
        ));
    }

    if memchr::memchr(b'>', data).is_some() {
        return Err(ChromsizeError::InvalidFasta(
            "Record contains '>' inside sequence data".to_string(),
        ));
    }

    if data.iter().any(|&b| match b {
        b'\n' | b'\r' => false,
        0x00..=0x08 | 0x0b | 0x0c | 0x0e..=0x1f => true,
        0x20..=0x7e => false,
        _ => true,
    }) {
        return Err(ChromsizeError::InvalidFasta(
            "Record contains invalid control or non-ASCII characters".to_string(),
        ));
    }

    let count_newlines = bytecount::count(data, b'\n') as u64;
    let count_cr = bytecount::count(data, b'\r') as u64;
    let totals = data.len() as u64 - count_newlines - count_cr;

    Ok((header.to_string(), totals))
}

/// Writes chromosome sizes to a tab-delimited file.
///
/// This function takes the calculated chromosome sizes and writes them
/// to the specified output file in a standard two-column format:
/// chromosome_name<TAB>size
///
/// # Arguments
///
/// * `sizes` - Vector of (chromosome_name, size) tuples
/// * `out` - Output file path
///
/// # Examples
///
/// ```ignore
/// let sizes = vec![("chr1".to_string(), 248956422), ("chr2".to_string(), 242193529)];
/// writer(&sizes, "chrom.sizes")?;
/// // Output file contains:
/// // chr1    248956422
/// // chr2    242193529
/// ```
pub fn writer<T>(sizes: &[(String, u64)], outdir: T, prefix: String) -> Result<(), ChromsizeError>
where
    T: AsRef<Path> + Debug,
{
    std::fs::create_dir_all(&outdir)?;
    let file = File::create(outdir.as_ref().join(prefix))?;
    let mut writer = BufWriter::with_capacity(64 * 1024, file);

    for (k, v) in sizes.iter() {
        writeln!(writer, "{}\t{}", k, v)?;
    }

    Ok(())
}