package WebConfig;
require Exporter;

use strict;
use DBI;
use Genome;
use Scaffold;
use Gene;
use GenomicsUtils;
use Description;
use lib "/var/www/cgi-bin";
############################
sub template();

our @ISA = qw(Exporter);
our @EXPORT = qw(); #export by default
our @EXPORT_OK = qw(); #export by request
my ($db);
sub BEGIN()
{

}
########################

#must have a new function
sub new()
{
    my $self = {};
    bless $self;

    return($self);
}

#must have a DESTROY function
sub DESTROY()
{
}


#my subroutines from here!

sub cgiDir(){
    return "/var/www/cgi-bin";
}
sub htmlDir(){
    return "/var/www/html";
}
sub imgDir(){
    return "/var/www/html/temp";
}
sub taxes($){
    my ($taxIdR) = @_;
    my @temp = ();
    my %unique = ();
    my $count = 0;
    foreach (@$taxIdR){
	unless (/^g/){
	    $count++;
	    $unique{$_}=$count unless $unique{$_};
	    next;
	}
	(my $groupTaxId = $_) =~ s/g//;
	if ($groupTaxId == 0){
	    @temp = (83333, 1423, 211586, 891, 881, 207559, 28232, 243231);
	    
	}
	else {
	    my $dataR = GenomicsUtils::query(qq{SELECT distinct(childId) FROM TaxParentChild, Taxonomy where parentId=$groupTaxId and childId=taxonomyId order by placement});
	    foreach (@$dataR){
		push @temp, $_->[0];
	    }
	}
	foreach (@temp){
	    $count++;
	    $unique{$_}=$count unless $unique{$_};
	}
    }
    @$taxIdR = sort {$unique{$a}<=>$unique{$b}} keys %unique;
    return;
}
sub template(){
    my $template=qq{
	<html>
	    <head>
	    <title>-myTITLE-</title>
	    <meta name="Authors" content="Katherine Huang">
	    <link rel="stylesheet" type="text/css" href="/ns.css" />
	    <SCRIPT language="JavaScript" type="text/javascript">
	    
	    function myFunction(TYPE){
		if (TYPE==0){
		    document.genomeOption.action="../cgi-bin/keywordSearch.cgi";
		    keyword = document.genomeOption.keyword.value;
		
		    if (/^GO:?/.test(keyword)){
			document.genomeOption.action="../cgi-bin/vertiGo.cgi";
			document.genomeOption.term.value=keyword;
		    }
		}
		if (TYPE==1){
		    document.genomeOption.action="../cgi-bin/fetchGenome2.cgi";
		}
		if (TYPE==2){
		    document.genomeOption.action="../cgi-bin/vertiGo.cgi";
		    
		}
		if (TYPE==3){
		    myCount=0;
		    for (index=0; index <document.genomeOption.taxId.length; index++){
			 if (document.genomeOption.taxId[index].selected){
			     myCount++;
			     if (myCount>2){alert("Only up to 2 genomes can be selected for viewing the KEGG metabolic pathways"); return;}
			     myValue = document.genomeOption.taxId[index].value;
			     if (myValue.match("g")){alert("Only up to 2 genomes can be selected for viewing the KEGG metabolic pathways"); return;}
			     
			 }
		    }
		    document.genomeOption.action="../cgi-bin/kegg2.cgi";
		}
	
		document.genomeOption.type.value = TYPE;
		document.genomeOption.submit();
		return;
		
	    }	
	    function popUp(URL) {
		day = new Date();
		id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=600,left = 500,top = 462');");
	    }
	function showmenu(elmnt){
	    document.all(elmnt).style.visibility="visible"
	    }
	function hidemenu(elmnt){
	    document.all(elmnt).style.visibility="hidden"
	    }
	</script>
	    
	    </head>
	    
	    <body bgcolor=#ffffff>
	    
	    <!-- Main Frame-->
	    <table border=0 cellpadding=5 cellspacing=0 bgcolor="#FFFFFF" width=800>
	    <tr>
	    <td width=150 valign=top><!-- Left -->
	    
	    <table border=0 cellpadding=4 cellspacing=1 width=100% bgcolor="#FFFFFF"> <!-- Left Table -->
	    <tr valign=top>
	    <td valign=top align=center>
	    <img src="/vimssCC.gif" border=0 align=center>
	      </td>
	      </tr>
	      <tr>
	      <td class=body9c valign=top align=center>
	      <form name=genomeOption action='../cgi-bin/keywordSearch.cgi' method="get">
	      
	      
	      <div class=body9>Keyword Search: <a href="javascript:popUp('../cgi-bin/keywordSearch.cgi?help=1')">[help]</a> </div><BR>
	      <input type=text name=keyword value="-- Keyword --"><br><BR>
	      <div class=body9>Select Genome(s):</div> <BR>
	      <select name=taxId size=20 class=text10noMargin multiple>
	      <option value=g0 class=text10noMargin selected>-- Favorites --</option>
	      <option value=83333 class=body9>Escherichia coli K12</option>
	      <option value=1423 class=body9>Bacillus subtilis</option>
	      <option value=211586 class=body9>Shewanella oneidensis MR-1</option>
	      <option value=891 class=body9>Desulfuromonas acetoxidans</option>
	      <option value=881 class=body9>Desulfovibrio vulgaris</option>
	      <option value=207559 class=body9>Desulfovibrio desulfuricans G20</option>
	      <option value=28232 class=body9>Geobacter metallireducens</option>
	      <option value=243231 class=body9>Geobacter sulfurreducens PCA</option>
	      <option value=g2 class=text10noMargin>-- Bacteria --</option>
	      <option value=224324 class=body9>Aquifex aeolicus VF5</option>
	      <option value=2336 class=body9>Thermotoga maritima</option>
	      <option value=1299 class=body9>Deinococcus radiodurans</option>
	      <option value=g1117 class=text10noMargin>---- Cyanobacteria </option>
	      <option value=197221 class=body9>Thermosynechococcus elongatus BP-1</option>
	      <option value=84588 class=body9>Synechococcus sp. WH 8102</option>
	      <option value=1148 class=body9>Synechocystis sp. PCC 6803</option>
	      <option value=103690 class=body9>Nostoc sp. PCC 7120</option>
	      <option value=g1224 class=text10noMargin>---- Proteobacteria </option> 
	      <option value=g28211 class=text10noMargin>------ Alphaproteobacteria </option>
	      <option value=194439 class=body9>Chlorobium tepidum TLS</option>
	      <option value=781 class=body9>Rickettsia conorii</option>
	      <option value=782 class=body9>Rickettsia prowazekii</option>
	      <option value=190650 class=body9>Caulobacter crescentus CB15</option>
	      <option value=181661 class=body9>Agrobacterium tumefaciens C58</option>
	      <option value=180835 class=body9>Agrobacterium tumefaciens C58 (UW)</option>
	      <option value=382 class=body9>Sinorhizobium meliloti</option>
	      <option value=29459 class=body9>Brucella melitensis</option>
	      <option value=204722 class=body9>Brucella suis 1330</option>
	      <option value=381 class=body9>Mesorhizobium loti</option>
	      <option value=375 class=body9>Bradyrhizobium japonicum</option>
	      <option value=g28216 class=text10noMargin>------ Betaproteobacteria </option>
	      <option value=305 class=body9>Ralstonia solanacearum</option>
	      <option value=122586 class=body9>Neisseria meningitidis MC58</option>
	      <option value=122587 class=body9>Neisseria meningitidis Z2491</option>
	      <option value=228410 class=body9>Nitrosomonas europaea ATCC 19718</option>
	      <option value=g1236 class=text10noMargin>------ Gammaproteobacteria </option>
	      <option value=190486 class=body9>Xanthomonas axonopodis</option>
	      <option value=190485 class=body9>Xanthomonas campestris</option>
	      <option value=160492 class=body9>Xylella fastidiosa 9a5c</option>
	      <option value=183190 class=body9>Xylella fastidiosa Temecula1</option>
	      <option value=227377 class=body9>Coxiella burnetii RSA 493</option>
	      <option value=208964 class=body9>Pseudomonas aeruginosa PAO1</option>
	      <option value=160488 class=body9>Pseudomonas putida KT2440</option>
	      <option value=223283 class=body9>Pseudomonas syringae</option>
	      <option value=211586 class=body9>Shewanella oneidensis MR-1</option>
	      <option value=666 class=body9>Vibrio cholerae</option>
	      <option value=223926 class=body9>Vibrio parahaemolyticus RIMD 2210633</option>
	      <option value=216895 class=body9>Vibrio vulnificus CMCP6</option>
	      <option value=224915 class=body9>Buchnera aphidicola str. Bp</option>
	      <option value=107806 class=body9>Buchnera aphidicola str. APS</option>
	      <option value=198804 class=body9>Buchnera aphidicola str. Sg</option>
	      <option value=199310 class=body9>Escherichia coli CFT073</option>
	      <option value=83333 class=body9>Escherichia coli K12</option>
	      <option value=83334 class=body9>Escherichia coli O157:H7</option>
	      <option value=155864 class=body9>Escherichia coli O157:H7 EDL933</option>
	      <option value=90370 class=body9>Salmonella enterica, Typhi</option>
	      <option value=209261 class=body9>Salmonella enterica, Typhi Ty2</option>
	      <option value=99287 class=body9>Salmonella typhimurium LT2</option>
	      <option value=198215 class=body9>Shigella flexneri 2a str. 2457T</option>
	      <option value=198214 class=body9>Shigella flexneri 2a str. 301</option>
	      <option value=36870 class=body9>Wigglesworthia glossinidia</option>
	      <option value=214092 class=body9>Yersinia pestis CO92</option>
	      <option value=187410 class=body9>Yersinia pestis KIM</option>
	      <option value=747 class=body9>Pasteurella multocida</option>
	      <option value=71421 class=body9>Haemophilus influenzae Rd KW20</option>
	      <option value=g28221 class=text10noMargin>------ Deltaproteobacteria </option>
	      <option value=891 class=body9>Desulfuromonas acetoxidans</option>
	      <option value=881 class=body9>Desulfovibrio vulgaris</option>
	      <option value=207559 class=body9>Desulfovibrio desulfuricans G20</option>
	      <option value=28232 class=body9>Geobacter metallireducens</option>
	      <option value=243231 class=body9>Geobacter sulfurreducens PCA</option>
	      <option value=g29547 class=text10noMargin>------ Epsilonproteobacteria </option>
	      <option value=197 class=body9>Campylobacter jejuni</option>
	      <option value=85962 class=body9>Helicobacter pylori 26695</option>
	      <option value=85963 class=body9>Helicobacter pylori J99</option>
	      <option value=g1239 class=text10noMargin>---- Firmicutes </option> 
	      <option value=g186801 class=text10noMargin>------ Clostridia </option>
	      <option value=1488 class=body9>Clostridium acetobutylicum</option>
	      <option value=1502 class=body9>Clostridium perfringens</option>
	      <option value=212717 class=body9>Clostridium tetani E88</option>
	      <option value=119072 class=body9>Thermoanaerobacter tengcongensis</option>
	      <option value=g31969 class=text10noMargin>------ Mollicutes </option>
	      <option value=233150 class=body9>Mycoplasma gallisepticum R</option>
	      <option value=2097 class=body9>Mycoplasma genitalium</option>
	      <option value=28227 class=body9>Mycoplasma penetrans</option>
	      <option value=2104 class=body9>Mycoplasma pneumoniae</option>
	      <option value=2107 class=body9>Mycoplasma pulmonis</option>
	      <option value=2130 class=body9>Ureaplasma urealyticum</option>
	      <option value=g91061 class=text10noMargin>------ Bacilli </option>	
	      <option value=g1385 class=text10noMargin>-------- Bacillales </option>
	      <option value=198094 class=body9>Bacillus anthracis str. Ames</option>
	      <option value=226900 class=body9>Bacillus cereus ATCC 14579</option>
	      <option value=86665 class=body9>Bacillus halodurans</option>
	      <option value=1423 class=body9>Bacillus subtilis</option>
	      <option value=182710 class=body9>Oceanobacillus iheyensis</option>
	      <option value=1642 class=body9>Listeria innocua</option>
	      <option value=169963 class=body9>Listeria monocytogenes EGD-e</option>
	      <option value=158878 class=body9>Staphylococcus aureus, Mu50</option>
	      <option value=196620 class=body9>Staphylococcus aureus, MW2</option>
	      <option value=158879 class=body9>Staphylococcus aureus, N315</option>
	      <option value=176280 class=body9>Staphylococcus epidermidis</option>
	      <option value=g186826 class=text10noMargin>-------- Lactobacillales </option>
	      <option value=220668 class=body9>Lactobacillus plantarum WCFS1</option>
	      <option value=226185 class=body9>Enterococcus faecalis V583</option>
	      <option value=208435 class=body9>Streptococcus agalactiae 2603V/R</option>
	      <option value=211110 class=body9>Streptococcus agalactiae NEM316</option>
	      <option value=210007 class=body9>Streptococcus mutans UA159</option>
	      <option value=171101 class=body9>Streptococcus pneumoniae R6</option>
	      <option value=170187 class=body9>Streptococcus pneumoniae TIGR4</option>
	      <option value=160490 class=body9>Streptococcus pyogenes M1 GAS</option>
	      <option value=198466 class=body9>Streptococcus pyogenes MGAS315</option>
	      <option value=186103 class=body9>Streptococcus pyogenes MGAS8232</option>
	      <option value=193567 class=body9>Streptococcus pyogenes SSI-1</option>
	      <option value=1360 class=body9>Lactococcus lactis subsp. lactis</option>
	      <option value=g201174 class=text10noMargin>---- Actinobacteria </option>
	      <option value=203267 class=body9>Tropheryma whipplei str. Twist</option>
	      <option value=218496 class=body9>Tropheryma whipplei TW08/27</option>
	      <option value=196164 class=body9>Corynebacterium efficiens YS-314</option>
	      <option value=196627 class=body9>Corynebacterium glutamicum</option>
	      <option value=1769 class=body9>Mycobacterium leprae</option>
	      <option value=83331 class=body9>Mycobacterium tuberculosis CDC1551</option>
	      <option value=83332 class=body9>Mycobacterium tuberculosis H37Rv</option>
	      <option value=227882 class=body9>Streptomyces avermitilis MA-4680</option>
	      <option value=100226 class=body9>Streptomyces coelicolor A3(2)</option>
	      <option value=206672 class=body9>Bifidobacterium longum NCC2705</option>
	      <option value=g51290 class=text10noMargin>---- Chlamydiae/Verrucomicrobia</option>
	      <option value=83560 class=body9>Chlamydia muridarum</option>
	      <option value=813 class=body9>Chlamydia trachomatis</option>
	      <option value=227941 class=body9>Chlamydophila caviae GPIC</option>
	      <option value=115711 class=body9>Chlamydophila pneumoniae AR39</option>
	      <option value=115713 class=body9>Chlamydophila pneumoniae CWL029</option>
	      <option value=138677 class=body9>Chlamydophila pneumoniae J138</option>
	      <option value=g203691 class=text10noMargin>---- Spirochaetes </option>
	      <option value=224326 class=body9>Borrelia burgdorferi B31</option>
	      <option value=160 class=body9>Treponema pallidum</option>
	      <option value=189518 class=body9>Leptospira interrogans, 56601</option>
	      <option value=226186 class=body9>Bacteroides thetaiotaomicron VPI-5482</option>
	      <option value=190304 class=body9>Fusobacterium nucleatum, ATCC25586</option>
	      <option value=g2157 class=text10noMargin>-- Archaea --</option>
	      <option value=13773 class=body9>Pyrobaculum aerophilum</option>
	      <option value=56636 class=body9>Aeropyrum pernix</option>
	      <option value=2287 class=body9>Sulfolobus solfataricus</option>
	      <option value=111955 class=body9>Sulfolobus tokodaii</option>
	      <option value=187420 class=body9>Methanothermobacter thermautotroph.</option>
	      <option value=2190 class=body9>Methanocaldococcus jannaschii</option>
	      <option value=188937 class=body9>Methanosarcina acetivorans C2A</option>
	      <option value=192952 class=body9>Methanosarcina mazei Goe1</option>
	      <option value=64091 class=body9>Halobacterium sp. NRC-1</option>
	      <option value=2303 class=body9>Thermoplasma acidophilum</option>
	      <option value=50339 class=body9>Thermoplasma volcanium</option>
	      <option value=29292 class=body9>Pyrococcus abyssi</option>
	      <option value=186497 class=body9>Pyrococcus furiosus DSM 3638</option>
	      <option value=53953 class=body9>Pyrococcus horikoshii</option>
	      <option value=2234 class=body9>Archaeoglobus fulgidus</option>
	      <option value=190192 class=body9>Methanopyrus kandleri AV19</option>
	      </select>
	      <input type=hidden name=type value=0>
	      <input type=hidden name=mapId value="MAPID">
	
	      <input type=hidden name=term value=1>
	     
	      <BR>	
	      <a href="javascript:myFunction(0)"><img src="/gSearch.png" border=1></a> 
	      <a href="javascript:myFunction(1)"><img src="/gInfo.png" border=1></a>
	      <a href="javascript:myFunction(2)"><img src="/gGo.png" border=1></a> 
	      <a href="javascript:myFunction(3)"><img src="/gPath.png" border=1></a> 
	      </form>
	      </td>
	      </tr> 
	      
	      </table> <!-- End of Left Table -->
	      
	      </td>
	      <td valign=top><!-- Right -->
              <table border=0 width=600><tr><td> <!-- header outer table-->
	      <table border=0 cellspacing=0 cellpadding=1 valign=top align=right width=400><!-- Header 
	      inner table  -->
	      <tr nowrap class=body10>
	      <td><img src="/cart.png" border=0></td>
	      <td>My Genes</td>
	      <td >|</td>
	      <td><a href="/blast/blast_cs2.html" onmouseover="style.backgroundColor='transparent',this.style.color='#333367',style.fontWeight='bold'" onmouseout="style.backgroundColor='transparent',this.style.color='#807000',style.fontWeight='bold'">WWW-BLAST</a></td>
	      <td>|</td> 
	      <td><a href="../cgi-bin/search2.cgi" onmouseover="style.backgroundColor='transparent',this.style.color='#333367',style.fontWeight='bold'" onmouseout="style.backgroundColor='transparent',this.style.color='#807000',style.fontWeight='bold'">Advanced Search</a></td> 
	      <td>|</td>
	      <td>Contact Us</td>
	      <td>|</td>
	      <td><a href="/home.html" onmouseover="style.backgroundColor='transparent',this.style.color='#333367',style.fontWeight='bold'" onmouseout="style.backgroundColor='transparent',this.style.color='#807000',style.fontWeight='bold'">Home</a></td> 
	     
	      </tr>
	      </table>
	      </td></tr></table><P>
	      -myCGI- <!-- end of cgi table-->
	      
	      </td>
	      </tr>
	      </table>
	      <!-- End of Main Frame -->
	  };
    return $template;
}

sub genesReport(@){
    my %param = @_;
   # my %genomes = Genome::getGenomeMap(); #map to a hash of keys:taxonomyId, values:taxonomyObj
    my $script = $param{cgi};
    my @genes = Scaffold::fetchGenesList(@_); #default sorted by locusId..., not what we want for search results
    
    my $out = "<P><P><TABLE border=0 class=\"text10noMargin\" width=600 bgcolor=\"#FFFFFF\">\n";
        
    $out .= "<TR><TD>".(join "</TD><TD colspan=3>", "",  "Genes")."</TD></TR>\n";
 
    my $count = defined($param{count})?$param{count}:0;
    foreach my $obj (@genes){
	
	my $locus = $obj->locusId();
	#print $locus."===cool<BR>\n";
	next unless $locus;
	my $locusLink = qq{
	     <A HREF=\"../cgi-bin/$script?locus=$locus&disp=0\" target=\"_protein\"><acronym title=\"Gene Info\">G</acronym></A>
	     <A HREF=\"../cgi-bin/$script?locus=$locus&disp=1\" target=\"_protein\"><acronym title=\"Operon & Regulon\"><font color="#336699">O</font></acronym></A> 
	     
	     <A HREF=\"../cgi-bin/$script?locus=$locus&disp=2\" target=\"_protein\"><acronym title=\"Domains\"><font color="#666699">D</font></acronym></A>
	     <A HREF=\"../cgi-bin/$script?locus=$locus&disp=3\" target=\"_protein\"><acronym title=\"Homologs\"><font color="#333333">H</font></acronym></A> 
	     <A HREF=\"../cgi-bin/$script?locus=$locus&disp=4\" target=\"_protein\"><acronym title=\"Sequences\"><font color="#336699">S</font></acronym></A>
	    };

	$count++;

	## basic gene info --

	#### Taxonomy ####
	my $taxId = $obj->taxonomyId();	
	my $taxName = $obj->taxonomyName();
	my $scaffoldId = $obj->scaffoldId();
	my $sInfo = $obj->scaffoldInfo();
   	$sInfo =~ s/$taxName//;
	$sInfo =~ s/^\s//;

	#### Position ####
	my $begin = $obj->begin(); #start (must be <= end)
	my $end = $obj->end(); #end
	my $strand = $obj->strand();

	#### Automatic Description ####
	my $cog = $obj->cog()?"COG".$obj->cog().", ".$obj->cogDesc():"";
	my ($ec, $go, $ipr);
	if ($param{disp} == 2){
	    #map{s/\t/==/; print $_."<BR>" } @{$obj->ec()};
	    $param{keyword} =~ s/EC:?//;
	    $param{keyword} =~ s/GO:?(\d+)/GO:$1/;
	    $param{keyword} =~ s/IPR:?/IPR/;
	    $ec = (join "<BR>",grep /$param{keyword}/i, @{$obj->ec()})||"";
	    $go = (join "<BR>", grep /$param{keyword}/i, @{$obj->go()})||"";
	    $ipr = (join "<BR>", grep /$param{keyword}/i, @{$obj->ipr()})||"";
	}
	else{
	    $ec = (join "<BR>",@{$obj->ec()})||"";
	    $go = (join "<BR>", @{$obj->go()})||"";
	    $ipr = (join "<BR>", @{$obj->ipr()})||"";
	}
	my $synO = $obj->synonym();
	my $name = "";
	my $synonym = "";
	my $gi = "";
	if ($synO){
	    $name = $synO->{0}||"";
	    $synonym = $synO->{1}||""; ## this method is not right....
	    $gi = ($synO->{2}&& $synO->{2} !~ /^9/)?$synO->{2}:"";
	}
	my $desc;
	foreach my $annObj(@{$obj->annotationRef()}){
	    $desc .= (join ": ", ($annObj->source(), $annObj->description()));
	}
	$desc = defined($desc)?$desc:"";
	

	##-----------------------------------------------------------------------------##
	
	## Display format --
	my $color = "#B22222";
	my @info = ($desc, $cog, $ec, $ipr, $go);
	
	@info = ($desc, $cog, $ipr) if $param{disp}==1;

	if ($param{disp} == 2){
	    #print $param{keyword};
	    $param{keyword} =~ s/%/.*/;
	    $name =~ s/($param{keyword})/<font color=\"$color\">$1<\/font>/gi if $name;
	    $cog = "" unless $cog =~ /$param{keyword}/; 
	} 
	my $line = "";
	foreach (@info){
	    if ($_){
		my @tmp = split /<BR>/;
	      if ($param{keyword}){

	        $param{keyword} =~ s/%/.*/;
		foreach (@tmp){
		    s/($param{keyword})/<font color=\"$color\">$1<\/font>/gi;
		}
		$_ = join "<BR>", @tmp;
	      }
		$line .= $_."<BR>";
	    }
	}
	
	$out .= "<TR valign=top><TD rolspan=2>".$count."</td>";
	$out .= qq{
	    <td>
		<table border=0 width=100%>
		<tr bgcolor="#e0e8FF"><td width=15%><B>$locusLink</b></td><td width=50%>VIMSS$locus : <B>$name</B> $synonym</td><td class=body10l>$taxName</td></tr>
		<tr><td colspan=3>$line</td></tr>
		</table>
		</td></tr>
	    };
	

    } # end of the for loop
    
    $out .= "</table>";
    
    return $out;
}




#END 
###########################

return 1;


