how to filter fasta file?
Hello, I need to filter this fasta file, on the basis of len mentioned in the header of the fasta file, I need less than equal to 100 len. I am new to bioinformatics, please let me know the one-line command for this.
>CM0 len:16 (+),score=7.52 CM040936.1:11567243-11571 523:2589-2636(+)
ATGGCATTAGTTCTGGCAGGTCACGTGAGTCAAGCTCGCATCAGCTGA
Thank you.
• 2,235 views
•
link
4 answers
Hi! I believe this command would do what you are looking for.
awk 'BEGIN{RS=">"}NR>1{sub("\n","\t"); gsub("\n",""); print RS$0}' test.fa | tail -n+1 | awk -F"\t" '{split($1,h,"len:");split(h[2],l," "); if (l[1]<=100){print}}' | awk -F'\t' '{print $1"\n"$2}'
This "one-liner" does different things:
1) convert fasta into tab format with:
awk 'BEGIN{RS=">"}NR>1{sub("\n","\t"); gsub("\n",""); print RS$0}' test.fa
1.5) Remove the unwanted first blank line with:
tail -n+1
2) Filter based on "len:" field in the header with:
awk -F"\t" '{split($1,h,"len:");split(h[2],l," "); if (l[1]<=100){print}}'
3) Go back from tab to fasta format with:
awk -F'\t' '{print $1"\n"$2}'
• 0 views
•
link
Using reformat.sh from BBMap suite.
reformat.sh -Xmx2g in=input.fa out=filtered.fa maxlength=100
• 0 views
•
link
Log in to answer this question.
duplicate of How To Filter Multi Fasta By Length?? ; FASTA file of fixed length ; Fasta Length ;