[Biopython-dev] [Biopython] Subclassing Seq and SeqRecord

Peter biopython at maubp.freeserve.co.uk
Tue Nov 24 13:10:15 UTC 2009


On Tue, Nov 24, 2009 at 12:31 PM, Jose Blanca <jblanca at btc.upv.es> wrote:
>
>> It is a reasonable change, but ONLY if all the subclasses support
>> the same __init__ method, which isn't true. For example, the
>> Bio.Seq.UnknownSeq subclasses Seq and uses a different __init__
>> method signature. This means any change would at a minimum
>> have to include lots of fixes to the UnknownSeq
>
> In this case what I do is to create a new __init__ for the inherited class,
> like:
>
> class SeqWithQuality(SeqRecord):
>    '''A wrapper around Biopython's SeqRecord that adds a couple of
> convenience methods'''
>    def __init__(self, seq, id = "<unknown id>", name = "<unknown name>",
>                 description = "<unknown description>", dbxrefs = None,
>                 features = None, annotations = None,
>                 letter_annotations = None, qual = None):
>        SeqRecord.__init__(self, seq, id=id, name=name,
>                           description=description, dbxrefs=dbxrefs,
>                           features=features, annotations=annotations,
>                           letter_annotations=letter_annotations)
>        if qual is not None:
>            self.qual = qual
>
>    def _set_qual(self, qual):
>        '''It stores the quality in the letter_annotations['phred_quality']'''
>        self.letter_annotations["phred_quality"] = qual
>    def _get_qual(self):
>        '''It gets the quality from letter_annotations['phred_quality']'''
>        return self.letter_annotations["phred_quality"]
>    qual = property(_get_qual, _set_qual)

I can see how adding a property makes accessing the PHRED
qualities much easier.

>    def __add__(self, seq2):
>        '''It returns a new object with both seq and qual joined '''
>        #per letter annotations
>        new_seq = self.__class__(name = self.name + '+' + seq2.name,
>                                 id = self.id + '+' + seq2.id,
>                                 seq  = self.seq + seq2.seq)
>        #the letter annotations, including quality
>        for name, annot in self.letter_annotations.items():
>            if name in seq2.letter_annotations:
>                new_seq.letter_annotations[name] = annot + \
>                                         seq2.letter_annotations[name]
>        return new_seq

This bit is much less clear to me - you completely ignore any
features. Was it written before I added the __add__ method
to the original SeqRecord (expected to be in Biopython 1.53)?

Anyway - it looks like your SeqRecord subclass should work
fine as it is (partly because the SeqRecord has relatively
few methods you may need to subclass).

Peter




More information about the Biopython-dev mailing list