#!/usr/bin/python
## All this code is copyright Ramon Diaz-Uriarte. For security reasons, this is for
## now confidential. No license is granted to copy, distribute, or modify it.
## Once everything is OK, it will be distributed under the GPL.
import sys
import os
import cgi
import types
import time
import shutil
import string
import signal
import re
import glob
import tarfile
import cgitb
cgitb.enable() ## zz: eliminar for real work?
sys.stderr = sys.stdout ## eliminar?
R_MAX_time = 12 * 3600 ## 4 hours is max duration allowd for any process
## For redirections, from Python Cookbook
def printPalsURLADaCGH(newDir, application_url = "http://adacgh.bioinfo.cnio.es"):
""" Based on Pomelo II's Send_to_Pals.cgi."""
f=open("idtype")
idtype = f.read().strip()
f.close()
f=open("organism")
organism = f.read().strip()
f.close()
if (idtype != "None" and organism != "None"):
url_org_id = "org=" + organism + "&idtype=" + idtype + "&"
else:
url_org_id = ""
gl_base = application_url + '/tmp/' + newDir + '/'
gl1 = gl_base + 'Lost_for_PaLS.txt'
gl2 = gl_base + 'Gained_for_PaLS.txt'
gl3 = gl_base + 'Gained_or_Lost_for_PaLS.txt'
outstr0 = '
Send set of genes with copy number LOSS to PaLS
' + \ 'Send set of genes with copy number GAIN to PaLS
' + \ 'Send set of genes with copy number ALTERATION (either gain or loss) to PaLS
' return(outstr) def pdf2html(rootname, tmpDir, outf, compressedFile, maxsthumb = 350): """ From a multipage pdf obtain jpegs; the thumbnails are inserted in the webpage, the large jpeg viewed on clik. rootname is all the stuff before the `pdf', tmpDir is the directory where the files live, maxsthumb max size of thumbnail and outf the html file. We also decrease the size of the jpeg for showing. Finally, we add the generated jpeg to the compressed file""" mst = str(maxsthumb) mst2 = str(1600) os.chdir(tmpDir) os.system('/usr/bin/pdftoppm ' + rootname + '.pdf tmpppms') tmps = glob.glob('tmpppms*.ppm') for fignum in range(len(tmps)): os.system('/usr/bin/ppmtojpeg ' + tmps[fignum] + ' > ' + rootname + '.' + str(fignum + 1) + '.jpeg') os.system('/usr/bin/convert -size ' + mst + 'x' + mst + ' ' + rootname + '.' + str(fignum + 1) + '.jpeg' + ' -resize ' + mst + 'x' + mst + ' thumb.' + rootname + '.' + str(fignum + 1) + '.jpeg') os.system('/usr/bin/convert ' + rootname + '.' + str(fignum + 1) + '.jpeg' + ' -resize ' + mst2 + 'x' + mst2 + ' ' + rootname + '.' + str(fignum + 1) + '.jpeg') outf.write(' \n') # compressedFile.add(rootname + '.' + str(fignum + 1) + '.jpeg', # rootname + '.' + str(fignum + 1) + '.jpeg') os.chdir('/http/adacgh/cgi') def thumb(tmpDir, fnames, outf, maxsthumb = 350): """ From a set of pngs, obtain thumbnails and add links to html. The thumbnails are inserted in the webpage, the large png viewed on clik. tmpDir is the directory where the files live, fnames is a list with the base file names to process maxsthumb max size of thumbnail and outf the html file. """ mst = str(maxsthumb) os.chdir(tmpDir) for bname in fnames: os.system(''.join(['/usr/bin/convert ', bname, '.png', ' -resize ', mst, 'x', mst, ' thumb.', bname, '.jpeg'])) outf.write(''.join([' '])) os.chdir('/http/adacgh/cgi') def getQualifiedURL(uri = None): """ Return a full URL starting with schema, servername and port. *uri* -- append this server-rooted uri (must start with a slash) """ schema, stdport = ('http', '80') host = os.environ.get('HTTP_HOST') if not host: host = os.environ.get('SERVER_NAME') port = os.environ.get('SERVER_PORT', '80') if port != stdport: host = host + ":" + port result = "%s://%s" % (schema, host) if uri: result = result + uri return result def getScriptname(): """ Return te scriptname part of the URL.""" return os.environ.get('SCRIPT_NAME', '') # def getPathinfo(): # """ Return the remaining part of the URL. """ # pathinfo = os.environ.get('PATH_INFO', '') # return pathinfo def getBaseURL(): """ Return a fully qualified URL to this script. """ return getQualifiedURL(getScriptname()) def commonOutput(): print "Content-type: text/html\n\n" print """This is an autorefreshing page; your results will eventually be displayed here.\n' print 'If your browser does not autorefresh, the results will be kept for five days at
' print '', 'http://adacgh.bioinfo.cnio.es/tmp/'+ newDir + '/results.html.' print '
' ## Output-generating functions def printErrorRun(): Rresults = open(tmpDir + "/results.txt") resultsFile = Rresults.read() errormsg = open(tmpDir + "/error.msg").read() outf = open(tmpDir + "/pre-results.html", mode = "w") outf.write("This could be a bug on our code, or a problem ") outf.write("with your data (that we hadn't tought of). Below is all the output from the execution ") outf.write("of the run. Unless it is obvious to you that this is a fault of your data ") outf.write("(and that there is no way we could have avoided the crash) ") outf.write("please let us know so we can fix the problem. ") outf.write("Please sed us this URL and the output below
") outf.write("This is the results file:
") outf.write("
") outf.write(cgi.escape(resultsFile)) outf.write("") outf.write("
And this is the error message:
") outf.write("
") outf.write(cgi.escape(errormsg)) outf.write("") outf.write("") outf.close() Rresults.close() shutil.copyfile(tmpDir + "/pre-results.html", tmpDir + "/results.html") def printOKRun(): Rresults = open(tmpDir + "/results.txt") resultsFile = Rresults.read() outf = open(tmpDir + "/pre-results.html", mode = "w") outf.write("
") outf.write('") outf.write("") outf.close() Rresults.close() shutil.copyfile(tmpDir + "/pre-results.html", tmpDir + "/results.html") else: outf.write('Results (help)
\n') outf.write("
") outf.write(cgi.escape(resultsFile)) outf.write("
Click on thumbnails to expand.
') outf.write('Smoothed values for all genes/clones are available from file' + ' "CBS.results.txt".
') outf.write('Click on thumbnails to expand.
') outf.write('Smoothed values for all genes/clones are available from file' + ' "Wavelets.results.txt".
') outf.write('