473,248 Members | 1,230 Online

# how to insert a string into a string of random letters in a for loop

111 100+
I have a code in which i create a set of alphabets of specified count .Now in particular positions in it i want to embed my choice of letters how do i do it.here is my code
Expand|Select|Wrap|Line Numbers
1. def random_seq():
2.     seq=""
3.     ch=""
4.     for i in range(0,50):
5.         ch=random.choice(("ATGC"))
6.         seq=seq+ch
7.         for i in seq:
8.
9.             seq[i:i+15]="CGTCAAGTTCAAGTGC"
10.     return seq
11.
12.
now here in this I am creating a list of sequence containing the letters only "ATGC".Now I want to specify in this that say at particular postions like say the first fifteen characters should be what i mentioned. and say i also want to do when for positions from 41 to 50 i want a specifies set of letters something like this " "CGTCAAGTTCAAGTGC".how to do this?
why is my code not working?
cheers!
Jul 12 '07 #1
2 2901
bartonc
6,596 Expert 4TB
I have a code in which i create a set of alphabets of specified count .Now in particular positions in it i want to embed my choice of letters how do i do it.here is my code
Expand|Select|Wrap|Line Numbers
1. def random_seq():
2.     seq=""
3.     ch=""
4.     for i in range(0,50):
5.         ch=random.choice(("ATGC"))
6.         seq=seq+ch
7.         for i in seq:
8.
9.             seq[i:i+15]="CGTCAAGTTCAAGTGC"
10.     return seq
11.
12.
now here in this I am creating a list of sequence containing the letters only "ATGC".Now I want to specify in this that say at particular postions like say the first fifteen characters should be what i mentioned. and say i also want to do when for positions from 41 to 50 i want a specifies set of letters something like this " "CGTCAAGTTCAAGTGC".how to do this?
why is my code not working?
cheers!
I hope this is what you are looking for:
Expand|Select|Wrap|Line Numbers
1. import random
2.
3. def random_seq(nChars, insertAt, aString):
4.     seq = ""
5.     for i in range(nChars):
6.         if i == insertAt:
7.             seq += aString
8.         ch=random.choice(("ATGC"))
9.         seq += ch
10.     return seq
11.
12. # I put markers in your string so you can see and make it an argurment to the function
13. theStringToInsert = "#CGTCAAGTTCAAGTGC#"
14. # calculate the count as total - the length of the string to insert
15. count = 50 - len(theStringToInsert)
16.
17.
18. result = random_seq(count, 15, theStringToInsert)
19. print result, "is %d characters long" %len(result)
20. print result.find('#')
21.
TATGTAGATACAGCT#CGTCAAGTTCAAGTGC#CGGGGTAGCCCGTTCAT is 50 characters long
15
Jul 12 '07 #2
aboxylica
111 100+
Thanks a lot!that helped.But I still have that matrix problem.
cheers!
I hope this is what you are looking for:
Expand|Select|Wrap|Line Numbers
1. import random
2.
3. def random_seq(nChars, insertAt, aString):
4.     seq = ""
5.     for i in range(nChars):
6.         if i == insertAt:
7.             seq += aString
8.         ch=random.choice(("ATGC"))
9.         seq += ch
10.     return seq
11.
12. # I put markers in your string so you can see and make it an argurment to the function
13. theStringToInsert = "#CGTCAAGTTCAAGTGC#"
14. # calculate the count as total - the length of the string to insert
15. count = 50 - len(theStringToInsert)
16.
17.
18. result = random_seq(count, 15, theStringToInsert)
19. print result, "is %d characters long" %len(result)
20. print result.find('#')
21.
TATGTAGATACAGCT#CGTCAAGTTCAAGTGC#CGGGGTAGCCCGTTCAT is 50 characters long
15
Jul 12 '07 #3