-
Notifications
You must be signed in to change notification settings - Fork 16
/
spqr.cpp
executable file
·553 lines (495 loc) · 14.8 KB
/
spqr.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <unordered_map>
#include <vector>
#include "cmdline/cmdline.h"
#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/simple_graph_alg.h>
#include <ogdf/decomposition/BCTree.h>
#include <ogdf/basic/GraphCopy.h>
#include <ogdf/decomposition/StaticSPQRTree.h>
#include <ogdf/decomposition/Skeleton.h>
using namespace std;
using namespace ogdf;
unordered_map<node,string> id2contig;
unordered_map<string,node> revid2contig;
unordered_map<int,string> intid2contig;
vector<pair<int,int> > pairs;
class Link
{
public:
int id;
string contig_a;
string contig_a_orientation;
string contig_b;
string contig_b_orientation;
double mean;
double stdev;
int bundle_size;
Link() {};
Link(int id, string contig_a, string contig_a_orientation, string contig_b, string contig_b_orientation, double mean, double stdev);
Link(int id, string contig_a, string contig_a_orientation, string contig_b, string contig_b_orientation, double mean, double stdev, int bundle_size);
double getmean();
double getstdev();
string getlinkorientation();
string getcontigs();
string getfirstcontig();
string getsecondcontig();
string getfirstorietation();
string getsecondorientation();
int get_bundle_size();
int getid();
};
Link :: Link(int id, string contig_a, string contig_a_orientation, string contig_b, string contig_b_orientation, double mean, double stdev, int bundle_size)
{
this->id = id;
this->contig_a = contig_a;
this->contig_b = contig_b;
this->contig_a_orientation = contig_a_orientation;
this->contig_b_orientation = contig_b_orientation;
this->mean = mean;
this->stdev = stdev;
this->bundle_size = bundle_size;
}
Link :: Link(int id, string contig_a, string contig_a_orientation, string contig_b, string contig_b_orientation, double mean, double stdev)
{
this->id = id;
this->contig_a = contig_a;
this->contig_b = contig_b;
this->contig_a_orientation = contig_a_orientation;
this->contig_b_orientation = contig_b_orientation;
this->mean = mean;
this->stdev = stdev;
}
string Link :: getfirstcontig()
{
return this->contig_a;
}
string Link :: getsecondcontig()
{
return this->contig_b;
}
string Link :: getfirstorietation()
{
return this->contig_a_orientation;
}
string Link :: getsecondorientation()
{
return this->contig_b_orientation;
}
int Link :: get_bundle_size()
{
return this->bundle_size;
}
double Link :: getmean()
{
return this->mean;
}
double Link :: getstdev()
{
return this->stdev;
}
string Link :: getlinkorientation()
{
return this->contig_a_orientation + this->contig_b_orientation;
}
string Link :: getcontigs()
{
return contig_a +"$"+contig_b;
}
int Link :: getid()
{
return this->id;
}
char* getCharExpr(string s)
{
char *a=new char[s.size()+1];
a[s.size()]=0;
memcpy(a,s.c_str(),s.size());
return a;
}
class Bicomponent {
private:
std::set<int> memberNodes;
std::set<int>::iterator nextIter(std::set<int>::iterator iter) {
return ++iter;
}
public:
Bicomponent(std::set<int> mn) {
memberNodes = mn;
} //constructor
};
int searchList (SList <edge> S,edge e)
{
int x = 0;
for(SListIterator <edge> i = S.begin(); i.valid(); ++i, ++x)
{
if(*i == e)
{
return x;
}
}
return -1;
}
string getTypeString(node &n, StaticSPQRTree &s) {
std::string res = "unkown";
int type = s.typeOf(n);
switch (type) {
case 0:
res = "S";
break;
case 1:
res = "P";
break;
case 2:
res = "R";
break;
}
return res;
}
void write_dot(Graph G, map<int,int> sk2origin, string file,Skeleton &sk)
{
ofstream of(getCharExpr(file));
of << "digraph {"<<endl;
edge e;
forall_edges(e,G)
{
if(!sk.isVirtual(e))
{
int source = sk2origin[e->source()->index()];
int target = sk2origin[e->target()->index()];
of <<"\t"<<source<<"->"<<target<<endl;
}
}
of<<"}";
}
void getCutVertexPair(const GraphCopy &GC, node bcTreeNode,BCTree &bc, int CC, \
Bicomponent &bicomp, \
int maxBNodeSize=30, int minBNodeSize=3) {
node n1,n2;
edge in1,in2,out1,out2;
if (bc.typeOfBNode(bcTreeNode) != 0) // Check if we're dealing with B-node
return ;
Graph bcT = bc.bcTree(); // the BT-Tree
List<edge> incoming, outgoing; // Edge lists
bcT.inEdges(bcTreeNode, incoming); // Get all incoming edges into BCTreeNode
bcT.outEdges(bcTreeNode, outgoing); // Get all outgoing edges out of BCTreeNode
if (incoming.size() + outgoing.size() == 2) {
if (incoming.size() == 2){
in1 = incoming.front();
in2 = incoming.back();
n1 = bc.cutVertex(in1->source(),in1->source());
n2 = bc.cutVertex(in2->source(),in2->source());
}
else if (outgoing.size() == 2) {
out1 = outgoing.front();
out2 = outgoing.back();
n1 = bc.cutVertex(out1->target(),out1->target());
n2 = bc.cutVertex(out1->target(),out1->target());
}
else {
out1 = outgoing.front();
in1 = incoming.front();
n1 = bc.cutVertex(out1->target(),out1->target());
n2 = bc.cutVertex(in1->source(),in1->source());
}
if (n1 && n2) {
n1 = bc.original(GC.original(n1));
n2 = bc.original(GC.original(n2));
pairs.push_back(make_pair(n1->index(), n2->index()));
}
}
}
struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1,T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
// Mainly for demonstration purposes, i.e. works but is overly simple
// In the real world, use sth. like boost.hash_combine
return h1 ^ h2;
}
};
void findTwoVertexCuts(Bicomponent &bicomp, Skeleton &sk, unordered_map<int,int> sk2orig, std::string type)
{
const Graph &G = sk.getGraph();
int virtualCount;
edge e;
node n1;
const int nrNodes = G.numberOfNodes();
//cout<<"Number of nodes = "<<nrNodes<<endl;
int allnodes[nrNodes];
int count = 0;
forall_nodes(n1, G) {
allnodes[count] = sk2orig[n1->index()];
count++;
}
//cout<<"Done"<<endl;
if (type == "R") {
//cout<<"R"<<endl;
//A virtual edge in an R node represents a two vertex cut
forall_edges(e,G) {
if (sk.isVirtual(e))
pairs.push_back(make_pair(sk2orig[e->source()->index()], sk2orig[e->target()->index()]));
} //forall edges
}//if
else if (type == "P") {
//Node associated with p-nodes with two or more virtual edges are 2-vertex cuts
//cout<<"P"<<endl;
virtualCount = 0;
forall_edges(e,G) {
if (sk.isVirtual(e)) {
virtualCount++;
if (virtualCount > 1) {
pairs.push_back(make_pair(sk2orig[e->source()->index()], sk2orig[e->target()->index()]));
break;
}//if
}//if
}//forall_edges
}//else if
else if (type == "S")
{
//cout<<"S"<<endl;
// A virtual edge in an S node represents a 2-vertex cuts
unordered_map<pair<int,int>, bool, pair_hash > adjacent;
forall_edges(e,G) {
if (sk.isVirtual(e))
pairs.push_back(make_pair(sk2orig[e->source()->index()], sk2orig[e->target()->index()]));
else
adjacent[make_pair(sk2orig[e->source()->index()], sk2orig[e->target()->index()])] = true;
adjacent[make_pair(sk2orig[e->target()->index()], sk2orig[e->source()->index()])] = true;
} //forall edges
// All non-adjacent nodes in an S-node are cut-vertices
for (int i = 0; i < nrNodes-1; i++)
for(int j = i+1; j < nrNodes; j++)
if(adjacent.find(make_pair(allnodes[i], allnodes[j])) == adjacent.end() or adjacent.find(make_pair(allnodes[j], allnodes[i])) == adjacent.end())
pairs.push_back(make_pair(allnodes[i], allnodes[j]));
}//else if
//cout<<pairs.size()<<endl;
} //getTwoVertexCuts
std::set<int> getBiComponent(GraphCopy *GC, BCTree *p_bct, node bcTreeNode)
{
node n;
edge e;
std::set<int> memberNodes; // Members of the N-node
const Graph &auxGraph = p_bct->auxiliaryGraph();
//GraphCopy GC(auxGraph); //copy of original
SList <edge> componentEdges = p_bct->hEdges(bcTreeNode); //edges in component bcTreeNode
forall_edges (e, auxGraph) { //Check if edge belongs to component
//cerr << "Testing edge " << e << endl;
if (searchList(componentEdges,e) == -1) { //If not, delete edge from copy
GC->delEdge(GC->copy(e));
}
}
forall_nodes(n, auxGraph)
{ //Delete nodes without edges
if (!GC->copy(n)->degree())
{
//cerr << "Deleting node: " << n->index() << endl;
GC->delNode(GC->copy(n));
} //if
else
{
int index = p_bct->original(n)->index();
memberNodes.insert(index);
} //else
}// forall_nodes
return memberNodes;
}
node original(node &n, BCTree &bc, const GraphCopy &GC, Skeleton &sk)
{
node np;
np = bc.original(GC.original(sk.original(n)));
return np;
}
int main(int argc, char* argv[])
{
cmdline ::parser pr;
pr.add<string>("oriented_graph",'l',"list of oriented links",true,"");
pr.add<string>("output",'o',"output file tow write sep pairs",true,"");
pr.parse_check(argc,argv);
Graph G;
ifstream linkfile(getCharExpr(pr.get<string>("oriented_graph")));
ofstream ofile(getCharExpr(pr.get<string>("output")));
string line;
//unordered_map<int, Link> linkmap;
unordered_map<string,node> revid2contig;
int contig_id = 1, linkid = 0;
while(getline(linkfile,line))
{
//cout<<line<<endl;
string a,b,c,d;
double e,f;
int g;
istringstream iss(line);
if(!(iss >> a >> b >> c >> d >> e >> f >> g))
break;
//Link l(linkid,a,b,c,d,e,f,g);
//Link l(linkid,a,b,c,d,e,f);
node first = 0, second = 0;
if(revid2contig.find(a) == revid2contig.end())
{
first = G.newNode(contig_id);
id2contig[first] = a;
intid2contig[contig_id] = a;
revid2contig[a] = first;
contig_id++;
}
if(revid2contig.find(c) == revid2contig.end())
{
second = G.newNode(contig_id);
intid2contig[contig_id] = c;
revid2contig[c] = second;
id2contig[second] = c;
contig_id++;
}
// cout<<first<<"\t"<<second<<endl;
// G.newEdge((node)first,(node)second);
// cout<<"edge added"<<endl;
//contigs2bundle[a+c] = g;
}
linkfile.close();
//cout<<"Nodes: "<<G.numberOfNodes()<<endl;
ifstream linkfile1(getCharExpr(pr.get<string>("oriented_graph")));
while(getline(linkfile1,line))
{
//cout<<line<<endl;
string a,b,c,d;
double e,f;
int g;
istringstream iss(line);
if(!(iss >> a >> b >> c >> d >> e >> f >> g))
break;
//Link l(linkid,a,b,c,d,e,f,g);
//Link l(linkid,a,b,c,d,e,f);
node first = revid2contig[a];
node second = revid2contig[c];
//cout<<first<<"\t"<<second<<endl;
edge x = G.newEdge(node(first),node(second));
//cout<<"edge added"<<endl;
//contigs2bundle[a+c] = g;
}
// GraphAttributes GA(G, GraphAttributes::nodeId);
// bool ok = GraphIO::readGML(GA,G,"test_graph/oriented.gml");
//since this is giving an error, lets just read tsv file and construct graph ourself
// GraphIO::writeDOT(G,"tmp/original.dot");
// cout<<ok<<endl;
// if(ok)
// {
// cout<<"Graph loaded correctly!"<<endl;
// }
// else
// {
// cout<<"Graph loaded incorrectly!"<<endl;
// }
//decompose into connected components
int nrCC = 0;
NodeArray<int> node2cc(G);
nrCC = connectedComponents(G, node2cc);
//cerr<<"Number of connected components = "<<nrCC<<endl;
node startNodes[nrCC];
int index = 0;
node n;
forall_nodes(n, G)
{
if (node2cc[n] == index)
{
startNodes[index] = n;
index++;
}
if (index == nrCC)
break;
}
set<int> memberNodes;
unordered_map<int,int> sk2orig; // node mapping
//Building BC tree for each component
Graph G_new;
int new_node_index = 1;
map<int,vector<node> > nodemapping;
for(int j = 0;j < nrCC; j++)
{
BCTree bc(G,startNodes[j]);
BCTree *p_bct = &bc;
//cerr<<"Number of Biconnected Components = "<<bc.numberOfBComps()<<endl;
if(bc.numberOfBComps() == 0)
{
continue;
//do some special processing here
}
//Now, for each Biconnected Component, build SPQR tree
//Connected Components in auxgraph are the biconnected components of original graph
const Graph &auxgraph = p_bct->auxiliaryGraph();
//cerr<<"graph made"<<endl;
node bcTreeNode;
forall_nodes(bcTreeNode,bc.bcTree())
{
if(bc.typeOfBNode(bcTreeNode) == 0)
{
GraphCopy GC(p_bct->auxiliaryGraph());
memberNodes = getBiComponent(&GC,p_bct,bcTreeNode);
//cerr<<memberNodes.size()<<endl;
Bicomponent bicomp(memberNodes);
//cer<<"membernodes found"<<endl;
//Now Generate SPQR tree for this component
bool biconnected = isBiconnected(GC);
int nrEdges = GC.numberOfEdges();
bool loopfree = isLoopFree(GC);
if(!biconnected || nrEdges <= 2 || !loopfree)
{
continue;
// cerr << "Graph is not a valid input for SPQR-tree decomposition!" << endl;
// cerr << "Reason(s):" << endl;
// if (!biconnected)
// cerr << "-> Graph is not biconnected" << endl;
// if (nrEdges <= 2)
// cerr << "-> Graph has "<< nrEdges << " edge(s). Should be more than 2." << endl;
// if (!loopfree)
// cerr << "-> Graph is not loop free" << endl;
}
getCutVertexPair(GC,bcTreeNode,bc,j,bicomp);
StaticSPQRTree spqr(GC);
//cout<<"SPQR generated"<<endl;
const Graph &T = spqr.tree();
//cout<<"SPQR tree made"<<endl;
GraphIO::writeDOT(T,"tmp/spqr.dot");
// cout<<"S nodes: "<<spqr.numberOfSNodes()<<endl;
// cout<<"P nodes: "<<spqr.numberOfPNodes()<<endl;
// cout<<"R nodes: "<<spqr.numberOfRNodes()<<endl;
int c = 0;
GraphCopy GCopy(T);
node n,Nn,cn;
forall_nodes(n, T)
{
const Graph &Gn = spqr.skeleton(n).getGraph(); // Print the skeleton of a tree node to dis
// Generate hash table: sk2orig[Skeleton node] = Original node
forall_nodes(Nn, Gn)
{
cn = original(Nn,bc,GC,spqr.skeleton(n)); //Node in original graph G
sk2orig[Nn->index()] = cn->index();
}
string type = getTypeString(n, spqr);
//Get 2-vertex cuts
findTwoVertexCuts(bicomp,spqr.skeleton(n) , sk2orig, type);
}
for(int i = 0;i < pairs.size();i++)
{
ofile<<intid2contig[pairs[i].first]<<"\t"<<intid2contig[pairs[i].second];
for(set<int> :: iterator it = memberNodes.begin(); it != memberNodes.end();++it)
{
ofile<<"\t"<<intid2contig[*it];
}
ofile<<endl;
}
pairs.clear();
}
}
}
//add edges in this new graph based on original graph
return 0;
}