#!/usr/bin/perl -w
##############################################################################
#
# Author:Jack Min, on June 1, 2006

# Name: joinTab.pl
# This program is used to join two tables.
#

############################################ Body of the code ################################

if ($#ARGV != 1)
{
	print "Usage: countSpecies.pl species.info species.count\n\n";
	exit;
}

print "In progress ...\n";

  $t1 = '';
  $t2  = '';

$t1 = $ARGV[0];
$t2 = $ARGV[1];

# Step one get new gene
  
unless( open(IN, $t1))
{
	print STDERR "Cannot open file IDfile\n\n";
   exit;
}

@famA = ();
$current = '';
$int = 0;
$spCount = 0;
while (<IN>)
{
	$line =$_;
	chomp ($line);
	$line =~ s/[\n\f\r\"]//;
	@tmp = split (/\t/, $line);
	$current = $tmp[2];
	$save = $tmp[3]."\t".$tmp[4]."\n";
	#print $current."\n";
	$int++; print $int."\n";
	if ($int ==1)
	{
		$spCount++;
		$preFam = $current;
		$preSave = $save;
	}	

	if ($int >1)
	{
		if ($current ne $preFam)
		{
			push (@famA, $preFam."\t".$spCount."\t".$preSave."\n");
			$preFam = $current;  $preSave = $save;
			$spCount = 1;
		}	
		else
		{$spCount++;
		$preSave = $save;  $preFam = $current;}
	}
}
push (@famA, $preFam."\t".$spCount."\t".$preSave."\n");
close (IN);
open (OUT, ">$t2");
print OUT (@famA);
close OUT;

print "The job is done!!\n\n";
exit;

