473,382 Members | 1,611 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,382 software developers and data experts.

In-place array modification

Hello everybody,

Say I have an array (Python array, not Numpy) that contains characters
representing a DNA sequence. Something like
from array import array
s = array('c', "ATAGCTGCT")
Now I want to calculate the reverse complement of this sequence. I could
make a simple loop:
d = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
s.reverse()
s array('c', 'TCGTCGATA') n = len(s)
for i in range(n): .... s[i] = d[s[i]] s array('c', 'AGCAGCTAT')

But usually DNA sequences are very long, so the loop might take a long
time to finish. So I thought I would use the map function instead:
s = map(lambda c: d[c], s)
s = array('c', s)


But then I am creating memory for s from scratch. So what I would really
like to do is to have a function similar to map, but have it modify the
array in place. Does such a function exist in Python?
Many thanks,

Michiel de Hoon
Human Genome Center, U Tokyo.
Jul 18 '05 #1
3 2339
umdehoon wrote:
Say I have an array (Python array, not Numpy) that contains characters
representing a DNA sequence. Something like
from array import array
s = array('c', "ATAGCTGCT")
Now I want to calculate the reverse complement of this sequence. I could


The following does not meet your spec (it's not in-place), but might be
worth considering as well:
import array, string
a = array.array("c", "ATAGCTGCT")
a.reverse()
t = string.maketrans("ATCG", "TAGC")
array.array("c", a.tostring().translate(t))

array('c', 'AGCAGCTAT')

Peter
Jul 18 '05 #2
umdehoon wrote:
Say I have an array (Python array, not Numpy) that contains characters for i in range(n):
s[i] = d[s[i]]


For large lists 'range' will affect the performance since it
creates another huge list. You might think about using xrange or
enumerate. With xrange I could replace 10 million bases in
20 seconds whereas I had to kill the test program using
range b/c it started gobbling up resources...

I suspected that psyco might do a good job of optimizing
this kind of looping. Indeed, the same job took less than 3
seconds with psyco enabled.

Istvan.

Jul 18 '05 #3
With 2.4's generator expressions and slice assignment, you should be
able to perform these kinds of operations in-place. You would write
something like
s[:] = (d[si] for si in s)
... unfortunately, this doesn't work.
TypeError: can only assign array (not "generator") to array slice

I thought that numarray arrays might accept generators, but I get an odd
error there too:
n = numarray.zeros((5,))
n[:] = iter(range(5)) Traceback (most recent call last):
File "<stdin>", line 1, in ?
libnumarray.error: Type object lookup returned NULL for type -1

If you can identify a small set of operations that need to be in-place
(like this simple base-to-base mapping), you could code them in C or
Pyrex---as a bonus, they'll be fast, too. The map() variant is not likely
to be very fast (in fact, I'd be surprised if it was faster than the
inplace for-loop version). string.translate() would work in this case,
very quickly, but create an extra copy just like the map() solution would.
s = map(d.get, s) might beat s = map(lambda c: d[c], s) but probably not by enough to matter.

Here's a run with my implementation of "itranslate" in Pyrex: from array import array
s = array('c', "ATAGCTGCT")
t = string.maketrans('ATCG', 'TAGC')
itranslate.itranslate(s, t)
s

array('c', 'TATCGACGA')

And here's the Pyrex source for itranslate:

cdef extern from "Python.h":
int PyObject_AsWriteBuffer(object, void **, int *) except -1
int PyString_AsStringAndSize(object, char **, int *) except -1

def itranslate(object obuf, object ot):
cdef int blen, tlen, i
cdef unsigned char *buf, *t

PyObject_AsWriteBuffer(obuf, <void **>&buf, &blen)
PyString_AsStringAndSize(ot, <char **>&t, &tlen)

if tlen != 256:
raise ValueError, "Translation must be length-256 string"

for i from 0 <= i < blen:
buf[i] = t[buf[i]]

On my 650MHz x86 machine, a translation of a 1 megabyte buffer with
itranslate takes 15ms according to "timeit" (that's 66 megs per second,
which doesn't seem that great, but probably beats the plain Python code
handily)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFA+946Jd01MZaTXX0RAvvyAJ4hgupdX+YLxzMGg2ijRz IF85dzXACdG4YH
vd35MQG+0Vm2dAinEIZwP58=
=2ELj
-----END PGP SIGNATURE-----

Jul 18 '05 #4

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

Similar topics

3
by: Curious Expatriate | last post by:
Hi- I'm completely stumped. I'm trying to write some code that will parse a file and rewrite it with all URLs replaced by something else. For example: if the file looks like this: <b>click...
0
by: Ben Eisenberg | last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the...
2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
1
by: James | last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP ? Where ID = $ID ! Any examples or URLS ? Thanks
1
by: Patrick Schlaepfer | last post by:
Why this code is not working on Solaris 2.8 host. Always getting: PHP Fatal error: swfaction() : getURL('http://www.php.net' ^ Line 1: Reason: 'syntax error' in /.../htdocs/ming2.php on...
1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
3
by: Quinten Carlson | last post by:
Is there a way to conditionally define a function in php? I'm trying to run a php page 10 times using the include statement, but I get an error because my function is already defined. The docs...
10
by: lawrence | last post by:
I get the impression that most people who are using objects in their PHP projects are mixing them with procedural code. The design, I think, is one where the procedural code is the client code, and...
4
by: Matt Schroeder | last post by:
Does anyone know how to count how many rows are in a mysql table? This is what I have, but it doesn't work right: <? $db = mysql_connect("localhost", "username", "password");...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.