#!/usr/bin/perl -s

require "MBGD_commonPath.pl";

$WWWROOT = '/db/db9/MBGG/uchiyama/WWW';
$dpres = "$WWWROOT/tmpresult/blastdpres";
$spdpres = "$WWWROOT/tmpresult/blastdpres.sp";
@files = ($dpres);
$treealig = "/bio/adm/package/treealig/align";
$tmpfile = "aligntmp$$";

$| = 1;

if ($verbose) {
        $OUT = '';
} else {
        $OUT = '> /dev/null';
}


$ENV{'DBTAB'} = "/bio/etc/dbtab:$FILE_dbtab";

if (@ARGV == 0) {
	while (<STDIN>) {
		chop;
		$ent = $_;
		$ENT{$ent} = 1;
		if ($ent =~ /sp:/) {
			if (! $flag) {
				@files = ($dpres, $spdpres);
				$flag = 1;
			}
		}
	}
} else {
	foreach $ent (@ARGV) {
		$ENT{$ent} = 1;
		if ($ent =~ /sp:/) {
			if (! $flag) {
				@files = ($dpres, $spdpres);
				$flag = 1;
			}
		}
	}
}


@entries = (keys %ENT);
$entries = join(' ', @entries);
$seqnum = 0 + @entries;

chdir "$DIR_tmp";
system("$CMD_bget $entries -f > $tmpfile");
system("$treealig $tmpfile -n$seqnum $tmpfile.tre $tmpfile.aln $OUT");
open(ALIG, "$tmpfile.aln") || die;
while (<ALIG>) {
	print;
}
close(ALIG);
open(TREE, "$tmpfile.tre") || die;
while (<TREE>) {
	print;
}
close(TREE);

#print "<IMG SRC=/htbin/drawtree?$tmpfile.dnd>";
#sleep(10);

unlink ("$tmpfile","$tmpfile.aln","$tmpfile.tre");
