Package Bio :: Module File
[hide private]
[frames] | no frames]

Module File

source code

Code for more fancy file handles.

Classes:

Additional private classes used in Bio.SeqIO and Bio.SearchIO for indexing files are also defined under Bio.File but these are not intended for direct use.

Classes [hide private]
  UndoHandle
A Python handle that adds functionality for saving lines.
  _IndexedSeqFileProxy
Base class for file format specific random access (PRIVATE).
  _IndexedSeqFileDict
Read only dictionary interface to a sequential record file.
  _SQLiteManySeqFilesDict
Read only dictionary interface to many sequential record files.
Functions [hide private]
 
as_handle(*args, **kwds)
Context manager to ensure we are using a handle.
source code
 
_open_for_random_access(filename)
Open a file in binary mode, spot if it is BGZF format etc (PRIVATE).
source code
Variables [hide private]
  __package__ = 'Bio'
Function Details [hide private]

as_handle(*args, **kwds)

source code 

Context manager to ensure we are using a handle.

Context manager for arguments that can be passed to SeqIO and AlignIO read, write, and parse methods: either file objects or strings.

When given a string, returns a file handle open to handleish with provided mode which will be closed when the manager exits.

All other inputs are returned, and are not closed

  • handleish - Either a string or file handle
  • mode - Mode to open handleish (used only if handleish is a string)
  • kwargs - Further arguments to pass to open(...)

Example:

>>> with as_handle('seqs.fasta', 'w') as fp:
...     fp.write('>test\nACGT')
>>> fp.closed
True
>>> handle = open('seqs.fasta', 'w')
>>> with as_handle(handle) as fp:
...     fp.write('>test\nACGT')
>>> fp.closed
False
>>> fp.close()

Note that if the mode argument includes U (for universal new lines) this will be removed under Python 3 where is is redundant and has been deprecated (this happens automatically in text mode).

Decorators:
  • @contextlib.contextmanager

_open_for_random_access(filename)

source code 

Open a file in binary mode, spot if it is BGZF format etc (PRIVATE).

This funcationality is used by the Bio.SeqIO and Bio.SearchIO index and index_db functions.