<jupyter_start><jupyter_text>Interact with mutations found in cancers, and explore the drugs that target them---------------Author: <NAME> (<EMAIL>)---------------  Table of Contents1. [Import packages](import)2. [Load drug-gene information](load_drug_gene)3. [Analyze commonly mutated genes](analyze_mutations)4. [Draw the bipartite mutation graph](draw_mutation_graph) Import packages<jupyter_code># import some useful packages
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import networkx as nx
import pandas as pd
import random
import json
import os

# latex rendering of text in graphs
import matplotlib as mpl
mpl.rc('text', usetex = False)
mpl.rc('font', family = 'serif')


% matplotlib inline

# import visJS_2_jupyter 
from visJS2jupyter import visJS_module<jupyter_output><empty_output><jupyter_text>[TOC](toc) Load drug-gene information- Load the drugbank drug-gene file, create a dictionary to map from gene target to drug- Run heat-prop simulation from a set of mutations in a single cluster- What are the nearby druggable genes?<jupyter_code>def load_DB_data(fname):
    '''
    Function to load drug bank data (in format of this file 'drugbank.0.json.new')
    '''

    with open(fname, 'r') as f:
        read_data = f.read()
    f.closed

    si = read_data.find('\'\n{\n\t"source":')
    sf = read_data.find('\ncurl')

    DBdict = dict()

    # fill in DBdict
    while si > 0:
        
        db_temp = json.loads(read_data[si+2:sf-2])
        DBdict[db_temp['drugbank_id']]=db_temp

        # update read_data
        read_data = read_data[sf+10:]
        
        si = read_data.find('\'\n{\n\t"source":')
        sf = read_data.find('\ncurl')
    return DBdict
DBdict = load_DB_data('drugbank.0.json.new')
# make a Series object to map from genes to drugs

drug_list = []
gene_list = []
for drug in DBdict.keys():
    
    gene_targets_temp = [n['name'] for n in DBdict[drug]['node_list']]
    gene_list.extend(gene_targets_temp)
    num_gene_targets = len(gene_targets_temp)
    
    drug_temp = [drug]*num_gene_targets
    drug_list.extend(drug_temp)
    
gene_to_drug = pd.Series(drug_list,index=gene_list)
drug_to_gene = pd.Series(gene_list,index=drug_list)<jupyter_output><empty_output><jupyter_text>[TOC](toc) Get a list of mutations- Many commonly mutated genes are found across lots of diseases.  Are these known oncogenes?- Build a bipartite graph consisting of commonly mutated genes (top 100 in each disease), and disease found in<jupyter_code># list of TCGA cancer types
d_list =  ['ACC','BLCA','BRCA','CESC','CHOL','COAD','COADREAD','DLBC','ESCA','GBM','GBMLGG','HNSC','KICH','KIPAN',
           'KIRC','KIRP','LAML','LGG','LIHC','LUAD','LUSC','OV','PAAD','PCPG','PRAD','READ','SARC','SKCM',
           'STAD','STES','TGCT','THCA','UCEC','UCS','UVM']
len(d_list)
# if you don't have access to TCGA mutation files, just load the pre-computed edge list
num_mutations=25
mutation_EL = pd.read_csv('mutation_EL.csv')
G_mutation = nx.from_pandas_dataframe(mutation_EL,source='gene1',target='gene2',edge_attr = 'weighted_mutation_average')
mutation_EL.head()
# --- UNCOMMENT THIS CELL IF YOU HAVE ACCESS TO TCGA MUTATION_MATRIX.TXT files ----

# mutation_EL = []
# num_mutations = 25
# for d1 in d_list:
#     print(d1)

#     mutation_df = pd.read_csv('/Users/brin/Documents/TCGA_data/expression_files/'+d1+'/mutation_matrix.txt',sep='\t')
#     mutation_df.index=mutation_df['gene_name']
#     index_no_v = [index[:-2] for index in list(mutation_df.index)]
#     mutation_df.index = index_no_v
#     mutation_df['row_average'] = mutation_df.mean(axis=1)
#     mutation_df = mutation_df.sort('row_average',ascending=False)
#     # reorder the columns
#     cols = mutation_df.columns
#     cols_reorder = [cols[0]]
#     cols_reorder.append(cols[-1])
#     cols_reorder.extend(cols[1:-1])
#     mutation_df = mutation_df[cols_reorder]


#     # sort by individual patient mutation list
#     patient_name = 'row_average' #cols[patient_num]
#     mutation_df = mutation_df.sort(patient_name,ascending=False)
#     mutation_df.head()

#     mutation_EL_temp = zip(list(mutation_df.head(num_mutations).index),[d1]*num_mutations,list(mutation_df.head(num_mutations)['row_average']))
#     mutation_EL.extend(mutation_EL_temp)
    
# # make a graph from mutation_EL
# G_mutation = nx.Graph()
# G_mutation.add_weighted_edges_from(mutation_EL)

# mutation_DF = pd.DataFrame(mutation_EL)
# mutation_DF.to_csv('mutation_EL.csv')
# prep graph for visJS_2_jupyter visualization
# add BC and CC as attributes
# create nodes_dict and edges_dict for input to visjs
pos = nx.spring_layout(G_mutation,k=.07)

nodes = list(G_mutation.nodes()) # to make compatible between nx 1.11 and 2.0, must cast to list

numnodes = len(nodes)
edges = list(G_mutation.edges()) # to make compatible between nx 1.11 and 2.0, must cast to list
edges_with_data = list(G_mutation.edges(data=True)) # to make compatible between nx 1.11 and 2.0, must cast to list
numedges = len(edges)

# add a node attributes to color-code by
cc = nx.clustering(G_mutation)
degree = dict(G_mutation.degree()) # to make compatible between nx 1.11 and 2.0, must cast to dict
bc = nx.betweenness_centrality(G_mutation)
nx.set_node_attributes(G_mutation, name = 'clustering_coefficient', values = cc) # must explicitly define arguments
nx.set_node_attributes(G_mutation, name = 'degree', values = degree)             # for compatibility with nx 1.11 and 2.0
nx.set_node_attributes(G_mutation, name = 'betweenness_centrality', values = bc)


# set node_size to degree
node_size = [int(float(n)/np.max(list(degree.values()))*25+1) for n in list(degree.values())]
node_to_nodeSize = dict(zip(degree.keys(),node_size))


# add nodes to highlight (druggable genes)
nodes_HL = [1 if node in gene_to_drug.keys() else 0 for node in G_mutation.nodes()]  
nodes_HL = dict(zip(G_mutation.nodes(),nodes_HL))

nodes_shape=[]
node_shape = ['triangle' if (node in d_list) else 'dot' for node in G_mutation.nodes()]
node_to_nodeShape=dict(zip(G_mutation.nodes(),node_shape))

# add a field for node labels
node_labels_temp = []
list_of_genes = list(np.setdiff1d(G_mutation.nodes(),d_list))
for node in G_mutation.nodes():
    label_temp = node
    
    if node in list_of_genes:
        label_temp+= ': <br/>'
        
        label_temp+='mutation is frequently mutated set in ' + str(nx.degree(G_mutation,node)) + ' out of '+str(len(d_list))+' diseases<br/><br/>'
        label_temp+='Drugs targeting this gene: <br/>'
        
        
        if node in gene_to_drug.keys():
            drugs_temp = gene_to_drug[node]
            if type(drugs_temp) != pd.core.series.Series: # if there is only one drug associated with the gene, fix the format
                drugs_temp = [drugs_temp]
            for d in drugs_temp:
                label_temp+=d + '<br/>'
        else:
            label_temp+='None'
        
    
    node_labels_temp.append(label_temp)

node_labels = dict(zip(G_mutation.nodes(),node_labels_temp))

node_titles = [node for node in G_mutation.nodes()]

node_titles = dict(zip(G_mutation.nodes(),node_titles))<jupyter_output><empty_output><jupyter_text>[TOC](toc) Draw the bipartite mutation graph- Circles are commonly mutated genes- Triangles are the TCGA diseases these mutations are found in<jupyter_code># draw the graph here

scaling_factor=1

node_to_color = visJS_module.return_node_to_color(G_mutation,field_to_map='degree',cmap=mpl.cm.jet,alpha = 1,
                                                  color_vals_transform=None,
                                                 color_max_frac = .9,color_min_frac = .2)

edge_to_color = visJS_module.return_edge_to_color(G_mutation,field_to_map = 'weighted_mutation_average',cmap=mpl.cm.Blues,alpha=.4)

nodes_dict = [{"id":n,"degree":G_mutation.degree(n),"color":node_to_color[n],
              "node_size":node_to_nodeSize[n],'border_width':nodes_HL[n],
              "node_label":node_labels[n],
              "edge_label":'',
              "title":node_labels[n],
              "node_shape":node_to_nodeShape[n],
              "x":pos[n][0]*1000*scaling_factor,
              "y":pos[n][1]*1000*scaling_factor} for n in nodes
              ]


node_map = dict(zip(nodes,range(numnodes)))  # map to indices for source/target in edges

edges_dict = [{"source":node_map[edges[i][0]], "target":node_map[edges[i][1]], 
              "color":edge_to_color[edges[i]],"title":edges_with_data[i][2]['weighted_mutation_average']} for i in range(numedges)]

visJS_module.visjs_network(nodes_dict,edges_dict,
                            node_color_highlight_border="black",
                            node_color_hover_border = 'orange',
                            node_color_border='black',
                            node_size_field='node_size',
                            node_size_transform='Math.sqrt',
                            node_size_multiplier=3*scaling_factor,
                            node_border_width=1*scaling_factor,
                            hover = False,
                            node_label_field='id',
                            edge_width=1*scaling_factor,
                            hover_connected_edges = False,
                            physics_enabled=False,
                            min_velocity=.5,
                            max_velocity=16,
                            draw_threshold=21,
                            min_label_size=12*scaling_factor,
                            max_label_size=25*scaling_factor,
                            max_visible=10*scaling_factor,
                            scaling_factor=scaling_factor,
                            edge_title_field='title',
                            graph_title = 'mutation graph- top '+str(num_mutations)+' mutations in each disease')<jupyter_output><empty_output><jupyter_text>Print out drugs associated with a particular gene<jupyter_code># change focal_gene, to see which drugs have been associated with it, in the DrugBank database
focal_gene = 'MGAM'

print('drugs associated with ' + focal_gene+': ')
if focal_gene in gene_to_drug.keys():
    drugs_temp = gene_to_drug[focal_gene]
    if type(drugs_temp) != pd.core.series.Series: # if there is only one drug associated with the gene, fix the format
        drugs_temp = [drugs_temp]

    
    for d in drugs_temp:
        print(d)
else:
    print('None')<jupyter_output>drugs associated with MGAM: 
DB04878
DB00284
DB00491
{'fraction_non_alphanumeric': 0.11720244929200153, 'fraction_numerical': 0.01081132797550708, 'mean_word_length': 3.285772857728577, 'pattern_counts': {'":': 16, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '213796', 'license': 'MIT', 'n_tokens_mistral': 3609, 'n_tokens_neox': 3407, 'n_words': 749, 'path': 'notebooks/tcga_mutation_example/TCGA_mutation_disease_drug.ipynb', 'repo_name': 'bjodah/visJS2jupyter', 'size': '10473'}