Skip to content

Commit

Permalink
BGZF function name 'open' for consistency with gzip library
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc committed Jun 21, 2012
1 parent 3b295cc commit 128f204
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Bio/SeqIO/_index.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def __init__(self, filename, format, alphabet, compression):
elif compression=="gzip": elif compression=="gzip":
self._handle = gzip.open(filename, "rb") self._handle = gzip.open(filename, "rb")
elif compression=="bgzf": elif compression=="bgzf":
self._handle = bgzf.bgzf_open(filename, "rb") self._handle = bgzf.open(filename, "rb")
else: else:
raise ValueError("Compression type %r not supported" % compression) raise ValueError("Compression type %r not supported" % compression)
self._alphabet = alphabet self._alphabet = alphabet
Expand Down
39 changes: 38 additions & 1 deletion Bio/bgzf.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -74,6 +74,38 @@
compression (useful for intermediate files in memory to reduced compression (useful for intermediate files in memory to reduced
CPU load). CPU load).
Warning about namespaces
------------------------
It is consider a bad idea to use "from XXX import *" in Python, because
it pollutes the names space. This is a real issue with Bio.bgzf (and the
standard Python library gzip) because they contain a function called open
i.e. Suppose you do this:
>>> from Bio.bgzf import *
>>> print open.__module__
Bio.bgzf
Or,
>>> from gzip import *
>>> print open.__module__
gzip
Notice that the open function has been replaced. You can "fix" this if you
need to by importing the built-in open function:
>>> from __builtin__ import open
>>> print open.__module__
__builtin__
However, what we recommend instead is to keep the name space, e.g.
>>> from Bio import bgzf
>>> print bgzf.open.__module__
Bio.bgzf
>>> print open.__module__
__builtin__
Example Example
------- -------
Expand Down Expand Up @@ -180,6 +212,7 @@
import zlib import zlib
import struct import struct
import __builtin__ #to access the usual open function import __builtin__ #to access the usual open function

from Bio._py3k import _as_bytes, _as_string from Bio._py3k import _as_bytes, _as_string


#For Python 2 can just use: _bgzf_magic = '\x1f\x8b\x08\x04' #For Python 2 can just use: _bgzf_magic = '\x1f\x8b\x08\x04'
Expand All @@ -191,7 +224,9 @@
_empty_bytes_string = _as_bytes("") _empty_bytes_string = _as_bytes("")
_bytes_newline = _as_bytes("\n") _bytes_newline = _as_bytes("\n")


def bgzf_open(filename, mode="rb"):
def open(filename, mode="rb"):
"""Open a BGZF file for reading, writing or appending."""
if "r" in mode.lower(): if "r" in mode.lower():
return BgzfReader(filename, mode) return BgzfReader(filename, mode)
elif "w" in mode.lower() or "a" in mode.lower(): elif "w" in mode.lower() or "a" in mode.lower():
Expand Down Expand Up @@ -269,6 +304,7 @@ def BgzfBlocks(handle):
decompressed length of the blocks contents (limited to 65536 in decompressed length of the blocks contents (limited to 65536 in
BGZF). BGZF).
>>> from __builtin__ import open
>>> handle = open("SamBam/ex1.bam", "rb") >>> handle = open("SamBam/ex1.bam", "rb")
>>> for values in BgzfBlocks(handle): >>> for values in BgzfBlocks(handle):
... print "Start %i, length %i, data %i" % values ... print "Start %i, length %i, data %i" % values
Expand Down Expand Up @@ -387,6 +423,7 @@ class BgzfReader(object):
Let's use the BgzfBlocks function to have a peak at the BGZF blocks Let's use the BgzfBlocks function to have a peak at the BGZF blocks
in an example BAM file, in an example BAM file,
>>> from __builtin__ import open
>>> handle = open("SamBam/ex1.bam", "rb") >>> handle = open("SamBam/ex1.bam", "rb")
>>> for values in BgzfBlocks(handle): >>> for values in BgzfBlocks(handle):
... print "Start %i, length %i, data %i" % values ... print "Start %i, length %i, data %i" % values
Expand Down

0 comments on commit 128f204

Please sign in to comment.