Skip to content

Commit

Permalink
Add row_diff_traverse, row_diff_successor
Browse files Browse the repository at this point in the history
  • Loading branch information
adamant-pwn committed May 23, 2024
1 parent 707f7c2 commit 429a9de
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ on:
tags:
- 'v*'
pull_request:
branches:
- master

env:
REGISTRY: ghcr.io
Expand Down
44 changes: 44 additions & 0 deletions metagraph/src/graph/representation/base/sequence_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,50 @@ void DeBruijnGraph::call_unitigs(const CallPath &callback,
::mtg::graph::call_sequences(*this, callback, num_threads, true, min_tip_size, kmers_in_single_form);
}

void DeBruijnGraph::row_diff_traverse(size_t num_threads,
size_t max_length,
const bit_vector &rd_succ,
sdsl::bit_vector *terminal) const {
sdsl::bit_vector visited(max_index() + 1, false);
auto finalised = visited;
std::vector<size_t> distance(max_index() + 1);
assert(terminal->size() == visited.size());
assert(rd_succ.size() == visited.size());
auto set_terminal = [&](int v) {
distance[v] = 0;
(*terminal)[v] = true;
};
call_nodes([&](node_index v) {
if (visited[v]) {
return;
}
static std::stack<node_index> path;
while (!visited[v]) {
path.push(v);
visited[v] = true;
if (!has_no_outgoing(v)) {
v = row_diff_successor(v, rd_succ);
}
}
if (!finalised[v]) {
set_terminal(v);
finalised[v] = true;
}
node_index u = v;
while (!path.empty()) {
std::tie(u, v) = std::tie(path.top(), u);
if (!finalised[u]) {
distance[u] = distance[v] + 1;
if (distance[u] == max_length) {
set_terminal(u);
}
finalised[u] = true;
}
path.pop();
}
});
}

/**
* Traverse graph and iterate over all nodes
*/
Expand Down
19 changes: 19 additions & 0 deletions metagraph/src/graph/representation/base/sequence_graph.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __SEQUENCE_GRAPH_HPP__
#define __SEQUENCE_GRAPH_HPP__

#include "common/vectors/bit_vector.hpp"

#include <vector>
#include <string>
#include <functional>
Expand Down Expand Up @@ -198,6 +200,7 @@ class DeBruijnGraph : public SequenceGraph {
virtual void call_kmers(const std::function<void(node_index, const std::string&)> &callback) const;

virtual size_t outdegree(node_index) const = 0;
virtual bool has_no_outgoing(node_index node) const { return outdegree(node) == 0; }
virtual bool has_single_outgoing(node_index node) const { return outdegree(node) == 1; }
virtual bool has_multiple_outgoing(node_index node) const { return outdegree(node) > 1; }

Expand Down Expand Up @@ -231,6 +234,22 @@ class DeBruijnGraph : public SequenceGraph {

// Call all nodes that have no incoming edges
virtual void call_source_nodes(const std::function<void(node_index)> &callback) const;

virtual void row_diff_traverse(size_t num_threads,
size_t max_length,
const bit_vector &rd_succ,
sdsl::bit_vector *terminal) const;

virtual node_index row_diff_successor(node_index node, const bit_vector &rd_succ) const {
node_index succ = npos;
adjacent_outgoing_nodes(node, [&](node_index adjacent_node) {
if(rd_succ[adjacent_node]) {
succ = adjacent_node;
}
});
assert(succ != npos && "a row diff successor must exist");
return succ;
}
};


Expand Down

0 comments on commit 429a9de

Please sign in to comment.