#! /usr/bin/python
# takes search term as command line argument, returns html documents 

from task2module import *

# takes the search term from a command line argument
inputterm = takeargument(1,"Please enter an input term")

# gets xml page of gis from eutils esearch
listofgis = esearch(inputterm)

# creates list of gis from xml file
output = []
for line in listofgis.readlines():
	id = re.search(r'<Id>(.*)</Id>',line)
	if id:
		output.append(id.group(1))

# prints some debugging information to terminal
for item in output:
	print "Found GID: " + item

# creates a blank index page
handle = createblankpage(inputterm)

# takes each gi and scrapes gets its xml file
# scrapes xml for information, creating an object of type "protein"
# and makes an html page
for biscuits in output:
	proteininfo = efetch(biscuits)
	sequence = xmlsearch("IUPACaa",proteininfo)
	name = xmlsearch("Seqdesc_title",proteininfo)
	organism = xmlsearch("Org-ref_taxname",proteininfo)
	pdbid = xmlsearch("PDB-mol-id", proteininfo)
	if pdbid != "no information available":
		structureid = structuresearch(pdbid)
		if structureid: 
			image = "http://ncbi.nlm.nih.gov/Structure/mmdb/mmdbimage.fcgi?&id="+structureid
			print "Structure image found for "+name
	else:
		image = None
	print "Creating HTML page for "+name
	thisobject = protein(biscuits,name,sequence,organism,pdbid,image)
	page = createpage(inputterm,thisobject)
# adds to index page. By doing this dynamically with each page created, the program falls over gracefully if it doesn't complete
	addtoindex(page,handle)

#adds end tags to index page
print "Finalising index page, " +inputterm+".html"
handle.write(bottom)

