-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembly_2.cpp
More file actions
431 lines (364 loc) · 14.7 KB
/
assembly_2.cpp
File metadata and controls
431 lines (364 loc) · 14.7 KB
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
#include <graphlab.hpp>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
struct read_data
{
int length;
std::string content;
int offset;
double score;
int end;
int next_id;
// std::list<int> parent_list;
bool valid;
read_data(): length(0), content(""), offset(0), score(0), end(0), next_id(0), valid(false){ }
explicit read_data(int length, std::string content, int offset, double score, int end, int next_id, bool valid) : length(length), content(content), offset(offset), score(score), end(end), valid(valid) { }
void save(graphlab::oarchive& oarc) const {
oarc << length << content << offset << score << end << next_id << valid;
}
void load(graphlab::iarchive& iarc) {
iarc >> length >> content >> offset >> score >> end >> next_id >> valid;
}
};
// struct edge_data
// {
// double weight;
// edge_data(double weight) : weight(weight) { }
// };
// The type of graph used in this program
typedef graphlab::distributed_graph<read_data, graphlab::empty> graph_type;
//Create graph line by line
//Input file format: ReadID, Read Length, Read, Match Position, Match Score, <edges>
// bool line_parser(graph_type& graph, const std::string& filename, const std::string& textline) {
// std::stringstream strm(textline);
// graphlab::vertex_id_type vid;
// int length;
// std::string content;
// int offset;
// double score;
// // first entry in the line is a vertex ID
// strm >> vid;
// strm >> length;
// strm >> content;
// strm >> offset;
// strm >> score;
// int end = offset + length;
// // insert this read
// graph.add_vertex(vid, read_data(length, content, offset, score, end, true));
// // while there are elements in the line, continue to read until we fail
// while(1){
// graphlab::vertex_id_type other_vid;
// strm >> other_vid;
// if (strm.fail())
// return false;
// graph.add_edge(vid, other_vid);
// }
// return true;
// }
bool load_graph(const std::string& filename, graph_type& graph) {
std::ifstream fin(filename.c_str());
std::cout << "Load " << filename << " now ... " << std::endl;
if(!fin.good()) return false;
std::string textline;
if (fin.is_open()) {
std::cout << "File is open! " << std::endl;
graphlab::vertex_id_type vid;
// graphlab::vertex_id_type last_vid = 0;
while (std::getline(fin, textline)) {
std::cout << "Read a line. " << std::endl;
std::stringstream strm(textline);
int length;
std::string content;
int offset;
double score;
//double dist;
// first entry in the line is a vertex ID
strm >> vid;
strm >> length;
strm >> content;
strm >> offset;
strm >> score;
int end = offset + length;
std::cout << "textline: " << textline << std::endl;
// insert this read
if(graph.add_vertex(vid, read_data(length, content, offset, score, end, true)))
std::cout << "add_vertex success!" << std::endl;
// if(graph.contains_vertex(vid)) {std::cout << "gaph contains the vid!!!!!" << std::endl;}
// if(graph.num_vertices() > 0) {std::cout << last_vid << " not empty" << std::endl;}
// std::cout << "xxxxx " << graph.vertex(vid).id() << std::endl;
std::cout << "vertex id: " << vid << ", score: " << score << std::endl;
// while there are elements in the line, continue to read until we fail
while(1){
graphlab::vertex_id_type other_vid;
strm >> other_vid;
std::cout << "xxxxxxxxx: " << other_vid << std::endl;
if (strm.fail())
break;
std::cout << "edge: (" << vid << ", " << other_vid << ")" << std::endl;
graph.add_edge(vid, other_vid);
}
// std::cout << "check last_vid..." << std::endl;
// std::cout << "last_vid: " << last_vid << std::endl;
// if (last_vid != 0){
// std::cout << "xxxxx!" <<std::endl;
// if(graph.num_vertices() > 0) {std::cout << last_vid << " not empty" << std::endl;}
// std::cout << graph.vertex(last_vid).id() << std::endl;
// std::cout << "last_vid is not zero! out edges = " << graph.vertex(last_vid).num_out_edges() << std::endl;
// if (graph.vertex(last_vid).num_out_edges() == 0)
// graph.add_edge(last_vid,vid);
// std::cout << "edge: (" << last_vid << ", " << vid << ")" << std::endl;
// }
// last_vid = vid;
}
}
return true;
}
// Get the other vertex in the edge.
graph_type::vertex_type get_other_vertex(const graph_type::edge_type& edge,
const graph_type::vertex_type& vertex) {
return vertex.id() == edge.source().id()? edge.target() : edge.source();
}
// Exempt reads that are leaves but are not the last read
class exempt_reads_program : public graphlab::ivertex_program<graph_type, graphlab::empty, graphlab::vertex_id_type>,
public graphlab::IS_POD_TYPE{
graphlab::vertex_id_type last_id;
public:
void init(icontext_type& context, const vertex_type& vertex, const graphlab::vertex_id_type& msg) {
//std::cout<<"EXEMPT_INI"<<std::endl;
last_id = msg;
}
edge_dir_type gather_edges (icontext_type& context, const vertex_type& vertex) const {
//std::cout << "EXEMPT_GARTHER" <<std::endl;
return graphlab:: NO_EDGES;
}
void apply(icontext_type& context, vertex_type& vertex, const graphlab::empty& empty){
// if a vertex do not have a child and is not the last vertex, then tag it as an invalid vertex.
//std::cout << "Exempt_apply" << std::endl;
//std::cout << "Exempt_ID: " << vertex.id() << std::endl;
if (vertex.num_out_edges() == 0 && vertex.id() != last_id) {
vertex.data().valid = false;
}
}
edge_dir_type scatter_edges(icontext_type& context, const vertex_type& vertex) const {
//std::cout << "EXEmpt_scatter"<< std::endl;
return graphlab::NO_EDGES;
} // end of scatter_edge
};
// gather type
struct id_and_score
{
std::vector<graphlab::vertex_id_type> ids;
std::vector<double> scores;
id_and_score(): ids(),scores() { }
id_and_score(graphlab::vertex_id_type id, double score): ids(), scores() {
ids.push_back(id);
scores.push_back(score);
}
id_and_score& operator += (const id_and_score& other) {
for (size_t i = 0; i < other.ids.size(); ++i) {
ids.push_back(other.ids[i]);
scores.push_back(other.scores[i]);
}
return *this;
}
void save(graphlab::oarchive& oarc) const {
size_t num = ids.size();
oarc << num;
for (size_t a = 0; a < num; ++a)
oarc << ids[a] << scores[a];
}
void load(graphlab::iarchive& iarc) {
ids.clear();
scores.clear();
size_t num = 0;
iarc >> num;
for (size_t a = 0; a < num; ++a) {
size_t id = 0;
double score = 0;
iarc >> id;
ids.push_back(id);
iarc >> score;
scores.push_back(score);
}
}
};
// Message Type
// store the vertex id with max score
struct score_message : public graphlab::IS_POD_TYPE {
graphlab::vertex_id_type id;
double score;
score_message():id(0),score(0) { }
score_message(graphlab::vertex_id_type id, double score) : id(id), score(score) { }
score_message& operator += (const score_message& other) {
id = other.id;
score = other.score;
return *this;
}
void save(graphlab::oarchive& oarc) const {
oarc << id << score;
}
void load(graphlab::iarchive& iarc) {
iarc >> id >> score;
}
};
// Find the children with max score for every valid vertex
class find_max_children : public graphlab::ivertex_program<graph_type, id_and_score, score_message>,
public graphlab::IS_POD_TYPE {
// this is a local copy of the message
double max_score;
graphlab::vertex_id_type max_id;
public:
void init(icontext_type& context, const vertex_type& vertex, const score_message& msg) {
//std::cout << "FIND_ININ" << std::endl;
max_score = msg.score;
max_id = msg.id;
}
edge_dir_type gather_edges (icontext_type& context, const vertex_type& vertex) const {
//std::cout << "FIND_GARTHER" <<std::endl;
return graphlab:: OUT_EDGES;
}
id_and_score gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
//std::cout << "FIND_GARTHER2" <<std::endl;
return id_and_score(edge.target().id(), edge.target().data().score);
}
//get values and make a vector
void apply(icontext_type& context, vertex_type& vertex, const id_and_score& total) {
std::cout << "The id now is: " << vertex.id() <<std::endl;
const std::vector<graphlab::vertex_id_type>& ids = total.ids;
// std::cout << "Here is wrong!" << std::endl;
const std::vector<double>& scores = total.scores;
// std::cout << "Here 2" << std::endl;
// std::cout << "socres.size is: " << scores.size() << std::endl;
if(scores.size()==0){
std::cout << "This is Leaf now " <<std::endl;
return;
}
max_score = scores[0];
//std::cout << "score[0] " << std::endl;
max_id = ids[0];
//std::cout << "FIND_Apply"<<std::endl;
//std::cout << "SIZE: " << ids.size() << std::endl;
// if(ids.size()==0){
// std::cout << "This is Leaf now " <<std::endl;
// return;
// }
for (size_t i = 0; i < ids.size(); ++i) {
std::cout << "find_here1" <<std::endl;
std::cout << "score for " << i << "is: " << scores[i] << std::endl;
std::cout << "max score is: " << max_score << std::endl;
if (scores[i] > max_score) {
std::cout << "find_HERE" <<std::endl;
max_score = scores[i];
max_id = ids[i];
std::cout << "MAX_ID:" <<max_id <<std::endl;
}
}
vertex.data().next_id = max_id
}
edge_dir_type scatter_edges(icontext_type& context, const vertex_type& vertex) const {
return graphlab::OUT_EDGES;
}
void scatter (icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
const score_message msg(max_score,max_id);
if (edge.target().id() != msg.id) {
edge.target().data().valid = false;
}
else{
context.signal(edge.target(),msg);
}
}
};
/**
* \brief We want to save the final graph so we define a write which will be
* used in graph.save("path/prefix", save_vertex) to save the graph.
*/
struct best_path_writer {
std::string save_vertex(const graph_type::vertex_type& vtx) {
std::stringstream strm;
strm << vtx.id() << "\t" << vtx.data().score << vtx.data().next_id << "\n";
return strm.str();
}
std::string save_edge(graph_type::edge_type e) {
std::stringstream strm;
strm << "(" << e.source().id() << "\t" << e.target().id() << ")" << "\n";
return strm.str();
}
//std::string save_edge(graph_type::edge_type e) { return ""; }
};
int main(int argc, char** argv) {
// Initialize control plain using mpi
graphlab::mpi_tools::init(argc, argv);
graphlab::distributed_control dc;
global_logger().set_log_level(LOG_INFO);
global_logger().set_log_to_console(true);
logger(LOG_INFO, "Assembly starting...\n");
std::string infile;
std::string outfile;
graphlab::vertex_id_type source;
graphlab::vertex_id_type destination;
// Parse command line options -----------------------------------------------
graphlab::command_line_options clopts("Welcome to assembly reads!");
clopts.attach_option("infile", infile, "The aligned reads filename (required)");
clopts.add_positional("infile");
clopts.attach_option("outfile", outfile, "The filename for save the assembly results (required)");
clopts.add_positional("outfile");
// These two parameters should not be inputed by user in the future.
clopts.attach_option("source", source, "The first reads of the sequence");
clopts.attach_option("destination", destination, "The last reads of the sequence");
clopts.print_description();
std::cout << infile << " " << outfile << " " << source << " " << destination << std::endl;
if(!clopts.parse(argc, argv)) {
dc.cout() << "Error in parsing command line arguments. \n";
return EXIT_FAILURE;
}
// Build the graph ----------------------------------------------------------
graph_type graph(dc, clopts);
if(!clopts.is_set("infile")) {
dc.cout() << "Input file not provided. \n";
return EXIT_FAILURE;
}
if(!clopts.is_set("outfile")) {
dc.cout() << "Output file not provided. \n";
return EXIT_FAILURE;
}
if(!clopts.is_set("source")) {
dc.cout() << "The first read of the sequence not provided. \n";
return EXIT_FAILURE;
}
if(!clopts.is_set("source")) {
dc.cout() << "The last read of the sequence not provided. \n";
return EXIT_FAILURE;
}
//graph.load(infile, line_parser);
std::cout << infile << " " << outfile << " " << source << " " << destination << std::endl;
if (!load_graph(infile, graph)) {
dc.cout() << "Cannot load the graph. \n";
return EXIT_FAILURE;
}
graph.finalize();
// Running The Engine1 for exempt_reads_program -------------------------------------------------------
dc.cout() << "Start to EXEMPT" <<std::endl;
graphlab::omni_engine<exempt_reads_program> engine1(dc, graph, "synchronous", clopts);
engine1.signal(source, destination);
engine1.start();
const float runtime1 = engine1.elapsed_seconds();
dc.cout() << "Finished Running engine1 in " << runtime1 << " seconds." << std::endl;
// Running The Engine2 for find_max_children
dc.cout() << "Start to FIND" <<std::endl;
graphlab::omni_engine<find_max_children> engine2(dc, graph, "synchronous", clopts);
engine2.signal_all();
// engine2.signal(source, score_message(source, 0));
engine2.start();
const float runtime2 = engine2.elapsed_seconds();
dc.cout() << "Finished Running engine2 in " << runtime2 << " seconds." << std::endl;
// Save the final graph
graph.save(outfile, best_path_writer(),
false, // do not gzip
true, // save vertices
// false); // do not save edges
true); //save edges
graphlab::mpi_tools::finalize();
return EXIT_SUCCESS;
}