-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_bfb_bridge.py
More file actions
432 lines (381 loc) · 15.8 KB
/
Copy pathcpp_bfb_bridge.py
File metadata and controls
432 lines (381 loc) · 15.8 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""
cpp_bfb_bridge.py - wrapper around the Ambigram C++ BFB ILP solver
(Lu/Yu et al., Nat Commun 2023, s41467-023-41259-w).
Provides candidate BFB derivative chromosome paths for v4's posterior ranker.
Workflow:
1. Auto-detect Ambigram binary, htslib, and cbc from environment variables
or command-line overrides.
2. Given a per-clone .txt file (Ambigram-canonical format), invoke
`Ambigram --op bfb --in_lh CLONE.txt --lp_prefix PREFIX [--all]`
3. Parse the BFB path string from stdout (last non-Cbc line containing
`\\d+[+-]\\|` patterns)
4. Convert path syntax `3-2-|1+2+3+2+|3-2-` to v4 path token list
`['3-', '2-', '1+', '2+', '3+', '2+', '3-', '2-']`
(the `|` separator marks foldback inversions but is already implicit in
the alternating signs; `||` marks translocations between chromosomes)
5. Optionally synthesize a per-clone .txt file from a multi-clone .lh + clone CN column.
"""
from __future__ import annotations
import os
import re
import subprocess
import tempfile
from collections import defaultdict
from typing import Dict, List, Optional, Tuple
# --- portable defaults, overridable by environment or caller ------------------
DEFAULT_BINARY = os.environ.get("SVGRAM_AMBIGRAM_BINARY", "Ambigram")
DEFAULT_HTSLIB_DIR = os.environ.get("SVGRAM_HTSLIB_DIR", "")
DEFAULT_CBC_DIR = os.environ.get("SVGRAM_CBC_DIR", "")
DEFAULT_TIMEOUT = 60
# --- output parsing -----------------------------------------------------------
# A BFB path token: digits + sign, e.g. "3+", "12-".
TOKEN_RE = re.compile(r"\d+[+\-]")
# A line is a candidate BFB-path output line if it contains at least 2 tokens
# and may contain the FBI ('|') or translocation ('||') separators.
PATH_LINE_RE = re.compile(r"^[\d+\-|]+$")
def parse_bfb_path_line(line: str) -> List[str]:
"""Convert `3-2-|1+2+3+2+|3-2-` -> ['3-','2-','1+','2+','3+','2+','3-','2-'].
`|` and `||` are dropped — they are descriptive separators (FBI / translocation)
but path semantics are fully captured by the token sequence + signs.
"""
line = line.strip()
if not line:
return []
return TOKEN_RE.findall(line)
def extract_bfb_paths(stdout_text: str) -> List[List[str]]:
"""Scan binary stdout for BFB-path lines.
The Ambigram binary prints BFB paths via `printBFB()` to stdout, mixed in
with Cbc solver chatter. A BFB-path line consists exclusively of segment
tokens (digit+sign) and `|` separators.
"""
out: List[List[str]] = []
for raw in stdout_text.splitlines():
s = raw.strip()
if not s:
continue
# quick reject: must contain at least one digit+sign token, and only
# consist of tokens + `|`
if not TOKEN_RE.search(s):
continue
# remove all tokens + pipes; remainder must be empty for a path line
residue = TOKEN_RE.sub("", s).replace("|", "")
if residue:
continue
tokens = parse_bfb_path_line(s)
if len(tokens) >= 2:
out.append(tokens)
# de-dup while preserving order
seen = set()
uniq: List[List[str]] = []
for p in out:
key = tuple(p)
if key in seen:
continue
seen.add(key)
uniq.append(p)
return uniq
# --- env + binary invocation --------------------------------------------------
def build_env(htslib_dir: str = DEFAULT_HTSLIB_DIR,
cbc_dir: str = DEFAULT_CBC_DIR) -> Dict[str, str]:
"""Build the os.environ extension required for the binary to run."""
env = os.environ.copy()
ld_paths = [p for p in (htslib_dir, env.get("LD_LIBRARY_PATH", ""))
if p]
env["LD_LIBRARY_PATH"] = ":".join(ld_paths)
if cbc_dir and cbc_dir not in env.get("PATH", "").split(":"):
env["PATH"] = cbc_dir + ":" + env.get("PATH", "")
return env
def run_bfb_binary(binary: str,
in_txt: str,
*,
lp_prefix: Optional[str] = None,
all_paths: bool = True,
reversed_strand: bool = False,
timeout: int = DEFAULT_TIMEOUT,
env: Optional[Dict[str, str]] = None) -> Tuple[int, str, str]:
"""Invoke the C++ BFB binary on a per-clone .txt file.
Returns (returncode, stdout, stderr). Caller parses stdout via
`extract_bfb_paths`.
"""
if env is None:
env = build_env()
if lp_prefix is None:
# binary writes <prefix>.lp / .sol / .mps.gz alongside; use a tempdir
td = tempfile.mkdtemp(prefix="cppbfb_")
lp_prefix = os.path.join(td, "run")
# The Ambigram binary internally reads `./<lp_prefix>.sol` after cbc writes
# `<lp_prefix>.sol`, so an absolute lp_prefix becomes `./tmp/...` which
# resolves relative to cwd and breaks. Workaround: split into workdir
# (chdir target) + basename (relative prefix passed to the binary).
workdir = os.path.dirname(lp_prefix) or os.getcwd()
lp_basename = os.path.basename(lp_prefix)
cmd = [binary, "--op", "bfb",
"--in_lh", in_txt,
"--lp_prefix", lp_basename]
if all_paths:
cmd.append("--all")
if reversed_strand:
cmd.append("--reversed")
try:
proc = subprocess.run(
cmd, cwd=workdir, env=env,
capture_output=True, text=True,
timeout=timeout,
)
return proc.returncode, proc.stdout, proc.stderr
except subprocess.TimeoutExpired as e:
return 124, e.stdout or "", e.stderr or "timeout"
# --- per-clone .txt synthesis from subclone .lh -------------------------------
def synthesize_per_clone_txt(
subclone_lh: str,
clone_name: str,
clone_index: int,
out_path: str,
) -> str:
"""Generate an Ambigram-canonical per-clone .txt from a subclone .lh.
Used when the per-clone .txt does not already exist next to the .lh.
Subclone .lh format observed:
SAMPLE clone0,clone1,clone2
SOURCE 1
SINK 4
SEG H:N:chr:start:end CN_clone0 CN_clone1 CN_clone2
JUNC H:u:dir H:v:dir aggregate_count [other]
Output Ambigram .txt format observed:
SOURCE 1 12
SINK 4 8
SEG H:N:chr:start:end CN read_count
JUNC H:u:dir H:v:dir count read_count U B
For JUNC counts we lack per-clone splits in the .lh, so we apportion the
aggregate count proportionally to clone CN coverage on the involved
endpoints (best-effort fallback). Real per-clone .txt files are preferred.
"""
sample = None
sources: List[Tuple[int, int]] = [] # (segid, count) — count fabricated as 1
sinks: List[Tuple[int, int]] = []
seg_lines: List[Tuple[str, List[int]]] = [] # (raw_seg_token, all CN values)
junc_lines: List[Tuple[str, str, int]] = [] # (u_token, v_token, agg_count)
with open(subclone_lh) as fh:
for raw in fh:
parts = raw.strip().split()
if not parts:
continue
tag = parts[0]
if tag == "SAMPLE":
sample = parts[1]
elif tag == "SOURCE":
segid = int(parts[1])
sources.append((segid, 1))
elif tag == "SINK":
segid = int(parts[1])
sinks.append((segid, 1))
elif tag == "SEG":
token = parts[1]
cns = [int(round(float(x))) for x in parts[2:]]
seg_lines.append((token, cns))
elif tag in ("JUNC", "REF"):
u, v = parts[1], parts[2]
# take the first numeric field after the two endpoint tokens
try:
agg = int(parts[3])
except (IndexError, ValueError):
agg = 0
junc_lines.append((u, v, agg))
if sample is None:
raise ValueError(f"{subclone_lh}: missing SAMPLE line")
clone_names = sample.split(",")
if clone_index >= len(clone_names):
raise ValueError(
f"{subclone_lh}: clone_index {clone_index} out of range "
f"({len(clone_names)} clones)")
if not seg_lines:
raise ValueError(f"{subclone_lh}: no SEG lines")
# Per-clone segment CN
seg_cn: Dict[int, int] = {}
for token, cns in seg_lines:
segid = int(token.split(":")[1])
# If this clone has no CN value for this seg, fall back to last column
cn = cns[clone_index] if clone_index < len(cns) else cns[-1]
seg_cn[segid] = cn
with open(out_path, "w") as out:
# SOURCE / SINK lines (fabricate count = clone CN at that segment)
for segid, _ in sources:
out.write(f"SOURCE {segid} {seg_cn.get(segid, 1)}\n")
for segid, _ in sinks:
out.write(f"SINK {segid} {seg_cn.get(segid, 1)}\n")
# SEG lines: keep token; CN = clone-specific; read_count fabricated as
# ratio of clone CN to the AVG_DP-equivalent (use cn directly)
for token, cns in seg_lines:
segid = int(token.split(":")[1])
cn = seg_cn[segid]
# fabricate read_count proportional to cn (Ambigram only checks > 0)
read_count = max(1, cn)
out.write(f"SEG {token} {cn} {read_count}\n")
# JUNC lines: apportion aggregate count by clone CN at the endpoints
# ratio = clone_cn_avg(u,v) / sum_over_clones(cn_avg(u,v))
clone_count = len(clone_names)
for u_tok, v_tok, agg in junc_lines:
try:
u_id = int(u_tok.split(":")[1])
v_id = int(v_tok.split(":")[1])
except (IndexError, ValueError):
continue
# sum-of-clone-CN-averages on this edge's endpoints
u_cns = next(
(cns for tok, cns in seg_lines
if int(tok.split(":")[1]) == u_id), [agg]
)
v_cns = next(
(cns for tok, cns in seg_lines
if int(tok.split(":")[1]) == v_id), [agg]
)
this_clone = (
(u_cns[clone_index] if clone_index < len(u_cns) else u_cns[-1]) +
(v_cns[clone_index] if clone_index < len(v_cns) else v_cns[-1])
)
total = sum(
((u_cns[i] if i < len(u_cns) else u_cns[-1]) +
(v_cns[i] if i < len(v_cns) else v_cns[-1]))
for i in range(clone_count)
)
count = int(round(agg * (this_clone / total))) if total > 0 else 0
count = max(0, count)
if count == 0:
continue
read_count = count # placeholder
out.write(
f"JUNC {u_tok} {v_tok} {count} {read_count} U B\n"
)
return out_path
# --- per-clone .txt discovery -------------------------------------------------
def discover_per_clone_txt(subclone_lh: str,
clone_name: str) -> Optional[str]:
"""Look for an existing per-clone .txt file next to the subclone .lh.
Convention observed in user's data:
<prefix>.lh -> subclone graph
<prefix>_<clone_name>.txt -> per-clone Ambigram input
Returns the path if found, else None.
"""
base = subclone_lh
if base.endswith(".lh"):
base = base[:-3]
candidates = [
f"{base}_{clone_name}.txt",
f"{base}.{clone_name}.txt",
f"{base}_with_ref_{clone_name}.txt", # observed in cell_line data
]
for c in candidates:
if os.path.isfile(c):
return c
return None
# --- top-level convenience ---------------------------------------------------
def get_bfb_candidates_for_clone(
*,
subclone_lh: str,
clone_name: str,
clone_index: int,
binary: str = DEFAULT_BINARY,
htslib_dir: str = DEFAULT_HTSLIB_DIR,
cbc_dir: str = DEFAULT_CBC_DIR,
timeout: int = DEFAULT_TIMEOUT,
all_paths: bool = True,
workdir: Optional[str] = None,
verbose: bool = False,
) -> Tuple[List[List[str]], str]:
"""Return BFB path candidates for one clone.
Returns (paths, info_str).
paths: list of token lists (each is one BFB-path candidate).
info_str: short status (which .txt was used, return code, # paths).
On any failure (binary not found, segfault, parse failure) returns ([], info).
"""
if not os.path.isfile(binary):
return [], {'src': 'none', 'rc': -1, 'n_paths': 0,
'stderr_head': f"binary not found: {binary}"}
# 1. find or synthesize the per-clone .txt
in_txt = discover_per_clone_txt(subclone_lh, clone_name)
synthesized = False
if in_txt is None:
if workdir is None:
workdir = tempfile.mkdtemp(prefix="cppbfb_synth_")
os.makedirs(workdir, exist_ok=True)
in_txt = os.path.join(workdir, f"clone_{clone_name}.txt")
try:
synthesize_per_clone_txt(
subclone_lh, clone_name, clone_index, in_txt)
synthesized = True
except Exception as e:
return [], {'src': 'synth_failed', 'rc': -1, 'n_paths': 0,
'stderr_head': f"synth failed: {e}"}
# 2. invoke binary
env = build_env(htslib_dir=htslib_dir, cbc_dir=cbc_dir)
if workdir is None:
workdir = tempfile.mkdtemp(prefix="cppbfb_run_")
os.makedirs(workdir, exist_ok=True)
lp_prefix = os.path.join(workdir, f"clone_{clone_name}_run")
rc, stdout, stderr = run_bfb_binary(
binary, in_txt, lp_prefix=lp_prefix,
all_paths=all_paths, timeout=timeout, env=env,
)
if verbose:
print(f"[cpp_bfb] clone={clone_name} txt={in_txt} "
f"synth={synthesized} rc={rc}")
# 3. parse stdout (defensive: TimeoutExpired may yield bytes; decode safely)
if isinstance(stdout, (bytes, bytearray)):
stdout = stdout.decode('utf-8', errors='replace')
if isinstance(stderr, (bytes, bytearray)):
stderr = stderr.decode('utf-8', errors='replace')
paths = extract_bfb_paths(stdout)
info = {
'src': 'synth' if synthesized else 'preexisting',
'rc': rc,
'n_paths': len(paths),
'in_txt': in_txt,
'stderr_head': stderr[:200] if (rc != 0 and not paths) else '',
}
return paths, info
def get_bfb_candidates_for_txt(
*,
in_txt: str,
clone_name: str,
binary: str = DEFAULT_BINARY,
htslib_dir: str = DEFAULT_HTSLIB_DIR,
cbc_dir: str = DEFAULT_CBC_DIR,
timeout: int = DEFAULT_TIMEOUT,
all_paths: bool = True,
workdir: Optional[str] = None,
verbose: bool = False,
) -> Tuple[List[List[str]], str]:
"""Return freshly generated BFB candidates from one clone-resolved graph.
This direct mode is for v5.3 ``--clone-graph`` inputs. It invokes the C++
BFB ILP solver on ``in_txt`` and parses stdout. It never discovers or
reads existing ``.path`` outputs.
"""
if not os.path.isfile(binary):
return [], {'src': 'none', 'rc': -1, 'n_paths': 0,
'stderr_head': f"binary not found: {binary}"}
if not os.path.isfile(in_txt):
return [], {'src': 'missing_txt', 'rc': -1, 'n_paths': 0,
'stderr_head': f"input graph not found: {in_txt}"}
env = build_env(htslib_dir=htslib_dir, cbc_dir=cbc_dir)
if workdir is None:
workdir = tempfile.mkdtemp(prefix="cppbfb_direct_")
os.makedirs(workdir, exist_ok=True)
lp_prefix = os.path.join(workdir, f"clone_{clone_name}_run")
rc, stdout, stderr = run_bfb_binary(
binary, in_txt, lp_prefix=lp_prefix,
all_paths=all_paths, timeout=timeout, env=env,
)
if verbose:
print(f"[cpp_bfb] clone={clone_name} txt={in_txt} direct rc={rc}")
if isinstance(stdout, (bytes, bytearray)):
stdout = stdout.decode('utf-8', errors='replace')
if isinstance(stderr, (bytes, bytearray)):
stderr = stderr.decode('utf-8', errors='replace')
paths = extract_bfb_paths(stdout)
info = {
'src': 'direct_txt',
'rc': rc,
'n_paths': len(paths),
'in_txt': in_txt,
'stderr_head': stderr[:200] if (rc != 0 and not paths) else '',
}
return paths, info