Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: fasta file

by BrowserUk (Pope)
on Sep 26, 2011 at 22:51 UTC ( #927967=note: print w/ replies, xml ) Need Help??


in reply to fasta file

There are many problems with your script.

When you read the lines in from the file

while($_ = <FILENAME>){

They have newlines attached to their ends. And when you store the sequence ID in teh key of the hash:

$accession{$seq} .= $_;

You have not removed it. But when you read the accession iD from the user:

print "Please enter in the accession_ID: "; chomp($accession_id = <STDIN>);

You did remove the newline. So when you try to compare those two:

if ($accession{$g} =~ m/$accession_id/i){

They will not match.

BUT ... when you are trying to compare the id entered by the user with the one read from the file, instead of comparing it with the key (ID) of the hash element:

if( $g =~ m/$accession_id/i){

You are comparing it with the sequence (value) of that hash entry:

if ($accession{$g} =~ m/$accession_id/i){

Which obviously wouldn't work, even if you had removed the newlines from both.

But then, even if you corrected all those problems, there are many other problems with your program.

First you read all the sequences into the hash -- despite that you know you are only looking for one and could stop reading as soon as you've seen that one.

And then you iterate over the hash checking if each of them matches the ID the user entered -- but the whole point of a hash is that you do not need to iterate over it. You can simply look up whether the key you need exists.

Finally, you should take more care over how you format your code. Think of your code as your workbench. If you keep it neat and tidy, you'll find it a lot easier to find what you are looking for.

Also, if you are intending using Perl to do work, rather than as a hobby, I strongly recommend that you take a basic programmers course, or at least work your way through one of the Learning Perl type books available. It would pay you dividends over and over in the long term.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: fasta file
Select or Download Code
Re^2: fasta file
by PrincePerl (Novice) on Sep 29, 2011 at 20:29 UTC

    Hello BrowerUk, I really appreicate the feedback, I was doing the wrong thing all along by not looking through the code. i finally figured it out. This was my revised code. Thanks again.

    #!/usr/bin/perl -w use strict; my $filename; my $accession_id; my %accession; my $gene_Name; print "Please enter in the filename: "; chomp($filename = <STDIN>); print "Please enter in the accession_ID: "; chomp($accession_id = <STDIN>); open(FILENAME, $filename) or die "cannot open file: $! "; while($_ = <FILENAME>) { if ($_ =~ />gi/) { $gene_Name = $_; } else { } $accession{$gene_Name} .= $_; } foreach $gene_Name (keys %accession) { if ($gene_Name =~ /$accession_id/i) { print "$accession{$gene_Name}\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://927967]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-07-14 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (254 votes), past polls