[Biopython] extending Motif class

Philip Machanick philip.machanick at gmail.com
Thu Mar 31 19:49:33 EDT 2011


I want to add a new scoring function to the Motif class and in true
object-oriented spirit would like to do it by deriving a new class rather
than hacking the existing code.

The general structure of my test program (all in 1 file) is:

from Bio.Motif import Motif

class ScannableMotif(Motif):
    def pwm_score_hit(self,sequence,position):
    ## stuff to compute my new score

from Bio import Motif
def main ():
    for motif in
ScannableMotif.parse(open("/Users/philip/tmp/meme.txt"),"MEME"):
        for i in range(3):
          print
motif.pwm_score_hit("CCTGGGGTCCCATTTCTCTTTTCTCTCCTGGGGTCCC",i)

The two different imports appear to be necessary. I need the first to be
able to use the base class to derive a new one, and without the second when
I use metaclass methods, I get

TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

The other problem: I can't directly invoke a metaclass method on a derived
instance as above. The snippet below works as expected, but looks like a
kludge to me. Is there a better way of accessing metaclass methods from a
derived class object?

    for motif in Motif.parse(open("/Users/philip/tmp/meme.txt"),"MEME"):
        motif.__class__ = ScannableMotif # promote to the new class
        for i in range(3):
          print
motif.pwm_score_hit("CCTGGGGTCCCATTTCTCTTTTCTCTCCTGGGGTCCC",i)

I think I have the class vs. metaclass concept straight but understanding
why I need the two different flavours of import would be useful.
-- 
Philip Machanick
Rhodes University, Grahamstown 6140, South Africa
http://opinion-nation.blogspot.com/
+61-7-3871-0963 mobile +61 42 234 6909 skype philipmach


More information about the Biopython mailing list