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


my $cgi=new CGI();

my %input=();

&HEADER("Search Result");

&GOTO_top;

&main_toolbar;

&fetch_form_data;

&CHECKIO;

print <<EOH;
<center style="padding-top:3.5cm;"><div class="body">

EOH

#connect to database
#==================
my $dbh=DBI->connect("DBI:mysql:database=HDRNAS;host=localhost", "debasish", "debasish**2014",{'RaiseError'=>1});
if(exists $input{'pdb'}){
	my $id_list=$input{pdb};
	my @list=split /,\s*/,$id_list;
	my $url=url();
print<<Tab;
<div style="width: 100%; margin: 0 auto; padding: 20px 0 0px;">
        <ul class="tabs" data-persist="true">
Tab
        foreach my $id(@list){
	print "<li><a href=\"#$id\">$id</a></li>\n";
}
print"</ul>\n<div class=\"tabcontents\">\n";
	foreach my $id(@list){
		$id=uc$id;
		my $id_low=lc$id;
		my $cth=$dbh->prepare("SELECT * FROM constants WHERE pdb_id=\"$id\"");
		$cth->execute();
                my $con_prop=$cth->fetchrow_hashref();
		my $vth=$dbh->prepare("SELECT * FROM variables WHERE pdb_id=\"$id\"");
                $vth->execute();
		if(!$con_prop->{'pdb_id'}){
		print "<div id=\"$id\">";
		&return_error (500, "Data Not Available ",
     		"Sorry we do not have the data you are looking for ! Please Try again.</div>");
		}
		else{
		print "<div id=\"$id\">";
#=head
print<<EndOfLast;
<table width=100% class="curvedEdges" cellspacing=10>
<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=$id">FASTA</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$id.pdb">PDB (.txt)</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$id.PDB? headerOnly=YES">PDB(header only)</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$id.cif">mmCIF </OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$id.cif? headerOnly=YES">mmCIF(header only)</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$id.xml">XML</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/files/$id.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=$id">FASTA (.txt)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/downloadFile.do?fileFormat=pdb&amp;compression=NO&amp;structureId=$id">PDB (.txt)</OPTION>
<OPTION value="http://www.rcsb.org/pdb/files/$id.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=$id">PDB(header only)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/downloadFile.do?fileFormat=cif&amp;compression=NO&amp;structureId=$id">mmCIF </OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/$id.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=$id">mmCIF(header only)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/downloadFile.do?fileFormat=xml&amp;compression=NO&amp;structureId=$id">XML</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/$id.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=$id">mmCIF(header only)</OPTION>">XML (header only)</OPTION>
<OPTION VALUE="http://www.rcsb.org/pdb/files/$id.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/$con_prop->{'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/$con_prop->{'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/$id/index.html">FDR3</OPTION>
        <OPTION VALUE="http://www.rcsb.org/pdb/explore/jmol.do?structureId=$id">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> $con_prop->{'pdb_id'}</br>
<font color=#000000><b>NDB ID:</b></font> $con_prop->{'ndb_id'}</br>
<font color=#000000><b>RCSB ID:</b></font> $con_prop->{'rcsb_id'}</br>
<font color=#000000><b>BMRB ID:</b></font> $con_prop->{'bmrb_id'}</br>
<font color=#000000><b>Molecule Title:</b></font> $con_prop->{'title'}</br>
<font color=#000000><b>Molecule Type:</b></font> $con_prop->{'dscrptr'}</br>
<font color=#000000><b>Release Date:</b></font> $con_prop->{'d_o_ent'}</br>
<font color=#000000><b>Revised Date:</b></font> $con_prop->{'d_o_rev'}</br>
<font color=#000000><b>Experiment:</b></font> $con_prop->{'expr'}</br>
<font color=#000000><b>Resolution:</b></font> $con_prop->{'resol'}</br>
<font color=#000000><b>R-value:</b></font> $con_prop->{'rval_free'}</br>
<font color=#000000><b>Primary Citation:</b></font> 
$con_prop->{'pri_cit'} $con_prop->{'journal'} , $con_prop->{'issn'}, $con_prop->{'page'}, $con_prop->{'year'},$con_prop->{'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=$id"><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 $ver_prop=$vth->fetchrow_hashref()){
        my $idm=$ver_prop->{'mol_id'};
        $idm=~ s/^\w+_//;
        print "<tr><th>$idm</th><th>$ver_prop->{'length'}</th><th>$ver_prop->{'mol_typ'}</th><th>$ver_prop->{'mol_org'}</th><th><a href=\"../../cgi-bin/download?pdb=$id&mol=$idm\">sequence</a></th></tr>";
}

print "</table></br></td>\n</table>\n</div>";
		}
	$cth->finish();
        $vth->finish();
	}
print"</div></div></div></centre>";
$dbh->disconnect();
}
if(exists $input{'org'}){
	my $org=$input{'org'};
	my $part1=substr($org,0,1);
	my $part2=$org;
	$part2=~ s/\w+\.?\s//;
	$org="$part1".'%'."$part2\%";
	my $vth=$dbh->prepare("SELECT mol_id, pdb_id, length, mol_typ FROM variables WHERE mol_org LIKE \"$org\"");
        $vth->execute();
	my %hash_org_id=();
	my %hash_length=();
	my %mol_des=();
        print <<EndOfTBL;
<center><table width=100% class=curvedEdges cellspacing=10 style="border:2px solid #199">
<tr>
<th width=10%><font color="#993366">PDB ID</font></th><th width=20%><font color="#993366">Mol ID</font></th><th width=20%><font color="#993366">Length</font></th><th width=40%><font color="#993366">Molecule Description</font></th><th width=10%><font color="#993366">Resolution in &#197;</font></th>
EndOfTBL
	while(my $ver_prop=$vth->fetchrow_hashref()){
		my $ida=$ver_prop->{'mol_id'};
		$ida=~ s/\w+_//;
		$hash_org_id{$ver_prop->{'pdb_id'}}.=", $ida";
		$hash_length{$ver_prop->{'pdb_id'}}.=", $ver_prop->{'length'}";
		$mol_des{$ver_prop->{'pdb_id'}}.=", $ver_prop->{'mol_typ'}";

	}
	foreach my $k(keys %hash_org_id){
		my $val=$hash_org_id{$k};
		$val=~ s/^,\s//;
		my $val_len=$hash_length{$k};
		$val_len=~ s/^,\s//;
		my $des=$mol_des{$k};
		$des=~ s/^,\s//;
		my $cth=$dbh->prepare("SELECT * FROM constants WHERE pdb_id=\"$k\"");
	        $cth->execute();
		while(my $con_prop=$cth->fetchrow_hashref()){
			print "<tr><th><a href=\"./sql_search_dtbs?pdb=$k\">$k</a></th><th> $val</th><th> $val_len </th><th> $des </th><th> $con_prop->{'resol'}</th></tr>";
		}
		$cth->finish();
	}
	print "</tr></table>";
        $vth->finish();
	print"</div>";
}
if(exists $input{"rdoSelect"}){
	my $fun_rna=$input{"rdoSelect"};
	if($fun_rna eq "tRNA"){
		$fun_rna.="-$input{'tRNA'}";
	}
	if($fun_rna eq "rRNA" ){
                $fun_rna.="-$input{'rRNA'}";
	}
	my $vth=$dbh->prepare("SELECT mol_id, pdb_id, length, mol_org FROM variables WHERE mol_typ LIKE \"$fun_rna\%\"");
        $vth->execute();
	my %hash_org_id=();
	my %hash_length=();
        my %mol_des=();
	print <<EndOfTBL;
<center><table width=100% class=curvedEdges cellspacing=10 style="border:2px solid #199">
<tr>
<th width=10%><font color="#993366">PDB ID</font></th><th width=20%><font color="#993366">Mol ID</font></th><th width=20%><font color="#993366">Length</font></th><th width=40%><font color="#993366">Organism</font></th><th width=10%><font color="#993366">Resolution in &#197;</font></th>
EndOfTBL
       	while(my $ver_prop=$vth->fetchrow_hashref()){
		print $ver_prop->{'mol_id'};
		my $ida=$ver_prop->{'mol_id'};
                $ida=~ s/\w+_//;
       	        $hash_org_id{$ver_prop->{'pdb_id'}}.=", $ida";
              	$hash_length{$ver_prop->{'pdb_id'}}.=", $ver_prop->{'length'}";
                $mol_des{$ver_prop->{'pdb_id'}}.=", $ver_prop->{'mol_org'}";
	}
	foreach my $k(keys %hash_org_id){
                my $val=$hash_org_id{$k};
       	        $val=~ s/^,\s//;
               	my $val_len=$hash_length{$k};
                $val_len=~ s/^,\s//;
       	        my $des=lc$mol_des{$k};
               	$des=~ s/^,\s//;
		$des=ucfirst$des;
                my $cth=$dbh->prepare("SELECT * FROM constants WHERE pdb_id=\"$k\"");
       	        $cth->execute();
               	while(my $con_prop=$cth->fetchrow_hashref()){
                       	print "<tr><th><a href=\"./sql_search_dtbs?pdb=$k\">$k</a></th><th> $val</th><th> $val_len </th><th> <i>$des</i> </th><th> $con_prop->{'resol'}</th></tr>";
               	}
		$cth->finish();
       	}
        print "</tr></table>";
        $vth->finish();
	print  "</div>";
}
if(exists $input{"seq"}){
	my $qseq=$input{"seq"};
	$qseq="%$qseq%";
        my $vth=$dbh->prepare("SELECT mol_id, pdb_id, length, mol_org,mol_typ FROM variables WHERE mol_fasta LIKE \"$qseq\"");
	$vth->execute();
        my %hash_org_id=();
        my %hash_length=();
        my %mol_des=();
	my %mol_typ=();
        print <<EndOfTBL;
<center><table width=100% class=curvedEdges cellspacing=10 style="border:2px solid #199">
<tr>
<th width=10%><font color="#993366">PDB ID</font></th><th width=20%><font color="#993366">Mol ID</font></th><th width=20%><font color="#993366">Length</font></th><th width=20%><font color="#993366">Molecule Type</font></th><th width=20%><font color="#993366">Organism</font></th><th width=10%><font color="#993366">Resolution in &#197;</font></th>
EndOfTBL
        while(my $ver_prop=$vth->fetchrow_hashref()){
                my $ida=$ver_prop->{'mol_id'};
                $ida=~ s/\w+_//;
                $hash_org_id{$ver_prop->{'pdb_id'}}.=", $ida";
                $hash_length{$ver_prop->{'pdb_id'}}.=", $ver_prop->{'length'}";
                $mol_des{$ver_prop->{'pdb_id'}}.=", $ver_prop->{'mol_org'}";
		$mol_typ{$ver_prop->{'pdb_id'}}.=", $ver_prop->{'mol_typ'}";
        }
        foreach my $k(keys %hash_org_id){
                my $val=$hash_org_id{$k};
                $val=~ s/^,\s//;
                my $val_len=$hash_length{$k};
                $val_len=~ s/^,\s//;
                my $des=lc$mol_des{$k};
                $des=~ s/^,\s//;
                $des=ucfirst$des;
		my $typ=lc$mol_typ{$k};
                $typ=~ s/^,\s//;
                my $cth=$dbh->prepare("SELECT * FROM constants WHERE pdb_id=\"$k\"");
                $cth->execute();
                while(my $con_prop=$cth->fetchrow_hashref()){
                        print "<tr><th><a href=\"./sql_search_dtbs?pdb=$k\">$k</a></th><th> $val</th><th> $val_len </th><th> $typ </th><th> <i>$des</i> </th><th> $con_prop->{'resol'}</th></tr>";
                }
		 $cth->finish();
        }
        print "</tr></table>";
        $vth->finish();
        print  "</div>";

}
if(exists $input{"nddt"} || exists $input{"stdt"}){
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	my $yyyymmdd = sprintf "%.4d-%.2d-%.2d", $year+1900, $mon+1, $mday;
	my $strt=$input{"stdt"}||'1978-04-12';
	my $end=$input{"nddt"}||$yyyymmdd;
	$strt=~ s/\//\-/g;
	$end=~ s/\//\-/g;
	my $cth=$dbh->prepare("SELECT pdb_id, resol, d_o_rev, d_o_ent FROM constants WHERE d_o_ent BETWEEN \"$strt\" AND \"$end\" ORDER BY d_o_ent DESC");
        $cth->execute();
	print <<EndOfTBL;
<center><table width=100% class=curvedEdges cellspacing=10 style="border:2px solid #199">
<tr>
<th width=10%><font color="#993366">PDB ID</font></th><th width=20%><font color="#993366">Date of Entry</font></th><th width=20%><font color="#993366">Date of Revision</font></th><th width=10%><font color="#993366">Resolution in &#197;</font></th>
EndOfTBL
	while(my $con_prop=$cth->fetchrow_hashref()){
		print "<tr><th><a href=\"./sql_search_dtbs?pdb=$con_prop->{'pdb_id'}\">$con_prop->{'pdb_id'}</a></th><th> $con_prop->{'d_o_ent'}</th><th> $con_prop->{'d_o_rev'} </th><th> $con_prop->{'resol'}</th></tr>";
	}
	$cth->finish();
	print "</tr></table>";
        print  "</div>";
}
if(exists $input{"auth"}){
	my $author=$input{"auth"};
	$author="\%$author%";
	my $cth=$dbh->prepare("SELECT pdb_id, resol, d_o_rev, d_o_ent, journal, auth FROM constants WHERE auth LIKE \"$author\"");
	$cth->execute();
print <<EndOfTBL;
<center><table width=100% class=curvedEdges cellspacing=10 style="border:2px solid #199">
<tr>
<th width=10%><font color="#993366">PDB ID</font></th><th width=40%><font color="#993366">Author List</font></th><th width=10%><font color="#993366">Journal</font></th><th width=15%><font color="#993366">Date of Entry</font></th><th width=15%><font color="#993366">Date of Revision</font></th><th width=10%><font color="#993366">Resolution in &#197;</font></th>
EndOfTBL
        while(my $con_prop=$cth->fetchrow_hashref()){
                print "<tr><th><a href=\"./sql_search_dtbs?pdb=$con_prop->{'pdb_id'}\">$con_prop->{'pdb_id'}</a></th><th> $con_prop->{'auth'}</th><th> $con_prop->{'journal'}</th><th> $con_prop->{'d_o_ent'}</th><th> $con_prop->{'d_o_rev'} </th><th> $con_prop->{'resol'}</th></tr>";
        }
        $cth->finish();
        print "</tr></table>";
        print  "</div>";
}
if(exists $input{"minres"} || exists $input{"maxres"}){
	my $l_res=$input{"minres"} || '0';
	my $h_res=$input{"maxres"} || '10';
	 my $cth=$dbh->prepare("SELECT pdb_id, resol, d_o_rev, d_o_ent, journal, auth FROM constants WHERE resol < \"$h_res\" AND resol > \"$l_res\"");
        $cth->execute();
print <<EndOfTBL;
<center><table width=100% class=curvedEdges cellspacing=10 style="border:2px solid #199">
<tr>
<th width=10%><font color="#993366">PDB ID</font></th><th width=40%><font color="#993366">Author List</font></th><th width=10%><font color="#993366">Journal</font></th><th width=15%><font color="#993366">Date of Entry</font></th><th width=15%><font color="#993366">Date of Revision</font></th><th width=10%><font color="#993366">Resolution in &#197;</font></th>
EndOfTBL
        while(my $con_prop=$cth->fetchrow_hashref()){
                print "<tr><th><a href=\"./sql_search_dtbs?pdb=$con_prop->{'pdb_id'}\">$con_prop->{'pdb_id'}</a></th><th> $con_prop->{'auth'}</th><th> $con_prop->{'journal'}</th><th> $con_prop->{'d_o_ent'}</th><th> $con_prop->{'d_o_rev'} </th><th> $con_prop->{'resol'}</th></tr>";
        }
        $cth->finish();
        print "</tr></table>";
        print  "</div>";
}
else{
}
&FOOTER;
#----------------------------------
sub fetch_form_data {
for my $key ( $cgi->param() ) {
    $input{$key} = $cgi->param($key);
}
}
#------------------------------------------
sub CHECKIO {
foreach my $key (keys %input) {
    if ($input{$key}  =~ /[;><&\*\`\|]/ ) {
      &return_error (500, "The SINP data checker ",
      "Sorry your illegal characters have been caught and discarded.");
    }
}
}
#------------------------------------------
sub return_error
{
my $status=$_[0];
my $keyword=$_[1];
my $message=$_[2];
my $webmaster = '<i><font color=#33CCFF>dhananjay.bhattacharyya[AT]saha.ac.in</font></i>';


    print <<End_of_Error;
<center>
<h1 style="color:red;">$keyword</h1>
<hr>$message</hr>
Please contact $webmaster for more information.
</center>
End_of_Error

    exit(1);
}
1;

