Ejemplo n.º 1
0
    def get_trait_info(self, trait_list, species = ''):
        for this_trait in trait_list:
            if not this_trait.haveinfo:
                this_trait.retrieve_info(get_qtl_info=True)

            description = this_trait.post_publication_description
            
            #If the dataset is confidential and the user has access to confidential
            #phenotype traits, then display the pre-publication description instead
            #of the post-publication description
            if this_trait.confidential:
                continue   # for now
            
                if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(
                        privilege=self.privilege,
                        userName=self.userName,
                        authorized_users=this_trait.authorized_users):
                        
                    description = this_trait.pre_publication_description
            
            this_trait.description_display = description.strip()

            if not this_trait.year.isdigit():
                this_trait.pubmed_text = "N/A"

            if this_trait.pubmed_id:
                this_trait.pubmed_link = webqtlConfig.PUBMEDLINK_URL % this_trait.pubmed_id

            #LRS and its location
            this_trait.LRS_score_repr = "N/A"
            this_trait.LRS_score_value = 0
            this_trait.LRS_location_repr = "N/A"
            this_trait.LRS_location_value = 1000000

            if this_trait.lrs:
                result = g.db.execute("""
                    select Geno.Chr, Geno.Mb from Geno, Species
                    where Species.Name = %s and
                        Geno.Name = %s and
                        Geno.SpeciesId = Species.Id
                """, (species, this_trait.locus)).fetchone()
                #result = self.cursor.fetchone()

                if result:
                    if result[0] and result[1]:
                        LRS_Chr = result[0]
                        LRS_Mb = result[1]

                        #XZ: LRS_location_value is used for sorting
                        try:
                            LRS_location_value = int(LRS_Chr)*1000 + float(LRS_Mb)
                        except:
                            if LRS_Chr.upper() == 'X':
                                LRS_location_value = 20*1000 + float(LRS_Mb)
                            else:
                                LRS_location_value = ord(str(LRS_chr).upper()[0])*1000 + float(LRS_Mb)

                        this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs
                        this_trait.LRS_score_value = LRS_score_value = this_trait.lrs
                        this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb))
Ejemplo n.º 2
0
    def get_trait_info(self, trait_list, species = ''):
        for this_trait in trait_list:

            if not this_trait.haveinfo:
                this_trait.retrieve_info(get_qtl_info=True)

            description = this_trait.post_publication_description

            #If the dataset is confidential and the user has access to confidential
            #phenotype traits, then display the pre-publication description instead
            #of the post-publication description
            if this_trait.confidential:
                this_trait.description_display = ""
                continue   # for now, because no authorization features

                if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(
                        privilege=self.privilege,
                        userName=self.userName,
                        authorized_users=this_trait.authorized_users):

                    description = this_trait.pre_publication_description

            if len(description) > 0:
                this_trait.description_display = description.strip()
            else:
                this_trait.description_display = ""

            if not this_trait.year.isdigit():
                this_trait.pubmed_text = "N/A"
            else:
                this_trait.pubmed_text = this_trait.year

            if this_trait.pubmed_id:
                this_trait.pubmed_link = webqtlConfig.PUBMEDLINK_URL % this_trait.pubmed_id

            #LRS and its location
            this_trait.LRS_score_repr = "N/A"
            this_trait.LRS_location_repr = "N/A"

            if this_trait.lrs:
                query = """
                    select Geno.Chr, Geno.Mb from Geno, Species
                    where Species.Name = '%s' and
                        Geno.Name = '%s' and
                        Geno.SpeciesId = Species.Id
                """ % (species, this_trait.locus)
                logger.sql(query)
                result = g.db.execute(query).fetchone()

                if result:
                    if result[0] and result[1]:
                        LRS_Chr = result[0]
                        LRS_Mb = result[1]

                        this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs
                        this_trait.LRS_location_repr = LRS_location_repr = 'Chr%s: %.6f' % (LRS_Chr, float(LRS_Mb))
Ejemplo n.º 3
0
    def getCollectionTableBody(self, traitList=None, formName=None, species=""):

        tblobj_body = []

        className = "fs12 fwn b1 c222"

        for thisTrait in traitList:
            tr = []

            if not thisTrait.haveinfo:
                thisTrait.retrieveInfo(QTL=1)

            trId = str(thisTrait)

            oneRadioName = thisTrait.getName()

            tr.append(TDCell(HT.TD(" ", align="center", valign="center", Class=className)))
            tr.append(
                TDCell(
                    HT.TD(
                        HT.Input(type="radio", name=oneRadioName, value="primary"),
                        align="center",
                        valign="center",
                        Class=className,
                    )
                )
            )
            tr.append(
                TDCell(
                    HT.TD(
                        HT.Input(type="radio", name=oneRadioName, value="control"),
                        align="center",
                        valign="center",
                        Class=className,
                    )
                )
            )
            tr.append(
                TDCell(
                    HT.TD(
                        HT.Input(type="radio", name=oneRadioName, value="target", checked="true"),
                        align="center",
                        valign="center",
                        Class=className,
                    )
                )
            )
            tr.append(
                TDCell(
                    HT.TD(
                        HT.Input(type="radio", name=oneRadioName, value="ignored"),
                        align="center",
                        valign="center",
                        Class=className,
                    )
                )
            )

            tr.append(
                TDCell(
                    HT.TD(thisTrait.db.displayname, Class="fs12 fwn b1 c222"),
                    thisTrait.db.displayname,
                    thisTrait.db.displayname.upper(),
                )
            )

            tr.append(
                TDCell(
                    HT.TD(
                        HT.Href(
                            text=thisTrait.name,
                            url="javascript:showDatabase3('%s','%s','%s','')"
                            % (formName, thisTrait.db.name, thisTrait.name),
                            Class="fs12 fwn",
                        ),
                        nowrap="yes",
                        align="left",
                        Class=className,
                    ),
                    str(thisTrait.name),
                    thisTrait.name,
                )
            )

            # description column
            if 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=className), PhenotypeString, PhenotypeString.upper()))
            elif thisTrait.db.type == "ProbeSet" or thisTrait.db.type == "Temp":
                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

                tr.append(TDCell(HT.TD(description_string, Class=className), description_string, description_string))
            else:
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))

            # location column
            if thisTrait.db.type == "Publish":
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))
            else:
                # ZS: trait_location_value is used for sorting
                trait_location_repr = "N/A"
                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))

                tr.append(
                    TDCell(
                        HT.TD(trait_location_repr, nowrap="yes", Class=className),
                        trait_location_repr,
                        trait_location_value,
                    )
                )

            if thisTrait.db.type == "ProbeSet":
                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

                # XZ, 06/05/2009: It is neccessary to turn on nowrap
                repr = "%2.3f" % mean
                tr.append(TDCell(HT.TD(repr, Class=className, align="right", nowrap="ON"), repr, mean))

            elif thisTrait.db.type == "Publish":
                self.cursor.execute(
                    """
                                select count(PublishData.value), sum(PublishData.value) from PublishData, PublishXRef, PublishFreeze
                                where PublishData.Id = PublishXRef.DataId and 
                                        PublishXRef.Id = %s and
                                        PublishXRef.InbredSetId = PublishFreeze.InbredSetId and
                                        PublishFreeze.Id = %d
                                """
                    % (thisTrait.name, thisTrait.db.id)
                )
                result = self.cursor.fetchone()

                if result:
                    if result[0] and result[1]:
                        mean = result[1] / result[0]
                    else:
                        mean = 0
                else:
                    mean = 0

                repr = "%2.3f" % mean
                tr.append(TDCell(HT.TD(repr, Class=className, align="right", nowrap="ON"), repr, mean))
            else:
                tr.append(TDCell(HT.TD("--", Class=className, align="left", nowrap="ON"), "--", 0))

            # Number of cases
            n_cases_value = 0
            n_cases_repr = "--"
            if thisTrait.db.type == "Publish":
                self.cursor.execute(
                    """
                                select count(PublishData.value) from PublishData, PublishXRef, PublishFreeze
                                where PublishData.Id = PublishXRef.DataId and 
                                        PublishXRef.Id = %s and
                                        PublishXRef.InbredSetId = PublishFreeze.InbredSetId and
                                        PublishFreeze.Id = %d
                                """
                    % (thisTrait.name, thisTrait.db.id)
                )
                result = self.cursor.fetchone()

                if result:
                    if result[0]:
                        n_cases_value = result[0]
                        n_cases_repr = result[0]

                if n_cases_value == "--":
                    tr.append(
                        TDCell(
                            HT.TD(n_cases_repr, Class=className, align="left", nowrap="on"), n_cases_repr, n_cases_value
                        )
                    )
                else:
                    tr.append(
                        TDCell(
                            HT.TD(n_cases_repr, Class=className, align="right", nowrap="on"),
                            n_cases_repr,
                            n_cases_value,
                        )
                    )

            elif thisTrait.db.type == "ProbeSet":
                self.cursor.execute(
                    """
                                select count(ProbeSetData.value) from ProbeSet, ProbeSetXRef, ProbeSetData, ProbeSetFreeze
                                where ProbeSet.Name='%s' and
                                        ProbeSetXRef.ProbeSetId = ProbeSet.Id and
                                        ProbeSetXRef.DataId = ProbeSetData.Id and
                                        ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id and
                                        ProbeSetFreeze.Name = '%s'
                                """
                    % (thisTrait.name, thisTrait.db.name)
                )
                result = self.cursor.fetchone()

                if result:
                    if result[0]:
                        n_cases_value = result[0]
                        n_cases_repr = result[0]
                if n_cases_value == "--":
                    tr.append(
                        TDCell(
                            HT.TD(n_cases_repr, Class=className, align="left", nowrap="on"), n_cases_repr, n_cases_value
                        )
                    )
                else:
                    tr.append(
                        TDCell(
                            HT.TD(n_cases_repr, Class=className, align="right", nowrap="on"),
                            n_cases_repr,
                            n_cases_value,
                        )
                    )

            elif thisTrait.db.type == "Geno":
                self.cursor.execute(
                    """
                                select count(GenoData.value) from GenoData, GenoXRef, GenoFreeze, Geno, Strain
                                where Geno.SpeciesId = %s and Geno.Name='%s' and
                                        GenoXRef.GenoId = Geno.Id and
                                        GenoXRef.DataId = GenoData.Id and
                                        GenoXRef.GenoFreezeId = GenoFreeze.Id and
                                        GenoData.StrainId = Strain.Id and
                                        GenoFreeze.Name = '%s'
                                """
                    % (
                        webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, thisTrait.db.riset),
                        thisTrait.name,
                        thisTrait.db.name,
                    )
                )
                result = self.cursor.fetchone()

                if result:
                    if result[0]:
                        n_cases_value = result[0]
                        n_cases_repr = result[0]
                if n_cases_value == "--":
                    tr.append(
                        TDCell(
                            HT.TD(n_cases_repr, Class=className, align="left", nowrap="on"), n_cases_repr, n_cases_value
                        )
                    )
                else:
                    tr.append(
                        TDCell(
                            HT.TD(n_cases_repr, Class=className, align="right", nowrap="on"),
                            n_cases_repr,
                            n_cases_value,
                        )
                    )

            else:
                tr.append(
                    TDCell(HT.TD(n_cases_repr, Class=className, align="left", nowrap="on"), n_cases_repr, n_cases_value)
                )

            if thisTrait.db.type != "Geno":
                # LRS and its location
                LRS_score_repr = "--"
                LRS_score_value = 0
                LRS_location_repr = "--"
                LRS_location_value = 1000000
                LRS_flag = 1

                # Max LRS and its Locus location
                if hasattr(thisTrait, "lrs") and hasattr(thisTrait, "locus") and thisTrait.lrs and thisTrait.locus:
                    self.cursor.execute(
                        """
                                        select Geno.Chr, Geno.Mb from Geno, Species
                                        where Species.Name = '%s' and
                                                Geno.Name = '%s' and
                                                Geno.SpeciesId = Species.Id
                                        """
                        % (species, thisTrait.locus)
                    )
                    result = self.cursor.fetchone()

                    if result:
                        if result[0] and result[1]:
                            LRS_Chr = result[0]
                            LRS_Mb = result[1]

                            # XZ: LRS_location_value is used for sorting
                            try:
                                LRS_location_value = int(LRS_Chr) * 1000 + float(LRS_Mb)
                            except:
                                if LRS_Chr.upper() == "X":
                                    LRS_location_value = 20 * 1000 + float(LRS_Mb)
                                else:
                                    LRS_location_value = ord(str(LRS_chr).upper()[0]) * 1000 + float(LRS_Mb)

                            LRS_score_repr = "%3.1f" % thisTrait.lrs
                            LRS_score_value = thisTrait.lrs
                            LRS_location_repr = "Chr%s: %.6f" % (LRS_Chr, float(LRS_Mb))
                            LRS_flag = 0

                            tr.append(
                                TDCell(
                                    HT.TD(LRS_score_repr, Class=className, align="right", nowrap="on"),
                                    LRS_score_repr,
                                    LRS_score_value,
                                )
                            )
                            tr.append(
                                TDCell(HT.TD(LRS_location_repr, Class=className), LRS_location_repr, LRS_location_value)
                            )

                if LRS_flag:
                    tr.append(TDCell(HT.TD(LRS_score_repr, Class=className), LRS_score_repr, LRS_score_value))
                    tr.append(TDCell(HT.TD(LRS_location_repr, Class=className), LRS_location_repr, LRS_location_value))
            else:
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", 0))
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", 1000000))

            tblobj_body.append(tr)

        return tblobj_body
Ejemplo n.º 4
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)
Ejemplo n.º 6
0
	def genHTML(self, formName = "", dispFromDatabase=0, privilege="guest", userName="Guest", authorized_users=""):
		if not self.haveinfo:
			self.retrieveInfo()
		
		if self.db.type == 'Publish':
			PubMedLink = ""
	 		if self.pubmed_id:
	 			PubMedLink = HT.Href(text="PubMed %d : " % self.pubmed_id,
	 			target = "_blank", url = webqtlConfig.PUBMEDLINK_URL % self.pubmed_id)
			else:
				PubMedLink = HT.Span("Unpublished : ", Class="fs15")
			
			if formName:
				setDescription2 = HT.Href(url="javascript:showDatabase3('%s','%s','%s','')" % 
				(formName, self.db.name, self.name), Class = "fs14")
			else:
				setDescription2 = HT.Href(url="javascript:showDatabase2('%s','%s','')" % 
				(self.db.name,self.name), Class = "fs14")
	
			if self.confidential and not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=privilege, userName=userName, authorized_users=authorized_users):
					setDescription2.append('RecordID/%s - %s' % (self.name, self.pre_publication_description))
			else:
				setDescription2.append('RecordID/%s - %s' % (self.name, self.post_publication_description))

			#XZ 03/26/2011: Xiaodong comment out the following two lins as Rob asked. Need to check with Rob why in PublishXRef table, there are few row whose Sequence > 1.
			#if self.sequence > 1:
			#	setDescription2.append(' btach %d' % self.sequence)
			if self.authors:
				a1 = string.split(self.authors,',')[0]
				while a1[0] == '"' or a1[0] == "'" :
					a1 = a1[1:]
				setDescription2.append(' by ')
				setDescription2.append(HT.Italic('%s, and colleagues' % a1))
			setDescription = HT.Span(PubMedLink, setDescription2)
			
		elif self.db.type == 'Temp':
			setDescription = HT.Href(text="%s" % (self.description),url="javascript:showDatabase2\
			('%s','%s','')" % (self.db.name,self.name), Class = "fs14")
			setDescription = HT.Span(setDescription)
			
		elif self.db.type == 'Geno': # Genome DB only available for single search
			if formName:
				setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\
			'%2.3f' % self.mb),url="javascript:showDatabase3('%s','%s','%s','')" % \
			(formName, self.db.name, self.name), Class = "fs14")
			else:
				setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\
			'%2.3f' % self.mb),url="javascript:showDatabase2('%s','%s','')" % \
			(self.db.name,self.name), Class = "fs14")
				
			setDescription = HT.Span(setDescription)
			
		else:
			if self.cellid:
				if formName:	
					setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name, self.cellid),url=\
				"javascript:showDatabase3('%s','%s','%s','%s')" % (formName, self.db.name,self.name,self.cellid), \
				Class = "fs14")
				else:	
					setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name,self.cellid),url=\
				"javascript:showDatabase2('%s','%s','%s')" % (self.db.name,self.name,self.cellid), \
				Class = "fs14")
			else:
				if formName:
					setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\
				"javascript:showDatabase3('%s','%s','%s','')" % (formName, self.db.name,self.name), \
				Class = "fs14")
				else:
					setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\
				"javascript:showDatabase2('%s','%s','')" % (self.db.name,self.name), \
				Class = "fs14")
			if self.symbol and self.chr and self.mb:
				setDescription.append(' [')
				setDescription.append(HT.Italic('%s' % self.symbol,Class="cdg fwb"))
				setDescription.append(' on Chr %s @ %s Mb]' % (self.chr,self.mb))
			if self.description:
				setDescription.append(': %s' % self.description)
			if self.probe_target_description:
				setDescription.append('; %s' % self.probe_target_description)
			setDescription = HT.Span(setDescription)
			
		if self.db.type != 'Temp' and dispFromDatabase:
			setDescription.append( ' --- FROM : ')
			setDescription.append(self.db.genHTML(Class='cori'))
		return setDescription
    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)
Ejemplo n.º 8
0
    def getCollectionTableBody(self, RISet=None, traitList=None, formName=None, species=''):

        tblobj_body = []

        className = "fs12 fwn b1 c222"

        for thisTrait in traitList:
            tr = []

            if not thisTrait.haveinfo:
                thisTrait.retrieveInfo(QTL=1)

            if thisTrait.riset != RISet:
                continue

            trId = str(thisTrait)

            #XZ: check box column
            tr.append(TDCell(HT.TD(HT.Input(type="checkbox", Class="checkallbox", name="searchResult",value=trId, onClick="highlight(this)"), nowrap="on", Class=className), text=trId))

            #XZ: Dataset column
            tr.append(TDCell(HT.TD(thisTrait.db.displayname, Class="fs12 fwn b1 c222"), thisTrait.db.displayname, thisTrait.db.displayname.upper()))

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

            #XZ: Symbol column and Description column
            if (thisTrait.db.type == "Publish"):
                AbbreviationString = "--"
                if (thisTrait.post_publication_abbreviation != None):
                    AbbreviationString = thisTrait.post_publication_abbreviation
                PhenotypeString = thisTrait.post_publication_description
                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 = "--"
                        PhenotypeString = thisTrait.pre_publication_description

                if AbbreviationString == "--":
                    tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))
                else:
                    tr.append(TDCell(HT.TD(AbbreviationString, Class=className), AbbreviationString, AbbreviationString.upper()))
                if PhenotypeString:
                    PhenotypeString_upper = PhenotypeString.upper()
                else:
                    PhenotypeString_upper = PhenotypeString
                tr.append(TDCell(HT.TD(PhenotypeString, Class=className), PhenotypeString, PhenotypeString_upper))


            elif (thisTrait.db.type == "ProbeSet" or thisTrait.db.type == "Temp"):
                description_string = str(thisTrait.description).strip()
                if (thisTrait.db.type == "ProbeSet"):
                    if (thisTrait.symbol != None):
                        if thisTrait.geneid:
                            symbolurl = HT.Href(text=thisTrait.symbol,target='_blank',url="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=Retrieve&dopt=Graphics&list_uids=%s" % thisTrait.geneid, Class="font_black fs12 fwn")
                        else:
                            symbolurl = HT.Href(text=thisTrait.symbol,target='_blank',url="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?CMD=search&DB=gene&term=%s" % thisTrait.symbol, Class="font_black fs12 fwn")
                        tr.append(TDCell(HT.TD(symbolurl, align="left", Class="fs12 fwn b1 c222 fsI"), thisTrait.symbol, thisTrait.symbol))
                    else:
                        tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))
                    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:
                    tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))
                tr.append(TDCell(HT.TD(description_string, Class=className), description_string, description_string))
            else:
                if (thisTrait.name != None):
                    tr.append(TDCell(HT.TD(thisTrait.name, Class="fs12 fwn b1 c222"), thisTrait.name, thisTrait.name))
                else:
                    tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))

            #XZ: Location column
            if (thisTrait.db.type == "Publish"):
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", "Zz"))
            else:
                if thisTrait.db.type == "ProbeSet" and thisTrait.cellid:
                    EnsemblProbeSetID = thisTrait.name
                    if '_at' in thisTrait.name:
                        EnsemblProbeSetID = thisTrait.name[0:thisTrait.name.index('_at')+3]

                    #These tables (Ensembl) were created by Xusheng Wang in 2010 and are mm9 (so they'll need to be changed at some point to be mm10.
                    self.cursor.execute('''
                                    SELECT EnsemblProbeLocation.*
                                    FROM EnsemblProbeLocation, EnsemblProbe, EnsemblChip, GeneChipEnsemblXRef, ProbeFreeze, ProbeSetFreeze
                                    WHERE EnsemblProbeLocation.ProbeId=EnsemblProbe.Id and EnsemblProbe.ChipId=GeneChipEnsemblXRef.EnsemblChipId and
                                            GeneChipEnsemblXRef.GeneChipId=ProbeFreeze.ChipId and EnsemblProbe.Name=%s and EnsemblProbe.ProbeSet=%s and
                                            ProbeSetFreeze.Id=%s and ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id group by Chr, Start, End'''
                                    ,(thisTrait.cellid, EnsemblProbeSetID, thisTrait.db.id))
                    LocationFields = self.cursor.fetchall()

                    Chr=''
                    Mb=''
                    Start=''
                    End=''
                    if (len(LocationFields)>=1):
                        Chr,Start,Start_2016,End,End_2016,Strand,MisMatch,ProbeId = map(self.nullRecord,LocationFields[0])
                        Start /= 1000000.0
                        End /= 1000000.0
                        Mb = Start
                    if (len(LocationFields)>1):
                        self.cursor.execute('''
                                        SELECT ProbeSet.Chr, ProbeSet.Mb FROM ProbeSet, ProbeFreeze, ProbeSetFreeze
                                        WHERE ProbeSet.ChipId=ProbeFreeze.ChipId and ProbeSet.Name=%s and ProbeSetFreeze.Id=%s and
                                                ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id'''
                                        ,(thisTrait.name, thisTrait.db.id))
                        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, ProbeSetFreeze
                                        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 ProbeSetFreeze.Id=%s and ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id order by Mb limit 1'''
                                        ,(ProbeSetMb, thisTrait.cellid, EnsemblProbeSetID, ProbeSetChr, thisTrait.db.id))
                        NewLocationFields = self.cursor.fetchall()
                        if (len(NewLocationFields)>0):
                            Chr,Start,Start_2016,End,End_2016,Strand,MisMatch,ProbeId,Mb = map(self.nullRecord,NewLocationFields[0])
                            Start /= 1000000.0
                            End /= 1000000.0
                            Mb = Start

                    #ZS: trait_location_value is used for sorting
                    trait_location_repr = "--"
                    trait_location_value = 1000000

                    if Chr and Mb:
                        try:
                            trait_location_value = int(Chr)*1000 + Mb
                        except:
                            if Chr.upper() == "X":
                                trait_location_value = 20*1000 + Mb
                            else:
                                trait_location_value = ord(str(Chr).upper()[0])*1000 + Mb

                        trait_location_repr = "Chr%s: %.6f" % (Chr, float(Mb) )

                    tr.append(TDCell(HT.TD(trait_location_repr, nowrap='ON', Class=className), trait_location_repr, trait_location_value))

                else:

                    #ZS: 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) )

                    tr.append(TDCell(HT.TD(trait_location_repr, nowrap='ON', Class=className), trait_location_repr, trait_location_value))

            #XZ: Mean column
            if (thisTrait.db.type == "ProbeSet"):
                if thisTrait.cellid:
                    mean = -10000.0
                    try:
                        thisTrait.retrieveData()
                        mean, median, var, stdev, sem, N = reaper.anova(thisTrait.exportInformative()[1])
                    except:
                        pass
                    repr = '%2.3f' % mean
                    mean = '%2.2f' % mean
                    tr.append(TDCell(HT.TD(repr, Class=className, align='right', nowrap='ON'),repr, mean))
                else:
                    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

                    #XZ, 06/05/2009: It is neccessary to turn on nowrap
                    repr = "%2.3f" % mean
                    tr.append(TDCell(HT.TD(repr, Class=className, align='right', nowrap='ON'),repr, mean))

            elif (thisTrait.db.type == "Publish"):
                self.cursor.execute("""
                select count(PublishData.value), sum(PublishData.value) from PublishData, PublishXRef, PublishFreeze
                where PublishData.Id = PublishXRef.DataId and
                        PublishXRef.Id = %s and
                        PublishXRef.InbredSetId = PublishFreeze.InbredSetId and
                        PublishFreeze.Id = %d
                """ % (thisTrait.name, thisTrait.db.id))
                result = self.cursor.fetchone()

                if result:
                    if result[0] and result[1]:
                        mean = result[1]/result[0]
                    else:
                        mean = 0
                else:
                    mean = 0

                repr = "%2.3f" % mean
                tr.append(TDCell(HT.TD(repr, Class=className, align='right', nowrap='ON'),repr, mean))
            else:
                tr.append(TDCell(HT.TD("--", Class=className, align='left', nowrap='ON'),"--", 0))

            #Number of cases
            n_cases_value = 0
            n_cases_repr = "--"
            if (thisTrait.db.type == "Publish"):
                self.cursor.execute("""
                select count(PublishData.value) from PublishData, PublishXRef, PublishFreeze
                where PublishData.Id = PublishXRef.DataId and
                        PublishXRef.Id = %s and
                        PublishXRef.InbredSetId = PublishFreeze.InbredSetId and
                        PublishFreeze.Id = %d
                """ % (thisTrait.name, thisTrait.db.id))
                result = self.cursor.fetchone()

                if result:
                    if result[0]:
                        n_cases_value = result[0]
                        n_cases_repr = result[0]

                if (n_cases_value == "--"):
                    tr.append(TDCell(HT.TD(n_cases_repr, Class=className, align='left', nowrap="on"), n_cases_repr, n_cases_value))
                else:
                    tr.append(TDCell(HT.TD(n_cases_repr, Class=className, align='right', nowrap="on"), n_cases_repr, n_cases_value))

            elif (thisTrait.db.type == "ProbeSet"):
                self.cursor.execute("""
                select count(ProbeSetData.value) from ProbeSet, ProbeSetXRef, ProbeSetData, ProbeSetFreeze
                where ProbeSet.Name='%s' and
                        ProbeSetXRef.ProbeSetId = ProbeSet.Id and
                        ProbeSetXRef.DataId = ProbeSetData.Id and
                        ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id and
                        ProbeSetFreeze.Name = '%s'
                """ % (thisTrait.name, thisTrait.db.name))
                result = self.cursor.fetchone()

                if result:
                    if result[0]:
                        n_cases_value = result[0]
                        n_cases_repr = result[0]
                if (n_cases_value == "--"):
                    tr.append(TDCell(HT.TD(n_cases_repr, Class=className, align='left', nowrap="on"), n_cases_repr, n_cases_value))
                else:
                    tr.append(TDCell(HT.TD(n_cases_repr, Class=className, align='right', nowrap="on"), n_cases_repr, n_cases_value))

            elif (thisTrait.db.type == "Geno"):
                self.cursor.execute("""
                select count(GenoData.value) from GenoData, GenoXRef, GenoFreeze, Geno, Strain
                where Geno.SpeciesId = %s and Geno.Name='%s' and
                        GenoXRef.GenoId = Geno.Id and
                        GenoXRef.DataId = GenoData.Id and
                        GenoXRef.GenoFreezeId = GenoFreeze.Id and
                        GenoData.StrainId = Strain.Id and
                        GenoFreeze.Name = '%s'
                """ % (webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, thisTrait.db.riset), thisTrait.name, thisTrait.db.name))
                result = self.cursor.fetchone()

                if result:
                    if result[0]:
                        n_cases_value = result[0]
                        n_cases_repr = result[0]
                if (n_cases_value == "--"):
                    tr.append(TDCell(HT.TD(n_cases_repr, Class=className, align='left', nowrap="on"), n_cases_repr, n_cases_value))
                else:
                    tr.append(TDCell(HT.TD(n_cases_repr, Class=className, align='right', nowrap="on"), n_cases_repr, n_cases_value))

            else:
                tr.append(TDCell(HT.TD(n_cases_repr, Class=className, align='left', nowrap="on"), n_cases_repr, n_cases_value))

            #XZ: Max LRS column and Max LRS Location column
            if (thisTrait.db.type != "Geno"):
                #LRS value
                LRS_score_repr = '--'
                LRS_score_value = 0
		if hasattr(thisTrait, 'lrs') and thisTrait.lrs:
			LRS_score_repr = '%3.1f' % thisTrait.lrs
			LRS_score_value = thisTrait.lrs
		tr.append(TDCell(HT.TD(LRS_score_repr, Class=className, align='right', nowrap="on"), LRS_score_repr, LRS_score_value))

		#LRS location
                LRS_location_repr = '--'
                LRS_location_value = 1000000
                LRS_flag = 1

                #Max LRS and its Locus location
                if hasattr(thisTrait, 'lrs') and hasattr(thisTrait, 'locus') and thisTrait.lrs and thisTrait.locus:
                    self.cursor.execute("""
                    select Geno.Chr, Geno.Mb from Geno, Species
                    where Species.Name = '%s' and
                            Geno.Name = '%s' and
                            Geno.SpeciesId = Species.Id
                    """ % (species, thisTrait.locus))
                    result = self.cursor.fetchone()

                    if result:
                        if result[0] and result[1]:
                            LRS_Chr = result[0]
                            LRS_Mb = result[1]

                            #XZ: LRS_location_value is used for sorting
                            try:
                                LRS_location_value = int(LRS_Chr)*1000 + float(LRS_Mb)
                            except:
                                if LRS_Chr.upper() == 'X':
                                    LRS_location_value = 20*1000 + float(LRS_Mb)
                                else:
                                    LRS_location_value = ord(str(LRS_chr).upper()[0])*1000 + float(LRS_Mb)

                            LRS_location_repr = 'Chr%s: %.6f' % (LRS_Chr, float(LRS_Mb))
                            LRS_flag = 0

                            tr.append(TDCell(HT.TD(LRS_location_repr, Class=className), LRS_location_repr, LRS_location_value))

                if LRS_flag:
                    tr.append(TDCell(HT.TD(LRS_location_repr, Class=className), LRS_location_repr, LRS_location_value))
            else:
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", 0))
                tr.append(TDCell(HT.TD("--", align="left", Class=className), "--", 1000000))

            try:
                additive = 	'%3.3f' % thisTrait.additive
            except Exception:
                additive = 	''
            tr.append(TDCell(HT.TD(additive, Class=className, align="right", nowrap="on"), additive, additive))
			
            tblobj_body.append(tr)

        return tblobj_body
    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'
Ejemplo n.º 10
0
    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']