def check_and_draw(base_graph, abstr): ''' Parameters ---------- base_graph: a base graph abstr: an abstract graph Returns ------- check if EVERY node in base_graph is in any abstr.graph.node['contracted'] ''' nodeset = set([a for n, d in abstr.nodes(data=True) for a in d['contracted']]) broken = [] for n in base_graph.nodes(): if n not in nodeset: broken.append(n) base_graph.node[n]['colo'] = .5 if len(broken) > 0: print "FOUND SOMETHING BROKEN:" draw.set_ids(base_graph) base_graph.graph['info'] = 'failed to see these:%s' % str(broken) edraw.draw_graph(base_graph, vertex_label='id', vertex_color='colo', edge_label=None, size=20) for e, d in abstr.nodes(data=True): d['label'] = str(d.get('contracted', '')) edraw.draw_graph(abstr, vertex_label='label', vertex_color=None, edge_label=None, size=20) return False return True
def _draw(self, seq, file_name=None, mutations=None, scores=None, fold=None): seqs = [('header_placeholder', seq)] graphs = fold(seqs) graph = graphs.next() graph = self._update_graph(graph, scores, mutations) opts = { 'size': 14, 'font_size': 9, 'layout': 'KK', 'vertex_border': False, 'vertex_size': 400, 'vertex_alpha': 0.5, 'invert_colormap': True, 'secondary_vertex_label': 'alternative', 'vertex_color': 'stability', 'colormap': 'OrRd', 'edge_label': None, 'edge_alpha': 0.2, 'dark_edge_alpha': 0.8, 'ignore_for_layout': 'nesting' } draw_graph(graph, file_name=file_name, **opts)
def display_edge_predictions(g, tr_graphs, tr_targets, te_graphs, te_targets, preds, vertex_color='typeof', size=15): """display_edge_predictions.""" tr_roots = [gg.graph['roots'] for gg in tr_graphs] graph = g.copy() for (u, v), t in zip(tr_roots, tr_targets): if t == 1: graph.edge[u][v]['color'] = 'gray' te_roots = [gg.graph['roots'] for gg in te_graphs] for (u, v), p, t in zip(te_roots, preds, te_targets): # for all predicted edges if p == 1: # true positive if t == 1: graph.edge[u][v]['color'] = 'limegreen' # false positive if t == 0: graph.add_edge(u, v) graph.edge[u][v]['nesting'] = True graph.edge[u][v]['color'] = 'gray' if p == 0: # false negative if t == 1: graph.edge[u][v]['color'] = 'crimson' draw_graph(graph, size=size, vertex_color=vertex_color, vertex_size=100, vertex_label=None, edge_label=None, edge_color='color', edge_alpha=1, ignore_for_layout='nesting', dark_edge_alpha=.9, dark_edge_color='color') return graph
def getGraph9(peak, alpha): seq, stru = peak bg = fgb.BulgeGraph.from_dotbracket(stru) dict = {} i = 0 G = nx.Graph() for key2 in bg.defines.keys(): G.add_node(i, label=key2[0]) dict[key2] = i i += 1 for key1 in bg.edges: for key2 in bg.edges[key1]: G.add_edge(dict[key1], dict[key2], label='v') if DEBUG == 1: print(seq) print(stru) display.draw_graph(G, size=15, node_size=1500, font_size=24, node_border=True, size_x_to_y_ratio=3) return G
def show_graph(g, vertex_color='typeof', size=15, vertex_label=None): """show_graph.""" degrees = [len(g.neighbors(u)) for u in g.nodes()] print(('num nodes=%d' % len(g))) print(('num edges=%d' % len(g.edges()))) print(('num non edges=%d' % len(list(nx.non_edges(g))))) print(('max degree=%d' % max(degrees))) print(('median degree=%d' % np.percentile(degrees, 50))) draw_graph(g, size=size, vertex_color=vertex_color, vertex_label=vertex_label, vertex_size=200, edge_label=None) # display degree distribution size = int((max(degrees) - min(degrees)) / 1.5) plt.figure(figsize=(size, 3)) plt.title('Degree distribution') _bins = np.arange(min(degrees), max(degrees) + 2) - .5 n, bins, patches = plt.hist(degrees, _bins, alpha=0.3, facecolor='navy', histtype='bar', rwidth=0.8, edgecolor='k') labels = np.array([str(int(i)) for i in n]) for xi, yi, label in zip(bins, n, labels): plt.text(xi + 0.5, yi, label, ha='center', va='bottom') plt.xticks(bins + 0.5) plt.xlim((min(degrees) - 1, max(degrees) + 1)) plt.ylim((0, max(n) * 1.1)) plt.xlabel('Node degree') plt.ylabel('Counts') plt.grid(linestyle=":") plt.show()
def draw_print(seq, shape, file_name= 'lol.svg',stru=None): '''seq+shape => image''' if stru != None: brack = stru else: brack = rnafold.fold(seq) graph = util.sequence_dotbracket_to_graph(seq,brack) graph.graph['structure']= brack graph = annotate(graph.copy(), shape) ed.draw_graph(graph, size=5, layout="RNA",vertex_color='col', vertex_label=None, edge_alpha=0.4, vertex_size=150, vertex_border=False, file_name=file_name)
def draw_match(GA, GB, pairings, size=10): G = nx.disjoint_union(GA, GB) for i, jj in enumerate(pairings): j = len(GA) + jj if G.node[i]['label'] == G.node[j]['label']: G.add_edge(i, j, label=i, nesting=True) draw_graph(G, size=size, vertex_border=False, vertex_size=400, edge_label=None, edge_alpha=.2, dark_edge_color='label', dark_edge_dotted=False, dark_edge_alpha=1, colormap='Set2', ignore_for_layout='nesting')
def draw_match(GA, GB, pairings, size=10): G = nx.disjoint_union(GA, GB) for i, jj in enumerate(pairings): j = len(GA) + jj if G.node[i]['label'] == G.node[j]['label']: G.add_edge(i, j, label=i, nesting=True) draw_graph( G, size=size, vertex_border=False, vertex_size=400, edge_label=None, edge_alpha=.2, dark_edge_color='label', dark_edge_dotted=False, dark_edge_alpha=1, colormap='Set2', ignore_for_layout='nesting')
def getGraph10(peak, G1, alpha): seq, stru = peak bg = fgb.BulgeGraph.from_dotbracket(stru) n = len(seq) G = G1.copy() i = n dict = {} for key2 in bg.defines.keys(): if key2[0] == 'h': G.add_node(i, label=key2[0]) elif key2[0] == 's': G.add_node(i, label=key2[0]) else: G.add_node(i, label='i') dict[key2] = i i += 1 for key1 in bg.edges: for key2 in bg.edges[key1]: G.add_edge(dict[key1], dict[key2], label='v') for i in range(n): key = bg.get_node_from_residue_num(i + 1) G.add_edge(i, dict[key], label='abb') if DEBUG == 1: print(seq) print(stru) display.draw_graph(G, size=25, node_size=2000, font_size=14, node_border=True, size_x_to_y_ratio=3) return G
def display_multiple_correspondences(Gs, GB, decomposition_function=None, size=6, n_graphs_per_line=6): pos = get_pos_from_abstract_graph(GB) if decomposition_function is None: df1 = do_decompose(decompose_nodes_and_edges, decompose_neighborhood(radius=1), compose_function=decompose_context(radius=1)) df2 = do_decompose(decompose_nodes_and_edges, decompose_neighborhood(radius=1), compose_function=decompose_context(radius=2)) decomposition_function = do_decompose( df1, df2, decompose_cycles, decompose_neighborhood(max_radius=3)) n = len(Gs) n_rows = n // n_graphs_per_line plt.figure(figsize=(n_graphs_per_line * size, size)) for i in range(n): GA = Gs[i] graph_A, pos_A, graph_B, pos_B, map_from_A_to_B = map_first_onto_second( GA, GB, pos=pos, decomposition_function=decomposition_function) colors = map_labels_to_colors([graph_A, graph_B]) kwargs = dict(size=None, colormap='tab20c', vertex_size=80, edge_label=None, vertex_color_dict=colors, vmax=1, vmin=0, vertex_color='_label_', vertex_label=None, layout='spring', ignore_for_layout='nesting') plt.subplot(n_rows + 1, n_graphs_per_line, i + 1) draw_graph(graph_A, pos=pos_A, **kwargs) plt.show()
def display_correspondences(GA, GB, decomposition_function=None, size=4): plt.figure(figsize=(4.2 * size, size)) graph_A, pos_A, graph_B, pos_B, map_from_A_to_B = map_first_onto_second( GA, GB, decomposition_function=decomposition_function) colors = map_labels_to_colors([graph_A, graph_B]) kwargs = dict(size=None, colormap='tab20c', vertex_size=80, edge_label=None, vertex_color_dict=colors, vmax=1, vmin=0, vertex_color='_label_', vertex_label=None, layout='spring', ignore_for_layout='nesting') plt.subplot(1, 4, 1) draw_graph(graph_A, pos=pos_A, **kwargs) plt.subplot(1, 4, 2) draw_graph(graph_B, pos=pos_B, **kwargs) graph_A_i, pos_A_i, graph_B_i, pos_B_i, map_from_A_to_B_i = map_first_onto_second( GB, GA, decomposition_function=decomposition_function) plt.subplot(1, 4, 3) draw_graph(graph_A_i, pos=pos_A_i, **kwargs) plt.subplot(1, 4, 4) draw_graph(graph_B_i, pos=pos_B_i, **kwargs) plt.show() if is_abstract_graph(GA) and is_abstract_graph(GB): graph_A_base_nodes = [ u for u in graph_A.nodes() if 'isa' not in graph_A.nodes[u] ] graph_A_base = nx.subgraph(graph_A, graph_A_base_nodes) graph_B_base_nodes = [ u for u in graph_B.nodes() if 'isa' not in graph_B.nodes[u] ] graph_B_base = nx.subgraph(graph_B, graph_B_base_nodes) graph_A_i_base_nodes = [ u for u in graph_A_i.nodes() if 'isa' not in graph_A_i.nodes[u] ] graph_A_i_base = nx.subgraph(graph_A_i, graph_A_i_base_nodes) graph_B_i_base_nodes = [ u for u in graph_B_i.nodes() if 'isa' not in graph_B_i.nodes[u] ] graph_B_i_base = nx.subgraph(graph_B_i, graph_B_i_base_nodes) plt.figure(figsize=(4.2 * size, size)) plt.subplot(1, 4, 1) draw_graph(graph_A_base, pos=pos_A, **kwargs) plt.subplot(1, 4, 2) draw_graph(graph_B_base, pos=pos_B, **kwargs) plt.subplot(1, 4, 3) draw_graph(graph_A_i_base, pos=pos_A_i, **kwargs) plt.subplot(1, 4, 4) draw_graph(graph_B_i_base, pos=pos_B_i, **kwargs) plt.show()
def getGraph14(peak, all, upsc, struYes, alpha): seq, stru2 = peak if struYes == 1: stru = stru2 else: stru = "" for i in range(len(seq)): stru += "." bgw = 1 bbw = 1 bg = fgb.BulgeGraph.from_dotbracket(stru) n = len(seq) if len(bg.defines.keys()) == 1: #Empty structure G = nx.Graph() for i in range(n): G.add_node(i, label=seq[i], weight=1) for i in range(n - 1): G.add_edge(i, i + 1, label='bb', weight=1) if DEBUG == 1: print(seq) print(stru) display.draw_graph(G, size=15, node_size=1500, font_size=24, node_border=True, size_x_to_y_ratio=3) return G n = len(seq) dict = {} i = 0 G = nx.Graph() for key2 in bg.defines.keys(): lb = key2[0] if lb != 's': lb = 'u' w = 10 else: w = 10 G.add_node(i, label=lb, weight=w) dict[key2] = i i += 1 hairpins = {} for key1 in bg.edges: for key2 in bg.edges[key1]: G.add_edge(dict[key1], dict[key2], label='v', weight=bgw) if all == 0: for j in range(n): key = bg.get_node_from_residue_num(j + 1) if key[0] == 'h' or G.number_of_nodes( ) == 1: #or key[0]=='m' or key[0]=='i': if key not in hairpins: hairpins[key] = [] hairpins[key].append(j) elif all == 1: for j in range(n): key = bg.get_node_from_residue_num(j + 1) if key[0] == 'h' or key[0] == 'i' or G.number_of_nodes() == 1: if key not in hairpins: hairpins[key] = [] hairpins[key].append(j) elif all == 2: for j in range(n): key = bg.get_node_from_residue_num(j + 1) if key[0] == 'i' or G.number_of_nodes() == 1: if key not in hairpins: hairpins[key] = [] hairpins[key].append(j) elif all == 3: for j in range(n): key = bg.get_node_from_residue_num(j + 1) if (key[0] == 's') or G.number_of_nodes() == 1: if key not in hairpins: hairpins[key] = [] hairpins[key].append(j) elif all == 4: for j in range(n): key = bg.get_node_from_residue_num(j + 1) if key not in hairpins: hairpins[key] = [] hairpins[key].append(j) elif all == 5: for j in range(n): key = bg.get_node_from_residue_num(j + 1) if key[0] == 'm' or key[0] == 'f' or key[ 0] == 't' or G.number_of_nodes() == 1: if key not in hairpins: hairpins[key] = [] hairpins[key].append(j) else: for j in range(n): key = bg.get_node_from_residue_num(j + 1) if (key[0] != 's') or G.number_of_nodes() == 1: if key not in hairpins: hairpins[key] = [] hairpins[key].append(j) for key in hairpins: st = min(hairpins[key]) ed = max(hairpins[key]) G.add_node(i, label=alpha[seq[st:st + 1]]) if key[0] != 'f': G.add_edge(dict[key], i, label='abb', weight=bgw) i += 1 if key[0] != 'i' and key[0] != 's': for j in range(st + 1, ed): G.add_node(i, label=alpha[seq[j:j + 1]]) G.add_edge(i - 1, i, label='bb', weight=bbw) #if upsc==1: G.add_edge(dict[key], i, label='abb', weight=bgw) i += 1 if len(hairpins[key]) > 1: G.add_node(i, label=alpha[seq[ed:ed + 1]]) G.add_edge(i - 1, i, label='bb', weight=bbw) i += 1 #if key[0]!='t': G.add_edge(i - 1, dict[key], label='abb', weight=bgw) else: j = st + 1 while (j in hairpins[key]): G.add_node(i, label=alpha[seq[j:j + 1]]) G.add_edge(i - 1, i, label='bb', weight=bbw) i += 1 j += 1 if j - st > 1: G.add_edge(i - 1, dict[key], label='abb', weight=bgw) elif upsc == 1: G.add_edge(dict[key], i - 1, label='abb', weight=bgw) if st != ed and j != ed + 1: G.add_node(i, label=alpha[seq[ed:ed + 1]]) G.add_edge(dict[key], i, label='abb', weight=bgw) if upsc == 1: G.add_edge(dict[key], i, label='abb', weight=bgw) i += 1 j = ed - 1 while (j in hairpins[key]): G.add_node(i, label=alpha[seq[j:j + 1]]) G.add_edge(i - 1, i, label='bb', weight=bbw) #if upsc==1: G.add_edge(dict[key], i, label='abb', weight=bgw) i += 1 j -= 1 if ed - j > 1 and upsc == 0: G.add_edge(i - 1, dict[key], label='abb', weight=bgw) if DEBUG == 1: print(seq) print(stru) display.draw_graph(G, size=15, node_size=1500, font_size=24, node_border=True, size_x_to_y_ratio=3, vertex_color='red') return G
def getGraph11(peak, G1, alpha): seq, stru = peak bps = getbps(stru) bg = fgb.BulgeGraph.from_dotbracket(stru) n = len(seq) G = G1.copy() i = n dict = {} for key2 in bg.defines.keys(): if key2[0] == 'h': G.add_node(i, label=key2[0]) elif key2[0] == 's': G.add_node(i, label=key2[0]) else: G.add_node(i, label='i') dict[key2] = i i += 1 for key1 in bg.edges: for key2 in bg.edges[key1]: G.add_edge(dict[key1], dict[key2], label='v') for i in range(n): key = bg.get_node_from_residue_num(i + 1) G.add_edge(i, dict[key], label='abb') for i in range(1, n - 4): j = bps[i] if i < j: if bps[i + 1] == i + 1 and bps[j - 1] == j - 1: key = bg.get_node_from_residue_num(i + 2) if key[0] == 'i' or key[0] == 'h': G.add_edge(i, dict[key], label='abb') G.add_edge(j, dict[key], label='abb') elif bps[i + 1] == i + 1: key = bg.get_node_from_residue_num(i + 2) if key[0] == 'i' or key[0] == 'h': G.add_edge(i, dict[key], label='abb') elif bps[j - 1] == j - 1: key = bg.get_node_from_residue_num(j) if key[0] == 'i' or key[0] == 'h': G.add_edge(j, dict[key], label='abb') if j + 1 < n and bps[i - 1] == i - 1 and bps[j + 1] == j + 1: key = bg.get_node_from_residue_num(i) if key[0] == 'i' or key[0] == 'h': G.add_edge(i, dict[key], label='abb') G.add_edge(j, dict[key], label='abb') elif bps[i - 1] == i - 1: key = bg.get_node_from_residue_num(i) if key[0] == 'i' or key[0] == 'h': G.add_edge(i, dict[key], label='abb') elif j + 1 < n and bps[j + 1] == j + 1: key = bg.get_node_from_residue_num(j + 2) if key[0] == 'i' or key[0] == 'h': G.add_edge(j, dict[key], label='abb') if DEBUG == 1: print(seq) print(stru) display.draw_graph(G, size=15, node_size=1500, font_size=24, node_border=True, size_x_to_y_ratio=3) return G
def draw_bundled_graph(lattice, pos=None, n_iter=1, n_voronoi_iter=2, display_size=10, display_nodes=True, node_size=50, display_node_density=True, node_colormap='bwr', edge_colormap='cividis_r', edge_intensity=True): if pos is None: pos = forceatlas2_networkx_layout( lattice, niter=2000, outboundAttractionDistribution=False, # Dissuade hubs linLogMode=False, # NOT IMPLEMENTED adjustSizes=False, # Prevent overlap (NOT IMPLEMENTED) edgeWeightInfluence=2.0, jitterTolerance=1.0, # Tolerance barnesHutOptimize=False, barnesHutTheta=1.2, scalingRatio=2.0, strongGravityMode=False, gravity=0.01) pos = get_canonical_orientation(pos) points = np.array([pos[u] for u in pos]) N = len(points) for iter in range(n_voronoi_iter): vor = Voronoi(points, qhull_options='Qbb Qc Qx') points = np.vstack([points, vor.vertices]) vor = Voronoi(points, qhull_options='Qbb Qc Qx') M = len(vor.vertices) id2hash = {i: u for i, u in enumerate(pos)} hash2id = {u: i for i, u in enumerate(pos)} points = np.array([pos[u] for u in pos]) all_points = np.vstack([points, vor.vertices]) all_pos = {i: x for i, x in enumerate(all_points)} tri = Delaunay(all_points) G = nx.Graph() G.add_nodes_from(range(N + M)) color = np.zeros(N) for i in range(N): G.nodes[i]['label'] = 0 G.nodes[i]['type'] = 'lattice' G.nodes[i]['pos'] = all_pos[i] u = id2hash[i] color[i] = lattice.nodes[u].get('num_type', 0) for j in range(N, N + M): G.nodes[j]['label'] = 1 G.nodes[j]['type'] = 'waypoint' G.nodes[j]['pos'] = all_pos[j] for simplex in tri.simplices: for i in range(len(simplex) - 1): u = simplex[i] v = simplex[i + 1] dist = np.sqrt( np.sum(np.power(G.nodes[u]['pos'] - G.nodes[v]['pos'], 2))) G.add_edge(u, v, label=0, weight=dist, count=0) G.add_edge(simplex[0], simplex[-1], label=0, weight=dist, count=0) for iter in range(n_iter): for u, v in lattice.edges(): i = hash2id[u] j = hash2id[v] try: path = nx.shortest_path(G, source=i, target=j, weight='weight') for k in range(len(path) - 1): a = path[k] b = path[k + 1] G.edges[a, b]['label'] = 1 G.edges[a, b]['count'] += 1 except Exception: pass # normalize thicknes max_count = max(G.edges[i, j]['count'] for i, j in G.edges()) for i, j in G.edges(): G.edges[i, j]['thickness'] = 4 * G.edges[i, j].get( 'count', 0) / float(max_count) # update weights for i, j in G.edges(): G.edges[i, j]['weight'] = 1.0 / (1 + G.edges[i, j].get('count', 0)) G.edges[i, j]['count'] = 0 args = dict(pos=all_pos, size=None, vertex_size=0, vertex_label=None, edge_label=None, edge_width='thickness', edge_alpha=1, edge_color='thickness', edge_colormap=edge_colormap, colormap=node_colormap) plt.figure(figsize=(1.62 * display_size, display_size)) if edge_intensity: draw_graph(G, **args) else: draw_graph(G, edge_fixed_color='k', **args) x, y = points.T if display_nodes: colors = [lattice.nodes[u]['label'] for u in lattice.nodes()] if len(set(colors)) == 1: colors = 'w' plt.scatter(x, y, s=node_size, c=colors, edgecolors='k', linewidths=0.5, cmap=node_colormap) if display_node_density: plot_pdf2D(points) dx = (max(x) - min(x)) / 20 dy = (max(y) - min(y)) / 20 plt.xlim(min(x) - dx, max(x) + dx) plt.ylim(min(y) - dy, max(y) + dy) plt.show() return pos
def getGraph8(peak, G1, alpha): seq, stru = peak bps = getbps(stru) G = G1.copy() #nx.Graph() pile = [] n = len(seq) next_pos = 0 last_pos = 0 node = n pendingexternal = -1 pendingstem = -1 while (next_pos < n): last_pos = next_pos while (next_pos < n and stru[next_pos] == '.'): next_pos += 1 if next_pos > last_pos: #starts with unpaired rexion G.add_node(node, label='EL') #Combine for i in range(last_pos, next_pos): G.add_edge(i, node, label='s') if pendingexternal != -1: G.add_edge(node, pendingexternal, label='abb') node += 1 pendingstem = node - 1 else: pendingstem = pendingexternal if next_pos == n: break #Start stem i = next_pos j = bps[i] pile.append(i) pile.append(j) pendingexternal = node next_pos = j + 1 while (i < j): while (bps[i + 1] == j - 1): i += 1 j -= 1 pile.append(i) pile.append(j) G.add_node(node, label='S') #Combine for p in range(len(pile)): G.add_edge(pile[p], node, label='s') pile = [] if pendingstem != -1: G.add_edge(pendingstem, node, label='abb') pendingstem = -1 node += 1 if hl(i + 1, j, stru): G.add_node(node, label='HL') G.add_edge(node - 1, node, label='abb') #Combine for p in range(i + 1, j): G.add_edge(p, node, label='s') node += 1 i = j elif stru[i + 1] == '.' and stru[j - 1] == '.': #IL i += 1 init_i = i init_j = j while (stru[i] == '.'): i += 1 j = bps[i] G.add_node(node, label='IL') G.add_edge(node - 1, node, label='abb') #Combine for p in range(init_i, i): G.add_edge(p, node, label='s') for p in range(j + 1, init_j): G.add_edge(p, node, label='s') pendingstem = node node += 1 elif stru[i + 1] == '.': #LB i += 1 init_i = i while (stru[i] == '.'): i += 1 j = bps[i] G.add_node(node, label='LB') G.add_edge(node - 1, node, label='abb') #Combine for p in range(init_i, i): G.add_edge(p, node, label='s') pendingstem = node node += 1 else: #RB init_j = j j -= 1 while (stru[j] == '.'): j -= 1 i = bps[j] G.add_node(node, label='RB') G.add_edge(node - 1, node, label='abb') #Combine for p in range(j + 1, init_j): G.add_edge(p, node, label='s') pendingstem = node node += 1 if DEBUG == 1: print(seq) print(stru) display.draw_graph(G, size=15, node_size=1500, font_size=24, node_border=True, size_x_to_y_ratio=3) return G
def getGraph5(peak, stems, alpha): seq, stru = peak bps = getbps(stru) G = nx.Graph() pile = [] open = 0 close = 0 single = 0 j = 0 opending = -1 cpending = -1 for i in range(len(seq)): #print(i,pile) if stru[i] == ".": if single == 1: G.add_node(j, label=alpha[seq[i]]) G.add_edge(j - 1, j, label='bb') j += 1 else: single = 1 if open > 0: G.add_node(j, label=alpha[seq[i]]) G.add_edge(j - 1, j, label='bb') j += 1 open = 0 elif close > 0: G.add_node(j, label=alpha[seq[i]]) G.add_edge(j - 1, j, label='bb') j += 1 else: #it is the first node G.add_node(j, label=alpha[seq[i]]) j += 1 elif stru[i] == "(": if stems == 1: lb = seq[i] else: lb = 'p' if single == 1: G.add_node(j, label=lb) G.add_edge(j - 1, j, label='bb') open = 1 pile.append((j, open)) j += 1 single = 0 elif open > 0: if bps[i - 1] == bps[i] + 1: G.add_node(j, label=lb) G.add_edge(j - 1, j, label='bb') open += 1 pile.append((j, open)) j += 1 else: #close loop G.add_node(j, label=lb) G.add_edge(j - 1, j, label='bb') j += 1 pile.append((j - 1, open)) open = 1 elif close > 0: G.add_node(j, label=lb) G.add_edge(j - 1, j, label='bb') open = 1 pile.append((j, open)) j += 1 else: #first nt open = 1 G.add_node(j, label=lb) pile.append((j, open)) j += 1 else: #stru[i]==")" if stems == 1: lb = seq[i] else: lb = 'p' if single == 1: G.add_node(j, label=lb) G.add_edge(j - 1, j, label='bb') k, w = pile.pop() G.add_edge(k, j, label='bp') j += 1 close = 1 single = 0 elif close > 0: if bps[i] == bps[i - 1] - 1: close += 1 G.add_node(j, label=lb) G.add_edge(j - 1, j, label='bb') k, w = pile.pop() G.add_edge(k, j, label='bp') j += 1 else: G.add_node(j, label=lb) G.add_edge(j - 1, j, label='bb') k, w = pile.pop() G.add_edge(k, j, label='bp') j += 1 close = 1 else: #first nt close = 1 if DEBUG == 1: print(seq) print(stru) display.draw_graph(G, size=15, node_size=1500, font_size=24, node_border=True, size_x_to_y_ratio=3) return G