This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Problem adding file names to fasta headers in multiple directories using awk

Hello all,

I have about ~150 sub directories in a directory. Each of those subdirectories contains multiple fasta files. I want to rename all the fasta headers with adding the name of the file.

I tried using this command:

for i in ./*mg/*.faa; do gawk -i inplace '/>/{gsub(">","&"FILENAME"_");gsub(/\.faa/,x)}1' $i; done

This command however renames the fasta headers with sub-folder name (*mg) and the filename. How do I modify this command to just include the filename??

fasta awk bash

2 answers

find /*mg -type f -name "*.faa" | while read F; do sed "/^>/s|\$| $(basename $F)|" "${F}" > "${F}.new" ; done

GNU parallel and seqkit answer for posterity.

parallel 'seqkit replace -p "(.+)" -r "\$1 {/}" {} > {= s/\.faa/.renamed.faa/ =}' ::: $(find ./*mg -name "*.faa")

If you get "Argument list too long" use:

find ./*mg -name "*.faa" | parallel 'seqkit replace -p "(.+)" -r "\$1 {/}" {} > {= s/\.faa/.renamed.faa/ =}'

Log in to answer this question.