<div dir="ltr">You can use a dictionary to map names to a storage place for each sequence. To avoid tedious checking if a name is already present in a dictionary, we can use the defaultdict type.<div><br></div><font face="monospace, monospace">from collections import defaultdict<br><br></font><div><font face="monospace, monospace">sequence_map = defaultdict(str)</font><div><font face="monospace, monospace"><br>for cur_record in SeqIO.parse('nucleotide_seq.fasta', "fasta"):<br></font> sequence_map[<a href="http://cur_record.name">cur_record.name</a>] += str(cur_record.seq)</div><div><br></div><div>You can then look up each sequence in the sequence_map by name or by using the common dictionary methods <a href="https://docs.python.org/2/library/stdtypes.html#typesmapping">here</a>.<br> <div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 17, 2015 at 5:50 AM, sunwm9 <span dir="ltr"><<a href="mailto:sunwm9@tom.com" target="_blank">sunwm9@tom.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><pre><strong style="font-family:Arial,Verdana,sans-serif"><br></strong></pre><pre><span style="font-family:Arial,Verdana,sans-serif">Dear all,</span></pre><pre><span style="font-family:Arial,Verdana,sans-serif">I am new self-educator of Biopython. I would like to concatenate fasta sequences (file attached) with same gene name using SeqIO.parse() function. </span></pre><pre><span style="font-family:Arial,Verdana,sans-serif">Here is a snippet from my code:</span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><br></span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><em>seq_all = ' '</em></span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><em>for cur_record in SeqIO.parse('nucleotide_seq.fasta', "fasta") :</em></span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><em> if <a href="http://cur_record.name" target="_blank">cur_record.name</a> == 'B103':</em></span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><em> seq_all = seq_all + str(cur_record.seq)</em></span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><em> print seq_all</em></span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><em><br></em></span></pre><pre><span style="font-family:Arial,Verdana,sans-serif">It need to change the gene name every time. How can I </span><span style="font-family:Arial,Verdana,sans-serif">concatenate all the sequences with same gene name automatically.</span></pre><pre><<a href="mailto:ribozyme@ioz.ac.cn" target="_blank">ribozyme@ioz.ac.cn</a>><br></pre><pre><span style="font-family:Arial,Verdana,sans-serif">Best regards,</span></pre><pre><span style="font-family:Arial,Verdana,sans-serif">Weiming Sun </span></pre><pre><span style="font-family:Arial,Verdana,sans-serif"><br></span></pre><br>_______________________________________________<br>
Biopython mailing list - <a href="mailto:Biopython@mailman.open-bio.org">Biopython@mailman.open-bio.org</a><br>
<a href="http://mailman.open-bio.org/mailman/listinfo/biopython" rel="noreferrer" target="_blank">http://mailman.open-bio.org/mailman/listinfo/biopython</a><br></blockquote></div><br></div>