[Biopython-dev] Quality scores (and per-letter-annotation) in a SeqRecord?

Jose Blanca jblanca at btc.upv.es
Tue Feb 24 10:24:07 UTC 2009


On Monday 23 February 2009 19:34:18 Peter wrote:
> class RestrictedDict(dict):
>     """A dictionary which only allows sequences of given length as
> values.""" def __init__(self, length) :
>         """Create an EMPTY dictionary."""
>         dict.__init__(self)
>         self._length = int(length)
>     def __setitem__(self, key, value) :
>         if not hasattr(value,"__len__") or not hasattr(value,"__getitem__")
> \ or len(value) != self._length :
>             raise TypeError("We only allow python sequences (lists,
> tuples or strings) of length %i." % self._length)
>         dict.__setitem__(self, key, value)

An alternternative implementation using weakref to link the RestrictedDict 
with the SeqRecord.

class RestrictedDict(dict):
    """A dictionary which only allows sequences of the same length as the 
parent
as values."""
    def __init__(self, parent):
        """Create an empty dictionary."""
        dict.__init__(self)
        import weakref
        self._parent = weakref.ref(parent)
    def __setitem__(self, key, value):
        attrs = dir(value)
        if not "__len__" in attrs or not "__getitem__" in attrs:
            raise TypeError("We only allow python sequences (lists, tuples or 
strings)")
        if len(value) != len(self._parent()):
            raise TypeError('Lengths do not match.')
        dict.__setitem__(self, key, value)

And in the SeqRecord __init__ we should add:
        #letter_annotations
        self.letter_annotations = RestrictedDict(self)

-- 
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