[Biopython-dev] creating Protein(structure) object

Kristian Rother krother at rubor.de
Mon Jun 14 15:13:19 UTC 2010


Hi Joao,

what you are describing is the classical Decorator Pattern (see
http://en.wikipedia.org/wiki/Decorator_pattern). In the books, they say
that the Decorator (Protein) must implement all methods of the decorated
object (Structure).
Of course, for a class as big as Bio.PDB.Structure, this sucks a lot. I
see two alternatives:

(1) override Protein.__getattr__(self, attr) to return self.struc.attr if
it exists. I tried this recently and it worked fine until the decorated
class used Python properties, when it started getting ugly again.

(2) have Protein inherit from Structure, and grab all the children from
the structure class, e.g.:

class Protein(Structure):
    def __init__(self, struc):
        """
        The given Structure instance becomes a Protein.
        """
        Structure.__init__(self, struc.id)
        for child in struc.child_list:
            # eventually check if its a protein chain.
            self.add_child(child)


Any comments?
    Kristian



> Hello all,
>
> I'm having some issues dealing with this :x
>
> I created a module Bio.Struct that has the following contents:
>
> __init__.py
> Protein.py
> WWW/
>
> The __init__.py file has a read() method that calls PDBParser and returns a
> Structure object. So far so good I think. Then I added a method to
> Bio.PDB.Structure more or less like this:
>
>     def as_protein(self):
>         from Bio.Struct.Protein import Protein
>         prot = Protein(self)
>         return prot
>
> so when you call it you get a new object. Protein is a class that inherits
> from Structure and that has the search_ss_bonds function.
>
> I can make the new object get all the methods from Structure AND from
> Protein, but when I try to execute search_ss_bonds, it fails because
> child_list, a Structure method, comes empty.. In fact, the whole SMCRA
> object comes empty..
>
> How do I effectively do the inheritance on the Protein class?
>
> from Bio.PDB.Structure import Structure
>
> class Protein(Structure):
>
>     def __init__(self, protein):
>
>         self = protein
>
> This is what I last tried and doesn't work.. I've tried Structure.__init__,
> and several other things but to no avail. I'm sure this is simple OOP but I
> really can't understand that well how to do it ...
>
> Care to give a hand to a friend in need? :)
>
> Thanks in advance! By the way, I assume that if I got no comments on
> anything else on the GSOC thread that I'm doing a perfect job :P Thanks for
> that too :D
>
> Best!
>
> João [...] Rodrigues
>
> _______________________________________________
> Biopython-dev mailing list
> Biopython-dev at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/biopython-dev
>
>
>
>





More information about the Biopython-dev mailing list