Skip to content

Commit

Permalink
Implement has_edges() and get_degree() to avoid clang bus errors / se…
Browse files Browse the repository at this point in the history
…gfaults
  • Loading branch information
jltsiren committed Sep 5, 2019
1 parent cf14cb7 commit cd02b59
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
33 changes: 33 additions & 0 deletions gbwtgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,39 @@ GBWTGraph::for_each_handle_impl(const std::function<bool(const handle_t&)>& iter
return true;
}

size_t
GBWTGraph::get_degree(const handle_t& handle, bool go_left) const
{
// Cache the node.
gbwt::node_type curr = handle_to_node(handle);
if(go_left) { curr = gbwt::Node::reverse(curr); }
gbwt::CachedGBWT cache(*(this->index), true);
gbwt::size_type cache_index = cache.findRecord(curr);

// The outdegree reported by GBWT might account for the endmarker, which is
// always the first successor.
size_t result = cache.outdegree(cache_index);
if(result > 0 && cache.successor(cache_index, 0) == gbwt::ENDMARKER) { result--; }
return result;
}

bool
GBWTGraph::has_edge(const handle_t& left, const handle_t& right) const
{
// Cache the node.
gbwt::node_type curr = handle_to_node(left);
gbwt::CachedGBWT cache(*(this->index), true);
gbwt::size_type cache_index = cache.findRecord(curr);

for(gbwt::rank_type outrank = 0; outrank < cache.outdegree(cache_index); outrank++)
{
gbwt::node_type next = cache.successor(cache_index, outrank);
if(node_to_handle(next) == right) { return true; }
}

return false;
}

//------------------------------------------------------------------------------

void
Expand Down
10 changes: 5 additions & 5 deletions gfa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ struct GFAFile
}
this->file_size = st.st_size;

void* temp_ptr = ::mmap(nullptr, file_size, PROT_READ, MAP_FILE | MAP_SHARED, this->fd, 0);
if(temp_ptr == MAP_FAILED)
{
void* temp_ptr = ::mmap(nullptr, file_size, PROT_READ, MAP_FILE | MAP_SHARED, this->fd, 0);
if(temp_ptr == MAP_FAILED)
{
std::cerr << "GFAFile::GFAFile(): Cannot memory map file " << filename << std::endl;
return;
}
}
this->ptr = static_cast<char*>(temp_ptr);
}

Expand Down Expand Up @@ -215,7 +215,7 @@ gfa_to_gbwt(const std::string& gfa_filename, gbwt::size_type node_width, gbwt::s

// Index the paths. Adjust batch size down if we are dealing with a small file.
gbwt::Verbosity::set(gbwt::Verbosity::SILENT);
if(gfa_file.size() < batch_size) { batch_size = gfa_file.size(); }
if(gfa_file.size() < batch_size) { batch_size = gfa_file.size(); }
gbwt::GBWTBuilder builder(node_width, batch_size, sample_interval);
SequenceSource* source = new SequenceSource();
gbwt::vector_type current_path;
Expand Down
15 changes: 15 additions & 0 deletions include/gbwtgraph/gbwtgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ class GBWTGraph : public HandleGraph, public SerializableHandleGraph
// stopped early.
virtual bool for_each_handle_impl(const std::function<bool(const handle_t&)>& iteratee, bool parallel = false) const;

public:

/*
Reimplementations to avoid weird segfaults / bus errors when calling lambdas
on clang.
*/

/// Get the number of edges on the right (go_left = false) or left (go_left
/// = true) side of the given handle.
virtual size_t get_degree(const handle_t& handle, bool go_left) const;

/// Returns true if there is an edge that allows traversal from the left
/// handle to the right handle.
virtual bool has_edge(const handle_t& left, const handle_t& right) const;

//------------------------------------------------------------------------------

/*
Expand Down

0 comments on commit cd02b59

Please sign in to comment.