-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyNetworkAnalizer.py
259 lines (221 loc) · 8.68 KB
/
pyNetworkAnalizer.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import networkx as nx
from os import listdir
import multiprocessing as mp
import argparse
def parallelProperties(name):
print name
#creating multi directed graph
MG=nx.MultiGraph()
#reading file and adding nodes - edges
file=None
if pathToFiles!=None:
file=open(pathToFiles+"/"+name,"r")
else:
file=open("./"+name,"r")
listOfInteractions=[] #i will save interactions to rebuild the directed digraph
for line in file:
splittedLine=line.split("\t")
node1=splittedLine[0]
node2=splittedLine[1]
listOfInteractions.append(node1+":"+node2)
MG.add_edge(node1, node2)
file.close()
#####################################
##
## dict to save measures
##
#####################################
dictProp={}
for node in MG.nodes():
dictProp[node]={"average_shortest_path_length":'', "clustering_coefficient":'0',"closeness_centrality":'', "eccentricity":'',"stress":'0',"edge_count":'',"In_degree":'0',"Out_degree":'0',"Betweenness_centrality":'', "Neighborhood_conectivity":''}
file=None
if pathToFiles!=None:
file=open(pathToFiles+"/"+name,"r")
else:
file=open("./"+name,"r")
####################################################################
##
## for in degree and out degree
##
####################################################################
for line in file:
splittedLine=line.split("\t")
node1=splittedLine[0]
node2=splittedLine[1]
dictProp[node1]["Out_degree"]=str(int(dictProp[node1]["Out_degree"])+1)
dictProp[node2]["In_degree"]=str(int(dictProp[node2]["In_degree"])+1)
file.close()
#we will see subgraphs
subGS=list(nx.connected_component_subgraphs(MG))
#now we will rebuild these graphs as multidigraphs
for subG in list(nx.connected_component_subgraphs(MG)):
#first step: create a multidigraph
md=nx.MultiDiGraph()
whitoutSL=nx.MultiGraph() #a graph without selfloops
directed=nx.DiGraph()
MDNoSelfLoop=nx.MultiDiGraph() #a graph without selfloops
#the second step is to loop over the edges, searching for the direction of interaction
for edge in nx.edges(subG):
nodeX, nodeY=edge
#if is a self interaction
if nodeX==nodeY:
md.add_edge(nodeX,nodeY)
directed.add_edge(nodeX,nodeY)
else:
#if is not a self interaction I will look for the directions (if exist A:B and/or B:A) and Ill add the edge
cont=0
if nodeX+":"+nodeY in listOfInteractions:
md.add_edge(nodeX,nodeY)
directed.add_edge(nodeX,nodeY)
whitoutSL.add_edge(nodeX,nodeY)
MDNoSelfLoop.add_edge(nodeX,nodeY)
if nodeY+":"+nodeX in listOfInteractions:
md.add_edge(nodeY,nodeX)
whitoutSL.add_edge(nodeY,nodeX)
directed.add_edge(nodeY,nodeX)
MDNoSelfLoop.add_edge(nodeY,nodeX)
####################################################################
##
## Metrics
##
####################################################################
for node in md.nodes():
####################################################################
##
## Edge count
##
####################################################################
dictProp[node]["edge_count"]=str(int(dictProp[node]["Out_degree"])+int(dictProp[node]["In_degree"]))
####################################################################
##
## average shortest path length
##
####################################################################
#at this point we have directed subgraphs, so now is time to comute average shortest path of each subgraph
#first we will compute shortest path of one node, then we will compute average shortest path length
shortestPaths=nx.shortest_path_length(md, source=node)
summatory=0
cont=0
for item in shortestPaths.items():
summatory+=float(item[1])
cont+=1
if (cont-1)!=0:
dictProp[node]["average_shortest_path_length"]=str(summatory/(cont-1))
#print node,(summatory/(cont-1))
else:
dictProp[node]["average_shortest_path_length"]="0"
####################################################################
##
## eccentricity
##
####################################################################
higher=0
for paths in shortestPaths.items():
if int(paths[1])>higher:
higher=int(paths[1])
dictProp[node]["eccentricity"]=str(higher)
####################################################################
##
## closeness centrality
##
####################################################################
for item in (nx.closeness_centrality(md, normalized=False)).items():
dictProp[item[0]]["closeness_centrality"]=str(item[1])
####################################################################
##
## neighborhood connectivity
##
####################################################################
for item in (nx.average_neighbor_degree(whitoutSL)).items():
dictProp[item[0]]["Neighborhood_conectivity"]=str(item[1])
####################################################################
##
## stress centrality
##
####################################################################
for Source in md.nodes():
for Target in md.nodes():
if Source!=Target:
try:
for path in nx.all_shortest_paths(md,source=Source,target=Target):
if len(path)>2:
for N in path[1:-1]:
dictProp[N]["stress"]=str(int(dictProp[N]["stress"])+1)
except:
pass
####################################################################
##
## betweenness centrality
##
####################################################################
for item in (nx.betweenness_centrality(md)).items():
dictProp[item[0]]["Betweenness_centrality"]=str(item[1])
####################################################################
##
## clustering coefficient
##
####################################################################
for node in MDNoSelfLoop.nodes():
inPlusOut=float(dictProp[node]["Out_degree"])+float(dictProp[node]["In_degree"])
division=(len(whitoutSL.neighbors(node))*(len(whitoutSL.neighbors(node))-1))
if len(whitoutSL.neighbors(node))>1: #if node has at least two neighbour
connectedNeighbors=0
neighbors=whitoutSL.neighbors(node)
for neighbor in neighbors:
#print neighbor
neighborsOfNeighbors=MDNoSelfLoop.neighbors(neighbor)
#print neighbor, neighborsOfNeighbors
for n in neighborsOfNeighbors:
#print n
if n in neighbors:
connectedNeighbors+=1
dictProp[node]["clustering_coefficient"]=str(float(connectedNeighbors)/division)
outFile=None
if Result!=None:
outFile=open(Result+"/"+name[:-4]+".csv","w")
else:
outFile=open("./"+name[:-4]+".csv","w")
outFile.write("\"AverageShortestPathLength\",\"BetweennessCentrality\",\"ClosenessCentrality\",\"ClusteringCoefficient\",\"Eccentricity\",\"EdgeCount\",\"Indegree\",\"name\",\"NeighborhoodConnectivity\",\"Outdegree\",\"Stress\"\n")
for item in dictProp.items():
node=item[0]
outFile.write("\""+dictProp[node]["average_shortest_path_length"]+"\",\""+dictProp[node]["Betweenness_centrality"]+"\",\""+dictProp[node]["closeness_centrality"]+"\",\""+dictProp[node]["clustering_coefficient"]+"\",\""+dictProp[node]["eccentricity"]+"\",\""+dictProp[node]["edge_count"]+"\",\""+dictProp[node]["In_degree"]+"\",\""+node+"\",\""+dictProp[node]["Neighborhood_conectivity"]+"\",\""+dictProp[node]["Out_degree"]+"\",\""+dictProp[node]["stress"]+"\"\n")
outFile.close()
def main():
global Result, pathToFiles
parser = argparse.ArgumentParser()
#number of processors to use
parser.add_argument("-P","--path", help="path where TSV files are located")
parser.add_argument("-R","--results", help="/Path/where/resultfiles/will/be/saved (do not put / after path) If you do not specify a path, current path of script will be used")
parser.add_argument("-I","--Input", help="A single TSV input file ")
parser.add_argument("-N","--nproc",help="Number of processors to use. Default: all", default="all")
args = parser.parse_args()
if args.results==None and (args.path==None or args.Input==None):
print "use --help option"
exit()
if args.path and args.Input:
print "\nYou only can use one option between -R and -I\n"
exit()
if args.results:
Result=args.results
else:
Result=None
if args.path:
pathToFiles=args.path
else:
pathToFiles=None
pool=None
if args.nproc=="all":
pool=mp.Pool()#processes=int(nproc)) #for multiprocessing
else:
pool=mp.Pool(processes=int(args.nproc)) #for multiprocessing
tsvList=[]
if args.path:
for file in listdir(str(args.path)):
if file[-4:]==".tsv":
tsvList.append(file)
elif args.Input:
#print args.Input
tsvList.append(args.Input)
processReturn=pool.map(parallelProperties,(tsvList))
if __name__=="__main__":
main()