<jupyter_start><jupyter_text>Simple implementation of network propagation from Vanunu et. al. Author: <NAME> (<EMAIL>) March 24, 2016Note: data and code for this notebook may be found in the 'data' and 'source' directories<jupyter_code># import some useful packages
import numpy as np
import matplotlib.pyplot as plt
import seaborn
import networkx as nx
import pandas as pd
import random

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

import sys
#sys.path.append('/Users/brin/Google Drive/UCSD/genome_interpreter_docs/barabasi_disease_distances/barabasi_incomplete_interactome/source/')
sys.path.append('source/')
import separation
import plotting_results
import network_prop

import imp
imp.reload(separation)
imp.reload(plotting_results)
imp.reload(network_prop)


% matplotlib inline<jupyter_output><empty_output><jupyter_text>Load the interactome from Barabasi paperInteractome downloaded from supplemental materials of http://science.sciencemag.org/content/347/6224/1257601 (Menche, Jörg, et al. "Uncovering disease-disease relationships through the incomplete interactome." Science 347.6224 (2015): 1257601.)- We need a reliable background interactome in order to correctly calculate localization and co-localization properties of node sets- We have a few choices in this decision, three of which are outlined below:<jupyter_code># load the interactome network (use their default network)
Gint = separation.read_network('data/DataS1_interactome.tsv')
# remove self links
separation.remove_self_links(Gint)
# Get rid of nodes with no edges
nodes_degree = Gint.degree()
nodes_0 = [n for n in nodes_degree.keys() if nodes_degree[n]==0]
Gint.remove_nodes_from(nodes_0)<jupyter_output>> done loading network:
> network contains 13460 nodes and 141296 links<jupyter_text>Load a focal gene setGene lists should follow the format shown here for kidney_diseases.txt and epilepsy_genes.txt, and should be in the entrez ID format<jupyter_code>genes_KID = separation.read_gene_list('kidney_diseases.txt')
genes_EPI = separation.read_gene_list('epilepsy_genes.txt')


# set disease name and focal genes here
dname = 'kidney'
genes_focal = genes_KID<jupyter_output>> done reading genes:
> 110 genes found in kidney_diseases.txt

> done reading genes:
> 32 genes found in epilepsy_genes.txt<jupyter_text>Network propagation from seed nodes First calculate the degree-corrected version of adjacency matrixNetwork propagation simulation follows methods in http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1000641 (Vanunu, Oron, et al. "Associating genes and protein complexes with disease via network propagation." PLoS Comput Biol 6.1 (2010): e1000641.)  Calculate the degree- normalized adjacency matrix, using network_prop.normalized_adj_matrix function<jupyter_code>Wprime= network_prop.normalized_adj_matrix(Gint)<jupyter_output><empty_output><jupyter_text>Propagate heat from seed disease, examine the community structure of hottest nodes<jupyter_code>seed_nodes = list(np.intersect1d(list(genes_focal),Gint.nodes()))
alpha=.5  # this parameter controls how fast the heat dissipates
Fnew = network_prop.network_propagation(Gint,Wprime,seed_nodes,alpha=alpha,num_its=20)<jupyter_output><empty_output><jupyter_text>Plot the hot subnetwork1. Create subgraphs from interactome containing only disease genes2. Sort the heat vector (Fnew), and select the top_N hottest genes to plot<jupyter_code>Fsort = Fnew.sort(ascending=False)
top_N = 200
F_top_N = Fnew.head(top_N)
gneigh_top_N = list(F_top_N.index)
G_neigh_N = Gint.subgraph(gneigh_top_N)
# pull out some useful subgraphs for use in plotting functions
# find genes which are neighbors of seed genes
genes_in_graph = list(np.intersect1d(Gint.nodes(),list(genes_focal)))
G_focal=G_neigh_N.subgraph(list(genes_in_graph))<jupyter_output>/root/anaconda3/envs/py27/lib/python2.7/site-packages/ipykernel/__main__.py:1: FutureWarning: sort is deprecated, use sort_values(inplace=True) for for INPLACE sorting
  if __name__ == '__main__':<jupyter_text>Set the node positions using nx.spring_layout.  Parameter k controls default spacing between nodes (lower k brings the nodes closer together, higher k pushes them apart)<jupyter_code>pos = nx.spring_layout(G_neigh_N,k=.03)  # set the node positions
plotting_results.plot_network_2_diseases(G_neigh_N,pos,G_focal,d1name=dname,saveflag=False)
nx.draw_networkx_nodes(G_neigh_N,pos=pos,node_color=Fnew[G_neigh_N.nodes()],cmap='YlOrRd',node_size=30,
                      vmin=0,vmax=max(Fnew)/3)
nx.draw_networkx_edges(G_neigh_N,pos=pos,edge_color='white',alpha=.2)
plt.title('Top '+str(top_N)+' genes propagated from '+dname+': alpha = ' + str(alpha),color='white',fontsize=16,y=.95)
plt.savefig('heat_prop_network.png',dpi=200)  # save the figure here<jupyter_output><empty_output><jupyter_text>What are the top N genes?  Print out gene symbols - These are the genes which are likely to be related to input gene set(Convert from entrez ID to gene symbol using MyGene.info)<jupyter_code>import mygene
mg = mygene.MyGeneInfo()
# print out the names of the top N genes (that don't include the seed set)
focal_group = list(F_top_N.index)
focal_group = np.setdiff1d(focal_group,list(genes_focal))
top_heat_focal = F_top_N[focal_group]
focal_temp = mg.getgenes(focal_group)
focal_entrez_names = [str(x['entrezgene']) for x in focal_temp if 'symbol' in x.keys()]
focal_gene_names = [str(x['symbol']) for x in focal_temp if 'symbol' in x.keys()]
top_heat_df = pd.DataFrame({'gene_symbol':focal_gene_names,'heat':top_heat_focal[focal_entrez_names]})
top_heat_df = top_heat_df.sort('heat',ascending=False)
# print the top 25 related genes, along with their heat values
top_heat_df.head(25)<jupyter_output>querying 1-103...done.
{'fraction_non_alphanumeric': 0.09217780840799449, 'fraction_numerical': 0.021536871123363197, 'mean_word_length': 6.9411764705882355, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '543675', 'license': 'MIT', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1896, 'n_words': 519, 'path': 'notebooks/networkAnalysis/network_propagation_example/propagation_example.ipynb', 'repo_name': 'AspirinCode/jupyter-genomics', 'size': '5825'}