orphos-core 0.2.0

Core library for Orphos, a tool for finding protein-coding genes in microbial genomes.
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
use bio::bio_types::strand::Strand;

use crate::types::{CodonType, Node, Training};

/// Record GC bias for training
pub fn record_gc_bias(gc_frame_data: &[i32], nodes: &mut [Node], training: &mut Training) {
    if nodes.is_empty() || gc_frame_data.is_empty() {
        return;
    }

    let mut frame_counts = [[0i32; 3]; 3];
    let mut last_position = [0; 3];

    process_forward_strand(gc_frame_data, nodes, &mut frame_counts, &mut last_position);

    process_reverse_strand(gc_frame_data, nodes, &mut frame_counts, &mut last_position);

    // Calculate overall bias with better error handling
    calculate_training_bias(nodes, training);
}

/// Process nodes on the forward strand
fn process_forward_strand(
    gc_frame_data: &[i32],
    nodes: &mut [Node],
    frame_counts: &mut [[i32; 3]; 3],
    last_position: &mut [usize; 3],
) {
    nodes
        .iter_mut()
        .rev()
        .filter(|node| node.position.strand == Strand::Forward)
        .for_each(|node| {
            let frame_index = node.position.index % 3;
            let frame_offset = 3 - frame_index;

            if node.position.codon_type == CodonType::Stop {
                // Reset counters for this frame
                frame_counts[frame_index]
                    .iter_mut()
                    .for_each(|count| *count = 0);
                last_position[frame_index] = node.position.index;

                if let Some(&gc_value) = gc_frame_data.get(node.position.index)
                    && gc_value >= 0
                {
                    let gc_index = (gc_value as usize + frame_offset) % 3;
                    frame_counts[frame_index][gc_index] = 1;
                }
            } else {
                if last_position[frame_index] >= 3 {
                    let mut position = last_position[frame_index] - 3;

                    while position >= node.position.index {
                        if let Some(&gc_value) = gc_frame_data.get(position)
                            && gc_value >= 0
                        {
                            let gc_index = (gc_value as usize + frame_offset) % 3;
                            frame_counts[frame_index][gc_index] += 1;
                        }

                        if position < 3 {
                            break;
                        }
                        position -= 3;
                    }
                }

                update_node_gc_info(node, &frame_counts[frame_index]);
                last_position[frame_index] = node.position.index;
            }
        });
}

/// Process nodes on the reverse strand
fn process_reverse_strand(
    gc_frame_data: &[i32],
    nodes: &mut [Node],
    frame_counts: &mut [[i32; 3]; 3],
    last_position: &mut [usize; 3],
) {
    nodes
        .iter_mut()
        .filter(|node| node.position.strand == Strand::Reverse)
        .for_each(|node| {
            let frame_index = node.position.index % 3;

            if node.position.codon_type == CodonType::Stop {
                // Reset counters for this frame
                frame_counts[frame_index]
                    .iter_mut()
                    .for_each(|count| *count = 0);
                last_position[frame_index] = node.position.index;

                if let Some(&gc_value) = gc_frame_data.get(node.position.index)
                    && gc_value >= 0
                {
                    let gc_index = ((3 - gc_value as usize) + frame_index) % 3;
                    frame_counts[frame_index][gc_index] = 1;
                }
            } else {
                let mut position = last_position[frame_index] + 3;

                while position <= node.position.index && position < gc_frame_data.len() {
                    if let Some(&gc_value) = gc_frame_data.get(position)
                        && gc_value >= 0
                    {
                        let gc_index = ((3 - gc_value as usize) + frame_index) % 3;
                        frame_counts[frame_index][gc_index] += 1;
                    }
                    position += 3;
                }

                update_node_gc_info(node, &frame_counts[frame_index]);
                last_position[frame_index] = node.position.index;
            }
        });
}

/// Calculate training bias from processed nodes
fn calculate_training_bias(nodes: &[Node], training: &mut Training) {
    training
        .gc_bias_factors
        .iter_mut()
        .for_each(|bias| *bias = 0.0);

    let total_bias: f64 = nodes
        .iter()
        .filter(|node| node.position.codon_type != CodonType::Stop)
        .filter_map(|node| {
            let gene_length = if node.position.stop_value < 0 {
                (node.position.index as isize - node.position.stop_value) as usize + 1
            } else if node.position.stop_value as usize > node.position.index {
                (node.position.stop_value as usize - node.position.index) + 1
            } else {
                (node.position.index - node.position.stop_value as usize) + 1
            };
            if node.state.gc_bias_frame < training.gc_bias_factors.len() && gene_length > 0 {
                let contribution = (node.scores.gc_frame_scores[node.state.gc_bias_frame]
                    * gene_length as f64)
                    / 1000.0;
                training.gc_bias_factors[node.state.gc_bias_frame] += contribution;
                Some(contribution)
            } else {
                None
            }
        })
        .sum();

    // Normalize bias values if we have any contributions
    if total_bias > 0.0 {
        let normalization_factor = 3.0 / training.gc_bias_factors.iter().sum::<f64>();
        training
            .gc_bias_factors
            .iter_mut()
            .for_each(|bias| *bias *= normalization_factor);
    }
}

/// Update GC bias information for a node
fn update_node_gc_info(node: &mut Node, frame_counts: &[i32; 3]) {
    let max_frame_index = find_max_frame_index(frame_counts[0], frame_counts[1], frame_counts[2]);
    node.state.gc_bias_frame = max_frame_index;

    let gene_length: isize = if node.position.strand == Strand::Forward {
        node.position.stop_value + 3 - node.position.index as isize
    } else {
        node.position.index as isize + 3 - node.position.stop_value
    };

    if gene_length > 0 {
        frame_counts
            .iter()
            .enumerate()
            .for_each(|(frame_index, &count)| {
                node.scores.gc_frame_scores[frame_index] =
                    (3.0 * count as f64) / (gene_length as f64);
            });
    }
}

/// Find the frame with maximum count
const fn find_max_frame_index(frame_a: i32, frame_b: i32, frame_c: i32) -> usize {
    if frame_a > frame_b {
        if frame_a > frame_c { 0 } else { 2 }
    } else if frame_b > frame_c {
        1
    } else {
        2
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::*;
    use bio::bio_types::strand::Strand;

    fn create_test_node(
        strand: Strand,
        index: usize,
        codon_type: CodonType,
        stop_value: isize,
    ) -> Node {
        Node {
            position: NodePosition {
                index,
                strand,
                codon_type,
                stop_value,
                is_edge: false,
            },
            scores: NodeScores {
                gc_frame_scores: [0.0; 3],
                ..Default::default()
            },
            state: NodeState {
                gc_bias_frame: 0,
                ..Default::default()
            },
            motif_info: NodeMotifInfo::default(),
        }
    }

    fn create_test_training() -> Training {
        Training {
            gc_content: 0.5,
            translation_table: 11,
            uses_shine_dalgarno: true,
            start_type_weights: [2.0, 1.5, 1.0],
            rbs_weights: Box::new([1.0; 28]),
            upstream_composition: Box::new([[0.25; 4]; 32]),
            motif_weights: Box::new([[[1.0; 4096]; 4]; 4]),
            no_motif_weight: 0.5,
            start_weight_factor: 4.35,
            gc_bias_factors: [1.0; 3],
            gene_dicodon_table: Box::new([1.0; 4096]),
            total_dicodons: 0,
        }
    }

    #[test]
    fn test_record_gc_bias_empty_inputs() {
        let gc_frame_data = vec![];
        let mut nodes = vec![];
        let mut training = create_test_training();

        record_gc_bias(&gc_frame_data, &mut nodes, &mut training);

        // Should handle empty inputs gracefully
        assert_eq!(nodes.len(), 0);
        assert_eq!(gc_frame_data.len(), 0);
    }

    #[test]
    fn test_record_gc_bias_forward_strand() {
        let gc_frame_data = vec![0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2];
        let mut nodes = vec![
            create_test_node(Strand::Forward, 3, CodonType::Atg, 12),
            create_test_node(Strand::Forward, 12, CodonType::Stop, 12),
        ];
        let mut training = create_test_training();

        record_gc_bias(&gc_frame_data, &mut nodes, &mut training);

        // Verify GC bias frame is set
        assert!(nodes[0].state.gc_bias_frame <= 2);
        // Verify GC frame scores are calculated
        assert!(
            nodes[0]
                .scores
                .gc_frame_scores
                .iter()
                .any(|&score| score >= 0.0)
        );
    }

    #[test]
    fn test_record_gc_bias_reverse_strand() {
        let gc_frame_data = vec![0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2];
        let mut nodes = vec![
            create_test_node(Strand::Reverse, 12, CodonType::Atg, 3),
            create_test_node(Strand::Reverse, 3, CodonType::Stop, 3),
        ];
        let mut training = create_test_training();

        record_gc_bias(&gc_frame_data, &mut nodes, &mut training);

        // Verify GC bias frame is set
        assert!(nodes[0].state.gc_bias_frame <= 2);
        // Verify GC frame scores are calculated
        assert!(
            nodes[0]
                .scores
                .gc_frame_scores
                .iter()
                .any(|&score| score >= 0.0)
        );
    }

    #[test]
    fn test_record_gc_bias_mixed_strands() {
        let gc_frame_data = vec![0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2];
        let mut nodes = vec![
            create_test_node(Strand::Forward, 3, CodonType::Atg, 12),
            create_test_node(Strand::Reverse, 15, CodonType::Gtg, 6),
            create_test_node(Strand::Forward, 12, CodonType::Stop, 12),
            create_test_node(Strand::Reverse, 6, CodonType::Stop, 6),
        ];
        let mut training = create_test_training();

        record_gc_bias(&gc_frame_data, &mut nodes, &mut training);

        for node in &nodes {
            if node.position.codon_type != CodonType::Stop {
                assert!(node.state.gc_bias_frame <= 2);
            }
        }
    }

    #[test]
    fn test_process_forward_strand() {
        let gc_frame_data = vec![0, 1, 2, 0, 1, 2, 0, 1, 2];
        let mut nodes = vec![
            create_test_node(Strand::Forward, 3, CodonType::Atg, 6),
            create_test_node(Strand::Forward, 6, CodonType::Stop, 6),
        ];
        let mut frame_counts = [[0i32; 3]; 3];
        let mut last_position = [0; 3];

        process_forward_strand(
            &gc_frame_data,
            &mut nodes,
            &mut frame_counts,
            &mut last_position,
        );

        // Verify processing completed without errors
        assert!(
            nodes[0]
                .scores
                .gc_frame_scores
                .iter()
                .any(|&score| score >= 0.0)
        );
    }

    #[test]
    fn test_process_reverse_strand() {
        let gc_frame_data = vec![0, 1, 2, 0, 1, 2, 0, 1, 2];
        let mut nodes = vec![
            create_test_node(Strand::Reverse, 6, CodonType::Atg, 3),
            create_test_node(Strand::Reverse, 3, CodonType::Stop, 3),
        ];
        let mut frame_counts = [[0i32; 3]; 3];
        let mut last_position = [0; 3];

        process_reverse_strand(
            &gc_frame_data,
            &mut nodes,
            &mut frame_counts,
            &mut last_position,
        );

        // Verify processing completed without errors
        assert!(
            nodes[0]
                .scores
                .gc_frame_scores
                .iter()
                .any(|&score| score >= 0.0)
        );
    }

    #[test]
    fn test_calculate_training_bias() {
        let mut nodes = vec![
            create_test_node(Strand::Forward, 3, CodonType::Atg, 12),
            create_test_node(Strand::Forward, 15, CodonType::Gtg, 24),
        ];

        // Set some GC frame scores
        nodes[0].scores.gc_frame_scores = [1.0, 2.0, 3.0];
        nodes[1].scores.gc_frame_scores = [2.0, 3.0, 4.0];
        nodes[0].state.gc_bias_frame = 0;
        nodes[1].state.gc_bias_frame = 1;

        let mut training = create_test_training();

        calculate_training_bias(&nodes, &mut training);

        let total: f64 = training.gc_bias_factors.iter().sum();
        assert!((total - 3.0).abs() < 1e-10 || total == 0.0);
    }

    #[test]
    fn test_calculate_training_bias_empty_nodes() {
        let nodes = vec![];
        let mut training = create_test_training();

        calculate_training_bias(&nodes, &mut training);

        // Should handle empty nodes gracefully
        assert_eq!(training.gc_bias_factors, [0.0; 3]);
    }

    #[test]
    fn test_calculate_training_bias_only_stop_codons() {
        let nodes = vec![
            create_test_node(Strand::Forward, 6, CodonType::Stop, 6),
            create_test_node(Strand::Reverse, 12, CodonType::Stop, 12),
        ];
        let mut training = create_test_training();

        calculate_training_bias(&nodes, &mut training);

        assert_eq!(training.gc_bias_factors, [0.0; 3]);
    }

    #[test]
    fn test_update_node_gc_info_forward() {
        let mut node = create_test_node(Strand::Forward, 3, CodonType::Atg, 12);
        let frame_counts = [5, 3, 2];

        update_node_gc_info(&mut node, &frame_counts);

        // Should select frame with maximum count (frame 0)
        assert_eq!(node.state.gc_bias_frame, 0);
        assert!(node.scores.gc_frame_scores[0] > 0.0);
    }

    #[test]
    fn test_update_node_gc_info_reverse() {
        let mut node = create_test_node(Strand::Reverse, 12, CodonType::Atg, 3);
        let frame_counts = [2, 5, 3];

        update_node_gc_info(&mut node, &frame_counts);

        // Should select frame with maximum count (frame 1)
        assert_eq!(node.state.gc_bias_frame, 1);
        assert!(node.scores.gc_frame_scores[1] > 0.0);
    }

    #[test]
    fn test_update_node_gc_info_zero_gene_length() {
        let mut node = create_test_node(Strand::Forward, 6, CodonType::Atg, 6);
        let frame_counts = [5, 3, 2];

        update_node_gc_info(&mut node, &frame_counts);

        // Should handle zero gene length gracefully
        assert_eq!(node.state.gc_bias_frame, 0);
    }

    #[test]
    fn test_find_max_frame_index_first() {
        assert_eq!(find_max_frame_index(5, 3, 2), 0);
    }

    #[test]
    fn test_find_max_frame_index_second() {
        assert_eq!(find_max_frame_index(2, 5, 3), 1);
    }

    #[test]
    fn test_find_max_frame_index_third() {
        assert_eq!(find_max_frame_index(2, 3, 5), 2);
    }

    #[test]
    fn test_find_max_frame_index_ties() {
        assert_eq!(find_max_frame_index(5, 5, 3), 1); // When a == b > c, choose b (index 1)
        assert_eq!(find_max_frame_index(3, 5, 5), 2); // When b == c > a, choose c (index 2)
        assert_eq!(find_max_frame_index(5, 3, 5), 2); // When a == c > b, choose c (index 2)
        assert_eq!(find_max_frame_index(5, 5, 5), 2); // When all equal, choose c (index 2)
    }

    #[test]
    fn test_record_gc_bias_negative_gc_values() {
        let gc_frame_data = vec![-1, 0, 1, -1, 2, -1];
        let mut nodes = vec![
            create_test_node(Strand::Forward, 1, CodonType::Atg, 4),
            create_test_node(Strand::Forward, 4, CodonType::Stop, 4),
        ];
        let mut training = create_test_training();

        record_gc_bias(&gc_frame_data, &mut nodes, &mut training);

        // Should handle negative GC values (invalid positions)
        assert!(nodes[0].state.gc_bias_frame <= 2);
    }

    #[test]
    fn test_record_gc_bias_out_of_bounds_indices() {
        let gc_frame_data = vec![0, 1, 2];
        let mut nodes = vec![
            create_test_node(Strand::Forward, 10, CodonType::Atg, 20), // Index beyond gc_frame_data
            create_test_node(Strand::Forward, 20, CodonType::Stop, 20),
        ];
        let mut training = create_test_training();

        record_gc_bias(&gc_frame_data, &mut nodes, &mut training);

        // Should handle out-of-bounds indices gracefully
        assert!(nodes[0].state.gc_bias_frame <= 2);
    }
}