align_trim 1.0.2__tar.gz → 1.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.
- align_trim-1.2.0/.github/workflows/python-publish.yml +64 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/PKG-INFO +26 -11
- {align_trim-1.0.2 → align_trim-1.2.0}/README.md +25 -10
- {align_trim-1.0.2 → align_trim-1.2.0}/align_trim/main.py +140 -176
- {align_trim-1.0.2 → align_trim-1.2.0}/pyproject.toml +1 -1
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_integration.py +10 -8
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_legacy.py +3 -2
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_main.py +3 -2
- align_trim-1.2.0/tests/test_normalise.py +221 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/uv.lock +1 -1
- {align_trim-1.0.2 → align_trim-1.2.0}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/.github/workflows/pytest.yml +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/.gitignore +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/.pre-commit-config.yaml +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/LICENSE +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/align_trim/__init__.py +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/__init__.py +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_data/primer.bed +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_data/sars-cov-2_v3.0.0_paired.bam +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_data/sars-cov-2_v5.3.2.bam +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_data/v1.0.0.primer.bed +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_data/v3.0.0.primer.bed +0 -0
- {align_trim-1.0.2 → align_trim-1.2.0}/tests/test_data/v5.3.2.primer.bed +0 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# This workflow will upload a Python Package to PyPI when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
|
|
3
|
+
|
|
4
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
5
|
+
# They are provided by a third-party and are governed by
|
|
6
|
+
# separate terms of service, privacy policy, and support
|
|
7
|
+
# documentation.
|
|
8
|
+
|
|
9
|
+
name: Upload Python Package
|
|
10
|
+
|
|
11
|
+
on:
|
|
12
|
+
release:
|
|
13
|
+
types: [published]
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
release-build:
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
|
|
25
|
+
- uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: "3.x"
|
|
28
|
+
|
|
29
|
+
- name: Build release distributions
|
|
30
|
+
run: |
|
|
31
|
+
python -m pip install build
|
|
32
|
+
python -m build
|
|
33
|
+
|
|
34
|
+
- name: Upload distributions
|
|
35
|
+
uses: actions/upload-artifact@v4
|
|
36
|
+
with:
|
|
37
|
+
name: release-dists
|
|
38
|
+
path: dist/
|
|
39
|
+
|
|
40
|
+
pypi-publish:
|
|
41
|
+
runs-on: ubuntu-latest
|
|
42
|
+
needs:
|
|
43
|
+
- release-build
|
|
44
|
+
permissions:
|
|
45
|
+
# IMPORTANT: this permission is mandatory for trusted publishing
|
|
46
|
+
id-token: write
|
|
47
|
+
|
|
48
|
+
# Dedicated environments with protections for publishing are strongly recommended.
|
|
49
|
+
# For more information, see: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deployment-protection-rules
|
|
50
|
+
environment:
|
|
51
|
+
name: pypi
|
|
52
|
+
url: https://pypi.org/p/align_trim
|
|
53
|
+
|
|
54
|
+
steps:
|
|
55
|
+
- name: Retrieve release distributions
|
|
56
|
+
uses: actions/download-artifact@v4
|
|
57
|
+
with:
|
|
58
|
+
name: release-dists
|
|
59
|
+
path: dist/
|
|
60
|
+
|
|
61
|
+
- name: Publish release distributions to PyPI
|
|
62
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
63
|
+
with:
|
|
64
|
+
packages-dir: dist/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: align_trim
|
|
3
|
-
Version: 1.0
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Soft-clip primer sites for SAM/BAM files generated from amplicon sequencing runs
|
|
5
5
|
Project-URL: Repository, https://github.com/artic-network/align_trim.git
|
|
6
6
|
Project-URL: Issues, https://github.com/artic-network/align_trim/issues
|
|
@@ -16,7 +16,7 @@ Description-Content-Type: text/markdown
|
|
|
16
16
|
|
|
17
17
|
# align_trim
|
|
18
18
|
|
|
19
|
-
Stand alone version of ARTIC's
|
|
19
|
+
Stand alone version of ARTIC's fieldbioinformatics align_trim.py
|
|
20
20
|
|
|
21
21
|
## Installation
|
|
22
22
|
|
|
@@ -41,10 +41,10 @@ uv run align_trim --help
|
|
|
41
41
|
### Basic Usage
|
|
42
42
|
|
|
43
43
|
```bash
|
|
44
|
-
|
|
44
|
+
align_trim [OPTIONS] BEDFILE
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
The tool reads alignment data from either a SAM file or stdin and outputs trimmed alignments to stdout in SAM format by default.
|
|
47
|
+
The tool reads alignment data from either a SAM/BAM file or stdin and outputs trimmed alignments to stdout in SAM format by default.
|
|
48
48
|
|
|
49
49
|
### Required Arguments
|
|
50
50
|
|
|
@@ -59,9 +59,9 @@ The tool reads alignment data from either a SAM file or stdin and outputs trimme
|
|
|
59
59
|
|
|
60
60
|
#### Processing Options
|
|
61
61
|
|
|
62
|
-
- `--normalise`, `-n` :
|
|
62
|
+
- `--normalise`, `-n` : Normalise to target depth N per amplicon using a greedy per-read algorithm. Each read is kept only if it brings the amplicon depth closer to the target. Use 0 for no normalisation (default: 0)
|
|
63
63
|
- `--min-mapq`, `-m` : Minimum mapping quality to keep an aligned read (default: 20)
|
|
64
|
-
- `--primer-match-threshold`, `-p` :
|
|
64
|
+
- `--primer-match-threshold`, `-p` : Add this many bases of padding to the 5' end of primer coordinates to allow fuzzy matching for reads with barcodes/adapters (default: 35)
|
|
65
65
|
|
|
66
66
|
#### Primer and Read Handling
|
|
67
67
|
|
|
@@ -73,6 +73,7 @@ The tool reads alignment data from either a SAM file or stdin and outputs trimme
|
|
|
73
73
|
|
|
74
74
|
- `--report`, `-r` : Output detailed report TSV to specified filepath
|
|
75
75
|
- `--amp-depth-report`, `-a` : Output mean depth for each amplicon as TSV to specified filepath
|
|
76
|
+
- `--genome-coverage-report`, `-g` : Output per-position genome coverage TSV(s) using the given prefix. Summary statistics (% genome covered at >=1x, >=10x, >=20x, >=100x) are printed to stderr. See [Genome Coverage Report](#genome-coverage-report) for details
|
|
76
77
|
- `--no-read-groups` : Do not divide reads into pool-based read groups in SAM/BAM output
|
|
77
78
|
|
|
78
79
|
#### General Options
|
|
@@ -85,30 +86,31 @@ The tool reads alignment data from either a SAM file or stdin and outputs trimme
|
|
|
85
86
|
|
|
86
87
|
#### Basic trimming with primer removal
|
|
87
88
|
```bash
|
|
88
|
-
|
|
89
|
+
align_trim primers.bed --samfile input.bam --output trimmed.bam
|
|
89
90
|
```
|
|
90
91
|
|
|
91
92
|
#### Normalize coverage and generate reports
|
|
92
93
|
```bash
|
|
93
|
-
|
|
94
|
+
align_trim primers.bed --samfile input.bam --normalise 100 \
|
|
94
95
|
--report alignment_report.tsv --amp-depth-report depth_report.tsv \
|
|
96
|
+
--genome-coverage-report sample1 \
|
|
95
97
|
--output normalized.bam
|
|
96
98
|
```
|
|
97
99
|
|
|
98
100
|
#### Process from stdin with verbose output
|
|
99
101
|
```bash
|
|
100
|
-
samtools view -h input.bam |
|
|
102
|
+
samtools view -h input.bam | align_trim primers.bed --verbose > trimmed.sam 2> verbose.out.txt
|
|
101
103
|
```
|
|
102
104
|
|
|
103
105
|
#### Strict full-length read filtering
|
|
104
106
|
```bash
|
|
105
|
-
|
|
107
|
+
align_trim primers.bed --samfile input.bam --require-full-length \
|
|
106
108
|
--min-mapq 30 --output filtered.bam
|
|
107
109
|
```
|
|
108
110
|
|
|
109
111
|
#### Allow mismatched primer pairs with custom threshold
|
|
110
112
|
```bash
|
|
111
|
-
|
|
113
|
+
align_trim primers.bed --samfile input.bam --allow-incorrect-pairs \
|
|
112
114
|
--primer-match-threshold 50 --output relaxed.bam
|
|
113
115
|
```
|
|
114
116
|
|
|
@@ -131,3 +133,16 @@ When using `--report`, a tab-separated file is generated with the following colu
|
|
|
131
133
|
- Additional alignment metrics
|
|
132
134
|
|
|
133
135
|
The `--amp-depth-report` generates a summary of coverage depth per amplicon.
|
|
136
|
+
|
|
137
|
+
### Genome Coverage Report
|
|
138
|
+
|
|
139
|
+
When using `--genome-coverage-report PREFIX`, per-position genome coverage is written as tab-separated files with columns `chrom`, `pos` (1-based), and `depth`.
|
|
140
|
+
|
|
141
|
+
**Without `--normalise`**, a single file is produced:
|
|
142
|
+
- `PREFIX.pre-normalisation.coverage.tsv` — coverage of all reads passing filtering and trimming
|
|
143
|
+
|
|
144
|
+
**With `--normalise`**, two files are produced:
|
|
145
|
+
- `PREFIX.pre-normalisation.coverage.tsv` — coverage of all reads passing filtering and trimming (before normalisation subsampling)
|
|
146
|
+
- `PREFIX.post-normalisation.coverage.tsv` — coverage of reads retained after normalisation
|
|
147
|
+
|
|
148
|
+
In both cases, a coverage summary is printed to stderr showing the percentage of genome positions covered at >=1x, >=10x, >=20x, >=100x, and >=1000x.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# align_trim
|
|
2
2
|
|
|
3
|
-
Stand alone version of ARTIC's
|
|
3
|
+
Stand alone version of ARTIC's fieldbioinformatics align_trim.py
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -25,10 +25,10 @@ uv run align_trim --help
|
|
|
25
25
|
### Basic Usage
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
|
-
|
|
28
|
+
align_trim [OPTIONS] BEDFILE
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
The tool reads alignment data from either a SAM file or stdin and outputs trimmed alignments to stdout in SAM format by default.
|
|
31
|
+
The tool reads alignment data from either a SAM/BAM file or stdin and outputs trimmed alignments to stdout in SAM format by default.
|
|
32
32
|
|
|
33
33
|
### Required Arguments
|
|
34
34
|
|
|
@@ -43,9 +43,9 @@ The tool reads alignment data from either a SAM file or stdin and outputs trimme
|
|
|
43
43
|
|
|
44
44
|
#### Processing Options
|
|
45
45
|
|
|
46
|
-
- `--normalise`, `-n` :
|
|
46
|
+
- `--normalise`, `-n` : Normalise to target depth N per amplicon using a greedy per-read algorithm. Each read is kept only if it brings the amplicon depth closer to the target. Use 0 for no normalisation (default: 0)
|
|
47
47
|
- `--min-mapq`, `-m` : Minimum mapping quality to keep an aligned read (default: 20)
|
|
48
|
-
- `--primer-match-threshold`, `-p` :
|
|
48
|
+
- `--primer-match-threshold`, `-p` : Add this many bases of padding to the 5' end of primer coordinates to allow fuzzy matching for reads with barcodes/adapters (default: 35)
|
|
49
49
|
|
|
50
50
|
#### Primer and Read Handling
|
|
51
51
|
|
|
@@ -57,6 +57,7 @@ The tool reads alignment data from either a SAM file or stdin and outputs trimme
|
|
|
57
57
|
|
|
58
58
|
- `--report`, `-r` : Output detailed report TSV to specified filepath
|
|
59
59
|
- `--amp-depth-report`, `-a` : Output mean depth for each amplicon as TSV to specified filepath
|
|
60
|
+
- `--genome-coverage-report`, `-g` : Output per-position genome coverage TSV(s) using the given prefix. Summary statistics (% genome covered at >=1x, >=10x, >=20x, >=100x) are printed to stderr. See [Genome Coverage Report](#genome-coverage-report) for details
|
|
60
61
|
- `--no-read-groups` : Do not divide reads into pool-based read groups in SAM/BAM output
|
|
61
62
|
|
|
62
63
|
#### General Options
|
|
@@ -69,30 +70,31 @@ The tool reads alignment data from either a SAM file or stdin and outputs trimme
|
|
|
69
70
|
|
|
70
71
|
#### Basic trimming with primer removal
|
|
71
72
|
```bash
|
|
72
|
-
|
|
73
|
+
align_trim primers.bed --samfile input.bam --output trimmed.bam
|
|
73
74
|
```
|
|
74
75
|
|
|
75
76
|
#### Normalize coverage and generate reports
|
|
76
77
|
```bash
|
|
77
|
-
|
|
78
|
+
align_trim primers.bed --samfile input.bam --normalise 100 \
|
|
78
79
|
--report alignment_report.tsv --amp-depth-report depth_report.tsv \
|
|
80
|
+
--genome-coverage-report sample1 \
|
|
79
81
|
--output normalized.bam
|
|
80
82
|
```
|
|
81
83
|
|
|
82
84
|
#### Process from stdin with verbose output
|
|
83
85
|
```bash
|
|
84
|
-
samtools view -h input.bam |
|
|
86
|
+
samtools view -h input.bam | align_trim primers.bed --verbose > trimmed.sam 2> verbose.out.txt
|
|
85
87
|
```
|
|
86
88
|
|
|
87
89
|
#### Strict full-length read filtering
|
|
88
90
|
```bash
|
|
89
|
-
|
|
91
|
+
align_trim primers.bed --samfile input.bam --require-full-length \
|
|
90
92
|
--min-mapq 30 --output filtered.bam
|
|
91
93
|
```
|
|
92
94
|
|
|
93
95
|
#### Allow mismatched primer pairs with custom threshold
|
|
94
96
|
```bash
|
|
95
|
-
|
|
97
|
+
align_trim primers.bed --samfile input.bam --allow-incorrect-pairs \
|
|
96
98
|
--primer-match-threshold 50 --output relaxed.bam
|
|
97
99
|
```
|
|
98
100
|
|
|
@@ -115,3 +117,16 @@ When using `--report`, a tab-separated file is generated with the following colu
|
|
|
115
117
|
- Additional alignment metrics
|
|
116
118
|
|
|
117
119
|
The `--amp-depth-report` generates a summary of coverage depth per amplicon.
|
|
120
|
+
|
|
121
|
+
### Genome Coverage Report
|
|
122
|
+
|
|
123
|
+
When using `--genome-coverage-report PREFIX`, per-position genome coverage is written as tab-separated files with columns `chrom`, `pos` (1-based), and `depth`.
|
|
124
|
+
|
|
125
|
+
**Without `--normalise`**, a single file is produced:
|
|
126
|
+
- `PREFIX.pre-normalisation.coverage.tsv` — coverage of all reads passing filtering and trimming
|
|
127
|
+
|
|
128
|
+
**With `--normalise`**, two files are produced:
|
|
129
|
+
- `PREFIX.pre-normalisation.coverage.tsv` — coverage of all reads passing filtering and trimming (before normalisation subsampling)
|
|
130
|
+
- `PREFIX.post-normalisation.coverage.tsv` — coverage of reads retained after normalisation
|
|
131
|
+
|
|
132
|
+
In both cases, a coverage summary is printed to stderr showing the percentage of genome positions covered at >=1x, >=10x, >=20x, >=100x, and >=1000x.
|
|
@@ -1,24 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import argparse
|
|
2
2
|
import csv
|
|
3
|
-
import
|
|
3
|
+
import itertools
|
|
4
4
|
import sys
|
|
5
|
-
import numpy as np
|
|
6
|
-
import random
|
|
7
|
-
import argparse
|
|
8
5
|
from collections import defaultdict
|
|
9
|
-
from
|
|
10
|
-
from pathlib import Path
|
|
11
|
-
import itertools
|
|
12
|
-
from typing import Union
|
|
13
|
-
|
|
6
|
+
from copy import copy
|
|
14
7
|
from importlib.metadata import version
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Optional, Union
|
|
15
10
|
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
import numpy as np
|
|
12
|
+
import pysam
|
|
18
13
|
from primalbedtools.amplicons import Amplicon, create_amplicons
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
from primalbedtools.bedfiles import BedLine, merge_primers
|
|
15
|
+
from primalbedtools.scheme import Scheme
|
|
22
16
|
|
|
23
17
|
# consumesReference lookup for if a CIGAR operation consumes the reference sequence
|
|
24
18
|
consumesReference = [True, False, True, True, False, False, False, True]
|
|
@@ -233,6 +227,7 @@ def handle_segments(
|
|
|
233
227
|
outfile_writer: pysam.AlignmentFile,
|
|
234
228
|
amp_depths: dict,
|
|
235
229
|
report_writer: csv.DictWriter = False, # type: ignore
|
|
230
|
+
genome_coverage: Optional[dict] = None,
|
|
236
231
|
):
|
|
237
232
|
"""Handle the alignment segment(s) including filtering, soft masking, and reporting.
|
|
238
233
|
|
|
@@ -458,7 +453,7 @@ def handle_segments(
|
|
|
458
453
|
return False
|
|
459
454
|
|
|
460
455
|
# softmask the alignment if right primer start/end inside alignment
|
|
461
|
-
if segment.reference_end > p2_position:
|
|
456
|
+
if segment.reference_end > p2_position: # type: ignore
|
|
462
457
|
try:
|
|
463
458
|
trim(segment, p2_position, True, args.verbose)
|
|
464
459
|
if args.verbose:
|
|
@@ -484,7 +479,7 @@ def handle_segments(
|
|
|
484
479
|
|
|
485
480
|
# Check require-full-length
|
|
486
481
|
if args.require_full_length:
|
|
487
|
-
if segment.reference_start > p1.end or segment.reference_end < p2.start:
|
|
482
|
+
if segment.reference_start > p1.end or segment.reference_end < p2.start: # type: ignore
|
|
488
483
|
if args.verbose:
|
|
489
484
|
print(
|
|
490
485
|
f"{segment.query_name}: ref_start {segment.reference_start} > p1.end {p1.end} or ref_end {segment.reference_end} < p2.start {p2.start}, does not span a full amplicon, skipping",
|
|
@@ -492,11 +487,16 @@ def handle_segments(
|
|
|
492
487
|
)
|
|
493
488
|
return False
|
|
494
489
|
|
|
490
|
+
if genome_coverage is not None:
|
|
491
|
+
genome_coverage[segment.reference_name][
|
|
492
|
+
segment.reference_start : segment.reference_end
|
|
493
|
+
] += 1
|
|
494
|
+
|
|
495
495
|
# If not normalising, write the segment to the output file and add it to amplicon depth numpy array
|
|
496
496
|
if not args.normalise:
|
|
497
497
|
outfile_writer.write(segment)
|
|
498
498
|
segment_amp_relative_start = segment.reference_start - p1.start
|
|
499
|
-
segment_amp_relative_end = segment.reference_end - p1.start
|
|
499
|
+
segment_amp_relative_end = segment.reference_end - p1.start # type: ignore
|
|
500
500
|
if segment_amp_relative_start < 0:
|
|
501
501
|
segment_amp_relative_start = 0
|
|
502
502
|
|
|
@@ -562,8 +562,8 @@ def handle_segments(
|
|
|
562
562
|
if args.require_full_length:
|
|
563
563
|
if segment1.reference_start < segment2.reference_start:
|
|
564
564
|
if (
|
|
565
|
-
segment1.reference_start > p1.end
|
|
566
|
-
or segment2.reference_end < p2.start
|
|
565
|
+
segment1.reference_start > p1.end # type: ignore
|
|
566
|
+
or segment2.reference_end < p2.start # type: ignore
|
|
567
567
|
):
|
|
568
568
|
if args.verbose:
|
|
569
569
|
print(
|
|
@@ -574,7 +574,7 @@ def handle_segments(
|
|
|
574
574
|
else:
|
|
575
575
|
if (
|
|
576
576
|
segment2.reference_start > p1.end
|
|
577
|
-
or segment1.reference_end < p2.start
|
|
577
|
+
or segment1.reference_end < p2.start # type: ignore
|
|
578
578
|
):
|
|
579
579
|
if args.verbose:
|
|
580
580
|
print(
|
|
@@ -583,13 +583,19 @@ def handle_segments(
|
|
|
583
583
|
)
|
|
584
584
|
return False
|
|
585
585
|
|
|
586
|
+
if genome_coverage is not None:
|
|
587
|
+
for seg in (segment1, segment2):
|
|
588
|
+
genome_coverage[seg.reference_name][
|
|
589
|
+
seg.reference_start : seg.reference_end
|
|
590
|
+
] += 1
|
|
591
|
+
|
|
586
592
|
# If not normalising, write the segments to the output file and add them to amplicon depth numpy array
|
|
587
593
|
if not args.normalise:
|
|
588
594
|
outfile_writer.write(segment1)
|
|
589
595
|
outfile_writer.write(segment2)
|
|
590
596
|
for segment_in_pair in (segment1, segment2):
|
|
591
597
|
segment_amp_relative_start = segment_in_pair.reference_start - p1.start
|
|
592
|
-
segment_amp_relative_end = segment_in_pair.reference_end - p1.start
|
|
598
|
+
segment_amp_relative_end = segment_in_pair.reference_end - p1.start # type: ignore
|
|
593
599
|
if segment_amp_relative_start < 0:
|
|
594
600
|
segment_amp_relative_start = 0
|
|
595
601
|
amp_depths[segment1.reference_name][amplicon][
|
|
@@ -601,129 +607,6 @@ def handle_segments(
|
|
|
601
607
|
return (amplicon, segment)
|
|
602
608
|
|
|
603
609
|
|
|
604
|
-
def normalise(
|
|
605
|
-
trimmed_segments: dict,
|
|
606
|
-
normalise: int,
|
|
607
|
-
primers: list[BedLine],
|
|
608
|
-
outfile: pysam.AlignmentFile,
|
|
609
|
-
verbose: bool = False,
|
|
610
|
-
):
|
|
611
|
-
"""Normalise the depth of the trimmed segments to a given value. Perform per-amplicon normalisation using numpy vector maths to determine whether the segment in question would take the depth closer to the desired depth across the amplicon.
|
|
612
|
-
|
|
613
|
-
Args:
|
|
614
|
-
trimmed_segments (dict): Dict containing amplicon number as key and list of pysam.AlignedSegment as value, if paired segments are used, the value will be a list of tuples containing the two segments.
|
|
615
|
-
normalise (int): Desired normalised depth
|
|
616
|
-
bed (list): Primer scheme as a list of BedLine objects
|
|
617
|
-
outfile (pysam.AlignmentFile): Output file handle to write the normalised segments to
|
|
618
|
-
verbose (bool): If True, will print normalisation info during processing
|
|
619
|
-
|
|
620
|
-
Raises:
|
|
621
|
-
ValueError: Amplicon assigned to segment not found in primer scheme file
|
|
622
|
-
|
|
623
|
-
Returns:
|
|
624
|
-
dict: A dictionary containing the mean depth for each amplicon post normalisation
|
|
625
|
-
"""
|
|
626
|
-
amplicons = {}
|
|
627
|
-
|
|
628
|
-
for amplicon in create_amplicons(primers):
|
|
629
|
-
amplicons.setdefault(amplicon.chrom, {})
|
|
630
|
-
amplicons[amplicon.chrom].setdefault(
|
|
631
|
-
amplicon.amplicon_number,
|
|
632
|
-
{
|
|
633
|
-
"length": amplicon.amplicon_end - amplicon.amplicon_start,
|
|
634
|
-
"p_start": amplicon.amplicon_start,
|
|
635
|
-
},
|
|
636
|
-
)
|
|
637
|
-
|
|
638
|
-
# mean_depths = {x: {} for x in amplicons}
|
|
639
|
-
mean_depths = {}
|
|
640
|
-
for chrom in amplicons:
|
|
641
|
-
for amplicon in amplicons[chrom]:
|
|
642
|
-
mean_depths[(chrom, amplicon)] = 0
|
|
643
|
-
|
|
644
|
-
for chrom, amplicon_dict in trimmed_segments.items():
|
|
645
|
-
for amplicon, segments in amplicon_dict.items():
|
|
646
|
-
if amplicon not in amplicons[chrom]:
|
|
647
|
-
raise ValueError(f"Amplicon {amplicon} not found in primer scheme file")
|
|
648
|
-
|
|
649
|
-
desired_depth = np.full_like(
|
|
650
|
-
(amplicons[chrom][amplicon]["length"],), normalise, dtype=int
|
|
651
|
-
)
|
|
652
|
-
|
|
653
|
-
amplicon_depth = np.zeros(
|
|
654
|
-
(amplicons[chrom][amplicon]["length"],), dtype=int
|
|
655
|
-
)
|
|
656
|
-
|
|
657
|
-
if not segments:
|
|
658
|
-
if verbose:
|
|
659
|
-
print(
|
|
660
|
-
f"No segments assigned to amplicon {amplicon}, skipping",
|
|
661
|
-
file=sys.stderr,
|
|
662
|
-
)
|
|
663
|
-
continue
|
|
664
|
-
|
|
665
|
-
random.Random(RANDOM_SEED).shuffle(segments)
|
|
666
|
-
|
|
667
|
-
distance = np.mean(np.abs(amplicon_depth - desired_depth))
|
|
668
|
-
|
|
669
|
-
for segment in segments:
|
|
670
|
-
paired = isinstance(segment, tuple)
|
|
671
|
-
|
|
672
|
-
if paired:
|
|
673
|
-
test_depths = np.copy(amplicon_depth)
|
|
674
|
-
segment1, segment2 = segment
|
|
675
|
-
for segment in (segment1, segment2):
|
|
676
|
-
relative_start = (
|
|
677
|
-
segment.reference_start
|
|
678
|
-
- amplicons[chrom][amplicon]["p_start"]
|
|
679
|
-
)
|
|
680
|
-
|
|
681
|
-
if relative_start < 0:
|
|
682
|
-
relative_start = 0
|
|
683
|
-
|
|
684
|
-
relative_end = (
|
|
685
|
-
segment.reference_end
|
|
686
|
-
- amplicons[chrom][amplicon]["p_start"]
|
|
687
|
-
)
|
|
688
|
-
|
|
689
|
-
test_depths[relative_start:relative_end] += 1
|
|
690
|
-
|
|
691
|
-
test_distance = np.mean(np.abs(test_depths - desired_depth))
|
|
692
|
-
|
|
693
|
-
if test_distance < distance:
|
|
694
|
-
amplicon_depth = test_depths
|
|
695
|
-
distance = test_distance
|
|
696
|
-
# write the segments to the output file
|
|
697
|
-
outfile.write(segment1)
|
|
698
|
-
outfile.write(segment2)
|
|
699
|
-
else:
|
|
700
|
-
test_depths = np.copy(amplicon_depth)
|
|
701
|
-
|
|
702
|
-
relative_start = (
|
|
703
|
-
segment.reference_start - amplicons[chrom][amplicon]["p_start"]
|
|
704
|
-
)
|
|
705
|
-
|
|
706
|
-
if relative_start < 0:
|
|
707
|
-
relative_start = 0
|
|
708
|
-
|
|
709
|
-
relative_end = (
|
|
710
|
-
segment.reference_end - amplicons[chrom][amplicon]["p_start"]
|
|
711
|
-
)
|
|
712
|
-
|
|
713
|
-
test_depths[relative_start:relative_end] += 1
|
|
714
|
-
|
|
715
|
-
test_distance = np.mean(np.abs(test_depths - desired_depth))
|
|
716
|
-
|
|
717
|
-
if test_distance < distance:
|
|
718
|
-
amplicon_depth = test_depths
|
|
719
|
-
distance = test_distance
|
|
720
|
-
outfile.write(segment)
|
|
721
|
-
|
|
722
|
-
mean_depths[(chrom, amplicon)] = np.mean(amplicon_depth)
|
|
723
|
-
|
|
724
|
-
return mean_depths
|
|
725
|
-
|
|
726
|
-
|
|
727
610
|
def read_pair_generator(bam, region_string=None):
|
|
728
611
|
"""
|
|
729
612
|
Generate read pairs in a BAM file or within a region string.
|
|
@@ -818,11 +701,37 @@ def create_primer_lookup(ref_len_tuple, amplicons: list[Amplicon], padding=35):
|
|
|
818
701
|
return lookups
|
|
819
702
|
|
|
820
703
|
|
|
704
|
+
def write_genome_coverage(filepath, genome_coverage, label=None):
|
|
705
|
+
total_len = sum(len(arr) for arr in genome_coverage.values())
|
|
706
|
+
with open(filepath, "w", newline="") as fh:
|
|
707
|
+
writer = csv.DictWriter(
|
|
708
|
+
fh, fieldnames=["chrom", "pos", "depth"], delimiter="\t"
|
|
709
|
+
)
|
|
710
|
+
writer.writeheader()
|
|
711
|
+
for chrom, depths in genome_coverage.items():
|
|
712
|
+
for i, d in enumerate(depths):
|
|
713
|
+
writer.writerow({"chrom": chrom, "pos": i + 1, "depth": int(d)})
|
|
714
|
+
all_depths = np.concatenate(list(genome_coverage.values()))
|
|
715
|
+
thresholds = [1, 10, 20, 100, 1000]
|
|
716
|
+
prefix = f"{label}: " if label else ""
|
|
717
|
+
print(f"{prefix}Genome coverage summary", file=sys.stderr)
|
|
718
|
+
print(f"{prefix} Total positions: {total_len}", file=sys.stderr)
|
|
719
|
+
for t in thresholds:
|
|
720
|
+
count = int(np.sum(all_depths >= t))
|
|
721
|
+
pct = (count / total_len) * 100 if total_len > 0 else 0
|
|
722
|
+
print(f"{prefix} >= {t}x: {count}/{total_len} ({pct:.2f}%)", file=sys.stderr)
|
|
723
|
+
|
|
724
|
+
|
|
821
725
|
def go(args):
|
|
822
726
|
"""Filter and soft mask an alignment file so that the alignment boundaries match the primer start and end sites.
|
|
823
727
|
|
|
824
728
|
Based on the most likely primer position, based on the alignment coordinates.
|
|
825
729
|
"""
|
|
730
|
+
# guard for negative normalise
|
|
731
|
+
if args.normalise is not None and args.normalise < 0:
|
|
732
|
+
print("normalise must be >= 0, exiting.", file=sys.stderr)
|
|
733
|
+
sys.exit(1)
|
|
734
|
+
|
|
826
735
|
# prepare the report outfile
|
|
827
736
|
if args.report:
|
|
828
737
|
reportfh = open(args.report, "w")
|
|
@@ -856,11 +765,10 @@ def go(args):
|
|
|
856
765
|
amplicon_list = create_amplicons(scheme.bedlines)
|
|
857
766
|
amplicons = {}
|
|
858
767
|
for amplicon in amplicon_list:
|
|
859
|
-
amplicon.length = amplicon.amplicon_end - amplicon.amplicon_start
|
|
768
|
+
amplicon.length = amplicon.amplicon_end - amplicon.amplicon_start # type: ignore
|
|
860
769
|
amplicons.setdefault(amplicon.chrom, {})[amplicon.amplicon_number] = amplicon
|
|
861
770
|
|
|
862
771
|
pools = set([bl.pool for bl in scheme.bedlines])
|
|
863
|
-
chroms = set([bl.chrom for bl in scheme.bedlines])
|
|
864
772
|
|
|
865
773
|
pools_str = {str(x) for x in pools}
|
|
866
774
|
pools_str.add("unmatched")
|
|
@@ -919,7 +827,8 @@ def go(args):
|
|
|
919
827
|
for amp in amplicon_list:
|
|
920
828
|
amp_depths.setdefault(amp.chrom, {})
|
|
921
829
|
amp_depths[amp.chrom].setdefault(
|
|
922
|
-
amp.amplicon_number,
|
|
830
|
+
amp.amplicon_number,
|
|
831
|
+
np.zeros(amp.length, dtype=int), # type: ignore
|
|
923
832
|
)
|
|
924
833
|
|
|
925
834
|
# Initialise the mean depths dictionary, this will get stomped over if normalisation is requested
|
|
@@ -936,7 +845,26 @@ def go(args):
|
|
|
936
845
|
padding=args.primer_match_threshold,
|
|
937
846
|
)
|
|
938
847
|
|
|
939
|
-
|
|
848
|
+
# Initialise genome-wide coverage arrays if requested
|
|
849
|
+
genome_coverage_pre = None
|
|
850
|
+
genome_coverage_post = None
|
|
851
|
+
if args.genome_coverage_report:
|
|
852
|
+
genome_coverage_pre = {}
|
|
853
|
+
for ref_name, ref_len in ref_lengths:
|
|
854
|
+
genome_coverage_pre[ref_name] = np.zeros(ref_len, dtype=int)
|
|
855
|
+
if args.normalise:
|
|
856
|
+
genome_coverage_post = {}
|
|
857
|
+
for ref_name, ref_len in ref_lengths:
|
|
858
|
+
genome_coverage_post[ref_name] = np.zeros(ref_len, dtype=int)
|
|
859
|
+
|
|
860
|
+
# Per-amplicon normalisation state: running depth array and current MAD from target
|
|
861
|
+
if args.normalise:
|
|
862
|
+
norm_state = {}
|
|
863
|
+
for amp in amplicon_list:
|
|
864
|
+
norm_state[(amp.chrom, amp.amplicon_number)] = {
|
|
865
|
+
"depth": np.zeros(amp.length, dtype=int), # type: ignore
|
|
866
|
+
"distance": float(args.normalise),
|
|
867
|
+
}
|
|
940
868
|
|
|
941
869
|
if paired:
|
|
942
870
|
read_pairs = read_pair_generator(chained_iterator)
|
|
@@ -951,6 +879,7 @@ def go(args):
|
|
|
951
879
|
min_mapq=args.min_mapq,
|
|
952
880
|
outfile_writer=outfile,
|
|
953
881
|
amp_depths=amp_depths,
|
|
882
|
+
genome_coverage=genome_coverage_pre,
|
|
954
883
|
)
|
|
955
884
|
else:
|
|
956
885
|
trimming_tuple = handle_segments(
|
|
@@ -960,6 +889,7 @@ def go(args):
|
|
|
960
889
|
min_mapq=args.min_mapq,
|
|
961
890
|
outfile_writer=outfile,
|
|
962
891
|
amp_depths=amp_depths,
|
|
892
|
+
genome_coverage=genome_coverage_pre,
|
|
963
893
|
)
|
|
964
894
|
|
|
965
895
|
if not trimming_tuple:
|
|
@@ -972,22 +902,29 @@ def go(args):
|
|
|
972
902
|
if not args.normalise and not trimmed_pair:
|
|
973
903
|
continue
|
|
974
904
|
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
905
|
+
if args.normalise and trimmed_pair:
|
|
906
|
+
chrom = trimmed_pair[0].reference_name # type: ignore
|
|
907
|
+
state = norm_state[(chrom, amplicon)]
|
|
908
|
+
p_start = amplicons[chrom][amplicon].amplicon_start
|
|
909
|
+
test_depths = np.copy(state["depth"])
|
|
910
|
+
for seg in trimmed_pair: # type: ignore
|
|
911
|
+
relative_start = max(0, seg.reference_start - p_start)
|
|
912
|
+
relative_end = seg.reference_end - p_start
|
|
913
|
+
test_depths[relative_start:relative_end] += 1
|
|
914
|
+
test_distance = np.mean(np.abs(test_depths - args.normalise))
|
|
915
|
+
if test_distance < state["distance"]:
|
|
916
|
+
state["depth"] = test_depths
|
|
917
|
+
state["distance"] = test_distance
|
|
918
|
+
outfile.write(trimmed_pair[0]) # type: ignore
|
|
919
|
+
outfile.write(trimmed_pair[1]) # type: ignore
|
|
920
|
+
if genome_coverage_post is not None:
|
|
921
|
+
for seg in trimmed_pair: # type: ignore
|
|
922
|
+
genome_coverage_post[seg.reference_name][
|
|
923
|
+
seg.reference_start : seg.reference_end
|
|
924
|
+
] += 1
|
|
981
925
|
|
|
982
|
-
# normalise if requested and write normalised segments to outfile
|
|
983
926
|
if args.normalise:
|
|
984
|
-
mean_amp_depths =
|
|
985
|
-
trimmed_segments=trimmed_segments,
|
|
986
|
-
normalise=args.normalise,
|
|
987
|
-
primers=scheme.bedlines,
|
|
988
|
-
outfile=outfile,
|
|
989
|
-
verbose=args.verbose,
|
|
990
|
-
)
|
|
927
|
+
mean_amp_depths = {k: np.mean(v["depth"]) for k, v in norm_state.items()}
|
|
991
928
|
else:
|
|
992
929
|
mean_amp_depths = {}
|
|
993
930
|
for chrom, chrom_amps in amp_depths.items():
|
|
@@ -1020,6 +957,7 @@ def go(args):
|
|
|
1020
957
|
lookup=primer_lookup,
|
|
1021
958
|
outfile_writer=outfile,
|
|
1022
959
|
amp_depths=amp_depths,
|
|
960
|
+
genome_coverage=genome_coverage_pre,
|
|
1023
961
|
)
|
|
1024
962
|
|
|
1025
963
|
else:
|
|
@@ -1030,6 +968,7 @@ def go(args):
|
|
|
1030
968
|
lookup=primer_lookup,
|
|
1031
969
|
outfile_writer=outfile,
|
|
1032
970
|
amp_depths=amp_depths,
|
|
971
|
+
genome_coverage=genome_coverage_pre,
|
|
1033
972
|
)
|
|
1034
973
|
|
|
1035
974
|
if not trimming_tuple:
|
|
@@ -1042,22 +981,27 @@ def go(args):
|
|
|
1042
981
|
if not args.normalise and not trimmed_segment:
|
|
1043
982
|
continue
|
|
1044
983
|
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
)
|
|
984
|
+
if args.normalise and trimmed_segment:
|
|
985
|
+
chrom = trimmed_segment.reference_name # type: ignore
|
|
986
|
+
state = norm_state[(chrom, amplicon)]
|
|
987
|
+
p_start = amplicons[chrom][amplicon].amplicon_start
|
|
988
|
+
test_depths = np.copy(state["depth"])
|
|
989
|
+
relative_start = max(0, trimmed_segment.reference_start - p_start) # type: ignore
|
|
990
|
+
relative_end = trimmed_segment.reference_end - p_start # type: ignore
|
|
991
|
+
test_depths[relative_start:relative_end] += 1
|
|
992
|
+
test_distance = np.mean(np.abs(test_depths - args.normalise))
|
|
993
|
+
if test_distance < state["distance"]:
|
|
994
|
+
state["depth"] = test_depths
|
|
995
|
+
state["distance"] = test_distance
|
|
996
|
+
outfile.write(trimmed_segment) # type: ignore
|
|
997
|
+
if genome_coverage_post is not None:
|
|
998
|
+
genome_coverage_post[trimmed_segment.reference_name][ # type: ignore
|
|
999
|
+
trimmed_segment.reference_start : trimmed_segment.reference_end # type: ignore
|
|
1000
|
+
] += 1
|
|
1051
1001
|
|
|
1052
1002
|
# normalise if requested
|
|
1053
1003
|
if args.normalise:
|
|
1054
|
-
mean_amp_depths =
|
|
1055
|
-
trimmed_segments=trimmed_segments,
|
|
1056
|
-
normalise=args.normalise,
|
|
1057
|
-
primers=scheme.bedlines,
|
|
1058
|
-
outfile=outfile,
|
|
1059
|
-
verbose=args.verbose,
|
|
1060
|
-
)
|
|
1004
|
+
mean_amp_depths = {k: np.mean(v["depth"]) for k, v in norm_state.items()}
|
|
1061
1005
|
|
|
1062
1006
|
else:
|
|
1063
1007
|
mean_amp_depths = {}
|
|
@@ -1080,6 +1024,18 @@ def go(args):
|
|
|
1080
1024
|
{"chrom": chrom, "amplicon": amplicon, "mean_depth": depth}
|
|
1081
1025
|
)
|
|
1082
1026
|
|
|
1027
|
+
# Write genome coverage reports
|
|
1028
|
+
if args.genome_coverage_report:
|
|
1029
|
+
pre_path = f"{args.genome_coverage_report}.pre-normalisation.coverage.tsv"
|
|
1030
|
+
write_genome_coverage(pre_path, genome_coverage_pre, label="Pre-normalisation")
|
|
1031
|
+
if args.normalise and genome_coverage_post is not None:
|
|
1032
|
+
post_path = (
|
|
1033
|
+
f"{args.genome_coverage_report}.post-normalisation.coverage.tsv"
|
|
1034
|
+
)
|
|
1035
|
+
write_genome_coverage(
|
|
1036
|
+
post_path, genome_coverage_post, label="Post-normalisation"
|
|
1037
|
+
)
|
|
1038
|
+
|
|
1083
1039
|
# close up the file handles
|
|
1084
1040
|
infile.close()
|
|
1085
1041
|
outfile.close()
|
|
@@ -1133,6 +1089,14 @@ def main():
|
|
|
1133
1089
|
type=Path,
|
|
1134
1090
|
help="Output amplicon depth TSV to filepath",
|
|
1135
1091
|
)
|
|
1092
|
+
parser.add_argument(
|
|
1093
|
+
"--genome-coverage-report",
|
|
1094
|
+
"-g",
|
|
1095
|
+
type=str,
|
|
1096
|
+
default=None,
|
|
1097
|
+
metavar="PREFIX",
|
|
1098
|
+
help="Output per-position genome coverage TSV(s) using PREFIX. Produces PREFIX.pre-normalisation.coverage.tsv (always) and PREFIX.post-normalisation.coverage.tsv (when --normalise is used). Summary stats are printed to stderr.",
|
|
1099
|
+
)
|
|
1136
1100
|
parser.add_argument(
|
|
1137
1101
|
"--no-trim-primers",
|
|
1138
1102
|
action="store_true",
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import pathlib
|
|
2
|
-
import sys
|
|
3
|
-
import unittest
|
|
4
1
|
import argparse
|
|
5
|
-
|
|
6
|
-
from primalbedtools.bedfiles import merge_primers
|
|
7
|
-
from primalbedtools.amplicons import create_amplicons
|
|
2
|
+
import pathlib
|
|
8
3
|
import tempfile
|
|
9
|
-
import
|
|
10
|
-
from align_trim.main import go, create_primer_lookup, find_primer_with_lookup
|
|
4
|
+
import unittest
|
|
11
5
|
from collections import defaultdict
|
|
12
6
|
|
|
7
|
+
import pysam
|
|
8
|
+
from primalbedtools.amplicons import create_amplicons
|
|
9
|
+
from primalbedtools.bedfiles import merge_primers
|
|
10
|
+
from primalbedtools.scheme import Scheme
|
|
11
|
+
|
|
12
|
+
from align_trim.main import create_primer_lookup, find_primer_with_lookup, go
|
|
13
|
+
|
|
13
14
|
BED_PATH_V5_3_2 = pathlib.Path(__file__).parent / "test_data/v5.3.2.primer.bed"
|
|
14
15
|
BAM_PATH_V5_3_2 = pathlib.Path(__file__).parent / "test_data/sars-cov-2_v5.3.2.bam"
|
|
15
16
|
BED_PATH_V3_0_0 = pathlib.Path(__file__).parent / "test_data/v3.0.0.primer.bed"
|
|
@@ -58,6 +59,7 @@ def create_args(**kwargs):
|
|
|
58
59
|
"allow_incorrect_pairs": False,
|
|
59
60
|
"require_full_length": False,
|
|
60
61
|
"output": None,
|
|
62
|
+
"genome_coverage_report": None,
|
|
61
63
|
}
|
|
62
64
|
defaults.update(kwargs)
|
|
63
65
|
return argparse.Namespace(**defaults)
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import pathlib
|
|
2
2
|
import unittest
|
|
3
|
-
|
|
4
|
-
from primalbedtools.bedfiles import BedLine
|
|
3
|
+
|
|
5
4
|
import pysam
|
|
5
|
+
from primalbedtools.bedfiles import BedLine
|
|
6
|
+
from primalbedtools.scheme import Scheme
|
|
6
7
|
|
|
7
8
|
from align_trim.main import find_primer, trim
|
|
8
9
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import pathlib
|
|
2
2
|
import unittest
|
|
3
|
-
|
|
4
|
-
from primalbedtools.bedfiles import merge_primers
|
|
3
|
+
|
|
5
4
|
from primalbedtools.amplicons import create_amplicons
|
|
5
|
+
from primalbedtools.bedfiles import merge_primers
|
|
6
|
+
from primalbedtools.scheme import Scheme
|
|
6
7
|
|
|
7
8
|
from align_trim.main import (
|
|
8
9
|
create_primer_lookup,
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import pathlib
|
|
3
|
+
import tempfile
|
|
4
|
+
import unittest
|
|
5
|
+
|
|
6
|
+
import pysam
|
|
7
|
+
|
|
8
|
+
from align_trim.main import go
|
|
9
|
+
|
|
10
|
+
BED_PATH_V5_3_2 = pathlib.Path(__file__).parent / "test_data/v5.3.2.primer.bed"
|
|
11
|
+
BAM_PATH_V5_3_2 = pathlib.Path(__file__).parent / "test_data/sars-cov-2_v5.3.2.bam"
|
|
12
|
+
BED_PATH_V3_0_0 = pathlib.Path(__file__).parent / "test_data/v3.0.0.primer.bed"
|
|
13
|
+
BAM_PATH_PAIRED_V3_0_0 = (
|
|
14
|
+
pathlib.Path(__file__).parent / "test_data/sars-cov-2_v3.0.0_paired.bam"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def create_args(**kwargs):
|
|
19
|
+
"""Create a fake args object with default values matching main.py argument parser"""
|
|
20
|
+
defaults = {
|
|
21
|
+
"bedfile": str(BED_PATH_V5_3_2.absolute()),
|
|
22
|
+
"samfile": str(BAM_PATH_V5_3_2.absolute()),
|
|
23
|
+
"normalise": 0,
|
|
24
|
+
"min_mapq": 20,
|
|
25
|
+
"primer_match_threshold": 35,
|
|
26
|
+
"report": None,
|
|
27
|
+
"amp_depth_report": None,
|
|
28
|
+
"no_trim_primers": False,
|
|
29
|
+
"paired": False,
|
|
30
|
+
"no_read_groups": False,
|
|
31
|
+
"verbose": False,
|
|
32
|
+
"allow_incorrect_pairs": False,
|
|
33
|
+
"require_full_length": False,
|
|
34
|
+
"output": None,
|
|
35
|
+
"genome_coverage_report": None,
|
|
36
|
+
}
|
|
37
|
+
defaults.update(kwargs)
|
|
38
|
+
return argparse.Namespace(**defaults)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def read_amp_depths(path):
|
|
42
|
+
"""Parse an amp_depth_report TSV into {amplicon: mean_depth}."""
|
|
43
|
+
depths = {}
|
|
44
|
+
with open(path) as f:
|
|
45
|
+
next(f) # skip header
|
|
46
|
+
for line in f:
|
|
47
|
+
_, amplicon, mean_depth = line.strip().split("\t")
|
|
48
|
+
depths[amplicon] = float(mean_depth)
|
|
49
|
+
return depths
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def count_reads(sam_path):
|
|
53
|
+
"""Count records in a SAM/BAM file."""
|
|
54
|
+
return sum(1 for _ in pysam.AlignmentFile(str(sam_path), "r"))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class TestNormaliseSingleEnd(unittest.TestCase):
|
|
58
|
+
def test_depth_bounded_by_target(self):
|
|
59
|
+
"""Mean amplicon depth after SE normalisation should not grossly exceed the target."""
|
|
60
|
+
with tempfile.TemporaryDirectory(
|
|
61
|
+
dir="tests", suffix="-se_norm_depth_bounded"
|
|
62
|
+
) as tempdir:
|
|
63
|
+
tempdir_path = pathlib.Path(tempdir)
|
|
64
|
+
normalise_target = 200
|
|
65
|
+
|
|
66
|
+
args = create_args(
|
|
67
|
+
output=(tempdir_path / "output.sam").absolute(),
|
|
68
|
+
normalise=normalise_target,
|
|
69
|
+
amp_depth_report=(tempdir_path / "amp_depths.tsv").absolute(),
|
|
70
|
+
)
|
|
71
|
+
go(args)
|
|
72
|
+
|
|
73
|
+
for amplicon, mean_depth in read_amp_depths(
|
|
74
|
+
tempdir_path / "amp_depths.tsv"
|
|
75
|
+
).items():
|
|
76
|
+
self.assertLessEqual(
|
|
77
|
+
mean_depth,
|
|
78
|
+
normalise_target * 1.5,
|
|
79
|
+
f"Amplicon {amplicon} mean depth {mean_depth:.1f} exceeds 1.5× target {normalise_target}",
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
def test_reduces_depth_vs_unnormalized(self):
|
|
83
|
+
"""SE normalisation must reduce depth for at least one amplicon vs. unnormalized."""
|
|
84
|
+
with tempfile.TemporaryDirectory(
|
|
85
|
+
dir="tests", suffix="-se_norm_reduces_depth"
|
|
86
|
+
) as tempdir:
|
|
87
|
+
tempdir_path = pathlib.Path(tempdir)
|
|
88
|
+
|
|
89
|
+
args_no_norm = create_args(
|
|
90
|
+
output=(tempdir_path / "no_norm.sam").absolute(),
|
|
91
|
+
normalise=0,
|
|
92
|
+
amp_depth_report=(tempdir_path / "depths_no_norm.tsv").absolute(),
|
|
93
|
+
)
|
|
94
|
+
go(args_no_norm)
|
|
95
|
+
|
|
96
|
+
# Use a low target (10) to ensure reduction fires even on small test BAMs
|
|
97
|
+
args_norm = create_args(
|
|
98
|
+
output=(tempdir_path / "norm.sam").absolute(),
|
|
99
|
+
normalise=10,
|
|
100
|
+
amp_depth_report=(tempdir_path / "depths_norm.tsv").absolute(),
|
|
101
|
+
)
|
|
102
|
+
go(args_norm)
|
|
103
|
+
|
|
104
|
+
no_norm = read_amp_depths(tempdir_path / "depths_no_norm.tsv")
|
|
105
|
+
norm = read_amp_depths(tempdir_path / "depths_norm.tsv")
|
|
106
|
+
|
|
107
|
+
reduction_found = any(
|
|
108
|
+
norm[amp] < no_norm[amp]
|
|
109
|
+
for amp in norm
|
|
110
|
+
if no_norm.get(amp, 0) > 0
|
|
111
|
+
)
|
|
112
|
+
self.assertTrue(
|
|
113
|
+
reduction_found,
|
|
114
|
+
"SE normalisation did not reduce depth for any amplicon",
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
def test_high_target_keeps_all_reads(self):
|
|
118
|
+
"""With a target far above actual coverage, all reads must be accepted."""
|
|
119
|
+
with tempfile.TemporaryDirectory(
|
|
120
|
+
dir="tests", suffix="-se_norm_high_target"
|
|
121
|
+
) as tempdir:
|
|
122
|
+
tempdir_path = pathlib.Path(tempdir)
|
|
123
|
+
|
|
124
|
+
args_no_norm = create_args(
|
|
125
|
+
output=(tempdir_path / "no_norm.sam").absolute(),
|
|
126
|
+
normalise=0,
|
|
127
|
+
)
|
|
128
|
+
go(args_no_norm)
|
|
129
|
+
|
|
130
|
+
args_high_norm = create_args(
|
|
131
|
+
output=(tempdir_path / "high_norm.sam").absolute(),
|
|
132
|
+
normalise=10000,
|
|
133
|
+
)
|
|
134
|
+
go(args_high_norm)
|
|
135
|
+
|
|
136
|
+
count_no_norm = count_reads(tempdir_path / "no_norm.sam")
|
|
137
|
+
count_high_norm = count_reads(tempdir_path / "high_norm.sam")
|
|
138
|
+
|
|
139
|
+
self.assertEqual(
|
|
140
|
+
count_no_norm,
|
|
141
|
+
count_high_norm,
|
|
142
|
+
f"High-target SE normalisation dropped reads: "
|
|
143
|
+
f"expected {count_no_norm}, got {count_high_norm}",
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class TestNormalisePaired(unittest.TestCase):
|
|
148
|
+
def test_reduces_depth_vs_unnormalized(self):
|
|
149
|
+
"""Paired normalisation must reduce depth for at least one amplicon vs. unnormalized."""
|
|
150
|
+
with tempfile.TemporaryDirectory(
|
|
151
|
+
dir="tests", suffix="-paired_norm_reduces_depth"
|
|
152
|
+
) as tempdir:
|
|
153
|
+
tempdir_path = pathlib.Path(tempdir)
|
|
154
|
+
|
|
155
|
+
args_no_norm = create_args(
|
|
156
|
+
output=(tempdir_path / "no_norm.bam").absolute(),
|
|
157
|
+
bedfile=BED_PATH_V3_0_0.absolute(),
|
|
158
|
+
samfile=BAM_PATH_PAIRED_V3_0_0.absolute(),
|
|
159
|
+
normalise=0,
|
|
160
|
+
amp_depth_report=(tempdir_path / "depths_no_norm.tsv").absolute(),
|
|
161
|
+
)
|
|
162
|
+
go(args_no_norm)
|
|
163
|
+
|
|
164
|
+
args_norm = create_args(
|
|
165
|
+
output=(tempdir_path / "norm.bam").absolute(),
|
|
166
|
+
bedfile=BED_PATH_V3_0_0.absolute(),
|
|
167
|
+
samfile=BAM_PATH_PAIRED_V3_0_0.absolute(),
|
|
168
|
+
normalise=200,
|
|
169
|
+
amp_depth_report=(tempdir_path / "depths_norm.tsv").absolute(),
|
|
170
|
+
)
|
|
171
|
+
go(args_norm)
|
|
172
|
+
|
|
173
|
+
no_norm = read_amp_depths(tempdir_path / "depths_no_norm.tsv")
|
|
174
|
+
norm = read_amp_depths(tempdir_path / "depths_norm.tsv")
|
|
175
|
+
|
|
176
|
+
reduction_found = any(
|
|
177
|
+
norm[amp] < no_norm[amp]
|
|
178
|
+
for amp in norm
|
|
179
|
+
if no_norm.get(amp, 0) > 0
|
|
180
|
+
)
|
|
181
|
+
self.assertTrue(
|
|
182
|
+
reduction_found,
|
|
183
|
+
"Paired normalisation did not reduce depth for any amplicon",
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
def test_high_target_keeps_all_reads(self):
|
|
187
|
+
"""With a target far above actual coverage, all paired reads must be accepted."""
|
|
188
|
+
with tempfile.TemporaryDirectory(
|
|
189
|
+
dir="tests", suffix="-paired_norm_high_target"
|
|
190
|
+
) as tempdir:
|
|
191
|
+
tempdir_path = pathlib.Path(tempdir)
|
|
192
|
+
|
|
193
|
+
args_no_norm = create_args(
|
|
194
|
+
output=(tempdir_path / "no_norm.bam").absolute(),
|
|
195
|
+
bedfile=BED_PATH_V3_0_0.absolute(),
|
|
196
|
+
samfile=BAM_PATH_PAIRED_V3_0_0.absolute(),
|
|
197
|
+
normalise=0,
|
|
198
|
+
)
|
|
199
|
+
go(args_no_norm)
|
|
200
|
+
|
|
201
|
+
args_high_norm = create_args(
|
|
202
|
+
output=(tempdir_path / "high_norm.bam").absolute(),
|
|
203
|
+
bedfile=BED_PATH_V3_0_0.absolute(),
|
|
204
|
+
samfile=BAM_PATH_PAIRED_V3_0_0.absolute(),
|
|
205
|
+
normalise=10000,
|
|
206
|
+
)
|
|
207
|
+
go(args_high_norm)
|
|
208
|
+
|
|
209
|
+
count_no_norm = count_reads(tempdir_path / "no_norm.bam")
|
|
210
|
+
count_high_norm = count_reads(tempdir_path / "high_norm.bam")
|
|
211
|
+
|
|
212
|
+
self.assertEqual(
|
|
213
|
+
count_no_norm,
|
|
214
|
+
count_high_norm,
|
|
215
|
+
f"High-target paired normalisation dropped reads: "
|
|
216
|
+
f"expected {count_no_norm}, got {count_high_norm}",
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
if __name__ == "__main__":
|
|
221
|
+
unittest.main()
|
|
@@ -9,7 +9,7 @@ resolution-markers = [
|
|
|
9
9
|
|
|
10
10
|
[[package]]
|
|
11
11
|
name = "align-trim"
|
|
12
|
-
version = "1.0
|
|
12
|
+
version = "1.2.0"
|
|
13
13
|
source = { editable = "." }
|
|
14
14
|
dependencies = [
|
|
15
15
|
{ name = "numpy", version = "2.0.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.10'" },
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|