#!/usr/bin/perl

use strict;
my $h;
my $se;
my $n;
my @type = ( "", "NOPOE", "POE" );
#my @hsq = glob("/Net/sparse/data/scratch/HSMICE/POE/GCTA.OUT/*.hsq");
my @hsq = glob("*.hsq");
foreach my $f ( @hsq ) {
  if ( $f =~ /^(.+)\.hsq/ ) {
    my $p = $1;
    open(FILE, $f);
    while(<FILE>) {
      chomp;
      if ( /^V\(G(\d)\)\/Vp\s+(\S+)\s+(\S+)/ ) {
	$h->{$p}{$type[$1]} = $2;
	$se->{$p}{$type[$1]} = $3;
      }
      if ( /^n\s+(\S+)/ ) {
	$n->{$p} = $1;
      }
    }
  }
}
print "Trait\tH2.POE\tH2.NOPOE\tSE.POE\tSE.NOPOE\tN\n";

foreach my $p ( sort keys %$h ) {
  my ($pp) = $p =~/([^\/]+)$/; 
  print join( "\t", $pp, $h->{$p}{"POE"} , $h->{$p}{"NOPOE"} , $se->{$p}{"POE"} , $se->{$p}{"NOPOE"} , $n->{$p} ) . "\n";
}
