def session(self,mytitle="",myHeading=""):
        'generate a auto-refreshing temporary html file(waiting page)'
        self.filename = webqtlUtil.generate_session()
        self.dict['title'] = mytitle
        self.dict['basehref'] = webqtlConfig.REFRESHSTR % (webqtlConfig.CGIDIR, self.filename) + "" #webqtlConfig.BASEHREF

        TD_LR = HT.TD(align="center", valign="middle", height=200,width="100%", bgColor='#eeeeee')
        Heading = HT.Paragraph(myHeading, Class="fwb fs16 cr")
        # NL, 07/27/2010. variable 'PROGRESSBAR' has been moved from templatePage.py to webqtlUtil.py;
        TD_LR.append(Heading, HT.BR(), webqtlUtil.PROGRESSBAR)
        self.dict['body'] = TD_LR
        self.writeFile(self.filename + '.html')
        return self.filename
Esempio n. 2
0
       	def buildCanvas(self, colorScheme='', targetDescriptionChecked='', clusterChecked='', sessionfile='', genotype=None, strainlist=None, ppolar=None, mpolar=None, traitList=None, traitDataList=None, userPrivilege=None, userName=None):
                labelFont = pid.Font(ttf="tahoma",size=14,bold=0)
                topHeight = 0
       	       	NNN = len(traitList)
       	       	#XZ: It's necessory to define canvas here
                canvas = pid.PILCanvas(size=(80+NNN*20,880))
                names = map(webqtlTrait.displayName, traitList)
                #XZ, 7/29/2009: create trait display and find max strWidth
                strWidth = 0
                for j in range(len(names)):
                        thisTrait = traitList[j]
                        if 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.confidential:
                                    if webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=userPrivilege, userName=userName, authorized_users=thisTrait.authorized_users):
                                        if thisTrait.post_publication_abbreviation:
                                            names[j] += ' [%s]' % (thisTrait.post_publication_abbreviation)
                                    else:
                                        if thisTrait.pre_publication_abbreviation:
                                            names[j] += ' [%s]' % (thisTrait.pre_publication_abbreviation)
                                else:
                                    if thisTrait.post_publication_abbreviation:
                                        names[j] += ' [%s]' % (thisTrait.post_publication_abbreviation)
                            else:
                                pass

                        i = canvas.stringWidth(names[j], font=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),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)
				
                if not 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+ topHeight +5+5+strWidth+nLoci*cellHeight+80+20*cellHeight))

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

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

                        nnCorr = len(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,topHeight)
                        self.drawTraitNameTop(canvas,names,yoffset,neworder,strWidth,topHeight,labelFont)

                        #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, topHeight*3/4,font=labelFont,angle=90)

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


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

                nearestMarkers = self.getNearestMarker(traitList, genotype)

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

                areas = []
				#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 = 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(strainlist)):
                                if tempVal[i] != None:
                                        _strains.append(strainlist[i])
                                        _vals.append(tempVal[i])

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

                        if sessionfile:
                                LRSArray = permData[str(traitList[order[1]])]
                        else:
                                LRSArray = 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 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 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 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,topHeight+40,startWidth+10,startHeight)
                        HREF = "javascript:showDatabase2('%s','%s','%s');" % (traitList[order[1]].db.name, traitList[order[1]].name, traitList[order[1]].cellid)
                        area = (COORDS, HREF, '%s' % names[order[1]])
                        areas.append(area)

                        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 = topHeight + 40+5+5+strWidth

                if 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 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 +' % ppolar,startSpect+120,startHeight+ 35,font=textFont,color=pid.red)
                        canvas.drawString('%s +' % mpolar,startSpect+120,startHeight+ 15,font=textFont,color=pid.blue)
                elif 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 +' % ppolar,startSpect+120,startHeight+ 35,font=textFont,color=pid.red)
                        canvas.drawString('%s +' % mpolar,startSpect+120,startHeight+ 15,font=textFont,color=pid.blue)
						
                filename= webqtlUtil.genRandStr("Heatmap_")
                canvas.save(webqtlConfig.IMGDIR+filename, format='png')
                if not sessionfile:
                        sessionfile = webqtlUtil.generate_session()
                        webqtlUtil.dump_session(permData, os.path.join(webqtlConfig.TMPDIR, sessionfile +'.session'))
                self.filename=filename
                self.areas=areas
                self.sessionfile=sessionfile
    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
                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


                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)
    def __init__(self,fd,InputData=None):

        templatePage.__init__(self, fd)

        if not self.openMysql():
            return
            
        if not fd.genotype:
            fd.readGenotype()
                
        self.searchResult = fd.formdata.getvalue('searchResult')

        self.tissueProbeSetFeezeId = "1" #XZ, Jan 03, 2010: currently, this dataset is "UTHSC Illumina V6.2 RankInv B6 D2 average CNS GI average (May 08)"
        TissueCorrMatrixObject = tissueCorrelationMatrix(tissueProbeSetFreezeId=self.tissueProbeSetFeezeId)

        if type("1") == type(self.searchResult):
            self.searchResult = string.split(self.searchResult, '\t')
        
        if (not self.searchResult or (len(self.searchResult) < 2)):
            heading = 'Network Graph'
            detail = ['You need to select at least two traits in order to generate Network Graph.']
            self.error(heading=heading,detail=detail)
            print 'Content-type: text/html\n'
            self.write()
            return
        
        if self.searchResult:
            if len(self.searchResult) > webqtlConfig.MAXCORR:
                heading = 'Network Graph'
                detail = ['In order to display Network Graph properly, Do not select more than %d traits for Network Graph.' % webqtlConfig.MAXCORR]
                self.error(heading=heading,detail=detail)
                print 'Content-type: text/html\n'
                self.write()
                return
            else:    
                pass
                
            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 = 'Network Graph'
            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

        NNN = len(traitList)
        
        if NNN < 2:
            templatePage.__init__(self, fd)
            heading = 'Network Graph'
            detail = ['You need to select at least two traits in order to generate a Network Graph']
            print 'Content-type: text/html\n'
            self.write()
            return
        else:
            pearsonArray = [([0] * (NNN))[:] for i in range(NNN)]
            spearmanArray = [([0] * (NNN))[:] for i in range(NNN)]
            GeneIdArray = []
            GeneSymbolList = [] #XZ, Jan 03, 2011: holds gene symbols for calculating tissue correlation
            traitInfoArray = []

            i = 0
            nnCorr = len(fd.strainlist)
            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)
                        pearsonArray[i][j] = corr
                        pearsonArray[j][i] = corr
                    elif j == i:
                        pearsonArray[i][j] = 1.0
                        spearmanArray[i][j] = 1.0
                    else:
                        corr,nOverlap = webqtlUtil.calCorrelationRank(traitDataList[i],traitDataList[j],nnCorr)
                        spearmanArray[i][j] = corr
                        spearmanArray[j][i] = corr
                    
                GeneId1 = None
                tmpSymbol = None
                if thisTrait.db.type == 'ProbeSet':
                    try:
                        GeneId1 = int(thisTrait.geneid)
                    except:
                        GeneId1 = 0
                    if thisTrait.symbol:
                        tmpSymbol = thisTrait.symbol.lower()
                GeneIdArray.append(GeneId1)
                GeneSymbolList.append(tmpSymbol)

            _traits = []
            _matrix = []

            for i in range(NNN):
                turl = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s&ProbeSetID=%s' % (traitList[i].db.name, traitList[i].name)
                if traitList[i].cellid:
                    turl += "&CellID=%s" % traitList[i].cellid
                    
                if traitList[i].db.type == 'ProbeSet':
                    if traitList[i].symbol:
                        _symbol = traitList[i].symbol
                    else:
                        _symbol = 'unknown'
                elif traitList[i].db.type == 'Publish':
                    _symbol = traitList[i].name
                    if traitList[i].confidential:
                        if webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=traitList[i].authorized_users):
                            if traitList[i].post_publication_abbreviation:
                                _symbol = traitList[i].post_publication_abbreviation
                        else:
                            if traitList[i].pre_publication_abbreviation:
                                _symbol = traitList[i].pre_publication_abbreviation
                    else:
                        if traitList[i].post_publication_abbreviation:
                            _symbol = traitList[i].post_publication_abbreviation

                #XZ, 05/26/2009: Xiaodong add code for Geno data
                elif traitList[i].db.type == 'Geno':
                    _symbol = traitList[i].name
                else:
                    _symbol = traitList[i].description
                    #####if this trait entered by user
                    if _symbol.__contains__('entered'):
                        _symbol = _symbol[:_symbol.index('entered')]
                    #####if this trait generaged by genenetwork
                    elif _symbol.__contains__('generated'):
                        _symbol = _symbol[_symbol.rindex(':')+1:]
                
                newTrait = smpTrait.Trait(name=str(traitList[i]), href=turl, symbol=_symbol)
                newTrait.color = "black"
                _traits.append(newTrait)
                
                for j in range(i+1, NNN):
                    dataPoint = smpTrait.RawPoint(i, j)
                    dataPoint.spearman = spearmanArray[i][j]
                    dataPoint.pearson = pearsonArray[i][j]

                    #XZ: get literature correlation info.
                    if GeneIdArray[i] and GeneIdArray[j]:
                        if GeneIdArray[i] == GeneIdArray[j]:
                            dataPoint.literature = 1
                        else:
                            self.cursor.execute("SELECT Value from LCorrRamin3 WHERE (GeneId1 = %d and GeneId2 = %d) or (GeneId1 = %d and GeneId2 = %d)" % (GeneIdArray[i], GeneIdArray[j], GeneIdArray[j], GeneIdArray[i]))
                            try:    
                                dataPoint.literature = self.cursor.fetchone()[0]
                            except:
                                dataPoint.literature = 0
                    else:
                        dataPoint.literature = 0

                    #XZ: get tissue correlation info
                    if GeneSymbolList[i] and GeneSymbolList[j]:
                        dataPoint.tissue = 0
                        geneSymbolPair = []
                        geneSymbolPair.append(GeneSymbolList[i])
                        geneSymbolPair.append(GeneSymbolList[j])
                        corrArray,pvArray = TissueCorrMatrixObject.getCorrPvArrayForGeneSymbolPair(geneNameLst=geneSymbolPair)
                        if corrArray[1][0]:
                            dataPoint.tissue = corrArray[1][0]
                    else:
                        dataPoint.tissue = 0

                    _matrix.append(dataPoint)
        
            OrigDir = os.getcwd()

            sessionfile = fd.formdata.getvalue('session')
            
            inputFilename = fd.formdata.getvalue('inputFile')

            #If there is no sessionfile generate one and dump all matrix/trait values
            if not sessionfile:
                filename = webqtlUtil.generate_session()    
                webqtlUtil.dump_session([_matrix, _traits], os.path.join(webqtlConfig.TMPDIR, filename + '.session'))
                sessionfile = filename
            
            startTime = time.time()
            
            #Build parameter dictionary used by networkGraphPage class using buildParamDict function
            params = networkGraphUtils.buildParamDict(fd, sessionfile)
    
            nodes = len(_traits)
            rawEdges = len(_matrix)
            
            if params["tune"] == "yes":
                params = networkGraphUtils.tuneParamDict(params, nodes, rawEdges)
              
            matrix = networkGraphUtils.filterDataMatrix(_matrix, params)
            
            optimalNode = networkGraphUtils.optimalRadialNode(matrix)
            
            if not inputFilename:
                inputFilename = tempfile.mktemp()
            
            inputFilename = webqtlConfig.IMGDIR + inputFilename.split("/")[2]
                                           
            #writes out 4 graph files for exporting
            graphFile = "/image/" + networkGraphUtils.writeGraphFile(matrix, _traits, inputFilename, params)
            
            networkGraphUtils.processDataMatrix(matrix, params)

            edges = 0

            for edge in matrix:
                if edge.value != 0:
                    edges +=1

            for trait in _traits:
                trait.name = networkGraphUtils.fixLabel(trait.name)
            
            RootDir = webqtlConfig.IMGDIR
            RootDirURL = "/image/"                  


                  
                        #This code writes the datafile that the graphviz function runNeato uses to generate the 
                        #"digraph" file that defines the graphs parameters
            datafile = networkGraphUtils.writeNeatoFile(matrix=matrix, traits=_traits, filename=inputFilename, GeneIdArray=GeneIdArray, p=params)
            
            #Generate graph in various file types                      
            layoutfile = networkGraphUtils.runNeato(datafile, "dot", "dot", params["gType"]) # XZ, 09/11/2008: add module name
            # ZS 03/04/2010 This second output file (layoutfile_pdf) is rotated by 90 degrees to prevent an issue with pdf output being cut off at the edges
            layoutfile_pdf = networkGraphUtils.runNeato(datafile + "_pdf", "dot", "dot", params["gType"]) # ZS 03/04/2010
            pngfile = networkGraphUtils.runNeato(layoutfile, "png", "png", params["gType"]) 
            mapfile = networkGraphUtils.runNeato(layoutfile, "cmapx", "cmapx", params["gType"])# XZ, 09/11/2008: add module name    
            giffile = networkGraphUtils.runNeato(layoutfile, "gif", "gif", params["gType"])# XZ, 09/11/2008:add module name
            psfile = networkGraphUtils.runNeato(layoutfile_pdf, "ps", "ps", params["gType"])# XZ, 09/11/2008: add module name
            pdffile = networkGraphUtils.runPsToPdf(psfile, params["width"], params["height"])# XZ, 09/11/2008: add module name
            
                        #This generates text files in XGGML (standardized graphing language) and plain text
                        #so the user can create his/her own graphs in a program like Cytoscape
                    
            htmlfile1 = datafile + ".html"
            htmlfile2 = datafile + ".graph.html"

            os.chdir(OrigDir)

            #This generates the graph in various image formats
            giffile = RootDirURL + giffile
            pngfile = RootDirURL + pngfile
            pdffile = RootDirURL + pdffile
            endTime = time.time()
            totalTime = endTime - startTime

            os.chdir(RootDir)

            page2 = GraphPage(giffile, mapfile)
            page2.writeToFile(htmlfile2)
            
            #This generates the HTML for the body of the Network Graph page
            page1 = networkGraphPageBody(fd, matrix, _traits, htmlfile2, giffile, pdffile, nodes, edges, rawEdges, totalTime, params, page2.content, graphFile, optimalNode)
            
            #Adds the javascript colorSel to the body to allow line color selection
            self.dict["js1"] = '<SCRIPT SRC="/javascript/colorSel.js"></SCRIPT><BR>'   
            #self.dict["js1"] += '<SCRIPT SRC="/javascript/networkGraph.js"></SCRIPT>' 
                        
            #Set body of current templatePage to body of the templatePage networkGraphPage                        
            self.dict['body'] = page1.dict['body']