예제 #1
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)
        selectedTasks = [key for key, val in choices.tasks.iteritems() if val]

        examAnalysis = TaskCorrelationAnalysis(resultsFN, galaxyFn)
        examAnalysis.run(selectedTasks)

        core = HtmlCore()
        core.begin()
        for plotUrl in examAnalysis.getPlotUrls():
            core.divBegin(divId='plot')
            core.image(plotUrl)
            core.divEnd()
        core.end()
        print core
    def execute(cls, choices, galaxyFn=None, username=''):

        cls._setDebugModeIfSelected(choices)

        gSuite = getGSuiteFromGalaxyTN(choices.gsuite)
        if gSuite.genome != choices.genome:
            gSuite.setGenomeOfAllTracks(choices.genome)
        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)
        paragraphs = []
        paragraphs += generatePilotPageTwoParagraphs(gSuite,
                                                     galaxyFn,
                                                     regSpec=regSpec,
                                                     binSpec=binSpec)
        paragraphs += generatePilotPageThreeParagraphs(gSuite,
                                                       galaxyFn,
                                                       regSpec=regSpec,
                                                       binSpec=binSpec)

        core = HtmlCore()
        core.begin()
        core.divBegin(divId='results-page')
        core.divBegin(divClass='results-section')
        core.header('Similarity and uniqueness of tracks')
        for prg in paragraphs:
            core.paragraph(prg)
        core.divEnd()
        core.divEnd()
        core.end()

        print core
예제 #3
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
예제 #4
0
 def getOptionsBoxExplainOutput(prevChoices):
     core = HtmlCore()
     core.divBegin(divClass='input-explanation')
     core.paragraph(
         """Select 'gsuite' for output to get a new GSuite with the results as a metadata column
     <br> or select 'html' to view a simple table of the results.""")
     core.divEnd()
     return '__rawstr__', str(core)
    def getOptionsBoxResultsExplanation(cls, prevChoices):
        core = HtmlCore()
        core.divBegin(divClass='resultsExplanation')

        core.paragraph('''
            The results page from this analysis will display the results in a table with one row per track.
            For your convenience you can include attribute (meta-data columns) from the selected GSuite to be displayed in the same table.
            Additionally you can select the leading column, which is by default the track title, but can also be any of the additional attributes you select here.
        ''')
        core.divEnd()

        return '__rawstr__', str(core)
 def createDescription(toolDescription=None, stepsToRunTool=None, toolResult=None, limitation=None):
     core = HtmlCore()
     
     if toolDescription!=None or stepsToRunTool!=None or toolResult!=None or limitation!=None:
         core.divBegin(divId='decription-page')
         core.divBegin(divClass='decription-section')
         core.header('Description')  
         
         #small description of tool (The resaon of creating the tool)
         if toolDescription!=None:
             core.divBegin(divClass='decription-section-main')
             core.paragraph(toolDescription)
             core.divEnd()
 
         #how to use tool
         if stepsToRunTool!=None:
             core.paragraph('To run the tool, follow these steps:')
             core.orderedList(stepsToRunTool)
 
         #what is the result of tool
         if toolDescription!=None:
             core.divBegin(divClass='decription-section-main')
             core.paragraph(toolResult)
             core.divEnd()
         
         #what are the limitation for tool
 #         if limitation:
 #             limits...
         
         
         core.divEnd()
         core.divEnd()
     
     return str(core)
예제 #7
0
    def generateQ2Output(cls, additionalAttributesDict, additionalResultsDict, analysisQuestion, choices,
                         galaxyFn, queryTrackTitle, gsuite, results, similarityStatClassName):
        gsPerTrackResultsModel = GSuitePerTrackResultModel(results, ['Similarity to query track', 'P-value'],
                                                           additionalResultsDict=additionalResultsDict,
                                                           additionalAttributesDict=additionalAttributesDict)
        if choices.leadAttribute and choices.leadAttribute != GSuiteConstants.TITLE_COL:
            gsPerTrackResults = gsPerTrackResultsModel.generateColumnTitlesAndResultsDict(choices.leadAttribute)
        else:
            gsPerTrackResults = gsPerTrackResultsModel.generateColumnTitlesAndResultsDict()
        core = HtmlCore()
        core.begin()
        core.divBegin(divId='results-page')
        core.divBegin(divClass='results-section')
        core.header(analysisQuestion)
        topTrackTitle = results.keys()[0]
        core.paragraph('''
                The track "%s" has the lowest P-value of %s corresponding to %s  similarity to the query track "%s"
                as measured by "%s" track similarity measure.
            ''' % (topTrackTitle, strWithNatLangFormatting(results[topTrackTitle][1]),
                   strWithNatLangFormatting(results[topTrackTitle][0]), queryTrackTitle, similarityStatClassName))

        addTableWithTabularAndGsuiteImportButtons(
            core, choices, galaxyFn, cls.Q2_SHORT, tableDict=gsPerTrackResults[1],
            columnNames=gsPerTrackResults[0], gsuite=gsuite, results=results,
            gsuiteAppendAttrs=['similarity_score', 'p_value'], sortable=True)

        columnInd = 0
        if choices.leadAttribute and choices.leadAttribute != GSuiteConstants.TITLE_COL:
            columnInd = 1

        resultsSeparateListPart = OrderedDict()
        additionalResultsDictIncludePartFromResults = OrderedDict()

        for k, v in results.iteritems():
            if k not in resultsSeparateListPart.keys():
                resultsSeparateListPart[k] = v[0]
            if k not in additionalResultsDictIncludePartFromResults.keys():
                additionalResultsDictIncludePartFromResults[k] = OrderedDict()
            additionalResultsDictIncludePartFromResults[k]['P-Value'] = v[1]
            for k1, v1 in additionalResultsDict[k].iteritems():
                additionalResultsDictIncludePartFromResults[k][k1] = v1

        res = GSuiteTracksCoincidingWithQueryTrackTool.drawPlot(
            resultsSeparateListPart, additionalResultsDictIncludePartFromResults,
            'Similarity to query track', columnInd=columnInd)
        core.line(res)
        core.divEnd()
        core.divEnd()
        core.end()
        return core
def getAnalysisQuestionInfoHtml(bmQid):
    '''
    Builds the div element that contains the appropriate basic mode analysis question.
    '''
    from quick.toolguide import BasicModeQuestionCatalog
    from proto.hyperbrowser.HtmlCore import HtmlCore

    if bmQid:
        htmlCore = HtmlCore()
        htmlCore.divBegin(divClass='analysis-info')
        prgrph = '''
    <b>Your question of interest was: %s<b>
    ''' % BasicModeQuestionCatalog.Catalog[bmQid]
        htmlCore.paragraph(prgrph)
        htmlCore.divEnd()
        return htmlCore
예제 #9
0
    def helpTextForUserBinSource(self):
        core = HtmlCore()
        core.paragraph(
            str(HtmlCore().emphasize(
                'Use the bounding regions of the selected '
                'track(s), if defined. If more than one track '
                'is selected, the intersection of the bounding '
                'regions is used, i.e. where the bounding '
                'regions are overlapping.')))

        core2 = HtmlCore()
        core2.divBegin(divId='help_bounding_regions',
                       divClass='infomessagesmall help',
                       style='display: block;')
        core2.paragraph(
            'Bounding regions are the regions where a track is defined, e.g. where '
            'there theoretically may be data. This means that if there is no data in '
            'a bounding region, the absence of data is informative, i.e. that the lack '
            'of data is not just caused by not looking at the particular region. '
            'Hence, the bounding region for most tracks should be defined without '
            'for instance the centromeres. For tracks with no explicitly defined '
            'bounding regions, the bounding regions are implicitly defined as all '
            '(complete) chromosomes containing at least one track element.')
        core2.divEnd()
        core2.toggle('More information about bounding regions',
                     styleId='help_bounding_regions')
        core.paragraph(str(core2))

        core3 = HtmlCore()
        core3.styleInfoBegin(styleClass='infomessagesmall')
        core3.descriptionLine(
            'Note',
            'Intersecting bounding regions currently only supported for '
            'two tracks. If using a third track or an intensity track, '
            'only the bounding regions of the two first tracks are '
            'considered.')
        core3.styleInfoEnd()
        core.paragraph(str(core3))

        self._appendChrArmNoteIfNeeded(core)

        core.hideToggle(styleId='help_bounding_regions')
        return str(core)
예제 #10
0
 def getToolDescription():
     '''
     Specifies a help text in HTML that is displayed below the tool.
     '''
     core = HtmlCore()
     core.divBegin()
     core.paragraph("""This tools implements a solution to the statistical question
                 'Are certain tracks of one suite coinciding particularly strongly with
                 certain tracks of another suite?'. To use the tool:""")
     core.orderedList(["Select the query GSuite (dataset collection of interest).",
                       "Select the reference GSuite (dataset collection to be screened against the query collection).",
                       "Select additional options (advanced mode only).",
                       "Execute the tool."])
     core.paragraph("""<br><br><b>Tool illustration.</b>
                 Coinciding tracks from two collections.<br>
                 Qi - Query track i (i = 1,..,m).<br>
                 Ri - Reference track i (i = 1,..,n).<br>
                 """)
     core.divEnd()
     return str(core)
    def execute(choices, galaxyFn=None, username=''):

        gSuite = getGSuiteFromGalaxyTN(choices.gsuite)
        if gSuite.genome != choices.genome:
            gSuite.setGenomeOfAllTracks(choices.genome)
#         regSpec, binSpec = UserBinSelector.getRegsAndBinsSpec(choices)
        paragraphs = generatePilotPageFiveParagraphs(gSuite, galaxyFn)

        core = HtmlCore()
        core.begin()
        core.divBegin(divId='results-page')
        core.divBegin(divClass='results-section')
        core.header('Clustering of track elements')
        for prg in paragraphs:
            core.paragraph(prg)
        core.divEnd()
        core.divEnd()
        core.end()

        print core
예제 #12
0
    def getToolDescription():
        '''
        Specifies a help text in HTML that is displayed below the tool.
        '''

        core = HtmlCore()
        core.divBegin()
        core.paragraph("""This tools implements a solution to the statistical question
            'Which tracks (in a suite) coincide most strongly with a separate single track?'. To use the tool:""")
        core.orderedList(["Select the query track (dataset of interest).",
                          "Select the reference GSuite (dataset collection to be screened against the query track).",
                          "Select additional options (advanced mode only).",
                          "Execute the tool."])
        core.paragraph("""<br><br><b>Tool illustration.</b>
            Tracks in a collection that coincide with a query track of interest.<br>
            Q - The query track.<br>
            Ri - Reference track i (i = 1,..,n).<br>
            """)
        core.divEnd()
        return str(core)
    def execute(self, printHtmlBeginEnd=True, printTrackNamesTable=True):
        print GalaxyInterface.getHtmlBeginForRuns(self._galaxyFn)
        core = HtmlCore()
        if printTrackNamesTable:
            core.divBegin('trackNames')
            dataDict = OrderedDict([(x, []) for x in self._trackNames])
            tblExpandable = True
            if len(self._trackNames) < 11:
                tblExpandable = False
            core.tableFromDictionary(dataDict, ['Tracks under analysis'],
                                     tableId="resTable",
                                     expandable=tblExpandable)
            #             core.tableHeader(['Tracks under analysis:'])
            #             for trackName in self._trackNames:
            #                 core.tableLine([trackName])
            #             core.tableFooter()
            core.divEnd()
        print core
        try:
            results = GalaxyInterface.run(self._tracks[0],
                                          self._tracks[1],
                                          self.getAnalysisDefinitionString(),
                                          self._regSpec,
                                          self._binSpec,
                                          self._genome,
                                          self._galaxyFn,
                                          printRunDescription=False,
                                          printHtmlBeginEnd=printHtmlBeginEnd,
                                          fromMainTool=False)

            if self.hasVisualization():
                print self.visualizeResults(results)


#        except:
#            pass
        finally:
            core2 = HtmlCore()
            core2.hideToggle(styleClass='infomessagesmall')
            print core2
            print GalaxyInterface.getHtmlEndForRuns()
예제 #14
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)
        selectedTasks = [key for key, val in choices.tasks.iteritems() if val]
        bins = int(choices.bins) if choices.bins else 20
        displayPoints = bool(choices.displayPoints['display']
                             ) if choices.displayPoints else False
        spar = float(choices.spar) if choices.spar else 1.0
        verticalLines = None
        if choices.verticalLines and \
            choices.analysis in [IndividualTaskAnalysis.ANALYSIS_BIN_AVG_SMOOTHED_PLOT,
                                    IndividualTaskAnalysis.ANALYSIS_MOVING_AVG_SMOOTHED_PLOT]:
            verticalLines = [
                float(x.strip()) for x in choices.verticalLines.split(',')
            ]
        examAnalysis = IndividualTaskAnalysis(resultsFN, galaxyFn)
        examAnalysis.run(analysis=choices.analysis,
                         selectedTasks=selectedTasks,
                         bins=bins,
                         displayPoints=displayPoints,
                         spar=spar,
                         verticalLines=verticalLines)

        core = HtmlCore()
        core.begin()
        for plotUrl in examAnalysis.getPlotUrls():
            core.divBegin(divId='plot')
            core.image(plotUrl)
            core.divEnd()
        core.end()
        print core
예제 #15
0
 def generateQ3output(cls, analysisQuestion, queryTrackTitle, results, similarityStatClassName):
     pval = results['P-value']
     observed = results['TSMC_SummarizedInteractionWithOtherTracksV2Stat']
     significanceLevel = 'strong' if pval < 0.01 else ('weak' if pval < 0.05 else 'no')
     core = HtmlCore()
     core.begin()
     core.divBegin(divId='results-page')
     core.divBegin(divClass='results-section')
     core.header(analysisQuestion)
     core.paragraph('''
                 The query track %s shows %s significance in similarity to the suite of %s
                 and corresponding p-value of %s,
                 as measured by "%s" track similarity measure.
             ''' % (
         queryTrackTitle, significanceLevel, strWithNatLangFormatting(observed),
         strWithNatLangFormatting(pval),
         similarityStatClassName))
     core.divEnd()
     core.divEnd()
     core.end()
     return core
예제 #16
0
 def getToolDescription():
     '''
     Specifies a help text in HTML that is displayed below the tool.
     '''
     core = HtmlCore()
     core.divBegin()
     core.paragraph(
         """This tools implements a solution to the statistical question
                 'Which tracks (in a suite) are most representative and most atypical'. To use the tool:"""
     )
     core.orderedList([
         "Select the GSuite (dataset collection of interest).",
         "Select additional options (advanced mode only).",
         "Execute the tool."
     ])
     core.paragraph("""<br><br><b>Tool illustration.</b>
                 Relatedness of tracks in a collection.<br>
                 Ri - Track i (i = 1,..,n).<br>
                 """)
     core.divEnd()
     return str(core)
예제 #17
0
    def generateQ1output(cls, additionalResultsDict, analysisQuestion, choices, galaxyFn,
                         gsPerTrackResults, queryTrackTitle, gsuite, results,
                         similarityStatClassName):
        core = HtmlCore()
        core.begin()
        core.divBegin(divId='results-page')
        core.divBegin(divClass='results-section')
        core.header(analysisQuestion)
        topTrackTitle = results.keys()[0]
        core.paragraph('''
                The track "%s" in the GSuite is the one most similar to the query track %s, with a similarity score of %s
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                as measured by the "%s" track similarity measure.
            ''' % (
            topTrackTitle, queryTrackTitle, strWithNatLangFormatting(results[topTrackTitle]),
            similarityStatClassName))
        core.divBegin()

        addTableWithTabularAndGsuiteImportButtons(
            core, choices, galaxyFn, cls.Q1_SHORT, tableDict=gsPerTrackResults[1],
            columnNames=gsPerTrackResults[0], gsuite=gsuite, results=results,
            gsuiteAppendAttrs=['similarity_score'], sortable=True)

        core.divEnd()
        columnInd = 0
        if choices.leadAttribute and choices.leadAttribute != GSuiteConstants.TITLE_COL:
            columnInd = 1

        res = GSuiteTracksCoincidingWithQueryTrackTool.drawPlot(
            results, additionalResultsDict,
            'Similarity to query track', columnInd=columnInd)
        core.line(res)
        core.divEnd()
        core.divEnd()
        core.end()
        return core
    def execute(cls, choices, galaxyFn=None, username=''):

        gSuite = getGSuiteFromGalaxyTN(choices.gsuite)
        if gSuite.genome != choices.genome:
            gSuite.setGenomeOfAllTracks(choices.genome)
        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)
        paragraphs = generatePilotPageOneParagraphs(gSuite,
                                                    galaxyFn,
                                                    regSpec=regSpec,
                                                    binSpec=binSpec,
                                                    username=username)

        core = HtmlCore()
        core.begin()
        core.divBegin(divId='results-page')
        core.divBegin(divClass='results-section')
        core.header('Basic overview of tracks in collection')
        for prg in paragraphs:
            core.paragraph(prg)
        core.divEnd()
        core.divEnd()
        core.end()

        print core
    def execute(cls, choices, galaxyFn=None, username=''):
        file = choices.file
        import quick.extra.stefania.Functions_defineDistance_CreateDistanceMatrix_ClusterAnalysis as cdm

        if choices.mothers == "":
            numMothers = None
        else:
            numMothers = int(choices.mothers)


        # inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(file.split(':')), 'r')
        # with inputFile as f:
        #     data = [x.strip('\n') for x in f.readlines()]
        # f.closed
        from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
        sf = GalaxyRunSpecificFile(["matrix.pickle"],galaxyFn)
        sfPng = GalaxyRunSpecificFile(["matrix.png"], galaxyFn)
        sfPng1 = GalaxyRunSpecificFile(["Flat1.csv"], galaxyFn)
        sfPng2 = GalaxyRunSpecificFile(["Flat2.csv"], galaxyFn)

        filename = ExternalTrackManager.extractFnFromGalaxyTN(file.split(':'))
        outFn = sf.getDiskPath(ensurePath=True)
        outDendrogram = sfPng.getDiskPath(ensurePath=True)
        clustersFileOutput1 = sfPng1.getDiskPath(ensurePath=True)
        clustersFileOutput2 = sfPng2.getDiskPath(ensurePath=True)
        cdm.createDistanceMatrix(filename, outFn, outFileType='pkl', womanIDcolPosition = 0, numRows=numMothers)

        cdm.clusteringFunction(outFn, outDendrogram, clustersFileOutput1, clustersFileOutput2, type='hierarchical', method1= 'centroid', method2='complete')
        print "Result: ", sf.getLink("pickle-file")

        htmlCore = HtmlCore()
        htmlCore.begin()

        htmlCore.divBegin('plot1')
        htmlCore.link('Download plot', sfPng.getURL())
        htmlCore.image(sfPng.getURL())
        htmlCore.divEnd()

        htmlCore.divBegin('plot1')
        htmlCore.link('Download file1', sfPng1.getURL())
        htmlCore.divEnd()

        htmlCore.divBegin('plot1')
        htmlCore.link('Download file2', sfPng2.getURL())
        htmlCore.divEnd()

        htmlCore.end()

        print htmlCore
예제 #20
0
    def execute(choices, galaxyFn=None, username=''):

        gSuite = getGSuiteFromGalaxyTN(choices.gsuite)
        if gSuite.genome != choices.genome:
            gSuite.setGenomeOfAllTracks(choices.genome)
        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)
        paragraphs = OrderedDict()
        paragraphs[
            'Basic overview of tracks in collection'] = generatePilotPageOneParagraphs(
                gSuite,
                galaxyFn,
                regSpec=regSpec,
                binSpec=binSpec,
                username=username)
        paragraphs['Overlap between tracks'] = generatePilotPageTwoParagraphs(
            gSuite, galaxyFn, regSpec=regSpec, binSpec=binSpec)
        paragraphs[
            'Similarity and uniqueness of tracks'] = generatePilotPageThreeParagraphs(
                gSuite, galaxyFn, regSpec=regSpec, binSpec=binSpec)
        paragraphs['Clustering of tracks'] = generatePilotPageFiveParagraphs(
            gSuite, galaxyFn)

        core = HtmlCore()
        core.begin()
        core.divBegin(divId='results-page', divClass='trackbook_main')
        for hdr, prgList in paragraphs.iteritems():
            core.divBegin(divClass='trackbook_section')
            core.divBegin(divClass='results-section')
            core.header(hdr)
            for prg in prgList:
                core.paragraph(prg)
            core.divEnd()
            core.divEnd()
        core.divEnd()
        core.end()

        print core
예제 #21
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.
        '''

        cls._setDebugModeIfSelected(choices)

        # First compute pvalue by running the statistic through a wrapper stat that computes the max per bin
        """
        from quick.statistic.RandomizationManagerV3Stat import RandomizationManagerV3Stat
        from quick.statistic.CollectionBinnedHypothesisWrapperStat import CollectionBinnedHypothesisWrapperStat
        analysisSpec = AnalysisSpec(CollectionBinnedHypothesisWrapperStat)
        analysisSpec.addParameter("rawStatistic", "GenericMaxBinValueStat")
        analysisSpec.addParameter('perBinStatistic', 'SummarizedStat')
        analysisSpec.addParameter('mcSamplerClass', 'NaiveMCSamplingV2Stat')
        analysisSpec.addParameter('pairwiseStatistic', 'ProportionCountStat')
        analysisSpec.addParameter('summaryFunc', choices.summaryFunc)
        analysisSpec.addParameter('evaluatorFunc','evaluatePvalueAndNullDistribution')
        analysisSpec.addParameter('tail', 'right-tail')
        analysisSpec.addParameter('assumptions', 'RandomGenomeLocationTrack')
        analysisSpec.addParameter('maxSamples', 10)

        gsuite = getGSuiteFromGalaxyTN(choices.gsuite)
        tracks = [Track(x.trackName) for x in gsuite.allTracks()]

        regSpec, binSpec = cls.getRegsAndBinsSpec(choices)

        analysisBins = GalaxyInterface._getUserBinSource(regSpec,
                                                         binSpec,
                                                         choices.genome)
        results = doAnalysis(analysisSpec, analysisBins, tracks)

        print "<p>Max stat results:</p>"

        print results.getGlobalResult()
        """
        # Stat question 4
        summaryFunc = choices.summaryFunc if choices.summaryFunc else cls.SUMMARY_FUNC_DEFAULT
        statTxt = "Average"
        if (summaryFunc == "max"): statTxt = "Maximum"

        statDesc = 'number of <b>segments</b> per base'
        if choices.analysisName == cls.Q2:
            statDesc = 'number of <b>base pairs covered by segments</b>'

        core = HtmlCore()
        core.begin()
        core.header("Enrichment of GSuite tracks across regions")
        core.divBegin(divClass='resultsExplanation')
        core.paragraph(
            'The following is a list of all regions (bins) and the <b>' +
            statTxt.lower() + '</b> ' + statDesc +
            ' across the tracks within each region.')
        core.divEnd()

        if choices.analysisName == cls.Q3:

            # Compute p-value per bin
            analysisSpec = AnalysisSpec(GSuiteBinEnrichmentPValWrapperStat)
            analysisSpec.addParameter('rawStatistic', 'BinSizeStat')
            #analysisSpec.addParameter('pairwiseStatistic', 'ProportionElementCountStat')
            #analysisSpec.addParameter('pairwiseStatistic', 'ProportionElementCountStat')
            #analysisSpec.addParameter('summaryFunc', summaryFunc)
            gsuite = getGSuiteFromGalaxyTN(choices.gsuite)
            tracks = [Track(x.trackName) for x in gsuite.allTracks()]
            regSpec, binSpec = cls.getRegsAndBinsSpec(choices)
            from quick.statistic.GenericRelativeToGlobalStat import GenericRelativeToGlobalStatUnsplittable
            #analysisSpec.addParameter("globalSource", GenericRelativeToGlobalStatUnsplittable.getGlobalSource('test', choices.genome, False))
            analysisSpec.addParameter("globalSource", 'userbins')
            analysisBins = GalaxyInterface._getUserBinSource(
                regSpec, binSpec, choices.genome)
            results_pval = doAnalysis(analysisSpec, analysisBins, tracks)

        #print results_pval

        analysisSpec = AnalysisSpec(SummarizedWrapperStat)
        analysisSpec.addParameter('rawStatistic', 'SummarizedWrapperStat')

        countStat = 'ProportionElementCountStat'
        if choices.analysisName == cls.Q2:
            countStat = 'ProportionCountStat'

        # analysisSpec.addParameter('pairwiseStatistic', 'ProportionCountStat')
        analysisSpec.addParameter('pairwiseStatistic', countStat)
        analysisSpec.addParameter('summaryFunc', summaryFunc)
        gsuite = getGSuiteFromGalaxyTN(choices.gsuite)
        tracks = [Track(x.trackName) for x in gsuite.allTracks()]

        regSpec, binSpec = cls.getRegsAndBinsSpec(choices)
        analysisBins = GalaxyInterface._getUserBinSource(
            regSpec, binSpec, choices.genome)
        results = doAnalysis(analysisSpec, analysisBins, tracks)

        prettyResults = {}
        #print results

        for key, val in results.iteritems():
            if "Result" in val.keys():

                if choices.analysisName == cls.Q3:
                    prettyResults[key] = (val["Result"],
                                          results_pval[key]["Result"])
                else:
                    prettyResults[key] = (val["Result"])
            else:
                prettyResults[key] = "No result"

        topTrackTitle = results.keys()[0]
        """
        core.paragraph('''
            Suite data is coinciding the most in bin %s
        ''' % ('test'))
        """

        columnNames = ['Bin', 'Representation within the bin']
        if choices.analysisName == cls.Q3:
            columnNames.append('p-value')

        core.divBegin()
        if choices.analysisName == cls.Q1:
            shortQuestion = cls.Q1_SHORT
        elif choices.analysisName == cls.Q2:
            shortQuestion = cls.Q2_SHORT
        else:  # Q3
            shortQuestion = cls.Q3_SHORT

        visibleRows = 20
        makeTableExpandable = len(prettyResults) > visibleRows

        addTableWithTabularAndGsuiteImportButtons(
            core,
            choices,
            galaxyFn,
            shortQuestion,
            tableDict=prettyResults,
            columnNames=columnNames,
            sortable=True,
            presorted=0,
            expandable=makeTableExpandable)

        core.divEnd()
        core.end()

        print str(core)
    def presentResults(self):
        """
        :return: Returns html core object
        """
        core = HtmlCore()
        core.begin()
        core.header("Results")
        core.divBegin(divClass='resultsExplanation')
        core.paragraph('''
            The table summarizes the results for each transcription factor and PWM that was analysed. Click on a row for details.
            ''')
        core.divEnd()

        core._str += """
         <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
         <script>

        jQuery(document).ready(function() {
          jQuery(".content").hide();
          //toggle the componenet with class msg_body
          jQuery(".heading").click(function()
          {
            jQuery(this).next(".content").slideToggle(1);
          });
        });
        </script>
        """

        core._str += "<table class='colored bordered'>"
        #columns = ["Transcription factor", ""]
        #core.tableFromDictionary(rows, columns)
        core._str += """
            <tr>
                <th class='header'>Transcription factor</th>
                <!--<th>Peak data</th>-->
                <th class='header'>Mofif</th>
                <th class='header'>Number of peaks</th>
                <th class='header'>Number of peaks with SNP(s)</th>
                <th class='header'>Number of changed bindings</th>
                <!--<th class='header'>Binding after mutation</th>-->
            </tr>
        """

        for tf in transcription_factors:
            # First print some summary information for this TF
            name = tf.name
            core._str += "<tr class='heading' style='cursor: pointer;'>"
            core._str += "<td>" + name + "</td>"
            #core._str +=  "<td>" + ''.join(tf.name) + "</td>"
            core._str += "<td>" + tf.motif.name + "</td>"
            core._str += "<td>%d</td>" % (len(tf.peaks))
            core._str += "<td>%d</td>" % (len(
                [p for p in tf.peaks if p.hasSnps]))

            subtable = ""
            subtable += "<tr class='content'><td colspan='7'>"
            subtable += "<br><h4 style='margin-left: 20px;'>Peaks that intersect with one or more SNPs</h4>"

            subtable += "<table border='1' cellpadding='5' style='margin-left: 20px;'>"
            subtable += """
                <tr>
                    <th>Position</th>
                    <th>Best binding before mutation</th>
                    <th>Best binding after mutation</th>
                </tr>
            """

            rows_important = []
            rows = []

            n_gain_loss = 0
            for peak in tf.peaks:

                important = False

                if len(peak.tf.motif) > len(peak.sequence):
                    continue  # Ignore motifs longer than peak sequence (will only occur on test sets
                row = ""
                row += "<tr>"
                row += "<td>%s %d:%d</td>" % (peak.chr, peak.start, peak.end)
                #print "<td>%s</td>" % (''.join(peak.sequence))
                p = peak.bestBindingPositionBeforeMutation
                #print "Sequence: " + str(peak.sequence[p - peak.start : p - peak.start + len(peak.tf.motif)])
                row += "<td>On pos %d with score %.10f<br>%s</td>" % (
                    p, peak.bestBindingScoreBeforeMutation,
                    prettySequence(
                        peak, peak.sequence[p - peak.start:p - peak.start +
                                            len(peak.tf.motif)], p))

                # Only present binding after if there was a mutation either within the old binding or within a new binding
                """
                if peak.hasSnpBetween(peak.bestBindingPositionAfterMutation, peak.bestBindingPositionAfterMutation + len(peak.tf.motif))  or \
                   peak.hasSnpBetween(peak.bestBindingPositionAfterMutation, peak.bestBindingPositionAfterMutation + len(peak.tf.motif)):
                    subtable +=  "<td>On pos %d with score %.10f<br>%s<br>Binding sequence: %s</td>" % (peak.bestBindingPositionAfterMutation, peak.bestBindingScoreAfterMutation, \
                                                                               prettySequence(peak, peak.bindingSequenceAfterMutations, peak.bestBindingPositionAfterMutation),\
                                                                                ''.join(peak.bindingSequenceAfterMutations))
                """

                if peak.hasSnpBetween(peak.start, peak.end):
                    if peak.bestBindingScoreAfterMutation != peak.bestBindingScoreBeforeMutation:
                        n_gain_loss += 1
                        row += "<td><font color='darkgreen'>"
                    else:
                        row += "<td><font>"

                    row +=  "On position %d with score %.10f<br>%s<br>Binding sequence: %s</font></td>" % (peak.bestBindingPositionAfterMutation, peak.bestBindingScoreAfterMutation, \
                                                                               prettySequence(peak, peak.bindingSequenceAfterMutations, peak.bestBindingPositionAfterMutation),\
                                                                                    ''.join(peak.bindingSequenceAfterMutations))
                    important = True
                else:
                    row += "<td><font color='#666666'>No change (no point mutations)</font></td>"

                row += "</tr>"

                if important:
                    rows_important.append(row)
                else:
                    rows.append(row)

            subtable += ''.join(rows_important)

            #subtable += ''.join(rows)

            subtable += "</table><br><br>"
            subtable += "</td></tr>"

            if n_gain_loss > 0:
                core._str += "<td><b>%d</b></td>" % n_gain_loss
            else:
                core._str += "<td>%d</td>" % n_gain_loss
            core._str += "</tr>"
            core._str += subtable

        core._str += "</table>"

        return core
예제 #23
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
예제 #24
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
예제 #25
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(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.
        '''
        gSuite = getGSuiteFromGalaxyTN(choices.gsuite)
        attributeNames = gSuite.attributes
        attributeValCountDict = dict()
        for attrName in attributeNames:
            attributeValCountDict[attrName] = defaultdict(int)
        for gsTrack in gSuite.allTracks():
            for attrName in gsTrack.attributes:
                attributeValCountDict[attrName][gsTrack.getAttribute(
                    attrName)] = attributeValCountDict[attrName][
                        gsTrack.getAttribute(attrName)] + 1

        htmlCore = HtmlCore()

        htmlCore.begin()
        htmlCore.divBegin(divId='results-page')
        htmlCore.divBegin(divClass='results-section')
        htmlCore.header('Meta-data summary:')

        summaryList = []
        locationLine = '''Location:<b> %s</b>. %s'''
        if gSuite.location == GSuiteConstants.UNKNOWN:
            summaryList.append(locationLine % (
                gSuite.location,
                "The location of the tracks in the GSuite is not specified as remote or local."
            ))
        elif gSuite.location == GSuiteConstants.REMOTE:
            summaryList.append(
                locationLine %
                (gSuite.location,
                 "The tracks in the GSuite are located in a remote server."))
        elif gSuite.location == GSuiteConstants.LOCAL:
            summaryList.append(
                locationLine %
                (gSuite.location,
                 "The tracks in the GSuite are located on your server."))
        elif gSuite.location == GSuiteConstants.MULTIPLE:
            summaryList.append(locationLine % (
                gSuite.location,
                "The tracks in the GSuite are located both on your local server and at a remote location."
            ))

        fileFormatLine = '''File format:<b> %s</b>. %s'''
        if gSuite.fileFormat == GSuiteConstants.UNKNOWN:
            summaryList.append(fileFormatLine % (
                gSuite.fileFormat,
                "The file format of the tracks in the GSuite is not specified."
            ))
        elif gSuite.fileFormat == GSuiteConstants.PREPROCESSED:
            summaryList.append(fileFormatLine % (
                gSuite.fileFormat,
                "The tracks in the GSuite are preprocessed and ready for analysis."
            ))
        elif gSuite.fileFormat == GSuiteConstants.PRIMARY:
            summaryList.append(fileFormatLine % (
                gSuite.fileFormat,
                "The tracks in the GSuite can be manipulated, but must be preprocessed first for analysis."
            ))
        elif gSuite.fileFormat == GSuiteConstants.MULTIPLE:
            summaryList.append(fileFormatLine % (
                gSuite.fileFormat,
                "The tracks in the GSuite are both in preprocessed and primary formats. Only preprocessed tracks can be analyzed by HyperBrowser tools."
            ))

        trackTypeLine = '''Track type:<b> %s</b>. %s'''
        if gSuite.trackType == GSuiteConstants.UNKNOWN:
            summaryList.append(
                trackTypeLine %
                (gSuite.trackType,
                 "The track type of the tracks in the GSuite is not specified."
                 ))
        elif gSuite.trackType == GSuiteConstants.MULTIPLE:
            summaryList.append(
                trackTypeLine %
                (gSuite.trackType,
                 "The tracks in the GSuite are of different track types."))
        else:
            summaryList.append(trackTypeLine % (
                gSuite.trackType,
                "The tracks in the GSuite are all (subtypes) of the same type."
            ))

        genomeLine = '''Genome:<b> %s</b>. %s'''
        if gSuite.genome == GSuiteConstants.UNKNOWN:
            summaryList.append(
                genomeLine %
                (gSuite.genome,
                 "The genome of the tracks in the GSuite is not specified."))
        elif gSuite.fileFormat == GSuiteConstants.MULTIPLE:
            summaryList.append(
                genomeLine %
                (gSuite.genome,
                 "The tracks in the GSuite are of different genomes."))
        else:
            summaryList.append(
                genomeLine %
                (gSuite.genome,
                 "The tracks in the GSuite come from the same genome."))

        htmlCore.unorderedList(summaryList)

        if len(attributeNames) > 0:

            paragraph2 = '''
                There are<b> %s </b>attributes in the GSuite. 
                For each of the attributes the most frequent value is given in the table below.
            ''' % str(len(attributeNames))

            htmlCore.paragraph(paragraph2)

            tableDataDict = OrderedDict()
            for attrName in attributeNames:
                maxVal, maxCount = max(
                    attributeValCountDict[attrName].iteritems(),
                    key=operator.itemgetter(1))

                if maxCount == 1:
                    mostFreqVal = '[All values are unique]'
                    nrOfOccurrences = 1
                elif maxCount == gSuite.numTracks():
                    mostFreqVal = maxVal
                    nrOfOccurrences = str(gSuite.numTracks()) + ' [all tracks]'
                else:
                    mostFreqValList = [
                        x for x, y in
                        attributeValCountDict[attrName].iteritems()
                        if y == maxCount
                    ]
                    mostFreqVal = ' | '.join(mostFreqValList)
                    nrOfOccurrences = str(maxCount) + ' [tie]' if len(
                        mostFreqValList) > 1 else maxCount

                tableDataDict[attrName] = [mostFreqVal, nrOfOccurrences]

            htmlCore.tableFromDictionary(tableDataDict, [
                'Attribute name', 'Most frequent value',
                'Number of occurrences'
            ],
                                         sortable=False,
                                         expandable=False)

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

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

        dictNum=OrderedDict()
        for key, it in attribute.iteritems():
            if it == True:
                dictNum[key] = False
        
        #check if it is dict or not
        if not isinstance(columnY, dict):
            tempDict={}
            tempDict[columnY] = 'True'
            columnY=tempDict
        
        
        seriesName=[]
        
        #check if user selected categoriesNumber and it is possible to use combinate 
        categoriesNumber = False
             
        sortedCat=None
        categories=None
        if columnX == TITLE_COL:
            categories = gSuite.allTrackTitles()
        elif columnX == 'line number':
            categories = None
        else:
            if columnX in dictNum.keys():
                categoriesBefore = [float(v) for v in gSuite.getAttributeValueList(columnX)]
                
                if axesScaleX == 'log10':
                    for cbN in range(0, len(categoriesBefore)):
                        if categoriesBefore[cbN]!=0:
                            categoriesBefore[cbN]=math.log(categoriesBefore[cbN], 10)
                
                sortedCat = sorted(range(len(categoriesBefore)), key=lambda k: categoriesBefore[k])
                categories=[]
                for n in sortedCat:
                    categories.append(categoriesBefore[n])
                
                categoriesNumber=True
                
            else:
                categories = gSuite.getAttributeValueList(columnX)
                
        
        #data are sorted according to numerical values
        data=[]
        for key, it in columnY.iteritems():
            if it == 'True':
                dataPart=[]
                seriesName.append(key)
                dataPart = []
                for x in gSuite.getAttributeValueList(key):
                    try:
                        if axesScaleY == 'log10':
                            if x!=0:
                                dataPart.append(math.log(float(x), 10))
                            else:
                                dataPart.append(0)
                        else:
                            dataPart.append(float(x))
                    except:
                        # need to support None in heatmap
                        if plotType == 'Heatmap':
                            dataPart.append(0)
                        else:
                            dataPart.append(x)
                if sortedCat!=None:
                    dataPartTemp=[]
                    for n in sortedCat:
                        dataPartTemp.append(dataPart[n])
                    dataPart = dataPartTemp
                data.append(dataPart)
                
        
        label=''
        if len(seriesName)!=0:
            label = '<b>{series.name}</b>: {point.x} {point.y}'
        else:
            label = '{point.x} {point.y}'
        
        vg = visualizationGraphs()
        
#         'Column', 'Scatter', 'Heatmap'
        
        if axesScaleX == 'log10':
            xAxisTitle = str(columnX) + ' (' + str(axesScaleX) + ')'
        else:
            xAxisTitle = str(columnX)
        
        if axesScaleY == 'log10':
            yAxisTitle = str('values') + ' (' + str(axesScaleY) + ')'
        else:
            yAxisTitle = str('values')    
        
        minFromList = min(min(d) for d in data)
        if minFromList > 0:
            minFromList = 0
        
        
        #combain series with data
        if plotRes == 'combine':
            if categoriesNumber == True:
                newData=[]
                for d in data:
                    newDataPart=[]
                    for cN in range(0, len(categories)):
                        newDataPart.append([categories[cN], d[cN]])
                    newData.append(newDataPart)
                data=newData
                categories=None
                        
                    
        
        
        res=''
        if plotSeries == 'Single':
            if plotType == 'Scatter':
                res += vg.drawScatterChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     )
            if plotType == 'Pie':
                res += vg.drawPieChart(
                    data[0],
                    seriesName = categories,
                    height = 400,
                    titleText = seriesName[0],
                    )
                
            if plotType == 'Column':
                res += vg.drawColumnChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     )
            if plotType == 'Line':
                res += vg.drawLineChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     )
            if plotType == 'Heatmap':
                res += vg.drawHeatmapSmallChart(
                     data,
                     categories = categories,
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = seriesName,
                     label = label,
#                      titleText = 'Plot',
                     )
        elif plotSeries == 'Multi':
            if plotType == 'Scatter':
                for nrD in range(0, len(data)):
                    if plotRes == 'combine':
                        data[nrD]=[data[nrD]]
                    res += vg.drawScatterChart(
                         data[nrD],
                         categories = categories,
                         xAxisRotation = 90,
                         marginTop = 30,
                         xAxisTitle = xAxisTitle,
                         yAxisTitle = yAxisTitle,
                         height = 500,
                         seriesName = [seriesName[nrD]],
                         label = label,
                         minY=minFromList
    #                      titleText = 'Plot',
                         )
            if plotType == 'Column':
                res += vg.drawColumnCharts(
                     data,
                     categories = [categories for x in range(0, len(data))],
                     xAxisRotation = 90,
                     marginTop = 30,
                     xAxisTitle = xAxisTitle,
                     yAxisTitle = yAxisTitle,
                     height = 500,
                     seriesName = [[seriesName[elD]] for elD in range(0, len(data))],
                     label = label,
                     minY=minFromList
#                      titleText = 'Plot',
                     ) 
            if plotType == 'Line':
                for nrD in range(0, len(data)):
                    if plotRes == 'combine':
                        data[nrD]=[data[nrD]]
                    res += vg.drawLineChart(
                         data[nrD],
                         categories = categories,
                         xAxisRotation = 90,
                         marginTop = 30,
                         xAxisTitle = xAxisTitle,
                         yAxisTitle = yAxisTitle,
                         height = 500,
                         seriesName = [seriesName[nrD]],
                         label = label,
                         minY=minFromList
    #                      titleText = 'Plot',
                         )                  
        
        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.divBegin(divId='results-page')
        htmlCore.divBegin(divClass='results-section')
        
        htmlCore.line(res)
        
        htmlCore.divEnd()
        htmlCore.divEnd()
        htmlCore.end()
        
        print htmlCore
예제 #28
0
 def _buildContent(self):
     #iterate through dictionary and for each key create a section (one of [GSuite, Track...])
     # each value in the dictionary is a list of GiudeData objects that go into the section defined by the key
     htmlCore = HtmlCore()
     htmlCore.divBegin('toolGuideInfo')
     htmlCore.divBegin(divClass='toolGuideInfoText')
     htmlCore.divBegin(divClass='toolGuideInfoTextHeader')
     htmlCore.line(TOOL_GUIDE_HELP_HEADER_TEXT)
     htmlCore.divEnd()
     htmlCore.divBegin(divClass='toolGuideInfoText')
     htmlCore.line(TOOL_GUIDE_HELP_HEADER_TEXT_TEXT)
     htmlCore.divEnd()
     htmlCore.divEnd()
     for guideDataKey, guideDataValues in self._guideDataDict.iteritems():
         htmlCore.divBegin('toolGuide')
         if guideDataKey in TOOL_INPUT_TYPE_TO_TOOL_GUIDE_HELP_HEADER_DICT:
             htmlCore.header(TOOL_INPUT_TYPE_TO_TOOL_GUIDE_HELP_HEADER_DICT[
                 guideDataKey])
         for guideDataValue in guideDataValues:
             htmlCore.divBegin(divClass='toolGuideData')
             htmlCore.divBegin(divClass='toolGuideImgTitle')
             if guideDataValue.imgUrl:
                 htmlCore.image(guideDataValue.imgUrl)
             htmlCore.link(text=guideDataValue.toolDisplayName,
                           url=str(guideDataValue.toolUrl),
                           args=(' onclick="%s"' % guideDataValue.onclick)
                           if guideDataValue.onclick else '')
             htmlCore.divEnd()
             htmlCore.divBegin(divClass='toolGuideDesc')
             htmlCore.append(guideDataValue.description)
             if guideDataValue.helpPageUrl:
                 htmlCore.link(text='...read more',
                               url=str(guideDataValue.helpPageUrl))
             htmlCore.divEnd()
             htmlCore.divEnd()
         htmlCore.divEnd()
     htmlCore.divEnd()
     #raise Exception(str(htmlCore))#to debug
     self._guideContent = str(htmlCore)
예제 #29
0
    def execute(choices, galaxyFn=None, username=''):

        #targetTrackNames, targetTrackCollection, targetTrackGenome = getGSuiteDataFromGalaxyTN(choices.gSuiteFirst)

        gFirst = choices.gSuiteFirst.split(':')
        firstGSuite = ScreenTwoTrackCollectionsAgainstEachOther2LevelDepth.returnGSuiteDict3LevelDept(
            gFirst)

        gSecond = choices.gSuiteSecond.split(':')
        secondGSuite = ScreenTwoTrackCollectionsAgainstEachOther2LevelDepth.returnGSuiteDict2LevelDept(
            gSecond)

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)

        if choices.intraOverlap == ScreenTwoTrackCollectionsAgainstEachOther2LevelDepth.MERGE_INTRA_OVERLAPS:
            analysisDef = 'dummy -> RawOverlapStat'
        else:
            analysisDef = 'dummy [withOverlaps=yes] -> RawOverlapAllowSingleTrackOverlapsStat'

        if choices.type == 'basic':
            results = []
            for elFG in firstGSuite:
                for elSG in secondGSuite:
                    if elFG['genome'] == elSG['genome']:
                        targetTrackGenome = elFG['genome']
                        resultPartList3 = []
                        for targetTrackDetailFolder1 in elFG[
                                'dataFolderValue0']:
                            resultPartList2 = []
                            for targetTrackDetail in targetTrackDetailFolder1[
                                    'dataFolderValue1']:
                                resultPartList1 = []
                                for el in elSG['dataFolderValue0']:
                                    result = GalaxyInterface.runManual(
                                        [
                                            targetTrackDetail['trackPath'],
                                            el['trackPath']
                                        ],
                                        analysisDef,
                                        regSpec,
                                        binSpec,
                                        elFG['genome'].split('-')[0],
                                        galaxyFn,
                                        printRunDescription=False,
                                        printResults=False)
                                    resultPartList1.append({
                                        'refTrackName':
                                        el['trackName'].replace(
                                            targetTrackGenome, ''),
                                        'data':
                                        processResult(result.getGlobalResult())
                                    })
                                resultPartList2.append({
                                    'folderName2':
                                    targetTrackDetail['folderName2'],
                                    'targetTrackName':
                                    targetTrackDetail['trackName'],
                                    'dataFolderValue2':
                                    resultPartList1
                                })
                            resultPartList3.append({
                                'folderName1':
                                targetTrackDetailFolder1['folderName1'],
                                'dataFolderValue1':
                                resultPartList2
                            })
                        results.append({
                            'genome': targetTrackGenome,
                            'dataFolderValue0': resultPartList3
                        })
        else:
            from quick.statistic.NumT2SegsTouchedByT1SegsStat import NumT2SegsTouchedByT1SegsStat
            results = []
            for elFG in firstGSuite:
                for elSG in secondGSuite:
                    if elFG['genome'] == elSG['genome']:
                        if choices.statistic == 'Number of touched segments':
                            analysisSpec = AnalysisSpec(
                                NumT2SegsTouchedByT1SegsStat)
                        #analysisBins = UserBinSource('*', '10m', genome=elFG['genome'].split('-')[0])
                        analysisBins = GlobalBinSource(
                            elFG['genome'].split('-')[0])
                        targetTrackGenome = elFG['genome']
                        resultPartList3 = []
                        for targetTrackDetailFolder1 in elFG[
                                'dataFolderValue0']:
                            resultPartList2 = []
                            for targetTrackDetail in targetTrackDetailFolder1[
                                    'dataFolderValue1']:
                                resultPartList1 = []
                                for el in elSG['dataFolderValue0']:
                                    res = doAnalysis(
                                        analysisSpec, analysisBins, [
                                            PlainTrack(
                                                targetTrackDetail['trackPath']
                                            ),
                                            PlainTrack(el['trackPath'])
                                        ])
                                    resultDict = res.getGlobalResult()
                                    resultPartList1.append({
                                        'refTrackName':
                                        el['trackName'].replace(
                                            targetTrackGenome, ''),
                                        'data': [resultDict['Result']]
                                    })
                                resultPartList2.append({
                                    'folderName2':
                                    targetTrackDetail['folderName2'],
                                    'targetTrackName':
                                    targetTrackDetail['trackName'],
                                    'dataFolderValue2':
                                    resultPartList1
                                })
                            resultPartList3.append({
                                'folderName1':
                                targetTrackDetailFolder1['folderName1'],
                                'dataFolderValue1':
                                resultPartList2
                            })
                        results.append({
                            'genome': targetTrackGenome,
                            'dataFolderValue0': resultPartList3
                        })
        if choices.type == 'basic':
            stat = choices.statistic
            #statIndex = STAT_LIST_INDEX[stat]
            statIndex = ScreenTwoTrackCollectionsAgainstEachOther2LevelDepth.STAT_LIST_INDEX
            statIndex = statIndex.index(stat)
        else:
            stat = '0'
            statIndex = 0

        htmlCore = HtmlCore()
        htmlCore.begin()

        htmlCore.line("""
                      <style type="text/css">
                        .hidden {
                             display: none;
                        {
                        .visible {
                             display: block;
                        }
                      </style>
                   """)

        folderValue0Unique = []
        folderValue1Unique = []
        folderValue2Unique = []
        targetTrackFeatureTitles = []
        for dataDetail0 in results:
            if dataDetail0['genome'] not in folderValue0Unique:
                folderValue0Unique.append(dataDetail0['genome'])
            for dataDetail1 in dataDetail0['dataFolderValue0']:
                if dataDetail1['folderName1'] not in folderValue1Unique:
                    folderValue1Unique.append(dataDetail1['folderName1'])
                for dataDetail2 in dataDetail1['dataFolderValue1']:
                    if dataDetail2['folderName2'] not in folderValue2Unique:
                        folderValue2Unique.append(dataDetail2['folderName2'])
                    for dataDetail3 in dataDetail2['dataFolderValue2']:
                        if dataDetail3[
                                'refTrackName'] not in targetTrackFeatureTitles:
                            targetTrackFeatureTitles.append(
                                dataDetail3['refTrackName'])

        #print 'folderValue0Unique=' + str(folderValue0Unique)
        #print 'folderValue1Unique=' + str(folderValue1Unique)
        #print 'folderValue2Unique=' + str(folderValue2Unique)
        #print 'targetTrackFeatureTitles=' + str(targetTrackFeatureTitles)

        targetTrackNameList = targetTrackFeatureTitles

        htmlCore.line('Statistic: ' + stat)
        htmlCore.line(
            addJS3levelOptionList(folderValue1Unique, folderValue2Unique,
                                  targetTrackFeatureTitles,
                                  targetTrackNameList, folderValue0Unique))

        htmlCore.divBegin('results')

        #htmlCore.paragraph(preporcessResults(results, folderValue1Unique, folderValue2Unique, targetTrackFeatureTitles, statIndex))
        htmlCore.paragraph(
            preporcessResults3(results, folderValue1Unique, folderValue2Unique,
                               targetTrackFeatureTitles, folderValue0Unique,
                               statIndex))
        htmlCore.divEnd()

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

        print htmlCore
    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.
        '''

        import warnings
        #warnings.simplefilter('error')

        cls._setDebugModeIfSelected(choices)

        similarityStatClassName = choices.similarityFunc if choices.similarityFunc else GSuiteStatUtils.T5_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP

        summaryFunc = choices.summaryFunc if choices.summaryFunc else cls.SUMMARY_FUNC_DEFAULT

        pairwiseStatName = GSuiteStatUtils.PAIRWISE_STAT_LABEL_TO_CLASS_MAPPING[similarityStatClassName]

        gsuite = getGSuiteFromGalaxyTN(choices.gsuite)
        tracks = [Track(x.trackName) for x in gsuite.allTracks()]

        statTxt = "Average"
        if(summaryFunc == "max"): statTxt = "Maximum"


        if choices.analysisName == cls.Q2:


            mcfdrDepth = choices.mcfdrDepth if choices.mcfdrDepth else AnalysisDefHandler(REPLACE_TEMPLATES['$MCFDR$']).getOptionsAsText().values()[0][0]

            # First compute pvalue by running the statistic through a wrapper stat that computes the max per bin
            #from quick.statistic.CollectionBinnedHypothesisWrapperStat import CollectionBinnedHypothesisWrapperStat
            #analysisSpec = AnalysisSpec(CollectionBinnedHypothesisWrapperStat)

            analysisDefString = REPLACE_TEMPLATES['$MCFDRv3$'] + ' -> CollectionBinnedHypothesisWrapperStat'
            analysisSpec = AnalysisDefHandler(analysisDefString)
            analysisSpec.setChoice('MCFDR sampling depth', mcfdrDepth)

            analysisSpec.addParameter("rawStatistic", "GenericMaxBinValueStat")
            # analysisSpec.addParameter('perBinStatistic', 'SummarizedStat')
            analysisSpec.addParameter('perBinStatistic', 'MultitrackSummarizedInteractionV2Stat')
            # analysisSpec.addParameter('mcSamplerClass', 'NaiveMCSamplingV2Stat')
            analysisSpec.addParameter('pairwiseStatistic', 'ObservedVsExpectedStat')
            analysisSpec.addParameter('summaryFunc', summaryFunc)
            # analysisSpec.addParameter('evaluatorFunc','evaluatePvalueAndNullDistribution')
            analysisSpec.addParameter('tail', 'right-tail')
            analysisSpec.addParameter('assumptions', 'RandomGenomeLocationTrack')
            #analysisSpec.addParameter('maxSamples', 10)
            analysisSpec.addParameter('multitrackSummaryFunc', summaryFunc)




            regSpec, binSpec = cls.getRegsAndBinsSpec(choices)

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

            results = doAnalysis(analysisSpec, analysisBins, tracks)
            results = results.getGlobalResult()
            resultsTxt = "The highest ranking bin based on the " + statTxt.lower() + " of the Forbes similarity measure for pairs of tracks within each bin had a score of <b>%.3f</b> with p-value <b>%.6f</b>" % (results["TSMC_GenericMaxBinValueStat"], results['P-value'])



        # Stat question 7
        core = HtmlCore()
        core.begin()
        analysisSpec = AnalysisSpec(MultitrackSummarizedInteractionWrapperStat)
        #analysisSpec.addParameter('pairwiseStatistic', 'ObservedVsExpectedStat')
        analysisSpec.addParameter('pairwiseStatistic', GSuiteStatUtils.PAIRWISE_STAT_LABEL_TO_CLASS_MAPPING[similarityStatClassName])
        analysisSpec.addParameter('summaryFunc', summaryFunc)
        analysisSpec.addParameter('multitrackSummaryFunc', summaryFunc)
        gsuite = getGSuiteFromGalaxyTN(choices.gsuite)
        tracks = [Track(x.trackName) for x in gsuite.allTracks()]

        regSpec, binSpec = cls.getRegsAndBinsSpec(choices)

        analysisBins = GalaxyInterface._getUserBinSource(regSpec,
                                                         binSpec,
                                                         choices.genome)
        results = doAnalysis(analysisSpec, analysisBins, tracks)
        #print '<br>results: ', results, '<br><br>'





        prettyResults = OrderedDict()
        for key, val in results.iteritems():
            if "Result" in val.keys():
                prettyResults[key] = val["Result"]
            else:
                prettyResults[key] = "No result"

        core.header(statTxt + " co-occurence between pairs of tracks within each bin")

        if choices.analysisName == cls.Q2:
            core.paragraph(resultsTxt)

        core.divBegin(divClass='resultsExplanation')
        core.paragraph('The following is a list of all bins and the <b>' + statTxt.lower() + '</b> co-occurrence of tracks within each bin.')
        core.divEnd()


        """
        core.paragraph('''
            Suite data is coinciding the most in bin %s
        ''' % ('test'))
        """

        visibleRows = 20
        makeTableExpandable = len(prettyResults) > visibleRows
        columnNames = ['Bin', 'Co-occurrence within the bin']
        if choices.analysisName == cls.Q1:
            shortQuestion = cls.Q1_SHORT
        else:
            shortQuestion = cls.Q2_SHORT

        addTableWithTabularAndGsuiteImportButtons(
            core, choices, galaxyFn, shortQuestion, tableDict=prettyResults,
            columnNames=columnNames, sortable=True, presorted=0,
            expandable=makeTableExpandable, visibleRows=visibleRows)

        core.divEnd()
        core.end()

        print str(core)