-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathassembly.py
More file actions
1403 lines (1138 loc) · 48.4 KB
/
assembly.py
File metadata and controls
1403 lines (1138 loc) · 48.4 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
r"""Assembly module for InstaNexus.
██████████ ███████████ █████ █████
░░███░░░░███ ░█░░░███░░░█░░███ ░░███
░███ ░░███░ ░███ ░ ░███ ░███
░███ ░███ ░███ ░███ ░███
░███ ░███ ░███ ░███ ░███
░███ ███ ░███ ░███ ░███
██████████ █████ ░░████████
░░░░░░░░░░ ░░░░░ ░░░░░░░░
__authors__ = Marco Reverenna & Konstantinos Kalogeropoulus
__copyright__ = Copyright 2024-2025
__research-group__ = DTU Biosustain (Multi-omics Network Analytics) and DTU Bioengineering
__date__ = 14 Nov 2025
__maintainer__ = Marco Reverenna
__email__ = marcor@dtu.dk
__status__ = Dev
"""
# import libraries
import argparse
import ast
import logging
import math
from collections import Counter, defaultdict
from dataclasses import dataclass
from itertools import combinations
from pathlib import Path
from typing import Dict, Iterable, List, Optional
import Bio
import networkx as nx
import pandas as pd
from tqdm import tqdm
from . import helpers, preprocessing
from . import visualization as viz
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
MAX_REFINE_ROUNDS = 10
def find_peptide_overlaps(peptides, min_overlap):
"""Finds overlaps between peptide sequences using a greedy approach."""
overlaps = defaultdict(list)
for index_a, peptide_a in tqdm(enumerate(peptides), desc="Finding overlaps"):
for index_b, peptide_b in enumerate(peptides):
if index_a != index_b: # Skip comparing the same peptide
max_possible_overlap = min(len(peptide_a), len(peptide_b))
for overlap_length in range(min_overlap, max_possible_overlap):
if peptide_a[-overlap_length:] == peptide_b[:overlap_length]:
overlaps[index_a].append((index_b, overlap_length)) # Add the overlap to the dictionary
if peptide_b[-overlap_length:] == peptide_a[:overlap_length]:
overlaps[index_b].append((index_a, overlap_length)) # Add the overlap to the dictionary
return overlaps
def assemble_contigs_greedy(peptides, min_overlap):
"""Assembles contigs from peptide sequences using a greedy approach."""
assembled_contigs = peptides[:]
iteration = 0
MAX_ITERATIONS = 50
while iteration < MAX_ITERATIONS:
iteration += 1
overlaps = find_peptide_overlaps(assembled_contigs, min_overlap)
if not overlaps:
break
new_contigs = []
used_indices = set()
# Process overlaps deterministically
for i in sorted(overlaps.keys()): # ensure deterministic order
if i in used_indices:
continue
# Sort overlaps_list deterministically: prioritize longer overlap, then lower index
overlaps_list = sorted(overlaps[i], key=lambda x: (-x[1], x[0]))
best_match = overlaps_list[0] if overlaps_list else None
if best_match:
j, overlap_len = best_match
if j not in used_indices:
new_contig = assembled_contigs[i] + assembled_contigs[j][overlap_len:]
new_contigs.append(new_contig)
used_indices.update([i, j])
# Add unused peptides
remaining_contigs = [contig for idx, contig in enumerate(assembled_contigs) if idx not in used_indices]
assembled_contigs = new_contigs + remaining_contigs
if len(new_contigs) == 0:
break
if iteration >= MAX_ITERATIONS:
logger.warning(f"Greedy assembly stopped after max iterations ({MAX_ITERATIONS}).")
return assembled_contigs
def merge_contigs_greedy(contigs):
"""Merges overlapping contigs into a set of unique contigs."""
contigs = sorted(contigs, key=len, reverse=True)
merged = set(contigs)
for c in tqdm(contigs, desc="Merging contigs"):
# print(c)
for c2 in contigs:
if c != c2 and c2 in c: # if c is a substring of c2
merged.discard(c2)
return list(merged)
def combine_seqs_into_scaffolds(contigs, min_overlap):
"""Combine contigs based on a minimum overlap length."""
overlaps = find_overlaps(contigs, min_overlap=min_overlap, disable_tqdm=True)
combined_contigs = []
for a, b, overlap in overlaps:
combined = a + b[overlap:]
combined_contigs.append(combined)
return combined_contigs + contigs
def scaffold_iterative_greedy(contigs, min_overlap, size_threshold, disable_tqdm=False):
"""Iterative scaffolding using Greedy approach."""
def clean(seqs):
"""Remove duplicates, filter by length, and sort by descending size."""
seqs = list(set(seqs))
seqs = [s for s in seqs if len(s) > size_threshold]
return sorted(seqs, key=len, reverse=True)
current = clean(contigs)
MAX_ROUNDS = 10
logger.info(f"Starting iterative scaffolding (Max rounds: {MAX_ROUNDS})...")
for i in range(MAX_ROUNDS):
next_round = combine_seqs_into_scaffolds(current, min_overlap)
next_round = merge_contigs_greedy(next_round)
next_round = clean(next_round)
if len(next_round) == len(current):
break
current = next_round
logger.info(f" Round {i + 1}: {len(current)} contigs")
return current
def get_weighted_kmers_from_df(
df: pd.DataFrame,
kmer_size: int,
use_abundance: bool = True,
use_quality: bool = True,
) -> Counter:
"""
Generates k-mer weights integrating multiple data modalities.
SCORING LOGIC:
1. Sequence Confidence (Deep Learning): Geometric Mean of 'instanovo_token_log_probabilities'.
2. Abundance (MS1): Logarithmic scaling of 'peptide_abundance'.
3. Quality (MS2): Linear boost from 'ion_match_intensity'.
4. Physics (iRT): Exponential penalty for 'iRT error'.
"""
kmer_weights = Counter()
# Column mapping
col_tokens = "instanovo_token_log_probabilities"
col_ms1_abundance = "peptide_abundance" # MS1 Area
col_ms2_intensity = "ion_match_intensity" # MS2 Quality (0-1)
col_irt = "iRT error"
for _, row in df.iterrows():
sequence = row.get("cleaned_preds")
# Skip invalid sequences
if not isinstance(sequence, str) or len(sequence) < kmer_size:
continue
# --- 1. PARSE TOKEN PROBABILITIES (Keep in Log-Space) ---
log_probs = []
raw_probs_str = row.get(col_tokens)
if isinstance(raw_probs_str, str) and raw_probs_str.startswith("["):
try:
parsed_log_probs = ast.literal_eval(raw_probs_str)
# Handle Start/End Tokens [SOS, A, B, C, EOS] -> [A, B, C]
if len(parsed_log_probs) == len(sequence) + 2:
log_probs = parsed_log_probs[1:-1]
elif len(parsed_log_probs) == len(sequence):
log_probs = parsed_log_probs
else:
log_probs = [0.0] * len(sequence) # Fallback: 100% prob
except Exception:
log_probs = [0.0] * len(sequence)
else:
log_probs = [0.0] * len(sequence)
# --- 2. CALCULATE GLOBAL PEPTIDE SCORE ---
global_multiplier = 1.0
if use_abundance:
# A. Base Weight: MS1 Abundance (Log Scale)
ms1_val = row.get(col_ms1_abundance, 0)
if pd.notnull(ms1_val) and ms1_val > 0:
# log10(1e6) = 6.0. Adding 10 ensures we don't get log(small number) issues.
base_weight = math.log10(float(ms1_val) + 10)
else:
base_weight = 1.0 # Default if MS1 missing
# B. Quality Boost: MS2 Intensity (0-1 range)
ms2_val = row.get(col_ms2_intensity, 0)
ms2_boost = 1.0
if pd.notnull(ms2_val):
# Linear boost: 0.1 -> 1.3x, 0.9 -> 3.7x
ms2_boost = 1.0 + (float(ms2_val) * 3.0)
global_multiplier = base_weight * ms2_boost
# C. Quality Penalty: iRT Error
if use_quality:
irt_err = row.get(col_irt, None)
is_missing = row.get("is_missing_irt_error", False)
pred_irt = row.get("predicted iRT", 0)
# Apply penalty ONLY if data is valid and error exists
# We ignore missing flags and dummy values (-30)
if pd.notnull(irt_err) and not is_missing and pred_irt > -20:
try:
# Exponential decay (Sigma = 20.0 to handle long tails)
global_multiplier *= math.exp(-abs(float(irt_err)) / 20.0)
except ValueError:
pass
# --- 3. K-MER WEIGHTING (Geometric Mean) ---
for i in range(len(sequence) - kmer_size + 1):
kmer = sequence[i : i + kmer_size]
# Extract log-probs for this specific k-mer
if i + kmer_size <= len(log_probs):
sub_logs = log_probs[i : i + kmer_size]
# Geometric Mean Calculation
# Math: GeoMean(p1...pn) = exp( average(ln(p1)...ln(pn)) )
if sub_logs:
avg_log_prob = sum(sub_logs) / len(sub_logs)
local_confidence = math.exp(avg_log_prob)
else:
local_confidence = 1.0
else:
local_confidence = 1.0
# Final Weight = Global (MS1/MS2/iRT) * Local (Token Prob)
weight = global_multiplier * local_confidence
kmer_weights[kmer] += weight
return kmer_weights
def get_debruijn_edges_from_kmers(kmers):
"""Generate edges of a De Bruijn graph from a list of k-mers."""
edges = set()
k_1mers = defaultdict(set)
for kmer in kmers:
k_1mers[kmer[:-1]].add(kmer[1:])
for prefix in k_1mers:
for suffix in k_1mers[prefix]:
edges.add((prefix, suffix))
return edges
def assemble_contigs_dbg(edges):
"""Assemble contigs from De Bruijn graph edges by traversing the graph; it takes a set of directed edges representing
a De Bruijn graph and assembles contigs by performing a depth-first traversal.
"""
graph = defaultdict(list)
for start, end in edges:
graph[start].append(end)
# find starting nodes (nodes with no incoming edges)
all_ends = set(e for _, e in edges)
start_nodes = set(graph.keys()) - all_ends
def traverse_iterative(start_node):
"""Traverse a graph iteratively to find paths (contigs) starting from a given node."""
stack = [(start_node, start_node)]
visited = set()
while stack:
node, path = stack.pop()
if node not in visited:
visited.add(node)
if node not in graph or not graph[node]: # end of a path
contigs.append(path)
else:
for next_node in graph[node]:
stack.append((next_node, path + next_node[-1]))
contigs = []
for start_node in tqdm(start_nodes, desc="Traversing nodes"):
traverse_iterative(start_node)
contigs = sorted(contigs, key=len, reverse=True)
contigs = list(set(contigs))
return contigs
def find_overlaps(contigs, min_overlap, disable_tqdm=False):
"""Find overlaps between pairs of contigs based on specified minimum overlap."""
overlaps = []
total_pairs = sum(1 for _ in combinations(contigs, 2))
with tqdm(total=total_pairs, desc="Finding overlaps", disable=disable_tqdm) as pbar:
for a, b in combinations(contigs, 2): # combinations() generates all pairs of contigs
for i in range(min_overlap, min(len(a), len(b)) + 1): # Check overlaps of different lengths
if a[-i:] == b[:i]:
overlaps.append((a, b, i))
if b[-i:] == a[:i]:
overlaps.append((b, a, i))
pbar.update(1)
return overlaps
def create_scaffolds(contigs, min_overlap, disable_tqdm=False):
"""Create scaffolds from a list of contigs by merging overlapping sequences."""
overlaps = find_overlaps(contigs, min_overlap=min_overlap, disable_tqdm=disable_tqdm)
combined_contigs = []
for a, b, overlap in tqdm(overlaps, desc="Merging overlaps", total=len(overlaps), disable=disable_tqdm):
combined = a + b[overlap:]
combined_contigs.append(combined)
return combined_contigs + contigs
def merge_sequences_dbg(contigs, disable_tqdm=False):
"""Merges overlapping sequences."""
contigs = sorted(contigs, key=len, reverse=True)
merged = set(contigs)
for c in tqdm(contigs, desc="Merging contigs", disable=disable_tqdm):
for c2 in contigs:
if c != c2 and c2 in c: # if c2 is a substring of c
merged.discard(c2)
return list(merged)
def scaffold_iterative_dbg(contigs, min_overlap, size_threshold, disable_tqdm=False):
"""Iterative scaffolding using DBG approach."""
prev = None
current = contigs
while prev != current:
prev = current
current = create_scaffolds(current, min_overlap, disable_tqdm)
current = merge_sequences_dbg(current, disable_tqdm)
current = list(set(current))
current = [s for s in current if len(s) > size_threshold]
return sorted(current, key=len, reverse=True)
def get_kmers(sequences: Iterable[str], kmer_size: int) -> List[str]:
"""Generate all k-mers from a list of sequences (preserves duplicates)."""
kmers = []
for seq in sequences:
if not seq:
continue
L = len(seq)
if L < kmer_size:
continue
kmers.extend(seq[i : i + kmer_size] for i in range(L - kmer_size + 1))
return kmers
def get_kmer_counts(kmers: Iterable[str]) -> Counter:
"""Return a Counter of k-mer frequencies."""
return Counter(kmers)
def build_dbg_from_kmers(kmers: Iterable[str], weights: Counter = None) -> nx.DiGraph:
"""
Build a De Bruijn graph.
If 'weights' (Counter) is provided, uses those values for edges.
Otherwise, counts occurrences from the list.
"""
G = nx.DiGraph()
if weights:
iterator = weights.items() # (kmer, calculated_weight)
else:
iterator = Counter(kmers).items() # (kmer, count)
for kmer, weight in iterator:
prefix, suffix = kmer[:-1], kmer[1:]
if G.has_edge(prefix, suffix):
G[prefix][suffix]["weight"] += weight
else:
G.add_edge(prefix, suffix, weight=weight)
return G
def filter_low_weight_edges(G: nx.DiGraph, min_weight: int = 2) -> nx.DiGraph:
"""Remove edges with weight < min_weight (light error correction)."""
to_remove = [(u, v) for u, v, d in G.edges(data=True) if d.get("weight", 0) < min_weight]
G.remove_edges_from(to_remove)
# drop isolated nodes
iso = [n for n in G.nodes if G.in_degree(n) == 0 and G.out_degree(n) == 0]
G.remove_nodes_from(iso)
return G
@dataclass
class ContigPath:
nodes: List[str] # list of (k-1)-mer node labels in path order
seq: str # assembled sequence
weights: List[int] # edge weights along the path
def _extend_linear_path(G: nx.DiGraph, start: str, succ: str) -> ContigPath:
"""Extend from start→succ while in/out-degree == 1 (unbranched)."""
path_nodes = [start, succ]
weights = [G[start][succ]["weight"]]
cur = succ
while G.in_degree(cur) == 1 and G.out_degree(cur) == 1:
nxt = next(iter(G.successors(cur)), None)
if nxt is None:
break
weights.append(G[cur][nxt]["weight"])
path_nodes.append(nxt)
cur = nxt
# build sequence from node labels
seq = path_nodes[0]
for n in path_nodes[1:]:
seq += n[-1]
return ContigPath(nodes=path_nodes, seq=seq, weights=weights)
def assemble_contigs_dbgx(G: nx.DiGraph, min_length: int = 0) -> List[ContigPath]:
"""
Collapse unbranched paths into contigs.
Returns ContigPath items (with sequence + per-edge weights).
"""
contigs: List[ContigPath] = []
# start from "branch" nodes (sources/sinks/branch points)
for node in tqdm(list(G.nodes), desc="Assembling contigs"):
if G.out_degree(node) == 0:
continue
if G.in_degree(node) != 1 or G.out_degree(node) != 1:
for succ in G.successors(node):
cp = _extend_linear_path(G, node, succ)
if len(cp.seq) >= min_length:
contigs.append(cp)
# edge case: pure cycles (every node deg=1/1). Traverse any cycle once.
if not contigs and len(G) > 0:
# pick arbitrary node and walk until it closes
start = next(iter(G.nodes))
succs = list(G.successors(start))
if succs:
cp = _extend_linear_path(G, start, succs[0])
if len(cp.seq) >= min_length:
contigs.append(cp)
# deduplicate by sequence
unique: Dict[str, ContigPath] = {}
for cp in contigs:
if cp.seq not in unique or len(cp.seq) > len(unique[cp.seq].seq):
unique[cp.seq] = cp
contigs = sorted(unique.values(), key=lambda c: len(c.seq), reverse=True)
return contigs
@dataclass
class ContigScore:
seq: str
length: int
mean_weight: float
min_weight: int
max_weight: int
score: float
def score_contig(
cp: ContigPath,
alpha_len: float = 1.0,
alpha_cov: float = 1.0,
alpha_min: float = 0.2,
) -> ContigScore:
"""
Simple reference-free score combining length and coverage:
score = alpha_len * log(length) + alpha_cov * mean_weight + alpha_min * min_weight
Adjust alphas to your data. You can also plug-in intensity-based terms later.
"""
import math
if cp.weights:
mean_w = sum(cp.weights) / len(cp.weights)
min_w = min(cp.weights)
max_w = max(cp.weights)
else:
mean_w = min_w = max_w = 0.0
L = len(cp.seq)
composite = alpha_len * math.log(max(L, 2)) + alpha_cov * mean_w + alpha_min * min_w
return ContigScore(
seq=cp.seq,
length=L,
mean_weight=mean_w,
min_weight=min_w,
max_weight=max_w,
score=composite,
)
def rank_contigs_by_score(
contigs: List[ContigPath],
alpha_len: float = 1.0,
alpha_cov: float = 1.0,
alpha_min: float = 0.2,
) -> List[ContigScore]:
scored = [score_contig(c, alpha_len, alpha_cov, alpha_min) for c in contigs]
return sorted(scored, key=lambda s: (s.score, s.length), reverse=True)
def build_overlap_graph(contigs: List[str], min_overlap: int) -> nx.DiGraph:
"""
Builds a directed graph where nodes are contigs and edges represent
valid suffix-prefix overlaps.
"""
G = nx.DiGraph()
for i, seq in enumerate(contigs):
G.add_node(i, seq=seq, length=len(seq))
n_contigs = len(contigs)
for i in range(n_contigs):
for j in range(n_contigs):
if i == j:
continue
seq_a = contigs[i]
seq_b = contigs[j]
max_ov = min(len(seq_a), len(seq_b))
if max_ov < min_overlap:
continue
best_overlap = 0
for k in range(max_ov, min_overlap - 1, -1):
if seq_a.endswith(seq_b[:k]):
best_overlap = k
break
if best_overlap > 0:
G.add_edge(i, j, weight=best_overlap)
return G
def merge_paths_from_overlap_graph(G: nx.DiGraph) -> List[str]:
"""
Traverses the overlap graph to find and merge the optimal paths
(heaviest overlaps), resolving branches by prioritizing longer overlaps.
"""
merged_contigs = []
G_work = G.copy()
while G_work.number_of_nodes() > 0:
start_nodes = [n for n in G_work.nodes if G_work.in_degree(n) == 0]
if not start_nodes:
start_node = max(G_work.nodes, key=lambda n: len(G_work.nodes[n]["seq"]))
else:
start_node = max(start_nodes, key=lambda n: len(G_work.nodes[n]["seq"]))
path = [start_node]
current = start_node
while True:
if G_work.out_degree(current) == 0:
break
neighbors = list(G_work.successors(current))
best_next = max(neighbors, key=lambda n: G_work[current][n]["weight"])
if best_next in path:
break
path.append(best_next)
current = best_next
if len(path) == 1:
merged_contigs.append(G_work.nodes[start_node]["seq"])
else:
first_idx = path[0]
super_seq = G_work.nodes[first_idx]["seq"]
for i in range(len(path) - 1):
u, v = path[i], path[i + 1]
overlap_len = G_work[u][v]["weight"]
seq_v = G_work.nodes[v]["seq"]
super_seq += seq_v[overlap_len:]
merged_contigs.append(super_seq)
G_work.remove_nodes_from(path)
return merged_contigs
def refine_using_overlap_graph(contigs: List[str], min_overlap: int) -> List[str]:
"""
Wrapper function: Builds graph -> Merges paths -> Cleans up substrings.
"""
if not contigs:
return []
G = build_overlap_graph(contigs, min_overlap)
refined = merge_paths_from_overlap_graph(G)
refined = sorted(list(set(refined)), key=len, reverse=True)
final_set = []
for seq in refined:
if not any(seq in other and seq != other for other in refined):
final_set.append(seq)
return final_set
def scaffold_iterative_dbgx(
seqs: List[str],
kmer_size: int,
size_threshold: int = 10,
min_weight: int = 2,
max_rounds: int = 5,
patience: int = 2,
alpha_len: float = 1.0,
alpha_cov: float = 1.0,
alpha_min: float = 0.2,
) -> List[str]:
"""
Optional refinement:
rebuild DBG from current contigs → collapse → filter by size → repeat
Stops when no improvement for `patience` rounds or `max_rounds` reached.
"""
best: List[str] = list(seqs)
no_improve = 0
for _rnd in range(1, max_rounds + 1):
kmers = get_kmers(seqs, kmer_size)
if not kmers:
break
G = build_dbg_from_kmers(kmers)
G = filter_low_weight_edges(G, min_weight=min_weight)
contigs = assemble_contigs_dbgx(G, min_length=size_threshold)
if not contigs:
break
ranked = rank_contigs_by_score(contigs, alpha_len, alpha_cov, alpha_min)
seqs_new = [r.seq for r in ranked]
# improvement heuristic: fewer contigs or longer top contig
improved = (len(seqs_new) < len(best)) or (seqs_new and best and len(seqs_new[0]) > len(best[0]))
if improved:
best = seqs_new
no_improve = 0
else:
no_improve += 1
seqs = seqs_new
if no_improve >= patience:
break
# final unique & size filter
uniq = []
seen = set()
for s in best:
if len(s) >= size_threshold and s not in seen:
seen.add(s)
uniq.append(s)
return uniq
def extend_path_dbg(G, contig, k, min_weight=1):
"""
Extend a contig in both directions along the DBG G, following dominant edges
"""
seq = contig
extended = True
while extended:
extended = False
suffix = seq[-(k - 1) :]
if suffix not in G or G.out_degree(suffix) == 0:
break
successors = list(G.successors(suffix))
if len(successors) > 1:
best_succ, best_w = None, 0
for s in successors:
w = G[suffix][s].get("weight", 0)
if w > best_w:
best_succ, best_w = s, w
if best_succ and best_w >= min_weight:
seq += best_succ[-1]
extended = True
else:
break
else:
nxt = successors[0]
w = G[suffix][nxt].get("weight", 0)
if w >= min_weight:
seq += nxt[-1]
extended = True
extended = True
while extended:
extended = False
prefix = seq[: k - 1]
if prefix not in G or G.in_degree(prefix) == 0:
break
predecessors = list(G.predecessors(prefix))
if len(predecessors) > 1:
best_pred, best_w = None, 0
for p in predecessors:
w = G[p][prefix].get("weight", 0)
if w > best_w:
best_pred, best_w = p, w
if best_pred and best_w >= min_weight:
seq = best_pred[0] + seq
extended = True
else:
break
else:
p = predecessors[0]
w = G[p][prefix].get("weight", 0)
if w >= min_weight:
seq = p[0] + seq
extended = True
return seq
def get_hybrid_kmer_weights(
df: pd.DataFrame,
kmer_size: int,
) -> Counter:
"""
Calculates k-mer weights using ONLY:
1. Frequency (Implicit via accumulation)
2. MS1 Abundance (Peptide Area)
3. AI Confidence (Token Probabilities)
"""
kmer_weights = Counter()
col_tokens = "instanovo_token_log_probabilities"
col_abundance = "peptide_abundance"
for _, row in df.iterrows():
sequence = row.get("cleaned_preds")
if not isinstance(sequence, str) or len(sequence) < kmer_size:
continue
# --- 1. AI Confidence (Token Probs) ---
raw_probs_str = row.get(col_tokens)
log_probs = [0.0] * len(sequence) # Default neutral
if isinstance(raw_probs_str, str) and raw_probs_str.startswith("["):
try:
parsed = ast.literal_eval(raw_probs_str)
# Adjust for SOS/EOS tokens if present
if len(parsed) == len(sequence) + 2:
log_probs = parsed[1:-1]
elif len(parsed) == len(sequence):
log_probs = parsed
except Exception:
pass # Keep default
# --- 2. MS1 Abundance Score ---
# Log scale: log10(Area + 10).
abundance_val = row.get(col_abundance, 0)
if pd.notnull(abundance_val) and abundance_val > 0:
abundance_score = math.log10(float(abundance_val) + 10)
else:
abundance_score = 1.0
# --- 3. Accumulate Weights ---
for i in range(len(sequence) - kmer_size + 1):
kmer = sequence[i : i + kmer_size]
# Calculate local AI confidence for this specific k-mer
sub_logs = log_probs[i : i + kmer_size] if i + kmer_size <= len(log_probs) else []
if sub_logs:
# Geometric mean of probabilities in the k-mer
avg_log_prob = sum(sub_logs) / len(sub_logs)
ai_score = math.exp(avg_log_prob)
else:
ai_score = 1.0
# Final Weight: Abundance * AI Confidence
# Frequency is handled implicitly because we += this value every time we see the k-mer
weight = abundance_score * ai_score
kmer_weights[kmer] += weight
return kmer_weights
class Assembler:
"""
Unified assembler supporting:
- 'greedy': Overlap-Layout-Consensus style.
- 'dbg': Standard De Bruijn Graph.
- 'dbg_weighted': DBG with node/edge filtering and scoring.
- 'dbgX': DBG with extension heuristics.
- 'fusion': Hybrid DBG + Greedy.
- 'multimodal': Heuristic DBG using MS1/MS2/AI/iRT features.
"""
def __init__(
self,
mode: str = "greedy",
min_overlap: int = 4,
size_threshold: int = 10,
kmer_size: int = 6,
min_identity: float = 0.8,
max_mismatches: int = 10,
min_weight: int = 2,
refine_rounds: int = 0,
refine_patience: int = 2,
alpha_len: float = 1.0,
alpha_cov: float = 1.0,
alpha_min: float = 0.2,
):
if mode not in ["greedy", "dbg", "dbg_weighted", "dbgX", "fusion", "multimodal_dbg", "hybrid_dbg"]:
raise ValueError(
"mode must be 'greedy', 'dbg', 'dbg_weighted', 'dbgX', 'fusion', 'multimodal_dbg' or 'hybrid_dbg'"
)
self.mode = mode
self.min_overlap = min_overlap
self.size_threshold = size_threshold
self.kmer_size = kmer_size
self.min_identity = min_identity
self.max_mismatches = max_mismatches
self.min_weight = min_weight
self.refine_rounds = refine_rounds
self.refine_patience = refine_patience
self.alpha_len = alpha_len
self.alpha_cov = alpha_cov
self.alpha_min = alpha_min
def assemble_greedy(self, sequences):
logger.info(f"[Assembler] Running Greedy assembly (min_overlap={self.min_overlap})")
contigs = assemble_contigs_greedy(sequences, self.min_overlap)
contigs = list(set(contigs))
contigs = sorted(contigs, key=len, reverse=True)
scaffolds = scaffold_iterative_greedy(contigs, self.min_overlap, self.size_threshold)
return scaffolds
def assemble_dbg(self, sequences):
logger.info(f"[Assembler] Running DBG assembly (kmer_size={self.kmer_size})")
kmers = get_kmers(sequences, self.kmer_size)
edges = get_debruijn_edges_from_kmers(kmers)
contigs = assemble_contigs_dbg(edges)
contigs = list(set(contigs))
contigs = sorted(contigs, key=len, reverse=True)
contigs = [seq for seq in contigs if len(seq) > self.size_threshold]
scaffolds = scaffold_iterative_dbg(contigs, self.min_overlap, self.size_threshold)
return scaffolds
def assemble_dbg_weighted(self, sequences: List[str]) -> List[str]:
logger.info(f"[Assembler] Running DBG weighted (k={self.kmer_size}, min_weight={self.min_weight})")
# 1. Standard Weighted DBG Assembly
kmers = get_kmers(sequences, self.kmer_size)
if not kmers:
logger.warning("No kmers generated; returning empty result.")
return []
G = build_dbg_from_kmers(kmers)
G = filter_low_weight_edges(G, min_weight=self.min_weight)
contigs_cp = assemble_contigs_dbgx(G, min_length=self.size_threshold)
if not contigs_cp:
logger.warning("No contigs assembled from DBGX; returning empty result.")
return []
ranked = rank_contigs_by_score(contigs_cp, self.alpha_len, self.alpha_cov, self.alpha_min)
contigs = [r.seq for r in ranked]
logger.info(f"DBG produced {len(contigs)} initial contigs.")
# 2. OVERLAP GRAPH REFINEMENT (The new logic)
# Only runs if refine_rounds is > 0
if self.refine_rounds > 0:
logger.info("Refining contigs using Overlap Graph (Bird's Eye View)...")
# Use a slightly safer/larger overlap for this final merge to avoid false positives
# e.g., max(min_overlap, 5) or just self.min_overlap
safe_overlap = max(self.min_overlap, 5)
iteration = 0
while iteration < self.refine_rounds:
iteration += 1
prev_count = len(contigs)
# Core logic
contigs = refine_using_overlap_graph(contigs, min_overlap=safe_overlap)
new_count = len(contigs)
# CONVERGENZA: Se non abbiamo fuso nulla, ci fermiamo.
if new_count >= prev_count:
logger.info(f"Refinement converged at round {iteration}.")
break
logger.info(f"Round {iteration}: reduced to {new_count} scaffolds.")
if iteration >= self.refine_rounds:
logger.warning(f"Refinement stopped hit max rounds limit ({self.refine_rounds}).")
return contigs
def assemble_dbgX(self, sequences):
logger.info(f"[Assembler] Running DBG-Extension (k={self.kmer_size})")
kmers = get_kmers(sequences, self.kmer_size)
G = build_dbg_from_kmers(kmers)
G = filter_low_weight_edges(G, min_weight=self.min_weight)
contigs_cp = assemble_contigs_dbgx(G, min_length=self.size_threshold)
contigs = [c.seq for c in contigs_cp]
logger.info("Extending contigs using DBG paths (coverage-aware)...")
extended_contigs = [extend_path_dbg(G, c, self.kmer_size, self.min_weight) for c in contigs]
extended_contigs = sorted(set(extended_contigs), key=len, reverse=True)
return extended_contigs
def assemble_fusion(self, sequences):
logger.info("[Assembler] Running FUSION (DBG weighted + greedy merge)")
contigs_dbg_weighted = self.assemble_dbg_weighted(sequences)
logger.info("Running greedy merge on DBG weighted contigs...")
contigs_greedy = assemble_contigs_greedy(sequences, self.min_overlap)
contigs_greedy = merge_contigs_greedy(contigs_greedy)
combined = list(set(contigs_dbg_weighted + contigs_greedy))
combined = [s for s in combined if len(s) > self.size_threshold]
logger.info(f"Combined {len(combined)} contigs from DBG weighted + Greedy")
fused = assemble_contigs_greedy(combined, self.min_overlap)
fused = merge_contigs_greedy(fused)
fused = [s for s in fused if len(s) > self.size_threshold]
fused = sorted(set(fused), key=len, reverse=True)
return fused
def assemble_multimodal_dbg(self, sequences: List[str], df_full: Optional[pd.DataFrame] = None) -> List[str]:
"""
Multimodal Heuristic Assembly Strategy.
Logic:
1. Landscape Construction: Weights nodes by MS1 Abundance, MS2 Intensity, iRT, and AI Confidence.
2. Seed Selection: Picks the 'Heaviest Seed' (highest confidence node).
3. Smart Navigation: Extends forward/backward using Lookahead Score (Edge * Node).
4. Path Burning: Removes assembled nodes to uncover lower-abundance variants.
"""
logger.info(f"[Assembler] Running Multimodal DBG (Heuristic) k={self.kmer_size}")
# --- 1. WEIGHT CALCULATION (NODES & EDGES) ---
if df_full is not None:
logger.info("Using Multimodal Features (Token Probs, MS1/MS2, iRT) for weighting.")
# Nodes are (k-1)-mers
node_weights = get_weighted_kmers_from_df(df_full, self.kmer_size - 1, use_abundance=True, use_quality=True)
# Edges are k-mers
edge_weights = get_weighted_kmers_from_df(df_full, self.kmer_size, use_abundance=True, use_quality=True)
# Build graph using calculated edge weights
G = build_dbg_from_kmers([], weights=edge_weights)
else:
# Fallback: Simple counts (if no dataframe provided)
logger.warning("No DataFrame provided for Multimodal DBG. Falling back to raw counts.")
node_kmers = get_kmers(sequences, self.kmer_size - 1)
node_weights = Counter(node_kmers)