How to create fasta file
Hi,
So I wrote a simple code that doesn't seem to be working well and I don't know why. I have two files:
seq.txt
AAAATTTTTCCCCCGGGG
AAAAAAAAAAAAAAAAAA
TTTTTTTTTTTTTTTTTTTT
....
Ids_to_add.txt
ID_1
ID_2
ID_3
.....
I want a file fasta like this:
>ID_1
AAAATTTTTCCCCCGGGG
>ID_2
AAAAAAAAAAAAAAAAAA
>ID_3
TTTTTTTTTTTTTTTTTTTT
...
My code is like this so far:
g = open("test.txt",'w')
f = open("Ids_to_add.txt", "r")
a = open("seq.txt", "r")
for line in f:
linef = line.strip()
for line in a:
linea = line.strip()
print(">" + linef.upper()+"\n"+linea.upper(), file=g)
Somehow the code output is:
>ID_3
AAAATTTTTCCCCCGGGG
>ID_3
AAAAAAAAAAAAAAAAAA
>ID_3
TTTTTTTTTTTTTTTTTTTT
...
So in conclusion the sequences are fine, but the IDs are always the last ID in the ID file. Any help is welcome!
• 1,120 views
•
link
1 answer
Your first for statement loops through the entire file, so that the last line of file f is the one present in linef when you go into the second for loop that reads the sequences. Try iterating through both files at the same time to pick the lines. Here's an overly simplified example to help:
g = open("test.txt",'w')
f = open("Ids_to_add.txt", "r")
a = open("seq.txt", "r")
for id_line, seq_line in zip(f, a):
linef = id_line.strip()
linea = seq_line.strip()
print(">" + linef.upper()+"\n"+linea.upper(), file=g)
Hope that helps.
• 0 views
•
link
Log in to answer this question.
Of course it is, your loop that extracts the header lines finishes even before the second one starts, therefore the
printcommand uses the last instance offwhich is the last header of ID.