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

Hi Monks,

I've written a short script which loops through a file, using some information from a specific line to extract a section of the file using an external program. This section (called $mafBlock) is then used in another external program, and it is the output of this program (called m2.gff) that I am interested in. However, each time the loop proceeds the output file m2.gff is overwritten, so I thought one solution would be to try and open this file within the loop, and then print the lines I require to another output file (m2_most-cons.gff). If I do this I get a syntax error for opening this second filehandle, I've tried a few different ways of writing this but none seem to work. My code is:

use warnings; use strict; my $maf = "input.maf"; open (IN1, "input.maf") or die; open (OUT1, ">m2.maf") or die; open (OUT2, ">m2_most-cons.gff") or die; while (my $line1 = <IN1>) { chomp $line1; if ($line1 =~ /^s\sficAlb2/) { my @columns = split(/\t/, $line1); my $start = $columns[2] + 1; my $end = $columns[2] + $columns[3]; my $chrom = $columns[1]; my $mafBlock = qx(maf_parse --start $start --end $end $maf); print OUT1 "$mafBlock1\n"; my $phastCons = qx(phastCons --target-coverage 0.3 --expected- +length 45 --rho 0.31 --most-conserved m2.gff --seqname $chrom --msa-f +ormat MAF m2.maf nonconserved-all-4d.mod); print "$phastCons\n" open (IN2, "m2.gff") or die; while (my $line2 = <IN2>) { if ($line2 =~ /^fic/) { print OUT2 "$line2\n"; } } } open (OUT1, ">m2.maf") or die; close IN2; } close IN1; close OUT1; close OUT2; exit;

Apologies if this is something completely wrong, I'm new to coding and this seemed the most intuitive way to collect the output required.

Many thanks in advance!

Replies are listed 'Best First'.
Re: Error opening file handle within while loop
by GotToBTru (Prior) on Feb 10, 2016 at 12:36 UTC

    Missing a semicolon in the print statement immediately prior to opening IN2.

    Why do you need to re-open OUT1?

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      Wow, that was obvious! Apologies for missing such a mistake, and thanks for your answer.

      OUT1 is re-opened just to clear it's contents, then it is re-filled with the next section of the file I need to analyse. I'm not sure this is the best way of doing this, but it seems to work.

      Now I've added that missing ;, I'm getting an error message: Use of uninitialized value $columnsA2 in addition (+) at phastCons_multiples_ids3.pl line 19

      I'm not sure why this is, it's declared within the first if statement?
        Also, apologies but ignore that it says $columnsA, just a discrepancy between the script I'm currently testing and the one I've posted here.
Re: Error opening file handle within while loop
by Corion (Patriarch) on Feb 10, 2016 at 12:31 UTC

    Where do you get the syntax error, and what does it say exactly?

      HI, thanks for your time. The syntax error was for the line "open (IN2, "m2.gff") or die;" but as was pointed out I was missing a ; on the preceding line. Now I get the error message: Use of uninitialized value $columnsA2 in addition (+) at phastCons_multiples_ids3.pl line 19

        I would then look at the values in @columnsA, and/or how @columnsA gets filled.

Re: Error opening file handle within while loop
by RichardK (Parson) on Feb 10, 2016 at 14:09 UTC

    This is always worth a read Basic debugging checklist as it contains lots of useful suggestions.

    BTW, I'm surprised that your code works, you write to m2.maf & then ask phastCons to read it, but you haven't closed the file. It may be better to close it first so that any buffering gets flushed, and you'll be sure that the program gets to see all the data.

      Hi, thanks for the advice, it does seem to work as expected but I hadn't considered this at all - have now added!
Re: Error opening file handle within while loop
by GotToBTru (Prior) on Feb 10, 2016 at 13:47 UTC

    I cannot recommend highly enough the Perl debugger as a tool for troubleshooting. You can verify that variables contain expected values, that program flow follows the path you expect. Most commonly, I run it interactively to test syntax before using it.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Error opening file handle within while loop
by neilwatson (Priest) on Feb 10, 2016 at 17:57 UTC