#!/usr/bin/perl -w
#
# author: axel wintsche
# mail: wint(spider monkey)bioinf(dot)uni-leipzig(dot)de

use strict;

$#ARGV == 1 or die "usage:\nspecname.pl <gencount> <motifcount>\n";
# countfiles have two cols: "spec name", "# of occurences/genes";



##-----------------------------------------------------------
#	create hash "spec name" -> "count"
#
my %gen;

open(GEN,"<$ARGV[0]") or die;

foreach(<GEN>) {
	chomp $_;
	my @temp = split(/\t/, $_);
	if(exists $gen{$temp[0]}) {
		$gen{$temp[0]} = ($temp[1] < $gen{$temp[0]})? $gen{$temp[0]} : $temp[1];
	}
	else { $gen{$temp[0]} = $temp[1]; }
}
close(GEN);


my %motif;

open(MOTIF,"<$ARGV[1]") or die;

foreach(<MOTIF>) {
	chomp $_;
	my @temp = split(/\t/, $_);
	if(exists $motif{$temp[0]}) {
		$motif{$temp[0]} = ($temp[1] < $motif{$temp[0]})? $motif{$temp[0]} : $temp[1];
	}
	else { $motif{$temp[0]} = $temp[1]; }
}
close(MOTIF);

foreach(keys %motif) {
	my $avg = sprintf "%.2f", ($motif{$_} / $gen{$_});
	print $_."\t".$gen{$_}."\t".$motif{$_}."\t".$avg."\n";
}
