-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessNetworkBatch8Bit.java
469 lines (404 loc) · 14.1 KB
/
ProcessNetworkBatch8Bit.java
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.*;
public class ProcessNetworkBatch8Bit
{
//Command line arguments
private static String graphListFileName = "";
private static int t = 5;
private static String graphFileName = "";
private static String outputFileName = "";
// The adjacency matrix (similarity network)
private static Hashtable AMatrix = new Hashtable(100000);
private static Hashtable edgeCounts = new Hashtable(4000);
private static Hashtable doubleEdgeCounts = new Hashtable(2000000);
private static Hashtable allEdges = new Hashtable(30000);
private static Hashtable printedEdges = new Hashtable(30000);
public static int intType(String interaction)
{
if (interaction.equals("interacts-with"))
return 0;
else if (interaction.equals("in-complex-with"))
return 1;
else if (interaction.equals("catalysis-precedes"))
return 2;
else if (interaction.equals("controls-expression-of"))
return 3;
else if (interaction.equals("controls-state-change-of"))
return 4;
else System.out.println("!!! Unknown Interaction Type in the Network File: "+interaction);
return -1;
}
public static String getSymmetric(String e1,int numUndirected)
{
String sym_e1;
char[] csym_e1 = new char[e1.length()];
int numDirected;
numDirected = (e1.length()-numUndirected)/2;
for (int i=0;i<e1.length();i++)
{
csym_e1[i] = '0';
}
for (int i=0;i<numUndirected;i++)
{
csym_e1[i] = e1.charAt(i);
}
for (int i=numUndirected;i<numUndirected+numDirected;i++)
{
if (e1.charAt(i)=='1')
csym_e1[i+numDirected] = '1';
}
for (int i=numUndirected+numDirected;i<e1.length();i++)
{
if (e1.charAt(i)=='1')
csym_e1[i-numDirected] = '1';
}
sym_e1 = String.valueOf(csym_e1);
return sym_e1;
}
public static boolean isIsomorphic2(String e1,String e2,int numUndirected)
{
String sym_e1;
String sym_e2;
char[] csym_e1 = new char[e1.length()];
char[] csym_e2 = new char[e2.length()];
int numDirected;
numDirected = (e1.length()-numUndirected)/2;
for (int i=0;i<e1.length();i++)
{
csym_e1[i] = '0';
csym_e2[i] = '0';
}
for (int i=0;i<numUndirected;i++)
{
csym_e1[i] = e1.charAt(i);
csym_e2[i] = e2.charAt(i);
}
for (int i=numUndirected;i<numUndirected+numDirected;i++)
{
if (e1.charAt(i)=='1')
csym_e1[i+numDirected] = '1';
if (e2.charAt(i)=='1')
csym_e2[i+numDirected] = '1';
}
for (int i=numUndirected+numDirected;i<e1.length();i++)
{
if (e1.charAt(i)=='1')
csym_e1[i-numDirected] = '1';
if (e2.charAt(i)=='1')
csym_e2[i-numDirected] = '1';
}
sym_e1 = String.valueOf(csym_e1);
sym_e2 = String.valueOf(csym_e2);
//System.out.print("e1: "+e1+" sym_e1: "+sym_e1+" e2: "+e2+" sym_e2: "+sym_e2);
if ((e1+sym_e1).equals(e2+sym_e2) || (e1+sym_e1).equals(sym_e2+e2))
{
//System.out.println(" returns true.");
return true;
}
else
{
//System.out.println(" returns false.");
return false;
}
}
public static boolean isIsomorphic3(String e1,String e2,int numUndirected)
{
String AB1, BC1, CA1, BA1, CB1, AC1;
String AB2, BC2, CA2, BA2, CB2, AC2;
int edgeLength = e1.length();
AB1 = e1.substring(0,edgeLength/3);
BA1 = getSymmetric(AB1,numUndirected);
BC1 = e1.substring(edgeLength/3,2*edgeLength/3);
CB1 = getSymmetric(BC1,numUndirected);
CA1 = e1.substring(2*edgeLength/3,edgeLength);
AC1 = getSymmetric(CA1,numUndirected);
AB2 = e2.substring(0,edgeLength/3);
BA2 = getSymmetric(AB2,numUndirected);
BC2 = e2.substring(edgeLength/3,2*edgeLength/3);
CB2 = getSymmetric(BC2,numUndirected);
CA2 = e2.substring(2*edgeLength/3,edgeLength);
AC2 = getSymmetric(CA2,numUndirected);
if ((AB1+BA1+BC1+CB1+AC1+CA1).equals(AB2+BA2+BC2+CB2+AC2+CA2) || (AB1+BA1+BC1+CB1+AC1+CA1).equals(BA2+AB2+AC2+CA2+BC2+CB2) || (AB1+BA1+BC1+CB1+AC1+CA1).equals(CB2+BC2+BA2+AB2+CA2+AC2) || (AB1+BA1+BC1+CB1+AC1+CA1).equals(AC2+CA2+CB2+BC2+AB2+BA2) || (AB1+BA1+BC1+CB1+AC1+CA1).equals(CA2+AC2+AB2+BA2+CB2+BC2) || (AB1+BA1+BC1+CB1+AC1+CA1).equals(BC2+CB2+CA2+AC2+BA2+AB2))
return true;
return false;
}
public static boolean isSymmetric(String e1, String e2)
{
int k = 0;
int i;
k = e1.length();
if (e2.length()!=k) return false;
if (e1.equals(e2)) return true;
if (k==8) {
return isIsomorphic2(e1,e2,2);
} else // three node graph
{
return isIsomorphic3(e1,e2,2);
}
}
public static void main(String[] argv) throws FileNotFoundException, IOException
{
// Get command line arguments
if (argv != null)
{
int len = argv.length;
if (len == 2)
{
graphListFileName = argv[0];
t = (new Integer(argv[1])).intValue();
}
else printUsage();
}
BufferedReader listReader = new BufferedReader(new FileReader(graphListFileName));
String listLine = listReader.readLine();
int ccc = 1;
while (listLine!=null)
{
graphFileName = listLine;
System.out.println("Processing file #"+ccc+" : "+listLine);
ccc++;
outputFileName = "counts_"+listLine;
//Output file
FileOutputStream outFile=new FileOutputStream(outputFileName);
DataOutputStream outData=new DataOutputStream(outFile);
// read the graph file and create the adjacency matrix
BufferedReader graphReader = new BufferedReader(new FileReader(graphFileName));
String graphLine = graphReader.readLine();
AMatrix = new Hashtable(100000);
edgeCounts = new Hashtable(1000);
doubleEdgeCounts = new Hashtable(200000);
allEdges = new Hashtable(200000);
Hashtable printedEdges = new Hashtable(100000);
while (graphLine!=null)
{
String[] strs = split(graphLine,'\t');
if (strs.length<3)
{
System.out.println("Unexpected line in the edge file: "+graphLine);
break;
}
String p1 = strs[0];
String p2 = strs[1];
String interaction = strs[2];
if (interaction.equals("controls-phosphorylation-of") || interaction.equals("controls-transport-of"))
{
graphLine = graphReader.readLine();
continue;
}
if (AMatrix.containsKey(p1))
{
Hashtable n = (Hashtable)(AMatrix.get(p1));
if (n.containsKey(p2))
{
String edges =(String)(n.get(p2));
char[] edgesChars = edges.toCharArray();
edgesChars[intType(interaction)]='1';
edges = String.valueOf(edgesChars);
n.remove(p2);
n.put(p2,edges);
}
else
{
String edges = "00000000";
char[] edgesChars = edges.toCharArray();
edgesChars[intType(interaction)]='1';
edges = String.valueOf(edgesChars);
n.put(p2,edges);
}
}
else
{
Hashtable n = new Hashtable(20);
String edges = "00000000";
char[] edgesChars = edges.toCharArray();
edgesChars[intType(interaction)]='1';
edges = String.valueOf(edgesChars);
n.put(p2,edges);
AMatrix.put(p1,n);
}
// insert the other edge for undirected interactions (and directed interactions in reverse: edges 7-11)
//if (interaction.equals("in-complex-with") || interaction.equals("interacts-with"))
//{
if (AMatrix.containsKey(p2))
{
Hashtable n = (Hashtable)(AMatrix.get(p2));
if (n.containsKey(p1))
{
String edges =(String)(n.get(p1));
char[] edgesChars = edges.toCharArray();
if (intType(interaction)<2)
edgesChars[intType(interaction)]='1';
else
edgesChars[intType(interaction)+3]='1'; // reverse edges are at i+3
edges = String.valueOf(edgesChars);
n.remove(p1);
n.put(p1,edges);
}
else
{
String edges = "00000000";
char[] edgesChars = edges.toCharArray();
if (intType(interaction)<2)
edgesChars[intType(interaction)]='1';
else
edgesChars[intType(interaction)+3]='1'; // reverse edges are at i+3
edges = String.valueOf(edgesChars);
n.put(p1,edges);
}
}
else
{
Hashtable n = new Hashtable(20);
String edges = "00000000";
char[] edgesChars = edges.toCharArray();
if (intType(interaction)<2)
edgesChars[intType(interaction)]='1';
else
edgesChars[intType(interaction)+3]='1'; // reverse edges are at i+3
edges = String.valueOf(edgesChars);
n.put(p1,edges);
AMatrix.put(p2,n);
}
//}
graphLine = graphReader.readLine();
}
System.out.println("There are "+AMatrix.size()+" nodes in the network.");
int i = 1;
for (Enumeration en = AMatrix.keys();en.hasMoreElements();)
{
String p = (String)en.nextElement();
Hashtable n = (Hashtable)AMatrix.get(p);
//System.out.println("Processing node #"+i+"\tsingle = "+edgeCounts.size()+"\tdouble = "+doubleEdgeCounts.size());
i++;
for (Enumeration en2 = n.keys();en2.hasMoreElements();)
{
String p2 = (String)en2.nextElement();
//if (!(p.compareToIgnoreCase(p2)<0)) continue; // handles symmetry as well
String edge = (String)n.get(p2);
if (!allEdges.containsKey(edge))
allEdges.put(edge,edge);
if (edgeCounts.containsKey(edge))
{
int count = ((Integer)(edgeCounts.get(edge))).intValue();
count++;
edgeCounts.remove(edge);
edgeCounts.put(edge,new Integer(count));
}
else
{
edgeCounts.put(edge,new Integer(1));
}
Hashtable n2 = (Hashtable)AMatrix.get(p2);
for (Enumeration en3 = n2.keys();en3.hasMoreElements();)
{
String p3 = (String)en3.nextElement();
if (p3.equals(p)) continue;
//if (p.compareToIgnoreCase(p2)<0 && p.compareToIgnoreCase(p3)<0)
//{
String edge2 = (String)n2.get(p3);
Hashtable n3 = (Hashtable)AMatrix.get(p3);
String edge3 = "00000000";
if (n3.containsKey(p))
{
edge3 = (String)(n3.get(p));
}
if (!allEdges.containsKey(edge+edge2+edge3))
allEdges.put(edge+edge2+edge3,edge+edge2+edge3);
if (doubleEdgeCounts.containsKey(edge+edge2+edge3))
{
int count = ((Integer)(doubleEdgeCounts.get(edge+edge2+edge3))).intValue();
count++;
doubleEdgeCounts.remove(edge+edge2+edge3);
doubleEdgeCounts.put(edge+edge2+edge3,new Integer(count));
}
else
{
doubleEdgeCounts.put(edge+edge2+edge3,new Integer(1));
}
//}
}
}
}
for (Enumeration en = edgeCounts.keys();en.hasMoreElements();)
{
String edge = (String)en.nextElement();
int count = ((Integer)(edgeCounts.get(edge))).intValue();
boolean isPrinted = false;
for (Enumeration en2 = printedEdges.keys();en2.hasMoreElements();)
{
String pEdge = (String)en2.nextElement();
if (isSymmetric(edge,pEdge))
{
int count2 = ((Integer)(printedEdges.get(pEdge))).intValue();
if (count!=count2)
{
System.out.println("Symmetric edges "+edge+" and "+pEdge+" have different counts:"+count+" and "+count2);
System.exit(1);
}
isPrinted = true;
break;
}
}
if (!isPrinted && count>=t)
{
printedEdges.put(edge,new Integer(count));
outData.writeBytes(edge+"\t"+count+"\n");
}
}
for (Enumeration en = doubleEdgeCounts.keys();en.hasMoreElements();)
{
String edge = (String)en.nextElement();
int count = ((Integer)(doubleEdgeCounts.get(edge))).intValue();
boolean isPrinted = false;
for (Enumeration en2 = printedEdges.keys();en2.hasMoreElements();)
{
String pEdge = (String)en2.nextElement();
if (isSymmetric(edge,pEdge))
{
int count2 = ((Integer)(printedEdges.get(pEdge))).intValue();
if (count!=count2)
{
System.out.println("Symmetric edges "+edge+" and "+pEdge+" have different counts:"+count+" and "+count2);
System.exit(1);
}
isPrinted = true;
break;
}
}
if (!isPrinted && count>=t)
{
printedEdges.put(edge,new Integer(count));
outData.writeBytes(edge+"\t"+count+"\n");
}
}
outData.close();
listLine = listReader.readLine();
}
listReader.close();
}
private static void printUsage()
{
System.out.println("Usage: java ProcessNetworkBatch8Bit <file listing the network file names> <count threshold>");
System.exit(1);
}
public static String[] split(String str, char delim)
{
// begin split
Vector strsVec = new Vector(0,1);
String tmp = str;
while (tmp.indexOf(delim)!=-1)
{
if (tmp.substring(0,tmp.indexOf(delim)).length()>0)
strsVec.addElement(new String(tmp.substring(0,tmp.indexOf(delim))));
tmp = tmp.substring(tmp.indexOf(delim)+1,tmp.length());
}
strsVec.addElement(new String(tmp));
String[] strs = new String[strsVec.capacity()];
for (int s = 0; s < strsVec.capacity(); s++)
strs[s] = (String)strsVec.elementAt(s);
// end of split
return strs;
}
}