Cristian Velandia Cristian Velandia - 1 month ago 9
Perl Question

While loop does not read a file generated from code on Perl

I was trying to generate an output file on the fly with my code in Perl:

#!/usr/bin/perl
use strict;
use Data::Dumper;
use warnings;
use Bio::Seq;
use Bio::SeqIO;
use Bio::DB::Fasta;
open my $OUT, "> ./temporal.gff";
my $seq = $db->get_Seq_by_id($key);
my $length = $seq->length;
my $all = $coord{$key};
foreach my $keys (@$all[0]){
@sorted = sort { $a->[0] <=> $b->[0] } @$all;
$num = scalar @sorted;
my @new = &infer_gaps(\@sorted, $num, $length);
foreach my $ln (@new){
my @tmp = split /\s{2, }|\t/, $ln;
print $OUT "$key\tFillSequencer\tinference\t$tmp[0]\t$tmp[1]\t\.\t$tmp[2]\t\.\t$specie $key:$tmp[0]\-$tmp[1]\n";
}
#print Dumper \@new;
}
}


But, When I want to use this generated file, as the usual way with a while loop, it does not read this file:

open my $ABC, "<./temporal.gff" or die $!;
my $mRNA_seq;
while (my $l = <$ABC>){
chomp $l;
my @arr = split /\t|\s+/, $l;
#my $coor = $arr[0] . " " . $arr[3] . " " . $arr[4];
$frame = $arr[6];
my $final_seq = $db->seq( $arr[0], $arr[3], $arr[4] );
my $output_nucleotide = Bio::Seq->new(
-seq => $final_seq,
-id => $mRNA_name,
-display_id => $mRNA_name,
-alphabet => 'dna',
);
if ($frame eq '-') {
$output_nucleotide = $output_nucleotide->revcom();
}
$outfile_coor->write_seq($output_nucleotide);
}

close $ABC;


Do you have some ideas? When I ran my code with -d flag it allowed me to confirm that the code skips the last while loop...I do not know if there is a problem with the definition of the FILE HANDLE variable or any other problem that does not generates warnings. Thanks in advance!

Answer

I think you need to close the file before you start reading it.

Alternatively you can flush the output, to make sure that the file is completely written before you start reading it.

sub flush {
   my $h = select($_[0]); my $af=$|; $|=1; $|=$af; select($h);
}

# Flush immediately without turning auto-flush on:
flush($OUT);

See this answer for examples of how to use flush:

http://www.perlmonks.org/bare/?node_id=489962