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.genome

        trackChoice = choices.history if choices.source == 'history' else choices.track
        trackName = trackChoice.split(':')

        if ExternalTrackManager.isGalaxyTrack(trackName):
            geSource = ExternalTrackManager.getGESourceFromGalaxyOrVirtualTN(
                trackName, genome)
            if not geSource.hasOrigFile():  #hbfunction
                stdTrackName = ExternalTrackManager.getStdTrackNameFromGalaxyTN(
                    trackName)
                ExternalTrackManager.renameExistingStdTrackIfNeeded(
                    genome, stdTrackName)
                geSource = FullTrackGenomeElementSource(genome,
                                                        stdTrackName,
                                                        allowOverlaps=False)
        else:
            try:
                geSource = FullTrackGenomeElementSource(genome,
                                                        trackName,
                                                        allowOverlaps=True)
                for ge in geSource:
                    break
            except:
                geSource = FullTrackGenomeElementSource(genome,
                                                        trackName,
                                                        allowOverlaps=False)

        threshold = float(choices.threshold)
        rule = RULE_DICT[choices.rule]

        composerCls = getComposerClsFromFileFormatName(choices.format)
        geModifier = GEValueThresholder(geSource, genome, threshold, rule)
        if choices.merge == 'Yes':
            if not geModifier.isSorted():
                geModifier = GenomeElementSorter(geModifier)
            geModifier = GEAdjacencyClusterer_Segment(geModifier)

        composerCls(geModifier).composeToFile(galaxyFn, ignoreEmpty=True)
    def execute(choices, galaxyFn=None, username=''):

        genome = choices.targetTrackGenome
        queryTrackName = choices.targetTrack.split(':')

        from quick.multitrack.MultiTrackCommon import getGSuiteDataFromGalaxyTN
        trackTitles, refTrackNameList, genome = getGSuiteDataFromGalaxyTN(choices.refTrackCollection)

        regSpec = 'track'
        binSpec = choices.targetTrack

        #future iter1
        #regSpec, binSpec = UserBinSelector.getRegsAndBinsSpec(choices)

        results = []
        for refTrack in refTrackNameList:
            analysisDef = '-> ProportionCountStat'
            res = GalaxyInterface.runManual([refTrack], analysisDef, regSpec, binSpec, genome, username=username, galaxyFn=galaxyFn) # res is of class Results
            segCoverageProp = [res[seg]['Result'] for seg in res.getAllRegionKeys()]#to heatmap
            results.append(segCoverageProp)


        geSource = FullTrackGenomeElementSource(genome,  queryTrackName)
        modGeSource = MyElementModifier(geSource, results, genome)
        composer = BedComposer(modGeSource, extraTrackLineAttributes={'itemRgb': '"On"'})
        composer.composeToFile(galaxyFn)
Esempio n. 3
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
Esempio n. 4
0
    def __new__(cls,
                regSpec,
                binSpec,
                genome=None,
                categoryFilterList=None,
                strictMatch=True):
        if regSpec in ['file', 'track'] + getSupportedFileSuffixesForBinning():
            if genome is None:
                genome = DEFAULT_GENOME

            from gold.origdata.GenomeElementSource import GenomeElementSource
            if regSpec == 'track':
                from quick.util.CommonFunctions import convertTNstrToTNListFormat
                from gold.origdata.TrackGenomeElementSource import FullTrackGenomeElementSource
                trackName = convertTNstrToTNListFormat(binSpec)
                geSource = FullTrackGenomeElementSource(genome,
                                                        trackName,
                                                        allowOverlaps=False)
            else:
                from quick.application.ExternalTrackManager import ExternalTrackManager
                try:
                    fn = ExternalTrackManager.getGalaxyFnFromEncodedDatasetId(
                        binSpec)
                except:
                    fn = binSpec
                geSource = GenomeElementSource(
                    fn,
                    genome=genome,
                    suffix=regSpec if regSpec != 'file' else None)

            if categoryFilterList is not None:
                from gold.origdata.GECategoryFilter import GECategoryFilter
                geSource = GECategoryFilter(geSource,
                                            categoryFilterList,
                                            strict=strictMatch)
            return cls._applyEnvelope(geSource)
        else:
            if binSpec == '*':
                binSize = None
            else:
                binSize = parseShortenedSizeSpec(binSpec)

            from quick.application.AutoBinner import AutoBinner
            return AutoBinner(parseRegSpec(regSpec, genome), binSize)
Esempio n. 5
0
def getGeSource(track, genome=None):
    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

    if isinstance(track, basestring):
        track = track.split(':')

    try:
        fileType = ExternalTrackManager.extractFileSuffixFromGalaxyTN(track)
        fn = ExternalTrackManager.extractFnFromGalaxyTN(track)
        if fileType == 'category.bed':
            return BedCategoryGenomeElementSource(fn)
        elif fileType == 'gtrack':
            return GtrackGenomeElementSource(fn)
        else:
            return BedGenomeElementSource(fn)
    except:
        return FullTrackGenomeElementSource(genome, track, allowOverlaps=False)
Esempio n. 6
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()