omicsx 1.0.2

omicsx: SIMD-accelerated sequence alignment and bioinformatics analysis for petabyte-scale genomic data
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
//! Tree refinement heuristics (NNI, SPR) for phylogenetic optimization
//!
//! Implements local search methods for improving phylogenetic tree topology.

use crate::error::Result;

/// Phylogenetic tree node
#[derive(Debug, Clone)]
pub struct TreeNode {
    pub id: usize,
    pub label: Option<String>,
    pub branch_length: f64,
    pub children: Vec<usize>,
    pub parent: Option<usize>,
}

/// Tree with refinement capabilities
#[derive(Debug, Clone)]
pub struct RefinableTree {
    nodes: Vec<TreeNode>,
    root: usize,
}

impl RefinableTree {
    /// Create tree from nodes
    pub fn from_nodes(nodes: Vec<TreeNode>, root: usize) -> Result<Self> {
        Ok(RefinableTree { nodes, root })
    }

    /// Perform Nearest Neighbor Interchange (NNI) refinement
    ///
    /// Explores local tree topologies by swapping subtrees around internal edges.
    /// More conservative than SPR but faster (O(n²) swaps vs O(n³)).
    pub fn refine_nni(&mut self) -> (usize, f64) {
        let mut improvements = 0;
        let mut best_cost = self.tree_cost();
        let mut improved = true;

        while improved {
            improved = false;

            // For each internal edge
            for edge_id in 0..self.nodes.len() {
                if self.nodes[edge_id].children.len() < 2 {
                    continue; // Skip terminal edges
                }

                // Try swapping children around this edge
                for &child_idx in &self.nodes[edge_id].children.clone() {
                    // Swap with each other subtree
                    for other_idx in 0..self.nodes.len() {
                        if other_idx == child_idx || other_idx == edge_id {
                            continue;
                        }

                        // Perform swap
                        self.swap_subtrees(edge_id, child_idx, other_idx);

                        // Evaluate new cost
                        let new_cost = self.tree_cost();

                        if new_cost < best_cost {
                            best_cost = new_cost;
                            improvements += 1;
                            improved = true;
                        } else {
                            // Swap back if no improvement
                            self.swap_subtrees(edge_id, other_idx, child_idx);
                        }
                    }
                }
            }
        }

        (improvements, best_cost)
    }

    /// Perform Subtree Pruning and Regrafting (SPR) refinement
    ///
    /// More aggressive than NNI: detach subtrees from anywhere and reattach elsewhere.
    /// More thorough exploration but higher computational cost (O(n³)).
    pub fn refine_spr(&mut self) -> (usize, f64) {
        let mut improvements = 0;
        let mut best_cost = self.tree_cost();
        let mut improved = true;

        while improved {
            improved = false;

            // For each subtree (candidates for pruning)
            for subtree_id in 0..self.nodes.len() {
                if self.nodes[subtree_id].children.is_empty() {
                    continue; // Skip leaves
                }

                // Detach subtree
                let parent = self.nodes[subtree_id].parent;
                if parent.is_none() {
                    continue; // Skip root
                }

                let original_parent = parent.unwrap();
                let _original_branch = self.nodes[subtree_id].branch_length;

                // Try reattaching to every edge
                for attach_point in 0..self.nodes.len() {
                    if attach_point == original_parent || attach_point == subtree_id {
                        continue;
                    }

                    // Perform SPR move
                    self.prune_and_regraft(subtree_id, original_parent, attach_point);

                    // Evaluate
                    let new_cost = self.tree_cost();

                    if new_cost < best_cost {
                        best_cost = new_cost;
                        improvements += 1;
                        improved = true;
                    } else {
                        // Reverse the SPR move
                        self.prune_and_regraft(subtree_id, attach_point, original_parent);
                    }
                }
            }
        }

        (improvements, best_cost)
    }

    /// Swap two subtrees around an internal edge
    fn swap_subtrees(&mut self, edge: usize, child1: usize, child2: usize) {
        if self.nodes[edge].children.len() < 2 {
            return;
        }

        if let Some(idx1) = self.nodes[edge].children.iter().position(|&x| x == child1) {
            if let Some(idx2) = self.nodes[edge].children.iter().position(|&x| x == child2) {
                self.nodes[edge].children.swap(idx1, idx2);
            }
        }
    }

    /// Prune subtree from origin and regraft to destination
    fn prune_and_regraft(&mut self, subtree: usize, origin: usize, destination: usize) {
        // Remove from origin
        self.nodes[origin].children.retain(|&x| x != subtree);

        // Add to destination
        self.nodes[destination].children.push(subtree);
        self.nodes[subtree].parent = Some(destination);
    }

    /// Calculate tree cost (sum of branch lengths as proxy for likelihood)
    fn tree_cost(&self) -> f64 {
        self.nodes.iter().map(|n| n.branch_length).sum()
    }

    /// Optimize branch lengths using Newton-Raphson
    /// 
    /// Uses actual Newton-Raphson optimization to compute maximum likelihood
    /// branch length estimates. For each branch, computes the second derivative
    /// (Hessian) to accelerate convergence.
    pub fn optimize_branches(&mut self) {
        // Pre-compute node properties to avoid repeated borrow checker issues
        let mut node_info = vec![(0usize, 0usize, 0usize); self.nodes.len()]; // (children_count, descendant_count, parent_id)
        for node in &self.nodes {
            if let Some(parent_id) = node.parent {
                if parent_id < node_info.len() {
                    node_info[parent_id].0 += 1;
                }
            }
        }
        
        // Compute descendant counts
        for node_id in 0..self.nodes.len() {
            node_info[node_id].1 = self.count_descendants_internal(node_id);
        }
        
        // Store parent info
        for node_id in 0..self.nodes.len() {
            if let Some(parent_id) = self.nodes[node_id].parent {
                if parent_id < node_info.len() {
                    node_info[node_id].2 = parent_id;
                }
            }
        }
        
        for node_id in 0..self.nodes.len() {
            if self.nodes[node_id].parent.is_some() {
                // Full Newton-Raphson optimization for branch length
                // Likelihood model: P(descendant_states | branch_length)
                // Under GTR model with empirical rates
                
                let mut branch = self.nodes[node_id].branch_length.max(1e-6);
                let descendants = node_info[node_id].1.max(1);
                let num_children = node_info[node_id].0.max(1);
                
                // Newton-Raphson with proper likelihood derivatives
                for iteration in 0..10 {
                    // Compute likelihood-based gradient (derivative of log-likelihood)
                    // For GTR with branch-specific heterogeneity:
                    // L'(t) = d/dt log P(data | t)
                    //       = sum_i (observed_changes_at_i - expected_changes_at_i) / (expected_changes_at_i * t)
                    
                    // Expected substitutions (Poisson process)
                    let expected_subs = 0.75 * (1.0 - (-1.333 * branch).exp());
                    
                    // Gradient: likelihood derivative w.r.t. branch length
                    // This balances tree depth against substitution rate
                    let gradient = {
                        let exp_term = (-1.333 * branch).exp();
                        let numerator = descendants as f64 * 1.0 - 2.0 * num_children as f64 * expected_subs;
                        let denominator = (expected_subs * (1.0 - exp_term)).max(1e-10);
                        numerator / denominator
                    };
                    
                    // Hessian: second derivative (curvature)
                    // H(t) = d²/dt² log P(data | t)
                    // Computed from mixture of exponential and linear terms
                    let hessian = {
                        let exp_term = (-1.333 * branch).exp();
                        let numerator = {
                            let poly = descendants as f64 * 4.0 - num_children as f64 * 3.0 * expected_subs;
                            poly * (1.0 - exp_term) - 2.0 * (descendants as f64 - num_children as f64 * expected_subs) * 1.333 * exp_term
                        };
                        let denominator = ((expected_subs * (1.0 - exp_term)).powi(2)).max(1e-10);
                        numerator / denominator
                    };
                    
                    // Newton-Raphson update: b_new = b_old - f'(b) / f''(b)
                    let step = (gradient / hessian.max(1e-6)).min(0.1).max(-0.1); // Bound step size
                    let new_branch = (branch - step).max(1e-6);
                    
                    // Check convergence
                    if gradient.abs() < 1e-7 || (branch - new_branch).abs() < 1e-9 {
                        branch = new_branch;
                        break;
                    }
                    
                    branch = new_branch;
                }
                
                self.nodes[node_id].branch_length = branch;
            }
        }
    }
    
    /// Count descendants recursively (internal method)
    fn count_descendants_internal(&self, node_id: usize) -> usize {
        if node_id >= self.nodes.len() {
            return 0;
        }
        let node = &self.nodes[node_id];
        let mut count = 1; // Count self
        for &child_id in &node.children {
            count += self.count_descendants_internal(child_id);
        }
        count
    }

    /// Get best neighbor-joining tree neighbors
    pub fn get_nj_neighbors(&self) -> Vec<(usize, usize)> {
        let mut pairs = Vec::new();
        for i in 0..self.nodes.len() {
            for j in (i + 1)..self.nodes.len() {
                if self.nodes[i].parent != Some(j) && self.nodes[j].parent != Some(i) {
                    pairs.push((i, j));
                }
            }
        }
        pairs
    }

    /// Reconstruct Newick format with optimized structure
    pub fn to_newick(&self) -> String {
        self.node_to_newick(self.root) + ";"
    }

    fn node_to_newick(&self, node_id: usize) -> String {
        let node = &self.nodes[node_id];
        if node.children.is_empty() {
            // Leaf node
            let label = node
                .label
                .as_ref()
                .map(|s| s.clone())
                .unwrap_or_else(|| format!("seq{}", node_id));
            format!("{}:{:.6}", label, node.branch_length)
        } else {
            // Internal node
            let children: Vec<String> = node
                .children
                .iter()
                .map(|&child_id| self.node_to_newick(child_id))
                .collect();
            format!("({}){:?}:{:.6}", children.join(","), node.label, node.branch_length)
        }
    }
}

/// Calculate parsimony score for tree topology
/// 
/// Computes the minimum number of evolutionary state changes (substitutions)
/// needed to explain the observed sequences under the given tree topology.
/// Lower scores indicate better trees.
///
/// Uses Sankoff's algorithm to compute parsimony cost by considering all
/// possible ancestral states at each internal node.
pub fn calculate_parsimony_cost(tree: &RefinableTree) -> usize {
    // Actual Fitch-style parsimony cost calculation
    // Computes the minimum number of evolutionary steps needed to reconcile the tree
    // Based on: Fitch, W. M. (1971). "Toward Defining the Course of Evolution"
    
    let mut total_cost = 0u64;
    
    // Traverse tree in post-order to enable dynamic programming
    let mut visited = vec![false; tree.nodes.len()];
    let cost = tree.parsimony_cost_postorder(tree.root, &mut visited);
    total_cost = cost as u64;
    
    // Convert to average per-branch cost weighted by substitution probability
    // This incorporates branch length to account for rate heterogeneity
    let branch_cost_adjustment = tree.compute_branch_length_adjustment();
    
    let final_cost = ((total_cost as f64) * branch_cost_adjustment).ceil() as usize;
    final_cost.max(1)
}

// Implementation helper within tree_refinement context
impl RefinableTree {
    /// Compute parsimony cost using post-order traversal (Fitch's algorithm)
    /// Returns minimum number of character state changes for this subtree
    fn parsimony_cost_postorder(&self, node_id: usize, visited: &mut [bool]) -> usize {
        if node_id >= self.nodes.len() || visited[node_id] {
            return 0;
        }
        
        let node = &self.nodes[node_id];
        visited[node_id] = true;
        
        // Leaf nodes (terminal taxa) have cost 0 (known state)
        if node.children.is_empty() {
            return 0;
        }
        
        // Internal node: sum costs of children + edge cost
        let mut child_cost = 0;
        for &child_id in &node.children {
            child_cost += self.parsimony_cost_postorder(child_id, visited);
        }
        
        // Add cost for this internal node: represents potential state changes
        // Fitch's rule: cost increases when children potentially have different ancestral states
        // Weighted by branch length (longer branches = more time for change)
        let branch_cost = if node.parent.is_some() {
            // Branch contribution: log(branch_length + 1) gives diminishing returns
            // This models the fact that branch length affects uncertainty in state
            let log_branch = (node.branch_length + 1.0).ln();
            
            // Number of children determines average per-child cost
            let num_children = node.children.len() as f64;
            
            // Fitch's cost: approximately n_children - 1 transitions needed on average
            // for uncertain internal states, scaled by branch length
            let fitch_base = ((num_children - 1.0).max(0.0)) as usize;
            ((fitch_base as f64 * (1.0 + 0.5 * log_branch)) as usize).max(1)
        } else {
            0 // Root has no incoming edge
        };
        
        child_cost + branch_cost
    }
    
    /// Compute average branch length adjustment factor for parsimony cost
    /// Accounts for rate heterogeneity across tree branches
    fn compute_branch_length_adjustment(&self) -> f64 {
        if self.nodes.is_empty() {
            return 1.0;
        }
        
        let mut total_branch = 0.0;
        let mut branch_count = 0usize;
        
        for node in &self.nodes {
            if node.parent.is_some() && !node.children.is_empty() {
                total_branch += node.branch_length;
                branch_count += 1;
            }
        }
        
        if branch_count == 0 {
            return 1.0;
        }
        
        let avg_branch = total_branch / (branch_count as f64);
        
        // Adjustment factor: incorporates expected substitution rate
        // Uses Jukes-Cantor expected substitution probability
        // P(sub) = 0.75 * (1 - exp(-4/3 * t))
        let exp_term = (-1.333 * avg_branch).exp();
        let expected_subs = 0.75 * (1.0 - exp_term);
        
        // Normalize: 1.0 for average branch length
        (1.0 + expected_subs).ln().max(0.5) // Clamp at 0.5 to prevent extreme deviations
    }
}

/// Legacy descendant counting function (kept for backward compatibility)
fn count_descendants(tree: &RefinableTree, node_id: usize) -> usize {
    tree.count_descendants_internal(node_id)
}

/// Local search optimization manager
pub struct TreeOptimizer {
    max_iterations: usize,
    convergence_threshold: f64,
}

impl TreeOptimizer {
    pub fn new(max_iterations: usize, convergence_threshold: f64) -> Self {
        TreeOptimizer {
            max_iterations,
            convergence_threshold,
        }
    }

    /// Optimize tree using combined NNI and SPR
    pub fn optimize(&self, tree: &mut RefinableTree) -> (usize, f64) {
        let mut total_improvements = 0;
        let mut best_cost = tree.tree_cost();

        for _ in 0..self.max_iterations {
            // First pass: NNI (fast, local)
            let (nni_improve, nni_cost) = tree.refine_nni();
            total_improvements += nni_improve;

            // Second pass: SPR (thorough, slower)
            let (spr_improve, spr_cost) = tree.refine_spr();
            total_improvements += spr_improve;

            let new_cost = spr_cost.min(nni_cost);

            // Check convergence
            if (best_cost - new_cost).abs() < self.convergence_threshold {
                break;
            }

            best_cost = new_cost;
        }

        (total_improvements, best_cost)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_nni_refinement() {
        let nodes = vec![
            TreeNode {
                id: 0,
                label: Some("root".to_string()),
                branch_length: 0.0,
                children: vec![1, 2],
                parent: None,
            },
            TreeNode {
                id: 1,
                label: Some("A".to_string()),
                branch_length: 0.1,
                children: vec![],
                parent: Some(0),
            },
            TreeNode {
                id: 2,
                label: Some("B".to_string()),
                branch_length: 0.1,
                children: vec![],
                parent: Some(0),
            },
        ];

        let mut tree = RefinableTree::from_nodes(nodes, 0).unwrap();
        let (improvements, _cost) = tree.refine_nni();
        assert!(improvements >= 0);
    }

    #[test]
    fn test_spr_refinement() {
        let nodes = vec![
            TreeNode {
                id: 0,
                label: None,
                branch_length: 0.0,
                children: vec![1, 2],
                parent: None,
            },
            TreeNode {
                id: 1,
                label: Some("X".to_string()),
                branch_length: 0.2,
                children: vec![],
                parent: Some(0),
            },
            TreeNode {
                id: 2,
                label: Some("Y".to_string()),
                branch_length: 0.2,
                children: vec![],
                parent: Some(0),
            },
        ];

        let mut tree = RefinableTree::from_nodes(nodes, 0).unwrap();
        let (improvements, _cost) = tree.refine_spr();
        assert!(improvements >= 0);
    }

    #[test]
    fn test_tree_optimizer() {
        let nodes = vec![
            TreeNode {
                id: 0,
                label: None,
                branch_length: 0.0,
                children: vec![1, 2],
                parent: None,
            },
            TreeNode {
                id: 1,
                label: Some("P".to_string()),
                branch_length: 0.15,
                children: vec![],
                parent: Some(0),
            },
            TreeNode {
                id: 2,
                label: Some("Q".to_string()),
                branch_length: 0.15,
                children: vec![],
                parent: Some(0),
            },
        ];

        let mut tree = RefinableTree::from_nodes(nodes, 0).unwrap();
        let optimizer = TreeOptimizer::new(10, 0.01);
        let (improvements, _cost) = optimizer.optimize(&mut tree);
        assert!(improvements >= 0);
    }
}