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

Hello y'all,

I cant seem to see why this simple looping system wont play properly.
I want it to loop through an array full of DNA sequences. For each sequence i want another array full of enzyme names to "cut" the sequence. The outer loop then should go to the next element in the array to get the second sequence.... but it doesnt :(

Can people see why..... i cant. Boo hoo.

############ outer loop################### foreach my $fileSeq(@sequences) # loop through all the sequences { $counter++; $seqobj = ''; print("<BR>$counter<BR>"); $seqobj = Bio::PrimarySeq->new ( -seq => $fileSeq, -alphabet => 'd +na'); # create an object of the sequence print("<BR>Sequence object created....all going well so far...<BR> +"); ############ inner loop ################### foreach $element(@enzymes) # look through all the enzymes for eac +h sequence { $newObject = ''; $newObject = new Bio::Tools::RestrictionEnzyme(-NAME =>$element) +; # create an object of the enzyme print("<BR>Enzyme object created for enzyme : $element<BR>"); $cutSeq = $newObject->site(); + # show the cut site for the enzyme $anotate = $newObject->annotate_seq($seqobj); + # annotate the sequence at cut site print("<BR>Sequence virtually labelled....<BR>"); @cut = $newObject->cut_seq($seqobj); +# virtualy cut the sequnce using the enzyme print("<BR>Sequence virtually cut....<BR>"); print("<TABLE BORDER=2>"); # + produce output print("<TR><TD><B>Sequence number $counter</B></TR></TD>"); print("<TR><TD>"); print("The cleavage site is $cutSeq for enzyme $element denoted +by \" ^ \"<BR></TD></TR>"); print("<TR><TD>The annotated sequence is given below along with +the digested sequence<BR>"); print("<B>Annotated:</B>$anotate<BR>"); print("<B>Digested:</B>@cut</TD></TR>"); print("</TABLE><BR><BR>"); } ############ END inner loop ################### print("Loop finished... returning to the start<BR>"); }############ END outer loop ###################

20050516 Edit by castaway: Changed title from 'Why it not work :('

Replies are listed 'Best First'.
Re: Looping failure
by ikegami (Patriarch) on May 15, 2005 at 13:53 UTC
    Be sure to use $| = 1 before your prints while debugging. It may in fact be existing the loop without you noticing because of output buffering.
Re: Looping failure
by sh1tn (Priest) on May 15, 2005 at 14:06 UTC
    And also:
    use CGI::Carp qw(fatalsToBrowser);


      Doesn't "use CGI::Carp qw(fatalsToBrowser);" just work for when a fatal eror has occurred, and so in this case would not report anything.
        You can print from the CLI and then, if it works as expected, test through CGI.


Re: Looping failure
by nedals (Deacon) on May 15, 2005 at 18:50 UTC

    Just a few thoughts...
    1. Use strict
    2. Use HTML::Template. It really cleans up your code
    3. Not sure what you are trying to do here. (See ysth post)
    @cut = $newObject->cut_seq($seqobj);

    my @tables = (); my $counter = 0; foreach my $fileSeq (@sequences) { # loop through all the sequences my $seqobj = Bio::PrimarySeq->new ( -seq => $fileSeq, -alphabet => ' +dna'); # create an object of the sequence foreach my $element (@enzymes) { # look through all the enzymes for + each sequence my $newEnzyme = new Bio::Tools::RestrictionEnzyme(-NAME => $elemen +t); push @tables,{ COUNTER => $counter++, CUTSEQ => $newEnzyme->site(), ELEMENT => $element, ANNOTATE => $newEnzyme->annotate_seq($seqobj), CUT => $newEnzyme->cut_seq($seqobj), ##### ???? ###### }; } } .. and in .tmpl file <TMPL_LOOP NAME=TABLES> <TABLE BORDER=2> <TR><TD><B>Sequence number <TMPL_VAR NAME=COUNTER></B></TD></TR> <!-- +FIX </tr></td> here --> <TR><TD>The cleavage site is <TMPL_VAR NAME=CUTSEQ> for enzyme <TMPL_V +AR NAME=ELEMENT> denoted by " ^ "<BR></TD></TR> <TR><TD> The annotated sequence is given below along with the digested sequen +ce<BR> <B>Annotated:</B><TMPL_VAR NAME=ANNOTATE><BR> <B>Digested:</B<TMPL_VAR NAME=CUT> </TD></TR> </TABLE><BR><BR> </TMPL_LOOP>
Re: Looping failure
by sh1tn (Priest) on May 15, 2005 at 13:27 UTC
    Please, give more info which print statement is executed last.
    Thus the problem will become more visible.


    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Looping failure
by Animator (Hermit) on May 15, 2005 at 15:12 UTC
    Normally an inner-loop has something to do with the outer-loop, yet in your code I see a foreach $element(@enzymes), but the outer loop doesn't define it (it = @enzymes), nor assigns anything to it...

    Update, clarified what I ment with 'it'

      The inner loop, as i said before is to cut the seq with a list of enzymes, the outer loop is for the sequence list.

      $element has been declared at the start of my code as global so i can use in lots of loops.