Perl one liner to extract sequences by their ID from a FASTA file - help
1
3
Entering edit mode
8.3 years ago
pendragon ▴ 50

Hi, I'm EXTREMELY new to perl and just about all serious bioinformatic work. I gratefully found some one-liner perl scripts from the Edwards Lab, one of which works and the other (and the one most useful to me) does not. This line (below) DOES work and extracts and prints particular sequences (id1 and id2 by name):

perl -ne 'if(/^>(\S+)/){$c=grep{/^$1$/}qw(id1 id2)}print if $c' fasta.file

However I have a txt file with all the names of the sequence identifiers (a set of receptor gene sequences) and I'd like to extract them from a large fasta file (in this case the mouse refseq database). This is a another line (below) from the same lab that should be able to do this, but I've had no luck.

perl -ne 'if(/^>(\S+)/){$c=$i{$1}}$c?print:chomp;$i{$_}=1 if @ARGV' ids.file fasta.file

Does anyone have any suggestions about what may be going wrong? Or other ways to efficiently do this as a complete beginner? I recognize this is quite a vague post, and there are related posts about performing similar tasks but I've been struggling to find efficient ways to do this and not get totally mired in possibilities.

fasta perl • 3.4k views
ADD COMMENT
0
Entering edit mode
ADD REPLY
3
Entering edit mode
8.3 years ago
Asaf 10k

It's a very easy task and a good practice. You'll spend much less time writing a real script which will be documented and version controlled than fixing a magic one liner which you'll then search in your emails to re-run. The script you'll write can be then used for other manipulations of fasta files and you'll already have the platform for implementing them quickly.

ADD COMMENT
0
Entering edit mode

Yeah I agree with this. I tend to see a lot of command-line warriors these days who spend 5 minutes perfecting a one-liner when they could have easily wrote the script in a minute. Work your data, not your tools.

ADD REPLY

Login before adding your answer.

Traffic: 1381 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