def replace_minor_components(graph, pos, scalefactor=1.25): components = list(nx.connected_components(graph)) maj = max(components, key=len) # get viral proteins not in major component vnodes = [n for n in viral if n not in maj] vedges = [] # if more than one viral protein, create an edge between them for comp in components: if len([n for n in comp if n in vnodes]) > 1: from itertools import product tmp_edge = list(product([n for n in comp if n in vnodes])) vedges.append((tmp_edge[0][0], tmp_edge[1][0])) V = nx.Graph() V.add_edges_from(vedges) V.add_nodes_from(vnodes) # spread viral proteins in a circle pvir = nx.circular_layout(V) # nx.draw(V, pos=nx.rescale_layout_dict(pvir,1.25), with_labels=True,alpha=0.5) for comp in components: if comp != maj: centers = [v for v in V.nodes() if v in comp] if len(centers) > 1: pcenter = ( np.average([pvir[n][0] for n in centers]), np.average([pvir[n][1] for n in centers]), ) else: center = centers[0] pcenter = pvir[center] # compute layout for every single component pcomp = nx.rescale_layout_dict( nx.kamada_kawai_layout(graph.subgraph(comp)), len(centers)**2 / len(components), ) for node in comp: # reset position for every node in minor compoents on the basis of the circle created above pos[node] = ( (pcomp[node][0] + pcenter[0]) * scalefactor, (pcomp[node][1] + pcenter[1]) * scalefactor, ) else: # rescale positions also for major component pcenter = ( np.average([pos[n][0] for n in viral]), np.average([pos[n][1] for n in viral]), ) # pcomp=nx.kamada_kawai_layout(graph.subgraph(comp)) pcomp = nx.rescale_layout_dict( graphviz_layout(graph.subgraph(comp), prog="neato", args="-Goverlap=scalexy"), 1, ) for node in comp: pos[node] = ( pcomp[node][0] + pcenter[0], pcomp[node][1] + pcenter[1], ) return pos
def test_rescale_layout_dict(self): G = nx.empty_graph() vpos = nx.random_layout(G, center=(1, 1)) assert nx.rescale_layout_dict(vpos) == {} G = nx.empty_graph(2) vpos = {0: (0.0, 0.0), 1: (1.0, 1.0)} s_vpos = nx.rescale_layout_dict(vpos) assert np.linalg.norm([sum(x) for x in zip(*s_vpos.values())]) < 1e-6 G = nx.empty_graph(3) vpos = {0: (0, 0), 1: (1, 1), 2: (0.5, 0.5)} s_vpos = nx.rescale_layout_dict(vpos) expectation = { 0: np.array((-1, -1)), 1: np.array((1, 1)), 2: np.array((0, 0)), } for k, v in expectation.items(): assert (s_vpos[k] == v).all() s_vpos = nx.rescale_layout_dict(vpos, scale=2) expectation = { 0: np.array((-2, -2)), 1: np.array((2, 2)), 2: np.array((0, 0)), } for k, v in expectation.items(): assert (s_vpos[k] == v).all()
def test_rescale_layout_dict(self): G = nx.empty_graph() vpos = nx.random_layout(G, center=(1, 1)) assert nx.rescale_layout_dict(vpos) == {} G = nx.empty_graph(2) vpos = {0: (0.0, 0.0), 1: (1.0, 1.0)} s_vpos = nx.rescale_layout_dict(vpos) assert np.linalg.norm([sum(x) for x in zip(*s_vpos.values())]) < 1e-6 G = nx.empty_graph(3) vpos = {0: (0, 0), 1: (1, 1), 2: (0.5, 0.5)} s_vpos = nx.rescale_layout_dict(vpos) assert s_vpos == {0: (-1, -1), 1: (1, 1), 2: (0, 0)} s_vpos = nx.rescale_layout_dict(vpos, scale=2) assert s_vpos == {0: (-2, -2), 1: (2, 2), 2: (0, 0)}
def visualize_subgraph(self, edge_index, edge_mask, num_nodes, threshold=None, **kwargs): assert edge_mask.size(0) == edge_index.size(1) if threshold is not None: edge_mask = (edge_mask >= threshold).to(torch.float) print(edge_mask) data = Data(edge_index=edge_index, att=edge_mask).to('cpu') data.num_nodes = num_nodes G = to_networkx(data, edge_attrs=['att']) # kwargs['with_labels'] = kwargs.get('with_labels') or True kwargs['font_size'] = kwargs.get('font_size') or 10 node_size = kwargs.get('node_size') or 800 # kwargs['cmap'] = kwargs.get('cmap') or 'cool' SCALE = 2 pos = nx.rescale_layout_dict(nx.kamada_kawai_layout(G), scale=SCALE) ax = plt.gca() ax.set_xlim((-SCALE - 0.1, SCALE + 0.1)) ax.set_ylim((-SCALE - 0.1, SCALE + 0.1)) for source, target, data in G.to_undirected().edges(data=True): ax.annotate( '', xy=pos[target], xycoords='data', xytext=pos[source], textcoords='data', arrowprops=dict( arrowstyle="-", alpha=max(data['att'], 0.1), shrinkA=sqrt(node_size) / 2.0, shrinkB=sqrt(node_size) / 2.0, connectionstyle="arc3,rad=0.00", )) nx.draw_networkx_labels(G, pos, **kwargs) return ax, G
def virus_host_interactome(self): print("Building Virus Host Interactome ...") from networkx.drawing.nx_agraph import graphviz_layout def replace_minor_components(graph, pos, scalefactor=1.25): components = list(nx.connected_components(graph)) maj = max(components, key=len) # get viral proteins not in major component vnodes = [n for n in viral if n not in maj] vedges = [] # if more than one viral protein, create an edge between them for comp in components: if len([n for n in comp if n in vnodes]) > 1: from itertools import product tmp_edge = list(product([n for n in comp if n in vnodes])) vedges.append((tmp_edge[0][0], tmp_edge[1][0])) V = nx.Graph() V.add_edges_from(vedges) V.add_nodes_from(vnodes) # spread viral proteins in a circle pvir = nx.circular_layout(V) # nx.draw(V, pos=nx.rescale_layout_dict(pvir,1.25), with_labels=True,alpha=0.5) for comp in components: if comp != maj: centers = [v for v in V.nodes() if v in comp] if len(centers) > 1: pcenter = ( np.average([pvir[n][0] for n in centers]), np.average([pvir[n][1] for n in centers]), ) else: center = centers[0] pcenter = pvir[center] # compute layout for every single component pcomp = nx.rescale_layout_dict( nx.kamada_kawai_layout(graph.subgraph(comp)), len(centers)**2 / len(components), ) for node in comp: # reset position for every node in minor compoents on the basis of the circle created above pos[node] = ( (pcomp[node][0] + pcenter[0]) * scalefactor, (pcomp[node][1] + pcenter[1]) * scalefactor, ) else: # rescale positions also for major component pcenter = ( np.average([pos[n][0] for n in viral]), np.average([pos[n][1] for n in viral]), ) # pcomp=nx.kamada_kawai_layout(graph.subgraph(comp)) pcomp = nx.rescale_layout_dict( graphviz_layout(graph.subgraph(comp), prog="neato", args="-Goverlap=scalexy"), 1, ) for node in comp: pos[node] = ( pcomp[node][0] + pcenter[0], pcomp[node][1] + pcenter[1], ) return pos chen_SFB_TAP = pd.read_excel( "data/others/Chen_Interactions.xlsx", sheet_name=0, usecols=["Bait", "Prey"], engine="openpyxl", ) chen_BioID2 = pd.read_excel( "data/others/Chen_Interactions.xlsx", sheet_name=1, usecols=["Bait", "Prey"], engine="openpyxl", ) chen_SFB_TAP = {(row[0], row[1]) for _, row in chen_SFB_TAP.iterrows()} chen_BioID2 = {(row[0], row[1]) for _, row in chen_BioID2.iterrows()} chen = chen_SFB_TAP.union(chen_BioID2) gordon = pd.read_excel( "data/others/Gordon_Interactions.xlsx", header=1, usecols=["Bait", "Preys", "PreyGene"], engine="openpyxl", ) gordon = {( row[0].replace("SARS-CoV2 ", "").replace("orf", "ORF").replace( "nsp", "NSP").replace("Spike", "S").replace("NSP5_C145A", "NSP5"), row[2], ) for _, row in gordon.iterrows()} edges = chen.union(gordon) viral = {e[0] for e in edges} human = {e[1] for e in edges} targeted_genes = human.intersection( set( dict(nx.get_node_attributes(self.__targettarget, "Gene")).values())) drugs = {} for name, node in self.__drugtarget.nodes(data=True): if node["kind"] == "Target" and node.get("Organism") == "Humans": if node.get("Gene") in human and name not in [ "HCG20471, isoform CRA_c", "Glutathione peroxidase", ]: # because they share the same gene name (SIGMAR1 and GPX1) with Sigma non-opioid intracellular receptor 1 and Glutathione Peroxidase 1 ## manually curation needed drugs[node["Gene"]] = list( self.__drugtarget.neighbors(name)) networker_edges = {(source, target) for source, targets in drugs.items() for target in targets} edges = edges.union(networker_edges) drugs = [t for ts in drugs.values() for t in ts] G = nx.from_edgelist(edges) pos = nx.rescale_layout_dict( graphviz_layout(G, prog="neato", args="-Goverlap=scalexy"), 1.1) pos = replace_minor_components(G, pos) nx.set_node_attributes(G, {node: node for node in G.nodes()}, "Gene") nx.set_node_attributes( G, {node: True if node in viral else False for node in G.nodes()}, "Viral") nx.set_node_attributes( G, {node: True if node in human else False for node in G.nodes()}, "Human") nx.set_node_attributes( G, { node: True if node in targeted_genes else False for node in G.nodes() }, "Targeted", ) nx.set_node_attributes( G, {node: True if node in drugs else False for node in G.nodes()}, "Drug") nx.set_node_attributes(G, {node: pos[node] for node in G.nodes()}, "pos") edge_source = {} for edge in edges: source = [] for name, s in { "Chen_SFB_TAP": chen_SFB_TAP, "Chen_BioID2": chen_BioID2, "Gordon": gordon, "COVIDrugNet": networker_edges, }.items(): if edge in s: source.append(name) edge_source[edge] = source nx.set_edge_attributes(G, edge_source, "Source") self.__virusHostInteractome = G self.save_graph( self.added_new_drugs, pd.DataFrame({ "Source": e[0], "Target": e[1] } for e in edges), G, "virus_host_interactome", )
def draw_chain_diff(old_data, new_data, target, format, extension=None): graph, common_edges, old_only_edges, new_only_edges, uid_edges, username_edges, common_names, old_only_names, new_only_names, old_names, new_names = _generate_diff_graph( old_data, new_data, _default_namer, False) fig = plt.figure(figsize=(200, 124)) ax = plt.axes() pos = {} # initialize each component at a different random location components = set( frozenset(c) for c in nx.weakly_connected_components(graph)) dist = dict(nx.shortest_path_length(graph)) for sources in components: for dests in components: if sources is dests: continue for source in sources: for dest in dests: dist[source][ dest] = 10 + len(sources) / 10 + len(dests) / 10 # prevent duplicate distances last_dist = -1 last_keys = [] for source, dists in dist.items(): for dest, distance in sorted(dists.items(), key=lambda k: (k[1], str(k[0]))): if distance != last_dist: count = len(last_keys) interval = 2 / (count + 1) for i, key in enumerate(last_keys): dists[key] = (i + 1) * interval + last_dist - 1 last_keys.clear() last_dist = distance last_keys.append(dest) for component in nx.weakly_connected_components(graph): x = random.triangular() y = random.triangular() for node in component: pos[node] = ((x * 4 + random.triangular()) / 5, (y * 4 + random.triangular()) / 5) pos = nx.kamada_kawai_layout(graph, dist=dist) pos = nx.rescale_layout_dict(pos, 50) nx.draw_networkx_nodes(graph, pos, ax=ax, nodelist=common_names, node_color="tab:blue", node_size=20) nx.draw_networkx_nodes(graph, pos, ax=ax, nodelist=old_only_names, node_color="tab:red", node_size=20) nx.draw_networkx_nodes(graph, pos, ax=ax, nodelist=new_only_names, node_color="tab:green", node_size=20) if target: old_chain = [ old_names[username] for username in make_chain(old_data, target) ] old_chain = [(old_chain[i], old_chain[i + 1]) for i in range(len(old_chain) - 1)] new_chain = [ new_names[username] for username in make_chain(new_data, target) ] new_chain = [(new_chain[i], new_chain[i + 1]) for i in range(len(new_chain) - 1)] nx.draw_networkx_edges(graph, pos, ax=ax, edgelist=new_chain, width=6, alpha=0.1, edge_color=np.linspace(0, 1, len(new_chain)), edge_cmap=plt.get_cmap("cool")) nx.draw_networkx_edges(graph, pos, ax=ax, edgelist=old_chain, width=6, alpha=0.1, edge_color=np.linspace(0, 1, len(old_chain)), edge_cmap=plt.get_cmap("autumn")) nx.draw_networkx_edges(graph, pos, ax=ax, edgelist=common_edges, width=2, alpha=0.5, edge_color="tab:blue") nx.draw_networkx_edges(graph, pos, ax=ax, edgelist=new_only_edges, width=2, alpha=0.5, edge_color="tab:green") nx.draw_networkx_edges(graph, pos, ax=ax, edgelist=old_only_edges, width=2, alpha=0.5, edge_color="tab:red") nx.draw_networkx_edges(graph, pos, ax=ax, edgelist=uid_edges, width=2, alpha=0.5, edge_color="tab:orange") nx.draw_networkx_edges(graph, pos, ax=ax, edgelist=username_edges, width=2, alpha=0.5, edge_color="tab:pink") nx.draw_networkx_labels(graph, pos, ax=ax, font_size=5) data = io.BytesIO() data.name = "chain." + (extension or format) fig.savefig(data, dpi=120, bbox_inches="tight", format=format) data.seek(0) return data
import networkx as nx from building_blocks import * from callbacks import * graph_title = "Target Projection" prefix = graph_title.lower().replace(" ", "_") #"target_projection" print("Loading " + graph_title + " ...") G = nx.read_gpickle("data/graphs/target_projection/target_projection.gpickle") nx.set_node_attributes(G, nx.get_node_attributes(G, "nameID"), "id") try: from networkx.drawing.nx_agraph import graphviz_layout pos = nx.rescale_layout_dict(graphviz_layout(G), len(G.nodes()) * 2.5) except: pos = nx.spring_layout(G, k=1 / (np.sqrt(len(G.nodes()) / 15)), scale=1000, seed=1) nodes = [{ "data": {key: value for key, value in attributes.items()}, "position": { "x": pos[node][0], "y": pos[node][1] } } for node, attributes in dict(G.nodes(data=True)).items()] edges = [{
# Trying to make a graph lol edges_list = [] points = [] random_state = np.random.RandomState(42) graph = nx.fast_gnp_random_graph(35, 0.1, random_state, directed=True) #path = nx.shortest_path(graph,0,12) #print(path) #nx.draw(graph, with_labels=True, node_size = 10, node_color = "purple") for e in list(graph.edges): edges_list.append(e) for j in list(graph.nodes): print(j) pos = nx.nx_agraph.graphviz_layout(graph) pos = nx.rescale_layout_dict(pos, scale=1000) # Position of the node as node atrribute # Testing road class st = street() for i in range(len(edges_list)): new_road = road(edges_list[i], pos, i) st.add_street(new_road) s_means = [] h_means = [] #a_route = nx.shortest_path(graph, "0", "12") #nx.draw(graph, pos,with_labels=True, node_size = 50, node_color = "red", width = 2.0, style = "dashed", label = "500m Road Link") for j in range(200): for i in range(10): speeds = [] headways = []
from building_blocks import * from callbacks import * graph_title="Drug Projection" prefix=graph_title.lower().replace(" ","_")#"drug_projection" print("Loading "+graph_title+" ...") G=nx.read_gpickle("data/graphs/drug_projection/drug_projection.gpickle") nx.set_node_attributes(G,nx.get_node_attributes(G,"nameID"),"id") try: from networkx.drawing.nx_agraph import graphviz_layout pos=nx.rescale_layout_dict(graphviz_layout(G),1000) except: pos=nx.spring_layout(G,k=1/(np.sqrt(len(G.nodes())/15)), scale=1000, seed=1) nodes=[{"data":{key:value for key,value in attributes.items()}, "position":{"x":pos[node][0],"y":pos[node][1]}} for node,attributes in dict(G.nodes(data=True)).items()] edges=[{"data":{"source":source,"target":target}} for source,target in G.edges] edges_to_show=edges.copy() layout=dbc.Col([ dbc.Row([ dbc.Col(sidebar(prefix), width=1, className="bg-light"), dbc.Col([ html.Br(), dbc.Row([ dbc.Col(graph(prefix, title=graph_title, nodes=nodes, edges=edges), xs=12, md=9), dbc.Col(nodes_info(prefix), md=3)