Bio.SeqIO.NibIO module

Bio.SeqIO support for the UCSC nib file format.

Nib stands for nibble (4 bit) representation of nucleotide sequences. The two nibbles in a byte each store one nucleotide, represented numerically as follows:

  • 0 - T

  • 1 - C

  • 2 - A

  • 3 - G

  • 4 - N (unknown)

As the first bit in a nibble is set if the nucleotide is soft-masked, we additionally have:

  • 8 - t

  • 9 - c

  • a - a

  • b - g

  • c - n (unknown)

A nib file contains only one sequence record. You are expected to use this module via the Bio.SeqIO functions under the format name “nib”:

>>> from Bio import SeqIO
>>> record ="Nib/test_even_bigendian.nib", "nib")
>>> print("%i %s..." % (len(record), record.seq[:20]))
50 nAGAAGagccgcNGgCActt...

For detailed information on the file format, please see the UCSC description at

Bio.SeqIO.NibIO.hex2bytes(string, /)

Create a bytes object from a string of hexadecimal numbers.

Spaces between two numbers are accepted. Example: bytes.fromhex(‘B9 01EF’) -> b’\xb9\x01\xef’.

Bio.SeqIO.NibIO.byte2int(b, byteorder)
Bio.SeqIO.NibIO.maketrans(x, y=None, z=None, /)

Return a translation table usable for str.translate().

If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None. Character keys will be then converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to None in the result.

Bio.SeqIO.NibIO.NibIterator(handle, alphabet=None)

Iterate over a nib file and yield a SeqRecord.

  • handle - input file in the nib file format as defibed by UCSC. This must be opened in binary mode!

  • alphabet - always ignored.

Note that a nib file always contains only one sequence record. The sequence of the resulting SeqRecord object should match the sequence generated by Jim Kent’s nibFrag utility, except that it will be in upper case, whereas nibFrag uses lower case.

This function is used internally via the Bio.SeqIO functions:

>>> from Bio import SeqIO
>>> record ="Nib/test_even_bigendian.nib", "nib")
>>> print("%s %i" % (record.seq, len(record)))
nAGAAGagccgcNGgCActtGAnTAtCGTCgcCacCaGncGncTtGNtGG 50

You can also call it directly:

>>> with open("Nib/test_even_bigendian.nib", "rb") as handle:
...     for record in NibIterator(handle):
...         print("%s %i" % (record.seq, len(record)))
nAGAAGagccgcNGgCActtGAnTAtCGTCgcCacCaGncGncTtGNtGG 50
class Bio.SeqIO.NibIO.NibWriter(handle)

Bases: Bio.SeqIO.Interfaces.SequenceWriter

Nib file writer.

__init__(self, handle)

Initialize an Nib writer object.

  • handle - Output handle, in binary write mode.

write_file(self, records)

Use this to write an entire file containing the given record.