#! /usr/bin/python

import cgi, pg, re, string
from pg import DB

buche=pg.DB(dbname='buche_4.8', user='postgres')
 
numeri=['1','2','3','4','5','6','7','8','9','0']

def header(title):
	print 'Content-Type: text/html\n'
        print '<HTML>\n<HEAD>\n<TITLE>%s\n</TITLE>\n<STYLE type=\"text/css\">p { font-family: \'Times New Roman\', \'Times\', \'serif\'; font-size:13px; font-style:normal; font-weight; normal; line-height: normal; text-transform: none; text-decoration: none} </STYLE>\n</HEAD>\n<BODY BGCOLOR=\"#ffffff\" TEXT=\"#311469\" LINK=\"0052a4\" vlink=\"800080\">\n' % (title)
def footer():
	print '</BODY></HTML>'

form=cgi.FieldStorage()
formkeys=form.keys()


header('Verify PDB code')

print '<TABLE FRAME=\"void\" BORDER=\"0\"><TR><TD><A HREF=main.htm><IMG src=logo.jpg BORDER=\"0\"></A></TD></TR></TABLE><P>'  

for k in formkeys:

#### controlla validita' del codice immesso  ####
	if form[k].value=='submit':
		pdbcode=string.strip(k)
	elif k=='pdbcode':
		link='http://www.rcsb.org/pdb/cgi/explore.cgi?pid=208991029170421&page=0&pdbId='+string.upper(form[k].value)
		print '<P>The submitted code is <A HREF=',link,'TARGET=\"_blank\">',form[k].value,'</A></P>'
		pdbcode=form[k].value
	else:
		break
	if len(pdbcode)!=4 or pdbcode[0] not in numeri:
       	        print '<P>Please, check the PDB code <BR></P>' 
               	chain_id='errore'
		print '<P><A HREF=\'form_pdbcode.htm\'>Retry</A></P>'
        else:
       	        check=buche.query('select pdb.oid, pdb.header, pdb.compnd, pdb.source, pdb.author, pdb.title, pdb.keywds from pdb where pdb.name=\''+pdbcode+'\'').getresult()
               	if check:
			print '<TABLE FRAME=\"void\" BORDER=\"1\" WIDTH=\"600\">'
		        if check[0][1]:
		                print '<TR><TH bgcolor=\'CCCCFF\'>Header:</TD><TD><P>',re.sub(';','<BR>',(re.sub('\$','',check[0][1]))),'</P></TD></TR>'
        		if check[0][2]:
		                print '<TR><TH bgcolor=\'CCCCFF\'>Compound:</TD><TD><P>',re.sub(';','<BR>',(re.sub('\$','',check[0][2]))),'</P></TD></TR>'
		        if check[0][3]:
		                print '<TR><TH bgcolor=\'CCCCFF\'>Source:</TD><TD><P>',re.sub(';','<BR>',(re.sub('\$','',check[0][3]))),'</P></TD></TR>'
		        if check[0][4]:
		                print '<TR><TH bgcolor=\'CCCCFF\'>Author:</TD><TD><P>',re.sub(';','<BR>',(re.sub('\$','',check[0][4]))),'</P></TD></TR>'
		        if check[0][5]:
		                print '<TR><TH bgcolor=\'CCCCFF\'>Title:</TD><TD><P>',re.sub(';','<BR>',(re.sub('\$','',check[0][5]))),'</P></TD></TR>'
		        if check[0][6]:
		                print '<TR><TH bgcolor=\'CCCCFF\'>Keywords:</TD><TD><P>',re.sub(';','<BR>',(re.sub('\$','',check[0][6]))),'</P></TD></TR>'
#        		print '</TABLE>'
			if check[0][0]:
				pdboid=check[0][0]
				catene=buche.query('select chains.name, chains.oid from chains where chains.pdb_id=\''+"%s"%(pdboid)+'\' order by chains.name').getresult()
				if not catene:
					print '<P>Something wrong happened, sorry</P><BR>'
					print '<A HREF=\'form_pdbcode.htm\'>Try</A> a different PDB code <BR>'
				else:	
					technique=buche.query('select chains.exp, chains.rid1 where chains.pdb_id='+"%s"%(pdboid)+' and chains.name=\''+string.strip(catene[0][0])+'\'').getresult()
					if technique[0][0]=='X':
						print '<TR><TH bgcolor=\'CCCCFF\'>Experimental Technique:</TD><TD><P>X-RAY DIFFRACTION</P></TD></TR>'
	                                	print '</TABLE>'
                                        elif technique[0][0]=='N':
                                                print '<TR><TH bgcolor=\'CCCCFF\'>Experimental Technique:</TD><TD><P>NMR</P></TD></TR>'
                                                print '</TABLE>'
					print '<P><TABLE FRAME=\"box\" BORDER=\"1\" WIDTH=\"100\">'
					print '<TR bgcolor=\'#CCCCFF\'><TH>Chain</TH><TH>Redundancy</TH><TH>Representative redundancy group member</TH><TH>Submit</TH></TR>'
					for i in range(len(catene)):
#						print check[0][0],catene[i][0],pdboid
						print '<TR><TD align=\'center\' bgcolor=\'#C0EAC0\'>',catene[i][0],'</TD>'
						catena=buche.query('select chains.groupid1, chains.rid1, chains.oid, chains.exp, chains.type from chains where chains.pdb_id='+"%s"%(pdboid)+' and chains.name=\''+string.strip(catene[i][0])+'\'').getresult()
#				        	catene=buche.query('select chains.groupid1, chains.rid1, chains.oid from chains where chains.pdb_id='+"%s"%(pdboid)+' and chains.name=\''+catena+'\'').getresult()
						if catena[0][1]=='f':
					                print '<TD align=\'center\'>Not representative</TD>'
               						scelta=buche.query('select pdb.name, chains.pdb_id, chains.name, chains.rid1, chains.oid from pdb, chains where chains.rid1=\'t\' and chains.groupid1=\''+catena[0][0]+'\' and pdb.oid=chains.pdb_id and chains.exp!=\'N\'').getresult()
							if scelta:
								print '<TD align=\'center\'>','<A HREF=http://www.rcsb.org/pdb/cgi/explore.cgi?pid=208991029170421&page=0&pdbId='+scelta[0][0]+' TARGET=\"_blank\">',scelta[0][0],'</A>',scelta[0][2],'</TD>'
					        	        check=buche.query('select pdb.oid, pdb.name, pdb.header, pdb.compnd, pdb.source, pdb.author, pdb.title, pdb.keywds from pdb where pdb.name=\''+scelta[0][0]+'\'').getresult()
               							chain_id=catene[i][1]
					        	        print '<FORM action=\'mostra_buche.py\' method=\'POST\'>'
       	        						print '<INPUT type=\'hidden\' name=\'pdbcode\' value=\'',scelta[0][0],'\'>'
					        	        print '<INPUT type=\'hidden\' name=\'pdboid\' value=\'',pdboid,'\'>'
						                print '<INPUT type=\'hidden\' name=\'chain_id\' value=\'',scelta[0][4],'\'>'
					        	        print '<INPUT type=\'hidden\' name=\'chain_name\' value=\'',scelta[0][2],'\'>'
			        		        	print '<TD><INPUT type=\'submit\' name=\'procedi\' value=\'Submit\'></TD>'
								print '</FORM>'
							else:
								print '<TD align=\'center\'>No representative member available</TD><TD></TD>'
				 	       	else:
							print '<TD>Representative</TD><TD align=\'center\'><A HREF=http://www.rcsb.org/pdb/cgi/explore.cgi?pid=208991029170421&page=0&pdbId='+pdbcode+' TARGET=\"_blank\">',pdbcode,'</A>',catene[i][0]+'</TD>'
               						chain_id=catene[i][1]
					                print '<FORM action=\'mostra_buche.py\' method=\'POST\'>'
					                print '<INPUT type=\'hidden\' name=\'pdbcode\' value=\'',pdbcode,'\'>'
			        		        print '<INPUT type=\'hidden\' name=\'pdboid\' value=\'',check[0][0],'\'>'
			                		print '<INPUT type=\'hidden\' name=\'chain_id\' value=\'',catene[i][1],'\'>'
			                		print '<INPUT type=\'hidden\' name=\'chain_name\' value=\'',catene[i][0],'\'>'
				        	        print '<TD><INPUT type=\'submit\' name=\'proceed\' value=\'Submit\'></TD>'
							print '</FORM>'
#						if catena[0][4]=='int':
#							print '<TD>Interesting</TD>'
#							chain_id=catene[i][1]
#                                                        print '<FORM action=\'mostra_buche.py\' method=\'POST\'>'
#                                                        print '<INPUT type=\'hidden\' name=\'pdbcode\' value=\'',pdbcode,'\'>'
#							print '<INPUT type=\'hidden\' name=\'pdboid\' value=\'',pdboid,'\'>'
#							print '<INPUT type=\'hidden\' name=\'chain_id\' value=\'',catene[i][1],'\'>'
#							print '<INPUT type=\'hidden\' name=\'chain_name\' value=\'',catene[i][0],'\'>'
#							print '<TD><INPUT type=\'submit\' name=\'proceed\' value=\'Submit\'></TD></TR>'
#							print '</FORM>'
#						else:
#							print '</TR>'
#					elif technique[0][0]=='N' and technique[0][1]=='f':
#						print '<TR><TH bgcolor=\'CCCCFF\'>Experimental Technique:</TD><TD><P>NMR</P></TD></TR>'
#						print '</TABLE>'
#                                               print '<P><TABLE FRAME=\"box\" BORDER=\"1\" WIDTH=\"100\">'
					print '</TABLE></P>'
		else:
			print '<P>The submitted code is not present in the used PDB release<BR></P>'
			print '<P><A HREF=\'form_pdbcode.htm\'>Try</A> a different PDB code <BR></P>'

print '<TABLE><TR><TD><IMG src=linea.jpg></TD></TR></TABLE>'
print 'mail to: <A HREF=mailto:surface@cbm.bio.uniroma2.it>surface@cbm.bio.uniroma2.it</A>'
print '<P>'
print 'go to the<A HREF=\'main.htm\'> home page </A>'
print '</P>'

footer()