#!perl -w
use strict;
use warnings;

# skip first line
$_=<>;

my $h;

while (<>)
{
  chomp;
  @_ = split/\t/;

  # ploticus does not support much longer labels
  $_[1] = substr $_[1], 0, 40;
  my $g;

  if    ($_[3] == 0) { $g = 4 }
  elsif ($_[4] == 0) { $g = 1 }
  elsif ($_[4] < 11) { $g = 2 }
  else               { $g = 3 }

  $h->{$_[1]}->{$_[0]} = $g;
}


sub weight
{
  my $hash = shift;
  my %resu; $resu{$_} = 0 for 1..4;
  $resu{$hash->{$_}}++ for keys %$hash;
  my $c = scalar(keys %$hash);
  my @x = map { 100 * $resu{$_} / $c } 1..4;
  sprintf "%04d-%04d-%04d-%04d-%04d", @x, $c;
}

my @order = sort {
  weight($h->{$b}) cmp weight($h->{$a}) || $a cmp $b
} keys %$h;

foreach my $name (@order)
{
  foreach my $date (sort { $a cmp $b } keys %{$h->{$name}})
  {
    printf "$date\t$name\t%d\n", $h->{$name}->{$date};
  }

}
