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 path-like objects (strings, pathlib.Path instances, or more generally, anything that can be handled by the builtin 'open' function).

When given a path-like object, returns an open file handle to that path, with provided mode, which will be closed when the manager exits.

All other inputs are returned, and are not closed.

Arguments:
  • handleish - Either a file handle or path-like object (anything which can be

    passed to the builtin 'open' function: str, bytes, and under Python >= 3.6, pathlib.Path, os.DirEntry)

  • mode - Mode to open handleish (used only if handleish is a string)

  • kwargs - Further arguments to pass to open(...)

Examples

>>> 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 functionality is used by the Bio.SeqIO and Bio.SearchIO index and index_db functions.