asebind 0.1.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.
- asebind-0.1.0/.gitignore +41 -0
- asebind-0.1.0/LICENSE +21 -0
- asebind-0.1.0/PKG-INFO +138 -0
- asebind-0.1.0/README.md +103 -0
- asebind-0.1.0/pyproject.toml +70 -0
- asebind-0.1.0/src/asebind/__init__.py +3 -0
- asebind-0.1.0/src/asebind/__main__.py +4 -0
- asebind-0.1.0/src/asebind/ase/__init__.py +1 -0
- asebind-0.1.0/src/asebind/ase/betabinom.py +173 -0
- asebind-0.1.0/src/asebind/benchmark/__init__.py +1 -0
- asebind-0.1.0/src/asebind/benchmark/asbdata.py +107 -0
- asebind-0.1.0/src/asebind/benchmark/clean.py +38 -0
- asebind-0.1.0/src/asebind/benchmark/features.py +71 -0
- asebind-0.1.0/src/asebind/benchmark/metrics.py +69 -0
- asebind-0.1.0/src/asebind/benchmark/models.py +151 -0
- asebind-0.1.0/src/asebind/benchmark/retest.py +183 -0
- asebind-0.1.0/src/asebind/benchmark/splits.py +61 -0
- asebind-0.1.0/src/asebind/cli.py +302 -0
- asebind-0.1.0/src/asebind/cli_model.py +539 -0
- asebind-0.1.0/src/asebind/config.py +340 -0
- asebind-0.1.0/src/asebind/data/asebind.example.yaml +56 -0
- asebind-0.1.0/src/asebind/finemap.py +86 -0
- asebind-0.1.0/src/asebind/haplotype.py +141 -0
- asebind-0.1.0/src/asebind/io/__init__.py +1 -0
- asebind-0.1.0/src/asebind/io/variants.py +107 -0
- asebind-0.1.0/src/asebind/model/__init__.py +1 -0
- asebind-0.1.0/src/asebind/model/adastra.py +113 -0
- asebind-0.1.0/src/asebind/model/classifier.py +122 -0
- asebind-0.1.0/src/asebind/model/context.py +60 -0
- asebind-0.1.0/src/asebind/model/features.py +201 -0
- asebind-0.1.0/src/asebind/model/other_motifs.py +168 -0
- asebind-0.1.0/src/asebind/model/train.py +331 -0
- asebind-0.1.0/src/asebind/motifs/__init__.py +1 -0
- asebind-0.1.0/src/asebind/motifs/escore.py +112 -0
- asebind-0.1.0/src/asebind/motifs/pwm.py +203 -0
- asebind-0.1.0/src/asebind/mpra.py +94 -0
- asebind-0.1.0/src/asebind/predict.py +163 -0
- asebind-0.1.0/src/asebind/resources/__init__.py +1 -0
- asebind-0.1.0/src/asebind/resources/ancestral.py +74 -0
- asebind-0.1.0/src/asebind/resources/conservation.py +77 -0
- asebind-0.1.0/src/asebind/resources/fetch.py +227 -0
- asebind-0.1.0/src/asebind/resources/genome.py +93 -0
- asebind-0.1.0/src/asebind/resources/registry.py +304 -0
- asebind-0.1.0/src/asebind/runrecord.py +108 -0
- asebind-0.1.0/src/asebind/tools.py +170 -0
- asebind-0.1.0/tests/__init__.py +0 -0
- asebind-0.1.0/tests/sim/__init__.py +0 -0
- asebind-0.1.0/tests/sim/simulate.py +213 -0
- asebind-0.1.0/tests/test_ancestral.py +63 -0
- asebind-0.1.0/tests/test_ase.py +87 -0
- asebind-0.1.0/tests/test_benchmark.py +228 -0
- asebind-0.1.0/tests/test_cli.py +82 -0
- asebind-0.1.0/tests/test_config.py +127 -0
- asebind-0.1.0/tests/test_conservation.py +52 -0
- asebind-0.1.0/tests/test_haplotype.py +98 -0
- asebind-0.1.0/tests/test_mpra_finemap.py +77 -0
- asebind-0.1.0/tests/test_phase3.py +378 -0
- asebind-0.1.0/tests/test_runrecord.py +39 -0
- asebind-0.1.0/tests/test_tools.py +117 -0
asebind-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.venv/
|
|
6
|
+
venv/
|
|
7
|
+
build/
|
|
8
|
+
dist/
|
|
9
|
+
.pytest_cache/
|
|
10
|
+
.ruff_cache/
|
|
11
|
+
.coverage
|
|
12
|
+
htmlcov/
|
|
13
|
+
|
|
14
|
+
# Never commit data, resources, models or secrets
|
|
15
|
+
.env
|
|
16
|
+
*.env
|
|
17
|
+
/data/
|
|
18
|
+
/resources/
|
|
19
|
+
/results/
|
|
20
|
+
/work/
|
|
21
|
+
*.keras
|
|
22
|
+
*.h5
|
|
23
|
+
*.pickle
|
|
24
|
+
*.pkl
|
|
25
|
+
*.tar.gz
|
|
26
|
+
*.bam
|
|
27
|
+
*.bai
|
|
28
|
+
*.cram
|
|
29
|
+
*.fastq*
|
|
30
|
+
*.fq*
|
|
31
|
+
*.vcf.gz
|
|
32
|
+
*.fa
|
|
33
|
+
*.fa.gz
|
|
34
|
+
*.fasta
|
|
35
|
+
*.fai
|
|
36
|
+
Escores*.txt
|
|
37
|
+
/asebind.yaml
|
|
38
|
+
|
|
39
|
+
# Local tool state
|
|
40
|
+
.sciweave/objects/
|
|
41
|
+
brainny-out/
|
asebind-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The ASEbind authors
|
|
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.
|
asebind-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: asebind
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Predict variant effects on transcription-factor binding and test them against allele-specific expression
|
|
5
|
+
Project-URL: Homepage, https://github.com/AlsammanAlsamman/ASEbind
|
|
6
|
+
Project-URL: Repository, https://github.com/AlsammanAlsamman/ASEbind
|
|
7
|
+
Project-URL: Documentation, https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/ASEbind_tutorial.pdf
|
|
8
|
+
Project-URL: Issues, https://github.com/AlsammanAlsamman/ASEbind/issues
|
|
9
|
+
Author: The ASEbind authors
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: click>=8.1
|
|
19
|
+
Requires-Dist: joblib>=1.2
|
|
20
|
+
Requires-Dist: numpy>=1.24
|
|
21
|
+
Requires-Dist: openpyxl>=3.1
|
|
22
|
+
Requires-Dist: pandas>=2.0
|
|
23
|
+
Requires-Dist: pydantic>=2.5
|
|
24
|
+
Requires-Dist: pyyaml>=6.0
|
|
25
|
+
Requires-Dist: scikit-learn>=1.3
|
|
26
|
+
Provides-Extra: cnn
|
|
27
|
+
Requires-Dist: tensorflow-cpu>=2.13; (platform_system != 'Darwin') and extra == 'cnn'
|
|
28
|
+
Requires-Dist: tensorflow>=2.13; (platform_system == 'Darwin') and extra == 'cnn'
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=7.4; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
32
|
+
Provides-Extra: evolution
|
|
33
|
+
Requires-Dist: pybigwig>=0.3.22; extra == 'evolution'
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
<p align="center">
|
|
37
|
+
<img src="https://raw.githubusercontent.com/AlsammanAlsamman/ASEbind/main/docs/assets/asebind_logo_rounded.png" alt="ASEbind" width="280">
|
|
38
|
+
</p>
|
|
39
|
+
|
|
40
|
+
<p align="center">
|
|
41
|
+
<b>Does this variant change transcription-factor binding?</b><br>
|
|
42
|
+
Calibrated probabilities of gain, loss or no change for every SNP × TF,
|
|
43
|
+
learned from allele-specific binding measured in ChIP-seq.
|
|
44
|
+
</p>
|
|
45
|
+
|
|
46
|
+
<p align="center">
|
|
47
|
+
<img src="https://img.shields.io/badge/python-%E2%89%A53.10-2a78d6" alt="Python 3.10+">
|
|
48
|
+
<img src="https://img.shields.io/badge/licence-MIT-d9d8d3" alt="MIT licence">
|
|
49
|
+
<a href="https://pypi.org/project/asebind/">
|
|
50
|
+
<img src="https://img.shields.io/pypi/v/asebind?color=2a78d6" alt="PyPI"></a>
|
|
51
|
+
<img src="https://img.shields.io/badge/TFs-275-e34948" alt="275 TFs">
|
|
52
|
+
</p>
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Why ASEbind
|
|
57
|
+
|
|
58
|
+
- **Learned from measurements, not motif scores alone.** 612,864 SNP × TF observations from
|
|
59
|
+
[ADASTRA](https://adastra.autosome.org) allele-specific ChIP-seq: 75,584 gains, 124,798 losses and
|
|
60
|
+
412,482 measured *no-change* sites.
|
|
61
|
+
- **275 TFs**, 65 of them with protein-binding-microarray evidence as well as a motif.
|
|
62
|
+
- **Probabilities you can trust as probabilities.** Calibration error 0.028, against
|
|
63
|
+
0.217 for plain gradient boosting.
|
|
64
|
+
- **Consistent by construction.** Swapping the two alleles swaps gain and loss exactly.
|
|
65
|
+
- **Careful with input.** Genome build check, swapped and minus-strand alleles fixed and flagged, A/T and C/G SNPs
|
|
66
|
+
flagged.
|
|
67
|
+
|
|
68
|
+
## How it works
|
|
69
|
+
|
|
70
|
+
<p align="center">
|
|
71
|
+
<img src="https://raw.githubusercontent.com/AlsammanAlsamman/ASEbind/main/docs/assets/how_it_works.svg" alt="How ASEbind works" width="100%">
|
|
72
|
+
</p>
|
|
73
|
+
|
|
74
|
+
Steps 1-6 run on your variants. The model in step 5 was trained once on measured allele-specific
|
|
75
|
+
binding and tested on SNPs, chromosomes and TFs it had not seen.
|
|
76
|
+
|
|
77
|
+
## Quick start
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pip install asebind
|
|
81
|
+
asebind fetch asebind_model hocomoco_v12 jaspar2024 hg19 # model, motifs and genome (~1 GB, once)
|
|
82
|
+
asebind predict my_variants.vcf --out results # P(gain/loss/none) per variant x TF
|
|
83
|
+
|
|
84
|
+
# the worked example below needs the repository's example files
|
|
85
|
+
git clone https://github.com/AlsammanAlsamman/ASEbind.git && cd ASEbind
|
|
86
|
+
asebind --config examples/quickstart/asebind.yaml predict # -> results/quickstart/
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Five well-studied regulatory SNPs, scored with no tuning (`examples/quickstart/expected/`; top calls, P in brackets):
|
|
90
|
+
|
|
91
|
+
| SNP | Gene | What is known | ASEbind |
|
|
92
|
+
|---|---|---|---|
|
|
93
|
+
| `rs12740374` | SORT1 | the T allele creates a C/EBP site (Musunuru et al. 2010) | CEBPA gained (0.69), CEBPB gained (0.68), CEBPG gained (0.57) |
|
|
94
|
+
| `rs6983267` | MYC 8q24 | the G allele binds TCF7L2 more strongly (Pomerantz et al. 2009) | LEF1 gained (0.65), TCF7 gained (0.62), PRDM1 lost (0.56) |
|
|
95
|
+
| `rs1421085` | FTO | the C allele disrupts an ARID5B motif (Claussnitzer et al. 2015) | ONECUT2 lost (0.66) |
|
|
96
|
+
| `rs4784227` | TOX3 | the T allele increases FOXA1 binding (Cowper-Sal-lari et al. 2012) | MAFG gained (0.51) |
|
|
97
|
+
| `rs10993994` | MSMB | the T allele lowers promoter activity and CREB binding (Lou et al. 2009) | CREB1 lost (0.76), ATF1 lost (0.63), CREM lost (0.53) |
|
|
98
|
+
|
|
99
|
+
A step-by-step guide with the output explained is in **[docs/ASEbind_tutorial.pdf](https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/ASEbind_tutorial.pdf)**.
|
|
100
|
+
|
|
101
|
+
## How accurate is it
|
|
102
|
+
|
|
103
|
+
Held out from training (5 folds; `benchmarks/PHASE3_REPORT.md`):
|
|
104
|
+
|
|
105
|
+
| Held out | Gain vs loss AUC | Macro AUC (3 classes) | Calibration error | Calibration error, plain boosting |
|
|
106
|
+
|---|---|---|---|---|
|
|
107
|
+
| unseen SNPs | 0.63 | 0.59 | 0.028 | 0.217 |
|
|
108
|
+
| unseen chromosomes | 0.63 | 0.59 | 0.029 | 0.218 |
|
|
109
|
+
| unseen TFs | 0.63 | 0.58 | 0.030 | 0.213 |
|
|
110
|
+
|
|
111
|
+
Telling gain from loss from sequence alone is hard, and the AUCs are modest. Use the probabilities, not only the
|
|
112
|
+
calls.
|
|
113
|
+
|
|
114
|
+
## What it does not do
|
|
115
|
+
|
|
116
|
+
Tested genome-wide and reported as found (`benchmarks/validation_v1/`):
|
|
117
|
+
|
|
118
|
+
- **Predict which copy of a gene is more expressed.** Summed over the variants on each copy, binding changes did
|
|
119
|
+
not explain Geuvadis allele-specific expression beyond the eQTL itself. Where the lead eQTL could not explain it,
|
|
120
|
+
direction agreement was 52% (95% CI 47%-57%). The positive
|
|
121
|
+
control, the lead eQTL allele, agreed 85%.
|
|
122
|
+
- **Improve fine-mapping as a prior.** No gain in ranking MPRA-validated variants on held-out chromosomes (AUPRC
|
|
123
|
+
difference -0.003, 95% CI -0.0285 to 0.0199). On its own, the score separates MPRA emVars from other active
|
|
124
|
+
variants with AUC 0.54.
|
|
125
|
+
|
|
126
|
+
## More
|
|
127
|
+
|
|
128
|
+
| | |
|
|
129
|
+
|---|---|
|
|
130
|
+
| [Tutorial (PDF)](https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/ASEbind_tutorial.pdf) | install, predict, read the output, ASE test |
|
|
131
|
+
| [docs/CONFIG.md](https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/CONFIG.md) | configuration: YAML, command line, environment, HPC modules |
|
|
132
|
+
| [benchmarks/PHASE3_REPORT.md](https://github.com/AlsammanAlsamman/ASEbind/blob/main/benchmarks/PHASE3_REPORT.md) | the model on ADASTRA, every split |
|
|
133
|
+
| [scripts/hpc/](https://github.com/AlsammanAlsamman/ASEbind/blob/main/scripts/hpc/README.md) | retrain or re-run the validations on a SLURM cluster |
|
|
134
|
+
|
|
135
|
+
**Licence:** MIT. The model was trained on ADASTRA, HOCOMOCO v12, JASPAR 2024 and CIS-BP data; please cite those
|
|
136
|
+
resources too.
|
|
137
|
+
|
|
138
|
+
<sub>This README is generated by `benchmarks/make_readme.py`: every number above is read from a result file.</sub>
|
asebind-0.1.0/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/AlsammanAlsamman/ASEbind/main/docs/assets/asebind_logo_rounded.png" alt="ASEbind" width="280">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<b>Does this variant change transcription-factor binding?</b><br>
|
|
7
|
+
Calibrated probabilities of gain, loss or no change for every SNP × TF,
|
|
8
|
+
learned from allele-specific binding measured in ChIP-seq.
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<img src="https://img.shields.io/badge/python-%E2%89%A53.10-2a78d6" alt="Python 3.10+">
|
|
13
|
+
<img src="https://img.shields.io/badge/licence-MIT-d9d8d3" alt="MIT licence">
|
|
14
|
+
<a href="https://pypi.org/project/asebind/">
|
|
15
|
+
<img src="https://img.shields.io/pypi/v/asebind?color=2a78d6" alt="PyPI"></a>
|
|
16
|
+
<img src="https://img.shields.io/badge/TFs-275-e34948" alt="275 TFs">
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Why ASEbind
|
|
22
|
+
|
|
23
|
+
- **Learned from measurements, not motif scores alone.** 612,864 SNP × TF observations from
|
|
24
|
+
[ADASTRA](https://adastra.autosome.org) allele-specific ChIP-seq: 75,584 gains, 124,798 losses and
|
|
25
|
+
412,482 measured *no-change* sites.
|
|
26
|
+
- **275 TFs**, 65 of them with protein-binding-microarray evidence as well as a motif.
|
|
27
|
+
- **Probabilities you can trust as probabilities.** Calibration error 0.028, against
|
|
28
|
+
0.217 for plain gradient boosting.
|
|
29
|
+
- **Consistent by construction.** Swapping the two alleles swaps gain and loss exactly.
|
|
30
|
+
- **Careful with input.** Genome build check, swapped and minus-strand alleles fixed and flagged, A/T and C/G SNPs
|
|
31
|
+
flagged.
|
|
32
|
+
|
|
33
|
+
## How it works
|
|
34
|
+
|
|
35
|
+
<p align="center">
|
|
36
|
+
<img src="https://raw.githubusercontent.com/AlsammanAlsamman/ASEbind/main/docs/assets/how_it_works.svg" alt="How ASEbind works" width="100%">
|
|
37
|
+
</p>
|
|
38
|
+
|
|
39
|
+
Steps 1-6 run on your variants. The model in step 5 was trained once on measured allele-specific
|
|
40
|
+
binding and tested on SNPs, chromosomes and TFs it had not seen.
|
|
41
|
+
|
|
42
|
+
## Quick start
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install asebind
|
|
46
|
+
asebind fetch asebind_model hocomoco_v12 jaspar2024 hg19 # model, motifs and genome (~1 GB, once)
|
|
47
|
+
asebind predict my_variants.vcf --out results # P(gain/loss/none) per variant x TF
|
|
48
|
+
|
|
49
|
+
# the worked example below needs the repository's example files
|
|
50
|
+
git clone https://github.com/AlsammanAlsamman/ASEbind.git && cd ASEbind
|
|
51
|
+
asebind --config examples/quickstart/asebind.yaml predict # -> results/quickstart/
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Five well-studied regulatory SNPs, scored with no tuning (`examples/quickstart/expected/`; top calls, P in brackets):
|
|
55
|
+
|
|
56
|
+
| SNP | Gene | What is known | ASEbind |
|
|
57
|
+
|---|---|---|---|
|
|
58
|
+
| `rs12740374` | SORT1 | the T allele creates a C/EBP site (Musunuru et al. 2010) | CEBPA gained (0.69), CEBPB gained (0.68), CEBPG gained (0.57) |
|
|
59
|
+
| `rs6983267` | MYC 8q24 | the G allele binds TCF7L2 more strongly (Pomerantz et al. 2009) | LEF1 gained (0.65), TCF7 gained (0.62), PRDM1 lost (0.56) |
|
|
60
|
+
| `rs1421085` | FTO | the C allele disrupts an ARID5B motif (Claussnitzer et al. 2015) | ONECUT2 lost (0.66) |
|
|
61
|
+
| `rs4784227` | TOX3 | the T allele increases FOXA1 binding (Cowper-Sal-lari et al. 2012) | MAFG gained (0.51) |
|
|
62
|
+
| `rs10993994` | MSMB | the T allele lowers promoter activity and CREB binding (Lou et al. 2009) | CREB1 lost (0.76), ATF1 lost (0.63), CREM lost (0.53) |
|
|
63
|
+
|
|
64
|
+
A step-by-step guide with the output explained is in **[docs/ASEbind_tutorial.pdf](https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/ASEbind_tutorial.pdf)**.
|
|
65
|
+
|
|
66
|
+
## How accurate is it
|
|
67
|
+
|
|
68
|
+
Held out from training (5 folds; `benchmarks/PHASE3_REPORT.md`):
|
|
69
|
+
|
|
70
|
+
| Held out | Gain vs loss AUC | Macro AUC (3 classes) | Calibration error | Calibration error, plain boosting |
|
|
71
|
+
|---|---|---|---|---|
|
|
72
|
+
| unseen SNPs | 0.63 | 0.59 | 0.028 | 0.217 |
|
|
73
|
+
| unseen chromosomes | 0.63 | 0.59 | 0.029 | 0.218 |
|
|
74
|
+
| unseen TFs | 0.63 | 0.58 | 0.030 | 0.213 |
|
|
75
|
+
|
|
76
|
+
Telling gain from loss from sequence alone is hard, and the AUCs are modest. Use the probabilities, not only the
|
|
77
|
+
calls.
|
|
78
|
+
|
|
79
|
+
## What it does not do
|
|
80
|
+
|
|
81
|
+
Tested genome-wide and reported as found (`benchmarks/validation_v1/`):
|
|
82
|
+
|
|
83
|
+
- **Predict which copy of a gene is more expressed.** Summed over the variants on each copy, binding changes did
|
|
84
|
+
not explain Geuvadis allele-specific expression beyond the eQTL itself. Where the lead eQTL could not explain it,
|
|
85
|
+
direction agreement was 52% (95% CI 47%-57%). The positive
|
|
86
|
+
control, the lead eQTL allele, agreed 85%.
|
|
87
|
+
- **Improve fine-mapping as a prior.** No gain in ranking MPRA-validated variants on held-out chromosomes (AUPRC
|
|
88
|
+
difference -0.003, 95% CI -0.0285 to 0.0199). On its own, the score separates MPRA emVars from other active
|
|
89
|
+
variants with AUC 0.54.
|
|
90
|
+
|
|
91
|
+
## More
|
|
92
|
+
|
|
93
|
+
| | |
|
|
94
|
+
|---|---|
|
|
95
|
+
| [Tutorial (PDF)](https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/ASEbind_tutorial.pdf) | install, predict, read the output, ASE test |
|
|
96
|
+
| [docs/CONFIG.md](https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/CONFIG.md) | configuration: YAML, command line, environment, HPC modules |
|
|
97
|
+
| [benchmarks/PHASE3_REPORT.md](https://github.com/AlsammanAlsamman/ASEbind/blob/main/benchmarks/PHASE3_REPORT.md) | the model on ADASTRA, every split |
|
|
98
|
+
| [scripts/hpc/](https://github.com/AlsammanAlsamman/ASEbind/blob/main/scripts/hpc/README.md) | retrain or re-run the validations on a SLURM cluster |
|
|
99
|
+
|
|
100
|
+
**Licence:** MIT. The model was trained on ADASTRA, HOCOMOCO v12, JASPAR 2024 and CIS-BP data; please cite those
|
|
101
|
+
resources too.
|
|
102
|
+
|
|
103
|
+
<sub>This README is generated by `benchmarks/make_readme.py`: every number above is read from a result file.</sub>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.21"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "asebind"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Predict variant effects on transcription-factor binding and test them against allele-specific expression"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
authors = [{ name = "The ASEbind authors" }]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3.10",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Bio-Informatics",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"click>=8.1",
|
|
23
|
+
"pydantic>=2.5",
|
|
24
|
+
"PyYAML>=6.0",
|
|
25
|
+
"numpy>=1.24",
|
|
26
|
+
"pandas>=2.0",
|
|
27
|
+
"scikit-learn>=1.3",
|
|
28
|
+
"openpyxl>=3.1",
|
|
29
|
+
"joblib>=1.2",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/AlsammanAlsamman/ASEbind"
|
|
34
|
+
Repository = "https://github.com/AlsammanAlsamman/ASEbind"
|
|
35
|
+
Documentation = "https://github.com/AlsammanAlsamman/ASEbind/blob/main/docs/ASEbind_tutorial.pdf"
|
|
36
|
+
Issues = "https://github.com/AlsammanAlsamman/ASEbind/issues"
|
|
37
|
+
|
|
38
|
+
[project.optional-dependencies]
|
|
39
|
+
dev = ["pytest>=7.4", "ruff>=0.4"]
|
|
40
|
+
evolution = ["pyBigWig>=0.3.22"]
|
|
41
|
+
cnn = ["tensorflow-cpu>=2.13; platform_system != 'Darwin'", "tensorflow>=2.13; platform_system == 'Darwin'"]
|
|
42
|
+
|
|
43
|
+
[project.scripts]
|
|
44
|
+
asebind = "asebind.cli:main"
|
|
45
|
+
|
|
46
|
+
[tool.hatch.version]
|
|
47
|
+
path = "src/asebind/__init__.py"
|
|
48
|
+
|
|
49
|
+
[tool.hatch.build.targets.wheel]
|
|
50
|
+
packages = ["src/asebind"]
|
|
51
|
+
|
|
52
|
+
# explicit list: the working folder holds local-only files (.git/info/exclude) that hatchling would not skip
|
|
53
|
+
[tool.hatch.build.targets.sdist]
|
|
54
|
+
only-include = ["src/asebind", "tests", "README.md", "LICENSE", "pyproject.toml"]
|
|
55
|
+
|
|
56
|
+
[tool.pytest.ini_options]
|
|
57
|
+
testpaths = ["tests"]
|
|
58
|
+
addopts = "-ra"
|
|
59
|
+
pythonpath = ["."]
|
|
60
|
+
|
|
61
|
+
[tool.ruff]
|
|
62
|
+
line-length = 110
|
|
63
|
+
target-version = "py310"
|
|
64
|
+
src = ["src", "tests"]
|
|
65
|
+
|
|
66
|
+
[tool.ruff.lint]
|
|
67
|
+
select = ["E", "F", "W", "I", "B", "UP", "SIM", "RUF"]
|
|
68
|
+
|
|
69
|
+
[tool.ruff.lint.pycodestyle]
|
|
70
|
+
max-line-length = 120
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Allele-specific expression tests (Phase 5, I-21, I-22)."""
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"""Beta-binomial test of allelic imbalance between the two copies of a gene (cases vs controls).
|
|
2
|
+
|
|
3
|
+
Data: per person, ``k`` reads on the copy of interest (e.g. the risk copy) out of ``n`` reads on both copies,
|
|
4
|
+
summed over that person's usable heterozygous SNVs. Cases (heterozygous for the variant) have mean fraction p;
|
|
5
|
+
controls (homozygous for it, with heterozygous SNVs elsewhere in the gene) have p = 0.5 and pin the
|
|
6
|
+
overdispersion. One rho is shared by cases and controls of a group.
|
|
7
|
+
|
|
8
|
+
logL(k, n | p, rho) = ln C(n, k) + ln B(k + a, n - k + b) - ln B(a, b),
|
|
9
|
+
a = p (1 - rho) / rho, b = (1 - p)(1 - rho) / rho
|
|
10
|
+
|
|
11
|
+
p-hat maximises the profile likelihood (rho refitted at every p, on a grid then refined); the test of 50:50 is a
|
|
12
|
+
likelihood-ratio test with 1 df; the 95% CI is the profile interval (logL >= max - 1.92).
|
|
13
|
+
|
|
14
|
+
Reference-allele bias (I-22): at the SNV level a per-observation null fraction ``q`` (the share of reads expected
|
|
15
|
+
on the copy of interest when there is no imbalance, e.g. estimated from controls' REF share at that SNV) enters
|
|
16
|
+
as an offset: mean = expit(logit(p) + logit(q)).
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from dataclasses import dataclass
|
|
22
|
+
|
|
23
|
+
import numpy as np
|
|
24
|
+
import pandas as pd
|
|
25
|
+
from scipy import stats
|
|
26
|
+
from scipy.optimize import minimize_scalar
|
|
27
|
+
from scipy.special import betaln, expit, gammaln, logit
|
|
28
|
+
|
|
29
|
+
LOGIT_RHO_BOUNDS = (-12.0, 4.0)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def loglik(k: np.ndarray, n: np.ndarray, mean: np.ndarray | float, rho: float) -> float:
|
|
33
|
+
k, n = np.asarray(k, float), np.asarray(n, float)
|
|
34
|
+
mean = np.clip(np.broadcast_to(mean, k.shape), 1e-9, 1 - 1e-9)
|
|
35
|
+
s = (1 - rho) / rho
|
|
36
|
+
a, b = mean * s, (1 - mean) * s
|
|
37
|
+
lc = gammaln(n + 1) - gammaln(k + 1) - gammaln(n - k + 1)
|
|
38
|
+
return float(np.sum(lc + betaln(k + a, n - k + b) - betaln(a, b)))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class Data:
|
|
43
|
+
k_case: np.ndarray
|
|
44
|
+
n_case: np.ndarray
|
|
45
|
+
k_ctrl: np.ndarray
|
|
46
|
+
n_ctrl: np.ndarray
|
|
47
|
+
q_case: np.ndarray | None = None # null fraction per case observation (reference-bias offset)
|
|
48
|
+
q_ctrl: np.ndarray | None = None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _means(d: Data, p: float) -> tuple[np.ndarray, np.ndarray]:
|
|
52
|
+
mc = (
|
|
53
|
+
np.full(len(d.k_case), p)
|
|
54
|
+
if d.q_case is None
|
|
55
|
+
else expit(logit(p) + logit(np.clip(d.q_case, 1e-06, 1 - 1e-06)))
|
|
56
|
+
)
|
|
57
|
+
mk = np.full(len(d.k_ctrl), 0.5) if d.q_ctrl is None else np.clip(d.q_ctrl, 1e-6, 1 - 1e-6)
|
|
58
|
+
return mc, mk
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def profile(d: Data, p: float, rho: float | None = None) -> tuple[float, float]:
|
|
62
|
+
"""(max over rho of logL at this p, that rho); with ``rho`` given, logL at that rho."""
|
|
63
|
+
mc, mk = _means(d, p)
|
|
64
|
+
|
|
65
|
+
def nll(lr: float) -> float:
|
|
66
|
+
r = float(expit(lr))
|
|
67
|
+
return -(loglik(d.k_case, d.n_case, mc, r) + loglik(d.k_ctrl, d.n_ctrl, mk, r))
|
|
68
|
+
|
|
69
|
+
if rho is not None:
|
|
70
|
+
return -nll(float(logit(rho))), rho
|
|
71
|
+
res = minimize_scalar(nll, bounds=LOGIT_RHO_BOUNDS, method="bounded", options={"xatol": 1e-6})
|
|
72
|
+
return -float(res.fun), float(expit(res.x))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass
|
|
76
|
+
class Fit:
|
|
77
|
+
p: float
|
|
78
|
+
rho: float
|
|
79
|
+
rho0: float
|
|
80
|
+
loglik: float
|
|
81
|
+
loglik0: float
|
|
82
|
+
chi2: float
|
|
83
|
+
pvalue: float
|
|
84
|
+
ci_low: float
|
|
85
|
+
ci_high: float
|
|
86
|
+
grid: pd.DataFrame
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def fold(self) -> float:
|
|
90
|
+
return self.p / (1 - self.p)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def fit(d: Data, step: float = 0.005, lo: float = 0.02, hi: float = 0.98, rho: float | None = None) -> Fit:
|
|
94
|
+
grid = np.round(np.arange(lo, hi + step / 2, step), 6)
|
|
95
|
+
prof = [profile(d, float(p), rho) for p in grid]
|
|
96
|
+
ll = np.array([x[0] for x in prof])
|
|
97
|
+
i = int(ll.argmax())
|
|
98
|
+
a, b = grid[max(i - 1, 0)], grid[min(i + 1, len(grid) - 1)]
|
|
99
|
+
res = minimize_scalar(
|
|
100
|
+
lambda p: -profile(d, p, rho)[0], bounds=(a, b), method="bounded", options={"xatol": 1e-7}
|
|
101
|
+
)
|
|
102
|
+
p_hat = float(res.x) if -res.fun >= ll[i] else float(grid[i])
|
|
103
|
+
ll_hat, rho_hat = profile(d, p_hat, rho)
|
|
104
|
+
ll0, rho0 = profile(d, 0.5, rho)
|
|
105
|
+
chi2 = max(0.0, 2 * (ll_hat - ll0))
|
|
106
|
+
inside = grid[ll >= ll_hat - stats.chi2.ppf(0.95, 1) / 2]
|
|
107
|
+
return Fit(
|
|
108
|
+
p=p_hat,
|
|
109
|
+
rho=rho_hat,
|
|
110
|
+
rho0=rho0,
|
|
111
|
+
loglik=ll_hat,
|
|
112
|
+
loglik0=ll0,
|
|
113
|
+
chi2=chi2,
|
|
114
|
+
pvalue=float(stats.chi2.sf(chi2, 1)),
|
|
115
|
+
ci_low=float(inside.min()),
|
|
116
|
+
ci_high=float(inside.max()),
|
|
117
|
+
grid=pd.DataFrame({"p": grid, "loglik": ll, "rho": [x[1] for x in prof]}),
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def per_person(counts: pd.DataFrame, min_snv_reads: int = 5, min_person_reads: int = 10) -> pd.DataFrame:
|
|
122
|
+
"""Sum a long table (sample, k, n [, population, role]) over SNVs: SNVs with < ``min_snv_reads`` reads and
|
|
123
|
+
people with < ``min_person_reads`` reads are dropped."""
|
|
124
|
+
c = counts[counts["n"] >= min_snv_reads]
|
|
125
|
+
keys = [x for x in ("sample", "population", "role") if x in c.columns]
|
|
126
|
+
g = c.groupby(keys, as_index=False)[["k", "n"]].sum()
|
|
127
|
+
g = g[g["n"] >= min_person_reads].reset_index(drop=True)
|
|
128
|
+
g["fraction"] = g["k"] / g["n"]
|
|
129
|
+
g["log2_ratio"] = np.log2((g["k"] + 0.5) / (g["n"] - g["k"] + 0.5))
|
|
130
|
+
return g
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_group(people: pd.DataFrame, rho: float | None = None, step: float = 0.005) -> dict:
|
|
134
|
+
"""Beta-binomial LRT plus the direction (sign) test and Wilcoxon on the cases' log2 ratios."""
|
|
135
|
+
case = people[people["role"] == "case"]
|
|
136
|
+
ctrl = people[people["role"] == "control"]
|
|
137
|
+
d = Data(case["k"].to_numpy(), case["n"].to_numpy(), ctrl["k"].to_numpy(), ctrl["n"].to_numpy())
|
|
138
|
+
f = fit(d, step=step, rho=rho)
|
|
139
|
+
higher = int((case["k"] > case["n"] - case["k"]).sum())
|
|
140
|
+
out = {
|
|
141
|
+
"cases": len(case),
|
|
142
|
+
"controls": len(ctrl),
|
|
143
|
+
"case_reads_copy": int(case["k"].sum()),
|
|
144
|
+
"case_reads_other": int((case["n"] - case["k"]).sum()),
|
|
145
|
+
"pooled_share": float(case["k"].sum() / case["n"].sum()) if len(case) else np.nan,
|
|
146
|
+
"p_hat": f.p,
|
|
147
|
+
"rho_hat": f.rho,
|
|
148
|
+
"rho0": f.rho0,
|
|
149
|
+
"chi2": f.chi2,
|
|
150
|
+
"p_value": f.pvalue,
|
|
151
|
+
"ci_low": f.ci_low,
|
|
152
|
+
"ci_high": f.ci_high,
|
|
153
|
+
"fold": f.fold,
|
|
154
|
+
"cases_copy_higher": higher,
|
|
155
|
+
"p_direction": float(stats.binomtest(higher, len(case), 0.5).pvalue) if len(case) else np.nan,
|
|
156
|
+
}
|
|
157
|
+
lr = (
|
|
158
|
+
case["log2_ratio"].to_numpy()
|
|
159
|
+
if "log2_ratio" in case
|
|
160
|
+
else np.log2((case["k"] + 0.5) / (case["n"] - case["k"] + 0.5))
|
|
161
|
+
)
|
|
162
|
+
out["p_wilcoxon"] = float(stats.wilcoxon(lr).pvalue) if len(lr) > 1 and np.any(lr != 0) else np.nan
|
|
163
|
+
return out
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def run(counts: pd.DataFrame, by: str | None = "population", **kw) -> pd.DataFrame:
|
|
167
|
+
"""All people together (``ALL``), then each value of ``by`` (each group with its own fitted rho)."""
|
|
168
|
+
people = per_person(counts, kw.pop("min_snv_reads", 5), kw.pop("min_person_reads", 10))
|
|
169
|
+
rows = [{"group": "ALL", **test_group(people, **kw)}]
|
|
170
|
+
if by and by in people.columns:
|
|
171
|
+
for g, d in people.groupby(by):
|
|
172
|
+
rows.append({"group": str(g), **test_group(d, **kw)})
|
|
173
|
+
return pd.DataFrame(rows)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Honest re-test of SNPeBoT-style binding-change models (benchmarks/REPORT.md)."""
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""Read the SNPeBoT processed ASB table and its sequence file into named columns.
|
|
2
|
+
|
|
3
|
+
Layout of ``Processed_ASB_data.csv`` (no header; Data_and_Training/README.txt, checked on the file 2026-11-30):
|
|
4
|
+
columns 1-15 windowed E-scores of the reference allele, 16-30 of the alternate allele; 31 motif-change indicator;
|
|
5
|
+
32-39 / 40-47 non-windowed E-scores ref / alt (the eight 8-mers over the SNP); 48 label (gained / lost / none);
|
|
6
|
+
49-50 best FIMO p-value ref / alt; 51 ID ``chrom-pos-ref-alt-TF``.
|
|
7
|
+
|
|
8
|
+
The file is the authors' data: read it for analysis, never copy it into this repository.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
import pandas as pd
|
|
17
|
+
|
|
18
|
+
W_REF = [f"w_ref_{i}" for i in range(1, 16)]
|
|
19
|
+
W_ALT = [f"w_alt_{i}" for i in range(1, 16)]
|
|
20
|
+
NW_REF = [f"nw_ref_{i}" for i in range(1, 9)]
|
|
21
|
+
NW_ALT = [f"nw_alt_{i}" for i in range(1, 9)]
|
|
22
|
+
COLUMNS = [*W_REF, *W_ALT, "motif_change", *NW_REF, *NW_ALT, "label", "p_ref", "p_alt", "id"]
|
|
23
|
+
LABELS = ("gained", "lost", "none")
|
|
24
|
+
SWAP_LABEL = {"gained": "lost", "lost": "gained", "none": "none"}
|
|
25
|
+
|
|
26
|
+
SEQ_COLUMNS = [
|
|
27
|
+
"chrom",
|
|
28
|
+
"pos",
|
|
29
|
+
"ref",
|
|
30
|
+
"alt",
|
|
31
|
+
"cell_line",
|
|
32
|
+
"tf_raw",
|
|
33
|
+
"p_ref_asb",
|
|
34
|
+
"p_alt_asb",
|
|
35
|
+
"flag",
|
|
36
|
+
"ratio",
|
|
37
|
+
"region",
|
|
38
|
+
"seq_ref",
|
|
39
|
+
"seq_alt",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def normalise_tf(name: str) -> str:
|
|
44
|
+
"""One spelling per TF (``cebpb`` and ``CEBPB`` are the same factor, I-1/I-9)."""
|
|
45
|
+
return name.strip().upper()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def read_processed(path: str | Path) -> pd.DataFrame:
|
|
49
|
+
df = pd.read_csv(path, header=None, names=COLUMNS, skipinitialspace=True, dtype={"label": str, "id": str})
|
|
50
|
+
if df.shape[1] != len(COLUMNS):
|
|
51
|
+
raise ValueError(f"{path}: expected {len(COLUMNS)} columns, got {df.shape[1]}")
|
|
52
|
+
df["label"] = df["label"].str.strip().str.lower()
|
|
53
|
+
bad = sorted(set(df["label"]) - set(LABELS))
|
|
54
|
+
if bad:
|
|
55
|
+
raise ValueError(f"{path}: unexpected labels {bad}")
|
|
56
|
+
parts = df["id"].str.strip().str.split("-", n=4, expand=True)
|
|
57
|
+
if parts.shape[1] != 5 or parts.isna().any().any():
|
|
58
|
+
raise ValueError(f"{path}: IDs must look like chrom-pos-ref-alt-TF")
|
|
59
|
+
df["id"] = df["id"].str.strip()
|
|
60
|
+
df["chrom"] = parts[0]
|
|
61
|
+
df["pos"] = parts[1].astype(int)
|
|
62
|
+
df["ref"] = parts[2].str.upper()
|
|
63
|
+
df["alt"] = parts[3].str.upper()
|
|
64
|
+
df["tf_raw"] = parts[4]
|
|
65
|
+
df["tf"] = df["tf_raw"].map(normalise_tf)
|
|
66
|
+
df["snp"] = df["chrom"] + ":" + df["pos"].astype(str) + ":" + df["ref"] + ":" + df["alt"]
|
|
67
|
+
return df
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def read_sequences(path: str | Path) -> pd.DataFrame:
|
|
71
|
+
seq = pd.read_csv(path, sep="\t", header=None, names=SEQ_COLUMNS, dtype=str)
|
|
72
|
+
seq["pos"] = seq["pos"].astype(int)
|
|
73
|
+
seq["tf"] = seq["tf_raw"].map(normalise_tf)
|
|
74
|
+
seq["cell_line"] = seq["cell_line"].str.strip().str.lower()
|
|
75
|
+
return seq
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def attach_cell_lines(df: pd.DataFrame, seq: pd.DataFrame) -> pd.DataFrame:
|
|
79
|
+
"""Add ``cell_line``: the single named cell line of the row's (SNP, TF), else ``unknown`` / ``multiple``.
|
|
80
|
+
|
|
81
|
+
``placeholder`` in the sequence file means no cell line was recorded.
|
|
82
|
+
"""
|
|
83
|
+
key = ["chrom", "pos", "ref", "alt", "tf"]
|
|
84
|
+
named = seq[seq["cell_line"] != "placeholder"].drop_duplicates([*key, "cell_line"])
|
|
85
|
+
per = (
|
|
86
|
+
named.groupby(key)["cell_line"].agg(lambda s: s.iloc[0] if len(s) == 1 else "multiple").reset_index()
|
|
87
|
+
)
|
|
88
|
+
out = df.drop(columns=["cell_line"], errors="ignore").merge(per, on=key, how="left")
|
|
89
|
+
out["cell_line"] = out["cell_line"].fillna("unknown")
|
|
90
|
+
return out
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def swap_alleles(df: pd.DataFrame) -> pd.DataFrame:
|
|
94
|
+
"""The same rows with reference and alternate exchanged (features, p-values and label)."""
|
|
95
|
+
out = df.copy()
|
|
96
|
+
for a, b in ((W_REF, W_ALT), (NW_REF, NW_ALT), (["p_ref"], ["p_alt"])):
|
|
97
|
+
out[a] = df[b].to_numpy()
|
|
98
|
+
out[b] = df[a].to_numpy()
|
|
99
|
+
out["label"] = df["label"].map(SWAP_LABEL)
|
|
100
|
+
if "ref" in df and "alt" in df:
|
|
101
|
+
out["ref"], out["alt"] = df["alt"].to_numpy(), df["ref"].to_numpy()
|
|
102
|
+
return out
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def feature_matrix(df: pd.DataFrame) -> np.ndarray:
|
|
106
|
+
"""All 47 numeric features in file order (used to find identical rows)."""
|
|
107
|
+
return df[[*W_REF, *W_ALT, "motif_change", *NW_REF, *NW_ALT]].to_numpy(dtype=float)
|