Advertisement
Guest User

a

a guest
Oct 31st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.08 KB | None | 0 0
  1. use warnings;
  2. use strict;
  3.  
  4.  
  5. my $input = $ARGV[0];
  6.  
  7. my %dict;
  8. open(INPUT, "$input");
  9. my $headers = <INPUT>;
  10. while (my $line = <INPUT>)
  11. {
  12.     my @currentLine = split(/\t/, $line);
  13.     next if $#currentLine < 0;
  14.     next if $currentLine[4] == 0;
  15.     my $_key = "$currentLine[0]\t$currentLine[1]";
  16.     $dict{$_key}{$currentLine[$#currentLine]} = $currentLine[4];
  17.  
  18. }
  19. close(INPUT);
  20.  
  21.  
  22.  
  23. my %return_dict;
  24. foreach my $key (keys %dict)
  25. {
  26.     my %_hash = %{$dict{$key}};
  27.     my @key_list = keys %_hash;
  28.     my $max_key = pop @key_list;
  29.     foreach (@key_list)
  30.     {
  31.         $max_key = $_ if $_hash{$_} > $_hash{$max_key};
  32.     }
  33.     my $c = calc($_hash{$max_key}, \%_hash);
  34.     print STDOUT "$key\t$c\n";
  35. }
  36.  
  37. sub log2
  38. {
  39.     my $num1 = shift;
  40.     return log($num1)/log(2);
  41. }
  42.  
  43.  
  44. sub calc
  45. {
  46.     my $max = shift;
  47.     my $hashRef = shift;
  48.     my %hash = %{$hashRef};
  49.     my @keylist = keys %hash;
  50.     my $N = $#keylist;
  51.     if ($N == 0)
  52.     {
  53.         return 0
  54.     }
  55.  
  56.     my $TSMI = 0;
  57.     for my $i (0..$#keylist)
  58.     {
  59.         #print "$hash{$keylist[$i]}\t$max\n";
  60.         my $_sum = (log2($hash{$keylist[$i]})/log2($max) - 1)/$N;
  61.         $TSMI += $_sum;
  62.     }
  63.     return $TSMI;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement