Data Structures#

class pyhmmer.easel.Bitfield#

A statically sized sequence of booleans stored as a packed bitfield.

Example

Instantiate a bitfield from an iterable, where each object will be tested for truth:

>>> bitfield = Bitfield([True, False, False])
>>> len(bitfield)
3
>>> bitfield[0]
True
>>> bitfield[1]
False

Use Bitfield.zeros and Bitfield.ones to initialize a bitfield of a given length with all fields set to \(0\) or \(1\):

>>> Bitfield.zeros(4)
Bitfield([False, False, False, False])
>>> Bitfield.ones(4)
Bitfield([True, True, True, True])

Use indexing to access and edit individual bits:

>>> bitfield[0] = True
>>> bitfield[0]
True
>>> bitfield[0] = False
>>> bitfield[0]
False
__init__(iterable)#

Create a new bitfield from an iterable of objects.

Objects yielded by the iterable can be of any type and will be tested for truth before setting the corresponding field.

Raises:

ValueError – When given an empty iterable.

copy()#

Return a copy of this bitfield object.

Added in version 0.7.0.

count(value=1)#

Count the number occurrences of value in the bitfield.

If no argument is given, counts the number of True occurences.

Example

>>> bitfield = Bitfield.zeros(8)
>>> bitfield.count(False)
8
>>> bitfield[0] = bitfield[1] = True
>>> bitfield.count()
2
classmethod ones(n)#

Create a new bitfield of size n with all elements set to True.

Added in version 0.7.0.

toggle(index)#

Switch the value of one single bit.

Example

>>> bitfield = Bitfield.zeros(8)
>>> bitfield[0]
False
>>> bitfield.toggle(0)
>>> bitfield[0]
True
>>> bitfield.toggle(0)
>>> bitfield[0]
False
classmethod zeros(n)#

Create a new bitfield of size n with all elements set to False.

Added in version 0.7.0.

class pyhmmer.easel.KeyHash#

A dynamically resized container to store byte keys using a hash table.

Internally uses Bob Jenkins’ one at a time hash, a simple and efficient hash function published in 1997 that exhibits avalanche behaviour.

Example

Add new keys to the key hash using the add method like you would with a Python set:

>>> kh = KeyHash()
>>> kh.add(b"key")
0

Check if a key hash contains a given key:

>>> b"key" in kh
True
>>> b"missing" in kh
False

Get the index associated with a key using the indexing notation:

>>> kh[b"key"]
0
>>> kh[b"missing"]
Traceback (most recent call last):
  ...
KeyError: b'missing'

Iterate over the keys of the key hash, in the order of insertion:

>>> kh.add(b"key2")
1
>>> for k in kh:
...     print(k)
b'key'
b'key2'

See also

The Wikipedia article for Bob Jenkins’ hash functions: https://en.wikipedia.org/wiki/Jenkins_hash_function

__init__()#

Create a new empty key-hash collection.

add(key)#

Add a new key to the hash table, and return its index.

If key was already in the hash table, the previous index is returned:

>>> kh = KeyHash()
>>> kh.add(b"first")
0
>>> kh.add(b"second")
1
>>> kh.add(b"first")
0
Parameters:

key (bytes) – The key to add to the hash table.

Returns:

int – The index corresponding to the added key.

Added in version 0.3.0.

clear()#

Remove all entries from the collection.

copy()#

Create and return an exact copy of this mapping.

Example

>>> kh = KeyHash()
>>> kh.add(b"key")
0
>>> copy = kh.copy()
>>> b"key" in copy
True