-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewkl.py
More file actions
108 lines (79 loc) · 3.29 KB
/
newkl.py
File metadata and controls
108 lines (79 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from petsc4py import PETSc as Pet
import networkx as nx
import numpy as np
import scipy
import timeit
#import matplotlib.pylab as plt
import kl_connected_subgraph as kl
G = nx.read_edgelist('newmetabolic.txt',nodetype=int)
start_time = timeit.default_timer()
P = G.copy()
counter = 0
for edge in P.edges():
#counter+=1
#print counter
(u,v) = edge #get edge
paths = [[u,v]]
cnt = 1 #accounts for direct path
uneighbors = list(nx.all_neighbors(P,u)) #get list of neighbors of u
uneighbors.remove(v) # remove v because we already accounted for it
for neighbor1 in uneighbors: # loop through neighbors of u
u1neighbors = list(nx.all_neighbors(P,neighbor1)) #find list of neighbors of each neighbor of u
#print u1neighbors
#print ""
if v in u1neighbors:
#cnt += 1 #if v in this list then there is a path of length 2 from u to v
temppath = [u,neighbor1,v]
found = False
for j in range(1,len(temppath)):
for path in paths:
for i in range(1,len(path)):
if path[i-1] == temppath[j-1] and path[i] == temppath[j]:
found = True
if found == False:
cnt +=1
paths.append([u,neighbor1,v])
#print "path length 2 with ", neighbor1
if cnt >=3:
break
#u1neighbors = [x for x in u1neighbors if x not in uneighbors] #remove all items from this second neighbor list that were in the first neighbor list
if u in u1neighbors:
u1neighbors.remove(u)
for neighbor2 in u1neighbors:
u2neighbors = list(nx.all_neighbors(P,neighbor2)) #these are third neighbors
if v in u2neighbors:
#cnt += 1 #add 1 to count if v is in this set
temppath = [u,neighbor1,neighbor2,v]
found = False
for j in range(1,len(temppath)):
for path in paths:
for i in range(1,len(path)):
if path[i-1] == temppath[j-1] and path[i] == temppath[j]:
found = True
if found == False:
cnt +=1
paths.append([u,neighbor1,neighbor2,v])
if cnt >=3:
break
if cnt <=2: #cnt must be 3 or greater to remain in the graph
P.remove_edge(u,v)
#print "removed edge: ", (u,v)
elapsed = timeit.default_timer() - time
print "Protein partition ran in %f seconds" %elapsed
P1 = kl.kl_connected_subgraph(G, 3, 3, low_memory=True, same_as_graph=False)
H = nx.Graph()
for node in G.nodes():
H.add_node(node)
for edge in P1.edges():
H.add_edge(edge[0],edge[1])
P1 = H
A_1 = nx.adjacency_matrix(P1)
A_1 = A_1.todense()
A = nx.adjacency_matrix(P)
A = A.todense()
print np.nonzero(A_1-A)