package Az; use strict; # percent takes a reference on hash argument sub percent { my ($hash, $pseudo) = @_; if (!defined $pseudo) {$pseudo = 0.01;} my $sum = 0; my %p = (); %p = %$hash; my $ref = ref $hash; if ($ref eq 'HASH') { ($sum += $_) for values %p; if ($sum == 0) {die "the sum calculated in sub percent equal zero\n"} foreach my $key (keys %p) { $p{$key} += $pseudo; # add PSEUDO } $sum += $pseudo*(keys %p); foreach my $key (keys %p) { $p{$key} = $p{$key}/$sum; } } else { die "the reference you send do not point to hash\n"; } return \%p; } # reverse compliment for DNA sequence (blanks allowed) sub revcomp { my ($seq) = @_; $seq =~ tr[ACGT] [TGCA]; return reverse($seq); } # date and in the format: 2009-02-01 21:18:05 sub dateNtime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); printf "%4d-%02d-%02d %02d:%02d:%02d\n", $year+1900,$mon+1,$mday,$hour,$min,$sec; } ###################################################### ######## PRINTING ############################## ###################################################### # print hash in column: key and value per one line sub printHashColumn { my ($hash, $sortBy, $max) = @_; my $out = ""; if (!defined $max) {$max = 0} my $count = 1; if ($sortBy eq "key") { foreach my $key (sort keys %$hash) { if ($max != 0 and $count > $max) {last} $out .= "$key\t"; $out .= $hash->{$key}; $out .= "\n"; $count++; } } elsif ($sortBy eq "value") { foreach my $key (sort {$hash->{$b} <=> $hash->{$a}} keys %$hash) { if ($max != 0 and $count > $max) {last} $out .= "$key\t"; $out .= $hash->{$key}; $out .= "\n"; $count++; } } else { die "error in Az::printHashColumn - didn't specify how to sort column; use: key or value\n" } return \$out; } # print double hash in line: first keys in one line; second line: second Key and # values, corresponded for those two keys. All tab delimited sub printDoubleHashLine { my ($hash) = @_; my $out = ""; my @secondKeys = (); foreach my $firstKey (sort keys %$hash) { $out .= "\t$firstKey"; if (@secondKeys) {next} else { foreach my $secondKey (sort keys %{$hash->{$firstKey}}) { push @secondKeys, $secondKey; } } } $out .= "\n"; foreach my $secondKey (@secondKeys) { $out .= "$secondKey"; foreach my $firstKey (sort keys %{$hash}) { $out .= "\t$hash->{$firstKey}{$secondKey}"; } $out .= "\n"; } return \$out; } 1;