-
Notifications
You must be signed in to change notification settings - Fork 2
/
AcyclicGraphDNA.py
374 lines (276 loc) · 11.1 KB
/
AcyclicGraphDNA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import numpy as np
import itertools
import networkx as nx
import pymetis
from graphviz import Digraph
import re
def sign(node, edge):
if node == edge[0]:
return -1 #output edge
elif node == edge[1]:
return 1 #input edge
else:
return 0 # node not in edge
def get_edges_for_node(node, edges):
node_edges = []
for edge in edges:
if node in edge:
node_edges.append(edge)
return node_edges
#!This transformation could be used when Longest path=Hamiltonian path and only for acyclic graphs!
def to_qubo(adjacency_matrix, A=1):
all_edges = np.argwhere(adjacency_matrix==1).tolist()
dim = len(all_edges)
nodes_quantity = len(adjacency_matrix)
M = np.zeros((dim, dim))
for node in range(nodes_quantity):
node_edges = get_edges_for_node(node, all_edges)
node_edges_plus = [edge for edge in node_edges if sign(node, edge)==1]
node_edges_minus = [edge for edge in node_edges if sign(node, edge)==-1]
# interaction of edges, sign depends on node direction: -1 output, +1 input
for edges in itertools.combinations(node_edges_plus, 2):
i, j = all_edges.index(edges[0]), all_edges.index(edges[1])
M[i][j] += sign(node, edges[0])*sign(node, edges[1])*A
M[j][i] += sign(node, edges[0])*sign(node, edges[1])*A
for edges in itertools.combinations(node_edges_minus, 2):
i, j = all_edges.index(edges[0]), all_edges.index(edges[1])
M[i][j] += sign(node, edges[0])*sign(node, edges[1])*A
M[j][i] += sign(node, edges[0])*sign(node, edges[1])*A
# diagonal part
for edge in node_edges_plus:
i = all_edges.index(edge)
M[i][i] -= A
# diagonal part
for edge in node_edges_minus:
i = all_edges.index(edge)
M[i][i] -= A
return M
def load_file(filename):
fd = open(filename , 'r')
lines = []
segments = []
links = []
containments = []
for line in fd:
if line[0] == 'S':
segments.append(line[1:])
if line[0] == 'L':
links.append(line[1:])
if line[0] == 'C':
containments.append(line[1:])
lines.append(line)
print("segments: %s, links: %s, containments: %s, lines: %s" %(len(segments), len(links), len(containments), len(lines)))
return segments, links, containments
# get all nodes
def get_nodes(segments):
nodes = []
for segment in segments:
name = re.search('[!-)+-<>-~][!-~]*', segment)
nodes.append(name[0]+'+')
nodes.append(name[0]+'-')
print('\nnodes quantity:', len(nodes))
nodes_labels = dict(enumerate(nodes))
return nodes, nodes_labels
# get all links
def get_edges(links, nodes):
dim = len(nodes)
adjacency_matrix = np.zeros(shape=(dim, dim))
swap = lambda sign: '+' if sign=='-' else '-'
links_edges = {}
for link in links:
l = re.findall('[!-)+-<>-~][!-~]*', link)
node1 = l[0]+l[1]
node2 = l[2]+l[3]
if node1 in nodes:
i, j = nodes.index(node1), nodes.index(node2)
adjacency_matrix[i, j] = 1
links_edges[(i, j)] = [node1, node2]
#adding strand
node1 = l[2]+swap(l[3])
node2 = l[0]+swap(l[1])
i, j = nodes.index(node1), nodes.index(node2)
adjacency_matrix[i, j] = 1
links_edges[(i, j)] = [node1, node2]
return adjacency_matrix, links_edges
def get_initial_graph(segments, links):
nodes, nodes_labels = get_nodes(segments)
adjacency_matrix, links_edges = get_edges(links, nodes)
edges = list(links_edges.values())
graph = nx.DiGraph()
graph.add_edges_from(edges)
return graph, adjacency_matrix, nodes, nodes_labels, links_edges
def get_part_graph(nodes, links):
G = nx.DiGraph()
for link in links.values():
node1 = link[0]
node2 = link[1]
if (node1 in nodes) and (node2 in nodes):
G.add_edge(node1, node2)
return G
def get_parted_graphs(part_map, node_labels, edges):
part_graphs = []
for part, part_nodes in part_map.items():
G = nx.DiGraph()
for edge in edges:
node1 = edge[0]
node2 = edge[1]
labels = [node_labels[i] for i in part_nodes]
if (node1 in labels) and (node2 in labels):
G.add_edge(node1, node2)
part_graphs.append(G)
return part_graphs
def get_parted_graphs(part_map, nodes_labels, edges):
part_graphs = []
for part, part_nodes in part_map.items():
G = nx.DiGraph()
for edge in edges:
node1 = edge[0]
node2 = edge[1]
labels = [nodes_labels[i] for i in part_nodes]
if (node1 in labels) and (node2 in labels):
G.add_edge(node1, node2)
part_graphs.append(G)
return part_graphs
def part_graph(cluster_number, adjacency_matrix, log=False):
adjacency_list = adjacency_matrix_to_adjacency_list(adjacency_matrix)
cut_count, part_vert = pymetis.part_graph(cluster_number, adjacency=adjacency_list)
# Find nodes in each partition:
part_map = {}
for i, p in enumerate(set(part_vert)):
part_map[p] = np.argwhere(np.array(part_vert) == p).ravel()
if log:
for part, nodes in part_map.items():
print('part %s, nodes quantity: %s' %(part, len(nodes)))
print(nodes)
return part_map, cut_count, part_vert
def draw_graph(node_labels, solve_edges, edges):
size = len(node_labels)
node_cnt = len(node_labels)
g = Digraph()
for i in range(size):
g.node(node_labels[i])
for edge in edges:
if edge not in solve_edges:
g.edge(node_labels[edge[0]], node_labels[edge[1]], color = 'grey')
for edge in solve_edges:
g.node(node_labels[edge[0]], style='filled', fillcolor='white')
g.node(node_labels[edge[1]], style='filled', fillcolor='white')
g.edge(node_labels[edge[0]], node_labels[edge[1]], color = 'lightskyblue', label = '', fontsize='20', fontcolor='red')
return g
def get_parted_graphs_graphiz(nodes, node_labels, edges):
part_graphs = []
colors = ['yellow', 'red', 'green', 'orange', 'brown']
for part, part_nodes in nodes.items():
G = Digraph()
for i in part_nodes:
G.node(node_labels[i], style='filled', fillcolor=colors[part])
for edge in edges:
node1 = edge[0]
node2 = edge[1]
labels = [node_labels[i] for i in part_nodes]
if (node1 in labels) and (node2 in labels):
G.edge(node1, node2, color='black')
part_graphs.append(G)
return part_graphs
def adjacency_matrix_to_adjacency_list(adjacency_matrix):
adjacency_list = []
indices = np.argwhere(adjacency_matrix==1)
for node in range(len(adjacency_matrix)):
adjacency_list.append([])
for index in indices:
if index[0] == node:
adjacency_list[node].append(index[1])
adjacency_list = [np.asarray(el) for el in adjacency_list]
return adjacency_list
def complement_to_undirected_graph(graph=None, edges=None):
if edges == None:
edges = graph.edges()
undirected_edges = []
for edge in edges:
undirected_edges.append(edge)
edge_reverse = [edge[1], edge[0]]
undirected_edges.append(edge_reverse)
graph = nx.DiGraph()
graph.add_edges_from(undirected_edges)
return graph
def get_strand_graph(graph):
undirected_graph = complement_to_undirected_graph(graph)
adjacency_matrix = nx.to_numpy_matrix(undirected_graph)
nodes_labels = list(undirected_graph.nodes())
#part graph into two
part_map, _ , _ = part_graph(cluster_number=2, adjacency_matrix=adjacency_matrix)
#take one strand
strand_graph = get_parted_graphs(part_map, nodes_labels, edges=graph.edges)[0]
return strand_graph
def get_source_and_sink(nodes, edges, log=False):
node_degrees = {}
for node in nodes:
input_degree = 0
output_degree = 0
for edge in edges:
if node==edge[1]:
input_degree += 1
elif node==edge[0]:
output_degree += 1
node_degrees[node] = [input_degree, output_degree]
result = []
for node, degree in node_degrees.items():
if degree[0] == 0:
if log:
print('node %s has degree 1 in input, index %s' %(node, nodes.index(node)))
result.append(node)
elif degree[1] == 0:
if log:
print('node %s has degree 1 in output, index %s' %(node, nodes.index(node)))
result.append(node)
return result
def get_unconnected_nodes(adjacency_matrix):
idx_row = np.where(~adjacency_matrix.any(axis=0))[0]
idx_columns = np.where(~adjacency_matrix.any(axis=1))[0]
print('rows to delete:', idx_row)
print('columns to delete:', idx_columns)
idx = np.intersect1d(idx_row, idx_columns)
print('indices to delete', idx)
return idx
def drop_isolated_nodes_and_update(adjacency_matrix, nodes, links):
idx = get_unconnected_nodes(adjacency_matrix)
print("idices of unconnected nodes", idx)
for i in np.flip(idx.flatten()):
print("node %s with index %s deleted" %(nodes[i], i))
del nodes[i]
nodes_labels = dict(enumerate(nodes))
adjacency_matrix = np.delete(adjacency_matrix, idx, axis=1) # drop zero rows
adjacency_matrix = np.delete(adjacency_matrix, idx, axis=0) # drop zero columns
print('adjacency_matrix.shape:', adjacency_matrix.shape)
#print(nodes_labels)
adjacency_matrix, links_edges = get_edges(links, nodes)
print('Links quantity: ', len(links_edges))
return nodes, nodes_labels, links_edges
def draw_solution(graphs, strand_graph, strand_graph_undirected, part_map, solve_edges, show_all_edges=True):
bridge_nodes = []
for pg in graphs:
bridge_nodes += get_source_and_sink(list(pg.nodes), list(pg.edges))
bridge_nodes
node_labels = list(strand_graph_undirected.nodes)
edges = strand_graph.edges
bridge_eges = []
for node1 in bridge_nodes:
for node2 in bridge_nodes:
if (node1, node2) in edges:
bridge_eges.append((node1, node2))
graph = Digraph()
colors = ['yellow', 'red', 'green', 'orange', 'brown']
for part, nodes in part_map.items():
for i in nodes:
graph.node(node_labels[i], style='filled', fillcolor=colors[part])
if show_all_edges:
for edge in edges:
if edge not in solve_edges and edge not in bridge_eges:
graph.edge(edge[0], edge[1], color='black')
for edge in solve_edges:
graph.edge(edge[0], edge[1], color = 'lightskyblue', label = '', fontsize='20', fontcolor='red')
for node1 in bridge_nodes:
for node2 in bridge_nodes:
if (node1, node2) in edges:
graph.edge(node1, node2, color = 'red', label = '', fontsize='20', fontcolor='red')
return graph