Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hello,

Further to my problems i posted yesterday i cannot seem to get the program to recognise information stored in one file and compare it to another file.

This is some of the code a colleague gave me to get it to recognise an Accession number in one file and compare it to an element in an array, obtained from a second file.

The $ref_filehandle holds the data from the reference file that contains only a list of Accession numbers, i.e.

AB014570.2 AB055861.1 AB067522.1 AB073617.1 AC004166.12 AC004851.2 AC004867.5 AC004883.3 AC005077.5 AC005080.2
Where as the @subjects holds in each element :
gi|14670349|ref|NM_032999.1| Homo sapiens general transcription factor II, i (GTF2I), transcript .... or a derivetive of.

The problem i have is that the hash %refList holds all the accession numbers in KEY = Accession VALUE = Accession, so both accession values are stored in key and value - should be an array i know but i need speed when searching through for each element of @subjects, hence the use of "defined" function.
The only problem is that the " if " statement highlighted always equates to false....WHY?

I have found, using print statements, that the hash does have the right values stored in it, but when searched for $elements[3] element it cant find it, so i tried "! defined" - and guess what, it equates to true. Knowing that the values are stored in the hash i can only assume the problem lies with the if statement.

sub get_Results() { open( NEWACC, ">ref_files/$newRefFile" ) || die "$!"; open( NEWALIGN, ">blast_files/$newBlastFile") || die "$!"; ## put list of reference accessions into a structure so we can parse i +t... my %refList = (); while (<$ref_filehandle>) # used instead of REFFILE { chomp; # removes newline character my $key = $_; $refList{ $key } = "$_"; } #PARSE BLAST RESULT HERE.... #read results file one line at a time my @resultLine = <$blast_filehandle>; # used instead of BLASTFILE my $alignment = {}; # reference to an empty hash my @subjects; # reference to empty array my $current_subject = "front_matter"; $alignment->{$current_subject} = ""; for (my $i = 0 ; $i<scalar @resultLine; $i++) { if ($resultLine[$i] =~ /^>/) { $current_subject = $resultLine[ $i ]; chomp ($current_subject); push (@subjects, $current_subject); $alignment->{$current_subject} = ""; } $alignment->{$current_subject} = $alignment->{$current_subject}.$r +esultLine[ $i ]; } my @elements; foreach my $z (@subjects) { chomp $z; @elements = split('\|', $z); if ( ! defined $elements[ 3 ] ) { print ("Parsing Error<BR>"); print ("Line $z"); } ##### PROBLEM LIES HERE WITH IF STATEMENT if (defined $elements[3] && defined $refList{$elements[3]} ) { print ("Already Present in file"); } else { print "No match in reference file"; print (NEWALIGN $alignment->{$z}."\n"); print (NEWACC $elements[3]."\n"); } } #close files close NEWALIGN; close NEWACC; }

Any help is really appreciated - including any critical appraisal as its not my handy work.

Cheers people
MonkPaul.


In reply to If statement problem with hash values by MonkPaul

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2023-02-06 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (37 votes). Check out past polls.

    Notices?