Extract specific fasta sequences from a big fasta file
<p>I want to extract specific fasta sequences from a big fasta file using the following script, but the output is empty.</p>

<p>The <code>transcripts.txt</code> file contains the list transcripts IDs that I want to export (both the IDs and the sequences) from <code>assembly.fasta</code> to <code>selected_transcripts.fasta</code>. 
For example:</p>

<ol>
<li>transcripts.txt:

<pre>
Transcript_00004|5601
Transcript_00005|5352</pre></li>
<li>assembly.fasta:<pre>
>Transcript_00004|5601
GATCTGGCGCTGAGCTGGGTGCTGATCGACCCGGCGTCCGGCCGCTCCGTGAACGCCTCGAGTCGGCGCCCGGTGTGCGTTGACCGGAGATCGCGATCTGGGGAGACCGTCGTGCGGTT
>Transcript_00004|5360
CGATCTGGCGCTGAGCTGGGTGCTGATCGACCCGGCGTCCGGCCGCTCCGTGAACGCCTCGAGTCGGCGCCCGGTGTGCGTTGACCGGAGATCGCGATCTGGGGAGACCGTCGTGCGGTT
</pre></li>
</ol>

<p>The IDs are preceded by the <code>&gt;</code> symbol: <code>>Transcripts_00004|5601</code>.</p>

<p>I have to read the <code>assembly.fasta</code> file, if the transcript ID in <code>assembly.fasta</code> is the same of that write in <code>transcripts.txt</code>, I have to write this transcript ID and its sequence in <code>selected_transcripts.fasta</code>. So, in the example above, I have to write only the first transcript.</p>

<p>Any suggestions?
Thanks.</p>

<pre><code>from Bio import SeqIO

my_list = [line.split(',') for line in open("/home/universita/transcripts.txt")]

fin = open('/home/universita/assembly.fasta', 'r')
fout = open('/home/universita/selected_transcripts.fasta', 'w')

for record in SeqIO.parse(fin,'fasta'):
    for item in my_list:
        if item == record.id:
            fout.write("&gt;" + record.id + "\n")
            fout.write(record.seq + "\n")

fin.close()
fout.close()
</code></pre>