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

refactor to put everything in graph attributes instead of name

parent db2ebce9
......@@ -74,16 +74,15 @@ class D2Graph(nx.Graph):
# Write the distances
for x, y, data in self.edges(data=True):
dg1 = self.node_by_name[x]
dg2 = self.node_by_name[y]
fp.write(f"{dg1.idx} {dg2.idx} {data['distance']}\n")
fp.write(f"{x} {y} {data['distance']}\n")
def load(self, filename):
# Reload the graph
G = nx.read_gexf(filename)
for node, attrs in G.nodes(data=True):
self.add_node(node, attr_dict=attrs)
self.add_node(node)
self.nodes[node].update(attrs)
for edge in G.edges(data=True):
self.add_edge(edge[0], edge[1], distance=edge[2]["distance"])
......@@ -92,8 +91,9 @@ class D2Graph(nx.Graph):
self.node_by_idx = {}
self.node_by_name = {}
self.bidict_nodes = {}
for idx, node in enumerate(self.nodes()):
dg = Dgraph.load(node, self.barcode_graph)
for idx, node in enumerate(self.nodes(data=True)):
node, data = node
dg = Dgraph.load(data["udg"], self.barcode_graph)
self.bidict_nodes[node] = dg
self.all_d_graphs.append(dg)
if dg.idx == -1:
......@@ -125,8 +125,8 @@ class D2Graph(nx.Graph):
def compute_distances(self):
for x, y, data in self.edges(data=True):
dg1 = self.node_by_name[x]
dg2 = self.node_by_name[y]
dg1 = self.node_by_idx[x]
dg2 = self.node_by_idx[y]
if dg1 == dg2:
continue
......@@ -174,10 +174,16 @@ class D2Graph(nx.Graph):
for d_idx, dg in enumerate(self.index[dmer]):
# Create a node name
if not dg in nodes:
nodes[dg] = str(dg)
nodes[dg] = dg.idx
# Add the node
self.add_node(nodes[dg])
# Add covering barcode edges
barcode_edges = " ".join([str(self.barcode_edge_idxs[x]) for x in dg.edges])
self.nodes[nodes[dg]]["barcode_edges"] = barcode_edges
self.nodes[nodes[dg]]["score"] = f"{dg.score}/{dg.get_optimal_score()}"
self.nodes[nodes[dg]]["udg"] = str(dg)
# Add the edges
for prev_node in self.index[dmer][:d_idx]:
......
......@@ -29,7 +29,7 @@ class Dgraph(object):
# Head parsing
head = head.split(' ')
center = head[-3]
center = head[0]
if not center in barcode_graph:
center = int(center)
dg = Dgraph(center)
......@@ -86,12 +86,12 @@ class Dgraph(object):
def get_link_divergence(self):
return abs(self.score - self.get_optimal_score())
return int(abs(self.score - self.get_optimal_score()))
def get_optimal_score(self):
max_len = max(len(self.halves[0]), len(self.halves[1]))
return max_len * (max_len - 1) / 2
return int(max_len * (max_len - 1) / 2)
def to_list(self):
......@@ -195,8 +195,7 @@ class Dgraph(object):
def __repr__(self):
# print(self.halves)
representation = "" if self.idx == -1 else f"{self.idx} "
representation += str(self.center) + " " + str(self.score) + "/" + str(self.get_optimal_score()) + " "
representation = str(self.center) + " "
representation += "[" + ", ".join([f"{node} {self.connexity[0][node]}" for node in self.halves[0]]) + "]"
representation += "[" + ", ".join([f"{node} {self.connexity[1][node]}" for node in self.halves[1]]) + "]"
return representation
......
......@@ -69,8 +69,8 @@ class TestD2Graph(unittest.TestCase):
}
for x, y, data in d2.edges(data=True):
dg1 = d2.node_by_idx[int(x.split(" ")[0])]
dg2 = d2.node_by_idx[int(y.split(" ")[0])]
dg1 = d2.node_by_idx[x]
dg2 = d2.node_by_idx[y]
awaited_dist = awaited_distances[dg1.center][dg2.center]
self.assertEquals(data["distance"], awaited_dist)
......
Markdown is supported
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