Thanks. I must have missed that in that documentation. How AWK-ward.
Hi all. Really basic question here. I'd like to grab the sequences from a FASTA file with an AWK one-liner. To grab the headers, I can do:
awk < seq.fasta '/^>/ { print $0 }'
How do I negate this, so that it grabs the lines that do NOT begin with the '>' character. Feel free to chime in with other methods to solve the problem, but I'd like to learn an AWK-specific solution as I am trying to level up my AWK.
Thanks!
3 answers
awk < seq.fasta '!/^>/ { print $0 }'
or (preferred for clarity):
awk < seq.fasta '$0 !~ /^>/ { print $0 }'
or merely:
awk < seq.fasta '$0 !~ /^>/'
or grep
grep -v ^\> seq.fasta
or some people prefer "perl one liners" for this sort of thing because you can conceivably use Perl for awk-ish filters and for your day to day scripting.
perl -lne 'print if !($_ =~ /^\>/)' seq.fasta
Perl line is not quite right. Your command will print all lines that don't have '>' anywhere. To print just those lines that don't start with '>':
perl -lne 'print if !($_ =~ /^>/)' seq.fasta
right, thanks Chris. updated the perl example to match the awk regex.
Btw the shortest awk syntax is actually:
awk '!/^>/' seq.fasta
Note that awk can take filename as an argument, but you could also use your < syntax.
awk '($0 ~ /^[^>]/)' < file.fasta
You can also use this:
grep -v '>' file.fasta
In my blog you can find a comprehensive posto about formatting and splitting fasta files using python scripts:
http://basicbioinformatics.blogspot.com/2011/10/split-fasta-file.html
A minor point, but you really want to ensure that the > starts at the beginning of the line, per the FASTA spec.
Is there any valid fasta where this is a problem? I mean, if the > is in the middle of the header line, then the header line still gets captured properly. If it's in the middle of your sequence, then you have a bigger problem (i.e. file corruption) on your hands.
I have the same question about # in header of vcf files. I always filter just for # and so far no burns, but is there any realistic scenario where # can appear below header in a valid vcf?
I guess I can imagine somebody could put # into CHROM, ID, or FILTER columns of a vcf - so maybe I can start doing the proper ^# thing. I still think that this is a non-issue for fasta though. You either capture header or you have corrupted sequence, neither is solved by searching ^>. On the contrary in fact, since you will not notice immediately that your seq is corrupted. My 2cents.
i.e. grep -v "^>"
Log in to answer this question.
IMHO, you really want to "level up" in regular expressions, not awk specfically. The more experience you develop with regex, you'll be able to apply it to awk, sed, and grep (as well as most programming languages) equally well.