Package kmer.core

Class KMerUtils

java.lang.Object
kmer.core.KMerUtils

public class KMerUtils extends Object
An assortment of utility methods useful for counting k-mers.
Author:
Dennis Cosgrove (http://www.cse.wustl.edu/~cosgroved/)
  • Method Details

    • toString

      public static String toString​(byte[] sequence, int offset, int k)
      Stores the information from the given sequence into a String. For example, if you had the sequence, "ACCTGTCAAAA" and you called this method with an offset of 1 and a k of 4, it would return "CCTG".
      Parameters:
      sequence - the sequence of nucleobases to draw the bytes from
      offset - the offset for where to start looking for bytes
      k - the length of the k-mer to make a String for
      Returns:
      a String representation of the k-mer at the desired position
    • toString

      public static String toString​(byte[] kMer)
      Converts the given k-mer (represented as an array of bytes) into a human-readable String. For example, if you had the k-mer, {'C', 'C', 'T', 'G'}, this method would return "CCTG".
      Parameters:
      kMer - the k-mer to generate a String for.
      Returns:
      the k-mer's String reprentation
    • toBytes

      public static byte[] toBytes​(String s)
    • toByteArrayRange

      public static ByteArrayRange toByteArrayRange​(byte[] sequence, int offset, int k)
    • toByteArrayRange

      public static ByteArrayRange toByteArrayRange​(byte[] kMer)
    • toBytes

      public static byte[] toBytes​(ByteArrayRange range)
    • toPackedInt

      public static int toPackedInt​(byte[] sequence, int offset, int k)
      "Packs" a k-mer into an int. The bits of the int will be used to store individual nucleobases.
      Parameters:
      sequence - a sequence of nucleobases
      offset - the index of the first nucleobase in the desired k-mer
      k - the length of the desired k-mer
      Returns:
      an integer uniquely representing this k-mer
    • toPackedInt

      public static int toPackedInt​(byte[] kMer)
      Packs the entire k-mer into an int. Exactly like toPackedInt(byte[], int, int), but treats the whole array as a single k-mer.
      Parameters:
      kMer - the k-mer represented as an array of bytes
      Returns:
      an integer uniquely representing this k-mer
    • unpackInt

      public static byte[] unpackInt​(int kMer, int k)
      Unpacks the k-mer from an integer back to an array of bytes.
      Parameters:
      kMer - an integer uniquely identifying a k-mer
      k - the length of the k-mer packed in the integer
      Returns:
      an array of bytes representing the same k-mer
    • toPackedLong

      public static long toPackedLong​(byte[] sequence, int offset, int k)
      "Packs" a k-mer into a long. The bits of the long will be used to store individual nucleobases.
      Parameters:
      sequence - a sequence of nucleobases
      offset - the index of the first nucleobase in the desired k-mer
      k - the length of the desired k-mer
      Returns:
      a long integer uniquely representing this k-mer
    • toPackedLong

      public static long toPackedLong​(byte[] sequence)
      Packs the entire k-mer into a long. Exactly like toPackedLong(byte[], int, int), but treats the whole array as a single k-mer.
      Parameters:
      sequence - the k-mer represented as an array of bytes
      Returns:
      a long integer uniquely representing this k-mer
    • unpackLong

      public static byte[] unpackLong​(long kMer, int k)
      Unpacks the k-mer from a long back to an array of bytes.
      Parameters:
      kMer - a long integer uniquely identifying a k-mer
      k - the length of the k-mer packed in the long
      Returns:
      an array of bytes representing the same k-mer
    • createIntTMask

      public static int createIntTMask​(int k)
    • createIntCMask

      public static int createIntCMask​(int k)
    • createIntGMask

      public static int createIntGMask​(int k)
    • createLongTMask

      public static long createLongTMask​(int k)
    • createLongCMask

      public static long createLongCMask​(int k)
    • createLongGMask

      public static long createLongGMask​(int k)
    • shiftAndUpdateLastBaseIndexOfPackedInt

      public static int shiftAndUpdateLastBaseIndexOfPackedInt​(int prev, byte[] sequence, int offset, int k, int T_MASK, int C_MASK, int G_MASK)
    • shiftAndUpdateLastBaseIndexOfPackedLong

      public static long shiftAndUpdateLastBaseIndexOfPackedLong​(long prev, byte[] sequence, int offset, int k, long T_MASK, long C_MASK, long G_MASK)
    • nextRandom

      public static byte[] nextRandom​(int k)
    • calculatePossibleKMers

      public static long calculatePossibleKMers​(int k)
      Gets the number of possible k-mers that can exist with the given length. Note that all possible k-mers with the desired length can be represented by the integers from 0 (inclusive) to the result of this method (exclusive).
      Parameters:
      k - the k in k-mer
      Returns:
      the number of possible k-mers that could exist with the given length
    • calculateSumOfAllKMers

      public static int calculateSumOfAllKMers​(List<byte[]> sequences, int k)
      Gets the total number of (not necessarily unique) k-mers in all of the sequences. If a single sequence is given, this method will return n - k + 1.
      Parameters:
      sequences - a list of sequences of nucleobases
      k - the k in k-mer
      Returns:
      the sum of all k-mer counts
    • toArrayLength

      public static int toArrayLength​(long possibleKMers)