#!/usr/bin/perl -w

use strict;

my $helpStr=qq{

Usage:
	diff2files.pl <first inputFile> <file ID column> <second inputFile> <2nd file ID column> 
	
Description:
	This script takes two tab-delimited files and join them by their common identifiers.
	Output is to STDOUT, which prints out the difference of IDs between the two files.

	To find common identifiers, use join2files.pl

	<file ID column>: which column the common IDs are in.  Counting columns from zero!!
Note:

};

unless (@ARGV==4){die "$helpStr";}

my ($fileA, $idColA, $fileB, $idColB) = @ARGV;

my %hash = ();

&parse(\%hash, $fileA, $idColA, 'A');
&parse(\%hash, $fileB, $idColB, 'B');
&diff (\%hash, $fileA, 'A', 'B');
&diff (\%hash, $fileB, 'B', 'A');

exit;

sub parse(){
 	my ($hashR, $file, $idCol, $name) = @_;
	open FILE, "$file" or die "Can't open $file: $!\n";
	while (<FILE>){
		s/\n//g;
		my @temp = split /\t/;
		$hashR->{$temp[$idCol]}{$name}=$_;
	}
	close FILE;
	return;
}
sub diff (){
    my ($hashR, $file, $A, $B) = @_;
    print "=============== Only in $file =============\n";
    map{ my $name = $_; if ( defined( $hashR->{$name}{$A} ) ){ print $name."\n" unless defined($hashR->{$name}{$B}); } } sort keys %$hashR;
    return;
}
