-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAlignment.cpp
executable file
·479 lines (421 loc) · 16.6 KB
/
Alignment.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
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
469
470
471
472
473
474
475
476
477
478
479
#include "Alignment.h"
#include <fstream>
#include <cstdlib>
using namespace std;
//constructor
//finds the smaller network and the maximum degree of the input networks
//Inputs are two files of networks net1 and net2
Alignment::Alignment( Network net1, Network net2)
{
//compare networks to find the biggest one
if( net1.size > net2.size )
{
reverse = true;
network1 = net2;
network2 = net1;
}
else
{
reverse = false;
network1 = net1;
network2 = net2;
}
//maximum degree of the network
if(network1.maxDeg > network2.maxDeg)
maxDeg = network1.maxDeg;
else
maxDeg = network2.maxDeg;
blast = new float*[network1.size];
for (int c=0; c<network1.size; c++) {
blast[c]=new float[network2.size];
}
for (int c1=0; c1<network1.size; c1++) {
for (int c2=0; c2<network2.size; c2++) {
blast[c1][c2]=0;
}
}
}
void Alignment::readblast(string blastFile) {
float ** temp = new float*[network1.size];
for (int c=0; c<network1.size; c++) {
temp[c]=new float[network2.size];
}
for (int c1=0; c1<network1.size; c1++) {
for (int c2=0; c2<network2.size; c2++) {
temp[c1][c2]=0;
}
}
float max = 0 ;
//blast values
ifstream inputFile;
string token1,token2,line;
float token3;
inputFile.open(blastFile.c_str());
while (getline(inputFile, line)) {
istringstream tokenizer(line);
getline(tokenizer, token1, '\t');
getline(tokenizer, token2, '\t');
tokenizer >> token3;
if(max<token3) max = token3;
temp[network1.mapName[token1]][network2.mapName[token2]]=token3;
}
//normalize between zero and 1
for (int c1=0; c1<network1.size; c1++)
for (int c2=0; c2<network2.size; c2++)
blast[c1][c2] = temp[c1][c2]/max;
}
//produce a mapping between nodes of two network with respect to input parameter a.
//Input parameter a acontrols the factor edgeWeight in assigning the scores to the nodes. a should be between 0 and 1.
void Alignment::align(double lambda, double alpha)
{
bool flag; //check wether or not all the nodes of the smaller network are aligned?
//temporary
float temp;
float a1,a11;
float a2,a22;
float MINSCORE = -100000;
int coeff;
if(network2.numOfEdge>network1.numOfEdge) {
coeff = network2.numOfEdge/network1.numOfEdge;
}
else {
coeff = network1.numOfEdge/network2.numOfEdge;
}
int maxNode; // node with max score
bool *alignNodes1 = new bool[network1.size]; //aligned nodes of the smaller network
bool *alignNodes2 = new bool[network2.size]; //aligned nodes of the bigger network
alignment = new int[network1.size]; //alignment array
float *nodeScore1 = new float[network1.size]; //scores of nodes of smaller network
float *nodeScore2 = new float[network2.size]; //scores of nodes of bigger network
double **alignScore = new double*[network1.size]; //this matrix contains the score of each matching pair
int *best = new int[network1.size]; //array of best align scores
float ss;
//initial values
for(int c1=0; c1<network1.size; c1++)
alignScore[c1]=new double[network2.size];
for(int c1=0; c1<network1.size; c1++)
alignNodes1[c1]=false;
for(int c1=0; c1<network2.size; c1++)
alignNodes2[c1]=false;
for(int c1=0; c1<network1.size; c1++)
alignment[c1]=-1;
for(int c1=0; c1<network1.size; c1++)
best[c1]=-1;
ofstream NS;
//initialize nodeScore fro both networks
for(int c1=0; c1< network1.size; c1++)
nodeScore1[c1]=(1-lambda)*network1.nodeWeight[c1];
for(int c1=0; c1< network2.size; c1++)
nodeScore2[c1]=(1-lambda)*network2.nodeWeight[c1];
//find max score
//finding the nodescore
for (int c1=0; c1<network1.size; c1++){
for (int c2=0; c2<network1.size; c2++)
nodeScore1[c1]+= lambda*network1.edgeWeight[c1][c2];
}
for (int c1=0; c1<network2.size; c1++){
for (int c2=0; c2<network2.size; c2++)
nodeScore2[c1] += lambda*network2.edgeWeight[c1][c2];
}
//======first network
float max = -10000;
for (int c1=0; c1<network1.size; c1++) {
if (max < nodeScore1[c1]) {
max = nodeScore1[c1];
}
}
//====== second network
for (int c1=0; c1<network2.size; c1++) {
if (max < nodeScore2[c1]) {
max = nodeScore2[c1];
}
}
//normalize with respect to max
for (int c1=0; c1<network1.size; c1++) {
nodeScore1[c1] = nodeScore1[c1]/max;
}
for (int c1=0; c1<network2.size; c1++) {
nodeScore2[c1] = nodeScore2[c1]/max;
}
//END of normalization
//finding the alignscore
for(int c1=0; c1<network1.size; c1++)
for(int c2=0; c2<network2.size; c2++){
alignScore[c1][c2] = (nodeScore1[c1]>nodeScore2[c2])? nodeScore2[c2]:nodeScore1[c1];
alignScore[c1][c2] = alpha * (alignScore[c1][c2]);
alignScore[c1][c2] += (1-alpha)*blast[c1][c2]; //adding similarity
}
int counter = 0;
flag=true;
int temp1,temp2;
int counteralign=0;
while(flag){ //there is some unaligned nodes in determined iteration
//find the maximum value of each row of alignscore and save it in array "best"
for(int c1=0; c1<network1.size; c1++)
{
if(!alignNodes1[c1]){
temp=MINSCORE;
for(int c2=0; c2<=network2.size; c2++)
if(temp<alignScore[c1][c2] && !alignNodes2[c2]){
if(alignScore[c1][c2]==temp) {
temp1 = (network1.deg[c1]>network2.deg[c2]) ? network2.deg[c2]/network1.deg[c1]:network1.deg[c1]/network2.deg[c2];
temp2 = (network1.deg[c1]>network2.deg[best[c1]]) ? network2.deg[best[c1]]/network1.deg[c1]:network1.deg[c1]/network2.deg[best[c1]];
if(temp1 > temp2) {
best[c1]=c2;
temp = alignScore[c1][c2];
}
}
else {
best[c1]=c2;
temp = alignScore[c1][c2];
}
}
}
}
//doing the alignment
//find the maximum value of array "best" that means the best score in matrix "alignScore"
temp=MINSCORE;
flag=false;
for(int c1=0; c1<network1.size; c1++)
if(temp<alignScore[c1][best[c1]] && !alignNodes1[c1] && !alignNodes2[best[c1]]){ //=
flag=true; //still there is node that is not aligned
if(alignScore[c1][best[c1]]==temp) {
if(network1.deg[c1] > network1.deg[maxNode]) {
maxNode = c1;
temp = alignScore[c1][best[c1]];
}
}
else {
temp = alignScore[c1][best[c1]];
maxNode = c1;
}
}
if(flag){ //there is some node in first network that are not still aligned
alignment[maxNode]=best[maxNode]; //align two nodes;
alignNodes1[maxNode]=true;
alignNodes2[best[maxNode]]=true;
//align degree one neighbors together
for(int j=0; j<network1.deg[maxNode]; j++)
for(int k=0; k<network2.deg[best[maxNode]]; k++)
if( !alignNodes1[network1.neighbor[maxNode][j]] && !alignNodes2[network2.neighbor[best[maxNode]][k]])
{
if(network1.deg[network1.neighbor[maxNode][j]]==1 && network2.deg[network2.neighbor[best[maxNode]][k]]==1)
{
alignment[network1.neighbor[maxNode][j]] = network2.neighbor[best[maxNode]][k];
alignNodes1[network1.neighbor[maxNode][j]] = true;
alignNodes2[network2.neighbor[best[maxNode]][k]] = true;
}
}
//update the align scores
for(int c1=0; c1 <network1.deg[maxNode]; c1++)
for(int c2=0; c2<network2.deg[best[maxNode]]; c2++)
alignScore[ network1.neighbor[maxNode][c1]][network2.neighbor[best[maxNode]][c2]]=alignScore[ network1.neighbor[maxNode][c1]][network2.neighbor[best[maxNode]][c2]]+(coeff/max);
}
counter = counter + 1;
if ( counter % 1000 == 0)
cout << counter << endl;
}//end flag
//memory leak
delete [] alignNodes1;
delete [] alignNodes2;
delete [] nodeScore1;
delete [] nodeScore2;
delete [] best;
for(int j=0; j<network1.size; j++)
{
delete [] alignScore[j];
}
delete [] alignScore;
evaluate(); //calculate the measurment evaluations
}
//calculate the evaluation measurments EC (Edge Correctness), IC (Interaction Correctness), NC (Node Correctness), CCCV and CCCE (largest Common Connected subraph with recpect to Vertices and Edges)
void Alignment::evaluate(void)
{
CCCV = getCCCV(); //calculate CCCV
CCCE = getCCCE(); //calculate CCCE
EC = getEC(); //calculate Edge Correctness
S3 = getS3(); //calculate S3
}
//calculate CCCV
//return the number of vertices of largest common connected subgraph of the alignment
int Alignment::getCCCV(void)
{
int *subGraph;
int compNum = 1; //number of connected components
int *q = new int[network1.size]; //nodes that are already processed
comp = new int[network1.size]; //dtermines the connected component each node belongs to.
for(int i=0; i<network1.size; i++)
{
comp[i] = network1.size;
q[i] = i;
}
int last = 0;
//for each node of the network
for(int i=0; i<network1.size; i++)
{
if(comp[i]==network1.size)
{
q[0] = i;
comp[i] = compNum;
compNum++;
last = 1;
//finds all connected nodes tho the node i that is not alredy in a connected component
for(int k=0; k<last; k++)
for(int j=0; j<network1.deg[q[k]]; j++)
//the node is not already processed
if( comp[q[k]] < comp[network1.neighbor[q[k]][j]])
{
if (alignment[q[k]] != -1)
for( int l=0; l < network2.deg[alignment[q[k]]]; l++ )
if(network2.neighbor[alignment[q[k]]][l] == alignment[network1.neighbor[q[k]][j]])
{
comp[network1.neighbor[q[k]][j]] = comp[q[k]];
q[last] = network1.neighbor[q[k]][j];
last++;
}
}
}
}
subGraph = new int[compNum-1]; //array of connected components
for(int i=0; i<compNum-1; i++)
subGraph[i] = 0;
for(int i=0; i<network1.size; i++)
subGraph[comp[i]-1]++; //number of nodes in a definit connected component
//find the component with maximum nodes
maxComp = 0;
for(int i=0; i<compNum-1; i++)
{
if(subGraph[maxComp] < subGraph[i])
maxComp = i;
}
int temp = subGraph[maxComp];
//memory leak
delete [] subGraph;
delete [] q;
return temp;
}
//calculate the evaluation measurment CCCE
//return the number of edges of largest common connected subgraph of the alignment
int Alignment::getCCCE(void)
{
int edgeComp = 0;
ofstream CC;
//for each node of first network
for(int i=0; i<network1.size; i++)
{
//for each neighbor of node i
for(int j=0; j<network1.deg[i]; j++)
//for each neighbor l of a node in second network that is aligned with node i
if (alignment[i] != -1)
for( int l=0; l < network2.deg[alignment[i]]; l++ )
if(network2.neighbor[ alignment[i] ][l] == alignment[network1.neighbor[i][j]])
if( comp[i]-1 == maxComp){
edgeComp++;
}
}
return ( edgeComp / 2 );
}
//calculate the evaluation measurment EC
//returns the percent of edges that are mapped correctly in alignment
float Alignment::getEC(void)
{
int totalScore=0;
//for each node i in first network
for(int i=0; i<network1.size; i++)
{
//for each neighbor j of node i
for(int j=0; j<network1.deg[i]; j++)
//for each neighbor l of a node in second network that is aligned with node i
if (alignment[i] != -1)
for( int l=0; l < network2.deg[alignment[i]]; l++ ) {
if(network2.neighbor[ alignment[i] ][l] == alignment[ network1.neighbor[i][j] ]) {
totalScore++;
}
}
}
//minimum number of edges of two networks
int minEdge = ( network1.numOfEdge > network2.numOfEdge)? network2.numOfEdge : network1.numOfEdge;
//calculate EC(edge correctness)
return ( (float) totalScore ) / ( 2 * minEdge );
}
float Alignment::getS3(void)
{
int totalScore=0;
int* alignnodes = new int[network1.size];
int num_edge_net2=0;
//for each node i in first network
for(int i=0; i<network1.size; i++)
{
alignnodes[i]=alignment[i];
//for each neighbor j of node i
for(int j=0; j<network1.deg[i]; j++)
//for each neighbor l of a node in second network that is aligned with node i
if (alignment[i] != -1)
for( int l=0; l < network2.deg[alignment[i]]; l++ ) {
if(network2.neighbor[ alignment[i] ][l] == alignment[ network1.neighbor[i][j] ]) {
totalScore++;
}
}
}
totalScore=totalScore/2;
for(int i=0; i<network1.size; i++)
if (alignment[i] != -1)
for(int j=0; j<network2.deg[alignnodes[i]]; j++)
for(int l=0; l<network1.size; l++)
if(network2.neighbor[alignnodes[i]][j]==alignnodes[l])
num_edge_net2++;
num_edge_net2=num_edge_net2/2;
//minimum number of edges of two networks
int minEdge = ( network1.numOfEdge > network2.numOfEdge)? network2.numOfEdge : network1.numOfEdge;
//calculate EC(edge correctness)
return ( (float) totalScore ) / ( minEdge + float(num_edge_net2) - totalScore );
}
//print the evaluation measurments in a file with input parameter name
//Input parameter name determines the file that result are to be written in.
void Alignment::outputEvaluation(string name)
{
string outFile = name;
//add a definit suffix to the file
outFile.append(".eval");
ofstream outputFile( outFile.c_str());
//print in console
outputFile << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
outputFile << "*** CONNECTED COMPONENTS SIZE : " << endl;
outputFile << "Nodes = " << CCCV << endl;
outputFile << "Edges = " << CCCE << endl;
outputFile << "===============================================================" << endl;
if(reverse)
{
outputFile << "G1: Nodes : " << network2.size << " - Edges : " << network2.numOfEdge << endl;
outputFile << "G2: Nodes : " << network1.size << " - Edges : " << network1.numOfEdge << endl;
}
else
{
outputFile << "G1: Nodes : " << network1.size << " - Edges : " << network1.numOfEdge << endl;
outputFile << "G2: Nodes : " << network2.size << " - Edges : " << network2.numOfEdge << endl;
}
outputFile << "EC : " << EC << endl;
outputFile << "S3 : " << S3 << endl;
}
//print the alignment(mapping) in a file with input parameter name
//Input parameter name determines the file that mapping is to be written in.
void Alignment::outputAlignment(string name)
{
string alignFile = name;
alignFile.append(".alignment");
ofstream alignmentFile( alignFile.c_str());
if(reverse)
for(int i=0; i<network1.size; i++)
alignmentFile << network1.getName( alignment[ i ] ) << ' ' << network2.getName( i )<< endl;
else
for(int i=0; i<network1.size; i++)
alignmentFile << network1.getName( i ) << ' ' << network2.getName( alignment[ i ] )<< endl;
}
//instructor
Alignment::Alignment(void)
{
}
//destructor
Alignment::~Alignment(void)
{
}