UPDATE: Solved and removed if(defined $seq) line
Hello monks, I have been trying a bio perl challenge site- so meta hints are welcome, too. I am having trouble reading a file into a hash properly. My data will be a fasta file in the format of:
>sequence_5849 CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCC TCCCACTAATAATTCTGAGG >sequence_5959 CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCT ATATCCATTTGTCAGCAGACACGC >sequence_0808 CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGAC TGGGAACCTGCGGGCAGTAGGTGGAAT
my code is:
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; use feature qw(say); my $file = 'file.txt'; open (my $fh, '<', $file) or die "Could not open file '$file' $!"; my (%sequence_hash, $header, $seq, $count); while ( my $line = <$fh> ) { chomp($line); if ( $line =~ m/^>(.*)/ ) { if ( $seq ) { say $seq; $sequence_hash{$header} = $seq; } $header = $1; $seq = ''; } else { $seq .= $line; } } close $fh; print Dumper(\%sequence_hash);
My problem is that I am not getting the header and sequence. I have a feeling that its because of clearing out the $seq variable, but I am not sure how else to get the header and sequences in the hash. Any insight would be highly appreciated :)

In reply to Reading file into a hash by PerlSufi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.