forked from satijalab/seurat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModularityOptimizer.java
executable file
·257 lines (221 loc) · 9.42 KB
/
ModularityOptimizer.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
/**
* ModularityOptimizer
*
* @author Ludo Waltman
* @author Nees Jan van Eck
* @version 1.3.0, 08/31/15
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Console;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
public class ModularityOptimizer
{
public static void main(String[] args) throws IOException
{
boolean printOutput, update;
Clustering clustering;
Console console;
double modularity, maxModularity, resolution, resolution2;
int algorithm, i, j, modularityFunction, nIterations, nRandomStarts;
long beginTime, endTime, randomSeed;
Network network;
Random random;
String inputFileName, outputFileName;
VOSClusteringTechnique VOSClusteringTechnique;
if (args.length == 9)
{
inputFileName = args[0];
outputFileName = args[1];
modularityFunction = Integer.parseInt(args[2]);
resolution = Double.parseDouble(args[3]);
algorithm = Integer.parseInt(args[4]);
nRandomStarts = Integer.parseInt(args[5]);
nIterations = Integer.parseInt(args[6]);
randomSeed = Long.parseLong(args[7]);
printOutput = (Integer.parseInt(args[8]) > 0);
if (printOutput)
{
System.out.println("Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck");
System.out.println();
}
}
else
{
console = System.console();
System.out.println("Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck");
System.out.println();
inputFileName = console.readLine("Input file name: ");
outputFileName = console.readLine("Output file name: ");
modularityFunction = Integer.parseInt(console.readLine("Modularity function (1 = standard; 2 = alternative): "));
resolution = Double.parseDouble(console.readLine("Resolution parameter (e.g., 1.0): "));
algorithm = Integer.parseInt(console.readLine("Algorithm (1 = Louvain; 2 = Louvain with multilevel refinement; 3 = smart local moving): "));
nRandomStarts = Integer.parseInt(console.readLine("Number of random starts (e.g., 10): "));
nIterations = Integer.parseInt(console.readLine("Number of iterations (e.g., 10): "));
randomSeed = Long.parseLong(console.readLine("Random seed (e.g., 0): "));
printOutput = (Integer.parseInt(console.readLine("Print output (0 = no; 1 = yes): ")) > 0);
System.out.println();
}
if (printOutput)
{
System.out.println("Reading input file...");
System.out.println();
}
network = readInputFile(inputFileName, modularityFunction);
if (printOutput)
{
System.out.format("Number of nodes: %d%n", network.getNNodes());
System.out.format("Number of edges: %d%n", network.getNEdges());
System.out.println();
System.out.println("Running " + ((algorithm == 1) ? "Louvain algorithm" : ((algorithm == 2) ? "Louvain algorithm with multilevel refinement" : "smart local moving algorithm")) + "...");
System.out.println();
}
resolution2 = ((modularityFunction == 1) ? (resolution / (2 * network.getTotalEdgeWeight() + network.totalEdgeWeightSelfLinks)) : resolution);
beginTime = System.currentTimeMillis();
clustering = null;
maxModularity = Double.NEGATIVE_INFINITY;
random = new Random(randomSeed);
for (i = 0; i < nRandomStarts; i++)
{
if (printOutput && (nRandomStarts > 1))
System.out.format("Random start: %d%n", i + 1);
VOSClusteringTechnique = new VOSClusteringTechnique(network, resolution2);
j = 0;
update = true;
do
{
if (printOutput && (nIterations > 1))
System.out.format("Iteration: %d%n", j + 1);
if (algorithm == 1)
update = VOSClusteringTechnique.runLouvainAlgorithm(random);
else if (algorithm == 2)
update = VOSClusteringTechnique.runLouvainAlgorithmWithMultilevelRefinement(random);
else if (algorithm == 3)
VOSClusteringTechnique.runSmartLocalMovingAlgorithm(random);
j++;
modularity = VOSClusteringTechnique.calcQualityFunction();
if (printOutput && (nIterations > 1))
System.out.format("Modularity: %.4f%n", modularity);
}
while ((j < nIterations) && update);
if (modularity > maxModularity)
{
clustering = VOSClusteringTechnique.getClustering();
maxModularity = modularity;
}
if (printOutput && (nRandomStarts > 1))
{
if (nIterations == 1)
System.out.format("Modularity: %.4f%n", modularity);
System.out.println();
}
}
endTime = System.currentTimeMillis();
if (printOutput)
{
if (nRandomStarts == 1)
{
if (nIterations > 1)
System.out.println();
System.out.format("Modularity: %.4f%n", maxModularity);
}
else
System.out.format("Maximum modularity in %d random starts: %.4f%n", nRandomStarts, maxModularity);
System.out.format("Number of communities: %d%n", clustering.getNClusters());
System.out.format("Elapsed time: %d seconds%n", Math.round((endTime - beginTime) / 1000.0));
System.out.println();
System.out.println("Writing output file...");
System.out.println();
}
writeOutputFile(outputFileName, clustering);
}
private static Network readInputFile(String fileName, int modularityFunction) throws IOException
{
BufferedReader bufferedReader;
double[] edgeWeight1, edgeWeight2, nodeWeight;
int i, j, nEdges, nLines, nNodes;
int[] firstNeighborIndex, neighbor, nNeighbors, node1, node2;
Network network;
String[] splittedLine;
bufferedReader = new BufferedReader(new FileReader(fileName));
nLines = 0;
while (bufferedReader.readLine() != null)
nLines++;
bufferedReader.close();
bufferedReader = new BufferedReader(new FileReader(fileName));
node1 = new int[nLines];
node2 = new int[nLines];
edgeWeight1 = new double[nLines];
i = -1;
for (j = 0; j < nLines; j++)
{
splittedLine = bufferedReader.readLine().split("\t");
node1[j] = Integer.parseInt(splittedLine[0]);
if (node1[j] > i)
i = node1[j];
node2[j] = Integer.parseInt(splittedLine[1]);
if (node2[j] > i)
i = node2[j];
edgeWeight1[j] = (splittedLine.length > 2) ? Double.parseDouble(splittedLine[2]) : 1;
}
nNodes = i + 1;
bufferedReader.close();
nNeighbors = new int[nNodes];
for (i = 0; i < nLines; i++)
if (node1[i] < node2[i])
{
nNeighbors[node1[i]]++;
nNeighbors[node2[i]]++;
}
firstNeighborIndex = new int[nNodes + 1];
nEdges = 0;
for (i = 0; i < nNodes; i++)
{
firstNeighborIndex[i] = nEdges;
nEdges += nNeighbors[i];
}
firstNeighborIndex[nNodes] = nEdges;
neighbor = new int[nEdges];
edgeWeight2 = new double[nEdges];
Arrays.fill(nNeighbors, 0);
for (i = 0; i < nLines; i++)
if (node1[i] < node2[i])
{
j = firstNeighborIndex[node1[i]] + nNeighbors[node1[i]];
neighbor[j] = node2[i];
edgeWeight2[j] = edgeWeight1[i];
nNeighbors[node1[i]]++;
j = firstNeighborIndex[node2[i]] + nNeighbors[node2[i]];
neighbor[j] = node1[i];
edgeWeight2[j] = edgeWeight1[i];
nNeighbors[node2[i]]++;
}
if (modularityFunction == 1)
network = new Network(nNodes, firstNeighborIndex, neighbor, edgeWeight2);
else
{
nodeWeight = new double[nNodes];
Arrays.fill(nodeWeight, 1);
network = new Network(nNodes, nodeWeight, firstNeighborIndex, neighbor, edgeWeight2);
}
return network;
}
private static void writeOutputFile(String fileName, Clustering clustering) throws IOException
{
BufferedWriter bufferedWriter;
int i, nNodes;
nNodes = clustering.getNNodes();
clustering.orderClustersByNNodes();
bufferedWriter = new BufferedWriter(new FileWriter(fileName));
for (i = 0; i < nNodes; i++)
{
bufferedWriter.write(Integer.toString(clustering.getCluster(i)));
bufferedWriter.newLine();
}
bufferedWriter.close();
}
}