Example #1
0
def readHuffTable(f):
  huffTable = {}

  buffer = ''
  bufferSize = 0

  # Get the number of codewords
  (numCodewords, buffer, bufferSize) = bits.getVarBits(f, 32, buffer, bufferSize)
  numCodewords = int(numCodewords, 2)

  # Keep reading codewords
  while numCodewords > 0:
    # Get the key
    (key, buffer, bufferSize) = bits.getVarBits(f, 32, buffer, bufferSize)
    key= int(key, 2)

    # Get the codeword length
    (codewordLen, buffer, bufferSize) = bits.getVarBits(f, 8, buffer, bufferSize)
    codewordLen = int(codewordLen, 2)

    (codeword, buffer, bufferSize) = bits.getVarBits(f, codewordLen, buffer, bufferSize)

    huffTable[codeword] = key

    numCodewords -= 1

  return huffTable
Example #2
0
def readHuffTable2(f):
  huffTable = {}

  buffer = ''
  bufferSize = 0

  # Get the number of codewords
  (numCodewords, buffer, bufferSize) = bits.getVarBits(f, 32, buffer, bufferSize)
  numCodewords = int(numCodewords, 2)

  # Keep reading codewords
  while numCodewords > 0:
    # Get the key
    stop = 0
    key = ''
    while stop == 0:
      # First get 1 bit
      (temp, buffer, bufferSize) = bits.getVarBits(f, 1, buffer, bufferSize)

      # If first bit is a 0, then get one more bit
      if (temp == '0'):
        currbp = '0'
        (temp, buffer, bufferSize) = bits.getVarBits(f, 1, buffer, bufferSize)
        currbp += temp
        
        if currbp == '00':
          key += 'A'
        else:
          key += 'C'

      # Otherwise, get two bits
      else:
        currbp = '1'
        (temp, buffer, bufferSize) = bits.getVarBits(f, 2, buffer, bufferSize)
        currbp += temp
        if (currbp == '100'):
          key += 'G'
        elif (currbp == '101'):
          key += 'T'
        elif (currbp == '110'):
          key += 'N'
        else:
          stop = 1

    # Get the codeword length
    (codewordLen, buffer, bufferSize) = bits.getVarBits(f, 8, buffer, bufferSize)
    codewordLen = int(codewordLen, 2)

    (codeword, buffer, bufferSize) = bits.getVarBits(f, codewordLen, buffer, bufferSize)

    huffTable[codeword] = key

    numCodewords -= 1

  return huffTable
Example #3
0
def readGolombCodedHuffTable(f):
  huffTable = {}

  buffer = ''
  bufferSize = 0

  # Get the number of codewords
  (numCodewords, buffer, bufferSize) = bits.getVarBits(f, tableSize, buffer, bufferSize)
  numCodewords = int(numCodewords, 2)

  # Get the number of consecutive keys
  (sparseStart, buffer, bufferSize) = bits.getVarBits(f, denseSize, buffer, bufferSize)
  sparseStart = int(sparseStart, 2)

  # Get count for the Golomb code (count is the first number in the dense region: usually 0)
  (count, buffer, bufferSize) = bits.getVarBits(f, countSize, buffer, bufferSize)
  count = int(count, 2)

  # Get M for the Golomb code
  (M, buffer, bufferSize) = bits.getVarBits(f, MSize, buffer, bufferSize)
  M = int(M, 2)

  # Calculate the length of the remainder part
  MLen = int(math.ceil(math.log(M,2)))

  ind = 0
  currKey = 0
  prevKey = 0

  # Keep reading codewords
  for ind in xrange(numCodewords):
    # The key's not specified if we're in the dense region
    if ind < sparseStart:
      currKey = count + ind
    else:
      # Decode by Golomb
      # Get the quotient
      quotient = 0
      (currBit, buffer, bufferSize) = bits.getVarBits(f, 1, buffer, bufferSize)
      while (currBit == '1'):
        (currBit, buffer, bufferSize) = bits.getVarBits(f, 1, buffer, bufferSize)
        quotient = quotient + 1
      # Get the remainder
      (remainder, buffer, bufferSize) = bits.getVarBits(f, MLen, buffer, bufferSize)
      remainder = int(remainder, 2)

      # Get the delta between the previous key and the current key
      delta = quotient*M + remainder + 1
 
      # Now get the actual key
      currKey = prevKey + delta

    # Update previous key (needed when we're in the sparse region)
    prevKey = currKey

    # Get the codeword length
    (codewordLen, buffer, bufferSize) = bits.getVarBits(f, lenSize, buffer, bufferSize)
    codewordLen = int(codewordLen, 2)

    # Get the codeword
    (codeword, buffer, bufferSize) = bits.getVarBits(f, codewordLen, buffer, bufferSize)

    huffTable[codeword] = currKey

  return huffTable