#!/usr/bin/python
import os
import sys;
import cgi
import tempfile
import string
import efp, efpXML, efpConfig, efpService
import re
import subprocess
# Open a file
fo = open("writefile.txt", "r+")
groupPermission = fo.read();
# Close opend file
fo.close()
form = cgi.FieldStorage(keep_blank_values=1)
error = 0
errorStrings = []
alertStrings = []
testalertStrings = []
orthologStrings = {}
alignOrthologs = {}
lowAlert = 0
sdAlert = 0
primaryProbeset = None
secondaryProbeset = None
img = None
imgMap = {}
imgFilename = {}
tableFile = {}
views = []
# Retrieve cgi inputs
dataSource = form.getvalue("dataSource")
primaryGene = form.getvalue("primaryGene")
secondaryGene = form.getvalue("secondaryGene")
thresholdIn = form.getvalue("threshold")
mode = form.getvalue("modeInput")
useThreshold = form.getvalue("override")
grey_low = form.getvalue("modeMask_low")
grey_stddev = form.getvalue("modeMask_stddev")
ncbi_gi = form.getvalue("ncbi_gi")
primaryProbeset = form.getvalue("primaryProbeset")
secondaryProbeset = form.getvalue("secondaryProbeset")
# Default gene id
if primaryGene == None:
primaryGene = efpConfig.GENE_ID_DEFAULT1
if secondaryGene == None:
secondaryGene = efpConfig.GENE_ID_DEFAULT2
if useThreshold == "":
useThreshold = None
# set orthoListOn to off
orthoListOn = "0"
# Try Entered Threshold; if fails or threshold not checked use default threshold
if useThreshold != None:
try:
threshold = float(thresholdIn) # Convert str to float
except:
# Threshold string was malformed
error = 1
errorStr = 'Invalid Threshold Value "%s"
' % thresholdIn
errorStrings.append(errorStr)
useThreshold = None
if useThreshold == None and thresholdIn == None:
# assign a default value for first calls
if mode == "Relative" or mode == "Compare":
threshold = 2.0
else: #Absolute or none
threshold = 500
firstCall = 1
else:
threshold = float(thresholdIn)
firstCall = 0
if dataSource == None:
dataSource = efpConfig.defaultDataSource
# Serialize data from XML file into a Specimen object
spec = efp.Specimen()
xmlName = "%s/%s.xml" % (efpConfig.dataDir, dataSource)
spec.load(xmlName)
# Right now the browser only has one view - "all"
# In the future, there should be a drop down menu letting users
# choose multiple views
defaultImgFilename = "%s/%s_image.png" % (efpConfig.dataDir, dataSource)
if mode == None:
# If no mode is selected (99% of the time this means the user just arrived
# at the page), just show them a color map
# Set Developmental_Map as default DataSource
if dataSource == None:
dataSource = efpConfig.defaultDataSource
else:
###-----------------------------------"all" view-------------------------------------------------------------------------------------------------------------------------------
for name, view in spec.getViews().iteritems():
# If either of these probe IDs are None (bad inputs), then we just
# spit out the default image again
gene1 = view.createGene(primaryGene, probesetId=primaryProbeset)
gene2 = view.createGene(secondaryGene, probesetId=secondaryProbeset)
if gene1.getGeneId() is None or gene1.getProbeSetId() is None:
errorStr = 'The requested Primary gene / probeset ID "%s" cannot be found in %s datasource ' % (primaryGene, view.dbGroup)
error = 1
errorStrings.append(errorStr)
elif mode == 'Compare' and gene2.getProbeSetId() is None:
error = 1
errorStr = 'The requested Secondary gene / probeset ID "%s" cannot be found in %s datasource
' % (secondaryGene, view.dbGroup)
errorStrings.append(errorStr)
elif primaryGene == secondaryGene and mode == 'Compare':
error = 1
errorStr = 'The requested Secondary gene / probeset ID "%s" must be different than the Primary ID
' % secondaryGene
errorStrings.append(errorStr)
viewMaxSignal = 2.0
else:
if mode == 'Absolute':
if useThreshold:
(img,viewMaxSignal,viewMaxSignal1,viewMaxSignal2,sdAlert) = view.renderAbsolute(gene1, threshold, grey_mask=grey_stddev)
else:
(img,viewMaxSignal,viewMaxSignal1,viewMaxSignal2,sdAlert) = view.renderAbsolute(gene1, grey_mask=grey_stddev)
elif mode == 'Relative':
if useThreshold:
(img,viewMaxSignal,viewMaxSignal1,viewMaxSignal2,lowAlert) = view.renderRelative(gene1, threshold, grey_mask=grey_low)
else:
(img,viewMaxSignal,viewMaxSignal1,viewMaxSignal2,lowAlert) = view.renderRelative(gene1, grey_mask=grey_low)
elif mode == 'Compare':
if useThreshold:
(img,viewMaxSignal,viewMaxSignal1,viewMaxSignal2) = view.renderComparison(gene1, gene2, threshold)
else:
(img,viewMaxSignal,viewMaxSignal1,viewMaxSignal2) = view.renderComparison(gene1, gene2)
# find the max signal across all datasources and provide a link to that datasource
(maxSignalInDatasource, maxDatasource) = view.getMaxInDatasource(gene1)
# not needed because w overwrites
#os.system("rm -irf efpWeb.log");
xxxfile = open("efpWeb.log","w")
xxxfile.write('first geneid: %s\n' % gene1.getProbeSetId());
xxxfile.write('permission number: %s\n' % groupPermission);
xxxfile.write('line: 145');
xxxfile.close();
maxSignalInDatasource = round(maxSignalInDatasource,2)
# maxDatasource = re.sub("_"," ", maxDatasource)
alertStr = "This probe set reaches its maximum expression level (expression potential) of %s." % (maxSignalInDatasource)
alertStrings.append(alertStr)
# alert the user that the scale has changed if no threshold is set
if useThreshold == None and firstCall != 1:
if viewMaxSignal > threshold:
useThresholdFlag = "on"
thresholdLevelSuggested = maxSignalInDatasource
if mode == 'Relative':
thresholdLevelSuggested = 4
if mode == 'Compare':
thresholdLevelSuggested = 4
alertStr = "For %s data, note the maximum signal value has increased to %s from %s. Use the Signal Threshold option to keep it constant at %s, or enter a value in the Signal Threshold box, such as %s. The same colour scheme will then be applied across all views.
" % (view.dbGroup, viewMaxSignal, threshold, dataSource, mode, primaryGene, secondaryGene, useThresholdFlag, threshold, grey_low, grey_stddev, threshold, dataSource, mode, primaryGene, secondaryGene, useThresholdFlag, thresholdLevelSuggested, grey_low, grey_stddev, thresholdLevelSuggested)
alertStrings.append(alertStr)
elif viewMaxSignal < threshold:
useThresholdFlag = "on"
thresholdLevelSuggested = maxSignalInDatasource
if mode == 'Relative':
thresholdLevelSuggested = 4
if mode == 'Compare':
thresholdLevelSuggested = 4
alertStr = "For %s data, note the maximum signal value has decreased to %s from %s. Use the Signal Threshold option to keep it constant at %s, or enter a value in the Signal Threshold box, such as %s. The same colour scheme will then be applied across all views.
" % (view.dbGroup, viewMaxSignal, threshold, dataSource, mode, primaryGene, secondaryGene, useThresholdFlag, threshold, grey_low, grey_stddev, threshold, dataSource, mode, primaryGene, secondaryGene, useThresholdFlag, thresholdLevelSuggested, grey_low, grey_stddev, thresholdLevelSuggested)
alertStrings.append(alertStr)
else:
alertStr = ""
threshold = viewMaxSignal
elif useThreshold == None and firstCall == 1:
threshold = viewMaxSignal
# alert the user if SD filter or low filter should be activated
if grey_stddev != "on" and sdAlert == 1 and mode == 'Absolute':
grey_stddev_flag = "on"
if useThreshold == None:
useThreshold = ""
alertStr = "Some samples exhibit high standard deviations for replicates. You can use standard deviation filtering to mask those with a deviation greater than half their expression value.
" % (dataSource, mode, primaryGene, secondaryGene, useThreshold, threshold, grey_low, grey_stddev_flag)
alertStrings.append(alertStr)
# alert the user if SD filter or low filter should be activated
if grey_low != "on" and lowAlert == 1 and mode == 'Relative':
grey_low_flag = "on"
if useThreshold == None:
useThreshold = ""
alertStr = "Some sample ratios were calculated with low values that exhibit higher variation, potentially leading to ratios that are not a good reflection of the biology. You can low filter below 20 units to mask these.
" % (dataSource, mode, primaryGene, secondaryGene, useThreshold, threshold, grey_low_flag, grey_stddev)
alertStrings.append(alertStr)
# Otherwise, we render and display the option
imgMap[view.name] = view.getImageMap(mode, gene1, gene2, useThreshold, threshold, dataSource, grey_low, grey_stddev)
if img != None:
imgFilename[view.name] = view.drawImage(mode, maxSignalInDatasource, viewMaxSignal1, viewMaxSignal2, gene1, gene2, img)
#Create a table of Expression Values and save it in a temporary file
expTable = view.table
tableFile[view.name] = tempfile.mkstemp(suffix='.html', prefix='efp-', dir='output')
# python can do this for us
#os.system("chmod 644 " + tableFile[view.name][1])
import stat
os.chmod(tableFile[view.name][1], stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
tf = open(tableFile[view.name][1], 'w')
tf.write(expTable)
tf.close()
chartFile = tableFile[view.name][1].replace(".html", ".png")
view.saveChart(chartFile, mode)
views.append(view.name)
###-------------------------------------------------------HTML codes----------------------------------------------------------------------------------------------------------------------
print 'Content-Type: text/html\n'
print ''
print ''
print '