#!/usr/bin/perl -w

# This script takes the output of samples_stats distributed with ms,
# and makes a tab delimited statistics files.

# For example, the following file is the input to this program.
# ms 30 4 -t 3.0 | sample_stats 
# pi:	1.88	ss:	9	D:	-0.52	thetaH:	4.80	H:	-2.91
# pi:	2.13	ss:	9	D:	-0.19	thetaH:	0.83	H:	1.29
#  :
#  :

# It will output
# 1.88	9	-0.52	4.80	-2.91
# 2.13	9	-0.19	0.83	1.29


while(<>) {            # read in each line
    chomp;             # remove new line character
    s/^\s+//;          # remove the leading spaces
    s/\s+$//;          # remove the trailing spaces
    next if (/^\s*$/); # ignore the empty line
    if (/pi:\s+([^\s]+)\s+ss:\s+([^\s]+)\s+D:\s+([^\s]+)\s+thetaH:\s+([^\s]+)\s+H:\s+([^\s]+)/) {
	my $pi = $1;
	my $ss = $2;
	my $D = $3;
	my $thetaH = $4;
	my $H = $5;

	print join("\t", ($pi, $ss, $D, $thetaH, $H));
	print "\n";
    } else {
	warn "WARN: This line does not follow the normal output pattern, " .
	    "Ignored...:\n$_";
    }
}

exit;
