#!/usr/bin/env perl
# eats the interaction ss, but it's not perfectly reciprocal.  ie some PIs are listed in the 1st col
# and second col, but some are listed ONLY in the second col.
# so have to set up a PI-specific hash whereby the 2nd is checked against the 1st.
# 
my %PI;

while (<>) {
    if ($_ !~ /^#/) {
      chomp;
      my $N = my @A = split(/\t/, $_);
      my $fpi = $A[0];
      if (!defined $PI{$A[0]}) {$PI{$A[0]} = 0; } # init the PI hash
      if (!defined $PI{$A[1]}) {$PI{$A[1]} = 0; } # ditto
      my $fpiid = mkID($A[0]);
      my $spiid = mkID($A[1]);
      for (my $i=2; $i<5; $i++) {
        if ($A[$i] == 1) {
          #print "A[$i] = [$A[$i]]\n";
          #print "$_\n";
          #sleep 1;
          $PI{$A[0]}++; $PI{$A[1]}++;
          #print "$A[0] : $PI{$A[0]} $A[1]: $PI{$A[1]} \n\n";
          #sleep 1;
        }
      }
    }
}

# dump the sorted keys and values for all the PI hashes.
foreach my $key (sort keys %PI) {
    my $id = mkID($key);
    print "$key\t$id\t$PI{$key}\n";
}

sub mkID($) {
  my $FN = shift;
  $Nn = @An = split(/,/, $FN);
  $LN = substr($An[0],0,3);
  return "${LN}${An[1]}";;
}
