#!/usr/bin/env perl
#perl extractCol.pl tests/1.txt tests/2.txt tests/3.txt tests/4.txt tests/5.txt tests/6.txt tests/7.txt tests/8.txt tests/9.txt tests/10.txt tests/11.txt
#Auteur : Ibouniyamine Nabihoudine
#Date : 16/09/2013


use strict;
use warnings;
use File::Basename;

=pod
my $countfile1 = $ARGV[0];
my $countfile2 = $ARGV[1];
my $countfile3 = $ARGV[2];
my $countfile4 = $ARGV[3];
my $countfile5 = $ARGV[4];
my $countfile6 = $ARGV[5];
my $countfile7 = $ARGV[6];
my $countfile8 = $ARGV[7];
my $countfile9 = $ARGV[8];
my $countfile10 = $ARGV[9];
my $countfile11 = $ARGV[10];


my $path= dirname($countfile1);


my $countmatrix1= "$path/tempFile1";
my $countmatrix2= "$path/tempFile2";
my $countmatrix3= "$path/tempFile3";
my $countmatrix4= "$path/tempFile4";
my $countmatrix5= "$path/tempFile5";
my $countmatrix6= "$path/tempFile6";
my $countmatrix7= "$path/tempFile7"; 
my $countmatrix8= "$path/tempFile8";
my $countmatrix9= "$path/tempFile9";
my $countmatrix10= "$path/tempFile10";
my $countmatrixFinal= "$path/finalFile.txt";
=cut

my %hash = {};

foreach my $file (@ARGV)
{
    open(TMP, $file) or die"$!";
    while( my $line =<TMP> )
    {
	chomp $line;
        my @F = split(/\t/,$line);
	if ! exists($hash{$F[0]}) {
		$hash{$F[0]} = [];
	}
	push ( @$hash{$F[0]}, $F[2]);
     }
    close TMP;
}
open( FINAL , '>', "FinalCount.txt") or die "$!";
foreach my $k in keys(%hash) {
	print FINAL join("\t", $k, @$hash{$k});
}
close FINAL;


#`awk '{str1=\$1; str2=\$3; print str1 "\t" str2 > "$countmatrix1"}' $countfile1`;
#`awk '{str1=\$1;str2=\$2;get line < "$countfile2"; print str1 "\t" str2 "\t" \$3 > "$countmatrix2"}' $countmatrix1`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;get line < "$countfile3"; print str1 "\t" str2 "\t" str3 "\t"\$3 > "$countmatrix3"}' $countmatrix2`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;get line < "$countfile4"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" \$3 > "$countmatrix4"}' $countmatrix3`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;str5=\$5;get line < "$countfile5"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" str5 "\t" \$3 > "$countmatrix5"}' $countmatrix4`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;str5=\$5;str6=\$6;get line < "$countfile6"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" str5 "\t" str6 "\t" \$3 > "$countmatrix6"}' $countmatrix5`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;str5=\$5;str6=\$6;str7=\$7;get line < "$countfile7"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" str5 "\t" str6 "\t" str7 "\t" \$3 > "$countmatrix7"}' $countmatrix6`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;str5=\$5;str6=\$6;str7=\$7;str8=\$8;get line < "$countfile8"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" str5 "\t" str6 "\t" str7 "\t" str8 "\t" \$3 > "$countmatrix8"}' $countmatrix7`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;str5=\$5;str6=\$6;str7=\$7;str8=\$8;str9=\$9;get line < "$countfile9"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" str5 "\t" str6 "\t" str7 "\t" str8 "\t" str9 "\t" \$3 > "$countmatrix9"}' $countmatrix8`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;str5=\$5;str6=\$6;str7=\$7;str8=\$8;str9=\$9;str10=\$10;get line < "$countfile10"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" str5 "\t" str6 "\t" str7 "\t" str8 "\t" str9 "\t" str10 "\t" \$3 > "$countmatrix10"}' $countmatrix9`;
#`awk '{str1=\$1;str2=\$2;str3=\$3;str4=\$4;str5=\$5;str6=\$6;str7=\$7;str8=\$8;str9=\$9;str10=\$10;str11=\$11;get line < "$countfile11"; print str1 "\t" str2 "\t" str3 "\t" str4 "\t" str5 "\t" str6 "\t" str7 "\t" str8 "\t" str9 "\t" str10 "\t" str11 "\t" \$3 > "$countmatrixFinal"}' $countmatrix10`;
#`rm $path/tempFile*`;


