-2

I have this file which has many sequences up to 500 sequence some of these sequence has same name I want to combine the replication in one

file 1

>1
aa
>2
cc
>3
tt
>4
atc
>2
tag
>1
gg

outfile 

>1
aagg
>2
cctag
>3
tt
>4
atc
  • How big is this Fasta file? – Kusalananda Jan 23 '17 at 8:20
  • I have range of size from 100k byte up to 1 M byte – Ali Jan 23 '17 at 8:33
  • You're more likely to get answers if you describe the problem in terms of what needs to be done. It looks like you have a series of header lines followed by data lines, and you want to aggregate the data lines by header. Is that correct? If so, please put the new information on your question. DO NOT PUT IT HERE IN THE COMMENTS. – roaima Jan 23 '17 at 8:45
0

This is an Awk script:

#!/usr/bin/awk -f

/^>/    { header = $0 }
!/^>/   { sequence[header] = sequence[header] $0 }

END {
    for (head in sequence) {
        printf("%s\n%s\n", head, sequence[head])
    }
}

It will parse the complete Fasta file into memory, concatenating all sequence data that has identical headers. At the end, it will output the data.

Note that this approach is not good for large Fasta files, and will certainly break completely on genome-sized files (not enough memory). For a better approach in those cases, one could instead consider storing the parsed data into files that are concatenated at the end. I haven't looked at implementing that though.

To run the script:

$ awk -f ./script.awk file.fa
>1
aagg
>2
cctag
>3
tt
>4
atc
|improve this answer|||||
  • thanks for your help it is work as I want much appreciate – Ali Jan 23 '17 at 10:08
  • @Ali Good! If this solves your issue, please consider accepting the answer. – Kusalananda Jan 23 '17 at 10:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.