my $Rscript = qq~

    tr <- read.tree(\"$treeFile\")
    ids <- read.table(\"$idsFile\")[,1]
    p <- first.common.ancestor( tr, ids )
    p <- tree.parent.node( tr, p ) # get the grandparent of p
    ids2.idx <- node.leaves( tr, p )
    ids2 <- tr\$tip.label[ids2.idx]
    cmd <- sprintf(\"nw_clade %s \", treeFile)
    for ( id in ids2 )
  {
  cmd <- paste(cmd, id)
  }
  cmd <- paste(cmd, \" > \", \"$cladeTreeFile\")
    system(cmd)

    cmd <- sprintf("nw_rename %s %s > %s", cladeTreeFile, \"$annFile\", pFile2)
    system(cmd)

    tip.colors <- rep( 1, length(tr\$tip.label) )
    names(tip.colors) <- clade.tr\$tip.label
    tip.colors[rids] <- 4 # blue

    figH <- 8
    figW <- 12
    if ( nLeaves >= 50 )
  {
  figH <- 6.0/50.0 * ( nLeaves - 50) + 10
    figW <- 6.0/50.0 * ( nLeaves - 50) + 6
  }

  tr\$node.label <- rep("", length(tr\$node.label))

    pdf(pdfFile, width=figW, height=figH)
    op <- par(mar=c(0,0,1.5,0), mgp=c(2.85,0.6,0),tcl = -0.3)
    plot(tr, type=\"phylogram\", use.edge.length=FALSE, no.margin=FALSE, show.node.label=T, cex=0.8, tip.color=tip.colors, main=title)
    nodelabels(cl, p)
    par(op)
    dev.off()

    ~;

  unlink $idsFile;
}

# print elements of a hash table
sub print_tbl
{
  my ($rTbl, $r) = @_;
  if ( $r )
  {
    map {print "$_\t". $rTbl->{$_}. "\n"} @{$r};
  }
  else
  {
    map {print "$_\t". $rTbl->{$_}. "\n"} keys %{$rTbl};
  }
}

# Parse vicut's Cltrs table
sub parse_cltr_tbl
{
  my $cltrFile = $vicutDir. "/minNodeCut.cltrs";

  if (! -e $cltrFile )
  {
    warn "\n\n\tERROR: vicut cltr file $cltrFile does not exist";
    print "\n\n";
    exit 1;
  }

  my %tbl; # tbl{cl}{tx} ref to seq IDs of tx taxonomy within the cl cluster.
  open IN, "$cltrFile" or die "Cannot open $cltrFile for reading: $OS_ERROR";
  my $header = <IN>;
  foreach (<IN>)
  {
    chomp;
    my ($id, $cl, $tx) = split /\s+/,$_;
    $cl = "c$cl";
    ##print "$i: $id  $cl  $tx\n";
    $tx =~ s/Unclassified/NA/;
    push @{$tbl{$cl}{$tx}}, $id;
  }
  close IN;

  return %tbl;
}

sub build_ann_seqID_tree
{
  my ($treeFile, $annFile, $annTreeFile ) = @_;

  my ($fh, $annFile2) = tempfile("tmp.XXXX", SUFFIX => '.tx', OPEN => 0, DIR => $tmpDir);
  $cmd = "awk '{print \$1\"\\t\"\$2\"__\"\$1}' $annFile > $annFile2";
  print "\tcmd=$cmd\n" if $dryRun || $debug;
  system($cmd) == 0 or die "system($cmd) failed:$?\n" if!$dryRun;

  $cmd = "rm -f $annTreeFile; nw_rename $treeFile $annFile2 | nw_order -  > $annTreeFile";
  print "\tcmd=$cmd\n" if $dryRun || $debug;
  system($cmd) == 0 or die "system($cmd) failed:$?\n" if!$dryRun;
}


sub build_ann_cltr_tree
{
  my ($treeFile, $annFile, $annTreeFile ) = @_;

  # $annFile file format

  # readId	clstr	annot
  # Chitinivibrio	0	Fibrobacteres
  # Ignavibacterium	1	Chlorobi
  # Caldisericum	2	Caldiserica

  my ($fh, $annFile2) = tempfile("tmp.XXXX", SUFFIX => '.tx', OPEN => 0, DIR => $tmpDir);
  $cmd = "awk '{print \$1\"\\t\"\$3\"__\"\$2}' $annFile > $annFile2";
  print "\tcmd=$cmd\n" if $dryRun || $debug;
  system($cmd) == 0 or die "system($cmd) failed:$?\n" if!$dryRun;

  $cmd = "rm -f $annTreeFile; nw_rename $treeFile $annFile2 | nw_order -  > $annTreeFile";
  print "\tcmd=$cmd\n" if $dryRun || $debug;
  system($cmd) == 0 or die "system($cmd) failed:$?\n" if!$dryRun;
}



sub build_ann_tree
{
  my ($treeFile, $annFile, $annTreeFile ) = @_;

  ## get leaves and test if they have the same number of elements as the table or
  ## the table has those plus more

  $cmd = "rm -f $annTreeFile; nw_rename $treeFile $annFile | nw_order -  > $annTreeFile";
  print "\tcmd=$cmd\n" if $dryRun || $debug;
  system($cmd) == 0 or die "system($cmd) failed:$?\n" if!$dryRun;
}

## plot tree with clade colors
sub plot_tree
{
  my ($treeFile, $title, $pdfFile, $type) = @_;

  # type: a character string specifying the type of phylogeny to be
  #         drawn; it must be one of "phylogram" (the default),
  #         "cladogram", "fan", "unrooted", "radial" or any unambiguous
  #         abbreviation of these.

  if (!$type )
  {
    $type = "phylogram";
  }
  my $showBoostrapVals = "T";

  if (!defined $title)
  {
    $title = "";
  }

  my $readNewickFile = "/Users/pgajer/organizer/programming/R/libs/read.newick.R";

  my $Rscript = qq~

    source(\"$readNewickFile\")
    require(phytools)

    tr <- read.newick(file=\"$treeFile\")
    tr <- collapse.singles(tr)

    (nLeaves <- length(tr\$tip.label))

    figH <- 8
    figW <- 6
    if ( nLeaves >= 50 )
  {
  figH <- 6.0/50.0 * ( nLeaves - 50) + 10
    figW <- 6.0/50.0 * ( nLeaves - 50) + 6
  }

  pdf(\"$pdfFile\", width=figW, height=figH)
    op <- par(mar=c(0,0,1.5,0), mgp=c(2.85,0.6,0),tcl = -0.3)
    plot(tr, type=\"$type\", no.margin=FALSE, use.edge.length=FALSE, show.node.label=F, cex=0.8, main=\"$title\")
    par(op)
    dev.off()
    ~;

  run_R_script( $Rscript );
}

# execute an R-script
sub run_R_script
{
  my $