Skip to content

Commit

Permalink
Solved segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
vellamike committed Jan 10, 2024
1 parent faf3c13 commit a6a203e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dorado/utils/sequence_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ std::tuple<int, int, std::vector<uint8_t>> realign_moves(const std::string& quer
query_sequence_component.data(), static_cast<int>(query_sequence_component.length()),
align_config);

// Check if alignment failed (edlib_result.startLocations is null)
if (edlib_result.startLocations == nullptr) {
// Free the memory allocated by edlibAlign
edlibFreeAlignResult(edlib_result);

// Return the tuple (-1, -1) and an empty vector
return std::make_tuple(-1, -1, std::vector<uint8_t>());
}

// Let's keep two cursor positions - one for the new move table and one for the old:
int new_move_cursor = 0;
int old_move_cursor = 0;
Expand Down
24 changes: 24 additions & 0 deletions dorado/utils/sequence_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ class BaseInfo {

int count_trailing_chars(const std::string_view adapter, char c);

/**
* @brief Realigns a move table based on a given query sequence and a target sequence.
*
* This function adjusts the moves table to align with the target sequence, accounting
* for any differences between the query and target sequences. It returns a tuple containing
* an offset into the original moves table to account for trimming, a location in the target
* sequence where the realigned sequence starts, and the newly computed move table.
* If the new move table cannot be computed, the function returns a tuple with values (-1, -1)
* and an empty vector.
*
* @param query_sequence The original sequence of moves, representing the initial alignment.
* @param target_sequence The sequence to which the moves need to be realigned. This could
* differ from the query sequence.
* @param moves The original move table as a vector of unsigned 8-bit integers, aligned with
* the query sequence.
*
* @return std::tuple<int, int, std::vector<uint8_t>>
* A tuple containing:
* 1. An offset into the old moves table (int), accounting for adjustments made during realignment.
* 2. The start location in the target sequence (int) where the realigned sequence begins.
* 3. The newly computed move table (std::vector<uint8_t>).
* If the move table cannot be computed, returns (-1, -1) and an empty vector.
*/
std::tuple<int, int, std::vector<uint8_t>> realign_moves(const std::string& query_sequence,
std::tuple<int, int, std::vector<uint8_t>> realign_moves(const std::string& query_sequence,
const std::string& target_sequence,
const std::vector<uint8_t>& moves);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(UNIT_TEST_SOURCE_FILES
PolyACalculatorTest.cpp
ReadFilterNodeTest.cpp
ReadTest.cpp
RealignMovesTest.cpp
RNASplitTest.cpp
ResumeLoaderTest.cpp
SampleSheetTests.cpp
Expand Down
35 changes: 35 additions & 0 deletions tests/RealignMovesTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "utils/sequence_utils.h"

#include <ATen/ATen.h>
#include <catch2/catch.hpp>

using Slice = at::indexing::Slice;
using namespace dorado;

#define TEST_GROUP "[utils][realign_moves]"

TEST_CASE("Realign Moves No Error", TEST_GROUP) {
std::string query_sequence = "ACGT"; // Example query sequence
std::string target_sequence = "ACGT"; // Example target sequence
std::vector<uint8_t> moves = {1, 0, 1, 0, 1, 0, 0, 1}; // Example moves vector

// Test that calling realign_moves does not throw any exceptions
REQUIRE_NOTHROW(utils::realign_moves(query_sequence, target_sequence, moves));
}

TEST_CASE("No alignment doesn't produce an error", TEST_GROUP) {
std::string query_sequence = "ACGT"; // Example query sequence
std::string target_sequence = "TGAC"; // Example target sequence
std::vector<uint8_t> moves = {1, 0, 1, 0, 1, 0, 0, 1}; // Original moves vector

// Check that the function does not throw an exception
REQUIRE_NOTHROW(utils::realign_moves(query_sequence, target_sequence, moves));

// Call the function and store the result
auto result = utils::realign_moves(query_sequence, target_sequence, moves);

// Check that the returned tuple has the expected values
REQUIRE(std::get<0>(result) == -1); // Check the first value of the tuple
REQUIRE(std::get<1>(result) == -1); // Check the second value of the tuple
REQUIRE(std::get<2>(result).empty()); // Check that the vector in the tuple is empty
}

0 comments on commit a6a203e

Please sign in to comment.