Home > Net >  Save a modied FASTA file with Biopython
Save a modied FASTA file with Biopython

Time:10-24

I have used Biopython to remove some sequences due to they are too short. However, I don't know how to save the printed new sequences in a txt file.

This is the code that I have:

from Bio import SeqIO

for seq_record in SeqIO.parse("aminoacid_example.txt", "fasta"):

if len(seq_record.seq)>=30:
    print(">",seq_record.id)
    print(seq_record.seq)

Output:

">NP_414584.1

MNTFSQVWVFSDTPSRLPELMNGAQALANQINTFVLNDADGAQAIQLGANHVWKLN

"> NP_414563.1

MASVSISCPSCSATDGVVRNGKSTAGHQRYLCSHCRKTWQLQFTYTASQPGTHQKIIDMAMNG

"> NP_414564.1

MANIKSAKKRAIQSEKARKHNASRRSMMRTFIKKVYAAIEAGDKAAAQKAFNEMQPIVDRQAAKGLIHK

How can I save this sequences in a txt file?

Thank you for your help!

CodePudding user response:

it should go something like this:

text_list=[]
for seq_record in SeqIO.parse("aminoacid_example.txt", "fasta"):
    if len(seq_record.seq)>=30:
        elem1=">" seq_record.id
        elem2=seq_record.seq
        text_list.extend([elem1, elem2])
with open('filetowrite.txt', 'w') as f:
    f.write('\n'.join(text_list)

This would create a list with all the elements that you want, then it would write them to a file 'filetowrite.txt' (or however you want to call it), with each value on a new line.

  • Related