Loading...
Searching...
No Matches
dna4_implicit_conversion_from_rna4

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

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

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

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

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

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

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

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