#!/usr/bin/perl

#######################################################
# Written by Justin Elser 9/30/10                     #
#  takes the genome_map.tab and tree files from hal   #
#   and writes a new tree file with 2 char id         #
#   replaced by full genus + species                  #
#						      #
#######################################################

use strict;
use warnings;

if($#ARGV != 2) {
	print "usage: species_tree_names_fix.pl genome_map.tab tree_file output_tree_file\n";
	exit;
}

my $tab_file = $ARGV[0];
my $tree_file = $ARGV[1];
my $out_file = $ARGV[2];

open(table_file,"$tab_file");


my %species_hash;
while (<table_file>){
	my $line = $_;
	chomp $line;
	my ($spec_id,$spec_name) = split("\t",$line);
	$spec_name =~ s/\.fasta//g;
	$spec_name =~ s/\.fa//g;
	my ($spec_genus, $spec_species) = split(/[_]/,$spec_name);
	if (defined($spec_species)) {
			$spec_name = $spec_genus . " " . $spec_species;
	}else{
	$spec_name = $spec_genus;
	}
	if(!defined($species_hash{$spec_id})) {
		$species_hash{$spec_id} = $spec_name;
	}else{
		print "duplicate entries in hash!!!\n";
		exit;
	}
}

close(table_file);

open(tree_file,"$tree_file");
open(output_file, ">$out_file");

while(<tree_file>) {
	my $line = $_;
	chomp $line;
	foreach my $id (keys %species_hash) {
		$line =~ s/$id\:/$species_hash{$id}\:/g;
	}
	print output_file "$line\n";
}
