#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI qw(:cgi-lib :standard);
use CGI::Carp qw(fatalsToBrowser);

### Header
my $q = new CGI;
print $q->header;
print $q->start_html(-title => 'AlignLoc');
print $q->p({-align=>'CENTER'},"<img height=\"89\" src=\"affiliations.png\" width=\"700\"/>");
print "<br><hr size=4><br>";
print $q->h1({-align=>'CENTER'},"<i>Anopheles</i> Alignment Locator");
print $q->h2({-align=>'CENTER'},"Gene Identifier Search");


### Search Form
print $q->start_form(
    -name    => 'main_form',
    -method  => 'GET',
    -enctype => &CGI::URL_ENCODED,
    -onsubmit => 'return javascript:validation_function()'
    );

my %labels = (
    'AalbS1' => 'Anopheles albimanus',
    'AaraD1' => 'Anopheles arabiensis',
    'AatrE1' => 'Anopheles atroparvus',
    'AchrA1' => 'Anopheles christyi',
    'AculA1' => 'Anopheles culicifacies',
    'AdarC2' => 'Anopheles darlingi',
    'AdirW1' => 'Anopheles dirus',
    'AepiE1' => 'Anopheles epiroticus',
    'AfarF1' => 'Anopheles farauti',
    'AfunF1' => 'Anopheles funestus',
    'AgamP3' => 'Anopheles gambiae (PEST)',
    'AmacM1' => 'Anopheles maculatus',
    'AmelC1' => 'Anopheles melas',
    'AmerM1' => 'Anopheles merus',
    'AminM1' => 'Anopheles minimus',
    'AquaS1' => 'Anopheles quadriannulatus',
    'AsinS1' => 'Anopheles sinensis',
    'AsteI2' => 'Anopheles stephensi (Indian)',
    'AsteS1' => 'Anopheles stephensi (SDA-500)'
    );
print "<p align='CENTER'>Species: ";
print $q->popup_menu(
    -name => 'species',
    -values     => [sort keys %labels],
    -default    => 'AgamP3',
    -labels     => \%labels,
    );
print "&nbsp;&nbsp;&nbsp;";

print "Transcript ID: ";
print $q->textfield(
        -name      => 'geneid',
        -value     => 'AGAP006348-RA',
        -size      => 20,
        -maxlength => 30,
    );
print "&nbsp;&nbsp;&nbsp;";

print $q->submit(
        -name     => 'submit_form',
        -value    => 'Search',
        -onsubmit => 'javascript: validate_form()',
    );
print "</p>";

print $q->end_form;



### get any defined variables
my $all_params = $q->Vars;
# AGAP004677-RB
if(defined($all_params->{'geneid'})) {

    if($all_params->{'geneid'}!~/\w+\d+\-R\w+/) {
	print "<p align='CENTER'>";
	print "Sorry, but your transcript ID '<b>$all_params->{'geneid'}</b>' does not look like a real transcript ID<br>";
	print "Real transcript IDs look like this: <b>AGAP004677-RB</b>";
	print "</p>";
    }

    else {
	my $result=`grep $all_params->{'geneid'} AGCC/GFF3/$all_params->{'species'}.cds.gff3`;
	if($result=~/$all_params->{'geneid'}/) {
	    print "<p align='CENTER'>";
	    print "Transcript ID '<b>$all_params->{'geneid'}</b>' found in <i>$labels{$all_params->{'species'}}</i><br>";

	    my @results=split(/\n/,$result);
	    my $strand='+';
	    my @intervals=();
	    my $bpsum=0;
	    for(my $i=0; $i<=$#results; $i++) {
		my @coords=split(/\s+/,$results[$i]);
		push(@intervals,"chr$coords[0]:$coords[3]\-$coords[4]");
		if($coords[6] eq '-') { $strand='-'; }
		$bpsum=($bpsum+($coords[4]-$coords[3]))+1;
	    }
	    print "<br>A total of $bpsum basepairs<br><br>";

	    my $URL="http://www.broadinstitute.org/compbio1/cav.php?alnset=";  
	    #$URL.=lc($all_params->{'species'});
	    $URL.=$all_params->{'species'};
	    $URL.="&interval=";
	    if($strand eq '+') { 
		$URL.=join("+",@intervals); 
		print join("<br>",@results)."<br><br>";
	    }
	    else { 
		$URL.=join("+",reverse(@intervals)); 
		print join("<br>",reverse(@results))."<br><br>";
	    }
	    $URL.="&ancestor=None&strand=$strand&wrap=40";
	    if(($bpsum/3)>1000) { my $max=($bpsum/3)+100; $URL.="&maxCodons=$max";  }

	    # &hideInserts
	    # &fastaOut

	    print "<table align='center' cellpadding='5'><tr><td align='center'><b>With Gaps in Reference</b></td><td align='center'><b>Without Gaps in Reference</b></td></tr><tr><td>";
	    # ALN with gaps
	    print "<a href=\"$URL\" target=\"_blank\" title=\"Click to view gapped alignment!\"><img style=\"max-width:95%;border:8px outset #cbccd0;\" height=\"110\" src=\"aln2.png\"/></a><br><br>";
	    print "</td> <td>";
	    # ALN without gaps
	    print "<a href=\"$URL\&hideInserts\" target=\"_blank\" title=\"Click to view ungapped alignment!\"><img style=\"max-width:95%;border:8px outset #cbccd0;\" height=\"110\" src=\"aln1.png\"/></a><br><br>";
	    print "</tr><tr><td>";
	    # FAS with gaps
	    print "<a href=\"$URL\&fastaOut\" target=\"_blank\" title=\"Click to view gapped FASTA!\"><img style=\"max-width:95%;border:8px outset #cbccd0;\" height=\"110\" src=\"fas2.png\"/></a><br><br>";
	    print "</td> <td>";
	    # FAS without gaps
	    print "<a href=\"$URL\&fastaOut\&hideInserts\" target=\"_blank\" title=\"Click to view ungapped FASTA!\"><img style=\"max-width:95%;border:8px outset #cbccd0;\" height=\"110\" src=\"fas1.png\"/></a><br><br>";
	    print "</td></tr></table></p>";
	}
	else {
	    print "<p align='CENTER'>";
	    print "Sorry, no matches were found for Transcript ID '<b>$all_params->{'geneid'}</b>' in Species ";
	    print "<b><i>$labels{$all_params->{'species'}}</i></b>";
	    print "<br><br>Did you select the correct species to search?";
	    print "</p>";
	}
    }
}

print "<br><hr size=4><br>";
print "<h2 align=\"CENTER\">Important User Notes</h2>";
print "<p align=\"CENTER\">This work is ongoing, therefore any results presented here are only temporary and are likely to be updated as further analyses steps are completed.<br>The results are provided to assist your research efforts, please respect data usage policies of the AGCC.<br><br>";
print "The alignment viewer is under continual development, and therefore it may 'break' at any time, please bear with us as we strive to build a useful tool.<br>";
print "The options for viewing alignment regions are controlled through the URL, please review the current documentation to explore all the options: <a href=\"http://www.broadinstitute.org/compbio1/cav.php\" target=\"_blank\">readme</a><br><br>";
print "If any parts of the CDS region(s) have no alignments to any other species, then no alignment will be displayed - please edit the URL intervals to search for subsets of your region(s).";

print $q->end_html;
