Zip and Skip Tries
Loading...
Searching...
No Matches
Enumerations | Functions | Variables
nucleotide.hpp File Reference

Defines nucleotide representation and related utility functions. More...

#include <array>
#include <cstdint>
#include <stdexcept>
#include <fstream>
Include dependency graph for nucleotide.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum class  Nucleotide : uint8_t {
  A = 0 , C = 1 , G = 2 , T = 3 ,
  INVALID
}
 Enumerates the four nucleotides of DNA. More...
 

Functions

bool is_valid_nucleotide (char nucleotide)
 Checks if a character represents a valid nucleotide.
 
std::ostreamoperator<< (std::ostream &os, const Nucleotide &nucleotide)
 Inserts a textual representation of a nucleotide into an output stream.
 

Variables

static constexpr std::array< Nucleotide, 256 > char_to_nucleotide
 Lookup table for converting characters to nucleotides.
 
static constexpr std::array< char, 4 > nucleotide_to_char
 Lookup table for converting nucleotides to characters.
 

Detailed Description

Defines nucleotide representation and related utility functions.

This file provides a type-safe representation of DNA nucleotides and utility functions for converting between character and nucleotide formats. It supports case-insensitive conversion and implements streaming operators for convenient output. This is used primarily for genetic sequence processing in the trie data structures.

See also
genetics.hpp
BitString.cuh

Definition in file nucleotide.hpp.

Enumeration Type Documentation

◆ Nucleotide

Enumerates the four nucleotides of DNA.

This enumeration provides a type-safe way of representing the four nucleotides in DNA: Adenine (A), Cytosine (C), Guanine (G), and Thymine (T). It also includes an INVALID value for error handling.

Enumerator

Adenine.

Cytosine.

Guanine.

Thymine.

INVALID 

Invalid nucleotide.

Definition at line 29 of file nucleotide.hpp.

30{
31 A = 0,
32 C = 1,
33 G = 2,
34 T = 3,
35 INVALID
36};
@ C
Cytosine.
@ A
Adenine.
@ T
Thymine.
@ INVALID
Invalid nucleotide.
@ G
Guanine.

Function Documentation

◆ is_valid_nucleotide()

bool is_valid_nucleotide ( char  nucleotide)

Checks if a character represents a valid nucleotide.

Determines whether the provided character is a valid nucleotide representation (A, C, G, T in either uppercase or lowercase).

Parameters
nucleotideThe character to check.
Returns
true if the character is a valid nucleotide representation, false otherwise.
See also
char_to_nucleotide

Definition at line 14 of file nucleotide.cpp.

15{
16 // Use the lookup table approach for efficient character validation
17 // This avoids multiple comparisons by directly indexing into the table
19}
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
static constexpr std::array< Nucleotide, 256 > char_to_nucleotide
Lookup table for converting characters to nucleotides.

◆ operator<<()

std::ostream & operator<< ( std::ostream os,
const Nucleotide nucleotide 
)

Inserts a textual representation of a nucleotide into an output stream.

Converts the Nucleotide enum value to its character representation and inserts it into the provided output stream. Uses a switch statement to handle each nucleotide type explicitly and throws an std::invalid_argument exception for Nucleotide::INVALID rather than silently outputting a placeholder. This allows Nucleotide objects to be used directly with stream operators.

Parameters
osThe output stream to insert into.
nucleotideThe nucleotide to insert.
Returns
Reference to the output stream for chaining.
Exceptions
std::invalid_argumentif nucleotide is Nucleotide::INVALID.
See also
nucleotide_to_char

Definition at line 21 of file nucleotide.cpp.

22{
23 // Handle each nucleotide case explicitly in the switch statement
24 // Note: We could use the nucleotide_to_char array directly for valid nucleotides,
25 // but the switch approach provides clearer error handling for invalid values
26 switch (nucleotide)
27 {
28 case Nucleotide::A:
29 os << 'A';
30 break;
31 case Nucleotide::C:
32 os << 'C';
33 break;
34 case Nucleotide::G:
35 os << 'G';
36 break;
37 case Nucleotide::T:
38 os << 'T';
39 break;
40 default:
41 // Throw an exception for invalid nucleotides rather than silently outputting a placeholder
42 // This helps catch programming errors early
43 throw std::invalid_argument("Invalid nucleotide");
44 }
45
46 return os;
47}

Variable Documentation

◆ char_to_nucleotide

constexpr std::array<Nucleotide, 256> char_to_nucleotide
staticconstexpr
Initial value:
= []() {
std::array<Nucleotide, 256> result;
result.fill(Nucleotide::INVALID);
result['A'] = Nucleotide::A;
result['a'] = Nucleotide::A;
result['C'] = Nucleotide::C;
result['c'] = Nucleotide::C;
result['G'] = Nucleotide::G;
result['g'] = Nucleotide::G;
result['T'] = Nucleotide::T;
result['t'] = Nucleotide::T;
return result;
}()

Lookup table for converting characters to nucleotides.

This constexpr array provides O(1) conversion from ASCII characters to Nucleotide enum values. It supports both uppercase and lowercase letters (A/a, C/c, G/g, T/t) and marks all other characters as INVALID. Using a lookup table avoids branching in critical code paths.

Definition at line 44 of file nucleotide.hpp.

44 {
45 std::array<Nucleotide, 256> result;
46 result.fill(Nucleotide::INVALID);
47
48 // Initialize valid nucleotide mappings
49 result['A'] = Nucleotide::A;
50 result['a'] = Nucleotide::A;
51 result['C'] = Nucleotide::C;
52 result['c'] = Nucleotide::C;
53 result['G'] = Nucleotide::G;
54 result['g'] = Nucleotide::G;
55 result['T'] = Nucleotide::T;
56 result['t'] = Nucleotide::T;
57
58 return result;
59}();

◆ nucleotide_to_char

constexpr std::array<char, 4> nucleotide_to_char
staticconstexpr
Initial value:
= {
'A', 'C', 'G', 'T'
}

Lookup table for converting nucleotides to characters.

This array provides O(1) conversion from Nucleotide enum values to their corresponding ASCII character representation. The array is indexed by the underlying uint8_t value of the Nucleotide enum, so Nucleotide::A (0) maps to 'A', etc. Note that this array doesn't include a mapping for Nucleotide::INVALID.

See also
Nucleotide

Definition at line 69 of file nucleotide.hpp.

69 {
70 'A', 'C', 'G', 'T'
71};