<jupyter_start><jupyter_text>PPI network analysis<jupyter_code>#%matplotlib notebook
%matplotlib inline
import sys
import csv

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import scipy.sparse as ss
import scipy.stats as st

from signet.cluster import Cluster
import signet.utils as ut
import matplotlib
from mpl_toolkits.axes_grid1 import AxesGrid

def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'):
    '''
    Function to offset the "center" of a colormap. Useful for
    data with a negative min and positive max and you want the
    middle of the colormap's dynamic range to be at zero.

    Input
    -----
      cmap : The matplotlib colormap to be altered
      start : Offset from lowest point in the colormap's range.
          Defaults to 0.0 (no lower offset). Should be between
          0.0 and `midpoint`.
      midpoint : The new center of the colormap. Defaults to 
          0.5 (no shift). Should be between 0.0 and 1.0. In
          general, this should be  1 - vmax / (vmax + abs(vmin))
          For example if your data range from -15.0 to +5.0 and
          you want the center of the colormap at 0.0, `midpoint`
          should be set to  1 - 5/(5 + 15)) or 0.75
      stop : Offset from highest point in the colormap's range.
          Defaults to 1.0 (no upper offset). Should be between
          `midpoint` and 1.0.
    '''
    cdict = {
        'red': [],
        'green': [],
        'blue': [],
        'alpha': []
    }

    # regular index to compute the colors
    reg_index = np.linspace(start, stop, 257)

    # shifted index to match the data
    shift_index = np.hstack([
        np.linspace(0.0, midpoint, 128, endpoint=False), 
        np.linspace(midpoint, 1.0, 129, endpoint=True)
    ])

    for ri, si in zip(reg_index, shift_index):
        r, g, b, a = cmap(ri)

        cdict['red'].append((si, r, r))
        cdict['green'].append((si, g, g))
        cdict['blue'].append((si, b, b))
        cdict['alpha'].append((si, a, a))

    newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict)
    plt.register_cmap(cmap=newcmap)

    return newcmap

# shifted cmap (useful to plot correlation matrices)
s_cmap = shiftedColorMap(plt.cm.seismic, start= -4., midpoint=0., stop=2., name='shiftedcmap')
#s_cmap = shiftedColorMap(plt.cm.seismic,  midpoint=0., name='shiftedcmap')<jupyter_output><empty_output><jupyter_text>Import data<jupyter_code>import pandas
# numpy arrays, days and prices
data = pandas.read_csv('PPI.csv')
dir(data)
edges = data['Sign_Score']
data_ID1 = data['ID1']
data_ID2 = data['ID2']
#data['Sign_Score']
G = nx.Graph()

for i in np.arange(edges.shape[0]):

    node1, node2, weight = data_ID1[i], data_ID2[i], edges[i]

    G.add_edges_from([(node1, node2, {'weight': weight})])



G=G.subgraph(max(nx.connected_components(G), key=len))
A = nx.adj_matrix(G, weight = 'weight').tocsc()

Abar = abs(A)

A_p = (A + Abar)/2
A_n = -(A - Abar)/2
A_p.eliminate_zeros()
A_n.eliminate_zeros()
# ss.save_npz('adjacency_plus.npz', A_p)
# ss.save_npz('adjacency_minus.npz', A_n)
Ad = A.todense()<jupyter_output><empty_output><jupyter_text>Analyse Adjacency<jupyter_code>plt.hist(edges, 20);
edges[edges>0].shape[0]*2
edges[edges<0].shape
A_p.data.shape
s_cmap = shiftedColorMap(plt.cm.seismic, start= -2.5, midpoint=0.2335, stop=2.5, name='shiftedcmap')
plt.figure();
#corrs[np.diag_indices(corrs.shape[0])]=0
plt.matshow(Ad, cmap = s_cmap);#
plt.colorbar();
# plt.savefig('A.png', dpi = 250)<jupyter_output><empty_output>