Extraction Of Header Of Sequences In Fasta File
3
2
Entering edit mode
12.9 years ago

Hi all I have a fasta file that i want to extract just header of sequences. is there any perl code or some thing like this to do that? thanks a lot in advance

regards

perl fasta python parsing • 50k views
ADD COMMENT
1
Entering edit mode

By "header", you mean everything after the ">"? Or just some part of everything after the ">"? Or including the ">"? It's important to be specific since a lot of people misunderstand "header".

ADD REPLY
0
Entering edit mode

I just want everything after the ">". and i have to say that i am not familiar with perl and i want a perl code to run. if possible help me. thanks a lot. regards

ADD REPLY
0
Entering edit mode

err, why don't you just post your code then?

ADD REPLY
16
Entering edit mode
12.9 years ago

For perl code, you can visit http://www.bioperl.org/wiki/Main_Page. If you just want to extract the headers, on a Linux/Unix system, a simple grep "^>" myfile.fasta should work.

ADD COMMENT
14
Entering edit mode
12.9 years ago
Michael 55k

Why so complicated? ;) Only the header in a fasta file contains > so you can use grep :

grep -e ">" my.fasta

or awk to remove the >:

$ awk 'sub(/^>/, "")' 
>aksdjfljfd
aksdjfljfd
ADD COMMENT
0
Entering edit mode

Thanks so much, but i am not familiar with perl code. i need a complete code to run it. if possible guide me more. thanks again

ADD REPLY
0
Entering edit mode

Thanks. I fixed my problem. regards

ADD REPLY
0
Entering edit mode

this is not perl, it's unix ;)

ADD REPLY
0
Entering edit mode

what about i want to extract the header and their belonging sequences?

ADD REPLY
0
Entering edit mode

$ awk 'sub(/^>/, "")' your_file.fasta > desired_headers.txt

This is a single-line code solution. No residual ">" before each header.

Learned from the blog "AWK: the substr command to select a substring" by Thomas Cokelaer

https://thomas-cokelaer.info/blog/2011/05/awk-the-substr-command-to-select-a-substring/

ADD REPLY
0
Entering edit mode

Yeah, sure, no rocket science ....

ADD REPLY
7
Entering edit mode
12.9 years ago
Caddymob ★ 1.0k

Expression in perl would be basically the same as the grep above (m/^>/).. There are easier 1-liner ways to do this, but this is a basic outline of the perl code that should be pretty readable.

#!/usr/bin/perl

open(FASTA, "<your.fa");
while(<FASTA>) {
    chomp($_);
    if ($_ =~  m/^>/ ) {
        my $header = $_;
        print "$header\n";
    }
}
ADD COMMENT
0
Entering edit mode

thanks so much. your code is ok but how can i write it in a text file. i am not familiar with perl code.

thanks

ADD REPLY
0
Entering edit mode

thanks so much i fixed my problem. regards

ADD REPLY

Login before adding your answer.

Traffic: 2533 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6