strainphase 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- strainphase/__init__.py +76 -0
- strainphase/cli.py +532 -0
- strainphase/coherence.py +69 -0
- strainphase/core.py +3719 -0
- strainphase/lineages.py +102 -0
- strainphase/longitudinal.py +853 -0
- strainphase/simulation/__init__.py +17 -0
- strainphase/simulation/synthetic_data.py +486 -0
- strainphase/sv_encoding.py +514 -0
- strainphase/track_merge.py +242 -0
- strainphase/window_groups.py +64 -0
- strainphase-0.1.0.dist-info/METADATA +297 -0
- strainphase-0.1.0.dist-info/RECORD +17 -0
- strainphase-0.1.0.dist-info/WHEEL +5 -0
- strainphase-0.1.0.dist-info/entry_points.txt +2 -0
- strainphase-0.1.0.dist-info/licenses/LICENSE +29 -0
- strainphase-0.1.0.dist-info/top_level.txt +1 -0
strainphase/lineages.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Lineage containers: one merged entity, and its pooled per-sample abundance.
|
|
3
|
+
|
|
4
|
+
The construction logic (``build_lineages`` and the cross-window chaining pass) was
|
|
5
|
+
removed once ``track_merge.build_lineages_from_tracks`` replaced steps 2 and 3; what
|
|
6
|
+
remains is the data these produce.
|
|
7
|
+
|
|
8
|
+
A lineage's per-sample abundance is pooled - ``sum(reads) / sum(total_reads)`` over
|
|
9
|
+
its windows in that sample - never the mean of the per-window ratios, which would
|
|
10
|
+
inflate the estimate. See docs/design/lineages.md.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
from strainphase.window_groups import WindowGroup
|
|
19
|
+
|
|
20
|
+
__all__ = ["Lineage", "PooledAbundance"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class PooledAbundance:
|
|
25
|
+
"""One lineage's pooled read counts in one sample, with both denominators.
|
|
26
|
+
|
|
27
|
+
Counts are pooled, never averaged: sum(reads) / sum(denominator) over the windows
|
|
28
|
+
the lineage occupies in that sample. See docs/design/lineages.md.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
abundance: float # reads / phased reads
|
|
32
|
+
abundance_all_reads: float # reads / (phased + junk)
|
|
33
|
+
reads: int
|
|
34
|
+
total_reads: int
|
|
35
|
+
junk_reads: int
|
|
36
|
+
n_windows: int
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class Lineage:
|
|
41
|
+
"""A chain of window groups along one contig."""
|
|
42
|
+
|
|
43
|
+
lineage_id: str
|
|
44
|
+
contig: str
|
|
45
|
+
groups: list[WindowGroup] = field(default_factory=list)
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def n_windows(self) -> int:
|
|
49
|
+
return len({g.window_start for g in self.groups})
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def window_start(self) -> int:
|
|
53
|
+
return min(g.window_start for g in self.groups)
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def window_end(self) -> int:
|
|
57
|
+
return max(g.window_end for g in self.groups)
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def samples(self) -> set[str]:
|
|
61
|
+
return {m.sample for g in self.groups for m in g.members}
|
|
62
|
+
|
|
63
|
+
def abundance_by_sample(self) -> dict[str, "PooledAbundance"]:
|
|
64
|
+
"""Pooled abundance per sample, both denominators.
|
|
65
|
+
|
|
66
|
+
Pooled, not averaged: ``sum(supporting reads) / sum(non-junk reads)`` over the
|
|
67
|
+
windows this lineage occupies in that sample - the same estimator step 1 uses
|
|
68
|
+
for a track. ``abundance`` divides by the reads that phased;
|
|
69
|
+
``abundance_all_reads`` divides by every read at those loci, so a
|
|
70
|
+
poorly-resolving window pulls it down. ``n_windows`` and raw counts are returned
|
|
71
|
+
so a consumer can require a minimum. See docs/design/lineages.md.
|
|
72
|
+
"""
|
|
73
|
+
acc: dict[str, list[int]] = {}
|
|
74
|
+
# Denominators are a per-(sample, window) constant, accumulated once per window
|
|
75
|
+
# while the numerator sums over members; counting per haplotype would halve the
|
|
76
|
+
# abundance where a strain split into two near-identical haplotypes.
|
|
77
|
+
counted: set[tuple[str, int]] = set()
|
|
78
|
+
for g in self.groups:
|
|
79
|
+
for m in g.members:
|
|
80
|
+
a = acc.setdefault(m.sample, [0, 0, 0, 0])
|
|
81
|
+
a[0] += m.reads
|
|
82
|
+
cell = (m.sample, g.window_start)
|
|
83
|
+
if cell in counted:
|
|
84
|
+
continue
|
|
85
|
+
counted.add(cell)
|
|
86
|
+
a[1] += m.total_reads
|
|
87
|
+
a[2] += m.junk_reads
|
|
88
|
+
a[3] += 1
|
|
89
|
+
out = {}
|
|
90
|
+
for smp, (k, n, j, w) in acc.items():
|
|
91
|
+
out[smp] = PooledAbundance(
|
|
92
|
+
abundance=(k / n) if n else float("nan"),
|
|
93
|
+
abundance_all_reads=(k / (n + j)) if (n + j) else float("nan"),
|
|
94
|
+
reads=k, total_reads=n, junk_reads=j, n_windows=w)
|
|
95
|
+
return out
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def marker_span(self) -> tuple[int, int]:
|
|
99
|
+
"""First and last marker position - what the lineage actually resolves, not the
|
|
100
|
+
tiles it nominally covers."""
|
|
101
|
+
pos = [p for g in self.groups for m in g.members for p in m.consensus]
|
|
102
|
+
return (min(pos), max(pos)) if pos else (0, 0)
|