473,765 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

slicing functionality for strings / Python suitability for bioinformatics

>>> rs='AUGCUAGACGU GGAGUAG'
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 2052
jb********@yaho o.com wrote:
rs='AUGCUAGACGU GGAGUAG'
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************ ************@wo lke7.net> wrote in
message news:3p******** ****@individual .net...
jb********@yaho o.com wrote:
> rs='AUGCUAGACGU GGAGUAG'
> 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********@yaho o.com
<jb********@yah oo.com> wrote:
rs='AUGCUAGACGU GGAGUAG'
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.spreadfiref ox.com/?q=affiliates&i d=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('Ill egal character "%s".' % c)
self.value = array.array('c' , value)

def __repr__(self):
return self.value.tost ring()

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(kin d, value)
return mkarray

chars = arraytype('c')
seq = chars("tacatcgt cgacgtcgatcagta ccc")
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

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

Similar topics

0
1720
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, graphic display, realtime simulation and communities tools like pygame and ...) It's in San Diego February 9-12, 2004. "Session presentations are 45 or 90 minutes long, and tutorials are either a half-day (3 hours) or a full day (6 hours)."
57
4262
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 time I visited a borders (last week), there was 1 (sic) book about python on the shelve compared to dozens on perl & java! On my last inquiry about who teaching python, I got two, maybe three, responses. I really want to see python succeed! It's...
54
3977
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 but *NOT* mystring (5th element). Many people don't like idea that 5th element is not invited. (BTW, yes I'm aware of the explanation where slicing
5
2065
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
2061
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 it is. Also, my recent notes on Python warts with respect to negative indexes were based on problems I encoutered debugging this module, so I'm posting it partially as a concrete example of what I was talking about.
22
2368
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 not expert enough (so I don't post this on the Py3.0 mailing list), but I agree with most of the things they are agreeing to. Few notes: - input() vanishes and raw_input() becomes sys.stdin.readline(). I think a child that is learning to program...
14
3032
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 web interface he should upload the file to the web server , the server then will do the image process with the python program I have wrote and when it finish the user must get the image file back . My question is how difficult is to set up a web...
8
1875
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? I would like to have a kind of search like google where you can enter several words to search for, guess that calls for a regular expression "word1|word2|word3|...". Is there any kind of indexing tools available for this kind of thing, I have my...
2
2008
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 functions (Lists, Maps, Vectors, arrays) - primarily because the data volume is high (2,500,000+ records). The main thing I'd like to do is port one part of the system that has a series of user menus and works with several text data files (one _very_...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.