morio56 has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks! I am having a problem which looks simple on paper but is proving difficult than I'd wish it to. I have a fasta file (with ids and sequences e.g >123 656 nnnnnnnnnnnnnnn) and another file with just the fasta ids (e.g 123, 234, etc) I want to search in for the ids in the fasta file and return the corresponding sequences. I was hoping to read in the the fasta file and store it in hash with keys being the ids and values the sequences then search for the keys (ids from the id files) and return the sequences. However I've not been able to achieve this as the print out only gives me the last value but all the existing keys. Something tells me I need to not only use "if exist" but also ask perl to print the corresponding value for the existing key, I haven't been able to do this. Here is my code
and here is an example of a fasta file am usingif(@ARGV < 2){ die "Not enough arguments\n"; } $sequence=""; $fastaID=""; open(FILE1,"$ARGV[0]") or die "No fasta file provided in command line: + $!\n"; while ($line=<FILE1>){ chomp($line); if ($line=~/^\s*$/){ next; }elsif ($line=~/^>/){ @data=split(" ",$line); $fastahash{$fastaID}=$sequence; $sequence=""; $fastaID=$data[1]; }else{ $sequence .=$line; $sequence=~s/\s*//g; } $fastahash{$fastaID}=$sequence; } open(OUT,"$ARGV[1]") or die "No output file:$!\n"; open(FILE2,"$ARGV[2]") or die "No fasta file provided in command line: + $!\n"; while($line2=<FILE2>){ chomp($line2); if (exists $fastahash{$line2}){ print "$line2\t $sequence\n"; } } exit;
> 2056360012 1047627436237 yyyacgagchagshgashcgahcgac acsasasasacsacsasasacaca ascassacsaascascascascac > 2056360013 1047627436238 xxxxcgagchagshgashcgahcgac acsasasasacsacsasasacaca ascassacsaascascascascac
And a sample of the fasta ids file
2056360012 2056360013
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: fasta hash
by moritz (Cardinal) on Aug 26, 2011 at 06:32 UTC | |
by morio56 (Initiate) on Aug 26, 2011 at 13:51 UTC | |
by ForgotPasswordAgain (Priest) on Aug 26, 2011 at 22:37 UTC | |
by moritz (Cardinal) on Aug 26, 2011 at 13:56 UTC | |
by morio56 (Initiate) on Aug 26, 2011 at 15:12 UTC | |
by moritz (Cardinal) on Aug 26, 2011 at 16:38 UTC | |
| |
A reply falls below the community's threshold of quality. You may see it by logging in. |