Class SuffixArray.Array<T>

java.lang.Object
org.bzdev.util.SuffixArray
org.bzdev.util.SuffixArray.Array<T>
Enclosing class:
SuffixArray

public static final class SuffixArray.Array<T> extends SuffixArray
Class providing a suffix array for Object sequences.
  • Constructor Details

    • Array

      public Array(T[] sequence, Set<T> alphabet) throws IllegalArgumentException
      Constructor. The sequence must not be changed after this constructor is called as long as this suffix array is used.
      Parameters:
      sequence - an array representing a sequence of objects.
      alphabet - the objects that make up the sequence
      Throws:
      IllegalArgumentException - an object in the sequence was null or was not in the alphabet
  • Method Details

    • getSequence

      public T[] getSequence()
      Get the sequence associated with this suffix array.
      Returns:
      the sequence that this suffix array describes
    • findInstance

      public int findInstance(T[] subsequence)
      Find the index into the sequence associated with a suffix array for an arbitrary instance of a subsequence.

      Using an LCP-LR table (created by calling SuffixArray.useLCPLR()) will change with time complexity of this method from O(m log n) to O(m + log n), where m is the length of a subsequence and n is the length of the sequence array. These, however, are worst-case numbers: while it can take m steps for a comparison function to determine that two suffixes differ, the comparison will stop at the first step at which the suffixes actually differ: the difference in running time in practice is data-set dependent.

      If the argument has a length of 0, the value returned is the length of the sequence.

      Parameters:
      subsequence - the subsequence.
      Returns:
      the index into the sequence; -1 if the subsequence cannot be found
      See Also:
    • findInstance

      public int findInstance(T[] subsequence, int start, int end)
      Find the index into the sequence associated with a suffix array for an arbitrary instance of a subsequence given a starting and ending index into an array containing the subsequence.

      Using an LCP-LR table (created by calling SuffixArray.useLCPLR()) will change with time complexity of this method from O(m log n) to O(m + log n), where m is the length of a subsequence and n is the length of the sequence array. These, however, are worst-case numbers: while it can take m steps for a comparison function to determine that two suffixes differ, the comparison will stop at the first step at which the suffixes actually differ: the difference in running time in practice is data-set dependent.

      If the argument has a length of 0, or the second and third arguments are equal, the value returned is the length of the sequence. This is also true if the second and third arguments are equal.

      Parameters:
      subsequence - array containing the subsequence.
      start - the starting index in the subsequence array (inclusive)
      end - the ending index in the subsequence array (exclusive)
      Returns:
      the index into the sequence; -1 if the subsequence cannot be found
      See Also:
    • findSubsequence

      public int findSubsequence(T[] subsequence)
      Find the suffix-array index of an arbitrary instance of a subsequence. This method finds the index into a suffix array corresponding to a subsequence.
      Parameters:
      subsequence - the subsequence.
      Returns:
      the index into the suffix array; -1 if the subsequence cannot be found
      See Also:
    • findSubsequence

      public int findSubsequence(T[] sarray, int start, int end)
      Find the suffix-array index of an arbitrary instance of a subsequence given a starting index and ending index for the subsequence. This method finds the index into a suffix array corresponding to a subsequence. The subsequence consists of the elements of the array sarray with a starting index named start and and ending index named end.
      Parameters:
      sarray - the subsequence array.
      start - the starting index in the subsequence array (inclusive)
      end - the ending index in the subsequence array (exclusive)
      Returns:
      the index into the suffix array; -1 if the subsequence cannot be found
      See Also:
    • findSubsequence

      public int findSubsequence(T[] subsequence, boolean keyflag)
      Find a subsequence. This method finds the index into a suffix array corresponding to a subsequence.
      Parameters:
      subsequence - the subsequence.
      keyflag - true if the highest index should be returned; false if the lowest index should be returned.
      Returns:
      the index into the suffix array; -1 if the subsequence cannot be found
      See Also:
    • findSubsequence

      public int findSubsequence(T[] subsequence, int start, int end, boolean keyflag)
      Find a subsequence given a starting index and ending index for the subsequence. This method finds the index into a suffix array corresponding to a subsequence. The subsequence consists of the elements of the array sarray with a starting index named start and and ending index named end.
      Parameters:
      subsequence - the subsequence array.
      start - the starting index in the subsequence array (inclusive)
      end - the ending index in the subsequence array (exclusive)
      keyflag - true if the highest index should be returned; false if the lowest index should be returned.
      Returns:
      the index into the suffix array; -1 if the subsequence cannot be found
      See Also:
    • findSubsequence

      public int findSubsequence(int offset, T[] subsequence, int start, int end, int first, int last, boolean keyflag)
      Find a subsequence given a starting index and ending index for the subsequence, restricting the search to a range in this suffix array. This method finds the index into a suffix array corresponding to a subsequence. The subsequence consists of the elements of the array sarray with a starting index named start and and ending index named end. When the range provided by the fifth and sixth arguments is such that all suffixes in this range start with the same N symbols, the offset can be set to N so that these symbols are ignored in comparisions, in which case the third argument should also be set so that the subsequence does not contain these N symbols at its start.
      Parameters:
      offset - an offset to add to the index into a sequence provided by the suffix array when comparing values
      subsequence - the subsequence array
      start - the starting index in the subsequence array (inclusive)
      end - the ending index in the subsequence array (exclusive)
      first - the starting index in the suffix array (inclusive)
      last - the ending index int he suffix array (exclusive)
      keyflag - true if the highest index should be returned; false if the lowest index should be returned.
      Returns:
      the index into the suffix array; -1 if the subsequence cannot be found
      See Also:
    • findRange

      public SuffixArray.Range findRange(T[] subsequence)
      Find all instances of a subsequence.
      Parameters:
      subsequence - the subsequence
      Returns:
      the subsequences corresponding to a range in the suffix array
      See Also:
    • findRange

      public SuffixArray.Range findRange(T[] sarray, int start, int end)
      Find all instances of a subsequence given a starting index and ending index.
      Parameters:
      sarray - the subsequence array.
      start - the starting index in the subsequence array (inclusive)
      end - the ending index in the subsequence array (exclusive)
      Returns:
      the subsequences corresponding to a range in the suffix array
      See Also:
    • getBWT

      public int getBWT(int[] bwt)
      Get the Burrows-Wheeler Transform (BWT) of the sequence associated with this suffix array

      Unlike the other subclasses of SuffixArray, in this case the transform is not of the same type as the elements of the original sequence. Instead, int-valued codes are used, as an explicit alphabet is needed in any case. The end-of-text symbol is -1.

      The value returned includes the end-of-text symbol in the transform when the length of the array is one more than the length of the sequence associated with this suffix array.

      Parameters:
      bwt - the array to store the BWT (an array whose length is the length of the sequence if the end-of-text symbol does not appear in the BWT and one more than the length of the sequence if the end-of-text symbol does appear in the BWT)
      Returns:
      the index for the sorted permutation that matches the sequence
    • inverseBWT

      public static <T> void inverseBWT(int[] bwt, T[] result, int index, Set<T> alphabet) throws IllegalArgumentException
      Compute the inverse Burrows-Wheeler transform.

      Unlike the other subclasses of SuffixArray, in this case the transform is not of the same type as the elements of the original sequence. Instead, int-valued codes are used, as an explicit alphabet is needed in any case. The end-of-text symbol is -1.

      When the length of of the BWT array is one more than the length of the result array, the BTW array is assumed to contain an end-of-text symbol (-1 for this case), and the index parameter is ignored. If the two arrays have the same length, all symbols in the BWT array must be in the alphabet and the index must be provided (it will be the value returned by a call to getBWT(int[])).

      Type Parameters:
      T - the type of the objects that the alphabet represents
      Parameters:
      bwt - the Burrows-Wheeler transform
      result - the inverse of the Burrons-Wheeler transform
      index - the index parameter for the Burrows-Wheeler transform
      alphabet - the alphabet
      Throws:
      IllegalArgumentException - bwt and result have inconsistent lengths
    • fillLCPArray

      protected void fillLCPArray(int[] ourlcpArray, int[] rank)
      Description copied from class: SuffixArray
      Fill the LCP array with the correct values. (the value at index 0 will be set outside of this method).

      A typical implementation would use the following code:

      
        protected void fillLCPArray(int[] ourlcpArray, int[] rank) {
            int k = 0;
            int n = sequence.length;
                  for (int i = 0; i < n; i++) {
                      if (rank[i] == n) {
                          k = 0;
                          continue;
                      }
                      int j = array[rank[i]+1];
                      while (i+k < n && j+k < n &&
                          sequence[i+k] == sequence[j+k]) {
                          k++;
                      }
                      ourlcpArray[rank[i]+1] = k;
                      if (k > 0) k--;
            }
              }
       
      Subclasses will have different types for the variable sequence.
      Specified by:
      fillLCPArray in class SuffixArray
      Parameters:
      ourlcpArray - the LCP array to fill
      rank - the inverse mapping for this suffix array
    • commonPrefixLength

      protected int commonPrefixLength(int index1, int index2)
      Description copied from class: SuffixArray
      Given two indices into the sequence array, compute the length of a common prefix.

      This method is abstract because the implementation is dependent on the type of sequence-array elements. It's implementation will typically compare the prefixes element by element until an index is found where the elements differ. The implementation, however, must not be dependent on the existence of either the LCP table or the LCP_LR table.

      Specified by:
      commonPrefixLength in class SuffixArray
      Parameters:
      index1 - the index into the suffix array for the first suffix
      index2 - the index into the suffix array for the second suffix
      Returns:
      the length of the LCP for these two suffixes