473,246 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

slicing functionality for strings / Python suitability for bioinformatics

>>> rs='AUGCUAGACGUGGAGUAG'
rs[12:15]='GAG'

Traceback (most recent call last):
File "<pyshell#119>", line 1, in ?
rs[12:15]='GAG'
TypeError: object doesn't support slice assignment

You can't assign to a section of a sliced string in
Python 2.3 and there doesn't seem to be mention of this
as a Python 2.4 feature (don't have time to actually try
2.4 yet).

Q1. Does extended slicing make use of the Sequence protocol?
Q2. Don't strings also support the Sequence protcol?
Q3. Why then can't you make extended slicing assignment work
when dealing with strings?

This sort of operation (slicing/splicing of sequences represented
as strings) would seem to be a very fundamental oepration when doing
rna/dna/protein sequencing algorithms, and it would greatly enhance
Python's appeal to those doing bioinformatics work if the slicing
and extended slicing operators worked to their logical limit.

Doing a cursory search doesn't seem to reveal any current PEPs
dealing with extending the functionality of slicing/extended
slicing operators.

Syntax and feature-wise, is there a reason why Python can't kick
Perl's butt as the dominant language for bioinformatics and
eventually become the lingua franca of this fast-growing and
funding-rich field?

Sep 19 '05 #1
11 2010
jb********@yahoo.com wrote:
rs='AUGCUAGACGUGGAGUAG'
rs[12:15]='GAG'

Traceback (most recent call last):
File "<pyshell#119>", line 1, in ?
rs[12:15]='GAG'
TypeError: object doesn't support slice assignment

You can't assign to a section of a sliced string in
Python 2.3 and there doesn't seem to be mention of this
as a Python 2.4 feature (don't have time to actually try
2.4 yet).


Strings are immutable in Python, which is why assignment to
slices won't work.

But why not use lists?

rs = list('AUGC...')
rs[12:15] = list('GAG')

Reinhold
Sep 19 '05 #2

"Reinhold Birkenfeld" <re************************@wolke7.net> wrote in
message news:3p************@individual.net...
jb********@yahoo.com wrote:
> rs='AUGCUAGACGUGGAGUAG'
> rs[12:15]='GAG'

Traceback (most recent call last):
File "<pyshell#119>", line 1, in ?
rs[12:15]='GAG'
TypeError: object doesn't support slice assignment

You can't assign to a section of a sliced string in
Python 2.3 and there doesn't seem to be mention of this
as a Python 2.4 feature (don't have time to actually try
2.4 yet).


Strings are immutable in Python, which is why assignment to
slices won't work.

But why not use lists?

rs = list('AUGC...')
rs[12:15] = list('GAG')


Or arrays of characters: see the array module.

Terry J. Reedy

Sep 19 '05 #3
right, i forgot about that...

Sep 20 '05 #4
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.

Sep 20 '05 #5
On 19 Sep 2005 12:25:16 -0700, jb********@yahoo.com
<jb********@yahoo.com> wrote:
rs='AUGCUAGACGUGGAGUAG'
rs[12:15]='GAG'

Traceback (most recent call last):
File "<pyshell#119>", line 1, in ?
rs[12:15]='GAG'
TypeError: object doesn't support slice assignment


You should try Biopython (www.biopython.org). There is a sequence
method you could try.

--
<a href="http://www.spreadfirefox.com/?q=affiliates&id=24672&t=1">La
web sin popups ni spyware: Usa Firefox en lugar de Internet
Explorer</a>
Sep 20 '05 #6
Great suggestion... I was naively trying to turn the string into a list
and slice
that which I reckon would be significantly slower.

Sep 20 '05 #7
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.

Sep 21 '05 #8
On Wed, 21 Sep 2005, Steven D'Aprano wrote:
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')


There's a special hell for people who override builtins.
although to be frank I'm not sure that something as simple as this
deserves to be dignified with the name currying.
It's definitely not currying - it doesn't create a new function. Currying
would be:

def arraytype(kind):
def mkarray(value):
return array.array(kind, value)
return mkarray

chars = arraytype('c')
seq = chars("tacatcgtcgacgtcgatcagtaccc")
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:


Definitely - there are lots of things to know about DNA molecules or parts
of them that aren't captured by the sequence.

tom

--
If it ain't Alberta, it ain't beef.
Sep 21 '05 #9
Tom Anderson wrote:
There's a special hell for people who override builtins.


which is, most likely, chock full of highly experienced python programmers.

</F>

Sep 21 '05 #10
On Wed, 21 Sep 2005 11:37:38 +0100, Tom Anderson wrote:
There's a special hell for people who override builtins.


[slaps head]

Of course there is, and I will burn in it for ever...

--
Steven.

Sep 21 '05 #11
On Wed, 21 Sep 2005, Fredrik Lundh wrote:
Tom Anderson wrote:
There's a special hell for people who override builtins.


which is, most likely, chock full of highly experienced python
programmers.


You reckon? I've never felt the need to do it myself, and instinctively,
it seems like a bad idea. Perhaps i've been missing something, though -
could you give me some examples of when overriding a builtin is a good
thing to do?

tom

--
Fitter, Happier, More Productive.
Sep 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cognite | last post by:
This venue would surely appreciate the cool stuff being done in python on bioinformatics and python's tools for info parsing and extraction (like fulltext indexing, xml tools, parser builders,...
57
by: John Howard | last post by:
I've sent several messages over the last year asking about python - Who teaches python? Is python losing steam? etc. I have noticed, eg, the declinng number of books at my local borders. The last...
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
5
by: Owen | last post by:
Hi all, i want to know some rss library in python.For example, some for rss readers, and some for generator. Thanks for any information. Owen.
3
by: Bryan Olson | last post by:
I recently wrote a module supporting value-shared slicing. I don't know if this functionality already existed somewhere, but I think it's useful enough that other Pythoners might want it, so here...
22
by: bearophileHUGS | last post by:
>From this interesting blog entry by Lawrence Oluyede: http://www.oluyede.org/blog/2006/07/05/europython-day-2/ and the Py3.0 PEPs, I think the people working on Py3.0 are doing a good job, I am...
14
by: nicolasg | last post by:
Hi folks, I have accomplished to make a python program that make some image manipulation to bmp files. I now want to provide this program as a web service. A user can visit a site and through a...
8
by: Allan Ebdrup | last post by:
What would be the fastest way to search 18,000 strings of an average size of 10Kb, I can have all the strings in memory, should I simply do a instr on all of the strings? Or is there a faster way?...
2
by: Michael R. Copeland | last post by:
I have some questions about the suitability of Python for some applications I've developed in C/C++. These are 32 bit Console applications, but they make extensive use of STL structures and...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.