#!/usr/bin/perl
package HtmlAlignment;

##########################################################################
##### Author :	Shuai Weng
##### Date   :  Sept. 2002
##### Description : 
#####
#######################################################################
use strict;
use CGI qw/:all :html3/;

#######################################################################
######################## global variables #############################


#######################################################################
sub new {      ############ constructor ###############################
#######################################################################
       
    my ($self, %args) = @_;

    $self = {};
    bless $self;

    $self->{'_listArrayRef'} = $args{'listArrayRef'};
    $self->{'_urlArrayRef'} = $args{'urlArrayRef'};
    $self->{'_colorHashRef'} = $args{'colorHashRef'};

    if (!$self->{'_listArrayRef'}) {
	print "The aligned sequence 'listArrayRef' must be passed to the 'HtmlAlignment' object.";
	return;
    }
    return $self;
}

########################################################################
sub showAlignment {
########################################################################
    my ($self) = @_;

    my ($rows, @symbolChars);
    my $symbol = pop(@{$self->{'_listArrayRef'}});
    my $symbolUrl = pop(@{$self->{'_urlArrayRef'}});

    if ($symbol =~ /^[\*\:\. ]+$/) {
	@symbolChars = split(//, $symbol);
	if ($self->{'_listArrayRef'}->[0] =~ /^[^ ]+ +(.+)$/) {
	    my $len = length($1);
	    while (@symbolChars > $len) {
		 shift @symbolChars;
	    }
	}
    }
    for (my $i = 0; $i < @{$self->{'_listArrayRef'}}; $i++) {

	if ($self->{'_listArrayRef'}->[$i] =~ /^([^ ]+) +(.+)$/) {
	    
	    my $showNm = $1;
	    my $seq = $2;
	    my $len = length($seq);
	    my $seqUrl;
	    if ($self->{'_urlArrayRef'}) {
		$seqUrl = $self->{'_urlArrayRef'}->[$i];
		if ($seqUrl) {
		    $showNm = a({-href=>$seqUrl},
				$showNm);
		}
	    }

	    #######

	    my $aaLine;
	    my @aaChars = split(//, $seq);
	    my $bgcolor = "white";
	    for (my $j = 0; $j <= $#aaChars; $j++) {
		my $bgcolor;
		if ($self->{'_colorHashRef'}) {
		    $bgcolor = $self->{'_colorHashRef'}{$symbolChars[$j]} 
		                  || "white";
		}
		$aaLine .= td({-bgcolor=>"$bgcolor"},
			      (tt($aaChars[$j])));
	    }
	    $rows .= Tr(th({align=>'left'},
			   $showNm).td(br).
			$aaLine);
	}
    }
    my $symbolLine;
    foreach my $char (@symbolChars) {
	$symbolLine .= td({-bgcolor=>'white'},
			      (tt($char)));
    }
    my $symbol = "Symbols";
    if ($symbolUrl) {
	$symbol = a({-href=>$symbolUrl}, "Symbols");
    }
    $rows .= Tr(th({align=>'left'},
		    $symbol).td(br).
		$symbolLine);
    print table({-align=>'center',
		 -border=>'0',
		 -cellspacing=>'0',
		 -cellpading=>'0'},
		$rows), p;

}

########################################################################
1;
########################################################################

=pod

=head1 Name

HtmlAlignment.pm    

=head1 Description

This perl object (HtmlAlignment.pm) contains methods for displaying the 
DNA/protein sequence alignments.

=head1 Instantiating a New HtmlAlignment Object

To instantiate a new HtmlAlignment object, you may use one of following syntaxes:

# my $obj = GCG->new(map=>$map);


OR


# my $obj = 


=head1 Accessor Methods


=head2 

Usage:






=head1 Author

shuai@genome.stanford.edu

=cut








