restricting module use in Perl script
3
0
Entering edit mode
7.6 years ago
mccormack ▴ 90

I want to use the perl module re::engine::TRE in my perl script for fuzzy matching, but not for all the regex's in the script, only for certain regex's.

re::engine::TRE brings in a different regex engine than the Perl default. But, I want the default regex engine for some of the regex's in the script and re::engine::TRE for other regex's.

What would be the best way of going about to do this ?

sequence • 1.8k views
ADD COMMENT
0
Entering edit mode

A question more suitable for Perlmonks perhaps.

ADD REPLY
2
Entering edit mode
7.6 years ago

i don't use perl, but the manual says that it can work in a lexical scoping http://search.cpan.org/~syp/re-engine-TRE-0.09/lib/re/engine/TRE.pm

Details of lexical scoping in perl http://docstore.mik.ua/orelly/perl/prog3/ch04_08.htm

Ps: I'm also guessing that it should work in a block of curly braces, or some similar braces which defines a local scope in perl.

ADD COMMENT
0
Entering edit mode
7.6 years ago
mccormack ▴ 90

Thank you Santosh and genomax2. Yes, the scoping will be important but I am not sure of whether I should make a module or package or subroutine or how to best go about it. Perlmonks would probably have more experience with this. Thanks for your answers.

ADD COMMENT
0
Entering edit mode

Just curious: what happens if you try to use this module inside a curly brace, only where it is needed. Something like..

{use re::engine::TRE; # your rest of the code needing this Engine}

Does the effect of the TRE-Engine remains persistent after the curly braces, or does it revert back to perl default engine?

ADD REPLY
0
Entering edit mode
7.6 years ago
mccormack ▴ 90

Nice question. It will revert back to the perl default engine. Here is some code:

#!/usr/bin/perl
use strict;
use warnings;

my $seq1 = "TTTTACGAGAGAATATGTTAGGTGAAGGAACCTCTATCTGAGAGAAAAA";
my $seq2 = "TTTTGAGCTCGTTGTCGATCCGAGGTACTTTTGAATCCGCAGTTTCTTG";

if ("A pearl is a hard object produced ..." =~ /\(Perl\)/i)
  {
   print "$1\n";
  }

{
       use re::engine::TRE max_cost => 5;
       if(($seq2 =~ /GTTGTTCGATCCAGGTAC/) && ($seq1 =~ /ACGAGAGATAGATGA/))
          {
           print "Have a match\n";
          }
 }

my $ifile = "3C49_283831w_CF4.fastq";
$ifile =~ /(\w*)\.fastq/;
my $ifile_name = $1;
print "ifile name: $1\n";

This will print out:

Have a match

ifile name: 3C49_283831w_CF4

However, this code:

#!/usr/bin/perl
use strict;
use warnings;

  use re::engine::TRE max_cost => 5;

my $seq1 = "TTTTACGAGAGAATATGTTAGGTGAAGGAACCTCTATCTGAGAGAAAAA";
my $seq2 = "TTTTGAGCTCGTTGTCGATCCGAGGTACTTTTGAATCCGCAGTTTCTTG";

if ("A pearl is a hard object produced ..." =~ /\(Perl\)/i)
  {
   print "$1\n";
  }

{
     use re::engine::TRE max_cost => 5;

   if(($seq2 =~ /GTTGTTCGATCCAGGTAC/) && ($seq1 =~ /ACGAGAGATAGATGA/))
      {
        print "Have a match\n";
      }
}

my $ifile = "3C49_283831w_CF4.fastq";
$ifile =~ /(\w*)\.fastq/;
my $ifile_name = $1;
print "\nifile name: $1\n";

Will print out:

pearl

Have a match

Use of uninitialized value $1 in concatenation (.) or string at TRE_test.pl line 30.

ifile name:

BTW, I cross posted this question on Perlmonks, http://perlmonks.org/index.pl?node=Seekers%20of%20Perl%20Wisdom and PerlGuru, http://perlguru.com/gforum.cgi?post=83921;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;guest=36323138

ADD COMMENT
0
Entering edit mode

Doesn't the first example solve your problem? Use the TRE-engine with curly braces, wherever you need it. Outside the braces, it will be out of scope. That is what I meant by my scoping comment earlier.

PS: In the 2nd example, the TRE engine will be activated for whole code, as the scope is not limited. Don't understand why you used that example

ADD REPLY
0
Entering edit mode

Yes, the first example does solve my problem. The second example was put in to show that outside of the curly braces perl reverts back to its default engine.

The first code where the re::engine::TRE is limited to inside the braces is able to extract the ifile name from the .fastq suffix (3C49_283831w_CF4 is taken from 3C49_283831w_CF4.fastq), but the in the second example where re::engine::TRE is global, the script was unable to extract the ifile name because re::engine::TRE can not handle that regular expression.

If, in the first example, re::engine::TRE was still still persistent after the {}, then the script would not have been able to extract the ifile name. The fact that the first script did extract the ifile name shows that perl's default regex engine was being used after re::engine::TRE was used between the {}.

ADD REPLY
0
Entering edit mode

Go ahead and "accept" this (and perhaps @Santosh's) answers (you can accept multiple answers, use green check-mark) to provide closure to this thread.

ADD REPLY

Login before adding your answer.

Traffic: 2522 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6