phykit 2.2.0__tar.gz → 2.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. {phykit-2.2.0 → phykit-2.2.1}/PKG-INFO +1 -1
  2. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/parsimony_utils.py +145 -0
  3. {phykit-2.2.0 → phykit-2.2.1}/phykit/phykit.py +32 -3
  4. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/character_map.py +172 -29
  5. phykit-2.2.1/phykit/version.py +1 -0
  6. {phykit-2.2.0 → phykit-2.2.1}/phykit.egg-info/PKG-INFO +1 -1
  7. phykit-2.2.0/phykit/version.py +0 -1
  8. {phykit-2.2.0 → phykit-2.2.1}/LICENSE.md +0 -0
  9. {phykit-2.2.0 → phykit-2.2.1}/README.md +0 -0
  10. {phykit-2.2.0 → phykit-2.2.1}/phykit/__init__.py +0 -0
  11. {phykit-2.2.0 → phykit-2.2.1}/phykit/__main__.py +0 -0
  12. {phykit-2.2.0 → phykit-2.2.1}/phykit/cli_registry.py +0 -0
  13. {phykit-2.2.0 → phykit-2.2.1}/phykit/errors.py +0 -0
  14. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/__init__.py +0 -0
  15. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/boolean_argument_parsing.py +0 -0
  16. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/caching.py +0 -0
  17. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/circular_layout.py +0 -0
  18. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/color_annotations.py +0 -0
  19. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/discrete_models.py +0 -0
  20. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/files.py +0 -0
  21. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/geological_timescale.py +0 -0
  22. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/json_output.py +0 -0
  23. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/parallel.py +0 -0
  24. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/pgls_utils.py +0 -0
  25. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/plot_config.py +0 -0
  26. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/quartet_utils.py +0 -0
  27. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/stats_summary.py +0 -0
  28. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/streaming.py +0 -0
  29. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/trait_parsing.py +0 -0
  30. {phykit-2.2.0 → phykit-2.2.1}/phykit/helpers/tree_paths.py +0 -0
  31. {phykit-2.2.0 → phykit-2.2.1}/phykit/service_factories.py +0 -0
  32. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/__init__.py +0 -0
  33. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/__init__.py +0 -0
  34. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/_fasta.py +0 -0
  35. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/alignment_entropy.py +0 -0
  36. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/alignment_length.py +0 -0
  37. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/alignment_length_no_gaps.py +0 -0
  38. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/alignment_outlier_taxa.py +0 -0
  39. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/alignment_recoding.py +0 -0
  40. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/alignment_subsample.py +0 -0
  41. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/base.py +0 -0
  42. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/column_score.py +0 -0
  43. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/composition_per_taxon.py +0 -0
  44. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/compositional_bias_per_site.py +0 -0
  45. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/create_concatenation_matrix.py +0 -0
  46. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/dfoil.py +0 -0
  47. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/dna_threader.py +0 -0
  48. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/dstatistic.py +0 -0
  49. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/evolutionary_rate_per_site.py +0 -0
  50. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/faidx.py +0 -0
  51. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/gc_content.py +0 -0
  52. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/identity_matrix.py +0 -0
  53. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/mask_alignment.py +0 -0
  54. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/occupancy_filter.py +0 -0
  55. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/occupancy_per_taxon.py +0 -0
  56. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/pairwise_identity.py +0 -0
  57. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/parsimony_informative_sites.py +0 -0
  58. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/phylo_gwas.py +0 -0
  59. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/plot_alignment_qc.py +0 -0
  60. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/rcv.py +0 -0
  61. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/rcvt.py +0 -0
  62. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/rename_fasta_entries.py +0 -0
  63. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/sum_of_pairs_score.py +0 -0
  64. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/taxon_groups.py +0 -0
  65. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/alignment/variable_sites.py +0 -0
  66. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/base.py +0 -0
  67. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/__init__.py +0 -0
  68. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/ancestral_reconstruction.py +0 -0
  69. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/base.py +0 -0
  70. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/bipartition_support_stats.py +0 -0
  71. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/branch_length_multiplier.py +0 -0
  72. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/chronogram.py +0 -0
  73. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/collapse_branches.py +0 -0
  74. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/concordance_asr.py +0 -0
  75. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/consensus_network.py +0 -0
  76. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/consensus_tree.py +0 -0
  77. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/cont_map.py +0 -0
  78. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/cophylo.py +0 -0
  79. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/covarying_evolutionary_rates.py +0 -0
  80. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/density_map.py +0 -0
  81. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/discordance_asymmetry.py +0 -0
  82. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/dtt.py +0 -0
  83. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/dvmc.py +0 -0
  84. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/evo_tempo_map.py +0 -0
  85. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/evolutionary_rate.py +0 -0
  86. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/faiths_pd.py +0 -0
  87. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/fit_continuous.py +0 -0
  88. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/fit_discrete.py +0 -0
  89. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/hidden_paralogy_check.py +0 -0
  90. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/hybridization.py +0 -0
  91. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/independent_contrasts.py +0 -0
  92. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/internal_branch_stats.py +0 -0
  93. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/internode_labeler.py +0 -0
  94. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/kf_distance.py +0 -0
  95. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/last_common_ancestor_subtree.py +0 -0
  96. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/lb_score.py +0 -0
  97. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/ltt.py +0 -0
  98. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/monophyly_check.py +0 -0
  99. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/nearest_neighbor_interchange.py +0 -0
  100. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/neighbor_net.py +0 -0
  101. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/network_signal.py +0 -0
  102. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/ou_shift_detection.py +0 -0
  103. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/ouwie.py +0 -0
  104. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/parsimony_score.py +0 -0
  105. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/patristic_distances.py +0 -0
  106. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phenogram.py +0 -0
  107. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylo_anova.py +0 -0
  108. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylo_heatmap.py +0 -0
  109. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylo_impute.py +0 -0
  110. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylo_logistic.py +0 -0
  111. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylo_path.py +0 -0
  112. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylogenetic_glm.py +0 -0
  113. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylogenetic_ordination.py +0 -0
  114. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylogenetic_regression.py +0 -0
  115. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylogenetic_signal.py +0 -0
  116. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/phylomorphospace.py +0 -0
  117. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/polytomy_test.py +0 -0
  118. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/print_tree.py +0 -0
  119. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/prune_tree.py +0 -0
  120. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/quartet_network.py +0 -0
  121. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/quartet_pie.py +0 -0
  122. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/rate_heterogeneity.py +0 -0
  123. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/relative_rate_test.py +0 -0
  124. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/rename_tree_tips.py +0 -0
  125. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/rf_distance.py +0 -0
  126. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/root_tree.py +0 -0
  127. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/saturation.py +0 -0
  128. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/simmap_summary.py +0 -0
  129. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/spectral_discordance.py +0 -0
  130. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/spr.py +0 -0
  131. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/spurious_sequence.py +0 -0
  132. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/stochastic_character_map.py +0 -0
  133. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/terminal_branch_stats.py +0 -0
  134. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/threshold_model.py +0 -0
  135. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/tip_labels.py +0 -0
  136. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/tip_to_tip_distance.py +0 -0
  137. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/tip_to_tip_node_distance.py +0 -0
  138. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/total_tree_length.py +0 -0
  139. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/trait_correlation.py +0 -0
  140. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/trait_rate_map.py +0 -0
  141. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/transfer_annotations.py +0 -0
  142. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/tree_space.py +0 -0
  143. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/treeness.py +0 -0
  144. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/treeness_over_rcv.py +0 -0
  145. {phykit-2.2.0 → phykit-2.2.1}/phykit/services/tree/vcv_utils.py +0 -0
  146. {phykit-2.2.0 → phykit-2.2.1}/phykit.egg-info/SOURCES.txt +0 -0
  147. {phykit-2.2.0 → phykit-2.2.1}/phykit.egg-info/dependency_links.txt +0 -0
  148. {phykit-2.2.0 → phykit-2.2.1}/phykit.egg-info/entry_points.txt +0 -0
  149. {phykit-2.2.0 → phykit-2.2.1}/phykit.egg-info/requires.txt +0 -0
  150. {phykit-2.2.0 → phykit-2.2.1}/phykit.egg-info/top_level.txt +0 -0
  151. {phykit-2.2.0 → phykit-2.2.1}/setup.cfg +0 -0
  152. {phykit-2.2.0 → phykit-2.2.1}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: phykit
3
- Version: 2.2.0
3
+ Version: 2.2.1
4
4
  Home-page: https://github.com/jlsteenwyk/phykit
5
5
  Author: Jacob L. Steenwyk
6
6
  Author-email: jlsteenwyk@gmail.com
@@ -480,6 +480,151 @@ def _mask_to_state_set(states: tuple[str, ...], mask: int):
480
480
  )
481
481
 
482
482
 
483
+ def sankoff_downpass(
484
+ tree,
485
+ tip_states: dict[str, list[str]],
486
+ ) -> tuple[
487
+ dict[int, list[tuple[int, ...]]],
488
+ list[tuple[str, ...]],
489
+ list[int],
490
+ ]:
491
+ """Calculate exact unordered-parsimony costs on any tree topology.
492
+
493
+ Unlike the optimized Fitch path, this dynamic program handles nodes with
494
+ any number of children without introducing arbitrary binary resolutions.
495
+ Missing states (``?`` and ``-``) are compatible with every observed state.
496
+ """
497
+ n_chars = len(next(iter(tip_states.values())))
498
+ wildcard = {"?", "-"}
499
+ observed_states = [set() for _ in range(n_chars)]
500
+ for states in tip_states.values():
501
+ for index, state in enumerate(states):
502
+ if state not in wildcard:
503
+ observed_states[index].add(state)
504
+
505
+ states_by_char = [
506
+ tuple(sorted(states)) if states else ("?",)
507
+ for states in observed_states
508
+ ]
509
+ state_indexes = [
510
+ {state: index for index, state in enumerate(states)}
511
+ for states in states_by_char
512
+ ]
513
+
514
+ clades = _postorder_clades_direct(tree)
515
+ if clades is None:
516
+ clades = list(tree.find_clades(order="postorder"))
517
+ unreachable = len(clades) + 1
518
+ node_costs: dict[int, list[tuple[int, ...]]] = {}
519
+
520
+ for clade in clades:
521
+ cid = id(clade)
522
+ children = clade.clades
523
+ if not children:
524
+ costs_by_char = []
525
+ for index, state in enumerate(tip_states[clade.name]):
526
+ state_count = len(states_by_char[index])
527
+ if state in wildcard:
528
+ costs_by_char.append((0,) * state_count)
529
+ continue
530
+
531
+ observed_index = state_indexes[index][state]
532
+ costs_by_char.append(
533
+ tuple(
534
+ 0 if candidate == observed_index else unreachable
535
+ for candidate in range(state_count)
536
+ )
537
+ )
538
+ node_costs[cid] = costs_by_char
539
+ continue
540
+
541
+ child_costs = [node_costs[id(child)] for child in children]
542
+ costs_by_char = []
543
+ for char_index, states in enumerate(states_by_char):
544
+ state_count = len(states)
545
+ parent_costs = []
546
+ for parent_index in range(state_count):
547
+ total = 0
548
+ for child in child_costs:
549
+ child_char_costs = child[char_index]
550
+ total += min(
551
+ cost + (child_index != parent_index)
552
+ for child_index, cost in enumerate(child_char_costs)
553
+ )
554
+ parent_costs.append(total)
555
+ costs_by_char.append(tuple(parent_costs))
556
+ node_costs[cid] = costs_by_char
557
+
558
+ root_costs = node_costs[id(tree.root)]
559
+ scores = [min(costs) for costs in root_costs]
560
+ return node_costs, states_by_char, scores
561
+
562
+
563
+ def sankoff_uppass(
564
+ tree,
565
+ node_costs: dict[int, list[tuple[int, ...]]],
566
+ states_by_char: list[tuple[str, ...]],
567
+ parent_map: dict[int, object],
568
+ optimization: str = "acctran",
569
+ ) -> dict[int, list[str]]:
570
+ """Trace back one deterministic, globally optimal reconstruction.
571
+
572
+ DELTRAN prefers inheriting the parent state when a tie permits delaying a
573
+ change. ACCTRAN prefers a different child state when a tie permits moving
574
+ that change toward the root. Root ties use lexical state order for stable
575
+ output independent of child order.
576
+ """
577
+ node_states: dict[int, list[str]] = {}
578
+ root = tree.root
579
+ root_costs = node_costs[id(root)]
580
+ node_states[id(root)] = [
581
+ states[min_costs.index(min(min_costs))]
582
+ for states, min_costs in zip(states_by_char, root_costs)
583
+ ]
584
+
585
+ clades = _preorder_clades_direct(tree)
586
+ if clades is None:
587
+ clades = list(tree.find_clades(order="preorder"))
588
+
589
+ delay_changes = optimization == "deltran"
590
+ for clade in clades:
591
+ if clade == root:
592
+ continue
593
+
594
+ cid = id(clade)
595
+ parent_states = node_states[id(parent_map[cid])]
596
+ costs_by_char = node_costs[cid]
597
+ assigned = []
598
+ for states, costs, parent_state in zip(
599
+ states_by_char,
600
+ costs_by_char,
601
+ parent_states,
602
+ ):
603
+ transition_costs = [
604
+ cost + (state != parent_state)
605
+ for state, cost in zip(states, costs)
606
+ ]
607
+ best_cost = min(transition_costs)
608
+ candidates = [
609
+ state
610
+ for state, cost in zip(states, transition_costs)
611
+ if cost == best_cost
612
+ ]
613
+ if delay_changes and parent_state in candidates:
614
+ assigned.append(parent_state)
615
+ continue
616
+
617
+ changed_candidates = [
618
+ state for state in candidates if state != parent_state
619
+ ]
620
+ assigned.append(
621
+ changed_candidates[0] if changed_candidates else candidates[0]
622
+ )
623
+ node_states[cid] = assigned
624
+
625
+ return node_states
626
+
627
+
483
628
  def fitch_uppass_acctran(
484
629
  tree,
485
630
  node_state_sets: dict[int, list[set[str]]],
@@ -3011,7 +3011,7 @@ class Phykit:
3011
3011
  {help_header}
3012
3012
 
3013
3013
  Map discrete character changes onto a phylogenetic tree
3014
- using Fitch parsimony, classifying each change as a
3014
+ using unordered parsimony, classifying each change as a
3015
3015
  synapomorphy, convergence, or reversal.
3016
3016
 
3017
3017
  Two optimization strategies are supported:
@@ -3023,7 +3023,11 @@ class Phykit:
3023
3023
  Output is a cladogram (default) or phylogram (with
3024
3024
  --phylogram) annotated with colored circles indicating
3025
3025
  character state changes on each branch. Polytomies are
3026
- automatically resolved by adding zero-length branches.
3026
+ retained and analyzed directly.
3027
+
3028
+ Tree and matrix taxon labels are matched exactly and are
3029
+ case-sensitive. Mismatches are errors unless
3030
+ --allow-taxon-mismatch is specified.
3027
3031
 
3028
3032
  Summary statistics include tree length (total parsimony
3029
3033
  steps), consistency index (CI), and retention index (RI).
@@ -3036,7 +3040,9 @@ class Phykit:
3036
3040
  Usage:
3037
3041
  phykit character_map -t <tree> -d <data> -o <output>
3038
3042
  [--optimization acctran|deltran] [--phylogram]
3039
- [--characters 0,1,3] [--verbose] [--json]
3043
+ [--characters 0,1,3] [--allow-taxon-mismatch]
3044
+ [--change-marker-size <float>] [--change-fontsize <float>]
3045
+ [--verbose] [--json]
3040
3046
  [--fig-width <float>] [--fig-height <float>]
3041
3047
  [--dpi <int>] [--no-title] [--title <str>]
3042
3048
  [--legend-position <str>]
@@ -3067,6 +3073,17 @@ class Phykit:
3067
3073
  plot (e.g., 0,1,3); all
3068
3074
  characters shown by default
3069
3075
 
3076
+ --allow-taxon-mismatch warn and analyze only taxa shared
3077
+ by the tree and character matrix;
3078
+ mismatches are errors by default
3079
+
3080
+ --change-marker-size character-change circle size in
3081
+ points squared; must be positive
3082
+
3083
+ --change-fontsize font size for character indices
3084
+ and state transitions; must be
3085
+ positive
3086
+
3070
3087
  --verbose print per-character details
3071
3088
  including CI/RI and changes
3072
3089
 
@@ -3095,6 +3112,18 @@ class Phykit:
3095
3112
  "--characters", type=str, default=None,
3096
3113
  required=False, help=SUPPRESS, metavar=""
3097
3114
  )
3115
+ parser.add_argument(
3116
+ "--allow-taxon-mismatch", action="store_true",
3117
+ required=False, help=SUPPRESS,
3118
+ )
3119
+ parser.add_argument(
3120
+ "--change-marker-size", type=float, default=None,
3121
+ required=False, help=SUPPRESS, metavar="",
3122
+ )
3123
+ parser.add_argument(
3124
+ "--change-fontsize", type=float, default=None,
3125
+ required=False, help=SUPPRESS, metavar="",
3126
+ )
3098
3127
  parser.add_argument(
3099
3128
  "--verbose", action="store_true", required=False, help=SUPPRESS
3100
3129
  )
@@ -1,9 +1,10 @@
1
1
  """
2
2
  Character mapping on a phylogenetic tree.
3
3
 
4
- Performs Fitch parsimony reconstruction on a discrete character matrix,
5
- classifies changes as synapomorphies, convergences, or reversals, and
6
- produces a phylogram/cladogram plot with annotated character changes.
4
+ Performs unordered parsimony reconstruction on a discrete character matrix,
5
+ using optimized Fitch reconstruction for binary trees and exact Sankoff costs
6
+ for multifurcations. Classifies changes as synapomorphies, convergences, or
7
+ reversals and produces a phylogram/cladogram plot with annotated changes.
7
8
 
8
9
  Uses the generalized parsimony utilities in phykit.helpers.parsimony_utils.
9
10
  """
@@ -39,10 +40,6 @@ def build_parent_map(*args, **kwargs):
39
40
  return _parsimony_utils().build_parent_map(*args, **kwargs)
40
41
 
41
42
 
42
- def resolve_polytomies(*args, **kwargs):
43
- return _parsimony_utils().resolve_polytomies(*args, **kwargs)
44
-
45
-
46
43
  def fitch_downpass(*args, **kwargs):
47
44
  return _parsimony_utils().fitch_downpass(*args, **kwargs)
48
45
 
@@ -55,6 +52,14 @@ def fitch_uppass_deltran(*args, **kwargs):
55
52
  return _parsimony_utils().fitch_uppass_deltran(*args, **kwargs)
56
53
 
57
54
 
55
+ def sankoff_downpass(*args, **kwargs):
56
+ return _parsimony_utils().sankoff_downpass(*args, **kwargs)
57
+
58
+
59
+ def sankoff_uppass(*args, **kwargs):
60
+ return _parsimony_utils().sankoff_uppass(*args, **kwargs)
61
+
62
+
58
63
  def detect_changes(*args, **kwargs):
59
64
  return _parsimony_utils().detect_changes(*args, **kwargs)
60
65
 
@@ -76,6 +81,9 @@ class CharacterMap(Tree):
76
81
  self.optimization = parsed["optimization"]
77
82
  self.phylogram = parsed["phylogram"]
78
83
  self.characters_filter = parsed["characters_filter"]
84
+ self.allow_taxon_mismatch = parsed["allow_taxon_mismatch"]
85
+ self.change_marker_size = parsed["change_marker_size"]
86
+ self.change_fontsize = parsed["change_fontsize"]
79
87
  self.verbose = parsed["verbose"]
80
88
  self.json_output = parsed["json_output"]
81
89
  self.plot_config = parsed["plot_config"]
@@ -89,6 +97,18 @@ class CharacterMap(Tree):
89
97
  if chars_str is not None:
90
98
  characters_filter = [int(c.strip()) for c in chars_str.split(",")]
91
99
 
100
+ change_marker_size = getattr(args, "change_marker_size", None)
101
+ change_fontsize = getattr(args, "change_fontsize", None)
102
+ for option, value in (
103
+ ("--change-marker-size", change_marker_size),
104
+ ("--change-fontsize", change_fontsize),
105
+ ):
106
+ if value is not None and value <= 0:
107
+ raise PhykitUserError(
108
+ [f"{option} must be greater than 0."],
109
+ code=2,
110
+ )
111
+
92
112
  return dict(
93
113
  tree_file_path=args.tree,
94
114
  data_path=args.data,
@@ -96,6 +116,9 @@ class CharacterMap(Tree):
96
116
  optimization=getattr(args, "optimization", "acctran"),
97
117
  phylogram=getattr(args, "phylogram", False),
98
118
  characters_filter=characters_filter,
119
+ allow_taxon_mismatch=getattr(args, "allow_taxon_mismatch", False),
120
+ change_marker_size=change_marker_size,
121
+ change_fontsize=change_fontsize,
99
122
  verbose=getattr(args, "verbose", False),
100
123
  json_output=getattr(args, "json", False),
101
124
  plot_config=PlotConfig.from_args(args),
@@ -111,6 +134,7 @@ class CharacterMap(Tree):
111
134
  shared_count, tips_to_prune, tip_states = self._shared_character_taxa_setup(
112
135
  tree_tip_names,
113
136
  tip_states,
137
+ allow_taxon_mismatch=self.allow_taxon_mismatch,
114
138
  )
115
139
  if shared_count < 3:
116
140
  raise PhykitUserError(
@@ -122,26 +146,20 @@ class CharacterMap(Tree):
122
146
  )
123
147
 
124
148
  preorder_clades = list(self._iter_preorder(tree.root))
125
- needs_polytomy_resolution = any(
149
+ has_multifurcation = any(
126
150
  len(clade.clades) > 2 for clade in preorder_clades
127
151
  )
128
152
  needs_branch_length_fill = any(
129
153
  clade.branch_length is None for clade in preorder_clades
130
154
  )
131
155
  needs_working_copy = bool(tips_to_prune) or (
132
- needs_polytomy_resolution
133
- or needs_branch_length_fill
134
- or self.plot_config.ladderize
156
+ needs_branch_length_fill or self.plot_config.ladderize
135
157
  )
136
158
  if needs_working_copy:
137
159
  tree = self._fast_copy(tree)
138
160
  if tips_to_prune:
139
161
  tree = self.prune_tree_using_taxa_list(tree, tips_to_prune)
140
162
 
141
- # Resolve polytomies
142
- if needs_polytomy_resolution:
143
- resolve_polytomies(tree)
144
-
145
163
  # Ladderize if requested
146
164
  if self.plot_config.ladderize:
147
165
  tree.ladderize()
@@ -155,14 +173,33 @@ class CharacterMap(Tree):
155
173
  # Build parent map
156
174
  parent_map = build_parent_map(tree)
157
175
 
158
- # Fitch downpass
159
- node_state_sets, scores = fitch_downpass(tree, tip_states)
160
-
161
- # Uppass: ACCTRAN or DELTRAN
162
- if self.optimization == "deltran":
163
- node_states = fitch_uppass_deltran(tree, node_state_sets, parent_map)
176
+ if has_multifurcation:
177
+ node_costs, states_by_char, scores = sankoff_downpass(
178
+ tree,
179
+ tip_states,
180
+ )
181
+ node_states = sankoff_uppass(
182
+ tree,
183
+ node_costs,
184
+ states_by_char,
185
+ parent_map,
186
+ optimization=self.optimization,
187
+ )
164
188
  else:
165
- node_states = fitch_uppass_acctran(tree, node_state_sets, parent_map)
189
+ # Keep the optimized Fitch implementation for binary trees.
190
+ node_state_sets, scores = fitch_downpass(tree, tip_states)
191
+ if self.optimization == "deltran":
192
+ node_states = fitch_uppass_deltran(
193
+ tree,
194
+ node_state_sets,
195
+ parent_map,
196
+ )
197
+ else:
198
+ node_states = fitch_uppass_acctran(
199
+ tree,
200
+ node_state_sets,
201
+ parent_map,
202
+ )
166
203
 
167
204
  # Detect and classify changes
168
205
  branch_changes = detect_changes(tree, node_states, parent_map)
@@ -219,21 +256,98 @@ class CharacterMap(Tree):
219
256
  )
220
257
 
221
258
  @staticmethod
222
- def _shared_character_taxa_setup(tree_tip_names: list[str], tip_states: dict):
223
- """Return shared count, tree tips to prune, and filtered states."""
259
+ def _shared_character_taxa_setup(
260
+ tree_tip_names: list[str],
261
+ tip_states: dict,
262
+ allow_taxon_mismatch: bool = False,
263
+ ):
264
+ """Validate taxa and optionally return their shared subset."""
224
265
  state_count = len(tip_states)
225
266
  if state_count >= 3 and len(tree_tip_names) == state_count:
226
267
  if tree_tip_names[0] == next(iter(tip_states)):
227
268
  if tree_tip_names[-1] == next(reversed(tip_states)):
228
269
  state_names = list(tip_states)
229
270
  if state_names == tree_tip_names:
271
+ # Matrix parsing has already rejected empty and duplicate
272
+ # labels, so an exact ordered match proves the tree labels
273
+ # have those properties as well.
230
274
  return state_count, [], tip_states
231
275
 
276
+ unnamed_tips = sum(
277
+ 1
278
+ for name in tree_tip_names
279
+ if not isinstance(name, str) or not name.strip()
280
+ )
281
+ if unnamed_tips:
282
+ raise PhykitUserError(
283
+ [
284
+ f"Tree contains {unnamed_tips} empty or unnamed taxon labels.",
285
+ "Every tree tip must have a unique, non-empty name.",
286
+ ],
287
+ code=2,
288
+ )
289
+
290
+ duplicate_tree_tips = sorted(
291
+ name
292
+ for name, count in Counter(tree_tip_names).items()
293
+ if count > 1
294
+ )
295
+ if duplicate_tree_tips:
296
+ raise PhykitUserError(
297
+ [
298
+ f"Tree contains duplicate taxon labels: "
299
+ f"{', '.join(duplicate_tree_tips)}",
300
+ "Every tree tip must have a unique name.",
301
+ ],
302
+ code=2,
303
+ )
304
+
305
+ empty_matrix_taxa = [
306
+ name
307
+ for name in tip_states
308
+ if not isinstance(name, str) or not name.strip()
309
+ ]
310
+ if empty_matrix_taxa:
311
+ raise PhykitUserError(
312
+ ["Character matrix contains an empty taxon label."],
313
+ code=2,
314
+ )
315
+
232
316
  tree_tips = set(tree_tip_names)
233
317
  matrix_taxa = set(tip_states)
234
318
  shared = tree_tips & matrix_taxa
319
+ tree_only = tree_tips - matrix_taxa
320
+ matrix_only = matrix_taxa - tree_tips
321
+ if not tree_only and not matrix_only:
322
+ return len(shared), [], tip_states
323
+
324
+ tree_only_text = ", ".join(sorted(tree_only)) if tree_only else "none"
325
+ matrix_only_text = ", ".join(sorted(matrix_only)) if matrix_only else "none"
326
+ tree_taxon_word = "taxon" if len(tree_only) == 1 else "taxa"
327
+ matrix_taxon_word = "taxon" if len(matrix_only) == 1 else "taxa"
328
+ difference_messages = [
329
+ "Taxon labels differ between the tree and character matrix.",
330
+ f"{len(tree_only)} {tree_taxon_word} in tree but not in character matrix: "
331
+ f"{tree_only_text}",
332
+ f"{len(matrix_only)} {matrix_taxon_word} in character matrix but not in tree: "
333
+ f"{matrix_only_text}",
334
+ ]
335
+ if not allow_taxon_mismatch:
336
+ difference_messages.append(
337
+ "Use --allow-taxon-mismatch to analyze only shared taxa."
338
+ )
339
+ raise PhykitUserError(difference_messages, code=2)
340
+
341
+ import sys
342
+
343
+ for message in difference_messages[1:]:
344
+ print(f"Warning: {message}", file=sys.stderr)
235
345
  tips_to_prune = [tip for tip in tree_tip_names if tip not in shared]
236
- filtered_states = {taxon: tip_states[taxon] for taxon in shared}
346
+ filtered_states = {
347
+ taxon: states
348
+ for taxon, states in tip_states.items()
349
+ if taxon in shared
350
+ }
237
351
  return len(shared), tips_to_prune, filtered_states
238
352
 
239
353
  @staticmethod
@@ -515,6 +629,19 @@ class CharacterMap(Tree):
515
629
  fields = line.rstrip("\n\r").split("\t")
516
630
  taxon = fields[0]
517
631
  states = fields[1:]
632
+ if not taxon.strip():
633
+ raise PhykitUserError(
634
+ [f"Row {row_num} has an empty taxon label."],
635
+ code=2,
636
+ )
637
+ if taxon in tip_states:
638
+ raise PhykitUserError(
639
+ [
640
+ f"Row {row_num} contains duplicate taxon label "
641
+ f"'{taxon}'."
642
+ ],
643
+ code=2,
644
+ )
518
645
  if len(states) != n_expected:
519
646
  raise PhykitUserError(
520
647
  [
@@ -822,8 +949,16 @@ class CharacterMap(Tree):
822
949
 
823
950
  # Character change circles on branches
824
951
  # Smaller markers in circular mode — radial branches are shorter
825
- marker_size = max(10, min(50, 300 / max(n_tips, 1)))
826
- change_fontsize = max(3.0, min(5.0, 6.0 - n_tips * 0.03))
952
+ marker_size = (
953
+ self.change_marker_size
954
+ if self.change_marker_size is not None
955
+ else max(10, min(50, 300 / max(n_tips, 1)))
956
+ )
957
+ change_fontsize = (
958
+ self.change_fontsize
959
+ if self.change_fontsize is not None
960
+ else max(3.0, min(5.0, 6.0 - n_tips * 0.03))
961
+ )
827
962
 
828
963
  filter_set = set(self.characters_filter) if self.characters_filter else None
829
964
  change_x = []
@@ -993,8 +1128,16 @@ class CharacterMap(Tree):
993
1128
  # Character change circles on branches
994
1129
  # Use scatter (marker size in points²) so circles stay round
995
1130
  # regardless of axis aspect ratio.
996
- marker_size = max(15, min(80, 600 / max(n_tips, 1)))
997
- change_fontsize = max(3.0, min(6.0, 7.0 - n_tips * 0.03))
1131
+ marker_size = (
1132
+ self.change_marker_size
1133
+ if self.change_marker_size is not None
1134
+ else max(15, min(80, 600 / max(n_tips, 1)))
1135
+ )
1136
+ change_fontsize = (
1137
+ self.change_fontsize
1138
+ if self.change_fontsize is not None
1139
+ else max(3.0, min(6.0, 7.0 - n_tips * 0.03))
1140
+ )
998
1141
 
999
1142
  # Filter characters if requested
1000
1143
  filter_set = set(self.characters_filter) if self.characters_filter else None
@@ -0,0 +1 @@
1
+ __version__ = "2.2.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: phykit
3
- Version: 2.2.0
3
+ Version: 2.2.1
4
4
  Home-page: https://github.com/jlsteenwyk/phykit
5
5
  Author: Jacob L. Steenwyk
6
6
  Author-email: jlsteenwyk@gmail.com
@@ -1 +0,0 @@
1
- __version__ = "2.2.0"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes