PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:
my code is:>sequence_5849 CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCC TCCCACTAATAATTCTGAGG >sequence_5959 CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCT ATATCCATTTGTCAGCAGACACGC >sequence_0808 CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGAC TGGGAACCTGCGGGCAGTAGGTGGAAT
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 :)#! /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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading file into a hash
by davido (Cardinal) on May 28, 2014 at 16:56 UTC | |
by PerlSufi (Friar) on May 29, 2014 at 19:45 UTC | |
|
Re: Reading file into a hash
by hippo (Archbishop) on May 28, 2014 at 14:51 UTC | |
by PerlSufi (Friar) on May 28, 2014 at 14:53 UTC | |
by LanX (Saint) on May 28, 2014 at 15:14 UTC | |
|
Re: Reading file into a hash
by LanX (Saint) on May 28, 2014 at 14:53 UTC | |
by PerlSufi (Friar) on May 28, 2014 at 14:56 UTC |