#!/usr/bin/perl -w
my $numrow=4;
if (scalar(@ARGV)==1) {
  $numrow=$ARGV[0];
}
open(INFILE, "/usr/data/arghmm/mt269/unrelated54.names1") or die;
my @inds=<INFILE>;
close(INFILE);
chomp(@inds);
open(INFILE, "cut -f 1,2 /usr/projects/arghmm/mt269/cg_ancestry.txt |") or die;
my %pops=();
my %uniqpops=();
my %shortdesc=();
my $found=0;
while (<INFILE>) {
  my ($tmpind, $tmppop) = split();
  chomp($tmppop);
  foreach my $ind (@inds) {
    if ($tmpind eq $ind) {
      $pops{$ind}=$tmppop;
      $uniqpops{$tmppop}=1;
      $found++;
    }
  }
}
close(INFILE);
die if ($found != scalar(@inds));

open(INFILE, "pop_descriptions.txt") or die;
while (<INFILE>) {
  my ($tmppop, $desc)=split(/: /);
  chomp($desc);
  die if (!exists($uniqpops{$tmppop}));
  $uniqpops{$tmppop} =$desc;
}
close(INFILE); 

open(INFILE, "short_pop_descriptions.txt") or die;
while (<INFILE>) {
  my ($tmppop, $desc) = split(/: /);
  chomp($desc);
  die if (!exists($uniqpops{$tmppop}));
  $shortdesc{$tmppop}=$desc;
}
close(INFILE);

print "<table><tr>\n";
my $counter=0;
my $indcounter=0;
foreach my $pop (sort(keys %uniqpops)) {
  if ($counter%$numrow==0) {print "<td valign=\"top\">\n";}
  print "<label><input type=\"checkbox\" name=\"pop\" id=\"$pop\" onclick=\"select$pop()\" checked=\"checked\"><span title=\"$uniqpops{$pop}\"><i>$pop: $shortdesc{$pop}</i></span></label><br>\n";
  print "<div style=\"margin-left: 1.5em;\">\n";
  $counter++;
  foreach my $ind (@inds) {
    if ($pops{$ind} eq $pop) {
      print "<label><input type=\"checkbox\" name=\"ind[]\" value=\"$ind\" id=\"$ind\" onclick=\"selectInd(this.checked)\" checked=\"checked\">$ind</label><br>\n";
      $indcounter++;
    }
  }
  print "</div>\n";
  if ($counter%$numrow==0) {print "</td>\n";}
}
print "</tr></table>\n";

print "<script>\n";

print "function selectInd(val) {\n";
print "if (val) {\n";
print "document.getElementById(\"clear\").checked=false\n";
print "} else {\n";
print "document.getElementById(\"selectAll\").checked=false\n";
print"}}\n";


print "function selectAllInds() {\n";
foreach my $ind (@inds) {
  print "document.getElementById(\"$ind\").checked=\"checked\"\n";
}
foreach my $pop (keys %uniqpops) {
  print "document.getElementById(\"$pop\").checked=\"checked\"\n";
}
print "}\n";

print "function clearAllInds() {\n";
foreach my $ind (@inds) {
  print "document.getElementById(\"$ind\").checked=false\n";
}
foreach my $pop (keys %uniqpops) {
  print "document.getElementById(\"$pop\").checked=false\n";
}
print "}\n";

foreach my $pop (keys %uniqpops) {
print "function select$pop() {\n";
print "if (document.getElementById(\"$pop\").checked) {\n";
foreach my $ind (@inds) {
  if ($pops{$ind} eq $pop) {
     print "document.getElementById(\"$ind\").checked=\"checked\"\n";
  }
}
print "document.getElementById(\"clear\").checked=false\n";
print "} else {\n";
foreach my $ind (@inds) {
 if ($pops{$ind} eq $pop) {
   print "document.getElementById(\"$ind\").checked=false\n";
 }
}
print "document.getElementById(\"selectAll\").checked=false\n";
print "}\n";
print "}\n";
}

print "</script>\n";
   
