MonkPaul has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Im working on a script at the moment that looks through a file and stores each line into an element for a given record. Say one array per record.

I know that in the last element of the array that their is information relating to an organisms species. I previously split the $line on a "|" and printed the elements to the screen, so i know that the array holds the right info.

What i now want to do is show in a web page only those that match e.g Homo sapiens. This value to filter by is chosen by the user on a web page and stored in a variable called $species_filter.

I have tried to just get the regex working for a simple case when it first failed but even that didnt work. The example of the array stuff is
gi|2827179|gb|AF035737.1|AF035737 Homo sapiens general transcription factor 2-I (GTF2I) mRNA, complete.
The important parts of the code are below:

sub filter_by_Species() { print("New Blast File Opened<BR>"); open( NEWALIGN, "<blast_files/$newBlastFile") || die "$!"; print("Filtering Data........Please wait<P>"); #PARSE BLAST RESULT HERE.... #read results file one line at a time my @resultLine = <NEWALIGN>; # 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} = ""; #so I produce a hash with key = >gi line of report and value = all lin +es after until next >gi 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} means give value of $alignment when $current_subject is the + key } $alignment->{$current_subject} = $alignment->{$current_subject}.$r +esultLine[ $i ]; } my @elements; my $boolean; print("<TABLE BORDER=2>"); print("<th align=center><B>Status</B></th>"); print("<th align=center><B>Information on new hits</B></th>"); foreach my $z (@subjects) { chomp $z; @elements = split('\|', $z); if($elements[4] =~ /^$species_filter/) ##### filtering part here { $boolean = 0; print ("<TR><TD>Match to $species_filter"); show_Output($boolean, $z, @elements); } } print("</TABLE><BR><BR>"); #close files close NEWALIGN; } ###################################################################### +########## # Display the blast results to user in easy to view format sub show_Output() # HTML code { my ($boolean, $z, @elements_copy) = @_; $elements_copy[1] = '<a href="http://www.ncbi.nlm.nih.gov/entrez/v +iewer.fcgi?db=nucleotide&val='.$elements_copy[1].">Link</a>"; if($boolean == 0) { print("<TR><TD> $z </TD><TD>@elements_copy[1]</TD></TR>"); } else { print("<TD> $z <BR>&nbsp - New value added to new reference list + &nbsp</TD><TD>@elements_copy[1]</TD></TR>"); } }

The books i have for this sort of thing arent very good so im struggling a bit with the use of Regex.

Can anybody give me some pointers on why its failing to find $species_filter. (Oh and im not sure if i can use /^$species_filter/ as i think that it may just look for ^"$species_filter" and not its value.

Cheers
MonkPaul.

Replies are listed 'Best First'.
Re: RegExpression fails to work
by jeffa (Bishop) on Jun 14, 2005 at 13:31 UTC

    I wish i had more time to go over this with you, but right off the bat:

    my @subjects = []; # reference to empty array
    is wrong. That is an array that holds an empty array reference. Try this instead:
    my @subjects;
    There is no need to assign anything, just declare the variable. Further more, this loop:
    for (my $i = 0 ; $i<scalar @resultLine; $i++) {
    can be written in a more Perlish fashion like so:
    for my $i (0 .. $#resultLine) {
    This is Perl, not C. :) Finally, do yourself a big favor. Slow down and read some material. I recommend davorg's excellent book, Data Munging With Perl.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Hey, thanks.
      I had a problem with the first element of the array causing some problems with printing out the hashref&&& stuff on the screen. Did not realise that it was the @array = []; causing the problem.

      MonkPaul.

Re: RegExpression fails to work
by tlm (Prior) on Jun 14, 2005 at 13:24 UTC

    You may want to use \Q...\E , e.g.

    /^\Q$species_filter\E/
    ...to quote any special regex chars contained in $species_filter.

    the lowliest monk

Re: RegExpression fails to work
by thundergnat (Deacon) on Jun 14, 2005 at 14:15 UTC

    To answer your primary question, the /^$species_filter/ match is failing because the $species_filter does not come at the beginning of the string. Remove the caret to match correctly.

    for the string:

    'AF035737 Homo sapiens general transcription factor 2-I (GTF2I) mRNA, +complete.' /Homo sapiens/ will match /^Homo sapiens/ will not

    However there are many other things wrong with this routine. Here's a few comments.


    sub filter_by_Species() # Don't use prototypes unless you NEED them. And even then, you probab +ly don't. open( NEWALIGN, "<blast_files/$newBlastFile") || die "$!"; #use lexical filehandles and three argument opens to prevent # obscure bugs. The || is very high precedent. Not wrong # really, but unecessarily powerful. #read results file one line at a time my @resultLine = <NEWALIGN>; # Arggh. Read entire file into an array at once. my @subjects = []; # reference to empty array # nope. Array whose first element is a reference to an empty array #so I produce a hash with key = >gi line of report and value = all lin +es after until next >gi # I saw no "greater than" in the example data... for (my $i = 0 ; $i<scalar @resultLine; $i++) # C style loops. Avoid unless necessary. They work ok but # clutter up the script and make it harder figure out what # you are doing. if ($resultLine[$i] =~ /^>/) # again, no "greater than" in the example data... { $current_subject = $resultLine[ $i ]; chomp ($current_subject); push (@subjects, $current_subject); $alignment->{$current_subject} = ""; $alignment->{$current_subject}.$resultLine[ $i ]; } # you never use the %$alignment hash anywhere so why bother? my @elements; my $boolean; # declare variables in the smallest scope possible foreach my $z (@subjects) { chomp $z; # already chomped in previous loop show_Output($boolean, $z, @elements); # boolean ALWAYS equal 0. Why not just pass 0?

    If I were to write this getting the same functionality it would look something like:


    sub filter_by_Species { print 'New Blast File Opened<BR>'; open(my $newalign, '<', "blast_files/$newBlastFile") or die $!; print 'Filtering Data........Please wait<P>'; my @subjects; while <$newalign> { push @subjects, $_ if (/^>gi/) } print '<TABLE BORDER=2>'; print '<th align=center><B>Status</B></th>'; print '<th align=center><B>Information on new hits</B></th>'; for (@subjects) { chomp; my @elements = split '\|'; if($elements[-1] =~ /$species_filter/) { print "<TR><TD>Match to $species_filter"; show_Output(0, $_, @elements); } } print'</TABLE><BR><BR>'; }

    Also, since your show_Output used blank prototypes, it never recieves the passed values. Again, don't use prototypes unless you NEED to. Never mind that last bit, that is not true.

    Update: fixed a few typos.

      Thanks a lot for your help.

      The symbol " > " was not a greater than symbol though, it is the start of the record in the file: >gi|2827179|gb|AF035737.1|AF035737........etc I was not aware that the prototypes were also causing a problem. Will have to sort that out. thanks.

      MonkPaul.