Skip to content Skip to sidebar Skip to footer

Storing The Output To A Fasta File

from Bio import SeqIO from Bio import SeqRecord from Bio import SeqFeature for rec in SeqIO.parse('C:/Users/Siva/Downloads/sequence.gp','genbank'): if rec.features:

Solution 1:

First, you're trying to write a plain sequence as a fasta record. A fasta record consists of a sequence plus an ID line (prepended by ">"). You haven't provided an ID, so the fasta writer has nothing to write. You should either write the whole record, or turn the sequence into a fasta record by adding an ID yourself.

Second, even if your approach wrote anything, it's continually overwriting each new record into the same file. You'd end up with just the last record in the file.

A simpler approach is to store everything in a list, and then write the whole list when you're done the loop. For example:

new_fasta = []
for rec in SeqIO.parse("C:/Users/Siva/Downloads/sequence.gp","genbank"):
    if rec.features:
        for feature in rec.features:
            if feature.type =="Region":
                seq1 = feature.location.extract(rec).seq
                # Use an appropriate string for id 
                new_fasta.append('>%s\n%s' % (rec.id, seq1))  

withopen('region_AA_output1.fasta', 'w') as f:
    f.write('\n'.join(new_fasta))

Post a Comment for "Storing The Output To A Fasta File"