Bifrost: Kmer Class Reference

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...
Kmer & | operator= (const Kmer &o)
| Copy assignment operator (copy a k-mer). More...
void | set_empty ()
| Set a k-mer as "empty".
void | set_deleted ()
| Set a k-mer as "deleted".
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

  o | is 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

  s | is 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

  b | is 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

  b | is 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

  offset | is 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

  seed | is a seed number for the hash function (0 by default).

Returns
  a hash of the k-mer.

## ◆ operator!=()

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

Inequality comparison operator.

Parameters

  o | is 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

  o | is a constant reference to the k-mer to copy

## ◆ operator==()

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

Equality comparison operator.

Parameters

  o | is 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_in | is 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

  b | is 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

  _k | is 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

  offset | is the position of the character to set in the k-mer.
  b | is 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]

void Kmer::toString | ( | char * | s | ) | const

Get the string of a k-mer.

Parameters

  s | is a pointer to an array of characters that will be set with the string of a k-mer.

## ◆ toString() [2/2]

std::string Kmer::toString | ( |  | ) | const

Get the string of a k-mer.

Returns
  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_out | is 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:

- src/Kmer.hpp