0

I want to create a sequence of the bases (A,T,G,C) which all appear at the same probability. The sequence should have a variable length n. Could you help me?

  • Check out [this question](https://stackoverflow.com/questions/37212700/how-to-generate-random-sequence-of-numbers-in-python) – Not_a_programmer Jun 04 '20 at 10:42
  • Welcome to StackOverflow! If you phrase the question as a computer-science question like 'I want to use Python to generate a sequence of numbers selected with same probability' it would allow other users to help you better – Dhruvan Ganesh Jun 04 '20 at 11:03

1 Answers1

3

python has a module named random to choose randomly from a sample.

import random
n = 5 # set n
random_list = random.choices(range(4), k=n)
dic = {0:"A", 1:"C", 2:"G", 3:"T"}
bases_list = [dic[v] for v in random_list]
seq = ""
for item in bases_list:
    seq += item

At first you create a list of n random numbers, all in range [0,3]. Then you use a dictionary to replace numbers with strings representing bases.

Now when we have a list, I just put them all together in one string to create one sequence. That what the last three lines are doing.

As a general note for this site: for next time, give explanation about what you already tried or what difficulties you encountered. The community is glad to help, but not to solve homework for you

Roim
  • 2,986
  • 2
  • 10
  • 25
  • Thank you for your help! Next time I will remember to post my attempts. –  Jun 04 '20 at 15:21
  • But does the code "seq = "" " mean? That the quotation marks are not printed in the seq? –  Jun 04 '20 at 15:22
  • an empty string. You do have a basic understanding of python I hope? It creates an variable which holds an empty string. bases_list is a list, not an string. I assumed you wanted the final sequence as a string. Created empty string (seq) then manually add each item in list to variable. – Roim Jun 04 '20 at 16:30