#!/usr/bin/python

import pickle

aa_dictfile=open('aa_dict.txt', 'r')
aa_dict = pickle.load(aa_dictfile)
aa_dictfile.close()

amino_acid_properfile=open('amino_acid_proper.txt', 'r')
amino_acid_proper = pickle.load(amino_acid_properfile)
amino_acid_properfile.close()


dbfile=open('aa_database.txt', 'r')
dbfile.close()


import cgi
form = cgi.FieldStorage()
code = form.getvalue("code")
figure = form.getvalue("figure")

code=code.upper()

print "Content-type: text/html"      # HTML is following
print

print """
<HTML>

<HEAD>
<TITLE> The Structure of Amino Acids </TITLE></HEAD>

<LINK rel="stylesheet" href="/gv/students/mtom/selstyle.css">
<script language = "JavaScript1.2" href="/gv/students/mtom/selscript.js"></script>

</HEAD>
<BODY BGCOLOR="white">
<center><h2> The Structure of Amino Acids </h2></center>
<TABLE WIDTH="100%" CELLSPACING="0" BORDER="1">
    
    <TR><TD WIDTH="450" ROWSPAN="3">
    
"""

if aa_dict.has_key(code):
    if figure == "mol":
        print """
                 <applet name='jmola' code='JmolApplet' archive='/gv/students/mtom/jmol-10.2.0/JmolApplet0.jar,/gv/students/mtom/jmol-10.2.0/JmolApplet1.jar,/gv/students/mtom/jmol-10.2.0/JmolApplet2.jar,/gv/students/mtom/jmol-10.2.0/JmolApplet3.jar,/gv/students/mtom/jmol-10.2.0/JmolApplet4.jar,/gv/students/mtom/jmol-10.2.0/JmolApplet5.jar,/gv/students/mtom/jmol-10.2.0/JmolApplet6.jar'
        width='450' height='450' align='bottom' mayScript='true'>
                  <param name='progressbar' value='true'>
		  
                  <param name='script' value='load /gv/students/mtom/%s.mol;wireframe 30;spacefill 90;select hydrogen;spacefill 70;zoom 160;select *;set specular on;set background lawngreen;rotate x 60;set frank off'> 
                  </applet>
        """ % (aa_dict[code])
    else:
        print '<img src="/gv/students/mtom/%s.gif" width="350" height="350">' % aa_dict[code]
else:
    print "<center><h3> Wrong amino acid code</h3></center>"

if aa_dict.has_key(code) and amino_acid_proper.has_key(code):
    print """
	   </TD> 

	   <TD BGCOLOR="#CCCCFF"><p><font face="arial"><b>Enter the one-letter or the three-letter code of an amino acid </b></font></p>
              <form name ="control" ACTION="aa_load.py" ENCTYPE="multipart/form-data" METHOD="POST">
              <input type=text size=18 name="code" value=""> <br><br>
	      <input type="radio" name="figure" value="mol" > 3D 
              <input type="radio" checked name="figure" value="gif" >2D <br><br> 
              <input type="submit" name="submit" value="submit"> 
	      </form>
	   </TD></TR>
	<TR><TD BGCOLOR="#CCCCFF">Amino acid: %s<br><br> The properties: %s
	  </TD>
	</TR>
    </TABLE>
    </BODY>
    </HTML>
    """ % (aa_dict[code], amino_acid_proper[code])
else:
    print """
	   </TD> 

	   <TD BGCOLOR="#CCCCFF"><p><font face="arial"><b>Enter the one-letter or the three-letter code of an amino acid </b></font></p>
              <form name ="control" ACTION="aa_load.py" ENCTYPE="multipart/form-data" METHOD="POST">
              <input type=text size=18 name="code" value=""> <br><br>
	      <input type="radio" name="figure" value="mol" > 3D 
              <input type="radio" checked name="figure" value="gif" >2D <br><br> 
              <input type="submit" name="submit" value="submit"> 
	      </form>
	   </TD></TR>
	<TR><TD BGCOLOR="#CCCCFF">Amino acid: no amino acid selected<br><br> The properties: no amino acid selected<br><br>
	  </TD>
	</TR>
    </TABLE>
    </BODY>
    </HTML>
    """
















