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 def getter(self): 45 if len(self._items) > 1: 46 raise ValueError("More than one HSPFragment objects " 47 "found in HSP") 48 if attr is None: 49 return self._items[0] 50 return getattr(self._items[0], attr)
51 return property(fget=getter, doc=doc) 52 53
54 -def allitems(attr=None, doc=''):
55 """Returns a property that fetches the given attributes from 56 all items in a SearchIO container object.""" 57 def getter(self): 58 if attr is None: 59 return self._items 60 return [getattr(frag, attr) for frag in self._items]
61 return property(fget=getter, doc=doc) 62 63
64 -def fullcascade(attr, doc=''):
65 """Returns a getter property with a cascading setter. 66 67 This is similar to ``optionalcascade``, but for SearchIO containers that have 68 at least one item (HSP). The getter always retrieves the attribute 69 value from the first item. If the items have more than one attribute values, 70 an error will be raised. The setter behaves like ``partialcascade``, except 71 that it only sets attributes to items in the object, not the object itself. 72 73 """ 74 def getter(self): 75 return getattr(self._items[0], attr)
76 77 def setter(self, value): 78 for item in self: 79 setattr(item, attr, value) 80 81 return property(fget=getter, fset=setter, doc=doc) 82 83
84 -def optionalcascade(cont_attr, item_attr, doc=''):
85 """Returns a getter property with a cascading setter. 86 87 This is used for the ``id`` and ``description`` properties of the container 88 objects with zero or more items. These items have their own private 89 attributes that stores query and/or hit ID and description. When the 90 container has zero items, attribute values are always retrieved from the 91 container's attribute. Otherwise, the first item's attribute is used. 92 93 To keep the container items' query and/or hit ID and description in-sync, 94 the setter cascades any new value given to the items' values. 95 96 """ 97 def getter(self): 98 if self._items: 99 # don't use self._items here, so QueryResult can use this property 100 # as well (the underlying OrderedDict is not integer-indexable) 101 return getattr(self[0], item_attr) 102 else: 103 return getattr(self, cont_attr)
104 105 def setter(self, value): 106 setattr(self, cont_attr, value) 107 for item in self: 108 setattr(item, item_attr, value) 109 110 return property(fget=getter, fset=setter, doc=doc) 111 112
113 -def fragcascade(attr, seq_type, doc=''):
114 """Returns a getter property with cascading setter, for HSPFragment objects. 115 116 Similar to ``partialcascade``, but for HSPFragment objects and acts on ``query`` 117 or ``hit`` properties of the object if they are not None. 118 119 """ 120 assert seq_type in ('hit', 'query') 121 attr_name = '_%s_%s' % (seq_type, attr) 122 123 def getter(self): 124 return getattr(self, attr_name)
125 126 def setter(self, value): 127 setattr(self, attr_name, value) 128 seq = getattr(self, seq_type) 129 if seq is not None: 130 setattr(seq, attr, value) 131 132 return property(fget=getter, fset=setter, doc=doc) 133