#!/usr/bin/perl -w
use lib '../lib';
use Bio::CDAT::QIU::CDAT;
use Bio::AlignIO;
use strict;
use Data::Dumper;
use Bio::CDAT::QIU::CDAT::Matrix::Align;
use Bio::TreeIO;
use Getopt::Std;

# Optional input of selected elements: 
# -o <OTU> || <first_seq_id>
# -c <Character> || 1 
# -t <tree id> || 1
# -m <matrix id> || 1
my %opts;
getopts('o:c:t:m:', \%opts); 

my $cdat = new Bio::CDAT::QIU::CDAT();

##############################################################
#### PART I. Convert a Bio::SimpleAlign to a Bio::CDAT::QIU::CDAT::Matrix ####
##############################################################
my $aln_file = shift @ARGV;
my $aln_in   = Bio::AlignIO->new( '-file' => $aln_file );
my $aln      = $aln_in->next_aln();
my $mat      = Bio::CDAT::QIU::CDAT::Matrix::Align->new();
$mat->aln_to_mat($aln);
my @mat_otu=@{ $mat->get_otus() };
my $selected_otu  = $opts{'o'} || $mat_otu[0]; # default first OTU
my $selected_char = $opts{'c'} || 1; # default: first char

##############################################################
#### PART II. Build a Bio::CDAT::QIU::CDAT            #######
##############################################################

my $tree_file = shift @ARGV;
my $tree_in   = Bio::TreeIO->new(-file=>$tree_file);
my $tree      = $tree_in->next_tree();
my @tree_otu  = grep { $_->is_Leaf() } $tree->get_nodes();

my @tree = ( $tree );
my @mat  = ( $mat );
$cdat->set_name('test');
$cdat->set_otus(\@mat_otu); # set alignment OTUs as Bio::CDAT::QIU::CDAT OTUs
$cdat->set_trees( \@tree );
$cdat->set_matrices ( \@mat );

##############################################################
#### PART III USE CASE 1: Visualization                #######
##############################################################

use Bio::CDAT::QIU::CDAT::Graphics;

my $panel = Bio::CDAT::QIU::CDAT::Graphics->new($cdat);
$panel->add_chars(1); # Display the 1st character, and ...
for ( my $i = 298; $i <= 300; $i++ ) { # Display the 100th codons 
    $panel->add_chars($i);
}     
print $panel->draw();

exit;
