#! /usr/bin/perl -w
#
# author: axel wintsche
# mail: wint(spider monkey)bioinf(dot)uni-leipzig(dot)de
# reads a newick tree from stdin and extracts the species names in this tree

use strict;

my $organism_list = "/homes/bierdepot/wint/praktikum/bioinfprak09/data/superfam2ncbi.tab";

foreach my $line (<>) {
	# remove braces
	$line =~ s/[\(\)]//g;
	
	# extract names
	
	foreach(split(/,/,$line)) {
		my @name_string = split(/:/, $_);
		$name_string[0] =~ s/_/[_ ]/g;
		my $grep = `grep -x -P \".*\\t$name_string[0]\" \"$organism_list\"`;
		
		# if the corresponding organism was not found print the search term
		# on STDERR
		if($grep eq ""){ print STDERR "$name_string[0]\n"; next; }
		print $grep;
	}
}
