Loading...
Searching...
No Matches
rna5_implicit_conversion_from_dna5

Normally, we do not allow implicit conversion of single argument constructors, but in this case we make an exception, because seqan3::rna5 and seqan3::dna5 are interchangeable as they behave nearly the same (e.g. same ranks, same char to rank conversion).

#include <seqan3/alphabet/nucleotide/dna5.hpp>
#include <seqan3/alphabet/nucleotide/rna5.hpp>
int main()
{
using namespace seqan3::literals;
seqan3::rna5 letter1 = 'C'_dna5; // implicitly converted
seqan3::rna5 letter2{};
letter2 = 'C'_dna5; // implicitly converted
}

seqan3::sequences (e.g. seqan3::rna5_vector) in general are not implicitly convertible and must be explicitly copied to be converted:

#include <vector>
#include <seqan3/alphabet/nucleotide/dna5.hpp>
#include <seqan3/alphabet/nucleotide/rna5.hpp>
int main()
{
using namespace seqan3::literals;
seqan3::rna5_vector vector{'A'_dna5, 'C'_dna5, 'G'_dna5}; // (element-wise) implicit conversion
// but this won't work:
// seqan3::rna5_vector rna5_vector{"ACGT"_dna5};
// as a workaround you can use:
// side note: this would also work without the implicit conversion.
seqan3::dna5_vector dna5_vector = "ACGT"_dna5;
seqan3::rna5_vector rna5_vector{dna5_vector.begin(), dna5_vector.end()};
}

You can avoid this copy by using std::ranges::views:

#include <vector>
#include <seqan3/alphabet/nucleotide/dna5.hpp>
#include <seqan3/alphabet/nucleotide/rna5.hpp>
#include <seqan3/utility/views/convert.hpp>
int main()
{
using namespace seqan3::literals;
seqan3::rna5_vector vector = "ACG"_rna5;
auto dna5_view = vector | seqan3::views::convert<seqan3::dna5>;
for (auto && chr : dna5_view) // converts lazily on-the-fly
{
static_assert(std::same_as<decltype(chr), seqan3::dna5 &&>);
}
}

This conversion constructor only allows converting seqan3::dna5 to seqan3::rna5. Other alphabets that inherit from seqan3::dna5 will not be implicitly convertible to seqan3::rna5.

#include <seqan3/alphabet/nucleotide/dna5.hpp>
#include <seqan3/alphabet/nucleotide/rna5.hpp>
struct my_rna5 : public seqan3::rna5
{
// using seqan3::rna5::rna5; // uncomment to import implicit conversion shown by letter1
};
struct my_dna5 : public seqan3::dna5
{};
int main()
{
using namespace seqan3::literals;
// my_rna5 letter1 = 'C'_dna5; // NO automatic implicit conversion!
// seqan3::rna5 letter2 = my_dna5{}; // seqan3::rna5 only allows implicit conversion from seqan3::dna5!
}
Hide me