Nandita Mullapudi wrote: > > Hi all, Hello,
> am using the following script to parse a long list of files. funnily > enough, it works fine when i try a couple sample files, but when i'm > using a long list of files, it comes up with this error: > > readline() on closed filehandle FILE at hashing22.pl line 29, <FILE> line > 13 (#1) > (W closed) The filehandle you're reading from got itself closed > sometime > before now. Check your logic flow. > > i dont know what could be causing this- any ideas? > > here's the script: > #!/usr/bin/perl-w > # takes a file where the first line specifies query length, puts in ahash > # the seqid and numbers listed, adds up the numbers, divides by qlen > # and give len coverage. use strict; > use diagnostics; > > $out_dir = "post_parsing_len_cov_files"; > mkdir $out_dir, 0777 unless (-e $out_dir); > $| = 1; > > $files= "./listofnames2"; > > open (FH,"$files") or die "cannot create list :$!"; The quotes and the parenthesis are not required. You are not trying to "create" the list, you are trying to read a list that has already been created. open FH, $files or die "cannot open $files:$!"; > while (<FH>) { > > push (@list, $_); > } You need to chomp the lines from the file to get the correct file names. chomp( my @list = <FH> ); > foreach (@list) { > $filename=$_; foreach my $filename ( @list ) { > open (FILE, $filename); You should ALWAYS verify that the file opened correctly. open FILE, $filename or die "Cannot open $filename: $!"; > open (HASH_OUT, ">$out_dir/$filename.stats"); open HASH_OUT, ">$out_dir/$filename.stats" or die "Cannot open $out_dir/$filename.stats: $!"; > my %hash; > while (<FILE>) > { > if (/^Query : (.*)\S/m){ You are reading a line at a time from <FILE> so you don't need the /m option. Are you sure you don't want the last non-whitespace included in the capturing parenthesis? > $qname = "$1"; The quotes are not needed. > print HASH_OUT "Query : $qname\n"; > } > else > { > if (/^qlen = (\d+)/m){ The /m option is not needed. > $qlen = "$1"; The quotes are not needed. > print HASH_OUT "querylen = $qlen\n"; > } > else > {@line = split (/\n/); > foreach $line (@line){ You are processing the file a line at a time so there is no need for the split() and foreach loop. > ($key,$value) = split (/:/); > $hash {$key} = $value; > #print " $value"; > @nums = split (" ",$value); > #print "$key: @nums"; > > $tot = &total(@nums); > $cov = $tot/$qlen * 100 ; > if ($cov >= 50){ > print HASH_OUT "$key : $cov\n"; > }}} > }}} > > close FILE; > > sub total { > my $sum; > foreach (@_){ > $sum += $_; > } > $sum; > } > ------------------------------ > a typical file to be read by this script is would look like this: > > Query : MAL13P1.227_ubiquitin-conjugatin > qlen = 278 > > >CpType1H_3652:111 81 > >CpIowa_1214.266:111 121 > >CpGSS_AQ842837:98 > >CpType1H_3631:118 > >CpIowa_1214.272:118 74 26 > >CpIowa_1214.275:77 90 141 17 10 17 > >CpEST_AA390251:117 > >CpEST_AA390240:102 > >CpType1H_3267:77 143 > >CpGSS_AQ842682:98 > > ---------------------------------- > SOME files also don't have any lines in them after the first two. > any suggestions on improving my long winded coding skills (or lack > thereof) are most welcome. #!/usr/bin/perl -w # takes a file where the first line specifies query length, puts in ahash # the seqid and numbers listed, adds up the numbers, divides by qlen # and give len coverage. use strict; use diagnostics; my $out_dir = 'post_parsing_len_cov_files'; mkdir $out_dir, 0777 unless -e $out_dir; my $files = 'listofnames2'; open FH, $files or die "Cannot open $files: $!"; chomp( @ARGV = <FH> ); my $qlen; while ( <> ) { if ( $. == 1 ) { open HASH_OUT, '>', "$out_dir/$ARGV.stats" or die "Cannot open $out_dir/$ARGV.stats: $!"; } next unless /\S/; if ( /^Query : (.*\S)/ ) { print HASH_OUT "Query : $1\n"; } elsif ( /^qlen = (\d+)/ ) { $qlen = $1; print HASH_OUT "querylen = $qlen\n"; } elsif ( /^(.+):(.+)/ ) { my $cov = total( split ' ', $2 ) / $qlen * 100; if ( $cov >= 50 ) { print HASH_OUT "$1 : $cov\n"; } } } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]