slicing functionality for strings / Python suitability forbioinformatics
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Tue Sep 20 18:55:04 EDT 2005
On Mon, 19 Sep 2005 19:40:12 -0700, jbperez808 wrote:
> Having to do an array.array('c',...):
>
> >>> x=array.array('c','ATCTGACGTC')
> >>> x[1:9:2]=array.array('c','AAAA')
> >>> x.tostring()
> 'AACAGACATC'
>
> is a bit klunkier than one would want, but I guess
> the efficient performance is the silver lining here.
There are a number of ways to streamline that. The simplest is to merely
create an alias to array.array:
from array import array as str
Then you can say x = str('c', 'ATCTGACGTC').
A little more sophisticated would be to use currying:
def str(value):
return array.array('c', value)
x = str('ATCTGACGTC')
although to be frank I'm not sure that something as simple as this
deserves to be dignified with the name currying.
Lastly, you could create a wrapper class that implements everything you
want. For a serious application, this is probably what you want to do
anyway:
class DNA_Sequence:
alphabet = 'ACGT'
def __init__(self, value):
for c in value:
if c not in self.__class__.alphabet:
raise ValueError('Illegal character "%s".' % c)
self.value = array.array('c', value)
def __repr__(self):
return self.value.tostring()
and so on. Obviously you will need more work than this, and it may be
possible to subclass array directly.
--
Steven.
More information about the Python-list
mailing list