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