Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Iterate through characters

I have been trying to pull of median string search for a sequence in the ACGT genome. The problem I have is going to say AAAAAAAA to AAAAAAAC and so forth until I have tried every possible combination.

I've been essentially going brute force at it by creating two lists, one containg A,C,G,T and the other the 8 character sequence, and after each search iterating and swapping characters. The problem is that I don't test all combinations because when two iterate at the same time it jumps a letter.

Is there any way to go AAAAAAAA - AAAAAAAC - AAAAAAAG - AAAAAAAT - AAAAAACA and so forth easily?

like image 756
Andrew Severs Avatar asked Jul 13 '12 02:07

Andrew Severs


3 Answers

Using itertools

itertools.product("ACGT", repeat=8)
like image 94
jamylak Avatar answered Oct 20 '22 00:10

jamylak


As above suggested use itertools,

itertools.product("ACGT", repeat=8) # will work in your case.
like image 34
Tauquir Avatar answered Oct 19 '22 23:10

Tauquir


Using the regex inverter from the pyparsing wiki Examples page, invert this regex: [ACGT]{8}. You can also try the online inverter at the UtilityMill, but this server will timeout when generating 8-character strings, but I have successfully gotten up to 6 characters within the allowed time.

like image 35
PaulMcG Avatar answered Oct 20 '22 00:10

PaulMcG