d2_path.py 2.25 KB
Newer Older
Yoann Dufresne's avatar
Yoann Dufresne committed
1
2
import networkx as nx

3

Yoann Dufresne's avatar
Yoann Dufresne committed
4
5
""" Represent an udg path into a d2g graph
"""
Yoann Dufresne's avatar
Yoann Dufresne committed
6
class Path(list):
7

Yoann Dufresne's avatar
Yoann Dufresne committed
8
    def __init__(self, d2g):
9
        super(Path, self).__init__()
Yoann Dufresne's avatar
Yoann Dufresne committed
10
11
        self.d2g = d2g
        self.covering_variables = {x: 0 for x in self.d2g.barcode_edge_idxs.values()}
Yoann Dufresne's avatar
Yoann Dufresne committed
12
        self.covering_value = 0
13

Yoann Dufresne's avatar
Yoann Dufresne committed
14
    def append(self, obj) -> None:
Yoann Dufresne's avatar
Yoann Dufresne committed
15
        lcp = self.d2g.get_lcp(obj)
Yoann Dufresne's avatar
Yoann Dufresne committed
16
17
18
19
20
21
22
23
24
25

        # Update the covering variables
        for barcode_edge in lcp.edges:
            edge_idx = self.d2g.barcode_edge_idxs[barcode_edge]
            if self.covering_variables[edge_idx] == 0:
                self.covering_value += 1
            self.covering_variables[edge_idx] += 1

        super(Path, self).append(lcp)

Yoann Dufresne's avatar
Yoann Dufresne committed
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
    def pop(self, index=-1):
        lcp = super(Path, self).pop(index)

        # Update the covering variables
        for barcode_edge in lcp.edges:
            edge_idx = self.d2g.barcode_edge_idxs[barcode_edge]
            self.covering_variables[edge_idx] -= 1
            if self.covering_variables[edge_idx] == 0:
                self.covering_value -= 1

        return lcp

    def copy(self):
        copy = Path(self.d2g)

        # Copy the list
        for x in self:
            super(Path, copy).append(x)

        # Copy the variables
        for key, val in self.covering_variables.items():
            copy.covering_variables[key] = val
        copy.covering_value = self.covering_value

        return copy

Yoann Dufresne's avatar
Yoann Dufresne committed
52
53
54
    def add_path(self, path):
        for lcp in path:
            self.append(lcp)
Yoann Dufresne's avatar
Yoann Dufresne committed
55

Yoann Dufresne's avatar
Yoann Dufresne committed
56
57
    def covering_score(self):
        return self.covering_value / len(self.covering_variables)
Yoann Dufresne's avatar
Yoann Dufresne committed
58

Yoann Dufresne's avatar
Yoann Dufresne committed
59
    def save_gexf(self, filename):
Yoann Dufresne's avatar
Yoann Dufresne committed
60
61
62
63
64
65
66
        d2p = nx.Graph()
        # Add the nodes
        for udg in self:
            d2p.add_node(udg.idx)
            d2p.nodes[udg.idx]["center"] = udg.center
            d2p.nodes[udg.idx]["udg"] = str(udg)
            d2p.nodes[udg.idx]["score"] = f"{udg.score}/{udg.get_optimal_score()}"
Yoann Dufresne's avatar
Yoann Dufresne committed
67
68
            barcode_edges = " ".join([str(self.d2g.barcode_edge_idxs[x]) for x in udg.edges])
            d2p.nodes[udg.idx]["barcode_edges"] = barcode_edges
Yoann Dufresne's avatar
Yoann Dufresne committed
69
70
71
72
73
74
75
76

        # add the edges
        for idx in range(len(self)-1):
            udg1 = self[idx]
            udg2 = self[idx+1]

            d2p.add_edge(udg1.idx, udg2.idx)

Yoann Dufresne's avatar
Yoann Dufresne committed
77
78
        nx.write_gexf(d2p, filename)