This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Appending Seqrecords In Biopython

I'm trying to generate a test dataset of randomly sampled 150bp sequences from a complete genome. I found what appeared to be the perfect Biopython script on the SeqIO Wiki page (http://biopython.org/wiki/SeqIO#Randomsubsequences) but genomefrags.append(record) throws an Attribute error that 'Seq' object has no attribute append. I'm not sure why this script is listed if that is really the case unless this is an issue with my current version of Biopython (1.52) which I run off my work server (so it would take some work to get it updated). The Biopython Tutorial and Cookbook says that Seq object could only be added starting with version 1.53 but adding is more of a concatenation, not an appending of separate objects into a single file. Does anyone know a way around this keeping in mind that I can write very minimal code at this point?

genome_frags=[]
limit=len(genome_record.seq)
for i in range(0, 1000) :
    start=randint(0,limit-150)
    end=start+150
    genome_frags=genome_record.seq[start:end]
    print (type(genome_frags))
    record=SeqRecord(genome_frags,'fragment_%i' % (i+1),'','')
    genome_frags.append(record)
biopython random fasta

Do you want a list of SeqRecords, each containing a random sample or one long SeqRecord made of those random samples?

3 answers

You're overwriting genome_frags. Try:

genome_frags=[]
limit=len(genome_record.seq)
for i in range(0, 1000) :
    start=randint(0,limit-150)
    end=start+150
    frag=genome_record.seq[start:end]
    record=SeqRecord(frag,'fragment_%i' % (i+1),'','')
    genome_frags.append(record)

This worked beautifully. Simple mistake. Thanks so much! It's small victories like this that keep me going.

You are assigning something to the variable genome_frags twice in the same loop. You should rename the first genome_frags = genome_record.seq[start:end] variable and rename it in record=... accordingly.

Some tools are optimized for that purpose if you need: How To Produce Simulated 'Synthetic' Sequences

Log in to answer this question.