[BioPython] Question about Seq.count()

Peter biopython at maubp.freeserve.co.uk
Tue Oct 30 12:32:10 UTC 2007


Peter wrote:
>> I've found the bug!
>>
>> The code for Bio.Seq.count is:
>>
>> def count(self, item):
>>         return len([x for x in self.data if x == item])
> 
> Yeah - by design this (and the functionally similar version for the
> MutableSeq) both expect the count argument to be a single letter.  The
> simple fix for the Seq object is to use the string method internally:
> 
> def count(self, item):
>         return self.data.count(item)
> 
> For the MutableSeq things are not so straight forward, but supporting
> multiple character arguments can be done.

Bug 2386 and proposed patch here:
http://bugzilla.open-bio.org/show_bug.cgi?id=2386

This also lets the count methods take Seq or MutableSeq objects as 
arguments - in addition to plain strings.

Note there is room for improvement in my patch: For the case of the 
MutableSeq, we might want to investigate counting from the array of 
characters directly, rather than taking the lazy option of turning it 
into a string and counting that way.

Peter




More information about the Biopython mailing list