Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
How to get the mean?

Posted by revtopo (revtopo), 11 December 2007
hi all,


I would like to get the counts that are greater than the mean. but since it is within the loop i couldnot get what i needed. Instead all the counts.
The file would be some thing like this

1            1             200            3
1             50           250            12
1              100        300             16
.....

The files are tab delimited. Counts represents the fourht coulmn.
Is there any other way? i have attached my program along.
#! usr/bin/perl -w
use strict;
use Getopt::Long;

my $file;
my $maximum_gap =150;
my %pro_locus;
GetOptions('f=s'=>\$file,
        'g=i' => \$maximum_gap) or exit;
warn "\n Can't proceed!!!No file name given\n" if !$file;

open (FILE ,"$file") or die "Cannot open the file\n";
my @hit_clusters = <FILE>;

my $line_count = `wc -l < $file`;
my $total_count = 0;

print " the number of line:$line_count\n";
close FILE;
#parsing tthe file
foreach my $file_line(@hit_clusters){

   next if $file_line =~m/^\s*$/;
   next if $file_line =~m/^(Chromosome.+)$/;
   #print "$file_line\n";
   if ($file_line =~m/^(.+?)\t(\d+?)\t(\d+?)\t(\d+?)\b/){
     
     my ($chro_id, $hit_start, $hit_stop, $count)= ($1, $2, $3, $4);
     $total_count+=$count;
           my $mean = $total_count/$line_count;
     #print "the mean is $mean \n";
     if ($count >= $mean){
         print"$count\n";
     }

       
   }                                      

}
   

Thanks,

Posted by admin (Graham Ellis), 11 December 2007
Don't think you need code that's as long as that ... try:

Code:
@info = <DATA>;

$sum += (split)[-1] foreach (@info);
$mean =  $sum / @info;
print (grep ((split)[-1] > $mean,@info));

__END__
1       1       200     3
1       50      250     12
1       100     300     16




Posted by revtopo (revtopo), 12 December 2007
Could you please explain your code. Since i get only the list of file contents rather than mean?

Posted by admin (Graham Ellis), 12 December 2007
Oh - I misunderstood - I thought you wanted all lines from the file who's 4th value was in excedd of the mean of the 4th values.   If you pint $mean as well, you'll see the mean as well.




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho