#! /usr/bin/perl

#This program runs rapsearch and hmmer on the given input files and processes their outputs to provide final results

`~/Software/hmmer-3.1b1-linux-intel-x86_64/binaries/hmmscan --acc --tblout $ARGV[0].hout -E 21 complete.hmm $ARGV[0] & ~/Software/RAPSearch2.22_64bits/bin/rapsearch -q $ARGV[0] -d complete.db -o $ARGV[0] -z 6 -e -6 -v 1`;
@alcohol = ("1.10.3.2" , "1.1.1.10" , "1.11.1.13" , "1.11.1.14" , "1.1.1.12" , "1.1.1.157" , "1.1.1.1" , "1.1.1.202" , "1.1.1.21" , "1.1.1.244" , "1.1.1.307" , "1.1.1.4" , "1.1.1.6" , "1.1.1.86" , "1.1.1.9" , "1.17.7.1" , "1.2.1.2" , "1.2.1.46" , "1.2.1.9" , "1.3.8.1" , "2.2.1.1" , "2.2.1.2" , "2.2.1.6" , "2.2.1.7" , "2.3.1.182" , "2.3.1.9" , "2.5.1.54" , "2.7.1.17" , "3.1.1.11" , "3.1.1.1" , "3.1.1.20" , "3.1.1.72" , "3.1.1.73" , "3.1.3.8" , "3.2.1.139" , "3.2.1.14" , "3.2.1.151" , "3.2.1.15" , "3.2.1.177" , "3.2.1.1" , "3.2.1.21" , "3.2.1.23" , "3.2.1.24" , "3.2.1.25" , "3.2.1.37" , "3.2.1.39" , "3.2.1.3" , "3.2.1.40" , "3.2.1.4" , "3.2.1.52" , "3.2.1.55" , "3.2.1.58" , "3.2.1.6" , "3.2.1.78" , "3.2.1.80" , "3.2.1.86" , "3.2.1.8" , "3.2.1.91" , "3.2.1.96" , "3.2.1.99" , "3.4.21.4" , "4.1.1.1" , "4.1.1.4" , "4.2.1.17" , "4.2.1.30" , "4.2.1.51" , "4.2.2.10" , "4.2.2.23" , "4.2.2.2" , "4.2.2.3" , "4.2.2.6" , "5.3.1.4" , "5.3.1.5" , "6.2.1.1");

@diesel = ("1.1.1.100" , "1.14.19.1" , "1.2.1.3" , "1.3.1.9" , "2.3.1.15" , "2.3.1.180" , "2.3.1.20" , "2.3.1.39" , "2.3.1.41" , "2.3.1.75" , "2.3.1.85" , "2.3.1.86" , "2.3.1.9" , "2.3.3.10" , "2.5.1.10" , "2.7.1.36" , "2.7.4.2" , "3.1.1.1" , "3.1.1.3" , "3.1.1.74" , "3.1.2.4" , "3.1.3.4" , "3.2.1.7" , "3.2.1.8" , "4.1.1.33" , "4.1.1.39" , "4.2.1.1" , "4.2.1.59" , "6.3.4.14" , "6.4.1.2");

@fuelcell = ("1.10.3.2","1.11.1.6","1.1.1.175","1.11.1.7","1.1.1.1","1.1.1.37","1.1.1.42","1.1.1.47","1.12.7.2","1.12.99.6","1.13.12.4","1.1.3.13","1.1.3.4","1.1.5.2","1.1.99.18","1.2.1.3","1.2.1.5","1.2.4.2","1.3.3.5","3.2.1.26","3.2.1.28","3.2.1.3","4.2.1.2","4.2.1.3","5.1.3.3","6.2.1.1","6.2.1.4");

@alternate = ("1.1.1.267" , "1.2.1.2" , "1.2.1.50" , "1.2.1.n2" , "1.3.1.83" , "2.2.1.7" , "2.3.1.9" , "2.3.3.10" , "2.5.1.10" , "2.5.1.1" , "2.7.1.36" , "2.7.4.2" , "3.2.1.1" , "3.2.1.4" , "3.4.11.1" , "3.4.21.62" , "4.1.1.33" , "4.1.99.5" , "4.2.1.1");

#opening the hmm output here
open(IF1,"$ARGV[0].hout")|| die "can not open the hmm output file";

my %hash1;
while(chomp($line=<IF1>)){

	if($line =~ /^#/){next;}
	
	$line =~ s/\s+/\t/g;
	@arr1 = split("\t",$line);#splitting the result line into @arr1
	$query = $arr1[2];

#check if the key already exists and skip to the next line if it does

	if (exists $hash1{$key}) {next;}
	$out = $arr1[0]."|||".$arr1[4]."|||".$arr1[5];
	$hash1{"$query"} = "$out";
}

#opening the rapsearch m8 output here
open(IF2,"$ARGV[0].m8")|| die "can not open the rapsearch file";

my %hash2;
while(chomp($line=<IF2>)){

	if($line =~ /^#/){next;}

	@arr2 = split("\t",$line);
	$query = $arr2[0];
	@name = split("\\|",$arr2[1]);
	$out = $name[2]."|||".$arr2[10]."|||".$arr2[11];
	$hash2{"$query"} = "$out";
}

open(OF,">$ARGV[0].csv")|| die "can not make the final output file";
print OF "Query\tHMM Hit\tHMM E-value\tHMM Score\tRAPSearch2 Hit\tRAPSearch2 E-value\tRAPSearch2 Score\tTags\n";

foreach $key (keys %hash1){
	if (exists $hash2{$key}){
		$printable = "$key\t$hash1{$key}\t$hash2{$key}";
		$printable =~ s/\|\|\|/\t/g;
                $a = $hash1{$key}; $b = $hash2{$key};
                @arr1 = split("\\|\\|",$a);
                @arr2 = split("\\|\\|",$b);
                if ($arr1[0] eq $arr2[0]){
			print OF "$printable\tHR-agree\n";
		}
		else{
			print OF "$printable\tHR-disagree\t";
			if($arr1[0] ~~ @alcohol){print OF "Alcohol ";}
			if($arr1[0] ~~ @diesel){print OF "Biodiesel ";}
			if($arr1[0] ~~ @fuelcell){print OF "Fuel_Cell ";}
			if($arr1[0] ~~ @alternate){print OF "Alternate";}
			print OF "\n";
		}
		delete $hash2{$key};
	}
	else {
		$printable =  "$key\t$hash1{$key}\t-\t-\t-";
		$printable =~ s/\|\|\|/\t/g;
                print OF "$printable\tH-only\n";
#		if ($printable ~~ @

	}
}
foreach $key (keys %hash2){
	$printable = "$key\t-\t-\t-\t$hash2{$key}";
	$printable =~ s/\|\|\|/\t/g;
        print OF "$printable\tR-only\n";
}




