user14410448
user14410448

Reputation:

How to write a list of strings to a text (FASTA) file?

I am currently writing a program that takes in a lot of different amino acid sequences (string), cleaves them with an enzyme, and then returns the resulting peptides (lots of smaller strings).

I have written the program and it works, although I am having trouble writing the output into a text file.

For example, input would be something like:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

and output would be something like:

Peptide 1

'ABCDE'

Peptide 2

'FGHIJKLMNOP'

Peptide 3

'QRSTUVWXYZ'

How can I write this into a text (fasta) file, as converting to a string just bunches them all up and doesn't separate them with the peptide number and sequence on a new line?

string_peptides = str(all_peptides)                       
peptide_file = open(r'Cleaved Peptides.fasta', 'w+')
peptide_file.write(string_peptides)
peptide_file.close()

Upvotes: 0

Views: 1462

Answers (1)

StupidWolf
StupidWolf

Reputation: 46898

You can join the name to the sequence first, then output something joined by a newline:

all_peptides = ['ABCDE','FGHIJKLMNOP','QRSTUVWXYZ']
peptide_file = open(r'Cleaved Peptides.fasta', 'w+')
out = '\n'.join(['>Peptide' + str(i+1) + "\n" + j for i,j in enumerate(all_peptides)])
peptide_file.write(out)
peptide_file.close()

Upvotes: 0

Related Questions