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

Source Code for Module Bio.SearchIO._model._base

 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  """Abstract base classes for the SearchIO object model.""" 
 7   
 8   
 9  from Bio._utils import getattr_str, trim_str 
10   
11   
12  __docformat__ = "restructuredtext en" 
13   
14   
15 -class _BaseSearchObject(object):
16 17 """Abstract class for SearchIO objects.""" 18 19 _NON_STICKY_ATTRS = () 20
21 - def _transfer_attrs(self, obj):
22 """Transfer instance attributes to the given object. 23 24 This method is used to transfer attributes set externally (for example 25 using `setattr`) to a new object created from this one (for example 26 from slicing). 27 28 The reason this method is necessary is because different parsers will 29 set different attributes for each QueryResult, Hit, HSP, or HSPFragment 30 objects, depending on the attributes they found in the search output 31 file. Ideally, we want these attributes to 'stick' with any new instance 32 object created from the original one. 33 34 """ 35 # list of attribute names we don't want to transfer 36 for attr in self.__dict__: 37 if attr not in self._NON_STICKY_ATTRS: 38 setattr(obj, attr, self.__dict__[attr])
39 40
41 -class _BaseHSP(_BaseSearchObject):
42 43 """Abstract base class for HSP objects.""" 44
45 - def _str_hsp_header(self):
46 """Prints the alignment header info.""" 47 lines = [] 48 # set query id line 49 qid_line = trim_str(' Query: %s %s' % 50 (self.query_id, self.query_description), 80, '...') 51 # set hit id line 52 hid_line = trim_str(' Hit: %s %s' % 53 (self.hit_id, self.hit_description), 80, '...') 54 lines.append(qid_line) 55 lines.append(hid_line) 56 57 # coordinates 58 query_start = getattr_str(self, 'query_start') 59 query_end = getattr_str(self, 'query_end') 60 hit_start = getattr_str(self, 'hit_start') 61 hit_end = getattr_str(self, 'hit_end') 62 63 # strands 64 try: 65 qstrand = self.query_strand 66 hstrand = self.hit_strand 67 except ValueError: 68 qstrand = self.query_strand_all[0] 69 hstrand = self.hit_strand_all[0] 70 lines.append('Query range: [%s:%s] (%r)' % (query_start, query_end, 71 qstrand)) 72 lines.append(' Hit range: [%s:%s] (%r)' % (hit_start, hit_end, 73 hstrand)) 74 75 return '\n'.join(lines)
76