Sudeep
Sudeep

Reputation: 3195

perl subroutine

I am having trouble with execution of my perl subroutines. Here is the definition:

sub primer{
     print STDERR "primer is $_[0]\n";
    $primer=$_[0];
   if ($_[0]=~/ATTACCGC/){
           mkdir ("Primer1") || die "Unable to create directory <$!>\n";
           open OUTFILE1,">","Primer1/lt450";
           open OUTFILE2,">","Primer1/no_primer_lt450";
           open REPORT,">","Primer1/Report";
           &primer_analysis('ATTACCGC');}

           if ($_[0]=~/CCGTCAATTC[AC]/){
           mkdir ("Primer2") || die "Unable to create directory <$!>\n";
           open OUTFILE1,">","Primer2/lt450";
           open OUTFILE2,">","Primer2/no_primer_lt450";
            open REPORT,">","Primer2/Report";
           &primer_analysis('CCGTCAATTC[AC]');}

Here is part of another subroutine that should be invoked by above:

sub primer_analysis{
     $primer=$_[0];
    while ($line = <INFILE>){
         if ($line =~ /^>/) {
             $header = $line;
            $headcnt++;}
         if ($line !~/^>/){
         $seq = $line;
          chomp($seq);
        if (length($seq)<450 && $seq=~/^$primer/){
            $len_450=length($seq);
            $TB_450=$len_450+$TB_450;
               $cnt450++;
              print OUTFILE1 "$header";
              print OUTFILE1 "$seq\n";}}

And following is calling the function:

&primer('ATTACCGC');
&primer('CCGTCAATTC[AC]');

problem is that when I run the program only the Primer1 gets executed i.e. directories and data is properly created, however, nothing happens to Primer2. Any ideas why only one part of the function is getting executed? Thanks

Upvotes: 2

Views: 267

Answers (2)

Ian Gibson
Ian Gibson

Reputation: 1

General advice:

  1. Use strict; use warnings;
  2. Consider using shift instead of $_[0], i.e.

    $primer = shift;
        if $primer =~ /ATTACCGC/){ ..... etc.
    

    instead of

    $primer=$_[0];
        if ($_[0]=~/ATTACCGC/){
    

    ...and then use $primer instead of $_[0] in your function.

  3. Lexical filehandles are recommended.
  4. Don't use the ampersand to call your functions.

Upvotes: 0

Dan
Dan

Reputation: 10786

the characters [] are special characters in regular expressions. You need to escape them in sub primer by writing CCGTCAATTC\[AC\], or simply use the eq string comparison. As is, your second if is failing because the literal [ in your primer does not match the [AC] character class.

Upvotes: 2

Related Questions