#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);

my $sequence = "";

# read data from our data.txt file (with our own demo sequence)
open( INFILE, "data.txt");
                while(<infile>){
                                my $line = $_;
                                chomp $line;
                                $sequence = $sequence . $line;
                }
close(INFILE);


# create a CGI object in order to create the HTML file
my $q = new CGI;


# read the parameters sent thru the form and do necessary initializations
my $new_seq = $q->param("sequence") || "No Sequence";
$new_seq =~ s/[\s\n\t]+//g unless $new_seq =~ /No Sequence/;
$new_seq = "No Sequence" if $new_seq =~ /[^AaCcTtGg]/;
$sequence = $new_seq unless $new_seq =~ /No Sequence/ ;
$sequence = uc $sequence;

# define the trimers
my $aw_trimers = {
                                'AAA' => [0], 'AAC' => [0], 'AAT' => [0], 'AAG' => [0],
                                'AGC' => [0], 'AGT' => [0], 'AGG' => [0], 'AGA' => [0],
                                'ACT' => [0], 'ACG' => [0], 'ACC' => [0], 'ACA' => [0],
                                'ATG' => [0], 'ATC' => [0], 'ATT' => [0], 'ATA' => [0],

                                'CAA' => [0], 'CAC' => [0], 'CAT' => [0], 'CAG' => [0],
                                'CGC' => [0], 'CGT' => [0], 'CGG' => [0], 'CGA' => [0],
                                'CCT' => [0], 'CCG' => [0], 'CCC' => [0], 'CCA' => [0],
                                'CTG' => [0], 'CTC' => [0], 'CTT' => [0], 'CTA' => [0],

                                'GAA' => [0], 'GAC' => [0], 'GAT' => [0], 'GAG' => [0],
                                'GGC' => [0], 'GGT' => [0], 'GGG' => [0], 'GGA' => [0],
                                'GCT' => [0], 'GCG' => [0], 'GCC' => [0], 'GCA' => [0],
                                'GTG' => [0], 'GTC' => [0], 'GTT' => [0], 'GTA' => [0],

                                'TAA' => [0], 'TAC' => [0], 'TAT' => [0], 'TAG' => [0],
                                'TGC' => [0], 'TGT' => [0], 'TGG' => [0], 'TGA' => [0],
                                'TCT' => [0], 'TCG' => [0], 'TCC' => [0], 'TCA' => [0],
                                'TTG' => [0], 'TTC' => [0], 'TTT' => [0], 'TTA' => [0] };

# start printing the HTML file
print $q->header();

# print the sequence that has been pasted
$q->print("<html><head><title>Sequence Results </title></head><body onLoad='javascript:focus()'>");
$q->print("<table border=0 cellpadding=3 cellspacing=2 width=\"700\"><tr width='650'><td width=\"650\">");

my $pp = $sequence;
$pp =~ s/(\w{40})/$1\n/g;
$q->print("$pp<br>\n");
undef $pp;
$q->print("</td></tr></table>");

# parameters initialization
my $no_of_trimers = 0;

#compute the requested parameters
for( my $i=0; $i<$size; $i++){
                my $trimer = substr( $sequence, $i, 3 );

                $aw_trimers->{$trimer}->[0]++ if defined $aw_trimers->{$trimer};
}

# print the trimers
&showTrimers($aw_trimers) if ($actions->{'trimers'}->[0] || $actions->{'all'}->[0]);

# close the HTML file
$q->print("</body></html>");


#__subs__#

sub showTrimers{
                my $aw_trimers = shift;

                foreach my $trios (sort keys %$aw_trimers){
                        $q->print("$trios\t $aw_trimers->{$trios}->[0]<br>\n");
                };

                map{$no_of_trimers = $no_of_trimers + $aw_trimers->{$_}->[0]} (keys %$aw_trimers);
                $q->print("The number of trimers is: $no_of_trimers<br>\n");

                return;
}