-
Notifications
You must be signed in to change notification settings - Fork 15
/
bundler.cpp
390 lines (349 loc) · 12.7 KB
/
bundler.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
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include "cmdline/cmdline.h"
using namespace std;
class Link
{
private:
int id;
string contig_a;
string contig_a_orientation;
string contig_b;
string contig_b_orientation;
int bundle_size;
double mean;
double stdev;
public:
Link() {};
Link(int id, string contig_a, string contig_a_orientation, string contig_b, string contig_b_orientation, double mean, double stdev);
double getmean();
double getstdev();
string getlinkorientation();
string getcontigs();
string getfirstcontig();
string getsecondcontig();
string getfirstorietation();
string getsecondorientation();
void set_bundle_size(int size);
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)
{
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;
}
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;
}
void Link :: set_bundle_size(int size)
{
this->bundle_size = size;
}
int Link :: get_bundle_size()
{
return this->bundle_size;
}
int Link :: getid()
{
return this->id;
}
bool pairCompare(const std::pair<int, int>& firstElem, const std::pair<int, int>& secondElem) {
return firstElem.second < secondElem.second;
}
char* getCharExpr(string s)
{
char *a=new char[s.size()+1];
a[s.size()]=0;
memcpy(a,s.c_str(),s.size());
return a;
}
int main(int argc, char* argv[])
{
cmdline ::parser pr;
pr.add<string>("contigs",'l',"contig links",true,"");
pr.add<string>("output",'o',"output file",true,"");
pr.add<string>("bgraph",'b',"bundled graph in gml format",true,"");
pr.add<int>("cutoff",'c',"number of mate pairs to support an edge",false,3);
pr.parse_check(argc,argv);
ifstream linkfile(getCharExpr(pr.get<string>("contigs")));
ofstream ofile(getCharExpr(pr.get<string>("output")));
ofstream g(getCharExpr(pr.get<string>("bgraph")));
string line;
int linkid = 1;
int cutoff = pr.get<int>("cutoff");
map<int, Link> linkmap;
while(getline(linkfile,line))
{
string a,b,c,d;
double e,f;
istringstream iss(line);
if(!(iss >> a >> b >> c >> d >> e >> f))
break;
Link l(linkid,a,b,c,d,e,f);
linkmap[linkid] = l;
linkid++;
}
//Store links for a pair of contigs and orientation. For each possible pair, there can be 4 orientations
map<int, Link> :: iterator it;
//first map: contig-> second_map, second_map: orientation->links
map<string, map<string, vector<Link> > > contig_to_links;
for(it = linkmap.begin(); it!= linkmap.end(); ++it)
{
Link link = it->second;
string contigs = link.getcontigs();
string orientations = link.getlinkorientation();
if(contig_to_links.find(contigs) == contig_to_links.end())
{
map<string, vector<Link> > orientation_to_links;
if(orientation_to_links.find(orientations) == orientation_to_links.end())
{
vector<Link> links;
links.push_back(link);
orientation_to_links[orientations] = links;
}
contig_to_links[contigs] = orientation_to_links;
}
else
{
map<string, vector<Link> > orientation_to_links = contig_to_links[contigs];
if(orientation_to_links.find(orientations) == orientation_to_links.end())
{
vector<Link> links;
links.push_back(link);
orientation_to_links[orientations] = links;
}
else
{
vector<Link> links = orientation_to_links[orientations];
links.push_back(link);
orientation_to_links[orientations] = links;
}
contig_to_links[contigs] = orientation_to_links;
}
}
//cerr<<"Links loaded and stored by orientation"<<endl;
//code to test if link are properly loaded
for(map<string, map<string, vector<Link> > > :: iterator it = contig_to_links.begin(); it != contig_to_links.end(); ++it)
{
//cout<<it->first<<endl;
map<string, vector<Link> > olinks = it->second;
for(map<string, vector<Link> > :: iterator it1 = olinks.begin(); it1 != olinks.end(); ++it1)
{
//cout<<"\t"<<it1->first<<endl;
vector<Link> l = it1->second;
for(int i = 0; i < l.size();i++)
{
Link link = l[i];
//cout<<"\t\t"<<link.getcontigs()<<endl;
}
}
}
//For each pair of contig, for each possible orientation apply maximal clique algorithm and compress links to 1
vector<Link> bundled_links;
map<string, map<string, vector<Link> > > :: iterator linkit;
for(linkit = contig_to_links.begin(); linkit != contig_to_links.end();++linkit)
{
map<string, vector<Link> > orientation_to_links = linkit->second;
map<string, vector<Link> > :: iterator it1;
for(it1 = orientation_to_links.begin(); it1 != orientation_to_links.end();it1++)
{
string orientation = it1->first;
vector<Link> links = it1->second;
//Apply clique algorithm only if number of link with same orientation is more than cutoff
if(links.size() > cutoff)
{
vector< pair<int,double> > begins;
vector< pair<int,double> > ends;
for(int i = 0;i < links.size();i++)
{
Link link = links[i];
double mean = link.getmean();
double stdev = link.getstdev();
begins.push_back(make_pair(link.getid(), mean - 3*stdev));
ends.push_back(make_pair(link.getid(),mean + 3* stdev));
}
//sort begins and ends in increasing order
sort(begins.begin(),begins.end(),pairCompare);
sort(ends.begin(),ends.end(),pairCompare);
int start_index = 0;
int end_index = 0;
int curr_clique = 0, best_clique = 0;
double best_coord = -100000;
vector<Link> clique_links;
double begin_left, begin_right, end_left, end_right;
int printed = 0;
while(start_index < begins.size() && end_index < ends.size())
{
if(start_index < begins.size() - 1 && begins[start_index].second <= ends[end_index].second)
{
int linkno = begins[start_index].first;
Link curlink = linkmap[linkno];
begin_left = curlink.getmean() - 3*curlink.getstdev();
begin_right = curlink.getmean() + 3*curlink.getstdev();
//cout<<begin_right<<endl;
curr_clique++;
//cout<<"here"<<endl;
if (curr_clique > best_clique)
{
best_clique = curr_clique;
clique_links.clear();
best_coord = begin_left;
}
start_index++;
}
else
{
if((end_index < ends.size()) && ((start_index == begins.size() - 1 || (begins[start_index].second > ends[end_index].second))))
{
//cout<<"here"<<endl;
int linkno = ends[end_index].first;
Link curlink = linkmap[linkno];
end_left = curlink.getmean() - 3*curlink.getstdev();
end_right = curlink.getmean() + 3*curlink.getstdev();
if(end_left <= best_coord && end_right >= best_coord)
{
clique_links.push_back(linkmap[ends[end_index].first]);
//cout<<"adding link"<<endl;
}
curr_clique--;
end_index++;
}
}
}
//cerr<<"Clique Done"<<endl;
//cerr<<best_clique<<endl;
if(clique_links.size() != 0)
{
double min_range = clique_links[0].getmean() - 3*clique_links[0].getstdev();
double max_range = clique_links[0].getmean() + 3*clique_links[0].getstdev();
for(int i = 1; i < clique_links.size();i++)
{
Link link = clique_links[i];
double mean = link.getmean();
double stdev = link.getstdev();
if(mean - 3*stdev > min_range)
min_range = mean - 3*stdev;
if(mean + 3*stdev < max_range)
max_range = mean + 3*stdev;
}
//write code to log invalid links
double newmean, newsd, p = 0,q = 0;
for(int i = 0;i < clique_links.size();i++)
{
Link link = clique_links[i];
double tmp = link.getstdev();
if(tmp == 0)
tmp = 1;
tmp = tmp*tmp;
p += link.getmean()*1.0/tmp;
q += 1.0/tmp;
}
newmean = p/q;
newsd = 1/sqrt(q);
//cout<<clique_links.size()<<endl;
Link templink = clique_links[0];
Link newlink(0, templink.getfirstcontig(), templink.getfirstorietation(), templink.getsecondcontig(), templink.getsecondorientation(),
newmean, newsd);
//cout<<clique_links.size()<<endl;
newlink.set_bundle_size(clique_links.size());
bundled_links.push_back(newlink);
}
}
else
{
links[0].set_bundle_size(1);
bundled_links.push_back(links[0]);
}
}
}
int nodeid = 1;
map<string,int> contig2node;
for(int i = 0;i < bundled_links.size();i++)
{
Link l = bundled_links[i];
string contiga = l.getfirstcontig();
string contigb = l.getsecondcontig();
if(contig2node.find(contiga) == contig2node.end())
{
contig2node[contiga] = nodeid;
nodeid++;
}
if(contig2node.find(contigb) == contig2node.end())
{
contig2node[contigb] = nodeid;
nodeid++;
}
}
g <<"graph ["<<endl;
g <<" directed 1"<<endl;
for(map<string,int> :: iterator it = contig2node.begin(); it != contig2node.end(); ++it)
{
g<<" node ["<<endl;
g<<" id "<<it->second<<endl;
g<<" label \""<<it->first<<"\""<<endl;
g<<" ]"<<endl;
}
for(int i = 0;i < bundled_links.size();i++)
{
Link l = bundled_links[i];
if (l.get_bundle_size() >= cutoff)
{
g<<" edge ["<<endl;
g<<" source "<<contig2node[l.getfirstcontig()]<<endl;
g<<" target "<<contig2node[l.getsecondcontig()]<<endl;
g<<" mean "<<l.getmean()<<endl;
g<<" stdev "<<l.getstdev()<<endl;
g<<" bsize "<<l.get_bundle_size()<<endl;
g<<" ]"<<endl;
}
}
g<<"]";
for(int i = 0;i < bundled_links.size();i++)
{
Link l = bundled_links[i];
if (l.get_bundle_size() >= cutoff)
ofile<<l.getfirstcontig()<<"\t"<<l.getfirstorietation()<<"\t"<<l.getsecondcontig()<<"\t"<<l.getsecondorientation()<<"\t"<<l.getmean()<<"\t"<<l.getstdev()<<"\t"<<l.get_bundle_size()<<endl;
}
//write code to dump to gml file
return 0;
}