if $locationOfRead == $location $chrHash->{$chr}->{$location}++ #### use POSIX qw(ceil floor); use strict; my $binSize = $ARGV[0]; # size of each bin (default 1000) my $readSize = $ARGV[1]; my @chromosomes = ("chr1","chr2","chr3"); while() {} # parse through file once to get number of lines my $maxCount = $.; # max count => all reads from the same locus => number of lines in SAM file my @counts = (0..$maxCount); my @countHash; map { $countHash{$_} = "" } @counts; # ??? my %chrHash; map { $chrHash{$_} = { %countHash } } @chromosomes; # old version: my @bins = (0..(250000000/$binSize); # chr1 ~ 250 mio bases my @binHash; my %chrHash; map { $binHash{$_} = 0 } @bins; # assume that all chromosomes are equal size and create a hash of hashes for counting map { $chrHash{$_} = { %binHash } } @chromosomes; # Read in SAM file and count reads per bin while() { chomp(); my @line = split(/\t/,$_); if ($line[2] ~~ @chromosomes) { my $location = floor(ceil((2*$line[3]+$readSize)/2)/1000); $chrHash{$line[2]}{$location}++; } }