Esempio n. 1
0
    def getHtmlResultsTable(self):

        headerTab = [
            'TF', 'Chip-seq peaks: ', 'PWM',
            'Number of SNV-intersected binding regions',
            'Highest binding difference', 'Avg binding difference',
            'Number of regions with binding difference', 'Original Fasta',
            'Mutated Fasta', 'PWM score for each region',
            'Gtrack of PWM score for each region',
            'BED of PWM score for each region'
        ]
        core = HtmlCore()
        core.begin()
        core.tableHeader(headerTab, sortable=True)
        for tfObj in self.values():
            if True:  #hasattr(tfObj,'maxPwmDiff'):
                core.tableLine([
                    tfObj.tf, tfObj.chipSeqPeaks, tfObj.pwm,
                    tfObj.intersectingPoints, tfObj.maxPwmDiff,
                    tfObj.avgPwmDiff, tfObj.numPwmDiff,
                    tfObj.regularFasta.getLink('Original Fasta'),
                    tfObj.mutatedFasta.getLink('Mutated Fasta'),
                    tfObj.pwmDiffScore.getLink('PWM score for each region'),
                    tfObj.gtrackDiffScore.getLink(
                        'Gtrack of PWM score for each region'),
                    tfObj.bedPwmDiffScore.getLink(
                        'BED of PWM score for each region')
                ])

        core.tableFooter()
        core.end()
        return str(core)
Esempio n. 2
0
    def ParseDatasetsXmlDoc(xmlDoc):
        core = HtmlCore()
        core.tableHeader([
            'Dataset Name', 'Description', 'Created', 'Owner', 'Size',
            'Qty files in Dataset', 'State'
        ],
                         sortable=True)
        for dataset in xmlDoc.getElementsByTagName('DataSet'):
            datasetName = DomUtility.getNodeValue(dataset, 'Name', 'str')
            owner = DomUtility.getNodeValue(dataset, 'Owner', 'str')
            dateCreated = DomUtility.getNodeValue(dataset, 'Created', 'date')
            size = sum([
                int(size.childNodes[0].nodeValue)
                for size in dataset.getElementsByTagName('Size')
            ])
            description = DomUtility.getNodeValue(dataset, 'Description',
                                                  'str')
            numOfFilesInDataset = len(dataset.getElementsByTagName('Resource'))
            state = DomUtility.getNodeValue(dataset, 'State', 'str')
            core.tableLine([
                datasetName, description, dateCreated, owner,
                str(size),
                str(numOfFilesInDataset), state
            ])

        return str(core)
 def printHtml(cols,rows,colListString,outFile):
     
     colListString = colListString.replace('_','').upper()
     colListString = colListString.replace('"','')
     
     
     from proto.hyperbrowser.HtmlCore import HtmlCore
     core = HtmlCore()
     core.begin()
     
     core.tableHeader(colListString.split(','),sortable = True)
     
     if len(TrackFileImport3.SELECTED_FILES_INDEXES)>0:
        arr = TrackFileImport3.SELECTED_FILES_INDEXES
     else:
        arr = range(len(rows))
     for i in arr:
         row = rows[i]
         if row == None or len(row)<len(cols):
            continue
         url = str(row[0])
         filename = url.split('/')[-1]
         link = HtmlCore().link(filename,url)
         row = list(row)
         row[0] = link
         core.tableLine([str(x) for x in row])
         
     core.tableFooter()
     core.end()
     outFile.write(str(core))
         
     outFile.close()
Esempio n. 4
0
    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.
        '''

        resultsFN = ExternalTrackManager.extractFnFromGalaxyTN(
            choices.resultsFile)
        examResults = TaskScoreOverview(resultsFN, galaxyFn)
        examResults.run()
        core = HtmlCore()
        core.begin()
        core.header('Overview of exam scores')
        for table in examResults.getTables():
            core.divBegin(divClass='resultsTable')
            core.tableHeader([])
            for key, val in table.iteritems():
                core.tableLine([key, val])
            core.tableFooter()
            core.divEnd()

        for plotUrl in examResults.getPlotUrls():
            core.divBegin(divClass='plot')
            core.image(plotUrl)
            core.divEnd()
        core.end()
        print core
Esempio n. 5
0
 def getHtmlPwmTable(self, lineTab):
     headerTab = [
         'chrom', 'start', 'end', 'max PWM difference',
         'best reference seq_PWM score -> corresponding mut seq score',
         'best mut seq PWM score -> corresponding_ref seq score',
         'ref region', 'ref seq', 'corresponding mut seq', 'mut region',
         'mut seq', 'corresponding ref seq'
     ]
     core = HtmlCore()
     core.begin()
     core.tableHeader(headerTab, sortable=True)
     for row in lineTab:
         if True:  #hasattr(tfObj,'maxPwmDiff'):
             core.tableLine(row)
     core.tableFooter()
     core.end()
     return str(core)
    def getHtmlLocalResultsTable(self,
                                 resDictKey=None,
                                 fillInNoneValues=False):
        allLocalResults = self.getAllLocalResults(resDictKey, fillInNoneValues)
        allLocalRegions = self.getLocalRegions()
        allRefSubTypes = self.getRefSubTypes()

        core = HtmlCore()
        core.tableHeader(['-'] + allRefSubTypes, sortable=True)
        #for refSubType in allLocalResults.keys():
        #    core.tableLine([refSubType] + [allLocalResults[refSubType][localRegion] for localRegion in localRegions])
        for localRegion in allLocalRegions:
            core.tableLine([localRegion] + [
                allLocalResults[refSubType][localRegion]
                for refSubType in allRefSubTypes
            ])
        core.tableFooter()
        #core.end()
        return str(core)
Esempio n. 7
0
    def execute(cls, 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 quick.application.GalaxyInterface import GalaxyInterface
        from proto.hyperbrowser.HtmlCore import HtmlCore

        print GalaxyInterface.getHtmlBeginForRuns(galaxyFn)
        print GalaxyInterface.getHtmlForToggles(withRunDescription=False)
        print str(HtmlCore().styleInfoBegin(styleClass='debug'))

        from quick.application.ExternalTrackManager import ExternalTrackManager
        datasetId = ExternalTrackManager.extractIdFromGalaxyFn(ExternalTrackManager.extractFnFromGalaxyTN(choices[0]))[1]
        #print datasetId
        #print choices

        ##batchid = index for kjoering i batch. Ikke-batch-kjoering: 0
        #batchid = 0

        ##mapId = Definer hva slags type innhold i Google map-bokser (naar du trykker). Innholdet er definert (hardkodet) i GoogleMapsInterface.py: getHtmlText() og getClusterHtmlText()
        ##Eks: encode_tf_vs_tf
        if choices[1] != 'make new map id':
            mapId = choices[1]
        else:
            mapId = choices[2]
            from quick.application.ExternalTrackManager import ExternalTrackManager
            histHtml = open(ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(':'))).read()

            #rowTrackName = histHtml.split('<h3>TRACK 1</h3>')[-1].split('</b>')[1].split('\n')[0].strip()#.split(':')
            #colTrackName = histHtml.split('<h3>TRACK 2</h3>')[-1].split('</b>')[1].split('\n')[0].strip()#.split(':')

            from quick.application.GalaxyInterface import GalaxyInterface

            batchLine = histHtml.split('<h3>CORRESPONDING BATCH COMMAND LINE</h3>')[-1].split('<p>')[1].split('/p')[0].strip()
            genome = histHtml.split('<h3>GENOME</h3>')[-1].split('name:</b>')[1].split('<')[0].strip()
            fullAccess = GalaxyInterface.userHasFullAccess(username)

            from quick.batch.BatchRunner import BatchRunner
            batchContents = BatchRunner.parseBatchLine(batchLine, genome, fullAccess)
            rowTrackName = ':'.join(batchContents.cleanedTrackName1)
            colTrackName = ':'.join(batchContents.cleanedTrackName2)

            from quick.extra.CreateGoogleMapType import createMapType
            createMapType(mapId, genome, rowTrackName, colTrackName, 'None', 'None', 'None')

            from config.Config import HB_SOURCE_CODE_BASE_DIR
            GMapInterfaceFn = HB_SOURCE_CODE_BASE_DIR+'/quick/extra/GoogleMapsInterface.py'
            GMapInterfaceContent = open(GMapInterfaceFn).read()
            replaceStr = "'%s', 'encode_gwas_vs_dhs'" % mapId
            GMapInterfaceContent = GMapInterfaceContent.replace("'encode_gwas_vs_dhs'", replaceStr)
            open(GMapInterfaceFn,'w').write(GMapInterfaceContent)

        #title = navn paa Regulomet slik brukeren ser det, Eks: "ENCODE test, TFs vs TFs"
        title = choices[3]

        ##name = navn paa katalog for google map: Eks: encode_test_tf_vs_tf
        name = choices[4]

        from quick.extra.CreateGoogleMap import GoogleMapCreator
        from quick.extra.GoogleMapsInterface import Map


        onMedSeqProd = False
        #batchid
        creator = GoogleMapCreator(name, datasetId, mapId, genome=genome)
        creator.tile(onMedSeqProd)
        creator.copyResultFiles()
        creator.createResultShelves()
        creator.createIndexFile(title, genome)
        creator.fixPermissions()

        print str(HtmlCore().styleInfoEnd())

        map = Map(name)

        row = []
        row.append( str(HtmlCore().link(map.getPrettyName(), map.getUrl())) )
        row.append( str(HtmlCore().link('Run description', map.getRunDescriptionUrl())) )
        row.append( str(HtmlCore().link('Counts', map.getCountUrl())) )
        row.append( str(HtmlCore().link('Effect size', map.getEffectSizeUrl())) )
        row.append( str(HtmlCore().link('P-values', map.getPvalUrl())) )

        core = HtmlCore()
        core.tableHeader(None)
        core.tableLine(row)
        core.tableFooter()

        print core
        print GalaxyInterface.getHtmlEndForRuns()
    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.
        '''
        genome = choices.genome
        targetTrack = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(
            genome,
            choices.targetTrack,
            printErrors=False,
            printProgress=False)

        refGSuite = getGSuiteFromGalaxyTN(choices.refTrackCollection)

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)

        analysisBins = GalaxyInterface._getUserBinSource(regSpec,
                                                         binSpec,
                                                         genome=genome)

        results = TrackReportCommon.getOverlapResultsForTrackVsCollection(
            genome, targetTrack, refGSuite, analysisBins=analysisBins)
        processedResults = TrackReportCommon.processRawResults(results)

        targetTrackTitle = prettyPrintTrackName(targetTrack)
        title = 'Screening of track ' + targetTrackTitle
        sortedProcessedResultsTupleList = sorted(
            processedResults.iteritems(),
            key=lambda x: x[1][STAT_LIST_INDEX[STAT_FACTOR_OBSERVED_VS_EXPECTED
                                               ]],
            reverse=True)
        refTrackNames = [x[0] for x in sortedProcessedResultsTupleList]
        refTrackNames = [
            x.replace('\'', '').replace('"', '') for x in refTrackNames
        ]
        plotData = [x[1] for x in sortedProcessedResultsTupleList]
        #         plotData = zip(*plotData) #invert
        plotData = normalizeMatrixData(plotData)

        printVals = tuple([str(targetTrackTitle)]) + tuple(
            [str(x[0]) for x in sortedProcessedResultsTupleList[0:3]])

        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.header(title)

        if choices.bmQid and choices.bmQid not in ['None']:
            htmlCore.append(
                str(
                    quick.gsuite.GSuiteHbIntegration.
                    getAnalysisQuestionInfoHtml(choices.bmQid)))

        htmlCore.divBegin('resultsDiv')
        htmlCore.paragraph('''
            The query track <b>%s</b> overlaps most strongly (is most highly enriched) with the tracks <b>%s</b>, <b>%s</b> and <b>%s</b> 
            from the selected collection. See below for a full (ranked) table of overlap and enrichment.
        ''' % printVals)

        htmlCore.paragraph('''
        The coverage of the query track is %s bps.
        ''' % strWithNatLangFormatting(
            TrackReportCommon.getQueryTrackCoverageFromRawOverlapResults(
                results)))

        htmlCore.tableHeader(TrackReportCommon.HEADER_ROW,
                             sortable=True,
                             tableId='resultsTable')
        for refTrackName, refTrackResults in sortedProcessedResultsTupleList:
            line = [refTrackName
                    ] + [strWithNatLangFormatting(x) for x in refTrackResults]
            htmlCore.tableLine(line)
        htmlCore.tableFooter()
        htmlCore.divEnd()
        '''

        addColumnPlotToHtmlCore(htmlCore, 
                                refTrackNames,  
                                TrackReportCommon.HEADER_ROW[1:], 
                                'stat', 'Results plot (data is normalized for better visual comparison) ', 
                                plotData, xAxisRotation = 315)
        '''
        addPlotToHtmlCore(
            htmlCore,
            refTrackNames,
            TrackReportCommon.HEADER_ROW[1:],
            'stat',
            'Results plot (data is normalized for better visual comparison) ',
            plotData,
            xAxisRotation=315)

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

        print htmlCore
Esempio n. 9
0
    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)
        assert targetTrackGenome == refTrackCollectionGenome, 'Reference genome must be the same one in both GSuite files.'
        refTracksDict = OrderedDict(zip(refTrackNames, refTrackCollection))

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)

        analysisDef = 'dummy -> RawOverlapStat'
        results = OrderedDict()
        for targetTrackName, targetTrack in targetTracksDict.iteritems():
            for refTrackName, refTrack in refTracksDict.iteritems():
                result = GalaxyInterface.runManual([targetTrack, refTrack],
                                                   analysisDef,
                                                   regSpec,
                                                   binSpec,
                                                   targetTrackGenome,
                                                   galaxyFn,
                                                   printRunDescription=False,
                                                   printResults=False)
                if targetTrackName not in results:
                    results[targetTrackName] = OrderedDict()
                results[targetTrackName][
                    refTrackName] = result.getGlobalResult()

        targetTrackTitles = results.keys()

        stat = choices.statistic
        statIndex = STAT_LIST_INDEX[stat]
        title = stat + ' analysis of track collections'

        processedResults = []
        headerColumn = []
        for targetTrackName in targetTrackTitles:
            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'] + targetTrackTitles
        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]] + row
            htmlCore.tableLine(line)
        htmlCore.tableFooter()
        htmlCore.divEnd()

        addColumnPlotToHtmlCore(htmlCore,
                                targetTrackNames,
                                refTrackNames,
                                stat,
                                title + ' plot',
                                processedResults,
                                xAxisRotation=315)

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

        print htmlCore
Esempio n. 10
0
    def _buildHtml(self, done):
        htmlCore = HtmlCore()
        htmlCore.begin(reloadTime=RELOAD_TIME)
        htmlCore.divBegin(divId='progress')

        runningTimeStr = self._getRunningTimeStr()
        htmlCore.header(runningTimeStr)

        remainingTime, unknown = self._estimateRemainingTime()
        timeRemainingStr = self._getEstimatedTimeRemainingStr(
            remainingTime, unknown)

        if unknown:
            if remainingTime > 0:
                timeRemainingStr += '+'

        htmlCore.header(timeRemainingStr)

        nameCellColSpan = 4  #colspan for the first cell that displays the process name

        for progressObj in self._progressObjList:
            htmlCore.tableHeader([], tableClass='progress')
            htmlCore.tableRowBegin(rowClass='progressRow')
            htmlCore.tableCell(progressObj.name, colSpan=nameCellColSpan)

            #             for i in range(progressObj.status):
            #                 content = ''
            #                 if i == int(progressObj.elementCount / 2):
            #                     content = "%0.2f" % float(progressObj.status) / progressObj.elementCount  * 100
            #                 if i == int(progressObj.elementCount / 2 + 1):
            #                     content = '%'
            #                 htmlCore.tableCell(content, cellClass='progressCellDone')
            #
            #             for i in range(progressObj.status, progressObj.elementCount):
            #                 content = ''
            #                 if i == int(progressObj.elementCount / 2):
            #                     content = "%0.2f" % float(progressObj.status) / progressObj.elementCount  * 100
            #                 if i == int(progressObj.elementCount / 2 + 1):
            #                     content = '%'
            #                 htmlCore.tableCell(content, cellClass='progressCell')

            for i in range(progressObj.elementCount):
                content = ''
                if i == int(progressObj.elementCount / 2):
                    content = "%0.2f" % (float(progressObj.status) /
                                         progressObj.elementCount * 100)
                if i == int(progressObj.elementCount / 2 + 1):
                    content = '%'
                cellCls = 'progressCellDone' if i < progressObj.status else 'progressCell'
                htmlCore.tableCell(content, cellClass=cellCls)

            htmlCore.tableRowEnd()
            htmlCore.tableFooter()

            estimatedRemainingTime = progressObj.estimateRemainingTime()
            unknown = estimatedRemainingTime == UNKNOWN_TIME_REMAINING
            progressObjInfo = self._getEstimatedTimeRemainingStr(
                estimatedRemainingTime, unknown)
            htmlCore.paragraph(progressObjInfo)

        htmlCore.divEnd()
        htmlCore.end(stopReload=done)
        return htmlCore
    def executePairDistance(cls, genome, tracks, track_names, clusterMethod,
                            extra_option, feature, extra_feature, galaxyFn,
                            regSpec, binSpec):
        from proto.RSetup import r
        silenceRWarnings()
        #jobFile = galaxyFn

        if feature is not None:  # must use "" here because the '' does not work

            l = len(tracks)
            d_matrix = np.zeros((l, l))
            for i in range(l):
                for j in range(l):
                    if i < j:
                        if extra_feature == "1 minus the ratio":
                            d_matrix[
                                i,
                                j] = 1 - ClusteringExecution.computeDistance(
                                    genome, tracks[i], tracks[j], feature,
                                    regSpec, binSpec, galaxyFn)
                            d_matrix[j, i] = d_matrix[i, j]
                        elif extra_feature == "1 over the ratio":
                            d_matrix[
                                i,
                                j] = 1 / ClusteringExecution.computeDistance(
                                    genome, tracks[i], tracks[j], feature,
                                    regSpec, binSpec, galaxyFn)
                            d_matrix[j, i] = d_matrix[i, j]
                        else:
                            d_matrix[i,
                                     j] = ClusteringExecution.computeDistance(
                                         genome, tracks[i], tracks[j], feature,
                                         regSpec, binSpec, galaxyFn)
                            d_matrix[j, i] = d_matrix[i, j]

            jobFile = open(galaxyFn, 'w')
            print >> jobFile, '<h3>Results for the "direct sequence-level similarity" way of clustering<h3/><br/><br/>'
            figure = GalaxyRunSpecificFile(
                ['cluster_tracks_result_figure.pdf'], galaxyFn
            )  #this figure is runspecific and is put in the directory
            distMatrix = GalaxyRunSpecificFile(['distance_matrix_result.html'],
                                               galaxyFn)
            distMatrixPath = distMatrix.getDiskPath(True)
            with open(distMatrixPath, 'w') as distObj:
                distTable = d_matrix.tolist()
                core = HtmlCore()
                core.tableHeader([''] + track_names, firstRow=True)
                for index, row in enumerate(distTable):
                    core.tableLine([track_names[index]] +
                                   [str(v) for v in row])
                core.tableFooter()
                print >> distObj, str(core)

            figurepath = figure.getDiskPath(True)
            #r.pdf(figurepath, 8, 8)
            r.assign('track_names', track_names)
            r.assign('d_matrix', d_matrix)
            r('row.names(d_matrix) <- track_names')

            r('d <- as.dist(d_matrix)')
            if clusterMethod == 'Hierarchical clustering' and extra_option != "--select--":
                cls._clusterAndPlotDendrogram(figurepath, extra_option, 'd',
                                              'd_matrix', track_names)
                #r.assign('extra_option',extra_option)
                #r('hr <- hclust(d, method=extra_option, members=NULL)')
                #r('hr$height <- hr$height/max(hr$height)*10')
                #r('plot(hr, ylab="Distance", hang=-1)')

            #r('dev.off()')
            batchRun = GalaxyRunSpecificFile(['batch_run_job.txt'], galaxyFn)
            with open(batchRun.getDiskPath(ensurePath=True), 'w') as batchFile:
                print >> batchFile, '$clusterByPairDistance', (
                    genome, '$'.join([':'.join(t) for t in tracks
                                      ]), ':'.join(track_names), clusterMethod,
                    extra_option, feature, extra_feature, regSpec, binSpec)
            print >> jobFile, batchRun.getLink(
                'View batch script line for this analysis <br/>')
            #print>>jobFile, 'Batch script syntax for this analysis:<br>$clusterByPairDistance', (genome, '$'.join([':'.join(t) for t in tracks]), ':'.join(track_names)  , clusterMethod, extra_option, feature, extra_feature, regSpec, binSpec), '<br><br>'
            print >> jobFile, figure.getLink(
                'View the clustering tree (dendrogram) for this analysis <br>')
            print >> jobFile, distMatrix.getLink(
                'View the distance matrix for this analysis <br>')
Esempio n. 12
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
    def execute_batch(cls, choices, galaxyFn=None, username=''):
        print GalaxyInterface.getHtmlBeginForRuns(galaxyFn)
        html = HtmlCore()
        html.header('Batch run results')

        refSnps = cls.get_ref_snp(choices)
        #print refSnps

        batchMal = "$Tool[hb_variant_melting_profiles](" + '|'.join(
            ["'%s'"] * len(choices)) + ")"
        cmdList = []
        for rs in refSnps:
            #if len(rs[4]) > 1:
            #    rs = list(rs)
            #    rs[4] = list(rs[4])[0]
            #    rs = tuple(rs)
            fakeChoices = (choices.genome, 'Single',
                           '__batch__') + rs + choices[8:]
            #print rs
            cmdList.append(batchMal % fakeChoices)

        #print cmdList
        GalaxyInterface.runBatchLines(cmdList,
                                      galaxyFn,
                                      username=username,
                                      printResults=False,
                                      printProgress=True)
        #print HtmlCore().styleInfoEnd()

        results_tsv = GalaxyRunSpecificFile(['results.tsv'], galaxyFn)
        results = results_tsv.getFile()
        dir = os.path.dirname(results_tsv.getDiskPath())
        for i in range(0, len(cmdList)):
            header = True
            ri = 0
            for resultline in open(os.path.join(dir, str(i), 'results.tsv')):
                if header:
                    header = False
                    if i == 0:
                        headertxt = '#run\t' + resultline
                        results.write(headertxt)
                        html.tableHeader(headertxt.split('\t'))
                else:
                    results.write(str(i) + '\t' + resultline)
                    if resultline.count('?') == 0:
                        link = '<a href="%d/html/chart-%d.html">%d (graph)</a>' % (
                            i, ri, i)
                    else:
                        link = str(i)
                    html.tableLine([link] + resultline.split('\t'))
                    ri += 1

        results.close()
        html.tableFooter()

        # XXX: temp fix for HB/stable bug
        if URL_PREFIX == '/hb':
            print '</div>'

        print '<p><b>' + results_tsv.getLink('Download results') + '</b></p>'
        print html
        print GalaxyInterface.getHtmlEndForRuns()
    def execute(cls, choices, galaxyFn=None, username=''):
        cls._setDebugModeIfSelected(choices)

        genome = choices.genome
        genomicRegionsSource = choices.genomicRegionsSource
        genomicRegions = choices.genomicRegions
        #upFlankSize = int(choices.upFlankSize)
        #downFlankSize = int(choices.downFlankSize)
        sourceTfs = choices.sourceTfs
        tfTracks = choices.tfTracks

        # Get TF track name:
        if sourceTfs == cls.REGIONS_FROM_HISTORY:
            galaxyTN = tfTracks.split(':')
            tfTrackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(
                genome, galaxyTN)
        else:
            tfTrackName = TfTrackNameMappings.getTfTrackNameMappings(
                genome)[sourceTfs] + [tfTracks]

        # Get Genomic Regions track names:
        selectedTrackNames = []

        if isinstance(genomicRegions, dict):
            selectedGenRegions = [
                key for key, val in genomicRegions.iteritems() if val == 'True'
            ]
        else:
            selectedGenRegions = genomicRegions

        if genomicRegionsSource == 'Hyperbrowser repository (single tracks)':
            for i in selectedGenRegions:
                selectedTrackNames.append(
                    TfbsTrackNameMappings.getTfbsTrackNameMappings(genome)[i])
        elif genomicRegionsSource == 'Hyperbrowser repository (cell-specific multi-tracks)':
            for i in selectedGenRegions:
                genElementGSuiteName = TfbsGSuiteNameMappings.getTfbsGSuiteNameMappings(
                    genome)[i]
                gSuite = getGSuiteFromGSuiteFile(genElementGSuiteName)
                for track in gSuite.allTracks():
                    selectedTrackNames.append(track.trackName)
        elif genomicRegionsSource == 'History (user-defined)':
            if genomicRegions.split(":")[1] == "gsuite":
                gSuite = getGSuiteFromGalaxyTN(selectedGenRegions)
                for track in gSuite.allTracks():
                    selectedTrackNames.append(track.trackName)
            else:
                galaxyTN = selectedGenRegions.split(':')
                gRegTrackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(
                    genome, galaxyTN)
                selectedTrackNames.append(gRegTrackName)
        else:
            return

        #Intersection:
        title = 'Targets of ' + tfTrackName[-1] + ' TF track'
        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.header(title)
        htmlCore.divBegin('resultsDiv')
        htmlCore.tableHeader([
            'Genomic Region', 'Number of Target Regions',
            'Download bed file of Target Regions', 'Send bed file to history'
        ],
                             sortable=True,
                             tableId='resultsTable')

        n = 0
        allTargetBins = []
        dataY = []
        allRefSetNames = []
        #print 'all:', selectedTrackNames, '<p>'
        #print 'tf:', tfTrackName, '<p>'
        for i in selectedTrackNames:
            n = n + 1
            #newGalaxyFn = galaxyFn.split(".")[0] + str(n) + "." + "dat"

            tfIntersection = TrackIntersection(genome, i, tfTrackName,
                                               galaxyFn, str(n))
            #tfIntersection.expandReferenceTrack(upFlankSize, downFlankSize)
            regFileNamer = tfIntersection.getIntersectedRegionsStaticFileWithContent(
            )
            targetBins = tfIntersection.getIntersectedReferenceBins()
            '''print 'Target Bins = ', targetBins, '<p>'
            if genomicRegionsSource=='Hyperbrowser repository (single tracks)':
                print '\"', tfTracks, '\" in \"', ":".join((i[len(i)-2],i[len(i)-1])), '":<p>'
            elif genomicRegionsSource=='History (user-defined)':
                print '\"', tfTracks, '\" in \"', i[len(i)-1], '":<p>'
            else:
                listGenRegion = i[0].split(":")
                maxIndex = len(listGenRegion)-1
                print '\"', tfTracks, '\" in \"', ":".join((listGenRegion[maxIndex-1],listGenRegion[maxIndex])), '":<p>'
            print '<p>Number of Targets = ', len(targetBins), 'regions.</p>'
            print '<p>', regFileNamer.getLink('Download bed-file'), ' of all regions with 1 or more hits.</p>'
            print '<p>', regFileNamer.getLoadToHistoryLink('Download bed-file to History'), ' of all regions with 1 or more hits.</p>'
            print '<p>==============================================</p>'
            #with open(galaxyFn, 'w') as outFile:
                #print>>outFile, 'TargetBins=', targetBins, '<p>'
                #print >>outFile, selectedGenRegions, '<p>' '''
            # Collect all target bins and data to plot:
            allTargetBins = allTargetBins + targetBins
            dataY = dataY + [
                TrackIntersection.prepareDataForPlot(genome, targetBins)
            ]
            refSetName = i[len(i) - 1]
            allRefSetNames = allRefSetNames + [refSetName]

            # Print output to table:
            line = [refSetName] + [len(targetBins)] + [
                regFileNamer.getLink('Download bed-file')
            ] + [
                regFileNamer.getLoadToHistoryLink(
                    'Download bed-file to History')
            ]
            #print line, '<p>'
            htmlCore.tableLine(line)

        line = ['Total'] + [len(allTargetBins)] + [''] + ['']
        dataY = dataY + [
            TrackIntersection.prepareDataForPlot(genome, allTargetBins)
        ]
        allRefSetNames = allRefSetNames + ['Total']

        htmlCore.tableLine(line)
        htmlCore.tableFooter()
        htmlCore.divEnd()
        htmlCore.hideToggle(styleClass='debug')
        htmlCore.end()
        print htmlCore
        #print 'ALL Target Bins = ', allTargetBins, '<p>'
        #print 'dataY = ', dataY, '<p>'

        # Plot:
        if genome == 'hg19':
            chrNames = [
                'chr1', 'chr2', 'chr3', 'chr4', 'chr5', 'chr6', 'chr7', 'chr8',
                'chr9', 'chr10', 'chr11', 'chr12', 'chr13', 'chr14', 'chr15',
                'chr16', 'chr17', 'chr18', 'chr19', 'chr20', 'chr21', 'chr22',
                'chrX', 'chrY'
            ]
        if genome == 'mm9':
            chrNames = [
                'chr1', 'chr2', 'chr3', 'chr4', 'chr5', 'chr6', 'chr7', 'chr8',
                'chr9', 'chr10', 'chr11', 'chr12', 'chr13', 'chr14', 'chr15',
                'chr16', 'chr17', 'chr18', 'chr19', 'chrX', 'chrY'
            ]

        titleText = 'Targets per Chromosome'
        dataX = [[dataY[i][j] for i in range(len(dataY))]
                 for j in range(len(dataY[0]))]
        seriesType = ['column'] * len(dataX)
        categories = allRefSetNames
        yAxisTitle = 'Number of Targets'
        seriesName = chrNames
        shared = False
        legend = True
        xAxisRotation = 0
        #print 'dataX = ', dataX, '<p>'

        htmlCore = HtmlCore()
        htmlCore.begin()
        title = 'Targets of ' + tfTrackName[-1] + ' TF track per chromosome'
        htmlCore.header(title)
        htmlCore.line('<a href="#" id="linkContainer1">Click to see plot</a>')
        htmlCore.divBegin(divId='plotDiv', style=' margin: 0 auto')
        htmlCore.line(vp.addJSlibs())
        htmlCore.line(vp.useThemePlot())
        htmlCore.line(vp.addJSlibsExport())
        plot = vp.drawChart(dataX,
                            type='column',
                            legend=legend,
                            height=600,
                            xAxisRotation=xAxisRotation,
                            seriesType=seriesType,
                            seriesName=seriesName,
                            shared=shared,
                            titleText=titleText,
                            overMouseAxisX=True,
                            categories=categories,
                            showChartClickOnLink=True)
        htmlCore.line(plot)
        htmlCore.divEnd()
        htmlCore.end()
        print htmlCore
    def getToolDescription(cls):
        '''
        Specifies a help text in HTML that is displayed below the tool.
        '''

        core = HtmlCore()
        core.paragraph('This tool is used to specify a set of analyses to be run in sequence, defined via a set of ' + \
                       str(HtmlCore().emphasize('batch command lines')) + '.')
        core.paragraph('Batch command lines can be found by ' +\
                       'clicking the "Inspect parameters of this analysis" link, either from the "Analyze genomic tracks" tool ' +\
                       'interface, or from the history element of previously run analyses. These batch run lines can then ' +\
                       'be copied to this tool and duplicated or modified if needed.')
        core.paragraph('The batch command lines are executed in sequence, that is, ' +\
                       'each batch command line is started only after the previous line has been fully executed.')

        core.divider()
        core.smallHeader('Batch command line format')
        core.styleInfoBegin(styleClass='debug')
        core.append('genome|regions|binsize|track1|track2|statistic')
        core.styleInfoEnd()
        core.descriptionLine('genome', 'The short name of the reference genome for the analysis (can be found in ' +\
                                        'the genome info box of the "Analyze genomic tracks" tool)', emphasize=True)
        core.descriptionLine('regions', 'The regions where the analysis should be performed, in the following format:' +\
                                        cls._exampleText('seqid[:start-[end]],seqid[:start-[end]],...') +\
                                        str(HtmlCore().descriptionLine('seqid', 'The sequence id of the region, e.g. "chr1" for chromosome 1', \
                                                                       emphasize=True)) +\
                                        str(HtmlCore().descriptionLine('start', 'The start position of the region. If the start position ' +\
                                                                                'is omitted, the region starts at posision 1.', emphasize=True)) +\
                                        str(HtmlCore().descriptionLine('seqid', 'The end position of the region. If the end position is ' +\
                                                                                'omitted, the region ends at the end of the specified ' +\
                                                                                'sequence (e.g. chromosome 1).', emphasize=True)) +\
                                        'Note:' + str(HtmlCore().unorderedList( \
                                            ['All positions are 1-based, and end-inclusive (i.e. the first base pair ' +\
                                                'of the sequence has position 1, and the end position is included in the region). ', \
                                             str(HtmlCore().emphasize('k')) + ' and ' + str(HtmlCore().emphasize('m')) +\
                                                ' can be used for specifying thousand (kilo) and million (mega) base pairs, ' +\
                                                'respectively (e.g. "chr1:1k-2k" corresponds to "chr1:1001-2000").', \
                                             '* denotes all (standard) sequences of the reference genome, e.g. all chromosomes'])) +\
                                        'Examples: ' +\
                                        cls._exampleText('chr1,chr2\nchr1:1001-1500\nchr2:1m-2m,chr2:3m-\n*'), emphasize=True)
        core.descriptionLine('binsize', 'The regions are further divided into smaller bins of this size. ' +\
                                        str(HtmlCore().indent( \
                                        'Note:' + str(HtmlCore().unorderedList( \
                                            [str(HtmlCore().emphasize('k')) + ' and ' + str(HtmlCore().emphasize('m')) +\
                                                ' denotes thousand and million base pairs, respectively', \
                                             '* denotes that the regions are not subdivided.'])) +\
                                        'Examples: ' +\
                                        cls._exampleText('5000\n100k\n*'))), emphasize=True)
        core.descriptionLine('track1', 'The first track of the analysis. ' +\
                                        str(HtmlCore().indent( \
                                        'Note:' + str(HtmlCore().unorderedList( \
                                            ['Colon, ":", is used to separate the different levels of the track hierarchy', \
                                             str(HtmlCore().link('URL-encoding', 'http://www.w3schools.com/tags/ref_urlencode.asp')) + \
                                                ' is supported, as non-ASCII characters will not work', \
                                            'A special format, starting with "galaxy", is used to represent a track from history'])) +\
                                        'Examples: ' +\
                                        cls._exampleText('Genes and gene subsets:Genes:CCDS\n' +\
                                                                  'Sequence:Repeating%20elements:SINE\ngalaxy:bed:%2Fusit%2Finvitro' +\
                                                                  '%2Fdata%2Fgalaxy%2Fgalaxy-dist-hg-stable%2Fdatabase%2Ffiles%2F028' +\
                                                                  '%2Fdataset_28276.dat:3%20-%20Extract%20track'))), emphasize=True)
        core.descriptionLine('track2', 'The second track of the analysis, specified in the same way as the first track. ' +\
                                        'If only a single track is to be analyzed, this field should be left empty', emphasize=True)
        core.descriptionLine('statistic', 'The specification of the analysis to run, and its parameters, in the following format:' +
                                        str(HtmlCore().indent( \
                                        cls._exampleText('statisticClass(paramA=valueA,paramB=valueB,...)') +\
                                        'The exact specification possibilities, with different statistic classes and parameter values, ' +\
                                        'are diverse and extensive. However, one may easily find a particular specification by ' +\
                                        'specifying the analysis and parameters in the "Analyze genomic tracks" tool and clicking the ' +\
                                        '"Inspect parameters of this analysis" link.')), emphasize=True)

        core.divider()
        core.smallHeader('Region specification variants')
        core.paragraph(
            'Other specifications of analysis regions are also supported, using both "regions" and "binsize" fields, as follows:'
        )
        core.tableHeader([
            'regions',
            'binsize (example)',
            'description',
        ])
        core.tableLine([cls._exampleText('__brs__'), cls._exampleText(''), \
            'Use the bounding region of the track if only one track is specified, else use the intersection '
            'of the bounding regions of the two tracks'])
        core.tableLine([cls._exampleText('__chrs__'), cls._exampleText('chr1,chr2'), \
            'List of complete sequence ids of the reference genome, e.g. chromosome names'])
        core.tableLine([cls._exampleText('__chrArms__'), cls._exampleText('chr1q,chr2p'), \
            'List of chromosome arm names. (Note: not supported for all reference genomes)'])
        core.tableLine([cls._exampleText('__genes__'), cls._exampleText('ENSG00000208234,ENSG00000199674'), \
            'List of Ensembl gene ids. (Note: not supported for all reference genomes)'])
        core.tableLine([''.join([cls._exampleText(x) for x in ['bed','gff','wig','bedgraph','gtrack']]), \
                        cls._exampleText('/usit/invitro/data/galaxy/galaxy-dist-hg-stable/database/files/028/dataset_28276.dat'), \
                        'Internal path to a file of the specified format, containing custom regions'])
        core.tableFooter()

        core.divider()
        core.smallHeader('Multiple run expansion')
        core.paragraph('The batch command line format supports two options for automatic expansion of a single batch ' +\
                       'command line into multiple batch lines, thus supporting multiple analyses from a single line:')
        core.orderedList(['Multiple values of the ' + str(HtmlCore().emphasize('track1')) + ', ' + str(HtmlCore().emphasize('track2')) + ', and' +\
                          str(HtmlCore().emphasize('statistic')) + ' fields can be specified using the slash character, "/", as ' +\
                          'separator. If more than one field is specified this way, all combinations of the values are expanded and ' +\
                          'executed. Example expansion:' + \
                          cls._exampleText('hg18|*|*|Genes and gene subsets:Genes:CCDS/Genes and gene subsets:Genes:Refseq||ProportionCountStat()/CountPointAllowingOverlapStat()') + \
                          'This expands to the following lines internally:' + \
                          cls._exampleText('hg18|*|*|Genes and gene subsets:Genes:CCDS||ProportionCountStat()\n' +\
                                                    'hg18|*|*|Genes and gene subsets:Genes:CCDS||CountPointAllowingOverlapStat()\n' +\
                                                    'hg18|*|*|Genes and gene subsets:Genes:Refseq||ProportionCountStat()\n' +\
                                                    'hg18|*|*|Genes and gene subsets:Genes:Refseq||CountPointAllowingOverlapStat()'),
                          'All tracks under a specific hierarchy can be specified using the "*" character. This works for ' +\
                          'values of the ' + str(HtmlCore().emphasize('track1')) + ' and ' + str(HtmlCore().emphasize('track2')) +\
                          ' fields. This expansion is also combinatorical, in the same manner as with the "/" character, and can ' +\
                          'be freely combined with such notation. Example:' + \
                          cls._exampleText('hg19|*|*|Genes and gene subsets:Genes:*|Chromatin:Chromatin State Segmentation:wgEncodeBroadHmmK562HMM:*|DerivedOverlapStat()') +\
                          'This batch command line will result in 30 analyses (2 gene tracks * 15 chromatin tracks).'])
        core.divider()
        core.smallHeader('Defining variables')
        core.paragraph('The batch command line format allows definition of variables using the following format:' +\
                       cls._exampleText('@variable=value') +\
                       'To use a variable, simply enter the variable name with a starting "@" character in a batch ' +\
                       'command line. Example:' +\
                       cls._exampleText('@trackname=Genes and gene subsets:Genes:CCDS\nhg18|*|*|@trackname||ProportionCountStat()') +\
                       'Nested variable declarations, i.e. defining a variable using previously defined variables, are also allowed. Example:' +\
                       cls._exampleText('@TN1=Genes and gene subsets:Genes:CCDS\n' +\
                                  '@TN2=Genes and gene subsets:Genes:Refseq\n' +\
                                  '@TNs=@TN1/@TN2\n' +\
                                  'hg18|*|*|@TNs||ProportionCountStat()/CountPointAllowingOverlapStat()') +\
                       'Note:' +\
                        str(HtmlCore().unorderedList(['The variable names are case sensitive', \
                                                      '"=" characters are allowed in variable values'])))

        return str(core)
Esempio n. 16
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
Esempio n. 17
0
    def execute(cls, 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.
        '''

        genome = choices[0]
        regSpec = '__chrs__'
        binSpec = '*'
        if choices[6] == 'Chromosome arms':
            regSpec = '__chrArms__'
        elif choices[6] == 'Track from history...':
            #put in history bins support here
            #print choices[4:]
            regSpec = ExternalTrackManager.extractFileSuffixFromGalaxyTN(choices[7].split(':'))
            binSpec = ExternalTrackManager.extractFnFromGalaxyTN(choices[7].split(':'))
            #print 'regSpec, binSpec,', regSpec, binSpec
            lineList, counter, tooManyBins = [], 0, False
            for line in open(binSpec):
                if line.strip() !='':
                    if counter == cls.MAX_NUM_ROWS:
                        tooManyBins = True
                        break
                    lineList.append(line)
                    counter+= 1 if line.strip()[0] !='#' else 0

            if tooManyBins:
                newHist = GalaxyRunSpecificFile(['newHistFile.%s' % regSpec], galaxyFn)
                binSpec = newHist.getDiskPath(ensurePath=True)
                open(binSpec, 'w').write(''.join(lineList))

        print GalaxyInterface.getHtmlBeginForRuns(galaxyFn)
        print GalaxyInterface.getHtmlForToggles(withRunDescription=False)

        core = HtmlCore()
        core.styleInfoBegin(styleClass='debug')

        figImage = GalaxyRunSpecificFile(['VizTrackOnGenome.png'], galaxyFn)
        #StaticImage(['VizTrackOnGenome.png'])
        analysisDef = ' [normalizeRows=%s] [centerRows=%s]  -> RawVisualizationDataStat' % \
            (choices[4] == 'Scale to same size', choices[5] == 'Center')

        if choices[1] == 'HyperBrowser repository':
            trackName = choices[2].split(':')
        else:
            trackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, choices[3].split(':'))

        res = GalaxyInterface.runManual([trackName], analysisDef, regSpec, binSpec, genome, username=username, printResults=False, printHtmlWarningMsgs=False)

        core.styleInfoEnd()
        core.line('')

        core.tableHeader(None)
        #visPresenter = RawVisualizationPresenter(res, galaxyFn,'')#os.path.split()[0]
        #htmlStreng = visPresenter.getReference('Result', fullImage=True)
        rScript = cls.customRExecution(res, figImage.getDiskPath(ensurePath=True), '')

        figUrl = figImage.getURL()
        figLinkText ='<img src="%s" alt="Figure" height="%i" width="800"/>' % (figUrl, 20 *min(cls.MAX_NUM_ROWS, len(res)))
        core.tableLine([figImage.getLink(figLinkText)])

        rScriptGalaxyFile = GalaxyRunSpecificFile(['RScript.R'], galaxyFn)
        with open(rScriptGalaxyFile.getDiskPath(ensurePath=True), 'w') as rScriptFile:
            rScriptFile.write(rScript)

        core.tableLine([rScriptGalaxyFile.getLink('R script')])

        core.tableFooter()

        print core
        print GalaxyInterface.getHtmlEndForRuns()
Esempio n. 18
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(cls, choices, galaxyFn=None, username=''):
        path = str(URL_PREFIX)
        dataset = choices.dataset
        genome = choices.genome
        text = choices.newtrack
        secondDataset = choices.newdataset
        inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(dataset),
                         'r')
        with inputFile as f:
            data = [x for x in f.readlines()]
        silenceRWarnings()
        binSourceParam = '*'
        regSourceParam = '*'
        trackNamePrep = cls.preprocessTrack(genome, dataset)

        if text == 'No':

            figUrl = ''
            if (len(data) > 30000):

                core = HtmlCore()
                core.styleInfoBegin(styleClass='debug')
                figImage = GalaxyRunSpecificFile(['VizTrackOnGenome.png'],
                                                 galaxyFn)
                analysisDef = ' [normalizeRows=%s] [centerRows=%s]  -> RawVisualizationDataStat'

                res = GalaxyInterface.runManual([trackNamePrep],
                                                analysisDef,
                                                regSourceParam,
                                                binSourceParam,
                                                genome,
                                                username=username,
                                                printResults=False,
                                                printHtmlWarningMsgs=False)
                core.styleInfoEnd()
                core.line('')
                core.tableHeader(None)
                rScript = VisualizeTrackPresenceOnGenome.customRExecution(
                    res, figImage.getDiskPath(ensurePath=True), '')
                figUrl = figImage.getURL()
                print GalaxyInterface.getHtmlEndForRuns()
                binSourceParam = '10m'
                regSourceParam = '*'
                cls.resultPrintGeneric(genome, binSourceParam, regSourceParam,
                                       figUrl, path, trackNamePrep)

            else:
                if isinstance(trackNamePrep[0], (list, )):
                    numTracks = len(trackNamePrep[0])
                    firstTrack = cls.prepareTracknameForURL(trackNamePrep[0])
                    trackTitle = json.dumps(trackNamePrep[1])
                    cls.resultPrintGSuite(genome, binSourceParam,
                                          regSourceParam, figUrl, path,
                                          firstTrack, trackTitle, numTracks)
                else:
                    firstTrack = cls.prepareTracknameForURL(trackNamePrep)
                    cls.resultPrintGeneric(genome, binSourceParam,
                                           regSourceParam, figUrl, path,
                                           firstTrack)
        else:
            trackName2 = cls.preprocessTrack(genome, secondDataset)
            firstTrack = cls.prepareTracknameForURL(trackNamePrep)
            secondTrack = cls.prepareTracknameForURL(trackName2)
            cls.resultPrintOverlap(genome, binSourceParam, regSourceParam,
                                   path, firstTrack, secondTrack)
    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
Esempio n. 21
0
    def getToolDescription():
        core = HtmlCore()
        core.paragraph('This tool is used to create GTrack files from any tabular input file. The '
                       'user must select the column names for the table, enabling the GTrack '
                       'header expander to automatically expand the headers, effectively converting '
                       'the file to a GTrack file. Custom column names are also supported.')
        core.divider()

        core.smallHeader('The following column names are part of the GTrack specification')
        core.descriptionLine('seqid', "An identifier of the underlying sequence of "
                                      "the track element (i.e. the row). Example: 'chr1'", emphasize=True)
        core.descriptionLine('start', 'The start position of the track element', emphasize=True)
        core.descriptionLine('end', 'The end position of the track element', emphasize=True)
        core.descriptionLine('value', 'A value associated to the track element. '
                                      'The value type is automatically found by the tool.', emphasize=True)
        core.descriptionLine('strand', "The strand of the track element, either '+', '-' or '.'", emphasize=True)
        core.descriptionLine('id', "An unique identifier of the track element, e.g. 'a'", emphasize=True)
        core.descriptionLine('edges', "A semicolon-separated list of id's, representing "
                                      "edges from this track element to other elements. "
                                      "Weights may also be specified. Example: 'a=1.0;b=0.9'", emphasize=True)
        core.paragraph("See the 'Show GTrack specification' tool for more information.")
        core.divider()

        core.smallHeader('Column selection method')
        core.paragraph('The tool supports two ways of selecting column names. First, you can select '
                       'the column names manually. The other option is to select a GTrack file in the '
                       'the history. The tool will then use the same column names (only using the first '
                       'columns if the number of columns in the current tabular file is less than in the '
                       'GTrack file.')
        core.divider()

        core.smallHeader('Genome')
        core.paragraph("Some GTrack files require a specified genome to be valid, e.g. if bounding regions "
                       "are specified without explicit end coordinates. A genome build must thus be selected if "
                       "such a GTrack file is to be used as template file for column specification. "
                       "Also, auto-correction of the sequence id ('seqid') column requires the selection of a "
                       "genome build. The resulting GTrack file in the history will be associated with the "
                       "selected genome.")
        core.divider()

        core.smallHeader('Track type')
        core.paragraph('According to the columns selected, the tool automatically finds the '
                       'corresponding track type according to the GTrack specification. '
                       'Note that dense track types are noe supported yet byt this tool.')
        core.divider()

        core.smallHeader('Indexing standard')
        core.paragraph('Two common standards of coordinate indexing are common in bioinformatics. A track '
                       'element covering the first 10 base pairs of chr1 are represented in two ways:' )
        core.descriptionLine('0-indexed, end exclusive', "seqid=chr1, start=0, end=10", emphasize=True)
        core.descriptionLine('1-indexed, end inclusive', "seqid=chr1, start=1, end=10", emphasize=True)
        core.paragraph('The GTrack format supports both standards, but the user must inform the system '
                       'which standard is used for each particular case.')
        core.divider()

        core.smallHeader('Auto-correction of sequence id')
        core.paragraph("The tool supports auto-correction of the sequence id ('seqid') column. "
                       "If this is selected, a search is carried out on the sequence id's defined "
                       "for the current genome build. The nearest match, if unique, is inserted in "
                       "the new GTrack file. If no unique match is found, the original value is "
                       "used. The algorithm also handles roman numbers. Example: 'IV' -> 'chr4'")
        core.divider()

        core.smallHeader('Example')
        core.paragraph('Input table')
        core.tableHeader(['start','','id','something','seqid'])
        core.tableLine(['100','.','a','yes','chr1'])
        core.tableLine(['250','.','b','yes','chr1'])
        core.tableLine(['120','.','c','no','chr2'])
        core.tableFooter()

        core.paragraph('Output file')
        core.styleInfoBegin(styleClass='debug')
        core.append(
'''##gtrack version: 1.0
##track type: points
##uninterrupted data lines: true
##sorted elements: true
##no overlapping elements: true
###seqid  start  id	 something
chr1	  100	 a	 yes
chr1	  250	 b	 yes
chr2	  120	 c	 no''')
        core.styleInfoEnd()

        return str(core)