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

Hi, please bear with me as I'm new to Perl. I have two arrays: one is a list of full gene names (GI), and one is a list of just their accession numbers (Accession). It looks like:

my @GI = ("\Qgi|Q384722390|emb|WP_938420210.1|Gene name\E","\Qgi|34254 +6780|emb|WP_934203412.1|Gene name\E"); my @Accession = ("WP_938420210.1","WP_934203412.1");

That is only an abbreviated example. In the real program, the GI list is much longer because it contains all the genes, and the Accession array only contains the Accession numbers of the ones I'm interested in. The Accession numbers are part of the GI full name, so I thought I could use a regular expression to go through each element of the arrays and find matches for the 469 accession numbers, like this:

my $X = 0; my $Y = 0; while($X <= 468){ if(/$Accession[$X]/ =~ $GI[$Y]){ print $GI[$Y]; $X = $X + 1; $Y = 0; } else{$Y++}; }

However, when I do this, I get the error "Use of uninitialized value in pattern match (m//) and also use of uninitialized value within @GI in regexp compilation. Does anyone know what I am doing wrong? Thank you!

Replies are listed 'Best First'.
Re: Using regular expressions with arrays
by AnomalousMonk (Archbishop) on Apr 13, 2015 at 16:37 UTC

    Maybe one way:

    c:\@Work\Perl\monks>perl -wMstrict -le "my @GI = ( 'gi|Q384722390|emb|WP_938420210.1|Gene name', 'gi|342546780|emb|WP_934203412.1|Gene name', 'gi|342546780|emb|WP_93420341211|Gene name', 'gi|987654321|emb|WP_555555555.1|Jean name', ); ;; my @Accession = qw(WP_938420210.1 WP_934203412.1); my ($rx_acc) = map qr{ $_ }xms, join q{|}, map quotemeta, @Accession ; ;; GENE: for my $gi (@GI) { next GENE unless my ($acc) = $gi =~ m{ \b ($rx_acc) \b }xms; print qq{'$acc' in '$gi'}; } " 'WP_938420210.1' in 'gi|Q384722390|emb|WP_938420210.1|Gene name' 'WP_934203412.1' in 'gi|342546780|emb|WP_934203412.1|Gene name'

    Updates:

    1. Noticed accession numbers have  . (dot) metacharacter in them: added  map quotemeta, step to building accession number regex to make dot an ordinary literal character.
    2. Added some negative matches to  @GI test cases.
    3. The match expression  m{ (?<= [|]) ($rx_acc) (?= [|]) }xms may be preferable | is almost certainly better because  | (pipe) characters are exactly what must match before and after an accession number (\b just requires a word boundary). (Tested; same output.)
    4. Usually, the regex engine begins searching a string from the beginning of the string. If you know that a match cannot possibly start before a certain character offset in the string, this information can be used to allow the RE to jump ahead in its search, skipping over characters in which there can be no match and potentially speeding a match. Further, if it is known that interpolated regex components (like  $n and  $rx_acc below) will not change during script execution, the  /o regex modifier (see  m/PATTERN/ in the Regexp Quote-Like Operators section of perlop) can be used to prevent regex re-compilation on each pass through the loop, again potentially speeding matches. So a loop like:
      my $n = 12; GENE: for my $gi (@GI) { next GENE unless my ($acc) = $gi =~ m{ \A .{$n,}? (?<= [|]) ($rx_acc) (?= [|]) }xms +o; print qq{'$acc' in '$gi'}; }
      (tested; same output) may be significantly faster; this can only be determined for certain by some kind of Benchmark-ing.
    5. Actually, in answer to the question posed by hippo in the reply below, I think a hash-based approach would probably be more appropriate (insofar as I understand the problem): simpler, more maintainable, likely much faster than a regex approach.


    Give a man a fish:  <%-(-(-(-<

Re: Using regular expressions with arrays
by hippo (Archbishop) on Apr 13, 2015 at 16:50 UTC

    Would a hash not be more appropriate? If we remove the \Q and \E from the data, then it's almost trivial:

    #!/usr/bin/perl -w use strict; use warnings; my @GI = ("gi|Q384722390|emb|WP_938420210.1|Gene name", "gi|342546780|emb|WP_934203412.1|Gene name"); my @Accession = ("WP_938420210.1","WP_934203412.1"); # Convert to hash my %foo = (); for my $gene (@GI) { my @fields = split (/\|/, $gene); $foo{$fields[3]} = $gene; } # Now extract for my $accno (@Accession) { print "Acc no: $accno, Gene: $foo{$accno}\n"; }
Re: Using regular expressions with arrays
by jeffa (Bishop) on Apr 13, 2015 at 16:51 UTC

    If you wish to loop through an array, you probably want a for loop, not a while loop.

    for my $target (@Accession) { }

    While inside the for loop, you can then grep out any rows that match from @GI:

    my @found = grep /$match/, @GI;

    More than likely you only ever match one element, but you should be thinking about the notion that you might find multiples. Once you find your match(es), you will then most likely want to split the record(s) up. Put the whole thing together and you have something like this:

    use strict; use warnings; use Data::Dumper; my @GI = ( 'gi|Q384722390|emb|WP_938420210.1|Gene name', 'gi|342546780|emb|WP_934203412.1|Gene name', ); my @Accession = ( 'WP_938420210.1', 'WP_934203412.1' ); for my $match (@Accession) { my @found = grep /$match/, @GI; print "$match\n", Dumper [ split /\|/, $_ ] for @found; }

    UPDATE: You know ... since you already have all that data within @GI and it is already in memory, you could ease the problem by using a HoA (hash of arrays) instead of an Array that essentially contains unparsed CSV lines. Once you have this HoA, you simply lookup your accession value. The following code should demonstrate and notice that i am taking the liberty to rename these variables:

    use strict; use warnings; use Data::Dumper; my @genes = ( 'gi|Q384722390|emb|WP_938420210.1|Gene name', 'gi|342546780|emb|WP_934203412.1|Gene name', ); my @accession = ( 'WP_938420210.1', 'WP_934203412.1' ); my %genes = map { my @s = split /\|/, $_; ( $s[3] => [@s] ); } @genes; print Dumper \%genes; # list the 2nd field from each gene for (@accession) { print $genes{$_}[1], $/; }
    Now then ... i hope that really helps! :)

    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)
    
Re: Using regular expressions with arrays
by kennethk (Abbot) on Apr 13, 2015 at 16:53 UTC
    You are getting warnings about uninitialized values because you are walking off the end of your arrays (I think). Am I correct in thinking you'd like to systematically check all Accession numbers against your GI list? To do that, you need to iterate over both lists, which in general means you need two loops - one for GI and one for Accession. If you also use last (assuming max 1 hit per target) to do some Loop Control, you might code something like:
    for my $Accession (@Accession) { for my $GI (@GI) { if ($Accession =~ $GI) { print "$GI\n"; last; } } }
    Note I've used the Foreach Loops construct instead of using an explicit index because you do not actually care for your output what the index was.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Please see my reply to james28909 questioning the wisdom of depending on certain characters (separators) of the data to be regex metacharacters while ignoring the fact that others are also.


      Give a man a fish:  <%-(-(-(-<

Re: Using regular expressions with arrays
by james28909 (Deacon) on Apr 13, 2015 at 17:00 UTC
    Youre getting these errors because your while loop loops 468 times. Each time it iterates it increases both $X and $Y, which in turn will cause the loop to search for an array value that indeed is not initialized. Either add 466 more items to each array, or decrease the amount of loops.

    I would use a different approach myself. Soemthing like:
    use strict; use warnings; my @GI = ( "gi|Q384722390|emb|WP_938420210.1|Gene name", "gi|342546780|emb|WP_934203412.1|Gene name" ); my @Accession = qw( WP_938420210.1 WP_934203412.1 ); my $i = 0; for my $A(@Accession) { for my $G(@GI) { if ( $A =~ $G ) { print "$G\n"; } } }

      This approach depends on the fact that the CSV separator character in  "gi|Q384722390|emb|WP_938420210.1|Gene name" ( |(pipe)) is also the regex ordered alternation operator, but ignores the fact that  . (dot) in  |WP_938420210.1| is also a regex metacharacter that matches (almost) anything. Is this a good idea?


      Give a man a fish:  <%-(-(-(-<