bdms-sim 0.2.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.
- bdms_sim-0.2.0/.flake8 +10 -0
- bdms_sim-0.2.0/.github/workflows/build-and-test.yml +44 -0
- bdms_sim-0.2.0/.github/workflows/docs-build-and-deploy.yml +59 -0
- bdms_sim-0.2.0/.github/workflows/publish.yml +33 -0
- bdms_sim-0.2.0/.gitignore +167 -0
- bdms_sim-0.2.0/CITATION.cff +59 -0
- bdms_sim-0.2.0/LICENSE +21 -0
- bdms_sim-0.2.0/PKG-INFO +27 -0
- bdms_sim-0.2.0/README.md +7 -0
- bdms_sim-0.2.0/bdms/__init__.py +9 -0
- bdms_sim-0.2.0/bdms/mutators.py +232 -0
- bdms_sim-0.2.0/bdms/poisson.py +305 -0
- bdms_sim-0.2.0/bdms/tree.py +646 -0
- bdms_sim-0.2.0/bdms/utils.py +129 -0
- bdms_sim-0.2.0/docs/Makefile +26 -0
- bdms_sim-0.2.0/docs/_templates/autoapi/python/class.rst +60 -0
- bdms_sim-0.2.0/docs/_templates/autoapi/python/module.rst +114 -0
- bdms_sim-0.2.0/docs/cite.rst +6 -0
- bdms_sim-0.2.0/docs/conf.py +147 -0
- bdms_sim-0.2.0/docs/developer.rst +65 -0
- bdms_sim-0.2.0/docs/index.rst +47 -0
- bdms_sim-0.2.0/docs/install.rst +21 -0
- bdms_sim-0.2.0/docs/notebooks/performance.ipynb +223 -0
- bdms_sim-0.2.0/docs/notebooks/quickstart.ipynb +451 -0
- bdms_sim-0.2.0/docs/static/logo.png +0 -0
- bdms_sim-0.2.0/docs/todo.rst +4 -0
- bdms_sim-0.2.0/pyproject.toml +106 -0
- bdms_sim-0.2.0/tests/test_bdms.py +87 -0
bdms_sim-0.2.0/.flake8
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: build and test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
build-and-test:
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
os: [macos-latest]
|
|
14
|
+
python-version: [3.11, 3.12]
|
|
15
|
+
|
|
16
|
+
runs-on: ${{ matrix.os }}
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- name: Checkout 🛎️
|
|
20
|
+
uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
23
|
+
uses: actions/setup-python@v5
|
|
24
|
+
with:
|
|
25
|
+
python-version: ${{ matrix.python-version }}
|
|
26
|
+
cache: pip
|
|
27
|
+
|
|
28
|
+
- name: Setup Graphviz
|
|
29
|
+
uses: ts-graphviz/setup-graphviz@v1
|
|
30
|
+
|
|
31
|
+
- name: Install BDMS
|
|
32
|
+
run: |
|
|
33
|
+
python -m pip install --upgrade pip
|
|
34
|
+
pip install hatch
|
|
35
|
+
hatch env create
|
|
36
|
+
|
|
37
|
+
- name: Lint
|
|
38
|
+
run: hatch run lint
|
|
39
|
+
|
|
40
|
+
- name: Run tests
|
|
41
|
+
run: hatch run tests
|
|
42
|
+
|
|
43
|
+
- name: Test docs build
|
|
44
|
+
run: hatch run docs
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
name: docs build and deploy
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: ["main"]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
pages: write
|
|
10
|
+
id-token: write
|
|
11
|
+
|
|
12
|
+
concurrency:
|
|
13
|
+
group: "pages"
|
|
14
|
+
cancel-in-progress: false
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
build:
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
steps:
|
|
20
|
+
- name: Checkout
|
|
21
|
+
uses: actions/checkout@v4
|
|
22
|
+
|
|
23
|
+
- name: Setup Pages
|
|
24
|
+
uses: actions/configure-pages@v4
|
|
25
|
+
|
|
26
|
+
- name: Set up Python
|
|
27
|
+
uses: actions/setup-python@v5
|
|
28
|
+
with:
|
|
29
|
+
python-version: 3.12
|
|
30
|
+
cache: pip
|
|
31
|
+
|
|
32
|
+
- name: Setup Graphviz
|
|
33
|
+
uses: ts-graphviz/setup-graphviz@v1
|
|
34
|
+
|
|
35
|
+
- name: Install BDMS
|
|
36
|
+
run: |
|
|
37
|
+
python -m pip install --upgrade pip
|
|
38
|
+
pip install hatch
|
|
39
|
+
hatch env create
|
|
40
|
+
|
|
41
|
+
- name: Build 🔧
|
|
42
|
+
run: |
|
|
43
|
+
hatch run docs
|
|
44
|
+
|
|
45
|
+
- name: Upload artifact
|
|
46
|
+
uses: actions/upload-pages-artifact@v3
|
|
47
|
+
with:
|
|
48
|
+
path: docs/_build/html
|
|
49
|
+
|
|
50
|
+
deploy:
|
|
51
|
+
environment:
|
|
52
|
+
name: github-pages
|
|
53
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
|
54
|
+
runs-on: ubuntu-latest
|
|
55
|
+
needs: build
|
|
56
|
+
steps:
|
|
57
|
+
- name: Deploy to GitHub Pages
|
|
58
|
+
id: deployment
|
|
59
|
+
uses: actions/deploy-pages@v4
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
deploy:
|
|
12
|
+
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
|
|
15
|
+
environment: release
|
|
16
|
+
permissions:
|
|
17
|
+
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v4
|
|
21
|
+
- name: Set up Python
|
|
22
|
+
uses: actions/setup-python@v5
|
|
23
|
+
with:
|
|
24
|
+
python-version: '3.12'
|
|
25
|
+
cache: 'pip'
|
|
26
|
+
- name: Install dependencies
|
|
27
|
+
run: |
|
|
28
|
+
python -m pip install --upgrade pip
|
|
29
|
+
pip install hatch
|
|
30
|
+
- name: Build package
|
|
31
|
+
run: hatch build
|
|
32
|
+
- name: Publish package distributions to PyPI
|
|
33
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
docs/_autosummary/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
#Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# poetry
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
103
|
+
#poetry.lock
|
|
104
|
+
|
|
105
|
+
# pdm
|
|
106
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
107
|
+
#pdm.lock
|
|
108
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
109
|
+
# in version control.
|
|
110
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
111
|
+
.pdm.toml
|
|
112
|
+
|
|
113
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
114
|
+
__pypackages__/
|
|
115
|
+
|
|
116
|
+
# Celery stuff
|
|
117
|
+
celerybeat-schedule
|
|
118
|
+
celerybeat.pid
|
|
119
|
+
|
|
120
|
+
# SageMath parsed files
|
|
121
|
+
*.sage.py
|
|
122
|
+
|
|
123
|
+
# Environments
|
|
124
|
+
.env
|
|
125
|
+
.venv
|
|
126
|
+
env/
|
|
127
|
+
venv/
|
|
128
|
+
ENV/
|
|
129
|
+
env.bak/
|
|
130
|
+
venv.bak/
|
|
131
|
+
|
|
132
|
+
# Spyder project settings
|
|
133
|
+
.spyderproject
|
|
134
|
+
.spyproject
|
|
135
|
+
|
|
136
|
+
# Rope project settings
|
|
137
|
+
.ropeproject
|
|
138
|
+
|
|
139
|
+
# mkdocs documentation
|
|
140
|
+
/site
|
|
141
|
+
|
|
142
|
+
# mypy
|
|
143
|
+
.mypy_cache/
|
|
144
|
+
.dmypy.json
|
|
145
|
+
dmypy.json
|
|
146
|
+
|
|
147
|
+
# Pyre type checker
|
|
148
|
+
.pyre/
|
|
149
|
+
|
|
150
|
+
# pytype static type analyzer
|
|
151
|
+
.pytype/
|
|
152
|
+
|
|
153
|
+
# Cython debug symbols
|
|
154
|
+
cython_debug/
|
|
155
|
+
|
|
156
|
+
# PyCharm
|
|
157
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
158
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
159
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
160
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
161
|
+
#.idea/
|
|
162
|
+
|
|
163
|
+
# vscode
|
|
164
|
+
.vscode/
|
|
165
|
+
|
|
166
|
+
# hatch
|
|
167
|
+
.hatch/
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
title: BDMS
|
|
3
|
+
message: 'If you use BDMS in your work, please cite as follows.'
|
|
4
|
+
type: software
|
|
5
|
+
authors:
|
|
6
|
+
- given-names: William S.
|
|
7
|
+
family-names: DeWitt
|
|
8
|
+
affiliation: 'University of California, Berkeley'
|
|
9
|
+
email: wsdewitt@berkeley.edu
|
|
10
|
+
orcid: 'https://orcid.org/0000-0002-6802-9139'
|
|
11
|
+
- given-names: Michael
|
|
12
|
+
family-names: Celentano
|
|
13
|
+
affiliation: 'University of California, Berkeley'
|
|
14
|
+
email: mcelentano@berkeley.edu
|
|
15
|
+
orcid: 'https://orcid.org/0000-0001-9474-1827'
|
|
16
|
+
- given-names: Thanasi
|
|
17
|
+
family-names: Bakis
|
|
18
|
+
affiliation: 'University of California, Irvine'
|
|
19
|
+
email: abakis@uci.edu
|
|
20
|
+
orcid: 'https://orcid.org/0000-0002-5730-1483'
|
|
21
|
+
- given-names: Duncan K.
|
|
22
|
+
family-names: Ralph
|
|
23
|
+
affiliation: Fred Hutchinson Cancer Center
|
|
24
|
+
email: dralph@fredhutch.org
|
|
25
|
+
orcid: 'https://orcid.org/0000-0002-2527-8610'
|
|
26
|
+
- given-names: Sebastian
|
|
27
|
+
family-names: Prillo
|
|
28
|
+
affiliation: 'University of California, Berkeley'
|
|
29
|
+
email: sprillo@berkeley.edu
|
|
30
|
+
orcid: 'https://orcid.org/0000-0002-2427-8627'
|
|
31
|
+
- given-names: Frederick A.
|
|
32
|
+
family-names: Matsen
|
|
33
|
+
name-suffix: IV
|
|
34
|
+
affiliation: Fred Hutchinson Cancer Center
|
|
35
|
+
email: matsen@fredhutch.org
|
|
36
|
+
orcid: 'https://orcid.org/0000-0003-0607-6025'
|
|
37
|
+
- given-names: Yun S.
|
|
38
|
+
family-names: Song
|
|
39
|
+
affiliation: 'University of California, Berkeley'
|
|
40
|
+
email: yss@berkeley.edu
|
|
41
|
+
orcid: 'https://orcid.org/0000-0002-0734-9868'
|
|
42
|
+
- given-names: Volodymyr M.
|
|
43
|
+
family-names: Minin
|
|
44
|
+
affiliation: 'University of California, Irvine'
|
|
45
|
+
email: vminin@uci.edu
|
|
46
|
+
orcid: 'https://orcid.org/0000-0002-1917-9288'
|
|
47
|
+
identifiers:
|
|
48
|
+
- type: doi
|
|
49
|
+
value: none
|
|
50
|
+
description: placeholder
|
|
51
|
+
repository-code: 'https://github.com/WSDeWitt/BDMS'
|
|
52
|
+
url: 'https://wsdewitt.github.io/BDMS'
|
|
53
|
+
abstract: >-
|
|
54
|
+
BDMS is a Python package for simulating trees from
|
|
55
|
+
birth-death-mutation-sampling (BDMS) processes. It
|
|
56
|
+
is developed by collaborators at the University of
|
|
57
|
+
California, Berkeley and the University of
|
|
58
|
+
California, Irvine, and the Fred Hutchinson Cancer.
|
|
59
|
+
license: MIT
|
bdms_sim-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 William DeWitt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
bdms_sim-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: bdms-sim
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Tree simulator for birth-death-mutation-sampling (BDMS) processes
|
|
5
|
+
Project-URL: Homepage, https://github.com/WSDeWitt/BDMS
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/WSDeWitt/BDMS/issues
|
|
7
|
+
Project-URL: Documentation, https://wsdewitt.github.io/BDMS
|
|
8
|
+
Author-email: Thanasi Bakis <abakis@uci.edu>, Michael Celentano <mcelentano@berkeley.edu>, William DeWitt <wsdewitt@gmail.com>, Frederick Matsen <ematsen@gmail.com>, Volodymyr Minin <yss@berkeley.edu>, Sebastian Prillo <sprillo@berkeley.edu>, Duncan Ralph <dkralph@gmail.com>, Yoonseo Song <ysong07@cs.washington.edu>, Yun Song <yss@berkeley.edu>
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Requires-Dist: ete3
|
|
15
|
+
Requires-Dist: matplotlib
|
|
16
|
+
Requires-Dist: numpy
|
|
17
|
+
Requires-Dist: pyqt5
|
|
18
|
+
Requires-Dist: scipy
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
[](https://github.com/WSDeWitt/BDMS/actions/workflows/build-and-test.yml)
|
|
22
|
+
[](https://github.com/WSDeWitt/BDMS/actions/workflows/docs-build-and-deploy.yml)
|
|
23
|
+
|
|
24
|
+
# BDMS
|
|
25
|
+
Tree simulator for birth-death-mutation-sampling (BDMS) processes
|
|
26
|
+
|
|
27
|
+
Documentation: https://wsdewitt.github.io/BDMS
|
bdms_sim-0.2.0/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
[](https://github.com/WSDeWitt/BDMS/actions/workflows/build-and-test.yml)
|
|
2
|
+
[](https://github.com/WSDeWitt/BDMS/actions/workflows/docs-build-and-deploy.yml)
|
|
3
|
+
|
|
4
|
+
# BDMS
|
|
5
|
+
Tree simulator for birth-death-mutation-sampling (BDMS) processes
|
|
6
|
+
|
|
7
|
+
Documentation: https://wsdewitt.github.io/BDMS
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
r"""Mutation effects generators.
|
|
2
|
+
|
|
3
|
+
Abstract base class for defining generic mutation effect generators (i.e.
|
|
4
|
+
:math:`\mathcal{p}(x\mid x')`), with arbitrary :py:class:`ete3.TreeNode` attribute
|
|
5
|
+
dependence. Some concrete child classes are included.
|
|
6
|
+
|
|
7
|
+
These classes are used to define mutation effects for simulations with
|
|
8
|
+
:py:class:`bdms.TreeNode.evolve`.
|
|
9
|
+
|
|
10
|
+
Example:
|
|
11
|
+
|
|
12
|
+
>>> import bdms
|
|
13
|
+
|
|
14
|
+
Define a discrete mutation model.
|
|
15
|
+
|
|
16
|
+
>>> mutator = bdms.mutators.DiscreteMutator(
|
|
17
|
+
... state_space=["a", "b", "c"],
|
|
18
|
+
... transition_matrix=[[0.0, 0.5, 0.5],
|
|
19
|
+
... [0.5, 0.0, 0.5],
|
|
20
|
+
... [0.5, 0.5, 0.0]],
|
|
21
|
+
... )
|
|
22
|
+
|
|
23
|
+
Mutate a node.
|
|
24
|
+
|
|
25
|
+
>>> node = bdms.TreeNode(state="a")
|
|
26
|
+
>>> mutator.mutate(node, seed=0)
|
|
27
|
+
>>> node.state
|
|
28
|
+
'c'
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from __future__ import annotations
|
|
32
|
+
from abc import ABC, abstractmethod
|
|
33
|
+
from typing import Any
|
|
34
|
+
from collections.abc import Sequence, Callable, Hashable
|
|
35
|
+
import numpy as np
|
|
36
|
+
import scipy.stats as stats
|
|
37
|
+
import ete3
|
|
38
|
+
|
|
39
|
+
# NOTE: sphinx is currently unable to present this in condensed form when the
|
|
40
|
+
# sphinx_autodoc_typehints extension is enabled
|
|
41
|
+
# TODO: use ArrayLike in various phenotype/time methods (current float types)
|
|
42
|
+
# once it is available in a stable release
|
|
43
|
+
# from numpy.typing import ArrayLike
|
|
44
|
+
from numpy.typing import NDArray
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Mutator(ABC):
|
|
48
|
+
r"""Abstract base class for mutators that mutate a specified
|
|
49
|
+
:py:class:`ete3.TreeNode` node attribute.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
attr: Node attribute to mutate.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, attr: str = "state", *args: Any, **kwargs: Any) -> None:
|
|
56
|
+
self.attr = attr
|
|
57
|
+
|
|
58
|
+
@abstractmethod
|
|
59
|
+
def mutate(
|
|
60
|
+
self,
|
|
61
|
+
node: ete3.TreeNode,
|
|
62
|
+
seed: int | np.random.Generator | None = None,
|
|
63
|
+
) -> None:
|
|
64
|
+
r"""Mutate a :py:class:`bdms.TreeNode` object in place.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
node: A :py:class:`bdms.TreeNode` to mutate.
|
|
68
|
+
seed: A seed to initialize the random number generation. If ``None``, then
|
|
69
|
+
fresh, unpredictable entropy will be pulled from the OS. If an
|
|
70
|
+
``int``, then it will be used to derive the initial state. If a
|
|
71
|
+
:py:class:`numpy.random.Generator`, then it will be used directly.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
@abstractmethod
|
|
75
|
+
def prob(self, attr1: float, attr2: float, log: bool = False) -> float:
|
|
76
|
+
r"""Probability of mutating from ``attr1`` to ``attr2``.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
attr1: The initial attribute value.
|
|
80
|
+
attr2: The final attribute value.
|
|
81
|
+
log: If ``True``, return the log-probability.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
Mutation probability (or log probability).
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
def __repr__(self) -> str:
|
|
88
|
+
keyval_strs = (
|
|
89
|
+
f"{key}={value}"
|
|
90
|
+
for key, value in vars(self).items()
|
|
91
|
+
if not key.startswith("_")
|
|
92
|
+
)
|
|
93
|
+
return f"{self.__class__.__name__}({', '.join(keyval_strs)})"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class GaussianMutator(Mutator):
|
|
97
|
+
r"""Gaussian mutation effects on a specified attribute.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
shift: Mean shift wrt current attribute value.
|
|
101
|
+
scale: Standard deviation of mutation effect.
|
|
102
|
+
attr: Node attribute to mutate.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
def __init__(
|
|
106
|
+
self,
|
|
107
|
+
shift: float = 0.0,
|
|
108
|
+
scale: float = 1.0,
|
|
109
|
+
attr: str = "state",
|
|
110
|
+
):
|
|
111
|
+
super().__init__(attr=attr)
|
|
112
|
+
self.shift = shift
|
|
113
|
+
self.scale = scale
|
|
114
|
+
self._distribution = stats.norm(loc=self.shift, scale=self.scale)
|
|
115
|
+
|
|
116
|
+
def mutate(
|
|
117
|
+
self,
|
|
118
|
+
node: ete3.TreeNode,
|
|
119
|
+
seed: int | np.random.Generator | None = None,
|
|
120
|
+
) -> None:
|
|
121
|
+
current_state = getattr(node, self.attr)
|
|
122
|
+
Δ = self._distribution.rvs(random_state=seed)
|
|
123
|
+
setattr(node, self.attr, current_state + Δ)
|
|
124
|
+
|
|
125
|
+
def prob(self, attr1: float, attr2: float, log: bool = False) -> float:
|
|
126
|
+
Δ = np.asarray(attr2) - np.asarray(attr1)
|
|
127
|
+
if log:
|
|
128
|
+
return self._distribution.logpdf(Δ) # type:ignore
|
|
129
|
+
return self._distribution.pdf(Δ) # type:ignore
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class KdeMutator(Mutator):
|
|
133
|
+
r"""Gaussian kernel density estimator (KDE) for mutation effect on a specified
|
|
134
|
+
attribute.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
data: Data to fit the KDE to.
|
|
138
|
+
attr: Node attribute to mutate.
|
|
139
|
+
bw_method: KDE bandwidth (see :py:class:`scipy.stats.gaussian_kde`).
|
|
140
|
+
weights: Weights of data points (see :py:class:`scipy.stats.gaussian_kde`).
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
def __init__(
|
|
144
|
+
self,
|
|
145
|
+
data: NDArray[np.float64],
|
|
146
|
+
attr: str = "state",
|
|
147
|
+
bw_method: str | float | Callable[..., float] | None = None,
|
|
148
|
+
weights: NDArray[np.float64] | None = None,
|
|
149
|
+
):
|
|
150
|
+
super().__init__(attr=attr)
|
|
151
|
+
self._distribution = stats.gaussian_kde(data, bw_method, weights)
|
|
152
|
+
|
|
153
|
+
def mutate(
|
|
154
|
+
self,
|
|
155
|
+
node: ete3.TreeNode,
|
|
156
|
+
seed: int | np.random.Generator | None = None,
|
|
157
|
+
) -> None:
|
|
158
|
+
current_state = getattr(node, self.attr)
|
|
159
|
+
Δ = self._distribution.resample(size=1, seed=seed)[0, 0]
|
|
160
|
+
setattr(node, self.attr, current_state + Δ)
|
|
161
|
+
|
|
162
|
+
def prob(self, attr1: float, attr2: float, log: bool = False) -> float:
|
|
163
|
+
Δ = np.asarray(attr2) - np.asarray(attr1)
|
|
164
|
+
if log:
|
|
165
|
+
return self._distribution.logpdf(Δ)
|
|
166
|
+
return self._distribution.pdf(Δ)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class DiscreteMutator(Mutator):
|
|
170
|
+
r"""Mutations on a discrete space with a stochastic matrix.
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
state_space: hashable state values.
|
|
174
|
+
transition_matrix: Right-stochastic matrix, where column and row orders match
|
|
175
|
+
the order of `state_space`.
|
|
176
|
+
attr: Node attribute to mutate.
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
def __init__(
|
|
180
|
+
self,
|
|
181
|
+
state_space: Sequence[Hashable],
|
|
182
|
+
transition_matrix: NDArray[np.float64],
|
|
183
|
+
attr: str = "state",
|
|
184
|
+
):
|
|
185
|
+
transition_matrix = np.asarray(transition_matrix, dtype=float)
|
|
186
|
+
if not (
|
|
187
|
+
transition_matrix.ndim == 2
|
|
188
|
+
and transition_matrix.shape[0]
|
|
189
|
+
== transition_matrix.shape[1]
|
|
190
|
+
== len(state_space)
|
|
191
|
+
):
|
|
192
|
+
raise ValueError(
|
|
193
|
+
f"Transition matrix shape {transition_matrix.shape} "
|
|
194
|
+
f"does not match state space size {state_space}"
|
|
195
|
+
)
|
|
196
|
+
if (
|
|
197
|
+
np.any(transition_matrix < 0)
|
|
198
|
+
or np.any(transition_matrix > 1)
|
|
199
|
+
or not np.allclose(transition_matrix.sum(axis=1), 1)
|
|
200
|
+
):
|
|
201
|
+
raise ValueError(
|
|
202
|
+
f"Transition matrix {transition_matrix} is not a valid stochastic"
|
|
203
|
+
" matrix."
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
super().__init__(attr=attr)
|
|
207
|
+
|
|
208
|
+
self.state_space: dict[Any, int] = {
|
|
209
|
+
state: index for index, state in enumerate(state_space)
|
|
210
|
+
}
|
|
211
|
+
"""Mapping from state values to their indices in the transition matrix."""
|
|
212
|
+
self.transition_matrix = transition_matrix
|
|
213
|
+
|
|
214
|
+
def mutate(
|
|
215
|
+
self,
|
|
216
|
+
node: ete3.TreeNode,
|
|
217
|
+
seed: int | np.random.Generator | None = None,
|
|
218
|
+
) -> None:
|
|
219
|
+
rng = np.random.default_rng(seed)
|
|
220
|
+
transition_probs = self.transition_matrix[
|
|
221
|
+
self.state_space[getattr(node, self.attr)], :
|
|
222
|
+
]
|
|
223
|
+
new_value = rng.choice(list(self.state_space.keys()), p=transition_probs)
|
|
224
|
+
setattr(node, self.attr, new_value)
|
|
225
|
+
|
|
226
|
+
def prob(self, attr1: float, attr2: float, log: bool = False) -> float:
|
|
227
|
+
result = self.transition_matrix[
|
|
228
|
+
self.state_space[attr1], self.state_space[attr2]
|
|
229
|
+
]
|
|
230
|
+
if log:
|
|
231
|
+
result = np.log(result)
|
|
232
|
+
return result
|