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.
        '''
        genome = choices[0]
        trackName = choices[1].split(':')
        
        galaxyOutTrackName = 'galaxy:hbfunction:%s:Create function track of distance to nearest segment' % galaxyFn
        outTrackName = ExternalTrackManager.getStdTrackNameFromGalaxyTN(galaxyOutTrackName.split(':'))
        
        if choices[2] == 'No transformation':
            valTransformation = 'None'
        elif choices[2] =='Logarithmic (log10(x))':
            valTransformation = 'log10'
        elif choices[2] == 'Fifth square root (x**0.2)':
            valTransformation = 'power0.2'
        
        analysisDef ='[dataStat=MakeDistanceToNearestSegmentStat] [valTransformation=%s][outTrackName=' % valTransformation \
                     + '^'.join(outTrackName) + '] -> CreateFunctionTrackStat'
        #userBinSource, fullRunArgs = GalaxyInterface._prepareRun(trackName, None, analysisDef, '*', '*', genome)
        #
        #for el in userBinSource:
        #    print el.chr, el.start, el.end
            
        from quick.application.GalaxyInterface import GalaxyInterface

        print GalaxyInterface.getHbFunctionOutputBegin(galaxyFn, withDebug=False)
        
        GalaxyInterface.runManual([trackName], analysisDef, '*', '*', genome, username=username, printResults=False, printHtmlWarningMsgs=False)
        #job = AnalysisDefJob(analysisDef, trackName, None, userBinSource).run()
        
        print GalaxyInterface.getHbFunctionOutputEnd('A custom track has been created by finding the bp-distance to the nearest segment', withDebug=False)
    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.
        '''
        
        genome = choices.genome
        seqTrackName = choices.seqTrack.split(':')
        outBaseTrackName = choices.outTrackStr.split(':')
        
        regSpec = '__brs__' if choices.regions == 'Bounding regions of the sequence track' else '*'

        for pwm, chosen in choices.pwms.iteritems():
            if chosen:
                pfmFileName = os.sep.join([CreatePwmScoreTracksTool.JASPAR_DIR, pwm + '.pfm'])
                outTrackName = outBaseTrackName + [pwm]
                analysisDef ='[dataStat=MaxPwmScoreInWindowArrayStat] [pfmFileName=%s] [outTrackName=%s] [valDataType=float32]-> CreateFunctionTrackStat' \
                    % (pfmFileName.replace('/','^'), '^'.join(outTrackName))
                
                from quick.application.GalaxyInterface import GalaxyInterface
                GalaxyInterface.runManual([seqTrackName], analysisDef, regSpec, '*', genome, username=username, printResults=False, printHtmlWarningMsgs=False)
Example #3
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.genome
        seqTrackName = choices.seqTrack.split(':')
        outBaseTrackName = choices.outTrackStr.split(':')

        regSpec = '__brs__' if choices.regions == 'Bounding regions of the sequence track' else '*'

        for pwm, chosen in choices.pwms.iteritems():
            if chosen:
                pfmFileName = os.sep.join([cls.JASPAR_DIR, pwm + '.pfm'])
                outTrackName = outBaseTrackName + [pwm]
                analysisDef ='[dataStat=MaxPwmScoreInWindowArrayStat] [pfmFileName=%s] [outTrackName=%s] [valDataType=float32]-> CreateFunctionTrackStat' \
                    % (pfmFileName.replace('/','^'), '^'.join(outTrackName))

                from quick.application.GalaxyInterface import GalaxyInterface
                GalaxyInterface.runManual([seqTrackName],
                                          analysisDef,
                                          regSpec,
                                          '*',
                                          genome,
                                          username=username,
                                          printResults=False,
                                          printHtmlWarningMsgs=False)
    def findOverrepresentedTFsFromGeneSet(genome, tfSource, ensembleGeneIdList,upFlankSize, downFlankSize, geneSource, galaxyFn):
        #galaxyFn = '/usit/insilico/web/lookalike/galaxy_dist-20090924-dev/database/files/003/dataset_3347.dat'
        #print 'overriding galaxyFN!: ', galaxyFn
        galaxyId = extractIdFromGalaxyFn(galaxyFn)
        uniqueWebPath = getUniqueWebPath(extractIdFromGalaxyFn(galaxyFn))

        assert genome == 'hg18'
        
        tfTrackNameMappings = TfInfo.getTfTrackNameMappings(genome)
        tfTrackName = tfTrackNameMappings[tfSource]
        
        
        #Get gene track
        assert geneSource == 'Ensembl'
        targetGeneRegsTempFn = uniqueWebPath + os.sep + 'geneRegs.bed'
        geneRegsTrackName = GenomeInfo.getStdGeneRegsTn(genome)
        geneRegsFn = getOrigFn(genome, geneRegsTrackName, '.category.bed')
        GalaxyInterface.getGeneTrackFromGeneList(genome, geneRegsTrackName, ensembleGeneIdList, targetGeneRegsTempFn )
        
        assert upFlankSize == downFlankSize == 0 #Should instead extend regions to include flanks
        
        tcGeneRegsTempFn = uniqueWebPath + os.sep + 'tcGeneRegs.targetcontrol.bedgraph'
        #Think this will be okay, subtraction not necessary as targets are put first:
        controlGeneRegsTempFn = geneRegsFn
        #print targetGeneRegsTempFn, controlGeneRegsTempFn, tcGeneRegsTempFn
        GalaxyInterface.combineToTargetControl(targetGeneRegsTempFn, controlGeneRegsTempFn, tcGeneRegsTempFn)
        
        #tcGeneRegsExternalTN = ['external'] +galaxyId +  [tcGeneRegsTempFn]
        tcGeneRegsExternalTN = ExternalTrackManager.createStdTrackName(galaxyId, 'tempTc')
        
        #tcGeneRegsExternalTN = ['external'] +targetGalaxyId +  [tcGeneRegsTempFn]
        #tcGeneRegsExternalTN = ['galaxy', externalId, tcGeneRegsTempFn]
        
        targetGeneRegsExternalTN = ExternalTrackManager.createStdTrackName(galaxyId, 'tempTc', '1')
        controlGeneRegsExternalTN = ExternalTrackManager.createStdTrackName(galaxyId, 'tempTc', '0')
        
        #pre-process
        print 'Pre-processing file: %s, with trackname: %s ' % (tcGeneRegsTempFn, tcGeneRegsExternalTN)
        ExternalTrackManager.preProcess(tcGeneRegsTempFn, tcGeneRegsExternalTN, 'targetcontrol.bedgraph',genome)
        print 'Pre-processing TN: ', targetGeneRegsExternalTN
        ExternalTrackManager.preProcess(targetGeneRegsTempFn, targetGeneRegsExternalTN, 'bed',genome)
        print 'Pre-processing TN: ', controlGeneRegsExternalTN
        ExternalTrackManager.preProcess(controlGeneRegsTempFn, controlGeneRegsExternalTN, 'bed',genome)
        
        #print tcGeneRegsExternalTN
        trackName1, trackName2 = tfTrackName, tcGeneRegsExternalTN
        
        analysisDef = 'Categories differentially located in targets?: Which categories of track1-points fall more inside case than control track2-segments? [rawStatistic:=PointCountInsideSegsStat:]' +\
                  '[tf1:=SegmentToStartPointFormatConverter:] [tf2:=TrivialFormatConverter:]' +\
                  '-> DivergentRowsInCategoryMatrixStat'
        regSpec, binSpec = '*','*'
        
        #print 'skipping preproc!!'
        #ExternalTrackManager.preProcess(tcGeneRegsExternalTN[-1], tcGeneRegsExternalTN, 'targetcontrol.bedgraph', genome)
        #ExternalTrackManager.preProcess(targetGeneRegsTempFn, targetGeneRegsExternalTN, 'bed', genome)
        
        GalaxyInterface.runManual([trackName1, trackName2], analysisDef, regSpec, binSpec, genome, printResults=True, printHtmlWarningMsgs=False)
    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 gold.application.LogSetup import setupDebugModeAndLogging
        # setupDebugModeAndLogging()

        # Hack to not have to refactor tool to use keyword-based choices
        class MockChoices(object):
            debugMode = choices[10]

        mockChoices = MockChoices()
        cls._setDebugModeIfSelected(mockChoices)

        analysisDef = choices[2].replace(os.linesep, '')
        #from gold.application.StatRunner import AnalysisDefJob

        # print 'Preparing arguments'
        genome = choices[3]

        prefixTN1 = cls.STD_PREFIX_TN if choices[4] == 'yes' else []
        tn1 = prefixTN1 + choices[5].split(':')
        prefixTN2 = cls.STD_PREFIX_TN if choices[6] == 'yes' else []
        tn2 = prefixTN2 + choices[7].split(':')
        if tn2[-1] == cls.NO_TRACK_SHORTNAME:
            tn2 = None
        #from gold.track.GenomeRegion import GenomeRegion
        #region = GenomeRegion(genome, 'chr1',1000,2000)
        #region2 = GenomeRegion(genome, 'chr1',5000,6000)

        #kwArgs = {}
        regVal = choices[8]
        binSpecVal = choices[9]
        #ubSource = UserBinSource(regVal, binSpecVal, genome=genome)
        from quick.application.GalaxyInterface import GalaxyInterface

        # print tn1, tn2
        GalaxyInterface.runManual([tn1, tn2],
                                  analysisDef,
                                  regVal,
                                  binSpecVal,
                                  genome,
                                  galaxyFn,
                                  username=username)
    def computeDistance(
            cls,
            genome,
            track1,
            track2,
            feature,
            regSpec,
            binSpec,
            galaxyFn=None):  #direct distance between track1, track2
        '''
        track1 and track2 are two lists like : ['Sequence','Repeating elements','LINE']
        feature specifies how the distance between track1 and track2 is defined
        '''
        validFeature = DirectDistanceCatalog.getValidAnalyses(
            genome, track1, track2)[feature]
        analysisDef = validFeature[
            0]  #'bla bla -> PropFreqOfTr1VsTr2Stat' #or any other statistic from the HB collection
        #userBinSource = GalaxyInterface._getUserBinSource(regSpec,binSpec,genome)

        result = GalaxyInterface.runManual([track1, track2],
                                           analysisDef,
                                           regSpec,
                                           binSpec,
                                           genome,
                                           galaxyFn=galaxyFn)
        #result = AnalysisDefJob(analysisDef, track1, track2, userBinSource).run()
        mainResultDict = result.getGlobalResult()
        return mainResultDict[validFeature[1]]
 def _runSingleStatistic(self, regionTrackName, analysisDef, predictionTrackName, answerTrackName=None):
     splittedPredictionTrackName = predictionTrackName.split(':')
     regionFileName = regionTrackName.split(':')[2]
     
     if answerTrackName == None:
         resultObject = GalaxyInterface.runManual([splittedPredictionTrackName], 
                     analysisDef, 'gtrack', regionFileName, self._genome, username=self._username, 
                     printResults=False, printHtmlWarningMsgs=False)
     else:
         splittedAnswerTrackName = answerTrackName.split(':')
     
         resultObject = GalaxyInterface.runManual([splittedPredictionTrackName, splittedAnswerTrackName], 
                     analysisDef, 'gtrack', regionFileName, self._genome, username=self._username, 
                     printResults=False, printHtmlWarningMsgs=False)
     
     return resultObject.getGlobalResult()
    def extract_feature(cls, genome, track, ref, option, regSpec, binSpec,
                        trackFormat):
        #print 'genome, track, ref, option, regSpec, binSpec, trackFormat: ', genome, track, ref, option, regSpec, binSpec, trackFormat
        #featureKeys = FeatureCatalog.getFeaturesFromTracks(genome,track,ref).keys()
        #logMessage('ClusterExecution (extract_feature) track og refTrack = ('+ repr(track)+'  #####  '+repr(ref))
        #logMessage('ClusterExecution (extract_feature) featureKeys = '+ repr(featureKeys))
        validFeature = FeatureCatalog.getFeaturesFromTracks(
            genome, track, ref
        )[option]  #validFeature contains analysisDef and the key to get the needed number from the global result
        if option == 'Prop. of tr1-points falling inside segments of tr2' and trackFormat in [
                'Segments', 'Valued segments'
        ]:
            analysisDef = 'dummy [tf1=SegmentToMidPointFormatConverter] -> DerivedPointCountsVsSegsStat'
        else:
            analysisDef = validFeature[
                0]  #or any other statistic from the HB collection

        #userBinSource = GalaxyInterface._getUserBinSource(regSpec,binSpec,genome)
        result = GalaxyInterface.runManual([ref, track],
                                           analysisDef,
                                           regSpec,
                                           binSpec,
                                           genome,
                                           printResults=False,
                                           printProgress=False,
                                           printHtmlWarningMsgs=False,
                                           printRunDescription=False)
        #result = AnalysisDefJob(analysisDef, ref, track, userBinSource).run() if option == 'Prop. of tr2 covered by tr1' else AnalysisDefJob(analysisDef, track, ref, userBinSource).run()

        validAnalysisDef = validFeature[1]
        assert result.getGlobalResult(
        ) is not None, 'Did not get any global result for analysisDef: ' + validAnalysisDef
        return result.getGlobalResult()[validAnalysisDef]
    def execute(cls, choices, galaxyFn=None, username=''):

        start = time.time()
        genome = choices[0]
        trackName = choices[1].split(':')
        outFn = galaxyFn
        if choices[5] == 'Write to Standardised file':
            outFn = createOrigPath(genome, choices[-1].split(':'),
                                   'collapsed_result.bedgraph')
            ensurePathExists(outFn[:outFn.rfind('/') + 1])

        combineMethod = choices[2]
        category = choices[3] if choices[3] else ''
        numSamples = choices[4] if choices[4] else '1'

        analysisDef = 'dummy [combineMethod=%s] %s [numSamples=%s] -> ConvertToNonOverlappingCategorySegmentsPythonStat' % \
                        (combineMethod, '[category=%s]' % category if category != '' else '', numSamples) #'Python'

        for regSpec in GenomeInfo.getChrList(genome):
            res = GalaxyInterface.runManual([trackName], analysisDef, regSpec, '*', genome, username=username, \
                                            printResults=False, printHtmlWarningMsgs=False)

            from gold.origdata.TrackGenomeElementSource import TrackViewGenomeElementSource
            from gold.origdata.BedComposer import CategoryBedComposer
            for resDict in res.values():
                trackView = resDict['Result']
                tvGeSource = TrackViewGenomeElementSource(
                    genome, trackView, trackName)
                CategoryBedComposer(tvGeSource).composeToFile(outFn)
Example #10
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.ExternalTrackManager import ExternalTrackManager
        from quick.application.GalaxyInterface import GalaxyInterface

        from gold.application.LogSetup import setupDebugModeAndLogging
        #setupDebugModeAndLogging()

        genome = choices[0]
        tn1, tn2 = choices[1:]
        fnTn1 = ExternalTrackManager.extractFnFromGalaxyTN(tn1)

        track1 = [
            'Sequence', 'DNA'
        ]  #ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, tn1.split(':'))
        track2 = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(
            genome, tn2.split(':'))
        analysisDef = 'dummy -> GetMutatedSequenceStat'

        res = GalaxyInterface.runManual([track1, track2], analysisDef, 'bed',
                                        fnTn1, genome)
        with open(galaxyFn, 'w') as utfil:
            for region, sequence in res.items():
                print >> utfil, '>' + region.chr + ' ' + str(region.start +
                                                             1) + '-' + str(
                                                                 region.end)
                print >> utfil, sequence['Result']
 def execute(cls, choices, galaxyFn=None, username=''):
     start = time.time()
     genome = choices[0]
     trackName = choices[1].split(':')
     outFn = galaxyFn
     if choices[5] == 'Write to Standardised file':
         outFn = createOrigPath(genome, choices[-1].split(':'), 'collapsed_result.bedgraph')
         ensurePathExists(outFn[:outFn.rfind('/')+1])
             
     combineMethod = choices[2]
     category = choices[3] if choices[3] else ''
     numSamples = choices[4] if choices[4] else '1'
     
     analysisDef = 'dummy [combineMethod=%s] %s [numSamples=%s] -> ConvertToNonOverlappingCategorySegmentsPythonStat' % \
                     (combineMethod, '[category=%s]' % category if category != '' else '', numSamples) #'Python'
                                               
     for regSpec in  GenomeInfo.getChrList(genome):
         res = GalaxyInterface.runManual([trackName], analysisDef, regSpec, '*', genome, username=username, \
                                         printResults=False, printHtmlWarningMsgs=False)
         
         from gold.origdata.TrackGenomeElementSource import TrackViewGenomeElementSource
         from gold.origdata.BedComposer import CategoryBedComposer
         for resDict in res.values():
             tvGeSource = TrackViewGenomeElementSource(genome, resDict['Result'], trackName)
             CategoryBedComposer(tvGeSource).composeToFile(outFn)
    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)
    def run(self):
        regSpec, binSpec = 'file', self._referenceTrackFn
        trackName1 = self._queryTrackName
        trackName2 = None
        from gold.description.TrackInfo import TrackInfo
        
        formatName = TrackInfo(self._genome, trackName1).trackFormatName
        formatConv = ''
        if 'segments' in formatName:
            formatConv = '[tf1:=SegmentToStartPointFormatConverter:]'

        analysisDef = formatConv + '-> CountPointStat'

        print '<div class="debug">'
        #trackName1, trackName2, analysisDef = GalaxyInterface._cleanUpAnalysisDef(trackName1, trackName2, analysisDef)
        #trackName1, trackName2 = GalaxyInterface._cleanUpTracks([trackName1, trackName2], genome, realPreProc=True)
        #
        #userBinSource, fullRunArgs = GalaxyInterface._prepareRun(trackName1, trackName2, analysisDef, regSpec, binSpec, self._genome)
        #res = AnalysisDefJob(analysisDef, trackName1, trackName2, userBinSource, **fullRunArgs).run()
        res = GalaxyInterface.runManual([trackName1, trackName2], analysisDef, regSpec, binSpec, self._genome, printResults=False, printHtmlWarningMsgs=False)
        #print res
        print '</div>'
        
        resDictKeys = res.getResDictKeys()
        assert len(resDictKeys)==1, resDictKeys
        resDictKey = resDictKeys[0]
        targetBins = [bin for bin in res.keys() if res[bin][resDictKey]>0]
        self._result = res
        self._intersectedReferenceBins = targetBins            
Example #14
0
    def execute(cls, choices, galaxyFn=None, username=''):
        from gold.origdata.TrackGenomeElementSource import TrackViewListGenomeElementSource
        from gold.origdata.GtrackComposer import StdGtrackComposer

        genome = choices[0]
        if choices[1] == 'track':
            trackName = choices[2].split(':')
        else:
            trackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(
                genome, choices[2].split(':'))

        outFn = galaxyFn
        if choices[4] == 'Write to Standardised file':
            outFn = createOrigPath(genome, choices[-1].split(':'),
                                   'collapsed_result.bedgraph')
            ensurePathExists(outFn[:outFn.rfind('/') + 1])

        threshold = choices[3]
        analysisDef = 'dummy [threshold=%s] -> ForEachSegmentDistToNearestInSameTrackStat' % threshold  #'Python'
        res = GalaxyInterface.runManual([trackName], analysisDef, '*', '*', genome, username=username, \
                                        printResults=False, printHtmlWarningMsgs=False)

        tvGeSource = TrackViewListGenomeElementSource(
            genome, [x['Result'] for x in res.values()], trackName)
        StdGtrackComposer(tvGeSource).composeToFile(outFn)
Example #15
0
 def runAllAnalysises(self, genome, tn, regSpec, binSpec):
     for analysisKey, analysisVals in self.analysisDict.items():
             trackName, analysisDef, resKey = analysisVals
             trackNames = [tn] if trackName is None else [tn, trackName]
             try:
                 self.resultDict[analysisKey] = (resKey, GalaxyInterface.runManual(trackNames, analysisDef, regSpec, binSpec, genome, galaxyFn=None, printResults=False, printProgress=False))
             except:
                 pass
Example #16
0
 def analyseTrackData(genome, baseTn, username, galaxyFn):
     from quick.application.GalaxyInterface import GalaxyInterface
     print GalaxyInterface.getSubTrackNames(genome,
                                            baseTn,
                                            deep=False,
                                            username=username)
     allTns = [
         baseTn + [leafTn[0]]
         for leafTn in GalaxyInterface.getSubTrackNames(
             genome, baseTn, deep=False, username=username)[0]
         if leafTn != None
     ]
     mainTns = allTns[:2]
     extraTracks = '&'.join(['^'.join(tn) for tn in allTns[2:]])
     analysisDef = '[extraTracks=%s] -> ThreeWayCoverageDepthStat' % extraTracks
     print allTns, mainTns
     GalaxyInterface.runManual(mainTns, analysisDef, 'chr1:1-150m', '*', genome, galaxyFn=galaxyFn, username=username, \
               printResults=True, printProgress=True, printHtmlWarningMsgs=True, applyBoundaryFilter=False, printRunDescription=True)
    def build_feature_vector(genome, ctrack, feature, regSpec, binSpec):
        '''
        this function create a feature vector for ctrack
        feature specifies how the vector is constructed
        '''
        #print 'TEMP1:', ctrack, '<br>', LocalResultsAsFeaturesCatalog.getValidAnalyses(genome, ctrack, []),'<br>'
        #validFeature = LocalResultsAsFeaturesCatalog.getValidAnalyses(genome, ctrack, [])[feature]
        validFeature = LocalResultsAsFeaturesCatalog.getAllFeatures()[feature]
        analysisDef = validFeature[0]
        #regSpec = self.params.get("region")
        #binSpec = self.params.get("binsize")
        #userBinSource = GalaxyInterface._getUserBinSource(regSpec,binSpec,genome)

        #result = GalaxyInterface.runManual([ctrack], analysisDef, regSpec, binSpec, genome, printResults=False, printProgress=False)
        #result = AnalysisDefJob(analysisDef, ctrack, [], userBinSource).run()
        if validFeature[1] == 'Microbins':
            microBinConfig = 'extraDummy [microBin=%i] ' % int(
                binSpec.replace('k', '000').replace('m', '000000'))
            analysisDef = microBinConfig + analysisDef
            result = GalaxyInterface.runManual([ctrack],
                                               analysisDef,
                                               regSpec,
                                               '*',
                                               genome,
                                               printResults=False,
                                               printProgress=False)
            res = result.getGlobalResult()['Result']
            return res
        else:
            result = GalaxyInterface.runManual([ctrack],
                                               analysisDef,
                                               regSpec,
                                               binSpec,
                                               genome,
                                               printResults=False,
                                               printProgress=False)
            return [
                result[localKey][validFeature[1]]
                for localKey in sorted(result.keys())
            ]
Example #18
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.genome
        
        trackChoice = choices.history if choices.source == 'history' else choices.track
        trackName = trackChoice.split(':')
        
        galaxyOutTrackName = 'galaxy:hbfunction:%s:Create function track of distance to nearest segment' % galaxyFn
        outTrackName = ExternalTrackManager.getStdTrackNameFromGalaxyTN(galaxyOutTrackName.split(':'))
        
        if choices.transform == 'no transformation':
            valTransformation = 'None'
        elif choices.transform =='logarithmic (log10(x))':
            valTransformation = 'log10'
        elif choices.transform == 'fifth square root (x**0.2)':
            valTransformation = 'power0.2'
        else:
            raise ShouldNotOccurError
        
        analysisDef ='[dataStat=MakeDistanceToNearestSegmentStat] [valTransformation=%s][outTrackName=' % valTransformation \
                     + '^'.join(outTrackName) + '] -> CreateFunctionTrackStat'
        #userBinSource, fullRunArgs = GalaxyInterface._prepareRun(trackName, None, analysisDef, '*', '*', genome)
        #
        #for el in userBinSource:
        #    print el.chr, el.start, el.end
            
        from quick.application.GalaxyInterface import GalaxyInterface

        print GalaxyInterface.getHbFunctionOutputBegin(galaxyFn, withDebug=False)
        
        GalaxyInterface.runManual([trackName], analysisDef, '*', '*', genome, username=username, printResults=False, printHtmlWarningMsgs=False)
        #job = AnalysisDefJob(analysisDef, trackName, None, userBinSource).run()
        
        print GalaxyInterface.getHbFunctionOutputEnd('A custom track has been created by finding the bp-distance to the nearest segment', withDebug=False)
    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 config.Config import DebugConfig
        #DebugConfig.PASS_ON_VALIDSTAT_EXCEPTIONS = True
        #DebugConfig.PASS_ON_COMPUTE_EXCEPTIONS = True
        ##DebugConfig.VERBOSE = True
        #
        from quick.webtools.restricted.DebugAnalysisTool import DebugAnalysisTool        
        DebugAnalysisTool.setupDebugModeAndLogging(verbose=False) #set from selection box..
        
        analysisDef = choices[2].replace(os.linesep,'')
        #from gold.application.StatRunner import AnalysisDefJob

        print 'Preparing arguments'
        genome = choices[3]
        
        prefixTN1 = cls.STD_PREFIX_TN if choices[4] == 'yes' else []
        tn1 = prefixTN1 + choices[5].split(':') 
        prefixTN2 = cls.STD_PREFIX_TN if choices[6] == 'yes' else []
        tn2 = prefixTN2 + choices[7].split(':')
        if tn2[-1] == cls.NO_TRACK_SHORTNAME:
            tn2 = None
        #from gold.track.GenomeRegion import GenomeRegion
        #region = GenomeRegion(genome, 'chr1',1000,2000)
        #region2 = GenomeRegion(genome, 'chr1',5000,6000)
        
        #kwArgs = {}
        regVal = choices[8]
        binSpecVal = choices[9]
        #ubSource = UserBinSource(regVal, binSpecVal, genome=genome)
        from quick.application.GalaxyInterface import GalaxyInterface
        
        print tn1, tn2
        GalaxyInterface.runManual([tn1, tn2], analysisDef, regVal, binSpecVal, genome, galaxyFn, username=username)
Example #20
0
 def _runCategoryPointCount(cls, genome, regSpec, binSpec, trackName1, applyBoundaryFilter=True):
     analysisDef = 'Category point count: Number of elements each category of track1 (with overlaps)'+\
               '[tf1:=SegmentToStartPointFormatConverter:]'+\
               '-> FreqByCatStat'    
     print '<div class="debug">'
     res = GalaxyInterface.runManual([trackName1], analysisDef, regSpec, binSpec, genome, \
                                     printResults=False, printHtmlWarningMsgs=False, applyBoundaryFilter=applyBoundaryFilter)
     #userBinSource, fullRunArgs = GalaxyInterface._prepareRun(trackName1, None, analysisDef, regSpec, binSpec, genome)
     #if applyBoundaryFilter:
     #    userBinSource = RegionBoundaryFilter(userBinSource, GlobalBinSource(genome))
     #res = AnalysisDefJob(analysisDef, trackName1, None, userBinSource, **fullRunArgs).run()        
     print res        
     print '</div>'
     return res
Example #21
0
    def execute(cls, choices, galaxyFn=None, username=''):
        start = time.time()
        genome = choices[0]
        trackName = choices[1].split(':')
        #outFn = open(NONSTANDARD_DATA_PATH+'/hg19/Private/Sigven/resultat.bed','w')
        analysisDef = '-> ConvertToNonOverlappingCategorySegmentsPythonStat' #'Python'
        for regSpec in  GenomeInfo.getChrList(genome):
            res = GalaxyInterface.runManual([trackName], analysisDef, regSpec, '*', genome, username=username, \
                                            printResults=False, printHtmlWarningMsgs=False)

            from gold.origdata.TrackGenomeElementSource import TrackViewGenomeElementSource
            from gold.origdata.BedComposer import CategoryBedComposer
            for resDict in res.values():
                tvGeSource = TrackViewGenomeElementSource(genome, resDict['Result'], trackName)
                CategoryBedComposer(tvGeSource).composeToFile(outFn)
 def PrintResultToHistItem(cls, outFn, geSource, preProcTN1, genome, username):
     analysisDef = 'dummy ->SplitToCorrespondingSubBinsBasedOnBreakpointsStat'
     #outFile = open(outFn,'w')
     with open(outFn,'w') as outFile:
         print>>outFile, '##track type: valued segments\n###seqid\tstart\tend\tvalue\tstrand'
         for ge in geSource:
             regSpec = '%s:%i-%i' %(ge.chr, ge.start+1, ge.end) #+1 since assumed 1-indexed, end-inclusive
 
             #userBinSource, fullRunArgs = GalaxyInterface._prepareRun(preProcTN1, None, analysisDef, regSpec, '*', genome)
             #res = AnalysisDefJob(analysisDef, preProcTN1, None, userBinSource, **fullRunArgs).run(printProgress=False)
             #trackName
             res = GalaxyInterface.runManual([preProcTN1], analysisDef, regSpec, '*', genome, username=username, \
                                             printResults=False, printProgress=False, printHtmlWarningMsgs=False)
 
             for region,resDict in res.items():
                 print>>outFile, resDict['Result']
    def execute(cls, choices, galaxyFn=None, username=''):
        start = time.time()
        genome = choices[0]
        trackName = choices[1].split(':')
        #outFn = open(NONSTANDARD_DATA_PATH+'/hg19/Private/Sigven/resultat.bed','w')
        analysisDef = '-> ConvertToNonOverlappingCategorySegmentsPythonStat'  #'Python'
        for regSpec in GenomeInfo.getChrList(genome):
            res = GalaxyInterface.runManual([trackName], analysisDef, regSpec, '*', genome, username=username, \
                                            printResults=False, printHtmlWarningMsgs=False)

            from gold.origdata.TrackGenomeElementSource import TrackViewGenomeElementSource
            from gold.origdata.BedComposer import CategoryBedComposer
            for resDict in res.values():
                tvGeSource = TrackViewGenomeElementSource(
                    genome, resDict['Result'], trackName)
                CategoryBedComposer(tvGeSource).composeToFile(outFn)
Example #24
0
    def PrintResultToHistItem(cls, outFn, geSource, preProcTN1, genome,
                              username):
        analysisDef = 'dummy ->SplitToCorrespondingSubBinsBasedOnBreakpointsStat'
        #outFile = open(outFn,'w')
        with open(outFn, 'w') as outFile:
            print >> outFile, '##track type: valued segments\n###seqid\tstart\tend\tvalue\tstrand'
            for ge in geSource:
                regSpec = '%s:%i-%i' % (
                    ge.chr, ge.start + 1, ge.end
                )  #+1 since assumed 1-indexed, end-inclusive

                #userBinSource, fullRunArgs = GalaxyInterface._prepareRun(preProcTN1, None, analysisDef, regSpec, '*', genome)
                #res = AnalysisDefJob(analysisDef, preProcTN1, None, userBinSource, **fullRunArgs).run(printProgress=False)
                #trackName
                res = GalaxyInterface.runManual([preProcTN1], analysisDef, regSpec, '*', genome, username=username, \
                                                printResults=False, printProgress=False, printHtmlWarningMsgs=False)

                for region, resDict in res.items():
                    print >> outFile, resDict['Result']
Example #25
0
 def _runCategoryPointCount(cls,
                            genome,
                            regSpec,
                            binSpec,
                            trackName1,
                            applyBoundaryFilter=True):
     analysisDef = 'Category point count: Number of elements each category of track1 (with overlaps)'+\
               '[tf1:=SegmentToStartPointFormatConverter:]'+\
               '-> FreqByCatStat'
     print '<div class="debug">'
     res = GalaxyInterface.runManual([trackName1], analysisDef, regSpec, binSpec, genome, \
                                     printResults=False, printHtmlWarningMsgs=False, applyBoundaryFilter=applyBoundaryFilter)
     #userBinSource, fullRunArgs = GalaxyInterface._prepareRun(trackName1, None, analysisDef, regSpec, binSpec, genome)
     #if applyBoundaryFilter:
     #    userBinSource = GERegionBoundaryFilter(userBinSource, GlobalBinSource(genome))
     #res = AnalysisDefJob(analysisDef, trackName1, None, userBinSource, **fullRunArgs).run()
     print res
     print '</div>'
     return res
    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.
        '''

        analysisDef = 'dummy -> ProportionCountStat'
        genome = choices[0]
        tn1 = choices[2].split(':')
        binSpec = choices[4]
        regSpec = 'track'
        if choices[3] == 'from history':
            regSpec = ExternalTrackManager.extractFileSuffixFromGalaxyTN(
                choices[4].split(':'))
            binSpec = ExternalTrackManager.extractFnFromGalaxyTN(
                choices[4].split(':'))
        numBins = open(binSpec).read().count('\n')
        if numBins > 330000:
            gold.application.StatRunner.MAX_NUM_USER_BINS = numBins

        percent = float(choices[5]) if float(
            choices[5]) <= 1.0 else float(choices[5]) / 100.0
        GalaxyInterface.ALLOW_OVERLAPPING_USER_BINS = True
        resultDict = GalaxyInterface.runManual([tn1],
                                               analysisDef,
                                               regSpec,
                                               binSpec,
                                               genome,
                                               galaxyFn,
                                               printResults=False,
                                               printProgress=True)
        overlapRegions = [
            k for k, v in resultDict.items() if v['Result'] >= percent
        ]
        with open(galaxyFn, 'w') as utfil:
            for i in overlapRegions:
                print >> utfil, '\t'.join([i.chr, str(i.start), str(i.end)])
 def execute(cls, choices, galaxyFn=None, username=''):
     from gold.origdata.TrackGenomeElementSource import TrackViewListGenomeElementSource
     from gold.origdata.GtrackComposer import StdGtrackComposer
     genome = choices[0]
     if choices[1] == 'Track':
         trackName = choices[2].split(':')
     else:
         trackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, choices[2].split(':'))
         
     outFn = galaxyFn
     if choices[4] == 'Write to Standardised file':
         outFn = createOrigPath(genome, choices[-1].split(':'), 'collapsed_result.bedgraph')
         ensurePathExists(outFn[:outFn.rfind('/')+1])
            
     threshold = choices[3]
     analysisDef = 'dummy [threshold=%s] -> ForEachSegmentDistToNearestInSameTrackStat' % threshold #'Python'
     res = GalaxyInterface.runManual([trackName], analysisDef, '*', '*', genome, username=username, \
                                     printResults=False, printHtmlWarningMsgs=False)
             
     tvGeSource = TrackViewListGenomeElementSource(genome, [x['Result'] for x in res.values()], trackName)    
     StdGtrackComposer(tvGeSource).composeToFile(outFn)
Example #28
0
    def execute(cls, choices, galaxyFn=None, username=''):
        from quick.application.ProcTrackOptions import ProcTrackOptions

        from quick.application.GalaxyInterface import GalaxyInterface
        import gold.application.StatRunner

        analysisDef = 'dummy -> PercentageChangeStat'
        genome = choices[0]
        binSpec = '*'
        regSpec = 'Days_1900_2036:36890-41424'
        tnRoot = 'Company stocks:Historical prices:OSE:'
        stockList = [k for k, v in choices[1].items() if v]

        numStocks = 0
        totalPercent = 0.0
        for stock in stockList:
            tn = tnRoot + stock
            if ProcTrackOptions.isValidTrack(genome,
                                             tn.split(':'),
                                             fullAccess=True):
                resultDict = GalaxyInterface.runManual(
                    [tn.split(':'), choices[2].split(':')],
                    analysisDef,
                    regSpec,
                    binSpec,
                    'days',
                    galaxyFn,
                    printResults=False,
                    printProgress=False)

                for k, v in resultDict.items():
                    print 'increase (from jan. 2001 - jun. 2013) for ', stock, ':  ', v
                    res = v['Result']
                    if res == 0.0 or res > 10000:
                        continue
                    totalPercent += v['Result']
                    numStocks += 1
            else:
                print 'this is not a valid track', tn
        print 'Average increase (from jan. 2001 - jun. 2013):  ', totalPercent / numStocks, ' (number of stocks =', numStocks, ')'
    def execute(choices, galaxyFn=None, username=''):
        '''
        Is called when execute-button is pushed by web-user. Should print
        output as HTML to standard out, which will be directed to a results page
        in Galaxy history. If getOutputFormat is anything else than HTML, the
        output should be written to the file with path galaxyFn. If needed,
        StaticFile can be used to get a path where additional files can be put
        (e.g. generated image files). choices is a list of selections made by
        web-user in each options box.
        '''
        from gold.application.LogSetup import setupDebugModeAndLogging
        setupDebugModeAndLogging()

        targetTrackNames, targetTrackCollection, targetTrackGenome = getGSuiteDataFromGalaxyTN(
            choices.gSuiteFirst)
        targetTracksDict = OrderedDict(
            zip(targetTrackNames, targetTrackCollection))
        refTrackNames, refTrackCollection, refTrackCollectionGenome = getGSuiteDataFromGalaxyTN(
            choices.gSuiteSecond)
        assert targetTrackGenome == refTrackCollectionGenome, 'Reference genome must be the same one in both GSuite files.'
        refTracksDict = OrderedDict(zip(refTrackNames, refTrackCollection))

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)

        analysisDef = 'dummy -> RawOverlapStat'
        results = OrderedDict()
        for targetTrackName, targetTrack in targetTracksDict.iteritems():
            for refTrackName, refTrack in refTracksDict.iteritems():
                result = GalaxyInterface.runManual([targetTrack, refTrack],
                                                   analysisDef,
                                                   regSpec,
                                                   binSpec,
                                                   targetTrackGenome,
                                                   galaxyFn,
                                                   printRunDescription=False,
                                                   printResults=False)
                if targetTrackName not in results:
                    results[targetTrackName] = OrderedDict()
                results[targetTrackName][
                    refTrackName] = result.getGlobalResult()

        targetTrackTitles = results.keys()

        stat = choices.statistic
        statIndex = STAT_LIST_INDEX[stat]
        title = stat + ' analysis of track collections'

        processedResults = []
        headerColumn = []
        for targetTrackName in targetTrackTitles:
            resultRowDict = processRawResults(results[targetTrackName])
            resultColumn = []
            headerColumn = []
            for refTrackName, statList in resultRowDict.iteritems():
                resultColumn.append(statList[statIndex])
                headerColumn.append(refTrackName)
            processedResults.append(resultColumn)

        transposedProcessedResults = [list(x) for x in zip(*processedResults)]

        tableHeader = ['Track names'] + targetTrackTitles
        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.header(title)
        htmlCore.divBegin('resultsDiv')
        htmlCore.tableHeader(tableHeader,
                             sortable=True,
                             tableId='resultsTable')
        for i, row in enumerate(transposedProcessedResults):
            line = [headerColumn[i]] + row
            htmlCore.tableLine(line)
        htmlCore.tableFooter()
        htmlCore.divEnd()

        addColumnPlotToHtmlCore(htmlCore,
                                targetTrackNames,
                                refTrackNames,
                                stat,
                                title + ' plot',
                                processedResults,
                                xAxisRotation=315)

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

        print htmlCore
Example #30
0
    def execute(cls, choices, galaxyFn=None, username=''):
        cls._setDebugModeIfSelected(choices)

        targetGSuite = getGSuiteFromGalaxyTN(choices.gSuiteFirst)
        refGSuite = getGSuiteFromGalaxyTN(choices.gSuiteSecond)

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)

        analysisDef = 'dummy -> RawOverlapStat'
        # analysisDef = 'dummy [withOverlaps=yes] -> RawOverlapAllowSingleTrackOverlapsStat'
        results = OrderedDict()

        for targetTrack in targetGSuite.allTracks():
            targetTrackName = targetTrack.title
            for refTrack in refGSuite.allTracks():
                refTrackName = refTrack.title
                if targetTrack.trackName == refTrack.trackName:
                    # print targetTrack.title
                    # print targetTrack.trackName
                    result = DetermineSuiteTracksCoincidingWithAnotherSuite.handleSameTrack(
                        targetTrack.trackName, regSpec, binSpec,
                        targetGSuite.genome, galaxyFn)
                else:
                    result = GalaxyInterface.runManual(
                        [targetTrack.trackName, refTrack.trackName],
                        analysisDef,
                        regSpec,
                        binSpec,
                        targetGSuite.genome,
                        galaxyFn,
                        printRunDescription=False,
                        printResults=False,
                        printProgress=False).getGlobalResult()
                if targetTrackName not in results:
                    results[targetTrackName] = OrderedDict()
                results[targetTrackName][refTrackName] = result

        stat = STAT_OVERLAP_COUNT_BPS
        statIndex = STAT_LIST_INDEX[stat]
        title = ''

        processedResults = []
        headerColumn = []
        for targetTrackName in targetGSuite.allTrackTitles():
            resultRowDict = processRawResults(results[targetTrackName])
            resultColumn = []
            headerColumn = []
            for refTrackName, statList in resultRowDict.iteritems():
                resultColumn.append(statList[statIndex])
                headerColumn.append(refTrackName)
            processedResults.append(resultColumn)

        outputTable = {}
        for elN in range(0, len(headerColumn)):
            outputTable[elN] = {}
            outputTable[elN]['id'] = headerColumn[elN]

        transposedProcessedResults = [list(x) for x in zip(*processedResults)]

        # second question sumSecondgSuite
        # first question numSecondgSuite
        # fifth question numSecondgSuitePercentage
        for i in range(0, len(transposedProcessedResults)):
            outputTable[i]['sumSecondgSuite'] = sum(
                transposedProcessedResults[i])
            if not 'numSecondgSuite' in outputTable[i]:
                outputTable[i]['numSecondgSuite'] = 0
            for j in range(0, len(transposedProcessedResults[i])):
                if transposedProcessedResults[i][j] >= 1:
                    outputTable[i]['numSecondgSuite'] += 1
                else:
                    outputTable[i]['numSecondgSuite'] += 0
            outputTable[i]['numSecondgSuitePercentage'] = float(
                outputTable[i]['numSecondgSuite']) / float(
                    targetGSuite.numTracks()) * 100

        from gold.statistic.CountSegmentStat import CountSegmentStat
        from gold.statistic.CountPointStat import CountPointStat
        from gold.description.TrackInfo import TrackInfo
        from gold.statistic.CountStat import CountStat

        # third question numPairBpSecondgSuite
        # fourth question numFreqBpSecondgSuite
        i = 0
        for refTrack in refGSuite.allTracks():
            formatName = TrackInfo(refTrack.genome,
                                   refTrack.trackName).trackFormatName
            analysisDef = CountStat
            analysisBins = GalaxyInterface._getUserBinSource(
                regSpec, binSpec, refTrack.genome)
            results = doAnalysis(AnalysisSpec(analysisDef), analysisBins,
                                 [PlainTrack(refTrack.trackName)])
            resultDict = results.getGlobalResult()
            if len(resultDict) == 0:
                outputTable[i]['numPairBpSecondgSuite'] = None
                outputTable[i]['numFreqBpSecondgSuite'] = None
                outputTable[i]['numFreqUniqueBpSecondgSuite'] = None
            else:
                outputTable[i]['numPairBpSecondgSuite'] = resultDict['Result']

                if outputTable[i]['numPairBpSecondgSuite'] != 0:
                    outputTable[i]['numFreqBpSecondgSuite'] = float(
                        outputTable[i]['sumSecondgSuite']) / float(
                            outputTable[i]['numPairBpSecondgSuite'])
                else:
                    outputTable[i]['numFreqBpSecondgSuite'] = None

                if outputTable[i]['sumSecondgSuite'] != 0:
                    outputTable[i]['numFreqUniqueBpSecondgSuite'] = float(
                        outputTable[i]['numPairBpSecondgSuite']) / float(
                            outputTable[i]['sumSecondgSuite'])
                else:
                    outputTable[i]['numFreqUniqueBpSecondgSuite'] = None

            i += 1

        # sortTable
        outputTableLine = []
        for key, item in outputTable.iteritems():
            line = [
                item['id'], item['numSecondgSuite'], item['sumSecondgSuite'],
                item['numPairBpSecondgSuite'], item['numFreqBpSecondgSuite'],
                item['numFreqUniqueBpSecondgSuite'],
                item['numSecondgSuitePercentage']
            ]
            outputTableLine.append(line)

        import operator
        outputTableLineSort = sorted(outputTableLine,
                                     key=operator.itemgetter(1),
                                     reverse=True)

        tableHeader = [
            'Region ID ', 'Number of cases with at least one event ',
            'Total number of events', 'Genome coverage (unique bp)',
            'Number of events per unique bp', 'Number of unique bp per event',
            'Percentage of cases with at least one event'
        ]
        htmlCore = HtmlCore()

        htmlCore.begin()

        htmlCore.line(
            "<b>Identification of genomic elements with high event recurrence</b> "
        )

        htmlCore.header(title)
        htmlCore.divBegin('resultsDiv')
        htmlCore.tableHeader(tableHeader,
                             sortable=True,
                             tableId='resultsTable')

        for line in outputTableLineSort:
            htmlCore.tableLine(line)

        plotRes = []
        plotXAxis = []
        for lineInx in range(1, len(outputTableLineSort[0])):
            plotResPart = []
            plotXAxisPart = []
            for lineInxO in range(0, len(outputTableLineSort)):
                # if outputTableLineSort[lineInxO][lineInx]!=0 and
                # if outputTableLineSort[lineInxO][lineInx]!=None:
                plotResPart.append(outputTableLineSort[lineInxO][lineInx])
                plotXAxisPart.append(outputTableLineSort[lineInxO][0])
            plotRes.append(plotResPart)
            plotXAxis.append(plotXAxisPart)

        htmlCore.tableFooter()
        htmlCore.divEnd()

        htmlCore.divBegin('plot', style='padding-top:20px;margin-top:20px;')

        vg = visualizationGraphs()
        res = vg.drawColumnCharts(
            plotRes,
            titleText=tableHeader[1:],
            categories=plotXAxis,
            height=500,
            xAxisRotation=270,
            xAxisTitle='Ragion ID',
            yAxisTitle='Number of cases with at least one event',
            marginTop=30,
            addTable=True,
            sortableAccordingToTable=True,
            legend=False)
        htmlCore.line(res)
        htmlCore.divEnd()

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

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

#         targetTrackNames, targetTrackCollection, targetTrackGenome = getGSuiteDataFromGalaxyTN(choices.gSuiteFirst)
#         targetTracksDict = OrderedDict(zip(targetTrackNames, targetTrackCollection))
#         refTrackNames, refTrackCollection, refTrackCollectionGenome = getGSuiteDataFromGalaxyTN(choices.gSuiteSecond)
#         refTracksDict = OrderedDict(zip(refTrackNames, refTrackCollection))
#         
        targetGSuite = getGSuiteFromGalaxyTN(choices.gSuiteFirst)
        refGSuite = getGSuiteFromGalaxyTN(choices.gSuiteSecond)

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)


        if choices.intraOverlap == TrackCollectionsAnalysis.MERGE_INTRA_OVERLAPS:
            analysisDef = 'dummy -> RawOverlapStat'
        else:
            analysisDef = 'dummy [withOverlaps=yes] -> RawOverlapAllowSingleTrackOverlapsStat'
        results = OrderedDict()
#         for targetTrackName, targetTrack in targetTracksDict.iteritems():
#             for refTrackName, refTrack in refTracksDict.iteritems():
        for targetTrack in targetGSuite.allTracks():
            targetTrackName = targetTrack.title
            for refTrack in refGSuite.allTracks():
                refTrackName = refTrack.title
                if targetTrack.trackName == refTrack.trackName:
                    result = TrackCollectionsAnalysis.handleSameTrack(targetTrack.trackName, regSpec, binSpec,
                                                       choices.genome, galaxyFn)
                else:
                    result = GalaxyInterface.runManual([targetTrack.trackName, refTrack.trackName],
                                                       analysisDef, regSpec, binSpec,
                                                       choices.genome, galaxyFn,
                                                       printRunDescription=False,
                                                       printResults=False).getGlobalResult()
                if targetTrackName not in results :
                    results[targetTrackName] = OrderedDict()
                results[targetTrackName][refTrackName] = result

        stat = choices.statistic
        statIndex = STAT_LIST_INDEX[stat]
        title = 'Screening track collections  (' + stat + ')'

        processedResults = []
        headerColumn = []
        for targetTrackName in targetGSuite.allTrackTitles():
            resultRowDict = processRawResults(results[targetTrackName])
            resultColumn = []
            headerColumn = []
            for refTrackName, statList in resultRowDict.iteritems():
                resultColumn.append(statList[statIndex])
                headerColumn.append(refTrackName)
            processedResults.append(resultColumn)

        transposedProcessedResults = [list(x) for x in zip(*processedResults)]

        tableHeader = ['Track names'] + targetGSuite.allTrackTitles()
        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.header(title)
        htmlCore.divBegin('resultsDiv')
        htmlCore.tableHeader(tableHeader, sortable=True, tableId='resultsTable')
        for i, row in enumerate(transposedProcessedResults):
            line = [headerColumn[i]] + [strWithStdFormatting(x) for x in row]
            htmlCore.tableLine(line)
        htmlCore.tableFooter()
        htmlCore.divEnd()

#         #hicharts can't handle strings that contain ' or " as input for series names
        targetTrackNames = [x.replace('\'', '').replace('"','') for x in targetGSuite.allTrackTitles()]
        refTrackNames = [x.replace('\'', '').replace('"','') for x in refGSuite.allTrackTitles()]
# 
#         '''
#         addColumnPlotToHtmlCore(htmlCore, targetTrackNames, refTrackNames,
#                                 stat, title + ' plot',
#                                 processedResults, xAxisRotation = -45, height=800)
#         '''
#         '''
#         addPlotToHtmlCore(htmlCore, targetTrackNames, refTrackNames,
#                                 stat, title + ' plot',
#                                 processedResults, xAxisRotation = -45, height=400)
#         '''
#         
        from quick.webtools.restricted.visualization.visualizationGraphs import visualizationGraphs
        vg = visualizationGraphs()
        result = vg.drawColumnChart(processedResults,
                      height=600,
                      yAxisTitle=stat,
                      categories=refTrackNames,
                      xAxisRotation=90,
                      seriesName=targetTrackNames,
                      shared=False,
                      titleText=title + ' plot',
                      overMouseAxisX=True,
                      overMouseLabelX = ' + this.value.substring(0, 10) +')
        
        htmlCore.line(result)
        #htmlCore.line(vg.visualizeResults(result, htmlCore))
        
        htmlCore.hideToggle(styleClass='debug')
        htmlCore.end()

        print htmlCore
    def makeAnalysis(cls, gSuite1, gSuite2, columnsForStat, galaxyFn, choices):

        genomeType = 'single'
        
        #if True then none of column in both gsuite are equal then for example, for gSUite 2 dim and 3 dim we have 5 dim
        # in the other case (when some columns are equal we have 4 dim)
        checkWhichDimension = True 
        for el in columnsForStat:
            if el == 0:
                checkWhichDimension=False
                break
                
        
        if choices.type == 'basic':        
            stat = choices.statistic
            statIndex = CountStatisticForCombinationsFromTwoLevelSuites.STAT_LIST_INDEX
            statIndex = statIndex.index(stat)
        else:
            stat= '0'
            statIndex = 0
        
        if choices.intraOverlap == CountStatisticForCombinationsFromTwoLevelSuites.MERGE_INTRA_OVERLAPS:
            analysisDef = 'dummy -> RawOverlapStat'
        else:
            analysisDef = 'dummy [withOverlaps=yes] -> RawOverlapAllowSingleTrackOverlapsStat'
        
        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)
        
        listResults = []
        for el1 in gSuite1:
            for el2 in gSuite2:
                if genomeType == 'single':
                    
                    listPartResults=[]

                    if (checkWhichDimension == True and el1[columnsForStat[0]] == el2[columnsForStat[1]]) or (checkWhichDimension == False):
                        gSuite1Path = el1[0]#path for track1
                        gSuite2Path = el2[0]#path for track2
                        
                        #print [gSuite1Path, gSuite2Path]
                        
                        result = GalaxyInterface.runManual([gSuite1Path, gSuite2Path],
                                   analysisDef, regSpec, binSpec, choices.genome, galaxyFn,
                                   printRunDescription=False,
                                   printResults=False)
                        
                        
                        #print str(result) + '<br \>'

                        #print str(processResult(result.getGlobalResult())) + '<br \>'

                        resVal = processResult(result.getGlobalResult())[statIndex]
                        
                    else:
                        resVal = 0
                    
                    if checkWhichDimension == False:
                        listPartResults = el1[1:] + el2[1:] + [resVal]
                    else:
                        shorterEl2=[]
                        for eN in range(0, len(el2[1:])):
                            if el2[1:][eN] != el2[columnsForStat[1]]:
                                shorterEl2.append(el2[1:][eN])
                        #listPartResults = el1[1:] + shorterEl2 + [resVal]
                        listPartResults = el1[1:] + el2[1:] + [resVal]
                        
                    listResults.append(listPartResults)
        
                    
                if genomeType == 'multi':
                    #count only values with the same genome
                    pass
            
        return listResults
Example #33
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.
     '''
     regSpec = '*'
     binSpec = '*'
     analysisDef = 'dummy -> PropPointCountsAllowOverlapsVsSegsStat'
     genome = choices[0]
     tn1 = choices[1].split(':')
     trInfo1 = TrackInfo(genome, tn1)
     trackType = trInfo1.trackFormatName
     resultDict = defaultdict(dict)
     singleTrackDict = defaultdict(list)
     geneSourceList = []
     singleRuns = 0
     trackType, trackCatObj = cls.getTrackTypeAnalysis(genome, trackType)
     trackCatObj.runAllAnalysises(genome, tn1, regSpec, binSpec)
     
     #for index, values  in  enumerate(analysisList):
     #    geneSource, AnalysisElements = values
     #    
     #    if geneSource:
     #        geneSourceList +=[geneSource]
     #    else:
     #        singleRuns +=1
     #    if not geneSource and singleRuns>1:
     #        continue
     #    
     #    for analysisKey, analysisVals in AnalysisElements.items():
     #        trackName, analysisDef, resKey = analysisVals
     #        trackNames = [tn1] if trackName is None else [tn1, trackName]
     #        if trackName == None:
     #            if index>0:
     #                
     #                gen, an = singleTrackDict[analysisKey]
     #                resultDict[geneSource][analysisKey] = resultDict[gen][an]
     #                
     #            else: 
     #                singleTrackDict[analysisKey]+=[geneSource, analysisKey]
     #                resultDict[geneSource][analysisKey] = (resKey, GalaxyInterface.runManual(trackNames, analysisDef, regSpec, binSpec, genome, galaxyFn, printResults=False, printProgress=False))
     #                
     #        else:    
     #            resultDict[geneSource][analysisKey] = (resKey, GalaxyInterface.runManual(trackNames, analysisDef, regSpec, binSpec, genome, galaxyFn, printResults=False, printProgress=False))
     #            
                 
     
     core = HtmlCore()
     core.begin(extraJavaScriptFns=['tabber.js','https://www.google.com/jsapi'],\
                extraCssFns=['tabber.css'])
     stack = []
     
     SummaryTextTemplate = '''<div style="background-color:#FFE899;"><h3>Track report:</h3><br/><b>The track consists of %i elements along the genome (%i after merging overlapping elements), and covers %i base pairs (%s percent ) of the genome.
     The distribution of track elements along the genome is *visualized below*. It overlaps %s percent with exons,
     %s percent with introns, and %s percent with remaining inter-genic regions (according to the Ensembl gene definition, version <E>).
     Corresponding numbers for other gene definitions, as well as local results per chromosome, are given in the *tables below*. </b><br></div>'''# %(resultDict[geneSourceList[0]]['CountPointStat'].getGlobalResult()['Result'])
     
     
     #if singleTrackDict.has_key('CountPointStat'):
     if trackCatObj.resultDict.has_key('CountPointStat'):
     
         
         #globalResDict, localResDict = cls.makeGlobalAndLocalResDicts(resultDict)
         globalResDict, localResDict = trackCatObj.makeGlobalAndLocalResDicts()
         geneSourceList = globalResDict.keys()
         print 'globalResDict', globalResDict
         print 'localResDict', localResDict
         if len(geneSourceList)==0:
             exonPercent, intronsPercent, interGeneticPercent = '0','0','0'
             geneSourceList += [None]
         else:
             sectionsEnsmblRes = globalResDict[geneSourceList[0]]
             totalEnsmbl = float(sum(sectionsEnsmblRes))
             exonPercent = str(round(sectionsEnsmblRes[0]*100/totalEnsmbl, 2)) if sectionsEnsmblRes>0 else '0'
             intronsPercent = str(round(sectionsEnsmblRes[1]*100/totalEnsmbl, 2)) if totalEnsmbl>0 else '0'
             interGeneticPercent = str(round(sectionsEnsmblRes[2]*100/totalEnsmbl, 2) ) if totalEnsmbl>0 else '0'
         
         
         resKey, result = trackCatObj.resultDict.get('CountPointStat')
         bpCoverage = numElems = int(result.getGlobalResult()[resKey])
         
         resKey, result = trackCatObj.resultDict.get('numElAllowOverlap')
         numUniqueElems = int(result.getGlobalResult()[resKey])
         
         if trackType.lower().find('segment')>=0:
             resKey, result = trackCatObj.resultDict.get('bpCoverage')
             bpCoverage = int(result.getGlobalResult()[resKey])
         
         genomeBps = sum( GenomeInfo.getStdChrLengthDict(genome).values())
         core.paragraph(SummaryTextTemplate % (numUniqueElems, numElems, bpCoverage, str(round(float(bpCoverage)/genomeBps, 2)) , exonPercent, intronsPercent, interGeneticPercent))
         core._str += '<div class="tabber">\n'
         stack.append('</div>\n')
         
         analysisDef = ' [centerRows=True] [normalizeRows=True] -> RawVisualizationDataStat'
         res = GalaxyInterface.runManual([tn1], analysisDef, regSpec, binSpec, genome, username=username, printResults=False, printHtmlWarningMsgs=False)
        
         cls.MakeGlobalResultsDiv(globalResDict, res, core, galaxyFn)
         cls.MakeLocalResultsDiv(localResDict, core)
         
         core._str += stack.pop()
         if len(cls.pieList)>0:
             core.line(cls.makeAllGooglePieChart(cls.pieList))
         
         print core
Example #34
0
    def execute(choices, galaxyFn=None, username=''):

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

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

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

        regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)

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

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

        htmlCore = HtmlCore()
        htmlCore.begin()

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

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

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

        targetTrackNameList = targetTrackFeatureTitles

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

        htmlCore.divBegin('results')

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

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

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

        import gold.gsuite.GSuiteComposer as GSuiteComposer
        from gold.gsuite.GSuite import GSuite
        from gold.gsuite.GSuiteTrack import GSuiteTrack, HbGSuiteTrack
        from gold.origdata.TrackGenomeElementSource import TrackViewListGenomeElementSource
        from gold.origdata.FileFormatComposer import getComposerClsFromFileSuffix
        from quick.multitrack.MultiTrackCommon import getGSuiteFromGalaxyTN
        from quick.application.ExternalTrackManager import ExternalTrackManager
        from quick.application.GalaxyInterface import GalaxyInterface
        from quick.application.UserBinSource import UserBinSource
        from quick.extra.TrackExtractor import TrackExtractor

        genome = choices.genome
        gSuite = getGSuiteFromGalaxyTN(choices.gSuite)

        if choices.withOverlaps == cls.NO_OVERLAPS:
            if choices.trackSource == cls.FROM_HISTORY_TEXT:
                filterTrackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, choices.trackHistory)
            else:
                filterTrackName = choices.track.split(':')
        else:
            if choices.trackSource == cls.FROM_HISTORY_TEXT:
                regSpec = ExternalTrackManager.extractFileSuffixFromGalaxyTN(choices.trackHistory)
                binSpec = ExternalTrackManager.extractFnFromGalaxyTN(choices.trackHistory)
            else:
                regSpec = 'track'
                binSpec = choices.track

            userBinSource = UserBinSource(regSpec, binSpec, genome)

        desc = cls.OUTPUT_GSUITE_DESCRIPTION
        emptyFn = cls.extraGalaxyFn \
            [getGSuiteHistoryOutputName('nointersect', description=desc, datasetInfo=choices.gSuite)]
        primaryFn = cls.extraGalaxyFn \
            [getGSuiteHistoryOutputName('primary', description=desc, datasetInfo=choices.gSuite)]
        errorFn = cls.extraGalaxyFn \
            [getGSuiteHistoryOutputName('nopreprocessed', description=desc, datasetInfo=choices.gSuite)]
        preprocessedFn = cls.extraGalaxyFn \
            [getGSuiteHistoryOutputName('preprocessed', description=desc, datasetInfo=choices.gSuite)]
        hiddenStorageFn = cls.extraGalaxyFn \
            [getGSuiteHistoryOutputName('storage', description=desc, datasetInfo=choices.gSuite)]

        analysisDef = '-> TrackIntersectionStat'
#         analysisDef = '-> TrackIntersectionWithValStat'

        numTracks = gSuite.numTracks()
        progressViewer = ProgressViewer([(cls.PROGRESS_INTERSECT_MSG, numTracks),
                                         (cls.PROGRESS_PREPROCESS_MSG, numTracks)], galaxyFn)
        emptyGSuite = GSuite()
        primaryGSuite = GSuite()

        for track in gSuite.allTracks():
            newSuffix = cls.OUTPUT_TRACKS_SUFFIX
            extraFileName = os.path.sep.join(track.trackName)
            extraFileName = changeSuffixIfPresent(extraFileName, newSuffix=newSuffix)
            title = getTitleWithSuffixReplaced(track.title, newSuffix)

            primaryTrackUri = GalaxyGSuiteTrack.generateURI(
                galaxyFn=hiddenStorageFn, extraFileName=extraFileName,
                suffix=newSuffix if not extraFileName.endswith(newSuffix) else '')
            primaryTrack = GSuiteTrack(primaryTrackUri, title=title,
                                       genome=track.genome, attributes=track.attributes)

            if choices.withOverlaps == cls.NO_OVERLAPS:
                res = GalaxyInterface.runManual([track.trackName, filterTrackName], analysisDef, '*', '*',
                                                 genome=genome, galaxyFn=galaxyFn, username=username)

                trackViewList = [res[key]['Result'] for key in sorted(res.keys())]

                tvGeSource = TrackViewListGenomeElementSource(genome, trackViewList)

                composerCls = getComposerClsFromFileSuffix(cls.OUTPUT_TRACKS_SUFFIX)
                composerCls(tvGeSource).composeToFile(primaryTrack.path)
            else:
                TrackExtractor.extractOneTrackManyRegsToOneFile( \
                    track.trackName, userBinSource, primaryTrack.path, fileFormatName=cls.OUTPUT_TRACKS_SUFFIX, \
                    globalCoords=True, asOriginal=False, allowOverlaps=True)

            # Temporary hack until better solution for empty result tracks have been implemented

            from gold.origdata.GenomeElementSource import GenomeElementSource
            geSource = GenomeElementSource(primaryTrack.path, genome=genome, suffix=cls.OUTPUT_TRACKS_SUFFIX)

            try:
                geSource.parseFirstDataLine()
                primaryGSuite.addTrack(primaryTrack)
            except Exception, e: # Most likely empty file
                primaryTrack.comment = e.message
                emptyGSuite.addTrack(primaryTrack)
                numTracks -= 1
                progressViewer.updateProgressObjectElementCount(
                    cls.PROGRESS_PREPROCESS_MSG, numTracks)
            #

            progressViewer.update()
    def execute(cls, choices, galaxyFn=None, username=''):
        path = str(URL_PREFIX)
        dataset = choices.dataset
        genome = choices.genome
        text = choices.newtrack
        secondDataset = choices.newdataset
        inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(dataset),
                         'r')
        with inputFile as f:
            data = [x for x in f.readlines()]
        silenceRWarnings()
        binSourceParam = '*'
        regSourceParam = '*'
        trackNamePrep = cls.preprocessTrack(genome, dataset)

        if text == 'No':

            figUrl = ''
            if (len(data) > 30000):

                core = HtmlCore()
                core.styleInfoBegin(styleClass='debug')
                figImage = GalaxyRunSpecificFile(['VizTrackOnGenome.png'],
                                                 galaxyFn)
                analysisDef = ' [normalizeRows=%s] [centerRows=%s]  -> RawVisualizationDataStat'

                res = GalaxyInterface.runManual([trackNamePrep],
                                                analysisDef,
                                                regSourceParam,
                                                binSourceParam,
                                                genome,
                                                username=username,
                                                printResults=False,
                                                printHtmlWarningMsgs=False)
                core.styleInfoEnd()
                core.line('')
                core.tableHeader(None)
                rScript = VisualizeTrackPresenceOnGenome.customRExecution(
                    res, figImage.getDiskPath(ensurePath=True), '')
                figUrl = figImage.getURL()
                print GalaxyInterface.getHtmlEndForRuns()
                binSourceParam = '10m'
                regSourceParam = '*'
                cls.resultPrintGeneric(genome, binSourceParam, regSourceParam,
                                       figUrl, path, trackNamePrep)

            else:
                if isinstance(trackNamePrep[0], (list, )):
                    numTracks = len(trackNamePrep[0])
                    firstTrack = cls.prepareTracknameForURL(trackNamePrep[0])
                    trackTitle = json.dumps(trackNamePrep[1])
                    cls.resultPrintGSuite(genome, binSourceParam,
                                          regSourceParam, figUrl, path,
                                          firstTrack, trackTitle, numTracks)
                else:
                    firstTrack = cls.prepareTracknameForURL(trackNamePrep)
                    cls.resultPrintGeneric(genome, binSourceParam,
                                           regSourceParam, figUrl, path,
                                           firstTrack)
        else:
            trackName2 = cls.preprocessTrack(genome, secondDataset)
            firstTrack = cls.prepareTracknameForURL(trackNamePrep)
            secondTrack = cls.prepareTracknameForURL(trackName2)
            cls.resultPrintOverlap(genome, binSourceParam, regSourceParam,
                                   path, firstTrack, secondTrack)
Example #37
0
    def findOverrepresentedTFsFromGeneSet(genome, tfSource, ensembleGeneIdList,
                                          upFlankSize, downFlankSize,
                                          geneSource, galaxyFn):
        #galaxyFn = '/usit/insilico/web/lookalike/galaxy_dist-20090924-dev/database/files/003/dataset_3347.dat'
        #print 'overriding galaxyFN!: ', galaxyFn
        galaxyId = extractIdFromGalaxyFn(galaxyFn)
        uniqueWebPath = GalaxyRunSpecificFile([], galaxyFn).getDiskPath()

        assert genome == 'hg18'

        tfTrackNameMappings = TfInfo.getTfTrackNameMappings(genome)
        tfTrackName = tfTrackNameMappings[tfSource]

        #Get gene track
        assert geneSource == 'Ensembl'
        targetGeneRegsTempFn = uniqueWebPath + os.sep + 'geneRegs.bed'
        geneRegsTrackName = GenomeInfo.getStdGeneRegsTn(genome)
        geneRegsFn = getOrigFn(genome, geneRegsTrackName, '.category.bed')
        GalaxyInterface.getGeneTrackFromGeneList(genome, geneRegsTrackName,
                                                 ensembleGeneIdList,
                                                 targetGeneRegsTempFn)

        assert upFlankSize == downFlankSize == 0  #Should instead extend regions to include flanks

        tcGeneRegsTempFn = uniqueWebPath + os.sep + 'tcGeneRegs.targetcontrol.bedgraph'
        #Think this will be okay, subtraction not necessary as targets are put first:
        controlGeneRegsTempFn = geneRegsFn
        #print targetGeneRegsTempFn, controlGeneRegsTempFn, tcGeneRegsTempFn
        GalaxyInterface.combineToTargetControl(targetGeneRegsTempFn,
                                               controlGeneRegsTempFn,
                                               tcGeneRegsTempFn)

        #tcGeneRegsExternalTN = ['external'] +galaxyId +  [tcGeneRegsTempFn]
        tcGeneRegsExternalTN = ExternalTrackManager.createStdTrackName(
            galaxyId + ['tempTc'])

        #tcGeneRegsExternalTN = ['external'] +targetGalaxyId +  [tcGeneRegsTempFn]
        #tcGeneRegsExternalTN = ['galaxy', externalId, tcGeneRegsTempFn]

        targetGeneRegsExternalTN = ExternalTrackManager.createStdTrackName(
            galaxyId + ['tempTc', '1'])
        controlGeneRegsExternalTN = ExternalTrackManager.createStdTrackName(
            galaxyId + ['tempTc', '0'])

        #pre-process
        print 'Pre-processing file: %s, with trackname: %s ' % (
            tcGeneRegsTempFn, tcGeneRegsExternalTN)
        ExternalTrackManager.preProcess(tcGeneRegsTempFn, tcGeneRegsExternalTN,
                                        'targetcontrol.bedgraph', genome)
        print 'Pre-processing TN: ', targetGeneRegsExternalTN
        ExternalTrackManager.preProcess(targetGeneRegsTempFn,
                                        targetGeneRegsExternalTN, 'bed',
                                        genome)
        print 'Pre-processing TN: ', controlGeneRegsExternalTN
        ExternalTrackManager.preProcess(controlGeneRegsTempFn,
                                        controlGeneRegsExternalTN, 'bed',
                                        genome)

        #print tcGeneRegsExternalTN
        trackName1, trackName2 = tfTrackName, tcGeneRegsExternalTN

        analysisDef = 'Categories differentially located in targets?: Which categories of track1-points fall more inside case than control track2-segments? [rawStatistic:=PointCountInsideSegsStat:]' +\
                  '[tf1:=SegmentToStartPointFormatConverter:] [tf2:=TrivialFormatConverter:]' +\
                  '-> DivergentRowsInCategoryMatrixStat'
        regSpec, binSpec = '*', '*'

        #print 'skipping preproc!!'
        #ExternalTrackManager.preProcess(tcGeneRegsExternalTN[-1], tcGeneRegsExternalTN, 'targetcontrol.bedgraph', genome)
        #ExternalTrackManager.preProcess(targetGeneRegsTempFn, targetGeneRegsExternalTN, 'bed', genome)

        GalaxyInterface.runManual([trackName1, trackName2],
                                  analysisDef,
                                  regSpec,
                                  binSpec,
                                  genome,
                                  printResults=True,
                                  printHtmlWarningMsgs=False)
Example #38
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()
Example #39
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