Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
Bio.bgzf.BgzfWriter Class Reference

List of all members.

Public Member Functions

def __init__
def write
def flush
def close
def tell

Public Attributes

 compresslevel

Private Member Functions

def _write_block

Private Attributes

 _text
 _handle
 _buffer

Detailed Description

Definition at line 654 of file bgzf.py.


Constructor & Destructor Documentation

def Bio.bgzf.BgzfWriter.__init__ (   self,
  filename = None,
  mode = "w",
  fileobj = None,
  compresslevel = 6 
)

Definition at line 656 of file bgzf.py.

00656 
00657     def __init__(self, filename=None, mode="w", fileobj=None, compresslevel=6):
00658         if fileobj:
00659             assert filename is None
00660             handle = fileobj
00661         else:
00662             if "w" not in mode.lower() \
00663             and "a" not in mode.lower():
00664                 raise ValueError("Must use write or append mode, not %r" % mode)
00665             if "a" in mode.lower():
00666                 handle = __builtin__.open(filename, "ab")
00667             else:
00668                 handle = __builtin__.open(filename, "wb")
00669         self._text = "b" not in mode.lower()
00670         self._handle = handle
00671         self._buffer = _empty_bytes_string
00672         self.compresslevel = compresslevel

Here is the caller graph for this function:


Member Function Documentation

def Bio.bgzf.BgzfWriter._write_block (   self,
  block 
) [private]

Definition at line 673 of file bgzf.py.

00673 
00674     def _write_block(self, block):
00675         #print "Saving %i bytes" % len(block)
00676         start_offset = self._handle.tell()
00677         assert len(block) <= 65536
00678         #Giving a negative window bits means no gzip/zlib headers, -15 used in samtools
00679         c = zlib.compressobj(self.compresslevel,
00680                              zlib.DEFLATED,
00681                              -15,
00682                              zlib.DEF_MEM_LEVEL,
00683                              0)
00684         compressed = c.compress(block) + c.flush()
00685         del c
00686         assert len(compressed) < 65536, "TODO - Didn't compress enough, try less data in this block"
00687         crc = zlib.crc32(block)
00688         #Should cope with a mix of Python platforms...
00689         if crc < 0:
00690             crc = struct.pack("<i", crc)
00691         else:
00692             crc = struct.pack("<I", crc)
00693         bsize = struct.pack("<H", len(compressed)+25) #includes -1
00694         crc = struct.pack("<I", zlib.crc32(block) & 0xffffffffL)
00695         uncompressed_length = struct.pack("<I", len(block))
00696         #Fixed 16 bytes,
00697         # gzip magic bytes (4) mod time (4),
00698         # gzip flag (1), os (1), extra length which is six (2),
00699         # sub field which is BC (2), sub field length of two (2),
00700         #Variable data,
00701         #2 bytes: block length as BC sub field (2)
00702         #X bytes: the data
00703         #8 bytes: crc (4), uncompressed data length (4)
00704         data = _bgzf_header + bsize + compressed + crc + uncompressed_length
00705         self._handle.write(data)

Here is the caller graph for this function:

def Bio.bgzf.BgzfWriter.close (   self)
Flush data, write 28 bytes empty BGZF EOF marker, and close the BGZF file.

Definition at line 730 of file bgzf.py.

00730 
00731     def close(self):
00732         """Flush data, write 28 bytes empty BGZF EOF marker, and close the BGZF file."""
00733         if self._buffer:
00734             self.flush()
00735         #samtools will look for a magic EOF marker, just a 28 byte empty BGZF block,
00736         #and if it is missing warns the BAM file may be truncated. In addition to
00737         #samtools writing this block, so too does bgzip - so we should too.
00738         self._handle.write(_bgzf_eof)
00739         self._handle.flush()
00740         self._handle.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.bgzf.BgzfWriter.flush (   self)

Definition at line 722 of file bgzf.py.

00722 
00723     def flush(self):
00724         while len(self._buffer) >= 65536:
00725             self._write_block(self._buffer[:65535])
00726             self._buffer = self._buffer[65535:]
00727         self._write_block(self._buffer)
00728         self._buffer = _empty_bytes_string
00729         self._handle.flush()

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.bgzf.BgzfWriter.tell (   self)
Returns a BGZF 64-bit virtual offset.

Definition at line 741 of file bgzf.py.

00741 
00742     def tell(self):
00743         """Returns a BGZF 64-bit virtual offset."""
00744         return make_virtual_offset(self.handle.tell(), len(self._buffer)) 

Here is the call graph for this function:

def Bio.bgzf.BgzfWriter.write (   self,
  data 
)

Definition at line 706 of file bgzf.py.

00706 
00707     def write(self, data):
00708         #TODO - Check bytes vs unicode
00709         data = _as_bytes(data)
00710         #block_size = 2**16 = 65536
00711         data_len = len(data)
00712         if len(self._buffer) + data_len < 65536:
00713             #print "Cached %r" % data
00714             self._buffer += data
00715             return
00716         else:
00717             #print "Got %r, writing out some data..." % data
00718             self._buffer += data
00719             while len(self._buffer) >= 65536:
00720                 self._write_block(self._buffer[:65536])
00721                 self._buffer = self._buffer[65536:]

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 670 of file bgzf.py.

Definition at line 669 of file bgzf.py.

Definition at line 668 of file bgzf.py.

Definition at line 671 of file bgzf.py.


The documentation for this class was generated from the following file: