Repository navigation
new vg combine, adding new options #4965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xinzilan
wants to merge
7
commits into
master
Choose a base branch
from
new_vg_combine
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9734b63
new vg combine, adding new options
xinzilan 5051aec
Merge branch 'master' into new_vg_combine
faithokamoto d9ea751
remove concat
faithokamoto 37de3a1
Refactor the vg combine and add tests
xinzilan f8dbb59
Merge remote-tracking branch 'origin/master' into new_vg_combine
xinzilan 11d3327
Update index test to use vg combine instead of removed vg concat
xinzilan b2f290f
Address review comments on vg combine
xinzilan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /** | ||
| * \file fuse_nodes.cpp | ||
| * | ||
| * Defines an algorithm to fuse one pair of adjacent nodes into one node. | ||
| */ | ||
|
|
||
| #include "fuse_nodes.hpp" | ||
|
|
||
| #include <unordered_set> | ||
| #include <vector> | ||
|
|
||
| namespace vg { | ||
| namespace algorithms { | ||
|
|
||
| using namespace std; | ||
|
|
||
| /// Check whether two nodes can be fused. | ||
| bool can_fuse_nodes(const HandleGraph& graph, handle_t left, handle_t right) { | ||
| if (graph.get_id(left) == graph.get_id(right)) { | ||
| // A node cannot be concatenated onto itself. | ||
| return false; | ||
| } | ||
| bool clean = true; | ||
| // left must have nothing attached on its right side. | ||
| graph.follow_edges(left, false, [&](const handle_t&) { | ||
| clean = false; | ||
| return false; | ||
| }); | ||
| if (!clean) { | ||
| return false; | ||
| } | ||
| // right must have nothing attached on its left side. | ||
| graph.follow_edges(right, true, [&](const handle_t&) { | ||
| clean = false; | ||
| return false; | ||
| }); | ||
| return clean; | ||
| } | ||
|
|
||
| /// Fuse two nodes into one node. | ||
| handle_t fuse_nodes(handlegraph::MutablePathDeletableHandleGraph* graph, | ||
| handle_t left, handle_t right) { | ||
|
|
||
| // Create the fused node. | ||
| handle_t fused = graph->create_handle(graph->get_sequence(left) | ||
| + graph->get_sequence(right)); | ||
|
|
||
| // Collect all neighbours of left and right. | ||
| unordered_set<handle_t> predecessors; | ||
| unordered_set<handle_t> successors; | ||
| graph->follow_edges(left, true, [&](const handle_t& p) { | ||
| predecessors.insert(p); | ||
| }); | ||
| graph->follow_edges(right, false, [&](const handle_t& n) { | ||
| successors.insert(n); | ||
| }); | ||
|
|
||
| // Handle a neighbour that is left or right itself, so the edges built below | ||
| // land on fused. | ||
| auto translate = [&](const handle_t& h) { | ||
| if (h == left || h == right) { | ||
| return fused; | ||
| } | ||
| if (h == graph->flip(left) || h == graph->flip(right)) { | ||
| return graph->flip(fused); | ||
| } | ||
| return h; | ||
| }; | ||
| // Build the edges to the neighbours. | ||
| for (const handle_t& p : predecessors) { | ||
| handle_t from = translate(p); | ||
| if (!graph->has_edge(from, fused)) { | ||
| graph->create_edge(from, fused); | ||
| } | ||
| } | ||
| for (const handle_t& n : successors) { | ||
| handle_t to = translate(n); | ||
| if (!graph->has_edge(fused, to)) { | ||
| graph->create_edge(fused, to); | ||
| } | ||
| } | ||
|
|
||
| // Collect every step that visits left or right. | ||
| vector<pair<step_handle_t, bool>> to_rewrite; | ||
| for (handle_t original : {left, right}) { | ||
| graph->for_each_step_on_handle(original, [&](const step_handle_t& s) { | ||
| bool flipped = graph->get_is_reverse(original) | ||
| != graph->get_is_reverse(graph->get_handle_of_step(s)); | ||
| to_rewrite.emplace_back(s, flipped); | ||
| }); | ||
| } | ||
| // Rewrite each collected step to point at fused. | ||
| for (const auto& step_and_flip : to_rewrite) { | ||
| graph->rewrite_segment(step_and_flip.first, | ||
| graph->get_next_step(step_and_flip.first), | ||
| {step_and_flip.second ? graph->flip(fused) : fused}); | ||
| } | ||
|
|
||
| // Tear down the old nodes and the edges on them. | ||
| for (handle_t original : {left, right}) { | ||
| unordered_set<edge_t> to_remove; | ||
| graph->follow_edges(original, false, [&](const handle_t& h) { | ||
| to_remove.insert(graph->edge_handle(original, h)); | ||
| }); | ||
| graph->follow_edges(original, true, [&](const handle_t& h) { | ||
| to_remove.insert(graph->edge_handle(h, original)); | ||
| }); | ||
| for (const edge_t& e : to_remove) { | ||
| graph->destroy_edge(e); | ||
| } | ||
| graph->destroy_handle(original); | ||
| } | ||
|
|
||
| return fused; | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #ifndef VG_ALGORITHMS_FUSE_NODES_HPP_INCLUDED | ||
| #define VG_ALGORITHMS_FUSE_NODES_HPP_INCLUDED | ||
|
|
||
| #include "../handle.hpp" | ||
| #include <handlegraph/mutable_path_deletable_handle_graph.hpp> | ||
|
|
||
| namespace vg { | ||
| namespace algorithms { | ||
|
|
||
| /// Check whether two nodes can be fused. | ||
| bool can_fuse_nodes(const HandleGraph& graph, handle_t left, handle_t right); | ||
|
|
||
| /// Fuse two nodes into one node. | ||
| handle_t fuse_nodes(handlegraph::MutablePathDeletableHandleGraph* graph, | ||
| handle_t left, handle_t right); | ||
|
adamnovak marked this conversation as resolved.
Resolved
|
||
|
|
||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might not want to duplicate doc comments on definitions.