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
pub mod default_node_centric_trivial_omnitigs;
pub mod default_trivial_omnitigs;
pub mod incremental_hydrostructure_macrotig_based_non_trivial_omnitigs;
pub mod univocal_extension_algorithms;
use crate::macrotigs::macrotigs::Macrotigs;
use crate::omnitigs::default_node_centric_trivial_omnitigs::DefaultTrivialNodeCentricOmnitigAlgorithm;
use crate::omnitigs::default_trivial_omnitigs::{
NonSccTrivialOmnitigAlgorithm, SccTrivialOmnitigAlgorithm,
};
use crate::omnitigs::incremental_hydrostructure_macrotig_based_non_trivial_omnitigs::IncrementalHydrostructureMacrotigBasedNonTrivialOmnitigAlgorithm;
use crate::omnitigs::univocal_extension_algorithms::{
NonSccNodeCentricUnivocalExtensionStrategy, SccNodeCentricUnivocalExtensionStrategy,
};
use crate::walks::EdgeOmnitigLikeExt;
use bigraph::interface::static_bigraph::{StaticBigraph, StaticEdgeCentricBigraph};
use bigraph::interface::BidirectedData;
use std::borrow::{Borrow, BorrowMut};
use std::cmp::Ordering;
use std::iter::FromIterator;
use std::mem;
use traitgraph::index::GraphIndex;
use traitgraph::interface::{GraphBase, StaticGraph};
use traitgraph::walks::{EdgeWalk, VecEdgeWalk, VecNodeWalk};
use traitsequence::interface::Sequence;
#[derive(Clone)]
pub struct Omnitig<Graph: GraphBase> {
omnitig: VecEdgeWalk<Graph>,
first_heart_edge: usize,
last_heart_edge: usize,
}
impl<Graph: StaticGraph> Omnitig<Graph> {
pub fn compute_from_heart(graph: &Graph, heart: &[Graph::EdgeIndex]) -> Self {
let (first_heart_edge, univocal_extension) =
heart.compute_univocal_extension_with_original_offset(graph);
let last_heart_edge = first_heart_edge + heart.len() - 1;
Self::new(univocal_extension, first_heart_edge, last_heart_edge)
}
pub fn compute_from_non_trivial_heart_superwalk(
graph: &Graph,
heart_superwalk: &[Graph::EdgeIndex],
) -> Self {
let mut omnitig = Self::compute_from_heart(graph, heart_superwalk);
while !graph.is_join_edge(omnitig[omnitig.first_heart_edge]) {
omnitig.first_heart_edge += 1;
debug_assert!(
omnitig.first_heart_edge < omnitig.last_heart_edge,
"First join is not before last split"
);
}
while !graph.is_split_edge(omnitig[omnitig.last_heart_edge]) {
omnitig.last_heart_edge -= 1;
debug_assert!(
omnitig.first_heart_edge < omnitig.last_heart_edge,
"First join is not before last split"
);
}
omnitig
}
}
impl<Graph: GraphBase> Omnitig<Graph> {
pub fn new(edges: VecEdgeWalk<Graph>, first_heart_edge: usize, last_heart_edge: usize) -> Self {
Self {
omnitig: edges,
first_heart_edge,
last_heart_edge,
}
}
pub fn iter_heart(&self) -> impl Iterator<Item = &Graph::EdgeIndex> {
self.omnitig
.iter()
.take(self.last_heart_edge + 1)
.skip(self.first_heart_edge)
}
pub fn heart(&self) -> &[Graph::EdgeIndex] {
&self.omnitig[self.first_heart_edge..=self.last_heart_edge]
}
pub fn len_heart(&self) -> usize {
self.heart().len()
}
}
impl<'a, Graph: GraphBase> EdgeWalk<'a, Graph, [Graph::EdgeIndex]> for Omnitig<Graph> where
Graph::EdgeIndex: 'a
{
}
impl<'a, Graph: GraphBase> Sequence<'a, Graph::EdgeIndex, [Graph::EdgeIndex]> for Omnitig<Graph>
where
Graph::EdgeIndex: 'a,
{
type Iterator =
<VecEdgeWalk<Graph> as Sequence<'a, Graph::EdgeIndex, [Graph::EdgeIndex]>>::Iterator;
fn iter(&'a self) -> Self::Iterator {
self.omnitig.iter()
}
fn len(&self) -> usize {
self.omnitig.len()
}
}
impl<Graph: GraphBase> Extend<Graph::EdgeIndex> for Omnitig<Graph> {
fn extend<T: IntoIterator<Item = Graph::EdgeIndex>>(&mut self, iter: T) {
self.omnitig.extend(iter)
}
}
impl<Graph: GraphBase> IntoIterator for Omnitig<Graph> {
type Item = <VecEdgeWalk<Graph> as IntoIterator>::Item;
type IntoIter = <VecEdgeWalk<Graph> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.omnitig.into_iter()
}
}
impl<Graph: GraphBase> PartialEq for Omnitig<Graph>
where
Graph::EdgeIndex: PartialEq,
{
fn eq(&self, rhs: &Self) -> bool {
self.omnitig == rhs.omnitig
&& self.first_heart_edge == rhs.first_heart_edge
&& self.last_heart_edge == rhs.last_heart_edge
}
}
impl<Graph: GraphBase> Eq for Omnitig<Graph> where Graph::EdgeIndex: Eq {}
impl<Graph: GraphBase> From<Omnitig<Graph>> for VecEdgeWalk<Graph> {
fn from(omnitig: Omnitig<Graph>) -> Self {
omnitig.omnitig
}
}
impl<Graph: GraphBase> std::fmt::Debug for Omnitig<Graph>
where
Graph::EdgeIndex: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Omnitig[")?;
if let Some((i, first)) = self.iter().enumerate().next() {
if i == self.first_heart_edge {
write!(f, "|")?;
}
write!(f, "{:?}", first)?;
if i == self.last_heart_edge {
write!(f, "|")?;
}
}
for (i, edge) in self.iter().enumerate().skip(1) {
write!(f, ", ")?;
if i == self.first_heart_edge {
write!(f, "|")?;
}
write!(f, "{:?}", edge)?;
if i == self.last_heart_edge {
write!(f, "|")?;
}
}
write!(f, "]")
}
}
impl<Graph: GraphBase, IndexType> std::ops::Index<IndexType> for Omnitig<Graph>
where
Vec<Graph::EdgeIndex>: std::ops::Index<IndexType>,
{
type Output = <Vec<Graph::EdgeIndex> as std::ops::Index<IndexType>>::Output;
fn index(&self, index: IndexType) -> &Self::Output {
self.omnitig.index(index)
}
}
#[derive(Clone)]
pub struct Omnitigs<Graph: GraphBase> {
omnitigs: Vec<Omnitig<Graph>>,
omnitigs_per_macrotig: Vec<usize>,
}
impl<Graph: StaticGraph> Omnitigs<Graph> {
pub fn compute(graph: &Graph) -> Self {
let maximal_macrotigs = Macrotigs::compute(graph);
debug!("Found {} macrotigs", maximal_macrotigs.len());
let maximal_non_trivial_omnitigs = IncrementalHydrostructureMacrotigBasedNonTrivialOmnitigAlgorithm::compute_maximal_non_trivial_omnitigs(graph, &maximal_macrotigs);
debug!(
"Found {} non-trivial omnitigs",
maximal_non_trivial_omnitigs.len()
);
let result = SccTrivialOmnitigAlgorithm::compute_maximal_trivial_omnitigs(
graph,
maximal_non_trivial_omnitigs,
);
debug!("Found {} omnitigs", result.len());
result
}
pub fn compute_non_trivial_only(graph: &Graph) -> Self {
let maximal_macrotigs = Macrotigs::compute(graph);
IncrementalHydrostructureMacrotigBasedNonTrivialOmnitigAlgorithm::compute_maximal_non_trivial_omnitigs(graph, &maximal_macrotigs)
}
pub fn compute_trivial_only(graph: &Graph) -> Self {
SccTrivialOmnitigAlgorithm::compute_maximal_trivial_omnitigs(graph, Omnitigs::default())
}
pub fn compute_trivial_only_non_scc(graph: &Graph) -> Self {
NonSccTrivialOmnitigAlgorithm::compute_maximal_trivial_omnitigs(graph, Omnitigs::default())
}
}
impl<Graph: StaticEdgeCentricBigraph> Omnitigs<Graph>
where
Graph::EdgeData: BidirectedData + Eq,
Graph::NodeData: std::fmt::Debug,
{
pub fn remove_reverse_complements(&mut self, graph: &Graph) {
info!("Removing reverse complements");
let initial_len = self.len();
let mut first_heart_edge_map = vec![usize::max_value(); graph.edge_count()];
for (i, omnitig) in self.iter().enumerate() {
let first_heart_edge = omnitig.iter_heart().next().expect("Omnitig has no heart");
debug_assert_eq!(
first_heart_edge_map[first_heart_edge.as_usize()],
usize::max_value(),
"Found two omnitigs hearts starting with the same edge."
);
first_heart_edge_map[first_heart_edge.as_usize()] = i;
}
let mut retain_indices = Vec::with_capacity(self.len());
for (i, omnitig) in self.iter().enumerate() {
let reverse_complement_first_heart_edge = graph
.mirror_edge_edge_centric(
*omnitig.iter_heart().last().expect("Omnitig has no heart."),
)
.expect("Edge has no reverse complement.");
let reverse_complement_candidate_index =
first_heart_edge_map[reverse_complement_first_heart_edge.as_usize()];
if reverse_complement_candidate_index < i {
let reverse_complement_candidate = &self[reverse_complement_candidate_index];
for (edge, reverse_complement_edge) in omnitig
.iter()
.zip(reverse_complement_candidate.iter().rev())
{
let complements_complement_edge = graph
.mirror_edge_edge_centric(*reverse_complement_edge)
.expect("Edge has no reverse complement.");
debug_assert_eq!(
*edge,
complements_complement_edge,
"Found reverse complement candidate, but it is not a reverse complement:\nomnitig: {:?}\nnode omnitig: {:?}\nomnitig indegree: {}\nomnitig outdegree: {}\nrevcomp: {:?}\nnode revcomp: {:?}\nrevcomp indegree: {}\nrevcomp outdegree: {}",
omnitig,
omnitig.clone_as_node_walk::<VecNodeWalk<Graph>>(graph).unwrap().iter().map(|&n| graph.node_data(n)).collect::<Vec<_>>(),
graph.in_degree(*omnitig.clone_as_node_walk::<VecNodeWalk<Graph>>(graph).unwrap().first().unwrap()),
graph.out_degree(*omnitig.clone_as_node_walk::<VecNodeWalk<Graph>>(graph).unwrap().last().unwrap()),
reverse_complement_candidate,
reverse_complement_candidate.clone_as_node_walk::<VecNodeWalk<Graph>>(graph).unwrap().iter().map(|&n| graph.node_data(n)).collect::<Vec<_>>(),
graph.in_degree(*reverse_complement_candidate.clone_as_node_walk::<VecNodeWalk<Graph>>(graph).unwrap().first().unwrap()),
graph.out_degree(*reverse_complement_candidate.clone_as_node_walk::<VecNodeWalk<Graph>>(graph).unwrap().last().unwrap()),
);
}
} else {
retain_indices.push(i);
}
}
let mut omnitigs = Vec::new();
std::mem::swap(&mut omnitigs, &mut self.omnitigs);
for (i, omnitig) in omnitigs.into_iter().enumerate() {
if self.omnitigs.len() == retain_indices.len() {
break;
}
if i == retain_indices[self.omnitigs.len()] {
self.omnitigs.push(omnitig);
}
}
let removed_count = initial_len - self.len();
debug!(
"Removed {} reverse complements, decreasing the number of omnitigs from {} to {}",
removed_count,
initial_len,
self.len()
);
}
}
pub fn remove_subwalks_and_reverse_complements_from_walks<Graph: StaticEdgeCentricBigraph>(
walks: &mut Vec<VecEdgeWalk<Graph>>,
graph: &Graph,
) where
Graph::EdgeData: BidirectedData + Eq,
Graph::NodeData: std::fmt::Debug,
{
info!("Removing subwalks and reverse complements with a slow algorithm");
let initial_len = walks.len();
debug!("Finding subwalks to remove");
let mut remove = vec![false; walks.len()];
for (index_a, walk_a) in walks.iter().enumerate() {
if remove[index_a] {
continue;
}
for (index_b, walk_b) in walks.iter().enumerate().skip(index_a + 1) {
if remove[index_a] {
break;
}
if remove[index_b] {
continue;
}
match is_subwalk_or_reverse_complement(walk_a, walk_b, graph) {
SubwalkOrReverseComplement::No => {}
SubwalkOrReverseComplement::ReverseComplement
| SubwalkOrReverseComplement::Equal
| SubwalkOrReverseComplement::ASubwalkOfB
| SubwalkOrReverseComplement::AReverseComplementSubwalkOfB => {
remove[index_a] = true;
}
SubwalkOrReverseComplement::BSubwalkOfA
| SubwalkOrReverseComplement::BReverseComplementSubwalkOfA => {
remove[index_b] = true;
}
}
}
}
debug!("Removing subwalks");
let mut original_walks = Vec::new();
mem::swap(&mut original_walks, walks);
walks.extend(
original_walks
.into_iter()
.zip(remove.iter())
.filter_map(|(walk, &remove)| if remove { None } else { Some(walk) }),
);
let removed_count = initial_len - walks.len();
debug!(
"Removed {} reverse complements, decreasing the number of omnitigs from {} to {}",
removed_count,
initial_len,
walks.len()
);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SubwalkOrReverseComplement {
No,
ReverseComplement,
Equal,
ASubwalkOfB,
BSubwalkOfA,
AReverseComplementSubwalkOfB,
BReverseComplementSubwalkOfA,
}
fn is_subwalk_or_reverse_complement<Graph: StaticEdgeCentricBigraph>(
walk_a: &VecEdgeWalk<Graph>,
walk_b: &VecEdgeWalk<Graph>,
graph: &Graph,
) -> SubwalkOrReverseComplement
where
Graph::EdgeData: BidirectedData + Eq,
{
match walk_a.len().cmp(&walk_b.len()) {
Ordering::Less => {
let difference = walk_b.len() - walk_a.len();
for skip in 0..difference {
if walk_a
.iter()
.zip(walk_b.iter().skip(skip))
.all(|(&edge_a, &edge_b)| edge_a == edge_b)
{
return SubwalkOrReverseComplement::ASubwalkOfB;
}
if walk_a
.iter()
.rev()
.zip(walk_b.iter().skip(skip))
.all(|(&edge_a, &edge_b)| {
graph.mirror_edge_edge_centric(edge_a) == Some(edge_b)
})
{
return SubwalkOrReverseComplement::AReverseComplementSubwalkOfB;
}
}
}
Ordering::Equal => {
if walk_a == walk_b {
return SubwalkOrReverseComplement::Equal;
}
if walk_a
.iter()
.rev()
.zip(walk_b.iter())
.all(|(&edge_a, &edge_b)| graph.mirror_edge_edge_centric(edge_a) == Some(edge_b))
{
return SubwalkOrReverseComplement::ReverseComplement;
}
}
Ordering::Greater => {
return match is_subwalk_or_reverse_complement(walk_b, walk_a, graph) {
SubwalkOrReverseComplement::No => SubwalkOrReverseComplement::No,
SubwalkOrReverseComplement::ASubwalkOfB => SubwalkOrReverseComplement::BSubwalkOfA,
SubwalkOrReverseComplement::AReverseComplementSubwalkOfB => {
SubwalkOrReverseComplement::BReverseComplementSubwalkOfA
}
other => unreachable!("{:?}", other),
};
}
}
SubwalkOrReverseComplement::No
}
impl<Graph: GraphBase> Omnitigs<Graph> {
pub fn new(omnitigs: Vec<Omnitig<Graph>>, omnitigs_per_macrotig: Vec<usize>) -> Self {
Self {
omnitigs,
omnitigs_per_macrotig,
}
}
pub fn iter(&self) -> impl Iterator<Item = &Omnitig<Graph>> {
self.omnitigs.iter()
}
pub fn len(&self) -> usize {
self.omnitigs.len()
}
pub fn is_empty(&self) -> bool {
self.omnitigs.is_empty()
}
pub fn push(&mut self, omnitig: Omnitig<Graph>) {
self.omnitigs.push(omnitig);
}
pub fn omnitigs_per_macrotig(&self) -> &[usize] {
&self.omnitigs_per_macrotig
}
}
impl<Graph: GraphBase> Default for Omnitigs<Graph> {
fn default() -> Self {
Self {
omnitigs: Default::default(),
omnitigs_per_macrotig: Default::default(),
}
}
}
impl<Graph: GraphBase> From<Vec<Omnitig<Graph>>> for Omnitigs<Graph> {
fn from(omnitigs: Vec<Omnitig<Graph>>) -> Self {
Self {
omnitigs,
omnitigs_per_macrotig: Default::default(),
}
}
}
impl<Graph: GraphBase, IndexType> std::ops::Index<IndexType> for Omnitigs<Graph>
where
Vec<Omnitig<Graph>>: std::ops::Index<IndexType>,
{
type Output = <Vec<Omnitig<Graph>> as std::ops::Index<IndexType>>::Output;
fn index(&self, index: IndexType) -> &Self::Output {
self.omnitigs.index(index)
}
}
impl<Graph: GraphBase> std::fmt::Debug for Omnitigs<Graph>
where
Graph::NodeIndex: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Omnitigs[")?;
if let Some(first) = self.iter().next() {
write!(f, "{:?}", first)?;
}
for edge in self.iter().skip(1) {
write!(f, ", {:?}", edge)?;
}
write!(f, "]")
}
}
impl<Graph: GraphBase> PartialEq for Omnitigs<Graph>
where
Graph::EdgeIndex: PartialEq,
{
fn eq(&self, rhs: &Self) -> bool {
self.omnitigs == rhs.omnitigs
}
}
impl<Graph: GraphBase> Eq for Omnitigs<Graph> where Graph::EdgeIndex: Eq {}
impl<Graph: GraphBase> IntoIterator for Omnitigs<Graph> {
type Item = Omnitig<Graph>;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.omnitigs.into_iter()
}
}
impl<Graph: GraphBase> Borrow<[Omnitig<Graph>]> for Omnitigs<Graph> {
fn borrow(&self) -> &[Omnitig<Graph>] {
&self.omnitigs
}
}
impl<Graph: GraphBase> BorrowMut<[Omnitig<Graph>]> for Omnitigs<Graph> {
fn borrow_mut(&mut self) -> &mut [Omnitig<Graph>] {
&mut self.omnitigs
}
}
pub trait MacrotigBasedNonTrivialOmnitigAlgorithm<Graph: StaticGraph> {
fn compute_maximal_non_trivial_omnitigs(
graph: &Graph,
macrotigs: &Macrotigs<Graph>,
) -> Omnitigs<Graph>;
}
pub trait TrivialOmnitigAlgorithm<Graph: StaticGraph> {
type UnivocalExtensionStrategy: UnivocalExtensionAlgorithm<Graph, VecEdgeWalk<Graph>>;
fn compute_maximal_trivial_omnitigs(
graph: &Graph,
omnitigs: Omnitigs<Graph>,
) -> Omnitigs<Graph>;
}
pub trait UnivocalExtensionAlgorithm<Graph: StaticGraph, ResultWalk: From<Vec<Graph::EdgeIndex>>> {
fn compute_univocal_extension(graph: &Graph, walk: &[Graph::EdgeIndex]) -> ResultWalk;
}
pub trait NodeCentricOmnitigs<
Graph: GraphBase,
NodeCentricOmnitigsSubsequence: for<'a> Sequence<'a, VecNodeWalk<Graph>, NodeCentricOmnitigsSubsequence> + ?Sized,
>:
From<Vec<VecNodeWalk<Graph>>>
+ for<'a> Sequence<'a, VecNodeWalk<Graph>, NodeCentricOmnitigsSubsequence>
{
fn compute_trivial_node_centric_omnitigs(graph: &Graph) -> Self
where
Graph: StaticGraph,
{
DefaultTrivialNodeCentricOmnitigAlgorithm::<SccNodeCentricUnivocalExtensionStrategy>::compute_maximal_trivial_node_centric_omnitigs(graph, Vec::new()).into()
}
fn compute_trivial_node_centric_omnitigs_non_scc(graph: &Graph) -> Self
where
Graph: StaticGraph,
{
DefaultTrivialNodeCentricOmnitigAlgorithm::<NonSccNodeCentricUnivocalExtensionStrategy>::compute_maximal_trivial_node_centric_omnitigs(graph, Vec::new()).into()
}
fn remove_reverse_complements(&mut self, graph: &Graph)
where
Graph: StaticBigraph,
Self: FromIterator<VecNodeWalk<Graph>>,
{
let mut first_node_map = vec![Vec::new(); graph.node_count()];
for (i, omnitig) in self.iter().enumerate() {
let first_node = omnitig.iter().next().expect("Omnitig is empty");
first_node_map[first_node.as_usize()].push(i);
}
let mut retain_indices = Vec::with_capacity(self.len() / 2);
for (i, omnitig) in self.iter().enumerate() {
let reverse_complement_first_node = graph
.mirror_node(*omnitig.last().expect("Omnitig is empty"))
.expect("Node has no mirror");
let mut reverse_complement_count = 0;
let mut self_complemental = false;
for &reverse_complement_candidate_index in
&first_node_map[reverse_complement_first_node.as_usize()]
{
let reverse_complement_candidate = &self[reverse_complement_candidate_index];
let is_reverse_complemental = omnitig
.iter()
.zip(reverse_complement_candidate.iter().rev())
.all(|(&n1, &n2)| n1 == graph.mirror_node(n2).expect("Node has no mirror"));
if is_reverse_complemental {
debug_assert_eq!(omnitig.len(), reverse_complement_candidate.len(), "Walks are reverse complemental, but do not have the same length. This means one of them is not maximal.");
debug_assert_eq!(
reverse_complement_count, 0,
"Walk has more than one reverse complement."
);
reverse_complement_count += 1;
match reverse_complement_candidate_index.cmp(&i) {
Ordering::Less => retain_indices.push(reverse_complement_candidate_index),
Ordering::Equal => self_complemental = true,
Ordering::Greater => (),
}
}
if self_complemental {
retain_indices.push(reverse_complement_candidate_index);
}
}
}
retain_indices.sort_unstable();
debug_assert!(
retain_indices.windows(2).all(|w| w[0] < w[1]),
"retain_indices contains duplicate walk"
);
let mut retained_omnitigs = Vec::new();
for (i, omnitig) in self.iter().enumerate() {
if retained_omnitigs.len() == retain_indices.len() {
break;
}
if i == retain_indices[retained_omnitigs.len()] {
retained_omnitigs.push(omnitig);
}
}
*self = retained_omnitigs.into_iter().cloned().collect();
}
}
impl<Graph: 'static + GraphBase> NodeCentricOmnitigs<Graph, [VecNodeWalk<Graph>]>
for Vec<VecNodeWalk<Graph>>
{
}
pub trait TrivialNodeCentricOmnitigAlgorithm<Graph: StaticGraph> {
type NodeCentricUnivocalExtensionStrategy: NodeCentricUnivocalExtensionAlgorithm<
Graph,
VecNodeWalk<Graph>,
>;
fn compute_maximal_trivial_node_centric_omnitigs(
graph: &Graph,
omnitigs: Vec<VecNodeWalk<Graph>>,
) -> Vec<VecNodeWalk<Graph>>;
}
pub trait NodeCentricUnivocalExtensionAlgorithm<
Graph: StaticGraph,
ResultWalk: From<Vec<Graph::NodeIndex>>,
>
{
fn compute_univocal_extension(graph: &Graph, walk: &[Graph::NodeIndex]) -> ResultWalk;
}
#[cfg(test)]
mod tests {
use crate::omnitigs::{Omnitig, Omnitigs};
use traitgraph::implementation::petgraph_impl::PetGraph;
use traitgraph::interface::MutableGraphContainer;
use traitgraph::interface::WalkableGraph;
#[test]
fn test_compute_only_trivial_omnitigs_simple() {
let mut graph = PetGraph::new();
let n0 = graph.add_node(());
let n1 = graph.add_node(());
let n2 = graph.add_node(());
let n3 = graph.add_node(());
let n4 = graph.add_node(());
let n5 = graph.add_node(());
let n6 = graph.add_node(());
let n7 = graph.add_node(());
let n8 = graph.add_node(());
let n9 = graph.add_node(());
let n10 = graph.add_node(());
let n11 = graph.add_node(());
let n12 = graph.add_node(());
let n13 = graph.add_node(());
let n14 = graph.add_node(());
let n15 = graph.add_node(());
let n16 = graph.add_node(());
let n17 = graph.add_node(());
let n18 = graph.add_node(());
let n19 = graph.add_node(());
let n20 = graph.add_node(());
let e0 = graph.add_edge(n0, n1, ());
let e1 = graph.add_edge(n1, n2, ());
let e2 = graph.add_edge(n2, n3, ());
let e3 = graph.add_edge(n2, n4, ());
let e4 = graph.add_edge(n2, n5, ());
let e5 = graph.add_edge(n2, n6, ());
let e6 = graph.add_edge(n7, n0, ());
let e7 = graph.add_edge(n8, n0, ());
let e8 = graph.add_edge(n9, n0, ());
let e9 = graph.add_edge(n10, n0, ());
let e10 = graph.add_edge(n3, n11, ());
let e11 = graph.add_edge(n3, n12, ());
let e12 = graph.add_edge(n4, n13, ());
let e13 = graph.add_edge(n4, n14, ());
let e14 = graph.add_edge(n17, n8, ());
let e15 = graph.add_edge(n17, n9, ());
let e16 = graph.add_edge(n17, n10, ());
let e17 = graph.add_edge(n12, n18, ());
let e18 = graph.add_edge(n13, n18, ());
let e19 = graph.add_edge(n14, n18, ());
let e20 = graph.add_edge(n5, n18, ());
let e21 = graph.add_edge(n6, n18, ());
let e22 = graph.add_edge(n11, n15, ());
let e23 = graph.add_edge(n15, n16, ());
let e24 = graph.add_edge(n16, n17, ());
let e25 = graph.add_edge(n17, n17, ());
let e26 = graph.add_edge(n20, n7, ());
let e27 = graph.add_edge(n19, n20, ());
let e28 = graph.add_edge(n18, n19, ());
let e29 = graph.add_edge(n18, n18, ());
let maximal_trivial_omnitigs = Omnitigs::compute_trivial_only(&graph);
debug_assert_eq!(
maximal_trivial_omnitigs,
Omnitigs::from(vec![
Omnitig::new(graph.create_edge_walk(&[e0, e1, e4, e20]), 2, 3),
Omnitig::new(graph.create_edge_walk(&[e0, e1, e5, e21]), 2, 3),
Omnitig::new(graph.create_edge_walk(&[e28, e27, e26, e6, e0, e1]), 0, 3),
Omnitig::new(graph.create_edge_walk(&[e14, e7, e0, e1]), 0, 1),
Omnitig::new(graph.create_edge_walk(&[e15, e8, e0, e1]), 0, 1),
Omnitig::new(graph.create_edge_walk(&[e16, e9, e0, e1]), 0, 1),
Omnitig::new(
graph.create_edge_walk(&[e0, e1, e2, e10, e22, e23, e24]),
3,
6
),
Omnitig::new(graph.create_edge_walk(&[e0, e1, e2, e11, e17]), 3, 4),
Omnitig::new(graph.create_edge_walk(&[e0, e1, e3, e12, e18]), 3, 4),
Omnitig::new(graph.create_edge_walk(&[e0, e1, e3, e13, e19]), 3, 4),
Omnitig::new(graph.create_edge_walk(&[e25]), 0, 0),
Omnitig::new(graph.create_edge_walk(&[e29]), 0, 0),
])
);
}
}