Commit 4c84978d authored by Yoann Dufresne's avatar Yoann Dufresne
Browse files

d2 reloaded completed and tested

parent 96a90ed9
...@@ -11,6 +11,17 @@ class D2Graph(object): ...@@ -11,6 +11,17 @@ class D2Graph(object):
super(D2Graph, self).__init__() super(D2Graph, self).__init__()
self.graph = graph self.graph = graph
# Number the edges from original graph
self.edge_idxs = {}
self.nb_uniq_edge = 0
for idx, edge in enumerate(self.graph.edges()):
if edge == (edge[1], edge[0]):
self.nb_uniq_edge += 1
if edge in self.edge_idxs:
print("Edge already present")
self.edge_idxs[edge] = idx
self.edge_idxs[(edge[1], edge[0])] = idx
def construct_from_barcodes(self, index_size=3, verbose=True, debug=False): def construct_from_barcodes(self, index_size=3, verbose=True, debug=False):
# Compute all the d-graphs # Compute all the d-graphs
...@@ -29,17 +40,6 @@ class D2Graph(object): ...@@ -29,17 +40,6 @@ class D2Graph(object):
d_graph.idx = idx d_graph.idx = idx
self.node_by_idx[idx] = d_graph self.node_by_idx[idx] = d_graph
# Number the edges from original graph
self.edge_idxs = {}
self.nb_uniq_edge = 0
for idx, edge in enumerate(self.graph.edges()):
if edge == (edge[1], edge[0]):
self.nb_uniq_edge += 1
if edge in self.edge_idxs:
print("Edge already present")
self.edge_idxs[edge] = idx
self.edge_idxs[(edge[1], edge[0])] = idx
# Index all the d-graphes # Index all the d-graphes
if verbose: if verbose:
print("Compute the dmer index") print("Compute the dmer index")
...@@ -75,8 +75,31 @@ class D2Graph(object): ...@@ -75,8 +75,31 @@ class D2Graph(object):
# Extract d-graphs from nx graph # Extract d-graphs from nx graph
self.all_d_graphs = [] self.all_d_graphs = []
for node in self.nx_graph.nodes(): self.node_by_idx = {}
self.all_d_graphs.append(Dgraph.load(node, self.graph)) for idx, node in enumerate(self.nx_graph.nodes()):
dg = Dgraph.load(node, self.graph)
self.all_d_graphs.append(dg)
if dg.idx == -1:
dg.idx = idx
self.node_by_idx[dg.idx] = dg
# Extract edges and re-compute distances
self.distances = {}
for edge in self.nx_graph.edges():
# Get the dg pair
idx1 = int(edge[0].split(' ')[0])
idx2 = int(edge[1].split(' ')[0])
dg1 = self.node_by_idx[idx1]
dg2 = self.node_by_idx[idx2]
# Compute and save the distance
dist = dg1.distance_to(dg2)
if not idx1 in self.distances:
self.distances[idx1] = {}
self.distances[idx1][idx2] = dist
if not idx2 in self.distances:
self.distances[idx2] = {}
self.distances[idx2][idx1] = dist
def create_index_from_tuples(self, tuple_size=3): def create_index_from_tuples(self, tuple_size=3):
......
...@@ -29,10 +29,15 @@ class Dgraph(object): ...@@ -29,10 +29,15 @@ class Dgraph(object):
# Head parsing # Head parsing
head = head.split(' ') head = head.split(' ')
dg = Dgraph(head[-3]) dg = Dgraph(int(head[-3]))
if len(head) == 4: if len(head) == 4:
dg.idx = int(head[0]) dg.idx = int(head[0])
# Reload halves
h1 = [int(x.split(' ')[-2]) for x in h1.split(',')]
h2 = [int(x.split(' ')[-2]) for x in h2.split(',')]
dg.put_halves(h1, h2, barcode_graph)
return dg return dg
......
...@@ -113,6 +113,15 @@ class TestD2Graph(unittest.TestCase): ...@@ -113,6 +113,15 @@ class TestD2Graph(unittest.TestCase):
for dg in d2.all_d_graphs: for dg in d2.all_d_graphs:
self.assertTrue(dg.idx in reloaded_idxs) self.assertTrue(dg.idx in reloaded_idxs)
# Verify distances
self.assertEquals(len(d2.distances), len(d2_reloaded.distances))
for idx1 in d2.distances:
self.assertTrue(idx1 in d2_reloaded.distances)
self.assertEquals(len(d2.distances[idx1]), len(d2_reloaded.distances[idx1]))
for idx2 in d2.distances[idx1]:
self.assertTrue(idx2 in d2_reloaded.distances[idx1])
self.assertEquals(d2.distances[idx1][idx2], d2_reloaded.distances[idx1][idx2])
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
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