[Biopython-dev] [Biopython] Subclassing Seq and SeqRecord
Jose Blanca
jblanca at btc.upv.es
Tue Nov 24 12:31:04 UTC 2009
> 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)
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
--
Jose M. Blanca Postigo
Instituto Universitario de Conservacion y
Mejora de la Agrodiversidad Valenciana (COMAV)
Universidad Politecnica de Valencia (UPV)
Edificio CPI (Ciudad Politecnica de la Innovacion), 8E
46022 Valencia (SPAIN)
Tlf.:+34-96-3877000 (ext 88473)
More information about the Biopython-dev
mailing list