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
Exemplo n.º 2
0
 def MakeGlobalResultsDiv(cls, totResDict, visResults, core, galaxyFn):
     #core.divBegin(divId='globalResDiv', style='display:none')
     
     core._str+='<div class="tabbertab">\n     <h2>Global Result</h2>\n<div class="tabber" id="GlobalRes">\n'
     
     # tab for table
     
     yLabels = sorted([v for v in totResDict.keys() if v])
     dataList = [totResDict[v] for v in yLabels]
     xLabels = ['Gene source', 'Exon area', 'Intron area', 'Inter-gene area', 'Total']
     if len(yLabels)>0:
         core._str += '<div class="tabbertab">\n     <h2>Table</h2>\n'
         cls.makeHtmlTable(dataList, xLabels, yLabels, 'Global', core, 'Normal')
         core._str+='</div>\n'
     
     
     core._str += '<div class="tabbertab">\n     <h2>Visualization</h2>'
     figImage = GalaxyRunSpecificFile(['VizTrackOnGenome.png'], galaxyFn)
     cls.makeTrackImage(visResults, figImage.getDiskPath(ensurePath=True), '')
     figUrl = figImage.getURL()
     core.line('<img src="%s" alt="Figure" height="%i" width="800"/>' % (figUrl, max([200,30*min(100, len(visResults))]) ))
     if len(yLabels)>0:
         cls.putPieChartsOnPage('', core, xLabels, yLabels, dataList)
     
     core._str+='</div>\n</div>\n</div>\n'
Exemplo n.º 3
0
    def run(self):
        students = self._examResults.getStudentList()
        examTotalScores = []
        examTotalPercentageScores = []
        for student in students:
            examTotalScores.append(
                self._examResults.getExamScoreForStudent(
                    student.getStudentNr()))
            examTotalPercentageScores.append(
                self._examResults.getExamScorePercentageForStudent(
                    student.getStudentNr()))

        plotOutput = GalaxyRunSpecificFile(
            ['ExamScoresOverview', 'histogram1.png'], self._galaxyFN)
        plotOutput.openRFigure(h=600, w=720)

        examMaxScore = self._examResults.getExamMaxScore()
        mainTitle = 'Exam total scores histogram'
        xTitle = 'Total exam score = %r points' % examMaxScore
        yTitle = 'Students exam score'
        RPlotUtil.drawHistogram(examTotalScores, mainTitle, xTitle, yTitle,
                                [0, examMaxScore])

        RPlotUtil.rDevOff()
        self._plotUrls.append(plotOutput.getURL())
        self._plotPaths.append(plotOutput.getDiskPath())
Exemplo n.º 4
0
 def addVioplot(self):
     taskScoreData = self._examResults.getTaskListSortedByPercentageScore()
     plotOutput = GalaxyRunSpecificFile(
         ['TaskScoreOverview', 'vioplot.png'], self._galaxyFN)
     plotOutput.openRFigure()
     rawData = [x.getPercentScoresList() for x in taskScoreData]
     xlabels = [x.getName() for x in taskScoreData]
     mainTitle = 'Vioplot of task scores overview'
     xTitle = 'Task'
     yTitle = 'Percentage score (from max task score)'
     vioplotColor = 'magenta'
     xAxisAt = [x + 1 for x in range(len(xlabels))]
     xLimMin = 1
     xLimMax = len(xlabels) + 1
     xLas = 2
     yAxisAt = range(0, 100, 20)
     yLimMin = 0
     yLimMax = 100
     yLas = 1
     #plot is used to setup the chart for vioplot so we can control the x and y labels
     RPlotUtil.drawVioplot(rawData, xlabels, mainTitle, xTitle, yTitle,
                           vioplotColor, xAxisAt, xLimMin, xLimMax, xLas,
                           yAxisAt, yLimMin, yLimMax, yLas)
     RPlotUtil.rDevOff()
     self._plotUrls.append(plotOutput.getURL())
     self._plotPaths.append(plotOutput.getDiskPath())
    def execute(cls, choices, galaxyFn=None, username=''):

        #rsids = choices.rsid.split()
        if choices.run == 'Batch':
            #print rsids
            return cls.execute_batch(choices, galaxyFn, username)
        elif choices.batch != '__batch__':
            print '<div class="debug">'

        results = GalaxyRunSpecificFile(['html'], galaxyFn)

        dir = os.path.dirname(results.getDiskPath(ensurePath=True))
        os.mkdir(dir + '/html')

        #print '<div class="debug">'

        cls.choices = choices
        cls.run_varmelt(dir, choices)

        url = results.getURL()

        if choices.run == 'Single' and choices.batch != '__batch__':
            print '</div></pre>'

        core = HtmlCore()
        core.header('Primer3 candidates')
        VariantMeltingProfile.primer3_table_header(core)
        VariantMeltingProfile.primer3_resultsfile_header(dir)

        for r in range(0, int(choices.numReturn)):
            datafile = dir + '/tempdata.' + str(r) + '.results.txt'
            if os.path.exists(datafile):
                variant_pos = VariantMeltingProfile.proc_temp_data(dir, str(r))

                chart = open(dir + '/html/chart-' + str(r) + '.html', 'w')
                chart.write(VariantMeltingProfile.make_chart(variant_pos, r))
                chart.write(cls.primer3_results_table(dir, r))
                chart.write('</body></html>')
                chart.close()

                cls.primer3_results(dir, r)
                cls.primer3_results_table(dir, r, core, url)
                print '<a href="%s/chart-%d.html">Results/graph num %d</a><br>' % (
                    url, r, r + 1)
            else:
                cls.primer3_results(dir, r)
                cls.primer3_results_table(dir, r, core, None)
                break

        core.tableFooter()

        if choices.run == 'Single' and choices.batch != '__batch__':
            print str(core)
            print '<pre>'

        xcore = HtmlCore()
        xcore.begin()
        xcore.append(str(core))
        xcore.end()
        open(dir + '/results.html', 'w').write(str(xcore))
Exemplo n.º 6
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.
        '''
        contents = cls._getContentsOfHistoryFile(choices.history)
        averages = []
        for row in contents:
            row = [float(x) for x in row]
            averages.append(sum(row) / len(row))

        from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
        plot = GalaxyRunSpecificFile(['average.png'], galaxyFn)
        plot.openRFigure()
        from proto.RSetup import r
        r.plot(range(1,
                     len(averages) + 1),
               averages,
               xlab='Rows',
               ylab='Average value')
        plot.closeRFigure()
        #print plot.getURL() + '<br>'
        #print plot.getDiskPath()

        from proto.hyperbrowser.HtmlCore import HtmlCore
        core = HtmlCore()
        core.begin()
        core.bigHeader('Plot of row averages')
        core.paragraph('This is a plot of the average values of each row:')
        core.image(plot.getURL())
        core.paragraph(plot.getURL())
        core.paragraph(plot.getDiskPath())
        #core.paragraph(str(averages))
        core.end()
        print str(core)
Exemplo n.º 7
0
    def printLinkToCallGraph(self, id, galaxyFn, prune=True):
        statsFile = GalaxyRunSpecificFile(id + ['pstats.dump'], galaxyFn)
        dotFile = GalaxyRunSpecificFile(id + ['callGraph.dot'], galaxyFn)
        pngFile = GalaxyRunSpecificFile(id + ['callGraph.png'], galaxyFn)

        ensurePathExists(statsFile.getDiskPath())

        self._stats.dump_stats(statsFile.getDiskPath())
        stats = OverheadStats(statsFile.getDiskPath())
        stats.writeDotGraph(dotFile.getDiskPath(), prune=prune)
        stats.renderGraph(dotFile.getDiskPath(), pngFile.getDiskPath())

        print str(HtmlCore().link(
            'Call graph based on profiling (id=%s)' % ':'.join(id),
            pngFile.getURL()))
Exemplo n.º 8
0
 def execute(choices, galaxyFn=None, username=''):
     from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
     from proto.RSetup import r
     from quick.application.ExternalTrackManager import ExternalTrackManager
     from proto.hyperbrowser.HtmlCore import HtmlCore
     dataFn = ExternalTrackManager.extractFnFromGalaxyTN(choices[0])
     sf = GalaxyRunSpecificFile(['fig1.png'], galaxyFn)
     sf.openRFigure()
     r(PlotFigure1Tool.rCode)(dataFn)
     sf.closeRFigure()
     core = HtmlCore()
     core.begin()
     core.image(sf.getURL())
     core.end()
     print str(core)
Exemplo n.º 9
0
 def addExamScoresHistogram(self):
     studentList = self._examResults.getStudentList()
     examMaxScore = self._examResults.getExamMaxScore()
     data = [
         self._examResults.getExamScoreForStudent(x.getStudentNr())
         for x in studentList
     ]
     plotOutput = GalaxyRunSpecificFile(
         ['TaskScoreOverview', 'histogram1.png'], self._galaxyFN)
     plotOutput.openRFigure()
     mainTitle = 'Histogram of total scores per student'
     xTitle = ''
     yTitle = ''
     xLim = [0, examMaxScore]
     RPlotUtil.drawHistogram(data, mainTitle, xTitle, yTitle, xLim)
     RPlotUtil.rDevOff()
     self._plotUrls.append(plotOutput.getURL())
     self._plotPaths.append(plotOutput.getDiskPath())
Exemplo n.º 10
0
    def run(self, selectedTasks):
        from numpy import corrcoef
        plotOutput = GalaxyRunSpecificFile(['TaskScoreHeatmap', 'heatmap.png'],
                                           self._galaxyFN)
        plotOutput.openRFigure(h=600, w=720)

        heatmapPlotData = []
        for taskName1 in selectedTasks:
            taskCorrelations = []
            t1 = self._examResults.getTask(taskName1)
            for taskName2 in selectedTasks:
                t2 = self._examResults.getTask(taskName2)
                taskCorrelations.append(
                    corrcoef(t1.getPercentScoresList(),
                             t2.getPercentScoresList())[0][1])
            heatmapPlotData.append(taskCorrelations)
        RPlotUtil.drawHeatmap(heatmapPlotData, selectedTasks, selectedTasks,
                              'Task correlation heatmap')

        RPlotUtil.rDevOff()
        self._plotUrls.append(plotOutput.getURL())
        self._plotPaths.append(plotOutput.getDiskPath())
Exemplo n.º 11
0
    def addGradesHistogram(self):
        plotOutput = GalaxyRunSpecificFile(
            ['TaskScoreOverview', 'histogram2.png'], self._galaxyFN)
        plotOutput.openRFigure()
        dataDict = OrderedDict([(x, 0) for x in ExamResultsAnalysis.GRADES])
        studentList = self._examResults.getStudentList()
        for student in studentList:
            grade = student.getAttribute(ExamResultsAnalysis.COL_GRADE)
            dataDict[grade] += 1

        mainTitle = 'Histogram of grades'
        xTitle = ''
        yTitle = ''
        names = ExamResultsAnalysis.GRADES
        RPlotUtil.drawBarplot(dataDict.values(),
                              mainTitle,
                              xTitle,
                              yTitle,
                              names,
                              col='blue')
        RPlotUtil.rDevOff()
        self._plotUrls.append(plotOutput.getURL())
        self._plotPaths.append(plotOutput.getDiskPath())
Exemplo n.º 12
0
    def run(self, selectedTasks, **kwargs):

        taskScoreData = self._examResults.getTaskList()
        studentsData = self._examResults.getStudentList()

        if len(taskScoreData) == 0 or len(studentsData) == 0:
            print 'Empty data'
            return None

        from itertools import combinations
        for t1, t2 in combinations(selectedTasks, 2):

            plotOutput = GalaxyRunSpecificFile(
                ['TaskScatterPlotAnalysis', t1 + '_vs_' + t2 + '.png'],
                self._galaxyFN)
            plotOutput.openRFigure(h=600, w=720)

            task1 = self._examResults.getTask(t1)
            task2 = self._examResults.getTask(t2)
            self.drawTaskScatterPlot(task1, task2, noise=0.5)

            RPlotUtil.rDevOff()
            self._plotUrls.append(plotOutput.getURL())
            self._plotPaths.append(plotOutput.getDiskPath())
Exemplo n.º 13
0
    def execute(cls, choices, galaxyFn=None, username=''):
        '''
        Is called when execute-button is pushed by web-user. Should print
        output as HTML to standard out, which will be directed to a results page
        in Galaxy history. If getOutputFormat is anything else than HTML, the
        output should be written to the file with path galaxyFn. If needed,
        StaticFile can be used to get a path where additional files can be put
        (e.g. generated image files). choices is a list of selections made by
        web-user in each options box.
        '''

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

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

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

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

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

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

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

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

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

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

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

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

        core.tableFooter()

        print core
        print GalaxyInterface.getHtmlEndForRuns()
Exemplo n.º 14
0
    def _generateAnswerText(self, coreCls):
        onlyLocalPvals = self._results.hasOnlyLocalPvals()
        globalPval = self._results.getGlobalResult().get(
            self._results.getPvalKey()) if not onlyLocalPvals else None

        tableFile = GalaxyRunSpecificFile(['table.html'], galaxyFn='')
        localPvalsUrl = tableFile.getURL(
        )  #problematic dependency towards fn in tablePresenter..

        core = coreCls()
        core.styleInfoBegin(styleClass="infomessagesmall answerbox question")
        core.header('You asked:')
        core.line(str(coreCls().highlight(self._getHeader())))
        core.styleInfoEnd()

        #Simplistic answer
        core.styleInfoBegin(
            styleClass="infomessagesmall answerbox simplisticanswer")
        core.header(str(coreCls().link('Simplistic answer:', '#', \
                                        args='''onclick="return toggle('simplistic_answer_expl')"''')))

        core.styleInfoBegin(styleId="simplistic_answer_expl",
                            styleClass="infomessagesmall explanation")
        if onlyLocalPvals:
            core.line('''
Under "simplistic answer" you will find a simple statement on whether there were any findings for the local analysis. The number of significant bins at 10% false discovery rate (FDR) is provided.<br>
<br>
It is not possible to draw a decisive conclusion based on a p-value, so the statements are only meant as simple indications.<br>
                  ''')
        else:
            core.line('''
Under "simplistic answer" you will find a yes/maybe/no-conclusion answer to the question asked, based on a simple thresholding scheme on the p-value:<br>
"yes" if p-value < 0.01<br>
"maybe" if  0.01 < p-value < 0.1<br>
"no conclusion" if p-value > 0.1<br>
<br>
It is not possible to draw a decisive conclusion based on a p-value, so the statements are only meant as simple indications.<br>                  
                  ''')

        core.styleInfoEnd()

        if onlyLocalPvals:
            numSign, numTested, numIgnored = self._results.getFdrSignBins()
            if numSign == numTested and numSign != 0:
                simplisticPhrase = 'Yes - the data suggests this for all bins'
            elif numSign > 0:
                simplisticPhrase = 'Yes - the data suggests this at least in some bins'
                numSign, numTested, numIgnored = self._results.getFdrSignBins()
                simplisticPhrase += ' (%i significant bins out of %i, at %i%% FDR)' % (
                    numSign, numTested, self._results.FDR_THRESHOLD * 100)
            else:
                simplisticPhrase = 'No support from data for this conclusion in any bin'

            core.line(str(coreCls().highlight(simplisticPhrase)))
        else:
            assert globalPval is not None

            directionality = ''
            if self._results._isTwoSidedTest:
                tsValue, expTsValue = self._getTestStatisticAndExpectedValues()
                if tsValue is not None and expTsValue is not None and tsValue != expTsValue:
                    directionality = '(higher) ' if tsValue > expTsValue else '(lower) '

            if globalPval < 0.01:
                simplisticPhrase = 'Yes %s- the data suggests this' % directionality
            elif globalPval < 0.1:
                simplisticPhrase = 'Maybe %s- weak evidence' % directionality
            else:
                simplisticPhrase = 'No support from data for this conclusion'

            core.line(
                str(coreCls().highlight(simplisticPhrase + ' (p-value: ' +
                                        strWithStdFormatting(globalPval) +
                                        ')')))
        core.styleInfoEnd()

        #Precise answer
        core.styleInfoBegin(
            styleClass="infomessagesmall answerbox preciseanswer")
        core.header(str(coreCls().link('Precise answer:', '#', \
                                       args='''onclick="return toggle('precise_answer_expl')"''')))

        core.styleInfoBegin(styleId="precise_answer_expl",
                            styleClass="infomessagesmall explanation")
        if onlyLocalPvals:
            core.line('''
Significance testing evaluates a <b>null hypothesis (H0)</b> versus an <b>alternative hypothesis (H1)</b>. Low <b>p-values</b> are evidence against H0. The testing involves comparing the observed value of a  <b>test statistic</b> to the distribution of the test statistic under a <b>null model</b>. The testing was performed in each local bin, with a list of FDR-corrected p-values per bin provided.                  
                  ''')
        else:
            core.line('''
Significance testing evaluates a <b>null hypothesis (H0)</b> versus an <b>alternative hypothesis (H1)</b>. Low <b>p-values</b> are evidence against H0. The testing involves comparing the observed value of a  <b>test statistic</b> to the distribution of the test statistic under a <b>null model</b>. 
                  ''')

        core.styleInfoEnd()

        EffectSizeText = 'Please note that both the effect size and the p-value should be considered in order to assess the practical significance of a result.'

        FDR_text = '* False Discovery Rate: The expected proportion of false positive results among the significant bins is no more than %i%%.' \
                    % (self._results.FDR_THRESHOLD*100)

        if onlyLocalPvals:
            numSign, numTested, numIgnored = self._results.getFdrSignBins()

            core.line(str(coreCls().highlight('%i significant bins out of %i, at %i' \
                                              % (numSign, numTested, self._results.FDR_THRESHOLD*100) + '% FDR*')))
            core.line('')
            localPvalsLink = str(coreCls().link(
                'collection of FDR-corrected p-values per bin', localPvalsUrl))
            notComputeLink = str(coreCls().link('Not able to compute', '#', \
                                               args='''onclick="return toggle('no_global_pval_expl')"'''))
            core.line('A ' + localPvalsLink + ' was computed. ' +
                      notComputeLink + ' a global p-value for this analysis.')
            core.styleInfoBegin(styleId="no_global_pval_expl",
                                styleClass="infomessagesmall explanation")
            core.line('(Explanation to appear in box)')
            core.styleInfoEnd()

            if numIgnored > 0:
                core.line('')
                core.line('%s bin%s excluded due to lack of data.' %
                          (numIgnored, 's' if numIgnored > 1 else ''))

            core.line('')
            core.line(EffectSizeText)
            core.line('')
            core.line(FDR_text)

            h0h1Text = self._getH0andH1Text(coreCls)
            if h0h1Text is not None:
                core.divider(withSpacing=True)
                core.line('In each bin, the test of')
                core.append(h0h1Text)
                core.line('was performed.')
        else:
            h0h1Text = self._getH0andH1Text(coreCls)
            if h0h1Text is not None:
                core.line('The p-value is %s for the test' %
                          strWithStdFormatting(globalPval))
                core.append(h0h1Text)
            else:
                core.line('The p-value is %s.' %
                          strWithStdFormatting(globalPval))
                core.line('')
            core.line('Low p-values are evidence against H0.')

            numSign, numTested, numIgnored = self._results.getFdrSignBins()
            if numTested + numIgnored > 1:
                localPvalsLink = str(coreCls().link('each bin separately',
                                                    localPvalsUrl))
                excludeText = ' (%i bin%s excluded from FDR-analysis due to lacking p-values).' \
                              % (numIgnored, 's' if numIgnored>1 else '.') if numIgnored>0 else ''
                core.line('')
                core.line('The test was also performed for ' + localPvalsLink + \
                          ', resulting in %i significant bins out of %i, at %i%% FDR*' % (numSign, numTested, self._results.FDR_THRESHOLD*100) +\
                          excludeText)

            core.line('')
            core.line(EffectSizeText)
            core.line('')
            core.line(FDR_text)

        nullModel = self._results._nullModel
        if nullModel is not None:
            core.divider(withSpacing=True)
            core.line('P-values were computed under the %s defined by the following preservation and randomization rules:' \
                      % str(coreCls().highlight('null model')))
            core.paragraph(nullModel, indent=True)

        testStatistic = self._results.getTestStatisticText()
        if testStatistic != None:
            #pick out relevant part:
            mo = re.search('^[tT]est.[sS]tatistic ?:? ?', testStatistic)
            if mo != None:
                testStatistic = testStatistic[mo.end():]
                #if len(testStatistic)>0 and testStatistic[0]=='(':
                #testStatistic = testStatistic[1:]
                #if len(testStatistic)>0 and testStatistic[-1]==')':
                #testStatistic = testStatistic[:-1]

            tsValue, expTsValue = self._getTestStatisticAndExpectedValues()
            core.divider(withSpacing=True)
            core.line('The %s used is:' %
                      str(coreCls().highlight('test statistic')))
            core.paragraph(testStatistic, indent=True)

            if tsValue is not None:
                if expTsValue is not None:
                    core.line('The value of the test statistic is %s, which is %s the expected value: %s.' \
                                % (strWithStdFormatting(tsValue), \
                                   (str(coreCls().emphasize('higher')) + ' than' if tsValue > expTsValue else \
                                    (str(coreCls().emphasize('lower')) + ' than' if tsValue < expTsValue else \
                                     str(coreCls().emphasize('equal')) + ' to')), \
                                   strWithStdFormatting(expTsValue)))
                else:
                    core.line('The value of the test statistic is %s.' %
                              (strWithStdFormatting(tsValue)))

        #temporary solution, as lacking objects needed to construct note-link directly..
        noteText = ''
        if self._results._runDescription is not None:
            #mo = re.search('<note.*note>', self._results._runDescription)
            mo = re.search('<a href[^>]*/notes/[^>]*>[^<]*</a>',
                           self._results._runDescription)
            if mo is not None:
                noteLink = mo.string[mo.start():mo.end()]
                noteText = ' See ' + noteLink + ' for a more complete description of the test.'

        if noteText == '':
            logMessage(
                'Note-link not found in runDescription, and thus omitted from results'
            )

        core.divider(withSpacing=True)

        runDescLink = str(coreCls().link('run description', '#', \
                                         args='''onclick="return toggle('run_description')"'''))
        core.line('The p-values may be subject to further parameter choices, which are listed in the %s.' %\
                  (runDescLink) + noteText)
        core.divider(withSpacing=True)
        #resultsLink = str(coreCls().link('See full details', '#', \
        #args='''onclick="return toggle('results_box')"'''))
        resultsLink = str(coreCls().link('See full details', '#', \
                                         args='''onclick="$('.resultsbox').toggle()"'''))
        core.line(resultsLink + ' of the results in table form.')
        core.styleInfoEnd()

        return str(core)
Exemplo n.º 15
0
 def execute(cls, choices, galaxyFn=None, username=''):
     #val = strVal.split(':')[1].split('k')[0];
     htmlTemplate = '''<html><head>\n\n<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>\n  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>\n  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>\n
     <script type='text/javascript' src='https://www.google.com/jsapi'></script>
     <script type='text/javascript'> 
       google.load("visualization", "1", {packages:["corechart"]});\n google.setOnLoadCallback(drawLine);
       function drawLine(divId) {\n}
   </script>
     <style type="text/css">\n    #slider { margin: 10px; }\n  </style>\n  <script type="text/javascript">\n  jQuery(document).ready(function() {\n    jQuery("#slider").slider({min: 0, value: 370, max: %i });\n  });\n  </script>\n\n\n  <link rel="stylesheet" type="text/css" href="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/styles/stylesheet.css" />
                 \n<script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/scripts/mootools-1.2.1-core.js">\n</script><script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/scripts/mootools-1.2-more.js">\n</script><script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/scripts/ImageZoom.js"></script>\n
     \n\n\n\n<script type="text/javascript" >\nliste =%s;\ncounter = 0;\n\n\nfunction point_it2(event){\n
     document.myform.posAnchor.value = "";
     chrom = %s;\n
     trackNames = %s;
     pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("zoomer_image").offsetLeft;\n	pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("zoomer_image").offsetTop;\n        factor = %i;\n        pos_x = Math.floor(pos_x/factor);\n	pos_y = Math.floor(pos_y/factor);\n	counter++;\n
     var strVal = liste[pos_y][pos_x];
     var strTab = strVal.split(",");
     
     
     val = strTab[0];
     streng = chrom+":"+strTab[0]+"k | ";
     for(i=0; i<trackNames.length; i++) { 
         streng = streng + trackNames[i]+': '+strTab[i+1]+'%% | ';
        }
         
     document.myform.posAnchor.value = streng;\n
     jQuery( "#slider" ).slider( "option", "value", val );\n
     
             }\n</script>\n\n\n\n\n</head>
     <body>
     <h2 align="center" style="color:#FF7400;">Heatmap for chromosome %s</h2> 
     <div id="slider" ></div><br>
     \n<form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">\n<div align="center">\n\n<input type="text" name="posAnchor" size="250" value=".">\n<br>\n</div>\n</form>\n<br>
     <div id="container"><!-- Image zoom start --><div id="zoomer_big_container"></div><div id="zoomer_thumb">\n<a href="%s" target="_blank" >\n<img src="%s" /></a></div><!-- Image zoom end --></div>\n\n\n%s
      
      <br/>%s</body></html>''' # onchange="jQuery('zoomer_region').css({ 'left': '31px', 'top': '15px'});"
     
     tableRowEntryTemplate = """<div class="tabbertab"><h2>%s</h2><a href="%s"><img src="%s" /></a></div>"""
     htmlPageTemplate = """<html><head>\n<script type="text/javascript" src="/gsuite/static/scripts/tabber.js"></script>\n<link href="/gsuite/static/style/tabber.css" rel="stylesheet" type="text/css" />\n
                 </head><body>%s</body></html>"""
     
     #fileDict = dict()
     binsize = parseShortenedSizeSpec(choices[10])
         
     tnList = []
     trackNameList = []
     genome = choices[0]
     chrLength = GenomeInfo.getStdChrLengthDict(genome)
     
     for index in [1,4,7]:
         startTime = time.time()
         if choices[index] in ['-- No track --','',None]:
             tnList.append(None)
             trackNameList.append('.')
             continue
         elif choices[index] == 'history':
             #trackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(choices[0], choices[index+1].split(':'))
             trackName = choices[index+1].split(':')
             tnList.append(ExternalTrackManager.extractFnFromGalaxyTN(trackName))
             trackNameList.append(prettyPrintTrackName(trackName))
         else:
             trackName = choices[index+2].split(':')
             track = PlainTrack(trackName)
             regionList = [GenomeRegion(genome, chrom, 0, chrLength[chrom]) for chrom in GenomeInfo.getChrList(genome)]
             tnList.append((track, regionList))    
             trackNameList.append(prettyPrintTrackName(trackName))
             
             
     trackNames = repr([v for v in trackNameList if v!='.'])
     tr1, tr2, tr3 = tnList
     ResultDicts = []
     maxVals = []#list of the maximum coverage in a bin for each track Used for normalization purposes
     chrsWithData = set()# set of chromosomes with any data. No point in generating images with no data...
     microDictList = []
     counter = 0
     for tr,color in [(tr1, (1,0,0)),(tr2, (0,1,0)),(tr3, (0,0,1))]:
         
         maxVal = None
         if tr:
             if len([1 for v in tnList if v]) == 1:
                 color = (1,1,1)
             
             res, microDict, maxVal, trackChrs = cls.getValuesFromBedFile(genome, tr,color, binsize)
             microDictList.append((trackNames[counter],microDict))
             counter+=1
             chrsWithData = chrsWithData|trackChrs
             ResultDicts += [res]   
         maxVals.append(maxVal)
         
     
     htmlTableContent = []
     resultDict = cls.syncResultDict(ResultDicts)
     binfactor = binsize/1000
     for chrom in sorted(list(chrsWithData), cmp=alphanum):
         valList = resultDict[chrom]
         areaList = []
         #For doing recursive pattern picture
         bigFactor = int(10*(binsize/10000.0)**(0.5))
         smallFactor = bigFactor/3
         posMatrix = cls.getResult(len(valList), 2,2)
         javaScriptList = [[0 for v in xrange(len(posMatrix[0])*bigFactor) ] for t in xrange(len(posMatrix)*bigFactor)]
         rowLen = len(posMatrix[0])
         
         im = Image.new("RGB", (rowLen, len(posMatrix)), "white")
         for yIndex, row in enumerate(posMatrix):
             for xIndex, elem in enumerate(row):
                 im.putpixel((xIndex, yIndex), valList[elem])
                 region = yIndex*rowLen + xIndex
                 #for yVals in range(yIndex*bigFactor, (yIndex+1)*bigFactor):
                 #    for xVals in range(yIndex*bigFactor, (yIndex+1)*bigFactor):
                 #        javaScriptList[yVals][xVals] = chrom+':'+str(elem)+'-'+str(elem+1)+': '+repr([ v/255.0 for v in valList[elem]])
                 
                 #javaScriptList[yIndex][xIndex] = chrom+':'+str(elem*binfactor)+'k - '+str((elem+1)*binfactor)+'k : '+repr([ trackNameList[indx]+'='+str(round(v*100/255.0, 2))+'%' for indx, v in enumerate(valList[elem])])
                 javaScriptList[yIndex][xIndex] = ','.join([str(elem*binfactor)]+[ str(round(v*100/255.0, 2)) for indx, v in enumerate(valList[elem]) if trackNameList[indx] !='.'] )
         for i in range(len(javaScriptList)):
             javaScriptList[i] = [v for v in javaScriptList[i] if v !=0]
             
     
         imSmall = im.resize((len(posMatrix[0])*smallFactor, len(posMatrix)*smallFactor))
         im2 = im.resize((len(posMatrix[0])*bigFactor, len(posMatrix)*bigFactor))
         
         fileElements = [GalaxyRunSpecificFile(['Recursive', chrom+'.png' ], galaxyFn ), GalaxyRunSpecificFile(['Recursive', chrom+'Big.png' ], galaxyFn), GalaxyRunSpecificFile(['Recursive', chrom+'Zooming.html' ], galaxyFn)]
         #fileDict['Recursive/'+chrom] = fileElements
         imSmall.save(fileElements[0].getDiskPath(ensurePath=True))
         im2.save(fileElements[1].getDiskPath(ensurePath=True))
         
         trackAndValRangeTab = zip(trackNameList, maxVals)
         colorTab = []
         onlyOneTrack = True if len([v for v in maxVals if v]) ==1 else False
         for color, vals in [('Red_combination',[1,0,0]), ('Green_combination',[0,1,0]), ('Blue_combination',[0,0,1]),('Red-Green_combination',[1,1,0]), ('Red-Blue_combination',[1,0,1]), ('Green-Blue_combination',[0,1,1]), ('Red-Green-Blue_combination',[1,1,1])]:    
             
             if not None in [maxVals[i] for i in range(len(vals)) if vals[i]>0]:
                 im = Image.new("RGB", (256 , 1), "white")
                 tracksInvolved = ' & '.join([str(index+1) for index, v in enumerate(vals) if v>0])
                 if onlyOneTrack:
                     vals = [1,1,1]
                 for val in range(256):
                     colVal = [val*v for v in vals]
                     
                     im.putpixel((val,0), tuple(colVal))
                 imColFile = GalaxyRunSpecificFile(['Recursive', color+'.png' ], galaxyFn)
                 imCol = im.resize((256, 10))
                 imCol.save(imColFile.getDiskPath(ensurePath=True))
                 colorTab.append('<tr><td>Track %s</td><td>  <img src="%s" /></td></tr>'% (tracksInvolved, imColFile.getURL()))
                 
         
         htmlTnRangeVals= '<br/><br/><table align="center"  cellspacing="10"><tr><th>Track number</th><th>Track name</th><th>Value range</th></tr>\n'
         htmlTnRangeVals += '\n'.join(['<tr/><td>Track %i </td><td>%s</td><td> 0 - %i</td></tr>' % (index+1, v[0], v[1]) for index, v in  enumerate(trackAndValRangeTab) if v[1]] )
         htmlTnRangeVals+='</table> <br/><table align="center"  cellspacing="10"><tr><th>Track combination</th><th>Colour range</th></tr>' + '\n'.join(colorTab) + '</table>\n'
         lineTabStr= ''
         #if chrom == 'chr1':
         #    tempList = [range(100)]+[v[1]['chr1'][26] for v in microDictList]
         #    chartTemplate =  "['%i',  %i, %i, %i]"
         #    lineTab = [ chartTemplate % v for v in zip(*tempList)]    
         #    lineTemplate = """<div id="%s" onclick="{\nvar data = google.visualization.arrayToDataTable([\n    %s\n  ]);\nvar options = {  title: 'Detailed Graph'    };var chart = new google.visualization.LineChart(document.getElementById('%s'));chart.draw(data, options);}" style="width: 1000px; height: 700px;"></div>"""
         #    lineTabStr = lineTemplate % ('line_div', ', '.join(lineTab),'line_div')    
         open(fileElements[2].getDiskPath(ensurePath=True),'w').write(htmlTemplate % (int(GenomeInfo.getChrLen(genome, chrom)/1000.0)+1, repr(javaScriptList), repr(chrom), trackNames,bigFactor, chrom, fileElements[1].getURL(), fileElements[0].getURL(), htmlTnRangeVals, lineTabStr) )# 
         htmlTableContent.append(tableRowEntryTemplate % (chrom, fileElements[2].getURL(), fileElements[0].getURL()))
         
         # FOr doing normal picture
         #columns = int(round((len(valList)/1000)+0.5))
         #im = Image.new("RGB", (1000, columns), "white")        
         #y=-1    
         #for index, valuTuple in enumerate(valList):
         #    x = index%1000
         #
         #    if x == 0:
         #        y+=1
         #    try:
         #        im.putpixel((x, y), valuTuple)
         #    except:
         #        pass
         #im.save(chrom+'.png')
         #htmlTableContent.append(tableRowEntryTemplate % (chrom, chrom+'.png'))
     
     tabberMal = '<div class="tabber">%s</div>'
     #tempRes, res = [],[]
     res = [tabberMal % v for v in htmlTableContent]
     #for i in htmlTableContent:
     #    if len(tempRes) == 10:
     #        res.append(tabberMal % '\n'.join(tempRes))
     #        tempRes = []
     #    tempRes.append(i)
     #if len(tempRes)>0:
     #    res.append(tabberMal % '\n'.join(tempRes))
     open(galaxyFn,'w').write(htmlPageTemplate % ('<br/>'.join(res)))
Exemplo n.º 16
0
    def run(self, analysis, selectedTasks, **kwargs):

        taskScoreData = self._examResults.getTaskList()
        studentsData = self._examResults.getStudentList()

        if len(taskScoreData) == 0 or len(studentsData) == 0:
            print 'Empty data'
            return None

        examMaxScore = 0.0
        for task in taskScoreData:
            examMaxScore += task.getMaxScore()

        rawDataDict = self.getTaskScoresPerGradeRawData(
            taskScoreData, studentsData)
        if analysis == self.ANALYSIS_AVG_SCORE_PER_GRADE_LINE_PLOT:
            plotDataDict = self.getPlotDataFromRawData(rawDataDict,
                                                       selectedTasks,
                                                       examMaxScore)
            maxPercentage = 10
            for taskScores in plotDataDict.values():
                if max(taskScores) > maxPercentage:
                    maxPercentage = max(taskScores)
            plotOutput = GalaxyRunSpecificFile(
                ['IndividualTaskAnalysis', 'lineplot.png'], self._galaxyFN)
            plotOutput.openRFigure(h=600, w=720)
            xTitle = 'Task'
            yTitle = 'Avg score per grade in percentage from total exam score'
            RPlotUtil.drawLineplot(plotDataDict, analysis, selectedTasks,
                                   maxPercentage, xTitle, yTitle)
            RPlotUtil.rDevOff()
            self._plotUrls.append(plotOutput.getURL())
            self._plotPaths.append(plotOutput.getDiskPath())

        elif analysis == self.ANALYSIS_SCORE_DISTRIBUTION_PER_GRADE:
            for taskName in selectedTasks:
                plotOutput = GalaxyRunSpecificFile(
                    ['IndividualTaskAnalysis', taskName + '_vioplot.png'],
                    self._galaxyFN)
                plotOutput.openRFigure(h=600, w=720)
                task = self._examResults.getTask(taskName)
                maxScore = task.getMaxScore()
                plotDataMatrix = []
                for grade in self.GRADES:
                    plotDataMatrix.append(rawDataDict[grade][taskName])

                mainTitle = 'Task: ' + taskName + ' - Vioplot of task scores per grade'
                xTitle = 'Grade'
                yTitle = 'Percentage score (from max task score)'
                vioplotColor = 'magenta'
                xAxisAt = [x + 1 for x in range(len(self.GRADES))]
                xLimMin = 0
                xLimMax = len(self.GRADES) + 1
                xLas = 1
                from math import ceil
                yAxisAt = range(0, int(ceil(maxScore)))
                yLimMin = 0
                yLimMax = maxScore
                yLas = 1

                #plot is used to setup the chart for vioplot so we can control the x and y labels
                RPlotUtil.drawVioplot(plotDataMatrix, self.GRADES, mainTitle,
                                      xTitle, yTitle, vioplotColor, xAxisAt,
                                      xLimMin, xLimMax, xLas, yAxisAt, yLimMin,
                                      yLimMax, yLas)

                RPlotUtil.rDevOff()
                self._plotUrls.append(plotOutput.getURL())
                self._plotPaths.append(plotOutput.getDiskPath())
        elif analysis == self.ANALYSIS_HISTOGRAM_PLOT:
            data = []
            for taskName in selectedTasks:
                task = self._examResults.getTask(taskName)
                data.append(task.getPercentScoresList())

            plotOutput = GalaxyRunSpecificFile(
                ['IndividualTaskAnalysis', 'histogram.png'], self._galaxyFN)
            plotOutput.openRFigure(h=600, w=720)

            mainTitle = 'Task scores distribution'
            xTitle = 'Task scores 0%-100%'
            yTitle = 'Score count (per bin)'
            colors = RPlotUtil.getRainbowColors(len(selectedTasks))
            RPlotUtil.drawMultiHistogram(data,
                                         mainTitle,
                                         xTitle,
                                         yTitle,
                                         names=selectedTasks,
                                         colors=colors,
                                         hasLegend=True)

            RPlotUtil.rDevOff()
            self._plotUrls.append(plotOutput.getURL())
            self._plotPaths.append(plotOutput.getDiskPath())

        else:
            #         elif analysis == self.ANALYSIS_BIN_AVG_SMOOTHED_PLOT:
            #sanity check
            examMaxScore = self._examResults.getExamMaxScore()
            if examMaxScore == 0:
                raise InvalidDataException(
                    'Exam max score must be larger than 0. Most probably no task were defined in the results input file.'
                )

            bins = int(kwargs['bins'])
            displayPoints = bool(kwargs['displayPoints'])
            spar = float(kwargs['spar'])
            verticalLines = kwargs['verticalLines']

            colors = RPlotUtil.getRainbowColors(len(selectedTasks))
            plotOutput = GalaxyRunSpecificFile(
                ['IndividualTaskAnalysis', 'smoothed_line_plot.png'],
                self._galaxyFN)
            plotOutput.openRFigure(h=600, w=720)
            xData = []
            students = self._examResults.getStudentList()
            for student in students:
                xData.append(
                    self._examResults.getExamScorePercentageForStudent(
                        student.getStudentNr()))

            mainTitle = 'Smoothed line plot - task score vs exam score'
            if analysis == self.ANALYSIS_BIN_AVG_SMOOTHED_PLOT:
                mainTitle += ' (Bin average)'
            else:
                mainTitle += ' (Moving average)'
            xTitle = 'Exam score (%)'
            yTitle = 'Task score (%)'
            xLim = [0, 100]
            yLim = [0, 100]
            RPlotUtil.drawEmptyPlot(xTitle, yTitle, mainTitle, xLim, yLim)
            RPlotUtil.drawLegend('topleft', selectedTasks, colors)

            for taskName, col in zip(selectedTasks, colors):
                task = self._examResults.getTask(taskName)
                yData = []
                for student in students:
                    yData.append(
                        task.getPercentageScore(student.getStudentNr()))

                if analysis == self.ANALYSIS_BIN_AVG_SMOOTHED_PLOT:
                    RPlotUtil.drawBinnedSmoothedLinePlot(
                        xData,
                        yData,
                        col=col,
                        bins=bins,
                        displayPoints=displayPoints,
                        spar=spar)
                else:  #analysis == self.ANALYSIS_AVG_SCORE_PER_GRADE_LINE_PLOT
                    RPlotUtil.drawMovingAvgSmoothedLinePlot(
                        xData,
                        yData,
                        col,
                        displayPoints=displayPoints,
                        spar=spar)

            if verticalLines:
                for verticalLine in verticalLines:
                    RPlotUtil.drawVerticalLine(verticalLine)

            RPlotUtil.rDevOff()
            self._plotUrls.append(plotOutput.getURL())
            self._plotPaths.append(plotOutput.getDiskPath())
    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 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.
        '''

        #print 'Executing...'
        genome = choices[0]
        galaxyTN = choices[1].split(':')
        #print 'galaxyTN: ',galaxyTN , type(galaxyTN)
        fn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN)
        print 'Disk path for selected history element: <br>%s<br><br>' % fn
        from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
        staticFile = GalaxyRunSpecificFile([],fn)
        print 'HyperBrowser data path (static file path) corresponding to selected history element: <br>%s<br><br>' % staticFile.getDiskPath()

        from config.Config import EXT_PROCESSED_DATA_PATH
        try:
            tn = ExternalTrackManager.getStdTrackNameFromGalaxyTN(galaxyTN)[:-1]
            preProcPath = '/'.join([EXT_PROCESSED_DATA_PATH,'100000','noOverlaps',genome] + tn)
            import os
            lastTnPart = os.listdir(preProcPath)
            assert len(lastTnPart)==1, lastTnPart
            #preProcPath += '/'+lastTnPart[0]
            tn += lastTnPart
            preProcPath = '/'.join([EXT_PROCESSED_DATA_PATH,'100000','noOverlaps',genome] + tn)

            print 'HyperBrowser pre-processed track name (if applicable): <br>%s<br><br>' % tn
            print 'HyperBrowser pre-processed track path (if applicable): <br>%s<br><br>' % preProcPath
        except:
            print '(Not printing track name, as history element does not appear to be a (valid) track.)<br>'
        print 'HyperBrowser URL path (static file URL) corresponding to selected history element: <br>%s<br><br>' % staticFile.getURL()

        # Does not work correctly, as the id needs to be the history id, and not the dataset id,
        # which may be different. Dataset id may perhaps be found from history id in the following manner (untested):
        #
        # data = trans.sa_session.query( trans.app.model.HistoryDatasetAssociation ).get( trans.security.decode_id( history_id ) )
        # dataset_id = extractIdFromGalaxyFn(data.file_name)
        #
        # Not sure how to go the other way.
        #
        #from quick.util.CommonFunctions import extractIdFromGalaxyFn
        #from galaxy.web.security import SecurityHelper
        #from config.Config import GALAXY_ID_SECRET
        #secHelper = SecurityHelper(id_secret=GALAXY_ID_SECRET)
        #id = extractIdFromGalaxyFn(galaxyFn)
        #encodedId = secHelper.encode_id(id[1])
        #print 'Galaxy URL id: <br>%s<br><br>' % encodedId

        import os.path, time
        print "Time of creation for Galaxy history file: %s" % time.ctime(os.path.getctime(fn))
        print "Last modification for Galaxy history file: %s" % time.ctime(os.path.getmtime(fn))
Exemplo n.º 18
0
    def execute(cls, choices, galaxyFn=None, username=''):
        genome = choices.genome
         

        from quick.multitrack.MultiTrackCommon import getGSuiteDataFromGalaxyTN
        trackTitles, refTrackNameList, genome = getGSuiteDataFromGalaxyTN(choices.gsuite)
        
        queryTrackName = ExternalTrackManager.extractFnFromGalaxyTN(choices.targetTrack)
        if choices.isBasic:
            suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(choices.targetTrack, False)
            regSpec = suffix
            binSpec = queryTrackName
        else:
            regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)
        
        #targetTrack = choices.targetTrack.split(':')
        #targetTrackTitle = targetTrack[-1]
        #print targetTrackTitle
        #
        #binSpec = targetTrackTitle
        #Phenotype and disease associations:Assorted experiments:Virus integration, HPV specific, Kraus and Schmitz, including 50kb flanks

        from gold.gsuite.GSuiteConstants import TITLE_COL
        from gold.gsuite.GSuite import GSuite
        from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
        from gold.gsuite.GSuiteEditor import selectColumnsFromGSuite
        staticFile=[]
        
        results = []
        for refTrack in refTrackNameList:
            analysisDef = '-> ProportionCountStat' #ProportionCountStat #CountStat
            res = GalaxyInterface.runManual([refTrack], analysisDef, regSpec, binSpec, genome, username=username, galaxyFn=galaxyFn, printRunDescription=False, printResults=False, printProgress=False)
            segCoverageProp = [res[seg]['Result'] for seg in res.getAllRegionKeys()]
            results.append(segCoverageProp)
            
            regFileNamer = GalaxyRunSpecificFile(refTrack, galaxyFn)
            staticFile.append([regFileNamer.getLink('Download bed-file'), regFileNamer.getLoadToHistoryLink('Download bed-file to History')])

        refGSuite = getGSuiteFromGalaxyTN(choices.gsuite)

        if TITLE_COL == choices.selectColumns:
            selected = trackTitles
        else:
            selected = refGSuite.getAttributeValueList(choices.selectColumns)

        yAxisNameOverMouse=[]
        metadataAll =[]

        for x in range(0, len(selected)):
            if selected[x] == None:
                yAxisNameOverMouse.append(str(trackTitles[x]) + ' --- ' + 'None')
            else:
                if TITLE_COL == choices.selectColumns:
                    yAxisNameOverMouse.append(selected[x].replace('\'', '').replace('"', ''))
                else:
                    metadata = str(selected[x].replace('\'', '').replace('"', ''))
                    yAxisNameOverMouse.append(str(trackTitles[x]) + ' --- ' + metadata)
                    metadataAll.append(metadata)

        colorListForYAxisNameOverMouse = []
        if len(metadataAll) > 0:
            import quick.webtools.restricted.visualization.visualizationGraphs as vg
            cList = vg.colorList().fullColorList()
            uniqueCList = list(set(metadataAll))

            for m in metadataAll:
                colorListForYAxisNameOverMouse.append(cList[uniqueCList.index(m)])

        #startEnd - order in res
        startEndInterval = []
        startEnd = []
        i=0
        

        extraX=[]
        rowLabel = []
        for ch in res.getAllRegionKeys():
            rowLabel.append(str(ch.chr) + ":" + str(ch.start) + "-" + str(ch.end) + str(' (Pos)' if ch.strand else ' (Neg)'))
            if not i==0 and not i==len(res.getAllRegionKeys())-1:
                start = ch.start
                if start-end > 0:
                    startEnd.append(start-end)
                else:
                    startEnd.append('null')
                    extraX.append("""{ color: 'orange', width: 5, value: '""" + str(i-0.5) + """' }""")
                startEndInterval.append(ch.end - ch.start)
            else:
                startEndInterval.append(ch.end - ch.start)
            end = ch.end
            i+=1

        extraXAxis='plotLines: [ '
        extraXAxis = extraXAxis + ",".join(extraX)
        extraXAxis = extraXAxis + """ ],  """

        #rowLabel = res.getAllRegionKeys()
        #rowLabel = [str(x) for x in rowLabel]
        

        import quick.webtools.restricted.visualization.visualizationPlots as vp

        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.divBegin(divId='results-page')
        htmlCore.divBegin(divClass='results-section')
        htmlCore.divBegin('plotDiv')
        htmlCore.line(vp.addJSlibs())
        htmlCore.line(vp.useThemePlot())
        htmlCore.line(vp.addJSlibsExport())
        htmlCore.line(vp.axaddJSlibsOverMouseAxisisPopup())
        #vp.addGuideline(htmlCore)
        htmlCore.line(vp._addGuidelineV1())

        htmlCore.line(vp.addJSlibsHeatmap())

        from config.Config import DATA_FILES_PATH

        from proto.StaticFile import StaticFile, GalaxyRunSpecificFile

        #sf = GalaxyRunSpecificFile(['result.txt'], galaxyFn)
        #outFile = sf.getDiskPath(ensurePath=True)

        htmlCore.divBegin()
        writeFile = open(
            cls.makeHistElement(galaxyExt='tabular',
                                title='result'), 'w')
        # htmlCore.link('Get all results', sf.getURL())
        htmlCore.divEnd()

        i = 0

        writeFile.write('Track' + '\t' + '\t'.join(rowLabel)+ '\n')
        for rList in results:
            writeFile.write(str(yAxisNameOverMouse[i]) + '\t' + '\t'.join([str(r) for r in rList]) + '\n')
            i+=1




        fileOutput = GalaxyRunSpecificFile(['heatmap.png'],
                                           galaxyFn)
        ensurePathExists(fileOutput.getDiskPath())

        fileOutputPdf = GalaxyRunSpecificFile(['heatmap.pdf'],
                                              galaxyFn)
        ensurePathExists(fileOutputPdf.getDiskPath())

        cls.generateStaticRPlot(results, colorListForYAxisNameOverMouse, rowLabel, yAxisNameOverMouse,
                                colorMaps[choices.colorMapSelectList],
                                fileOutput.getDiskPath(), fileOutputPdf.getDiskPath())


        htmlCore.divBegin(divId='heatmap', style="padding: 10px 0 px 10 px 0px;margin: 10px 0 px 10 px 0px")
        htmlCore.link('Download heatmap image', fileOutputPdf.getURL())
        htmlCore.divEnd()

        if len(results) * len(results[1]) >= 10000:
            htmlCore.image(fileOutput.getURL())


        else:

            min = 1000000000
            max = -1000000000
            for rList in results:
                for r in rList:
                    if min > r:
                        min = r
                    if max < r:
                        max = r




            if max-min != 0:
                resultNormalised = []
                for rList in results:
                    resultNormalisedPart = []
                    for r in rList:
                        resultNormalisedPart.append((r-min)/(max-min))
                    resultNormalised.append(resultNormalisedPart)

                addText = '(normalised to [0, 1])'
            else:
                resultNormalised = results
                addText = ''


            hm, heatmapPlotNumber, heatmapPlot = vp.drawHeatMap(
                                                    resultNormalised,
                                                    colorMaps[choices.colorMapSelectList],
                                                    label='this.series.xAxis.categories[this.point.x] + ' + "'<br >'" + ' + yAxisNameOverMouse[this.point.y] + ' + "'<br>Overlap proportion" + str(addText) + ": <b>'" + ' + this.point.value + ' + "'</b>'",
                                                    yAxisTitle= 'Reference tracks',
                                                    categories=rowLabel,
                                                    tickInterval=1,
                                                    plotNumber=3,
                                                    interaction=True,
                                                    otherPlotNumber=1,
                                                    titleText='Overlap with reference tracks for each local region',
                                                    otherPlotData=[startEnd, startEndInterval],
                                                    overMouseAxisX=True,
                                                    overMouseAxisY=True,
                                                    yAxisNameOverMouse=yAxisNameOverMouse,
                                                    overMouseLabelY=" + 'Track: '" + ' + this.value + ' + "' '" + ' + yAxisNameOverMouse[this.value] + ',
                                                    overMouseLabelX = ' + this.value.substring(0, 20) +',
                                                    extrOp = staticFile
                                                    )
            htmlCore.line(hm)
            htmlCore.line(vp.drawChartInteractionWithHeatmap(
                [startEndInterval, startEnd],
                tickInterval=1,
                type='line',
                categories=[rowLabel, rowLabel],
                seriesType=['line', 'column'],
                minWidth=300,
                height=500,
                lineWidth=3,
                titleText=['Lengths of segments (local regions)','Gaps between consecutive segments'],
                label=['<b>Length: </b>{point.y}<br/>', '<b>Gap length: </b>{point.y}<br/>'],
                subtitleText=['',''],
                yAxisTitle=['Lengths','Gap lengths'],
                seriesName=['Lengths','Gap lengths'],
                xAxisRotation=90,
                legend=False,
                extraXAxis=extraXAxis,
                heatmapPlot=heatmapPlot,
                heatmapPlotNumber=heatmapPlotNumber,
                overMouseAxisX=True,
                overMouseLabelX = ' + this.value.substring(0, 20) +'
                ))


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

        htmlCore.hideToggle(styleClass='debug')

        print htmlCore
Exemplo n.º 19
0
    def execute_batch(cls, choices, galaxyFn=None, username=''):
        print GalaxyInterface.getHtmlBeginForRuns(galaxyFn)
        html = HtmlCore()
        html.header('Batch run results')

        refSnps = cls.get_ref_snp(choices)
        #print refSnps

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

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

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

        results.close()
        html.tableFooter()

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

        print '<p><b>' + results_tsv.getLink('Download results') + '</b></p>'
        print html
        print GalaxyInterface.getHtmlEndForRuns()
Exemplo n.º 20
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.gtr
        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.
        '''
        messageSep = '##'
        start = time.time()
        outputFile = open(galaxyFn, "w")
        params = [
            'username:='******'password:='******'operation:=' + choices[2]
        ]

        if choices[2] == 'List Projects':
            params += ['class:=userMngtService']
            ListStorebioProjects.socket.send(messageSep.join(params))
            message = ListStorebioProjects.socket.recv_unicode().encode(
                'utf-8')  #, 'ignore'
            if message == 'something went wrong...':
                return None
            else:
                print >> outputFile, message  #ListStorebioProjects.ParseProjectListXmlDoc(minidom.parseString(message))

        elif choices[2] == 'Download file from Dataset':
            outputFile = open(galaxyFn, "w")

            filPathTab = []

            for i in range(9, 15):
                if choices[i] and choices[i].find(',') > 0:
                    filPathTab.append(choices[i].split(',')[0])
            dataSets = pickle.loads(unquote(choices[3]))
            datasetId = [ds.id for ds in dataSets if ds.name == choices[4]][0]
            #datasetId = choices[4].split('(')[-1].split(')')[0].strip()
            subList = [{
                'Subtype': choices[5],
                'FileToExtract': '/'.join(filPathTab)
            }]  #
            paramlist = ['username:='******'password:='******'operation:=GetFileUrlsFromDataSet','class:=dataStorageService',\
                'params:='+'<#>'.join([ repr(datasetId), repr(subList)])]
            ListStorebioProjects.socket.send(messageSep.join(paramlist))
            url = ListStorebioProjects.socket.recv_unicode().encode(
                'ascii', 'ignore')

            fileType = url.split('.')[-1].strip()
            if fileType == 'zip':
                validFiles = [k for k, v in choices[7].items() if v]

                remotezip = urllib2.urlopen(url)
                zipinmemory = cStringIO.StringIO(remotezip.read())
                zip = zipfile.ZipFile(zipinmemory)

                for fn in zip.namelist():
                    if fn in validFiles:
                        print >> outputFile, zip.read(fn)
                        print >> outputFile, "\n\n\n\n\n\n\n"
            else:
                ftpList = [
                    v.strip().split()[-1]
                    for v in urllib2.urlopen(url).read().split('\n')
                    if v.strip() != ''
                ]
                for relativeUrl in ftpList:
                    fileType = relativeUrl.split('.')[-1].strip()
                    if fileType in ['png', 'jpg', 'jpeg', 'gif']:
                        picFile = GalaxyRunSpecificFile([relativeUrl],
                                                        galaxyFn)
                        #CHUNKS = 512*1024
                        open(picFile.getDiskPath(ensurePath=True), 'wb').write(
                            urllib2.urlopen(url + relativeUrl).read())
                        #with open(picFile.getDiskPath(ensurePath=True), 'wb') as fp:
                        #    fileContent = req.read(CHUNKS)
                        #    if not  fileContent:
                        #        break
                        #    fp.write(fileContent)
                        print >> outputFile, '<img src="%s" width="500px" height="600px"/>' % picFile.getURL(
                        )
                    else:
                        print >> outputFile, urllib2.urlopen(
                            url + relativeUrl).read()

        else:
            params += ['class:=dataStorageService']
            ListStorebioProjects.socket.send(messageSep.join(params))
            message = ListStorebioProjects.socket.recv_unicode().encode(
                'ascii', 'ignore')
            print >> outputFile, ListStorebioProjects.ParseDatasetsXmlDoc(
                minidom.parseString(message))

        outputFile.close()
Exemplo n.º 21
0
    def buildfigure(cls):

        fig = Figure(figsize=(10, 6))

        uniform_x_values = arange(len(cls.y_values[0]))
        lines = 0

        ax = fig.add_axes([0.028, 0.311, 0.75, 0.65])  # Create axes for plot

        if (len(uniform_x_values) > 0):

            colors = ["r-", "k-", "b-", "g-", "c-", "m-", "y-"]

            ax2 = ax.twinx()  # To get vertical axes on both sides.
            # Needed for showing coverage and array units on different axes.

            ax2.yaxis.set_ticks_position("left")
            #        ax.yaxis.set_ticks_position("right")

            for t in ax2.get_yticklabels():
                t.set_fontsize(6)

            for item in cls.y_values:
                ind = lines % 7
                #print len(uniform_x_values), len(item)
                line = ax2.plot(uniform_x_values,
                                item,
                                '0.4',
                                markersize=2,
                                markerfacecolor='g',
                                label=cls.samples[lines],
                                linewidth=0.8,
                                zorder=2)
                lines = lines + 1

            prop = matplotlib.font_manager.FontProperties(size=6)

            if (cls.transcript_id == "0"):
                ax.set_title(cls.associated_gene_name + " / " + cls.gene_id,
                             fontsize=8)
            else:
                ax.set_title(cls.transcript_id, fontsize=8)

            ax2.yaxis.grid(True)

            ax2.axis([
                uniform_x_values[0],
                uniform_x_values[len(uniform_x_values) - 1], 0, 15
            ])
            ax.set_yticklabels([])

            ax.xaxis.tick_bottom()
            ax.set_xticks(
                uniform_x_values)  #set these the same as x input into plot

            exons_no = range(1, len(cls.exons) + 1)

            for i in range(0, len(cls.exons)):
                cls.probesets[i] = str(cls.probesets[i]) + " / " + cls.exons[i]
                labels = ax.set_xticklabels(cls.probesets)
                for label in labels:
                    label.set_rotation(90)
                    label.set_fontsize(6)

            for t in ax.get_yticklabels():
                t.set_fontsize(6)

        else:  # Give explanation if no data is present in the database.
            ax.text(0.1,
                    0.8,
                    'Currently no microarray data\navailable for ' + gene_id,
                    size='9',
                    horizontalalignment='left',
                    verticalalignment='top',
                    multialignment='center')
            ax.axis('off')

        canvas = FigureCanvasAgg(fig)

        tempfilenum, tempfilename = tempfile.mkstemp(
            suffix='.png')  #function print_figure below requires a suffix
        canvas.print_figure(tempfilename, dpi=150)
        imageFile = GalaxyRunSpecificFile(['Image', 'resultImage.png'],
                                          galaxyFn)
        #magefile=file(tempfilename,'rb')
        #print "Content-type: image/png\n"
        #imagefile.seek(0)
        #print imagefile.read()
        fig.savefig(imageFile.getDiskPath(ensurePath=True))
        #fig.savefig(sys.stdout)

        #imageFile.close()
        print '<img src="%s">' % (imageFile.getURL())