Skip to content

Commit

Permalink
[MISC] aligned_sequence_builder should use std::tuple instead of std:…
Browse files Browse the repository at this point in the history
…:pair

On step to fix issue seqan#1598.

The sequence builder uses internally a pair to represent a sequence
alignment that will be implicitly converted to a std::tuple in the final
returned result.
  • Loading branch information
marehr committed Feb 21, 2020
1 parent e417022 commit f82ad56
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct make_aligned_sequence_type
/*!\brief Builds the alignment for a given pair of sequences and the respective trace.
* \ingroup alignment_matrix
* \tparam fst_sequence_t The first sequence of the pairwise alignment; must model std::ranges::viewable_range.
* \tparam sec_sequence_t The first sequence of the pairwise alignment; must model std::ranges::viewable_range.
* \tparam sec_sequence_t The second sequence of the pairwise alignment; must model std::ranges::viewable_range.
*
* \details
*
Expand Down Expand Up @@ -100,6 +100,8 @@ class aligned_sequence_builder
"sec_aligned_t is required to model seqan3::aligned_sequence!");

public:
//!\brief The pairwise alignment type of the two sequences.
using alignment_type = std::tuple<fst_aligned_t, sec_aligned_t>;

//!\brief The result type when building the aligned sequences.
struct [[nodiscard]] result_type
Expand All @@ -110,7 +112,7 @@ class aligned_sequence_builder
std::pair<size_t, size_t> second_sequence_slice_positions{};
//!\brief The alignment over the slices of the first and second sequence, which corresponds to the given
//!\ trace path.
std::pair<fst_aligned_t, sec_aligned_t> alignment{};
alignment_type alignment{};
};

/*!\name Constructors, destructor and assignment
Expand Down Expand Up @@ -173,13 +175,17 @@ class aligned_sequence_builder
std::tie(res.first_sequence_slice_positions.first, res.second_sequence_slice_positions.first) =
std::pair<size_t, size_t>{trace_it.coordinate()};

assign_unaligned(res.alignment.first, fst_rng | views::slice(res.first_sequence_slice_positions.first,
res.first_sequence_slice_positions.second));
assign_unaligned(res.alignment.second, sec_rng | views::slice(res.second_sequence_slice_positions.first,
res.second_sequence_slice_positions.second));
assign_unaligned(std::get<0>(res.alignment),
fst_rng | views::slice(res.first_sequence_slice_positions.first,
res.first_sequence_slice_positions.second));
assign_unaligned(std::get<1>(res.alignment),
sec_rng | views::slice(res.second_sequence_slice_positions.first,
res.second_sequence_slice_positions.second));

// Now we need to insert the values.
fill_aligned_sequence(trace_segments | std::views::reverse, res.alignment.first, res.alignment.second);
fill_aligned_sequence(trace_segments | std::views::reverse,
std::get<0>(res.alignment),
std::get<1>(res.alignment));

return res;
}
Expand Down
60 changes: 30 additions & 30 deletions test/unit/alignment/matrix/detail/aligned_sequence_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_2_3)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0, 3}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0, 2}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"--ACG"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"AG---"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"--ACG"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"AG---"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_2_2)
Expand All @@ -112,8 +112,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_2_2)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 2u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 2u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"AC"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"AG"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"AC"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"AG"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_2_1)
Expand All @@ -123,8 +123,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_2_1)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 1u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 2u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"A--"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"-AG"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"A--"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"-AG"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_2_0)
Expand All @@ -134,8 +134,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_2_0)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 2u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"--"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"AG"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"--"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"AG"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_3)
Expand All @@ -145,8 +145,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_3)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 3u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 1u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"ACG"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"--A"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"ACG"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"--A"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_2)
Expand All @@ -156,8 +156,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_2)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 2u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 1u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"-AC"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"A--"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"-AC"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"A--"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_1)
Expand All @@ -167,8 +167,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_1)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 1u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 1u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"A"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"A"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"A"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"A"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_0)
Expand All @@ -178,8 +178,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_1_0)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 1u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"-"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"A"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"-"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"A"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_3)
Expand All @@ -189,8 +189,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_3)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 3u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"ACG"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"---"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"ACG"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"---"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_2)
Expand All @@ -200,8 +200,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_2)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 2u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"AC"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"--"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"AC"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"--"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_1)
Expand All @@ -211,8 +211,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_1)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 1u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"A"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"-"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"A"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"-"});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_0)
Expand All @@ -222,8 +222,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_0)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{""});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{""});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{""});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{""});
}

TYPED_TEST(aligned_sequence_builder_fixture, both_empty)
Expand All @@ -238,8 +238,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, both_empty)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{""});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{""});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{""});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{""});
}

TYPED_TEST(aligned_sequence_builder_fixture, first_empty)
Expand All @@ -253,8 +253,8 @@ TYPED_TEST(aligned_sequence_builder_fixture, first_empty)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 2u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"--"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"AG"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"--"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"AG"});
}

TYPED_TEST(aligned_sequence_builder_fixture, second_empty)
Expand All @@ -268,6 +268,6 @@ TYPED_TEST(aligned_sequence_builder_fixture, second_empty)

EXPECT_EQ(first_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 3u}));
EXPECT_EQ(second_sequence_slice_positions, (std::pair<size_t, size_t>{0u, 0u}));
EXPECT_EQ(alignment.first | views::to_char | views::to<std::string>, std::string{"ACG"});
EXPECT_EQ(alignment.second | views::to_char | views::to<std::string>, std::string{"---"});
EXPECT_EQ(std::get<0>(alignment) | views::to_char | views::to<std::string>, std::string{"ACG"});
EXPECT_EQ(std::get<1>(alignment) | views::to_char | views::to<std::string>, std::string{"---"});
}

0 comments on commit f82ad56

Please sign in to comment.