def getSubtracksAsGSuite(genome, parentTrack, username=''):
    from gold.description.TrackInfo import TrackInfo
    from quick.application.GalaxyInterface import GalaxyInterface
    from quick.application.ProcTrackNameSource import ProcTrackNameSource

    fullAccess = GalaxyInterface.userHasFullAccess(username)
    procTrackNameSource = ProcTrackNameSource(genome,
                                              fullAccess=fullAccess,
                                              includeParentTrack=False)

    gSuite = GSuite()
    for trackName in procTrackNameSource.yielder(parentTrack):
        trackType = TrackInfo(genome, trackName).trackFormatName.lower()
        trackType = cleanUpTrackType(trackType)
        uri = HbGSuiteTrack.generateURI(trackName=trackName)
        title = prettyPrintTrackName(trackName)
        if title.startswith("'") and title.endswith("'") and len(title) > 1:
            title = title[1:-1]
        gSuite.addTrack(
            GSuiteTrack(uri, title=title, trackType=trackType, genome=genome))

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

        from quick.application.GalaxyInterface import GalaxyInterface
        from proto.hyperbrowser.HtmlCore import HtmlCore

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

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

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

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

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

            from quick.application.GalaxyInterface import GalaxyInterface

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

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

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

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

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

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

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


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

        print str(HtmlCore().styleInfoEnd())

        map = Map(name)

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

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

        print core
        print GalaxyInterface.getHtmlEndForRuns()
 def _removeIllegalLines(lines, username):
     if GalaxyInterface.userHasFullAccess(username):
         return lines
     else:
         return [x for x in lines if (len(x) > 0 and x[0] != '$') or \
                 x.startswith('$cluster')] # Hack to allow batch clustering
Beispiel #4
0
    def execute(choices, galaxyFn=None, username=''):
        #setupDebugModeAndLogging()
        from time import time
        startTime = time()
        print HtmlCore().begin()
        print '<pre>'
        genome = choices[0]
        #assert genome=='hg19'
        flankSize = choices[3]

        if choices[1] == 'Prepared catalogues':
            if choices[2] == 'GiulioNewGwas':
                gwasTnBase = 'Private:GK:NewGwasBase'.split(':')
            elif choices[2] == 'GiulioAllGwas':
                gwasTnBase = 'Private:GK:AllGiulioGwasSnpsAsOf9feb13'.split(
                    ':')
            elif choices[2] == 'GiulioMay13Gwas':
                gwasTnBase = 'Private:GK:Gwas:GiulioMay13'.split(':')
            elif choices[2] == 'SmallTest':
                gwasTnBase = 'Private:GK:Gwas'.split(':')
            else:
                raise

            gwasTnBase += [flankSize]

        elif choices[1] == 'Custom track':
            gwasTnBase = choices[2].split(':')
            assert flankSize == 'SNPs'
        else:
            assert False, choices[1]
        referenceTrackSource = choices[4]
        normalization = choices[5]
        assert normalization == 'CoverageDepth'
        analysisType = choices[6]
        if analysisType == 'Enrichment':
            ResultClass = EnrichmentGwasResults
        elif analysisType == 'Testing':
            ResultClass = HypothesisTestingGwasResults
            nullmodelMapping = dict(
                zip([
                    'Sample disease regions uniformly',
                    'Sample disease regions with preserved inter-region spacings',
                    'Sample disease regions with preserved distance to nearest exon'
                ], [
                    'PermutedSegsAndSampledIntersegsTrack_',
                    'PermutedSegsAndIntersegsTrack_',
                    'SegsSampledByDistanceToReferenceTrack_,trackNameIntensity=Genes and gene subsets^Exons^Ensembl exons'
                ]))
            nullmodel = nullmodelMapping[choices[9]]
            assert nullmodel in [
                'PermutedSegsAndSampledIntersegsTrack_',
                'PermutedSegsAndIntersegsTrack_',
                'SegsSampledByDistanceToReferenceTrack_,trackNameIntensity=Genes and gene subsets^Exons^Ensembl exons'
            ]
        else:
            raise

        kernelType = choices[7]
        kernelParam = choices[8]

        if choices[10] == 'Include links to full underlying results':
            includeDetailedResults = True
        elif choices[10] == 'Only produce main result values':
            includeDetailedResults = False
        else:
            raise InvalidRunSpecException('Did not understand option: %s' %
                                          choices[12])

        mcDepth = choices[11]

        if choices[12] == 'yes':
            includeLocalResults = True
        elif choices[12] == 'no':
            includeLocalResults = False
        else:
            raise InvalidRunSpecException('Did not understand option: %s' %
                                          choices[12])

        if choices[15] == 'yes':
            useCache = True
        elif choices[15] == 'no':
            useCache = False
        else:
            raise InvalidRunSpecException('Did not understand option: %s' %
                                          choices[15])

        if choices[16] == 'yes':
            printProgress = True
        elif choices[16] == 'no':
            printProgress = False
        else:
            raise InvalidRunSpecException('Did not understand option: %s' %
                                          choices[16])

        from quick.application.GalaxyInterface import GalaxyInterface
        #print GalaxyInterface.getHtmlForToggles()
        #print GalaxyInterface.getHtmlBeginForRuns()
        #from quick.webtools.GwasAPI import getEnrichmentValues
        print 'Progress: '
        #print 'base: ',gwasTnBase
        #print 'leaves: ',GalaxyInterface.getSubTrackNames(genome, gwasTnBase,deep=False, username=username)
        disRes = MultiGwasResults()
        from gold.application.HyperBrowserCLI import getSubTrackLeafTerms
        from quick.application.ProcTrackOptions import ProcTrackOptions

        #for gwasTrackLeaf in GalaxyInterface.getSubTrackNames(genome, gwasTnBase,deep=False, username=username):
        allDiseases = getSubTrackLeafTerms(genome,
                                           gwasTnBase,
                                           username=username)
        if len(allDiseases) == 0:
            assert ProcTrackOptions.isValidTrack(
                genome, gwasTnBase, GalaxyInterface.userHasFullAccess(
                    username)), 'Genome: %s, TN: %s, Access: %s' % (
                        genome, gwasTnBase,
                        GalaxyInterface.userHasFullAccess(username))
            allDiseases = gwasTnBase[-1:]
            gwasTnBase = gwasTnBase[:-1]

        for disease in allDiseases:
            #print 'Leaf:',gwasTrackLeaf[0]
            #if not gwasTrackLeaf[0] in ['11 - Height.txt']:
            #if not disease in ['1 - Alzheimer.txt','10 - Graves.txt']:#['Malaria','UC']:
            #    print 'IGNORING: ', gwasTrackLeaf[0]
            #    continue

            #if gwasTrackLeaf in [[],None] or gwasTrackLeaf[0]=='-- All subtypes --':
            #continue

            #gwasTn = ':'.join(gwasTnBase + [gwasTrackLeaf[0]])
            gwasTn = ':'.join(gwasTnBase + [disease])
            #print 'Running API: ', "$getEnrichmentValues(%s, '%s', '%s')" % ([gwasTn], referenceTrackSource, normalization)
            #enrichmentsDict = getEnrichmentValues([gwasTn], referenceTrackSource, normalization)#, ['114 - Brain_Mid_Frontal_Lobe.txt','134 - Rectal_Smooth_Muscle.txt'])
            #assert len(enrichmentsDict.values())==1
            #enrichments = enrichmentsDict.values()[0]

            #if gwasTrackLeaf[0] in ['Malaria','UC']:
            #print 'HERE IS WHAT I GOT: ',enrichmentsDict
            #print 'ENR: ',enrichments
            #print 'One: ', (enrichments.values()[0])['enrichment']['13 - CD4'].getGlobalResult()
            #assert 'enrichment' in (enrichments.values()[0]), (enrichments.values()[0])
            #disRes[gwasTrackLeaf[0]] = (enrichments.values()[0])['enrichment']
            #disRes[gwasTrackLeaf[0]] = (enrichments.values()[0])
            #disease = gwasTrackLeaf[0]
            #disRes[disease] = [x.getGlobalResult() for x in enrichments]
            #print 'DISres: ', disRes[gwasTrackLeaf[0]]
            #from quick.util.CommonFunctions import extractIdFromGalaxyFn

            res = ResultClass(gwasId=disease, verbose=True, galaxyFn=galaxyFn)
            #referenceSubTypes = enrichments.keys()
            #referenceSubTypes = [x[0] for x in GalaxyInterface.getSubTrackNames(genome, 'Private:GK:Psych:DHSs'.split(':'), deep=False, username=username) if not x[0] == '-- All subtypes --']
            if referenceTrackSource == 'H3K4me3':
                refTrackBase = 'Private:GK:Psych:H3K4me3'
                refTrackCoverageFunction = 'Private^GK^Psych^H3K4me3CoverageTrack'
            elif referenceTrackSource == 'DHS':
                refTrackBase = 'Private:GK:Psych:DHSs'
                refTrackCoverageFunction = 'Private^GK^Psych^DHSCoverageTrack'
            elif referenceTrackSource == 'Chromatin state 1-AP':
                refTrackBase = 'Private:Anders:Chromatin State Segmentation:1_Active_Promoter'
                refTrackCoverageFunction = 'Private^GWAS^Chromatin^CoverageFunctionTracks^1_Active_PromoterV2'
            elif referenceTrackSource == 'Chromatin state 4-SE':
                refTrackBase = 'Private:Anders:Chromatin State Segmentation:4_Strong_Enhancer'
                refTrackCoverageFunction = 'Private^GWAS^Chromatin^CoverageFunctionTracks^4_Strong_Enhancer'
            elif referenceTrackSource == 'Chromatin state 5-SE':
                refTrackBase = 'Private:Anders:Chromatin State Segmentation:5_Strong_Enhancer'
                refTrackCoverageFunction = 'Private^GWAS^Chromatin^CoverageFunctionTracks^5_Strong_Enhancer'
            else:
                raise
            refTrackSelectType = choices[13]

            allReferenceTracks = [
                x[0] for x in GalaxyInterface.getSubTrackNames(
                    genome,
                    refTrackBase.split(':'),
                    deep=False,
                    username=username) if not x[0] == '-- All subtypes --'
            ]
            if refTrackSelectType == 'Use all reference tracks':
                referenceSubTypes = allReferenceTracks
            elif refTrackSelectType == 'Select single reference track':
                referenceSubTypes = [choices[14]]
                assert referenceSubTypes[0] in allReferenceTracks
            elif refTrackSelectType == 'Select a range among all reference tracks':
                try:
                    firstRefTrack, lastRefTrack = choices[14].split('-')
                    referenceSubTypes = allReferenceTracks[
                        int(firstRefTrack):int(lastRefTrack) + 1]
                    print 'Analyzing %s among a total of %s reference tracks' % (
                        choices[14], len(allReferenceTracks))
                except Exception:
                    print 'Range format should be e.g. "15-18".'
                    raise
            else:
                raise

            for referenceSubType in referenceSubTypes:
                #if not referenceSubType in ['107 - Adult_Kidney.txt','106 - Adipose_Nuclei.txt']:
                #    #print 'IGNORING: ',referenceSubType
                #    continue
                #
                if analysisType == 'Enrichment':
                    res[referenceSubType] = directGetEnrichment(
                        gwasTn, referenceSubType, refTrackBase, kernelType,
                        kernelParam, useCache, printProgress)
                elif analysisType == 'Testing':
                    res[referenceSubType] = directGetTestResults(
                        gwasTn, referenceSubType, refTrackBase, kernelType,
                        kernelParam, refTrackCoverageFunction, nullmodel,
                        mcDepth, useCache, printProgress)
                else:
                    raise

                #print disease, referenceSubType, res[referenceSubType]
                #print "ENR: ",enrichments
                #res[referenceSubType] = enrichments[referenceSubType]
            disRes[disease] = res

        #for disease in disRes:
        #    print 'D FULL %s:' %disease, disRes[disease]
        #    print 'D DICTS %s:'%disease, disRes[disease].getAllGlobalResultDicts()
        #    print 'DISEASE %s:'%disease, disRes[disease].getAllGlobalResults()
        print 'Total run time (excluding figure generation): %i seconds.' % (
            time() - startTime)
        print '</pre>'
        #print GalaxyInterface.getHtmlBeginForRuns()

        print '<h1>Results</h1>'
        if len(allDiseases) > 1:
            try:
                heatMapLink = disRes.getLinkToClusteredHeatmap(
                    'Heatmap', galaxyFn)
                print '<h3>Heatmap</h3>', heatMapLink  #, '<br>'
            except:
                print '<p>Creation of heatmap failed</p>'
        tableOutput = disRes.getHtmlResultsTable(includeDetailedResults)
        print '<h3>Results table</h3>', tableOutput

        if choices[-1]:
            print '<h3>Prior coloring table</h3>'
            colorFn = ExternalTrackManager.extractFnFromGalaxyTN(
                choices[-1].split(':'))
            print disRes.getColoredSortedReferencesTable(colorFn)

        if includeLocalResults:
            print '<h3>Local results</h3>'
            print disRes.getLinksToAllLocalHtmlResultsTables(galaxyFn)