Bifrost
Public Member Functions | Static Public Member Functions
Kmer Class Reference

Interface to store and manipulate k-mers. More...

Public Member Functions

 Kmer ()
 Constructor (initialize a k-mer with 'A' k times).
 
 Kmer (const Kmer &o)
 Copy constructor (copy a k-mer). More...
 
 Kmer (const char *s)
 Constructor. More...
 
Kmeroperator= (const Kmer &o)
 Copy assignment operator (copy a k-mer). More...
 
void set_deleted ()
 Set a k-mer as "deleted".
 
void set_empty ()
 Set a k-mer as "empty".
 
bool isDeleted () const
 Check whether a k-mer is "deleted". More...
 
bool isEmpty () const
 Check whether a k-mer is "empty". More...
 
bool operator== (const Kmer &o) const
 Equality comparison operator. More...
 
bool operator!= (const Kmer &o) const
 Inequality comparison operator. More...
 
uint64_t hash (const uint64_t seed=0) const
 Get the hash of a k-mer. More...
 
Kmer twin () const
 Get the reverse-complement of a k-mer. More...
 
Kmer rep () const
 Get the canonical k-mer (lexicographically smallest between a k-mer and its reverse-complement). More...
 
Kmer forwardBase (const char b) const
 Get a new k-mer which is the shift of the current k-mer of one base on the left with one new character on the right. More...
 
Kmer backwardBase (const char b) const
 Get a new k-mer which is the shift of the current k-mer of one base on the right with one new character on the left. More...
 
void selfForwardBase (const char b)
 Shift the current k-mer of one base on the left with one new character on the right. More...
 
char getChar (const size_t offset) const
 Get the character at a given position in a k-mer. More...
 
bool setChar (const size_t offset, const char b)
 Set a character at a given position in a k-mer. More...
 
void toString (char *s) const
 Get the string of a k-mer. More...
 
std::string toString () const
 Get the string of a k-mer. More...
 
bool write (std::ostream &stream_out) const
 Write a k-mer (binary) to a stream. More...
 
bool read (std::istream &stream_in)
 Read a k-mer (binary) from a stream. More...
 

Static Public Member Functions

static void set_k (const unsigned int _k)
 Set the length of k-mers. More...
 

Detailed Description

Interface to store and manipulate k-mers.

The default maximum supported k-mer size is 31. To work with larger k, you must replace the macro defined MAX_KMER_SIZE with a larger number which is a multiple of 32. For such a number K, the available maximum supported k-mer size will be K-1. For example, if MAX_KMER_SIZE is defined as 64, the maximum k allowed is 63. Keep in mind that increasing MAX_KMER_SIZE increases memory usage (k=31 uses 8 bytes of memory per k-mer while k=63 uses 16 bytes of memory per k-mer).

Constructor & Destructor Documentation

◆ Kmer() [1/2]

Kmer::Kmer ( const Kmer o)

Copy constructor (copy a k-mer).

After the call to this function, the same k-mer exists twice in memory.

Parameters
ois a constant reference to the k-mer to copy

◆ Kmer() [2/2]

Kmer::Kmer ( const char *  s)
explicit

Constructor.

Initialize a k-mer from a string of characters.

Parameters
sis a constant pointer to an array of characters. The array of characters must contain valid DNA characters ('A', 'C', 'G', 'T') and be of length at least k.

Member Function Documentation

◆ backwardBase()

Kmer Kmer::backwardBase ( const char  b) const

Get a new k-mer which is the shift of the current k-mer of one base on the right with one new character on the left.

For a k-mer km of length k, km.backwardBase(b) = b + km[0..k-2].

Parameters
bis a new character to add on the left of the shifted k-mer (as a first character). It must be either 'A', 'C', 'G' or 'T'.
Returns
a new k-mer which is the shifted k-mer.

◆ forwardBase()

Kmer Kmer::forwardBase ( const char  b) const

Get a new k-mer which is the shift of the current k-mer of one base on the left with one new character on the right.

For a k-mer km of length k, km.forwardBase(b) = km[1..k-1] + b.

Parameters
bis a new character to add on the right of the shifted k-mer (as a last character). It must be either 'A', 'C', 'G' or 'T'.
Returns
a new k-mer which is the shifted k-mer.

◆ getChar()

char Kmer::getChar ( const size_t  offset) const

Get the character at a given position in a k-mer.

Parameters
offsetis the position of the character to get in the k-mer.
Returns
the character to get in the k-mer

◆ hash()

uint64_t Kmer::hash ( const uint64_t  seed = 0) const
inline

Get the hash of a k-mer.

Parameters
seedis a seed number for the hash function (0 by default).
Returns
a hash of the k-mer.

◆ isDeleted()

bool Kmer::isDeleted ( ) const
inline

Check whether a k-mer is "deleted".

Returns
a boolean indicating if the k-mer is "deleted" (true) or not (false).

◆ isEmpty()

bool Kmer::isEmpty ( ) const
inline

Check whether a k-mer is "empty".

Returns
a boolean indicating if the k-mer is "empty" (true) or not (false).

◆ operator!=()

bool Kmer::operator!= ( const Kmer o) const

Inequality comparison operator.

Parameters
ois a constant reference to the k-mer to compare to.
Returns
a boolean indicating if two k-mers are the different (true), including their "empty" and "deleted" flags, or not (false).

◆ operator=()

Kmer& Kmer::operator= ( const Kmer o)

Copy assignment operator (copy a k-mer).

After the call to this function, the same k-mer exists twice in memory.

Parameters
ois a constant reference to the k-mer to copy

◆ operator==()

bool Kmer::operator== ( const Kmer o) const

Equality comparison operator.

Parameters
ois a constant reference to the k-mer to compare to.
Returns
a boolean indicating if two k-mers are the same (true), including their "empty" and "deleted" flags, or not (false).

◆ read()

bool Kmer::read ( std::istream &  stream_in)

Read a k-mer (binary) from a stream.

The stream must be opened prior to this function call and it won't be closed by this function.

Parameters
stream_inis an in stream from which the k-mer must be read. The stream must be opened prior to this function call and it won't be closed by this function.
Returns
a boolean indicating if the reading was successful (true) or not.

◆ rep()

Kmer Kmer::rep ( ) const

Get the canonical k-mer (lexicographically smallest between a k-mer and its reverse-complement).

Returns
a new k-mer which is the canonical k-mer.

◆ selfForwardBase()

void Kmer::selfForwardBase ( const char  b)

Shift the current k-mer of one base on the left with one new character on the right.

Parameters
bis a new character to add on the right (as a last character) after shifting the current k-mer. It must be either 'A', 'C', 'G' or 'T'.

◆ set_k()

static void Kmer::set_k ( const unsigned int  _k)
static

Set the length of k-mers.

After this function call, all Kmer functions considers _k to be the k-mer length.

Parameters
_kis the k-mer length to set.

◆ setChar()

bool Kmer::setChar ( const size_t  offset,
const char  b 
)

Set a character at a given position in a k-mer.

Parameters
offsetis the position of the character to set in the k-mer.
bis the character to set. It must be either 'A', 'C', 'G' or 'T'.
Returns
a boolean indicating if the character was succesfully set. The boolean will be equal to false if offset > k-mer length or if the character to set is not 'A', 'C', 'G' or 'T'.

◆ toString() [1/2]

std::string Kmer::toString ( ) const

Get the string of a k-mer.

Returns
the string of a k-mer.

◆ toString() [2/2]

void Kmer::toString ( char *  s) const

Get the string of a k-mer.

Parameters
sis a pointer to an array of characters that will be set with the string of a k-mer.

◆ twin()

Kmer Kmer::twin ( ) const

Get the reverse-complement of a k-mer.

Returns
a new k-mer which is the reverse-complement.

◆ write()

bool Kmer::write ( std::ostream &  stream_out) const

Write a k-mer (binary) to a stream.

The stream must be opened prior to this function call and it won't be closed by this function.

Parameters
stream_outis an out stream to which the k-mer must be written to. The stream must be opened prior to this function call and it won't be closed by this function.
Returns
a boolean indicating if the writing was successful (true) or not.

The documentation for this class was generated from the following file: