Package Bio :: Package SearchIO :: Module _utils
[hide private]
[frames] | no frames]

Source Code for Module Bio.SearchIO._utils

  1  # Copyright 2012 by Wibowo Arindrarto.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5  """Common SearchIO utility functions.""" 
  6   
  7  from Bio._py3k import basestring 
  8   
  9   
10 -def get_processor(format, mapping):
11 """Returns the object to process the given format according to the mapping. 12 13 :param format: format name 14 :type format: string, lower case 15 :param mapping: mapping of format name and its processor object 16 :type mapping: dictionary {string: object} 17 18 """ 19 # map file format to iterator name 20 try: 21 obj_info = mapping[format] 22 except KeyError: 23 # handle the errors with helpful messages 24 if format is None: 25 raise ValueError("Format required (lower case string)") 26 elif not isinstance(format, basestring): 27 raise TypeError("Need a string for the file format (lower case)") 28 elif format != format.lower(): 29 raise ValueError("Format string %r should be lower case" % 30 format) 31 else: 32 raise ValueError("Unknown format %r. Supported formats are " 33 "%r" % (format, "', '".join(mapping))) 34 35 mod_name, obj_name = obj_info 36 mod = __import__('Bio.SearchIO.%s' % mod_name, fromlist=['']) 37 38 return getattr(mod, obj_name)
39 40
41 -def singleitem(attr=None, doc=''):
42 """Returns a property that fetches the given attribute from 43 the first item in a SearchIO container object. 44 """ 45 def getter(self): 46 if len(self._items) > 1: 47 raise ValueError("More than one HSPFragment objects " 48 "found in HSP") 49 if attr is None: 50 return self._items[0] 51 return getattr(self._items[0], attr)
52 return property(fget=getter, doc=doc) 53 54
55 -def allitems(attr=None, doc=''):
56 """Returns a property that fetches the given attributes from 57 all items in a SearchIO container object. 58 """ 59 def getter(self): 60 if attr is None: 61 return self._items 62 return [getattr(frag, attr) for frag in self._items]
63 return property(fget=getter, doc=doc) 64 65
66 -def fullcascade(attr, doc=''):
67 """Returns a getter property with a cascading setter. 68 69 This is similar to ``optionalcascade``, but for SearchIO containers that have 70 at least one item (HSP). The getter always retrieves the attribute 71 value from the first item. If the items have more than one attribute values, 72 an error will be raised. The setter behaves like ``partialcascade``, except 73 that it only sets attributes to items in the object, not the object itself. 74 75 """ 76 def getter(self): 77 return getattr(self._items[0], attr)
78 79 def setter(self, value): 80 for item in self: 81 setattr(item, attr, value) 82 83 return property(fget=getter, fset=setter, doc=doc) 84 85
86 -def optionalcascade(cont_attr, item_attr, doc=''):
87 """Returns a getter property with a cascading setter. 88 89 This is used for the ``id`` and ``description`` properties of the container 90 objects with zero or more items. These items have their own private 91 attributes that stores query and/or hit ID and description. When the 92 container has zero items, attribute values are always retrieved from the 93 container's attribute. Otherwise, the first item's attribute is used. 94 95 To keep the container items' query and/or hit ID and description in-sync, 96 the setter cascades any new value given to the items' values. 97 98 """ 99 def getter(self): 100 if self._items: 101 # don't use self._items here, so QueryResult can use this property 102 # as well (the underlying OrderedDict is not integer-indexable) 103 return getattr(self[0], item_attr) 104 else: 105 return getattr(self, cont_attr)
106 107 def setter(self, value): 108 setattr(self, cont_attr, value) 109 for item in self: 110 setattr(item, item_attr, value) 111 112 return property(fget=getter, fset=setter, doc=doc) 113 114
115 -def fragcascade(attr, seq_type, doc=''):
116 """Returns a getter property with cascading setter, for HSPFragment objects. 117 118 Similar to ``partialcascade``, but for HSPFragment objects and acts on ``query`` 119 or ``hit`` properties of the object if they are not None. 120 121 """ 122 assert seq_type in ('hit', 'query') 123 attr_name = '_%s_%s' % (seq_type, attr) 124 125 def getter(self): 126 return getattr(self, attr_name)
127 128 def setter(self, value): 129 setattr(self, attr_name, value) 130 seq = getattr(self, seq_type) 131 if seq is not None: 132 setattr(seq, attr, value) 133 134 return property(fget=getter, fset=setter, doc=doc) 135