strainphase 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- strainphase-0.1.0/LICENSE +29 -0
- strainphase-0.1.0/MANIFEST.in +37 -0
- strainphase-0.1.0/PKG-INFO +297 -0
- strainphase-0.1.0/README.md +261 -0
- strainphase-0.1.0/pyproject.toml +80 -0
- strainphase-0.1.0/scripts/sniffles_to_sidecar.py +204 -0
- strainphase-0.1.0/setup.cfg +4 -0
- strainphase-0.1.0/strainphase/__init__.py +76 -0
- strainphase-0.1.0/strainphase/cli.py +532 -0
- strainphase-0.1.0/strainphase/coherence.py +69 -0
- strainphase-0.1.0/strainphase/core.py +3719 -0
- strainphase-0.1.0/strainphase/lineages.py +102 -0
- strainphase-0.1.0/strainphase/longitudinal.py +853 -0
- strainphase-0.1.0/strainphase/simulation/__init__.py +17 -0
- strainphase-0.1.0/strainphase/simulation/synthetic_data.py +486 -0
- strainphase-0.1.0/strainphase/sv_encoding.py +514 -0
- strainphase-0.1.0/strainphase/track_merge.py +242 -0
- strainphase-0.1.0/strainphase/window_groups.py +64 -0
- strainphase-0.1.0/strainphase.egg-info/PKG-INFO +297 -0
- strainphase-0.1.0/strainphase.egg-info/SOURCES.txt +37 -0
- strainphase-0.1.0/strainphase.egg-info/dependency_links.txt +1 -0
- strainphase-0.1.0/strainphase.egg-info/entry_points.txt +2 -0
- strainphase-0.1.0/strainphase.egg-info/requires.txt +14 -0
- strainphase-0.1.0/strainphase.egg-info/top_level.txt +2 -0
- strainphase-0.1.0/tests/README.md +24 -0
- strainphase-0.1.0/tests/__init__.py +1 -0
- strainphase-0.1.0/tests/data/bfragilis_upey_lineages.tsv.gz +0 -0
- strainphase-0.1.0/tests/data/bfragilis_w1370001.tsv.gz +0 -0
- strainphase-0.1.0/tests/test_cli.py +122 -0
- strainphase-0.1.0/tests/test_core.py +1099 -0
- strainphase-0.1.0/tests/test_linking_scenarios.py +139 -0
- strainphase-0.1.0/tests/test_longitudinal.py +232 -0
- strainphase-0.1.0/tests/test_memory_offload.py +466 -0
- strainphase-0.1.0/tests/test_parsing.py +887 -0
- strainphase-0.1.0/tests/test_real_data_regressions.py +122 -0
- strainphase-0.1.0/tests/test_sv_encoding.py +431 -0
- strainphase-0.1.0/tests/test_track_merge.py +264 -0
- strainphase-0.1.0/tests/test_window_linking_rework.py +1210 -0
- strainphase-0.1.0/tests/util_io.py +206 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Renee Oles
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# What goes into the source distribution.
|
|
2
|
+
#
|
|
3
|
+
# `[tool.setuptools.packages.find] exclude` controls which packages get INSTALLED
|
|
4
|
+
# (the wheel). It does not control what the sdist contains, and the sdist is what
|
|
5
|
+
# PyPI hosts and what bioconda downloads and rebuilds. Without the prunes below,
|
|
6
|
+
# `python -m build` shipped archive/ and benchmark/ - private working directories
|
|
7
|
+
# that are gitignored precisely because they are not for distribution.
|
|
8
|
+
|
|
9
|
+
include LICENSE
|
|
10
|
+
include README.md
|
|
11
|
+
recursive-include tests *.py *.txt *.md
|
|
12
|
+
recursive-include scripts *.py
|
|
13
|
+
# Regression fixtures: without these the tests in the sdist cannot run.
|
|
14
|
+
recursive-include tests/data *.tsv.gz
|
|
15
|
+
|
|
16
|
+
# Gitignored working directories. Present in a source tree on disk, never in a
|
|
17
|
+
# release.
|
|
18
|
+
prune archive
|
|
19
|
+
prune benchmark
|
|
20
|
+
prune benchmarks
|
|
21
|
+
prune validation
|
|
22
|
+
prune docs
|
|
23
|
+
|
|
24
|
+
# The demo dataset (~24 MB of BAMs) lives in the repo so it can be cloned and run,
|
|
25
|
+
# but it has no business inside a release tarball that PyPI hosts and bioconda rebuilds.
|
|
26
|
+
prune demo
|
|
27
|
+
|
|
28
|
+
# Build and tool droppings.
|
|
29
|
+
prune .github
|
|
30
|
+
prune .claude
|
|
31
|
+
prune .pytest_cache
|
|
32
|
+
prune .ruff_cache
|
|
33
|
+
prune build
|
|
34
|
+
prune dist
|
|
35
|
+
global-exclude *.py[cod] __pycache__/* .DS_Store
|
|
36
|
+
exclude .pre-commit-config.yaml
|
|
37
|
+
exclude .coverage
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: strainphase
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Hybrid graph-probabilistic haplotype reconstruction for PacBio HiFi metagenomic data
|
|
5
|
+
Author-email: Renee Oles <roles@ucsd.edu>
|
|
6
|
+
License: BSD-3-Clause
|
|
7
|
+
Project-URL: Homepage, https://github.com/rolesucsd/strainphase
|
|
8
|
+
Project-URL: Repository, https://github.com/rolesucsd/strainphase
|
|
9
|
+
Project-URL: Issues, https://github.com/rolesucsd/strainphase/issues
|
|
10
|
+
Keywords: metagenomics,haplotype,strain,microbiome,pacbio,hifi,longitudinal,IBD
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: numpy>=1.20
|
|
25
|
+
Requires-Dist: scipy>=1.7
|
|
26
|
+
Requires-Dist: networkx>=2.6
|
|
27
|
+
Requires-Dist: python-louvain>=0.16
|
|
28
|
+
Requires-Dist: pysam>=0.19
|
|
29
|
+
Provides-Extra: full
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
33
|
+
Provides-Extra: all
|
|
34
|
+
Requires-Dist: strainphase[dev,full]; extra == "all"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# Strainphase
|
|
38
|
+
|
|
39
|
+
**Hybrid graph-probabilistic haplotype reconstruction for PacBio HiFi metagenomic data**
|
|
40
|
+
|
|
41
|
+
[](https://www.python.org/downloads/)
|
|
42
|
+
[](https://opensource.org/license/bsd-3-clause/)
|
|
43
|
+
|
|
44
|
+
## Overview
|
|
45
|
+
|
|
46
|
+
Strainphase reconstructs strain-resolved haplotypes from longitudinal PacBio HiFi
|
|
47
|
+
metagenomes. It divides each reference contig into fixed overlapping windows,
|
|
48
|
+
builds a read-similarity graph per window, refines candidate haplotypes with a
|
|
49
|
+
base-quality-weighted expectation–maximization (EM) model that includes an
|
|
50
|
+
explicit outlier component, and integrates information across timepoints to link
|
|
51
|
+
haplotypes into contig-spanning lineages.
|
|
52
|
+
|
|
53
|
+
Key capabilities:
|
|
54
|
+
|
|
55
|
+
- **Multi-variant phasing:** Phases SNVs, length-specific indels, multi-allelic
|
|
56
|
+
sites, and structural variants (DEL, INS, INV, DUP, BND) jointly as alleles,
|
|
57
|
+
enabling haplotype separation even among near-identical strains (≥99.9% ANI)
|
|
58
|
+
where SNVs alone are sparse.
|
|
59
|
+
- **High purity in multi-strain mixtures:** Scales cleanly from two to six
|
|
60
|
+
coexisting strains (91% and 79% of all informative reads placed into pure
|
|
61
|
+
haplotypes in 4- and 6-strain benchmarks, with 0 substantial false splits on
|
|
62
|
+
monoclonal controls).
|
|
63
|
+
- **Targeted longitudinal rescue:** Recovers transiently rare strains (≤1×
|
|
64
|
+
coverage) using high-abundance haplotypes from other timepoints as anchors,
|
|
65
|
+
without inflating false positives at higher depth.
|
|
66
|
+
- **Longitudinal SV-to-haplotype tracking:** Directly phases structural variants
|
|
67
|
+
onto point-mutation backbones across multi-year time series, distinguishing
|
|
68
|
+
within-strain structural plasticity (such as phase-variable inversions) from
|
|
69
|
+
diverged strain-linked deletions and insertions.
|
|
70
|
+
- **Practical compute:** Runs in ~4.5–5.7 min and 2.5–2.8 GB peak RAM per
|
|
71
|
+
10-timepoint species on standard CPUs (competitive with Rust-based per-sample
|
|
72
|
+
phasers and >25–160× faster than assembly-graph phasing).
|
|
73
|
+
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install strainphase
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Or install from source:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
git clone https://github.com/rolesucsd/strainphase.git
|
|
84
|
+
cd strainphase
|
|
85
|
+
pip install -e .
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Dependencies:** `numpy`, `scipy`, `networkx`, `python-louvain`, `pysam`
|
|
89
|
+
|
|
90
|
+
## Quick Start
|
|
91
|
+
|
|
92
|
+
### Demo dataset
|
|
93
|
+
|
|
94
|
+
A small demo dataset is bundled in [`demo/`](demo/) so the tool can be run immediately
|
|
95
|
+
without supplying data. It is six timepoints of two strains sweeping past each other
|
|
96
|
+
over a 100 kb contig, and it runs in under a minute:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
bash demo/run_demo.sh
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
This phases all six timepoints and prints the recovered trajectories beside the
|
|
103
|
+
bundled ground truth. See [`demo/README.md`](demo/README.md) for what the dataset
|
|
104
|
+
contains and what the output should look like.
|
|
105
|
+
|
|
106
|
+
### Command Line Interface
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# Process single contig
|
|
110
|
+
# The BAM must be sorted and indexed; the VCF bgzip-compressed and tabix-indexed.
|
|
111
|
+
strainphase run \
|
|
112
|
+
--bam sample.sorted.bam \
|
|
113
|
+
--vcf clair3/pileup.vcf.gz \
|
|
114
|
+
--contig MAG_01_contig_1 \
|
|
115
|
+
--length 50000 \
|
|
116
|
+
--output haplotypes.tsv
|
|
117
|
+
|
|
118
|
+
# Longitudinal analysis (multiple timepoints)
|
|
119
|
+
strainphase longitudinal \
|
|
120
|
+
--samples T1,T2,T3,T4 \
|
|
121
|
+
--bams mapping/{sample}.sorted.bam \
|
|
122
|
+
--vcfs variants/{sample}/pileup.vcf.gz \
|
|
123
|
+
--reference combined_bins.fasta \
|
|
124
|
+
--output-dir results/ \
|
|
125
|
+
--mags MAG_01
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Python API
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from strainphase import HaplotyperConfig, process_contig
|
|
132
|
+
|
|
133
|
+
config = HaplotyperConfig(
|
|
134
|
+
window_size=20000,
|
|
135
|
+
identity_distance=0.02,
|
|
136
|
+
min_weight_for_anchor=0.15,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
results = process_contig(
|
|
140
|
+
bam_path="sample.bam",
|
|
141
|
+
vcf_path="variants.vcf.gz",
|
|
142
|
+
contig_id="MAG_01_contig_1",
|
|
143
|
+
contig_length=50000,
|
|
144
|
+
config=config,
|
|
145
|
+
)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## CLI Commands
|
|
149
|
+
|
|
150
|
+
| Command | Description |
|
|
151
|
+
|---------|-------------|
|
|
152
|
+
| `strainphase run` | Process a single contig |
|
|
153
|
+
| `strainphase longitudinal` | Multi-sample longitudinal analysis |
|
|
154
|
+
| `strainphase sv` | Structural-variant sidecar tools (`reconcile`, `verify`) |
|
|
155
|
+
| `strainphase test` | Run the unit test suite (source checkout only) |
|
|
156
|
+
| `strainphase version` | Show version |
|
|
157
|
+
|
|
158
|
+
### `strainphase run`
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
strainphase run --bam FILE --vcf FILE --contig ID --length INT [OPTIONS]
|
|
162
|
+
|
|
163
|
+
Required:
|
|
164
|
+
--bam FILE Input BAM file (sorted, indexed)
|
|
165
|
+
--vcf FILE Input VCF file (Clair3 format; bgzip-compressed, tabix-indexed)
|
|
166
|
+
--contig ID Contig ID to process
|
|
167
|
+
--length INT Contig length in bp
|
|
168
|
+
|
|
169
|
+
Options:
|
|
170
|
+
--sample ID Sample identifier
|
|
171
|
+
--output FILE Output TSV file [default: haplotypes.tsv]
|
|
172
|
+
--window-size INT Analysis window size [default: 20000]
|
|
173
|
+
--max-reads INT Max reads per window [default: 500]
|
|
174
|
+
--min-mapq INT Minimum MAPQ [default: 20]
|
|
175
|
+
--identity-distance FLT Max mismatch RATE at which two things are one entity [default: 0.02]
|
|
176
|
+
--seed INT Random seed for reproducibility
|
|
177
|
+
--log-level LEVEL Logging level [default: INFO]
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### `strainphase longitudinal`
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
strainphase longitudinal --samples LIST --bams TPL --vcfs TPL --reference FILE --output-dir DIR [OPTIONS]
|
|
184
|
+
|
|
185
|
+
Required:
|
|
186
|
+
--samples LIST Comma-separated sample IDs (e.g., T1,T2,T3)
|
|
187
|
+
--bams TPL BAM path template with {sample} placeholder
|
|
188
|
+
--vcfs TPL VCF path template with {sample} placeholder
|
|
189
|
+
--reference FILE Reference FASTA (with .fai index)
|
|
190
|
+
--output-dir DIR Output directory
|
|
191
|
+
|
|
192
|
+
Options:
|
|
193
|
+
--mags LIST Comma-separated MAG names [default: all]
|
|
194
|
+
--contig-filter F File listing allowed contigs
|
|
195
|
+
--sv-sidecars TPL SV sidecar TSV template with {sample}; co-phases structural variants
|
|
196
|
+
-j, --workers INT Worker processes for window processing [default: 1]
|
|
197
|
+
--window-size INT Window size [default: 20000]
|
|
198
|
+
--max-reads INT Max reads per window [default: 500]
|
|
199
|
+
--min-anchor-weight FLT Minimum weight for anchor panel [default: 0.15]
|
|
200
|
+
--rescued-min-weight FLT Minimum weight after rescue [default: 0.02]
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Key Parameters
|
|
204
|
+
|
|
205
|
+
| Parameter | Default | Description |
|
|
206
|
+
|-----------|---------|-------------|
|
|
207
|
+
| `window_size` | 20000 | Analysis window size (bp) |
|
|
208
|
+
| `identity_distance` | 0.02 | Max mismatch RATE at which two things are one entity — read to read, haplotype to haplotype, and rescue |
|
|
209
|
+
| `min_shared_snvs_for_edge` | 1 | Shared SNVs before that rate means anything, read to read |
|
|
210
|
+
| `min_shared_markers` | 3 | Shared markers before it means anything, haplotype to haplotype |
|
|
211
|
+
| `track_merge_min_shared_markers` | 1 | Agreeing markers before the cross-sample merge joins two tracks |
|
|
212
|
+
| `link_window_reach` | 2 | Windows ahead step 1 may link; >1 links NON-overlapping windows on shared reads |
|
|
213
|
+
| `assign_confidence_threshold` | 0.90 | γ threshold for hard read assignment |
|
|
214
|
+
| `min_weight_for_anchor` | 0.15 | Min abundance for anchor panel |
|
|
215
|
+
| `rescued_min_weight` | 0.02 | Min weight after longitudinal rescue |
|
|
216
|
+
| `junk_divergence_rate` | 0.10 | Junk model divergence rate |
|
|
217
|
+
|
|
218
|
+
## Output Format
|
|
219
|
+
|
|
220
|
+
### `strainphase run` → `haplotypes.tsv`
|
|
221
|
+
|
|
222
|
+
One row per track (a haplotype linked across windows) on the contig.
|
|
223
|
+
|
|
224
|
+
| Column | Description |
|
|
225
|
+
|--------|-------------|
|
|
226
|
+
| `contig` | Contig ID |
|
|
227
|
+
| `sample` | Sample ID |
|
|
228
|
+
| `track_id` | Linked haplotype track identifier |
|
|
229
|
+
| `span_start`, `span_end`, `span_bp` | Track extent |
|
|
230
|
+
| `n_windows` | Windows the track covers |
|
|
231
|
+
| `n_snvs` | Number of sites in the consensus |
|
|
232
|
+
| `mean_weight` | Read-weighted abundance pooled over the track's windows; `NaN` where no window could measure one |
|
|
233
|
+
| `total_supporting_reads` | Reads confidently assigned to the track |
|
|
234
|
+
| `mean_confidence` | Mean assignment confidence |
|
|
235
|
+
| `consensus` | Allele profile, `pos:allele` pairs separated by `\|` |
|
|
236
|
+
|
|
237
|
+
### `strainphase longitudinal` → `--output-dir`
|
|
238
|
+
|
|
239
|
+
| File | One row per |
|
|
240
|
+
|------|-------------|
|
|
241
|
+
| `lineages.tsv` | (lineage, sample): the main output |
|
|
242
|
+
| `haplotypes.tsv` | window-haplotype, with its consensus |
|
|
243
|
+
| `windows_within_sample.tsv` | within-sample track |
|
|
244
|
+
| `windows_across_samples.tsv` | per-window group of samples judged one entity |
|
|
245
|
+
|
|
246
|
+
Diagnostics are written to `tmp/` beside them.
|
|
247
|
+
|
|
248
|
+
`lineages.tsv`:
|
|
249
|
+
|
|
250
|
+
| Column | Description |
|
|
251
|
+
|--------|-------------|
|
|
252
|
+
| `lineage_id` | Lineage identifier |
|
|
253
|
+
| `mag`, `contig`, `sample` | MAG, contig and sample/timepoint |
|
|
254
|
+
| `n_windows_lineage`, `n_samples_lineage` | Windows and samples the lineage covers in total |
|
|
255
|
+
| `window_start`, `window_end` | Lineage window extent |
|
|
256
|
+
| `marker_start`, `marker_end`, `marker_span_bp` | First and last marker position, and the distance between them |
|
|
257
|
+
| `n_windows` | Windows this sample contributes |
|
|
258
|
+
| `abundance` | Reads assigned / non-junk reads, pooled over the lineage's windows in this sample |
|
|
259
|
+
| `abundance_all_reads` | The same over all reads, junk included |
|
|
260
|
+
| `reads`, `total_reads`, `junk_reads` | The counts behind both abundances |
|
|
261
|
+
| `window_group_ids` | Per-window groups this sample contributes to |
|
|
262
|
+
| `haplotype_ids` | Window-haplotype IDs behind the row (join key to `haplotypes.tsv`) |
|
|
263
|
+
|
|
264
|
+
An `abundance` of 0 with non-zero `total_reads` is a measured zero. A (lineage, sample)
|
|
265
|
+
pair with no row was not measured there.
|
|
266
|
+
|
|
267
|
+
## Structural variants
|
|
268
|
+
|
|
269
|
+
`--sv-sidecars` co-phases structural variants with SNVs and indels. Each sample needs a
|
|
270
|
+
sidecar TSV listing its SV events and the reads supporting them; the format is
|
|
271
|
+
documented at the top of `strainphase/sv_encoding.py`. For Sniffles2 calls made with
|
|
272
|
+
`--output-rnames`, `scripts/sniffles_to_sidecar.py` in this repository writes one:
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
python scripts/sniffles_to_sidecar.py --sniffles T1.sv.vcf.gz --out-sidecar sv/T1.tsv
|
|
276
|
+
strainphase sv reconcile --sidecars sv/*.tsv --out-dir sv_reconciled/
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
`reconcile` gives one event the same ID and position in every sample; run it before
|
|
280
|
+
phasing.
|
|
281
|
+
|
|
282
|
+
## Algorithm Overview
|
|
283
|
+
|
|
284
|
+
```
|
|
285
|
+
1. WINDOWS BAM + VCF → overlapping 20 kb windows, 10 kb step
|
|
286
|
+
2. GRAPH read-similarity graph per window → Louvain communities
|
|
287
|
+
3. EM γ responsibilities ↔ π weights and consensus, with a junk component
|
|
288
|
+
4. MERGE within-window: merge haplotypes within identity_distance;
|
|
289
|
+
a one-marker difference is adjudicated against a sequencing-error null
|
|
290
|
+
5. LINK chain a sample's haplotypes across windows into tracks
|
|
291
|
+
6. RESCUE recover low-abundance haplotypes from other timepoints (longitudinal)
|
|
292
|
+
7. LINEAGES merge tracks across samples into contig-spanning lineages
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## License
|
|
296
|
+
|
|
297
|
+
BSD 3-Clause License - see [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# Strainphase
|
|
2
|
+
|
|
3
|
+
**Hybrid graph-probabilistic haplotype reconstruction for PacBio HiFi metagenomic data**
|
|
4
|
+
|
|
5
|
+
[](https://www.python.org/downloads/)
|
|
6
|
+
[](https://opensource.org/license/bsd-3-clause/)
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
Strainphase reconstructs strain-resolved haplotypes from longitudinal PacBio HiFi
|
|
11
|
+
metagenomes. It divides each reference contig into fixed overlapping windows,
|
|
12
|
+
builds a read-similarity graph per window, refines candidate haplotypes with a
|
|
13
|
+
base-quality-weighted expectation–maximization (EM) model that includes an
|
|
14
|
+
explicit outlier component, and integrates information across timepoints to link
|
|
15
|
+
haplotypes into contig-spanning lineages.
|
|
16
|
+
|
|
17
|
+
Key capabilities:
|
|
18
|
+
|
|
19
|
+
- **Multi-variant phasing:** Phases SNVs, length-specific indels, multi-allelic
|
|
20
|
+
sites, and structural variants (DEL, INS, INV, DUP, BND) jointly as alleles,
|
|
21
|
+
enabling haplotype separation even among near-identical strains (≥99.9% ANI)
|
|
22
|
+
where SNVs alone are sparse.
|
|
23
|
+
- **High purity in multi-strain mixtures:** Scales cleanly from two to six
|
|
24
|
+
coexisting strains (91% and 79% of all informative reads placed into pure
|
|
25
|
+
haplotypes in 4- and 6-strain benchmarks, with 0 substantial false splits on
|
|
26
|
+
monoclonal controls).
|
|
27
|
+
- **Targeted longitudinal rescue:** Recovers transiently rare strains (≤1×
|
|
28
|
+
coverage) using high-abundance haplotypes from other timepoints as anchors,
|
|
29
|
+
without inflating false positives at higher depth.
|
|
30
|
+
- **Longitudinal SV-to-haplotype tracking:** Directly phases structural variants
|
|
31
|
+
onto point-mutation backbones across multi-year time series, distinguishing
|
|
32
|
+
within-strain structural plasticity (such as phase-variable inversions) from
|
|
33
|
+
diverged strain-linked deletions and insertions.
|
|
34
|
+
- **Practical compute:** Runs in ~4.5–5.7 min and 2.5–2.8 GB peak RAM per
|
|
35
|
+
10-timepoint species on standard CPUs (competitive with Rust-based per-sample
|
|
36
|
+
phasers and >25–160× faster than assembly-graph phasing).
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install strainphase
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or install from source:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
git clone https://github.com/rolesucsd/strainphase.git
|
|
48
|
+
cd strainphase
|
|
49
|
+
pip install -e .
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Dependencies:** `numpy`, `scipy`, `networkx`, `python-louvain`, `pysam`
|
|
53
|
+
|
|
54
|
+
## Quick Start
|
|
55
|
+
|
|
56
|
+
### Demo dataset
|
|
57
|
+
|
|
58
|
+
A small demo dataset is bundled in [`demo/`](demo/) so the tool can be run immediately
|
|
59
|
+
without supplying data. It is six timepoints of two strains sweeping past each other
|
|
60
|
+
over a 100 kb contig, and it runs in under a minute:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
bash demo/run_demo.sh
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
This phases all six timepoints and prints the recovered trajectories beside the
|
|
67
|
+
bundled ground truth. See [`demo/README.md`](demo/README.md) for what the dataset
|
|
68
|
+
contains and what the output should look like.
|
|
69
|
+
|
|
70
|
+
### Command Line Interface
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
# Process single contig
|
|
74
|
+
# The BAM must be sorted and indexed; the VCF bgzip-compressed and tabix-indexed.
|
|
75
|
+
strainphase run \
|
|
76
|
+
--bam sample.sorted.bam \
|
|
77
|
+
--vcf clair3/pileup.vcf.gz \
|
|
78
|
+
--contig MAG_01_contig_1 \
|
|
79
|
+
--length 50000 \
|
|
80
|
+
--output haplotypes.tsv
|
|
81
|
+
|
|
82
|
+
# Longitudinal analysis (multiple timepoints)
|
|
83
|
+
strainphase longitudinal \
|
|
84
|
+
--samples T1,T2,T3,T4 \
|
|
85
|
+
--bams mapping/{sample}.sorted.bam \
|
|
86
|
+
--vcfs variants/{sample}/pileup.vcf.gz \
|
|
87
|
+
--reference combined_bins.fasta \
|
|
88
|
+
--output-dir results/ \
|
|
89
|
+
--mags MAG_01
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Python API
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from strainphase import HaplotyperConfig, process_contig
|
|
96
|
+
|
|
97
|
+
config = HaplotyperConfig(
|
|
98
|
+
window_size=20000,
|
|
99
|
+
identity_distance=0.02,
|
|
100
|
+
min_weight_for_anchor=0.15,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
results = process_contig(
|
|
104
|
+
bam_path="sample.bam",
|
|
105
|
+
vcf_path="variants.vcf.gz",
|
|
106
|
+
contig_id="MAG_01_contig_1",
|
|
107
|
+
contig_length=50000,
|
|
108
|
+
config=config,
|
|
109
|
+
)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## CLI Commands
|
|
113
|
+
|
|
114
|
+
| Command | Description |
|
|
115
|
+
|---------|-------------|
|
|
116
|
+
| `strainphase run` | Process a single contig |
|
|
117
|
+
| `strainphase longitudinal` | Multi-sample longitudinal analysis |
|
|
118
|
+
| `strainphase sv` | Structural-variant sidecar tools (`reconcile`, `verify`) |
|
|
119
|
+
| `strainphase test` | Run the unit test suite (source checkout only) |
|
|
120
|
+
| `strainphase version` | Show version |
|
|
121
|
+
|
|
122
|
+
### `strainphase run`
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
strainphase run --bam FILE --vcf FILE --contig ID --length INT [OPTIONS]
|
|
126
|
+
|
|
127
|
+
Required:
|
|
128
|
+
--bam FILE Input BAM file (sorted, indexed)
|
|
129
|
+
--vcf FILE Input VCF file (Clair3 format; bgzip-compressed, tabix-indexed)
|
|
130
|
+
--contig ID Contig ID to process
|
|
131
|
+
--length INT Contig length in bp
|
|
132
|
+
|
|
133
|
+
Options:
|
|
134
|
+
--sample ID Sample identifier
|
|
135
|
+
--output FILE Output TSV file [default: haplotypes.tsv]
|
|
136
|
+
--window-size INT Analysis window size [default: 20000]
|
|
137
|
+
--max-reads INT Max reads per window [default: 500]
|
|
138
|
+
--min-mapq INT Minimum MAPQ [default: 20]
|
|
139
|
+
--identity-distance FLT Max mismatch RATE at which two things are one entity [default: 0.02]
|
|
140
|
+
--seed INT Random seed for reproducibility
|
|
141
|
+
--log-level LEVEL Logging level [default: INFO]
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### `strainphase longitudinal`
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
strainphase longitudinal --samples LIST --bams TPL --vcfs TPL --reference FILE --output-dir DIR [OPTIONS]
|
|
148
|
+
|
|
149
|
+
Required:
|
|
150
|
+
--samples LIST Comma-separated sample IDs (e.g., T1,T2,T3)
|
|
151
|
+
--bams TPL BAM path template with {sample} placeholder
|
|
152
|
+
--vcfs TPL VCF path template with {sample} placeholder
|
|
153
|
+
--reference FILE Reference FASTA (with .fai index)
|
|
154
|
+
--output-dir DIR Output directory
|
|
155
|
+
|
|
156
|
+
Options:
|
|
157
|
+
--mags LIST Comma-separated MAG names [default: all]
|
|
158
|
+
--contig-filter F File listing allowed contigs
|
|
159
|
+
--sv-sidecars TPL SV sidecar TSV template with {sample}; co-phases structural variants
|
|
160
|
+
-j, --workers INT Worker processes for window processing [default: 1]
|
|
161
|
+
--window-size INT Window size [default: 20000]
|
|
162
|
+
--max-reads INT Max reads per window [default: 500]
|
|
163
|
+
--min-anchor-weight FLT Minimum weight for anchor panel [default: 0.15]
|
|
164
|
+
--rescued-min-weight FLT Minimum weight after rescue [default: 0.02]
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Key Parameters
|
|
168
|
+
|
|
169
|
+
| Parameter | Default | Description |
|
|
170
|
+
|-----------|---------|-------------|
|
|
171
|
+
| `window_size` | 20000 | Analysis window size (bp) |
|
|
172
|
+
| `identity_distance` | 0.02 | Max mismatch RATE at which two things are one entity — read to read, haplotype to haplotype, and rescue |
|
|
173
|
+
| `min_shared_snvs_for_edge` | 1 | Shared SNVs before that rate means anything, read to read |
|
|
174
|
+
| `min_shared_markers` | 3 | Shared markers before it means anything, haplotype to haplotype |
|
|
175
|
+
| `track_merge_min_shared_markers` | 1 | Agreeing markers before the cross-sample merge joins two tracks |
|
|
176
|
+
| `link_window_reach` | 2 | Windows ahead step 1 may link; >1 links NON-overlapping windows on shared reads |
|
|
177
|
+
| `assign_confidence_threshold` | 0.90 | γ threshold for hard read assignment |
|
|
178
|
+
| `min_weight_for_anchor` | 0.15 | Min abundance for anchor panel |
|
|
179
|
+
| `rescued_min_weight` | 0.02 | Min weight after longitudinal rescue |
|
|
180
|
+
| `junk_divergence_rate` | 0.10 | Junk model divergence rate |
|
|
181
|
+
|
|
182
|
+
## Output Format
|
|
183
|
+
|
|
184
|
+
### `strainphase run` → `haplotypes.tsv`
|
|
185
|
+
|
|
186
|
+
One row per track (a haplotype linked across windows) on the contig.
|
|
187
|
+
|
|
188
|
+
| Column | Description |
|
|
189
|
+
|--------|-------------|
|
|
190
|
+
| `contig` | Contig ID |
|
|
191
|
+
| `sample` | Sample ID |
|
|
192
|
+
| `track_id` | Linked haplotype track identifier |
|
|
193
|
+
| `span_start`, `span_end`, `span_bp` | Track extent |
|
|
194
|
+
| `n_windows` | Windows the track covers |
|
|
195
|
+
| `n_snvs` | Number of sites in the consensus |
|
|
196
|
+
| `mean_weight` | Read-weighted abundance pooled over the track's windows; `NaN` where no window could measure one |
|
|
197
|
+
| `total_supporting_reads` | Reads confidently assigned to the track |
|
|
198
|
+
| `mean_confidence` | Mean assignment confidence |
|
|
199
|
+
| `consensus` | Allele profile, `pos:allele` pairs separated by `\|` |
|
|
200
|
+
|
|
201
|
+
### `strainphase longitudinal` → `--output-dir`
|
|
202
|
+
|
|
203
|
+
| File | One row per |
|
|
204
|
+
|------|-------------|
|
|
205
|
+
| `lineages.tsv` | (lineage, sample): the main output |
|
|
206
|
+
| `haplotypes.tsv` | window-haplotype, with its consensus |
|
|
207
|
+
| `windows_within_sample.tsv` | within-sample track |
|
|
208
|
+
| `windows_across_samples.tsv` | per-window group of samples judged one entity |
|
|
209
|
+
|
|
210
|
+
Diagnostics are written to `tmp/` beside them.
|
|
211
|
+
|
|
212
|
+
`lineages.tsv`:
|
|
213
|
+
|
|
214
|
+
| Column | Description |
|
|
215
|
+
|--------|-------------|
|
|
216
|
+
| `lineage_id` | Lineage identifier |
|
|
217
|
+
| `mag`, `contig`, `sample` | MAG, contig and sample/timepoint |
|
|
218
|
+
| `n_windows_lineage`, `n_samples_lineage` | Windows and samples the lineage covers in total |
|
|
219
|
+
| `window_start`, `window_end` | Lineage window extent |
|
|
220
|
+
| `marker_start`, `marker_end`, `marker_span_bp` | First and last marker position, and the distance between them |
|
|
221
|
+
| `n_windows` | Windows this sample contributes |
|
|
222
|
+
| `abundance` | Reads assigned / non-junk reads, pooled over the lineage's windows in this sample |
|
|
223
|
+
| `abundance_all_reads` | The same over all reads, junk included |
|
|
224
|
+
| `reads`, `total_reads`, `junk_reads` | The counts behind both abundances |
|
|
225
|
+
| `window_group_ids` | Per-window groups this sample contributes to |
|
|
226
|
+
| `haplotype_ids` | Window-haplotype IDs behind the row (join key to `haplotypes.tsv`) |
|
|
227
|
+
|
|
228
|
+
An `abundance` of 0 with non-zero `total_reads` is a measured zero. A (lineage, sample)
|
|
229
|
+
pair with no row was not measured there.
|
|
230
|
+
|
|
231
|
+
## Structural variants
|
|
232
|
+
|
|
233
|
+
`--sv-sidecars` co-phases structural variants with SNVs and indels. Each sample needs a
|
|
234
|
+
sidecar TSV listing its SV events and the reads supporting them; the format is
|
|
235
|
+
documented at the top of `strainphase/sv_encoding.py`. For Sniffles2 calls made with
|
|
236
|
+
`--output-rnames`, `scripts/sniffles_to_sidecar.py` in this repository writes one:
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
python scripts/sniffles_to_sidecar.py --sniffles T1.sv.vcf.gz --out-sidecar sv/T1.tsv
|
|
240
|
+
strainphase sv reconcile --sidecars sv/*.tsv --out-dir sv_reconciled/
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
`reconcile` gives one event the same ID and position in every sample; run it before
|
|
244
|
+
phasing.
|
|
245
|
+
|
|
246
|
+
## Algorithm Overview
|
|
247
|
+
|
|
248
|
+
```
|
|
249
|
+
1. WINDOWS BAM + VCF → overlapping 20 kb windows, 10 kb step
|
|
250
|
+
2. GRAPH read-similarity graph per window → Louvain communities
|
|
251
|
+
3. EM γ responsibilities ↔ π weights and consensus, with a junk component
|
|
252
|
+
4. MERGE within-window: merge haplotypes within identity_distance;
|
|
253
|
+
a one-marker difference is adjudicated against a sequencing-error null
|
|
254
|
+
5. LINK chain a sample's haplotypes across windows into tracks
|
|
255
|
+
6. RESCUE recover low-abundance haplotypes from other timepoints (longitudinal)
|
|
256
|
+
7. LINEAGES merge tracks across samples into contig-spanning lineages
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## License
|
|
260
|
+
|
|
261
|
+
BSD 3-Clause License - see [LICENSE](LICENSE) for details.
|