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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aboxylica
    New Member
    • Jul 2007
    • 111

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

    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
    Code:
    def random_seq():
        seq=""
        ch=""
        for i in range(0,50):
            ch=random.choice(("ATGC"))
            seq=seq+ch
            for i in seq:
                
                seq[i:i+15]="CGTCAAGTTCAAGTGC"
        return seq
    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 " "CGTCAAGTTCAAGT GC".how to do this?
    why is my code not working?
    waiting for your reply
    cheers!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by aboxylica
    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
    Code:
    def random_seq():
        seq=""
        ch=""
        for i in range(0,50):
            ch=random.choice(("ATGC"))
            seq=seq+ch
            for i in seq:
                
                seq[i:i+15]="CGTCAAGTTCAAGTGC"
        return seq
    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 " "CGTCAAGTTCAAGT GC".how to do this?
    why is my code not working?
    waiting for your reply
    cheers!
    I hope this is what you are looking for:[CODE=python]import random

    def random_seq(nCha rs, insertAt, aString):
    seq = ""
    for i in range(nChars):
    if i == insertAt:
    seq += aString
    ch=random.choic e(("ATGC"))
    seq += ch
    return seq

    # I put markers in your string so you can see and make it an argurment to the function
    theStringToInse rt = "#CGTCAAGTTCAAG TGC#"
    # calculate the count as total - the length of the string to insert
    count = 50 - len(theStringTo Insert)


    result = random_seq(coun t, 15, theStringToInse rt)
    print result, "is %d characters long" %len(result)
    print result.find('#' )
    [/CODE]TATGTAGATACAGCT #CGTCAAGTTCAAGT GC#CGGGGTAGCCCG TTCAT is 50 characters long
    15

    Comment

    • aboxylica
      New Member
      • Jul 2007
      • 111

      #3
      Thanks a lot!that helped.But I still have that matrix problem.
      waiting for ur reply,
      cheers!
      Originally posted by bartonc
      I hope this is what you are looking for:[CODE=python]import random

      def random_seq(nCha rs, insertAt, aString):
      seq = ""
      for i in range(nChars):
      if i == insertAt:
      seq += aString
      ch=random.choic e(("ATGC"))
      seq += ch
      return seq

      # I put markers in your string so you can see and make it an argurment to the function
      theStringToInse rt = "#CGTCAAGTTCAAG TGC#"
      # calculate the count as total - the length of the string to insert
      count = 50 - len(theStringTo Insert)


      result = random_seq(coun t, 15, theStringToInse rt)
      print result, "is %d characters long" %len(result)
      print result.find('#' )
      [/CODE]TATGTAGATACAGCT #CGTCAAGTTCAAGT GC#CGGGGTAGCCCG TTCAT is 50 characters long
      15

      Comment

      Working...