frugal 0.4.2

Fast rosella prodigal: metagenomic gene prediction, a fork of prodigal-rs
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
/*******************************************************************************
    PRODIGAL (PROkaryotic DynamIc Programming Genefinding ALgorithm)
    Copyright (C) 2007-2016 University of Tennessee / UT-Battelle

    Code Author:  Doug Hyatt

    Rust port of main.c — the pipeline orchestration module.
*******************************************************************************/

use std::ffi::CStr;
use std::mem::MaybeUninit;
use std::os::raw::{c_char, c_int};

use crate::types::{Gene, Mask, MetagenomicBin, Node, Training};
use crate::types::{MAX_GENES, MAX_LINE, MAX_MASKS, MAX_SEQ, NUM_META, STT_NOD};

mod emit;
mod handles;
mod meta;
mod train;

const VERSION: &str = "2.6.3";
const DATE: &str = "February, 2016";
const VERSION_CSTR: &[u8] = b"2.6.3\0";

/// Parsed pipeline configuration — constructed by the CLI (clap) layer.
pub struct PipelineConfig {
    pub input_file: Option<String>,
    pub output_file: Option<String>,
    pub trans_file: Option<String>,
    pub nuc_file: Option<String>,
    pub start_file: Option<String>,
    pub train_file: Option<String>,
    pub output_format: i32,
    pub trans_table: i32,
    pub closed: bool,
    pub do_mask: bool,
    pub force_nonsd: bool,
    pub is_meta: bool,
    pub quiet: bool,
}

use crate::dprog::{dprog, eliminate_bad_genes};
use crate::gene::{add_genes, record_gene_data, tweak_final_starts};
use crate::metagenomic::initialize_metagenomic_metadata;
use crate::node::{add_nodes, node_room, record_overlapping_starts, score_nodes, write_start_file};
use crate::reader::seq_reader_seek;
use crate::sequence::{calc_short_header, next_seq_multi, rcom_seq};
use crate::training::{read_training_file, write_training_file};

use emit::write_genes;
use handles::Handles;

unsafe fn sorted_nodes(
    seq: *mut u8,
    rseq: *mut u8,
    slen: c_int,
    nodes: *mut Node,
    closed: c_int,
    mlist: *mut Mask,
    nmask: c_int,
    tinf: *const Training,
) -> c_int {
    let nn = add_nodes(seq, rseq, slen, nodes, closed, mlist, nmask, tinf);
    std::slice::from_raw_parts_mut(nodes, nn as usize)
        .sort_unstable_by(|a, b| a.ndx.cmp(&b.ndx).then(b.strand.cmp(&a.strand)));
    nn
}

// ---------------------------------------------------------------------------
// Main pipeline: replaces C main()
// ---------------------------------------------------------------------------
/// Orchestrate the full Prodigal pipeline (port of C `main`).
///
/// Opens input/output handles, optionally reads or writes a training file, and
/// then for each sequence either runs the single-genome path (self-training
/// followed by gene finding) or the metagenomic path (scoring against the 50
/// pre-built bins and keeping the best-scoring one). Returns the process exit
/// code: 0 on success, non-zero on failure (matching the C exit codes).
#[allow(unused_assignments)]
pub unsafe fn run_pipeline(config: &PipelineConfig) -> i32 {
    let train_cstr = config
        .train_file
        .as_ref()
        .map(|s| std::ffi::CString::new(s.as_str()).unwrap());

    // Variable declarations
    let mut rv: c_int;
    let mut slen: c_int;
    let mut nn: c_int;
    let mut ng: c_int;
    let mut ipath: c_int;
    let mut do_training: c_int;
    let output: c_int = config.output_format;
    let mut max_phase: c_int;
    let closed: c_int = config.closed as c_int;
    let do_mask: c_int = config.do_mask as c_int;
    let mut nmask: c_int;
    let force_nonsd: c_int = config.force_nonsd as c_int;
    let user_tt: c_int = config.trans_table;
    let is_meta: c_int = config.is_meta as c_int;
    let mut num_seq: c_int;
    let quiet: c_int = config.quiet as c_int;
    let mut gc: f64 = 0.0;

    let train_file: *const c_char = train_cstr.as_ref().map_or(std::ptr::null(), |c| c.as_ptr());

    let mut cur_header: [c_char; MAX_LINE] = [0; MAX_LINE];
    let mut new_header: [c_char; MAX_LINE] = [0; MAX_LINE];
    let mut short_header: [c_char; MAX_LINE] = [0; MAX_LINE];

    // Reserve the same C-style work buffers without touching every page up
    // front. Nodes and genes are zeroed as individual records are emitted.
    let mut seq_vec: Vec<u8> = vec![0u8; MAX_SEQ / 4];
    let mut rseq_vec: Vec<u8> = vec![0u8; MAX_SEQ / 4];
    let mut useq_vec: Vec<u8> = vec![0u8; MAX_SEQ / 8];
    let mut nodes_vec: Vec<MaybeUninit<Node>> = Vec::with_capacity(STT_NOD);
    let mut genes_vec: Vec<MaybeUninit<Gene>> = Vec::with_capacity(MAX_GENES);

    let seq = seq_vec.as_mut_ptr();
    let rseq = rseq_vec.as_mut_ptr();
    let useq = useq_vec.as_mut_ptr();
    let mut nodes = nodes_vec.as_mut_ptr() as *mut Node;
    let genes = genes_vec.as_mut_ptr() as *mut Gene;

    let mut tinf: Training = std::mem::zeroed();

    let mut meta: [MetagenomicBin; NUM_META] = std::mem::zeroed();
    let mut mlist: [Mask; MAX_MASKS] = [Mask { begin: 0, end: 0 }; MAX_MASKS];

    let mut meta_gc: [f64; NUM_META] = [0.0; NUM_META];
    let mut meta_trans_table: [c_int; NUM_META] = [0; NUM_META];
    let mut meta_tinf: Training = std::mem::zeroed();
    let mut best_tinf: Training = std::mem::zeroed();

    // Initialize variables
    nn = 0;
    slen = 0;
    ipath = 0;
    ng = 0;
    nmask = 0;
    num_seq = 0;
    max_phase = 0;
    do_training = 0;

    // Set default training parameters
    tinf.st_wt = 4.35;
    tinf.trans_table = if user_tt > 0 { user_tt } else { 11 };

    // Print header
    if quiet == 0 {
        eprintln!("-------------------------------------");
        eprintln!("PRODIGAL v{} [{}]         ", VERSION, DATE);
        eprintln!("Univ of Tenn / Oak Ridge National Lab");
        eprintln!("Doug Hyatt, Loren Hauser, et al.     ");
        eprintln!("-------------------------------------");
    }

    // Read in the training file (if specified)
    if !train_file.is_null() {
        if is_meta == 1 {
            eprint!("\nError: cannot specify metagenomic sequence with a");
            eprintln!(" training file.");
            return 2;
        }
        rv = read_training_file(train_file, &mut tinf);
        if rv == 1 {
            do_training = 1;
        } else {
            if force_nonsd == 1 {
                eprint!("\nError: cannot force non-SD finder with a training");
                eprintln!(" file already created!");
                return 3;
            }
            if quiet == 0 {
                let tf_str = CStr::from_ptr(train_file).to_string_lossy();
                eprint!("Reading in training data from file {}...", tf_str);
            }
            if user_tt > 0 && user_tt != tinf.trans_table {
                eprint!("\n\nWarning: user-specified translation table does");
                eprintln!("not match the one in the specified training file! \n");
            }
            if rv == -1 {
                eprintln!("\n\nError: training file did not read correctly!");
                return 4;
            }
            if quiet == 0 {
                eprintln!("done!");
                eprintln!("-------------------------------------");
            }
        }
    }

    let handles = match Handles::open(config) {
        Ok(handles) => handles,
        Err(code) => return code,
    };

    // =========================================================================
    // Single Genome Training
    // =========================================================================
    if is_meta == 0 && (do_training == 1 || (do_training == 0 && train_file.is_null())) {
        match train::train_single(
            handles.input,
            seq,
            rseq,
            useq,
            &mut nodes_vec,
            &mut tinf,
            do_mask,
            mlist.as_mut_ptr(),
            &mut nmask,
            closed,
            force_nonsd,
            quiet,
        ) {
            Ok((trained, trained_nodes, trained_nn)) => {
                slen = trained;
                nodes = trained_nodes;
                nn = trained_nn;
            }
            Err(code) => return code,
        }

        // If training specified, write the training file and exit
        if do_training == 1 {
            if quiet == 0 {
                let tf_str = CStr::from_ptr(train_file).to_string_lossy();
                eprint!("Writing data to training file {}...", tf_str);
            }
            rv = write_training_file(train_file, &mut tinf);
            if rv != 0 {
                eprintln!("\nError: could not write training file!");
                return 12;
            } else {
                if quiet == 0 {
                    eprintln!("done!");
                }
                handles.close();
                return 0;
            }
        }

        // Rewind input file
        if quiet == 0 {
            eprintln!("-------------------------------------");
        }
        if seq_reader_seek(handles.input, 0, 0) == -1 {
            // SEEK_SET = 0
            eprintln!("\nError: could not rewind input file.");
            return 13;
        }

        // Reset sequence/DP variables
        std::ptr::write_bytes(seq, 0, (slen as usize / 4 + 1).min(seq_vec.len()));
        std::ptr::write_bytes(rseq, 0, (slen as usize / 4 + 1).min(rseq_vec.len()));
        std::ptr::write_bytes(useq, 0, (slen as usize / 8 + 1).min(useq_vec.len()));
        std::ptr::write_bytes(nodes, 0, nn as usize);
        nn = 0;
        slen = 0;
        ipath = 0;
        nmask = 0;
    }
    // Initialize metagenomic bins
    else if is_meta == 1 {
        if quiet == 0 {
            eprintln!("Request:  Metagenomic, Phase:  Training");
            eprint!("Initializing training files...");
        }
        initialize_metagenomic_metadata(
            meta.as_mut_ptr(),
            meta_gc.as_mut_ptr(),
            meta_trans_table.as_mut_ptr(),
        );
        if quiet == 0 {
            eprintln!("done!");
            eprintln!("-------------------------------------");
        }
    }

    // Print header for gene finding phase
    if quiet == 0 {
        if is_meta == 1 {
            eprintln!("Request:  Metagenomic, Phase:  Gene Finding");
        } else {
            eprintln!("Request:  Single Genome, Phase:  Gene Finding");
        }
    }

    // Read and process each sequence
    {
        let s = std::ffi::CString::new("Prodigal_Seq_1").unwrap();
        let bytes = s.as_bytes_with_nul();
        std::ptr::copy_nonoverlapping(
            bytes.as_ptr() as *const c_char,
            cur_header.as_mut_ptr(),
            bytes.len(),
        );
    }
    {
        let s = std::ffi::CString::new("Prodigal_Seq_2").unwrap();
        let bytes = s.as_bytes_with_nul();
        std::ptr::copy_nonoverlapping(
            bytes.as_ptr() as *const c_char,
            new_header.as_mut_ptr(),
            bytes.len(),
        );
    }

    loop {
        slen = next_seq_multi(
            handles.input,
            seq,
            useq,
            &mut num_seq,
            &mut gc,
            do_mask,
            mlist.as_mut_ptr(),
            &mut nmask,
            cur_header.as_mut_ptr(),
            new_header.as_mut_ptr(),
        );
        if slen == -1 {
            break;
        }

        rcom_seq(seq, rseq, useq, slen);
        if slen == 0 {
            eprint!("\nSequence read failed (file must be Fasta, ");
            eprintln!("Genbank, or EMBL format).\n");
            return 14;
        }

        if quiet == 0 {
            eprint!("Finding genes in sequence #{} ({} bp)...", num_seq, slen);
        }

        nodes = node_room(&mut nodes_vec, slen);

        // Calculate short header
        calc_short_header(cur_header.as_mut_ptr(), short_header.as_mut_ptr(), num_seq);

        if is_meta == 0 {
            // ---- Single Genome Version ----
            nn = sorted_nodes(
                seq,
                rseq,
                slen,
                nodes,
                closed,
                mlist.as_mut_ptr(),
                nmask,
                &tinf,
            );
            score_nodes(seq, rseq, slen, nodes, nn, &tinf, closed, is_meta);
            if handles.start != handles.stdout {
                write_start_file(
                    handles.start,
                    nodes,
                    nn,
                    &tinf,
                    num_seq,
                    slen,
                    0,
                    std::ptr::null_mut(),
                    VERSION_CSTR.as_ptr() as *mut c_char,
                    cur_header.as_mut_ptr(),
                );
            }
            record_overlapping_starts(nodes, nn, &tinf, 1);
            ipath = dprog(nodes, nn, &tinf, 1);
            eliminate_bad_genes(nodes, ipath, &tinf);
            ng = add_genes(genes, nodes, ipath);
            tweak_final_starts(genes, ng, nodes, nn, &tinf);
            record_gene_data(genes, ng, nodes, &tinf, num_seq);
            if quiet == 0 {
                eprintln!("done!");
            }
            write_genes(
                &handles,
                genes,
                ng,
                nodes,
                seq,
                rseq,
                useq,
                slen,
                output,
                num_seq,
                std::ptr::null_mut(),
                &tinf,
                cur_header.as_mut_ptr(),
                short_header.as_mut_ptr(),
            );
        } else {
            // ---- Metagenomic Version ----
            meta::best_bin(
                seq,
                rseq,
                slen,
                gc,
                nodes,
                &mut nn,
                genes,
                &mut ng,
                closed,
                mlist.as_mut_ptr(),
                nmask,
                num_seq,
                &meta_gc,
                &meta_trans_table,
                &mut meta_tinf,
                &mut best_tinf,
                &mut max_phase,
            );
            if handles.start != handles.stdout {
                write_start_file(
                    handles.start,
                    nodes,
                    nn,
                    &best_tinf,
                    num_seq,
                    slen,
                    1,
                    meta[max_phase as usize].desc.as_mut_ptr(),
                    VERSION_CSTR.as_ptr() as *mut c_char,
                    cur_header.as_mut_ptr(),
                );
            }
            if quiet == 0 {
                eprintln!("done!");
            }
            write_genes(
                &handles,
                genes,
                ng,
                nodes,
                seq,
                rseq,
                useq,
                slen,
                output,
                num_seq,
                meta[max_phase as usize].desc.as_mut_ptr(),
                &best_tinf,
                cur_header.as_mut_ptr(),
                short_header.as_mut_ptr(),
            );
        }

        // Reset sequence/DP variables
        std::ptr::write_bytes(seq, 0, (slen as usize / 4 + 1).min(seq_vec.len()));
        std::ptr::write_bytes(rseq, 0, (slen as usize / 4 + 1).min(rseq_vec.len()));
        std::ptr::write_bytes(useq, 0, (slen as usize / 8 + 1).min(useq_vec.len()));
        std::ptr::write_bytes(nodes, 0, nn as usize);
        nn = 0;
        slen = 0;
        ipath = 0;
        nmask = 0;
        std::ptr::copy_nonoverlapping(new_header.as_ptr(), cur_header.as_mut_ptr(), MAX_LINE);
        {
            let s = format!("Prodigal_Seq_{}\n", num_seq + 1);
            let c = std::ffi::CString::new(s).unwrap();
            let bytes = c.as_bytes_with_nul();
            std::ptr::copy_nonoverlapping(
                bytes.as_ptr() as *const c_char,
                new_header.as_mut_ptr(),
                bytes.len().min(MAX_LINE),
            );
        }
    }

    if num_seq == 0 {
        eprintln!("\nError:  no input sequences to analyze.\n");
        return 18;
    }

    handles.close();
    0
}