def create_network(pdb_id, database=None, net=None, pdbs_path=None, database_path=None, pathogenic=[], non_pathogenic=[], both=[], colors='mutations', sizes='neighborhood_watch_sharp'): if pdbs_path and database_path: if 'pdb' + pdb_id + '.pdb' in os.listdir(pdbs_path): pdb = os.path.join(pdbs_path, 'pdb' + pdb_id + '.pdb') else: pdb = os.path.join(pdbs_path, 'pdb' + pdb_id + '.ent') mol = bg.Pmolecule(pdb) net = mol.network() database = pd.DataFrame(pd.read_csv(database_path)) node_labels = {} for node in net.nodes: info = database[database['Residue name'] == 'pdb' + pdb_id + node] if len(info) > 1: info = info.iloc[0] # check why more than one type_aa = amino_acids_conversion.three2one( info["Type of residue"].item()) label = type_aa + node[1::] + ":" + node[0] node_labels[node] = label mutation_type = [] neighborhood_watch_sharp = [] neighborhood_watch_smooth = [] degree = [] pairwise_sharp = [] for node in net.nodes: if colors == 'mutations' or sizes == 'mutations': seq_pos = node[1::] if seq_pos in pathogenic: mutation_type.append('tomato') elif seq_pos in non_pathogenic: mutation_type.append('limegreen') elif seq_pos in both: mutation_type.append('gold') else: mutation_type.append('lightgrey') elif colors or sizes: k = nx.degree(net, node) degree.append(k) weight = nx.degree(net, node, weight='weight') if colors == 'neighborhood_watch_sharp': if weight / k < 5: neighborhood_watch_sharp.append('blue') elif weight / k < 10: neighborhood_watch_sharp.append('cyan') elif weight / k < 15: neighborhood_watch_sharp.append('greenyellow') elif weight / k < 20: neighborhood_watch_sharp.append('yellow') elif weight / k < 25: neighborhood_watch_sharp.append('orange') else: neighborhood_watch_sharp.append('red') elif sizes == 'neighborhood_watch_sharp': if weight / k < 5: neighborhood_watch_sharp.append(1000) elif weight / k < 10: neighborhood_watch_sharp.append(4000) elif weight / k < 15: neighborhood_watch_sharp.append(7000) elif weight / k < 20: neighborhood_watch_sharp.append(10000) elif weight / k < 25: neighborhood_watch_sharp.append(13000) else: neighborhood_watch_sharp.append(16000) elif colors == 'neighborhood_watch_smooth' or sizes == 'neighborhood_watch_smooth': neighborhood_watch_smooth.append(weight / k) if colors == 'pairwise_sharp' or sizes == 'pairwise_sharp': for u, v in net.edges: wij = net.get_edge_data(u, v)['weight'] if wij < 10: pairwise_sharp.append('blue') elif wij < 20: pairwise_sharp.append('cyan') elif wij < 30: pairwise_sharp.append('greenyellow') elif wij < 40: pairwise_sharp.append('yellow') elif wij < 50: pairwise_sharp.append('orange') else: pairwise_sharp.append('red') color_map = [] edge_color_map = [] if colors == 'mutations': color_map = mutation_type elif colors == 'degree': color_map = degree elif colors == 'neighborhood_watch_sharp': color_map = neighborhood_watch_sharp elif colors == 'neighborhood_watch_smooth': color_map = neighborhood_watch_smooth elif colors == 'pairwise_sharp': edge_color_map = pairwise_sharp size_map = [] # edge_size_map = [] if sizes == 'mutations': size_map = mutation_type elif sizes == 'degree': size_map = degree elif sizes == 'neighborhood_watch_sharp': size_map = neighborhood_watch_sharp elif sizes == 'neighborhood_watch_smooth': size_map = neighborhood_watch_smooth # elif sizes == 'pairwise_sharp': edge_size_map = pairwise_sharp return net, node_labels, size_map, color_map, edge_color_map
def create_perturbation_network(net1, net2, db1, db2): # check which edges are only in net1 or only in net2 and which are in common: edges1 = set(net1.edges) edges2 = set(net2.edges) common_edges = edges1.intersection(edges2) edges_1only = edges1.difference(edges2) edges_2only = edges2.difference(edges1) net = nx.Graph() edge_colors = {} size_map = [] node_borders = [] color_map = [] node_labels = {} for u, v in common_edges: wij1 = net1.get_edge_data(u, v)['weight'] wij2 = net2.get_edge_data(u, v)['weight'] deltawij = wij2 - wij1 if deltawij > 0: color = 'green' net.add_edge(u, v, weight=abs(deltawij)) edge_colors[(u, v)] = color elif deltawij < 0: color = 'red' net.add_edge(u, v, weight=abs(deltawij)) edge_colors[(u, v)] = color for u, v in edges_1only: wij = net1.get_edge_data(u, v)['weight'] color = 'red' net.add_edge(u, v, weight=wij) edge_colors[(u, v)] = color for u, v in edges_2only: wij = net2.get_edge_data(u, v)['weight'] color = 'green' net.add_edge(u, v, weight=wij) edge_colors[(u, v)] = color for node in net.nodes: k1 = net1.degree(node) k2 = net2.degree(node) w1 = net1.degree(node, weight='weight') w2 = net2.degree(node, weight='weight') if isinstance(k1, int) and isinstance(k2, int): nw1 = w1 / k1 nw2 = w2 / k2 info1 = db1[db1['Position'] == node] if len(info1) > 1: info1 = info1.iloc[0] # check why more than one type_aa_1 = amino_acids_conversion.three2one( info1["Type of residue"].item()) info2 = db2[db2['Position'] == node] if len(info2) > 1: info2 = info2.iloc[0] # check why more than one type_aa_2 = amino_acids_conversion.three2one( info2["Type of residue"].item()) if type_aa_1 == type_aa_2: color_map.append('gray') label = type_aa_1 + node[1::] + ":" + node[0] node_labels[node] = label else: color_map.append('blue') label = type_aa_1 + '-' + type_aa_2 + node[1::] + ":" + node[0] node_labels[node] = label elif isinstance(k2, int): nw1 = 0 nw2 = w2 / k2 color_map.append('green') info2 = db2[db2['Position'] == node] if len(info2) > 1: info2 = info2.iloc[0] # check why more than one type_aa_2 = amino_acids_conversion.three2one( info2["Type of residue"].item()) label = type_aa_2 + node[1::] + ":" + node[0] node_labels[node] = label else: nw1 = w1 / k1 nw2 = 0 color_map.append('red') info1 = db1[db1['Position'] == node] if len(info1) > 1: info1 = info1.iloc[0] # check why more than one type_aa_1 = amino_acids_conversion.three2one( info1["Type of residue"].item()) label = type_aa_1 + node[1::] + ":" + node[0] node_labels[node] = label deltanw = nw2 - nw1 size_map.append(abs(deltanw) * 100 + 500) if deltanw > 0: node_borders.append('green') elif deltanw < 0: node_borders.append('red') else: node_borders.append('gray') # # node_labels = {node: node for node in net.nodes} #TO DO: labels as in normal networks, but with mutations edge_color_map = [color for color in edge_colors.values()] return net, node_labels, size_map, color_map, edge_color_map, node_borders
def create_perturbation_network(net1, net2, db1, db2, threshold=4): # check which edges are only in net1 or only in net2 and which are in common: edges1 = set(net1.edges) edges2 = set(net2.edges) common_edges = edges1.intersection(edges2) edges_1only = edges1.difference(edges2) edges_2only = edges2.difference(edges1) net = nx.Graph() edge_colors = {} size_map = [] node_borders = [] color_map = [] node_labels = {} added_edges = [] for u, v in common_edges: wij1 = net1.get_edge_data(u, v)['weight'] wij2 = net2.get_edge_data(u, v)['weight'] deltawij = wij2 - wij1 if deltawij > threshold: color = 'green' net.add_edge(u, v, weight=abs(deltawij)) edge_colors[(u, v)] = color added_edges.append((u, v)) elif deltawij < -threshold: color = 'red' net.add_edge(u, v, weight=abs(deltawij)) edge_colors[(u, v)] = color added_edges.append((u, v)) for u, v in edges_1only: wij = net1.get_edge_data(u, v)['weight'] color = 'red' if wij > threshold: net.add_edge(u, v, weight=wij) edge_colors[(u, v)] = color added_edges.append((u, v)) for u, v in edges_2only: wij = net2.get_edge_data(u, v)['weight'] color = 'green' if wij > threshold: net.add_edge(u, v, weight=wij) edge_colors[(u, v)] = color added_edges.append((u, v)) sec_str = {} for node in net.nodes: k1 = net1.degree(node) k2 = net2.degree(node) w1 = net1.degree(node, weight='weight') w2 = net2.degree(node, weight='weight') if isinstance(k1, int) and isinstance(k2, int): nw1 = w1 / k1 nw2 = w2 / k2 info1 = db1[db1['Position'] == node] if len(info1) > 1: info1 = info1.iloc[0] # check why more than one type_aa_1 = amino_acids_conversion.three2one(info1["Type of residue"].item()) sec_str_1 = info1["Secondary structure"].item() chain1 = info1["Position"].item()[0] pos1 = info1["Sequence position"].item() info2 = db2[db2['Position'] == node] if len(info2) > 1: info2 = info2.iloc[0] # check why more than one type_aa_2 = amino_acids_conversion.three2one(info2["Type of residue"].item()) sec_str_2 = info2["Secondary structure"].item() chain2 = info2["Position"].item()[0] pos2 = info2["Sequence position"].item() if type_aa_1 == type_aa_2: color_map.append('gray') label = type_aa_1 + node[1::] + ":" + node[0] node_labels[node] = label else: color_map.append('blue') label = type_aa_1 + '-' + type_aa_2 + node[1::] + ":" + node[0] node_labels[node] = label elif isinstance(k2, int): nw1 = 0 nw2 = w2 / k2 color_map.append('green') info2 = db2[db2['Position'] == node] if len(info2) > 1: info2 = info2.iloc[0] # check why more than one type_aa_2 = amino_acids_conversion.three2one(info2["Type of residue"].item()) label = type_aa_2 + node[1::] + ":" + node[0] node_labels[node] = label sec_str_2 = info2["Secondary structure"].item() chain2 = info2["Position"].item()[0] pos2 = info2["Sequence position"].item() sec_str_1 = None chain1 = None pos1 = None else: nw1 = w1 / k1 nw2 = 0 color_map.append('red') info1 = db1[db1['Position'] == node] if len(info1) > 1: info1 = info1.iloc[0] # check why more than one type_aa_1 = amino_acids_conversion.three2one(info1["Type of residue"].item()) label = type_aa_1 + node[1::] + ":" + node[0] node_labels[node] = label sec_str_1 = info1["Secondary structure"].item() chain1 = info1["Position"].item()[0] pos1 = info1["Sequence position"].item() sec_str_2 = None chain2 = None pos2 = None deltanw = nw2 - nw1 size_map.append(abs(deltanw)* 100 + 500) if deltanw > 0: node_borders.append('green') elif deltanw < 0: node_borders.append('red') else: node_borders.append('gray') if chain1 == chain2 and sec_str_1[0] == sec_str_2[0]: sec_str[node] = (chain1, pos1, sec_str_1) else: sec_str[node] = ('extra', 'extra', 'extra') w1D = [] w2D = [] w3D = [] w4D = [] relations = {} for u, v in net.edges: chain_u, pos_u, sec_str_u = sec_str[u] chain_v, pos_v, sec_str_v = sec_str[v] wij = net.get_edge_data(u, v)['weight'] if chain_u == 'extra' or chain_v == 'extra': relation = 'extra' elif chain_u != chain_v: relation = '4D' w4D.append(wij) elif sec_str_u != sec_str_v: relation = '3D' w3D.append(wij) elif abs(pos2 - pos1) != 1: relation = '2D' w2D.append(wij) else: relation = '1D' w1D.append(wij) relations[(u, v)] = relation # # node_labels = {node: node for node in net.nodes} #TO DO: labels as in normal networks, but with mutations # edge_color_map = [edge_colors[edge] for edge in added_edges] return net, node_labels, size_map, color_map, edge_colors, node_borders, w1D, w2D, w3D, w4D, relations