phykit 2.3.0__tar.gz → 2.4.0__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 (154) hide show
  1. {phykit-2.3.0 → phykit-2.4.0}/PKG-INFO +1 -1
  2. {phykit-2.3.0 → phykit-2.4.0}/phykit/cli_registry.py +61 -1
  3. {phykit-2.3.0 → phykit-2.4.0}/phykit/errors.py +3 -0
  4. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/json_output.py +44 -10
  5. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/trait_parsing.py +52 -2
  6. {phykit-2.3.0 → phykit-2.4.0}/phykit/phykit.py +75 -13
  7. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/alignment_subsample.py +70 -65
  8. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/codon_dnds.py +25 -1
  9. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/create_concatenation_matrix.py +106 -15
  10. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/dna_threader.py +39 -87
  11. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/variable_sites.py +4 -1
  12. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/base.py +279 -19
  13. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/character_map.py +84 -3
  14. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/network_signal.py +98 -0
  15. phykit-2.4.0/phykit/services/tree/spurious_sequence.py +691 -0
  16. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/threshold_model.py +31 -18
  17. phykit-2.4.0/phykit/version.py +1 -0
  18. {phykit-2.3.0 → phykit-2.4.0}/phykit.egg-info/PKG-INFO +1 -1
  19. {phykit-2.3.0 → phykit-2.4.0}/phykit.egg-info/entry_points.txt +86 -0
  20. {phykit-2.3.0 → phykit-2.4.0}/setup.py +9 -7
  21. phykit-2.3.0/phykit/services/tree/spurious_sequence.py +0 -231
  22. phykit-2.3.0/phykit/version.py +0 -1
  23. {phykit-2.3.0 → phykit-2.4.0}/LICENSE.md +0 -0
  24. {phykit-2.3.0 → phykit-2.4.0}/README.md +0 -0
  25. {phykit-2.3.0 → phykit-2.4.0}/phykit/__init__.py +0 -0
  26. {phykit-2.3.0 → phykit-2.4.0}/phykit/__main__.py +0 -0
  27. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/__init__.py +0 -0
  28. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/boolean_argument_parsing.py +0 -0
  29. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/caching.py +0 -0
  30. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/circular_layout.py +0 -0
  31. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/color_annotations.py +0 -0
  32. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/discrete_models.py +0 -0
  33. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/files.py +0 -0
  34. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/geological_timescale.py +0 -0
  35. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/parallel.py +0 -0
  36. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/parsimony_utils.py +0 -0
  37. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/pgls_utils.py +0 -0
  38. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/plot_config.py +0 -0
  39. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/quartet_utils.py +0 -0
  40. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/stats_summary.py +0 -0
  41. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/streaming.py +0 -0
  42. {phykit-2.3.0 → phykit-2.4.0}/phykit/helpers/tree_paths.py +0 -0
  43. {phykit-2.3.0 → phykit-2.4.0}/phykit/service_factories.py +0 -0
  44. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/__init__.py +0 -0
  45. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/__init__.py +0 -0
  46. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/_fasta.py +0 -0
  47. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/alignment_entropy.py +0 -0
  48. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/alignment_length.py +0 -0
  49. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/alignment_length_no_gaps.py +0 -0
  50. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/alignment_outlier_taxa.py +0 -0
  51. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/alignment_recoding.py +0 -0
  52. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/base.py +0 -0
  53. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/column_score.py +0 -0
  54. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/composition_per_taxon.py +0 -0
  55. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/compositional_bias_per_site.py +0 -0
  56. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/dfoil.py +0 -0
  57. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/dstatistic.py +0 -0
  58. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/evolutionary_rate_per_site.py +0 -0
  59. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/faidx.py +0 -0
  60. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/gc_content.py +0 -0
  61. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/identity_matrix.py +0 -0
  62. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/mask_alignment.py +0 -0
  63. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/occupancy_filter.py +0 -0
  64. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/occupancy_per_taxon.py +0 -0
  65. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/pairwise_identity.py +0 -0
  66. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/parsimony_informative_sites.py +0 -0
  67. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/phylo_gwas.py +0 -0
  68. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/plot_alignment_qc.py +0 -0
  69. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/rcv.py +0 -0
  70. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/rcvt.py +0 -0
  71. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/rename_fasta_entries.py +0 -0
  72. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/sum_of_pairs_score.py +0 -0
  73. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/alignment/taxon_groups.py +0 -0
  74. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/base.py +0 -0
  75. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/__init__.py +0 -0
  76. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/ancestral_reconstruction.py +0 -0
  77. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/bipartition_support_stats.py +0 -0
  78. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/branch_length_multiplier.py +0 -0
  79. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/chronogram.py +0 -0
  80. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/collapse_branches.py +0 -0
  81. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/concordance_asr.py +0 -0
  82. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/consensus_network.py +0 -0
  83. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/consensus_tree.py +0 -0
  84. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/cont_map.py +0 -0
  85. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/cophylo.py +0 -0
  86. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/covarying_evolutionary_rates.py +0 -0
  87. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/density_map.py +0 -0
  88. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/discordance_asymmetry.py +0 -0
  89. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/dtt.py +0 -0
  90. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/dvmc.py +0 -0
  91. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/evo_tempo_map.py +0 -0
  92. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/evolutionary_rate.py +0 -0
  93. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/faiths_pd.py +0 -0
  94. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/fit_continuous.py +0 -0
  95. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/fit_discrete.py +0 -0
  96. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/hidden_paralogy_check.py +0 -0
  97. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/hybridization.py +0 -0
  98. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/independent_contrasts.py +0 -0
  99. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/internal_branch_stats.py +0 -0
  100. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/internode_labeler.py +0 -0
  101. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/kf_distance.py +0 -0
  102. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/last_common_ancestor_subtree.py +0 -0
  103. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/lb_score.py +0 -0
  104. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/ltt.py +0 -0
  105. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/monophyly_check.py +0 -0
  106. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/nearest_neighbor_interchange.py +0 -0
  107. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/neighbor_net.py +0 -0
  108. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/ou_shift_detection.py +0 -0
  109. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/ouwie.py +0 -0
  110. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/parsimony_score.py +0 -0
  111. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/patristic_distances.py +0 -0
  112. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phenogram.py +0 -0
  113. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylo_anova.py +0 -0
  114. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylo_heatmap.py +0 -0
  115. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylo_impute.py +0 -0
  116. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylo_logistic.py +0 -0
  117. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylo_path.py +0 -0
  118. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylogenetic_glm.py +0 -0
  119. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylogenetic_ordination.py +0 -0
  120. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylogenetic_regression.py +0 -0
  121. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylogenetic_signal.py +0 -0
  122. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/phylomorphospace.py +0 -0
  123. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/polytomy_test.py +0 -0
  124. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/print_tree.py +0 -0
  125. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/prune_tree.py +0 -0
  126. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/quartet_network.py +0 -0
  127. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/quartet_pie.py +0 -0
  128. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/rate_heterogeneity.py +0 -0
  129. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/relative_rate_test.py +0 -0
  130. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/rename_tree_tips.py +0 -0
  131. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/rf_distance.py +0 -0
  132. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/root_tree.py +0 -0
  133. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/saturation.py +0 -0
  134. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/simmap_summary.py +0 -0
  135. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/spectral_discordance.py +0 -0
  136. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/spr.py +0 -0
  137. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/stochastic_character_map.py +0 -0
  138. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/terminal_branch_stats.py +0 -0
  139. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/tip_labels.py +0 -0
  140. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/tip_to_tip_distance.py +0 -0
  141. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/tip_to_tip_node_distance.py +0 -0
  142. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/total_tree_length.py +0 -0
  143. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/trait_correlation.py +0 -0
  144. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/trait_rate_map.py +0 -0
  145. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/transfer_annotations.py +0 -0
  146. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/tree_space.py +0 -0
  147. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/treeness.py +0 -0
  148. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/treeness_over_rcv.py +0 -0
  149. {phykit-2.3.0 → phykit-2.4.0}/phykit/services/tree/vcv_utils.py +0 -0
  150. {phykit-2.3.0 → phykit-2.4.0}/phykit.egg-info/SOURCES.txt +0 -0
  151. {phykit-2.3.0 → phykit-2.4.0}/phykit.egg-info/dependency_links.txt +0 -0
  152. {phykit-2.3.0 → phykit-2.4.0}/phykit.egg-info/requires.txt +0 -0
  153. {phykit-2.3.0 → phykit-2.4.0}/phykit.egg-info/top_level.txt +0 -0
  154. {phykit-2.3.0 → phykit-2.4.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: phykit
3
- Version: 2.3.0
3
+ Version: 2.4.0
4
4
  Home-page: https://github.com/jlsteenwyk/phykit
5
5
  Author: Jacob L. Steenwyk
6
6
  Author-email: jlsteenwyk@gmail.com
@@ -1,4 +1,6 @@
1
- """Declarative CLI alias registry."""
1
+ """Declarative registry for PhyKIT commands and aliases."""
2
+
3
+ from dataclasses import dataclass
2
4
 
3
5
 
4
6
  # Map user-facing aliases (and a few long-form synonyms) to Phykit handler names.
@@ -240,3 +242,61 @@ ALIAS_TO_HANDLER: dict[str, str] = {
240
242
  "pal2nal": "thread_dna",
241
243
  "p2n": "thread_dna",
242
244
  }
245
+
246
+
247
+ # Most canonical command names are also handler names. These five retain their
248
+ # established descriptive command names while dispatching to legacy handlers.
249
+ _CANONICAL_NAME_BY_HANDLER = {
250
+ "dvmc": "degree_of_violation_of_a_molecular_clock",
251
+ "lb_score": "long_branch_score",
252
+ "rcv": "relative_composition_variability",
253
+ "rcvt": "relative_composition_variability_taxon",
254
+ "rf_distance": "robinson_foulds_distance",
255
+ }
256
+
257
+
258
+ @dataclass(frozen=True)
259
+ class CommandIdentity:
260
+ """Stable public identity for one command implementation."""
261
+
262
+ canonical: str
263
+ handler: str
264
+ aliases: tuple[str, ...]
265
+
266
+ @property
267
+ def entry_points(self) -> tuple[str, ...]:
268
+ """Return every standalone ``pk_*`` executable for the command."""
269
+
270
+ return tuple(f"pk_{name}" for name in (self.canonical, *self.aliases))
271
+
272
+
273
+ def _build_command_identities() -> tuple[CommandIdentity, ...]:
274
+ handlers = sorted(set(ALIAS_TO_HANDLER.values()))
275
+ identities = []
276
+ for handler in handlers:
277
+ canonical = _CANONICAL_NAME_BY_HANDLER.get(handler, handler)
278
+ public_names = {
279
+ name for name, mapped_handler in ALIAS_TO_HANDLER.items()
280
+ if mapped_handler == handler
281
+ }
282
+ public_names.add(handler)
283
+ public_names.discard(canonical)
284
+ identities.append(
285
+ CommandIdentity(
286
+ canonical=canonical,
287
+ handler=handler,
288
+ aliases=tuple(sorted(public_names)),
289
+ )
290
+ )
291
+ return tuple(identities)
292
+
293
+
294
+ COMMAND_IDENTITIES = _build_command_identities()
295
+
296
+ # Complete command surface used by packaging and documentation. Keep
297
+ # ALIAS_TO_HANDLER as the dispatch-only compatibility mapping.
298
+ PUBLIC_COMMAND_TO_HANDLER = {
299
+ name: identity.handler
300
+ for identity in COMMAND_IDENTITIES
301
+ for name in (identity.canonical, *identity.aliases)
302
+ }
@@ -7,3 +7,6 @@ class PhykitUserError(SystemExit):
7
7
  def __init__(self, messages, code: int = 2):
8
8
  self.messages: list[str] = list(messages)
9
9
  super().__init__(code)
10
+
11
+ def __reduce__(self):
12
+ return type(self), (self.messages, self.code)
@@ -1,10 +1,18 @@
1
+ from math import isfinite
2
+
3
+
1
4
  _JSON_SCALAR_TYPES = (str, int, float, bool)
2
5
  _LARGE_DICT_COPY_THRESHOLD = 50_000
3
6
  _JSON_DUMPS = None
4
7
 
5
8
 
6
9
  def to_builtin_json_types(value):
7
- if value is None or type(value) in _JSON_SCALAR_TYPES:
10
+ value_type = type(value)
11
+ if value is None:
12
+ return value
13
+ if value_type is float:
14
+ return value if isfinite(value) else None
15
+ if value_type in _JSON_SCALAR_TYPES:
8
16
  return value
9
17
  if isinstance(value, dict):
10
18
  converted = None
@@ -12,7 +20,12 @@ def to_builtin_json_types(value):
12
20
  type(value) is dict and len(value) >= _LARGE_DICT_COPY_THRESHOLD
13
21
  )
14
22
  for key, sub_value in value.items():
15
- if sub_value is None or type(sub_value) in _JSON_SCALAR_TYPES:
23
+ sub_value_type = type(sub_value)
24
+ if sub_value_type is float:
25
+ converted_value = (
26
+ sub_value if isfinite(sub_value) else None
27
+ )
28
+ elif sub_value is None or sub_value_type in _JSON_SCALAR_TYPES:
16
29
  converted_value = sub_value
17
30
  else:
18
31
  converted_value = to_builtin_json_types(sub_value)
@@ -32,7 +45,12 @@ def to_builtin_json_types(value):
32
45
  if isinstance(value, list):
33
46
  converted = None
34
47
  for index, sub_value in enumerate(value):
35
- if sub_value is None or type(sub_value) in _JSON_SCALAR_TYPES:
48
+ sub_value_type = type(sub_value)
49
+ if sub_value_type is float:
50
+ converted_value = (
51
+ sub_value if isfinite(sub_value) else None
52
+ )
53
+ elif sub_value is None or sub_value_type in _JSON_SCALAR_TYPES:
36
54
  converted_value = sub_value
37
55
  else:
38
56
  converted_value = to_builtin_json_types(sub_value)
@@ -45,7 +63,12 @@ def to_builtin_json_types(value):
45
63
  if isinstance(value, tuple):
46
64
  converted = None
47
65
  for index, sub_value in enumerate(value):
48
- if sub_value is None or type(sub_value) in _JSON_SCALAR_TYPES:
66
+ sub_value_type = type(sub_value)
67
+ if sub_value_type is float:
68
+ converted_value = (
69
+ sub_value if isfinite(sub_value) else None
70
+ )
71
+ elif sub_value is None or sub_value_type in _JSON_SCALAR_TYPES:
49
72
  converted_value = sub_value
50
73
  else:
51
74
  converted_value = to_builtin_json_types(sub_value)
@@ -61,7 +84,7 @@ def to_builtin_json_types(value):
61
84
  if hasattr(value, "tolist"):
62
85
  return to_builtin_json_types(value.tolist())
63
86
  if hasattr(value, "item"):
64
- return value.item()
87
+ return to_builtin_json_types(value.item())
65
88
  if hasattr(value, "tolist") and value.__class__.__name__ == "ndarray":
66
89
  return [to_builtin_json_types(sub_value) for sub_value in value.tolist()]
67
90
  return value
@@ -91,11 +114,22 @@ def print_json(payload, sort_keys=True):
91
114
  _JSON_DUMPS = dumps
92
115
 
93
116
  try:
94
- output = dumps(
95
- payload,
96
- sort_keys=sort_keys,
97
- default=_json_default,
98
- )
117
+ try:
118
+ output = dumps(
119
+ payload,
120
+ sort_keys=sort_keys,
121
+ default=_json_default,
122
+ allow_nan=False,
123
+ )
124
+ except ValueError as error:
125
+ if "Out of range float values" not in str(error):
126
+ raise
127
+ output = dumps(
128
+ to_builtin_json_types(payload),
129
+ sort_keys=sort_keys,
130
+ default=_json_default,
131
+ allow_nan=False,
132
+ )
99
133
  print(output)
100
134
  except BrokenPipeError:
101
135
  pass
@@ -5,11 +5,16 @@ Provides a single implementation for parsing tab-delimited multi-trait
5
5
  files with a header row, used across phylogenetic regression, signal,
6
6
  ordination, path analysis, ANOVA, and other comparative methods.
7
7
  """
8
+ from math import isfinite
8
9
  import sys
9
10
 
10
11
  from ..errors import PhykitUserError
11
12
 
12
13
 
14
+ def _split_tsv_line(line: str) -> list[str]:
15
+ return [part.strip() for part in line.rstrip("\r\n").split("\t")]
16
+
17
+
13
18
  def _parse_float_values_detailed(
14
19
  value_parts: list[str],
15
20
  trait_names: list[str],
@@ -61,7 +66,7 @@ def parse_multi_trait_file(
61
66
  stripped = line.strip()
62
67
  if not stripped or stripped[0] == "#":
63
68
  continue
64
- header_parts = stripped.split("\t")
69
+ header_parts = _split_tsv_line(line)
65
70
  break
66
71
 
67
72
  if header_parts is None:
@@ -81,8 +86,27 @@ def parse_multi_trait_file(
81
86
  )
82
87
  trait_names = header_parts[1:]
83
88
  trait_count = len(trait_names)
89
+ trait_columns = {}
90
+ for column, trait_name in enumerate(trait_names, start=2):
91
+ if not trait_name:
92
+ raise PhykitUserError(
93
+ [
94
+ f"Trait header is blank in column {column} on line 1."
95
+ ],
96
+ code=2,
97
+ )
98
+ if trait_name in trait_columns:
99
+ raise PhykitUserError(
100
+ [
101
+ f"Duplicate trait header '{trait_name}' in columns "
102
+ f"{trait_columns[trait_name]} and {column} on line 1."
103
+ ],
104
+ code=2,
105
+ )
106
+ trait_columns[trait_name] = column
84
107
 
85
108
  traits = {}
109
+ taxon_lines = {}
86
110
  saw_data = False
87
111
  data_line_idx = 2
88
112
  to_float = float
@@ -91,7 +115,7 @@ def parse_multi_trait_file(
91
115
  if not stripped or stripped[0] == "#":
92
116
  continue
93
117
  saw_data = True
94
- parts = stripped.split("\t")
118
+ parts = _split_tsv_line(line)
95
119
  if len(parts) != n_cols:
96
120
  raise PhykitUserError(
97
121
  [
@@ -102,6 +126,19 @@ def parse_multi_trait_file(
102
126
  code=2,
103
127
  )
104
128
  taxon = parts[0]
129
+ if not taxon:
130
+ raise PhykitUserError(
131
+ [f"Taxon name is blank on line {data_line_idx}."],
132
+ code=2,
133
+ )
134
+ if taxon in taxon_lines:
135
+ raise PhykitUserError(
136
+ [
137
+ f"Duplicate taxon '{taxon}' on line {data_line_idx}; "
138
+ f"first seen on line {taxon_lines[taxon]}."
139
+ ],
140
+ code=2,
141
+ )
105
142
  try:
106
143
  if trait_count == 1:
107
144
  values = [to_float(parts[1])]
@@ -129,7 +166,20 @@ def parse_multi_trait_file(
129
166
  taxon,
130
167
  data_line_idx,
131
168
  )
169
+ for trait_name, raw_value, value in zip(
170
+ trait_names, parts[1:], values
171
+ ):
172
+ if not isfinite(value):
173
+ raise PhykitUserError(
174
+ [
175
+ f"Non-finite trait value '{raw_value}' for taxon "
176
+ f"'{taxon}' (trait '{trait_name}') on line "
177
+ f"{data_line_idx}."
178
+ ],
179
+ code=2,
180
+ )
132
181
  traits[taxon] = values
182
+ taxon_lines[taxon] = data_line_idx
133
183
  data_line_idx += 1
134
184
 
135
185
  if not saw_data:
@@ -3211,7 +3211,8 @@ class Phykit:
3211
3211
  )
3212
3212
  parser.add_argument(
3213
3213
  "--optimization", type=str, default="acctran",
3214
- required=False, help=SUPPRESS, metavar=""
3214
+ choices=("acctran", "deltran"), required=False,
3215
+ help=SUPPRESS, metavar=""
3215
3216
  )
3216
3217
  parser.add_argument(
3217
3218
  "--phylogram", action="store_true", required=False, help=SUPPRESS
@@ -6973,9 +6974,9 @@ class Phykit:
6973
6974
  (Default: majority)
6974
6975
 
6975
6976
  --missing-taxa how to handle mismatched
6976
- taxa across trees:
6977
- error or shared
6978
- (Default: error)
6977
+ taxa across trees: allow,
6978
+ error, or shared
6979
+ (Default: allow)
6979
6980
 
6980
6981
  --json optional argument to output
6981
6982
  results as JSON
@@ -8532,21 +8533,36 @@ class Phykit:
8532
8533
  f"""\
8533
8534
  {help_header}
8534
8535
 
8535
- Determines potentially spurious homologs using branch lengths.
8536
+ Determines potentially spurious homologs using branch lengths
8537
+ or topology-aware tree-diameter impact.
8536
8538
 
8537
8539
  Identifies potentially spurious sequences and reports
8538
8540
  tips in the phylogeny that could possibly be removed
8539
8541
  from the associated multiple sequence alignment. PhyKIT
8540
- does so by identifying and reporting long terminal branches
8541
- defined as branches that are equal to or 20 times the median
8542
- length of all branches.
8543
-
8544
- PhyKIT reports the following information
8542
+ By default, PhyKIT identifies and reports long terminal
8543
+ branches defined as branches that are at least 20 times the
8544
+ median terminal-branch length. The diameter-impact method
8545
+ instead searches tree-diameter endpoints, measures the
8546
+ reduction caused by removing each selected endpoint, and
8547
+ reports statistically extreme log reductions.
8548
+
8549
+ Median-factor output reports the following information
8545
8550
  col1: name of tip that is a putatively spurious sequence
8546
8551
  col2: length of branch leading to putatively spurious sequence
8547
8552
  col3: threshold used to identify putatively spurious sequences
8548
8553
  col4: median branch length in the phylogeny
8549
8554
 
8555
+ Diameter-impact output for one tree reports
8556
+ col1: name of tip that is a putatively spurious sequence
8557
+ col2: log reduction in tree diameter
8558
+ col3: upper-tail p-value
8559
+ col4: removal rank
8560
+ col5: terminal branch length
8561
+ col6: tree diameter before removal
8562
+ col7: tree diameter after removal
8563
+
8564
+ With --tree-list, the tree path is prepended as column 1.
8565
+
8550
8566
  If there are no putatively spurious sequences, "None" is reported.
8551
8567
 
8552
8568
  Using this method to identify potentially spurious sequences
@@ -8560,6 +8576,10 @@ class Phykit:
8560
8576
 
8561
8577
  Usage:
8562
8578
  phykit spurious_sequence <file> [-f 20] [--json]
8579
+ phykit spurious_sequence <file> --method diameter-impact
8580
+ [--alpha 0.05] [--max-remove <count>] [--json]
8581
+ phykit spurious_sequence <tree_list> --method diameter-impact
8582
+ --tree-list [--per-species] [--alpha 0.05] [--json]
8563
8583
 
8564
8584
  Options
8565
8585
  =====================================================
@@ -8572,6 +8592,25 @@ class Phykit:
8572
8592
  the threshold of long branches.
8573
8593
  (Default: 20)
8574
8594
 
8595
+ --method detection method: median-factor
8596
+ or diameter-impact.
8597
+ (Default: median-factor)
8598
+
8599
+ --alpha upper-tail significance level for
8600
+ diameter-impact detection.
8601
+ (Default: 0.05)
8602
+
8603
+ --max-remove maximum number of diameter
8604
+ endpoints to evaluate. By default,
8605
+ uses min(n/4, 5*sqrt(n)).
8606
+
8607
+ --tree-list interpret <file> as a line-delimited
8608
+ list of tree paths and calibrate
8609
+ signatures across all trees.
8610
+
8611
+ --per-species with --tree-list, calibrate each
8612
+ taxon's signatures separately.
8613
+
8575
8614
  --json optional argument to output
8576
8615
  results as JSON
8577
8616
  """
@@ -8579,6 +8618,16 @@ class Phykit:
8579
8618
  )
8580
8619
  parser.add_argument("tree", type=str, help=SUPPRESS)
8581
8620
  parser.add_argument("-f", "--factor", type=float, required=False, help=SUPPRESS)
8621
+ parser.add_argument(
8622
+ "--method",
8623
+ choices=("median-factor", "diameter-impact"),
8624
+ default="median-factor",
8625
+ help=SUPPRESS,
8626
+ )
8627
+ parser.add_argument("--alpha", type=float, default=0.05, help=SUPPRESS)
8628
+ parser.add_argument("--max-remove", type=int, default=None, help=SUPPRESS)
8629
+ parser.add_argument("--tree-list", action="store_true", help=SUPPRESS)
8630
+ parser.add_argument("--per-species", action="store_true", help=SUPPRESS)
8582
8631
  _add_json_argument(parser)
8583
8632
  _run_service(parser, argv, SpuriousSequence)
8584
8633
 
@@ -10046,7 +10095,15 @@ class Phykit:
10046
10095
  """
10047
10096
  ),
10048
10097
  )
10049
- parser.add_argument("-a", "--alignment_list", type=str, help=SUPPRESS)
10098
+ parser.add_argument(
10099
+ "-a",
10100
+ "--alignment",
10101
+ "--alignment-list",
10102
+ "--alignment_list",
10103
+ dest="alignment_list",
10104
+ type=str,
10105
+ help=SUPPRESS,
10106
+ )
10050
10107
  parser.add_argument("-p", "--prefix", type=str, help=SUPPRESS)
10051
10108
  parser.add_argument("--threshold", type=float, required=False, default=0, help=SUPPRESS)
10052
10109
  parser.add_argument("--plot-occupancy", action="store_true", required=False, help=SUPPRESS)
@@ -10087,7 +10144,7 @@ class Phykit:
10087
10144
  pk_thread_dna, pk_pal2nal, pk_p2n
10088
10145
 
10089
10146
  Usage:
10090
- phykit thread_dna -p <file> -n <file> [-c/--clipkit_log_file
10147
+ phykit thread_dna -p <file> -n <file> [-c/--clipkit-log-file
10091
10148
  <clipkit outputted log file> -s] [--json]
10092
10149
 
10093
10150
  Options
@@ -10096,7 +10153,7 @@ class Phykit:
10096
10153
 
10097
10154
  -n/--nucleotide nucleotide sequence file
10098
10155
 
10099
- -c/--clipkit_log clipkit outputted log file
10156
+ -c/--clipkit-log-file ClipKIT output log file
10100
10157
 
10101
10158
  -s/--stop boolean for whether or not
10102
10159
  stop codons should be kept.
@@ -10112,6 +10169,7 @@ class Phykit:
10112
10169
  parser.add_argument("-n", "--nucleotide", type=str, help=SUPPRESS)
10113
10170
  parser.add_argument(
10114
10171
  "-c",
10172
+ "--clipkit-log-file",
10115
10173
  "--clipkit_log_file",
10116
10174
  type=str,
10117
10175
  required=False,
@@ -10146,6 +10204,10 @@ def alignment_entropy(argv=None):
10146
10204
  Phykit.alignment_entropy(sys.argv[1:])
10147
10205
 
10148
10206
 
10207
+ def alignment_recoding(argv=None):
10208
+ Phykit.alignment_recoding(sys.argv[1:])
10209
+
10210
+
10149
10211
  def alignment_outlier_taxa(argv=None):
10150
10212
  Phykit.alignment_outlier_taxa(sys.argv[1:])
10151
10213
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import re
6
+
5
7
  from ._fasta import read_fasta_first_token
6
8
  from .base import Alignment
7
9
  from ...errors import PhykitUserError
@@ -19,6 +21,9 @@ _RANGE_SITE_SAMPLE_MIN_LENGTH = 1_000_000
19
21
  _RANGE_SITE_SAMPLE_MID_MIN_LENGTH = 100_000
20
22
  _RANGE_SITE_SAMPLE_MAX_COUNT = 50_000
21
23
  _RANGE_SITE_SAMPLE_MAX_FRACTION = 0.1
24
+ _PARTITION_LINE_PATTERN = re.compile(
25
+ r"\s*(\S+)\s*,\s*(\S+)\s*=\s*(\d+)\s*-\s*(\d+)\s*"
26
+ )
22
27
 
23
28
 
24
29
  class AlignmentSubsample(Alignment):
@@ -112,6 +117,8 @@ class AlignmentSubsample(Alignment):
112
117
 
113
118
  partitions = self._parse_partition_file(self.partition_path)
114
119
  sequences = self._read_alignment(self.alignment_path)
120
+ alignment_length = self._alignment_length(sequences)
121
+ self._validate_partition_bounds(partitions, alignment_length)
115
122
  total = len(partitions)
116
123
  n = self._compute_n(total, "partitions")
117
124
  selected = self._sample(rng, partitions, n)
@@ -139,20 +146,7 @@ class AlignmentSubsample(Alignment):
139
146
  )
140
147
 
141
148
  sequences = self._read_alignment(self.alignment_path)
142
- seq_iter = iter(sequences.values())
143
- try:
144
- aln_len = len(next(seq_iter))
145
- except StopIteration:
146
- raise PhykitUserError(
147
- ["All sequences in the alignment must have the same length."],
148
- code=2,
149
- )
150
- for seq in seq_iter:
151
- if len(seq) != aln_len:
152
- raise PhykitUserError(
153
- ["All sequences in the alignment must have the same length."],
154
- code=2,
155
- )
149
+ aln_len = self._alignment_length(sequences)
156
150
  n = self._compute_n(aln_len, "sites")
157
151
 
158
152
  if not self.bootstrap and n == aln_len:
@@ -202,6 +196,24 @@ class AlignmentSubsample(Alignment):
202
196
  def _read_alignment(path: str) -> dict[str, str]:
203
197
  return read_fasta_first_token(path)
204
198
 
199
+ @staticmethod
200
+ def _alignment_length(sequences: dict[str, str]) -> int:
201
+ seq_iter = iter(sequences.values())
202
+ try:
203
+ alignment_length = len(next(seq_iter))
204
+ except StopIteration:
205
+ raise PhykitUserError(
206
+ ["All sequences in the alignment must have the same length."],
207
+ code=2,
208
+ )
209
+ for sequence in seq_iter:
210
+ if len(sequence) != alignment_length:
211
+ raise PhykitUserError(
212
+ ["All sequences in the alignment must have the same length."],
213
+ code=2,
214
+ )
215
+ return alignment_length
216
+
205
217
  @staticmethod
206
218
  def _parse_partition_file(path: str) -> list[tuple[str, int, int]]:
207
219
  """Parse RAxML-style partition file.
@@ -210,68 +222,61 @@ class AlignmentSubsample(Alignment):
210
222
  """
211
223
  partitions: list[tuple[str, int, int]] = []
212
224
  with open(path) as fh:
213
- for line in fh:
225
+ for line_number, line in enumerate(fh, start=1):
214
226
  stripped = line.strip()
215
227
  if not stripped or stripped[0] == "#":
216
228
  continue
217
229
  partition = AlignmentSubsample._parse_partition_line(stripped)
218
- if partition is not None:
219
- partitions.append(partition)
230
+ if partition is None:
231
+ raise PhykitUserError(
232
+ [
233
+ f"Malformed partition row on line {line_number}: "
234
+ f"'{stripped}'.",
235
+ "Expected: MODEL, name=start-end",
236
+ ],
237
+ code=2,
238
+ )
239
+ partitions.append(partition)
220
240
  return partitions
221
241
 
222
242
  @staticmethod
223
243
  def _parse_partition_line(line: str) -> tuple[str, int, int] | None:
224
- try:
225
- model, rest = line.split(", ", 1)
226
- name, range_part = rest.split("=", 1)
227
- start_text, end_text = range_part.split("-", 1)
228
- if (
229
- model
230
- and " " not in model
231
- and name
232
- and " " not in name
233
- and start_text.isdigit()
234
- ):
235
- if end_text.isdigit():
236
- return name, int(start_text), int(end_text)
237
- end_digits = AlignmentSubsample._leading_digits(end_text)
238
- if end_digits.isdigit():
239
- return name, int(start_text), int(end_digits)
240
- except (IndexError, ValueError):
241
- pass
242
-
243
- try:
244
- model, rest = line.split(",", 1)
245
- name_part, range_part = rest.lstrip().split("=", 1)
246
- start_part, end_part = range_part.lstrip().split("-", 1)
247
- except ValueError:
244
+ match = _PARTITION_LINE_PATTERN.fullmatch(line)
245
+ if match is None:
248
246
  return None
249
-
250
- if len(model.split()) != 1:
251
- return None
252
-
253
- name = name_part.rstrip()
254
- if not name or len(name.split()) != 1:
255
- return None
256
-
257
- start_text = start_part.strip()
258
- if not start_text.isdigit():
259
- return None
260
-
261
- end_text = end_part.lstrip()
262
- end_digits = AlignmentSubsample._leading_digits(end_text)
263
- if not end_digits.isdigit():
264
- return None
265
-
266
- return name, int(start_text), int(end_digits)
247
+ _model, name, start_text, end_text = match.groups()
248
+ return name, int(start_text), int(end_text)
267
249
 
268
250
  @staticmethod
269
- def _leading_digits(text: str) -> str:
270
- idx = 0
271
- text_len = len(text)
272
- while idx < text_len and text[idx].isdigit():
273
- idx += 1
274
- return text[:idx]
251
+ def _validate_partition_bounds(
252
+ partitions: list[tuple[str, int, int]],
253
+ alignment_length: int,
254
+ ) -> None:
255
+ for name, start, end in partitions:
256
+ if start < 1:
257
+ raise PhykitUserError(
258
+ [
259
+ f"Partition '{name}' starts at {start}; "
260
+ "coordinates must be >= 1."
261
+ ],
262
+ code=2,
263
+ )
264
+ if end < start:
265
+ raise PhykitUserError(
266
+ [
267
+ f"Partition '{name}' ends at {end} before its start "
268
+ f"at {start}."
269
+ ],
270
+ code=2,
271
+ )
272
+ if end > alignment_length:
273
+ raise PhykitUserError(
274
+ [
275
+ f"Partition '{name}' ends at {end}, beyond alignment "
276
+ f"length {alignment_length}."
277
+ ],
278
+ code=2,
279
+ )
275
280
 
276
281
  def _compute_n(self, total: int, label: str) -> int:
277
282
  if self.number is not None: