densecall 0.0.1__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.
- densecall-0.0.1/LICENCE.txt +0 -0
- densecall-0.0.1/MANIFEST.in +4 -0
- densecall-0.0.1/PKG-INFO +175 -0
- densecall-0.0.1/README.md +166 -0
- densecall-0.0.1/densecall/__init__.py +37 -0
- densecall-0.0.1/densecall/__main__.py +2 -0
- densecall-0.0.1/densecall/aligner.py +83 -0
- densecall-0.0.1/densecall/classes.py +482 -0
- densecall-0.0.1/densecall/data.py +173 -0
- densecall-0.0.1/densecall/fast5.py +240 -0
- densecall-0.0.1/densecall/io.py +851 -0
- densecall-0.0.1/densecall/mod_util.py +182 -0
- densecall-0.0.1/densecall/models/configs/conformer.toml +54 -0
- densecall-0.0.1/densecall/models/configs/conformer_fast.toml +49 -0
- densecall-0.0.1/densecall/multiprocessing.py +266 -0
- densecall-0.0.1/densecall/pod5.py +143 -0
- densecall-0.0.1/densecall/read.py +260 -0
- densecall-0.0.1/densecall/reader.py +222 -0
- densecall-0.0.1/densecall/schedule.py +143 -0
- densecall-0.0.1/densecall/training.py +804 -0
- densecall-0.0.1/densecall/util.py +573 -0
- densecall-0.0.1/densecall.egg-info/PKG-INFO +175 -0
- densecall-0.0.1/densecall.egg-info/SOURCES.txt +30 -0
- densecall-0.0.1/densecall.egg-info/dependency_links.txt +1 -0
- densecall-0.0.1/densecall.egg-info/entry_points.txt +2 -0
- densecall-0.0.1/densecall.egg-info/not-zip-safe +1 -0
- densecall-0.0.1/densecall.egg-info/requires.txt +54 -0
- densecall-0.0.1/densecall.egg-info/top_level.txt +1 -0
- densecall-0.0.1/requirements.txt +62 -0
- densecall-0.0.1/setup.cfg +7 -0
- densecall-0.0.1/setup.py +50 -0
|
File without changes
|
densecall-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: densecall
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Home-page: https://github.com/n-damo/densecall
|
|
5
|
+
Author: linlian
|
|
6
|
+
Author-email: 21620151153308@stu.xmu.edu.cn
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENCE.txt
|
|
9
|
+
|
|
10
|
+
# DenseCall2
|
|
11
|
+
|
|
12
|
+
**DenseCall2: De Novo Base-Calling of DNA Modifications Using Nanopore Sequencing**
|
|
13
|
+
|
|
14
|
+
## Contents
|
|
15
|
+
- [DenseCall2](#densecall2)
|
|
16
|
+
- [Contents](#contents)
|
|
17
|
+
- [Requirements](#requirements)
|
|
18
|
+
- [Hardware](#hardware)
|
|
19
|
+
- [Software](#software)
|
|
20
|
+
- [Installation](#installation)
|
|
21
|
+
- [DenseCall2](#densecall2-1)
|
|
22
|
+
- [Basecalling of FAST5/Pod5 Files](#basecalling-of-fast5pod5-files)
|
|
23
|
+
- [Usage Notes](#usage-notes)
|
|
24
|
+
- [Training Your Own Basecalling Model (Optional)](#training-your-own-basecalling-model-optional)
|
|
25
|
+
- [Knowledge distilation](#knowledge-distilation)
|
|
26
|
+
- [Downstream Analysis](#downstream-analysis)
|
|
27
|
+
- [Citing](#citing)
|
|
28
|
+
- [License](#license)
|
|
29
|
+
- [Acknowledgement](#acknowledgement)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
DenseCall2 is an updated base-caller built on an optimized Conformer architecture for nanopore signal processing, enabling simultaneous base-calling and modification detection.
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
## Requirements
|
|
37
|
+
|
|
38
|
+
### Hardware
|
|
39
|
+
- **RAM**: 2 GB minimum; 16 GB or more recommended
|
|
40
|
+
- **CPU**: 4 cores minimum, ≥ 2.3 GHz per core
|
|
41
|
+
- **GPU**: NVIDIA RTX 4090 or newer (required for DenseCall2)
|
|
42
|
+
|
|
43
|
+
Benchmarks were collected on an ASUSTeK SVR TS700-E9-RS8 workstation
|
|
44
|
+
(Xeon Silver 4214 @ 2.20 GHz, 64 GB RAM, RTX 4090 24 GB).
|
|
45
|
+
|
|
46
|
+
### Software
|
|
47
|
+
|
|
48
|
+
**Supported Operating Systems**
|
|
49
|
+
- Linux: Ubuntu 22.04 or newer
|
|
50
|
+
- Windows and macOS are not yet supported
|
|
51
|
+
|
|
52
|
+
**Python**
|
|
53
|
+
- Version 3.10 or higher is required
|
|
54
|
+
Install on Ubuntu with:
|
|
55
|
+
```bash
|
|
56
|
+
sudo apt update
|
|
57
|
+
sudo apt install python3 python3-pip
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
### DenseCall2
|
|
63
|
+
|
|
64
|
+
First, set up a new environment and install the necessary Python packages using conda and pip:
|
|
65
|
+
|
|
66
|
+
```shell
|
|
67
|
+
# Create a new conda environment
|
|
68
|
+
conda create -n densecall python=3.10 -y
|
|
69
|
+
conda activate densecall
|
|
70
|
+
|
|
71
|
+
# Upgrade pip
|
|
72
|
+
pip install --upgrade pip
|
|
73
|
+
|
|
74
|
+
# Download and install
|
|
75
|
+
git clone https://github.com/LuChenLab/DENSECALL2.git
|
|
76
|
+
cd DENSECALL2
|
|
77
|
+
pip install -r requirements.txt
|
|
78
|
+
pip install flash-attn==2.8.3 --no-build-isolation --no-cache-dir
|
|
79
|
+
python setup.py develop
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
DenseCall2 is compatible with the basecaller of ont-bonito, allowing our trained models to be used for the basecalling process. Install ont-bonito as follows:
|
|
83
|
+
|
|
84
|
+
```shell
|
|
85
|
+
cd ont-bonito-0.7.3
|
|
86
|
+
python setup.py develop
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Basecalling of FAST5\/Pod5 Files
|
|
90
|
+
|
|
91
|
+
After installing DenseCall2, download the pre-trained models for human-specific models from [Pre-trained Basecalling Models](https://figshare.com/articles/dataset/Densecall_models/25712856). Available models include `dna_r9.4.1_hac_m5C@v1.0.tar.gz` for r9.4.1 data and `dna_r10.4.1_hac_m5C@v1.0.tar.gz` for r10.4.1 data.
|
|
92
|
+
|
|
93
|
+
DenseCall2 provides a method for transforming `.fast5` files into `.fastq` format or `.sam` format. Follow the commands below to perform basecalling:
|
|
94
|
+
|
|
95
|
+
```shell
|
|
96
|
+
# Activate the DenseCall2 conda environment
|
|
97
|
+
conda activate densecall2
|
|
98
|
+
|
|
99
|
+
# Navigate to the directory where you want to download the models
|
|
100
|
+
cd /path/to/Densecall2/densecall/models/
|
|
101
|
+
|
|
102
|
+
# Download and extract the models
|
|
103
|
+
# Note: Ensure you have already downloaded the .tar.gz files to this directory
|
|
104
|
+
tar -xzvf dna_r9.4.1_hac_m5C@v1.0.tar.gz
|
|
105
|
+
|
|
106
|
+
# Perform basecalling on the .fast5 files to generate .fastq files
|
|
107
|
+
densecall basecaller dna_r9.4.1_hac_m5C@v1.0 /path/to/fast5_data/ --mod --chunksize 12000 --overlap 600 --reference chr22.mmi --recursive --alignment-threads 12 > mod.sam
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
If you are using the tool solely for standard basecalling, you can omit the --mod flag.
|
|
111
|
+
```
|
|
112
|
+
densecall basecaller dna_r9.4.1_hac_m5C@v1.0 /path/to/fast5_data/ --chunksize 12000 --overlap 600 > mod.fastq
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Usage Notes
|
|
116
|
+
|
|
117
|
+
- **Modified-base calling**
|
|
118
|
+
Add `--mod` together with `--reference` and ensure the output file has a `.sam` extension.
|
|
119
|
+
DenseCall2 will perform Viterbi decoding and append MM/ML tags to the SAM output.
|
|
120
|
+
|
|
121
|
+
- **Standard base calling**
|
|
122
|
+
Omit `--mod` and set the output extension to `.fastq` or `.fq`.
|
|
123
|
+
DenseCall2 will use beam-search decoding.
|
|
124
|
+
|
|
125
|
+
### Training Your Own Basecalling Model (Optional)
|
|
126
|
+
|
|
127
|
+
`densecall train` - train a DenseCall2 model
|
|
128
|
+
|
|
129
|
+
To train a model using your own reads, first get a trained model from [Remora](https://github.com/nanoporetech/remora).
|
|
130
|
+
|
|
131
|
+
```shell
|
|
132
|
+
remora model download
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```shell
|
|
136
|
+
densecall basecaller dna_r10.4.1_e8.2_400bps_hac@v3.5.2 ./chr1_fast5 --batchsize 64 --chunksize 5000 --overlap 100 \
|
|
137
|
+
--reference chr1.mmi --recursive --save-ctc --min-accuracy-save-ctc 0.9 \
|
|
138
|
+
--alphabet NACZGT \
|
|
139
|
+
--modified-codes Z \
|
|
140
|
+
--modified-base-model /path/to/dna_r10.4.1_e8.2_400bps_hac_v3.5.1_5mc_CG_v2.pt \
|
|
141
|
+
--max-reads 100000 > r10_train_data/test.sam
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Training a new model from scratch:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
densecall train test --directory r10_train_data/ -f --batch 64 --epochs 30 \
|
|
148
|
+
--no-quantile-grad-clip --lr 0.002 --alphabet NACZGT \
|
|
149
|
+
--config conformer.toml --new --compile
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
## Knowledge distilation
|
|
154
|
+
To train a model using knowledge distilation, add the `--teacher` flag to the training command.
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
densecall train r10_student --directory r10_train_data/ -f --batch 64 --epochs 20 --grad-accum-split 2 --no-quantile-grad-clip --lr 0.002 --alphabet NACZGT --config conformer_fast.toml --new --compile --teacher r10_teacher/
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
All training calls use Automatic Mixed Precision to speed up training.
|
|
161
|
+
|
|
162
|
+
## Downstream Analysis
|
|
163
|
+
|
|
164
|
+
The results were analyzed using the ONT tool [modkit](https://github.com/nanoporetech/modkit), which processes BAM files containing MM/ML tags to generate comprehensive statistical reports. This study specifically employed modkit's "validate" and "pileup" functions.
|
|
165
|
+
|
|
166
|
+
## Citing
|
|
167
|
+
|
|
168
|
+
A pre-print will be uploaded soon.
|
|
169
|
+
|
|
170
|
+
## License
|
|
171
|
+
|
|
172
|
+
GNU General Public License v3.0
|
|
173
|
+
|
|
174
|
+
## Acknowledgement
|
|
175
|
+
We thank Bonito for making its source code available. DenseCall2 was built on Bonito’s framework: the save-CTC module, the training pipeline and the conversion of Conformer-based basecall outputs to base sequences have all been modified from Bonito’s original implementation. Bonito’s licence can be found [here](https://github.com/nanoporetech/bonito/blob/master/LICENCE.txt).
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# DenseCall2
|
|
2
|
+
|
|
3
|
+
**DenseCall2: De Novo Base-Calling of DNA Modifications Using Nanopore Sequencing**
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
- [DenseCall2](#densecall2)
|
|
7
|
+
- [Contents](#contents)
|
|
8
|
+
- [Requirements](#requirements)
|
|
9
|
+
- [Hardware](#hardware)
|
|
10
|
+
- [Software](#software)
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [DenseCall2](#densecall2-1)
|
|
13
|
+
- [Basecalling of FAST5/Pod5 Files](#basecalling-of-fast5pod5-files)
|
|
14
|
+
- [Usage Notes](#usage-notes)
|
|
15
|
+
- [Training Your Own Basecalling Model (Optional)](#training-your-own-basecalling-model-optional)
|
|
16
|
+
- [Knowledge distilation](#knowledge-distilation)
|
|
17
|
+
- [Downstream Analysis](#downstream-analysis)
|
|
18
|
+
- [Citing](#citing)
|
|
19
|
+
- [License](#license)
|
|
20
|
+
- [Acknowledgement](#acknowledgement)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
DenseCall2 is an updated base-caller built on an optimized Conformer architecture for nanopore signal processing, enabling simultaneous base-calling and modification detection.
|
|
24
|
+
|
|
25
|
+

|
|
26
|
+
|
|
27
|
+
## Requirements
|
|
28
|
+
|
|
29
|
+
### Hardware
|
|
30
|
+
- **RAM**: 2 GB minimum; 16 GB or more recommended
|
|
31
|
+
- **CPU**: 4 cores minimum, ≥ 2.3 GHz per core
|
|
32
|
+
- **GPU**: NVIDIA RTX 4090 or newer (required for DenseCall2)
|
|
33
|
+
|
|
34
|
+
Benchmarks were collected on an ASUSTeK SVR TS700-E9-RS8 workstation
|
|
35
|
+
(Xeon Silver 4214 @ 2.20 GHz, 64 GB RAM, RTX 4090 24 GB).
|
|
36
|
+
|
|
37
|
+
### Software
|
|
38
|
+
|
|
39
|
+
**Supported Operating Systems**
|
|
40
|
+
- Linux: Ubuntu 22.04 or newer
|
|
41
|
+
- Windows and macOS are not yet supported
|
|
42
|
+
|
|
43
|
+
**Python**
|
|
44
|
+
- Version 3.10 or higher is required
|
|
45
|
+
Install on Ubuntu with:
|
|
46
|
+
```bash
|
|
47
|
+
sudo apt update
|
|
48
|
+
sudo apt install python3 python3-pip
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
### DenseCall2
|
|
54
|
+
|
|
55
|
+
First, set up a new environment and install the necessary Python packages using conda and pip:
|
|
56
|
+
|
|
57
|
+
```shell
|
|
58
|
+
# Create a new conda environment
|
|
59
|
+
conda create -n densecall python=3.10 -y
|
|
60
|
+
conda activate densecall
|
|
61
|
+
|
|
62
|
+
# Upgrade pip
|
|
63
|
+
pip install --upgrade pip
|
|
64
|
+
|
|
65
|
+
# Download and install
|
|
66
|
+
git clone https://github.com/LuChenLab/DENSECALL2.git
|
|
67
|
+
cd DENSECALL2
|
|
68
|
+
pip install -r requirements.txt
|
|
69
|
+
pip install flash-attn==2.8.3 --no-build-isolation --no-cache-dir
|
|
70
|
+
python setup.py develop
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
DenseCall2 is compatible with the basecaller of ont-bonito, allowing our trained models to be used for the basecalling process. Install ont-bonito as follows:
|
|
74
|
+
|
|
75
|
+
```shell
|
|
76
|
+
cd ont-bonito-0.7.3
|
|
77
|
+
python setup.py develop
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Basecalling of FAST5\/Pod5 Files
|
|
81
|
+
|
|
82
|
+
After installing DenseCall2, download the pre-trained models for human-specific models from [Pre-trained Basecalling Models](https://figshare.com/articles/dataset/Densecall_models/25712856). Available models include `dna_r9.4.1_hac_m5C@v1.0.tar.gz` for r9.4.1 data and `dna_r10.4.1_hac_m5C@v1.0.tar.gz` for r10.4.1 data.
|
|
83
|
+
|
|
84
|
+
DenseCall2 provides a method for transforming `.fast5` files into `.fastq` format or `.sam` format. Follow the commands below to perform basecalling:
|
|
85
|
+
|
|
86
|
+
```shell
|
|
87
|
+
# Activate the DenseCall2 conda environment
|
|
88
|
+
conda activate densecall2
|
|
89
|
+
|
|
90
|
+
# Navigate to the directory where you want to download the models
|
|
91
|
+
cd /path/to/Densecall2/densecall/models/
|
|
92
|
+
|
|
93
|
+
# Download and extract the models
|
|
94
|
+
# Note: Ensure you have already downloaded the .tar.gz files to this directory
|
|
95
|
+
tar -xzvf dna_r9.4.1_hac_m5C@v1.0.tar.gz
|
|
96
|
+
|
|
97
|
+
# Perform basecalling on the .fast5 files to generate .fastq files
|
|
98
|
+
densecall basecaller dna_r9.4.1_hac_m5C@v1.0 /path/to/fast5_data/ --mod --chunksize 12000 --overlap 600 --reference chr22.mmi --recursive --alignment-threads 12 > mod.sam
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
If you are using the tool solely for standard basecalling, you can omit the --mod flag.
|
|
102
|
+
```
|
|
103
|
+
densecall basecaller dna_r9.4.1_hac_m5C@v1.0 /path/to/fast5_data/ --chunksize 12000 --overlap 600 > mod.fastq
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Usage Notes
|
|
107
|
+
|
|
108
|
+
- **Modified-base calling**
|
|
109
|
+
Add `--mod` together with `--reference` and ensure the output file has a `.sam` extension.
|
|
110
|
+
DenseCall2 will perform Viterbi decoding and append MM/ML tags to the SAM output.
|
|
111
|
+
|
|
112
|
+
- **Standard base calling**
|
|
113
|
+
Omit `--mod` and set the output extension to `.fastq` or `.fq`.
|
|
114
|
+
DenseCall2 will use beam-search decoding.
|
|
115
|
+
|
|
116
|
+
### Training Your Own Basecalling Model (Optional)
|
|
117
|
+
|
|
118
|
+
`densecall train` - train a DenseCall2 model
|
|
119
|
+
|
|
120
|
+
To train a model using your own reads, first get a trained model from [Remora](https://github.com/nanoporetech/remora).
|
|
121
|
+
|
|
122
|
+
```shell
|
|
123
|
+
remora model download
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```shell
|
|
127
|
+
densecall basecaller dna_r10.4.1_e8.2_400bps_hac@v3.5.2 ./chr1_fast5 --batchsize 64 --chunksize 5000 --overlap 100 \
|
|
128
|
+
--reference chr1.mmi --recursive --save-ctc --min-accuracy-save-ctc 0.9 \
|
|
129
|
+
--alphabet NACZGT \
|
|
130
|
+
--modified-codes Z \
|
|
131
|
+
--modified-base-model /path/to/dna_r10.4.1_e8.2_400bps_hac_v3.5.1_5mc_CG_v2.pt \
|
|
132
|
+
--max-reads 100000 > r10_train_data/test.sam
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Training a new model from scratch:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
densecall train test --directory r10_train_data/ -f --batch 64 --epochs 30 \
|
|
139
|
+
--no-quantile-grad-clip --lr 0.002 --alphabet NACZGT \
|
|
140
|
+
--config conformer.toml --new --compile
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
## Knowledge distilation
|
|
145
|
+
To train a model using knowledge distilation, add the `--teacher` flag to the training command.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
densecall train r10_student --directory r10_train_data/ -f --batch 64 --epochs 20 --grad-accum-split 2 --no-quantile-grad-clip --lr 0.002 --alphabet NACZGT --config conformer_fast.toml --new --compile --teacher r10_teacher/
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
All training calls use Automatic Mixed Precision to speed up training.
|
|
152
|
+
|
|
153
|
+
## Downstream Analysis
|
|
154
|
+
|
|
155
|
+
The results were analyzed using the ONT tool [modkit](https://github.com/nanoporetech/modkit), which processes BAM files containing MM/ML tags to generate comprehensive statistical reports. This study specifically employed modkit's "validate" and "pileup" functions.
|
|
156
|
+
|
|
157
|
+
## Citing
|
|
158
|
+
|
|
159
|
+
A pre-print will be uploaded soon.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
GNU General Public License v3.0
|
|
164
|
+
|
|
165
|
+
## Acknowledgement
|
|
166
|
+
We thank Bonito for making its source code available. DenseCall2 was built on Bonito’s framework: the save-CTC module, the training pipeline and the conversion of Conformer-based basecall outputs to base sequences have all been modified from Bonito’s original implementation. Bonito’s licence can be found [here](https://github.com/nanoporetech/bonito/blob/master/LICENCE.txt).
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
|
|
2
|
+
from densecall.cli import train, basecaller, accuracy
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
modules = [
|
|
6
|
+
'accuracy', 'train','basecaller'
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
__version__ = '0.0.1'
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main():
|
|
15
|
+
parser = ArgumentParser(
|
|
16
|
+
'densecall',
|
|
17
|
+
formatter_class=ArgumentDefaultsHelpFormatter
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
'-v', '--version', action='version',
|
|
22
|
+
version='%(prog)s {}'.format(__version__)
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
subparsers = parser.add_subparsers(
|
|
26
|
+
title='subcommands', description='valid commands',
|
|
27
|
+
help='additional help', dest='command'
|
|
28
|
+
)
|
|
29
|
+
subparsers.required = True
|
|
30
|
+
|
|
31
|
+
for module in modules:
|
|
32
|
+
mod = globals()[module]
|
|
33
|
+
p = subparsers.add_parser(module, parents=[mod.argparser()])
|
|
34
|
+
p.set_defaults(func=mod.main)
|
|
35
|
+
|
|
36
|
+
args = parser.parse_args()
|
|
37
|
+
args.func(args)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""
|
|
2
|
+
modified based on Bonito Aligner
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from threading import Thread
|
|
6
|
+
from functools import partial
|
|
7
|
+
import mappy
|
|
8
|
+
import numpy as np
|
|
9
|
+
from mappy import Aligner, ThreadBuffer
|
|
10
|
+
import sys
|
|
11
|
+
from densecall.multiprocessing import ThreadMap
|
|
12
|
+
from densecall.mod_util import call_mods, load_mods_model
|
|
13
|
+
|
|
14
|
+
def align_map(aligner, sequences, n_thread=4, mod_model=None, use_reference_anchored=False):
|
|
15
|
+
"""
|
|
16
|
+
Align `sequences` with minimap using `n_thread` threads.
|
|
17
|
+
"""
|
|
18
|
+
return ThreadMap(partial(MappyWorker, aligner, mod_model, use_reference_anchored), sequences, n_thread)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ManagedThreadBuffer:
|
|
22
|
+
"""
|
|
23
|
+
Minimap2 ThreadBuffer that is periodically reallocated.
|
|
24
|
+
"""
|
|
25
|
+
def __init__(self, max_uses=20):
|
|
26
|
+
self.max_uses = max_uses
|
|
27
|
+
self.uses = 0
|
|
28
|
+
self._b = ThreadBuffer()
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def buffer(self):
|
|
32
|
+
if self.uses > self.max_uses:
|
|
33
|
+
self._b = ThreadBuffer()
|
|
34
|
+
self.uses = 0
|
|
35
|
+
self.uses += 1
|
|
36
|
+
return self._b
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MappyWorker(Thread):
|
|
40
|
+
"""
|
|
41
|
+
Process that reads items from an input_queue, applies a func to them and puts them on an output_queue
|
|
42
|
+
"""
|
|
43
|
+
def __init__(self, aligner, mod_model, use_reference_anchored, input_queue=None, output_queue=None):
|
|
44
|
+
super().__init__()
|
|
45
|
+
self.aligner = aligner
|
|
46
|
+
self.input_queue = input_queue
|
|
47
|
+
self.output_queue = output_queue
|
|
48
|
+
self.mod_model = mod_model
|
|
49
|
+
self.use_reference_anchored = use_reference_anchored
|
|
50
|
+
|
|
51
|
+
def run(self):
|
|
52
|
+
thrbuf = ManagedThreadBuffer()
|
|
53
|
+
while True:
|
|
54
|
+
item = self.input_queue.get()
|
|
55
|
+
if item is StopIteration:
|
|
56
|
+
self.output_queue.put(item)
|
|
57
|
+
break
|
|
58
|
+
k, v = item
|
|
59
|
+
|
|
60
|
+
mapping = next(self.aligner.map(v['sequence'], buf=thrbuf.buffer, MD=True), None)
|
|
61
|
+
|
|
62
|
+
if self.mod_model and mapping:
|
|
63
|
+
|
|
64
|
+
refseq = self.aligner.seq(mapping.ctg, mapping.r_st, mapping.r_en)
|
|
65
|
+
cigar = np.array(mapping.cigar)
|
|
66
|
+
cigar = cigar[:, [1, 0]].tolist()
|
|
67
|
+
if mapping.strand == -1:
|
|
68
|
+
refseq = mappy.revcomp(refseq)
|
|
69
|
+
cigar = cigar[::-1]
|
|
70
|
+
|
|
71
|
+
v['refseq'] = refseq
|
|
72
|
+
v['cigar'] = cigar
|
|
73
|
+
v['modified_results'] = []
|
|
74
|
+
for mod_model, code in self.mod_model:
|
|
75
|
+
remora_model, remora_metadata = mod_model
|
|
76
|
+
can_base = remora_metadata['can_base']
|
|
77
|
+
v = call_mods(mod_model, k, v, use_reference_anchored=self.use_reference_anchored)
|
|
78
|
+
v['modified_results'].append((can_base, code, v['modified_result']))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
self.output_queue.put((k, {**v, 'mapping': mapping}))
|
|
82
|
+
|
|
83
|
+
|