Пример #1
0
    def execute(cls, choices, galaxyFn=None, username=''):
        genome = choices[0]
        outputFile = open(galaxyFn, "w")

        index2name = ast.literal_eval(choices[2])
        name2index = dict([(name, idx)
                           for idx, name in index2name.iteritems()])
        galaxyTnList = [tn.split(':') for tn in choices[1].values() if tn]
        #print>>outputFile,'\n'.join([str(i)+':\t'+str(v) for i, v in enumerate(choices)])
        #trackIndexStart = cls.NUM_CATEGORY_FIELDS*2 + 2
        for ind, galaxyTn in enumerate(galaxyTnList):
            name = ExternalTrackManager.extractNameFromHistoryTN(galaxyTn)
            fnSource = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn)
            category = choices[2 * name2index[name] + 4]

            for i in open(fnSource, 'r'):
                if any(i.startswith(x) for x in ['#', 'track']):
                    continue

                linetab = i.strip().split('\t')
                linetab.insert(3, category)
                if len(linetab) > 4:
                    linetab.pop(4)
                print >> outputFile, '\t'.join(linetab)

        outputFile.close()
    def execute(cls, choices, galaxyFn=None, username=''):
        from gold.gsuite.GSuite import GSuite
        from gold.gsuite.GSuiteTrack import GalaxyGSuiteTrack, GSuiteTrack
        import gold.gsuite.GSuiteComposer as GSuiteComposer
        from quick.application.ExternalTrackManager import ExternalTrackManager

        genome = choices.genome if choices.selectGenome == 'Yes' else None
        selectedHistories = [
            key for key, val in choices.history.iteritems() if val is not None
        ]

        gSuite = GSuite()

        for histGalaxyId in selectedHistories:
            galaxyTrackName = choices.history[histGalaxyId].split(':')

            histGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN(
                galaxyTrackName)
            histName = ExternalTrackManager.extractNameFromHistoryTN(
                galaxyTrackName)
            histSuffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(
                galaxyTrackName)

            uri = GalaxyGSuiteTrack.generateURI(galaxyFn=histGalaxyFn,
                                                suffix=histSuffix)
            gSuite.addTrack(GSuiteTrack(uri, title=histName, genome=genome))

        GSuiteComposer.composeToFile(gSuite, galaxyFn)
    def _getResultsLists(histChoices):
        if len([x for x in histChoices.values() if x is not None]) == 0:
            return [], []

        galaxyTNs = [
            x.split(':') for x in histChoices.values() if x is not None
        ]

        galaxyFns = [
            ExternalTrackManager.extractFnFromGalaxyTN(tn) for tn in galaxyTNs
        ]
        historyNames = [
            ExternalTrackManager.extractNameFromHistoryTN(tn)
            for tn in galaxyTNs
        ]
        staticFiles = [
            GalaxyRunSpecificFile(['results.pickle'], gfn) for gfn in galaxyFns
        ]
        #fileSpecificFile = [GalaxyRunSpecificFile([], gfn) for gfn in galaxyFns]

        #paths = [x.getDiskPath()+'/0' for x in fileSpecificFile]
        #pngList = [[v for v in x[2] if v.find('.png')>0] for x in os.walk(paths[0])]

        try:
            resultsLists = [load(sf.getFile('r')) for sf in staticFiles]
        except:
            resultsLists = []

        return resultsLists, historyNames
Пример #4
0
 def getGeSourceList(cls, genome, tracks):
     from quick.application.ExternalTrackManager import ExternalTrackManager
     from gold.origdata.BedGenomeElementSource import BedGenomeElementSource, BedCategoryGenomeElementSource
     from gold.origdata.GtrackGenomeElementSource import GtrackGenomeElementSource
     from gold.origdata.TrackGenomeElementSource import FullTrackGenomeElementSource
     geSourceList = []
     trackNamesWithoutPath = []
     for track in tracks:
         try:
             fileType = ExternalTrackManager.extractFileSuffixFromGalaxyTN(
                 track)
             fn = ExternalTrackManager.extractFnFromGalaxyTN(track)
             if fileType == 'category.bed':
                 geSourceList.append(BedCategoryGenomeElementSource(fn))
             elif fileType == 'gtrack':
                 geSourceList.append(GtrackGenomeElementSource(fn))
             else:
                 geSourceList.append(BedGenomeElementSource(fn))
             trackNamesWithoutPath.append(
                 ExternalTrackManager.extractNameFromHistoryTN(track))
         except:  # it is not a history, must be in HB track repository
             geSourceList.append(
                 FullTrackGenomeElementSource(genome,
                                              track,
                                              allowOverlaps=True))
             trackNamesWithoutPath.append(':'.join(track))
     return geSourceList, trackNamesWithoutPath
    def execute(cls, choices, galaxyFn=None, username=''):
        '''Is called when execute-button is pushed by web-user.
        Should print output as HTML to standard out, which will be directed to a results page in Galaxy history.
        If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.
        If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
        choices is a list of selections made by web-user in each options box.
        '''
        from time import time
        startTime = time()
        from quick.application.ExternalTrackManager import ExternalTrackManager
        from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile

        motifFn = ExternalTrackManager.extractFnFromGalaxyTN(
            choices[0].split(':'))
        observedFasta = ExternalTrackManager.extractFnFromGalaxyTN(
            choices[1].split(':'))

        randomGalaxyTN = choices[2].split(':')
        randomName = ExternalTrackManager.extractNameFromHistoryTN(
            randomGalaxyTN)
        randomGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN(
            randomGalaxyTN)
        randomStatic = GalaxyRunSpecificFile(
            ['random'], randomGalaxyFn
        )  #finds path to static file created for a previous history element (randomFn), and directs to a folder containing several files..
        #print os.listdir(randomStatic.getDiskPath())
        randomFastaPath = randomStatic.getDiskPath()

        #motifFn, observedFasta, randomFastaPath = '/Users/sandve/egne_dokumenter/_faglig/NullModels/DnaSeqExample/liver.pwm', 'liver.fa', 'randomFastas'
        testStatistic = choices[3]
        if testStatistic == 'Average of max score per sequence':
            scoreFunc = scoreMotifOnFastaAsAvgOfBestScores
        elif testStatistic == 'Sum of scores across all positions of all sequences':
            scoreFunc = scoreMotifOnFastaAsSumOfAllScores
        elif testStatistic == 'Score of Frith et al. (2004)':
            scoreFunc = lr4
        elif testStatistic == 'Product of max per sequence':
            scoreFunc = scoreMotifOnFastaAsProductOfBestScores
        else:
            raise

        pvals = mcPvalFromMotifAndFastas(motifFn, observedFasta,
                                         randomFastaPath, scoreFunc)
        print 'Pvals for motifs (%s) against observed (%s) vs random (%s - %s) sequences.' % (
            motifFn, observedFasta, randomName, randomFastaPath)
        for motif, pval in sorted(pvals.items()):
            print motif + '\t' + ('%.4f' % pval)

        from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
        from proto.RSetup import robjects
        histStaticFile = GalaxyRunSpecificFile(['pvalHist.png'], galaxyFn)
        #histStaticFile.openRFigure()
        histStaticFile.plotRHist(pvals.values(), [x / 40.0 for x in range(41)],
                                 'Histogram of p-values',
                                 xlim=robjects.FloatVector([0.0, 1.0]))
        #r.hist(robjects.FloatVector(pvals.values()), breaks=robjects.FloatVector([x/40.0 for x in range(41)]), xlim=robjects.FloatVector([0.0, 1.0]), main='Histogram of p-values' )
        #histStaticFile.closeRFigure()
        print histStaticFile.getLink('Histogram')
        print 'Time (s):', time() - startTime
    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 time import time
        startTime = time()
        from quick.application.ExternalTrackManager import ExternalTrackManager
        from quick.util.StaticFile import GalaxyRunSpecificFile
        import os

        motifFn = ExternalTrackManager.extractFnFromGalaxyTN( choices[0].split(':'))
        observedFasta = ExternalTrackManager.extractFnFromGalaxyTN( choices[1].split(':'))

        randomGalaxyTN = choices[2].split(':')
        randomName = ExternalTrackManager.extractNameFromHistoryTN(randomGalaxyTN)
        randomGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( randomGalaxyTN)
        randomStatic = GalaxyRunSpecificFile(['random'],randomGalaxyFn) #finds path to static file created for a previous history element (randomFn), and directs to a folder containing several files..
        #print os.listdir(randomStatic.getDiskPath())
        randomFastaPath = randomStatic.getDiskPath()

        #motifFn, observedFasta, randomFastaPath = '/Users/sandve/egne_dokumenter/_faglig/NullModels/DnaSeqExample/liver.pwm', 'liver.fa', 'randomFastas'
        testStatistic = choices[3]
        if testStatistic == 'Average of max score per sequence':
            scoreFunc = scoreMotifOnFastaAsAvgOfBestScores
        elif testStatistic == 'Sum of scores across all positions of all sequences':
            scoreFunc = scoreMotifOnFastaAsSumOfAllScores
        elif testStatistic == 'Score of Frith et al. (2004)':
            scoreFunc = lr4
        elif testStatistic == 'Product of max per sequence':
            scoreFunc = scoreMotifOnFastaAsProductOfBestScores
        else:
            raise
        
        pvals = mcPvalFromMotifAndFastas(motifFn, observedFasta, randomFastaPath, scoreFunc)
        print 'Pvals for motifs (%s) against observed (%s) vs random (%s - %s) sequences.' % (motifFn, observedFasta, randomName, randomFastaPath)
        for motif,pval in sorted(pvals.items()):
            print motif+'\t'+('%.4f'%pval)
            
        from quick.util.StaticFile import GalaxyRunSpecificFile
        from gold.application.RSetup import r, robjects
        histStaticFile = GalaxyRunSpecificFile(['pvalHist.png'],galaxyFn)
        #histStaticFile.openRFigure()
        histStaticFile.plotRHist(pvals.values(), [x/40.0 for x in range(41)], 'Histogram of p-values', xlim=robjects.FloatVector([0.0, 1.0]))
        #r.hist(robjects.FloatVector(pvals.values()), breaks=robjects.FloatVector([x/40.0 for x in range(41)]), xlim=robjects.FloatVector([0.0, 1.0]), main='Histogram of p-values' )
        #histStaticFile.closeRFigure()
        print histStaticFile.getLink('Histogram')
        print 'Time (s):', time()-startTime
Пример #7
0
    def getOptionsBox3(prevChoices):
        prevDict = ast.literal_eval(prevChoices[-1]) if prevChoices[-1] else {}
        index2name = prevDict

        selectedHists = [ExternalTrackManager.extractNameFromHistoryTN(unquote(val).split(':')) \
                         for id,val in prevChoices[1].iteritems() if val]
        newlySelected = list(set(selectedHists) - set(prevDict.values()))
        oldCount = len(index2name)
        for i, name in enumerate(newlySelected):
            index2name[i + oldCount] = name

        newlyRemovedList = list(set(prevDict.values()) - set(selectedHists))
        for name in newlyRemovedList:
            for idx, val in index2name.iteritems():
                if val == name and idx >= 0:
                    index2name[idx] = None

        return '__hidden__', repr(index2name)
    def _getResultsLists(histChoices):
        if len([x for x in histChoices.values() if x!=None])==0:
            return [],[]
        #print 'histChoices',histChoices
        #return []

        galaxyTNs = [x.split(':') for x in histChoices.values() if x!=None]
                
        galaxyFns = [ExternalTrackManager.extractFnFromGalaxyTN(tn) for tn in galaxyTNs]
        historyNames= [ExternalTrackManager.extractNameFromHistoryTN(tn) for tn in galaxyTNs]
        staticFiles = [GalaxyRunSpecificFile(['results.pickle'], gfn) for gfn in galaxyFns]
        fileSpecificFile = [GalaxyRunSpecificFile([], gfn) for gfn in galaxyFns]
        print 'Using pickles: ', [x.getDiskPath() for x in staticFiles]
        
        paths = [x.getDiskPath()+'/0' for x in fileSpecificFile]
        pngList = [[v for v in x[2] if v.find('.png')>0] for x in os.walk(paths[0])]
        
        resultsLists = [load(sf.getFile('r')) for sf in staticFiles]
        return resultsLists, historyNames
Пример #9
0
    def execute(choices, galaxyFn=None, username=''):
        '''
        Is called when execute-button is pushed by web-user. Should print
        output as HTML to standard out, which will be directed to a results page
        in Galaxy history. If getOutputFormat is anything else than HTML, the
        output should be written to the file with path galaxyFn. If needed,
        StaticFile can be used to get a path where additional files can be put
        (e.g. generated image files). choices is a list of selections made by
        web-user in each options box.
        '''

        from gold.gsuite.GSuite import GSuite
        from gold.gsuite.GSuiteTrack import GalaxyGSuiteTrack, GSuiteTrack
        import gold.gsuite.GSuiteComposer as GSuiteComposer
        from quick.application.ExternalTrackManager import ExternalTrackManager

        genome = choices.genome if choices.selectGenome == 'Yes' else None
        selectedHistories = [
            key for key, val in choices.history.iteritems() if val is not None
        ]

        gSuite = GSuite()

        for histGalaxyId in selectedHistories:
            galaxyTrackName = choices.history[histGalaxyId].split(':')

            histGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN(
                galaxyTrackName)
            histName = ExternalTrackManager.extractNameFromHistoryTN(
                galaxyTrackName)
            histSuffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(
                galaxyTrackName)

            uri = GalaxyGSuiteTrack.generateURI(galaxyFn=histGalaxyFn,
                                                suffix=histSuffix)
            gSuite.addTrack(GSuiteTrack(uri, title=histName, genome=genome))

        GSuiteComposer.composeToFile(gSuite, galaxyFn)
Пример #10
0
    def getRunDescription(genome, trackNames, analysisDef, ubSource,
                          revEngBatchLine, urlForTrackAutoSelection, **kwArgs):
        # genome = ubSource.genome
        assert len(trackNames) == 3
        core = HtmlCore()

        analysis = Analysis(analysisDef, genome, trackNames[0], trackNames[1],
                            **kwArgs)

        core.header('GENOME')
        core.append(GenomeInfo(genome).mainInfo(printEmpty=False))
        core.divider()

        formatChoices = analysis.getFormatConverterChoicesAsText().items()
        tr1FormatChoice, tr2FormatChoice = formatChoices if len(
            formatChoices) == 2 else (None, None)

        first = True
        for tn,label,formatChoice in zip(trackNames, \
                                         ['TRACK 1', 'TRACK 2', 'INTENSITY TRACK'], \
                                         [tr1FormatChoice, tr2FormatChoice, None]):
            if tn in [None, []]:
                continue

            if not first:
                core.divider()

            core.header(label)
            trackInfo = TrackInfo(genome, tn)
            trackText = ''
            if ExternalTrackManager.isHistoryTrack(tn):
                assert len(
                    tn) >= 4, 'Length of external track name < 4: %s' % str(tn)
                core.descriptionLine(
                    'Name',
                    ExternalTrackManager.extractNameFromHistoryTN(tn) +
                    ' (from history)' + os.linesep)
            else:
                core.descriptionLine('Name', ':'.join(tn) + os.linesep)
            core.append(trackInfo.mainInfo(printEmpty=False))

            if formatChoice is not None:
                core.descriptionLine('Treated as', formatChoice[1])

            first = False

        core.divider()
        core.header('ANALYSIS')
        core.paragraph(''.join(str(analysis).split(':')[1:]))

        first = True
        for label, choice in analysis.getInterfaceChoicesAsText().items():
            if first:
                core.divider()
                core.header('OPTIONS')

            core.descriptionLine(label, choice)
            first = False

        h0 = analysis.getH0()
        if h0 is not None:
            core.divider()
            core.header('NULL HYPOTHESIS')
            core.paragraph(h0)

        h1 = analysis.getH1()
        if h1 is not None:
            core.divider()
            core.header('ALTERNATIVE HYPOTHESIS')
            core.paragraph(h1)

        core.divider()
        core.header('ANALYSIS REGIONS')
        if hasattr(ubSource, 'description'):
            core.paragraph(ubSource.description)

        core.divider()
        core.header('SOLUTION')

        statClass = analysis.getStat()
        #One alternative is to put getDescription in MagicStatFactory-hierarchy as class-method, and get real class behind partial-object.
        #if isinstance(statClass, functools.partial):
        #statClass = statClass.func
        #core.paragraph( statClass.getDescription() )

        #Chosen alternative is to Instantiate an object, which will automatically give object of real class..
        #and then use the following two lines, which will get class in Statistic-hierarchy instead of MagicStatFactory-hierarchy ..
        try:
            reg = ubSource.__iter__().next()
        except:
            core.paragraph(
                'Solution not relevant, as there are no specified analysis regions..'
            )
        else:
            track1, track2 = analysis.getTracks()
            if statClass is None:
                core.paragraph(
                    'Solution not available, due to currently invalid analysis'
                )
                logMessage('Solution not available, with params: ' +
                           str([trackNames[0], trackNames[1], analysisDef]),
                           level=logging.WARN)
            else:
                statObj = statClass(reg, track1, track2)
                statDescr = statObj.getDescription()
                replPat = '<a href=' + os.sep.join(
                    [STATIC_REL_PATH, 'notes', 'stats', '']) + r'\1>note</a>'
                statDescr = re.sub('<note>(.*)</note>', replPat, statDescr)

                core.paragraph(statDescr)

        core.divider()
        core.header('TIME OF ANALYSIS')
        core.paragraph('Analysis initiated at time: ' +
                       str(datetime.datetime.now()))

        if urlForTrackAutoSelection not in [None, '']:
            core.divider()
            core.header('URL FOR TRACK AUTOSELECTION')
            #urlOptions = '&'.join(['track1=' + quote(':'.join(trackName1)), 'track2=' + quote(':'.join(trackName2))])
            #core.paragraph(URL_PREFIX + '/hyper?' + urlOptions)
            core.styleInfoBegin(styleClass='break-word')
            core.paragraph(urlForTrackAutoSelection)
            core.styleInfoEnd()

        if revEngBatchLine not in [None, '']:
            core.divider()
            core.header('CORRESPONDING BATCH COMMAND LINE')
            #if any(ExternalTrackManager.isRedirectOrExternalTrack(tn) for tn in [trackName1, trackName2]):
            #core.paragraph('Batch-run line not available with tracks from history')
            #else:
            core.styleInfoBegin(styleClass='break-word')
            core.paragraph(revEngBatchLine)
            core.styleInfoEnd()

        core.divider()
        core.header('REFERENCES')
        core.paragraph(
            'The HyperBrowser system is described in:<br>"Sandve et al., <a href="http://genomebiology.com/2010/11/12/R121/">The Genomic HyperBrowser: inferential genomics at the sequence level</a>, Genome Biol. 2010;11(12):R121'
        )
        from gold.statistic.RandomizationManagerStat import RandomizationManagerStat
        if statClass is not None and RandomizationManagerStat.getMcSamplingScheme(
                statClass.keywords) == 'MCFDR':
            core.paragraph('The p-values of this analysis were computed using the MCFDR scheme for Monte Carlo based p-value computation'+\
                           ', described in:<br>Sandve et al., <a href="http://bioinformatics.oxfordjournals.org/content/early/2011/10/13/bioinformatics.btr568.long">Sequential Monte Carlo multiple testing</a>, Bioinformatics 2011')

#        description = \
#'''
#Run descriptions will be introduced in the next version of HB. <br>
#Below is an example run description, which is a static text unconnected to your choices. The purpose is to get feedback from you on what this should look like:<br>
#Track1 (refseg:genes): Unmarked points (converted from unmarked segments, taking midpoints)<br>
#Track2 (DNA melting:meltmap): Function<br>
#Bins: Chr1, divided into bins of 10 megabases<br>
#Question: Are track1-points occurring with different frequency inside track2-segment than outside?<br>
#Analysis:<br>
#The main result is a p-value resulting from a statistical test connected to the question.<br>
#The null-hypothesis assumes that the track1-points are randomly distributed according to a poisson-distribution, with the same number of points as in the original data. Track2-segment are assumed fixed as they are in the original data. This can be answered by a binomial test. The alternative hypothesis is then that the count of points inside segments has resulted from a different distribution of points, where the points are then either distributed more or less inside segments versus outside. See the note on this question in the user guide for further info.<br>
#'''
        return str(core)
 def getExternalTrackName(self):
     from quick.application.ExternalTrackManager import ExternalTrackManager
     name = ExternalTrackManager.extractNameFromHistoryTN(self._galaxyFn)
     return ExternalTrackManager.createStdTrackName(self.getId(), name)
Пример #12
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.
     '''
     
     
     #matchPos = strVal.search(':');\n   if (matchPos !=-1)\n    {\n   valList = eval(strVal.split(:)[1]);\n                formVal ="%s: " +valList[0]+"\n%s: " +valList[1]+"\n%s: " +valList[2];\n                document.myform.posAnchor.value = liste[pos_y][pos_x] + counter;\n            }\n        else\n            {\n                document.myform.posAnchor.value = "0";\n            }\n\n
     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    <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/dev2/static/hyperbrowser/files/kaitre//image_zoom/styles/stylesheet.css" />
                 \n<script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/dev2/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/dev2/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/dev2/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	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        pos_x = Math.floor(pos_x/10);\n	pos_y = Math.floor(pos_y/10);\n	counter++;\n	strVal = liste[pos_y][pos_x];\n   	document.myform.posAnchor.value = liste[pos_y][pos_x] + counter;\n         val = strVal.split(':')[1].split('-')[0];\n	jQuery( "#slider" ).slider( "option", "value", val );\n        }\n</script>\n\n\n\n\n</head>
     <body><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<form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">\n<div align="center">\n\n<input type="text" name="posAnchor" size="50" value=".">\n<br>\n</div>\n</form>\n\n<div id="slider" onchange="jQuery('zoomer_region').css({ 'left': '31px', 'top': '15px'});"></div>\n%s</body></html>'''
     
     tableRowEntryTemplate = """<tr><td>%s</td><td><a href="%s"><img src="%s" /></a></td></tr>"""
     htmlPageTemplate = """<html><body><table border="1">%s</table></body></html>"""
     #fileDict = dict()
     tnList = []
     trackNameList = []
     genome = choices[0]
     for index in [1,3,5]:
         if choices[index] == 'no selection':
             tnList.append(None)
             trackNameList.append('.')
         elif choices[index] == 'from history':
             tnList.append(ExternalTrackManager.extractFnFromGalaxyTN(choices[index+1].split(':')))
             trackNameList.append(ExternalTrackManager.extractNameFromHistoryTN(choices[index+1].split(':')))
         else:
             regionList = []
             for chrom in GenomeInfo.getChrList(genome):
                 start = 0
                 binsize = 10000
                 chromSize = GenomeInfo.getChrLen(genome, chrom)
                 while start<=chromSize-binsize:
                     regionList.append(GenomeRegion(genome, chrom, start, start+binsize))
                     start+=binsize
                 if start<chromSize:
                     regionList.append(GenomeRegion(genome, chrom, start, chromSize))
             trackNameList.append(choices[index+1].split(':')[-1])        
             trackGESource = TrackGenomeElementSource(genome, choices[index+1].split(':'), regionList)
             bedgraphComp = BedGraphComposer(trackGESource)
             tnList.append(bedgraphComp)
             
     
     tr1, tr2, tr3 = tnList
     tName1, tName2, tName3 = trackNameList
     ResultDicts = []
     maxVals = []
     for tr,color in [(tr1, (1,0,0)),(tr2, (0,1,0)),(tr3, (0,0,1))]:
         logMessage(repr(tr))
         if tr:
             res, maxVal = cls.getValuesFromBedFile(genome, tr,color)
             logMessage('maxval:  '+str(maxVal))
             ResultDicts += [res]
             maxVals.append(maxVal)
         else:
             maxVals.append(None)
     htmlTnRangeVals = '\n'.join(['<br/>value range for track %i(%s): 0 - %i' % (index, v[0], v[1]) for index, v in  enumerate(zip(trackNameList, maxVals)) if v[0]!='.'])
     
     #ResultDicts = [cls.getValuesFromBedFile(genome, tr1,colorPattern=(1,0,0))]
     #ResultDicts += [cls.getValuesFromBedFile(genome, tr2,colorPattern=(0,1,0))] if tr2 else []
     #ResultDicts += [cls.getValuesFromBedFile(genome, tr3,colorPattern=(0,0,1))] if tr3 else []
 
 
     htmlTableContent = []
     resultDict = cls.syncResultDict(ResultDicts)
     
     for chrom, valList in resultDict.items():
         areaList = []
         #For doing recursive pattern picture
         posMatrix = cls.getResult(len(valList), 2,2)
         javaScriptList = [[0 for v in xrange(len(posMatrix[0])) ] for t in xrange(len(posMatrix))]
         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
                 javaScriptList[yIndex][xIndex] = chrom+':'+str(elem*10)+'-'+str((elem+1)*10)+': '+repr([ round((255-v)/255.0 ,2 ) for v in valList[elem]])
                 #areaList.append(areaTemplate % (xIndex*10, yIndex*10, xIndex*11, yIndex*11, repr(valList[elem])))
         imSmall = im.resize((len(posMatrix[0])*3, len(posMatrix)*3))
         im2 = im.resize((len(posMatrix[0])*10, len(posMatrix)*10))
         
         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))
         
         #
         open(fileElements[2].getDiskPath(ensurePath=True),'w').write(htmlTemplate % (int(GenomeInfo.getChrLen(genome, chrom)/1000.0)+1, str(javaScriptList), fileElements[1].getURL(), fileElements[0].getURL(), htmlTnRangeVals) )#tName1, tName2, tName3, 
         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'))
     
     
     open(galaxyFn,'w').write(htmlPageTemplate % ('\n'.join(htmlTableContent)))
Пример #13
0
    def getRunDescription(trackName1, trackName2, trackNameIntensity, analysisDef, ubSource, revEngBatchLine, \
                          urlForTrackAutoSelection, manualSeed, **kwArgs):
        genome = ubSource.genome
        core = HtmlCore()

        analysis = Analysis(analysisDef, genome, trackName1, trackName2, **kwArgs)
        
        core.header('GENOME')
        core.append(GenomeInfo(genome).mainInfo(printEmpty=False))
        core.divider()
                
        formatChoices = analysis.getFormatConverterChoicesAsText().items()
        tr1FormatChoice, tr2FormatChoice = formatChoices if len(formatChoices) == 2 else (None, None) 
        
        first = True
        for tn,label,formatChoice in zip([trackName1,trackName2,trackNameIntensity], \
                                         ['TRACK 1','TRACK 2','INTENSITY TRACK'], \
                                         [tr1FormatChoice,tr2FormatChoice,None]):
            if tn in [None, []]:
                continue
            
            if not first:
                core.divider()

            core.header(label)
            trackInfo = TrackInfo(genome, tn)
            trackText = ''
            if ExternalTrackManager.isHistoryTrack(tn):
                assert len(tn)>=4, 'Length of external track name < 4: %s' % str(tn)
                core.descriptionLine('Name', ExternalTrackManager.extractNameFromHistoryTN(tn) + ' (from history)' + os.linesep)
            else:
                core.descriptionLine('Name', ':'.join(tn) + os.linesep)
            core.append(trackInfo.mainInfo(printEmpty=False))

            if formatChoice is not None:
                core.descriptionLine('Treated as', formatChoice[1])
            
            first = False
        
        core.divider()
        core.header('ANALYSIS')
        core.paragraph( ''.join(str(analysis).split(':')[1:]) )

        first = True
        for label,choice in analysis.getInterfaceChoicesAsText().items():
            if first:
                core.divider()
                core.header('OPTIONS')
            
            if manualSeed is not None and label == 'Random seed' and choice == 'Random':
                choice = str(manualSeed)
                
            core.descriptionLine(label, choice)
            first = False
            
        h0 = analysis.getH0()
        if h0 is not None:
            core.divider()
            core.header('NULL HYPOTHESIS')
            core.paragraph(h0)
            
        h1 = analysis.getH1()
        if h1 is not None:
            core.divider()
            core.header('ALTERNATIVE HYPOTHESIS')
            core.paragraph(h1)
            
        core.divider()
        core.header('ANALYSIS REGIONS')
        if hasattr(ubSource, 'description'):
            core.paragraph(ubSource.description)
            
        core.divider()
        core.header('SOLUTION')

        statClass = analysis.getStat()
        #One alternative is to put getDescription in MagicStatFactory-hierarchy as class-method, and get real class behind partial-object.
        #if isinstance(statClass, functools.partial):
            #statClass = statClass.func
        #core.paragraph( statClass.getDescription() )

        #Chosen alternative is to Instantiate an object, which will automatically give object of real class..
        #and then use the following two lines, which will get class in Statistic-hierarchy instead of MagicStatFactory-hierarchy ..
        try:
            reg = ubSource.__iter__().next()
        except:
            core.paragraph('Solution not relevant, as there are no specified analysis regions..')
        else:
            track1, track2 = analysis.getTracks()
            if statClass is None:
                core.paragraph('Solution not available, due to currently invalid analysis')
                logMessage('Solution not available, with params: ' + str([trackName1, trackName2, analysisDef]), level=logging.WARN )
            else:
                statObj = statClass(reg,track1, track2)
                statDescr = statObj.getDescription()
                replPat = '<a href=' + os.sep.join([STATIC_REL_PATH,'notes','stats','']) + r'\1>note</a>'
                statDescr = re.sub('<note>(.*)</note>', replPat, statDescr)
        
                core.paragraph( statDescr )

        core.divider()
        core.header('TIME OF ANALYSIS')
        core.paragraph('Analysis initiated at time: ' + str( datetime.datetime.now() ) )
        
        if urlForTrackAutoSelection not in [None, '']:
            core.divider()
            core.header('URL FOR TRACK AUTOSELECTION')
            #urlOptions = '&'.join(['track1=' + quote(':'.join(trackName1)), 'track2=' + quote(':'.join(trackName2))])
            #core.paragraph(URL_PREFIX + '/hyper?' + urlOptions)
            core.styleInfoBegin(styleClass='break-word')
            core.paragraph(urlForTrackAutoSelection)
            core.styleInfoEnd()
            
        if revEngBatchLine not in [None, '']:
            core.divider()
            core.header('CORRESPONDING BATCH-RUN LINE')
            #if any(ExternalTrackManager.isRedirectOrExternalTrack(tn) for tn in [trackName1, trackName2]):
                #core.paragraph('Batch-run line not available with tracks from history')
            #else:
            core.styleInfoBegin(styleClass='break-word')
            core.paragraph(revEngBatchLine)
            core.styleInfoEnd()

        core.divider()
        core.header('REFERENCES')
        core.paragraph('The HyperBrowser system is described in:<br>"Sandve et al., <a href="http://genomebiology.com/2010/11/12/R121/">The Genomic HyperBrowser: inferential genomics at the sequence level</a>, Genome Biol. 2010;11(12):R121')
        from gold.statistic.RandomizationManagerStat import RandomizationManagerStat
        if statClass is not None and RandomizationManagerStat.getMcSamplingScheme(statClass.keywords) == 'MCFDR':
            core.paragraph('The p-values of this analysis were computed using the MCFDR scheme for Monte Carlo based p-value computation'+\
                           ', described in:<br>Sandve et al., <a href="http://bioinformatics.oxfordjournals.org/content/early/2011/10/13/bioinformatics.btr568.long">Sequential Monte Carlo multiple testing</a>, Bioinformatics 2011')
        
#        description = \
#'''
#Run descriptions will be introduced in the next version of HB. <br>
#Below is an example run description, which is a static text unconnected to your choices. The purpose is to get feedback from you on what this should look like:<br>
#Track1 (refseg:genes): Unmarked points (converted from unmarked segments, taking midpoints)<br>
#Track2 (DNA melting:meltmap): Function<br>
#Bins: Chr1, divided into bins of 10 megabases<br>
#Question: Are track1-points occurring with different frequency inside track2-segment than outside?<br>
#Analysis:<br>
#The main result is a p-value resulting from a statistical test connected to the question.<br>
#The null-hypothesis assumes that the track1-points are randomly distributed according to a poisson-distribution, with the same number of points as in the original data. Track2-segment are assumed fixed as they are in the original data. This can be answered by a binomial test. The alternative hypothesis is then that the count of points inside segments has resulted from a different distribution of points, where the points are then either distributed more or less inside segments versus outside. See the note on this question in the user guide for further info.<br>
#'''
        return str(core)
Пример #14
0
    def execute(cls, choices, galaxyFn=None, username=''):

        from quick.application.ExternalTrackManager import ExternalTrackManager
        from collections import defaultdict
        from gold.origdata.BedGenomeElementSource import BedGenomeElementSource, BedCategoryGenomeElementSource
        from gold.origdata.GtrackGenomeElementSource import GtrackGenomeElementSource
        from gold.origdata.TrackGenomeElementSource import FullTrackGenomeElementSource
        from urllib import unquote
        print choices

        genome = choices[0]
        geSourceList, labelNames = [], []
        selectedHists = [
            unquote(val).split(':') for id, val in choices[1].iteritems()
            if val
        ]
        inorout = [int(x) for x in choices[2].split(',')]
        selectedHists += [
            v.split(':') for v in choices[3:]
            if v not in ['-----  Select  -----', 'no', 'yes', None, '']
        ]
        for track in selectedHists:
            try:
                fileType = ExternalTrackManager.extractFileSuffixFromGalaxyTN(
                    track)
                fn = ExternalTrackManager.extractFnFromGalaxyTN(track)
                if fileType == 'category.bed':
                    geSourceList.append(BedCategoryGenomeElementSource(fn))
                elif fileType == 'gtrack':
                    geSourceList.append(GtrackGenomeElementSource(fn))
                else:
                    geSourceList.append(BedGenomeElementSource(fn))

                labelNames.append(
                    ExternalTrackManager.extractNameFromHistoryTN(track))
            except:
                geSourceList.append(
                    FullTrackGenomeElementSource(genome,
                                                 track,
                                                 allowOverlaps=False))
                #labelNames.append(track[-1])
                labelNames.append(':'.join(track))

        primeList = [
            2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59
        ]
        resultCounter = defaultdict(int)
        posDict = defaultdict(list)
        catDict = defaultdict(list)

        debugstring = 'debug out:'

        for index, geSource in enumerate(geSourceList):
            primeNum = primeList[index]
            prevEnd = -1
            prevChr = ''
            for ge in geSource:

                posDict[ge.chr] += [ge.start, ge.end]
                catDict[ge.chr] += [primeNum, -primeNum]
                prevEnd = ge.end
                prevChr = ge.chr

        debugstring += 'posDict elements/2: ' + str(
            sum(len(v) for v in posDict.itervalues()) / 2) + '\n'
        debugstring += 'catDict elements/2: ' + str(
            sum(len(v) for v in catDict.itervalues()) / 2) + '\n'

        #maxState = reduce( lambda x, y: x*y, primeList[:len(geSourceList)] ) #assuming all tracks are in.
        selectedState = 1
        for n in range(len(geSourceList)):
            if inorout[n]:
                selectedState = selectedState * primeList[n]

        utfil = open(galaxyFn, 'w')
        for chrom in posDict.keys():
            indxSortedList = sorted(range(len(posDict[chrom])),
                                    key=posDict[chrom].__getitem__)

            posList = posDict[chrom]
            catList = catDict[chrom]
            catCoverageDepth = defaultdict(int)

            currentState = 1
            currentPos = 0

            for indx in indxSortedList:
                pos = posList[indx]
                primeVal = catList[indx]
                #print 'pos, primeVal: ', pos, primeVal
                #print 'resultCounter: ', resultCounter
                if currentPos != pos:
                    if abs(currentState) == selectedState:
                        print >> utfil, '%s\t%i\t%i' % (chrom, currentPos, pos)
                    resultCounter[abs(currentState)] += pos - currentPos
                    #debugstring +='resultCounter='+str(resultCounter)+ ' currentPos='+ str(currentPos) + '    pos='+str(pos)+ '   chrom='+str(chrom)+  '   primeVal='+str(primeVal)+ '    catCoverageDepth='+str(catCoverageDepth) +'<br/>'
                    #print 'resultCounter,currentState,  pos and currentPos',abs(currentState),':',  pos, currentPos
                    currentPos = pos

                if primeVal < 0:
                    catCoverageDepth[abs(primeVal)] -= 1
                    if catCoverageDepth[abs(primeVal)] == 0:
                        currentState /= primeVal
                else:
                    catCoverageDepth[primeVal] += 1
                    if catCoverageDepth[primeVal] == 1:
                        currentState *= primeVal

        utfil.close()