gReLU 1.0.1__py3-none-any.whl
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.
- gReLU-1.0.1.dist-info/LICENSE.txt +21 -0
- gReLU-1.0.1.dist-info/METADATA +106 -0
- gReLU-1.0.1.dist-info/RECORD +58 -0
- gReLU-1.0.1.dist-info/WHEEL +5 -0
- gReLU-1.0.1.dist-info/top_level.txt +1 -0
- grelu/__init__.py +16 -0
- grelu/data/__init__.py +0 -0
- grelu/data/augment.py +227 -0
- grelu/data/dataset.py +1013 -0
- grelu/data/preprocess.py +710 -0
- grelu/data/utils.py +63 -0
- grelu/design.py +287 -0
- grelu/interpret/__init__.py +0 -0
- grelu/interpret/motifs.py +319 -0
- grelu/interpret/score.py +390 -0
- grelu/io/__init__.py +136 -0
- grelu/io/bed.py +97 -0
- grelu/io/bigwig.py +98 -0
- grelu/io/fasta.py +48 -0
- grelu/io/genome.py +93 -0
- grelu/io/meme.py +115 -0
- grelu/lightning/__init__.py +1097 -0
- grelu/lightning/losses.py +77 -0
- grelu/lightning/metrics.py +146 -0
- grelu/model/__init__.py +0 -0
- grelu/model/blocks.py +761 -0
- grelu/model/heads.py +135 -0
- grelu/model/layers.py +399 -0
- grelu/model/models.py +692 -0
- grelu/model/position.py +80 -0
- grelu/model/trunks/__init__.py +338 -0
- grelu/model/trunks/borzoi.py +166 -0
- grelu/model/trunks/enformer.py +277 -0
- grelu/model/trunks/explainn.py +119 -0
- grelu/resources/__init__.py +138 -0
- grelu/resources/__pycache__/__init__.cpython-310.pyc +0 -0
- grelu/resources/blacklists/encode/hg19-blacklist.v2.bed +834 -0
- grelu/resources/blacklists/encode/hg38-blacklist.v2.bed +636 -0
- grelu/resources/blacklists/encode/mm10-blacklist.v2.bed +3435 -0
- grelu/resources/blacklists/mito_combined/hg19.full.blacklist.bed +902 -0
- grelu/resources/blacklists/mito_combined/hg19_mm10_c.full.blacklist.bed +1425 -0
- grelu/resources/blacklists/mito_combined/hg38.full.blacklist.bed +820 -0
- grelu/resources/blacklists/mito_combined/mm10.full.blacklist.bed +523 -0
- grelu/resources/blacklists/mito_combined/mm9.full.blacklist.bed +3415 -0
- grelu/resources/meme/JASPAR2022_CORE_non-redundant_pfms_meme.txt +32224 -0
- grelu/resources/meme/jvierstra_consensus_pwms.meme +10405 -0
- grelu/sequence/__init__.py +0 -0
- grelu/sequence/format.py +428 -0
- grelu/sequence/metrics.py +105 -0
- grelu/sequence/mutate.py +252 -0
- grelu/sequence/utils.py +452 -0
- grelu/transforms/__init__.py +0 -0
- grelu/transforms/label_transforms.py +50 -0
- grelu/transforms/prediction_transforms.py +248 -0
- grelu/transforms/seq_transforms.py +132 -0
- grelu/utils.py +153 -0
- grelu/variant.py +389 -0
- grelu/visualize.py +740 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Genentech, Inc.
|
|
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.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: gReLU
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: gReLU is a python library to train, interpret, and apply deep learning models to DNA sequences
|
|
5
|
+
Home-page: https://code.roche.com/gred-rb-ai-ml-regulatory-genomics/grelu
|
|
6
|
+
Author: lal.avantika,eraslan.gokcen
|
|
7
|
+
Author-email: lal.avantika@gene.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Documentation, https://code.roche.com/gred-rb-ai-ml-regulatory-genomics/grelu
|
|
10
|
+
Project-URL: Source, https://code.roche.com/gred-rb-ai-ml-regulatory-genomics/grelu
|
|
11
|
+
Platform: Linux
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown; charset=UTF-8
|
|
16
|
+
License-File: LICENSE.txt
|
|
17
|
+
Requires-Dist: importlib-metadata
|
|
18
|
+
Requires-Dist: importlib-resources
|
|
19
|
+
Requires-Dist: wandb >=0.14
|
|
20
|
+
Requires-Dist: numpy
|
|
21
|
+
Requires-Dist: pandas
|
|
22
|
+
Requires-Dist: seaborn
|
|
23
|
+
Requires-Dist: matplotlib
|
|
24
|
+
Requires-Dist: plotnine >=0.8
|
|
25
|
+
Requires-Dist: anndata >=0.8
|
|
26
|
+
Requires-Dist: scikit-learn
|
|
27
|
+
Requires-Dist: pymemesuite
|
|
28
|
+
Requires-Dist: torch >=2.0
|
|
29
|
+
Requires-Dist: pytorch-lightning >=2.0
|
|
30
|
+
Requires-Dist: modisco-lite >=2.2.1
|
|
31
|
+
Requires-Dist: biopython
|
|
32
|
+
Requires-Dist: enformer-pytorch
|
|
33
|
+
Requires-Dist: genomepy
|
|
34
|
+
Requires-Dist: bioframe >=0.4
|
|
35
|
+
Requires-Dist: captum ==0.5.0
|
|
36
|
+
Requires-Dist: bpnet-lite ==0.5.7
|
|
37
|
+
Requires-Dist: logomaker >=0.8
|
|
38
|
+
Requires-Dist: pyBigWig
|
|
39
|
+
Requires-Dist: ledidi
|
|
40
|
+
Requires-Dist: tangermeme
|
|
41
|
+
Requires-Dist: pygenomeviz <=0.4.4
|
|
42
|
+
Provides-Extra: testing
|
|
43
|
+
Requires-Dist: setuptools ; extra == 'testing'
|
|
44
|
+
Requires-Dist: pytest ; extra == 'testing'
|
|
45
|
+
Requires-Dist: pytest-cov ; extra == 'testing'
|
|
46
|
+
|
|
47
|
+
# gReLU
|
|
48
|
+
|
|
49
|
+
gReLU is a Python library to train, interpret, and apply deep learning models to DNA sequences. Code documentation is available [here](https://genentech.github.io/gReLU/).
|
|
50
|
+
|
|
51
|
+

|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
To install from source:
|
|
56
|
+
|
|
57
|
+
```shell
|
|
58
|
+
git clone https://github.com/Genentech/gReLU.git
|
|
59
|
+
cd gReLU
|
|
60
|
+
pip install .
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Contributing
|
|
64
|
+
|
|
65
|
+
This project uses [pre-commit](https://pre-commit.com/). Please make sure to install it before making any changes:
|
|
66
|
+
|
|
67
|
+
```shell
|
|
68
|
+
pip install pre-commit
|
|
69
|
+
cd gReLU
|
|
70
|
+
pre-commit install
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
It is a good idea to update the hooks to the latest version:
|
|
74
|
+
|
|
75
|
+
```shell
|
|
76
|
+
pre-commit autoupdate
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Additional requirements
|
|
80
|
+
|
|
81
|
+
If you want to use genome annotation features through the function `grelu.io.genome.read_gtf`, you will need to install the following UCSC utilities: `genePredToBed`, `genePredToGtf`, `bedToGenePred`, `gtfToGenePred`, `gff3ToGenePred`.
|
|
82
|
+
|
|
83
|
+
If you want to create bigWig files through the function `grelu.data.preprocess.make_insertion_bigwig`, you will need to install the following UCSC utilities: `bedGraphToBigWig`.
|
|
84
|
+
|
|
85
|
+
UCSC utilities can be installed from `http://hgdownload.cse.ucsc.edu/admin/exe/`, for example using the following commands:
|
|
86
|
+
|
|
87
|
+
```shell
|
|
88
|
+
rsync -aP rsync://hgdownload.soe.ucsc.edu/genome/admin/exe/linux.x86_64/bedGraphToBigWig /usr/bin/
|
|
89
|
+
rsync -aP rsync://hgdownload.soe.ucsc.edu/genome/admin/exe/linux.x86_64/genePredToBed /usr/bin/
|
|
90
|
+
rsync -aP rsync://hgdownload.soe.ucsc.edu/genome/admin/exe/linux.x86_64/genePredToGtf /usr/bin/
|
|
91
|
+
rsync -aP rsync://hgdownload.soe.ucsc.edu/genome/admin/exe/linux.x86_64/bedToGenePred /usr/bin/
|
|
92
|
+
rsync -aP rsync://hgdownload.soe.ucsc.edu/genome/admin/exe/linux.x86_64/gtfToGenePred /usr/bin/
|
|
93
|
+
rsync -aP rsync://hgdownload.soe.ucsc.edu/genome/admin/exe/linux.x86_64/gff3ToGenePred /usr/bin/
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
or via bioconda:
|
|
97
|
+
|
|
98
|
+
```shell
|
|
99
|
+
conda install -y \
|
|
100
|
+
bioconda::ucsc-bedgraphtobigwig \
|
|
101
|
+
bioconda::ucsc-genepredtobed \
|
|
102
|
+
bioconda::ucsc-genepredtogtf \
|
|
103
|
+
bioconda::ucsc-bedtogenepred \
|
|
104
|
+
bioconda::ucsc-gtftogenepred \
|
|
105
|
+
bioconda::ucsc-gff3togenepred
|
|
106
|
+
```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
grelu/__init__.py,sha256=pH5i4Fj1tbXLqLtTVIdoojiplZssQn0nnud8-HXodRE,577
|
|
2
|
+
grelu/design.py,sha256=UulKoTgYnUoRBe3Rf0rpAS_hUTEX5bqw2U1sHoGC0AA,9086
|
|
3
|
+
grelu/utils.py,sha256=4mspV5R1-P1TT4F6cjUwkKSqBkpBAV77pKz77jtA4PY,4505
|
|
4
|
+
grelu/variant.py,sha256=uuFI6WccxA_puLLtLg0R2v2-ZGaASDh5tiCZsysWeyU,12955
|
|
5
|
+
grelu/visualize.py,sha256=76tegK8ivTmJySsq0_gDMmRdxr_dg-WSN9IIHqKRmlg,23685
|
|
6
|
+
grelu/data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
grelu/data/augment.py,sha256=NdKlz3gNRhNl0JnCC7Gl4cR-vJKZbsRrZM2PoVyQEho,7630
|
|
8
|
+
grelu/data/dataset.py,sha256=IiXvAn77MXmUsun0qe08KVtFrnf3y_L80HIy5vDlBQ0,35158
|
|
9
|
+
grelu/data/preprocess.py,sha256=QmZHSja50sjFGlnFvLpNTAgyxC8LoHb1muWbOvUZZdk,22898
|
|
10
|
+
grelu/data/utils.py,sha256=Db1vYb-C8c7vknKnWj7-DDLXPyF_o7U6LeAjyhTfFbE,1905
|
|
11
|
+
grelu/interpret/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
grelu/interpret/motifs.py,sha256=mzaf9JAxPCCpyIXRIIypPYqk_chGL0JwqNx-3o0491k,10766
|
|
13
|
+
grelu/interpret/score.py,sha256=JT6exAaXeB38Yxs8fiPe2xWByzf_6aae6vuRhYo2_l4,13815
|
|
14
|
+
grelu/io/__init__.py,sha256=bp6yP62ViCaFy00JtDWccwbVNjpN9_oxrzIuwxWQT4U,4921
|
|
15
|
+
grelu/io/bed.py,sha256=_C-aTHNiaRYJo5QCbvXrchXa6-fgL_3CZ3p-0ngDSKU,3056
|
|
16
|
+
grelu/io/bigwig.py,sha256=HYChK3qalKCK-bl_902JppyQoPTM9ep0G_5x8Wph_DE,3065
|
|
17
|
+
grelu/io/fasta.py,sha256=8OKhwsQ9LNoZ2l6SSP6vMxe9dbMHqMZ32o9fru3FogA,1353
|
|
18
|
+
grelu/io/genome.py,sha256=Pci2z1kMtxleGUSqeFtQVRRHmZOlkDLVat8XuKjqrKo,2736
|
|
19
|
+
grelu/io/meme.py,sha256=b0YNdLHcRsAk7EBxFsBYpC_mbJ8VB7wG64QvKqp5U_c,3394
|
|
20
|
+
grelu/lightning/__init__.py,sha256=ydLU-ZgNUiRgWwJzri9f4DGhADzoUIDCn7-zbf_MRyE,38383
|
|
21
|
+
grelu/lightning/losses.py,sha256=NLht1LhnleP9gp893jQXSwreM9_4wP--vGSPg8UX5Ws,2174
|
|
22
|
+
grelu/lightning/metrics.py,sha256=bXi8hDHRAet-v_0X8RfDY1YgXeWkFQhqity1rnWaTGA,5271
|
|
23
|
+
grelu/model/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
|
+
grelu/model/blocks.py,sha256=EHchALlLKUltwk7L1ekC7D3ECjXTJvARMjtq0pXZSoc,22131
|
|
25
|
+
grelu/model/heads.py,sha256=vw9vnT1f9u5u5y_8cw0pE0Aa8CCAGB4oyuejWpcic6k,3843
|
|
26
|
+
grelu/model/layers.py,sha256=YOZ1e5C_KmwzysmK3a916uEtLRYPn05usHacvM4SypI,11516
|
|
27
|
+
grelu/model/models.py,sha256=9QYm7Ei-ysZlIeV-zhVQPUpsK_GChWKA2r9mtl2KK6Y,22922
|
|
28
|
+
grelu/model/position.py,sha256=3jOsYj8dZnkaMQCtr10d25awxtmD3UwDQhuqjfmCLuc,2310
|
|
29
|
+
grelu/model/trunks/__init__.py,sha256=RbKEKwbmbAlMDDtco3bIejXYXBgVxWjDORQdneKUjqc,10899
|
|
30
|
+
grelu/model/trunks/borzoi.py,sha256=fR9OhfYt6kQkrarzOUcOHhR0ktzAfY_vbbxwMIxN_4Q,4752
|
|
31
|
+
grelu/model/trunks/enformer.py,sha256=3DzlxVJZ2fG-ZAOoewbcS9ZGIN-C35sGu9_WT0EmSJo,8147
|
|
32
|
+
grelu/model/trunks/explainn.py,sha256=pnLNdj6NeK_2UDrqmbvF0DzwIo02OHtMsQ4k0dck-8c,3072
|
|
33
|
+
grelu/resources/__init__.py,sha256=2_P9g27sxjb9gXwjOr9vYQvfaD0It2RRkyNWsgn1uq8,4086
|
|
34
|
+
grelu/resources/__pycache__/__init__.cpython-310.pyc,sha256=WnUF1McC_BVkL1uRLf0NiLDbvvKpI3GHrVyE6bDHcZY,5116
|
|
35
|
+
grelu/resources/blacklists/encode/hg19-blacklist.v2.bed,sha256=H2FLnBw5oO-KehaJHf9BcarN-Pxig1tlRlWdR2Wift8,34914
|
|
36
|
+
grelu/resources/blacklists/encode/hg38-blacklist.v2.bed,sha256=McaTQt9Du8Gd2O8ohmEagVDLU-kPNB3hTTDnQsklFzc,26898
|
|
37
|
+
grelu/resources/blacklists/encode/mm10-blacklist.v2.bed,sha256=lji760vo2Z3fVvG3BwD2qTNs5_VNhwMr0mJGXs87-sc,144278
|
|
38
|
+
grelu/resources/blacklists/mito_combined/hg19.full.blacklist.bed,sha256=e4zmtfBQyapVfhqv7NyNzm5QjjK67ciC6xaBAjlKKTs,21537
|
|
39
|
+
grelu/resources/blacklists/mito_combined/hg19_mm10_c.full.blacklist.bed,sha256=CeEf4IaFJs5nPFMVacVPtH07HpFYB9OIJgHuYSoltQ4,36721
|
|
40
|
+
grelu/resources/blacklists/mito_combined/hg38.full.blacklist.bed,sha256=YEIzJjIlWiMG5h9LnEbWORyxDwepUS33nQaDBf5DHTw,19642
|
|
41
|
+
grelu/resources/blacklists/mito_combined/mm10.full.blacklist.bed,sha256=h5ddCbAQPpLR2xcaaNvYbAfKniRct7G5OGAgmR1ePnA,12334
|
|
42
|
+
grelu/resources/blacklists/mito_combined/mm9.full.blacklist.bed,sha256=ufHX4cLr4dO19HxxBu1Ld_tPxcUQ9yBDI_TbXijzymg,81053
|
|
43
|
+
grelu/resources/meme/JASPAR2022_CORE_non-redundant_pfms_meme.txt,sha256=6bJSI99ptJNT_4Qamv40mER37niCJ-_OeHcAruTv__k,1229369
|
|
44
|
+
grelu/resources/meme/jvierstra_consensus_pwms.meme,sha256=_bk2YiqiriS8Lb4EUitx8-eGcBzO64hxhArL8jl4z2Q,424027
|
|
45
|
+
grelu/sequence/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
46
|
+
grelu/sequence/format.py,sha256=7sS3oOGCKKEzZfikULgUrReEii-4hRw_ka-VPdOZb1s,13510
|
|
47
|
+
grelu/sequence/metrics.py,sha256=vfdSixbna1xkkX9mwgUmpLDwNz-u4UWkiez5qwiu9Rs,3562
|
|
48
|
+
grelu/sequence/mutate.py,sha256=LWvYydwfSFpcI-R8ITQgCEInGhIwPuywfqOBm04e_Mc,8429
|
|
49
|
+
grelu/sequence/utils.py,sha256=Wb6u4O2cfR0Hi5Jbzfp0dvf8M_2GbN1rShfNtQFFfT8,14398
|
|
50
|
+
grelu/transforms/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
51
|
+
grelu/transforms/label_transforms.py,sha256=TTvyuYmjfEX9PRaPiHPnMlCc1i0zB7Sr8Zb4vcajWHM,1593
|
|
52
|
+
grelu/transforms/prediction_transforms.py,sha256=PvnQBHeCoWBT8StTXXHaPW7wBPAgIrR0Qg8gPpwG_4Q,9465
|
|
53
|
+
grelu/transforms/seq_transforms.py,sha256=E15HNQZE7U-n4zhq5Ce-cYR2Uw-zVykCWRcowQFQe6c,4096
|
|
54
|
+
gReLU-1.0.1.dist-info/LICENSE.txt,sha256=s3XG-K-P6kWmWKQRBWP2wEGncZSc-PLYZ39-pno7abQ,1082
|
|
55
|
+
gReLU-1.0.1.dist-info/METADATA,sha256=ORKitAq8LWCHG0rcu4Nmc2VqPHJ-cZ5_LjDeAYTH2_E,3627
|
|
56
|
+
gReLU-1.0.1.dist-info/WHEEL,sha256=y4mX-SOX4fYIkonsAGA5N0Oy-8_gI4FXw5HNI1xqvWg,91
|
|
57
|
+
gReLU-1.0.1.dist-info/top_level.txt,sha256=9US1FHRDdiDL1cOAWVbMYkbdHRI6Q1kRLEhX3ur15Dk,6
|
|
58
|
+
gReLU-1.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
grelu
|
grelu/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
if sys.version_info[:2] >= (3, 8):
|
|
4
|
+
# TODO: Import directly (no need for conditional) when `python_requires = >= 3.8`
|
|
5
|
+
from importlib.metadata import PackageNotFoundError, version # pragma: no cover
|
|
6
|
+
else:
|
|
7
|
+
from importlib_metadata import PackageNotFoundError, version # pragma: no cover
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
# Change here if project is renamed and does not equal the package name
|
|
11
|
+
dist_name = __name__
|
|
12
|
+
__version__ = version(dist_name)
|
|
13
|
+
except PackageNotFoundError: # pragma: no cover
|
|
14
|
+
__version__ = "unknown"
|
|
15
|
+
finally:
|
|
16
|
+
del version, PackageNotFoundError
|
grelu/data/__init__.py
ADDED
|
File without changes
|
grelu/data/augment.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Functions to augment data. All functions assume that the input is a numpy array containing an integer
|
|
3
|
+
encoded DNA sequence of shape (L,) or a numpy array containing a label of shape (T, L).
|
|
4
|
+
The augmented output will be in the same format.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import List, Optional, Tuple, Union
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
from grelu.sequence.mutate import random_mutate
|
|
12
|
+
from grelu.sequence.utils import reverse_complement
|
|
13
|
+
|
|
14
|
+
# This is the number of output sequences expected from each type of augmentation
|
|
15
|
+
AUGMENTATION_MULTIPLIER_FUNCS = {
|
|
16
|
+
"rc": lambda x: 2**x,
|
|
17
|
+
"max_seq_shift": lambda x: (2 * x) + 1,
|
|
18
|
+
"max_pair_shift": lambda x: (2 * x) + 1,
|
|
19
|
+
"n_mutated_seqs": lambda x: max(1, x),
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _get_multipliers(**kwargs) -> List[int]:
|
|
24
|
+
return [AUGMENTATION_MULTIPLIER_FUNCS[k](v) for k, v in kwargs.items()]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _split_overall_idx(idx: int, max_values: List[int]) -> List[List[int]]:
|
|
28
|
+
"""
|
|
29
|
+
Given an integer index, split it into multiple indices, each ranging from 0
|
|
30
|
+
to a specified maximum value
|
|
31
|
+
"""
|
|
32
|
+
out = []
|
|
33
|
+
products = np.concatenate([np.flip(np.cumprod(np.flip(max_values[1:]))), [1]])
|
|
34
|
+
for v, p in zip(max_values, products):
|
|
35
|
+
out.append(int((idx // p) % v))
|
|
36
|
+
return out
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def shift(arr: np.ndarray, seq_len: int, idx: int) -> np.ndarray:
|
|
40
|
+
"""
|
|
41
|
+
Shift a sliding window along a sequence or label by the given number of bases.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
arr: Numpy array with length as the last dimension.
|
|
45
|
+
seq_len: Desired length for the output sequence.
|
|
46
|
+
idx: Start position
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Shifted sequence
|
|
50
|
+
"""
|
|
51
|
+
return arr[..., idx : idx + seq_len]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def rc_seq(seq: np.ndarray, idx: bool) -> np.ndarray:
|
|
55
|
+
"""
|
|
56
|
+
Reverse complement a sequence based on the index
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
seq: Integer-encoded sequence.
|
|
60
|
+
idx: If True, the reverse complement sequence will be returned.
|
|
61
|
+
If False, the sequence will be returned unchanged.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
Same or reverse complemented sequence
|
|
65
|
+
"""
|
|
66
|
+
return reverse_complement(seq, input_type="indices") if idx else seq
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def rc_label(label: np.ndarray, idx: bool) -> np.ndarray:
|
|
70
|
+
"""
|
|
71
|
+
Reverse a label based on the index
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
label: Numpy array with length as the last dimension
|
|
75
|
+
idx: If True, the label will be reversed along the length axis.
|
|
76
|
+
If False, the label will be returned unchanged.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
Same or reversed label
|
|
80
|
+
"""
|
|
81
|
+
return np.flip(label, -1).copy() if idx else label
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class Augmenter:
|
|
85
|
+
"""
|
|
86
|
+
A class that generates augmented DNA sequences or (sequence, label) pairs.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
rc: If True, augmentation by reverse complementation will be performed.
|
|
90
|
+
max_seq_shift: Maximum number of bases by which the sequence alone can be shifted.
|
|
91
|
+
This is normally a small value (< 10).
|
|
92
|
+
max_pair_shift: Maximum number of bases by which the sequence and label can be jointly
|
|
93
|
+
shifted. This can be a larger value.
|
|
94
|
+
n_mutated_seqs: Number of augmented sequences to generate by random mutation
|
|
95
|
+
n_mutated_bases: The number of bases to mutate in each augmented sequence. Only used
|
|
96
|
+
if n_mutated_seqs is greater than 0.
|
|
97
|
+
protect: A list of positions to protect from random mutation. Only used
|
|
98
|
+
if n_mutated_seqs is greater than 0.
|
|
99
|
+
seq_len: Length of the augmented sequences
|
|
100
|
+
label_len: Length of the augmented labels
|
|
101
|
+
seed: Random seed for reproducibility.
|
|
102
|
+
mode: "random" or "serial"
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
def __init__(
|
|
106
|
+
self,
|
|
107
|
+
rc: bool = False,
|
|
108
|
+
max_seq_shift: int = 0,
|
|
109
|
+
max_pair_shift: int = 0,
|
|
110
|
+
n_mutated_seqs: int = 0,
|
|
111
|
+
n_mutated_bases: Optional[int] = None,
|
|
112
|
+
protect: List[int] = [],
|
|
113
|
+
seq_len: Optional[int] = None,
|
|
114
|
+
label_len: Optional[int] = None,
|
|
115
|
+
seed: Optional[int] = None,
|
|
116
|
+
mode: str = "serial",
|
|
117
|
+
):
|
|
118
|
+
# Save general params
|
|
119
|
+
self.protect = protect
|
|
120
|
+
self.seq_len = seq_len
|
|
121
|
+
self.label_len = label_len
|
|
122
|
+
self.n_mutated_bases = n_mutated_bases
|
|
123
|
+
|
|
124
|
+
# Save augmentation params
|
|
125
|
+
self.rc = rc
|
|
126
|
+
self.max_seq_shift = max_seq_shift
|
|
127
|
+
self.max_pair_shift = max_pair_shift
|
|
128
|
+
self.n_mutated_seqs = n_mutated_seqs
|
|
129
|
+
self.shift_label = self.max_pair_shift > 0
|
|
130
|
+
self.shift_seq = (self.max_seq_shift > 0) or (self.shift_label)
|
|
131
|
+
self.mutate = (self.n_mutated_seqs > 0) and (self.n_mutated_bases > 0)
|
|
132
|
+
|
|
133
|
+
# Create settings
|
|
134
|
+
self.max_values = _get_multipliers(
|
|
135
|
+
rc=rc,
|
|
136
|
+
max_seq_shift=max_seq_shift,
|
|
137
|
+
max_pair_shift=max_pair_shift,
|
|
138
|
+
n_mutated_seqs=n_mutated_seqs,
|
|
139
|
+
)
|
|
140
|
+
self.products = np.concatenate(
|
|
141
|
+
[np.flip(np.cumprod(np.flip(self.max_values[1:]))), [1]]
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
# Set mode
|
|
145
|
+
self.mode = mode
|
|
146
|
+
|
|
147
|
+
# Set seed
|
|
148
|
+
self.rng = np.random.RandomState(seed)
|
|
149
|
+
|
|
150
|
+
def __len__(self) -> int:
|
|
151
|
+
"""
|
|
152
|
+
The total number of augmented sequences that can be produced from a single
|
|
153
|
+
DNA sequence
|
|
154
|
+
"""
|
|
155
|
+
return 1 if self.mode == "random" else np.product(self.max_values)
|
|
156
|
+
|
|
157
|
+
def _split(self, idx: int) -> List[tuple]:
|
|
158
|
+
"""
|
|
159
|
+
Function to split an input index into indices specifying each type
|
|
160
|
+
of augmentation
|
|
161
|
+
"""
|
|
162
|
+
return [(idx // p) % v for v, p in zip(self.max_values, self.products)]
|
|
163
|
+
|
|
164
|
+
def _get_random_idxs(self) -> List[tuple]:
|
|
165
|
+
"""
|
|
166
|
+
Function to select indices for each type of augmentation randomly
|
|
167
|
+
"""
|
|
168
|
+
return [self.rng.randint(v) for v in self.max_values]
|
|
169
|
+
|
|
170
|
+
def __call__(
|
|
171
|
+
self, idx: int, seq: np.ndarray, label: Optional[np.ndarray] = None
|
|
172
|
+
) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
|
|
173
|
+
"""
|
|
174
|
+
Perform augmentation on a given integer-encoded DNA sequence or (sequence, label) pair
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
idx: Index specifying the augmentation to be performed.
|
|
178
|
+
seq: A single integer encoded DNA sequence
|
|
179
|
+
label: A numpy array of shape (T, L) containing the label
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
The augmented DNA sequence or (sequence, label) pair if label is supplied.
|
|
183
|
+
"""
|
|
184
|
+
# Get index for each augmentation
|
|
185
|
+
if self.mode == "serial":
|
|
186
|
+
rc_idx, seq_shift_idx, pair_shift_idx, _ = self._split(idx)
|
|
187
|
+
elif self.mode == "random":
|
|
188
|
+
rc_idx, seq_shift_idx, pair_shift_idx, _ = self._get_random_idxs()
|
|
189
|
+
else:
|
|
190
|
+
raise NotImplementedError
|
|
191
|
+
|
|
192
|
+
# Augment the sequence
|
|
193
|
+
|
|
194
|
+
# Shift sequence
|
|
195
|
+
if self.shift_seq:
|
|
196
|
+
seq = shift(seq, seq_len=self.seq_len, idx=seq_shift_idx + pair_shift_idx)
|
|
197
|
+
|
|
198
|
+
# Reverse complement sequence
|
|
199
|
+
if self.rc:
|
|
200
|
+
seq = rc_seq(seq, idx=rc_idx)
|
|
201
|
+
|
|
202
|
+
# Introduce random mutations into the sequence
|
|
203
|
+
if self.mutate:
|
|
204
|
+
for _ in range(self.n_mutated_bases):
|
|
205
|
+
seq = random_mutate(
|
|
206
|
+
seq,
|
|
207
|
+
pos=None,
|
|
208
|
+
drop_ref=True,
|
|
209
|
+
protect=self.protect,
|
|
210
|
+
input_type="indices",
|
|
211
|
+
rng=self.rng,
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# If no label is provided, return only the sequence
|
|
215
|
+
if label is None:
|
|
216
|
+
return seq
|
|
217
|
+
|
|
218
|
+
else:
|
|
219
|
+
# Augment the label too
|
|
220
|
+
if self.shift_label:
|
|
221
|
+
# Shift label
|
|
222
|
+
label = shift(label, seq_len=self.label_len, idx=pair_shift_idx)
|
|
223
|
+
if self.rc:
|
|
224
|
+
# Reverse label
|
|
225
|
+
label = rc_label(label, idx=rc_idx)
|
|
226
|
+
|
|
227
|
+
return seq, label
|