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
// Copyright 2017 10x Genomics

//! DeBruijn graph simplification routines. Currently tip-removal is implemented.
use crate::Kmer;
use crate::graph::{DebruijnGraph, Node};
use std::marker::PhantomData;
use std::fmt::Debug;


pub struct CleanGraph<K: Kmer, D, T1>
where
    T1: Fn(&Node<'_, K, D>) -> bool,
{
    tip_predicate: T1,
    _k: PhantomData<K>,
    _d: PhantomData<D>,
}


impl<K: Kmer, D: Debug, T1> CleanGraph<K, D, T1>
where
    T1: Fn(&Node<'_, K, D>) -> bool,
{
    pub fn new(tip_predicate: T1) -> CleanGraph<K, D, T1> {
        CleanGraph {
            tip_predicate,
            _k: PhantomData,
            _d: PhantomData,
        }
    }

    fn test_tip(&self, graph: &DebruijnGraph<K, D>, id: usize) -> Option<usize> {

        let node = graph.get_node(id);
        let exts = node.exts();
        if exts.num_exts_r() > 0 && exts.num_exts_l() > 0 {
            return None;
        }

        if (exts.num_exts_l() == 0 && exts.num_exts_r() <= 1) ||
            (exts.num_exts_r() == 0 && exts.num_exts_l() <= 1)
        {
            if (self.tip_predicate)(&node) {
                return Some(id);
            }
        }

        None
    }

    pub fn find_bad_nodes(&self, graph: &DebruijnGraph<K, D>) -> Vec<usize> {

        (0..graph.len())
            .map(|i| self.test_tip(graph, i))
            .filter_map(|x| x)
            .collect()
    }
}