[Biopython] extending Motif class

Brad Chapman chapmanb at 50mail.com
Fri Apr 1 00:59:52 UTC 2011


Philip;

> 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 approach you want to take here is to define a function
that takes a motif as an input:

def pwm_score_hit(motif, sequence, position):

instead of trying to inherit from Motif. What happens in 
your example is that you inherit from the Motif class:

> from Bio.Motif import Motif
> 
> class ScannableMotif(Motif):
>     def pwm_score_hit(self,sequence,position):
>     ## stuff to compute my new score

Then you call ScannableMotif as if it were the Motif namespace, when
it is actually a class. The parse function is defined in Bio.Motif
not the Motif class:

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

Which is why you get an error. Your promotion trick does work but
really is too tricky and you are better off with just a separate
function that works on motif objects.

Hope this helps,
Brad


More information about the Biopython mailing list