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   
  6  """Common SearchIO utility functions.""" 
  7   
  8   
9 -def get_processor(format, mapping):
10 """Returns the object to process the given format according to the mapping. 11 12 Arguments: 13 format -- Lower case string denoting one of the supported formats. 14 mapping -- Dictionary of format and object name mapping. 15 16 """ 17 # map file format to iterator name 18 try: 19 obj_info = mapping[format] 20 except KeyError: 21 # handle the errors with helpful messages 22 if format is None: 23 raise ValueError("Format required (lower case string)") 24 elif not isinstance(format, basestring): 25 raise TypeError("Need a string for the file format (lower case)") 26 elif format != format.lower(): 27 raise ValueError("Format string %r should be lower case" % 28 format) 29 else: 30 raise ValueError("Unknown format %r. Supported formats are " 31 "%r" % (format, "', '".join(mapping.keys()))) 32 33 mod_name, obj_name = obj_info 34 mod = __import__('Bio.SearchIO.%s' % mod_name, fromlist=['']) 35 36 return getattr(mod, obj_name)
37 38
39 -def singleitem(attr=None, doc=''):
40 """Returns a property that fetches the given attribute from 41 the first item in a SearchIO container object.""" 42 def getter(self): 43 if len(self._items) > 1: 44 raise ValueError("More than one HSPFragment objects " 45 "found in HSP") 46 if attr is None: 47 return self._items[0] 48 return getattr(self._items[0], attr)
49 return property(fget=getter, doc=doc) 50 51
52 -def allitems(attr=None, doc=''):
53 """Returns a property that fetches the given attributes from 54 all items in a SearchIO container object.""" 55 def getter(self): 56 if attr is None: 57 return self._items 58 return [getattr(frag, attr) for frag in self._items]
59 return property(fget=getter, doc=doc) 60 61
62 -def partialcascade(cont_attr, item_attr, doc=''):
63 """Returns a getter property with a cascading setter. 64 65 This is used for the `id` and `description` properties of the container 66 objects. These items have their own private attributes that stores query 67 and/or hit ID and description. To keep the container items' query and/or 68 hit ID and description in-sync, the setter cascades any new value given 69 to the items' values as well. 70 71 """ 72 def getter(self): 73 return getattr(self, cont_attr)
74 75 def setter(self, value): 76 setattr(self, cont_attr, value) 77 for item in self: 78 setattr(item, item_attr, value) 79 80 return property(fget=getter, fset=setter, doc=doc) 81 82
83 -def fullcascade(attr, doc=''):
84 """Returns a getter property with a cascading setter. 85 86 This is similar to `partialcascade`, but for SearchIO containers that have 87 at least one item (Hit and HSP). The getter always retrieves the attribute 88 value from the first item. If the items have more than one attribute values, 89 an error will be raised. The setter behaves like `partialcascade`, except 90 that it only sets attributes to items in the object, not the object itself. 91 92 """ 93 def getter(self): 94 attrset = set([getattr(item, attr) for item in self._items]) 95 if len(attrset) != 1: 96 if len(attrset) > 1: 97 raise ValueError("More than one value present in the contained" 98 " %s objects: %r" % (self._items[0].__class__.__name__, 99 list(attrset))) 100 else: 101 raise AttributeError("%r attribute requires %s objects to be " 102 "filled" % (attr, self.__class__.__name__)) 103 104 return getattr(self._items[0], attr)
105 106 def setter(self, value): 107 for item in self: 108 setattr(item, attr, value) 109 110 return property(fget=getter, fset=setter, doc=doc) 111
112 -def optionalcascade(attr, doc=''):
113 """Returns a getter property with a cascading setter. 114 115 This is similar to `fullcascade`, but for SearchIO containers that have 116 at zero or more items. The getter always tries to retrieve the attribute 117 value from the first item, but falls back to the value in the container. 118 If the items have more than one attribute values, an error will be raised. 119 The setter behaves like `partialcascade`. 120 121 """ 122 def getter(self): 123 attrset = set([getattr(item, attr) for item in self._items]) 124 if len(attrset) != 1: 125 if len(attrset) > 1: 126 raise ValueError("More than one value present in the contained" 127 " %s objects: %r" % (self._items[0].__class__.__name__, 128 list(attrset))) 129 else: 130 return getattr(self, "_%s" % attr) 131 132 return getattr(self._items[0], attr)
133 134 def setter(self, value): 135 setattr(self, "_%s" % attr, value) 136 for item in self: 137 setattr(item, attr, value) 138 139 return property(fget=getter, fset=setter, doc=doc) 140 141
142 -def fragcascade(attr, seq_type, doc=''):
143 """Returns a getter property with cascading setter, for HSPFragment objects. 144 145 Similar to `partialcascade`, but for HSPFragment objects and acts on `query` 146 or `hit` properties of the object if they are not None. 147 148 """ 149 assert seq_type in ('hit', 'query') 150 attr_name = '_%s_%s' % (seq_type, attr) 151 152 def getter(self): 153 return getattr(self, attr_name)
154 155 def setter(self, value): 156 setattr(self, attr_name, value) 157 seq = getattr(self, seq_type) 158 if seq is not None: 159 setattr(seq, attr, value) 160 161 return property(fget=getter, fset=setter, doc=doc) 162