Example #1
0
 def __init__(self, fd=None, searchResult=None, colorScheme=None, userPrivilege=None, userName=None):
     cursor = webqtlDatabaseFunction.getCursor()
     if not cursor:
         return
     targetDescriptionChecked = fd.formdata.getvalue("targetDescriptionCheck", "")
     clusterChecked = fd.formdata.getvalue("clusterCheck", "")
     sessionfile = fd.formdata.getvalue("session")
     genotype = fd.genotype
     strainlist = [strain for strain in fd.strainlist if strain not in fd.parlist]
     ppolar = fd.ppolar
     mpolar = fd.mpolar
     traitList = []
     traitDataList = []
     for item in searchResult:
         thisTrait = webqtlTrait(fullname=item, cursor=cursor)
         thisTrait.retrieveInfo()
         thisTrait.retrieveData(strainlist)
         traitList.append(thisTrait)
         traitDataList.append(thisTrait.exportData(strainlist))
     self.buildCanvas(
         colorScheme=colorScheme,
         targetDescriptionChecked=targetDescriptionChecked,
         clusterChecked=clusterChecked,
         sessionfile=sessionfile,
         genotype=genotype,
         strainlist=strainlist,
         ppolar=ppolar,
         mpolar=mpolar,
         traitList=traitList,
         traitDataList=traitDataList,
         userPrivilege=userPrivilege,
         userName=userName,
     )
def controlStrains(controls, strainlst):

    controls = controls.split(',')

    cvals = {}
    for oneTraitName in controls:
        oneTrait = webqtlTrait(fullname=oneTraitName, cursor=webqtlDatabaseFunction.getCursor() )
        oneTrait.retrieveData()
        cvals[oneTraitName] = oneTrait.data

    tcstrains = []
    tcvals = []
    tcvars = []

    for oneTraitName in controls:
        strains = []
        vals = []
        vars = []

        for _strain in strainlst:
            if cvals[oneTraitName].has_key(_strain):
                _val = cvals[oneTraitName][_strain].val
                if _val != None:
                    strains.append(_strain)
                    vals.append(_val)
                    vars.append(None)

        tcstrains.append(strains)
        tcvals.append(vals)
        tcvars.append(vars)

    return tcstrains, tcvals, tcvars, [len(x) for x in tcstrains]
	def __init__(self,fd):
		templatePage.templatePage.__init__(self, fd)

                if not self.openMysql():
                        return

		cursor = self.cursor
		params = buildParamDict(cursor, fd)

		# get the input data
		inbredSetName, traits = readInputFile(cursor, RootDir + params["filename"])
        
		# and what we are comparing the data to
		dbTraits = []
		if params["targetDatabaseType"] != "ProbeSet":
			dbTraits = loadDatabase(cursor, params)

        
		# run the comparison itself
		strainCount = trait.queryStrainCount(cursor) # XZ, 09/10/2008: add module name 
		if params["targetDatabaseType"] == "ProbeSet":
			results = runProbeSetCorrelations(cursor, params, traits)
		else:
			results = runCorrelations(params, strainCount, traits, dbTraits)

		# try to be smart about what to output:
		# we want to limit the number of traits shown, at least initially
		# and since traitArray is already sorted with most interesting
		# subsets first, we simply pick up the first 500 or so traits
		# that we find
		if params["displaySets"] == 0:
			selectedTraits = 0
			for j in range(len(results)):
				#print "Scanning subarray %d" % j
				if selectedTraits <= 200:
					params["displaySets"] += pow(2, j)
					selectedTraits += len(results[j][1])

		traitList = []
		for oneTrait in traits:  # XZ, 09/10/2008: change the original variable name 'trait' to 'oneTrait'
			traitName = oneTrait.dbName+'::'+oneTrait.name  # XZ, 09/10/2008: change the original variable name 'trait' to 'oneTrait'
			aTrait =  webqtlTrait(cursor=self.cursor, fullname=traitName)
			traitList.append(aTrait)

		# and generate some output
		txtOutputFilename = tempfile.mktemp() 
		txtOutputHandle = open(txtOutputFilename, "w")
		txtOutput = TraitCorrelationText(params, traits, results)
		txtOutputHandle.write(str(txtOutput))
		txtOutputHandle.close()
		txtOutputFilename = os.path.split(txtOutputFilename)[1]

		self.dict['body'] = TraitCorrelationPage(fd, params, cursor, traitList,
					results, inbredSetName,
					txtOutputFilename).dict['body']
Example #4
0
    def __init__(self,fd):

        self.theseTraits = []
        TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee',valign="middle")

        templatePage.__init__(self, fd)

        if not self.openMysql():
            return

        self.searchResult = fd.formdata.getvalue('searchResult', [])
        if type("1") == type(self.searchResult):
            self.searchResult = [self.searchResult]

        for item in self.searchResult:
            try:
                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                thisTrait.retrieveInfo(QTL=1)
                if thisTrait.db.type == "ProbeSet":
                    self.theseTraits.append(thisTrait)
            except:
                pass

        if self.theseTraits:
            pass
        else:
            templatePage.__init__(self, fd)
            heading = 'GCAT'
            detail = ['You need to select at least one microarray trait to submit to GCAT.']
            self.error(heading=heading,detail=detail)
            return

        geneSymbolList = self.getGeneSymbolList()

        geneSymbolSet = set(geneSymbolList)

        if ( len(geneSymbolSet) < 500 ):
            temp = '+'.join(geneSymbolSet)
            GCATurl = "http://binf1.memphis.edu/gcat/?organism=mouse&subset=all&year=2010&geneInput=%s" % temp

            self.dict['js1'] = """
                <SCRIPT LANGUAGE="JavaScript">
                setTimeout( 'window.location = "%s"', 2000 );
                </SCRIPT>
            """ % GCATurl

            TD_LR.append(HT.Paragraph("Your selection of %d genes is being submitted to GCAT" % len(geneSymbolSet), Class="cr fs16 fwb", align="Center"))
        else:
            TD_LR.append(HT.Paragraph("Your selection of %d genes exceeds the limit of 500. Please reduce your gene number to below the limit." % len(geneSymbolSet), Class="cr fs16 fwb", align="Center"))


        self.dict['body'] = TD_LR
Example #5
0
	def __init__(self,fd):
		templatePage.__init__(self, fd)
		if not self.openMysql():
			return
		
		searchResult = fd.formdata.getvalue('searchResult')
		if not searchResult:
			heading = 'BNW'
			detail = ['You need to select at least one trait.']
			self.error(heading=heading, detail=detail)
			return
		if type("1") == type(searchResult):
			searchResult = string.split(searchResult, '\t')
			
		fd.incparentsf1 = 1
		if not fd.genotype:
			fd.readGenotype()
		strainlist = fd.f1list + fd.strainlist
		
		valuesList = []
		formvalue = ''
		for item in searchResult:
			#
			thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
			thisTrait.retrieveData(strainlist=strainlist)
			values = thisTrait.exportData(strainlist)
			valuesList.append(values)
			#
			item = item.replace("Geno::", "_")
			item = item.replace("Publish::", "_")
			item = item.replace("::", "_")
			formvalue += item
			formvalue += ','
		valuesList = zip(*valuesList)
		formvalue = formvalue[:-1]
		formvalue += ';'

		for row in valuesList:
			hasNone = False
			for cell in row:
				if not cell:
					hasNone = True
					break
			if hasNone:
				continue
			formvalue += ','.join(str(cell) for cell in row)
			formvalue += ';'

		self.dict['body'] = BNWPage.body % formvalue
		return
    def genSelection(self, fd=None, checkPreSelection = 1):
        collectionName = '%s_Select' % fd.RISet

        if checkPreSelection:
            try:
                preSelection = fd.input_session_data[collectionName]
                preSelection = list(string.split(preSelection,','))
            except:
                preSelection = []
        else:
            preSelection = []

        if preSelection:
            for item in preSelection:
                if item not in self.searchResult:
                    self.searchResult.append(item)

        self.searchResult = map(self.transfer2NewName, self.searchResult)

        for item in self.searchResult:
            if not item:
                self.searchResult.remove(item)

        if len(self.searchResult) > 3000:
            heading = 'Add Collections'
            detail = ['You are adding over 3000 traits to selections, please reduce your number of traits.']
            self.error(heading=heading,detail=detail)
            return 0

        searchResult2 = []
        self.theseTraits = []
        for item in self.searchResult:
            try:
                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                thisTrait.retrieveInfo(QTL=1)
                self.theseTraits.append(thisTrait)
                searchResult2.append(item)
            except:
                pass

        allTraitStr = string.join(searchResult2,',')

        self.session_data_changed[collectionName] = allTraitStr

        return 1
    def checkInputPage(self, PublishFreeze_Name, traitID):
        TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee')

        try:
            db = webqtlDataset(PublishFreeze_Name, self.cursor)
            thisTrait = webqtlTrait(db=db, cursor=self.cursor, name=traitID)
            thisTrait.retrieveInfo()
            setDescription = thisTrait.genHTML(dispFromDatabase=1, privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users)
        except:
            TD_LR.append( HT.Font('This trait is not found. Please go back to check if you selected correct Group Name and inputed correct trait ID.', color='red') )
            return str(TD_LR)

        #TD_LR.append(HT.Font('Publish Freeze Name: %s' % PublishFreeze_Name, color='red'),HT.BR(), HT.Font('trait ID: %s' % traitID, color='red'), HT.BR())

        formMain = HT.Form(cgi=os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase', submit=HT.Input(type='hidden'))

        formMain.append(
                HT.Blockquote(
                              HT.Font('The trait '),
                              setDescription,
                              HT.Font(' will be deleted.'),
                              HT.BR(), HT.BR(),
                              HT.Font('Please open the trait and make sure you do want to delete it.', color = 'red')
                ),
                HT.Input(type='hidden',name='FormID',value=''),
                HT.Input(type='hidden',name='database',value=''),
                HT.Input(type='hidden',name='ProbeSetID',value=''),
                HT.Input(type='hidden',name='CellID',value='')
        )

        deletePhenotypeTraitForm = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='deletePhenotypeTraitForm', submit=HT.Input(type='hidden'))
        deletePhenotypeTraitForm.append(
            HT.Input(type='Submit', value='Delete Trait', Class="button"),
            HT.Input(type='hidden',name='FormID',value='deletePhenotypeTrait'),
            HT.Input(type='hidden',name='ifVerified',value='GN@UTHSC'),
            HT.Input(type='hidden',name='status',value='delete'),
            HT.Input(type='hidden',name='PublishFreeze_Name',value=db),
            HT.Input(type='hidden',name='traitID',value=traitID)
        )


        TD_LR.append(formMain, HT.BR(), HT.BR(), deletePhenotypeTraitForm)
        return str(TD_LR)
	def genSelection(self, fd=None):
		collectionName = '%s_Select' % fd.RISet

		try:
			preSelection = fd.input_session_data[collectionName]
			preSelection = list(string.split(preSelection,','))
		except:
			preSelection = []
		
		if type("1") == type(self.searchResult):
			self.searchResult = [self.searchResult]
		
		if preSelection:
			for item in self.searchResult:
				try:
					preSelection.remove(item)
				except:
					pass
		self.searchResult = preSelection[:]
		
		if not self.searchResult:
			self.session_data_changed[collectionName] = ""
			return
		
		#self.searchResult.sort()
		for item in self.searchResult:
			if not item:
				self.searchResult.remove(item)
				
		searchResult2 = []
		self.theseTraits = []
		for item in self.searchResult:
			try:
				thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
				thisTrait.retrieveInfo(QTL=1)
				self.theseTraits.append(thisTrait)
				searchResult2.append(item)
			except:
				pass

		allTraitStr = string.join(searchResult2,',')

		self.session_data_changed[collectionName] = allTraitStr
	def __init__(self, fd):

		templatePage.__init__(self, fd)

		if not self.updMysql():
			return

		if not fd.genotype:
			fd.readGenotype()
			fd.strainlist = fd.f1list + fd.strainlist
		
		fd.readData()
		
		self.formdata = fd.formdata
		self.dict['title'] = 'Data Updating'
		
		try:
			thisTrait = webqtlTrait(fullname=self.formdata.getvalue('fullname'), cursor=self.cursor)
			thisTrait.retrieveInfo()
		except:
			heading = "Updating Database"
			detail = ["The trait doesn't exist."]
			self.error(heading=heading,detail=detail,error="Error")
			return

		if webqtlConfig.USERDICT[self.privilege] >= webqtlConfig.USERDICT['user']:
				pass
		else:
			heading = "Updating Database"
			detail = ["You don't have the permission to modify this trait"]
			self.error(heading=heading,detail=detail,error="Error")
			return

			
		status = self.formdata.getvalue('curStatus')
		if status == 'updateCheck': #XZhou: Check the change
			self.updateCheckPage(fd, thisTrait)
		elif status == 'updateResult': #XZhou: make the changes to database
			self.updateResultPage(fd, thisTrait)
		else: #XZhou: show info retrieved from database
			self.dispTraitPage(fd, thisTrait)
	def __init__(self, fd):

		LRSFullThresh = 30
		LRSInteractThresh = 25

		templatePage.__init__(self, fd)

		if not fd.genotype:
			fd.readData()

		incVars = 0
		_genotype = fd.genotype_1
		_strains, _vals, _vars, N = fd.informativeStrains(_genotype.prgy, incVars)

		self.dict['title'] = 'Pair-Scan Plot'
		if not self.openMysql():
			return

		iPermuCheck = fd.formdata.getvalue('directPermuCheckbox')

		try:
			graphtype = int(fd.formdata.getvalue('graphtype'))
		except:
			graphtype = 1
		try:
			graphsort = int(fd.formdata.getvalue('graphSort'))
		except:
			graphsort = 1
		try:
			returnIntervalPairNum = int(fd.formdata.getvalue('pairScanReturn'))
		except:
			returnIntervalPairNum = 50

		pairIntro = HT.Blockquote("The graph below displays pair-scan results for the trait ",HT.Strong(" %s" % fd.identification))
		if not graphsort:
			tblIntro = HT.Blockquote('This table lists LRS scores for the top %d pairs of intervals (Interval 1 on the left and Interval 2 on the right). Pairs are sorted by the "LRS Full" column. Both intervals are defined by proximal and distal markers that flank the single best position.' % returnIntervalPairNum)
		else:
			tblIntro = HT.Blockquote('This table lists LRS scores for the top %d pairs of intervals (Interval 1 on the left and Interval 2 on the right). Pairs are sorted by the "LRS Interaction" column. Both intervals are defined by proximal and distal markers that flank the single best position.' % returnIntervalPairNum)

		try:
			thisTrait = webqtlTrait(fullname=fd.formdata.getvalue("fullname"), cursor=self.cursor)
			pairIntro.append(' from the database ' , thisTrait.db.genHTML())
		except:
			pass

		pairIntro.append('. The upper left half of the plot highlights any epistatic interactions (corresponding to the column labeled "LRS Interact"). In contrast, the lower right half provides a summary of LRS of the full model, representing cumulative effects of linear and non-linear terms (column labeled "LRS Full"). The WebQTL implementation of the scan for 2-locus epistatic interactions is based on the DIRECT global optimization algorithm developed by ',HT.Href(text ="Ljungberg",url='http://user.it.uu.se/~kl/qtl_software.html',target="_blank", Class = "fs14 fwn"),', Holmgren, and Carlborg (',HT.Href(text = "2004",url='http://bioinformatics.oupjournals.org/cgi/content/abstract/bth175?ijkey=21Pp0pgOuBL6Q&keytype=ref', Class = "fs14 fwn"),').')

		main_title = HT.Paragraph("Pair-Scan Results: An Analysis of Epistatic Interactions")
		main_title.__setattr__("class","title")

		subtitle1 = HT.Paragraph("Pair-Scan Graph")
		subtitle3 = HT.Paragraph("Pair-Scan Top LRS")
		subtitle1.__setattr__("class","subtitle")
		subtitle3.__setattr__("class","subtitle")

		self.identification = "unnamed trait"
		if fd.identification:
			self.identification = fd.identification
			self.dict['title'] = self.identification + ' / '+self.dict['title']

		#####################################
		#
		# Remove the Parents & F1 data
		#
		#####################################

		if _vals:
			if len(_vals) > webqtlConfig.KMININFORMATIVE:
				ResultFull = []
				ResultInteract = []
				ResultAdd = []

				#permutation test
				subtitle2 = ''
				permuTbl = ''
				permuIntro = ''
				if iPermuCheck:
					subtitle2 = HT.Paragraph("Pair-Scan Permutation Results")
					subtitle2.__setattr__("class","subtitle")
					permuIntro = HT.Blockquote("Phenotypes were randomly permuted 500 times among strains or individuals and reanalyzed using the pair-scan algorithm. We extracted the single highest LRS for the full model for each of these permuted data sets. The histograms of these highest LRS values provide an empirical way to estimate the probability of obtaining an LRS above suggestive or significant thresholds.")

					prtmuTblIntro1 = HT.Paragraph("The following table gives threshold values for Suggestive (P=0.63) and Significant associations (P=0.05) defined by Lander & Kruglyak and for the slightly more stringent P=0.01 level. (The Highly Significant level of Lander & Kruglyak corresponds to P=0.001 and cannot be estimated with 500 permutations.)")
					prtmuTblIntro2 = HT.Paragraph("If the full model exceeds the permutation-based Significant threshold, then different models for those locations can be tested by conventional chi-square tests at P<0.01. Interaction is significant if LRS Interact exceeds 6.64 for RI strains or 13.28 for an F2. If interaction is not significant, the two-QTL model is better than a one-QTL model if LRS Additive exceeds LRS 1 or LRS 2 by 6.64 for RI strains or 9.21 for an F2.")
					ResultFull, ResultInteract, ResultAdd = direct.permu(webqtlConfig.GENODIR, _vals, _strains, fd.RISet, 500) #XZ, 08/14/2008: add module name webqtlConfig
					ResultFull.sort()
					ResultInteract.sort()
					ResultAdd.sort()
					nPermuResult = len(ResultFull)
					# draw Histogram
					cFull = pid.PILCanvas(size=(400,300))
					Plot.plotBar(cFull, ResultFull,XLabel='LRS',YLabel='Frequency',title=' Histogram of LRS Full')
					#plotBar(cFull,10,10,390,290,ResultFull,XLabel='LRS',YLabel='Frequency',title=' Histogram of LRS Full')
					filename= webqtlUtil.genRandStr("Pair_")
					cFull.save(webqtlConfig.IMGDIR+filename, format='gif')
					imgFull=HT.Image('/image/'+filename+'.gif',border=0,alt='Histogram of LRS Full')


					superPermuTbl = HT.TableLite(border=0, cellspacing=0, cellpadding=0,bgcolor ='#999999')
					permuTbl2 = HT.TableLite(border=0, cellspacing= 1, cellpadding=5)
					permuTbl2.append(HT.TR(HT.TD(HT.Font('LRS', color = '#FFFFFF')), HT.TD(HT.Font('p = 0.63', color = '#FFFFFF'), width = 150, align='Center'), HT.TD(HT.Font('p = 0.05', color = '#FFFFFF'), width = 150, align='Center'), HT.TD(HT.Font('p = 0.01', color = '#FFFFFF'), width = 150, align='Center'),bgColor='royalblue'))
					permuTbl2.append(HT.TR(HT.TD('Full'), HT.TD('%2.1f' % ResultFull[int(nPermuResult*0.37 -1)], align="Center"), HT.TD('%2.1f' % ResultFull[int(nPermuResult*0.95 -1)], align="Center"), HT.TD('%2.1f' % ResultFull[int(nPermuResult*0.99 -1)], align="Center"),bgColor="#eeeeee"))
					superPermuTbl.append(HT.TD(HT.TD(permuTbl2)))

					permuTbl1 = HT.TableLite(border=0, cellspacing= 0, cellpadding=5,width='100%')
					permuTbl1.append(HT.TR(HT.TD(imgFull, align="Center", width = 410), HT.TD(prtmuTblIntro1, superPermuTbl, prtmuTblIntro2, width = 490)))

					permuTbl = HT.Center(permuTbl1, HT.P())

					#permuTbl.append(HT.TR(HT.TD(HT.BR(), 'LRS Full  = %2.1f, ' % ResultFull[int(nPermuResult*0.37 -1)], 'LRS Full  = %2.1f, ' % ResultFull[int(nPermuResult*0.95 -1)], 'LRS Full highly significant (p=0.001) = %2.1f, ' % ResultFull[int(nPermuResult*0.999 -1)] , HT.BR(), 'LRS Interact suggestive (p=0.63) = %2.1f, ' % ResultInteract[int(nPermuResult*0.37 -1)], 'LRS Interact significant (p=0.05) = %2.1f, ' % ResultInteract[int(nPermuResult*0.95 -1)], 'LRS Interact  = %2.1f, ' % ResultInteract[int(nPermuResult*0.999 -1)] , HT.BR(),'LRS Additive suggestive (p=0.63) = %2.1f, ' % ResultAdd[int(nPermuResult*0.37 -1)], 'LRS Additive significant (p=0.05) = %2.1f, ' % ResultAdd[int(nPermuResult*0.95 -1)], 'LRS Additive highly significant (p=0.001) = %2.1f, ' % ResultAdd[int(nPermuResult*0.999 -1)], HT.BR(), 'Total number of permutation is %d' % nPermuResult, HT.BR(), HT.BR(),colspan=2)))
					#tblIntro.append(HT.P(), HT.Center(permuTbl))

				#print vals, strains, fd.RISet
				d = direct.direct(webqtlConfig.GENODIR, _vals, _strains, fd.RISet, 8000)#XZ, 08/14/2008: add module name webqtlConfig
				chrsInfo = d[2]
				sum = 0
				offsets = [0]
				i = 0
				for item in chrsInfo:
					if i > 0:
						offsets.append(sum)
					sum += item[0]
					i += 1
				offsets.append(sum)
				#print sum,offset,d[2]
				canvasWidth = 880
				canvasHeight = 880
				if graphtype:
					colorAreaWidth = 230
				else:
					colorAreaWidth = 0
				c = pid.PILCanvas(size=(canvasWidth + colorAreaWidth ,canvasHeight))
				xoffset = 40
				yoffset = 40
				width = canvasWidth - xoffset*2
				height = canvasHeight - yoffset*2

				xscale = width/sum
				yscale = height/sum

				rectInfo = d[1]
				rectInfo.sort(webqtlUtil.cmpLRSFull)

				finecolors = Plot.colorSpectrum(250)
				finecolors.reverse()
				regLRS = [0]*height
				#draw LRS Full

				for item in rectInfo:
					LRSFull,LRSInteract,LRSa,LRSb,chras,chram,chrae,chrbs,chrbm,chrbe,chra,chrb,flanka,flankb = item
					if LRSFull > 30:
						dcolor = pid.red
					elif LRSFull > 20:
						dcolor = pid.orange
					elif LRSFull > 10:
						dcolor = pid.olivedrab
					elif LRSFull > 0:
						dcolor = pid.grey
					else:
						LRSFull = 0
						dcolor = pid.grey

					chras += offsets[chra]
					chram += offsets[chra]
					chrae += offsets[chra]
					chrbs += offsets[chrb]
					chrbm += offsets[chrb]
					chrbe += offsets[chrb]

					regLRSD = int(chram*yscale)
					if regLRS[regLRSD] < LRSa:
						regLRS[regLRSD] = LRSa
					regLRSD = int(chrbm*yscale)
					if regLRS[regLRSD] < LRSb:
						regLRS[regLRSD] = LRSb

					if graphtype:
						colorIndex = int(LRSFull *250 /LRSFullThresh)
						if colorIndex >= 250:
							colorIndex = 249
						dcolor = finecolors[colorIndex]
						if chra != chrb or ((chrbe - chrae) > 10 and (chrbs - chras) > 10):
							c.drawRect(xoffset+chrbs*xscale,yoffset+height-chras*yscale,xoffset+chrbe*xscale,yoffset+height-chrae*yscale,edgeColor=dcolor,fillColor=dcolor,edgeWidth = 0)
						else:
							c.drawPolygon([(xoffset+chrbs*xscale,yoffset+height-chras*yscale),(xoffset+chrbe*xscale,yoffset+height-chras*yscale),(xoffset+chrbe*xscale,yoffset+height-chrae*yscale)],edgeColor=dcolor,fillColor=dcolor,edgeWidth = 0,closed =1)
					else:
						c.drawCross(xoffset+chrbm*xscale,yoffset+height-chram*yscale,color=dcolor,size=2)
				#draw Marker Regression LRS
				if graphtype:
					"""
					maxLRS = max(regLRS)
					pts = []
					i = 0
					for item in regLRS:
						pts.append((xoffset+width+35+item*50/maxLRS, yoffset+height-i))
						i += 1
					c.drawPolygon(pts,edgeColor=pid.blue,edgeWidth=1,closed=0)
					"""
					LRS1Thresh = 16.2
					i = 0
					for item in regLRS:
						colorIndex = int(item *250 /LRS1Thresh)
						if colorIndex >= 250:
							colorIndex = 249
						dcolor = finecolors[colorIndex]
						c.drawLine(xoffset+width+35,yoffset+height-i,xoffset+width+55,yoffset+height-i,color=dcolor)
						i += 1
					labelFont=pid.Font(ttf="arial",size=20,bold=0)
					c.drawString('Single Locus Regression',xoffset+width+90,yoffset+height, font = labelFont,color=pid.dimgray,angle=90)
				#draw LRS Interact
				rectInfo.sort(webqtlUtil.cmpLRSInteract)
				for item in rectInfo:
					LRSFull,LRSInteract,LRSa,LRSb,chras,chram,chrae,chrbs,chrbm,chrbe,chra,chrb,flanka,flankb = item
					if LRSInteract > 30:
						dcolor = pid.red
					elif LRSInteract > 20:
						dcolor = pid.orange
					elif LRSInteract > 10:
						dcolor = pid.olivedrab
					elif LRSInteract > 0:
						dcolor = pid.grey
					else:
						LRSInteract = 0
						dcolor = pid.grey
					chras += offsets[chra]
					chram += offsets[chra]
					chrae += offsets[chra]
					chrbs += offsets[chrb]
					chrbm += offsets[chrb]
					chrbe += offsets[chrb]
					if graphtype:
						colorIndex = int(LRSInteract *250 / LRSInteractThresh )
						if colorIndex >= 250:
							colorIndex = 249
						dcolor = finecolors[colorIndex]
						if chra != chrb or ((chrbe - chrae) > 10 and (chrbs - chras) > 10):
							c.drawRect(xoffset+chras*xscale,yoffset+height-chrbs*yscale,xoffset+chrae*xscale,yoffset+height-chrbe*yscale,edgeColor=dcolor,fillColor=dcolor,edgeWidth = 0)
						else:
							c.drawPolygon([(xoffset+chras*xscale,yoffset+height-chrbs*yscale),(xoffset+chras*xscale,yoffset+height-chrbe*yscale),(xoffset+chrae*xscale,yoffset+height-chrbe*yscale)],edgeColor=dcolor,fillColor=dcolor,edgeWidth = 0,closed =1)
					else:
						c.drawCross(xoffset+chram*xscale,yoffset+height-chrbm*yscale,color=dcolor,size=2)
				#draw chromosomes label
				labelFont=pid.Font(ttf="tahoma",size=24,bold=0)
				i = 0
				for item in chrsInfo:
					strWidth = c.stringWidth(item[1],font=labelFont)
					c.drawString(item[1],xoffset+offsets[i]*xscale +(item[0]*xscale-strWidth)/2,canvasHeight -15,font = labelFont,color=pid.dimgray)
					c.drawString(item[1],xoffset+offsets[i]*xscale +(item[0]*xscale-strWidth)/2,yoffset-10,font = labelFont,color=pid.dimgray)
					c.drawString(item[1],xoffset-strWidth-5,yoffset+height - offsets[i]*yscale -(item[0]*yscale-22)/2,font = labelFont,color=pid.dimgray)
					c.drawString(item[1],canvasWidth-xoffset+5,yoffset+height - offsets[i]*yscale -(item[0]*yscale-22)/2,font = labelFont,color=pid.dimgray)
					i += 1


				c.drawRect(xoffset,yoffset,xoffset+width,yoffset+height)
				for item in offsets:
					c.drawLine(xoffset,yoffset+height-item*yscale,xoffset+width,yoffset+height-item*yscale)
					c.drawLine(xoffset+item*xscale,yoffset,xoffset+item*xscale,yoffset+height)

				#draw pngMap
				pngMap = HT.Map(name='pairPlotMap')
				#print offsets, len(offsets)
				for i in range(len(offsets)-1):
					for j in range(len(offsets)-1):
						COORDS = "%d,%d,%d,%d" %(xoffset+offsets[i]*xscale, yoffset+height-offsets[j+1]*yscale, xoffset+offsets[i+1]*xscale, yoffset+height-offsets[j]*yscale)
						HREF = "javascript:showPairPlot(%d,%d);" % (i,j)
						Areas = HT.Area(shape='rect',coords=COORDS,href=HREF)
						pngMap.areas.append(Areas)

				#draw spectrum
				if graphtype:
					i = 0
					labelFont=pid.Font(ttf="tahoma",size=14,bold=0)
					middleoffsetX = 180
					for dcolor in finecolors:
						if i % 50 == 0:
							c.drawLine(xoffset+ width +middleoffsetX-15 , height + yoffset -i, xoffset+ width +middleoffsetX-20,height + yoffset -i, color=pid.black)
							c.drawString('%d' % int(LRSInteractThresh*i/250.0),xoffset+ width+ middleoffsetX-40,height + yoffset -i +5, font = labelFont,color=pid.black)
							c.drawLine(xoffset+ width +middleoffsetX+15 , height + yoffset -i, xoffset+ width +middleoffsetX+20 ,height + yoffset -i, color=pid.black)
							c.drawString('%d' % int(LRSFullThresh*i/250.0),xoffset+ width + middleoffsetX+25,height + yoffset -i +5, font = labelFont,color=pid.black)
						c.drawLine(xoffset+ width +middleoffsetX-15 , height + yoffset -i, xoffset+ width +middleoffsetX+15 ,height + yoffset -i, color=dcolor)
						i += 1

					if i % 50 == 0:
						i -= 1
						c.drawLine(xoffset+ width +middleoffsetX-15 , height + yoffset -i, xoffset+ width +middleoffsetX-20,height + yoffset -i, color=pid.black)
						c.drawString('%d' % ceil(LRSInteractThresh*i/250.0),xoffset+ width + middleoffsetX-40,height + yoffset -i +5, font = labelFont,color=pid.black)
						c.drawLine(xoffset+ width +middleoffsetX+15 , height + yoffset -i, xoffset+ width +middleoffsetX+20 ,height + yoffset -i, color=pid.black)
						c.drawString('%d' % ceil(LRSFullThresh*i/250.0),xoffset+ width + middleoffsetX+25,height + yoffset -i +5, font = labelFont,color=pid.black)

					labelFont=pid.Font(ttf="verdana",size=20,bold=0)
					c.drawString('LRS Interaction',xoffset+ width + middleoffsetX-50,height + yoffset, font = labelFont,color=pid.dimgray,angle=90)
					c.drawString('LRS Full',xoffset+ width + middleoffsetX+50,height + yoffset, font = labelFont,color=pid.dimgray,angle=90)

				filename= webqtlUtil.genRandStr("Pair_")
				c.save(webqtlConfig.IMGDIR+filename, format='png')
				img2=HT.Image('/image/'+filename+'.png',border=0,usemap='#pairPlotMap')


				form0 = HT.Form( cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showPairPlot', submit=HT.Input(type='hidden'))
				hddn0 = {'FormID':'pairPlot','Chr_A':'_','Chr_B':'','idata':string.join(map(str, _vals), ','),'istrain':string.join(_strains, ','),'RISet':fd.RISet}
				for key in hddn0.keys():
					form0.append(HT.Input(name=key, value=hddn0[key], type='hidden'))

				form0.append(img2, pngMap)

				mainfmName = webqtlUtil.genRandStr("fm_")
				txtform = HT.Form( cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name=mainfmName, submit=HT.Input(type='hidden'))
				hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':fd.RISet+"Geno",'CellID':'_','RISet':fd.RISet}
				#XZ, Aug 11, 2010: The variable traitStrains is not assigned right values before (should not be assigned fd.strainlist).
				#hddn['traitStrains'] = string.join(fd.strainlist, ',')
				hddn['traitStrains'] = string.join(_strains, ',')
				hddn['traitValues'] = string.join(map(str, _vals), ',')
				hddn['interval1'] = ''
				hddn['interval2'] = ''
				if fd.incparentsf1:
					hddn['incparentsf1']='ON'
				for key in hddn.keys():
					txtform.append(HT.Input(name=key, value=hddn[key], type='hidden'))

				tbl = HT.TableLite(Class="collap", cellspacing=1, cellpadding=5,width=canvasWidth + colorAreaWidth)

				c1 = HT.TD('Interval 1',colspan=3,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c2 = HT.TD('Interval 2',colspan=3,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c11 = HT.TD('Position',rowspan=2,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c12 = HT.TD('Flanking Markers',colspan=2,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c111 = HT.TD('Proximal',align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c112 = HT.TD('Distal',align="Center", Class="fs13 fwb ffl b1 cw cbrb")


				c3 = HT.TD('LRS Full',rowspan=3,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c4 = HT.TD('LRS Additive',rowspan=3,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c5 = HT.TD('LRS Interact',rowspan=3,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c6 = HT.TD('LRS 1',rowspan=3,align="Center", Class="fs13 fwb ffl b1 cw cbrb")
				c7 = HT.TD('LRS 2',rowspan=3,align="Center", Class="fs13 fwb ffl b1 cw cbrb")


				tbl.append(HT.TR(c1,c3,c4,c5,c6,c7,c2))

				tbl.append(HT.TR(c11,c12,c11,c12))
				tbl.append(HT.TR(c111,c112,c111,c112))
				if not graphsort: #Sort by LRS Full
					rectInfo.sort(webqtlUtil.cmpLRSFull)
				rectInfoReturned = rectInfo[len(rectInfo) - returnIntervalPairNum:]
				rectInfoReturned.reverse()

				for item in rectInfoReturned:
					LRSFull,LRSInteract,LRSa,LRSb,chras,chram,chrae,chrbs,chrbm,chrbe,chra,chrb,flanka,flankb = item
					LRSAdditive = LRSFull - LRSInteract
					flanka1,flanka2 = string.split(flanka)
					flankb1,flankb2 = string.split(flankb)
					urla1 = HT.Href(text = flanka1, url = "javascript:showTrait('%s','%s');" % (mainfmName, flanka1),Class= "fs12 fwn")
					urla2 = HT.Href(text = flanka2, url = "javascript:showTrait('%s','%s');" % (mainfmName, flanka2),Class= "fs12 fwn")
					urlb1 = HT.Href(text = flankb1, url = "javascript:showTrait('%s','%s');" % (mainfmName, flankb1),Class= "fs12 fwn")
					urlb2 = HT.Href(text = flankb2, url = "javascript:showTrait('%s','%s');" % (mainfmName, flankb2),Class= "fs12 fwn")
					urlGenGraph = HT.Href(text = "Plot", url = "javascript:showCateGraph('%s',  '%s %s %2.3f', '%s %s %2.3f');" % (mainfmName, flanka1, flanka2, chram, flankb1, flankb2, chrbm),Class= "fs12 fwn")
					tr1 = HT.TR(
						HT.TD('Chr %s @ %2.1f cM ' % (chrsInfo[chra][1],chram),Class= "fs12 b1 fwn"),
						HT.TD(urla1,Class= "fs12 b1 fwn"),
						HT.TD(urla2,Class= "fs12 b1 fwn"),
						HT.TD('%2.3f ' % LRSFull, urlGenGraph,Class= "fs12 b1 fwn"),
						HT.TD('%2.3f' % LRSAdditive,Class= "fs12 b1 fwn"),
						HT.TD('%2.3f' % LRSInteract,Class= "fs12 b1 fwn"),
						HT.TD('%2.3f' % LRSa,Class= "fs12 b1 fwn"),
						HT.TD('%2.3f' % LRSb,Class= "fs12 b1 fwn"),
						HT.TD('Chr %s @ %2.1f cM' % (chrsInfo[chrb][1],chrbm),Class= "fs12 b1 fwn"),
						HT.TD(urlb1,Class= "fs12 b1 fwn"),
						HT.TD(urlb2,Class= "fs12 b1 fwn"))
					tbl.append(tr1)

				plotType1 = HT.Input(type="radio", name="plotType", value ="Dot", checked=1)
				plotType2 = HT.Input(type="radio", name="plotType", value ="Box")
				plotText = HT.Paragraph("Plot Type : ", plotType1, " Dot ", plotType2, " Box",  )

				txtform.append(plotText, tbl)
				TD_LR = HT.TD(colspan=2,height=200,width="100%",bgColor='#eeeeee')
				TD_LR.append(main_title,HT.Blockquote(subtitle1, pairIntro, HT.P(), HT.Center(form0,HT.P())),HT.Blockquote(subtitle2, permuIntro,HT.P(), HT.Center(permuTbl)), HT.Blockquote(subtitle3, tblIntro, HT.P(),HT.Center(txtform), HT.P()))
				self.dict['body'] = str(TD_LR)
			else:
				heading = "Direct Plot"
				detail = ['Fewer than %d strain data were entered for %s data set. No statitical analysis has been attempted.' % (webqtlConfig.KMININFORMATIVE, fd.RISet)]
				self.error(heading=heading,detail=detail)
				return
		else:
			heading = "Direct Plot"
			detail = ['Empty data set, please check your data.']
			self.error(heading=heading,detail=detail)
			return
	def __init__(self,fd):

		########## geneName means symbol ##########
		geneName = fd.formdata.getvalue('gene')
		if geneName:
			geneName = string.strip(geneName)

		refseq = fd.formdata.getvalue('refseq')
		if refseq:
			refseq = string.strip(refseq)

		genbankid = fd.formdata.getvalue('genbankid')
		if genbankid:
			genbankid = string.strip(genbankid)

		geneid = fd.formdata.getvalue('geneid')
		if geneid:
			geneid = string.strip(geneid)

		species = fd.formdata.getvalue('species')
		tissue = fd.formdata.getvalue('tissue')
		database = fd.formdata.getvalue('database')

		########## searchAlias is just a singal, so it doesn't need be stripped ##########
		searchAlias = fd.formdata.getvalue('searchAlias')

		if not self.openMysql():
			return

		if database:
			if geneName:
				if searchAlias:
					self.cursor.execute(""" SELECT ProbeSetXRef.*
								FROM
									ProbeSet, ProbeSetXRef, DBList
								WHERE
									ProbeSetXRef.ProbeSetFreezeId = DBList.FreezeId AND
									ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
									(DBList.Name=%s or DBList.Code=%s) AND
									MATCH (ProbeSet.symbol, alias) AGAINST ("+%s" IN BOOLEAN MODE)
								ORDER BY ProbeSetXRef.mean DESC
							    """ , (database, database, geneName))
				else:
					self.cursor.execute(""" SELECT ProbeSetXRef.*
								FROM    
									ProbeSet, ProbeSetXRef, DBList
								WHERE   
									ProbeSetXRef.ProbeSetFreezeId = DBList.FreezeId AND
									ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
									(DBList.Name=%s or DBList.Code=%s) AND
									ProbeSet.symbol = %s
								ORDER BY ProbeSetXRef.mean DESC
							    """ , (database, database, geneName))
			elif refseq:
				self.cursor.execute(""" SELECT ProbeSetXRef.*
							FROM
								ProbeSet, ProbeSetXRef, DBList
							WHERE
								ProbeSetXRef.ProbeSetFreezeId = DBList.FreezeId AND
								ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
								(DBList.Name=%s or DBList.Code=%s) AND
								ProbeSet.RefSeq_TranscriptId = %s
							ORDER BY ProbeSetXRef.mean DESC
						    """ , (database, database, refseq))
			elif genbankid:
				self.cursor.execute(""" SELECT ProbeSetXRef.*
							FROM
								ProbeSet, ProbeSetXRef, DBList
							WHERE
								ProbeSetXRef.ProbeSetFreezeId = DBList.FreezeId AND
								ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
								(DBList.Name=%s or DBList.Code=%s) AND
								ProbeSet.GenbankId = %s
							ORDER BY ProbeSetXRef.mean DESC
						    """ , (database, database, genbankid))
			elif geneid:
				self.cursor.execute(""" SELECT ProbeSetXRef.*
							FROM
								ProbeSet, ProbeSetXRef, DBList
							WHERE
								ProbeSetXRef.ProbeSetFreezeId = DBList.FreezeId AND
								ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
								(DBList.Name=%s or DBList.Code=%s) AND
								ProbeSet.GeneId = %s
							ORDER BY ProbeSetXRef.mean DESC
						    """ , (database, database, geneid))

			Results = self.cursor.fetchone()



			########## select the Data that match the selection(currently, only max mean available) ##########
			if Results:
				ProbeSetFreezeId = Results[0]
				ProbeSetId = Results[1]
				DataId = Results[2]

				self.cursor.execute("""
					select
						InbredSet.Name
					from
						InbredSet, ProbeFreeze, ProbeSetFreeze
					where
						InbredSet.Id=ProbeFreeze.InbredSetId and
						ProbeFreeze.Id=ProbeSetFreeze.ProbeFreezeId and
						ProbeSetFreeze.Id=%s
					""", ProbeSetFreezeId)
				fd.RISet = self.cursor.fetchone()[0]
				#fd.RISet = Results[0]

				self.cursor.execute("select Name, FullName from ProbeSetFreeze where Id=%s", ProbeSetFreezeId)
				fd.database, fd.identification = self.cursor.fetchone()

				self.cursor.execute("select Name, symbol, description from ProbeSet where Id=%s", ProbeSetId)
				fd.ProbeSetID, fd.symbol, fd.description = self.cursor.fetchone()

				fd.identification += ' : '+fd.ProbeSetID
				fd.formdata['fullname'] = fd.database+'::'+fd.ProbeSetID

				#XZ, 03/03/2009: Xiaodong changed Data to ProbeSetData	
				self.cursor.execute("select Strain.Name, ProbeSetData.Value from Strain, ProbeSetData where Strain.Id=ProbeSetData.StrainId and ProbeSetData.Id=%s", DataId)
				Results = self.cursor.fetchall()

				fd.allstrainlist = []
				for item in Results:
					fd.formdata[item[0]] = item[1]
					fd.allstrainlist.append(item[0])

				#XZ, 03/12/2009: Xiaodong changed SE to ProbeSetSE
				self.cursor.execute("select Strain.Name, ProbeSetSE.error from Strain, ProbeSetSE where Strain.Id = ProbeSetSE.StrainId and ProbeSetSE.DataId=%s", DataId)
				Results = self.cursor.fetchall()
				for item in Results:
					fd.formdata['V'+item[0]] = item[1]
			else:
				fd.RISet = 'BXD'
				fd.database = 'KI_2A_0405_Rz'
				fd.ProbeSetID = '1367452_at'
		else:
			fd.RISet = 'BXD'
			fd.database = 'KI_2A_0405_Rz'
			fd.ProbeSetID = '1367452_at'


		#BasicStatisticsPage.__init__(self, fd)


		thisTrait = webqtlTrait(db=fd.database, name=fd.ProbeSetID, cursor=self.cursor)
		thisTrait.retrieveInfo()
		thisTrait.retrieveData()
		DataEditingPage.__init__(self, fd, thisTrait)
		self.dict['title'] = '%s: Display Trait' % fd.identification
	def createTableBodies(self, fd, species, tissue, ADMIN_search_dbs):

		this_species_txtOutput = []

		#priority GeneName > refseq > genbankid
		this_species_trait_count = 0 #count of all traits in this species
		this_species_dataset_count = 0 #Number of datasets in this species
		row_count = 0 #Index number used in the first row of the trait table
		trait_tblobj_body = [] #body of table with the results themselves; 
		tissue_tblobj_body = [] #body of table with the number of results for each tissue group
		className = "fs12 fwn b1 c222"

		for i, tissueGrp in enumerate(ADMIN_search_dbs.keys()):
			if tissue and tissue.upper() != tissueGrp.upper():
				continue
			dbNames = ADMIN_search_dbs[tissueGrp]

			tissue_tr = [] #Table row for tissue group
			tissue_tr.append(TDCell(HT.TD('', Class=className)))
			tissue_tr.append(TDCell(HT.TD(tissueGrp.capitalize(), Class=className), tissueGrp, tissueGrp)) #Append cell with tissue name to row

			this_tissue_record_count = 0 #Count of the results for each tissue		
			for dbName in dbNames:
				this_species_dataset_count += 1
				thisDB = webqtlDataset(dbName, self.cursor)

				if fd.geneName:
					if fd.searchAlias:
						self.cursor.execute("""SELECT ProbeSet.Name
											FROM
										ProbeSet, ProbeSetFreeze, ProbeSetXRef
											WHERE
										ProbeSetFreeze.Name = "%s" AND
										ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId AND
										MATCH (ProbeSet.symbol, alias) AGAINST ("+%s" IN BOOLEAN MODE) AND
										ProbeSet.Id = ProbeSetXRef.ProbeSetId""" % (dbName, fd.geneName))
					else:
						self.cursor.execute("""SELECT ProbeSet.Name
											FROM
										ProbeSet, ProbeSetFreeze, ProbeSetXRef
											WHERE
										ProbeSetFreeze.Name = "%s" AND
										ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId AND
										ProbeSet.symbol = "%s" AND
										ProbeSet.Id = ProbeSetXRef.ProbeSetId""" % (dbName, fd.geneName))
				elif fd.refseq:

				# XZ, Oct/08/2009: Search for RefSeq ID is kind of tricky. One probeset can have multiple RefseqIDs that are delimited by ' /// ' (currently). 
				# So I have to use 'like' instead of '=' in SQL query. But user search with one short string, for example 'NM_1', it will return thousands of results. 
				# To prevent this, I set the restriction that the length of input Refseq ID must be at least 9 characters. Otherwise, do NOT start searching. 
				# Even with the restriction of input RefSeqID, I'm still worried about the 'like' in SQL query. My concern is in future, there might be RefSeqIDs with 
				# 10 characters whose first 9 characters are the same as the existing ones. So I decide to further check the result. We should also consider that the 
				# RefSeqID in database may have version number such as "NM_177938.2". If the input RefSeqID is 'NM_177938', it should be matched. I think we should get rid of the version number in database.

					if len(fd.refseq) < 9:
						if fd.returnFmt != 'text':
							heading = "Search Result"
							detail = ["The RefSeq ID that you inputed is less than 9 characters. GeneNetwork thinks it is not a legitimate RefSeq ID and did not do the search. Please try to use a RefSeq ID with at least 9 characters."]
							self.error(heading=heading,detail=detail,error="Not Found")
						else:
							self.output = "#The gene name or IDs you submitted did not match any record in the databases available. You may try different gene names or tissue type."
						return
					else:
						sqlString = """SELECT ProbeSet.Id, ProbeSet.RefSeq_TranscriptId
										FROM
									ProbeSet, ProbeSetFreeze, ProbeSetXRef
										WHERE
									ProbeSetFreeze.Name = "%s" AND
									ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId AND
									MATCH(ProbeSet.RefSeq_TranscriptId) AGAINST ("+%s" IN BOOLEAN MODE) AND
									ProbeSet.Id = ProbeSetXRef.ProbeSetId""" % (dbName, fd.refseq)

						self.cursor.execute(sqlString)

						results = self.cursor.fetchall()
						if results:
							Id_of_really_matched_probeset = []

							for one_result in results:
								ProbeSet_Id, ProbeSet_RefSeq_TranscriptId = one_result
								multiple_RefSeqId = string.split(string.strip(ProbeSet_RefSeq_TranscriptId), '///')
								for one_RefSeqId in multiple_RefSeqId:
									tokens = string.split( one_RefSeqId, '.' )
									one_RefSeqId_without_versionNum = string.strip(tokens[0])
									if one_RefSeqId_without_versionNum == fd.refseq:
										Id_of_really_matched_probeset.append( ProbeSet_Id )
										break

							if Id_of_really_matched_probeset:
								condition_string = " or ".join(["Id = %s" % one_ID for one_ID in Id_of_really_matched_probeset])
								sqlString = """SELECT ProbeSet.Name from ProbeSet where (%s)""" % condition_string

								self.cursor.execute(sqlString)
						else:
							pass

				elif fd.genbankid:
					self.cursor.execute("""SELECT ProbeSet.Name
										FROM
									ProbeSet, ProbeSetFreeze, ProbeSetXRef
										WHERE
									ProbeSetFreeze.Name = "%s" AND
									ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId AND
									ProbeSet.GenbankId = "%s" AND
									ProbeSet.Id = ProbeSetXRef.ProbeSetId""" % (dbName, fd.genbankid))
				elif fd.geneid:
					self.cursor.execute("""SELECT ProbeSet.Name
										FROM
									ProbeSet, ProbeSetFreeze, ProbeSetXRef
										WHERE
									ProbeSetFreeze.Name = "%s" AND
									ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId AND
									ProbeSet.GeneId = "%s" AND
									ProbeSet.Id = ProbeSetXRef.ProbeSetId""" % (dbName, fd.geneid))
				else:
					continue

				results = self.cursor.fetchall()			
				if len(results) > 0:
					this_tissue_record_count += len(results)
					this_species_trait_count += this_tissue_record_count

					for result in results:
				 		_ProbeSetID = result[0]
						thisTrait = webqtlTrait(db=thisDB, name=_ProbeSetID, cursor=self.cursor)
						results_tr = []
						trId = str(thisTrait)
						_traitUrl = thisTrait.genHTML(dispFromDatabase=1)
						_traitName = str(thisTrait)
						
						#ZS: check box column
						results_tr.append(TDCell(HT.TD(str(row_count+1), HT.Input(type="checkbox", Class="checkallbox", name="searchResult",value=trId, onClick="highlight(this)"), nowrap="on", align="right", Class=className), str(row_count+1), row_count+1))
						row_count += 1

						#ZS: Tissue column
						results_tr.append(TDCell(HT.TD(tissueGrp.capitalize(), Class=className), tissueGrp, tissueGrp))

						#ZS: Group column
						risetUrl = HT.Href(text=thisTrait.riset, url="http://www.genenetwork.org/%sCross.html#%s" % (species, thisTrait.riset), target="_blank", Class=className)
						results_tr.append(TDCell(HT.TD(risetUrl, Class=className), thisTrait.riset, thisTrait.riset))

						#ZS: Dataset column
						results_tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.db.fullname, url = webqtlConfig.INFOPAGEHREF % thisTrait.db.name,
								target='_blank', Class="fs13 fwn non_bold"), Class=className), thisTrait.db.name.upper(), thisTrait.db.name.upper()))

						#ZS: Trait ID column
						results_tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.getGivenName(),url="javascript:showDatabase3('%s','%s','%s','')" % ('showDatabase', thisTrait.db.name, thisTrait.name), Class="fs12 fwn"), nowrap="yes",align="left", Class=className),str(thisTrait.name), thisTrait.name))

						#ZS: Symbol column and Description column
						description_string = str(thisTrait.description).strip()
						if (thisTrait.db.type == "ProbeSet"):
							target_string = str(thisTrait.probe_target_description).strip()

            						description_display = ''

							if len(description_string) > 1 and description_string != 'None':
								description_display = description_string
							else:
           		    					description_display = thisTrait.symbol

            						if len(description_display) > 1 and description_display != 'N/A' and len(target_string) > 1 and target_string != 'None':
                						description_display = description_display + '; ' + target_string.strip()
					
							description_string = description_display
						else:
							results_tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))
							
            					results_tr.append(TDCell(HT.TD(description_string, Class=className), description_string, description_string))

						#XZ: trait_location_value is used for sorting
						trait_location_repr = "--"
						trait_location_value = 1000000
	
						if hasattr(thisTrait, 'chr') and hasattr(thisTrait, 'mb') and thisTrait.chr and thisTrait.mb:
							try:
								trait_location_value = int(thisTrait.chr)*1000 + thisTrait.mb
							except:
								if thisTrait.chr.upper() == "X":
									trait_location_value = 20*1000 + thisTrait.mb
								else:
									trait_location_value = ord(str(thisTrait.chr).upper()[0])*1000 + thisTrait.mb
						
							trait_location_repr = "Chr%s: %.6f" % (thisTrait.chr, float(thisTrait.mb) )
				
						results_tr.append(TDCell(HT.TD(trait_location_repr, nowrap='ON', Class=className), trait_location_repr, trait_location_value))
	
						#ZS: Mean column
	           	 			self.cursor.execute("""
	                   	 			select ProbeSetXRef.mean from ProbeSetXRef, ProbeSet
	                   	 			where ProbeSetXRef.ProbeSetFreezeId = %d and
	                        	  			ProbeSet.Id = ProbeSetXRef.ProbeSetId and
	                        	  			ProbeSet.Name = '%s'
	            				""" % (thisTrait.db.id, thisTrait.name))
	            				result = self.cursor.fetchone()
	            				if result:
	                				if result[0]:
	                    					mean = result[0]
	                				else:
	                    					mean=0
	            				else:
	                				mean = 0
	
	            				repr = "%2.3f" % mean
	            				results_tr.append(TDCell(HT.TD(repr, Class=className, align='right', nowrap='ON'),repr, mean))
						trait_tblobj_body.append(results_tr)

						this_species_txtOutput.append([tissueGrp, thisTrait])


			tissue_tr.append(TDCell(HT.TD(str(this_tissue_record_count), Class=className), str(this_tissue_record_count), this_tissue_record_count))
			tissue_tblobj_body.append(tissue_tr)
	
		self.output = "self.output"

		return tissue_tblobj_body, trait_tblobj_body, this_species_dataset_count, this_species_trait_count, this_species_txtOutput
    def __init__(self,fd):

        templatePage.__init__(self, fd)

        if not self.openMysql():
            return

        TD_LR = HT.TD(colspan=2,height=200,width="100%",bgColor='#eeeeee')

        TD_LR.append(HT.Paragraph("Partial Correlation Table", Class="title"), '\n')

        pc_method = fd.formdata.getvalue('pcMethod')

        primaryTraitString = fd.formdata.getvalue('primaryTrait')
        primaryTrait = (webqtlTrait(fullname=primaryTraitString, cursor=self.cursor))

        controlTraitsString = fd.formdata.getvalue('controlTraits')
        controlTraitsList = list(string.split(controlTraitsString,','))
        controlTraits = []
        for item in controlTraitsList:
            controlTraits.append(webqtlTrait(fullname=item, cursor=self.cursor))

        targetTraitsString = fd.formdata.getvalue('targetTraits')
        targetTraitsList = list(string.split(targetTraitsString,','))
        targetTraits = []
        _targetnames = []
        for item in targetTraitsList:
            oneTargetTrait = webqtlTrait(fullname=item, cursor=self.cursor)
            oneTargetTrait.retrieveInfo()
            targetTraits.append( oneTargetTrait )
            _targetnames.append( oneTargetTrait.name )

        #XZ: filter out the strains that have no value.
        primaryTrait.retrieveData()
        _strains, _vals, _vars = primaryTrait.exportInformative()

        #XZ: _controlstrains, _controlvals and _controlvars are list of list [ [], [], ...]. _controlNs is number
        _controlstrains,_controlvals,_controlvars,_controlNs = correlationFunction.controlStrains(controlTraitsString,_strains)

        N = len(_strains)

        allsame = True
        ##allsame is boolean for whether or not primary and control trait have values for the same strains
        for i in _controlstrains:
                if _strains != i:
                        allsame=False
                        break

        ##   If the strains for which each of the control traits and the primary trait have values are not identical, 
        ## we must remove from the calculation all vlaues for strains that are not present in each. Without doing this,
        ## undesirable biases would be introduced.
        # XZ, 01/11/2010: After execution of function fixStrains, variables _vals,_controlvals,_vars,_controlvars have the same number and same order of strains as strains in variable _strains. The _controlstrains remains intact.
        if not allsame:
                _strains,_vals,_controlvals,_vars,_controlvars = correlationFunction.fixStrains(_strains,_controlstrains,_vals,_controlvals,_vars,_controlvars)
                N = len(_strains)

        #XZ: We should check the value of control trait and primary trait here.
        nameOfIdenticalTraits = correlationFunction.findIdenticalTraits ( _vals, primaryTraitString, _controlvals, controlTraitsList )
        if nameOfIdenticalTraits:
            heading = "Partial Correlation Table"
            detail = ['%s and %s have same values for the %s strains that will be used to calculate partial correlation (common for all primary and control traits). In such case, partial correlation can NOT be calculated. Please re-select your traits.' % (nameOfIdenticalTraits[0], nameOfIdenticalTraits[1], len(_vals))]
            self.error(heading=heading,detail=detail)
            return


        if N < self.corrMinInformative:
            heading = "Partial Correlation Table"
            detail = ['Fewer than %d strain data were entered for %s data set. No calculation of correlation has been attempted.' % (self.corrMinInformative, fd.RISet)]
            self.error(heading=heading,detail=detail)
            return

        #XZ, 01/11/2010: Pay attention to the target trait strain number and order!
        #XZ 03/29/2010: need to input target trait values to this function.

        _targetvals = []
        for oneTargetTrait in targetTraits:
            oneTargetTrait.retrieveData()
            oneTraitVals = oneTargetTrait.exportData( _strains )
            _targetvals.append(oneTraitVals)


        if pc_method == 'spearman':
            allcorrelations = correlationFunction.determinePartialsByR(primaryVal = _vals, controlVals = _controlvals, targetVals = _targetvals, targetNames = _targetnames, method='s')
        else:
            allcorrelations = correlationFunction.determinePartialsByR(primaryVal = _vals, controlVals = _controlvals, targetVals = _targetvals, targetNames = _targetnames)

        totalTraits = len(allcorrelations)


        info_form = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase', submit=HT.Input(type='hidden'))

        hddn = {'FormID':'showDatabase', 'database':'_', 'ProbeSetID':'_', 'CellID':'_' }#XZ: These four parameters are required by javascript function showDatabase2.
        
        for key in hddn.keys():
            info_form.append(HT.Input(name=key, value=hddn[key], type='hidden'))

        info_form.append(HT.Paragraph("Primary Trait", Class="subtitle"), '\n')

        primaryTraitTable = HT.TableLite(cellSpacing=4,cellPadding=0,width="90%",border=0)

        descriptionString = primaryTrait.genHTML(dispFromDatabase=1)
        if primaryTrait.db.type == 'Publish' and primaryTrait.confidential:
            descriptionString = primaryTrait.genHTML(dispFromDatabase=1, privilege=self.privilege, userName=self.userName, authorized_users=primaryTrait.authorized_users)
        primaryTraitTable.append(HT.TR(HT.TD(HT.Href(text='%s' % descriptionString, url="javascript:showDatabase2('%s','%s','%s')" % (primaryTrait.db.name,primaryTrait.name,primaryTrait.cellid), Class="fs12 fwn") )))

        info_form.append(primaryTraitTable)

        info_form.append(HT.Paragraph("Control Traits", Class="subtitle"), '\n')

        controlTraitsTable = HT.TableLite(cellSpacing=4,cellPadding=0,width="90%",border=0)

        seq = 1

        ## Generate the listing table for control traits
        for thisTrait in controlTraits:
            descriptionString = thisTrait.genHTML(dispFromDatabase=1)
            if thisTrait.db.type == 'Publish' and thisTrait.confidential:
                descriptionString = thisTrait.genHTML(dispFromDatabase=1, privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users)
            controlTraitsTable.append(HT.TR(HT.TD("%d."%seq,align="left", width=10),
                                            HT.TD(HT.Href(text='%s' % descriptionString,url="javascript:showDatabase2('%s','%s','%s')" % (thisTrait.db.name,thisTrait.name,thisTrait.cellid), Class="fs12 fwn") )))
            seq += 1

        info_form.append(controlTraitsTable)


        TD_LR.append(info_form)        


        mainfmName = webqtlUtil.genRandStr("fm_")
        form = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name= mainfmName, submit=HT.Input(type='hidden'))

        hddn = {'FormID':'showDatabase', 'database':'_', 'ProbeSetID':'_', 'CellID':'_' }#XZ: These four parameters are required by javascript function showDatabase2.

        for key in hddn.keys():
            form.append(HT.Input(name=key, value=hddn[key], type='hidden'))


        filename= webqtlUtil.genRandStr("Corr_")

        tblobj = {}

        if pc_method == 'spearman':
            tblobj['header'] = \
            [[THCell(HT.TD('', Class="fs13 fwb ffl b1 cw cbrb"), sort=0),
              THCell(HT.TD('Database', Class="fs13 fwb ffl b1 cw cbrb",align='center'), text='db', idx=1),
              THCell(HT.TD('Record', Class="fs13 fwb ffl b1 cw cbrb",align='center'), text='id', idx=2),
              THCell(HT.TD('Symbol', Class="fs13 fwb ffl b1 cw cbrb"), text='symbol', idx=3),
              THCell(HT.TD('Description', Class="fs13 fwb ffl b1 cw cbrb", align='center'), text='desc', idx=4),
              THCell(HT.TD('N ', nowrap="on", Class="fs13 fwb ffl b1 cw cbrb"), text='nstr', idx=5),
              THCell(HT.TD('Partial rho ', nowrap="on", Class="fs13 fwb ffl b1 cw cbrb"), text='partial_corr', idx=6),
              THCell(HT.TD('p(partial rho)', Class="fs13 fwb ffl b1 cw cbrb",nowrap='ON'), text='partial_pv', idx=7),
              THCell(HT.TD('rho ', nowrap="on", Class="fs13 fwb ffl b1 cw cbrb"), text='corr', idx=8),
              THCell(HT.TD('p(rho)', Class="fs13 fwb ffl b1 cw cbrb",nowrap='ON'), text='pv', idx=9),
              THCell(HT.TD('delta rho', Class="fs13 fwb ffl b1 cw cbrb",nowrap='ON'), text='delta_rho', idx=10)]]
        else:
            tblobj['header'] = \
            [[THCell(HT.TD('', Class="fs13 fwb ffl b1 cw cbrb"), sort=0),
              THCell(HT.TD('Database', Class="fs13 fwb ffl b1 cw cbrb",align='center'), text='db', idx=1),
              THCell(HT.TD('Record', Class="fs13 fwb ffl b1 cw cbrb",align='center'), text='id', idx=2),
              THCell(HT.TD('Symbol', Class="fs13 fwb ffl b1 cw cbrb"), text='symbol', idx=3),
              THCell(HT.TD('Description', Class="fs13 fwb ffl b1 cw cbrb", align='center'), text='desc', idx=4),
              THCell(HT.TD('N ', nowrap="on", Class="fs13 fwb ffl b1 cw cbrb"), text='nstr', idx=5),
              THCell(HT.TD('Partial r ', nowrap="on", Class="fs13 fwb ffl b1 cw cbrb"), text='partial_corr', idx=6),
              THCell(HT.TD('p(partial r)', Class="fs13 fwb ffl b1 cw cbrb",nowrap='ON'), text='partial_pv', idx=7),
              THCell(HT.TD('r ', nowrap="on", Class="fs13 fwb ffl b1 cw cbrb"), text='corr', idx=8),
              THCell(HT.TD('p(r)', Class="fs13 fwb ffl b1 cw cbrb",nowrap='ON'), text='pv', idx=9),
              THCell(HT.TD('delta r', Class="fs13 fwb ffl b1 cw cbrb",nowrap='ON'), text='delta_r', idx=10)]]

        sortby = ("partial_pv", "up")

        tblobj['body'] = []
        for i, thisTrait in enumerate(targetTraits):
            tr = []

            trId = str(thisTrait)
            tr.append(TDCell(HT.TD(HT.Input(type="checkbox", Class="checkbox", name="searchResult",value=trId, onClick="highlight(this)"), nowrap="on", Class="fs12 fwn ffl b1 c222"), text=trId))
            tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.db.fullname,url=webqtlConfig.INFOPAGEHREF % thisTrait.db.name,target="_blank", Class="fs12 fwn"), Class="fs12 fwn ffl b1 c222"), text=thisTrait.db.name, val=thisTrait
.db.name.upper()))
            tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.name,url="javascript:showDatabase3('%s', '%s', '%s', '%s')" % (mainfmName,thisTrait.db.name,thisTrait.name,thisTrait.cellid), Class="fs12 fwn"), Class="fs12 fwn b1 c222"), text=thisTrait.name, val=thisTrait.name))

            #XZ: Symbol column
            if thisTrait.db.type =="ProbeSet":
                if thisTrait.symbol:
                    tr.append(TDCell(HT.TD(thisTrait.symbol, Class="fs12 fwn ffl b1 c222"), text=thisTrait.symbol, val=thisTrait.symbol.upper()))
                else:
                    tr.append(TDCell(HT.TD('NA', Class="fs12 fwn ffl b1 c222"), text='NA', val='NA'))
            elif thisTrait.db.type =="Publish":
                AbbreviationString = "--"
                if (thisTrait.post_publication_abbreviation != None):
                    AbbreviationString = thisTrait.post_publication_abbreviation

                if thisTrait.confidential:
                    if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users):
                        if thisTrait.pre_publication_abbreviation:
                            AbbreviationString = thisTrait.pre_publication_abbreviation
                        else:
                            AbbreviationString = "--"

                if AbbreviationString == "--":
                    tr.append(TDCell(HT.TD('NA', Class="fs12 fwn ffl b1 c222"), text='NA', val='NA'))
                else:
                    tr.append(TDCell(HT.TD(AbbreviationString, Class="fs12 fwn ffl b1 c222"), text=AbbreviationString, val=AbbreviationString.upper()))
            else:
                tr.append(TDCell(HT.TD(thisTrait.name, Class="fs12 fwn ffl b1 c222"), text=thisTrait.name, val=thisTrait.name))

            #XZ: Description column
            if thisTrait.db.type =="ProbeSet" or thisTrait.db.type == "Temp":
                tr.append(TDCell(HT.TD(thisTrait.description, Class="fs12 fwn ffl b1 c222"), text=thisTrait.description, val=thisTrait.description.upper()))
            elif thisTrait.db.type =="Publish":
		PhenotypeString = thisTrait.post_publication_description
		if thisTrait.confidential:
			if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users):
				PhenotypeString = thisTrait.pre_publication_description
                tr.append(TDCell(HT.TD(PhenotypeString, Class="fs12 fwn ffl b1 c222"), text=PhenotypeString, val=PhenotypeString.upper()))
            else:
                tr.append(TDCell(HT.TD(thisTrait.name, Class="fs12 fwn ffl b1 c222"), text=thisTrait.name, val=thisTrait.name))

            tr.append(TDCell(HT.TD(allcorrelations[i][1], Class="fs12 fwn ffl b1 c222", align='right'), text=allcorrelations[i][1], val=allcorrelations[i][1]))

            #partial correlation result
            try:
                repr = '%3.3f' % float(allcorrelations[i][2])
                tr.append(TDCell(HT.TD(repr, Class="fs12 fwn ffl b1 c222", align='right'), text=repr, val=abs(allcorrelations[i][2])))
            except:
                repr = 'NA'
                tr.append(TDCell(HT.TD(repr, Class="fs12 fwn ffl b1 c222", align='left'), text=repr, val=0 ))

            repr = webqtlUtil.SciFloat(allcorrelations[i][3])
            tr.append(TDCell(HT.TD(repr, Class="fs12 fwn ffl b1 c222", nowrap='ON', align='right'), text=repr, val=allcorrelations[i][3]))

            #zero order correlation result
            repr = '%3.3f' % float(allcorrelations[i][4])
            tr.append(TDCell(HT.TD(repr, Class="fs12 fwn ffl b1 c222", align='right'), text=repr, val=abs(allcorrelations[i][4])))

            repr = webqtlUtil.SciFloat(allcorrelations[i][5])
            tr.append(TDCell(HT.TD(repr, Class="fs12 fwn ffl b1 c222", nowrap='ON', align='right'), text=repr, val=allcorrelations[i][5]))

            #delta
            try:
                repr = '%3.3f' % ( float(allcorrelations[i][2]) - float(allcorrelations[i][4]) )
                tr.append(TDCell(HT.TD(repr, Class="fs12 fwn ffl b1 c222", align='right'), text=repr, val=repr ))
            except:
                repr = 'NA'
                tr.append(TDCell(HT.TD(repr, Class="fs12 fwn ffl b1 c222", align='left'), text=repr, val=0 ))

            tblobj['body'].append(tr)

        objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb')
        cPickle.dump(tblobj, objfile)
        objfile.close()
		# NL, 07/27/2010. genTableObj function has been moved from templatePage.py to webqtlUtil.py;
        div = HT.Div(webqtlUtil.genTableObj(tblobj=tblobj, file=filename, sortby=sortby, tableID = "sortable", addIndex = "1"), Id="sortable")
        form.append(div)


        TD_LR.append(HT.Center(form),HT.P())

        self.dict['body'] =  str(TD_LR)
		# updated by NL, moved js function xmlhttpPost() and updatepage() to dhtml.js
        self.dict['js1'] = ''
        self.dict['title'] = 'Partial Correlation Result'
    def __init__(self, fd):

        templatePage.__init__(self, fd)

        if not self.openMysql():
            return

        fd.incparentsf1 = 1
        if not fd.genotype:
            fd.readGenotype()

        locusChr = {}
        locusMb = {}
        for chr in fd.genotype:
            for locus in chr:
                locusChr[locus.name] = locus.chr
                locusMb[locus.name] = locus.Mb

        self.searchResult = fd.formdata.getvalue("searchResult")

        if not self.searchResult:
            templatePage.__init__(self, fd)
            heading = "Export Collection"
            detail = ["You need to select at least one trait to export."]
            self.error(heading=heading, detail=detail)
            return

        self.RISet = fd.formdata.getvalue("RISet")
        self.cursor.execute(
            "Select Species.Name from Species, InbredSet where InbredSet.SpeciesId = Species.Id and InbredSet.Name = '%s'"
            % self.RISet
        )
        self.Species = self.cursor.fetchone()[0]

        if type("1") == type(self.searchResult):
            self.searchResult = string.split(self.searchResult, "\t")
        strainlist = fd.f1list + fd.strainlist
        fields = [
            "ID",
            "Species",
            "Cross",
            "Database",
            "ProbeSetID / RecordID",
            "Symbol",
            "Description",
            "ProbeTarget",
            "PubMed_ID",
            "Phenotype",
            "Chr",
            "Mb",
            "Alias",
            "Gene_ID",
            "HomoloGene_ID",
            "UniGene_ID",
            "Strand_Probe ",
            "Strand_Gene ",
            "Probe_set_specificity",
            "Probe_set_BLAT_score",
            "Probe_set_BLAT_Mb_start",
            "Probe_set_BLAT_Mb_end ",
            "QTL_Chr",
            "QTL_Mb",
            "Locus_at_Peak",
            "Max_LRS",
            "P_value_of_MAX",
            "Mean_Expression",
        ] + strainlist

        if self.searchResult:
            traitList = []
            for item in self.searchResult:
                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                thisTrait.retrieveInfo(QTL=1)
                thisTrait.retrieveData(strainlist=strainlist)
                traitList.append(thisTrait)

            text = [fields]
            for i, thisTrait in enumerate(traitList):
                if thisTrait.db.type == "ProbeSet":
                    if not thisTrait.cellid:  # ProbeSet
                        # 12/22/2009, XZ: We calculated LRS for each marker(locus) in geno file and record the max LRS and its corresponding marker in MySQL database. But after the calculation, Rob deleted several markers. If one of the deleted markers happen to be the one recorded in database, error will occur. So we have to deal with this situation.
                        if locusChr.has_key(thisTrait.locus) and locusMb.has_key(thisTrait.locus):
                            text.append(
                                [
                                    str(i + 1),
                                    self.Species,
                                    self.RISet,
                                    thisTrait.db.fullname,
                                    thisTrait.name,
                                    thisTrait.symbol,
                                    thisTrait.description,
                                    thisTrait.probe_target_description,
                                    "",
                                    "",
                                    thisTrait.chr,
                                    thisTrait.mb,
                                    thisTrait.alias,
                                    thisTrait.geneid,
                                    thisTrait.homologeneid,
                                    thisTrait.unigeneid,
                                    thisTrait.strand_probe,
                                    thisTrait.strand_gene,
                                    thisTrait.probe_set_specificity,
                                    thisTrait.probe_set_blat_score,
                                    thisTrait.probe_set_blat_mb_start,
                                    thisTrait.probe_set_blat_mb_end,
                                    locusChr[thisTrait.locus],
                                    locusMb[thisTrait.locus],
                                    thisTrait.locus,
                                    thisTrait.lrs,
                                    thisTrait.pvalue,
                                ]
                            )
                        else:
                            text.append(
                                [
                                    str(i + 1),
                                    self.Species,
                                    self.RISet,
                                    thisTrait.db.fullname,
                                    thisTrait.name,
                                    thisTrait.symbol,
                                    thisTrait.description,
                                    thisTrait.probe_target_description,
                                    "",
                                    "",
                                    thisTrait.chr,
                                    thisTrait.mb,
                                    thisTrait.alias,
                                    thisTrait.geneid,
                                    thisTrait.homologeneid,
                                    thisTrait.unigeneid,
                                    thisTrait.strand_probe,
                                    thisTrait.strand_gene,
                                    thisTrait.probe_set_specificity,
                                    thisTrait.probe_set_blat_score,
                                    thisTrait.probe_set_blat_mb_start,
                                    thisTrait.probe_set_blat_mb_end,
                                    "",
                                    "",
                                    "",
                                    "",
                                    "",
                                ]
                            )
                    else:  # Probe
                        text.append(
                            [
                                str(i + 1),
                                self.Species,
                                self.RISet,
                                thisTrait.db.fullname,
                                thisTrait.name + " : " + thisTrait.cellid,
                                thisTrait.symbol,
                                thisTrait.description,
                                thisTrait.probe_target_description,
                                "",
                                "",
                                thisTrait.chr,
                                thisTrait.mb,
                                thisTrait.alias,
                                thisTrait.geneid,
                                thisTrait.homologeneid,
                                thisTrait.unigeneid,
                                "",
                                "",
                                "",
                                "",
                                "",
                                "",
                                "",
                                "",
                                "",
                                "",
                                "",
                            ]
                        )

                elif thisTrait.db.type == "Publish":
                    # XZ: need to consider confidential phenotype
                    PhenotypeString = thisTrait.post_publication_description
                    if thisTrait.confidential:
                        if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(
                            privilege=self.privilege,
                            userName=self.userName,
                            authorized_users=thisTrait.authorized_users,
                        ):
                            PhenotypeString = thisTrait.pre_publication_description
                    text.append(
                        [
                            str(i + 1),
                            self.Species,
                            self.RISet,
                            thisTrait.db.fullname,
                            thisTrait.name,
                            "",
                            "",
                            "",
                            thisTrait.pubmed_id,
                            PhenotypeString,
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                        ]
                    )
                elif thisTrait.db.type == "Temp":
                    text.append(
                        [
                            str(i + 1),
                            self.Species,
                            self.RISet,
                            thisTrait.db.fullname,
                            thisTrait.name,
                            "",
                            thisTrait.description,
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                        ]
                    )
                elif thisTrait.db.type == "Geno":
                    text.append(
                        [
                            str(i + 1),
                            self.Species,
                            self.RISet,
                            thisTrait.db.fullname,
                            thisTrait.name,
                            "",
                            thisTrait.name,
                            "",
                            "",
                            "",
                            thisTrait.chr,
                            thisTrait.mb,
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                            "",
                        ]
                    )
                else:
                    continue

                testval = thisTrait.exportData(strainlist)
                try:
                    mean = reaper.anova(testval)[0]
                except:
                    count = 0
                    sum = 0
                    for oneValue in testval:
                        try:
                            oneValue = float(oneValue)
                            sum = sum + oneValue
                            count = count + 1
                        except:
                            pass
                    if count == 0:
                        mean = 0
                    else:
                        mean = sum / count
                text[-1].append(mean)
                text[-1] += testval
            if len(text[0]) < 255 or len(text) < 255:
                transpose = 0
                if len(text[0]) >= 255:
                    text = webqtlUtil.transpose(text)
                    transpose = 1
                filename = os.path.join(webqtlConfig.TMPDIR, webqtlUtil.generate_session() + ".xls")

                # Create a new Excel workbook
                workbook = xl.Writer(filename)
                worksheet = workbook.add_worksheet()
                headingStyle = workbook.add_format(align="center", bold=1, size=13, color="green")
                titleStyle = workbook.add_format(align="left", bold=0, size=13, border=1, border_color="gray")

                ##Write title Info
                # Modified by Hongqiang Li
                # worksheet.write([0, 0], "Data source: The GeneNetwork at web2qtl.utmem.edu:88", titleStyle)
                # worksheet.write([1, 0], "Citations: Please see web2qtl.utmem.edu:88/reference.html", titleStyle)
                worksheet.write([0, 0], "Data source: The GeneNetwork at %s" % webqtlConfig.PORTADDR, titleStyle)
                worksheet.write([1, 0], "Citations: Please see %s/reference.html" % webqtlConfig.PORTADDR, titleStyle)
                #
                worksheet.write([2, 0], "Date : %s" % time.strftime("%B %d, %Y", time.gmtime()), titleStyle)
                worksheet.write([3, 0], "Time : %s GMT" % time.strftime("%H:%M ", time.gmtime()), titleStyle)

                # Modified by Hongqiang Li
                # worksheet.write([4, 0], "Status of data ownership: Possibly unpublished data; please see web2qtl.utmem.edu:88/statusandContact.html for details on sources, ownership, and usage of these data.", titleStyle)
                worksheet.write(
                    [4, 0],
                    "Status of data ownership: Possibly unpublished data; please see %s/statusandContact.html for details on sources, ownership, and usage of these data."
                    % webqtlConfig.PORTADDR,
                    titleStyle,
                )
                #
                worksheet.write(
                    [6, 0],
                    "This output file contains data from %d GeneNetwork databases listed below" % len(traitList),
                    titleStyle,
                )

                # Row and column are zero indexed
                nrow = startRow = 8
                for row in text:
                    for ncol, cell in enumerate(row):
                        if nrow == startRow:
                            worksheet.write([nrow, ncol], cell.strip(), headingStyle)
                            worksheet.set_column([ncol, ncol], 2 * len(cell))
                        else:
                            worksheet.write([nrow, ncol], cell)
                    nrow += 1

                worksheet.write(
                    [nrow + 1, 0],
                    "Funding for The GeneNetwork: NIAAA (U01AA13499, U24AA13513), NIDA, NIMH, and NIAAA (P20-DA 21131), NCI MMHCC (U01CA105417), and NCRR (U24 RR021760)",
                    titleStyle,
                )
                worksheet.write([nrow + 2, 0], "PLEASE RETAIN DATA SOURCE INFORMATION WHENEVER POSSIBLE", titleStyle)
                workbook.close()

                fp = open(filename, "rb")
                text = fp.read()
                fp.close()

                self.content_type = "application/xls"
                self.content_disposition = "attachment; filename=%s" % (
                    "export-%s.xls" % time.strftime("%y-%m-%d-%H-%M")
                )
                self.attachment = text
            else:
                self.content_type = "application/xls"
                self.content_disposition = "attachment; filename=%s" % (
                    "export-%s.txt" % time.strftime("%y-%m-%d-%H-%M")
                )
                for item in text:
                    self.attachment += string.join(map(str, item), "\t") + "\n"
            self.cursor.close()
        else:
            fd.req.content_type = "text/html"
            heading = "Export Collection"
            detail = [
                HT.Font("Error : ", color="red"),
                HT.Font("Error occurs while retrieving data from database.", color="black"),
            ]
            self.error(heading=heading, detail=detail)
	def __init__(self, fd):

		templatePage.__init__(self, fd)

		if not self.openMysql():
			return
		
		fd.readGenotype()
		TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee')
		self.database = fd.formdata.getfirst('database')
		self.ProbeSetID = fd.formdata.getfirst('ProbeSetID')
		self.CellID = fd.formdata.getfirst('CellID')
		
		self.db = webqtlDataset(self.database, self.cursor)
		thisTrait = webqtlTrait(db= self.db, cursor=self.cursor, name=self.ProbeSetID) #, cellid=CellID)
		thisTrait.retrieveInfo()
		
		try:
			self.cursor.execute('SELECT ProbeFreeze.Name FROM ProbeFreeze,ProbeSetFreeze WHERE ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId and ProbeSetFreeze.Name = "%s"' % self.db.name)
			self.probeDatabase = self.cursor.fetchall()[0][0]
			self.probeInfoDatabase = 'Probe'
		except:
			heading = 'Probe Information'
			intro = ['Trying to retrieve the probe information for ProbeSet ',HT.Span('%s' % self.ProbeSetID, Class="fwb cdg"),' in Database ',HT.Href(text='%s' % self.db.fullname,url=webqtlConfig.infopagehref % self.database)]
			detail = ['The information you just requested is not available at this time.']
			self.error(heading=heading,intro=intro,detail=detail)
			return


		form = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase', submit=HT.Input(type='hidden'))
		hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':'_','CellID':'_','RISet':fd.RISet, 'incparentsf1':'on'}
		if fd.RISet == 'BXD':
			hddn['parentsf1']='ON'
			
		for key in hddn.keys():
			form.append(HT.Input(name=key, value=hddn[key], type='hidden'))


		#Buttons on search page
		linkinfo ="%s/probeInfo.html" % webqtlConfig.PORTADDR
		mintmap = "" 
		probeinfo = HT.Input(type='button' ,name='mintmap',value='Info', onClick="openNewWin('%s');" % linkinfo, Class="button")
		cormatrix = HT.Href(url="#redirect", onClick="databaseFunc(document.getElementsByName('showDatabase')[0], 'corMatrix');")
		cormatrix_img = HT.Image("/images/correlation_matrix1_final.jpg", alt="Correlation Matrix and PCA", title="Correlation Matrix and PCA", style="border:none;")
		cormatrix.append(cormatrix_img)
		heatmap = HT.Href(url="#redirect", onClick="databaseFunc(document.getElementsByName('showDatabase')[0], 'heatmap');")
		heatmap_img = HT.Image("/images/heatmap2_final.jpg", name='mintmap', alt="QTL Heat Map and Clustering", title="QTL Heatmap and Clustering", style="border:none;")
		heatmap.append(heatmap_img)
		if self.ProbeSetID[-2:] in ('_A', '_B'):
			thisProbeSetID = self.ProbeSetID[:-2]
		else:
			thisProbeSetID = self.ProbeSetID
		thisurl = 'http://www.ensembl.org/Mus_musculus/featureview?type=AffyProbe&id=%s' % thisProbeSetID
		verifyButton = HT.Input(type="button",value="Verify Ensembl",onClick= "openNewWin('%s')" % thisurl, Class="button")
		
		addselect = HT.Input(type='button' ,name='addselect',value='Add to Collection', onClick="addRmvSelection('%s', this.form, 'addToSelection');"  % fd.RISet,Class="button")
		selectall = HT.Input(type='button' ,name='selectall',value='Select All', onClick="checkAll(this.form);",Class="button")
		selectpm = HT.Input(type='button' ,name='selectall',value='Select PM', onClick="checkPM(this.form);",Class="button")
		selectmm = HT.Input(type='button' ,name='selectall',value='Select MM', onClick="checkMM(this.form);",Class="button")
		selectinvert = HT.Input(type='button' ,name='selectinvert',value='Select Invert', onClick="checkInvert(this.form);",Class="button")
		reset = HT.Input(type='reset',name='',value='Select None',Class="button")
		chrMenu = HT.Input(type='hidden',name='chromosomes',value='all')
		probedata = HT.Input(type='hidden',name='probedata',value='all')
		
		url_rudi_track = self.getProbeTrackURL(self.probeDatabase, self.ProbeSetID)
		if url_rudi_track:		
		   rudi_track = HT.Input(type='button', name='ruditrack', value='Probe Track', onClick="openNewWin('%s')"%url_rudi_track, Class="button")
		else: rudi_track = None
		
		pinfopage = "/probeInfo.html"
		
		#updated by NL: 07-22-2011  get chosenStrains
		_f1, _f12, _mat, _pat = webqtlUtil.ParInfo[fd.RISet]
		chosenStrains="%s,%s"%(_mat,_pat)
		tblobj = {}
		tblobj['header']=[]

		tblobj['header'].append([
			THCell(HT.TD("", Class="cbrb cw fwb fs13 b1", rowspan=2,nowrap='ON'), sort=0),
			THCell(HT.TD(HT.Href(target="_PROBEINFO", url=pinfopage+"#probe", text=HT.Span('Probe', Class="cw fwb fs13")), HT.Sup(HT.Italic('1')), Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,nowrap='ON'), text="probe", idx=1), 
			THCell(HT.TD(HT.Href(text=HT.Span('Sequence', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#Sequence"),HT.Sup(HT.Italic('2')), Class="cbrb cw fwb fs13 b1", align='center',rowspan=2,nowrap='ON'), text="seq", idx=2), 
			THCell(HT.TD(HT.Href(text=HT.Span('bl2seq', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#bl2seq"),HT.Sup(HT.Italic('3')), Class="cbrb cw fwb fs13 b1", align='center',rowspan=2,nowrap='ON'), sort=0), 
			THCell(HT.TD(HT.Href(text=HT.Span('Exons', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#Exon"),HT.Sup(HT.Italic('4')), Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,nowrap='ON'), sort=0),
			THCell(HT.TD(HT.Href(text=HT.Span('Tm &deg;C', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#Tm"),HT.Sup(HT.Italic('5')), Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,nowrap='ON'), text="tm", idx=5),
			THCell(HT.TD(HT.Href(text=HT.Span('Stacking Energy K', HT.Sub('B'),'T', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#KBT"),HT.Sup(HT.Italic('6')), Class="cbrb cw fwb fs13 b1",align='center',colspan=2,NOWRAP="yes",nowrap='ON'), sort=0),
			THCell(HT.TD(HT.Href(text=HT.Span('Mean', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#Mean"),HT.Sup(HT.Italic('7')), Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,nowrap='ON'), text="mean", idx=8),
			THCell(HT.TD(HT.Href(text=HT.Span('Stdev', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#Stdev"),HT.Sup(HT.Italic('8')), Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,nowrap='ON'), text="std", idx=9),
			THCell(HT.TD(HT.Href(text=HT.Span('Probe h2', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#h2"),HT.Sup(HT.Italic('9')), Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,NOWRAP="yes"), text="h2", idx=10),
			THCell(HT.TD(HT.Href(text=HT.Span('Probe Location', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#location"), HT.Sup(HT.Italic('10')),Class="cbrb cw fwb fs13 b1",align='center',colspan=3)),
			THCell(HT.TD(HT.Href(text=HT.Span('SNPs', HT.BR(), '(Across all strains)', Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#snps"), HT.Sup(HT.Italic('11')),Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,NOWRAP="yes")),
			THCell(HT.TD(HT.Href(text=HT.Span('SNPs', HT.BR(),'(Different alleles only between %s and %s)'%(_mat,_pat), Class="cw fwb fs13"), target="_PROBEINFO", url=pinfopage+"#snps"), HT.Sup(HT.Italic('11')),Class="cbrb cw fwb fs13 b1",align='center',rowspan=2,NOWRAP="yes"))

		])
		
		tblobj['header'].append([
			THCell(HT.TD(HT.Span('GSB', Class="cw fwb fs13"),align='center', Class="cbrb ffl fwb fs13 b1",), text="gsb", idx=6),
			THCell(HT.TD(HT.Span('NSB', Class="cw fwb fs13"),align='center', Class="cbrb ffl fwb fs13 b1",), text="nsb", idx=7),
			THCell(HT.TD(HT.Span('Chr', Class="cw fwb fs13"), align='center', Class="cbrb ffl2 fwb fs13 b1",)),
			THCell(HT.TD(HT.Span('Start', Class="cw fwb fs13"),align='center', Class="cbrb ffl fwb fs13 b1",)),
			THCell(HT.TD(HT.Span('End', Class="cw fwb fs13"),align='center', Class="cbrb ffl fwb fs13 b1",)),
		])
		
		tblobj['body'] = []
		
		blatbutton = ''

		fetchField = ['Probe.Name','Probe.Sequence','Probe.ExonNo','Probe.Tm', 'Probe.E_GSB','Probe.E_NSB', 'ProbeH2.h2', 'ProbeH2.weight']

		query = "SELECT %s FROM (Probe, ProbeSet, ProbeFreeze) left join ProbeH2  on ProbeH2.ProbeId = Probe.Id and ProbeH2.ProbeFreezeId = ProbeFreeze.Id WHERE ProbeSet.Name = '%s' and Probe.ProbeSetId = ProbeSet.Id and ProbeFreeze.Name = '%s' order by Probe.SerialOrder" % (string.join(fetchField,','), self.ProbeSetID, self.probeDatabase)
		self.cursor.execute(query)
		results = self.cursor.fetchall()
		
		blatsequence = ""
		
		# add by NL: get strains' name in SnpPattern database table
		strainsInSnpPatternDBtable=self.getStrainNameIndexPair()  # after snpBrowserPage.py change to MVC, this function can be removed in this class and called from other class; 
		allStrainNameList=[v[0] for v in strainsInSnpPatternDBtable]
			
		speciesid = webqtlDatabaseFunction.retrieveSpeciesId(cursor=self.cursor,RISet=fd.RISet)
		for result in results:
			"""
			ProbeId, CellID,Sequence,ExonNo,Tm, E_GSB,E_NSB = map(self.nullRecord,result)
			h2 = ''
			query = "SELECT h2 FROM ProbeH2 WHERE ProbeFreezeId = '%s' and ProbeId=%s" % (self.probeDatabase, ProbeId)
			self.cursor.execute(query)
			results = self.cursor.fetchall()
			"""

			CellID,Sequence,ExonNo,Tm, E_GSB,E_NSB,h2, weight = map(self.nullRecord,result)
			
	
			Average = ''
			STDEV = ''
			mean = -10000.0
			stdev = -10000.0
			try:
				thisTrait.cellid = CellID
				thisTrait.retrieveData()
		
				mean, median, var, stdev, sem, N = reaper.anova(thisTrait.exportInformative()[1])
			
				if mean:
					Average = '%2.2f' % mean
				if stdev:
					STDEV = '%2.2f' % stdev
			except:
				pass

			if CellID == self.CellID:
				bkColor = "cbrdull fs11 b1"
			else:
				bkColor = "fs11 b1"
			seqcolor= ''
			
			if thisTrait.blatseq:
				blatsequence = thisTrait.blatseq
				if int(CellID[-1]) % 2 == 1:
					seqcolor= 'cdg'
			else:
				if int(CellID[-1]) % 2 == 1:
					seqcolor= 'cdg'
					blatsequence += string.strip(Sequence)
						
			if thisTrait.genbankid  and (int(CellID[-1]) % 2 == 1):
				probeurl = 'http://www.ncbi.nlm.nih.gov/blast/bl2seq/wblast2.cgi?one=%s&sseq=%s'  % (thisTrait.genbankid, Sequence)
				probefy1 = HT.Input(type="button",value="Blast",onClick= "openNewWin('%s')" % probeurl, Class="buttonsmaller")
			else:  
				probefy1 = ''
			
			traitName = str(thisTrait)

			#XZ, Aug 08, 2011: Note that probesets on some affy chips are not name as "xxx_at" (i.e., Affy Mouse Gene 1.0 ST (GPL6246)). 
			#EnsemblProbeSetID = self.ProbeSetID[0:self.ProbeSetID.index('_at')+3]
			EnsemblProbeSetID = self.ProbeSetID
			if '_at' in self.ProbeSetID:
				EnsemblProbeSetID = self.ProbeSetID[0:self.ProbeSetID.index('_at')+3]

			self.cursor.execute('''
					SELECT EnsemblProbeLocation.* 
					FROM EnsemblProbeLocation, EnsemblProbe, EnsemblChip, GeneChipEnsemblXRef, ProbeFreeze
					WHERE EnsemblProbeLocation.ProbeId=EnsemblProbe.Id and EnsemblProbe.ChipId=GeneChipEnsemblXRef.EnsemblChipId and
						GeneChipEnsemblXRef.GeneChipId=ProbeFreeze.ChipId and EnsemblProbe.Name=%s and EnsemblProbe.ProbeSet=%s and 
						ProbeFreeze.Name=%s group by Chr, Start, End'''
					,(CellID, EnsemblProbeSetID, self.probeDatabase))
			LocationFields = self.cursor.fetchall()

			Chr=''
			Start=''
			End=''
			if (len(LocationFields)>=1):
				Chr,Start,End,Strand,MisMatch,ProbeId = map(self.nullRecord,LocationFields[0])
				Start /= 1000000.0
				End /= 1000000.0
			if (len(LocationFields)>1):
				self.cursor.execute('''
						SELECT ProbeSet.Chr, ProbeSet.Mb FROM ProbeSet, ProbeFreeze 
						WHERE ProbeSet.ChipId=ProbeFreeze.ChipId and ProbeSet.Name=%s and ProbeFreeze.Name=%s'''
						,(self.ProbeSetID, self.probeDatabase))
				ProbeSetChr, ProbeSetMb = map(self.nullRecord,self.cursor.fetchall()[0])
					
				self.cursor.execute('''
						SELECT EnsemblProbeLocation.*, ABS(EnsemblProbeLocation.Start/1000000-%s) as Mb 
						FROM EnsemblProbeLocation, EnsemblProbe, EnsemblChip, GeneChipEnsemblXRef, ProbeFreeze
						WHERE EnsemblProbeLocation.ProbeId=EnsemblProbe.Id and EnsemblProbe.ChipId=GeneChipEnsemblXRef.EnsemblChipId and
							GeneChipEnsemblXRef.GeneChipId=ProbeFreeze.ChipId and EnsemblProbe.Name=%s and EnsemblProbe.ProbeSet=%s and
							EnsemblProbeLocation.Chr=%s and ProbeFreeze.Name=%s order by Mb limit 1'''
						,(ProbeSetMb, CellID, EnsemblProbeSetID, ProbeSetChr, self.probeDatabase))
				NewLocationFields = self.cursor.fetchall()
				if (len(NewLocationFields)>0):
					Chr,Start,End,Strand,MisMatch,ProbeId,Mb = map(self.nullRecord,NewLocationFields[0])
					Start /= 1000000.0
					End /= 1000000.0
					
			snp_collection = []	
			snpDiff_collection=[]
			
			startIndex=3
			if Chr != '' and Start != '' and End != '' and speciesid != None:
									
				self.cursor.execute('''
						   SELECT a.SnpName, a.Id, b.* FROM SnpAll a, SnpPattern b
						   WHERE a.Chromosome=%s and a.Position>=%s and a.Position<=%s 
						   and a.SpeciesId=%s and a.Id=b.SnpId'''
							,(Chr, Start, End, speciesid)) #chr,Start, End, 1))							
				snpresults = self.cursor.fetchall()
				
				index1=allStrainNameList.index(_mat) #_mat index in results
				index2=allStrainNameList.index(_pat) #_pat index in results
			
				for v in snpresults:
					#updated by NL: 07-22-2011  check 'limit to' to get snpBrowser snpresults
					snp_collection.append(HT.Href(text=v[0], url=os.path.join(webqtlConfig.CGIDIR, 
							"main.py?FormID=SnpBrowserResultPage&submitStatus=1&customStrain=1")+ "&geneName=%s" % v[0], Class="fs12 fwn", target="_blank"))
					snp_collection.append(HT.BR())
					#updated by NL: 07-27-2011  link snp info for different allele only	
					strain1_allele=v[startIndex+index1]
					strain2_allele=v[startIndex+index2]
					
					if strain1_allele!=strain2_allele:
						snpDiff_collection.append(HT.Href(text=v[0], url=os.path.join(webqtlConfig.CGIDIR, 
								"main.py?FormID=SnpBrowserResultPage&submitStatus=1&customStrain=1&diffAlleles=1&chosenStrains=%s"%chosenStrains)+ "&geneName=%s" % v[0], Class="fs12 fwn", target="_blank"))
						snpDiff_collection.append(HT.BR())
							

			tr = []	
			tr.append(TDCell(HT.TD(HT.Input(type="checkbox", Class='checkbox', name="searchResult",value=traitName, onClick="highlight(this)"), align="right", Class=bkColor, nowrap="on"), text=traitName))
			
			tr.append(TDCell(HT.TD(HT.Href(text=CellID, url = "javascript:showDatabase2('%s','%s','%s');" % (self.database,self.ProbeSetID,CellID),Class="fs12 fwn"),Class=bkColor), traitName, traitName.upper()))
			
			tr.append(TDCell(HT.TD(Sequence, Class=bkColor + " %s ffmono fs14" % seqcolor),Sequence,Sequence.upper()))
			tr.append(TDCell(HT.TD(probefy1,align='center',Class=bkColor))) 
			tr.append(TDCell(HT.TD(ExonNo,align='center',Class=bkColor)))
			
			try:
				TmValue = float(Tm)
			except:
				TmValue = 0.0
			tr.append(TDCell(HT.TD(Tm,align='center',Class=bkColor), Tm, TmValue))
			
			try:
				E_GSBValue = float(E_GSB)
			except:
				E_GSBValue = -10000.0
			tr.append(TDCell(HT.TD(E_GSB,align='center',Class=bkColor), E_GSB, E_GSBValue))

			try:
				E_NSBValue = float(E_NSB)
			except:
				E_NSBValue = -10000.0
			tr.append(TDCell(HT.TD(E_NSB,align='center',Class=bkColor), E_NSB, E_NSBValue))
			
			tr.append(TDCell(HT.TD(Average,align='center',Class=bkColor), Average, mean))
			tr.append(TDCell(HT.TD(STDEV,align='center',Class=bkColor), STDEV, stdev))

			try:
				h2Value = float(h2)
			except:
				h2Value = -10000.0
			tr.append(TDCell(HT.TD(h2,align='center',Class=bkColor), h2, h2Value))

			tr.append(TDCell(HT.TD(Chr,align='left',Class=bkColor)))
			tr.append(TDCell(HT.TD(Start,align='left',Class=bkColor)))
			tr.append(TDCell(HT.TD(End,align='left',Class=bkColor)))

			snp_td = HT.TD(align='left',Class=bkColor)
			for one_snp_href in snp_collection:
			    snp_td.append(one_snp_href)
	
			tr.append(TDCell(snp_td)) 
			
			#07-27-2011:add by NL: show SNP results for different allele only
			snpDiff_td= HT.TD(align='left', valign='top', Class=bkColor)
			for one_snpDiff_href in snpDiff_collection:
			    snpDiff_td.append(one_snpDiff_href)
			tr.append(TDCell(snpDiff_td))
			
			tblobj['body'].append(tr)
		
		# import cPickle
		filename = webqtlUtil.genRandStr("Probe_")
		objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb')
		cPickle.dump(tblobj, objfile)
		objfile.close()
		# NL, 07/27/2010. genTableObj function has been moved from templatePage.py to webqtlUtil.py;		
		div = HT.Div(webqtlUtil.genTableObj(tblobj=tblobj, file=filename, sortby=("", ""), tableID = "sortable", addIndex = "1"), Id="sortable")

		#UCSC
		_Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=fd.RISet)
		if _Species == "rat":
			thisurl = webqtlConfig.UCSC_BLAT % ('rat', 'rn3', blatsequence)
		elif _Species == "mouse":
			thisurl = webqtlConfig.UCSC_BLAT % ('mouse', 'mm9', blatsequence)
		else:
			thisurl = ""
		if thisurl:	
			blatbutton = HT.Input(type='button' ,name='blatPM',value='Verify UCSC', onClick="window.open('%s','_blank')" % thisurl,Class="button")
		else:
			blatbutton = ""
		
		#GenBank
		genbankSeq = ""
		if thisTrait.genbankid:
			self.cursor.execute("SELECT Sequence FROM Genbank WHERE Id = '%s'" % thisTrait.genbankid )
			genbankSeq = self.cursor.fetchone()
			if genbankSeq:
				genbankSeq = genbankSeq[0]
		
		if genbankSeq: 
			if _Species == "rat":
				thisurl2 = webqtlConfig.UCSC_BLAT % ('rat', 'rn3', genbankSeq)
			if _Species == "mouse":
				thisurl2 = webqtlConfig.UCSC_BLAT % ('mouse', 'mm9', genbankSeq)
		else:
			thisurl2 = ''
		if thisurl2:	
			blatbutton2 = HT.Input(type='button' ,name='blatPM',value='Verify GenBank', onClick="window.open('%s','_blank')" % thisurl2,Class="button")
		else:
			blatbutton2 = ""
		
		#Snp
		snpBrowser = ""
		if thisTrait.symbol and _Species == 'mouse':
			self.cursor.execute("select geneSymbol from GeneList where geneSymbol = %s", thisTrait.symbol)
			geneName = self.cursor.fetchone()
			if geneName:
				snpurl = os.path.join(webqtlConfig.CGIDIR, "main.py?FormID=snpBrowser") + "&geneName=%s" % geneName[0]	
			else:
				if thisTrait.chr and thisTrait.mb:
					snpurl = os.path.join(webqtlConfig.CGIDIR, "main.py?FormID=snpBrowser") + \
							"&chr=%s&start=%2.6f&end=%2.6f" % (thisTrait.chr, thisTrait.mb-0.002, thisTrait.mb+0.002)
				else:
					snpurl = ""

			if snpurl:
				snpBrowser = HT.Input(type="button",value="SNP Browser",onClick= \
						"openNewWin('%s')" % snpurl, Class="button")

			else:
				snpBrowser = ""
		#end if
		
		heading = HT.Paragraph('Probe Information', Class="title")
		intro = HT.Paragraph('The table below lists information of all probes of probe set ',HT.Span(self.ProbeSetID, Class="fwb fs13"),' from database ', HT.Span(self.probeDatabase, Class="fwb fs13"), ".")
		buttons = HT.Paragraph(probedata,probeinfo,heatmap,cormatrix,blatbutton,blatbutton2,verifyButton,snpBrowser, HT.P(),selectall,selectpm,selectmm,selectinvert,reset,addselect)
		if rudi_track:
		   buttons.append(rudi_track)	
		form.append(buttons,div,HT.P())
	
		TD_LR.append(heading,intro,form, HT.P())
		self.dict['basehref'] = ''
		self.dict['body'] = str(TD_LR)
		self.dict['title'] = self.db.shortname + ' : ' + self.ProbeSetID +' / Probe Information'
        # updated by NL, javascript function xmlhttpPost(strURL, div, querystring) and function updatepage(Id, str)
		# have been moved to dhtml.js
	   	self.dict['js1'] = ''
	def __init__(self, fd):

		templatePage.__init__(self, fd)

		self.initializeDisplayParameters(fd)

		if not fd.genotype:
			fd.readGenotype()

		if fd.allstrainlist:
			mdpchoice = fd.formdata.getvalue('MDPChoice')
			if mdpchoice == "1":
				strainlist = fd.f1list + fd.strainlist
			elif mdpchoice == "2":
				strainlist = []
				strainlist2 = fd.f1list + fd.strainlist
				for strain in fd.allstrainlist:
					if strain not in strainlist2:
						strainlist.append(strain)
				#So called MDP Panel
				if strainlist:
					strainlist = fd.f1list+fd.parlist+strainlist
			else:
				strainlist = fd.allstrainlist
			fd.readData(fd.allstrainlist)
		else:
			mdpchoice = None
			strainlist = fd.strainlist
			fd.readData()

		#if fd.allstrainlist:
		#	fd.readData(fd.allstrainlist)
		#	strainlist = fd.allstrainlist
		#else:
		#	fd.readData()
		#	strainlist = fd.strainlist
		
		
		if not self.openMysql():
			return

		isSampleCorr = 0 #XZ: initial value is false
		isTissueCorr = 0 #XZ: initial value is false

		#Javascript functions (showCorrelationPlot2, showTissueCorrPlot) have made sure the correlation type is either sample correlation or tissue correlation.
		if (self.database and (self.ProbeSetID != 'none')):
			isSampleCorr = 1
		elif (self.X_geneSymbol and self.Y_geneSymbol):
			isTissueCorr = 1
		else:
			heading = "Correlation Type Error"
			detail = ["For the input parameters, GN can not recognize the correlation type is sample correlation or tissue correlation."]
			self.error(heading=heading,detail=detail)
			return


        	TD_LR = HT.TD(colspan=2,height=200,width="100%",bgColor='#eeeeee', align="left", wrap="off")


		dataX=[]
		dataY=[]
		dataZ=[] # shortname
		fullTissueName=[]

		if isTissueCorr:
			dataX, dataY, xlabel, ylabel, dataZ, fullTissueName = self.getTissueLabelsValues(X_geneSymbol=self.X_geneSymbol, Y_geneSymbol=self.Y_geneSymbol, TissueProbeSetFreezeId=self.TissueProbeSetFreezeId)
			plotHeading = HT.Paragraph('Tissue Correlation Scatterplot')
			plotHeading.__setattr__("class","title")

			if self.xAxisLabel == '':
				self.xAxisLabel = xlabel

			if self.yAxisLabel == '':
				self.yAxisLabel = ylabel
			
		if isSampleCorr:
			plotHeading = HT.Paragraph('Sample Correlation Scatterplot')
                        plotHeading.__setattr__("class","title")

			#XZ: retrieve trait 1 info, Y axis
			trait1_data = [] #trait 1 data
			trait1Url = ''

			try:
				Trait1 = webqtlTrait(db=self.database, name=self.ProbeSetID, cellid=self.CellID, cursor=self.cursor)
				Trait1.retrieveInfo()
				Trait1.retrieveData()
			except:
				heading = "Retrieve Data"
				detail = ["The database you just requested has not been established yet."]
				self.error(heading=heading,detail=detail)
				return
	
			trait1_data = Trait1.exportData(strainlist)
			if Trait1.db.type == 'Publish' and Trait1.confidential:
				trait1Url = Trait1.genHTML(dispFromDatabase=1, privilege=self.privilege, userName=self.userName, authorized_users=Trait1.authorized_users)
			else:
				trait1Url = Trait1.genHTML(dispFromDatabase=1)
			
			if self.yAxisLabel == '':
				self.yAxisLabel= '%s : %s' % (Trait1.db.shortname, Trait1.name)
				if Trait1.cellid:
					self.yAxisLabel += ' : ' + Trait1.cellid


			#XZ, retrieve trait 2 info, X axis
			traitdata2 = [] #trait 2 data
			_vals = [] #trait 2 data
			trait2Url = ''

			if ( self.database2 and (self.ProbeSetID2 != 'none') ):
				try:
					Trait2 = webqtlTrait(db=self.database2, name=self.ProbeSetID2, cellid=self.CellID2, cursor=self.cursor)
					Trait2.retrieveInfo()
					Trait2.retrieveData()
				except:
					heading = "Retrieve Data"
					detail = ["The database you just requested has not been established yet."]
					self.error(heading=heading,detail=detail)
					return

				if Trait2.db.type == 'Publish' and Trait2.confidential:
					trait2Url = Trait2.genHTML(dispFromDatabase=1, privilege=self.privilege, userName=self.userName, authorized_users=Trait2.authorized_users)
				else:
					trait2Url = Trait2.genHTML(dispFromDatabase=1)
				traitdata2 = Trait2.exportData(strainlist)
				_vals = traitdata2[:]
				
				if self.xAxisLabel == '':
					self.xAxisLabel = '%s : %s' % (Trait2.db.shortname, Trait2.name)
					if Trait2.cellid:
						self.xAxisLabel += ' : ' + Trait2.cellid
			else:
				for item in strainlist:
					if fd.allTraitData.has_key(item):
						_vals.append(fd.allTraitData[item].val)
					else:
						_vals.append(None)

				if self.xAxisLabel == '':
					if fd.identification:
						self.xAxisLabel = fd.identification
					else:
						self.xAxisLabel = "User Input Data"

				try:
					Trait2 = webqtlTrait(fullname=fd.formdata.getvalue('fullname'), cursor=self.cursor)
					trait2Url = Trait2.genHTML(dispFromDatabase=1)
				except:
					trait2Url = self.xAxisLabel

			if (_vals and trait1_data):
				if len(_vals) != len(trait1_data):
					errors = HT.Blockquote(HT.Font('Error: ',color='red'),HT.Font('The number of traits are inconsistent, Program quit',color='black'))
					errors.__setattr__("class","subtitle")
					TD_LR.append(errors)
					self.dict['body'] = str(TD_LR)
					return

				for i in range(len(_vals)):
					if _vals[i]!= None and trait1_data[i]!= None:
						dataX.append(_vals[i])
						dataY.append(trait1_data[i])
						strainName = strainlist[i]
						if self.showstrains:
							dataZ.append(webqtlUtil.genShortStrainName(RISet=fd.RISet, input_strainName=strainName))
			else:
				heading = "Correlation Plot"
				detail = ['Empty Dataset for sample correlation, please check your data.']
				self.error(heading=heading,detail=detail)
				return


		#XZ: We have gotten all data for both traits.
		if len(dataX) >= self.corrMinInformative:

			if self.rankOrder == 0:
				rankPrimary = 0
				rankSecondary = 1
			else:
				rankPrimary = 1
				rankSecondary = 0

			lineColor = self.setLineColor();
			symbolColor = self.setSymbolColor();
			idColor = self.setIdColor();					
				
			c = pid.PILCanvas(size=(self.plotSize, self.plotSize*0.90))
			data_coordinate = Plot.plotXY(canvas=c, dataX=dataX, dataY=dataY, rank=rankPrimary, dataLabel = dataZ, labelColor=pid.black, lineSize=self.lineSize, lineColor=lineColor, idColor=idColor, idFont=self.idFont, idSize=self.idSize, symbolColor=symbolColor, symbolType=self.symbol, filled=self.filled, symbolSize=self.symbolSize, XLabel=self.xAxisLabel, connectdot=0, YLabel=self.yAxisLabel, title='', fitcurve=self.showline, displayR =1, offset= (90, self.plotSize/20, self.plotSize/10, 90), showLabel = self.showIdentifiers)
				
			if rankPrimary == 1:
				dataXlabel, dataYlabel = webqtlUtil.calRank(xVals=dataX, yVals=dataY, N=len(dataX))
			else:
				dataXlabel, dataYlabel = dataX, dataY
					
			gifmap1 = HT.Map(name='CorrelationPlotImageMap1')
				
			for i, item in enumerate(data_coordinate):
				one_rect_coordinate = "%d, %d, %d, %d" % (item[0] - 5, item[1] - 5, item[0] + 5, item[1] + 5)
				if isTissueCorr:
					one_rect_title = "%s (%s, %s)" % (fullTissueName[i], dataXlabel[i], dataYlabel[i])
				else:
					one_rect_title = "%s (%s, %s)" % (dataZ[i], dataXlabel[i], dataYlabel[i])
				gifmap1.areas.append(HT.Area(shape='rect',coords=one_rect_coordinate, title=one_rect_title) )

			filename= webqtlUtil.genRandStr("XY_")
			c.save(webqtlConfig.IMGDIR+filename, format='gif')
			img1=HT.Image('/image/'+filename+'.gif',border=0, usemap='#CorrelationPlotImageMap1')

			mainForm_1 = HT.Form( cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase', submit=HT.Input(type='hidden'))
			hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':'_','CellID':'_','RISet':fd.RISet, 'ProbeSetID2':'_', 'database2':'_', 'CellID2':'_', 'allstrainlist':string.join(fd.strainlist, " "), 'traitList': fd.formdata.getvalue("traitList")}
			if fd.incparentsf1:
				hddn['incparentsf1'] = 'ON'		
			for key in hddn.keys():
				mainForm_1.append(HT.Input(name=key, value=hddn[key], type='hidden'))
			
			if isSampleCorr:
					mainForm_1.append(HT.P(), HT.Blockquote(HT.Strong('X axis:'),HT.Blockquote(trait2Url),HT.Strong('Y axis:'),HT.Blockquote(trait1Url), style='width: %spx;' % self.plotSize, wrap="hard"))
			
			graphForm = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), name='MDP_Form',submit=HT.Input(type='hidden'))
			graph_hddn = self.setHiddenParameters(fd, rankPrimary)
			webqtlUtil.exportData(graph_hddn, fd.allTraitData) #XZ: This is necessary to replot with different groups of strains
							
			for key in graph_hddn.keys():
				graphForm.append(HT.Input(name=key, value=graph_hddn[key], type='hidden'))			

			options = self.createOptionsMenu(fd, mdpchoice)	
			
			if (self.showOptions == '0'):
					showOptionsButton = HT.Input(type='button' ,name='optionsButton',value='Hide Options', onClick="showHideOptions();", Class="button")
			else:
					showOptionsButton = HT.Input(type='button' ,name='optionsButton',value='Show Options', onClick="showHideOptions();", Class="button")
					
			# updated by NL: 12-07-2011 add variables for tissue abbreviation page
			if isTissueCorr: 
				graphForm.append(HT.Input(name='shortTissueName', value='', type='hidden'))
				graphForm.append(HT.Input(name='fullTissueName', value='', type='hidden'))
				shortTissueNameStr=string.join(dataZ, ",")
				fullTissueNameStr=string.join(fullTissueName, ",")
		
				tissueAbbrButton=HT.Input(type='button' ,name='tissueAbbrButton',value='Show Abbreviations', onClick="showTissueAbbr('MDP_Form','%s','%s')" % (shortTissueNameStr,fullTissueNameStr), Class="button")
				graphForm.append(showOptionsButton,'&nbsp;&nbsp;&nbsp;&nbsp;',tissueAbbrButton, HT.BR(), HT.BR())
			else:
				graphForm.append(showOptionsButton, HT.BR(), HT.BR())

			graphForm.append(options, HT.BR())				
			graphForm.append(HT.HR(), HT.BR(), HT.P())
						
			TD_LR.append(plotHeading, HT.BR(),graphForm, HT.BR(), gifmap1, HT.P(), img1, HT.P(), mainForm_1)
			TD_LR.append(HT.BR(), HT.HR(color="grey", size=5, width="100%"))



			c = pid.PILCanvas(size=(self.plotSize, self.plotSize*0.90))
			data_coordinate = Plot.plotXY(canvas=c, dataX=dataX, dataY=dataY, rank=rankSecondary, dataLabel = dataZ, labelColor=pid.black,lineColor=lineColor, lineSize=self.lineSize, idColor=idColor, idFont=self.idFont, idSize=self.idSize, symbolColor=symbolColor, symbolType=self.symbol, filled=self.filled, symbolSize=self.symbolSize, XLabel=self.xAxisLabel, connectdot=0, YLabel=self.yAxisLabel,title='', fitcurve=self.showline, displayR =1, offset= (90, self.plotSize/20, self.plotSize/10, 90), showLabel = self.showIdentifiers)

			if rankSecondary == 1:
				dataXlabel, dataYlabel = webqtlUtil.calRank(xVals=dataX, yVals=dataY, N=len(dataX))
			else:
				dataXlabel, dataYlabel = dataX, dataY
				
			gifmap2 = HT.Map(name='CorrelationPlotImageMap2')
				
			for i, item in enumerate(data_coordinate):
				one_rect_coordinate = "%d, %d, %d, %d" % (item[0] - 6, item[1] - 6, item[0] + 6, item[1] + 6)
				if isTissueCorr:
					one_rect_title = "%s (%s, %s)" % (fullTissueName[i], dataXlabel[i], dataYlabel[i])
				else:
					one_rect_title = "%s (%s, %s)" % (dataZ[i], dataXlabel[i], dataYlabel[i])
					
				gifmap2.areas.append(HT.Area(shape='rect',coords=one_rect_coordinate, title=one_rect_title) )

			filename= webqtlUtil.genRandStr("XY_")
			c.save(webqtlConfig.IMGDIR+filename, format='gif')
			img2=HT.Image('/image/'+filename+'.gif',border=0, usemap='#CorrelationPlotImageMap2')

			mainForm_2 = HT.Form( cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase2', submit=HT.Input(type='hidden'))
			hddn = {'FormID':'showDatabase2','ProbeSetID':'_','database':'_','CellID':'_','RISet':fd.RISet, 'ProbeSetID2':'_', 'database2':'_', 'CellID2':'_', 'allstrainlist':string.join(fd.strainlist, " "), 'traitList': fd.formdata.getvalue("traitList")}
			if fd.incparentsf1:
				hddn['incparentsf1'] = 'ON'
			for key in hddn.keys():
				mainForm_2.append(HT.Input(name=key, value=hddn[key], type='hidden'))
				
			if isSampleCorr:
				mainForm_2.append(HT.P(), HT.Blockquote(HT.Strong('X axis:'),HT.Blockquote(trait2Url),HT.Strong('Y axis:'),HT.Blockquote(trait1Url), style='width:%spx;' % self.plotSize))

	
			TD_LR.append(HT.BR(), HT.P())
			TD_LR.append('\n', gifmap2, HT.P(), HT.P(), img2, HT.P(), mainForm_2)

			self.dict['body'] = str(TD_LR)
		else:
			heading = "Correlation Plot"
			detail = ['Fewer than %d strain data were entered for %s data set. No statitical analysis has been attempted.' % (self.corrMinInformative, fd.RISet)]
			self.error(heading=heading,detail=detail)
			return
    def __init__(self,fd,InputData=None):

        templatePage.__init__(self, fd)
        self.dict['title'] = 'Correlation Matrix'

        if not self.openMysql():
            return
            
        if not fd.genotype:
            fd.readGenotype()
            fd.strainlist = fd.f1list + fd.strainlist
            
        #self.searchResult = fd.formdata.getvalue('searchResult')
        self.oldSearchResult = fd.formdata.getvalue('oldSearchResult')
        
        if self.oldSearchResult:
            try:
                self.searchResult = fd.formdata.getvalue('oldSearchResult')
            except:
            	self.searchResult = fd.formdata.getvalue('searchResult')
        
        else:
        	self.searchResult = fd.formdata.getvalue('searchResult')
        
        if not self.searchResult:
            heading = 'Correlation Matrix'
            detail = ['You need to select at least two traits in order to generate correlation matrix.']
            self.error(heading=heading,detail=detail)
            return
        if type("1") == type(self.searchResult):
            self.searchResult = [self.searchResult]
        
        if self.searchResult:
            #testvals,names,dbInfos = self.getAllSearchResult(fd,self.searchResult)
            if len(self.searchResult) > webqtlConfig.MAXCORR:
                heading = 'Correlation Matrix'
                detail = ['In order to display Correlation Matrix properly, Do not select more than %d traits for Correlation Matrix.' % webqtlConfig.MAXCORR]
                self.error(heading=heading,detail=detail)
                return

            #XZ, 7/22/2009: this block is not necessary
            #elif len(self.searchResult) > 40:
            #    noPCA = 1
            #else:
            #    noPCA = 0
    
            traitList = []
            traitDataList = []
            for item in self.searchResult:
                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                thisTrait.retrieveInfo()
                thisTrait.retrieveData(fd.strainlist)
                traitList.append(thisTrait)
                traitDataList.append(thisTrait.exportData(fd.strainlist))
                
        else:
            heading = 'Correlation Matrix'
            detail = [HT.Font('Error : ',color='red'),HT.Font('Error occurs while retrieving data FROM database.',color='black')]
            self.error(heading=heading,detail=detail)
            return

        NNN = len(traitList)
        
        if NNN == 0:
            heading = "Correlation Matrix"
            detail = ['No trait was selected for %s data set. No matrix generated.' % self.data.RISet]
            self.error(heading=heading,detail=detail)
            return
        elif NNN < 2:
            heading = 'Correlation Matrix'
            detail = ['You need to select at least two traits in order to generate correlation matrix.']
            self.error(heading=heading,detail=detail)
            return
        else:
        	
        	
        	
            corArray = [([0] * (NNN+1))[:] for i in range(NNN+1)]
            pearsonArray = [([0] * (NNN))[:] for i in range(NNN)]
            spearmanArray = [([0] * (NNN))[:] for i in range(NNN)]
            corArray[0][0] = 'Correlation'
            TD_LR = HT.TD(colspan=2,width="100%",bgColor='#eeeeee')
            form = HT.Form( cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase', submit=HT.Input(type='hidden'))
            hddn = {'FormID':'showDatabase', 'ProbeSetID':'_','database':'_',
            'CellID':'_','ProbeSetID2':'_','database2':'_','CellID2':'_',
            'newNames':fd.formdata.getvalue("newNames", "_"), 
            'RISet':fd.RISet,'ShowStrains':'ON','ShowLine':'ON', 'rankOrder':'_', 
            "allstrainlist":string.join(fd.strainlist, " "), 'traitList':string.join(self.searchResult, "\t")}
            if fd.incparentsf1:
                hddn['incparentsf1']='ON'
            	    
            for key in hddn.keys():
                form.append(HT.Input(name=key, value=hddn[key], type='hidden'))
           
            for item in self.searchResult:
            	form.append(HT.Input(name='oldSearchResult', value=str(item), type='hidden'))
            
            traiturls = []
            traiturls2 = []
            shortNames = []
            verboseNames = []
            verboseNames2 = []
            verboseNames3 = []
            abbreviation = ''
            
            #dbInfo.ProbeSetID = ProbeSetID
            #dbInfo.CellID = CellID
            for i, thisTrait in enumerate(traitList):
                _url = "javascript:showDatabase2('%s','%s','%s');" % (thisTrait.db.name, thisTrait.name, thisTrait.cellid)
                #_text = 'Trait%d: ' % (i+1)+str(thisTrait)
                _text = 'Trait %d: ' % (i+1)+thisTrait.displayName()
                                
                if thisTrait.db.type == 'Geno':
                    _shortName = 'Genotype'
                    abbreviation = 'Genotype'
                    _verboseName = 'Locus %s' % (thisTrait.name)
                    _verboseName2 = 'Chr %s @ %s Mb' % (thisTrait.chr, '%2.3f' % thisTrait.mb)
                    _verboseName3 = ''
                elif thisTrait.db.type == 'Publish':
                    if thisTrait.post_publication_abbreviation:
                        AbbreviationString = thisTrait.post_publication_abbreviation
                    else:
                        AbbreviationString = ''
                    if thisTrait.confidential:
                        if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users):
                            if thisTrait.pre_publication_abbreviation:
                                AbbreviationString = thisTrait.pre_publication_abbreviation
                            else:
                                AbbreviationString = ''
                    _shortName = 'Phenotype: %s' % (AbbreviationString)  
                    _verboseName2 = ''
                    _verboseName3 = ''
                    if thisTrait.pubmed_id:
                        _verboseName = 'PubMed %d: ' % thisTrait.pubmed_id
                    else: 
                        _verboseName = 'Unpublished '
                    _verboseName += 'RecordID/%s' % (thisTrait.name)
                    PhenotypeString = thisTrait.post_publication_description
                    if thisTrait.confidential:
                        if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users):
                            PhenotypeString = thisTrait.pre_publication_description
                    _verboseName2 = 'Phenotype: %s' % (PhenotypeString)
                    if thisTrait.authors:
                        a1 = string.split(thisTrait.authors,',')[0]
                        while a1[0] == '"' or a1[0] == "'" :
                            a1 = a1[1:]
                            _verboseName += ' by '
                            _verboseName += HT.Italic('%s, and colleagues' % (a1))
                elif thisTrait.db.type == 'Temp':
                    abbreviation = ''
                    _shortName = thisTrait.name
                    if thisTrait.description:
                        _verboseName = thisTrait.description
                    else:
                        _verboseName = 'Temp'
                    _verboseName2 = ''
                    _verboseName3 = ''
                else:
                    if thisTrait.symbol:
                        abbreviation = thisTrait.symbol
                    else:
                        abbreviation = ''
                    _shortName = 'Symbol: %s ' % thisTrait.symbol
                    if thisTrait.symbol:
                        _verboseName = thisTrait.symbol
                    else:
                        _verboseName = ''
                    _verboseName2 = ''
                    _verboseName3 = ''
                    if thisTrait.chr and thisTrait.mb:
                        _verboseName += ' on Chr %s @ %s Mb' % (thisTrait.chr,thisTrait.mb)
                    if thisTrait.description:
                        _verboseName2 = '%s' % (thisTrait.description)
                    if thisTrait.probe_target_description:
                        _verboseName3 = '%s' % (thisTrait.probe_target_description)         
                                
                cururl = HT.Href(text=_text, url=_url,Class='fs12') 
                cururl2 = HT.Href(text='Trait%d' % (i+1),url=_url,Class='fs12')
                traiturls.append(cururl)
                traiturls2.append(cururl2)
                shortName = HT.Div(id="shortName_" + str(i), style="display:none")
                shortName.append(_shortName)
                shortNames.append(shortName)
                verboseName = HT.Div(id="verboseName_" + str(i), style="display:none")
                verboseName.append(_verboseName)
                verboseNames.append(verboseName)
                verboseName2 = HT.Div(id="verboseName2_" + str(i), style="display:none")
                verboseName2.append(_verboseName2)
                verboseNames2.append(verboseName2)
                verboseName3 = HT.Div(id="verboseName3_" + str(i), style="display:none")
                verboseName3.append(_verboseName3)
                verboseNames3.append(verboseName3)
                    

                                
                corArray[i+1][0] = 'Trait%d: ' % (i+1)+str(thisTrait) + '/' + str(thisTrait) + ': ' + abbreviation + '/' + str(thisTrait) + ': ' + str(_verboseName) + ' : ' + str(_verboseName2) + ' : ' + str(_verboseName3)
                corArray[0][i+1] = 'Trait%d: ' % (i+1)+str(thisTrait)
                
            corMatrixHeading = HT.Paragraph('Correlation Matrix', Class="title")
            
            tbl = HT.TableLite(Class="collap", border=0, cellspacing=1, 
                cellpadding=5, width='100%')
            row1 = HT.TR(HT.TD(Class="fs14 fwb ffl b1 cw cbrb"),
                HT.TD('Spearman Rank Correlation (rho)', Class="fs14 fwb ffl b1 cw cbrb", colspan= NNN+1,align="center")
                )
            row2 = HT.TR(
                HT.TD("P e a r s o n &nbsp;&nbsp;&nbsp; r", rowspan= NNN+1,Class="fs14 fwb ffl b1 cw cbrb", width=10,align="center"),
                HT.TD(Class="b1", width=300))
            for i in range(NNN):
                row2.append(HT.TD(traiturls2[i], Class="b1", align="center"))
            tbl.append(row1,row2)

            nOverlapTrait =9999            
            nnCorr = len(fd.strainlist)
            for i, thisTrait in enumerate(traitList):
                newrow = HT.TR()
                newrow.append(HT.TD(traiturls[i], shortNames[i], verboseNames[i], verboseNames2[i], 
                                    verboseNames3[i], Class="b1"))
                names1 = [thisTrait.db.name, thisTrait.name, thisTrait.cellid]
                for j, thisTrait2 in enumerate(traitList):
                    names2 = [thisTrait2.db.name, thisTrait2.name, thisTrait2.cellid]
                    if j < i:
                        corr,nOverlap = webqtlUtil.calCorrelation(traitDataList[i],traitDataList[j],nnCorr)
                        
                        rank = fd.formdata.getvalue("rankOrder", "0")
                        
                        if nOverlap < nOverlapTrait:
                            nOverlapTrait = nOverlap
                        if corr > 0.7:
                            fontcolor="red"
                        elif corr > 0.5:
                            fontcolor="#FF6600"
                        elif corr < -0.7:
                            fontcolor="blue"
                        elif corr < -0.5:
                            fontcolor="#009900"
                        else:
                            fontcolor ="#000000"

                        pearsonArray[i][j] = corr
                        pearsonArray[j][i] = corr
                        if corr!= 0.0:
                            corArray[i+1][j+1] = '%2.3f/%d' % (corr,nOverlap)
                            thisurl = HT.Href(text=HT.Font('%2.3f'% corr,HT.BR(),'%d' % nOverlap ,color=fontcolor, Class="fs11 fwn"),url = "javascript:showCorrelationPlot2(db='%s',ProbeSetID='%s',CellID='%s',db2='%s',ProbeSetID2='%s',CellID2='%s',rank='%s')" % (names1[0], names1[1], names1[2], names2[0], names2[1], names2[2], rank))
                        else:
                            corArray[i+1][j+1] = '---/%d' % nOverlap
                            thisurl = HT.Font('---',HT.BR(), '%d' % nOverlap)
                        
                        newrow.append(HT.TD(thisurl,Class="b1",NOWRAP="ON",align="middle"))
                    elif j == i:
                        corr,nOverlap = webqtlUtil.calCorrelation(traitDataList[i],traitDataList[j],nnCorr)
                        pearsonArray[i][j] = 1.0
                        spearmanArray[i][j] = 1.0
                        corArray[i+1][j+1] = '%2.3f/%d' % (corr,nOverlap)
                        nOverlap = webqtlUtil.calCorrelation(traitDataList[i],traitDataList[j],nnCorr)[1]
                        newrow.append(HT.TD(HT.Href(text=HT.Font(HT.Italic("n"),HT.BR(),str(nOverlap),Class="fs11 fwn b1",align="center", color="000000"), url="javascript:showDatabase2('%s','%s','%s')" % (thisTrait.db.name, thisTrait.name, thisTrait.cellid)), bgColor='#cccccc', align="center", Class="b1", NOWRAP="ON"))
                    else:       
                        corr,nOverlap = webqtlUtil.calCorrelationRank(traitDataList[i],traitDataList[j],nnCorr)
                        
                        rank = fd.formdata.getvalue("rankOrder", "1")
                        
                        if corr > 0.7:
                            fontcolor="red"
                        elif corr > 0.5:
                            fontcolor="#FF6600"
                        elif corr < -0.7:
                            fontcolor="blue"
                        elif corr < -0.5:
                            fontcolor="#009900"
                        else:
                            fontcolor ="#000000"
                        spearmanArray[i][j] = corr
                        spearmanArray[j][i] = corr
                        if corr!= 0.0:
                            corArray[i+1][j+1] = '%2.3f/%d' % (corr,nOverlap)
                            thisurl = HT.Href(text=HT.Font('%2.3f'% corr,HT.BR(),'%d' % nOverlap ,color=fontcolor, Class="fs11 fwn"),url = "javascript:showCorrelationPlot2(db='%s',ProbeSetID='%s',CellID='%s',db2='%s',ProbeSetID2='%s',CellID2='%s',rank='%s')" % (names1[0], names1[1], names1[2], names2[0], names2[1], names2[2], rank))
                        else:
                            corArray[i+1][j+1] = '---/%d' % nOverlap
                            thisurl = HT.Span('---',HT.BR(), '%d' % nOverlap, Class="fs11 fwn")
                        newrow.append(HT.TD(thisurl,Class="b1", NOWRAP="ON",align="middle"))
                tbl.append(newrow)
                
            info = HT.Blockquote('Lower left cells list Pearson product-moment correlations; upper right cells list Spearman rank order correlations. Each cell also contains the n of cases. Values higher than 0.7 are displayed in ',HT.Font('red', color='red'),'; those between 0.5 and 0.7 in  ',HT.Font('orange', color='#FF6600'),'; Values lower than -0.7 are in ',HT.Font('blue', color='blue'),'; between -0.5 and -0.7 in ',HT.Font('green', color='#009900'),'. Select any cell to generate a scatter plot. Select trait labels for more information.', Class="fs13 fwn")
            
            exportbutton = HT.Input(type='button',  name='export', value='Export', onClick="exportText(allCorrelations);",Class="button")
            shortButton = HT.Input(type='button' ,name='dispShort',value=' Short Labels ', onClick="displayShortName();",Class="button")
            verboseButton = HT.Input(type='button' ,name='dispVerbose',value=' Long Labels ', onClick="displayVerboseName();", Class="button")
            form.append(HT.Blockquote(tbl,HT.P(),shortButton,verboseButton,exportbutton))
            TD_LR.append(corMatrixHeading,info,form,HT.P())

            #if noPCA:
            #    TD_LR.append(HT.Blockquote('No PCA is computed if more than 32 traits are selected.'))

            #print corArray
            exportScript = """
                <SCRIPT language=JavaScript>
                var allCorrelations = %s;
                </SCRIPT>

            """
            exportScript = exportScript % str(corArray)
            self.dict['js1'] = exportScript+'<SCRIPT SRC="/javascript/correlationMatrix.js"></SCRIPT><BR>'
            self.dict['body'] = str(TD_LR)
            
            #don't calculate PCA while number exceed 32
            #if noPCA:
            #    return

            #XZ, 7/22/2009: deal with PCA stuff
            #Only for Array Data
            
            if NNN > 2:
    
                traitname = map(lambda X:str(X.name), traitList)
                
                #generate eigenvalues
                
                # import sys
                sys.argv=[" "]
                # import numarray
                # import numarray.linear_algebra as la
                #spearmanEigen = eigenvectors(array(spearmanArray))
                pearsonEigen = la.eigenvectors(numarray.array(pearsonArray))
                #spearmanEigenValue,spearmanEigenVectors = self.sortEigenVectors(spearmanEigen)
                pearsonEigenValue,pearsonEigenVectors = self.sortEigenVectors(pearsonEigen)
		
				
		"""
		for i in range(len(pearsonEigenValue)):
			if type(pearsonEigenValue[i]).__name__ == 'complex':
				pearsonEigenValue[i] = pearsonEigenValue[i].real
		for i in range(len(pearsonEigenVectors)):
			for j in range(len(pearsonEigenVectors[i])):
				if type(pearsonEigenVectors[i][j]).__name__ == 'complex':
					pearsonEigenVectors[i][j] = pearsonEigenVectors[i][j].real
				if type(pearsonEigenVectors[i][j]).__name__ == 'complex':
					pearsonEigenVectors[i][j] = pearsonEigenVectors[i][j].real		      
		"""
        
		#if type(pearsonEigenValue[0]).__name__ == 'complex':
		if False:
		   pass
		else:
            	   traitHeading = HT.Paragraph('PCA Traits',align='left', Class="title")  
                   
        	   tbl2 = self.calcPCATraits(traitDataList=traitDataList, nnCorr=nnCorr, NNN=NNN, pearsonEigenValue=pearsonEigenValue, 
                                         pearsonEigenVectors=pearsonEigenVectors, form=form, fd=fd)
                   #Buttons on search page
                   #mintmap = HT.Input(type='button' ,name='mintmap',value='Multiple Mapping', onClick="databaseFunc(this.form,'showIntMap');",Class="button")
                   addselect = HT.Input(type='button' ,name='addselect',value='Add to Collection', onClick="addRmvSelection('%s', this.form, 'addToSelection');"  % fd.RISet,Class="button")
                   selectall = HT.Input(type='button' ,name='selectall',value='Select All', onClick="checkAll(this.form);",Class="button")
                   reset = HT.Input(type='reset',name='',value='Select None',Class="button")
                   updateNames = HT.Input(type='button', name='updateNames',value='Update Trait Names', onClick="editPCAName(this.form);", Class="button")
                   chrMenu = HT.Input(type='hidden',name='chromosomes',value='all')
                   
                   """
                   #need to be refined
                   if fd.genotype.Mbmap:
                       scaleMenu = HT.Select(name='scale')
                       scaleMenu.append(tuple(["Genetic Map",'morgan']))
                       scaleMenu.append(tuple(["Physical Map",'physic']))
                   else:
                       scaleMenu = ""
                   """    
                   
                   tbl2.append(HT.TR(HT.TD(HT.P(),chrMenu,updateNames,selectall,reset,addselect,colspan=3)))
        	   form.append(HT.P(),traitHeading,HT.Blockquote(tbl2))
                
                   plotHeading1 = HT.Paragraph('Scree Plot', Class="title") 
                   TD_LR.append(plotHeading1)
                   img1 = self.screePlot(NNN=NNN, pearsonEigenValue=pearsonEigenValue)
    
                   TD_LR.append(HT.Blockquote(img1))
                   
                   plotHeading2 = HT.Paragraph('Factor Loadings Plot', Class="title")
                   TD_LR.append(plotHeading2)
                   img2 = self.factorLoadingsPlot(pearsonEigenVectors=pearsonEigenVectors, traitList=traitList)
                   
                   TD_LR.append(HT.Blockquote(img2))                      

        self.dict['body'] = str(TD_LR)
	def insertResultPage(self, fd, newRecord):
		#generate html
		if 1:

			#XZ: Create new publication record if necessary
			PublicationId = None
			if newRecord['pubmed_id']:
				self.cursor.execute('SelecT Id from Publication where PubMed_ID = %d' % newRecord['pubmed_id'])
				results = self.cursor.fetchall()
				if not results:
					pass
				else:
					PublicationId = results[0][0]

			if not PublicationId:
				insertFields = ['Id']
				self.cursor.execute('SelecT max(Id) from Publication')
				maxId = self.cursor.fetchall()[0][0] + 1
				insertValues = [maxId]
				for field in self.db.disfield:
					if field in ('authors', 'title', 'abstract', 'journal','volume','pages','month','year') and newRecord[field]:
						insertFields.append(field)
						insertValues.append(newRecord[field])
				NFields = ['%s'] * len(insertFields)
				query = "insert into Publication (%s) Values (%s)" % (string.join(insertFields, ','), string.join(NFields, ','))

				self.cursor.execute(query, tuple(insertValues))
				PublicationId = maxId


			#XZ: Create new phenotype
			self.cursor.execute('SelecT max(Id) from Phenotype')
			maxId = self.cursor.fetchall()[0][0] + 1
			PhenotypeId = maxId
			if not newRecord['units']:
				newRecord['units'] = "Unknown"

			insertFields = ['Id']
			insertValues = [PhenotypeId]
			insertFields.append( 'Post_publication_description' )
			insertValues.append( newRecord['post_publication_description'] )
			insertFields.append( 'Units' )
			insertValues.append( newRecord['units'] )
			insertFields.append( 'Post_publication_abbreviation' )
                        insertValues.append( newRecord['post_publication_abbreviation'] )

			insertFields.append( 'Submitter' )
                        insertValues.append( self.userName )
			insertFields.append( 'Authorized_Users' )
			insertValues.append( self.userName )

			if newRecord['pre_publication_description']:
				insertFields.append( 'Pre_publication_description' )
				insertValues.append( newRecord['pre_publication_description'] )

			insertFields.append( 'Original_description' )
			original_desc_string = 'Original post publication description: ' + newRecord['post_publication_description']
			if newRecord['pre_publication_description']:
				original_desc_string = original_desc_string + '\n\nOriginal pre publication description: ' + newRecord['pre_publication_description']
			insertValues.append( original_desc_string )

			if newRecord['pre_publication_abbreviation']:
				insertFields.append( 'Pre_publication_abbreviation' )
				insertValues.append( newRecord['pre_publication_abbreviation'] )

			if newRecord['lab_code']:
				insertFields.append( 'Lab_code' )
				insertValues.append( newRecord['lab_code'] )

			if newRecord['owner']:
				insertFields.append( 'Owner' )
				insertValues.append( newRecord['owner'] )


			NFields = ['%s'] * len(insertFields)
			query = "insert into Phenotype (%s) Values (%s)" % (string.join(insertFields, ','), string.join(NFields, ','))
			self.cursor.execute(query, tuple(insertValues))

			


			#XZ: Insert data into PublishData, PublishSE and NStrain tables.
			self.cursor.execute('SelecT max(Id) from PublishData')
			DataId = self.cursor.fetchall()[0][0] + 1

			self.db.getRISet()
			InbredSetId = self.db.risetid

			self.cursor.execute('Select SpeciesId from InbredSet where Id=%s' % InbredSetId)
			SpeciesId = self.cursor.fetchone()[0]

			StrainIds = []
			for item in fd.strainlist:
				self.cursor.execute('Select Id from Strain where SpeciesId=%s and Name = "%s"' % (SpeciesId, item) )
				StrainId = self.cursor.fetchall()
				if not StrainId:
					raise ValueError
				else:
					StrainIds.append(StrainId[0][0])
			
			for i, strainName in enumerate(fd.strainlist):
				if fd.allTraitData.has_key(strainName):
					tdata = fd.allTraitData[strainName]
					traitVal, traitVar, traitNP = tdata.val, tdata.var, tdata.N
				else:
					continue
					
				if traitVal != None:
					#print 'insert into Data values(%d, %d, %s)' % (DataId, StrainIds[i], traitVal), "<BR>"
					#XZ, 03/05/2009: Xiaodong changed Data to PublishData
					self.cursor.execute('insert into PublishData values(%d, %d, %s)' % (DataId, StrainIds[i], traitVal))
				if traitVar != None:
					#print 'insert into SE values(%d, %d, %s)' % (DataId, StrainIds[i], traitVar), "<BR>"
					#XZ, 03/13/2009: Xiaodong changed SE to PublishSE
					self.cursor.execute('insert into PublishSE values(%d, %d, %s)' % (DataId, StrainIds[i], traitVar))
				if traitNP != None:
					#print 'insert into NStrain values(%d, %d, %s)' % (DataId, StrainIds[i], traitNP), "<BR>"
					self.cursor.execute('insert into NStrain values(%d, %d, %d)' % (DataId, StrainIds[i], traitNP))


			self.cursor.execute('SelecT max(Sequence) from PublishXRef where InbredSetId = %d and PhenotypeId = %d and PublicationId = %d' % (InbredSetId,PhenotypeId,PublicationId))
			Sequence = self.cursor.fetchall()
			if not Sequence or not Sequence[0][0]:
				Sequence = 1
			else:
				Sequence = Sequence[0][0] + 1
			
			self.cursor.execute('SelecT max(Id) from PublishXRef where InbredSetId = %d' % InbredSetId)
			try:
				InsertId = self.cursor.fetchall()[0][0] + 1
			except:
				InsertId = 10001
			
			ctime = time.ctime()
			comments = "Inserted by %s at %s\n" % (self.userName, ctime)
			#print 'insert into PublishXRef(Id, PublicationId, InbredSetId, PhenotypeId, DataId, Sequence, comments) values(%s, %s, %s, %s, %s, %s, %s)' % (InsertId , PublicationId, InbredSetId, PhenotypeId, DataId, Sequence, comments)
			self.cursor.execute('insert into PublishXRef(Id, PublicationId, InbredSetId, PhenotypeId, DataId, Sequence, comments) values(%s, %s, %s, %s, %s, %s, %s)', (InsertId , PublicationId, InbredSetId, PhenotypeId, DataId, Sequence, comments))
			
			TD_LR = HT.TD(valign="top",colspan=2,bgcolor="#ffffff", height=200)
			form = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase', submit=HT.Input(type='hidden'))
			hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':'_','CellID':'_','RISet':fd.RISet, 'incparentsf1':'on'}
			for key in hddn.keys():
				form.append(HT.Input(name=key, value=hddn[key], type='hidden'))
			
			mainTitle = HT.Paragraph("Add Trait to Published Database", Class="title")
			
			info = HT.Paragraph("Your Trait has been succesfully added to ", self.db.genHTML(), ".")
			
			thisTrait = webqtlTrait(db=self.db, cursor=self.cursor, name=InsertId)
			thisTrait.retrieveInfo()
			
			tbl = HT.TableLite(cellSpacing=2,cellPadding=0,width="90%",border=0)
			
			checkBox = HT.Input(type="checkbox",name="searchResult",value="%s" % thisTrait)
			tbl.append(HT.TR(HT.TD(width=30), HT.TD(thisTrait.genHTML(dispFromDatabase=1, privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users))))
			form.append(info, HT.P(), tbl)
			TD_LR.append(mainTitle, HT.Blockquote(form))
			
			self.dict['body'] = TD_LR
		else:
			heading = "Add to Published Database"
			detail = ["Error occured while adding the data."]
			self.error(heading=heading,detail=detail,error="Error")
			return
	def updateResultPage(self, fd, thisTrait):
	
		comments = []
		ctime = time.ctime()
		##Start Updating
		dataID = -1
		if thisTrait.db.type == 'Publish':
			self.cursor.execute("SelecT PublishXRef.InbredSetId, PublishXRef.DataId, PublishXRef.PublicationId, PublishXRef.PhenotypeId, PublishXRef.Sequence from PublishXRef, PublishFreeze where PublishXRef.InbredSetId= PublishFreeze.InbredSetId and  PublishFreeze.Name = '%s' and PublishXRef.Id = %s" % (thisTrait.db.name, thisTrait.name))
			PInbredSetId, dataID, PublicationId, PhenotypeId, Sequence = self.cursor.fetchall()[0]
			
		modifyField = self.formdata.getvalue('modifiedField')
		###Modify Trait Informations
		if modifyField:
			modifyField = string.split(modifyField, '::')
			comments += modifyField
			updateHomologeneid = False

			if thisTrait.db.type == 'Publish':
				PhenotypeItemUpdate = []
				PhenotypeItemValues = []
				PublicationItemUpdate = []
				PublicationItemValues = []

				for item in modifyField:
					itemvalue = self.formdata.getvalue(item)

					#XZ: identify Phenotype items
					if item in ['pre_publication_description', 'post_publication_description', 'original_description', 'pre_publication_abbreviation', 'post_publication_abbreviation', 'lab_code', 'submitter', 'owner', 'authorized_users', 'units']:
						if itemvalue != None: #XZ: the problem is that the item value can not be deleted
							PhenotypeItemUpdate.append('%s=%%s' % item)
							PhenotypeItemValues.append(itemvalue)

							continue #XZ: this is important to distinguish Phenotype item and Publication item

					elif item == "pubmed_id":
						#Only integer allowed in this field
						try:
							itemvalue = int(itemvalue)
						except:
							itemvalue = None
							
						#whether old PMID exists
						self.cursor.execute("SelecT PubMed_ID from Publication where Id = %d" % PublicationId)
						oldPMID = self.cursor.fetchone()
						if oldPMID:
							oldPMID = oldPMID[0]
						
						#whether new PMID already exists
						newPMID = None
						self.cursor.execute("SelecT Id from Publication where PubMed_ID = %d" % itemvalue)
						newPMID = self.cursor.fetchone()
						if newPMID:
							newPMID = newPMID[0]
						
						##the logic is still not very clear here
						if newPMID:
							#new PMID in record
							self.cursor.execute("Update PublishXRef set PublicationId = %d where InbredSetId=%d and PhenotypeId=%d and PublicationId=%d and Sequence=%d" % (newPMID, PInbredSetId, PhenotypeId, PublicationId, Sequence))
							#no need to update other fields
							PublicationItemUpdate = []
							break
						elif itemvalue:
							#have new PMID, but not in record or need to change
							self.cursor.execute("Update Publication set pubmed_id=%d where Id = %s" % (itemvalue,PublicationId))
						else:
							#no new PMID
							if oldPMID:
								#remove a pubmed_id, don't know if this ever gonna happen
								self.cursor.execute("SelecT max(Id) from Publication")
								maxId = self.cursor.fetchone()[0] + 1
								self.cursor.execute("SelecT * from Publication where Id = %d" % PublicationId)
								oldRecs = list(self.cursor.fetchone())
								oldRecs[0] = maxId
								oldRecs[1] = None
								NFields = ['%s'] * len(oldRecs)
								query = "insert into Publication Values (%s)" % string.join(NFields, ',')
								self.cursor.execute(query, tuple(oldRecs))
								self.cursor.execute("Update PublishXRef set PublicationId = %d where InbredSetId=%d and PhenotypeId=%d and PublicationId=%d and Sequence=%d" % (maxId, PInbredSetId, PhenotypeId, PublicationId, Sequence))
								PublicationId = maxId
								pass
							else:
								pass
						continue
					else:
						pass

					if itemvalue:						
						PublicationItemUpdate.append('%s=%%s' % item)
						PublicationItemValues.append(itemvalue)

				if PhenotypeItemUpdate:
                                        updateStr= string.join(PhenotypeItemUpdate,',')
                                        query = "Update Phenotype set %s where Id = %s" % (updateStr, PhenotypeId)
                                        self.cursor.execute(query,tuple(PhenotypeItemValues))

				if PublicationItemUpdate:
					updateStr= string.join(PublicationItemUpdate,',')
					query = "Update Publication set %s where Id = %s" % (updateStr, PublicationId)
					self.cursor.execute(query,tuple(PublicationItemValues))

			else: #ProbeSet or Genotype Data
				itemValues = []
	                        itemUpdate = []

				for item in modifyField:
					itemvalue = self.formdata.getvalue(item)
					if itemvalue != None:
						itemvalue = string.strip(itemvalue)
					else:
						pass
					if item == 'homologeneid':
						updateHomologeneid = True
						new_homologeneid = 0

						if itemvalue and len(itemvalue) > 0:
							try:
								new_homologeneid = int(itemvalue)
							except:
								heading = "Record Updating Result"
								detail = ["Can't update database. Homologeneid must be integer!"]
								self.error(heading=heading,detail=detail,error="Error")
								return
					else:
						itemUpdate.append('%s=%%s' % item) #XZ: Use %% to put a % in the output string
						itemValues.append(itemvalue)

				if itemUpdate:
					updateStr= string.join(itemUpdate,', ')
					comments = "%s modified %s at %s\n" % (self.userName, string.join(comments, ', '), ctime)
					if thisTrait.db.type == "ProbeSet":#XZ, June 29, 2010: The algorithm is not good. Need to fix it later.
					 	if thisTrait.chipid in (2,4):
					 		if thisTrait.name[-2:] == '_A':
								thisTrait.name = string.replace(thisTrait.name, '_A', '')
					 		elif thisTrait.name[-2:] == '_B':
								thisTrait.name = string.replace(thisTrait.name, '_B', '')
							else:
								pass
							query = "Update %s set %s where Name like '%s%%%%'" % (thisTrait.db.type,updateStr,thisTrait.name)
							self.cursor.execute(query,tuple(itemValues))
							self.cursor.execute("Update %s set comments = CONCAT(comments,'%s') where Name like '%s%%%%'" % (thisTrait.db.type, comments, thisTrait.name))
						elif thisTrait.sequence:
							query = "Update %s set %s where BlatSeq='%s'" % (thisTrait.db.type,updateStr,thisTrait.sequence)
							self.cursor.execute(query,tuple(itemValues))
							self.cursor.execute("Update %s set comments = CONCAT(comments,'%s') where BlatSeq='%s'" % (thisTrait.db.type, comments, thisTrait.sequence))
						else:
							query = "Update %s set %s where Name='%s'" % (thisTrait.db.type,updateStr,thisTrait.name)
							self.cursor.execute(query,tuple(itemValues))
							self.cursor.execute("Update %s set comments = CONCAT(comments,'%s') where Name='%s'" % (thisTrait.db.type, comments, thisTrait.name))
					else: #XZ: Genotype
						query = "Update %s set %s where SpeciesId=%s and Name='%s'" % (thisTrait.db.type,updateStr, webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, thisTrait.db.riset), thisTrait.name)
						self.cursor.execute(query,tuple(itemValues))

				if updateHomologeneid: #XZ: to update homologene id must be after updating geneid.
				#XZ: In one species, one homologeneid can have multiple geneid. One geneid only can have one homologeneid.
				#XZ: In Homologene table, GeneId is unique.
                                #XZ: Geneid might just being updated.
                                        thisTrait = webqtlTrait(fullname=self.formdata.getvalue('fullname'), cursor=self.cursor)
                                        thisTrait.retrieveInfo()

					if not thisTrait.geneid:
                                        	heading = "Record Updating Result"
						detail = ["There is no geneid associated with this trait. Can't update homologeneid info"]
						self.error(heading=heading,detail=detail,error="Error")
						return
					else:
						query = """
							SELECT Species.TaxonomyId
							FROM Species, InbredSet
							WHERE InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id
							""" % thisTrait.db.riset
						self.cursor.execute(query)
	                                        taxonomyId = self.cursor.fetchone()[0]

						if not new_homologeneid:
							query = """DELETE FROM Homologene WHERE GeneId=%s""" % thisTrait.geneid
                                                        self.cursor.execute(query)
						else:
							query = """SELECT GeneId FROM Homologene WHERE GeneId=%s""" % thisTrait.geneid
							self.cursor.execute(query)
							result = self.cursor.fetchone()

							if not result:
	                                                        query = """INSERT into Homologene (HomologeneId, GeneId, TaxonomyId) VALUES (%s, %s, %s)""" % (new_homologeneid, thisTrait.geneid, taxonomyId)
        	                                                self.cursor.execute(query)
							else:
								query = """UPDATE Homologene SET HomologeneId=%s WHERE GeneId=%s""" % (new_homologeneid, thisTrait.geneid)
								self.cursor.execute(query)


                                #XZ: It's critical to get lasted info first, then update gene level info across traits by geneid.
				#XZ: Need to build index on GeneId. Otherwise, it's too slow.
                                if thisTrait.db.type == 'ProbeSet':
                                        thisTrait = webqtlTrait(fullname=self.formdata.getvalue('fullname'), cursor=self.cursor)
                                        thisTrait.retrieveInfo()

					if thisTrait.geneid:
						if 'symbol' in modifyField:
							if thisTrait.symbol:
								query = """UPDATE ProbeSet SET Symbol='%s' WHERE GeneId=%s""" % (thisTrait.symbol, thisTrait.geneid)
							else:
                        	                                query = """UPDATE ProbeSet SET Symbol=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)

						if 'alias' in modifyField:
							if thisTrait.alias:
								newalias = []
								for alias in re.split('[,;|\s]+', thisTrait.alias):
									if alias and 0<len(alias):
										newalias.append(alias)
								thisTrait.alias = '; '.join(newalias)
								query = """UPDATE ProbeSet SET alias='%s' WHERE GeneId=%s""" % (thisTrait.alias, thisTrait.geneid)
							else:
								query = """UPDATE ProbeSet SET alias=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)

						if 'description' in modifyField:
							if thisTrait.description: #XZ: Attention, we must use "%s" instead of '%s'. Otherwise, to insert 3'UTR will generate error.
								query = """UPDATE ProbeSet SET description="%s" WHERE GeneId=%s""" % (thisTrait.description, thisTrait.geneid)
							else:
								query = """UPDATE ProbeSet SET description=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)

						if 'strand_gene' in modifyField:
							if thisTrait.strand_gene:
								query = """UPDATE ProbeSet SET Strand_Gene='%s' WHERE GeneId=%s""" % (thisTrait.strand_gene, thisTrait.geneid)
							else:
								query = """UPDATE ProbeSet SET Strand_Gene=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)

						if 'unigeneid' in modifyField:
							if thisTrait.unigeneid:
								query = """UPDATE ProbeSet SET UniGeneId='%s' WHERE GeneId=%s""" % (thisTrait.unigeneid, thisTrait.geneid)
							else:
								query = """UPDATE ProbeSet SET UniGeneId=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)

						if 'refseq_transcriptid' in modifyField:
							if thisTrait.refseq_transcriptid:
								query = """UPDATE ProbeSet SET RefSeq_TranscriptId='%s' WHERE GeneId=%s""" % (thisTrait.refseq_transcriptid, thisTrait.geneid)
							else:
								query = """UPDATE ProbeSet SET RefSeq_TranscriptId=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)

						if 'genbankid' in modifyField:
							if thisTrait.genbankid:
								query = """UPDATE ProbeSet SET GenbankId='%s' WHERE GeneId=%s""" % (thisTrait.genbankid, thisTrait.geneid)
							else:
								query = """UPDATE ProbeSet SET GenbankId=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)

						if 'omim' in modifyField:
							if thisTrait.omim:
								query = """UPDATE ProbeSet SET OMIM='%s' WHERE GeneId=%s""" % (thisTrait.omim, thisTrait.geneid)
							else:
								query = """UPDATE ProbeSet SET OMIM=NULL WHERE GeneId=%s""" % (thisTrait.geneid)
							self.cursor.execute(query)


		###Modify Trait Data
		if thisTrait.db.type == 'Publish' and dataID > 0 and fd.formdata.getvalue("modifiedDataField"):
			StrainIds = []
			for item in fd.strainlist:
				self.cursor.execute('SelecT Id from Strain where Name = "%s"' % item)
				StrainId = self.cursor.fetchone()
				if not StrainId:
					raise ValueError
				else:
					StrainIds.append(StrainId[0])
			comments.append('Trait Value')
			#XZ, 03/05/2009: Xiaodong changed Data to PublishData, SE to PublishSE
			self.cursor.execute('delete from PublishData where Id = %d' % dataID)
			self.cursor.execute('delete from PublishSE where DataId = %d' % dataID)
			self.cursor.execute('delete from NStrain where DataId = %d' % dataID)
				
			for i, strain in enumerate(fd.strainlist):
				sId = StrainIds[i]
				if fd.allTraitData.has_key(strain):
					tdata = fd.allTraitData[strain]
					_val, _var, _N = tdata.val, tdata.var, tdata.N
					if _val != None:
						#XZ, 03/05/2009: Xiaodong changed Data to PublishData, SE to PublishSE
						self.cursor.execute('insert into PublishData values(%d, %d, %s)' % (dataID, sId, _val))
					if _var != None:
						self.cursor.execute('insert into PublishSE values(%d, %d, %s)' % (dataID, sId, _var))
					if _N != None:
						self.cursor.execute('insert into NStrain values(%d, %d, %s)' % (dataID, sId, _N))
				else:
					pass
			#end for
		else:
			pass
		TD_LR = HT.TD(valign="top", bgcolor="#eeeeee",height=200,width="100%")
		main_title = HT.Paragraph(" Record Updating Result", Class="title")

		TD_LR.append(main_title,HT.Blockquote('Successfully updated record %s in database ' % thisTrait.name, thisTrait.db.genHTML(), '.'))
		if thisTrait.db.type == 'Publish':
			comments = "%s modified %s at %s\n" % (self.userName, string.join(comments, ', '), ctime)
			self.cursor.execute("Update PublishXRef set comments = CONCAT(comments,'%s') where InbredSetId=%d and PhenotypeId=%d and PublicationId=%d and Sequence=%d" % (comments,  PInbredSetId, PhenotypeId, PublicationId, Sequence))
			
		if 0:
			heading = "Record Updating Result"
			detail = ["Can't update database. The server may be down at this time or you don't have the permission"]
			self.error(heading=heading,detail=detail,error="Error")
			return
		self.dict['body'] = str(TD_LR)
        def __init__(self,fd):

                templatePage.__init__(self, fd)

                if not self.openMysql():
                        return
                if not fd.genotype:
                        fd.readData()

                self.searchResult = fd.formdata.getvalue('searchResult')
                if not self.searchResult:
                        heading = 'Compare Correlates'
                        detail = ['You need to select at least two traits in order to generate correlation matrix.']
                        self.error(heading=heading,detail=detail)
                        print 'Content-type: text/html\n'
                        self.write()
                        return
                if type("1") == type(self.searchResult):
                        self.searchResult = [self.searchResult]

                if self.searchResult:
                        if len(self.searchResult) > 100:
                                heading = 'Compare Correlates'
                                detail = ['In order to display Compare Correlates properly, Do not select more than %d traits for Compare Correlates.' % 100]
                                self.error(heading=heading,detail=detail)
                                print 'Content-type: text/html\n'
                                self.write()
                                return
                        else:
                                pass

                        traitList = []
                        for item in self.searchResult:
                                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                                thisTrait.retrieveInfo()
                                traitList.append(thisTrait)
                else:
                        heading = 'Compare Correlates'
                        detail = [HT.Font('Error : ',color='red'),HT.Font('Error occurs while retrieving data from database.',color='black')]
                        self.error(heading=heading,detail=detail)
                        print 'Content-type: text/html\n'
                        self.write()
                        return


                ##########
                filename= webqtlUtil.genRandStr("mult_")
                fp = open(webqtlConfig.IMGDIR+filename, 'wb')
                fp.write('%s\n' % fd.RISet)
                for thisTrait in traitList:
                        fp.write("%s,%s,%s\n" % (thisTrait.db.type,thisTrait.db.id,thisTrait.name))
                fp.close()
                fd.formdata["filename"] = filename

                params = {"filename":filename, "targetDatabase":"",
                        "threshold":0.5, "subsetSize":10,
                        "correlation":"pearson", "subsetCount":10,
                        "firstRun":"1"}
                results = []
                txtOutputFileName = ""

                self.dict['body'] = multitrait.TraitCorrelationPage(fd, params, self.cursor, traitList, results, 
                                                        fd.RISet,txtOutputFileName).dict['body']
                self.dict['title'] = 'Compare Correlates'
        def __init__(self,fd):

                templatePage.__init__(self, fd)

                if not self.openMysql():
                        return
                if not fd.genotype:
                        fd.readGenotype()


                self.searchResult = fd.formdata.getvalue('searchResult')

                if not self.searchResult:
                        templatePage.__init__(self, fd)
                        heading = 'QTL Heatmap'
                        detail = ['You need to select at least two traits in order to generate correlation matrix.']
                        self.error(heading=heading,detail=detail)
                        return
                if type("1") == type(self.searchResult):
                        self.searchResult = string.split(self.searchResult,'\t')


                if self.searchResult:
                        if len(self.searchResult) > webqtlConfig.MAXCORR:
                                heading = 'QTL Heatmap'
                                detail = ['In order to display the QTL heat map properly, do not select more than %d traits for analysis.' % webqtlConfig.MAXCORR]
                                self.error(heading=heading,detail=detail)
                                return

                        traitList = []
                        traitDataList = []
                        for item in self.searchResult:
                                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                                thisTrait.retrieveInfo()
                                thisTrait.retrieveData(fd.strainlist)
                                traitList.append(thisTrait)
                                traitDataList.append(thisTrait.exportData(fd.strainlist))
                else:
                        heading = 'QTL Heatmap'
                        detail = [HT.Font('Error : ',color='red'),HT.Font('Error occurs while retrieving data from database.',color='black')]
                        self.error(heading=heading,detail=detail)
                        return

                self.colorScheme = fd.formdata.getvalue('colorScheme')
                if not self.colorScheme:
                        self.colorScheme = '1'

                self.dict['title'] = 'QTL heatmap'

                NNN = len(traitList)

                if NNN == 0:
                        heading = "QTL Heatmap"
                        detail = ['No trait was selected for %s data set. No QTL heatmap was generated.' % fd.RISet]
                        self.error(heading=heading,detail=detail)
                        return
                elif NNN < 2:
                        templatePage.__init__(self, fd)
                        heading = 'QTL Heatmap'
                        detail = ['You need to select at least two traits in order to generate QTL heatmap.']
                        self.error(heading=heading,detail=detail)
                        return
                else:
                        #XZ: It's necessory to define canvas here
                        canvas = pid.PILCanvas(size=(80+NNN*20,880))

                        names = map(webqtlTrait.displayName, traitList)

                        self.targetDescriptionChecked = fd.formdata.getvalue('targetDescriptionCheck', '')

                        #XZ, 7/29/2009: create trait display and find max strWidth
                        strWidth = 0
                        for j in range(len(names)):
                                thisTrait = traitList[j]

                                if self.targetDescriptionChecked:
                                    if thisTrait.db.type == 'ProbeSet':
                                        if thisTrait.probe_target_description:
                                                names[j] += ' [%s at Chr %s @ %2.3fMB, %s]' % (thisTrait.symbol, thisTrait.chr, thisTrait.mb, thisTrait.probe_target_description)
                                        else:
                                                names[j] += ' [%s at Chr %s @ %2.3fMB]' % (thisTrait.symbol, thisTrait.chr, thisTrait.mb)
                                    elif thisTrait.db.type == 'Geno':
                                        names[j] += ' [Chr %s @ %2.3fMB]' % (thisTrait.chr, thisTrait.mb)
                                    elif thisTrait.db.type == 'Publish':
                                        if thisTrait.abbreviation:
                                            names[j] += ' [%s]' % (thisTrait.abbreviation)
                                        else:
                                            pass
                                    else:
                                        pass

                                i = canvas.stringWidth(names[j],font=self.labelFont)
                                if i > strWidth:
                                        strWidth = i

                        width = NNN*20
                        xoffset = 40
                        yoffset = 40
                        cellHeight = 3
                        nLoci = reduce(lambda x,y: x+y, map(lambda x: len(x),fd.genotype),0)

                        if nLoci > 2000:
                                cellHeight = 1
                        elif nLoci > 1000:
                                cellHeight = 2
                        elif nLoci < 200:
                                cellHeight = 10
                        else:
                                pass

                        pos = range(NNN)
                        neworder = []
                        BWs = Plot.BWSpectrum()
                        colors100 = Plot.colorSpectrum()
                        colors = Plot.colorSpectrum(130)
                        finecolors = Plot.colorSpectrum(250)
                        colors100.reverse()
                        colors.reverse()
                        finecolors.reverse()

                        scaleFont=pid.Font(ttf="tahoma",size=10,bold=0)

                        self.clusterChecked = fd.formdata.getvalue('clusterCheck', '')


                        if not self.clusterChecked: #XZ: this part is for original order
                                for i in range(len(names)):
                                        neworder.append((xoffset+20*(i+1), i))

                                canvas = pid.PILCanvas(size=(80+NNN*20+240,80+ self.topHeight +5+5+strWidth+nLoci*cellHeight+80+20*cellHeight))

                                self.drawTraitNameBottom(canvas,names,yoffset,neworder,strWidth)
                        else: #XZ: this part is to cluster traits
                                self.topHeight = 400
                                canvas = pid.PILCanvas(size=(80+NNN*20+240,80+ self.topHeight +5+5+strWidth+nLoci*cellHeight+80+20*cellHeight))

                                corArray = [([0] * (NNN))[:] for i in range(NNN)]

                                nnCorr = len(fd.strainlist)

                                #XZ, 08/04/2009: I commented out pearsonArray, spearmanArray
                                for i, thisTrait in enumerate(traitList):
                                    names1 = [thisTrait.db.name, thisTrait.name, thisTrait.cellid]
                                    for j, thisTrait2 in enumerate(traitList):
                                            names2 = [thisTrait2.db.name, thisTrait2.name, thisTrait2.cellid]
                                            if j < i:
                                                    corr,nOverlap = webqtlUtil.calCorrelation(traitDataList[i],traitDataList[j],nnCorr)
                                                    if (1-corr) < 0:
                                                            distance = 0.0
                                                    else:
                                                            distance = 1-corr
                                                    corArray[i][j] = distance
                                                    corArray[j][i] = distance
                                            elif j == i:
                                                    corArray[i][j] = 0.0
                                            else:
                                                    pass

                                #XZ, 7/29/2009: The parameter d has info of cluster (group member and distance). The format of d is tricky. Print it out to see it's format.
                                d = slink.slink(corArray)

                                #XZ, 7/29/2009: Attention: The 'neworder' is changed by the 'draw' function
                                #XZ, 7/30/2009: Only toppos[1][0] and top[1][1] are used later. Then what toppos[0] is used for? 
                                toppos = self.draw(canvas,names,d,xoffset,yoffset,neworder)
                                self.drawTraitNameTop(canvas,names,yoffset,neworder,strWidth)

                                #XZ, 7/29/2009: draw the top vertical line
                                canvas.drawLine(toppos[1][0],toppos[1][1],toppos[1][0],yoffset)

                                #XZ: draw string 'distance = 1-r'
                                canvas.drawString('distance = 1-r',neworder[-1][0] + 50, self.topHeight*3/4,font=self.labelFont,angle=90)

                                #draw Scale
                                scaleFont=pid.Font(ttf="tahoma",size=10,bold=0)
                                x = neworder[-1][0]
                                canvas.drawLine(x+5, self.topHeight+yoffset, x+5, yoffset, color=pid.black)
                                y = 0
                                while y <=2:
                                        canvas.drawLine(x+5, self.topHeight*y/2.0+yoffset, x+10, self.topHeight*y/2.0+yoffset)
                                        canvas.drawString('%2.1f' % (2-y), x+12, self.topHeight*y/2.0+yoffset, font=scaleFont)
                                        y += 0.5


                        chrname = 0
                        chrnameFont=pid.Font(ttf="tahoma",size=24,bold=0)
                        Ncol = 0

                        gifmap = HT.Map(name='traitMap')

                        nearestMarkers = self.getNearestMarker(traitList, fd.genotype)

                        # import cPickle
                        sessionfile = fd.formdata.getvalue("session")

                        if sessionfile:
                                fp = open(os.path.join(webqtlConfig.TMPDIR, sessionfile + '.session'), 'rb')
                                permData = cPickle.load(fp)
                                fp.close()
                        else:
                                permData = {}

                        #XZ, 7/31/2009: This for loop is to generate the heatmap
                        #XZ: draw trait by trait instead of marker by marker
                        for order in neworder:
                                #startHeight = 40+400+5+5+strWidth
                                startHeight = self.topHeight + 40+5+5+strWidth
                                startWidth = order[0]-5
                                if Ncol and Ncol % 5 == 0:
                                        drawStartPixel = 8
                                else:
                                        drawStartPixel = 9

                                tempVal = traitDataList[order[1]]
                                _vals = []
                                _strains = [] 
                                for i in range(len(fd.strainlist)):
                                        if tempVal[i] != None:
                                                _strains.append(fd.strainlist[i])
                                                _vals.append(tempVal[i])

                                qtlresult = fd.genotype.regression(strains = _strains, trait = _vals)

                                if sessionfile:
                                        LRSArray = permData[str(traitList[order[1]])]
                                else:
                                        LRSArray = fd.genotype.permutation(strains = _strains, trait = _vals, nperm = 1000)
                                        permData[str(traitList[order[1]])] = LRSArray

                                sugLRS = LRSArray[369]
                                sigLRS = LRSArray[949]
                                prechr = 0
                                chrstart = 0
                                nearest = nearestMarkers[order[1]]
                                midpoint = []

                                for item in qtlresult:
                                        if item.lrs > webqtlConfig.MAXLRS:
                                                adjustlrs = webqtlConfig.MAXLRS
                                        else:
                                                adjustlrs = item.lrs

                                        if item.locus.chr != prechr:
                                                if prechr:
                                                        canvas.drawRect(startWidth-drawStartPixel, startHeight, startWidth+10, startHeight+3,edgeColor=pid.white, edgeWidth=0, fillColor=pid.white)
                                                        startHeight+= 3
                                                        if not chrname:
                                                                canvas.drawString(prechr,xoffset-20,(chrstart+startHeight)/2,font = chrnameFont,color=pid.dimgray)
                                                prechr = item.locus.chr
                                                chrstart = startHeight
                                        if self.colorScheme == '0':
                                                if adjustlrs <= sugLRS:
                                                        colorIndex = int(65*adjustlrs/sugLRS)
                                                else:
                                                        colorIndex = int(65 + 35*(adjustlrs-sugLRS)/(sigLRS-sugLRS))
                                                if colorIndex > 99:
                                                        colorIndex = 99
                                                colorIndex = colors100[colorIndex]
                                        elif self.colorScheme == '1':
                                                sugLRS = LRSArray[369]/2.0
                                                if adjustlrs <= sugLRS:
                                                        colorIndex = BWs[20+int(50*adjustlrs/sugLRS)]
                                                else:
                                                        if item.additive > 0:
                                                                colorIndex = int(80 + 50*(adjustlrs-sugLRS)/(sigLRS-sugLRS))
                                                        else:
                                                                colorIndex = int(50 - 50*(adjustlrs-sugLRS)/(sigLRS-sugLRS))
                                                        if colorIndex > 129:
                                                                colorIndex = 129
                                                        if colorIndex < 0:
                                                                colorIndex = 0
                                                        colorIndex = colors[colorIndex]
                                        elif self.colorScheme == '2':
                                                if item.additive > 0:
                                                        colorIndex = int(150 + 100*(adjustlrs/sigLRS))
                                                else:
                                                        colorIndex = int(100 - 100*(adjustlrs/sigLRS))
                                                if colorIndex > 249:
                                                        colorIndex = 249
                                                if colorIndex < 0:
                                                                colorIndex = 0
                                                colorIndex = finecolors[colorIndex]
                                        else:
                                                colorIndex = pid.white

                                        if startHeight > 1:
                                                canvas.drawRect(startWidth-drawStartPixel, startHeight, startWidth+10, startHeight+cellHeight,edgeColor=colorIndex, edgeWidth=0, fillColor=colorIndex)
                                        else:
                                                canvas.drawLine(startWidth-drawStartPixel, startHeight, startWidth+10, startHeight, Color=colorIndex)

                                        if item.locus.name == nearest:
                                                midpoint = [startWidth,startHeight-5]
                                        startHeight+=cellHeight

                                #XZ, map link to trait name and band
                                COORDS = "%d,%d,%d,%d" %(startWidth-drawStartPixel,self.topHeight+40,startWidth+10,startHeight)
                                HREF = "javascript:showDatabase2('%s','%s','%s');" % (traitList[order[1]].db.name, traitList[order[1]].name, traitList[order[1]].cellid)
                                Areas = HT.Area(shape='rect',coords=COORDS,href=HREF, title='%s' % names[order[1]])
                                gifmap.areas.append(Areas)

                                if midpoint:
                                        traitPixel = ((midpoint[0],midpoint[1]),(midpoint[0]-6,midpoint[1]+12),(midpoint[0]+6,midpoint[1]+12))
                                        canvas.drawPolygon(traitPixel,edgeColor=pid.black,fillColor=pid.orange,closed=1)

                                if not chrname:
                                        canvas.drawString(prechr,xoffset-20,(chrstart+startHeight)/2,font = chrnameFont,color=pid.dimgray)
                                chrname = 1
                                Ncol += 1


                        #draw Spectrum
                        startSpect = neworder[-1][0] + 30
                        startHeight = self.topHeight + 40+5+5+strWidth

                        if self.colorScheme == '0':
                                for i in range(100):
                                        canvas.drawLine(startSpect+i,startHeight+20,startSpect+i,startHeight+40,color=colors100[i])
                                scaleFont=pid.Font(ttf="tahoma",size=10,bold=0)
                                canvas.drawLine(startSpect,startHeight+45,startSpect,startHeight+39,color=pid.black)
                                canvas.drawString('LRS = 0',startSpect,startHeight+55,font=scaleFont)
                                canvas.drawLine(startSpect+64,startHeight+45,startSpect+64,startHeight+39,color=pid.black)
                                canvas.drawString('Suggestive LRS',startSpect+64,startHeight+55,font=scaleFont)
                                canvas.drawLine(startSpect+99,startHeight+45,startSpect+99,startHeight+39,color=pid.black)
                                canvas.drawString('Significant LRS',startSpect+105,startHeight+40,font=scaleFont)
                        elif self.colorScheme == '1':
                                for i in range(50):
                                        canvas.drawLine(startSpect+i,startHeight,startSpect+i,startHeight+40,color=BWs[20+i])
                                for i in range(50,100):
                                        canvas.drawLine(startSpect+i,startHeight,startSpect+i,startHeight+20,color=colors[100-i])
                                        canvas.drawLine(startSpect+i,startHeight+20,startSpect+i,startHeight+40,color=colors[30+i])

                                canvas.drawLine(startSpect,startHeight+45,startSpect,startHeight+39,color=pid.black)
                                canvas.drawString('LRS = 0',startSpect,startHeight+60,font=scaleFont)
                                canvas.drawLine(startSpect+50,startHeight+45,startSpect+50,startHeight+39,color=pid.black)
                                canvas.drawString('0.5*Suggestive LRS',startSpect+50,startHeight+ 60,font=scaleFont)
                                canvas.drawLine(startSpect+99,startHeight+45,startSpect+99,startHeight+39,color=pid.black)
                                canvas.drawString('Significant LRS',startSpect+105,startHeight+50,font=scaleFont)
                                textFont=pid.Font(ttf="verdana",size=18,bold=0)
                                canvas.drawString('%s +' % fd.ppolar,startSpect+120,startHeight+ 35,font=textFont,color=pid.red)
                                canvas.drawString('%s +' % fd.mpolar,startSpect+120,startHeight+ 15,font=textFont,color=pid.blue)
                        elif self.colorScheme == '2':
                                for i in range(100):
                                        canvas.drawLine(startSpect+i,startHeight,startSpect+i,startHeight+20,color=finecolors[100-i])
                                        canvas.drawLine(startSpect+i,startHeight+20,startSpect+i,startHeight+40,color=finecolors[150+i])

                                canvas.drawLine(startSpect,startHeight+45,startSpect,startHeight+39,color=pid.black)
                                canvas.drawString('LRS = 0',startSpect,startHeight+60,font=scaleFont)
                                canvas.drawLine(startSpect+99,startHeight+45,startSpect+99,startHeight+39,color=pid.black)
                                canvas.drawString('Significant LRS',startSpect+105,startHeight+50,font=scaleFont)
                                textFont=pid.Font(ttf="verdana",size=18,bold=0)
                                canvas.drawString('%s +' % fd.ppolar,startSpect+120,startHeight+ 35,font=textFont,color=pid.red)
                                canvas.drawString('%s +' % fd.mpolar,startSpect+120,startHeight+ 15,font=textFont,color=pid.blue)


                        filename= webqtlUtil.genRandStr("Heatmap_")
                        canvas.save(webqtlConfig.IMGDIR+filename, format='png')
                        img2=HT.Image('/image/'+filename+'.png',border=0,usemap='#traitMap')
                        imgUrl = 'Right-click or control-click on the link to download this graph as a <a href="/image/%s.png" class="normalsize" target="_blank">PNG file</a>' % filename

                        form = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name='showDatabase', submit=HT.Input(type='hidden'))
                        hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':fd.RISet+"Geno",'CellID':'_','RISet':fd.RISet,'searchResult':string.join(self.searchResult,'\t')}
                        if fd.incparentsf1:
                                hddn['incparentsf1']='ON'
                        for key in hddn.keys():
                                form.append(HT.Input(name=key, value=hddn[key], type='hidden'))

                        heatmap = HT.Input(type='button' ,name='mintmap',value='Redraw QTL Heatmap', onClick="databaseFunc(this.form,'heatmap');",Class="button")
                        spects = {'0':'Single Spectrum','1':'Grey + Blue + Red','2':'Blue + Red'}
                        schemeMenu = HT.Select(name='colorScheme')
                        schemeMenu.append(('Single Spectrum',0))
                        schemeMenu.append(('Grey + Blue + Red',1))
                        schemeMenu.append(('Blue + Red',2))
                        schemeMenu.selected.append(spects[self.colorScheme])

                        clusterCheck= HT.Input(type='checkbox', Class='checkbox', name='clusterCheck',checked=0)
                        targetDescriptionCheck = HT.Input(type='checkbox', Class='checkbox', name='targetDescriptionCheck',checked=0)

                        form.append(gifmap,schemeMenu, heatmap, HT.P(), clusterCheck, '  Cluster traits  ', targetDescriptionCheck, '  Add description', HT.P(),img2, HT.P(), imgUrl)

                        if not sessionfile:
                                filename = webqtlUtil.generate_session()
                                webqtlUtil.dump_session(permData, os.path.join(webqtlConfig.TMPDIR, filename +'.session'))
                                sessionfile=filename

                        form.append(HT.Input(name='session', value=sessionfile, type='hidden'))

                        heatmapHelp = HT.Input(type='button' ,name='heatmapHelpButton',value='Info', onClick="openNewWin('/heatmap.html');",Class="button")

                        heatmapHeading = HT.Paragraph('QTL Heatmap ', heatmapHelp, Class="title")

                        TD_LR = HT.TD(colspan=2,height=200,width="100%",bgColor='#eeeeee')
                        TD_LR.append(heatmapHeading, HT.P(),HT.P(),HT.P(),HT.P(),HT.P(),form)

                        self.dict['body'] = str(TD_LR)
    def exportDatasetPage(self, fd, PublishFreeze_Name):

        #return PublishFreeze_Name

                if not self.openMysql():
                        return

                self.cursor.execute( "select InbredSet.Name from PublishFreeze, InbredSet where PublishFreeze.InbredSetId=InbredSet.Id and PublishFreeze.Name='%s'" % PublishFreeze_Name )
                self.RISet = self.cursor.fetchone()[0]

                fd.RISet = self.RISet
                fd.incparentsf1 = 1
                fd.readGenotype()
                strainlist = fd.f1list + fd.strainlist

                #return str(strainlist)

                self.cursor.execute("Select Species.Name from Species, InbredSet where InbredSet.SpeciesId = Species.Id and InbredSet.Name = '%s'" % fd.RISet)
                self.Species = self.cursor.fetchone()[0]

                #return Species

                self.searchResult = []

                self.cursor.execute("Select PublishXRef.Id from PublishXRef, InbredSet where PublishXRef.InbredSetId = InbredSet.Id and InbredSet.Name = '%s'" % self.RISet)
                result = self.cursor.fetchall()

                for one_result in result:
                    self.searchResult.append( "%s::%s" % (PublishFreeze_Name, one_result[0]) )
                
                #return self.searchResult

                strainlisthead = []
                for strain in strainlist:
                    strainlisthead += [strain + "_Value"]
                    strainlisthead += [strain + "_SE"]
                    strainlisthead += [strain + "_N"]

                fields = ["Index", "Species", "Cross", "Database", "ProbeSetID/RecordID", "PubMed_ID",
                    "Pre Publication Description", "Post Publication Description", "Original Description", "Pre Publication Abbreviation", "Post Publication Abbreviation",
                    "Mean_Expression"] + strainlisthead

                if self.searchResult:
                        traitList = []
                        for item in self.searchResult:
                                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                                thisTrait.retrieveInfo(QTL=1)
                                thisTrait.retrieveData(strainlist=strainlist)
                                traitList.append(thisTrait)

                        text = [fields]
                        for i, thisTrait in enumerate(traitList):
                                text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name, thisTrait.pubmed_id,
                                    thisTrait.pre_publication_description, thisTrait.post_publication_description,
                                    thisTrait.original_description,
                                    thisTrait.pre_publication_abbreviation, thisTrait.post_publication_abbreviation])

                                testval = thisTrait.exportData(strainlist)
                                try:
                                    mean = reaper.anova(testval)[0]
                                except:
                                    mean = 'N/A'
                                text[-1].append(mean)
                                testvar = thisTrait.exportData(strainlist, type="var")
                                testn = thisTrait.exportData(strainlist, type="N")
                                testdata = zip(testval, testvar, testn)
                                testdatalist = []
                                for data in testdata:
                                        testdatalist += list(data)
                                text[-1] += testdatalist

                        self.content_type = 'application/xls'
                        self.content_disposition = 'attachment; filename=%s' % ('export-%s.txt' % time.strftime("%y-%m-%d-%H-%M"))
                        self.attachment += ("Data source: The GeneNetwork at %s\n" % webqtlConfig.PORTADDR)
                        self.attachment += ("Citations: Please see %s/reference.html\n" % webqtlConfig.PORTADDR)
                        self.attachment += ("Date: %s\n" % time.strftime("%B %d, %Y", time.gmtime()))
                        self.attachment += ("Time: %s GMT\n" % time.strftime("%H:%M", time.gmtime()))
                        self.attachment += ("Status of data ownership: Possibly unpublished data; please see %s/statusandContact.html for details on sources, ownership, and usage of these data.\n" % webqtlConfig.PORTADDR)
                        self.attachment += ("This output file contains data from %d GeneNetwork databases listed below.\n" % len(traitList))
                        self.attachment += ("\n")
                        self.attachment += ("Funding for The GeneNetwork:\n")
                        self.attachment += ("NIGMS Systems Genetics and Precision Medicine project (R01 GM123489, 2017-2021)\n")
                        self.attachment += ("NIDA NIDA Core Center of Excellence in Transcriptomics, Systems Genetics, and the Addictome (P30 DA044223, 2017-2022)\n")
                        self.attachment += ("NIA Translational Systems Genetics of Mitochondria, Metabolism, and Aging (R01AG043930, 2013-2018)\n")
                        self.attachment += ("NIAAA Integrative Neuroscience Initiative on Alcoholism (U01 AA016662, U01 AA013499, U24 AA013513, U01 AA014425, 2006-2017)\n")
                        self.attachment += ("NIDA, NIMH, and NIAAA (P20-DA 21131, 2001-2012)\n")
                        self.attachment += ("NCI MMHCC (U01CA105417), NCRR, BIRN, (U24 RR021760)\n")
                        self.attachment += ("PLEASE RETAIN DATA SOURCE INFORMATION WHENEVER POSSIBLE\n")
                        self.attachment += ("\n")
                        for item in text:
                                self.attachment += string.join(map(lambda cell : self.trim(str(cell)), item), '\t') + "\n"
                        self.cursor.close()
                else:
                        fd.req.content_type = 'text/html'
                        heading = 'Export Collection'
                        detail = [HT.Font('Error : ',color='red'),HT.Font('Error occurs while retrieving data from database.',color='black')]
                        self.error(heading=heading,detail=detail)
    def exportDatasetPage(self, fd, PublishFreeze_Name):

        #return PublishFreeze_Name

                if not self.openMysql():
                        return

                self.cursor.execute( "select InbredSet.Name from PublishFreeze, InbredSet where PublishFreeze.InbredSetId=InbredSet.Id and PublishFreeze.Name='%s'" % PublishFreeze_Name )
                self.RISet = self.cursor.fetchone()[0]

                fd.RISet = self.RISet
                fd.incparentsf1 = 1
                fd.readGenotype()
                strainlist = fd.f1list + fd.strainlist

                #return str(strainlist)

                self.cursor.execute("Select Species.Name from Species, InbredSet where InbredSet.SpeciesId = Species.Id and InbredSet.Name = '%s'" % fd.RISet)
                self.Species = self.cursor.fetchone()[0]

                #return Species

                self.searchResult = []

                self.cursor.execute("Select PublishXRef.Id from PublishXRef, InbredSet where PublishXRef.InbredSetId = InbredSet.Id and InbredSet.Name = '%s'" % self.RISet)
                result = self.cursor.fetchall()

                for one_result in result:
                    self.searchResult.append( "%s::%s" % (PublishFreeze_Name, one_result[0]) )
                
                #return self.searchResult


                fields = ["ID", "Species", "Cross", "Database", "ProbeSetID / RecordID", "Symbol", "Description", "ProbeTarget", "PubMed_ID", "Phenotype", "Chr", "Mb", "Alias", "Gene_ID", "UniGene_ID", "Strand_Probe ", "Strand_Gene ", 
"Probe_set_specificity", "Probe_set_BLAT_score", "Probe_set_BLAT_Mb_start", "Probe_set_BLAT_Mb_end ", "QTL_Chr", "Locus_at_Peak", "Max_LRS", "P_value_of_MAX", "Mean_Expression"] + strainlist


                if self.searchResult:
                        traitList = []
                        for item in self.searchResult:
                                thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
                                thisTrait.retrieveInfo(QTL=1)
                                thisTrait.retrieveData(strainlist=strainlist)
                                traitList.append(thisTrait)

                        text = [fields]
                        for i, thisTrait in enumerate(traitList):
                                if thisTrait.db.type == 'ProbeSet':
                                        if not thisTrait.cellid: #ProbeSet
                                                text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name, thisTrait.symbol, thisTrait.description, thisTrait.probe_target_description,"", "", thisTrait.chr, thisTrait.mb, thisTrait.alias, thisTrait.geneid, thisTrait.unigeneid, thisTrait.strand_probe, thisTrait.strand_gene, thisTrait.probe_set_specificity, thisTrait.probe_set_blat_score, thisTrait.probe_set_blat_mb_start, thisTrait.probe_set_blat_mb_end, locusChr[thisTrait.locus], thisTrait.locus, thisTrait.lrs, thisTrait.pvalue])
                                        else: #Probe
                                                text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name + " : " + thisTrait.cellid, thisTrait.symbol, thisTrait.description, thisTrait.probe_target_description,"", "", thisTrait.chr, thisTrait.mb, thisTrait.alias, thisTrait.geneid, thisTrait.unigeneid, "", "", "", "", "", "", "", "", "", ""])
                                elif thisTrait.db.type == 'Publish':
                                    if thisTrait.pre_publication_description:
                                        if thisTrait.pubmed_id:
                                            text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name, "", "", "", thisTrait.pubmed_id, thisTrait.post_publication_description, "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""])
                                        else:
                                            text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name, "", "", "", "", thisTrait.pre_publication_description, "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""])
                                    else:
                                            text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name, "", "", "", thisTrait.pubmed_id, thisTrait.post_publication_description, "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""])  

                                elif thisTrait.db.type == 'Temp':
                                        text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name, "", thisTrait.description, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""])
                                elif thisTrait.db.type == 'Geno':
                                        text.append([str(i+1), self.Species, self.RISet, thisTrait.db.fullname, thisTrait.name, "", thisTrait.name,"", "", "", thisTrait.chr, thisTrait.mb, "", "", "", "", "", "", "", "", "", "", "", "", ""])
                                else:
                                        continue

                                testval = thisTrait.exportData(strainlist)
                                try:
                                    mean = reaper.anova(testval)[0]
                                except:
                                    mean = 'N/A'
                                text[-1].append(mean)
                                text[-1] += testval
                        if len(text[0]) < 255 or len(text) < 255:
                                transpose = 0
                                if len(text[0]) >= 255:
                                        text = webqtlUtil.transpose(text)
                                        transpose = 1
                                filename = os.path.join(webqtlConfig.TMPDIR, webqtlUtil.generate_session() +'.xls')

                                # Create a new Excel workbook
                                workbook = xl.Writer(filename)
                                worksheet = workbook.add_worksheet()
                                headingStyle = workbook.add_format(align = 'center', bold = 1, size=13, color = 'green')
                                titleStyle = workbook.add_format(align = 'left', bold = 0, size=13, border = 1, border_color="gray")

                                ##Write title Info
                                worksheet.write([0, 0], "Data source: The GeneNetwork at %s" % webqtlConfig.PORTADDR, titleStyle)
                                worksheet.write([1, 0], "Citations: Please see %s/reference.html" % webqtlConfig.PORTADDR, titleStyle)
                                worksheet.write([2, 0], "Date : %s" % time.strftime("%B %d, %Y", time.gmtime()), titleStyle)
                                worksheet.write([3, 0], "Time : %s GMT" % time.strftime("%H:%M ", time.gmtime()), titleStyle)
                                worksheet.write([4, 0], "Status of data ownership: Possibly unpublished data; please see %s/statusandContact.html for details on sources, ownership, and usage of these data." % webqtlConfig.PORTADDR, titleStyle)
                                worksheet.write([6, 0], "This output file contains data from %d GeneNetwork databases listed below" % len(traitList), titleStyle)

                                # Row and column are zero indexed
                                nrow = startRow = 8
                                for row in text:
                                    for ncol, cell in enumerate(row):
                                        if nrow == startRow:
                                                worksheet.write([nrow, ncol], cell.strip(), headingStyle)
                                                worksheet.set_column([ncol, ncol], 2*len(cell))
                                        else:
                                                worksheet.write([nrow, ncol], cell)
                                    nrow += 1

                                worksheet.write([nrow+1, 0], "Funding for The GeneNetwork: NIAAA (U01AA13499, U24AA13513), NIDA, NIMH, and NIAAA (P20-DA 21131), NCI MMHCC (U01CA105417), and NCRR (U24 RR021760)", titleStyle)
                                worksheet.write([nrow+2, 0], "PLEASE RETAIN DATA SOURCE INFORMATION WHENEVER POSSIBLE", titleStyle)
                                workbook.close()

                                fp = open(filename, 'rb')
                                text = fp.read()
                                fp.close()

                                self.content_type = 'application/xls'
                                self.content_disposition = 'attachment; filename=%s' % ('export-%s.xls' % time.strftime("%y-%m-%d-%H-%M"))
                                self.attachment = text
                        else:
                                self.content_type = 'application/xls'
                                self.content_disposition = 'attachment; filename=%s' % ('export-%s.txt' % time.strftime("%y-%m-%d-%H-%M"))
                                for item in text:
                                        self.attachment += string.join(map(str, item), '\t')+ "\n"
                        self.cursor.close()
                else:
                        fd.req.content_type = 'text/html'
                        heading = 'Export Collection'
                        detail = [HT.Font('Error : ',color='red'),HT.Font('Error occurs while retrieving data from database.',color='black')]
                        self.error(heading=heading,detail=detail)
Example #24
0
    def __init__(self, fd):
        templatePage.__init__(self, fd)
        self.initializeDisplayParameters(fd)
        if not fd.genotype:
            fd.readGenotype()
        mdpchoice = None
        strainlist = fd.strainlist
        fd.readData()
        if not self.openMysql():
            return
        isSampleCorr = 1
        isTissueCorr = 0
        TD_LR = HT.TD(colspan=2, height=200, width="100%", bgColor="#eeeeee", align="left", wrap="off")
        dataX = []
        dataY = []
        dataZ = []  # shortname
        fullTissueName = []
        xlabel = ""
        ylabel = ""
        if isSampleCorr:
            plotHeading = HT.Paragraph("Sample Correlation Scatterplot")
            plotHeading.__setattr__("class", "title")

            # XZ: retrieve trait 1 info, Y axis
            trait1_data = []  # trait 1 data
            trait1Url = ""
            try:
                Trait1 = webqtlTrait(db=self.database, name=self.ProbeSetID, cellid=self.CellID, cursor=self.cursor)
                Trait1.retrieveInfo()
                Trait1.retrieveData()
            except:
                heading = "Retrieve Data"
                detail = ["The database you just requested has not been established yet."]
                self.error(heading=heading, detail=detail)
                return
            trait1_data = Trait1.exportData(strainlist)
            trait1Url = Trait1.genHTML(dispFromDatabase=1)
            ylabel = "%s : %s" % (Trait1.db.shortname, Trait1.name)
            if Trait1.cellid:
                ylabel += " : " + Trait1.cellid

                # XZ, retrieve trait 2 info, X axis
            trait2_data = []  # trait 2 data
            trait2Url = ""
            try:
                Trait2 = webqtlTrait(db=self.database2, name=self.ProbeSetID2, cellid=self.CellID2, cursor=self.cursor)
                Trait2.retrieveInfo()
                Trait2.retrieveData()
            except:
                heading = "Retrieve Data"
                detail = ["The database you just requested has not been established yet."]
                self.error(heading=heading, detail=detail)
                return
            trait2_data = Trait2.exportData(strainlist)
            trait2Url = Trait2.genHTML(dispFromDatabase=1)
            xlabel = "%s : %s" % (Trait2.db.shortname, Trait2.name)
            if Trait2.cellid:
                xlabel += " : " + Trait2.cellid

            for strain in Trait1.data.keys():
                if Trait2.data.has_key(strain):
                    dataX.append(Trait2.data[strain].val)
                    dataY.append(Trait1.data[strain].val)
                    if self.showstrains:
                        dataZ.append(webqtlUtil.genShortStrainName(RISet=fd.RISet, input_strainName=strain))

                # XZ: We have gotten all data for both traits.
        if len(dataX) >= self.corrMinInformative:

            if self.rankOrder == 0:
                rankPrimary = 0
                rankSecondary = 1
            else:
                rankPrimary = 1
                rankSecondary = 0

            lineColor = self.setLineColor()
            symbolColor = self.setSymbolColor()
            idColor = self.setIdColor()

            c = pid.PILCanvas(size=(self.plotSize, self.plotSize * 0.90))
            data_coordinate = Plot.plotXY(
                canvas=c,
                dataX=dataX,
                dataY=dataY,
                rank=rankPrimary,
                dataLabel=dataZ,
                labelColor=pid.black,
                lineSize=self.lineSize,
                lineColor=lineColor,
                idColor=idColor,
                idFont=self.idFont,
                idSize=self.idSize,
                symbolColor=symbolColor,
                symbolType=self.symbol,
                filled=self.filled,
                symbolSize=self.symbolSize,
                XLabel=xlabel,
                connectdot=0,
                YLabel=ylabel,
                title="",
                fitcurve=self.showline,
                displayR=1,
                offset=(90, self.plotSize / 20, self.plotSize / 10, 90),
                showLabel=self.showIdentifiers,
            )

            if rankPrimary == 1:
                dataXlabel, dataYlabel = webqtlUtil.calRank(xVals=dataX, yVals=dataY, N=len(dataX))
            else:
                dataXlabel, dataYlabel = dataX, dataY

            gifmap1 = HT.Map(name="CorrelationPlotImageMap1")

            for i, item in enumerate(data_coordinate):
                one_rect_coordinate = "%d, %d, %d, %d" % (item[0] - 5, item[1] - 5, item[0] + 5, item[1] + 5)
                if isTissueCorr:
                    one_rect_title = "%s (%s, %s)" % (fullTissueName[i], dataXlabel[i], dataYlabel[i])
                else:
                    one_rect_title = "%s (%s, %s)" % (dataZ[i], dataXlabel[i], dataYlabel[i])
                gifmap1.areas.append(HT.Area(shape="rect", coords=one_rect_coordinate, title=one_rect_title))

            filename = webqtlUtil.genRandStr("XY_")
            c.save(webqtlConfig.IMGDIR + filename, format="gif")
            img1 = HT.Image("/image/" + filename + ".gif", border=0, usemap="#CorrelationPlotImageMap1")

            mainForm_1 = HT.Form(
                cgi=os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE),
                enctype="multipart/form-data",
                name="showDatabase",
                submit=HT.Input(type="hidden"),
            )
            hddn = {
                "FormID": "showDatabase",
                "ProbeSetID": "_",
                "database": "_",
                "CellID": "_",
                "RISet": fd.RISet,
                "ProbeSetID2": "_",
                "database2": "_",
                "CellID2": "_",
                "allstrainlist": string.join(fd.strainlist, " "),
                "traitList": fd.formdata.getvalue("traitList"),
            }
            if fd.incparentsf1:
                hddn["incparentsf1"] = "ON"
            for key in hddn.keys():
                mainForm_1.append(HT.Input(name=key, value=hddn[key], type="hidden"))

            if isSampleCorr:
                mainForm_1.append(
                    HT.P(),
                    HT.Blockquote(
                        HT.Strong("X axis:"),
                        HT.Blockquote(trait2Url),
                        HT.Strong("Y axis:"),
                        HT.Blockquote(trait1Url),
                        style="width: %spx;" % self.plotSize,
                        wrap="hard",
                    ),
                )

            graphForm = HT.Form(
                cgi=os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE),
                name="MDP_Form",
                submit=HT.Input(type="hidden"),
            )
            graph_hddn = self.setHiddenParameters(fd, rankPrimary)
            webqtlUtil.exportData(
                graph_hddn, fd.allTraitData
            )  # XZ: This is necessary to replot with different groups of strains

            for key in graph_hddn.keys():
                graphForm.append(HT.Input(name=key, value=graph_hddn[key], type="hidden"))

            options = self.createOptionsMenu(fd, mdpchoice)

            if self.showOptions == "0":
                showOptionsButton = HT.Input(
                    type="button",
                    name="optionsButton",
                    value="Hide Options",
                    onClick="showHideOptions();",
                    Class="button",
                )
            else:
                showOptionsButton = HT.Input(
                    type="button",
                    name="optionsButton",
                    value="Show Options",
                    onClick="showHideOptions();",
                    Class="button",
                )

                # updated by NL: 12-07-2011 add variables for tissue abbreviation page
            if isTissueCorr:
                graphForm.append(HT.Input(name="shortTissueName", value="", type="hidden"))
                graphForm.append(HT.Input(name="fullTissueName", value="", type="hidden"))
                shortTissueNameStr = string.join(dataZ, ",")
                fullTissueNameStr = string.join(fullTissueName, ",")

                tissueAbbrButton = HT.Input(
                    type="button",
                    name="tissueAbbrButton",
                    value="Show Abbreviations",
                    onClick="showTissueAbbr('MDP_Form','%s','%s')" % (shortTissueNameStr, fullTissueNameStr),
                    Class="button",
                )
                graphForm.append(showOptionsButton, "&nbsp;&nbsp;&nbsp;&nbsp;", tissueAbbrButton, HT.BR(), HT.BR())
            else:
                graphForm.append(showOptionsButton, HT.BR(), HT.BR())

            graphForm.append(options, HT.BR())
            graphForm.append(HT.HR(), HT.BR(), HT.P())

            TD_LR.append(plotHeading, HT.BR(), graphForm, HT.BR(), gifmap1, HT.P(), img1, HT.P(), mainForm_1)
            TD_LR.append(HT.BR(), HT.HR(color="grey", size=5, width="100%"))

            c = pid.PILCanvas(size=(self.plotSize, self.plotSize * 0.90))
            data_coordinate = Plot.plotXY(
                canvas=c,
                dataX=dataX,
                dataY=dataY,
                rank=rankSecondary,
                dataLabel=dataZ,
                labelColor=pid.black,
                lineColor=lineColor,
                lineSize=self.lineSize,
                idColor=idColor,
                idFont=self.idFont,
                idSize=self.idSize,
                symbolColor=symbolColor,
                symbolType=self.symbol,
                filled=self.filled,
                symbolSize=self.symbolSize,
                XLabel=xlabel,
                connectdot=0,
                YLabel=ylabel,
                title="",
                fitcurve=self.showline,
                displayR=1,
                offset=(90, self.plotSize / 20, self.plotSize / 10, 90),
                showLabel=self.showIdentifiers,
            )

            if rankSecondary == 1:
                dataXlabel, dataYlabel = webqtlUtil.calRank(xVals=dataX, yVals=dataY, N=len(dataX))
            else:
                dataXlabel, dataYlabel = dataX, dataY

            gifmap2 = HT.Map(name="CorrelationPlotImageMap2")

            for i, item in enumerate(data_coordinate):
                one_rect_coordinate = "%d, %d, %d, %d" % (item[0] - 6, item[1] - 6, item[0] + 6, item[1] + 6)
                if isTissueCorr:
                    one_rect_title = "%s (%s, %s)" % (fullTissueName[i], dataXlabel[i], dataYlabel[i])
                else:
                    one_rect_title = "%s (%s, %s)" % (dataZ[i], dataXlabel[i], dataYlabel[i])

                gifmap2.areas.append(HT.Area(shape="rect", coords=one_rect_coordinate, title=one_rect_title))

            filename = webqtlUtil.genRandStr("XY_")
            c.save(webqtlConfig.IMGDIR + filename, format="gif")
            img2 = HT.Image("/image/" + filename + ".gif", border=0, usemap="#CorrelationPlotImageMap2")

            mainForm_2 = HT.Form(
                cgi=os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE),
                enctype="multipart/form-data",
                name="showDatabase2",
                submit=HT.Input(type="hidden"),
            )
            hddn = {
                "FormID": "showDatabase2",
                "ProbeSetID": "_",
                "database": "_",
                "CellID": "_",
                "RISet": fd.RISet,
                "ProbeSetID2": "_",
                "database2": "_",
                "CellID2": "_",
                "allstrainlist": string.join(fd.strainlist, " "),
                "traitList": fd.formdata.getvalue("traitList"),
            }
            if fd.incparentsf1:
                hddn["incparentsf1"] = "ON"
            for key in hddn.keys():
                mainForm_2.append(HT.Input(name=key, value=hddn[key], type="hidden"))

            if isSampleCorr:
                mainForm_2.append(
                    HT.P(),
                    HT.Blockquote(
                        HT.Strong("X axis:"),
                        HT.Blockquote(trait2Url),
                        HT.Strong("Y axis:"),
                        HT.Blockquote(trait1Url),
                        style="width:%spx;" % self.plotSize,
                    ),
                )

            TD_LR.append(HT.BR(), HT.P())
            TD_LR.append("\n", gifmap2, HT.P(), HT.P(), img2, HT.P(), mainForm_2)

            self.dict["body"] = str(TD_LR)
        else:
            heading = "Correlation Plot"
            detail = [
                "Fewer than %d strain data were entered for %s data set. No statitical analysis has been attempted."
                % (self.corrMinInformative, fd.RISet)
            ]
            self.error(heading=heading, detail=detail)
            return
	def dispFormPage(self, fd):
		###specical care, temporary trait data
		fullname =  fd.formdata.getvalue('fullname')
		if fullname:
			thisTrait = webqtlTrait(fullname=fullname, data= fd.allTraitData, cursor=self.cursor)
			thisTrait.retrieveInfo()
			PhenotypeValue = thisTrait.description
		else:
			thisTrait = webqtlTrait(data= fd.allTraitData)
			PhenotypeValue = thisTrait.identification
			
		self.dict['title'] = 'Add to Published Database'
		
		form = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), name='dataInput',submit=HT.Input(type='hidden'))

		recordTable = HT.TableLite(border=0, align="left")
		recordInfoContainer = HT.Div(align="left")
		recordDataContainer = HT.Div(align="left")
		title1 = HT.Paragraph("&nbsp;Trait Information:", align="left", Class="subtitle")
		title2 = HT.Paragraph("&nbsp;Trait Data:", align="left", Class="subtitle")
		addButton = HT.Input(type='submit',name='submit', value='Submit Trait',Class="button")
		resetButton = HT.Input(type='reset',Class="button")

		recordInfoTable = HT.TableLite(border=0, cellspacing=1, cellpadding=5,align="left")
		for field in self.db.disfield:
			fieldValue = ""

			if field == 'comments':
				continue
			elif field == 'name' or field == 'sequence' or field == 'original_description' or field == 'submitter' or field == 'authorized_users':
				form.append(HT.Input(type="hidden",name=field,value=fieldValue))
				continue
			elif field == 'post_publication_description':
				inputBox = HT.Textarea(name=field, cols=60, rows=6,text=PhenotypeValue)
			elif field == 'abstract' or field == 'pre_publication_description' or field == 'owner':
				inputBox = HT.Textarea(name=field, cols=60, rows=6,text=fieldValue)
			elif field == 'post_publication_abbreviation' or field == 'pre_publication_abbreviation':
				inputBox = HT.Input(type="text",name=field,size=60, maxlength=30,value=fieldValue)
			else:
				inputBox = HT.Input(type="text",name=field,size=60, maxlength=255,value=fieldValue)
			if field in ('post_publication_description', 'authors', 'title', 'year'):
				requiredSign = HT.Span('*', Class="cr")
			else:
				requiredSign = ''
				
			recordInfoTable.append(HT.TR(
				HT.TD(requiredSign, "%s :" % webqtlUtil.formatField(field), Class="fs12 fwb ff1", valign="top",align="right"),
				HT.TD(width=20),HT.TD(inputBox)))

			if field == 'pubmed_id':
				recordInfoTable.append(HT.TR(
				HT.TD(), HT.TD(width=20), 
				HT.TD("Do not enter PubMed_ID if this trait has not been Published.", 
					HT.BR(), "If the PubMed_ID you entered is alreday stored in our database, ",
					HT.BR(), "all the following fields except Post Publication Description will be ignored.",
					HT.BR(), "Do not enter any non-digit character in this field.", Class="fs11 cr")
				))
			if field == 'pre_publication_description':
				recordInfoTable.append(HT.TR(
				HT.TD(), HT.TD(width=20),
				HT.TD("If the PubMed ID is entered, the Post Publication Description will be shown to all",
					HT.BR(), " users. If there is no PubMed ID, and the Pre Publication Description is entered,",
					HT.BR(), "only you and authorized users can see the Post Publication Description.", Class="fs11 cr")
				))
			if field == 'owner':
				recordInfoTable.append(HT.TR(
				HT.TD(), HT.TD(width=20),
				HT.TD("Please provide detailed owner contact information including full name, title,",
                                        HT.BR(), " institution, address, email etc", Class="fs11 cr")
				))

		recordInfoTable.append(HT.TR(HT.TD(HT.Span('*', Class="cr"), " Required field", align="center", colspan=3)))
		recordInfoContainer.append(addButton, resetButton, HT.P(), title1, HT.BR(), recordInfoTable)

		recordDataTable = HT.TableLite(border=0, width = "90%",cellspacing=2, cellpadding=2)
		recordDataTable.append(HT.TR(HT.TD('Strain Name',Class="fs12 ffl fwb",align="left"), 
			HT.TD('Trait Data',Class="fs12 ffl fwb",align="right"), 
			HT.TD('SE',Class="fs12 ffl fwb",align="right"),
			HT.TD('N Per Strain',Class="fs12 ffl fwb",align="right"),
			HT.TD('&nbsp'*8,Class="fs12 ffl fwb",align="center"),
			HT.TD('Strain Name',Class="fs12 ffl fwb",align="left"), 
			HT.TD('Trait Data',Class="fs12 ffl fwb",align="right"), 
			HT.TD('SE',Class="fs12 ffl fwb",align="right"),
			HT.TD('N Per Strain',Class="fs12 ffl fwb",align="right")))
		
		tempTR = HT.TR(align="right")
		for i, strainName in enumerate(fd.strainlist):
			if thisTrait.data.has_key(strainName):
				tdata = thisTrait.data[strainName]
				traitVal, traitVar, traitNP = tdata.val, tdata.var, tdata.N
			else:
				traitVal, traitVar, traitNP = None, None, None
			
			if traitVal != None:	
				traitVal = "%2.3f" % traitVal
			else:
				traitVal = 'x'
			if traitVar != None:	
				traitVar = "%2.3f" % traitVar
			else:
				traitVar = 'x'
			if traitNP != None:	
				traitNP = "%d" % traitNP
			else:
				traitNP = 'x'

			tempTR.append(HT.TD(HT.Paragraph(strainName), width="120px", align='left'), \
				HT.TD(HT.Input(name=fd.strainlist[i], size=8, maxlength=8, value=traitVal, align="right"), width="100px", align='right'),
				HT.TD(HT.Input(name='V'+fd.strainlist[i], size=8, maxlength=8, value=traitVar, align="right"), width="100px", align='right'),
				HT.TD(HT.Input(name='N'+fd.strainlist[i], size=8, maxlength=8, value=traitNP, align="right"), width="120px", align='right'),
				HT.TD('', align='center'))
			if i % 2:
				recordDataTable.append(tempTR)
				tempTR = HT.TR(align="Center")
		
		if (i+1) % 2:
			tempTR.append(HT.TD(''))
			tempTR.append(HT.TD(''))
			tempTR.append(HT.TD(''))
			recordDataTable.append(tempTR)

		recordDataContainer.append(title2, HT.BR(), recordDataTable, HT.P(), addButton, resetButton, HT.P())

		recordTable.append(HT.TR(HT.TD(recordInfoContainer)), HT.TR(HT.TD(recordDataContainer)))

		"""
		"""
		
		hddn = {'database':self.db.name, 'curStatus':'insertCheck', 'FormID':'dataEditing', 'submitID':'addPublish', 'RISet':fd.RISet}
		for key in hddn.keys():
			form.append(HT.Input(name=key, value=hddn[key], type='hidden'))

		
		#############################
		TD_LR = HT.TD(valign="top",colspan=2,bgcolor="#ffffff")
		
		mainTitle = HT.Paragraph("Add Trait to Published Database", Class="title")
		
		form.append(recordTable)
		
		TD_LR.append(mainTitle, form)
		
		self.dict['body'] = TD_LR
    def __init__(self, fd, traitInfos=None):
        self.fd = fd

        # This sets self.cursor
        assert self.openMysql(), "No database"

        # When is traitInfos used?
        if traitInfos:
            database, ProbeSetID, CellID = traitInfos
        else:
            print("fd is:", fd)
            database = fd['database']
            ProbeSetID = fd['ProbeSetID']

            CellID = fd.get('CellID')

        thisTrait = webqtlTrait(db=database,
                                name=ProbeSetID,
                                cellid=CellID,
                                cursor=self.cursor)

        if thisTrait.db.type == "ProbeSet":

            self.cursor.execute(
                '''SELECT Id, Name, FullName, confidentiality, AuthorisedUsers
                                    FROM ProbeSetFreeze WHERE Name = "%s"''' %
                database)

            indId, indName, indFullName, confidential, AuthorisedUsers = self.cursor.fetchall(
            )[0]

            if confidential == 1:
                access_to_confidential_dataset = 0

                #for the dataset that confidentiality is 1
                #1. 'admin' and 'root' can see all of the dataset
                #2. 'user' can see the dataset that AuthorisedUsers contains his id(stored in the Id field of User table)
                if webqtlConfig.USERDICT[
                        self.privilege] > webqtlConfig.USERDICT['user']:
                    access_to_confidential_dataset = 1
                else:
                    AuthorisedUsersList = AuthorisedUsers.split(',')
                    if AuthorisedUsersList.__contains__(self.userName):
                        access_to_confidential_dataset = 1

                if not access_to_confidential_dataset:
                    #Error, Confidential Database
                    heading = "Show Database"
                    detail = [
                        "The %s database you selected is not open to the public \
                    at this time, please go back and select other database." %
                        indFullName
                    ]
                    self.error(heading=heading,
                               detail=detail,
                               error="Confidential Database")
                    return
        print("environ:", request.environ)

        # Becuase of proxying remote_addr is probably localhost, so we first try for
        # HTTP_X_FORWARDED_FOR
        user_ip = request.environ.get(
            'HTTP_X_FORWARDED_FOR'
        ) or request.remote_addr  # in old app was fd.remote_ip
        print("user_ip is:", user_ip)
        query = "SELECT count(id) FROM AccessLog WHERE ip_address = %s and \
                        UNIX_TIMESTAMP()-UNIX_TIMESTAMP(accesstime)<86400"

        self.cursor.execute(query, user_ip)
        daycount = self.cursor.fetchall()
        if daycount:
            daycount = daycount[0][0]
            if daycount > webqtlConfig.DAILYMAXIMUM:
                heading = "Retrieve Data"
                detail = [
                    'For security reasons, the maximum access to a database is \
                %d times per day per ip address. You have reached the limit, please \
                try it again tomorrow.' % webqtlConfig.DAILYMAXIMUM
                ]
                self.error(heading=heading, detail=detail)
                return

        if thisTrait.db.type != 'ProbeSet' and thisTrait.cellid:
            heading = "Retrieve Data"
            detail = ['The Record you requested doesn\'t exist!']
            self.error(heading=heading, detail=detail)
            return

        ##identification, etc.
        fd.identification = '%s : %s' % (thisTrait.db.shortname, ProbeSetID)
        thisTrait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\
                &ProbeSetID=%s&RISet=%s&parentsf1=on' % (database, ProbeSetID,
                                                         fd['RISet'])

        if CellID:
            fd.identification = '%s/%s' % (fd.identification, CellID)
            thisTrait.returnURL = '%s&CellID=%s' % (thisTrait.returnURL,
                                                    CellID)

        thisTrait.retrieveInfo()
        thisTrait.retrieveData()
        self.updMysql()
        self.cursor.execute(
            "insert into AccessLog(accesstime,ip_address) values(Now(),%s)",
            user_ip)
        self.openMysql()

        ##read genotype file
        fd.RISet = thisTrait.riset
        fd.readGenotype()

        #if webqtlUtil.ListNotNull(map(lambda x:x.var, thisTrait.data.values())):
        if any([x.variance for x in thisTrait.data.values()]):
            fd.display_variance = True
            fd.formID = 'varianceChoice'

        DataEditingPage.__init__(self, fd, thisTrait)
    def __init__(self, fd):

        templatePage.__init__(self, fd)

        if not fd.genotype:
            fd.readGenotype()
            strainlist2 = fd.strainlist

        if fd.allstrainlist:
            strainlist2 = fd.allstrainlist

        fd.readData(strainlist2)

        specialStrains = []
        setStrains = []
        for item in strainlist2:
            if item not in fd.strainlist and item.find('F1') < 0:
                specialStrains.append(item)
            else:
                setStrains.append(item)
        specialStrains.sort()
        #So called MDP Panel
        if specialStrains:
            specialStrains = fd.f1list+fd.parlist+specialStrains

        self.plotType = fd.formdata.getvalue('ptype', '0')
        plotStrains = strainlist2
        if specialStrains:
            if self.plotType == '1':
                plotStrains = setStrains
            if self.plotType == '2':
                plotStrains = specialStrains

        self.dict['title'] = 'Basic Statistics'
        if not self.openMysql():
            return

        self.showstrains = 1
        self.identification = "unnamed trait"

        self.fullname = fd.formdata.getvalue('fullname', '')
        if self.fullname:
            self.Trait = webqtlTrait(fullname=self.fullname, cursor=self.cursor)
            self.Trait.retrieveInfo()
        else:
            self.Trait = None

        if fd.identification:
            self.identification = fd.identification
            self.dict['title'] = self.identification + ' / '+self.dict['title']
        TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee')

        ##should not display Variance, but cannot convert Variance to SE
        #print plotStrains, fd.allTraitData.keys()
        if len(fd.allTraitData) > 0:
            vals=[]
            InformData = []
            for _strain in plotStrains:
                if fd.allTraitData.has_key(_strain):
                    _val, _var = fd.allTraitData[_strain].val, fd.allTraitData[_strain].var
                    if _val != None:
                        vals.append([_strain, _val, _var])
                        InformData.append(_val)

            if len(vals) >= self.plotMinInformative:
                supertable2 = HT.TableLite(border=0, cellspacing=0, cellpadding=5,width="800")

                staIntro1 = HT.Paragraph("The table and plots below list the basic statistical analysis result of trait",HT.Strong(" %s" % self.identification))

                #####
                #anova
                #####
                traitmean, traitmedian, traitvar, traitstdev, traitsem, N = reaper.anova(InformData)
                TDStatis = HT.TD(width="360", valign="top")
                tbl2 = HT.TableLite(cellpadding=5, cellspacing=0, Class="collap")
                dataXZ = vals[:]
                dataXZ.sort(self.cmpValue)
                tbl2.append(HT.TR(HT.TD("Statistic",align="center", Class="fs14 fwb ffl b1 cw cbrb", width = 200),
                                HT.TD("Value", align="center", Class="fs14 fwb ffl b1 cw cbrb", width = 140)))
                tbl2.append(HT.TR(HT.TD("N of Cases",align="center", Class="fs13 b1 cbw c222"),
                                HT.TD(N,nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                tbl2.append(HT.TR(HT.TD("Mean",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                                HT.TD("%2.3f" % traitmean,nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                tbl2.append(HT.TR(HT.TD("Median",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                                HT.TD("%2.3f" % traitmedian,nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                #tbl2.append(HT.TR(HT.TD("Variance",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                #               HT.TD("%2.3f" % traitvar,nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                tbl2.append(HT.TR(HT.TD("SEM",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                                HT.TD("%2.3f" % traitsem,nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                tbl2.append(HT.TR(HT.TD("SD",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                                HT.TD("%2.3f" % traitstdev,nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                tbl2.append(HT.TR(HT.TD("Minimum",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                                HT.TD("%s" % dataXZ[0][1],nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                tbl2.append(HT.TR(HT.TD("Maximum",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                                HT.TD("%s" % dataXZ[-1][1],nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                if self.Trait and self.Trait.db.type == 'ProbeSet':
                    #IRQuest = HT.Href(text="Interquartile Range", url=webqtlConfig.glossaryfile +"#Interquartile",target="_blank", Class="fs14")
                    #IRQuest.append(HT.BR())
                    #IRQuest.append(" (fold difference)")
                    tbl2.append(HT.TR(HT.TD("Range (log2)",align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                            HT.TD("%2.3f" % (dataXZ[-1][1]-dataXZ[0][1]),nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                    tbl2.append(HT.TR(HT.TD(HT.Span("Range (fold)"),align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                            HT.TD("%2.2f" % pow(2.0,(dataXZ[-1][1]-dataXZ[0][1])), nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                    tbl2.append(HT.TR(HT.TD(HT.Span("Quartile Range",HT.BR()," (fold difference)"),align="center", Class="fs13 b1 cbw c222",nowrap="yes"),
                            HT.TD("%2.2f" % pow(2.0,(dataXZ[int((N-1)*3.0/4.0)][1]-dataXZ[int((N-1)/4.0)][1])), nowrap="yes",align="center", Class="fs13 b1 cbw c222")))

                    # (Lei Yan)
                    # 2008/12/19
                    self.Trait.retrieveData()
                    #XZ, 04/01/2009: don't try to get H2 value for probe.
                    if self.Trait.cellid:
                        pass
                    else:
                        self.cursor.execute("SELECT DataId, h2 from ProbeSetXRef WHERE DataId = %d" % self.Trait.mysqlid)
                        dataid, heritability = self.cursor.fetchone()
                        if heritability:
                            tbl2.append(HT.TR(HT.TD(HT.Span("Heritability"),align="center", Class="fs13 b1 cbw c222",nowrap="yes"),HT.TD("%s" % heritability, nowrap="yes",align="center", Class="fs13 b1 cbw c222")))
                        else:
                            tbl2.append(HT.TR(HT.TD(HT.Span("Heritability"),align="center", Class="fs13 b1 cbw c222",nowrap="yes"),HT.TD("NaN", nowrap="yes",align="center", Class="fs13 b1 cbw c222")))

                    # Lei Yan
                    # 2008/12/19

                TDStatis.append(tbl2)

                plotHeight = 220
                plotWidth = 120
                xLeftOffset = 60
                xRightOffset = 25
                yTopOffset = 20
                yBottomOffset = 53

                canvasHeight = plotHeight + yTopOffset + yBottomOffset
                canvasWidth = plotWidth + xLeftOffset + xRightOffset
                canvas = pid.PILCanvas(size=(canvasWidth,canvasHeight))
                XXX = [('', InformData[:])]

                Plot.plotBoxPlot(canvas, XXX, offset=(xLeftOffset, xRightOffset, yTopOffset, yBottomOffset), XLabel= "Trait")
                filename= webqtlUtil.genRandStr("Box_")
                canvas.save(webqtlConfig.IMGDIR+filename, format='gif')
                img=HT.Image('/image/'+filename+'.gif',border=0)

                #supertable2.append(HT.TR(HT.TD(staIntro1, colspan=3 )))
                tb = HT.TableLite(border=0, cellspacing=0, cellpadding=0)
                tb.append(HT.TR(HT.TD(img, align="left", style="border: 1px solid #999999; padding:0px;")))
                supertable2.append(HT.TR(TDStatis, HT.TD(tb)))

                dataXZ = vals[:]
                tvals = []
                tnames = []
                tvars = []
                for i in range(len(dataXZ)):
                    tvals.append(dataXZ[i][1])
                    tnames.append(webqtlUtil.genShortStrainName(fd, dataXZ[i][0]))
                    tvars.append(dataXZ[i][2])
                nnStrain = len(tnames)

                sLabel = 1

                ###determine bar width and space width
                if nnStrain < 20:
                    sw = 4
                elif nnStrain < 40:
                    sw = 3
                else:
                    sw = 2

                ### 700 is the default plot width minus Xoffsets for 40 strains
                defaultWidth = 650
                if nnStrain > 40:
                    defaultWidth += (nnStrain-40)*10
                defaultOffset = 100
                bw = int(0.5+(defaultWidth - (nnStrain-1.0)*sw)/nnStrain)
                if bw < 10:
                    bw = 10

                plotWidth = (nnStrain-1)*sw + nnStrain*bw + defaultOffset
                plotHeight = 500
                #print [plotWidth, plotHeight, bw, sw, nnStrain]
                c = pid.PILCanvas(size=(plotWidth,plotHeight))
                Plot.plotBarText(c, tvals, tnames, variance=tvars, YLabel='Value', title='%s by Case (sorted by name)' % self.identification, sLabel = sLabel, barSpace = sw)

                filename= webqtlUtil.genRandStr("Bar_")
                c.save(webqtlConfig.IMGDIR+filename, format='gif')
                img0=HT.Image('/image/'+filename+'.gif',border=0)

                dataXZ = vals[:]
                dataXZ.sort(self.cmpValue)
                tvals = []
                tnames = []
                tvars = []
                for i in range(len(dataXZ)):
                    tvals.append(dataXZ[i][1])
                    tnames.append(webqtlUtil.genShortStrainName(fd, dataXZ[i][0]))
                    tvars.append(dataXZ[i][2])

                c = pid.PILCanvas(size=(plotWidth,plotHeight))
                Plot.plotBarText(c, tvals, tnames, variance=tvars, YLabel='Value', title='%s by Case (ranked)' % self.identification, sLabel = sLabel, barSpace = sw)

                filename= webqtlUtil.genRandStr("Bar_")
                c.save(webqtlConfig.IMGDIR+filename, format='gif')
                img1=HT.Image('/image/'+filename+'.gif',border=0)

                # Lei Yan
                # 05/18/2009
                # report

                title = HT.Paragraph('REPORT on the variation of Shh (or PCA Composite Trait XXXX) (sonic hedgehog) in the (insert Data set name) of (insert Species informal name, e.g., Mouse, Rat, Human, Barley, Arabidopsis)', Class="title")
                header = HT.Paragraph('''This report was generated by GeneNetwork on May 11, 2009, at 11.20 AM using the Basic Statistics module (v 1.0) and data from the Hippocampus Consortium M430v2 (Jun06) PDNN data set. For more details and updates on this data set please link to URL:get Basic Statistics''')
                hr = HT.HR()
                p1 = HT.Paragraph('''Trait values for Shh were taken from the (insert Database name, Hippocampus Consortium M430v2 (Jun06) PDNN). GeneNetwork contains data for NN (e.g., 99) cases. In general, data are averages for each case. A summary of mean, median, and the range of these data are provided in Table 1 and in the box plot (Figure 1). Data for individual cases are provided in Figure 2A and 2B, often with error bars (SEM). ''')
                p2 = HT.Paragraph('''Trait values for Shh range 5.1-fold: from a low of 8.2 (please round value) in 129S1/SvImJ to a high of 10.6 (please round value) in BXD9.  The interquartile range (the difference between values closest to the 25% and 75% levels) is a more modest 1.8-fold. The mean value is XX. ''')
                t1 = HT.Paragraph('''Table 1.  Summary of Shh data from the Hippocampus Consortium M430v2 (june06) PDNN data set''')
                f1 = HT.Paragraph('''Figure 1. ''')
                f1.append(HT.Href(text="Box plot", url="http://davidmlane.com/hyperstat/A37797.html", target="_blank", Class="fs14"))
                f1.append(HT.Text(''' of Shh data from the Hippocampus Consortium M430v2 (june06) PDNN data set'''))
                f2A = HT.Paragraph('''Figure 2A: Bar chart of Shh data ordered by case from the Hippocampus Consortium M430v2 (june06) PDNN data set''')
                f2B = HT.Paragraph('''Figure 2B: Bar chart of Shh values ordered by from the Hippocampus Consortium M430v2 (june06) PDNN data set''')
                TD_LR.append(HT.Blockquote(title, HT.P(), header, hr, p1, HT.P(), p2, HT.P(), supertable2, t1, f1, HT.P(), img0, f2A, HT.P(), img1, f2B))
                self.dict['body'] = str(TD_LR)
            else:
                heading = "Basic Statistics"
                detail = ['Fewer than %d case data were entered for %s data set. No statitical analysis has been attempted.' % (self.plotMinInformative, fd.RISet)]
                self.error(heading=heading,detail=detail)
                return
        else:
            heading = "Basic Statistics"
            detail = ['Empty data set, please check your data.']
            self.error(heading=heading,detail=detail)
            return
    def __init__(self, fd, traitInfos = None):
        self.fd = fd

        # This sets self.cursor
        assert self.openMysql(), "No database"

        # When is traitInfos used?
        if traitInfos:
            database, ProbeSetID, CellID = traitInfos
        else:
            print("fd is:", fd)
            database = fd['database']
            ProbeSetID = fd['ProbeSetID']

            CellID = fd.get('CellID')


        thisTrait = webqtlTrait(db=database, name=ProbeSetID, cellid=CellID, cursor=self.cursor)

        if thisTrait.db.type == "ProbeSet":

            self.cursor.execute('''SELECT Id, Name, FullName, confidentiality, AuthorisedUsers
                                    FROM ProbeSetFreeze WHERE Name = "%s"''' %  database)

            indId, indName, indFullName, confidential, AuthorisedUsers = self.cursor.fetchall()[0]

            if confidential == 1:
                access_to_confidential_dataset = 0

                #for the dataset that confidentiality is 1
                #1. 'admin' and 'root' can see all of the dataset
                #2. 'user' can see the dataset that AuthorisedUsers contains his id(stored in the Id field of User table)
                if webqtlConfig.USERDICT[self.privilege] > webqtlConfig.USERDICT['user']:
                    access_to_confidential_dataset = 1
                else:
                    AuthorisedUsersList=AuthorisedUsers.split(',')
                    if AuthorisedUsersList.__contains__(self.userName):
                        access_to_confidential_dataset = 1

                if not access_to_confidential_dataset:
                    #Error, Confidential Database
                    heading = "Show Database"
                    detail = ["The %s database you selected is not open to the public \
                    at this time, please go back and select other database." % indFullName]
                    self.error(heading=heading,detail=detail,error="Confidential Database")
                    return
        print("environ:", request.environ)

        # Becuase of proxying remote_addr is probably localhost, so we first try for
        # HTTP_X_FORWARDED_FOR
        user_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.remote_addr   # in old app was fd.remote_ip
        print("user_ip is:", user_ip)
        query = "SELECT count(id) FROM AccessLog WHERE ip_address = %s and \
                        UNIX_TIMESTAMP()-UNIX_TIMESTAMP(accesstime)<86400"
        self.cursor.execute(query,user_ip)
        daycount = self.cursor.fetchall()
        if daycount:
            daycount = daycount[0][0]
            if daycount > webqtlConfig.DAILYMAXIMUM:
                heading = "Retrieve Data"
                detail = ['For security reasons, the maximum access to a database is \
                %d times per day per ip address. You have reached the limit, please \
                try it again tomorrow.' % webqtlConfig.DAILYMAXIMUM]
                self.error(heading=heading,detail=detail)
                return


        if thisTrait.db.type != 'ProbeSet' and thisTrait.cellid:
            heading = "Retrieve Data"
            detail = ['The Record you requested doesn\'t exist!']
            self.error(heading=heading,detail=detail)
            return

        ##identification, etc.
        fd.identification = '%s : %s' % (thisTrait.db.shortname,ProbeSetID)
        thisTrait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\
                &ProbeSetID=%s&RISet=%s&parentsf1=on' %(database, ProbeSetID, fd['RISet'])

        if CellID:
            fd.identification = '%s/%s'%(fd.identification, CellID)
            thisTrait.returnURL = '%s&CellID=%s' % (thisTrait.returnURL, CellID)

        thisTrait.retrieveInfo()
        thisTrait.retrieveData()
        self.updMysql()
        self.cursor.execute("insert into AccessLog(accesstime,ip_address) values(Now(),%s)", user_ip)
        self.openMysql()


        ##read genotype file
        fd.RISet = thisTrait.riset
        fd.readGenotype()

        #if webqtlUtil.ListNotNull(map(lambda x:x.var, thisTrait.data.values())):
        if any([x.variance for x in thisTrait.data.values()]):
            fd.display_variance = True
            fd.formID = 'varianceChoice'

        DataEditingPage.__init__(self, fd, thisTrait)
	def __init__(self, fd, traitInfos = []):

		templatePage.__init__(self, fd)

		if not self.openMysql():
			return
			
		TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee')
		
		if traitInfos:
			database,ProbeSetID,CellID = traitInfos
		else:
			database = fd.formdata.getfirst('database')
			ProbeSetID = fd.formdata.getfirst('ProbeSetID')
			CellID = fd.formdata.getfirst('CellID')
		try:
			thisTrait = webqtlTrait(db=database, name=ProbeSetID, cellid= CellID, cursor=self.cursor)
		except:
			heading = "Trait Data and Analysis Form"
			detail = ["The trait isn't available currently."]
			self.error(heading=heading,detail=detail,error="Error")
			return

		if thisTrait.db.type == "ProbeSet":

			self.cursor.execute('''SELECT Id, Name, FullName, confidentiality, AuthorisedUsers 
						FROM ProbeSetFreeze WHERE Name = "%s"''' %  database)

			indId, indName, indFullName, confidential, AuthorisedUsers = self.cursor.fetchall()[0]

			if confidential == 1:
				access_to_confidential_dataset = 0

				#for the dataset that confidentiality is 1
				#1. 'admin' and 'root' can see all of the dataset
				#2. 'user' can see the dataset that AuthorisedUsers contains his id(stored in the Id field of User table)
				if webqtlConfig.USERDICT[self.privilege] > webqtlConfig.USERDICT['user']:
					access_to_confidential_dataset = 1
				else:
					AuthorisedUsersList=AuthorisedUsers.split(',')
					if AuthorisedUsersList.__contains__(self.userName):
						access_to_confidential_dataset = 1

				if not access_to_confidential_dataset:
					#Error, Confidential Database
					heading = "Show Database"
					detail = ["The %s database you selected is not open to the public \
					at this time, please go back and select other database." % indFullName]
					self.error(heading=heading,detail=detail,error="Confidential Database")
					return
					
		user_ip = fd.remote_ip
		query = "SELECT count(id) FROM AccessLog WHERE ip_address = %s and \
				UNIX_TIMESTAMP()-UNIX_TIMESTAMP(accesstime)<86400"
		self.cursor.execute(query,user_ip)
		daycount = self.cursor.fetchall()
		if daycount:
			daycount = daycount[0][0]
			if daycount > webqtlConfig.DAILYMAXIMUM:
				heading = "Retrieve Data"
				detail = ['For security reasons, the maximum access to a database is \
				%d times per day per ip address. You have reached the limit, please \
				try it again tomorrow.' % webqtlConfig.DAILYMAXIMUM]
				self.error(heading=heading,detail=detail)
				return
			else:
				pass
		else:
			pass
		
		if thisTrait.db.type != 'ProbeSet' and thisTrait.cellid:
				heading = "Retrieve Data"
				detail = ['The Record you requested doesn\'t exist!']
				self.error(heading=heading,detail=detail)
				return

		#XZ: Aug 23, 2010: I commented out this block because this feature is not used anymore		
		# check if animal information are available
		"""
		self.cursor.execute('''
						SELECT 
							SampleXRef.ProbeFreezeId 
						FROM 
							SampleXRef, ProbeSetFreeze 
						WHERE 
							SampleXRef.ProbeFreezeId = ProbeSetFreeze.ProbeFreezeId AND
							ProbeSetFreeze.Name = "%s"
					 ''' % thisTrait.db.name)
		
		sampleId = self.cursor.fetchall()
		if sampleId:
			thisTrait.strainInfo = 1
		else:
			thisTrait.strainInfo = None
		"""

		##identification, etc.
		fd.identification = '%s : %s'%(thisTrait.db.shortname,ProbeSetID)
		thisTrait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\
			&ProbeSetID=%s&RISet=%s&parentsf1=on' %(database,ProbeSetID,fd.RISet)
		
		if CellID:
			fd.identification = '%s/%s'%(fd.identification, CellID)
			thisTrait.returnURL = '%s&CellID=%s' % (thisTrait.returnURL, CellID)
		
		#retrieve trait information		
		try:
			thisTrait.retrieveInfo()
			thisTrait.retrieveData()
			self.updMysql()
			self.cursor.execute("insert into AccessLog(accesstime,ip_address) values(Now(),%s)" ,user_ip)
			self.openMysql()
		except:
			heading = "Retrieve Data"
			detail = ["The information you requested is not avaiable at this time."]
			self.error(heading=heading,detail=detail)
			return
			
		##read genotype file
		fd.RISet = thisTrait.riset
		fd.readGenotype()
		
		if webqtlUtil.ListNotNull(map(lambda x:x.var, thisTrait.data.values())):
			fd.displayVariance = 1
			fd.varianceDispName = 'SE'
			fd.formID = 'varianceChoice'
		
		self.dict['body']= thisTrait
		DataEditingPage.__init__(self, fd, thisTrait)
		self.dict['title'] = '%s: Display Trait' % fd.identification
Example #30
0
    def __init__(self, fd):

        templatePage.__init__(self, fd)

        if not self.openMysql():
            return

        searchResult = fd.formdata.getvalue("searchResult")

        if not searchResult:
            heading = "Partial Correlation"
            detail = ["You need to select at least three traits in order to calculate partial correlation."]
            self.error(heading=heading, detail=detail)
            return

        ## Adds the Trait instance for each trait name from the collection
        traits = []

        for item in searchResult:
            traits.append(webqtlTrait(fullname=item, cursor=self.cursor))

        RISet = fd.formdata.getvalue("RISet")
        species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=RISet)

        # XZ: HTML part
        TD_LR = HT.TD(colspan=2, height=200, width="100%", bgColor="#eeeeee")
        TD_LR.append(
            "Please select one primary trait, one to three control traits, and at least one target trait.", HT.P()
        )

        mainFormName = "showDatabase"
        mainForm = HT.Form(
            cgi=os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE),
            name=mainFormName,
            submit=HT.Input(type="hidden"),
        )

        # XZ: Add hidden form values
        hddn = {
            "FormID": "calPartialCorrTrait",
            "database": "",
            "ProbeSetID": "",
            "CellID": "",  # XZ: These four parameters are required by javascript function showDatabase2.
            "controlTraits": "",
            "primaryTrait": "",
            "targetTraits": "",
            "pcMethod": "",
            "RISet": RISet,
        }

        for key in hddn.keys():
            mainForm.append(HT.Input(type="hidden", name=key, value=hddn[key]))

        radioNames = []

        for thisTrait in traits:
            oneRadioName = thisTrait.getName()
            radioNames.append(oneRadioName)

        radioNamesString = ",".join(radioNames)

        # Creates the image href that runs the javascript setting all traits as target or ignored
        setAllTarget = HT.Href(
            url="#redirect",
            onClick="setAllAsTarget(document.getElementsByName('showDatabase')[0], '%s');" % radioNamesString,
        )
        setAllTargetImg = HT.Image("/images/select_all.gif", alt="Select All", title="Select All", style="border:none;")
        setAllTarget.append(setAllTargetImg)
        setAllIgnore = HT.Href(
            url="#redirect",
            onClick="setAllAsIgnore(document.getElementsByName('showDatabase')[0], '%s');" % radioNamesString,
        )
        setAllIgnoreImg = HT.Image("/images/select_all.gif", alt="Select All", title="Select All", style="border:none;")
        setAllIgnore.append(setAllIgnoreImg)

        tblobj = {}
        tblobj["header"] = self.getCollectionTableHeader()

        sortby = self.getSortByValue()

        tblobj["body"] = self.getCollectionTableBody(traitList=traits, formName=mainFormName, species=species)

        filename = webqtlUtil.genRandStr("Search_")

        objfile = open("%s.obj" % (webqtlConfig.TMPDIR + filename), "wb")
        cPickle.dump(tblobj, objfile)
        objfile.close()

        div = HT.Div(
            webqtlUtil.genTableObj(tblobj=tblobj, file=filename, sortby=sortby, tableID="sortable", addIndex="1"),
            Id="sortable",
        )

        mainForm.append(div)

        # XZ: Add button
        radioNamesString = ",".join(radioNames)
        jsCommand_1 = "validateTrait(this.form, '" + radioNamesString + "', 0, 1);"
        jsCommand_2 = "validateTrait(this.form, '" + radioNamesString + "', 0, 2);"
        partialCorrTraitButton_1 = HT.Input(
            type="button",
            name="submitPartialCorrTrait_1",
            value="Pearson's r",
            onClick="%s" % jsCommand_1,
            Class="button",
        )
        partialCorrTraitButton_2 = HT.Input(
            type="button",
            name="submitPartialCorrTrait_2",
            value="Spearman's rho",
            onClick="%s" % jsCommand_2,
            Class="button",
        )
        mainForm.append(
            HT.BR(),
            "Compute partial correlation for target selected above:",
            HT.BR(),
            partialCorrTraitButton_1,
            partialCorrTraitButton_2,
            HT.BR(),
            HT.BR(),
            HT.HR(color="gray", size=3),
        )

        jsCommand = "validateTrait(this.form, '" + radioNamesString + "', 1);"
        partialCorrDBButton = HT.Input(
            type="button", name="submitPartialCorrDB", value="Calculate", onClick="%s" % jsCommand, Class="button"
        )

        methodText = HT.Span("Calculate:", Class="ffl fwb fs12")

        methodMenu = HT.Select(name="method")
        methodMenu.append(("Genetic Correlation, Pearson's r", "1"))
        methodMenu.append(("Genetic Correlation, Spearman's rho", "2"))
        methodMenu.append(("SGO Literature Correlation", "3"))
        methodMenu.append(("Tissue Correlation, Pearson's r", "4"))
        methodMenu.append(("Tissue Correlation, Spearman's rho", "5"))

        databaseText = HT.Span("Choose Database:", Class="ffl fwb fs12")
        databaseMenu = HT.Select(name="database2")

        nmenu = 0

        self.cursor.execute(
            'SELECT PublishFreeze.FullName,PublishFreeze.Name FROM \
                                PublishFreeze,InbredSet WHERE PublishFreeze.InbredSetId = InbredSet.Id \
                                and InbredSet.Name = "%s" and PublishFreeze.public > %d'
            % (RISet, webqtlConfig.PUBLICTHRESH)
        )
        for item in self.cursor.fetchall():
            databaseMenu.append(item)
            nmenu += 1

        self.cursor.execute(
            'SELECT GenoFreeze.FullName,GenoFreeze.Name FROM GenoFreeze,\
                                InbredSet WHERE GenoFreeze.InbredSetId = InbredSet.Id and InbredSet.Name = \
                                "%s" and GenoFreeze.public > %d'
            % (RISet, webqtlConfig.PUBLICTHRESH)
        )
        for item in self.cursor.fetchall():
            databaseMenu.append(item)
            nmenu += 1

        # 03/09/2009: Xiaodong changed the SQL query to order by Name as requested by Rob.
        self.cursor.execute("SELECT Id, Name FROM Tissue order by Name")
        for item in self.cursor.fetchall():
            TId, TName = item
            databaseMenuSub = HT.Optgroup(label="%s ------" % TName)
            self.cursor.execute(
                'SELECT ProbeSetFreeze.FullName,ProbeSetFreeze.Name FROM ProbeSetFreeze, ProbeFreeze, \
                                InbredSet WHERE ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id and ProbeFreeze.TissueId = %d and \
                                ProbeSetFreeze.public > %d and ProbeFreeze.InbredSetId = InbredSet.Id and InbredSet.Name like "%s%%" \
                                order by ProbeSetFreeze.CreateTime desc, ProbeSetFreeze.AvgId '
                % (TId, webqtlConfig.PUBLICTHRESH, RISet)
            )
            for item2 in self.cursor.fetchall():
                databaseMenuSub.append(item2)
                nmenu += 1
            databaseMenu.append(databaseMenuSub)

        if nmenu:
            criteriaText = HT.Span("Return:", Class="ffl fwb fs12")
            criteriaMenu = HT.Select(name="criteria", selected="500")
            criteriaMenu.append(("top 100", "100"))
            criteriaMenu.append(("top 200", "200"))
            criteriaMenu.append(("top 500", "500"))
            criteriaMenu.append(("top 1000", "1000"))
            criteriaMenu.append(("top 2000", "2000"))
            criteriaMenu.append(("top 5000", "5000"))
            criteriaMenu.append(("top 10000", "10000"))
            criteriaMenu.append(("top 15000", "15000"))
            criteriaMenu.append(("top 20000", "20000"))

            self.MPDCell = HT.TD()
            correlationMenus = HT.TableLite(
                HT.TR(HT.TD(databaseText, HT.BR(), databaseMenu, colspan=4)),
                HT.TR(HT.TD(methodText, HT.BR(), methodMenu), self.MPDCell, HT.TD(criteriaText, HT.BR(), criteriaMenu)),
                border=0,
                cellspacing=4,
                cellpadding=0,
            )
        else:
            correlationMenus = ""

        mainForm.append(
            HT.Font("or", color="red", size=4),
            HT.BR(),
            HT.BR(),
            "Compute partial correlation for each trait in the database selected below:",
            HT.BR(),
        )
        mainForm.append(partialCorrDBButton, HT.BR(), HT.BR(), correlationMenus)

        TD_LR.append(mainForm)

        self.dict["body"] = str(TD_LR)
        self.dict["js1"] = ""
        self.dict["title"] = "Partial Correlation Input"
Example #31
0
	def __init__(self,fd):

		self.theseTraits = []
		TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee',valign="middle")
		
		templatePage.__init__(self, fd)

		if not self.openMysql():
			return

		self.searchResult = fd.formdata.getvalue('searchResult', [])
		if type("1") == type(self.searchResult):
			self.searchResult = [self.searchResult]

		#XZ, self.theseTraits holds the "ProbeSet" traits.

		for item in self.searchResult:
			try:
				thisTrait = webqtlTrait(fullname=item, cursor=self.cursor)
				thisTrait.retrieveInfo(QTL=1)
				if thisTrait.db.type == "ProbeSet":
					self.theseTraits.append(thisTrait)
			except:
				pass
				
		if self.theseTraits:
			pass
		else:
			templatePage.__init__(self, fd)
			heading = 'WebGestalt'
			detail = ['You need to select at least one microarray trait to submit.']
			self.error(heading=heading,detail=detail)
			return
			
		chipName = self.testChip(fd)

		#XZ, 8/24/2009: the name of arraylist is misleading. It holds the name of traits.
		arraylist, geneIdList = self.genGeneIdList(fd)
		
		target_url = "http://bioinfo.vanderbilt.edu/webgestalt/webgestalt.php"
		
		formWebGestalt = HT.Form(cgi=target_url, enctype='multipart/form-data', name='WebGestalt', submit = HT.Input(type='hidden'))

		id_type = chipName

		hddnWebGestalt = {'id_list':string.join(arraylist, ","),
				  'id_type':id_type}
		
		hddnWebGestalt['ref_type'] = hddnWebGestalt['id_type']
		hddnWebGestalt['analysis_type'] = 'GO'
		hddnWebGestalt['significancelevel'] = '.05'
		hddnWebGestalt['stat'] = 'Hypergeometric'
		hddnWebGestalt['mtc'] = 'BH'
		hddnWebGestalt['min'] = '2'
		hddnWebGestalt['id_value'] = fd.formdata.getvalue('correlation') 
			
                species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=fd.RISet)

                if species == 'rat':
                    hddnWebGestalt['org'] = 'Rattus norvegicus'
                elif species == 'human':
                    hddnWebGestalt['org'] = 'H**o sapiens'
                elif species == 'mouse':
                    hddnWebGestalt['org'] = 'Mus musculus'
                else:
                    hddnWebGestalt['org'] = ''

		hddnWebGestalt['org'] = hddnWebGestalt['org'].replace(' ','_')
		
		for key in hddnWebGestalt.keys():
				formWebGestalt.append(HT.Input(name=key, value=hddnWebGestalt[key], type='hidden'))
	
		TD_LR.append(formWebGestalt)

		TD_LR.append(HT.Paragraph("Your selection of %d traits is being submitted to GO Tree" % len(self.theseTraits), Class="cr fs16 fwb", align="Center"))
		
		# updated by NL, moved mixedChipError() to webqtl.js and change it to mixedChipError(methodName)
		#                moved unknownChipError() to webqtl.js and change it to unknownChipError(chipName)
		if chipName == 'mixed':
			methodName = "WebGestalt"
			self.dict['js1'] = """
				<SCRIPT LANGUAGE="JavaScript">		
					setTimeout("mixedChipError('%s')" ,1000);
				</SCRIPT>
			""" % methodName 
		elif chipName.find('_NA') > 0:
			chipName = chipName[0:-3]
			self.dict['js1'] = """
                <SCRIPT LANGUAGE="JavaScript">
                setTimeout("unknownChipError('%s')",1000);
                </SCRIPT>
            """ % chipName
		else:
			self.dict['js1'] = """
				<SCRIPT LANGUAGE="JavaScript">
					setTimeout('document.WebGestalt.submit()',1000);
				</SCRIPT>
			"""
			
		self.dict['body'] = TD_LR