def execute(choices, galaxyFn=None, username=''):

        gsuite = getGSuiteFromGalaxyTN(choices.gsuite)
        bedRegions = choices.bedRegions

        rp = RP(gsuite)
        bedData = rp.openBedFile(bedRegions)
        chrOrder, chrLength = rp.sortChrDict()
        dataDict, dataDictLine, elementOrder, listResCopy, newDictRegions = rp.countMutations(
            chrLength, bedData)

        vg = visualizationGraphs()
        tName = gsuite.allTrackTitles()

        uniformDictList = OrderedDict()  # expected values
        observedDictList = OrderedDict()  # observed values
        seriesNameRegionUDL = OrderedDict()
        seriesNameRegionODL = OrderedDict()
        seriesNameRegion = OrderedDict()

        GenerateDistributionOfPointsOfInterestTool.countRegionsForDistribution(
            newDictRegions, observedDictList, rp, seriesNameRegion,
            seriesNameRegionODL, seriesNameRegionUDL, tName, uniformDictList)
        res = ''
        for elK in uniformDictList.keys():
            res += GenerateDistributionOfPointsOfInterestTool.drawDistribution(
                elK, observedDictList, seriesNameRegion, seriesNameRegionODL,
                seriesNameRegionUDL, uniformDictList, vg)

        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.line(res)
        htmlCore.end()
        htmlCore.hideToggle(styleClass='debug')
        print htmlCore
    def visualizeResults(self, results):

        globalResult = results.getGlobalResult()
        res = OrderedDict([(x, globalResult[x]) for x in globalResult
                           if x not in ['Assembly_gap_coverage']])

        dT = dataTransformer(res)
        seriesName, categories, data = dT.changeDictIntoList()

        vg = visualizationGraphs()
        return vg.drawPieChart(data, seriesName=categories)
    def visualizeResults(self, results):

        globalResult = results.getGlobalResult()
        res = OrderedDict([(x, globalResult[x]) for x in globalResult
                           if x not in ['Assembly_gap_coverage', 'Inclusions']
                           ])

        dT = dataTransformer(res)
        seriesName, categories, data = dT.changeDictIntoListsByLevel()

        vg = visualizationGraphs()
        res = vg.drawColumnCharts(data, categories=categories)

        return vg.visualizeResults(res)
    def visualizeResults(self, results):

        globalResult = results.getGlobalResult()
        res = OrderedDict([(x, globalResult[x]) for x in globalResult
                           if x not in ['Assembly_gap_coverage', 'BinSize']])

        dT = dataTransformer(res)
        seriesName, categories, data = dT.changeDictIntoListByTrackString(
            numStart=0, numEnd=len(res))

        vg = visualizationGraphs()
        res = vg.drawColumnChart(data, categories=categories)

        return vg.visualizeResults(res)
Example #5
0
    def getInteractiveColumnChart(cls, distances, distCase, htmlCore):
        htmlCore.divider()
        htmlCore.header('Interactive distance plot')
        htmlCore.line('To see the exact distance for the different points ' + distCase +
                      ' tracks, hover over the bars of interest.')
        htmlCore.divEnd()  # End div set in execute to remove font settings

        vg = visualizationGraphs()
        lines = vg.drawColumnChart(
            dataY=distances,
            titleText='Smallest distances for all points ' + distCase + ' tracks',
            yAxisTitle='Distances'
        )

        htmlCore.line(lines)
    def visualizeResults(self, results):

        globalResult = results.getGlobalResult()
        res = OrderedDict([(x, globalResult[x]) for x in globalResult
                           if x not in ['Assembly_gap_coverage']])

        dT = dataTransformer(res)
        seriesName, categories, data = dT.changeDictIntoListsByNumber(
            math.sqrt(len(res)))

        vg = visualizationGraphs()
        return vg.drawPieCharts(
            data,
            seriesName=categories,
            addOptions='width: 50%; float:left; margin: 0 auto')
Example #7
0
    def execute(cls, choices, galaxyFn=None, username=''):

        #gsuite
        gsuite = getGSuiteFromGalaxyTN(choices.gsuite)

        #all boxes
        multiPlot = choices.multiPlot
        #scale = choices.scale
        #overlap = choices.overlap
        overlap = 'no'
        bps = int(choices.bps)

        rp = RP(gsuite)

        #get length of chromosomes and ordering
        chrItems = GenomeInfo.getStdChrLengthDict(gsuite.genome)
        chrOrder, chrLength = GenerateRainfallPlotTool.sortChrDict(chrItems)

        dataDict, dataDictLine, elementOrder, listResCopy, listDataCountPerBin, newResBinSizeListSum, chrList = GenerateRainfallPlotTool.countMutations(
            gsuite, chrLength, bps)

        seriesType, newSeriesNameRes, newSeriesNameResOE, yAxisMultiVal = rp.getOptionsForPlot(
            elementOrder, gsuite.allTrackTitles())
        newResList, newResBinSizeList, newResBinSizeListSortedList = rp.generateBinSizeList(
            elementOrder, listResCopy, listDataCountPerBin,
            newResBinSizeListSum)

        vg = visualizationGraphs()

        res = ''
        if multiPlot == 'Single':
            res += GenerateRainfallPlotTool.drawSinglePlot(
                vg, newResBinSizeListSortedList, chrLength, newResList,
                newSeriesNameRes, newResBinSizeList, overlap, seriesType,
                yAxisMultiVal)

        else:
            res += GenerateRainfallPlotTool.drawMultiPlot(
                newResList, newSeriesNameRes, newResBinSizeList, vg,
                seriesType, yAxisMultiVal)

        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.line('Bin size: ' + str(bps))
        htmlCore.line(res)
        htmlCore.end()
        htmlCore.hideToggle(styleClass='debug')
        print htmlCore
Example #8
0
    def getInteractiveColumnChartWithLabels(cls, data, labels, htmlCore):
        htmlCore.divider()
        htmlCore.header('Interactive SNP frequency plot')
        htmlCore.line('To see exact SNP counts for the different tracks, hover over the bars of interest.')
        htmlCore.divEnd()  # End div set in execute to remove font settings

        vg = visualizationGraphs()
        lines = vg.drawColumnChart(
            dataY=data,
            titleText='SNP frequencies in tracks',
            yAxisTitle='SNP count',
            categories=labels,
            xAxisRotation=90
        )

        htmlCore.line(lines)
    def visualizeResults(self, results):

        globalResult = results.getGlobalResult()
        res = OrderedDict([(x, globalResult[x]) for x in globalResult
                           if x not in ['Assembly_gap_coverage', 'BinSize']])

        dT = dataTransformer(res)
        seriesName, categories, data = dT.changeDictIntoListsByTrackString(
            strNum="T", numStart=1, numEnd=int(math.sqrt(len(res) / 2)))
        vg = visualizationGraphs()
        res = vg.drawColumnCharts(
            data,
            categories=categories,
            seriesName=seriesName,
            addOptions='width: 50%; float:left; margin: 0 auto')

        return res
Example #10
0
    def drawPlot(results, additionalResultsDict, title, columnInd=0):

        dictIt = OrderedDict()
        dictIt[title] = []
        categoriesPart = []

        for key0, it0 in results.iteritems():
            dictIt[title].append(it0)
            categoriesPart.append(key0)

        for elC in categoriesPart:
            if additionalResultsDict[elC]:
                for key1, it1 in additionalResultsDict[elC].iteritems():
                    if key1 not in dictIt:
                        dictIt[key1] = []
                    dictIt[key1].append(it1)

        plotName = dictIt.keys()
        res = [it[1] for it in dictIt.items()]
        categories = []
        for i in range(0, len(res)):
            categories.append(categoriesPart)

        # table1 = generateHistogram('elementCount', ['Track', 'Histogram'], tableData1, plotType='columnChart')

        from quick.webtools.restricted.visualization.visualizationGraphs import visualizationGraphs
        vg = visualizationGraphs()
        res = vg.drawColumnCharts(
            res,
            titleText=plotName,
            categories=categories,
            height=500,
            xAxisRotation=270,
            xAxisTitle='Track title',
            yAxisTitle='',
            marginTop=30,
            addTable=True,
            sortableAccordingToTableIndexWithTrackTitle=1 + int(columnInd),
            sortableAccordingToTable=True,
            plotOptions=True,
            legend=False
        )
        return res
    def visualizeResults(self, results):

        globalResult = results.getGlobalResult()
        res = OrderedDict([(x, globalResult[x]) for x in globalResult
                           if x not in ['Assembly_gap_coverage']])

        dT = dataTransformer(res)
        seriesName, categories, data = dT.changeDictIntoList()

        vg = visualizationGraphs()
        res = vg.drawColumnChart(data,
                                 categories=categories,
                                 extraScriptButton=[
                                     OrderedDict({
                                         'Use linear scale':
                                         'linear',
                                         'Use log10 scale':
                                         'logarithmic'
                                     }), 'yAxisType'
                                 ])

        return vg.visualizeResults(res)
    def execute(choices, galaxyFn=None, username=''):

        file = choices.file
        fileHeader = choices.fileHeader
        title = choices.title
        columnX = choices.columnX
        columnY = choices.columnY

        plot = choices.plot
        plotSeries = choices.plotSeries

        columnSorted = choices.columnSorted
        columnFiltered = choices.columnFiltered

        i = 0
        with open(ExternalTrackManager.extractFnFromGalaxyTN(file.split(':')),
                  'r') as f:
            dataX = []
            for x in f.readlines():
                if i == 0 and fileHeader == 'Yes':
                    pass
                else:
                    el = x.strip('\n').split('\t')
                    nr = 0

                    l = []
                    for e in el:
                        if e != '':
                            try:
                                l.append(float(e))
                            except:
                                l.append(e)
                            nr += 1
                    dataX.append(l)
                i += 1
        f.closed

        oryginalList = PlotLevelGSuite.returnList(columnX)

        columnX = PlotLevelGSuite.returnList(columnX, True)
        columnY = PlotLevelGSuite.returnList(columnY, True)

        title = PlotLevelGSuite.returnList(title, True)

        columnSortedList = columnSorted.strip(' ').split(',')
        columnFiltered = columnFiltered.strip(' ').split(',')

        for cNum in range(0, len(columnSortedList)):
            columnSortedList[cNum] = int(columnSortedList[cNum])

        dataX = sorted(dataX,
                       key=operator.itemgetter(*columnSortedList),
                       reverse=False)
        #dataX = sorted(dataX, key=operator.itemgetter(3,4,0,1,2), reverse=False)

        #prepare data for plotting

        final = OrderedDict()

        for dNum in range(0, len(dataX)):

            columnFilteredList = ''
            for eNum in columnFiltered:
                columnFilteredList += str(dataX[dNum][int(eNum)]) + '-'

            if not columnFilteredList in final:

                final[columnFilteredList] = OrderedDict()
                final[columnFilteredList]['title'] = ''

                tit = ''
                j = 0
                for eNum in title:
                    if j < len(title) - 1:
                        tit += str(dataX[dNum][eNum]) + ' '
                    else:
                        tit += str(dataX[dNum][eNum])
                    j += 1

                final[columnFilteredList]['title'] = tit
                final[columnFilteredList]['categories'] = []
                final[columnFilteredList]['data'] = OrderedDict()

            cat = ''
            j = 0
            for eNum in columnX:
                if j < len(columnX) - 1:
                    cat += str(dataX[dNum][eNum]) + ' '
                else:
                    cat += str(dataX[dNum][eNum])
                j += 1
            final[columnFilteredList]['categories'].append(cat)

            for eNum in columnY:
                if not eNum in final[columnFilteredList]['data']:
                    final[columnFilteredList]['data'][eNum] = []
                final[columnFilteredList]['data'][eNum].append(
                    dataX[dNum][eNum])

        seriesName = []
        data = []
        categories = []
        d = []
        cat = []
        title = []
        plotLinesName = []
        plotLines = []
        pl = []
        i = 0

        for k1, v1 in final.iteritems():
            #             d=[]
            for k2, v2 in v1['data'].iteritems():
                #                 d.append(v2)
                if plot == 'Single':
                    data += v2
                seriesName.append([oryginalList[k2].split("--")[0]])

            if plot == 'Single':
                categories += v1['categories']
                plotLinesName.append(k1)

                if i == 0:
                    #plotLines.append([0, len(v1['categories'])])
                    plotLines.append(0)
                else:
                    plotLines.append(plotLines[-1] + len(v1['categories']))
                    #plotLines.append([plotLines[-1][1], plotLines[-1][1]+len(v1['categories'])])

            title.append(v1['title'])

            if plot == 'Multi' and plotSeries == 'Single':
                data.append([v2])
                categories.append(v1['categories'])

            if plot == 'Multi' and plotSeries == 'Multi':

                if len(categories) > 0 and v1['categories'] != categories[
                        len(cat) - 1]:
                    data.append(d)
                    d = [v2]
                    plotLinesName.append(pl)
                    pl = [k1]
                    if not v1['categories'] in categories:
                        categories.append(v1['categories'])
                else:
                    if not v1['categories'] in categories:
                        categories.append(v1['categories'])
                    d.append(v2)
                    pl.append(k1)

            i += 1

        if plot == 'Multi' and plotSeries == 'Multi':
            data.append(d)
            plotLinesName.append(pl)

        vg = visualizationGraphs()
        res = ''

        if plot == 'Single':
            res += vg.drawColumnChart(data,
                                      categories=categories,
                                      xAxisRotation=90,
                                      marginTop=30,
                                      height=300,
                                      plotLines=plotLines,
                                      plotLinesName=plotLinesName,
                                      plotBandsColor=True,
                                      showInLegend=False)

        if plot == 'Multi':
            if plotSeries == 'Multi':

                res += vg.drawColumnCharts(data,
                                           categories=categories,
                                           xAxisRotation=90,
                                           marginTop=30,
                                           height=300,
                                           seriesName=plotLinesName,
                                           minY=0)
            if plotSeries == 'Single':
                res += vg.drawColumnCharts(
                    data,
                    categories=categories,
                    xAxisRotation=90,
                    marginTop=30,
                    height=300,
                    seriesName=seriesName,
                    minY=0,
                    titleText=title,
                )

        print res
Example #13
0
    def execute(cls, choices, galaxyFn=None, username=''):
        #cls._setDebugModeIfSelected(choices)
        # from config.DebugConfig import DebugConfig
        # from config.DebugConfig import DebugModes
        # DebugConfig.changeMode(DebugModes.RAISE_HIDDEN_EXCEPTIONS_NO_VERBOSE)

        # DebugUtil.insertBreakPoint(5678, suspend=False)

        choices_gsuite = choices.gsuite
        selected_metadata = choices.cat
        choices_queryTrack = choices.query
        #genome = 'hg19'
        genome = choices.genome

        queryTS = factory.getSingleTrackTS(genome, choices_queryTrack)
        refTS = factory.getFlatTracksTS(genome, choices_gsuite)

        categoricalTS = refTS.getSplittedByCategoryTS(selected_metadata)

        fullTS = TrackStructureV2()
        fullTS['query'] = queryTS
        fullTS['reference'] = categoricalTS
        spec = AnalysisSpec(SummarizedInteractionPerTsCatV2Stat)

        parameter = 'minLqMedUqMax'

        spec.addParameter('pairwiseStatistic', ObservedVsExpectedStat.__name__)
        spec.addParameter('summaryFunc', parameter)
        bins = UserBinSource('chr1', '*', genome=genome)
        res = doAnalysis(spec, bins, fullTS)
        tsRes = res.getGlobalResult()['Result']

        htmlCore = HtmlCore()
        htmlCore.begin()

        if parameter == 'minAndMax':
            htmlCore.tableHeader(['Track', 'min-max'],
                                 sortable=False,
                                 tableId='tab1')
            for k, it in tsRes.iteritems():
                htmlCore.tableLine([
                    k,
                    str("%.2f" % it.getResult()[0]) + '-' +
                    str("%.2f" % it.getResult()[1])
                ])
            htmlCore.tableFooter()

        if parameter == 'minLqMedUqMax':

            dataList = []
            categories = []
            for keyE, itE in tsRes.iteritems():
                categories.append(keyE)
                dataList.append(list(itE.getResult()))

            from quick.webtools.restricted.visualization.visualizationGraphs import \
                visualizationGraphs
            vg = visualizationGraphs()
            res = vg.drawBoxPlotChart(dataList,
                                      categories=categories,
                                      seriesName=selected_metadata)
            htmlCore.line(res)

        htmlCore.end()
        print htmlCore
    def execute(choices, galaxyFn=None, username=''):

        file = choices.file
        columnX = choices.columnX
        columnY = choices.columnY
        plotType = choices.plotType
        axesScaleX = choices.axesScaleX
        axesScaleY = choices.axesScaleY
        plotSeries = choices.plotSeries

        if axesScaleX == 'linear':
            plotRes = 'combine'
        elif axesScaleX == 'log10':
            plotRes = 'separate'
        elif axesScaleX == 'no uniform scale (sorted values as labels)':
            plotRes = 'separate'

        inputFile = open(
            ExternalTrackManager.extractFnFromGalaxyTN(file.split(':')), 'r')

        dataS = OrderedDict()
        dataS['xAxis'] = OrderedDict()
        dataS['yAxis'] = OrderedDict()
        i = 0

        with inputFile as f:
            for x in f.readlines():
                if i == 0:
                    rowColumn = list(x.strip('\n').split('\t'))
                else:
                    j = 0
                    for el in list(x.strip('\n').split('\t')):
                        #                         if columnX[rowColumn[j]] == 'True':
                        #                             if not rowColumn[j] in dataS['xAxis']:
                        #                                 dataS['xAxis'][rowColumn[j]] = []
                        #                             dataS['xAxis'][rowColumn[j]].append(el)
                        if rowColumn[j] in columnX:
                            if not rowColumn[j] in dataS['xAxis']:
                                dataS['xAxis'][rowColumn[j]] = []
                            dataS['xAxis'][rowColumn[j]].append(el)
                        if rowColumn[j] in columnY and columnY[
                                rowColumn[j]] == 'True':
                            if not rowColumn[j] in dataS['yAxis']:
                                dataS['yAxis'][rowColumn[j]] = []

                            dataS['yAxis'][rowColumn[j]].append(float(el))

                        j += 1
                i += 1
        inputFile.close()

        #        this will be used just for x - values
        #         keysX = dataS['xAxis'].keys()
        #         if keysX == 1:
        #             plotSeries = 'Single'
        #         else:
        #             plotSeries = 'Multi'

        #sorting categories values
        categoriesNumber = False

        sortedCat = None
        categories = None

        if columnX == 'line number':
            categories = None
        else:
            #if columnX['xAxis'] in columnY.keys():
            if columnX in columnY.keys():
                categoriesBefore = [float(v) for v in dataS['xAxis'][columnX]]

                if axesScaleX == 'log10':
                    for cbN in range(0, len(categoriesBefore)):
                        if categoriesBefore[cbN] != 0:
                            categoriesBefore[cbN] = math.log(
                                categoriesBefore[cbN], 10)

                sortedCat = sorted(range(len(categoriesBefore)),
                                   key=lambda k: categoriesBefore[k])
                categories = []
                for n in sortedCat:
                    categories.append(categoriesBefore[n])

                categoriesNumber = True
            else:
                categories = dataS['xAxis'][columnX]

        #dataS are sorted according to numerical values
        seriesName = []
        data = []
        for key, it in columnY.iteritems():
            if it == 'True':
                dataPart = []
                seriesName.append(key)
                dataPart = []

                for x in dataS['yAxis'][key]:
                    try:
                        if axesScaleY == 'log10':
                            if x != 0:
                                dataPart.append(math.log(float(x), 10))
                            else:
                                dataPart.append(0)
                        else:
                            dataPart.append(float(x))
                    except:
                        dataPart.append(x)

                if sortedCat != None:
                    dataPartTemp = []
                    for n in sortedCat:
                        dataPartTemp.append(dataPart[n])
                    dataPart = dataPartTemp
                data.append(dataPart)

        label = ''
        if len(seriesName) != 0:
            label = '<b>{series.name}</b>: {point.x} {point.y}'
        else:
            label = '{point.x} {point.y}'

#         'Column', 'Scatter', 'Heatmap'

        if axesScaleX == 'log10':
            xAxisTitle = str(columnX) + ' (' + str(axesScaleX) + ')'
        else:
            xAxisTitle = str(columnX)

        if axesScaleY == 'log10':
            yAxisTitle = str('values') + ' (' + str(axesScaleY) + ')'
        else:
            yAxisTitle = str('values')

        minFromList = min(min(d) for d in data)
        if minFromList > 0:
            minFromList = 0

        #combain series with data
        if plotRes == 'combine':
            if categoriesNumber == True:
                newData = []
                for d in data:
                    newDataPart = []
                    for cN in range(0, len(categories)):
                        newDataPart.append([categories[cN], d[cN]])
                    newData.append(newDataPart)
                data = newData
                categories = None

        vg = visualizationGraphs()

        res = ''
        if plotSeries == 'Single':
            if plotType == 'Scatter':

                res += vg.drawScatterChart(data,
                                           categories=categories,
                                           xAxisRotation=90,
                                           marginTop=30,
                                           xAxisTitle=xAxisTitle,
                                           yAxisTitle=yAxisTitle,
                                           height=500,
                                           seriesName=seriesName,
                                           label=label,
                                           minY=minFromList)

            if plotType == 'Column':
                res += vg.drawColumnChart(data,
                                          categories=categories,
                                          xAxisRotation=90,
                                          marginTop=30,
                                          xAxisTitle=xAxisTitle,
                                          yAxisTitle=yAxisTitle,
                                          height=500,
                                          seriesName=seriesName,
                                          label=label,
                                          minY=minFromList)

        elif plotSeries == 'Multi':
            if plotType == 'Scatter':
                for nrD in range(0, len(data)):
                    if plotRes == 'combine':
                        data[nrD] = [data[nrD]]
                    res += vg.drawScatterChart(
                        data[nrD],
                        categories=categories,
                        xAxisRotation=90,
                        marginTop=30,
                        xAxisTitle=xAxisTitle,
                        yAxisTitle=yAxisTitle,
                        height=500,
                        seriesName=[seriesName[nrD]],
                        label=label,
                        minY=minFromList
                        #                      titleText = 'Plot',
                    )
            if plotType == 'Column':
                res += vg.drawColumnCharts(
                    data,
                    categories=[categories for x in range(0, len(data))],
                    xAxisRotation=90,
                    marginTop=30,
                    xAxisTitle=xAxisTitle,
                    yAxisTitle=yAxisTitle,
                    height=500,
                    seriesName=[[seriesName[elD]]
                                for elD in range(0, len(data))],
                    label=label,
                    minY=minFromList
                    #                      titleText = 'Plot',
                )

        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.divBegin(divId='results-page')
        htmlCore.divBegin(divClass='results-section')

        htmlCore.line(res)

        htmlCore.divEnd()
        htmlCore.divEnd()
        htmlCore.end()

        print htmlCore
    def execute(choices, galaxyFn=None, username=''):
        '''
        Is called when execute-button is pushed by web-user. Should print
        output as HTML to standard out, which will be directed to a results page
        in Galaxy history. If getOutputFormat is anything else than HTML, the
        output should be written to the file with path galaxyFn. If needed,
        StaticFile can be used to get a path where additional files can be put
        (e.g. generated image files). choices is a list of selections made by
        web-user in each options box.
        '''
#         from gold.application.LogSetup import setupDebugModeAndLogging
        #setupDebugModeAndLogging()

#         targetTrackNames, targetTrackCollection, targetTrackGenome = getGSuiteDataFromGalaxyTN(choices.gSuiteFirst)
#         targetTracksDict = OrderedDict(zip(targetTrackNames, targetTrackCollection))
#         refTrackNames, refTrackCollection, refTrackCollectionGenome = getGSuiteDataFromGalaxyTN(choices.gSuiteSecond)
#         refTracksDict = OrderedDict(zip(refTrackNames, refTrackCollection))
#         
        targetGSuite = getGSuiteFromGalaxyTN(choices.gSuiteFirst)
        refGSuite = getGSuiteFromGalaxyTN(choices.gSuiteSecond)

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)


        if choices.intraOverlap == TrackCollectionsAnalysis.MERGE_INTRA_OVERLAPS:
            analysisDef = 'dummy -> RawOverlapStat'
        else:
            analysisDef = 'dummy [withOverlaps=yes] -> RawOverlapAllowSingleTrackOverlapsStat'
        results = OrderedDict()
#         for targetTrackName, targetTrack in targetTracksDict.iteritems():
#             for refTrackName, refTrack in refTracksDict.iteritems():
        for targetTrack in targetGSuite.allTracks():
            targetTrackName = targetTrack.title
            for refTrack in refGSuite.allTracks():
                refTrackName = refTrack.title
                if targetTrack.trackName == refTrack.trackName:
                    result = TrackCollectionsAnalysis.handleSameTrack(targetTrack.trackName, regSpec, binSpec,
                                                       choices.genome, galaxyFn)
                else:
                    result = GalaxyInterface.runManual([targetTrack.trackName, refTrack.trackName],
                                                       analysisDef, regSpec, binSpec,
                                                       choices.genome, galaxyFn,
                                                       printRunDescription=False,
                                                       printResults=False).getGlobalResult()
                if targetTrackName not in results :
                    results[targetTrackName] = OrderedDict()
                results[targetTrackName][refTrackName] = result

        stat = choices.statistic
        statIndex = STAT_LIST_INDEX[stat]
        title = 'Screening track collections  (' + stat + ')'

        processedResults = []
        headerColumn = []
        for targetTrackName in targetGSuite.allTrackTitles():
            resultRowDict = processRawResults(results[targetTrackName])
            resultColumn = []
            headerColumn = []
            for refTrackName, statList in resultRowDict.iteritems():
                resultColumn.append(statList[statIndex])
                headerColumn.append(refTrackName)
            processedResults.append(resultColumn)

        transposedProcessedResults = [list(x) for x in zip(*processedResults)]

        tableHeader = ['Track names'] + targetGSuite.allTrackTitles()
        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.header(title)
        htmlCore.divBegin('resultsDiv')
        htmlCore.tableHeader(tableHeader, sortable=True, tableId='resultsTable')
        for i, row in enumerate(transposedProcessedResults):
            line = [headerColumn[i]] + [strWithStdFormatting(x) for x in row]
            htmlCore.tableLine(line)
        htmlCore.tableFooter()
        htmlCore.divEnd()

#         #hicharts can't handle strings that contain ' or " as input for series names
        targetTrackNames = [x.replace('\'', '').replace('"','') for x in targetGSuite.allTrackTitles()]
        refTrackNames = [x.replace('\'', '').replace('"','') for x in refGSuite.allTrackTitles()]
# 
#         '''
#         addColumnPlotToHtmlCore(htmlCore, targetTrackNames, refTrackNames,
#                                 stat, title + ' plot',
#                                 processedResults, xAxisRotation = -45, height=800)
#         '''
#         '''
#         addPlotToHtmlCore(htmlCore, targetTrackNames, refTrackNames,
#                                 stat, title + ' plot',
#                                 processedResults, xAxisRotation = -45, height=400)
#         '''
#         
        from quick.webtools.restricted.visualization.visualizationGraphs import visualizationGraphs
        vg = visualizationGraphs()
        result = vg.drawColumnChart(processedResults,
                      height=600,
                      yAxisTitle=stat,
                      categories=refTrackNames,
                      xAxisRotation=90,
                      seriesName=targetTrackNames,
                      shared=False,
                      titleText=title + ' plot',
                      overMouseAxisX=True,
                      overMouseLabelX = ' + this.value.substring(0, 10) +')
        
        htmlCore.line(result)
        #htmlCore.line(vg.visualizeResults(result, htmlCore))
        
        htmlCore.hideToggle(styleClass='debug')
        htmlCore.end()

        print htmlCore
    def execute(choices, galaxyFn=None, username=''):
        
        #data from choices
        gSuite = choices.gSuite
        plotType = choices.plotType
        columnX = choices.columnX
        columnY = choices.columnY
        plotSeries = choices.plotSeries
        
        axesScaleX = choices.axesScaleX 
        axesScaleY = choices.axesScaleY
        
        #'linear', 'log10', 'no uniform scale (sorted values as labels)'
        if axesScaleX == 'linear':
        #plotRes = choices.plotRes
            plotRes = 'combine'
        elif  axesScaleX == 'log10':
            plotRes = 'separate'
        elif axesScaleX == 'no uniform scale (sorted values as labels)':
            plotRes = 'separate'
        
        
        
        #unpack gsuite
        gSuite = getGSuiteFromGalaxyTN(gSuite)
        
        #full list of attributes (meta-data)
        attributeList = gSuite.attributes
        
        #fill list of attributes plus title
        attributeList = [TITLE_COL] + attributeList
        
        #dictNum - include numerical values which can be presented in y-axes
        #need to do that because pie can have only one chocie and then it is not returing dict
        from quick.gsuite.GSuiteUtils import attributesType
        attribute = attributesType(gSuite)

        dictNum=OrderedDict()
        for key, it in attribute.iteritems():
            if it == True:
                dictNum[key] = False
        
        #check if it is dict or not
        if not isinstance(columnY, dict):
            tempDict={}
            tempDict[columnY] = 'True'
            columnY=tempDict
        
        
        seriesName=[]
        
        #check if user selected categoriesNumber and it is possible to use combinate 
        categoriesNumber = False
             
        sortedCat=None
        categories=None
        if columnX == TITLE_COL:
            categories = gSuite.allTrackTitles()
        elif columnX == 'line number':
            categories = None
        else:
            if columnX in dictNum.keys():
                categoriesBefore = [float(v) for v in gSuite.getAttributeValueList(columnX)]
                
                if axesScaleX == 'log10':
                    for cbN in range(0, len(categoriesBefore)):
                        if categoriesBefore[cbN]!=0:
                            categoriesBefore[cbN]=math.log(categoriesBefore[cbN], 10)
                
                sortedCat = sorted(range(len(categoriesBefore)), key=lambda k: categoriesBefore[k])
                categories=[]
                for n in sortedCat:
                    categories.append(categoriesBefore[n])
                
                categoriesNumber=True
                
            else:
                categories = gSuite.getAttributeValueList(columnX)
                
        
        #data are sorted according to numerical values
        data=[]
        for key, it in columnY.iteritems():
            if it == 'True':
                dataPart=[]
                seriesName.append(key)
                dataPart = []
                for x in gSuite.getAttributeValueList(key):
                    try:
                        if axesScaleY == 'log10':
                            if x!=0:
                                dataPart.append(math.log(float(x), 10))
                            else:
                                dataPart.append(0)
                        else:
                            dataPart.append(float(x))
                    except:
                        # need to support None in heatmap
                        if plotType == 'Heatmap':
                            dataPart.append(0)
                        else:
                            dataPart.append(x)
                if sortedCat!=None:
                    dataPartTemp=[]
                    for n in sortedCat:
                        dataPartTemp.append(dataPart[n])
                    dataPart = dataPartTemp
                data.append(dataPart)
                
        
        label=''
        if len(seriesName)!=0:
            label = '<b>{series.name}</b>: {point.x} {point.y}'
        else:
            label = '{point.x} {point.y}'
        
        vg = visualizationGraphs()
        
#         'Column', 'Scatter', 'Heatmap'
        
        if axesScaleX == 'log10':
            xAxisTitle = str(columnX) + ' (' + str(axesScaleX) + ')'
        else:
            xAxisTitle = str(columnX)
        
        if axesScaleY == 'log10':
            yAxisTitle = str('values') + ' (' + str(axesScaleY) + ')'
        else:
            yAxisTitle = str('values')    
        
        minFromList = min(min(d) for d in data)
        if minFromList > 0:
            minFromList = 0
        
        
        #combain series with data
        if plotRes == 'combine':
            if categoriesNumber == True:
                newData=[]
                for d in data:
                    newDataPart=[]
                    for cN in range(0, len(categories)):
                        newDataPart.append([categories[cN], d[cN]])
                    newData.append(newDataPart)
                data=newData
                categories=None
                        
                    
        
        
        res=''
        if plotSeries == 'Single':
            if plotType == 'Scatter':
                res += vg.drawScatterChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     )
            if plotType == 'Pie':
                res += vg.drawPieChart(
                    data[0],
                    seriesName = categories,
                    height = 400,
                    titleText = seriesName[0],
                    )
                
            if plotType == 'Column':
                res += vg.drawColumnChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     )
            if plotType == 'Line':
                res += vg.drawLineChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     )
            if plotType == 'Heatmap':
                res += vg.drawHeatmapSmallChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
#                      titleText = 'Plot',
                     )
        elif plotSeries == 'Multi':
            if plotType == 'Scatter':
                for nrD in range(0, len(data)):
                    if plotRes == 'combine':
                        data[nrD]=[data[nrD]]
                    res += vg.drawScatterChart(
                         data[nrD],
                         categories = categories,
                         xAxisRotation = 90,
                         marginTop = 30,
                         xAxisTitle = xAxisTitle,
                         yAxisTitle = yAxisTitle,
                         height = 500,
                         seriesName = [seriesName[nrD]],
                         label = label,
                         minY=minFromList
    #                      titleText = 'Plot',
                         )
            if plotType == 'Column':
                res += vg.drawColumnCharts(
                     data,
                     categories = [categories for x in range(0, len(data))],
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = [[seriesName[elD]] for elD in range(0, len(data))],
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     ) 
            if plotType == 'Line':
                for nrD in range(0, len(data)):
                    if plotRes == 'combine':
                        data[nrD]=[data[nrD]]
                    res += vg.drawLineChart(
                         data[nrD],
                         categories = categories,
                         xAxisRotation = 90,
                         marginTop = 30,
                         xAxisTitle = xAxisTitle,
                         yAxisTitle = yAxisTitle,
                         height = 500,
                         seriesName = [seriesName[nrD]],
                         label = label,
                         minY=minFromList
    #                      titleText = 'Plot',
                         )                  
        
        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.divBegin(divId='results-page')
        htmlCore.divBegin(divClass='results-section')
        
        htmlCore.line(res)
        
        htmlCore.divEnd()
        htmlCore.divEnd()
        htmlCore.end()
        
        print htmlCore
Example #17
0
    def execute(cls, choices, galaxyFn=None, username=''):
        cls._setDebugModeIfSelected(choices)

        genome = choices.genome
        genomicRegions = choices.genomicRegions
        genomicRegionsTracks = choices.genomicRegionsTracks
        sourceTfs = choices.sourceTfs
        sourceTfsDetails = choices.sourceTfsDetails
        tfTracks = choices.tfTracks

        # Get Genomic Region track name:
        if genomicRegions == cls.REGIONS_FROM_HISTORY:
            galaxyTN = genomicRegionsTracks.split(':')
            genElementTrackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(
                genome, galaxyTN)

            #queryGSuite = getGSuiteFromGalaxyTN(genomicRegionsTracks)
            #queryTrackList = [Track(x.trackName, x.title) for x in queryGSuite.allTracks()]

        elif genomicRegions == 'Hyperbrowser repository':
            selectedGenRegTrack = TfbsTrackNameMappings.getTfbsTrackNameMappings(
                genome)[genomicRegionsTracks]
            if isinstance(selectedGenRegTrack, dict):
                genElementTrackName = selectedGenRegTrack.values()
            else:
                genElementTrackName = selectedGenRegTrack
        elif genomicRegions == 'Hyperbrowser repository (cell-type-specific)':
            genElementTrackName = ['Private', 'Antonio'
                                   ] + genomicRegionsTracks.split(':')
        else:
            return

        # Get TF track names:
        if isinstance(tfTracks, dict):
            selectedTfTracks = [
                key for key, val in tfTracks.iteritems() if val == 'True'
            ]
        else:
            selectedTfTracks = [tfTracks]

        queryTrackTitle = '--'.join(genElementTrackName)

        trackTitles = [queryTrackTitle]
        tracks = [Track(genElementTrackName, trackTitle=queryTrackTitle)]

        for i in selectedTfTracks:
            if sourceTfs == 'Hyperbrowser repository':
                tfTrackName = TfTrackNameMappings.getTfTrackNameMappings(
                    genome)[sourceTfsDetails] + [i]
                tracks.append(
                    Track(tfTrackName,
                          trackTitle=tfTrackName[len(tfTrackName) - 1]))
                trackTitles.append(tfTrackName[len(tfTrackName) - 1])

            else:
                tfTrackName = i.split(':')

                queryGSuite = getGSuiteFromGalaxyTN(sourceTfsDetails)

                for x in queryGSuite.allTracks():
                    selectedTrackNames = (':'.join(x.trackName))
                    if i == selectedTrackNames:
                        tracks.append(Track(x.trackName, x.title))
                        trackTitles.append(x.trackName[-1])

                # queryGSuite = getGSuiteFromGalaxyTN(sourceTfsDetails)
                # tfTrackName = [x.trackName for x in queryGSuite.allTracks()] + [i]
                # tracks += [Track(x.trackName, x.title) for x in queryGSuite.allTracks()]
                # trackTitles += tfTrackName

        # print tfTrackName
        # print tracks
        # print trackTitles

        trackTitlesForStat = trackTitles

        trackTitles = CommonConstants.TRACK_TITLES_SEPARATOR.join(trackTitles)

        ##first statistic for Q2
        resultsForStatistics = OrderedDict()

        similarityFunc = [  #GSuiteStatUtils.T7_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP,
            GSuiteStatUtils.T5_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP
        ]

        for similarityStatClassName in similarityFunc:
            regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)
            analysisBins = GalaxyInterface._getUserBinSource(regSpec,
                                                             binSpec,
                                                             genome=genome)

            mcfdrDepth = AnalysisDefHandler(
                REPLACE_TEMPLATES['$MCFDR$']).getOptionsAsText().values()[0][0]
            analysisDefString = REPLACE_TEMPLATES[
                '$MCFDR$'] + ' -> GSuiteSimilarityToQueryTrackRankingsAndPValuesWrapperStat'
            analysisSpec = AnalysisDefHandler(analysisDefString)
            analysisSpec.setChoice('MCFDR sampling depth', mcfdrDepth)
            analysisSpec.addParameter('assumptions',
                                      'PermutedSegsAndIntersegsTrack_')
            analysisSpec.addParameter(
                'rawStatistic', GSuiteStatUtils.
                PAIRWISE_STAT_LABEL_TO_CLASS_MAPPING[similarityStatClassName])
            analysisSpec.addParameter(
                'pairwiseStatistic', GSuiteStatUtils.
                PAIRWISE_STAT_LABEL_TO_CLASS_MAPPING[similarityStatClassName]
            )  #needed for call of non randomized stat for assertion
            analysisSpec.addParameter('tail', 'more')
            analysisSpec.addParameter('trackTitles',
                                      trackTitles)  #that need to be string
            analysisSpec.addParameter('queryTracksNum', str(len(tracks)))

            results = doAnalysis(analysisSpec, analysisBins,
                                 tracks).getGlobalResult()

            if not similarityStatClassName in resultsForStatistics:
                resultsForStatistics[similarityStatClassName] = {}

            resultsForStatistics[similarityStatClassName] = results

        keyTitle = [
            #'Normalized ratio of observed to expected overlap (normalized Forbes similarity measure)',
            'Ratio of observed to expected overlap (Forbes similarity measure)'
        ]

        # 'Normalized Forbes coefficient: ratio of observed to expected overlap normalized in relation to the reference GSuite',
        # 'Forbes coefficient: ratio of observed to expected overlap'

        keyTitle = [
            #GSuiteStatUtils.T7_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP,
            GSuiteStatUtils.T5_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP
        ]

        resultDict = AllTfsOfRegions.countStatistics(similarityFunc, choices,
                                                     genome, tracks,
                                                     trackTitlesForStat)

        resultDictShow = AllTfsOfRegions.countStatisticResults(
            resultDict, keyTitle, trackTitlesForStat)

        #         print resultsForStatistics
        '''selectedTrackNames = []
        if sourceTfs == 'History (user-defined)':
            if selectedTfTracks.split(":")[1] == "gsuite":
                gSuite = getGSuiteFromGalaxyTN(selectedTfTracks)
                for track in gSuite.allTracks():
                    selectedTrackNames.append(track.trackName)
            else:
                galaxyTN = selectedTfTracks.split(':')
                gRegTrackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, galaxyTN)
                selectedTrackNames.append(gRegTrackName)
        else:'''

        tfNameList = []

        #Intersection between TF Tracks and selected region (Table 1):
        n = 0
        allTargetBins = []
        alltfNames = []
        table1 = []
        for i in selectedTfTracks:
            n = n + 1
            #newGalaxyFn = galaxyFn.split(".")[0] + str(n) + "." + "dat"

            if sourceTfs == 'Hyperbrowser repository':
                tfTrackName = TfTrackNameMappings.getTfTrackNameMappings(
                    genome)[sourceTfsDetails] + [i]
            else:
                tfTrackName = i.split(':')
                tfTrackName.pop(0)
            #tfIntersection.expandReferenceTrack(upFlankSize, downFlankSize)
            tfIntersection = TrackIntersection(genome, genElementTrackName,
                                               tfTrackName, galaxyFn, str(n))

            regFileNamer = tfIntersection.getIntersectedRegionsStaticFileWithContent(
            )
            targetBins = tfIntersection.getIntersectedReferenceBins()

            #regSpec, targetBins = UserBinSelector.getRegsAndBinsSpec(choices)

            tfHits = [i] * len(targetBins)
            fixedTargetBins = [str(a).split(" ")[0] for a in targetBins]
            extendedTargetBins = [
                list(a) for a in zip(fixedTargetBins, tfHits)
            ]
            allTargetBins = allTargetBins + extendedTargetBins
            tfName = i
            alltfNames = alltfNames + [tfName]

            # Save output table:
            tfNameList.append(tfName)
            line = [tfName] + [len(targetBins)] + [
                regFileNamer.getLink('Download bed-file')
            ] + [
                regFileNamer.getLoadToHistoryLink('Send bed-file to History')
            ]
            table1 = table1 + [line]

        # Computing totals:
        fullCase = ','.join(alltfNames)
        firstColumn = [item[0] for item in allTargetBins]
        uniqueAllTargetBins = list(set(firstColumn))

        # Group TFs by bound region:
        d1 = defaultdict(list)
        for k, v in allTargetBins:
            d1[k].append(v)
        allTFTargetBins = dict((k, ','.join(v)) for k, v in d1.iteritems())

        allTFTargetList = []
        fullCaseTFTargetList = []
        for key, value in allTFTargetBins.iteritems():
            allTFTargetList = allTFTargetList + [[key, value]]
            if value == fullCase:
                fullCaseTFTargetList = fullCaseTFTargetList + [[key, value]]

        analysis3 = TrackIntersection.getFileFromTargetBins(
            allTFTargetList, galaxyFn, str(3))
        analysis4 = TrackIntersection.getFileFromTargetBins(
            fullCaseTFTargetList, galaxyFn, str(4))

        # Print output to table:
        title = 'TF targets and co-occupancy of ' + genElementTrackName[
            -1] + ' genomic regions'
        htmlCore = HtmlCore()

        pf = plotFunction(tableId='resultsTable')

        htmlCore.begin()
        htmlCore.header(title)
        htmlCore.divBegin('resultsDiv')

        htmlCore.line(pf.createButton(bText='Show/Hide more results'))

        # htmlCore.tableHeader(['Transcription Factor', 'Normalized ratio of observed to expected overlap (normalized Forbes similarity measure) -- Similarity to genomic regions track', 'Normalized ratio of observed to expected overlap (normalized Forbes similarity measure) -- p-value','Ratio of observed to expected overlap (Forbes similarity measure) -- Similarity to genomic regions track', 'Ratio of observed to expected overlap (Forbes similarity measure) -- p-value', 'Number of TF-Target Track Regions', 'File of TF Target Regions', 'File of TF Target Regions', 'Number of TF-co-occupied Regions', 'File of TF co-occupied Regions', 'File of TF co-occupied Regions', 'Rank of TF co-occupancy motifs', 'Rank of TF co-occupancy motifs'], sortable=True, tableId='resultsTable')

        #previous ordering
        # htmlCore.tableHeader(['Transcription Factor', 'Normalized Forbes index --overlap score',
        #                       'Normalized Forbes index --p-value',
        #                       'Forbes index --overlap score', 'Forbes index --p-value',
        #                       'Number of TF-Target Track Regions', 'File of TF Target Regions',
        #                       'File of TF Target Regions', 'Number of target track regions occupied by this TF',
        #                       'File of TF co-occupied Regions', 'File of TF co-occupied Regions',
        #                       'Rank of TF co-occupancy motifs', 'Rank of TF co-occupancy motifs'],
        #                      sortable=True, tableId='resultsTable')

        htmlCore.tableHeader(
            [
                'Transcription Factor',
                'Number of TF-Target Track Regions',
                'File of TF Track Regions',
                'Number of target track regions occupied by this TF',
                'File of TF Target Regions',
                'Forbes index --overlap score',
                'Forbes index --p-value',
                #'Normalized Forbes index --overlap score', 'Normalized Forbes index --p-value',
                'File of TF co-occupied Regions',
                'Rank of TF co-occupancy motifs'
            ],
            sortable=True,
            tableId='resultsTable')

        # Adding co-occupancy results to table:
        n = 1000
        genRegionNumElements = [
            int(x) for x in getTrackRelevantInfo.getNumberElements(
                genome, genElementTrackName)
        ]

        for key0, it0 in resultsForStatistics.iteritems():
            for el in tfNameList:
                if el not in it0:
                    resultsForStatistics[key0][el] = [None, None]

        resultsPlotDict = {}
        resultPlotCat = []
        resultsPlot = []

        resultsForStatisticsProper = {}
        for key0, it0 in resultsForStatistics.iteritems():
            if not key0 in resultsPlotDict:
                resultsPlotDict[key0] = {}
            resultsPlotPart = []
            for key1, it1 in it0.iteritems():
                resultsPlotPart.append(it1[0])
                if not key1 in resultsForStatisticsProper:
                    resultsForStatisticsProper[key1] = []
                if not key1 in resultsPlotDict[key0]:
                    resultsPlotDict[key0][key1] = None
                for el in it1:
                    resultsForStatisticsProper[key1].append(el)
                resultsPlotDict[key0][key1] = it1[0]

        resultPlotCat.append(tfNameList)
        resultPlotCat.append(tfNameList)

        #resultPlotCatPart = tfNameList

        #         print resultPlotCatPart

        for key0, it0 in resultsPlotDict.iteritems():
            resultsPlotPart = []
            for el in tfNameList:
                if el in it0:
                    resultsPlotPart.append(it0[el])
                else:
                    resultsPlotPart.append(None)
            resultsPlot.append(resultsPlotPart)

        for i in table1:
            thisCaseTFTargetList = []
            for key, value in allTFTargetList:
                if i[0] in value and ',' in value:
                    thisCaseTFTargetList = thisCaseTFTargetList + [[
                        key, value
                    ]]
            n = n + 1

            thisAnalysis = TrackIntersection.getFileFromTargetBins(
                thisCaseTFTargetList, galaxyFn, str(n))

            thisCaseCoCountsList = []
            thing = [x[1] for x in thisCaseTFTargetList]
            for k in list(set(thing)):
                thisCount = thing.count(k)
                thisCaseCoCountsList = thisCaseCoCountsList +  \
                                       [[k, thisCount, 100*float(thisCount)/float(sum(genRegionNumElements)), 100*float(thisCount)/float(len(thisCaseTFTargetList))]]
            thisCaseCoCountsList.sort(key=lambda x: x[2], reverse=True)
            n = n + 1

            thisCoCountsAnalysis = TrackIntersection.getOccupancySummaryFile(
                thisCaseCoCountsList, galaxyFn, str(n))

            thisLine = [len(thisCaseTFTargetList)] + \
            [thisAnalysis.getLink('Download file')] + [thisAnalysis.getLoadToHistoryLink('Send file to History')] + \
            [thisCoCountsAnalysis.getLink('Download file')] + [thisCoCountsAnalysis.getLoadToHistoryLink('Send file to History')]

            newLineI = []
            tfName = i[0]
            newLineI.append(tfName)

            for el in resultsForStatisticsProper[tfName]:
                newLineI.append(el)

            for elN in range(1, len(i)):
                newLineI.append(i[elN])

#             htmlCore.tableLine(i + thisLine)

# htmlCore.tableHeader(['Transcription Factor', 'Normalized Forbes index --overlap score',
#                       'Normalized Forbes index --p-value',
#                       'Forbes index --overlap score', 'Forbes index --p-value',
#                       'Number of TF-Target Track Regions', 'File of TF Target Regions',
#                       'File of TF Target Regions', 'Number of target track regions occupied by this TF',
#                       'File of TF co-occupied Regions', 'File of TF co-occupied Regions',
#                       'Rank of TF co-occupancy motifs', 'Rank of TF co-occupancy motifs'],
#                      sortable=True, tableId='resultsTable')

# htmlCore.tableHeader(['Transcription Factor', 'Number of TF-Target Track Regions', 'File of TF Track Regions',
#                      'Number of target track regions occupied by this TF', 'File of TF Target Regions',
#                      'Forbes index --overlap score', 'Forbes index --p-value',
#                      'Normalized Forbes index --overlap score', 'Normalized Forbes index --p-value',
#                      'File of TF co-occupied Regions', 'Rank of TF co-occupancy motifs'],
#                     sortable=True, tableId='resultsTable')

            tl = newLineI + thisLine
            # previous ordering tl - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
            # actual ordering - 0, 5, 7, 8, 7, 3, 4, 1, 2, 9, 11

            #ordering  = [0, 5, 7, 8, 10, 3, 4, 1, 2, 10, 12]
            ordering = [0, 3, 5, 6, 8, 1, 2, 8, 10]

            #1, 2, => delete

            eoList = []
            for eo in ordering:
                eoList.append(tl[eo])

            htmlCore.tableLine(eoList)

        totalCoOccupancyTargetList = []
        n = 2000
        for key, value in allTFTargetList:
            n = n + 1
            if ',' in value:
                totalCoOccupancyTargetList = totalCoOccupancyTargetList + [[
                    key, value
                ]]
        #newGalaxyFn = galaxyFn.split(".")[0] + str(n) + "." + "dat"
        totalCoOccupancyAnalysis = TrackIntersection.getFileFromTargetBins(
            totalCoOccupancyTargetList, galaxyFn, str(n))
        #line = ['Total reported regions'] + [len(allTargetBins)] + [''] + [''] + [''] + [''] + ['']

        #line = ['Full co-occupancy of ' + fullCase] + ['-'] + ['-'] + ['-'] + ['-'] + ['-'] + ['-'] + ['-'] + [len(fullCaseTFTargetList)] + [analysis4.getLink('Download file')] + [analysis4.getLoadToHistoryLink('Send file to History')] + ['-'] + ['-']

        line = ['Full co-occupancy of ' + fullCase] + \
               ['-'] + \
               ['-'] + \
               [len(fullCaseTFTargetList)] + \
               ['-'] + \
               ['-'] + \
               ['-'] + \
               [analysis4.getLoadToHistoryLink('Send file to History')] + \
               ['-']

        htmlCore.tableLine(line)
        #line = ['Total unique regions'] + ['-'] + ['-'] + ['-'] + ['-']  + [len(allTFTargetList)] + [analysis3.getLink('Download bed-file')] + [analysis3.getLoadToHistoryLink('Send bed-file to History')] + [len(totalCoOccupancyTargetList)] + [totalCoOccupancyAnalysis.getLink('Download file')] + [totalCoOccupancyAnalysis.getLoadToHistoryLink('Send file to History')] + ['-'] + ['-']

        line = ['Total unique regions'] + \
               [len(allTFTargetList)] + \
               ['-'] + \
               [len(totalCoOccupancyTargetList)] + \
               [analysis3.getLoadToHistoryLink('Send bed-file to History')] + \
               ['-'] +\
               ['-'] + \
               [totalCoOccupancyAnalysis.getLoadToHistoryLink('Send file to History')] + \
               ['-']

        htmlCore.tableLine(line)

        htmlCore.tableFooter()
        htmlCore.divEnd()

        # htmlCore.line(pf.hideColumns(indexList=[2, 4]))
        #

        sumRes = 0
        for r in resultsPlot[0]:
            if r != None:
                sumRes += r

        if sumRes != 0:
            vg = visualizationGraphs()
            result = vg.drawColumnCharts(
                [resultsPlot[0]],
                height=300,
                categories=resultPlotCat,
                legend=False,
                addOptions='width: 90%; float:left; margin: 0 4%;',
                #titleText=['Overlap between TFs and genomic region using normalized Forbes', 'Overlap between TFs and genomic region using Forbes'],
                titleText=[
                    'Overlap between TFs and genomic region using Forbes'
                ],
                xAxisRotation=90,
                xAxisTitle='TF',
                yAxisTitle='value')

            htmlCore.line(result)

        for key0, it0 in resultDictShow.iteritems():
            htmlCore.divBegin('resultsDiv' + str(key0))
            htmlCore.header(key0)
            htmlCore.tableHeader(it0[0],
                                 sortable=True,
                                 tableId='resultsTable' + str(key0))

            for elN in range(1, len(it0)):
                htmlCore.tableLine(it0[elN])

            htmlCore.tableFooter()
            htmlCore.divEnd()

        htmlCore.hideToggle(styleClass='debug')

        htmlCore.end()
        print htmlCore
Example #18
0
    def execute(cls, choices, galaxyFn=None, username=''):
        cls._setDebugModeIfSelected(choices)

        targetGSuite = getGSuiteFromGalaxyTN(choices.gSuiteFirst)
        refGSuite = getGSuiteFromGalaxyTN(choices.gSuiteSecond)

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)

        analysisDef = 'dummy -> RawOverlapStat'
        # analysisDef = 'dummy [withOverlaps=yes] -> RawOverlapAllowSingleTrackOverlapsStat'
        results = OrderedDict()

        for targetTrack in targetGSuite.allTracks():
            targetTrackName = targetTrack.title
            for refTrack in refGSuite.allTracks():
                refTrackName = refTrack.title
                if targetTrack.trackName == refTrack.trackName:
                    # print targetTrack.title
                    # print targetTrack.trackName
                    result = DetermineSuiteTracksCoincidingWithAnotherSuite.handleSameTrack(
                        targetTrack.trackName, regSpec, binSpec,
                        targetGSuite.genome, galaxyFn)
                else:
                    result = GalaxyInterface.runManual(
                        [targetTrack.trackName, refTrack.trackName],
                        analysisDef,
                        regSpec,
                        binSpec,
                        targetGSuite.genome,
                        galaxyFn,
                        printRunDescription=False,
                        printResults=False,
                        printProgress=False).getGlobalResult()
                if targetTrackName not in results:
                    results[targetTrackName] = OrderedDict()
                results[targetTrackName][refTrackName] = result

        stat = STAT_OVERLAP_COUNT_BPS
        statIndex = STAT_LIST_INDEX[stat]
        title = ''

        processedResults = []
        headerColumn = []
        for targetTrackName in targetGSuite.allTrackTitles():
            resultRowDict = processRawResults(results[targetTrackName])
            resultColumn = []
            headerColumn = []
            for refTrackName, statList in resultRowDict.iteritems():
                resultColumn.append(statList[statIndex])
                headerColumn.append(refTrackName)
            processedResults.append(resultColumn)

        outputTable = {}
        for elN in range(0, len(headerColumn)):
            outputTable[elN] = {}
            outputTable[elN]['id'] = headerColumn[elN]

        transposedProcessedResults = [list(x) for x in zip(*processedResults)]

        # second question sumSecondgSuite
        # first question numSecondgSuite
        # fifth question numSecondgSuitePercentage
        for i in range(0, len(transposedProcessedResults)):
            outputTable[i]['sumSecondgSuite'] = sum(
                transposedProcessedResults[i])
            if not 'numSecondgSuite' in outputTable[i]:
                outputTable[i]['numSecondgSuite'] = 0
            for j in range(0, len(transposedProcessedResults[i])):
                if transposedProcessedResults[i][j] >= 1:
                    outputTable[i]['numSecondgSuite'] += 1
                else:
                    outputTable[i]['numSecondgSuite'] += 0
            outputTable[i]['numSecondgSuitePercentage'] = float(
                outputTable[i]['numSecondgSuite']) / float(
                    targetGSuite.numTracks()) * 100

        from gold.statistic.CountSegmentStat import CountSegmentStat
        from gold.statistic.CountPointStat import CountPointStat
        from gold.description.TrackInfo import TrackInfo
        from gold.statistic.CountStat import CountStat

        # third question numPairBpSecondgSuite
        # fourth question numFreqBpSecondgSuite
        i = 0
        for refTrack in refGSuite.allTracks():
            formatName = TrackInfo(refTrack.genome,
                                   refTrack.trackName).trackFormatName
            analysisDef = CountStat
            analysisBins = GalaxyInterface._getUserBinSource(
                regSpec, binSpec, refTrack.genome)
            results = doAnalysis(AnalysisSpec(analysisDef), analysisBins,
                                 [PlainTrack(refTrack.trackName)])
            resultDict = results.getGlobalResult()
            if len(resultDict) == 0:
                outputTable[i]['numPairBpSecondgSuite'] = None
                outputTable[i]['numFreqBpSecondgSuite'] = None
                outputTable[i]['numFreqUniqueBpSecondgSuite'] = None
            else:
                outputTable[i]['numPairBpSecondgSuite'] = resultDict['Result']

                if outputTable[i]['numPairBpSecondgSuite'] != 0:
                    outputTable[i]['numFreqBpSecondgSuite'] = float(
                        outputTable[i]['sumSecondgSuite']) / float(
                            outputTable[i]['numPairBpSecondgSuite'])
                else:
                    outputTable[i]['numFreqBpSecondgSuite'] = None

                if outputTable[i]['sumSecondgSuite'] != 0:
                    outputTable[i]['numFreqUniqueBpSecondgSuite'] = float(
                        outputTable[i]['numPairBpSecondgSuite']) / float(
                            outputTable[i]['sumSecondgSuite'])
                else:
                    outputTable[i]['numFreqUniqueBpSecondgSuite'] = None

            i += 1

        # sortTable
        outputTableLine = []
        for key, item in outputTable.iteritems():
            line = [
                item['id'], item['numSecondgSuite'], item['sumSecondgSuite'],
                item['numPairBpSecondgSuite'], item['numFreqBpSecondgSuite'],
                item['numFreqUniqueBpSecondgSuite'],
                item['numSecondgSuitePercentage']
            ]
            outputTableLine.append(line)

        import operator
        outputTableLineSort = sorted(outputTableLine,
                                     key=operator.itemgetter(1),
                                     reverse=True)

        tableHeader = [
            'Region ID ', 'Number of cases with at least one event ',
            'Total number of events', 'Genome coverage (unique bp)',
            'Number of events per unique bp', 'Number of unique bp per event',
            'Percentage of cases with at least one event'
        ]
        htmlCore = HtmlCore()

        htmlCore.begin()

        htmlCore.line(
            "<b>Identification of genomic elements with high event recurrence</b> "
        )

        htmlCore.header(title)
        htmlCore.divBegin('resultsDiv')
        htmlCore.tableHeader(tableHeader,
                             sortable=True,
                             tableId='resultsTable')

        for line in outputTableLineSort:
            htmlCore.tableLine(line)

        plotRes = []
        plotXAxis = []
        for lineInx in range(1, len(outputTableLineSort[0])):
            plotResPart = []
            plotXAxisPart = []
            for lineInxO in range(0, len(outputTableLineSort)):
                # if outputTableLineSort[lineInxO][lineInx]!=0 and
                # if outputTableLineSort[lineInxO][lineInx]!=None:
                plotResPart.append(outputTableLineSort[lineInxO][lineInx])
                plotXAxisPart.append(outputTableLineSort[lineInxO][0])
            plotRes.append(plotResPart)
            plotXAxis.append(plotXAxisPart)

        htmlCore.tableFooter()
        htmlCore.divEnd()

        htmlCore.divBegin('plot', style='padding-top:20px;margin-top:20px;')

        vg = visualizationGraphs()
        res = vg.drawColumnCharts(
            plotRes,
            titleText=tableHeader[1:],
            categories=plotXAxis,
            height=500,
            xAxisRotation=270,
            xAxisTitle='Ragion ID',
            yAxisTitle='Number of cases with at least one event',
            marginTop=30,
            addTable=True,
            sortableAccordingToTable=True,
            legend=False)
        htmlCore.line(res)
        htmlCore.divEnd()

        htmlCore.hideToggle(styleClass='debug')
        htmlCore.end()

        print htmlCore