willi26 0.0.2

An interactive parsimony phylogenetic analysis program inspired by Hennig86.
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*!Module: tree diagnostics for xsteps commands, including tree lengths,
per-character fits, hypothetical ancestor states, successive weighting, and
best/worst summaries across trees.

 */
use crate::engines::{
    ccode::{CharConfig, CharSetting},
    dataset::{Dataset, StateSet},
    search::{INF, NSTATES, calc_ci, calc_ri, maxsteps_sum, minsteps_sum, score_tree},
    trees::Tree,
    util,
};

/// One row in the tree-length report: total weighted length.
#[derive(Debug, Clone)]
pub struct TreeLengthRow {
    pub length: u64,
}

/// Computes whole-tree CI and RI from the active character configuration.
pub(crate) fn fit_statistics(ds: &Dataset, cfg: &CharConfig, length: u64) -> (f64, Option<f64>) {
    let min_len = minsteps_sum(ds, cfg);
    let max_len = maxsteps_sum(ds, cfg);
    (calc_ci(min_len, length), calc_ri(min_len, max_len, length))
}

/// Per-character fit statistics: steps, CI, RI for one tree.
#[derive(Debug, Clone)]
pub struct CharacterFitRow {
    pub character: usize,
    pub steps: u64,
    pub ci: f64,
    pub ri: Option<f64>,
}

/// Character-by-character report for one tree with state assignments.
#[derive(Debug, Clone)]
pub struct TreeCharacterReport {
    pub tree_index: usize,
    pub length: u64,
    pub ci: f64,
    pub ri: Option<f64>,
    pub rows: Vec<CharacterFitRow>,
}

/// Ancestral state and synapomorphy report for one character across trees.
#[derive(Debug, Clone)]
pub struct CharacterHistoryReport {
    pub character: usize,
    pub internal_nodes: Vec<usize>,
    pub node_states: Vec<String>,
}

/// Complete ancestral-state reconstruction for all active characters on one
/// tree.
#[derive(Debug, Clone)]
pub struct TreeHistoryReport {
    pub tree_index: usize,
    pub histories: Vec<CharacterHistoryReport>,
}

/// Full analysis of one character on one tree: steps, states, and per-node
/// assignments.
#[derive(Debug, Clone)]
pub struct SingleCharAnalysis {
    pub steps: u64,
    pub node_states: Vec<u64>, /* per node: low 36 bits used */
}
/// scores each supplied tree and returns one tree-length row per tree.

pub fn analyze_lengths(ds: &Dataset, cfg: &CharConfig, trees: &[Tree]) -> Vec<TreeLengthRow> {
    let mut rows = Vec::with_capacity(trees.len());
    let mut ws = crate::engines::search::ScoreWorkspace::new();

    for tr in trees.iter() {
        let len = score_tree(ds, cfg, tr, &mut ws);
        rows.push(TreeLengthRow { length: len });
    }

    rows
}
/// computes per-character fit rows and whole-tree CI/RI summaries for each
/// tree.

pub fn analyze_character_fits(
    ds: &Dataset,
    cfg: &CharConfig,
    trees: &[Tree],
) -> Result<Vec<TreeCharacterReport>, String> {
    let mut out = Vec::with_capacity(trees.len());

    for (tree_index, tr) in trees.iter().enumerate() {
        let rows = analyze_tree_character_fits(ds, cfg, tr)?;
        let length: u64 = rows.iter().map(|r| r.steps).sum();

        let (ci, ri) = fit_statistics(ds, cfg, length);

        out.push(TreeCharacterReport { tree_index, length, ci, ri, rows });
    }

    Ok(out)
}
/// computes possible ancestral state strings for every internal node and
/// character.

pub fn analyze_histories(
    ds: &Dataset,
    _cfg: &CharConfig,
    trees: &[Tree],
) -> Result<Vec<TreeHistoryReport>, String> {
    let mut out = Vec::with_capacity(trees.len());

    for (tree_index, tr) in trees.iter().enumerate() {
        let mut histories = Vec::with_capacity(ds.nchar);

        let internal_nodes: Vec<usize> = tr
            .nodes
            .iter()
            .enumerate()
            .filter_map(|(i, n)| if n.taxon.is_none() { Some(i) } else { None })
            .collect();

        for ch in 0..ds.nchar {
            let analyzed = analyze_single_character(ds, tr, ch)?;
            let mut node_states = Vec::with_capacity(internal_nodes.len());

            for &nid in &internal_nodes {
                node_states.push(format_state_bits(analyzed.node_states[nid]));
            }

            histories.push(CharacterHistoryReport {
                character: ch,
                internal_nodes: internal_nodes.clone(),
                node_states,
            });
        }

        out.push(TreeHistoryReport { tree_index, histories });
    }

    Ok(out)
}
/// evaluates every character on one tree using the active character
/// configuration.

fn analyze_tree_character_fits(
    ds: &Dataset,
    cfg: &CharConfig,
    tr: &Tree,
) -> Result<Vec<CharacterFitRow>, String> {
    let mut rows = Vec::with_capacity(ds.nchar);

    for ch in 0..ds.nchar {
        let cs = cfg.chars[ch];
        let analyzed = analyze_single_character_with_setting(ds, tr, ch, cs)?;
        let obs = analyzed.steps;
        let min_s = minsteps_char_with_setting(ds, ch, cs);
        let max_s = maxsteps_char_with_setting(ds, ch, cs);

        let ci = if obs == 0 { 1.0 } else { (min_s as f64) / (obs as f64) };

        let ri = if max_s > min_s {
            Some((max_s.saturating_sub(obs)) as f64 / (max_s - min_s) as f64)
        } else {
            None
        };

        rows.push(CharacterFitRow { character: ch, steps: obs, ci, ri });
    }

    Ok(rows)
}
/// diagnoses one character as active, non-additive, and unit-weighted for
/// display-only history output.

pub fn analyze_single_character(
    ds: &Dataset,
    tr: &Tree,
    ch: usize,
) -> Result<SingleCharAnalysis, String> {
    analyze_single_character_with_setting(
        ds,
        tr,
        ch,
        CharSetting { active: true, additive: false, weight: 1 },
    )
}
/// dispatches inactive, additive, and non-additive single-character scoring
/// paths.

fn analyze_single_character_with_setting(
    ds: &Dataset,
    tr: &Tree,
    ch: usize,
    cs: CharSetting,
) -> Result<SingleCharAnalysis, String> {
    if ch >= ds.nchar {
        return Err(format!("character {ch} out of range"));
    }

    if tr.ntax != ds.ntax {
        return Err(format!("tree ntax {} != dataset ntax {}", tr.ntax, ds.ntax));
    }

    if !cs.active {
        return Ok(SingleCharAnalysis { steps: 0, node_states: vec![0u64; tr.nodes.len()] });
    }

    if cs.additive {
        return analyze_single_additive_character(ds, tr, ch, cs.weight as u64);
    }

    let mut node_states = vec![0u64; tr.nodes.len()];
    let mut steps = 0u64;
    /// local Fitch recursion that propagates non-additive state sets upward and
    /// counts union events.

    fn rec(
        ds: &Dataset,
        tr: &Tree,
        v: usize,
        ch: usize,
        node_states: &mut [u64],
        steps: &mut u64,
    ) -> Result<u64, String> {
        let node = &tr.nodes[v];

        if let Some(taxon) = node.taxon {
            let ss = dataset_state_at(ds, taxon, ch)?;
            let bits = ss.bits();
            node_states[v] = bits;
            return Ok(bits);
        }

        if node.children.is_empty() {
            return Err(format!("internal node {v} has no children"));
        }

        let mut child_bits = Vec::with_capacity(node.children.len());
        for &c in &node.children {
            child_bits.push(rec(ds, tr, c, ch, node_states, steps)?);
        }

        let mut inter = child_bits[0];
        for &b in &child_bits[1..] {
            inter &= b;
        }

        let here = if inter != 0 {
            inter
        } else {
            let mut uni = 0u64;
            for &b in &child_bits {
                uni |= b;
            }
            *steps += 1;
            uni
        };

        node_states[v] = here;
        Ok(here)
    }

    rec(ds, tr, tr.root, ch, &mut node_states, &mut steps)?;

    Ok(SingleCharAnalysis { steps: steps * cs.weight as u64, node_states })
}

#[derive(Clone, Copy, Debug, Default)]
struct OrderedRange {
    lo: u8,
    hi: u8,
}
/// applies ordered-state Sankoff scoring to one additive character and records
/// optimal root-state sets.

fn analyze_single_additive_character(
    ds: &Dataset,
    tr: &Tree,
    ch: usize,
    weight: u64,
) -> Result<SingleCharAnalysis, String> {
    let mut postorder = Vec::with_capacity(tr.nodes.len());
    postorder_fill(tr, tr.root, &mut postorder);

    let mut costs = vec![INF; tr.nodes.len() * NSTATES];
    let mut node_states = vec![0u64; tr.nodes.len()];

    for (i, node) in tr.nodes.iter().enumerate() {
        if let Some(taxon) = node.taxon {
            let bits = dataset_state_at(ds, taxon, ch)?.bits();
            let base = i * NSTATES;
            for s in 0..NSTATES {
                if ((bits >> s) & 1) == 1 {
                    costs[base + s] = 0;
                }
            }
            node_states[i] = bits;
        }
    }

    for &v in &postorder {
        let node = &tr.nodes[v];
        if node.taxon.is_some() {
            continue;
        }

        if node.children.is_empty() {
            return Err(format!("internal node {v} has no children"));
        }

        let base = v * NSTATES;
        for s in 0..NSTATES {
            let mut total = 0u64;
            for &child in &node.children {
                let child_base = child * NSTATES;
                let mut best = INF;
                for k in 0..NSTATES {
                    let edge = s.abs_diff(k) as u64;
                    best = best.min(costs[child_base + k].saturating_add(edge));
                }
                total = total.saturating_add(best);
            }
            costs[base + s] = total;
        }

        let best = costs[base..base + NSTATES].iter().copied().min().unwrap_or(0);
        let mut bits = 0u64;
        for s in 0..NSTATES {
            if costs[base + s] == best {
                bits |= 1u64 << s;
            }
        }
        node_states[v] = bits;
    }

    let root_base = tr.root * NSTATES;
    let steps = costs[root_base..root_base + NSTATES].iter().copied().min().unwrap_or(0);

    Ok(SingleCharAnalysis { steps: steps * weight, node_states })
}
/// collects a postorder node traversal for dynamic programs.

fn postorder_fill(tr: &Tree, v: usize, out: &mut Vec<usize>) {
    for &child in &tr.nodes[v].children {
        postorder_fill(tr, child, out);
    }
    out.push(v);
}
/// converts a bitset into the lowest/highest observed ordered state interval.

fn range_from_bits(bits: u64) -> OrderedRange {
    if bits == StateSet::ALL36.bits() {
        return OrderedRange { lo: 0, hi: 35 };
    }

    let mut lo: Option<u8> = None;
    let mut hi = 0u8;
    for i in 0..36u8 {
        if ((bits >> i) & 1) == 1 {
            if lo.is_none() {
                lo = Some(i);
            }
            hi = i;
        }
    }

    OrderedRange { lo: lo.unwrap_or(0), hi }
}
/// safely retrieves one taxon-character state set from the row-major matrix.

fn dataset_state_at(ds: &Dataset, taxon: usize, ch: usize) -> Result<StateSet, String> {
    let idx = taxon
        .checked_mul(ds.nchar)
        .and_then(|x| x.checked_add(ch))
        .ok_or_else(|| "dataset index overflow".to_string())?;

    ds.matrix
        .get(idx)
        .copied()
        .ok_or_else(|| format!("dataset state index out of range: taxon={taxon} ch={ch}"))
}
/// computes default non-additive minimum steps for one character.

fn minsteps_char(ds: &Dataset, ch: usize) -> u64 {
    minsteps_char_with_setting(ds, ch, CharSetting { active: true, additive: false, weight: 1 })
}
/// computes weighted minimum possible steps for one character under
/// active/additive settings.

fn minsteps_char_with_setting(ds: &Dataset, ch: usize, cs: CharSetting) -> u64 {
    if !cs.active {
        return 0;
    }

    let weight = cs.weight as u64;

    if cs.additive {
        let mut lo: Option<u8> = None;
        let mut hi = 0u8;

        for taxon in 0..ds.ntax {
            let idx = taxon * ds.nchar + ch;
            let bits = ds.matrix[idx].bits();
            if bits == StateSet::ALL36.bits() {
                continue;
            }
            let rg = range_from_bits(bits);
            lo = Some(lo.map(|x| x.min(rg.lo)).unwrap_or(rg.lo));
            hi = hi.max(rg.hi);
        }

        return lo.map(|x| (hi - x) as u64 * weight).unwrap_or(0);
    }

    let mut seen = 0u64;

    for taxon in 0..ds.ntax {
        let idx = taxon * ds.nchar + ch;
        let bits = ds.matrix[idx].bits();
        if bits != StateSet::ALL36.bits() {
            seen |= bits;
        }
    }

    let k = seen.count_ones() as u64;
    if k == 0 { 0 } else { (k - 1) * weight }
}
/// computes default non-additive maximum steps for one character.

fn maxsteps_char(ds: &Dataset, ch: usize) -> u64 {
    maxsteps_char_with_setting(ds, ch, CharSetting { active: true, additive: false, weight: 1 })
}
/// computes weighted maximum possible steps for one character under
/// active/additive settings.

fn maxsteps_char_with_setting(ds: &Dataset, ch: usize, cs: CharSetting) -> u64 {
    if !cs.active {
        return 0;
    }

    let weight = cs.weight as u64;

    if cs.additive {
        let mut vals = Vec::<u8>::new();
        for taxon in 0..ds.ntax {
            let idx = taxon * ds.nchar + ch;
            let bits = ds.matrix[idx].bits();
            if bits == StateSet::ALL36.bits() || bits.count_ones() != 1 {
                continue;
            }
            vals.push(bits.trailing_zeros() as u8);
        }

        if vals.is_empty() {
            return 0;
        }

        let mut best = 0u64;
        for &candidate in &vals {
            let sum = vals.iter().map(|&v| v.abs_diff(candidate) as u64).sum::<u64>();
            best = best.max(sum);
        }
        return best * weight;
    }

    use std::collections::HashMap;

    let mut counts = HashMap::<u64, usize>::new();
    let mut total = 0usize;

    for taxon in 0..ds.ntax {
        let idx = taxon * ds.nchar + ch;
        let bits = ds.matrix[idx].bits();

        /*
          Only singleton states are treated as fixed here; polymorphic and
          missing states do not refine the maximum-step statistic.
        */
        if bits != 0 && bits.count_ones() == 1 {
            *counts.entry(bits).or_insert(0) += 1;
            total += 1;
        }
    }

    if total == 0 {
        return 0;
    }

    let max_count = counts.values().copied().max().unwrap_or(0);
    (total.saturating_sub(max_count)) as u64 * weight
}
/// formats low-36-bit state masks for xsteps output.

pub fn format_state_bits(bits: u64) -> String {
    if bits == 0 {
        return "?".to_string();
    }

    let mut s = String::new();
    for i in 0..36usize {
        if ((bits >> i) & 1) == 1 {
            s.push(util::idx_to_symbol(i as u8));
        }
    }
    s
}

/// Minimum and maximum possible steps for one character from the dataset
/// alone.
#[derive(Debug, Clone)]
pub struct CharMinMaxRow {
    pub character: usize,
    pub min_steps: u64,
    pub max_steps: u64,
}

/// For `steps;`: show min/max steps per character (dataset-only, independent of trees).
pub fn character_min_max_steps(ds: &Dataset) -> Vec<CharMinMaxRow> {
    let mut out = Vec::with_capacity(ds.nchar);
    for ch in 0..ds.nchar {
        out.push(CharMinMaxRow {
            character: ch,
            min_steps: minsteps_char(ds, ch),
            max_steps: maxsteps_char(ds, ch),
        });
    }
    out
}

/// Successive-weighting result: character and its integer rc10 bucket.
#[derive(Debug, Clone)]
pub struct SaWeightRow {
    pub character: usize,
    pub rc10: u32,
}
/// Computes successive weights across every supplied MPT. Each character's RC
/// is diagnosed independently on every tree, then the arithmetic mean RC is
/// converted to the rc10 bucket.
pub fn successive_weights_rc10_from_trees(
    ds: &crate::engines::dataset::Dataset,
    cfg: &crate::engines::ccode::CharConfig,
    trees: &[crate::engines::trees::Tree],
) -> Result<Vec<SaWeightRow>, String> {
    if ds.nchar == 0 {
        return Err("xsteps w: dataset has 0 characters".to_string());
    }
    if trees.is_empty() {
        return Err("xsteps w: tree set is empty".to_string());
    }
    if cfg.len() != ds.nchar {
        return Err(format!("xsteps w: ccode length {} != dataset nchar {}", cfg.len(), ds.nchar));
    }

    let mut rows = Vec::with_capacity(ds.nchar);

    for ch in 0..ds.nchar {
        let setting = CharSetting { active: true, additive: cfg.chars[ch].additive, weight: 1 };
        let min_s = minsteps_char_with_setting(ds, ch, setting);
        let max_s = maxsteps_char_with_setting(ds, ch, setting);
        let mut rc_sum = 0.0;

        for tr in trees {
            let steps = analyze_single_character_with_setting(ds, tr, ch, setting)?.steps;
            let ci = if steps == 0 { 1.0 } else { (min_s as f64) / (steps as f64) };
            let ri = if max_s > min_s {
                (max_s.saturating_sub(steps)) as f64 / (max_s - min_s) as f64
            } else {
                /* Undefined RI receives full credit, preserving prior behavior. */
                1.0
            };
            rc_sum += ci * ri;
        }

        let mean_rc = rc_sum / trees.len() as f64;
        let rc10 = rc_to_rc10_floor(mean_rc);

        rows.push(SaWeightRow { character: ch, rc10 });
    }

    Ok(rows)
}
/// converts a floating RC value to the integer weight bucket.

fn rc_to_rc10_floor(rc: f64) -> u32 {
    if !rc.is_finite() || rc <= 0.0 {
        0
    } else if rc >= 1.0 {
        10
    } else {
        (rc * 10.0).floor() as u32
    }
}
/// writes successive-weight  back to the mutable character
/// configuration.

pub fn apply_successive_weights_to_ccode(
    cfg: &mut crate::engines::ccode::CharConfig,
    nchar: usize,
    rows: &[SaWeightRow],
) -> Result<(), String> {
    if cfg.len() != nchar {
        return Err(format!("ccode length mismatch: cfg.len()={} nchar={}", cfg.len(), nchar));
    }

    for r in rows {
        if r.character >= nchar {
            return Err(format!("character {} out of range (nchar={})", r.character, nchar));
        }

        /* Write Hennig86-style rc10   */
        cfg.chars[r.character].weight = r.rc10;
    }

    Ok(())
}

/// Best and worst fit statistics for one character across a tree set.
#[derive(Debug, Clone)]
pub struct XStepsMBestWorstRow {
    pub character: usize,

    pub best_steps: u64,
    pub worst_steps: u64,

    pub best_ci: f64,
    pub worst_ci: f64,

    pub best_ri: Option<f64>,
    pub worst_ri: Option<f64>,
}
/// summarizes best and worst per-character fits across all current trees.

pub fn analyze_best_worst_fits_across_trees(
    ds: &Dataset,
    cfg: &CharConfig,
    trees: &[Tree],
) -> Result<Vec<XStepsMBestWorstRow>, String> {
    if trees.is_empty() {
        return Err("xsteps m: no trees provided".to_string());
    }
    if ds.nchar == 0 {
        return Err("xsteps m: dataset has 0 characters".to_string());
    }
    if cfg.len() != ds.nchar {
        return Err(format!(
            "xsteps m: char_config nchar={} != dataset nchar={}",
            cfg.len(),
            ds.nchar
        ));
    }

    let reports = analyze_character_fits(ds, cfg, trees)?;

    let mut best_steps = vec![u64::MAX; ds.nchar];
    let mut worst_steps = vec![0u64; ds.nchar];

    for rep in &reports {
        for row in &rep.rows {
            let ch = row.character;
            let s = row.steps;
            if s < best_steps[ch] {
                best_steps[ch] = s;
            }
            if s > worst_steps[ch] {
                worst_steps[ch] = s;
            }
        }
    }

    let mut out = Vec::with_capacity(ds.nchar);
    for ch in 0..ds.nchar {
        let min_s = minsteps_char(ds, ch);
        let max_s = maxsteps_char(ds, ch);

        let best_obs = best_steps[ch];
        let worst_obs = worst_steps[ch];

        let best_ci = if best_obs == 0 { 1.0 } else { (min_s as f64) / (best_obs as f64) };
        let worst_ci = if worst_obs == 0 { 1.0 } else { (min_s as f64) / (worst_obs as f64) };

        let best_ri = if max_s > min_s {
            Some((max_s.saturating_sub(best_obs)) as f64 / (max_s - min_s) as f64)
        } else {
            None
        };
        let worst_ri = if max_s > min_s {
            Some((max_s.saturating_sub(worst_obs)) as f64 / (max_s - min_s) as f64)
        } else {
            None
        };

        out.push(XStepsMBestWorstRow {
            character: ch,
            best_steps: best_obs,
            worst_steps: worst_obs,
            best_ci,
            worst_ci,
            best_ri,
            worst_ri,
        });
    }

    Ok(out)
}