This works, thanks a lot
I would like to ask how to merge different fasta files into one fasta file and name each contig according to the name of the original fasta file?
For example, gene1.fasta, gene2.fasta, and gene3.fasta are merged into gene.fasta, and the contig names in gene.fasta are >gene1, >gene2, >gene3.
4 answers
The key step is renaming the sequence of each file with the gene name in the file name. seqkit is used for renaming.
cat gene1.fasta
>seq1
aaa
for f in *.fasta; do \
gene=${f%.fasta}; \
seqkit replace -p ".+" -r $gene $f; \
done > result.fa
cat result.fa
>gene1
aaa
>gene2
ccc
For Windows users:
brename -q -l -p "fasta$" | rush -k "seqkit replace -p .+ -r {%:} {}" > result.fa
Where brename is for finding and listing files, rush is for batch operation and {%:} is for extracting the gene name.
what is inside each fasta file? Can you see if this works for you:
with awk:
$ awk '/^>/{sub(".*",">"FILENAME)}1' *.fa | sed -r '/^>/ s/\.fa$//'
with parallel:
$ parallel seqkit replace -p '.+' -r {.} {} ::: *.fa
Replace .fa with appropriate file extension.
Using Linux/Unix (e.g. OSX)
cat gene1.fasta gene2.fasta gene3.fasta > gene.fasta
If you've loads of files a bash for loop is more convenient rather than typing out the files individually
This also works:
cat gene?.fasta > gene.fasta
Log in to answer this question.