#!/usr/bin/perl -w
use strict;

#person in charge of this script
#this should be the person currently in charge of scripts for the SGN project
my $script_maintainer='Teri Solow <tms45@cornell.edu>';


#@ARGV or print "No input parameters, proceeding with default.\n";

my @arg_pairs = split (/\-/, (join ' ', @ARGV));

my %args=();

foreach (@arg_pairs){

    $_ or next;
    my ($flag, $val)=split /\s+/;
    $args{$flag}=$val;
}

my $in_file=$args{'i'};
my $bin_size=$args{'b'};
my $out_file=$args{'o'};
my $scaleto_size=$args{'s'};

$in_file or die "No input file specified, please use the -i option\n";

#defaults
$bin_size ||= 0.01;
$scaleto_size ||= 10000;
$out_file ||= "${in_file}.scaled.binned";

#main body of script
#####################

open FILEIN, $in_file;

my $nr_elements;
my %bins=();
while (<FILEIN>){
    chomp;
    /^\s*$/ and next;
    my $pct= int(($_) / $bin_size);
    $bins{$pct}++;
    $nr_elements++;
}
close FILEIN;

#compute the scaling factor
my $scaling_factor=$scaleto_size/$nr_elements;

print "Binned $nr_elements elements, scaling each value by $scaling_factor\n";

open FILEOUT, ">$out_file";
foreach (sort keys %bins){

#scale
    my $scaled_count=$bins{$_} * $scaling_factor;
    print FILEOUT "$_\t$scaled_count\n";
}
close FILEOUT;
