#!/usr/bin/perl
=head2 SYNOPSIS

	Description: Check whether the cross-mapping reads mapped to the syntenic gene pairs in two subgenomes.
             
=head2 AUTHOR

	Kang Zhang  Email:  demut@foxmail.com
	
=cut


open PAIR,"<$ARGV[0]" or die;
open MAP1,"<$ARGV[1]" or die;
open MAP2,"<$ARGV[2]" or die;


while(<MAP1>){
	next if (!/\s+Assigned\s+/);
	chomp;
	@arr=split;
	$hit{$arr[0]}=$arr[3];
	$raw{$arr[3]}++;
}

while(<PAIR>){
	chomp;
	my @arr=split;
	$pair{$arr[0]}=$arr[1];
}

while(<MAP2>){
	next if (!/\s+Assigned\s+/);
	chomp;
	my @arr=split;
	$raw{$arr[3]}++;
	next if (!defined $hit{$arr[0]});
	my $target=$hit{$arr[0]};
	if($pair{$target} eq $arr[3]){
		$pairCount{$target}++;
	}
}

foreach my $target (sort keys %pair){
	if(!defined $pairCount{$target}){
		$pairCount{$target}=0;
	}
	if(!defined $raw{$target}){$raw{$target}=0;}
	if(!defined $raw{$pair{$target}}){$raw{$pair{$target}}=0;}
	print "$target\t$pair{$target}\t$pairCount{$target}\t[$raw{$target}:$raw{$pair{$target}}]\n";
}
