简体   繁体   中英

Creating random DNA sequence and saving it as a csv file

a python newbie here, I want to generate 20 different DNA sequences. each with a length of 150, I also wanted the sequences to be saved as a csv file. but this part does not seem to work. I will be thankful for any tips!

from random import choice
import csv

output = open("random_sequences.csv", "w")
        
def make_random_sequence():
    random_sequence = ""
    bases = ["A", "T", "G", "C"]
    for i in range(0, 150):
        random_sequence = random_sequence + choice(bases)
    print(random_sequence)


def random_sequences():
    for i in range(0, 20):
        altogether = make_random_sequence()
    return altogether
            
    for row in altogether:
        csv.writer(outfile).writerow(row)

output.close()

One of several problems:

 def random_sequences(): for i in range(0, 20): altogether = make_random_sequence() return altogether for row in altogether: # will never be executed csv.writer(outfile).writerow(row) # as the function returned earlier

Another problem:

The csv modules needs the newline="" param when opening the file.


You can streamline your random generation of DNS data - adding string to string is really slow:

import random

def make_random_sequence():
    return random.choices("ATGC", k=150)  # just create 150 of them as list

with open("output.csv","w",newline="") as f:
    writer = csv.writer(f)
    for _ in range(20):
        writer.writerow(make_random_sequence())

Output.csv:

A,A,A,A,A,C,C,T,G,T,G,T,G,T,T,A,A,A,A,C,C,C,A,T,C,A,A,T,G,T,T,A,C,C,G,G,G,G,G,A,G,T,C,A,G,A,G,T,T,T,A,C,T,G,T,A,C,T,A,A,G,A,C,C,T,G,C,G,A,T,A,C,A,T,C,T,T,T,A,C,G,G,A,T,A,A,C,T,A,G,A,G,A,T,G,T,C,C,G,C,T,G,G,A,C,A,T,C,G,T,A,A,C,G,T,C,G,G,G,A,A,G,T,C,A,G,T,A,A,A,C,T,G,T,T,G,A,C,A,A,C,G,T,C,C,C,G,G,A,T
C,T,C,C,A,C,T,A,C,T,T,C,A,C,T,C,T,G,T,C,C,G,G,T,G,G,T,T,C,A,C,C,C,G,C,A,A,G,C,T,A,A,A,C,C,A,A,C,C,G,A,C,G,G,T,G,A,C,A,C,A,G,G,G,A,T,C,T,T,T,C,G,T,T,A,T,A,A,G,T,A,C,T,C,A,T,T,C,A,A,G,T,G,A,T,A,A,C,T,G,T,G,G,A,T,G,A,G,A,A,G,A,T,C,T,A,T,C,T,G,C,G,G,C,C,G,T,C,C,G,A,C,G,C,T,G,T,A,A,T,T,A,T,T,G,C,G,T,A,T
[....]
C,C,T,T,G,T,T,T,C,G,A,C,A,C,A,T,G,A,T,C,G,C,T,C,C,G,T,G,T,T,A,G,T,A,A,G,A,T,T,T,A,C,T,A,A,G,C,G,G,C,A,A,G,A,A,G,G,C,C,C,T,T,A,G,A,G,A,G,T,T,C,T,G,A,A,T,T,G,C,C,G,A,T,A,G,C,G,T,G,T,T,T,T,A,A,C,C,T,T,G,T,A,A,T,C,C,A,G,G,T,A,C,C,C,C,G,G,G,C,A,A,G,A,T,G,T,T,A,G,C,T,G,G,C,G,C,T,C,A,T,G,T,T,C,T,A,A,G,A,T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM