This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Appending sequences in fasta file with identical headers

Hi,

I have fasta file like follows:

>A1
aatggggta
>A1
atttggta
>A1
actcaagt
>B1
agctaaa
> B1
ttaagc
>C1
aattatggc

I would like concatenate, the sequences with with identical headers like follows

>A1
aatggggtaatttggtaactcaagt
>B1
agctaaattaagc
>C1
aattatggc

Is there any tool which I can make use to achieve the above output. Kindly guide me.

fasta

Out of curiosity... why would you want to do that?

2 answers

awk '/^>/ {if(prev!=$0) {prev=$0;printf("\n%s\n",$0);} next;} {printf("%s",$0);} END {printf("\n");}' input.fa

>A1
aatggggtaatttggtaactcaagt
>B1
agctaaattaagc
>C1
aattatggc

How about this

cat file.fa | paste - - | datamash -s -g 1 collapse 2 | sed 's/,//g' | tr '\t' '\n' > new_file.fa

ouput:

>A1
aatggggtaatttggtaactcaagt
>B1
agctaaattaagc
>C1
aattatggc

P.S: datamash-here

Log in to answer this question.