phykit 2.2.1__tar.gz → 2.3.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 (153) hide show
  1. {phykit-2.2.1 → phykit-2.3.0}/PKG-INFO +1 -1
  2. {phykit-2.2.1 → phykit-2.3.0}/phykit/cli_registry.py +4 -0
  3. {phykit-2.2.1 → phykit-2.3.0}/phykit/phykit.py +112 -0
  4. {phykit-2.2.1 → phykit-2.3.0}/phykit/service_factories.py +1 -0
  5. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/__init__.py +1 -0
  6. phykit-2.3.0/phykit/services/alignment/codon_dnds.py +400 -0
  7. phykit-2.3.0/phykit/version.py +1 -0
  8. {phykit-2.2.1 → phykit-2.3.0}/phykit.egg-info/PKG-INFO +1 -1
  9. {phykit-2.2.1 → phykit-2.3.0}/phykit.egg-info/SOURCES.txt +1 -0
  10. {phykit-2.2.1 → phykit-2.3.0}/phykit.egg-info/entry_points.txt +4 -0
  11. phykit-2.2.1/phykit/version.py +0 -1
  12. {phykit-2.2.1 → phykit-2.3.0}/LICENSE.md +0 -0
  13. {phykit-2.2.1 → phykit-2.3.0}/README.md +0 -0
  14. {phykit-2.2.1 → phykit-2.3.0}/phykit/__init__.py +0 -0
  15. {phykit-2.2.1 → phykit-2.3.0}/phykit/__main__.py +0 -0
  16. {phykit-2.2.1 → phykit-2.3.0}/phykit/errors.py +0 -0
  17. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/__init__.py +0 -0
  18. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/boolean_argument_parsing.py +0 -0
  19. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/caching.py +0 -0
  20. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/circular_layout.py +0 -0
  21. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/color_annotations.py +0 -0
  22. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/discrete_models.py +0 -0
  23. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/files.py +0 -0
  24. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/geological_timescale.py +0 -0
  25. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/json_output.py +0 -0
  26. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/parallel.py +0 -0
  27. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/parsimony_utils.py +0 -0
  28. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/pgls_utils.py +0 -0
  29. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/plot_config.py +0 -0
  30. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/quartet_utils.py +0 -0
  31. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/stats_summary.py +0 -0
  32. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/streaming.py +0 -0
  33. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/trait_parsing.py +0 -0
  34. {phykit-2.2.1 → phykit-2.3.0}/phykit/helpers/tree_paths.py +0 -0
  35. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/__init__.py +0 -0
  36. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/_fasta.py +0 -0
  37. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/alignment_entropy.py +0 -0
  38. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/alignment_length.py +0 -0
  39. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/alignment_length_no_gaps.py +0 -0
  40. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/alignment_outlier_taxa.py +0 -0
  41. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/alignment_recoding.py +0 -0
  42. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/alignment_subsample.py +0 -0
  43. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/base.py +0 -0
  44. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/column_score.py +0 -0
  45. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/composition_per_taxon.py +0 -0
  46. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/compositional_bias_per_site.py +0 -0
  47. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/create_concatenation_matrix.py +0 -0
  48. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/dfoil.py +0 -0
  49. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/dna_threader.py +0 -0
  50. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/dstatistic.py +0 -0
  51. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/evolutionary_rate_per_site.py +0 -0
  52. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/faidx.py +0 -0
  53. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/gc_content.py +0 -0
  54. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/identity_matrix.py +0 -0
  55. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/mask_alignment.py +0 -0
  56. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/occupancy_filter.py +0 -0
  57. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/occupancy_per_taxon.py +0 -0
  58. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/pairwise_identity.py +0 -0
  59. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/parsimony_informative_sites.py +0 -0
  60. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/phylo_gwas.py +0 -0
  61. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/plot_alignment_qc.py +0 -0
  62. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/rcv.py +0 -0
  63. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/rcvt.py +0 -0
  64. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/rename_fasta_entries.py +0 -0
  65. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/sum_of_pairs_score.py +0 -0
  66. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/taxon_groups.py +0 -0
  67. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/alignment/variable_sites.py +0 -0
  68. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/base.py +0 -0
  69. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/__init__.py +0 -0
  70. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/ancestral_reconstruction.py +0 -0
  71. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/base.py +0 -0
  72. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/bipartition_support_stats.py +0 -0
  73. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/branch_length_multiplier.py +0 -0
  74. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/character_map.py +0 -0
  75. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/chronogram.py +0 -0
  76. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/collapse_branches.py +0 -0
  77. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/concordance_asr.py +0 -0
  78. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/consensus_network.py +0 -0
  79. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/consensus_tree.py +0 -0
  80. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/cont_map.py +0 -0
  81. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/cophylo.py +0 -0
  82. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/covarying_evolutionary_rates.py +0 -0
  83. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/density_map.py +0 -0
  84. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/discordance_asymmetry.py +0 -0
  85. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/dtt.py +0 -0
  86. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/dvmc.py +0 -0
  87. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/evo_tempo_map.py +0 -0
  88. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/evolutionary_rate.py +0 -0
  89. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/faiths_pd.py +0 -0
  90. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/fit_continuous.py +0 -0
  91. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/fit_discrete.py +0 -0
  92. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/hidden_paralogy_check.py +0 -0
  93. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/hybridization.py +0 -0
  94. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/independent_contrasts.py +0 -0
  95. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/internal_branch_stats.py +0 -0
  96. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/internode_labeler.py +0 -0
  97. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/kf_distance.py +0 -0
  98. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/last_common_ancestor_subtree.py +0 -0
  99. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/lb_score.py +0 -0
  100. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/ltt.py +0 -0
  101. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/monophyly_check.py +0 -0
  102. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/nearest_neighbor_interchange.py +0 -0
  103. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/neighbor_net.py +0 -0
  104. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/network_signal.py +0 -0
  105. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/ou_shift_detection.py +0 -0
  106. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/ouwie.py +0 -0
  107. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/parsimony_score.py +0 -0
  108. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/patristic_distances.py +0 -0
  109. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phenogram.py +0 -0
  110. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylo_anova.py +0 -0
  111. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylo_heatmap.py +0 -0
  112. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylo_impute.py +0 -0
  113. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylo_logistic.py +0 -0
  114. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylo_path.py +0 -0
  115. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylogenetic_glm.py +0 -0
  116. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylogenetic_ordination.py +0 -0
  117. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylogenetic_regression.py +0 -0
  118. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylogenetic_signal.py +0 -0
  119. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/phylomorphospace.py +0 -0
  120. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/polytomy_test.py +0 -0
  121. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/print_tree.py +0 -0
  122. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/prune_tree.py +0 -0
  123. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/quartet_network.py +0 -0
  124. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/quartet_pie.py +0 -0
  125. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/rate_heterogeneity.py +0 -0
  126. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/relative_rate_test.py +0 -0
  127. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/rename_tree_tips.py +0 -0
  128. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/rf_distance.py +0 -0
  129. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/root_tree.py +0 -0
  130. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/saturation.py +0 -0
  131. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/simmap_summary.py +0 -0
  132. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/spectral_discordance.py +0 -0
  133. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/spr.py +0 -0
  134. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/spurious_sequence.py +0 -0
  135. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/stochastic_character_map.py +0 -0
  136. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/terminal_branch_stats.py +0 -0
  137. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/threshold_model.py +0 -0
  138. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/tip_labels.py +0 -0
  139. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/tip_to_tip_distance.py +0 -0
  140. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/tip_to_tip_node_distance.py +0 -0
  141. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/total_tree_length.py +0 -0
  142. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/trait_correlation.py +0 -0
  143. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/trait_rate_map.py +0 -0
  144. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/transfer_annotations.py +0 -0
  145. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/tree_space.py +0 -0
  146. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/treeness.py +0 -0
  147. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/treeness_over_rcv.py +0 -0
  148. {phykit-2.2.1 → phykit-2.3.0}/phykit/services/tree/vcv_utils.py +0 -0
  149. {phykit-2.2.1 → phykit-2.3.0}/phykit.egg-info/dependency_links.txt +0 -0
  150. {phykit-2.2.1 → phykit-2.3.0}/phykit.egg-info/requires.txt +0 -0
  151. {phykit-2.2.1 → phykit-2.3.0}/phykit.egg-info/top_level.txt +0 -0
  152. {phykit-2.2.1 → phykit-2.3.0}/setup.cfg +0 -0
  153. {phykit-2.2.1 → phykit-2.3.0}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: phykit
3
- Version: 2.2.1
3
+ Version: 2.3.0
4
4
  Home-page: https://github.com/jlsteenwyk/phykit
5
5
  Author: Jacob L. Steenwyk
6
6
  Author-email: jlsteenwyk@gmail.com
@@ -46,6 +46,10 @@ ALIAS_TO_HANDLER: dict[str, str] = {
46
46
  "seqid": "identity_matrix",
47
47
  "comp_taxon": "composition_per_taxon",
48
48
  "comp_tax": "composition_per_taxon",
49
+ "codon_dnds": "codon_dnds",
50
+ "dnds": "codon_dnds",
51
+ "dn_ds": "codon_dnds",
52
+ "kaks": "codon_dnds",
49
53
  "pis": "parsimony_informative_sites",
50
54
  "rel_comp_var": "rcv",
51
55
  "relative_composition_variability": "rcv",
@@ -546,6 +546,9 @@ class Phykit:
546
546
  - detects site-wise compositional biases in an alignment
547
547
  composition_per_taxon (alias: comp_taxon; comp_tax)
548
548
  - calculates sequence composition per taxon
549
+ codon_dnds (alias: dnds; dn_ds; kaks)
550
+ - estimate pairwise nonsynonymous and synonymous
551
+ substitution rates from a codon alignment
549
552
  create_concatenation_matrix (alias: create_concat; cc)
550
553
  - create concatenation matrix from a set of alignments
551
554
  evolutionary_rate_per_site (alias: evo_rate_per_site; erps)
@@ -1994,6 +1997,111 @@ class Phykit:
1994
1997
  _add_json_argument(parser)
1995
1998
  _run_service(parser, argv, PairwiseIdentity)
1996
1999
 
2000
+ @staticmethod
2001
+ def codon_dnds(argv):
2002
+ parser = _new_parser(
2003
+ description=_dedent(
2004
+ f"""\
2005
+ {help_header}
2006
+
2007
+ Estimate pairwise nonsynonymous substitutions per
2008
+ nonsynonymous site (dN), synonymous substitutions per
2009
+ synonymous site (dS), and omega (dN/dS) from an
2010
+ in-frame codon alignment.
2011
+
2012
+ Available estimators are Nei-Gojobori 1986 (NG86),
2013
+ Li-Wu-Luo 1985 (LWL85), Yang-Nielsen 2000 (YN00),
2014
+ and Goldman-Yang 1994 maximum likelihood (ML). NG86
2015
+ is the default. ML can be substantially slower for
2016
+ alignments containing many taxa.
2017
+
2018
+ Alignment length must be divisible by three. Gaps
2019
+ must occupy complete codons (---). Codons containing
2020
+ gaps or ambiguous bases are excluded independently
2021
+ for each sequence pair. Terminal stop codons are
2022
+ excluded. Internal stops fail by default.
2023
+
2024
+ Summary statistics are printed by default. Use
2025
+ --verbose to print one TSV row per pair. Omega is a
2026
+ descriptive pairwise estimate; this command does not
2027
+ perform a branch- or site-selection significance test.
2028
+
2029
+ Aliases:
2030
+ codon_dnds, dnds, dn_ds, kaks
2031
+ Command line interfaces:
2032
+ pk_codon_dnds, pk_dnds, pk_dn_ds, pk_kaks
2033
+
2034
+ Usage:
2035
+ phykit codon_dnds <alignment>
2036
+ [--method NG86|LWL85|YN00|ML]
2037
+ [--genetic-code <int>] [--kappa <float>]
2038
+ [--codon-frequency F1x4|F3x4|F61]
2039
+ [--stop-policy error|skip] [--reference <taxon>]
2040
+ [-v/--verbose] [--json]
2041
+
2042
+ Options
2043
+ =====================================================
2044
+ <alignment> in-frame nucleotide
2045
+ alignment
2046
+
2047
+ --method dN/dS estimator
2048
+ (default: NG86)
2049
+
2050
+ --genetic-code NCBI genetic-code ID
2051
+ (default: 1)
2052
+
2053
+ --kappa transition/transversion
2054
+ ratio used by NG86
2055
+ (default: 1.0)
2056
+
2057
+ --codon-frequency codon-frequency model used
2058
+ by ML (default: F3x4)
2059
+
2060
+ --stop-policy fail on internal stops or
2061
+ exclude them pairwise
2062
+ (default: error)
2063
+
2064
+ --reference estimate only pairs that
2065
+ include this exact taxon ID
2066
+
2067
+ -v/--verbose print pairwise TSV results
2068
+
2069
+ --json output structured JSON
2070
+ """
2071
+ ),
2072
+ )
2073
+ parser.add_argument("alignment", type=str, help=SUPPRESS)
2074
+ parser.add_argument(
2075
+ "--method",
2076
+ type=str.upper,
2077
+ choices=("NG86", "LWL85", "YN00", "ML"),
2078
+ default="NG86",
2079
+ help=SUPPRESS,
2080
+ )
2081
+ parser.add_argument(
2082
+ "--genetic-code", type=int, default=1, help=SUPPRESS
2083
+ )
2084
+ parser.add_argument("--kappa", type=float, default=1.0, help=SUPPRESS)
2085
+ parser.add_argument(
2086
+ "--codon-frequency",
2087
+ type=str.upper,
2088
+ choices=("F1X4", "F3X4", "F61"),
2089
+ default="F3X4",
2090
+ help=SUPPRESS,
2091
+ )
2092
+ parser.add_argument(
2093
+ "--stop-policy",
2094
+ choices=("error", "skip"),
2095
+ default="error",
2096
+ help=SUPPRESS,
2097
+ )
2098
+ parser.add_argument("--reference", type=str, default=None, help=SUPPRESS)
2099
+ parser.add_argument(
2100
+ "-v", "--verbose", action="store_true", required=False, help=SUPPRESS
2101
+ )
2102
+ _add_json_argument(parser)
2103
+ _run_service(parser, argv, CodonDnDs)
2104
+
1997
2105
  @staticmethod
1998
2106
  def identity_matrix(argv):
1999
2107
  parser = _new_parser(
@@ -10082,6 +10190,10 @@ def pairwise_identity(argv=None):
10082
10190
  Phykit.pairwise_identity(sys.argv[1:])
10083
10191
 
10084
10192
 
10193
+ def codon_dnds(argv=None):
10194
+ Phykit.codon_dnds(sys.argv[1:])
10195
+
10196
+
10085
10197
  def identity_matrix(argv=None):
10086
10198
  Phykit.identity_matrix(sys.argv[1:])
10087
10199
 
@@ -26,6 +26,7 @@ AlignmentOutlierTaxa = _LazyServiceFactory("phykit.services.alignment.alignment_
26
26
  ColumnScore = _LazyServiceFactory("phykit.services.alignment.column_score", "ColumnScore")
27
27
  CompositionalBiasPerSite = _LazyServiceFactory("phykit.services.alignment.compositional_bias_per_site", "CompositionalBiasPerSite")
28
28
  CompositionPerTaxon = _LazyServiceFactory("phykit.services.alignment.composition_per_taxon", "CompositionPerTaxon")
29
+ CodonDnDs = _LazyServiceFactory("phykit.services.alignment.codon_dnds", "CodonDnDs")
29
30
  CreateConcatenationMatrix = _LazyServiceFactory("phykit.services.alignment.create_concatenation_matrix", "CreateConcatenationMatrix")
30
31
  DNAThreader = _LazyServiceFactory("phykit.services.alignment.dna_threader", "DNAThreader")
31
32
  Dtt = _LazyServiceFactory("phykit.services.tree.dtt", "Dtt")
@@ -10,6 +10,7 @@ _EXPORTS = {
10
10
  "ColumnScore": "column_score",
11
11
  "CompositionalBiasPerSite": "compositional_bias_per_site",
12
12
  "CompositionPerTaxon": "composition_per_taxon",
13
+ "CodonDnDs": "codon_dnds",
13
14
  "CreateConcatenationMatrix": "create_concatenation_matrix",
14
15
  "DNAThreader": "dna_threader",
15
16
  "Dstatistic": "dstatistic",
@@ -0,0 +1,400 @@
1
+ from __future__ import annotations
2
+
3
+ import itertools
4
+ import math
5
+ import statistics
6
+
7
+ from .base import Alignment
8
+ from ...errors import PhykitUserError
9
+
10
+
11
+ _DNA_SYMBOLS = frozenset("ACGTRYSWKMBDHVN-?.U")
12
+ _UNAMBIGUOUS_DNA = frozenset("ACGT")
13
+ _CALCULATE_DN_DS = None
14
+ _BIO_ALIGNMENT = None
15
+ _BIO_SEQ = None
16
+ _CODON_TABLES = None
17
+
18
+
19
+ def print_json(*args, **kwargs):
20
+ from ...helpers.json_output import print_json as _print_json
21
+
22
+ return _print_json(*args, **kwargs)
23
+
24
+
25
+ def _load_codon_api():
26
+ global _CALCULATE_DN_DS, _BIO_ALIGNMENT, _BIO_SEQ, _CODON_TABLES
27
+
28
+ if _CALCULATE_DN_DS is None:
29
+ from Bio.Align import Alignment as BioAlignment
30
+ from Bio.Align import analysis
31
+ from Bio.Data import CodonTable
32
+ from Bio.Seq import Seq
33
+
34
+ _CALCULATE_DN_DS = analysis.calculate_dn_ds
35
+ _BIO_ALIGNMENT = BioAlignment
36
+ _BIO_SEQ = Seq
37
+ _CODON_TABLES = CodonTable.unambiguous_dna_by_id
38
+
39
+ return _CALCULATE_DN_DS, _BIO_ALIGNMENT, _BIO_SEQ, _CODON_TABLES
40
+
41
+
42
+ def _mean(values):
43
+ return statistics.fmean(values) if values else None
44
+
45
+
46
+ def _median(values):
47
+ return statistics.median(values) if values else None
48
+
49
+
50
+ def _format_number(value):
51
+ if value is None:
52
+ return "NA"
53
+ return f"{value:.8g}"
54
+
55
+
56
+ class CodonDnDs(Alignment):
57
+ """Estimate pairwise dN, dS, and omega from a codon alignment."""
58
+
59
+ def __init__(self, args) -> None:
60
+ parsed = self.process_args(args)
61
+ super().__init__(alignment_file_path=parsed["alignment_file_path"])
62
+ self.method = parsed["method"]
63
+ self.genetic_code = parsed["genetic_code"]
64
+ self.kappa = parsed["kappa"]
65
+ self.codon_frequency = parsed["codon_frequency"]
66
+ self.stop_policy = parsed["stop_policy"]
67
+ self.reference = parsed["reference"]
68
+ self.verbose = parsed["verbose"]
69
+ self.json_output = parsed["json_output"]
70
+ self._validate_options()
71
+
72
+ @staticmethod
73
+ def process_args(args) -> dict:
74
+ method = getattr(args, "method", "NG86").upper()
75
+ codon_frequency = getattr(args, "codon_frequency", "F3X4").upper()
76
+ return dict(
77
+ alignment_file_path=args.alignment,
78
+ method=method,
79
+ genetic_code=getattr(args, "genetic_code", 1),
80
+ kappa=getattr(args, "kappa", 1.0),
81
+ codon_frequency=codon_frequency.replace("X", "x"),
82
+ stop_policy=getattr(args, "stop_policy", "error"),
83
+ reference=getattr(args, "reference", None),
84
+ verbose=getattr(args, "verbose", False),
85
+ json_output=getattr(args, "json", False),
86
+ )
87
+
88
+ def _validate_options(self) -> None:
89
+ if self.method not in {"NG86", "LWL85", "YN00", "ML"}:
90
+ raise PhykitUserError(
91
+ [f"Unknown dN/dS method: {self.method}. Use NG86, LWL85, YN00, or ML."]
92
+ )
93
+ if not math.isfinite(self.kappa) or self.kappa <= 0.0:
94
+ raise PhykitUserError(["--kappa must be a finite value greater than 0."])
95
+ if self.codon_frequency not in {"F1x4", "F3x4", "F61"}:
96
+ raise PhykitUserError(
97
+ ["--codon-frequency must be F1x4, F3x4, or F61."]
98
+ )
99
+ if self.stop_policy not in {"error", "skip"}:
100
+ raise PhykitUserError(["--stop-policy must be error or skip."])
101
+
102
+ def run(self) -> None:
103
+ alignment, _, is_protein = self.get_alignment_and_format()
104
+ codon_table = self._get_codon_table()
105
+ records = self._validate_alignment(alignment, is_protein, codon_table)
106
+ pairs = self._select_pairs(records)
107
+ results = [
108
+ self._estimate_pair(left, right, codon_table) for left, right in pairs
109
+ ]
110
+ summary = self._summarize(results)
111
+
112
+ if self.json_output:
113
+ payload = {
114
+ "method": self.method,
115
+ "genetic_code": self.genetic_code,
116
+ "kappa": self.kappa,
117
+ "codon_frequency": (
118
+ self.codon_frequency if self.method == "ML" else None
119
+ ),
120
+ "reference": self.reference,
121
+ "summary": summary,
122
+ }
123
+ if self.verbose:
124
+ payload["pairs"] = results
125
+ print_json(payload)
126
+ return
127
+
128
+ if self.verbose:
129
+ self._print_pairs(results)
130
+ else:
131
+ self._print_summary(summary)
132
+
133
+ def _get_codon_table(self):
134
+ _, _, _, tables = _load_codon_api()
135
+ try:
136
+ return tables[self.genetic_code]
137
+ except KeyError:
138
+ available = ", ".join(str(code) for code in sorted(tables))
139
+ raise PhykitUserError(
140
+ [
141
+ f"Unknown NCBI genetic code: {self.genetic_code}.",
142
+ f"Available genetic-code IDs: {available}",
143
+ ]
144
+ )
145
+
146
+ def _validate_alignment(self, alignment, is_protein, codon_table):
147
+ if is_protein:
148
+ raise PhykitUserError(
149
+ ["Codon dN/dS analysis requires a nucleotide alignment."]
150
+ )
151
+ if len(alignment) < 2:
152
+ raise PhykitUserError(
153
+ ["Codon dN/dS analysis requires at least two sequences."]
154
+ )
155
+
156
+ alignment_length = alignment.get_alignment_length()
157
+ if alignment_length == 0:
158
+ raise PhykitUserError(["Codon alignment is empty."])
159
+ if alignment_length % 3:
160
+ raise PhykitUserError(
161
+ [
162
+ f"Codon alignment length ({alignment_length}) is not divisible by 3.",
163
+ "The alignment must begin at the first codon position and preserve frame.",
164
+ ]
165
+ )
166
+
167
+ stop_codons = frozenset(codon_table.stop_codons)
168
+ last_codon_start = alignment_length - 3
169
+ seen_ids = set()
170
+ records = []
171
+ for record in alignment:
172
+ taxon = record.id
173
+ if not taxon:
174
+ raise PhykitUserError(["Codon alignment contains an empty taxon ID."])
175
+ if taxon in seen_ids:
176
+ raise PhykitUserError(
177
+ [f"Codon alignment contains duplicate taxon ID: {taxon}"]
178
+ )
179
+ seen_ids.add(taxon)
180
+
181
+ sequence = str(record.seq).upper().replace("U", "T").replace(".", "-")
182
+ if len(sequence) != alignment_length:
183
+ raise PhykitUserError(
184
+ [
185
+ f"Sequence {taxon!r} has length {len(sequence)}, but the alignment length is {alignment_length}.",
186
+ "All codon-alignment sequences must have equal length.",
187
+ ]
188
+ )
189
+ invalid = sorted(set(sequence) - _DNA_SYMBOLS)
190
+ if invalid:
191
+ symbols = ", ".join(repr(symbol) for symbol in invalid)
192
+ raise PhykitUserError(
193
+ [f"Sequence {taxon!r} contains invalid DNA symbol(s): {symbols}"]
194
+ )
195
+
196
+ for start in range(0, alignment_length, 3):
197
+ codon = sequence[start : start + 3]
198
+ if "-" in codon and codon != "---":
199
+ raise PhykitUserError(
200
+ [
201
+ f"Sequence {taxon!r} has a frame-disrupting gap in codon {start // 3 + 1}: {codon}",
202
+ "Codon-alignment gaps must occupy complete codons (---).",
203
+ ]
204
+ )
205
+ if (
206
+ start != last_codon_start
207
+ and set(codon) <= _UNAMBIGUOUS_DNA
208
+ and codon in stop_codons
209
+ and self.stop_policy == "error"
210
+ ):
211
+ raise PhykitUserError(
212
+ [
213
+ f"Sequence {taxon!r} contains an internal stop codon at codon {start // 3 + 1}: {codon}",
214
+ "Use --stop-policy skip only when internal stops should be excluded pairwise.",
215
+ ]
216
+ )
217
+
218
+ records.append((taxon, sequence))
219
+ return records
220
+
221
+ def _select_pairs(self, records):
222
+ if self.reference is None:
223
+ return itertools.combinations(records, 2)
224
+
225
+ reference_record = None
226
+ for record in records:
227
+ if record[0] == self.reference:
228
+ reference_record = record
229
+ break
230
+ if reference_record is None:
231
+ raise PhykitUserError(
232
+ [f"Reference taxon {self.reference!r} is not present in the alignment."]
233
+ )
234
+ return (
235
+ (reference_record, record)
236
+ for record in records
237
+ if record is not reference_record
238
+ )
239
+
240
+ @staticmethod
241
+ def _prepare_pair(sequence_a, sequence_b, stop_codons):
242
+ clean_a = []
243
+ clean_b = []
244
+ skipped = 0
245
+ for start in range(0, len(sequence_a), 3):
246
+ codon_a = sequence_a[start : start + 3]
247
+ codon_b = sequence_b[start : start + 3]
248
+ if (
249
+ set(codon_a) <= _UNAMBIGUOUS_DNA
250
+ and set(codon_b) <= _UNAMBIGUOUS_DNA
251
+ and codon_a not in stop_codons
252
+ and codon_b not in stop_codons
253
+ ):
254
+ clean_a.append(codon_a)
255
+ clean_b.append(codon_b)
256
+ else:
257
+ skipped += 1
258
+ return "".join(clean_a), "".join(clean_b), skipped
259
+
260
+ def _estimate_pair(self, left, right, codon_table):
261
+ taxon_a, sequence_a = left
262
+ taxon_b, sequence_b = right
263
+ clean_a, clean_b, skipped = self._prepare_pair(
264
+ sequence_a, sequence_b, frozenset(codon_table.stop_codons)
265
+ )
266
+ codons_used = len(clean_a) // 3
267
+ result = {
268
+ "taxon_a": taxon_a,
269
+ "taxon_b": taxon_b,
270
+ "dN": None,
271
+ "dS": None,
272
+ "omega": None,
273
+ "codons_used": codons_used,
274
+ "codons_skipped": skipped,
275
+ "status": "no_valid_codons",
276
+ }
277
+ if not clean_a:
278
+ return result
279
+
280
+ calculate_dn_ds, bio_alignment, bio_seq, _ = _load_codon_api()
281
+ pair_alignment = bio_alignment([bio_seq(clean_a), bio_seq(clean_b)])
282
+ kwargs = {
283
+ "method": self.method,
284
+ "codon_table": codon_table,
285
+ }
286
+ if self.method == "NG86":
287
+ kwargs["k"] = self.kappa
288
+ elif self.method == "ML":
289
+ kwargs["cfreq"] = self.codon_frequency
290
+
291
+ try:
292
+ d_n, d_s = calculate_dn_ds(pair_alignment, **kwargs)
293
+ d_n = float(d_n)
294
+ d_s = float(d_s)
295
+ except (ArithmeticError, FloatingPointError, RuntimeError, ValueError) as err:
296
+ result["status"] = "estimation_failed"
297
+ result["message"] = str(err) or err.__class__.__name__
298
+ return result
299
+
300
+ valid_d_n = math.isfinite(d_n) and d_n >= 0.0
301
+ valid_d_s = math.isfinite(d_s) and d_s >= 0.0
302
+ if valid_d_n:
303
+ result["dN"] = d_n
304
+ if valid_d_s:
305
+ result["dS"] = d_s
306
+
307
+ if not valid_d_n or not valid_d_s:
308
+ result["status"] = "saturated_or_undefined"
309
+ elif d_s == 0.0:
310
+ result["status"] = "zero_synonymous_distance"
311
+ else:
312
+ omega = d_n / d_s
313
+ if math.isfinite(omega):
314
+ result["omega"] = omega
315
+ result["status"] = "ok"
316
+ else:
317
+ result["status"] = "saturated_or_undefined"
318
+ return result
319
+
320
+ @staticmethod
321
+ def _summarize(results):
322
+ d_n_values = [row["dN"] for row in results if row["dN"] is not None]
323
+ d_s_values = [row["dS"] for row in results if row["dS"] is not None]
324
+ omega_values = [
325
+ row["omega"] for row in results if row["omega"] is not None
326
+ ]
327
+ jointly_estimated = [
328
+ row
329
+ for row in results
330
+ if row["dN"] is not None
331
+ and row["dS"] is not None
332
+ and row["dS"] > 0.0
333
+ ]
334
+ joint_mean_d_n = _mean([row["dN"] for row in jointly_estimated])
335
+ joint_mean_d_s = _mean([row["dS"] for row in jointly_estimated])
336
+ ratio_of_means = None
337
+ if joint_mean_d_s is not None and joint_mean_d_s > 0.0:
338
+ ratio_of_means = joint_mean_d_n / joint_mean_d_s
339
+
340
+ status_counts = {}
341
+ for row in results:
342
+ status = row["status"]
343
+ status_counts[status] = status_counts.get(status, 0) + 1
344
+
345
+ return {
346
+ "pairs_total": len(results),
347
+ "pairs_with_dN": len(d_n_values),
348
+ "pairs_with_dS": len(d_s_values),
349
+ "pairs_with_omega": len(omega_values),
350
+ "mean_dN": _mean(d_n_values),
351
+ "median_dN": _median(d_n_values),
352
+ "mean_dS": _mean(d_s_values),
353
+ "median_dS": _median(d_s_values),
354
+ "mean_omega": _mean(omega_values),
355
+ "median_omega": _median(omega_values),
356
+ "ratio_of_mean_dN_to_mean_dS": ratio_of_means,
357
+ "status_counts": status_counts,
358
+ }
359
+
360
+ @staticmethod
361
+ def _print_pairs(results):
362
+ try:
363
+ print(
364
+ "taxon_a\ttaxon_b\tdN\tdS\tomega\tcodons_used\t"
365
+ "codons_skipped\tstatus"
366
+ )
367
+ for row in results:
368
+ print(
369
+ f"{row['taxon_a']}\t{row['taxon_b']}\t"
370
+ f"{_format_number(row['dN'])}\t"
371
+ f"{_format_number(row['dS'])}\t"
372
+ f"{_format_number(row['omega'])}\t"
373
+ f"{row['codons_used']}\t{row['codons_skipped']}\t"
374
+ f"{row['status']}"
375
+ )
376
+ except BrokenPipeError:
377
+ pass
378
+
379
+ def _print_summary(self, summary):
380
+ try:
381
+ lines = [
382
+ f"method: {self.method}",
383
+ f"genetic code: {self.genetic_code}",
384
+ f"sequence pairs: {summary['pairs_total']}",
385
+ f"pairs with omega: {summary['pairs_with_omega']}",
386
+ f"mean dN: {_format_number(summary['mean_dN'])}",
387
+ f"mean dS: {_format_number(summary['mean_dS'])}",
388
+ "dN/dS (ratio of means): "
389
+ f"{_format_number(summary['ratio_of_mean_dN_to_mean_dS'])}",
390
+ f"mean pairwise omega: {_format_number(summary['mean_omega'])}",
391
+ f"median pairwise omega: {_format_number(summary['median_omega'])}",
392
+ "pair statuses: "
393
+ + ", ".join(
394
+ f"{status}={count}"
395
+ for status, count in sorted(summary["status_counts"].items())
396
+ ),
397
+ ]
398
+ print("\n".join(lines))
399
+ except BrokenPipeError:
400
+ pass
@@ -0,0 +1 @@
1
+ __version__ = "2.3.0"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: phykit
3
- Version: 2.2.1
3
+ Version: 2.3.0
4
4
  Home-page: https://github.com/jlsteenwyk/phykit
5
5
  Author: Jacob L. Steenwyk
6
6
  Author-email: jlsteenwyk@gmail.com
@@ -43,6 +43,7 @@ phykit/services/alignment/alignment_outlier_taxa.py
43
43
  phykit/services/alignment/alignment_recoding.py
44
44
  phykit/services/alignment/alignment_subsample.py
45
45
  phykit/services/alignment/base.py
46
+ phykit/services/alignment/codon_dnds.py
46
47
  phykit/services/alignment/column_score.py
47
48
  phykit/services/alignment/composition_per_taxon.py
48
49
  phykit/services/alignment/compositional_bias_per_site.py
@@ -25,6 +25,7 @@ pk_chrono = phykit.phykit:chronogram
25
25
  pk_chronogram = phykit.phykit:chronogram
26
26
  pk_clan_check = phykit.phykit:hidden_paralogy_check
27
27
  pk_cmap = phykit.phykit:cont_map
28
+ pk_codon_dnds = phykit.phykit:codon_dnds
28
29
  pk_collapse = phykit.phykit:collapse_branches
29
30
  pk_comp_bias_per_site = phykit.phykit:compositional_bias_per_site
30
31
  pk_comp_tax = phykit.phykit:composition_per_taxon
@@ -48,6 +49,8 @@ pk_dimreduce = phykit.phykit:phylogenetic_ordination
48
49
  pk_disc_asym = phykit.phykit:discordance_asymmetry
49
50
  pk_disparity_through_time = phykit.phykit:dtt
50
51
  pk_dmap = phykit.phykit:density_map
52
+ pk_dn_ds = phykit.phykit:codon_dnds
53
+ pk_dnds = phykit.phykit:codon_dnds
51
54
  pk_dstat = phykit.phykit:dstatistic
52
55
  pk_dtt = phykit.phykit:dtt
53
56
  pk_entropy = phykit.phykit:alignment_entropy
@@ -76,6 +79,7 @@ pk_identity_matrix = phykit.phykit:identity_matrix
76
79
  pk_il = phykit.phykit:internode_labeler
77
80
  pk_impute = phykit.phykit:phylo_impute
78
81
  pk_is_monophyletic = phykit.phykit:monophyly_check
82
+ pk_kaks = phykit.phykit:codon_dnds
79
83
  pk_kf = phykit.phykit:kf_distance
80
84
  pk_kf_dist = phykit.phykit:kf_distance
81
85
  pk_kuhner_felsenstein_distance = phykit.phykit:kf_distance
@@ -1 +0,0 @@
1
- __version__ = "2.2.1"
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