Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autocomp.bash
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ _vg_complete()
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts="add align annotate augment autoindex bench-dist-query benchmark call chains chunk circularize clip cluster combine concat construct convert deconstruct depth describe dotplot filter find gamcompare gampcompare gamsort gbwt genotype giraffe haplotypes help ids index inject kmers map mask mcmc minimizer mod mpmap pack paths primers prune rna sim simplify snarls sort stats surject test trace translate validate vectorize version view viz zipcode"
opts="add align annotate augment autoindex bench-dist-query benchmark call chains chunk circularize clip cluster combine construct convert deconstruct depth describe dotplot filter find gamcompare gampcompare gamsort gbwt genotype giraffe haplotypes help ids index inject kmers map mask mcmc minimizer mod mpmap pack paths primers prune rna sim simplify snarls sort stats surject test trace translate validate vectorize version view viz zipcode"

if [ $COMP_CWORD -eq 1 ]
then
Expand Down
2 changes: 1 addition & 1 deletion autocomplete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ then
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts="add align annotate augment autoindex bench-dist-query benchmark call chains chunk circularize clip cluster combine concat construct convert deconstruct depth describe dotplot filter find gamcompare gampcompare gamsort gbwt genotype giraffe haplotypes help ids index inject kmers map mask mcmc minimizer mod mpmap pack paths primers prune rna sim simplify snarls sort stats surject test trace translate validate vectorize version view viz zipcode"
opts="add align annotate augment autoindex bench-dist-query benchmark call chains chunk circularize clip cluster combine construct convert deconstruct depth describe dotplot filter find gamcompare gampcompare gamsort gbwt genotype giraffe haplotypes help ids index inject kmers map mask mcmc minimizer mod mpmap pack paths primers prune rna sim simplify snarls sort stats surject test trace translate validate vectorize version view viz zipcode"

COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
Expand Down
2 changes: 1 addition & 1 deletion doc/vgmanmd.desc.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ For more in-depth explanations of tools and workflows, see the [general wiki pag
- [`vg annotate`](#annotate): annotate a graph or alignments.
- [`vg circularize`](#circularize): connect head and tail nodes to circularize paths.
- [`vg clip`](#clip): remove variation from a graph.
- [`vg combine`](#combine): merge graphs into a combined graph. Useful for per-chromosome graphs.
- [`vg combine`](#combine): merge graphs holding fragments of the same paths (e.g. chunks of one chromosome) into one graph.
- [`vg mask`](#mask): N-mask regions of a graph.
- [`vg mod`](#mod): filter, transform, and edit the graph.
- [`vg gbwt`](#gbwt): manipulate GBWTs and associated indexes. [wiki page](https://github.com/vgteam/vg/wiki/VG-GBWT-Subcommand)
Expand Down
118 changes: 118 additions & 0 deletions src/algorithms/fuse_nodes.cpp
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.

Copy link
Copy Markdown
Member

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.

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
// connect to 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;
}

}
}
20 changes: 20 additions & 0 deletions src/algorithms/fuse_nodes.hpp
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);
Comment thread
adamnovak marked this conversation as resolved.
Resolved

}
}

#endif
Loading
Loading