NETWORK BIOLOGY

Assignment 1

Submitted By:

Divyanshu Srivastava (MT16125)

In [5]:
# Import dependencies
import numpy as np
import pickle
import networkx as nx
import matplotlib.pyplot as plt

Qeustion 1


In [ ]:
## Undirected Unweighted Graph
G1 = nx.Graph()
G1.add_edges_from([(1, 2), (1, 3), (3, 4), (5, 6), (7, 8), (2, 5), (8, 3), (5, 8)])
plt.clf()

pos=nx.spring_layout(G1) # positions for all nodes
nx.draw(G1, pos)
nx.draw_networkx_labels(G1, pos)
plt.title('(a) Undirected Unwieghted Graph')
plt.show()

In [ ]:
## Undirected Weighted Graph
G2 = nx.MultiGraph()
G2.add_weighted_edges_from([(1, 2, 0.1), (1, 3, 0.7), (3, 4, 1.0), (5, 6, 0.4), 
                            (7, 8, 0.3), (2, 5, 0.3), (8, 3, 1.0), (5, 8, 0.5)])
pos=nx.spring_layout(G2) # positions for all nodes
nx.draw(G2, pos)
edge_labels=dict([((u,v,),d['weight'])
             for u,v,d in G2.edges(data=True)])
nx.draw_networkx_edge_labels(G2,pos,edge_labels=edge_labels)
nx.draw_networkx_labels(G2, pos)
plt.title('(b) Undirected Wieghted Graph')
plt.show()

In [ ]:
## Directed Unweighted Graph
G3 = nx.DiGraph()
G3.add_edges_from([(1, 2), (1, 3), (3, 4), (5, 6), (7, 8), (2, 5), (8, 3), (5, 8)])
pos=nx.spring_layout(G3) # positions for all nodes
nx.draw(G3, pos)
nx.draw_networkx_labels(G3, pos)
plt.title('(c) Directed Unweighted Graph')
plt.show()

In [ ]:
## Directed Weighted Graph
G4 = nx.MultiDiGraph()
G4.add_weighted_edges_from([(1, 2, 0.1), (1, 3, 0.7), (3, 4, 1.0), (5, 6, 0.4), 
                            (7, 8, 0.3), (2, 5, 0.3), (8, 3, 1.0), (5, 8, 0.5)])
pos=nx.spring_layout(G4) # positions for all nodes
nx.draw(G4, pos)
edge_labels=dict([((u,v,),d['weight'])
             for u,v,d in G4.edges(data=True)])
nx.draw_networkx_edge_labels(G4,pos,edge_labels=edge_labels)
nx.draw_networkx_labels(G4, pos)
plt.title('(d) Directed Wieghted Graph')
plt.show()

In [ ]:
## Storing these graphs as adjacency matrix and adjacency list
G = {'G1 (UD-UW)': G1, 'G2 (UD-W)': G2,'G3 (D-UW)': G3,'G4 (D-W)': G4}

adjacency_matrix = dict()
edgelist = dict()
for key in G:
    adjacency_matrix[key] = nx.to_numpy_matrix(G[key])
    edgelist[key] = nx.to_edgelist(G[key])

# saving
with open('ques1_stired_networks.pickle', 'w') as f:  
    pickle.dump([adjacency_matrix, edgelist], f)

Qeustion 2


In [99]:
## Fuction to find degree from adjacency matrix

# function definition
def print_degree(adj):
    """
        This function computes the degree of each node for a given adjacency matrix
        For Uniderected Graphs:
            It prints node wise degree and returns it.
        For Directed Graphs:
            It prnts both in degree and out degree and reurns both of them.
    """
    if np.allclose(adj, adj.T, atol=1e-8):
        print "Undirected Graph"
        deg = np.sum(adj != 0, axis=0)
        print "Degree of each node: " + np.str(deg)
    else:
        print "Directed Graph"
        deg = list()
        deg.append(np.sum(adj != 0, axis=0).astype(np.int))
        deg.append(np.sum(adj != 0, axis=1).astype(np.int).T)
        print "In Degree of each node: " + np.str(deg[0])
        print "Out Degree of each node: " + np.str(deg[1])
    return deg

# loading adjacency matrix which were saved in question 1
f = open('ques1_stired_networks.pickle', 'r')
adj_matrix, edge_list = pickle.load(f)

for key in adj_matrix:
    print key
    print_degree(adj_matrix[key])


G4 (D-W)
Directed Graph
In Degree of each node: [[0 1 2 1 1 1 0 2]]
Out Degree of each node: [[2 1 1 0 2 0 1 1]]
G1 (UD-UW)
Undirected Graph
Degree of each node: [[2 2 3 1 3 1 1 3]]
G2 (UD-W)
Undirected Graph
Degree of each node: [[2 2 3 1 3 1 1 3]]
G3 (D-UW)
Directed Graph
In Degree of each node: [[0 1 2 1 1 1 0 2]]
Out Degree of each node: [[2 1 1 0 2 0 1 1]]

Question 3


In [ ]:
# Computing A square for Undirected nnweighted graph G1
A = adjacency_matrix['G1 (UD-UW)']
A = A.astype(np.int)
A_square = A*A

pos=nx.spring_layout(G1) # positions for all nodes
nx.draw(G1, pos)
nx.draw_networkx_labels(G1, pos)
plt.title('Undirected Unwieghted Graph')
plt.show()
print "A_square : \n " + np.str(A_square)

Clearly, $A^2$ as computed above for the undirected unweighted graph G1 (question 1) encodes the information of the number of paths of length 2 from each node to every other node. For Example, Cosider the pair of nodes 1 and 4. There exists a path of length 2 from node 1 to node 4 via node 3, thereby giving $A^2_{(1, 4)} = 1$ . Similarly, there exists three paths of length 2 from node 8 back to node 8, via node 3, node 5 and node 7. Thus we have $A^2_{(8, 8)} = 3$. In general, $A^n_{(i, j)}$ gives the number of paths of length $n$ from node $i$ to node $j$ in the undirected unweighted graph.

Question 4


In [94]:
## Reading C. Elegans neural network dataset
G_celegans = nx.read_gml('Newman_Datasets/celegansneural/celegansneural.gml')
G_celegans_adj = nx.to_numpy_matrix(G_celegans)
G_celegans_deg = print_degree(G_celegans_adj) # function written in question 2

# Plotting
plt.hist(np.asarray(G_celegans_deg[0])[0], bins='auto')
plt.title("In Degree Distribution for C. Elegans Neural Network")
plt.xlabel("Degree")
plt.ylabel("Frequency")
plt.show()

plt.hist(np.asarray(G_celegans_deg[1])[0], bins='auto')
plt.title("Out Degree Distribution for C. Elegans Neural Network")
plt.xlabel("Degree")
plt.ylabel("Frequency")
plt.show()


Directed Graph
In Degree of each node: [[  2  24  41  33  35   8  27  14  10   8   9   7  45  26  17   4   3   6
    4   6   9  13  14  19   5   5   2   5   4   6   3   8   5  13   5  16
    7   5   3  10   5   6  10  15 134   5  14  18  20  13   6   9   7   1
    0   5   3   0   7  13   5   3   1   5  11   5   3   6   4   2   1   7
   11  17  18  13   2  10   6   4   1   2   2   2  36  11  36   7   5   8
    7   2   1   7   3   6   4  14  19   5   3   6  12  11   7   8   7   7
   11   6   4   7   7  12  25   9   3  26  31  20   4   3  11  11   8  25
    3   2   3   6  18   8  15   5   0   6   1  22   6   4   3   9  16   0
    2  12   2   3   8   2   1  12   7   6   7   9   7   9   5   9  10   9
    9  10   6   5  11  10   8   6   6   6  26  11   5   5   7  13  10   8
    7   6   9   4   4   8   6  16   3  17  31  11   5  10   5   8   4  14
    8  11  15   6  12   5   8   8   3   6   1   5   6   5   6  11   7   4
    4   4   9  16   4   8   1   1   3   4   7  10   2   2   0  15   4  15
   10   2   2   2   7   1   1   1   0   0   4   4   3   1   1   7   1   0
    5   4   3   4   5   3   1   0   0   0   2   2   2   2   2   2   8  11
    1  10   1   9   1  10  12   8   0   1   0   0   1   2   0   0   0   0
    0   0   0   0   0   0   0   0   0]]
Out Degree of each node: [[ 9 10 39 21 21  7 17  6 12  9  7  5 38 14  9 12 17  5 15  7 10  8  1  2
   2  1  5 15  2  5 10 17  8  1  9  2  3  1  6  0  5  9 18  4  0  3  3  7
   6  1 11  1 11 11  8  3  5  9 18 24  3  6 12 24  2  7 11 18  5 12 10 18
   7  4  3  6  6  7  1  1  1  1  7  8 18  4 18 15  1  4 22 11  4 18  1 14
   3  4 15  6  7 11 13 11  4 13  8  6 10 13  7 18  4 10 10 11 12 27 27 15
   6 16  8 15  7 34  2  8  2 12 17  5 14 13  8  9  8 30  7  9  4  4 28  5
   2 11  3  4 11 17 13  3  4  6  3  4  3  4  4  3  5  5  6  5  5  5  5  4
   8  9  8  8 34  1  1  3  5 12  5  6  4  5  3  2  9  8  1 12  5  3  0 23
   9 13  6 19  5  3 15 12  3 16 12 16 26 13 12 25  7 11  5  6  4 13  7  8
  10  7 13  4  7  2  2  2 15 11 22  1 11  7  8  1  8  4  1  3  5  7  5  9
   6  8  1  1  7 19  5  5  5  1  4  2  1  8  3  6  7  8  5  7  1  6  2  3
   2  3  2  3  1  1  3  1  4  1  3  4  4  4  8  3  3  2  5  7 16  1  1  1
   1  1  1  1  1  1  1  1  1]]

In [95]:
## Reading C. Elegans neural network dataset
G_adjnoun = nx.read_gml('Newman_Datasets/adjnoun/adjnoun.gml')
G_adjnoun_adj = nx.to_numpy_matrix(G_adjnoun)
G_adjnoun_deg = print_degree(G_adjnoun_adj) # function written in question 2

# Plotting
plt.hist(np.asarray(G_adjnoun_deg[0])[0], bins='auto')
plt.title("In Degree Distribution for C. Elegans Neural Network")
plt.xlabel("Degree")
plt.ylabel("Frequency")
plt.show()


Undirected Graph
Degree of each node: [[ 3 14 33  9  2  7  6 11  1 17  4  7 10  6 12 12  3 49 14  9  5 10  5  8
  15 14 11 15 13  5  6 12 10  5  7  6  7 10  9  5  4  8  7 28  7  6  3  5
  10  5 15 28  7  6 13  3  4  3  2 13  5  2  2  5  1  6  7  4 12  2 12  6
   7  7  1 10 12  3  3 13  8  6  1  5  6  2  2 10  6  7  1  1  6  2  3  1
   3  1  5  2  3  2 10  9 21  7  6  2  4  1  2  1]]

Question 5


In [98]:
## Random Undirected Unweighted Graph

def create_random_graph(n, e):
    """
        This function creates a random adjacncy matrix for a simple 
        undirected unweighted graph with n nodes and e edges.
        It returns the randomly computed adjacency matrix.
    """
    adj = np.zeros((nodes, nodes))
    edges = 0;
    while edges < e:
        i = np.random.randint(0, n-2)
        j = np.random.randint(1, n-1)
        if (adj[i][j] == 1):
            continue
        adj[i][j] = 1
        edges = edges+1

In [97]:
?np.zeros