<p>Certainly this python script is not the most elegant solution but it will give you the desired result:</p>

<pre><code>import sys

seqs = {}
with open(sys.argv[1], "r") as fh:
    curr = ""
    for line in fh:
        if line.startswith("&gt;"):
            curr = line
            if curr not in seqs:
                seqs[curr] = ""
        else:
            seqs[curr] += line.strip()

for ident, seq in seqs.items():
    print("{}{}".format(ident, seq))
</code></pre>

<p>If you saved the script in seq_merge.py just run it with:</p>

<pre><code>python seq_merge.py your_file.fasta &gt; result.fasta
</code></pre>

CC BY-SA 4.0
2018-07-19T04:57:22.133Z
2018-07-19T05:11:33.500Z
2018-07-19T05:11:33.500Z
phngs
<p>This is a scenario where awk excels and yields a much simpler solution than the other examples here (though the logic is essentially equivalent to terdon’s Perl solution):</p>

<pre class="lang-bash prettyprint-override"><code>awk '
    /&gt;/  { id = $0 }
    !/&gt;/ { seq[id] = seq[id] $0 }
    END  { for (id in seq) print id "\n" seq[id] }
' input.fasta \
&gt; output.fasta
</code></pre>

<p>That said, for anything beyond a quick’n’dirty application I strongly recommend using a <em>proper</em> FASTA parser, not an ad-hoc solution.</p>

CC BY-SA 4.0
2018-07-19T12:52:12.687Z
2018-07-19T12:52:12.687Z
Konrad Rudolph
<p>If you don't care about the order of the sequences and your file is small enough to fit in RAM, you can do:</p>

<pre><code>$ perl -nle 'if(/^&gt;/){
                $n=$_
             }
             else{
                $seq{$n}.=$_
            } 
            END{
                print "$_\n$seq{$_}" for keys(%seq)
            }' file.fa 
&gt;Seq1
GTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCATAAAAACGAAACTGTATCCCGTGTTT
&gt;Seq2
CGTGTTTAGCAAAGAAAT
</code></pre>

<p>Or, if you are into the whole brevity thing<sup><a href="https://www.youtube.com/watch?v=vgUHqk-4omc" rel="nofollow noreferrer">1</a> </sup>:</p>

<pre><code>perl -nle '/^&gt;/?($n=$_):($s{$n}.=$_);}{print"$_\n$s{$_}"for keys%s' file.fa 
</code></pre>

CC BY-SA 4.0
2018-07-19T10:28:21.217Z
2018-07-19T10:28:21.217Z
terdon
<p>assuming two lines per fasta record (or you can <a href="https://gist.github.com/lindenb/2c0d4e11fd8a96d4c345" rel="nofollow noreferrer">linearize</a>) and using datamash:</p>

<pre><code>$ cat jeter.fa | paste - - | sort -t $'\t' -k1,1 | datamash -g 1 collapse 2 | tr -d ',' | tr "\t" "\n"

&gt;Seq1
AAAAACGAAACTGTATCCCGTGTTTGTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCAT
&gt;Seq2
CGTGTTTAGCAAAGAAAT
</code></pre>

CC BY-SA 4.0
2018-07-19T07:11:12.090Z
2018-07-19T07:11:12.090Z
Pierre
<p>Suppose we have a fasta file like</p>

<pre><code>&gt;Seq1
GTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCAT
&gt;Seq1
AAAAACGAAACTGTATCCCGTGTTT
&gt;Seq2
CGTGTTTAGCAAAGAAAT
</code></pre>

<p>I want to produce</p>

<pre><code>&gt;Seq1
GTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCATAAAAACGAAACTGTATCCCGTGTTT
&gt;Seq2
CGTGTTTAGCAAAGAAAT
</code></pre>

<fasta>
How to merge transcript sequence with same name in a FASTA file.
# How to merge transcript sequence with same name in a FASTA file.

(Asked by: username_0 on 2018-07-18)

Suppose we have a fasta file like
>Seq1
GTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCAT
>Seq1
AAAAACGAAACTGTATCCCGTGTTT
>Seq2
CGTGTTTAGCAAAGAAAT

I want to produce
>Seq1
GTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCATAAAAACGAAACTGTATCCCGTGTTT
>Seq2
CGTGTTTAGCAAAGAAAT

**Tags:** fasta

---

## 4 answers

### Answer by username_1 on 2018-07-19. Score: 3 | Accepted answer

Certainly this python script is not the most elegant solution but it will give you the desired result:
import sys

seqs = {}
with open(sys.argv[1], "r") as fh:
    curr = ""
    for line in fh:
        if line.startswith(">"):
            curr = line
            if curr not in seqs:
                seqs[curr] = ""
        else:
            seqs[curr] += line.strip()

for ident, seq in seqs.items():
    print("{}{}".format(ident, seq))

If you saved the script in seq_merge.py just run it with:
python seq_merge.py your_file.fasta > result.fasta


---

### Answer by username_2 on 2018-07-19. Score: 4

This is a scenario where awk excels and yields a much simpler solution than the other examples here (though the logic is essentially equivalent to terdon’s Perl solution):
awk '
    />/  { id = $0 }
    !/>/ { seq[id] = seq[id] $0 }
    END  { for (id in seq) print id "\n" seq[id] }
' input.fasta \
> output.fasta

That said, for anything beyond a quick’n’dirty application I strongly recommend using a proper FASTA parser, not an ad-hoc solution.


---

### Answer by username_3 on 2018-07-19. Score: 1

If you don't care about the order of the sequences and your file is small enough to fit in RAM, you can do:
$ perl -nle 'if(/^>/){
                $n=$_
             }
             else{
                $seq{$n}.=$_
            } 
            END{
                print "$_\n$seq{$_}" for keys(%seq)
            }' file.fa 
>Seq1
GTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCATAAAAACGAAACTGTATCCCGTGTTT
>Seq2
CGTGTTTAGCAAAGAAAT

Or, if you are into the whole brevity thing1 :
perl -nle '/^>/?($n=$_):($s{$n}.=$_);}{print"$_\n$s{$_}"for keys%s' file.fa


---

### Answer by username_4 on 2018-07-19. Score: 1

assuming two lines per fasta record (or you can linearize) and using datamash:
$ cat jeter.fa | paste - - | sort -t $'\t' -k1,1 | datamash -g 1 collapse 2 | tr -d ',' | tr "\t" "\n"

>Seq1
AAAAACGAAACTGTATCCCGTGTTTGTTGAGAGGTGTATGGACACGAAAAACGAAACTGTATCCCGTGTTTAGCAAAGAAATCAT
>Seq2
CGTGTTTAGCAAAGAAAT


---

