http://qs1969.pair.com?node_id=1076057


in reply to Can you make this code shorter and/or quicker as well?

Perhaps the following will be helpful:

use strict; use warnings; local $/ = '>'; while (<>) { chomp; my ( $id, $seq ) = /(.+?\n)(.+)/s or next; $seq =~ s/\s+//g; print ">$id$seq\n"; }

Command-line usage: perl script.pl fastaIn [>fastaOut]

The last, optional parameter directs output to a file.

Replies are listed 'Best First'.
Re^2: Can you make this code shorter and/or quicker as well?
by Anonymous Monk on Feb 25, 2014 at 00:26 UTC
    How can I put it in a sub-routine that I could use inline my script?
    Something like:
    sub extract_query_data($file);
      Sorry, I meant:
      extract_query_data($file);

        Here's one option:

        use strict; use warnings; for my $fastaRec ( extract_query_data('fastaFile.fa') ) { print $fastaRec, "\n"; } sub extract_query_data { my ($file) = @_; my @arr; local $/ = '>'; open my $fh, '<', $file or die $!; while (<$fh>) { chomp; my ( $id, $seq ) = /(.+?\n)(.+)/s or next; $seq =~ s/\s+//g; push @arr, ">$id$seq"; } close $fh; return @arr; }

        You'll note that the subroutine returns an array of fasta records.