This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extracting interval from fasta header

Hi,

Im wondering about the most straightforward way to extract the interval information contained in a fasta header such as the one below, thanks! Also maybe to pipe into a newly created bed file.

>Mouse|chr12:112380949-112381824 | element 3 | positive  | neural tube[4/4] | hindbrain (rhombencephalon)[4/4] 
aAaaGAATAAGGCTCTGACATGTTATCCttaagagatttactgttatctgcctgtttcatgtttgctttttctttgacacagaatgtaatgcagcccagactggcctcaacttagtatgt
fasta header bed interval

Hello,

we are currently developing SEDA (http://www.sing-group.org/seda/ ), an application to easily process FASTA files.

Among its functions, the "Rename header" option (see section 3.7 of the manual: http://www.sing-group.org/seda/downloads/manuals/seda-user-manual-1.0.0.pdf) has been precisely created for doing what you are looking for. Please, have a look at it and do not hesitate to contact us in case you need further help.

With best regards,

Hugo.

2 answers

You can use the following oneliner

cat fasta_file.fa | grep '^>' | cut -d "|" -f2 | sed -e 's/:/\t/' -e 's/-/\t/' > fasta_to_bed.bed

This will generate a file with bed intervals

chr12   112380949   112381824

P.S. You might be interested to sort the bed file.

You can use awk and BEDOPS sort-bed to generate a sorted BED file from the intervals in the headers:

$ awk -vOFS="\t" -vFS="|" '/^>/ { n=split($2, a, /[:-]/); print a[1], a[2], a[3]; }' in.fa | sort-bed - > out.bed

If you use OS X, use Homebrew to install GNU awk via: brew install gawk, and replace awk with gawk.

Log in to answer this question.