ic23oluk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'd like to read a Fasta file, consisting of header1 newline sequence1 newline header2 newline... into a hash and print it out.

I am currently learning hashes, and I don't understand why my program doesn't work. It reads all sequences into one element and doesn't store the headers at all

all headers start with a '>', so I use a regex to identify them, here's my code:

my $file = "sequences.fasta"; open (READ, "$file") || die "Cannot open $file: $!.\n"; my %seqs = (); my $header = ''; while (my $line = <READ>){ chomp $line; $line =~ s/^\s*$//; if ($line =~ m/^>.*$/){ $line = $header; } else { $seqs{"$header"} .= $line; } } close (READ); my @count_seq = keys %seqs; foreach (@count_seq){ print $_, "\n", $seqs{$_}, "\n"; }

can someone indicate the problem? Thanks in advance!

Replies are listed 'Best First'.
Re: Read FASTA into Hash
by haukex (Archbishop) on Jul 09, 2017 at 10:43 UTC

    You never assign a new value to $header. Try changing the if like this:

    if ($line =~ m/^>(.*)$/){ $header = $1;

    See perlretut.

      works! Thank you very much!

        Yes, your method works as long as your input conforms to your specification (a simple subset of the FASTA format).
        Bill