Commit 897907a2 authored by Yoann Dufresne's avatar Yoann Dufresne
Browse files

d2-graph reconstruction by d-1 overlapps

parent d27838a2
......@@ -20,6 +20,24 @@ class D2Graph(object):
def create_index(self):
index = {}
perfect = 0
for node in self.d_graphs:
for dg in self.d_graphs[node]:
nodeset = dg.to_node_set()
# Generate all dmers without one node
for el in nodeset:
dmer = nodeset.difference(frozenset([el]))
if not dmer in index:
index[dmer] = [dg]
else:
index[dmer].append(dg)
return index
def create_index_ordered(self):
index = {}
perfect = 0
for node in self.d_graphs:
for dg in self.d_graphs[node]:
......@@ -49,25 +67,25 @@ class D2Graph(object):
return index
def save_to_file(self, filename):
def to_nx_graph(self):
next_idx = 0
nodes = {}
G = nx.Graph()
# for dmer in self.dmers:
# for d_idx, dg in enumerate(self.dmers[dmer]):
# # Create a node name
# if not dg in nodes:
# nodes[dg] = next_idx
# next_idx += 1
for dmer in self.index:
for d_idx, dg in enumerate(self.index[dmer]):
# Create a node name
if not dg in nodes:
nodes[dg] = next_idx
next_idx += 1
# # Add the node
# G.add_node(nodes[dg])
# Add the node
G.add_node(nodes[dg])
# # Add the edges
# for prev_node in self.dmers[dmer][:d_idx]:
# G.add_edge(nodes[dg], nodes[prev_node])
# Add the edges
for prev_node in self.index[dmer][:d_idx]:
G.add_edge(nodes[dg], nodes[prev_node])
nx.write_gexf(G,filename)
return G, nodes
\ No newline at end of file
......@@ -66,6 +66,10 @@ class Dgraph(object):
return hands[0][::-1] + [[self.center]] + hands[1]
def to_node_set(self):
return frozenset(self.halves[0] + self.halves[1] + [self.center])
def __eq__(self, other):
my_tuple = (self.get_link_divergence(), self.get_optimal_score())
other_tuple = (other.get_link_divergence(), other.get_optimal_score())
......@@ -80,6 +84,30 @@ class Dgraph(object):
return (my_tuple < other_tuple)
def __hash__(self):
nodelist = list(self.to_node_set())
nodelist.sort()
return ",".join(nodelist).__hash__()
def __ordered_hash__(self):
lst = self.to_ordered_lists()
fwd_uniq_lst = [sorted(l) for l in lst]
fwd_str = ",".join([f"[{'-'.join(l)}]" for l in fwd_uniq_lst])
fwd_hash = fwd_str.__hash__()
rev_uniq_lst = [sorted(l) for l in lst[::-1]]
rev_str = ",".join([f"[{'-'.join(l)}]" for l in rev_uniq_lst])
rev_hash = rev_str.__hash__()
# print()
# print(fwd_str)
# print(rev_str)
return int(min(fwd_hash, rev_hash))
def __repr__(self):
# print(self.halves)
representation = self.center + " " + str(self.score) + "/" + str(self.get_optimal_score()) + " "
......
......@@ -20,7 +20,8 @@ def main():
G = nx.read_gexf(filename)
d2g = d2.D2Graph(G)
# d2g.save_to_file("data/d2_graph.gexf")
G, names = d2g.to_nx_graph()
nx.write_gexf(G, "data/d2_graph.gexf")
if __name__ == "__main__":
main()
......@@ -37,5 +37,15 @@ class TestD2Graph(unittest.TestCase):
self.assertEquals(uodg, d2.index[key][0])
def test_to_nx_graph(self):
d2 = D2Graph(complete_graph)
d2G, node_names = d2.to_nx_graph()
nodes = list(d2G.nodes())
self.assertEquals(2, len(nodes))
edges = list(d2G.edges())
self.assertEquals(1, len(edges))
if __name__ == "__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