0

(Working in R studio) I am trying to generate a DNA sequence in FASTA format where each base has equal probability of being randomly selected. Specifically I need to write it as a function. Here is what I have so far:

    #Define length of sequence
length=100
#supply list of nucleotides
bases=["A","G","C","T"]
print(bases)

#Generate a sequence
from random import choice
  #create empty sequence
  sequence=""
  #randomly select base and add to sequence(repeat 100 times for length=100)
  for i in range(length):
    base=choice(bases)
    sequence+=base
print(sequence)

#create function to generate sequences
def getsequences(length):
  sequence=""
  for count in range(length):
    sequence+=choice(bases)
  return sequence
getsequences(length)
#where "getsequences" is the function name

This seems to work (if you spot an error please let me know), however now I need to create a text file where I can story this sequence data. Any ideas?

heather_l
  • 11
  • 4
  • Your code is fine though I would personally use a generator expression/comprehension to build the string. Check out this post for writing strings to files : https://stackoverflow.com/questions/59852831/write-a-multi-line-string-to-a-text-file-using-python – JonSG Mar 10 '22 at 18:28

0 Answers0