CodonAdaptPy 1.0.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 (62) hide show
  1. codonadaptpy-1.0.0/Dockerfile +13 -0
  2. codonadaptpy-1.0.0/LICENSE +14 -0
  3. codonadaptpy-1.0.0/MANIFEST.in +7 -0
  4. codonadaptpy-1.0.0/PKG-INFO +346 -0
  5. codonadaptpy-1.0.0/README.md +277 -0
  6. codonadaptpy-1.0.0/docs/api.md +128 -0
  7. codonadaptpy-1.0.0/docs/assets/codonadaptpy_graphical_abstract.png +0 -0
  8. codonadaptpy-1.0.0/docs/cli.md +76 -0
  9. codonadaptpy-1.0.0/docs/conf.py +61 -0
  10. codonadaptpy-1.0.0/docs/getting_started.md +84 -0
  11. codonadaptpy-1.0.0/docs/index.md +55 -0
  12. codonadaptpy-1.0.0/docs/input_validation.md +11 -0
  13. codonadaptpy-1.0.0/docs/interpretation.md +14 -0
  14. codonadaptpy-1.0.0/docs/metrics.md +25 -0
  15. codonadaptpy-1.0.0/docs/requirements.txt +4 -0
  16. codonadaptpy-1.0.0/environment.yml +27 -0
  17. codonadaptpy-1.0.0/examples/basic_analysis.py +14 -0
  18. codonadaptpy-1.0.0/pyproject.toml +66 -0
  19. codonadaptpy-1.0.0/setup.cfg +4 -0
  20. codonadaptpy-1.0.0/src/CodonAdaptPy.egg-info/PKG-INFO +346 -0
  21. codonadaptpy-1.0.0/src/CodonAdaptPy.egg-info/SOURCES.txt +60 -0
  22. codonadaptpy-1.0.0/src/CodonAdaptPy.egg-info/dependency_links.txt +1 -0
  23. codonadaptpy-1.0.0/src/CodonAdaptPy.egg-info/entry_points.txt +2 -0
  24. codonadaptpy-1.0.0/src/CodonAdaptPy.egg-info/requires.txt +50 -0
  25. codonadaptpy-1.0.0/src/CodonAdaptPy.egg-info/top_level.txt +1 -0
  26. codonadaptpy-1.0.0/src/codonadaptpy/__init__.py +67 -0
  27. codonadaptpy-1.0.0/src/codonadaptpy/__main__.py +17 -0
  28. codonadaptpy-1.0.0/src/codonadaptpy/__version__.py +21 -0
  29. codonadaptpy-1.0.0/src/codonadaptpy/aggregation.py +94 -0
  30. codonadaptpy-1.0.0/src/codonadaptpy/analyzer.py +270 -0
  31. codonadaptpy-1.0.0/src/codonadaptpy/cli.py +409 -0
  32. codonadaptpy-1.0.0/src/codonadaptpy/comparative.py +538 -0
  33. codonadaptpy-1.0.0/src/codonadaptpy/exceptions.py +39 -0
  34. codonadaptpy-1.0.0/src/codonadaptpy/genetic_code.py +115 -0
  35. codonadaptpy-1.0.0/src/codonadaptpy/metrics/__init__.py +28 -0
  36. codonadaptpy-1.0.0/src/codonadaptpy/metrics/adaptation.py +133 -0
  37. codonadaptpy-1.0.0/src/codonadaptpy/metrics/composition.py +173 -0
  38. codonadaptpy-1.0.0/src/codonadaptpy/metrics/evolutionary.py +60 -0
  39. codonadaptpy-1.0.0/src/codonadaptpy/metrics/host.py +145 -0
  40. codonadaptpy-1.0.0/src/codonadaptpy/metrics/pairs.py +107 -0
  41. codonadaptpy-1.0.0/src/codonadaptpy/metrics/usage.py +155 -0
  42. codonadaptpy-1.0.0/src/codonadaptpy/models.py +128 -0
  43. codonadaptpy-1.0.0/src/codonadaptpy/optimizer.py +194 -0
  44. codonadaptpy-1.0.0/src/codonadaptpy/parsers.py +220 -0
  45. codonadaptpy-1.0.0/src/codonadaptpy/phylo_visualization.py +334 -0
  46. codonadaptpy-1.0.0/src/codonadaptpy/phylogenetics.py +602 -0
  47. codonadaptpy-1.0.0/src/codonadaptpy/py.typed +1 -0
  48. codonadaptpy-1.0.0/src/codonadaptpy/references.py +169 -0
  49. codonadaptpy-1.0.0/src/codonadaptpy/reporting.py +174 -0
  50. codonadaptpy-1.0.0/src/codonadaptpy/validation.py +169 -0
  51. codonadaptpy-1.0.0/src/codonadaptpy/visualization.py +1073 -0
  52. codonadaptpy-1.0.0/tests/test_aggregation.py +69 -0
  53. codonadaptpy-1.0.0/tests/test_analyzer.py +22 -0
  54. codonadaptpy-1.0.0/tests/test_comparative.py +85 -0
  55. codonadaptpy-1.0.0/tests/test_independent_validation_fixtures.py +174 -0
  56. codonadaptpy-1.0.0/tests/test_metrics.py +110 -0
  57. codonadaptpy-1.0.0/tests/test_optimizer.py +26 -0
  58. codonadaptpy-1.0.0/tests/test_parsers.py +36 -0
  59. codonadaptpy-1.0.0/tests/test_phylogenetics.py +83 -0
  60. codonadaptpy-1.0.0/tests/test_references_reporting_cli.py +35 -0
  61. codonadaptpy-1.0.0/tests/test_validation.py +45 -0
  62. codonadaptpy-1.0.0/tests/test_visualization.py +38 -0
@@ -0,0 +1,13 @@
1
+ FROM python:3.12-slim
2
+
3
+ LABEL org.opencontainers.image.authors="Naveen Duhan"
4
+ LABEL org.opencontainers.image.version="1.0.0"
5
+ LABEL org.opencontainers.image.title="CodonAdaptPy"
6
+
7
+ WORKDIR /opt/codonadaptpy
8
+ COPY . .
9
+ RUN pip install --no-cache-dir ".[all]"
10
+
11
+ ENTRYPOINT ["codonadaptpy"]
12
+ CMD ["--help"]
13
+
@@ -0,0 +1,14 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2026 Naveen Duhan
5
+
6
+ This program is free software: you can redistribute it and/or modify it under
7
+ the terms of the GNU General Public License as published by the Free Software
8
+ Foundation, either version 3 of the License, or (at your option) any later
9
+ version.
10
+
11
+ This program is distributed in the hope that it will be useful, but WITHOUT
12
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ FOR A PARTICULAR PURPOSE. See <https://www.gnu.org/licenses/> for details.
14
+
@@ -0,0 +1,7 @@
1
+ include README.md LICENSE
2
+ include Dockerfile environment.yml
3
+ recursive-include docs *.md
4
+ include docs/conf.py docs/requirements.txt
5
+ include docs/assets/codonadaptpy_graphical_abstract.png
6
+ prune docs/_build
7
+ recursive-include examples *.py
@@ -0,0 +1,346 @@
1
+ Metadata-Version: 2.4
2
+ Name: CodonAdaptPy
3
+ Version: 1.0.0
4
+ Summary: Integrated codon-usage, host-adaptation, evolutionary analysis, and sequence optimization
5
+ Author: Naveen Duhan
6
+ License-Expression: GPL-3.0-or-later
7
+ Project-URL: Homepage, https://github.com/navduhan/CodonAdaptPy
8
+ Project-URL: Documentation, https://codonadaptpy.readthedocs.io
9
+ Project-URL: Repository, https://github.com/navduhan/CodonAdaptPy
10
+ Project-URL: Issues, https://github.com/navduhan/CodonAdaptPy/issues
11
+ Keywords: bioinformatics,codon usage,CAI,host adaptation,sequence optimization
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
22
+ Requires-Python: >=3.10
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Provides-Extra: analysis
26
+ Requires-Dist: numpy>=1.24; extra == "analysis"
27
+ Requires-Dist: pandas>=2; extra == "analysis"
28
+ Requires-Dist: scipy>=1.10; extra == "analysis"
29
+ Requires-Dist: scikit-learn>=1.3; extra == "analysis"
30
+ Requires-Dist: statsmodels>=0.14; extra == "analysis"
31
+ Provides-Extra: io
32
+ Requires-Dist: biopython>=1.81; extra == "io"
33
+ Requires-Dist: openpyxl>=3.1; extra == "io"
34
+ Provides-Extra: plot
35
+ Requires-Dist: plotly>=5.18; extra == "plot"
36
+ Requires-Dist: kaleido>=0.2; extra == "plot"
37
+ Requires-Dist: matplotlib>=3.7; extra == "plot"
38
+ Provides-Extra: phylo
39
+ Requires-Dist: biopython>=1.81; extra == "phylo"
40
+ Requires-Dist: ete3>=3.1.3; extra == "phylo"
41
+ Requires-Dist: numpy>=1.24; extra == "phylo"
42
+ Requires-Dist: scipy>=1.10; extra == "phylo"
43
+ Requires-Dist: matplotlib>=3.7; extra == "phylo"
44
+ Provides-Extra: report
45
+ Requires-Dist: jinja2>=3.1; extra == "report"
46
+ Requires-Dist: openpyxl>=3.1; extra == "report"
47
+ Requires-Dist: weasyprint>=61; extra == "report"
48
+ Provides-Extra: all
49
+ Requires-Dist: numpy>=1.24; extra == "all"
50
+ Requires-Dist: pandas>=2; extra == "all"
51
+ Requires-Dist: scipy>=1.10; extra == "all"
52
+ Requires-Dist: scikit-learn>=1.3; extra == "all"
53
+ Requires-Dist: statsmodels>=0.14; extra == "all"
54
+ Requires-Dist: biopython>=1.81; extra == "all"
55
+ Requires-Dist: ete3>=3.1.3; extra == "all"
56
+ Requires-Dist: openpyxl>=3.1; extra == "all"
57
+ Requires-Dist: plotly>=5.18; extra == "all"
58
+ Requires-Dist: kaleido>=0.2; extra == "all"
59
+ Requires-Dist: matplotlib>=3.7; extra == "all"
60
+ Requires-Dist: jinja2>=3.1; extra == "all"
61
+ Requires-Dist: weasyprint>=61; extra == "all"
62
+ Provides-Extra: dev
63
+ Requires-Dist: pytest>=7.4; extra == "dev"
64
+ Requires-Dist: pytest-cov>=4.1; extra == "dev"
65
+ Requires-Dist: ruff>=0.4; extra == "dev"
66
+ Requires-Dist: mypy>=1.8; extra == "dev"
67
+ Requires-Dist: build>=1.2; extra == "dev"
68
+ Dynamic: license-file
69
+
70
+ # CodonAdaptPy
71
+
72
+ [![CI](https://github.com/navduhan/CodonAdaptPy/actions/workflows/ci.yml/badge.svg)](https://github.com/navduhan/CodonAdaptPy/actions/workflows/ci.yml)
73
+ [![Documentation Status](https://readthedocs.org/projects/codonadaptpy/badge/?version=latest)](https://codonadaptpy.readthedocs.io/en/latest/?badge=latest)
74
+
75
+ CodonAdaptPy 1.0.0 is a standalone, class-based Python package for coding-sequence validation, codon-usage analysis, multi-host comparison, evolutionary diagnostics, reproducible reporting, and constrained codon optimization or deoptimization.
76
+
77
+ ![CodonAdaptPy workflow from validated coding sequences through modular codon analysis to auditable molecular-evolution evidence](docs/assets/codonadaptpy_graphical_abstract.png)
78
+
79
+ Author: Naveen Duhan
80
+ License: GNU General Public License v3 or later
81
+ Python: 3.10+
82
+
83
+ ## Highlights
84
+
85
+ - Input: pasted DNA/RNA, FASTA/multi-FASTA, GenBank, CSV/TSV, and ZIP archives of FASTA files.
86
+ - Quality control: frame, start/stop, internal stops, partial CDS, RNA conversion, ambiguity, unsupported characters, duplicates, and genetic-code validation.
87
+ - Metrics: CAI, simulated/expected CAI distribution, sliding CAI, RSCU, ENC, FOP, CBI, ICDI, RCDI, tAI, GC/GC1/GC2/GC3/GC3s, AT1/AT2/AT3, third-position bases, amino-acid composition, all 16 dinucleotide O/E ratios, trinucleotide frequencies, and codon-pair scores.
88
+ - Host analysis: multi-host CAI/RCDI, exact 59-codon SiD, RSCU distance, cosine/Euclidean distance, Jensen-Shannon divergence, Pearson/Spearman correlation, and a transparent composite score.
89
+ - Analysis scopes: individual genes, boundary-aware genome-wide CDS pooling per isolate, and arbitrary focus genes such as F.
90
+ - Evolutionary and comparative analysis: ENC-GC3s expectation, group-specific neutrality regression, PR2, group summaries, reported normality diagnostics, estimand-first paired/unpaired inference, explicit Welch/Mann-Whitney tests, effect sizes, bootstrap intervals, FDR correction, correspondence analysis, correlation matrices, PCA, repeated grouped LDA with held-out evaluation, hierarchical clustering, and distances.
91
+ - Phylogenetics: MAFFT alignment, mandatory trimAL publication trimming, IQ-TREE ModelFinder/ultrafast bootstrap or FastTree inference, ETE3-backed tree rendering, root-to-tip temporal signal, exploratory strict-clock time trees, lineage-through-time trajectories, and whole-genome/F-gene Robinson-Foulds concordance.
92
+ - Design: ranked synonymous candidates with CAI/GC/CpG/UpA objectives, motif and restriction-site constraints, homopolymer limits, pair scores, deterministic seeds, and visible trade-offs.
93
+ - Output: CSV, TSV, JSON, Excel, HTML, optional PDF, SVG/PNG/HTML plots, and optimized FASTA. Publication plots include PCA/LDA, confusion matrices, 59-codon RSCU profiles, nucleotide composition, ENC-GC3s/neutrality/PR2 diagnostics, all-16-dinucleotide profiles and heatmaps, and multi-host CAI/RCDI comparisons.
94
+
95
+ Codon-usage similarity is descriptive. It is not direct proof of increased expression, replication, virulence, host switching, transmission, or biological fitness.
96
+
97
+ ## Installation
98
+
99
+ Install the dependency-free core and CLI:
100
+
101
+ ```bash
102
+ python -m pip install .
103
+ ```
104
+
105
+ Install every standalone analysis, input, plot, and reporting feature:
106
+
107
+ ```bash
108
+ python -m pip install ".[all]"
109
+ ```
110
+
111
+ Phylogenetic workflows additionally require command-line programs. The
112
+ recommended complete environment installs them explicitly:
113
+
114
+ ```bash
115
+ conda env create -f environment.yml
116
+ conda activate codonadaptpy
117
+ ```
118
+
119
+ For an existing environment such as `ngs`:
120
+
121
+ ```bash
122
+ conda install -n ngs -c bioconda -c conda-forge mafft trimal iqtree fasttree ete3
123
+ python -m pip install ".[phylo]"
124
+ ```
125
+
126
+ CodonAdaptPy reports a missing executable instead of silently substituting a
127
+ different method. Publication mode requires trimAL unless `--no-trim` is
128
+ explicitly selected.
129
+
130
+ For development:
131
+
132
+ ```bash
133
+ python -m pip install -e ".[dev,all]"
134
+ pytest -q
135
+ ruff check .
136
+ ```
137
+
138
+ ## Command-line quick start
139
+
140
+ Analyze a multi-FASTA file against a target reference:
141
+
142
+ ```bash
143
+ codonadaptpy analyze coding_sequences.fasta \
144
+ --reference human_reference.json \
145
+ --formats json,csv,html \
146
+ --plots \
147
+ --output results
148
+ ```
149
+
150
+ Compare the same sequences with multiple hosts:
151
+
152
+ ```bash
153
+ codonadaptpy compare coding_sequences.fasta \
154
+ --host chicken.json \
155
+ --host turkey.json \
156
+ --host human.json \
157
+ --output host_comparison
158
+ ```
159
+
160
+ Analyze every gene, pool CDSs genome-wide by isolate, and report F separately:
161
+
162
+ ```bash
163
+ codonadaptpy analyze isolate_cds.csv \
164
+ --aggregate-by isolate \
165
+ --gene-key gene \
166
+ --focus-gene F \
167
+ --host chicken.json \
168
+ --host turkey.json \
169
+ --output ampv_analysis
170
+ ```
171
+
172
+ The input table may contain `identifier,sequence,isolate,gene,genotype,...` columns. Alternatively, FASTA descriptions may carry key/value metadata:
173
+
174
+ ```fasta
175
+ >isolate1_N isolate=isolate1 gene=N genotype=A
176
+ ATG...TAA
177
+ >isolate1_F isolate=isolate1 gene=F genotype=A
178
+ ATG...TAA
179
+ ```
180
+
181
+ For a FASTA plus a separate metadata file, pass `--metadata metadata.csv`. During genome-wide aggregation every CDS is validated independently, its reading frame starts again at position zero, its terminal stop is removed, and no nucleotide or codon pair is counted across gene boundaries.
182
+
183
+ Build a versioned reference profile from highly expressed coding sequences:
184
+
185
+ ```bash
186
+ codonadaptpy reference build highly_expressed_cds.fasta \
187
+ --name chicken_high_expression \
188
+ --reference-version 2026.1 \
189
+ --source "Ensembl release X; documented gene selection" \
190
+ --output chicken.json
191
+ ```
192
+
193
+ Generate several constrained candidates:
194
+
195
+ ```bash
196
+ codonadaptpy optimize gene.fasta \
197
+ --reference chicken.json \
198
+ --direction optimize \
199
+ --candidates 10 \
200
+ --target-gc 0.52 \
201
+ --avoid-motif AATAAA \
202
+ --remove-site GAATTC \
203
+ --seed 42 \
204
+ --output optimized_gene
205
+ ```
206
+
207
+ Run `codonadaptpy COMMAND --help` for all options.
208
+
209
+ Infer an ML tree and use a metadata sampling-year column for exploratory
210
+ temporal diagnostics:
211
+
212
+ ```bash
213
+ codonadaptpy phylogeny f_gene_sequences.fasta \
214
+ --metadata samples.csv \
215
+ --group-key genotype \
216
+ --date-key year \
217
+ --temporal \
218
+ --tree-method iqtree \
219
+ --bootstrap 1000 \
220
+ --threads 4 \
221
+ --output phylogeny_results
222
+ ```
223
+
224
+ This produces ML and time-tree figures, a root-to-tip regression, dated Newick,
225
+ lineage-through-time output, and JSON provenance. The deterministic time tree
226
+ and LTT curve are rapid deterministic diagnostics—not relaxed-clock posterior
227
+ dating, effective population-size inference, or 95% HPD intervals.
228
+
229
+ ## Python API
230
+
231
+ ```python
232
+ from codonadaptpy import CodonAnalyzer, SequenceRecord, ValidationConfig
233
+ from codonadaptpy.analyzer import AnalysisConfig
234
+ from codonadaptpy.references import ReferenceManager
235
+
236
+ manager = ReferenceManager()
237
+ reference = manager.load("chicken.json")
238
+
239
+ analyzer = CodonAnalyzer(
240
+ AnalysisConfig(validation=ValidationConfig(genetic_code=1)),
241
+ reference=reference,
242
+ )
243
+ result = analyzer.analyze(
244
+ SequenceRecord("example_gene", "ATGGCTGCTGACTAA")
245
+ )
246
+
247
+ print(result.metrics["cai"])
248
+ print(result.metrics["enc"])
249
+ print(result.metrics["gc3"])
250
+ ```
251
+
252
+ Create a publication-ready figure through the package:
253
+
254
+ ```python
255
+ from codonadaptpy import PublicationPlotManager
256
+
257
+ plots = PublicationPlotManager()
258
+ figure = plots.evolutionary_diagnostics(
259
+ results,
260
+ group_key="Genotype",
261
+ title="F-gene evolutionary diagnostics",
262
+ )
263
+ plots.save(figure, "f_gene_diagnostics.pdf")
264
+ plots.save(figure, "f_gene_diagnostics.png", dpi=600)
265
+ ```
266
+
267
+ Run phylogenetics through the class-based API:
268
+
269
+ ```python
270
+ from codonadaptpy import ETE3TreePlotter, PhylogeneticAnalyzer, TemporalAnalyzer
271
+
272
+ run = PhylogeneticAnalyzer().run(
273
+ records,
274
+ "phylogeny_results/inference",
275
+ tree_method="iqtree",
276
+ bootstrap=1000,
277
+ trim=True,
278
+ trim_required=True,
279
+ threads=4,
280
+ )
281
+ signal = TemporalAnalyzer().temporal_signal(run.tree, sample_dates, optimize_root=True)
282
+ dated_tree = TemporalAnalyzer().date_tree(run.tree, signal)
283
+ figure = ETE3TreePlotter().tree(dated_tree, groups=groups, time_scaled=True)
284
+ ETE3TreePlotter.save(figure, "phylogeny_results/time_tree.pdf")
285
+ ```
286
+
287
+ ## Reference profile format
288
+
289
+ JSON profiles preserve provenance and versioning:
290
+
291
+ ```json
292
+ {
293
+ "name": "host_high_expression",
294
+ "counts": {"AAA": 120, "AAG": 240},
295
+ "genetic_code": 1,
296
+ "version": "2026.1",
297
+ "source": "Database release and gene-selection method",
298
+ "description": "Reference CDS profile",
299
+ "metadata": {"taxon_id": 0000, "biological": true}
300
+ }
301
+ ```
302
+
303
+ Missing sense codons are completed with zero counts during validation. CSV/TSV profiles use `codon,count` or `codon,frequency` columns. The only bundled profile is `uniform_standard`, an explicitly non-biological testing baseline; real host tables must retain a documented source and release.
304
+
305
+ ## CAI edge-case policy
306
+
307
+ - CAI is calculated in logarithmic space.
308
+ - Stop and ambiguous codons do not contribute.
309
+ - Methionine and tryptophan contribute normally, usually with weight 1.
310
+ - A zero-frequency sense codon receives the configured positive floor (default 0.01).
311
+ - A sense codon missing from a custom weight mapping is skipped; non-positive supplied weights are rejected.
312
+ - Alternative NCBI codes require Biopython (`CodonAdaptPy[io]`).
313
+ - Very short sequences are accepted only when validation policy permits them; CAI still requires one eligible sense codon.
314
+
315
+ ## Output reproducibility
316
+
317
+ Every analysis captures CodonAdaptPy and Python versions, UTC analysis time, genetic code, reading frame, input SHA-256, random seed, and reference name/version. CLI runs also write the full quoted command and JSON configuration.
318
+
319
+ ## Documentation
320
+
321
+ - [Read the Docs](https://codonadaptpy.readthedocs.io)
322
+ - [Metric definitions](docs/metrics.md)
323
+ - [Input and validation guide](docs/input_validation.md)
324
+ - [Python API and extension guide](docs/api.md)
325
+ - [Interpretation and limitations](docs/interpretation.md)
326
+
327
+ ## Project structure
328
+
329
+ ```text
330
+ src/codonadaptpy/
331
+ ├── aggregation.py # Per-isolate gene and genome-wide CDS scopes
332
+ ├── analyzer.py # Main class-based API
333
+ ├── comparative.py # Group and multivariate analysis
334
+ ├── genetic_code.py # Translation tables and codon families
335
+ ├── metrics/ # Adaptation, usage, composition, pair, host, evolution
336
+ ├── models.py # Shared result and validation dataclasses
337
+ ├── optimizer.py # Constrained synonymous design
338
+ ├── parsers.py # FASTA, GenBank, tables, and ZIP input
339
+ ├── phylogenetics.py # Alignment, ML trees, temporal signal, topology tests
340
+ ├── phylo_visualization.py # ETE3-backed publication tree and LTT figures
341
+ ├── references.py # Versioned reference management
342
+ ├── reporting.py # JSON/CSV/Excel/HTML/PDF/FASTA output
343
+ ├── validation.py # Coding-sequence quality control
344
+ ├── visualization.py # Interactive and static plots
345
+ └── cli.py # Standalone subcommands
346
+ ```
@@ -0,0 +1,277 @@
1
+ # CodonAdaptPy
2
+
3
+ [![CI](https://github.com/navduhan/CodonAdaptPy/actions/workflows/ci.yml/badge.svg)](https://github.com/navduhan/CodonAdaptPy/actions/workflows/ci.yml)
4
+ [![Documentation Status](https://readthedocs.org/projects/codonadaptpy/badge/?version=latest)](https://codonadaptpy.readthedocs.io/en/latest/?badge=latest)
5
+
6
+ CodonAdaptPy 1.0.0 is a standalone, class-based Python package for coding-sequence validation, codon-usage analysis, multi-host comparison, evolutionary diagnostics, reproducible reporting, and constrained codon optimization or deoptimization.
7
+
8
+ ![CodonAdaptPy workflow from validated coding sequences through modular codon analysis to auditable molecular-evolution evidence](docs/assets/codonadaptpy_graphical_abstract.png)
9
+
10
+ Author: Naveen Duhan
11
+ License: GNU General Public License v3 or later
12
+ Python: 3.10+
13
+
14
+ ## Highlights
15
+
16
+ - Input: pasted DNA/RNA, FASTA/multi-FASTA, GenBank, CSV/TSV, and ZIP archives of FASTA files.
17
+ - Quality control: frame, start/stop, internal stops, partial CDS, RNA conversion, ambiguity, unsupported characters, duplicates, and genetic-code validation.
18
+ - Metrics: CAI, simulated/expected CAI distribution, sliding CAI, RSCU, ENC, FOP, CBI, ICDI, RCDI, tAI, GC/GC1/GC2/GC3/GC3s, AT1/AT2/AT3, third-position bases, amino-acid composition, all 16 dinucleotide O/E ratios, trinucleotide frequencies, and codon-pair scores.
19
+ - Host analysis: multi-host CAI/RCDI, exact 59-codon SiD, RSCU distance, cosine/Euclidean distance, Jensen-Shannon divergence, Pearson/Spearman correlation, and a transparent composite score.
20
+ - Analysis scopes: individual genes, boundary-aware genome-wide CDS pooling per isolate, and arbitrary focus genes such as F.
21
+ - Evolutionary and comparative analysis: ENC-GC3s expectation, group-specific neutrality regression, PR2, group summaries, reported normality diagnostics, estimand-first paired/unpaired inference, explicit Welch/Mann-Whitney tests, effect sizes, bootstrap intervals, FDR correction, correspondence analysis, correlation matrices, PCA, repeated grouped LDA with held-out evaluation, hierarchical clustering, and distances.
22
+ - Phylogenetics: MAFFT alignment, mandatory trimAL publication trimming, IQ-TREE ModelFinder/ultrafast bootstrap or FastTree inference, ETE3-backed tree rendering, root-to-tip temporal signal, exploratory strict-clock time trees, lineage-through-time trajectories, and whole-genome/F-gene Robinson-Foulds concordance.
23
+ - Design: ranked synonymous candidates with CAI/GC/CpG/UpA objectives, motif and restriction-site constraints, homopolymer limits, pair scores, deterministic seeds, and visible trade-offs.
24
+ - Output: CSV, TSV, JSON, Excel, HTML, optional PDF, SVG/PNG/HTML plots, and optimized FASTA. Publication plots include PCA/LDA, confusion matrices, 59-codon RSCU profiles, nucleotide composition, ENC-GC3s/neutrality/PR2 diagnostics, all-16-dinucleotide profiles and heatmaps, and multi-host CAI/RCDI comparisons.
25
+
26
+ Codon-usage similarity is descriptive. It is not direct proof of increased expression, replication, virulence, host switching, transmission, or biological fitness.
27
+
28
+ ## Installation
29
+
30
+ Install the dependency-free core and CLI:
31
+
32
+ ```bash
33
+ python -m pip install .
34
+ ```
35
+
36
+ Install every standalone analysis, input, plot, and reporting feature:
37
+
38
+ ```bash
39
+ python -m pip install ".[all]"
40
+ ```
41
+
42
+ Phylogenetic workflows additionally require command-line programs. The
43
+ recommended complete environment installs them explicitly:
44
+
45
+ ```bash
46
+ conda env create -f environment.yml
47
+ conda activate codonadaptpy
48
+ ```
49
+
50
+ For an existing environment such as `ngs`:
51
+
52
+ ```bash
53
+ conda install -n ngs -c bioconda -c conda-forge mafft trimal iqtree fasttree ete3
54
+ python -m pip install ".[phylo]"
55
+ ```
56
+
57
+ CodonAdaptPy reports a missing executable instead of silently substituting a
58
+ different method. Publication mode requires trimAL unless `--no-trim` is
59
+ explicitly selected.
60
+
61
+ For development:
62
+
63
+ ```bash
64
+ python -m pip install -e ".[dev,all]"
65
+ pytest -q
66
+ ruff check .
67
+ ```
68
+
69
+ ## Command-line quick start
70
+
71
+ Analyze a multi-FASTA file against a target reference:
72
+
73
+ ```bash
74
+ codonadaptpy analyze coding_sequences.fasta \
75
+ --reference human_reference.json \
76
+ --formats json,csv,html \
77
+ --plots \
78
+ --output results
79
+ ```
80
+
81
+ Compare the same sequences with multiple hosts:
82
+
83
+ ```bash
84
+ codonadaptpy compare coding_sequences.fasta \
85
+ --host chicken.json \
86
+ --host turkey.json \
87
+ --host human.json \
88
+ --output host_comparison
89
+ ```
90
+
91
+ Analyze every gene, pool CDSs genome-wide by isolate, and report F separately:
92
+
93
+ ```bash
94
+ codonadaptpy analyze isolate_cds.csv \
95
+ --aggregate-by isolate \
96
+ --gene-key gene \
97
+ --focus-gene F \
98
+ --host chicken.json \
99
+ --host turkey.json \
100
+ --output ampv_analysis
101
+ ```
102
+
103
+ The input table may contain `identifier,sequence,isolate,gene,genotype,...` columns. Alternatively, FASTA descriptions may carry key/value metadata:
104
+
105
+ ```fasta
106
+ >isolate1_N isolate=isolate1 gene=N genotype=A
107
+ ATG...TAA
108
+ >isolate1_F isolate=isolate1 gene=F genotype=A
109
+ ATG...TAA
110
+ ```
111
+
112
+ For a FASTA plus a separate metadata file, pass `--metadata metadata.csv`. During genome-wide aggregation every CDS is validated independently, its reading frame starts again at position zero, its terminal stop is removed, and no nucleotide or codon pair is counted across gene boundaries.
113
+
114
+ Build a versioned reference profile from highly expressed coding sequences:
115
+
116
+ ```bash
117
+ codonadaptpy reference build highly_expressed_cds.fasta \
118
+ --name chicken_high_expression \
119
+ --reference-version 2026.1 \
120
+ --source "Ensembl release X; documented gene selection" \
121
+ --output chicken.json
122
+ ```
123
+
124
+ Generate several constrained candidates:
125
+
126
+ ```bash
127
+ codonadaptpy optimize gene.fasta \
128
+ --reference chicken.json \
129
+ --direction optimize \
130
+ --candidates 10 \
131
+ --target-gc 0.52 \
132
+ --avoid-motif AATAAA \
133
+ --remove-site GAATTC \
134
+ --seed 42 \
135
+ --output optimized_gene
136
+ ```
137
+
138
+ Run `codonadaptpy COMMAND --help` for all options.
139
+
140
+ Infer an ML tree and use a metadata sampling-year column for exploratory
141
+ temporal diagnostics:
142
+
143
+ ```bash
144
+ codonadaptpy phylogeny f_gene_sequences.fasta \
145
+ --metadata samples.csv \
146
+ --group-key genotype \
147
+ --date-key year \
148
+ --temporal \
149
+ --tree-method iqtree \
150
+ --bootstrap 1000 \
151
+ --threads 4 \
152
+ --output phylogeny_results
153
+ ```
154
+
155
+ This produces ML and time-tree figures, a root-to-tip regression, dated Newick,
156
+ lineage-through-time output, and JSON provenance. The deterministic time tree
157
+ and LTT curve are rapid deterministic diagnostics—not relaxed-clock posterior
158
+ dating, effective population-size inference, or 95% HPD intervals.
159
+
160
+ ## Python API
161
+
162
+ ```python
163
+ from codonadaptpy import CodonAnalyzer, SequenceRecord, ValidationConfig
164
+ from codonadaptpy.analyzer import AnalysisConfig
165
+ from codonadaptpy.references import ReferenceManager
166
+
167
+ manager = ReferenceManager()
168
+ reference = manager.load("chicken.json")
169
+
170
+ analyzer = CodonAnalyzer(
171
+ AnalysisConfig(validation=ValidationConfig(genetic_code=1)),
172
+ reference=reference,
173
+ )
174
+ result = analyzer.analyze(
175
+ SequenceRecord("example_gene", "ATGGCTGCTGACTAA")
176
+ )
177
+
178
+ print(result.metrics["cai"])
179
+ print(result.metrics["enc"])
180
+ print(result.metrics["gc3"])
181
+ ```
182
+
183
+ Create a publication-ready figure through the package:
184
+
185
+ ```python
186
+ from codonadaptpy import PublicationPlotManager
187
+
188
+ plots = PublicationPlotManager()
189
+ figure = plots.evolutionary_diagnostics(
190
+ results,
191
+ group_key="Genotype",
192
+ title="F-gene evolutionary diagnostics",
193
+ )
194
+ plots.save(figure, "f_gene_diagnostics.pdf")
195
+ plots.save(figure, "f_gene_diagnostics.png", dpi=600)
196
+ ```
197
+
198
+ Run phylogenetics through the class-based API:
199
+
200
+ ```python
201
+ from codonadaptpy import ETE3TreePlotter, PhylogeneticAnalyzer, TemporalAnalyzer
202
+
203
+ run = PhylogeneticAnalyzer().run(
204
+ records,
205
+ "phylogeny_results/inference",
206
+ tree_method="iqtree",
207
+ bootstrap=1000,
208
+ trim=True,
209
+ trim_required=True,
210
+ threads=4,
211
+ )
212
+ signal = TemporalAnalyzer().temporal_signal(run.tree, sample_dates, optimize_root=True)
213
+ dated_tree = TemporalAnalyzer().date_tree(run.tree, signal)
214
+ figure = ETE3TreePlotter().tree(dated_tree, groups=groups, time_scaled=True)
215
+ ETE3TreePlotter.save(figure, "phylogeny_results/time_tree.pdf")
216
+ ```
217
+
218
+ ## Reference profile format
219
+
220
+ JSON profiles preserve provenance and versioning:
221
+
222
+ ```json
223
+ {
224
+ "name": "host_high_expression",
225
+ "counts": {"AAA": 120, "AAG": 240},
226
+ "genetic_code": 1,
227
+ "version": "2026.1",
228
+ "source": "Database release and gene-selection method",
229
+ "description": "Reference CDS profile",
230
+ "metadata": {"taxon_id": 0000, "biological": true}
231
+ }
232
+ ```
233
+
234
+ Missing sense codons are completed with zero counts during validation. CSV/TSV profiles use `codon,count` or `codon,frequency` columns. The only bundled profile is `uniform_standard`, an explicitly non-biological testing baseline; real host tables must retain a documented source and release.
235
+
236
+ ## CAI edge-case policy
237
+
238
+ - CAI is calculated in logarithmic space.
239
+ - Stop and ambiguous codons do not contribute.
240
+ - Methionine and tryptophan contribute normally, usually with weight 1.
241
+ - A zero-frequency sense codon receives the configured positive floor (default 0.01).
242
+ - A sense codon missing from a custom weight mapping is skipped; non-positive supplied weights are rejected.
243
+ - Alternative NCBI codes require Biopython (`CodonAdaptPy[io]`).
244
+ - Very short sequences are accepted only when validation policy permits them; CAI still requires one eligible sense codon.
245
+
246
+ ## Output reproducibility
247
+
248
+ Every analysis captures CodonAdaptPy and Python versions, UTC analysis time, genetic code, reading frame, input SHA-256, random seed, and reference name/version. CLI runs also write the full quoted command and JSON configuration.
249
+
250
+ ## Documentation
251
+
252
+ - [Read the Docs](https://codonadaptpy.readthedocs.io)
253
+ - [Metric definitions](docs/metrics.md)
254
+ - [Input and validation guide](docs/input_validation.md)
255
+ - [Python API and extension guide](docs/api.md)
256
+ - [Interpretation and limitations](docs/interpretation.md)
257
+
258
+ ## Project structure
259
+
260
+ ```text
261
+ src/codonadaptpy/
262
+ ├── aggregation.py # Per-isolate gene and genome-wide CDS scopes
263
+ ├── analyzer.py # Main class-based API
264
+ ├── comparative.py # Group and multivariate analysis
265
+ ├── genetic_code.py # Translation tables and codon families
266
+ ├── metrics/ # Adaptation, usage, composition, pair, host, evolution
267
+ ├── models.py # Shared result and validation dataclasses
268
+ ├── optimizer.py # Constrained synonymous design
269
+ ├── parsers.py # FASTA, GenBank, tables, and ZIP input
270
+ ├── phylogenetics.py # Alignment, ML trees, temporal signal, topology tests
271
+ ├── phylo_visualization.py # ETE3-backed publication tree and LTT figures
272
+ ├── references.py # Versioned reference management
273
+ ├── reporting.py # JSON/CSV/Excel/HTML/PDF/FASTA output
274
+ ├── validation.py # Coding-sequence quality control
275
+ ├── visualization.py # Interactive and static plots
276
+ └── cli.py # Standalone subcommands
277
+ ```