Skip to content

Commit

Permalink
[MISC] Adapt aligned sequence builder for banded alignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
rrahn committed Sep 17, 2019
1 parent d15d6e4 commit 5826ed0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

#pragma once

#include <optional>
#include <type_traits>
#include <vector>

#include <seqan3/alignment/aligned_sequence/aligned_sequence_concept.hpp>
#include <seqan3/alignment/band/static_band.hpp>
#include <seqan3/alignment/matrix/detail/matrix_coordinate.hpp>
#include <seqan3/alignment/matrix/trace_directions.hpp>
#include <seqan3/alphabet/gap/gapped.hpp>
Expand Down Expand Up @@ -93,6 +95,21 @@ class aligned_sequence_builder
{}
//!\}

/*!\brief Sets the column offset corresponding to the upper diagonal of the band within the matrix.
* \param[in] offset The offset to set.
*
* \details
*
* The offset corresponds to the cell where the upper diagonal intersects with the first row of the matrix after
* trimming the sequences. This option is only necessary if the matrix was computed with a banded alignment.
* In this case, the row index of the alignment coordinate must be synchronized with the actual global row index
* using the given offset.
*/
constexpr void set_band_column_offset(size_t const offset)
{
band_col_offset = offset;
}

/*!\brief Builds the aligned sequences from the given trace path.
* \tparam trace_path_t The type of the trace path; must model std::ranges::input_range and
* std::same_as<value_type_t<trace_path_t>, seqan::detail::trace_directions> must evaluate to
Expand Down Expand Up @@ -132,6 +149,12 @@ class aligned_sequence_builder

res.begin = trace_it.coordinate();

if (band_col_offset.has_value())
{
res.begin.row += res.begin.col - band_col_offset.value();
res.end.row += res.end.col - band_col_offset.value();
}

assign_unaligned(res.alignment.first, fst_rng | views::slice(res.begin.col, res.end.col));
assign_unaligned(res.alignment.second, sec_rng | views::slice(res.begin.row, res.end.row));

Expand All @@ -149,10 +172,8 @@ class aligned_sequence_builder
* \param[in,out] fst_aligned The first aligned sequence to insert gaps into.
* \param[in,out] sec_aligned The second aligned sequence to insert gaps into.
*/
template <typename reverse_traces_t>
void fill_aligned_sequence(reverse_traces_t && rev_traces,
fst_aligned_t & fst_aligned,
sec_aligned_t & sec_aligned) const
template <typename reverse_traces_t, typename fst_t, typename sec_t>
void fill_aligned_sequence(reverse_traces_t && rev_traces, fst_t && fst_aligned, sec_t && sec_aligned) const
{
if (std::ranges::empty(rev_traces))
return;
Expand All @@ -168,13 +189,14 @@ class aligned_sequence_builder
if (dir == trace_directions::left)
sec_it = insert_gap(sec_aligned, sec_it, span);

std::advance(fst_it, span);
std::advance(sec_it, span);
fst_it += span;
sec_it += span;
}
}

all_view<fst_rng_t> fst_rng; //!< A view over the first range.
all_view<sec_rng_t> sec_rng; //!< A view over the second range.
std::optional<size_t> band_col_offset; //!< The offset of the column where an optional band starts.
};

/*!\name Type deduction guides
Expand All @@ -184,5 +206,10 @@ class aligned_sequence_builder
//!\brief Deduces the type from the passed constructor arguments.
template <std::ranges::viewable_range fst_rng_t, std::ranges::viewable_range sec_rng_t>
aligned_sequence_builder(fst_rng_t, sec_rng_t) -> aligned_sequence_builder<fst_rng_t, sec_rng_t>;

//!\brief Deduces the type from the passed constructor arguments.
template <std::ranges::viewable_range fst_rng_t, std::ranges::viewable_range sec_rng_t>
aligned_sequence_builder(fst_rng_t, sec_rng_t, std::optional<static_band>)
-> aligned_sequence_builder<fst_rng_t, sec_rng_t>;
//!\}
} // namespace seqan3::detail
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ TYPED_TEST(aligned_sequence_builder_fixture, build_from_0_0)
EXPECT_EQ(alignment.second | views::to_char | std::ranges::to<std::string>, std::string{""});
}

TYPED_TEST(aligned_sequence_builder_fixture, build_with_band_offset)
{
auto p = this->path(matrix_offset{row_index_type{2}, column_index_type{3}});
this->builder.set_band_column_offset(0);
auto [begin, end, alignment] = this->builder(p);

EXPECT_EQ(begin.row, 0u);
EXPECT_EQ(begin.col, 0u);
EXPECT_EQ(end.row, 5u);
EXPECT_EQ(end.col, 3u);
EXPECT_EQ(alignment.first | views::to_char | std::ranges::to<std::string>, std::string{"--ACG"});
EXPECT_EQ(alignment.second | views::to_char | std::ranges::to<std::string>, std::string{"AG---"});
}

TYPED_TEST(aligned_sequence_builder_fixture, both_empty)
{
std::remove_reference_t<typename TestFixture::fst_seq_t> first{};
Expand Down

0 comments on commit 5826ed0

Please sign in to comment.