This is a test version of Biostars. For the public version, visit https://www.biostars.org.
merge two multifasta files

I have two multifasta files that have almost the same headers, for example

file1.fasta
    >header_1
    dnasequenceoffastafile1
    >header_2
    dnasequenceoffastafile1
    >header_3
    dnasequenceoffastafile1

  file2.fasta
        >header_1_f2
        dnasequencefastafile2
        >header_2_f2
        dnasequencefastafile2
        >header_4_f2
        dnasequencefastafile2

and I would like the next output

    merged.fasta 
        >header_1_header_1_f2
        dnasequenceoffastafile1dnasequencefastafile2
        >header_2_header_2_f2
        dnasequenceoffastafile1dnasequencefastafile2
sequence

1 answer

Here's a quick R solution using Bioconductor. There are analogous examples using a variety of different tools and languages (see Biopython, etc.). For quick manipulations, I like to use Biostrings due to how efficiently it handles long strings once in memory.

library(Biostrings)
a <- readDNAStringSet("file1", format="fasta")
b <- readDNAStringSet("file2", format="fasta")
d <- DNAStringSet(paste0(a, b))
# reassign names
names(d) <- names(a)
writeXStringSet(d, "your_fila_name.fa")

Hope this helps.

Log in to answer this question.