#!/usr/bin/perl
use strict;
use warnings;
use DBI();
require "toolhead.cgi";
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);


my $cgi=new CGI();

&HEADER("HD-RNAS");

&GOTO_top;

&main_toolbar;

print <<EOH;
<br></br><br></br><br></br><br></br>
<center><div class="body" style="width:1100px;">

EOH
if(!$cgi->param('id')){
        print "Unable to find Query\n</div></center>";
}
else{
        my $new=$cgi->param('id');
#connect to database
#==================
	my $dbh=DBI->connect("DBI:mysql:database=HDRNAS;host=localhost", "debasish", "debasish**2014",{'RaiseError'=>1});

	if($new=~ /:/g){				#*#*$&*#@##*(@#@)#(@)#@)++++===== BEFORE LAST PAGE
		my @array=split ':',$new;
		my $sth=$dbh->prepare("SELECT mol_id,mol_typ,length FROM variables WHERE mol_typ LIKE \"$array[0]\%\" AND mol_org LIKE \"$array[1]\%\"");
		$sth->execute();
print <<EndOfTBL;
<center><table width=100% class=curvedEdges cellspacing=10 style="border:2px solid #199">
<tr>
<th width=20%><font color="#993366">PDB ID</font></th><th width=20%><font color="#993366">Mol ID</font></th><th width=20%><font color="#993366">Molecule Description</font></th><th width=20%><font color="#993366">Length</font></th><th width=20%><font color="#993366">Resolution in &#197;</font></th>
EndOfTBL
		while(my $ref =$sth->fetchrow_hashref()){
			my @query=split '_',$ref->{'mol_id'};
                	my $nth=$dbh->prepare("SELECT resol FROM constants WHERE pdb_id=\"$query[0]\"");
			$nth->execute();
			my $property=$nth->fetchrow_hashref();
print <<EndOfTBL;
<tr align=center>
<td width=16%>
<a href=\"./sql_level_1?id=$query[0]\_$array[0]\"><b>$query[0]</b></a>&nbsp;</td><td width=12%><b>$query[1]</b></td><td width=12%><b>$ref->{'mol_typ'}</b></td><td width=12%><b>$ref->{'length'}</b></td><td width=12%><b>$property->{'resol'}</b></td>
</tr>
EndOfTBL
		}
		print"</table></center></div></centre>";
		$sth->finish();
                $dbh->disconnect();
	}
	elsif($new=~ /\_/g){				#*#*$&*#@##*(@#@)#(@)#@)++++===== LAST PAGE
		my @array=split '_',$new;
		my $id_low=$array[0];
		my $sth=$dbh->prepare("SELECT * FROM variables WHERE pdb_id=\"$array[0]\" AND mol_typ LIKE \"$array[1]\%\"");
                $sth->execute();
		my $nth=$dbh->prepare("SELECT * FROM constants WHERE pdb_id=\"$array[0]\"");
                $nth->execute();
		my $property=$nth->fetchrow_hashref();
print<<EndOfLast;
<table width=100% class="curvedEdges" cellspacing=10 style="border:2px solid #199">
<tr>
<td> 
        <ul>
<font align=left>
<br>
<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<!--                 USE OF DROPDOWN MENU             -->
<!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->

<!-- USE OF JAVA SCRIPT--- SO JAVA SCRIPT SHOULD BE ENABLED-->
<!--_____________________________________________________________________________________________________-->
<form class="dropDown">
<b><font color="#000000"><img src="http://127.0.0.1/HD-RNAS/view.png" border="0" height="25" width="20">&nbsp;&nbsp;View File &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></b>
<select onchange="window.open(this.value,'','');" style="background-color:#abf;">
        <option value="">Select Type</option>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/fasta.txt?structureIdList=$array[0]">FASTA</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].pdb">PDB (.txt)</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].PDB? headerOnly=YES">PDB(header only)</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].cif">mmCIF </OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].cif? headerOnly=YES">mmCIF(header only)</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].xml">XML</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].xml? headerOnly=YES">XML(header only)</OPTION>
</select>
</form>
<!---------------------------------------------------------------------------------------------------!-->
<!--USE OF DROP DOWN USING GO BUTTON!-->
<form>
<b><font color="#000000"><img src="http://127.0.0.1/HD-RNAS/down.png" border="0" height="20" width="20">&nbsp;&nbsp;Download File </font></b>
<select onchange="window.open(this.value,'','');" style="background-color:#abf;">
<option value="">Select Type</option>
<OPTION VALUE="http://www.rcsb.org/pdb/downloadFile.do?fileFormat=fasta&amp;compression=NO&amp;structureId=$array[0]">FASTA (.txt)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/downloadFile.do?fileFormat=pdb&amp;compression=NO&amp;structureId=$array[0]">PDB (.txt)</OPTION>
<OPTION value="http://www.rcsb.org/pdb/files/$array[0].pdb.gz">PDB format(.gz)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/downloadFile.do?headerOnly=YES&amp;fileFormat=pdb&amp;compression=NO&amp;structureId=$array[0]">PDB(header only)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/downloadFile.do?fileFormat=cif&amp;compression=NO&amp;structureId=$array[0]">mmCIF </OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].cif.gz">mmCIF(.gz)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/downloadFile.do?headerOnly=YES&amp;fileFormat=cif&amp;compression=NO&amp;structureId=$array[0]">mmCIF(header only)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/downloadFile.do?fileFormat=xml&amp;compression=NO&amp;structureId=$array[0]">XML</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].xml.gz">XML (.gz)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/downloadFile.do?headerOnly=YES&amp;fileFormat=xml&amp;compression=NO&amp;structureId=$array[0]">mmCIF(header only)</OPTION>">XML (header only)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/$array[0].pdb1.gz">Biological Assembly(gz)</OPTION>
</select>
</form>
<!--------------------------------------------------------------------------------------------------------->
<form class="dropDown">
<b><font color="#000000">External Links &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></b>
<select onchange="window.open(this.value,'','');" style="background-color:#abf;">
        <option value="">Select Type</option>
        <OPTION VALUE="http://www.cathdb.info/pdb/$id_low">CATH / Gene3D</OPTION>
        <OPTION VALUE="http://dx.doi.org/$property->{'doi'}">DOI</OPTION>
        <OPTION VALUE="http://ndbserver.rutgers.edu/service/ndb/atlas/summary?searchTarget=$id_low">NDB</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/explore/explore.do?structureId=$id_low">PDB</OPTION>
        <OPTION VALUE="http://www.ncbi.nlm.nih.gov/pubmed/$property->{'pubmed'}">PubMed </OPTION>
        <OPTION VALUE="http://scop.mrc-lmb.cam.ac.uk/scop/search.cgi?ver=1.75&key=$id_low">SCOP</OPTION>
</select>
</form>
<!--------------------------------------------------------------------------------------------------------->
<form class="dropDown" >
<b><font color="#000000">3D Visualization &nbsp;&nbsp;&nbsp;</font></b>
<select onchange="window.open(this.value,'','');" style="background-color:#abf;">
        <option value="">Select Type</option>
        <OPTION VALUE="http://rna.bgsu.edu/FR3D/AnalyzedStructures/$array[0]/index.html">FDR3</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/explore/jmol.do?structureId=$array[0]">PDB-Jmol</OPTION>
        <OPTION VALUE="http://www.proteopedia.org/wiki/index.php/$id_low">Proteopedia-Jmol</OPTION>
        <OPTION VALUE="http://scop.mrc-lmb.cam.ac.uk/scop/rsgen.cgi?chime=1;pd=$id_low;pr=a:8-338">SCOP-Chime </OPTION>
</select>
</form>
</br>
</br>


<!--________________________________________________________________________________________________________-->

<font color=#000000><b>PDB ID:</b></font> $property->{'pdb_id'}</br>
<font color=#000000><b>NDB ID:</b></font> $property->{'ndb_id'}</br>
<font color=#000000><b>RCSB ID:</b></font> $property->{'rcsb_id'}</br>
<font color=#000000><b>BMRB ID:</b></font> $property->{'bmrb_id'}</br>
<font color=#000000><b>Molecule Title:</b></font> $property->{'title'}</br>
<font color=#000000><b>Molecule Type:</b></font> $property->{'dscrptr'}</br>
<font color=#000000><b>Release Date:</b></font> $property->{'d_o_ent'}</br>
<font color=#000000><b>Revised Date:</b></font> $property->{'d_o_rev'}</br>
<font color=#000000><b>Experiment:</b></font> $property->{'expr'}</br>
<font color=#000000><b>Resolution:</b></font> $property->{'resol'}</br>
<font color=#000000><b>R-value:</b></font> $property->{'rval_free'}</br>
<font color=#000000><b>Primary Citation:</b></font> 
$property->{'pri_cit'} $property->{'journal'} , $property->{'issn'}, $property->{'page'}, $property->{'year'},$property->{'auth'}.</br></br>
<a href="http://nucleix.mbu.iisc.ernet.in/nuparm/nuparm.shtml"><b>Structural Parameter using : NUPARM<b></a>
<br><br>
<a href="http://www.saha.ac.in/biop/cgi-bin/bpfind_check.cgi?file=$array[0]"><b>Secondary Structural using : bpfind<b></a>

</font>
        </ul>
</td>


<td>
</br>
<center><table class="curvedEdges" cellspacing=10>
<tr>

<th width=20%><font color="#993366">Chain ID</font></th><th width=20%><font color="#993366">Length</font></th><th width=20%><font color="#993366">Type</font></th><th width=20%><font color="#993366">Source</font></th><th width=20%><font color="#993366">Sequence</font></th>
</tr>
EndOfLast
		while(my $ref =$sth->fetchrow_hashref()){
			my $id=$ref->{'mol_id'};
			$id=~ s/^.+_//;
print <<EndOfHTML;
<tr>
<th width=20%>$id</th><th width=20%>$ref->{'length'}</th><th width=20%>$ref->{'mol_typ'}</th><th width=20%><i>$ref->{'mol_org'}</i></th><th width=20%><a href=\"http://www.saha.ac.in/biop/cgi-bin/download?pdb=$array[0]&mol=$id\">Sequence</a></th>
</tr>
</center>

</font></th></tr>
EndOfHTML

		}
		print "</table></br></td>\n</table>\n</div>";
		$sth->finish();
                $dbh->disconnect();
	}
	else{						#*#*$&*#@##*(@#@)#(@)#@)++++===== FIRST & SECOND PAGE
##execute query
		my $sth=$dbh->prepare("SELECT mol_org, mol_typ FROM variables WHERE mol_typ LIKE \"$new%\" AND ec_no LIKE \"NULL\"");
		$sth->execute();

##iterate through resultset
		my %hash_typ=();
		print "<a href=\"#\">$new</a>";
		print "<ol style=\"line-height:1.5;\">";
		while(my $ref =$sth->fetchrow_hashref()){
			if($new eq "rRNA" ||$new eq "tRNA"){
				$hash_typ{$ref->{'mol_typ'}}+=1;
			}
			else{
				my $type=lc$ref->{'mol_org'};
				$hash_typ{$type}+=1;
			}
		}

		foreach my $k(sort keys %hash_typ){
			if($new eq "rRNA" ||$new eq "tRNA"){
                		print "<li><a href=\"./sql_level_1?id=$k\"><i><b>$k </b></i></a>( $hash_typ{$k} $new chain/s)</li>\n";
       		 	}
        		else{
				my $key=ucfirst$k;
				print "<li><a href=\"./sql_level_1?id=$new:$key\"><i><b>$key </b></i></a>( $hash_typ{$k} $new chain/s)</li>\n";
			}
		}

		$sth->finish();
		$dbh->disconnect();
print <<EOF;
</ol>
</div></centre>
EOF
	}
}

&FOOTER;

