Commit fcdd78b1 authored by Yoann Dufresne's avatar Yoann Dufresne
Browse files

d2 graph is working \o/

parent 897907a2
...@@ -6,7 +6,7 @@ from d_graph import compute_all_max_d_graphs ...@@ -6,7 +6,7 @@ from d_graph import compute_all_max_d_graphs
class D2Graph(object): class D2Graph(object):
"""D2Graph (read it (d-graph)²)""" """D2Graph (read it (d-graph)²)"""
def __init__(self, graph): def __init__(self, graph, index_size=10):
super(D2Graph, self).__init__() super(D2Graph, self).__init__()
self.graph = graph self.graph = graph
...@@ -14,19 +14,23 @@ class D2Graph(object): ...@@ -14,19 +14,23 @@ class D2Graph(object):
self.d_graphs = compute_all_max_d_graphs(self.graph) self.d_graphs = compute_all_max_d_graphs(self.graph)
# Index all the d-graphes # Index all the d-graphes
self.index = self.create_index() self.index = self.create_index_from_tuples(index_size)
def create_index(self):
def create_index_from_tuples(self, tuple_size=3):
index = {} index = {}
perfect = 0 perfect = 0
for node in self.d_graphs: for node in self.d_graphs:
for dg in self.d_graphs[node]: for dg in self.d_graphs[node]:
nodeset = dg.to_node_set() nodelist = dg.to_list()
# Generate all dmers without one node nodelist.sort()
for el in nodeset: if len(nodelist) < tuple_size:
dmer = nodeset.difference(frozenset([el])) continue
# Generate all tuplesize-mers
for dmer in itertools.combinations(nodelist, tuple_size):
if not dmer in index: if not dmer in index:
index[dmer] = [dg] index[dmer] = [dg]
else: else:
......
...@@ -51,6 +51,10 @@ class Dgraph(object): ...@@ -51,6 +51,10 @@ class Dgraph(object):
return max_len * (max_len - 1) / 2 return max_len * (max_len - 1) / 2
def to_list(self):
return self.halves[0]+ [self.center] + self.halves[1]
def to_ordered_lists(self): def to_ordered_lists(self):
hands = [[],[]] hands = [[],[]]
for idx in range(2): for idx in range(2):
...@@ -66,14 +70,12 @@ class Dgraph(object): ...@@ -66,14 +70,12 @@ class Dgraph(object):
return hands[0][::-1] + [[self.center]] + hands[1] return hands[0][::-1] + [[self.center]] + hands[1]
def to_node_set(self): def to_node_multiset(self):
return frozenset(self.halves[0] + self.halves[1] + [self.center]) return frozenset(self.to_list())
def __eq__(self, other): def __eq__(self, other):
my_tuple = (self.get_link_divergence(), self.get_optimal_score()) return self.to_ordered_lists() == other.to_ordered_lists()
other_tuple = (other.get_link_divergence(), other.get_optimal_score())
return (my_tuple == other_tuple)
def __ne__(self, other): def __ne__(self, other):
return not (self == other) return not (self == other)
...@@ -85,7 +87,7 @@ class Dgraph(object): ...@@ -85,7 +87,7 @@ class Dgraph(object):
def __hash__(self): def __hash__(self):
nodelist = list(self.to_node_set()) nodelist = list(self.to_list())
nodelist.sort() nodelist.sort()
return ",".join(nodelist).__hash__() return ",".join(nodelist).__hash__()
......
...@@ -8,7 +8,7 @@ from tests.d_graph_data import unit_d_graph, unit_overlapp_d_graph, complete_gra ...@@ -8,7 +8,7 @@ from tests.d_graph_data import unit_d_graph, unit_overlapp_d_graph, complete_gra
class TestD2Graph(unittest.TestCase): class TestD2Graph(unittest.TestCase):
def test_construction(self): def test_construction(self):
d2 = D2Graph(complete_graph) d2 = D2Graph(complete_graph, 6)
# Evaluate the number of candidate unit d_graphs generated # Evaluate the number of candidate unit d_graphs generated
for node, candidates in d2.d_graphs.items(): for node, candidates in d2.d_graphs.items():
...@@ -17,28 +17,20 @@ class TestD2Graph(unittest.TestCase): ...@@ -17,28 +17,20 @@ class TestD2Graph(unittest.TestCase):
else: else:
self.assertEquals(0, len(candidates)) self.assertEquals(0, len(candidates))
# Evaluate the hashes # Evaluate the index
self.assertEquals(3, len(d2.index)) self.assertEquals(13, len(d2.index))
udg = Dgraph(unit_d_graph[0]) overlap_key = ('A1', 'A2', 'B0', 'B1', 'B2', 'C')
udg.put_halves(unit_d_graph[1], unit_d_graph[2], unit_d_graph[3]) for dmer, dg_lst in d2.index.items():
uodg = Dgraph(unit_overlapp_d_graph[0]) if dmer == overlap_key:
uodg.put_halves(unit_overlapp_d_graph[1], unit_overlapp_d_graph[2], unit_overlapp_d_graph[3]) self.assertEquals(2, len(d2.index[dmer]))
self.assertNotEquals(d2.index[dmer][0], d2.index[dmer][1])
key = frozenset({'A2', 'A1', 'B1', 'C', 'B0', 'B2'}) else:
self.assertEquals(2, len(d2.index[key])) self.assertEquals(1, len(d2.index[dmer]))
self.assertTrue(udg in d2.index[key])
self.assertTrue(uodg in d2.index[key])
key = frozenset({'A0', 'A2', 'A1', 'B1', 'C', 'B2'})
self.assertEquals(1, len(d2.index[key]))
self.assertEquals(udg, d2.index[key][0])
key = frozenset({'A2', 'B-1', 'B1', 'C', 'B2', 'B0'})
self.assertEquals(1, len(d2.index[key]))
self.assertEquals(uodg, d2.index[key][0])
def test_to_nx_graph(self): def test_to_nx_graph(self):
d2 = D2Graph(complete_graph) d2 = D2Graph(complete_graph, 6)
d2G, node_names = d2.to_nx_graph() d2G, node_names = d2.to_nx_graph()
nodes = list(d2G.nodes()) nodes = list(d2G.nodes())
self.assertEquals(2, len(nodes)) self.assertEquals(2, len(nodes))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment