use strict;
use warnings;

my $AlleleCountFile = $ARGV[0];
my $AlleleLociFile = $ARGV[1];
my $Matrix = $ARGV[2];
my $EnvFile = $ARGV[3];
my $NumPops = $ARGV[4];
my $NumEnvs = $ARGV[5];
my $K = 10000;

my $out = "XtX_out.".$EnvFile;
system "rm $out\n";

open IN, $AlleleCountFile;
my $lastLine;
while(<IN>){
	chomp;
	my $line = $_;
	if (0 == $. % 2) {
		open TMP, ">TMP";
    		print TMP "$lastLine\n";
		print TMP "$line\n";
		system ("../bin/my_bayenv2/bayenv2 -i TMP -m $Matrix -e $EnvFile -n $NumPops -p $NumEnvs -k $K -t -X -r 13258\n");	
	}else{
		$lastLine = $line;
	}
}
system "sed 's/TMP/$AlleleCountFile/' $out > tmp.out\n";
system "paste $AlleleLociFile tmp.out > $out";

