Skip to content

Commit

Permalink
Merge pull request #299 from pangenome/id-increment-fix
Browse files Browse the repository at this point in the history
Id increment fix
  • Loading branch information
ekg authored Jul 15, 2021
2 parents c1173ad + c761c36 commit 0f9de83
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/gfa_to_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ std::map<char, uint64_t> gfa_line_counts(const char* filename) {

void gfa_to_handle(const string& gfa_filename,
handlegraph::MutablePathMutableHandleGraph* graph,
bool compact_ids,
uint64_t n_threads,
bool progress) {

Expand All @@ -52,12 +53,10 @@ void gfa_to_handle(const string& gfa_filename,
line_counts = gfa_line_counts(filename);
x.join();
}
uint64_t id_increment = min_id - 1;
uint64_t id_increment = (compact_ids ? min_id - 1 : 0);
uint64_t node_count = line_counts['S'];
uint64_t edge_count = line_counts['L'];
uint64_t path_count = line_counts['P'];
// set the min id as an offset
graph->set_id_increment(id_increment);
// build the nodes
{
std::unique_ptr<algorithms::progress_meter::ProgressMeter> progress_meter;
Expand Down Expand Up @@ -87,8 +86,8 @@ void gfa_to_handle(const string& gfa_filename,
filename,
[&](gfak::edge_elem e) {
if (e.source_name.empty()) return;
handlegraph::handle_t a = graph->get_handle(stol(e.source_name), !e.source_orientation_forward);
handlegraph::handle_t b = graph->get_handle(stol(e.sink_name), !e.sink_orientation_forward);
handlegraph::handle_t a = graph->get_handle(stol(e.source_name) - id_increment, !e.source_orientation_forward);
handlegraph::handle_t b = graph->get_handle(stol(e.sink_name) - id_increment, !e.sink_orientation_forward);
graph->create_edge(a, b);
if (progress) progress_meter->increment(1);
});
Expand All @@ -115,7 +114,7 @@ void gfa_to_handle(const string& gfa_filename,
uint64_t i = 0;
for (auto& s : p->gfak.segment_names) {
graph->append_step(p->path,
graph->get_handle(std::stoi(s),
graph->get_handle(std::stoi(s) - id_increment,
// in gfak, true == +
!p->gfak.orientations[i++]));
}
Expand Down Expand Up @@ -153,5 +152,10 @@ void gfa_to_handle(const string& gfa_filename,
progress_meter->finish();
}
}

if (compact_ids) {
graph->optimize();
}

}
}
5 changes: 3 additions & 2 deletions src/gfa_to_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ std::map<char, uint64_t> gfa_line_counts(const char* filename);
/// Handle graph must be empty when passed into function.
void gfa_to_handle(const string& gfa_filename,
handlegraph::MutablePathMutableHandleGraph* graph,
uint64_t n_threads = 1,
bool show_progress = false);
bool compact_ids,
uint64_t n_threads,
bool show_progress);

}
3 changes: 2 additions & 1 deletion src/subcommand/build_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ int main_build(int argc, char** argv) {
args::Group graph_files_io(parser, "[ Graph Files IO ]");
args::Flag to_gfa(graph_files_io, "to_gfa", "Write the graph to stdout in GFAv1 format.", {'G', "to-gfa"});
args::Group graph_sorting(parser, "[ Graph Sorting ]");
args::Flag optimize(graph_sorting, "optimize", "Compact the graph id space into a dense integer range.", {'O', "optimize"});
args::Flag toposort(graph_sorting, "sort", "Apply a general topological sort to the graph and order the node ids"
" accordingly. A bidirected adaptation of Kahn’s topological sort (1962)"
" is used, which can handle components with no heads or tails. Here, both heads and tails are taken into account.", {'s', "sort"});
Expand Down Expand Up @@ -74,7 +75,7 @@ int main_build(int argc, char** argv) {
return 1;
}
if (gfa_filename.size()) {
gfa_to_handle(gfa_filename, &graph, args::get(nthreads), args::get(progress));
gfa_to_handle(gfa_filename, &graph, args::get(optimize), args::get(nthreads), args::get(progress));
}

const uint64_t num_threads = args::get(nthreads) ? args::get(nthreads) : 1;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace utils {
"To save time in the future, please use odgi build -i=[FILE], --idx=[FILE] -o=[FILE], --out=[FILE] "
"to generate a graph in ODGI format. Such a graph can be supplied to all ODGI subcommands. Building graph in ODGI format from given GFA." << std::endl;
}
gfa_to_handle(infile, &graph, num_threads, progress);
gfa_to_handle(infile, &graph, false, num_threads, progress);
graph.set_number_of_threads(num_threads);
} else {
ifstream f(infile.c_str());
Expand Down

0 comments on commit 0f9de83

Please sign in to comment.