Random DNA Generation

I am trying to generate random DNA sequences in python using random numbers and random strings. But I get only one line as output. For example: if I give DNA with a length of 5 (String (5)), I should get the output "CTGAT". Similarly, if I give String (4), it should give me "CTGT". But I get "G" or "C" or "T" or "A"; those. only one line. Can anyone help me with this?

I tried the following code:

from random import choice
def String(length):

   DNA=""
   for count in range(length):
      DNA+=choice("CGTA")
      return DNA
+4
source share
4 answers

You come back too fast:

from random import choice
def String(length):

   DNA=""
   for count in range(length):
      DNA+=choice("CGTA")
      return DNA

return for, - return.

Python return: " return ( None) ."

, return :

def String(length):

       DNA=""
       for count in range(length):
          DNA+=choice("CGTA")
       return DNA

EDIT: ( , ).

def weightedchoice(items): # this doesn't require the numbers to add up to 100
    return choice("".join(x * y for x, y in items))

weightedchoice choice :

DNA+=weightedchoice([("C", 10], ("G", 20), ("A", 40"), ("T", 30)])

+5

, . Python , .

import random

def DNA(length):
    return ''.join(random.choice('CGTA') for _ in xrange(length))

print DNA(5)
+8

I updated the code to ensure a GC percentage distribution of 0 to 100%. The above code always gives a 50% distribution.

The string actg_distribution can be any length of an existing DNA sequence with a known percentage of GC. GK percent of a certain range is a common use case.


import random

# Return random CGTA sequences, set minimum = maximum to get a specified length.
def random_length_dnasequence(minimum=25, maximum=10000, actg_distribution=None):
    if (minimum == maximum):
        length = minimum
    else:
        length = random.randint(minimum, maximum)
    if (actg_distribution == None):
        actg_distribution = ''.join(random.choice('cgta') for _x in xrange(7))

    return ''.join(random.choice(actg_distribution) for _x in xrange(length))


def random_dnasequence(length, actg_distribution=None):
    return random_length_dnasequence(length, length, actg_distribution)
+1
source

Fast function for Python 3.6 using random.choices

import random

def string(length=int(), letters="CGTA"):
        #slower 0.05s for 20000 nt
#     dna =""
#     for count in range(length):
#         dna+=choice("CGTA")
#     return dna

    #0.013s for 20000 nt
    return''.join(random.choices(letters, k=length)
0
source

All Articles