forked from satijalab/seurat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRModularityOptimizer.cpp
179 lines (159 loc) · 5.4 KB
/
RModularityOptimizer.cpp
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
#include <chrono>
#include <exception>
#include <fstream>
#include <iostream>
#include <limits>
#include <memory>
#include <sstream>
#include <RcppEigen.h>
#include <Rcpp.h>
#include <progress.hpp>
#include "ModularityOptimizer.h"
using namespace ModularityOptimizer;
using namespace std::chrono;
using namespace Rcpp;
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppProgress)]]
// [[Rcpp::export]]
IntegerVector RunModularityClusteringCpp(Eigen::SparseMatrix<double> SNN,
int modularityFunction,
double resolution,
int algorithm,
int nRandomStarts,
int nIterations,
int randomSeed,
bool printOutput,
std::string edgefilename) {
// validate arguments
if(modularityFunction != 1 && modularityFunction != 2)
stop("Modularity parameter must be equal to 1 or 2.");
if(algorithm != 1 && algorithm !=2 && algorithm !=3 && algorithm !=4)
stop("Algorithm for modularity optimization must be 1, 2, 3, or 4");
if(nRandomStarts < 1)
stop("Have to have at least one start");
if(nIterations < 1)
stop("Need at least one interation");
if (modularityFunction == 2 && resolution > 1.0)
stop("error: resolution<1 for alternative modularity");
try {
bool update;
double modularity, maxModularity, resolution2;
int i, j;
std::string msg = "Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck";
if (printOutput)
Rcout << msg << std::endl << std::endl;
// Load netwrok
std::shared_ptr<Network> network;
if(edgefilename != "") {
if (printOutput)
Rcout << "Reading input file..." << std::endl << std::endl;
try{
network = readInputFile(edgefilename, modularityFunction);
} catch(...) {
stop("Could not parse edge file.");
}
} else {
// Load lower triangle
int network_size = (SNN.nonZeros() / 2) + 3;
IVector node1;
IVector node2;
DVector edgeweights;
node1.reserve(network_size);
node2.reserve(network_size);
edgeweights.reserve(network_size);
for (int k=0; k < SNN.outerSize(); ++k){
for (Eigen::SparseMatrix<double>::InnerIterator it(SNN, k); it; ++it){
if(it.col() >= it.row()){
continue;
}
node1.emplace_back(it.col());
node2.emplace_back(it.row());
edgeweights.emplace_back(it.value());
}
}
if(node1.size() == 0) {
stop("Matrix contained no network data. Check format.");
}
network = matrixToNetwork(node1, node2, edgeweights, modularityFunction);
Rcpp::checkUserInterrupt();
}
if (printOutput)
{
Rprintf("Number of nodes: %d\n", network->getNNodes());
Rprintf("Number of edges: %d\n", network->getNEdges());
Rcout << std::endl;
Rcout << "Running " << ((algorithm == 1) ? "Louvain algorithm" : ((algorithm == 2) ? "Louvain algorithm with multilevel refinement" : "smart local moving algorithm")) << "...";
Rcout << std::endl;
}
resolution2 = ((modularityFunction == 1) ? (resolution / (2 * network->getTotalEdgeWeight() + network->getTotalEdgeWeightSelfLinks())) : resolution);
auto beginTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
std::shared_ptr<Clustering> clustering;
maxModularity = -std::numeric_limits<double>::infinity();
JavaRandom random(randomSeed);
Progress p(nRandomStarts, printOutput);
for (i = 0; i < nRandomStarts; i++)
{
//if (printOutput && (nRandomStarts > 1))
//Rprintf("Random start: %d\n", i + 1);
VOSClusteringTechnique vosClusteringTechnique(network, resolution2);
j = 0;
update = true;
do
{
/*if (printOutput && (nIterations > 1))
Rprintf("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))
// Rprintf("Modularity: %.4f\n", modularity);
Rcpp::checkUserInterrupt();
}
while ((j < nIterations) && update);
if (modularity > maxModularity)
{
clustering = vosClusteringTechnique.getClustering();
maxModularity = modularity;
}
/*if (printOutput && (nRandomStarts > 1))
{
if (nIterations == 1)
Rprintf("Modularity: %.4f\n", modularity);
Rcout << std::endl;
}*/
p.increment();
}
auto endTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
if(clustering == nullptr) {
stop("Clustering step failed.");
}
if (printOutput)
{
if (nRandomStarts == 1)
{
if (nIterations > 1)
Rcout << std::endl;
Rprintf("Modularity: %.4f\n", maxModularity);
}
else
Rprintf("Maximum modularity in %d random starts: %.4f\n", nRandomStarts, maxModularity);
Rprintf("Number of communities: %d\n", clustering->getNClusters());
Rprintf("Elapsed time: %d seconds\n", static_cast<int>((endTime - beginTime).count() / 1000.0));
}
// Return results
clustering->orderClustersByNNodes();
IntegerVector iv(clustering->cluster.cbegin(), clustering->cluster.cend());
return iv;
} catch(std::exception &ex) {
forward_exception_to_r(ex);
} catch(...) {
::Rf_error("c++ exception (unknown reason)");
}
return IntegerVector(1);
}