How to generate a fasta file from a python dictionary or lists?
1
5
Entering edit mode
6.6 years ago
grayapply2009 ▴ 240

I have two lists:

list_seq = [sequence1, sequence2, sequence3, sequence4]

list_name = [name1, name2, name3, name4]

I can zip them to a dictionary:

dict_s_n = dict(zip(list_seq & list_name))

How do I generate a fasta file from these two lists or the dictionary?

sequence fasta list dictionary python • 18k views
ADD COMMENT
0
Entering edit mode

This is in python, isn't it? I'm adding the tag and editing the title to make it more clear.

ADD REPLY
6
Entering edit mode
6.6 years ago
Ming Tang ★ 2.7k

you just need to write the name and sequence to a fasta format

>name1

sequence1

>name2

sequence2

.......

ofile = open("my_fasta.txt", "w")

for i in range(len(list_seq)):

ofile.write(">" + list_name[i] + "\n" +list_seq[i] + "\n")

#do not forget to close it

ofile.close()

 

ADD COMMENT
1
Entering edit mode

Hi ! I would just add ofile.close() to your solution, tangming2005, as you did not used the "with" statement to open "my_fasta.txt".

ADD REPLY
0
Entering edit mode

You are right! Do not forget to close ..

ADD REPLY
0
Entering edit mode

Great! I didn't think  it was that easy. Many thanks, tangming2005!

ADD REPLY
1
Entering edit mode

yes, just read some more stuff here http://www.greenteapress.com/thinkpython/

ADD REPLY
0
Entering edit mode

Nice website!

ADD REPLY
0
Entering edit mode

nice and simple. I wanted to do it with biopython, but this one is much easier.

ADD REPLY

Login before adding your answer.

Traffic: 2209 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6