Skip to content

Commit

Permalink
simplify fasta parser and add support for gzipped fasta input
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuels committed Jan 27, 2012
1 parent d97f4d2 commit 5987b40
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions lib/FastaDatabase.pm
Expand Up @@ -59,26 +59,19 @@ sub from_fasta {
my $seqdata = {};

for my $filename ( @files ) {
my $sep = $/;
local *FILE;
open FILE, "<$filename" or die "Couldn't open '$filename': $!";

$/ = ">";
my $dummy = <FILE>;
my $name;
while (($/ = "\n", $name = <FILE>)[1]) {
chomp $name;
$name =~ s/^\s*(\S+).*$/$1/; # throw away description metadata after name
$/ = ">";
my $seq = <FILE>;
chomp $seq;
$/ = $sep;

my $gzip = $filename =~ /\.gz(ip)?$/i ? ':gzip' : '';

open my $fh, "<$gzip", $filename or die "Couldn't open '$filename': $!";

local $/ = "\n>";
while( my $entry = <$fh> ) {
chomp $entry;
my ( $name, $seq ) = split "\n", $entry, 2;
($name) = $name =~ s/^\s*>\s*(\S+)//;
$seq =~ s/\s//g;
$seqdata->{$name} = $seq;
}

close FILE;
$/ = $sep;
}

my $self = $class->new ('seqdata' => $seqdata);
Expand Down

0 comments on commit 5987b40

Please sign in to comment.