gxf2bed 0.3.2

fastest GTF/GFF-to-BED converter chilling around
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use crate::cli::BedType;
use crate::config::Config;
use crate::detect::{detect_input_kind, Compression, InputFormat};
use crate::error::Result;
use crate::memory::max_mem_usage_mb;
use genepred::{Bed12, Bed3, Bed4, Bed5, Bed6, Bed9, GenePred, Gff, Gtf, Reader, ReaderOptions};
use genepred::{Writer, WriterOptions};
use rayon::prelude::*;
use std::io::{BufWriter, Write};
use std::path::Path;
use std::time::{Duration, Instant};

/// Summary statistics for a conversion run.
#[derive(Debug, Clone, Copy)]
pub struct RunStats {
    /// Wall clock time spent in the conversion.
    pub elapsed: Duration,
    /// Delta in maximum RSS memory usage, in MB.
    pub mem_delta_mb: f64,
}

/// Runs a conversion with the provided configuration.
///
/// This function orchestrates the entire GTF/GFF to BED conversion process,
/// including input detection, parallel processing, and output writing.
///
/// # Arguments
///
/// * `config` - Configuration containing all conversion parameters
///
/// # Returns
///
/// Returns RunStats containing timing and memory usage information.
///
/// # Errors
///
/// Returns an error if any step of the conversion fails.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::{Config, run};
/// use std::path::PathBuf;
///
/// let config = Config {
///     input: PathBuf::from("input.gtf"),
///     output: PathBuf::from("output.bed"),
///     // ... other fields
/// };
/// let stats = run(&config)?;
/// println!("Conversion took: {:?}", stats.elapsed);
/// ```
pub fn run(config: &Config) -> Result<RunStats> {
    let start = Instant::now();
    let start_mem = max_mem_usage_mb();

    let reader_options = build_reader_options(config);
    let writer_options = build_writer_options(config);
    let input_kind = detect_input_kind(&config.input)?;

    let pool = rayon::ThreadPoolBuilder::new()
        .num_threads(config.threads)
        .build()?;

    let chunks = pool.install(|| {
        process_input(
            &config.input,
            &input_kind,
            reader_options,
            config,
            &writer_options,
        )
    })?;

    write_output(&config.output, chunks)?;

    let elapsed = start.elapsed();
    let mem_delta = (max_mem_usage_mb() - start_mem).max(0.0);

    Ok(RunStats {
        elapsed,
        mem_delta_mb: mem_delta,
    })
}

/// Processes an input file based on the detected format.
///
/// This function dispatches to the appropriate reader type based on whether
/// the input is GTF or GFF format.
///
/// # Arguments
///
/// * `path` - Path to the input file
/// * `input_kind` - Detected format and compression information
/// * `reader_options` - Reader configuration options
/// * `config` - Main conversion configuration
/// * `writer_options` - Writer configuration options
///
/// # Returns
///
/// Returns processed data chunks ready for output.
///
/// # Errors
///
/// Returns an error if the input processing fails.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::process_input;
/// use std::path::Path;
/// // This function is typically called internally by run()
/// ```
fn process_input(
    path: &Path,
    input_kind: &crate::detect::InputKind,
    reader_options: ReaderOptions<'_>,
    config: &Config,
    writer_options: &WriterOptions,
) -> Result<Vec<(usize, Vec<u8>)>> {
    match input_kind.format {
        InputFormat::Gtf => process_reader::<Gtf>(
            path,
            reader_options,
            config,
            writer_options,
            input_kind.compression,
        ),
        InputFormat::Gff => process_reader::<Gff>(
            path,
            reader_options,
            config,
            writer_options,
            input_kind.compression,
        ),
    }
}

/// Processes a reader into in-memory BED chunks.
///
/// This function opens the appropriate reader, processes the input in parallel chunks,
/// and renders each chunk into BED format bytes.
///
/// # Arguments
///
/// * `path` - Path to the input file
/// * `reader_options` - Reader configuration options
/// * `config` - Main conversion configuration
/// * `writer_options` - Writer configuration options
/// * `compression` - Compression type of the input
///
/// # Returns
///
/// Returns ordered chunks of BED data ready for writing.
///
/// # Errors
///
/// Returns an error if reading or processing fails.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::process_reader;
/// use std::path::Path;
/// // This function is typically called internally by process_input()
/// ```
fn process_reader<F>(
    path: &Path,
    reader_options: ReaderOptions<'_>,
    config: &Config,
    writer_options: &WriterOptions,
    compression: Compression,
) -> Result<Vec<(usize, Vec<u8>)>>
where
    F: GxfReader + Send,
{
    let reader = open_reader::<F>(path, reader_options, compression)?;
    let mut outputs = reader
        .par_chunks(config.chunks)?
        .map(|(idx, chunk)| render_chunk(idx, chunk, config.bed_type, writer_options))
        .collect::<Vec<_>>();

    let mut merged = Vec::with_capacity(outputs.len());
    for output in outputs.drain(..) {
        merged.push(output?);
    }

    merged.sort_by_key(|(idx, _)| *idx);
    Ok(merged)
}

/// Opens a GTF/GFF reader with mmap or buffered I/O based on compression.
///
/// For compressed files, uses buffered I/O. For uncompressed files,
/// uses memory mapping for better performance.
///
/// # Arguments
///
/// * `path` - Path to the input file
/// * `options` - Reader configuration options
/// * `compression` - Compression type of the input
///
/// # Returns
///
/// Returns a configured reader for the specified format.
///
/// # Errors
///
/// Returns an error if the reader cannot be opened.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::open_reader;
/// use std::path::Path;
/// // This function is typically called internally by process_reader()
/// ```
fn open_reader<F: GxfReader>(
    path: &Path,
    options: ReaderOptions<'_>,
    compression: Compression,
) -> Result<Reader<F>> {
    if compression.is_compressed() {
        Ok(F::from_gxf_with_options(path, options)?)
    } else {
        Ok(F::from_mmap_with_options(path, options)?)
    }
}

/// Builds reader options from the config.
///
/// Constructs ReaderOptions based on the configuration parameters
/// for parent/child features and attributes.
///
/// # Arguments
///
/// * `config` - Configuration containing reader preferences
///
/// # Returns
///
/// Returns configured ReaderOptions for the genepred reader.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::build_reader_options;
/// use gxf2bed::Config;
/// let config = Config { /* ... */ };
/// let options = build_reader_options(&config);
/// ```
fn build_reader_options(config: &Config) -> ReaderOptions<'_> {
    let mut options = ReaderOptions::default();

    if let Some(parent) = &config.parent_feature {
        options = options.parent_feature(parent.as_bytes());
    }
    if let Some(childs) = &config.child_features {
        let filtered: Vec<&[u8]> = childs
            .iter()
            .filter(|c| !c.is_empty())
            .map(|c| c.as_bytes())
            .collect();
        if filtered.is_empty() {
            options = options.clear_child_features();
        } else {
            options = options.child_features(filtered);
        }
    }
    if let Some(parent) = &config.parent_attribute {
        options = options.parent_attribute(parent.as_bytes());
    }
    if let Some(child) = &config.child_attribute {
        options = options.child_attribute(child.as_bytes());
    }

    options
}

/// Builds writer options from the config.
///
/// Constructs WriterOptions based on additional field configuration.
/// If additional fields are specified, configures the allowlist
/// and enables non-numeric extras.
///
/// # Arguments
///
/// * `config` - Configuration containing writer preferences
///
/// # Returns
///
/// Returns configured WriterOptions for the BED writer.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::build_writer_options;
/// use gxf2bed::Config;
/// let config = Config { /* ... */ };
/// let options = build_writer_options(&config);
/// ```
fn build_writer_options(config: &Config) -> WriterOptions {
    if let Some(additional_fields) = &config.additional_fields {
        let mut options = WriterOptions::default();
        options = options.extras_allowlist(additional_fields.clone());
        options = options.include_non_numeric_extras(true);
        options
    } else {
        WriterOptions::default()
    }
}

/// Renders a chunk of parsed records into BED bytes.
///
/// Converts a chunk of GenePred records into the specified BED format
/// and returns the rendered bytes along with the chunk index.
///
/// # Arguments
///
/// * `idx` - Index of this chunk for ordering
/// * `chunk` - Vector of parsed GenePred records
/// * `bed_type` - Target BED format type
/// * `writer_options` - Writer configuration options
///
/// # Returns
///
/// Returns a tuple containing the chunk index and rendered BED bytes.
///
/// # Errors
///
/// Returns an error if record parsing or writing fails.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::render_chunk;
/// // This function is typically called internally by process_reader()
/// ```
fn render_chunk(
    idx: usize,
    chunk: Vec<genepred::ReaderResult<GenePred>>,
    bed_type: BedType,
    writer_options: &WriterOptions,
) -> Result<(usize, Vec<u8>)> {
    let mut buffer = Vec::with_capacity(chunk.len().saturating_mul(128));
    {
        let mut writer = BufWriter::with_capacity(128 * 1024, &mut buffer);
        for record in chunk {
            let record = record?;
            write_record(&record, &mut writer, bed_type, writer_options)?;
        }
        writer.flush()?;
    }
    Ok((idx, buffer))
}

/// Writes a single record in the configured BED format.
///
/// Converts a GenePred record to the specified BED format and writes it
/// to the provided writer using the configured options.
///
/// # Arguments
///
/// * `record` - GenePred record to convert
/// * `writer` - Output writer to write the BED record to
/// * `bed_type` - Target BED format type
/// * `writer_options` - Writer configuration options
///
/// # Returns
///
/// Returns Ok(()) on successful write.
///
/// # Errors
///
/// Returns an error if the record cannot be written in the specified format.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::write_record;
/// use std::io::Write;
/// // This function is typically called internally by render_chunk()
/// ```
fn write_record<W: Write>(
    record: &GenePred,
    writer: &mut W,
    bed_type: BedType,
    writer_options: &WriterOptions,
) -> Result<()> {
    match bed_type {
        BedType::Bed3 => Writer::<Bed3>::from_record_with_options(record, writer, writer_options)?,
        BedType::Bed4 => Writer::<Bed4>::from_record_with_options(record, writer, writer_options)?,
        BedType::Bed5 => Writer::<Bed5>::from_record_with_options(record, writer, writer_options)?,
        BedType::Bed6 => Writer::<Bed6>::from_record_with_options(record, writer, writer_options)?,
        BedType::Bed9 => Writer::<Bed9>::from_record_with_options(record, writer, writer_options)?,
        BedType::Bed12 => {
            Writer::<Bed12>::from_record_with_options(record, writer, writer_options)?
        }
    }
    Ok(())
}

/// Writes ordered chunks to the output path.
///
/// Creates the output file and writes all chunks in order,
/// combining the parallel processed data into a single BED file.
///
/// # Arguments
///
/// * `path` - Output file path to write to
/// * `chunks` - Ordered vector of (index, data) tuples
///
/// # Returns
///
/// Returns Ok(()) on successful write.
///
/// # Errors
///
/// Returns an error if file creation or writing fails.
///
/// # Example
///
/// ```rust, ignore
/// use gxf2bed::convert::write_output;
/// use std::path::Path;
/// let chunks = vec![(0, b"chr1\t100\t200\n".to_vec())];
/// write_output(Path::new("output.bed"), chunks)?;
/// ```
fn write_output(path: &Path, chunks: Vec<(usize, Vec<u8>)>) -> Result<()> {
    let file = std::fs::File::create(path)?;
    let mut writer = BufWriter::with_capacity(256 * 1024, file);
    for (_, buffer) in chunks {
        writer.write_all(&buffer)?;
    }
    writer.flush()?;
    Ok(())
}

/// Trait for opening GTF/GFF readers with custom options.
///
/// This trait abstracts over GTF and GFF readers, providing a common interface
/// for opening readers with custom configuration options.
trait GxfReader: genepred::BedFormat + Into<GenePred> + Sized {
    /// Opens a buffered reader with custom options.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the input file
    /// * `options` - Reader configuration options
    ///
    /// # Returns
    ///
    /// Returns a Reader for the specified format.
    ///
    /// # Example
    ///
    /// ```rust, ignore
    /// use gxf2bed::convert::GxfReader;
    /// use genepred::Gtf;
    /// let reader = Gtf::from_gxf_with_options("file.gtf", options)?;
    /// ```
    fn from_gxf_with_options<P: AsRef<Path>>(
        path: P,
        options: ReaderOptions<'_>,
    ) -> genepred::ReaderResult<Reader<Self>>;

    /// Opens a memory-mapped reader with custom options.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the input file
    /// * `options` - Reader configuration options
    ///
    /// # Returns
    ///
    /// Returns a memory-mapped Reader for the specified format.
    ///
    /// # Example
    ///
    /// ```rust, ignore
    /// use gxf2bed::convert::GxfReader;
    /// use genepred::Gtf;
    /// let reader = Gtf::from_mmap_with_options("file.gtf", options)?;
    /// ```
    fn from_mmap_with_options<P: AsRef<Path>>(
        path: P,
        options: ReaderOptions<'_>,
    ) -> genepred::ReaderResult<Reader<Self>>;
}

impl GxfReader for Gtf {
    /// Opens a buffered GTF reader with custom options.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the GTF file
    /// * `options` - Reader configuration options
    ///
    /// # Returns
    ///
    /// Returns a buffered GTF Reader.
    ///
    /// # Example
    ///
    /// ```rust, ignore
    /// use gxf2bed::convert::GxfReader;
    /// use genepred::Gtf;
    /// let reader = Gtf::from_gxf_with_options("file.gtf", options)?;
    /// ```
    fn from_gxf_with_options<P: AsRef<Path>>(
        path: P,
        options: ReaderOptions<'_>,
    ) -> genepred::ReaderResult<Reader<Self>> {
        Reader::<Gtf>::from_gxf_with_options(path, options)
    }

    /// Opens a memory-mapped GTF reader with custom options.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the GTF file
    /// * `options` - Reader configuration options
    ///
    /// # Returns
    ///
    /// Returns a memory-mapped GTF Reader.
    ///
    /// # Example
    ///
    /// ```rust, ignore
    /// use gxf2bed::convert::GxfReader;
    /// use genepred::Gtf;
    /// let reader = Gtf::from_mmap_with_options("file.gtf", options)?;
    /// ```
    fn from_mmap_with_options<P: AsRef<Path>>(
        path: P,
        options: ReaderOptions<'_>,
    ) -> genepred::ReaderResult<Reader<Self>> {
        Reader::<Gtf>::from_mmap_with_options(path, options)
    }
}

impl GxfReader for Gff {
    /// Opens a buffered GFF reader with custom options.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the GFF file
    /// * `options` - Reader configuration options
    ///
    /// # Returns
    ///
    /// Returns a buffered GFF Reader.
    ///
    /// # Example
    ///
    /// ```rust, ignore
    /// use gxf2bed::convert::GxfReader;
    /// use genepred::Gff;
    /// let reader = Gff::from_gxf_with_options("file.gff", options)?;
    /// ```
    fn from_gxf_with_options<P: AsRef<Path>>(
        path: P,
        options: ReaderOptions<'_>,
    ) -> genepred::ReaderResult<Reader<Self>> {
        Reader::<Gff>::from_gxf_with_options(path, options)
    }

    /// Opens a memory-mapped GFF reader with custom options.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the GFF file
    /// * `options` - Reader configuration options
    ///
    /// # Returns
    ///
    /// Returns a memory-mapped GFF Reader.
    ///
    /// # Example
    ///
    /// ```rust, ignore
    /// use gxf2bed::convert::GxfReader;
    /// use genepred::Gff;
    /// let reader = Gff::from_mmap_with_options("file.gff", options)?;
    /// ```
    fn from_mmap_with_options<P: AsRef<Path>>(
        path: P,
        options: ReaderOptions<'_>,
    ) -> genepred::ReaderResult<Reader<Self>> {
        Reader::<Gff>::from_mmap_with_options(path, options)
    }
}