-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutload_summary.py
More file actions
318 lines (273 loc) · 10.9 KB
/
mutload_summary.py
File metadata and controls
318 lines (273 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
from __future__ import annotations
import argparse
import html
import sys
from pathlib import Path
import numpy as np
import msprime
from argtest_common import (
aggregate_by_individual,
load_ts,
mutational_load,
resolve_mu_rate,
sample_names,
)
def ascii_bar_html(value, max_value, width=40, color="#777777"):
n = max(0, int(round(value / max_value * width))) if max_value > 0 else 0
return f'<span style="color:{color}">{"█" * n}</span>'
def load_chart_html(load_1d, names, title, outlier_mask=None):
max_val = float(np.max(load_1d)) if load_1d.size else 1.0
label_width = max((len(n) for n in names), default=1)
rows = []
for i, (name, val) in enumerate(zip(names, load_1d)):
is_out = outlier_mask is not None and bool(outlier_mask[i])
color = "#d62728" if is_out else "#444444"
bar = ascii_bar_html(val, max_val, color=color)
label = html.escape(name.ljust(label_width))
rows.append(
f'<span style="color:{color}">{label}</span> {bar} '
f'<span style="color:{color}">{val:.4f}</span>'
)
inner = "\n".join(rows)
return (
f"<h2>{html.escape(title)}</h2>\n"
f'<pre style="line-height:1.4;font-size:12px">{inner}</pre>\n'
)
def lineage_label(name: str) -> str:
for sep in ("_", "-", "."):
if sep in name:
head = name.split(sep, 1)[0].strip()
if head:
return head
return name
def summarize_lineage_flags(names, flagged_mask):
summary: dict[str, list[int]] = {}
for name, flagged in zip(names, flagged_mask):
lineage = lineage_label(name)
bucket = summary.setdefault(lineage, [0, 0])
bucket[0] += 1
if bool(flagged):
bucket[1] += 1
return sorted(
((lineage, total, flagged) for lineage, (total, flagged) in summary.items()),
key=lambda item: (-item[2], item[0]),
)
def lineage_flag_table_html(names, flagged_mask):
rows = summarize_lineage_flags(names, flagged_mask)
body = "\n".join(
f"<tr><td>{html.escape(lineage)}</td><td>{flagged}</td><td>{total}</td></tr>"
for lineage, total, flagged in rows
)
return (
"<h2>Flagged individuals by lineage</h2>\n"
"<table>\n"
"<thead><tr><th>Lineage</th><th>Flagged</th><th>Total</th></tr></thead>\n"
f"<tbody>\n{body}\n</tbody>\n"
"</table>\n"
)
def parse_args():
p = argparse.ArgumentParser(
description="Summarize mutational load per individual from a tree sequence",
)
p.add_argument("ts", help="Tree sequence file (.ts, .trees, or .tsz)")
group = p.add_mutually_exclusive_group()
group.add_argument("--window-size", type=float, help="Window size in bp")
group.add_argument(
"--snp-window",
type=int,
help="Window size as a fixed number of variants",
)
p.add_argument(
"--cutoff",
type=float,
default=0.5,
help=(
"Per-window per-individual outlier cutoff as a fraction of the "
"sim-based expected load. Drives both the per-window flag (used "
"to prune (window, individual) pairs) and the residual band shown "
"in the summary (default: 0.5)."
),
)
p.add_argument(
"--mutation-rate",
type=float,
default=None,
help=(
"Scalar mutation-rate fallback when neither ts.metadata nor a sibling "
"*.mut_rate.p file provides a ratemap."
),
)
p.add_argument(
"--random-seed",
type=int,
default=1,
help="Seed for msprime.sim_mutations when computing expected load (default: 1).",
)
p.add_argument(
"--out",
default="mutational_load_summary.html",
help=(
"Output filename (default: mutational_load_summary.html). Only the "
"filename part is used; the file is always written to "
"<repo-root>/results/<filename>."
),
)
p.add_argument(
"--suffix-to-strip",
default="",
help='Suffix stripped from sample names before display (default: "").',
)
return p.parse_args()
def build_bp_windows(ts, window_size: float) -> np.ndarray:
if window_size <= 0:
raise ValueError("--window-size must be > 0")
L = float(ts.sequence_length)
windows = np.arange(0, L + window_size, window_size, dtype=float)
if windows[-1] > L:
windows[-1] = L
return windows
def build_snp_windows(ts, snp_window: int) -> np.ndarray:
if snp_window <= 0:
raise ValueError("--snp-window must be > 0")
positions = np.asarray(ts.sites_position, dtype=float)
L = float(ts.sequence_length)
if positions.size == 0:
return np.array([0.0, L], dtype=float)
edges = positions[snp_window::snp_window]
return np.concatenate((np.array([0.0]), edges, np.array([L])))
def simulate_expected_load(ts, ts_path, windows, names, scalar_rate, seed):
# Single-sim per-individual expected load matrix (windows x individuals).
mu = resolve_mu_rate(ts, ts_path, scalar_fallback=scalar_rate)
sim_ts = msprime.sim_mutations(ts, rate=mu, keep=False, random_seed=seed)
expected = mutational_load(sim_ts, windows=windows)
expected, _ = aggregate_by_individual(expected, names)
return expected
class Tee:
def __init__(self, *streams):
self.streams = streams
def write(self, data):
for s in self.streams:
s.write(data)
s.flush()
def flush(self):
for s in self.streams:
s.flush()
def main():
args = parse_args()
ts_path = Path(args.ts)
repo_root = Path(__file__).resolve().parent.parent
results_dir = repo_root / "results"
logs_dir = repo_root / "logs"
results_dir.mkdir(parents=True, exist_ok=True)
logs_dir.mkdir(parents=True, exist_ok=True)
out = results_dir / Path(args.out).name
log_path = logs_dir / f"{out.stem}.log"
old_stdout = sys.stdout
old_stderr = sys.stderr
with open(log_path, "w") as log_fh:
try:
sys.stdout = Tee(old_stdout, log_fh)
sys.stderr = Tee(old_stderr, log_fh)
ts = load_ts(ts_path)
windows = None
if args.window_size is not None:
windows = build_bp_windows(ts, args.window_size)
elif args.snp_window is not None:
windows = build_snp_windows(ts, args.snp_window)
load = mutational_load(ts, windows=windows)
names = sample_names(ts, suffix_to_strip=args.suffix_to_strip)
load, unique_names = aggregate_by_individual(load, names)
body_parts = []
meta_lines = [
f"Input: {html.escape(ts_path.name)} | "
f"Samples: {ts.num_samples} | "
f"Individuals: {len(unique_names)}"
]
if windows is None:
body_parts.append(load_chart_html(load, unique_names, "Mutational load"))
else:
expected = simulate_expected_load(
ts, ts_path, windows, names,
scalar_rate=args.mutation_rate,
seed=args.random_seed,
)
high = (1 + args.cutoff) * expected
low = (1 - args.cutoff) * expected
per_window_outlier = (load > high) | (load < low)
# Residual: sum obs and exp only over (sample, window) pairs
# that survive the per-window flag. With one cutoff knob the
# residual ratio is bounded inside the band by construction;
# the residual flag still triggers for floating-point or zero-
# expected edge cases and acts as a defensive recommendation.
kept = (~per_window_outlier).astype(float)
obs_residual = (load * kept).sum(axis=0)
exp_residual = (expected * kept).sum(axis=0)
high_total = (1 + args.cutoff) * exp_residual
low_total = (1 - args.cutoff) * exp_residual
flagged_mask = (obs_residual > high_total) | (obs_residual < low_total)
body_parts.append(
load_chart_html(
obs_residual, unique_names,
"Residual mutational load per individual "
"(observed total, summed only over windows kept after "
"per-window pruning)",
outlier_mask=flagged_mask,
)
)
pruned_pairs = int(per_window_outlier.sum())
total_pairs = int(load.size)
meta_lines.append(
f"{pruned_pairs} of {total_pairs} (window, individual) pairs "
f"flagged for trimming"
)
flagged_count = int(flagged_mask.sum())
total_inds = int(load.shape[1])
if flagged_count:
meta_lines.append(
f"{flagged_count} of {total_inds} individuals still "
f"outside the cutoff band after pruning — consider "
f"manual whole-sample removal"
)
else:
meta_lines.append(
f"All {total_inds} individuals within the cutoff band "
f"after pruning"
)
body_parts.append(lineage_flag_table_html(unique_names, flagged_mask))
if args.window_size is not None:
meta_lines.append(
f"Window size: {int(args.window_size)} bp | "
f"Outlier cutoff: {args.cutoff:.3f} of sim expectation"
)
elif args.snp_window is not None:
meta_lines.append(
f"Window size: {int(args.snp_window)} variants | "
f"Outlier cutoff: {args.cutoff:.3f} of sim expectation"
)
meta_html = "\n".join(
f'<div class="meta">{m}</div>' for m in meta_lines
)
html_doc = f"""<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Mutational load summary</title>
<style>
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 24px; }}
h1 {{ font-size: 20px; }}
h2 {{ font-size: 16px; margin-top: 24px; }}
.meta {{ color: #444; font-size: 13px; margin-bottom: 8px; }}
pre {{ background: #f8f8f8; padding: 12px; border-radius: 4px; overflow-x: auto; }}
table {{ border-collapse: collapse; margin-bottom: 20px; }}
th, td {{ border: 1px solid #cccccc; padding: 6px 10px; text-align: left; }}
</style>
<h1>Mutational load summary</h1>
{meta_html}
{"".join(body_parts)}</html>
"""
out.write_text(html_doc)
finally:
sys.stdout = old_stdout
sys.stderr = old_stderr
if __name__ == "__main__":
main()