#!/usr/bin/perl

open(DIST,"common_lengths");


while (<DIST>) {
 #   chop();
  @line=split(/[\s]/);
  $domlength{$line[0]}=$line[1];
}
close(DIST);
# for  PD-score use $line[3], por TM-score use $line[2]

$maxl = 759;
open(LIST,$ARGV[0]);

while (<LIST>) {
  @line=split(/\s+/);
  $len1 = $domlength{$line[0]};
  $len2 = $domlength{$line[1]};
  if ($len1 < $len2) {
     $minl = $len1;
  }
  else {
     $minl = $len2;
  }
  $counter[$minl]++;
  $scoresum[$minl] += $line[2];
}

close(LIST);

for ($i=0;$i<=$maxl;$i++) {
   if ($counter[$i] > 0) {
     $scoresum[$i] /= $counter[$i];
   }
}


open(LIST,$ARGV[0]);

while (<LIST>) {
  @line=split(/\s+/);
  $len1 = $domlength{$line[0]};
  $len2 = $domlength{$line[1]};
  if ($len1 < $len2) {
     $minl = $len1;
  }
  else {
     $minl = $len2;
  }
  $desvscore[$minl] += ($scoresum[$minl]-$line[2])*($scoresum[$minl]-$line[2]);
}

close(LIST);

for ($i=0;$i<=$maxl;$i++) {
   if ($counter[$i] > 0) {
     $desvscore[$i] /= $counter[$i];
   }
}

for ($i=0;$i<=$maxl;$i++) {
  if ($counter[$i] > 0) {
   print "$i $counter[$i] $scoresum[$i] $desvscore[$i]\n";
  }
}


