Sampler Data Binning

Example script of binning samtest data

This script takes two columns of numbers (ASCII format) and bins it, returning the average of each bin. Usage:

bin_data.pl [input file] [bin size] > [output_file]


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

my $in = $ARGV[0];
open IN, $in or die "can't open $in: $!";

my $i;
my $j;
my $sum1 = 0;
my $sum2 = 0;
my @lines = <IN>;
my $n = $#lines;
my $max = int($n/$ARGV[1]);
my ($a,$b);
for ($j =0; $j < $max;$j++) {
    for ($i = ($j*$ARGV[1]); $i < ($j*$ARGV[1])+$ARGV[1]; $i++) {
	($a,$b)=split " ",$lines[$i];
	$sum1+=$a;
	$sum2+=$b;
    }
    print $sum1/$ARGV[1], " ", $sum2/$ARGV[1], "\n";
    $sum1 = 0;
    $sum2=0;
}

close (IN);