Skip to content

Commit

Permalink
Use Bio.File._IndexedSeqFileDict in Bio.SearchIO.index(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc committed Dec 2, 2012
1 parent f7e072f commit f9cc32f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
12 changes: 6 additions & 6 deletions Bio/File.py
Expand Up @@ -253,10 +253,13 @@ class _IndexedSeqFileDict(_dict_base):
Note that this dictionary is essentially read only. You cannot
add or change values, pop values, nor clear the dictionary.
"""
def __init__(self, random_access_proxy, key_function):
def __init__(self, random_access_proxy, key_function,
repr, obj_repr):
#Use key_function=None for default value
self._proxy = random_access_proxy
self._key_function = key_function
self._repr = repr
self._obj_repr = obj_repr
if key_function:
offset_iter = (
(key_function(k), o, l) for (k, o, l) in random_access_proxy)
Expand All @@ -281,15 +284,12 @@ def __init__(self, random_access_proxy, key_function):
self._offsets = offsets

def __repr__(self):
#TODO - How best to handle the __repr__ for SeqIO and SearchIO?
return "SeqIO.index(%r, %r, alphabet=%r, key_function=%r)" \
% (self._proxy._handle.name, self._proxy._format,
self._proxy._alphabet, self._key_function)
return self._repr

def __str__(self):
#TODO - How best to handle the __str__ for SeqIO and SearchIO?
if self:
return "{%s : SeqRecord(...), ...}" % repr(self.keys()[0])
return "{%r : %s(...), ...}" % (self.keys()[0], self._obj_repr)
else:
return "{}"

Expand Down
8 changes: 6 additions & 2 deletions Bio/SearchIO/__init__.py
Expand Up @@ -463,8 +463,12 @@ def index(filename, format=None, key_function=None, **kwargs):
if not isinstance(filename, basestring):
raise TypeError("Need a filename (not a handle)")

from Bio.SearchIO._index import _IndexedSearch
return _IndexedSearch(filename, format, key_function, **kwargs)
from Bio.File import _IndexedSeqFileDict
proxy_class = get_processor(format, _INDEXER_MAP)
repr = "SearchIO.index(%r, %r, key_function=%r)" \
% (filename, format, key_function)
return _IndexedSeqFileDict(proxy_class(filename, **kwargs),
key_function, repr, "QueryResult")


def index_db(index_filename, filenames=None, format=None,
Expand Down
4 changes: 3 additions & 1 deletion Bio/SeqIO/__init__.py
Expand Up @@ -802,8 +802,10 @@ def index(filename, format, alphabet=None, key_function=None):
proxy_class = _FormatToRandomAccess[format]
except KeyError:
raise ValueError("Unsupported format %r" % format)
repr = "SeqIO.index(%r, %r, alphabet=%r, key_function=%r)" \
% (filename, format, alphabet, key_function)
return _IndexedSeqFileDict(proxy_class(filename, format, alphabet),
key_function)
key_function, repr, "SeqRecord")


def index_db(index_filename, filenames=None, format=None, alphabet=None,
Expand Down

0 comments on commit f9cc32f

Please sign in to comment.