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