示例#1
0
    def createRSquareGraph(cls, ldGraphTrackName, r2_threshold):
        """
        Creates a dictionary of all pairs in a linked point track.
        Variants in LD must have rsquare >= the rsquare threshold passed to the function.

        :param ldGraphTrackName: linked point track, as chosen in tool (choices.ldtrack)
        :param r2_threshold: Lower limit of square value
        :return: Dictionary of all ld-pairs with sorted key = (rsid1, rsid2), value = rSquare
        """
        from quick.application.ExternalTrackManager import ExternalTrackManager
        from gold.origdata.GtrackGenomeElementSource import GtrackGenomeElementSource

        fileName = ExternalTrackManager.extractFnFromGalaxyTN(ldGraphTrackName)
        suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(ldGraphTrackName)
        gtSource = GtrackGenomeElementSource(fileName, suffix=suffix)

        r2graph = {}

        for ge in gtSource:
            rsid = ge.id
            edges = ge.edges
            weights = ge.weights

            for i in range(0, len(edges)):
                ldRsid = edges[i]
                r2 = weights[i]

                if r2 >= float(r2_threshold):
                    cls.addEdge(r2graph, rsid, ldRsid, r2)

        return r2graph
示例#2
0
 def _collectTracks(self):
     tracks = [self._track, self._track2]
     if 'trackNameIntensity' in self._kwArgs:
         assert not 'extraTracks' in self._kwArgs
         self._kwArgs['extraTracks'] = self._kwArgs['trackNameIntensity']
         
     if 'extraTracks' in self._kwArgs:
         from gold.track.Track import PlainTrack
         import re
         from config.Config import MULTIPLE_EXTRA_TRACKS_SEPARATOR
         extraTracks = self._kwArgs['extraTracks']
         if type(extraTracks) == str:
             extraTracks = extraTracks.split(MULTIPLE_EXTRA_TRACKS_SEPARATOR)
         for extraT in extraTracks:
             if type(extraT) == str:
                 #extraT = extraT.split('|')
                 #extraT = re.split('\^|\|',extraT)                    
                 extraT = convertTNstrToTNListFormat(extraT)
             if type(extraT) == list:
                 #print 'TEMP1: ', extraT
                 from urllib import unquote
                 extraT = [unquote(part) for part in extraT]
                 from quick.application.ExternalTrackManager import ExternalTrackManager
                 if ExternalTrackManager.isGalaxyTrack(extraT):
                     extraT = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(self.getGenome(), extraT)
                 extraT = PlainTrack(extraT)
                 tracks.append(extraT)
             
     return tracks
    def execute(choices, galaxyFn=None, username=''):
        '''Is called when execute-button is pushed by web-user.
        Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr
        If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
        choices is a list of selections made by web-user in each options box.
        '''
        resultLines = []

        outputFile=open(galaxyFn,"w")
        fnSource = ExternalTrackManager.extractFnFromGalaxyTN(choices[2].split(':'))
        fnDB = ExternalTrackManager.extractFnFromGalaxyTN(choices[3].split(':'))
        intersectingFactor = 'id' if choices[4] == 'Element id' else 'position'
        
        colsToAdd = []
        colsToAddDict = choices[5]
        for key in colsToAddDict:
            if colsToAddDict[key]:
                colsToAdd.append(key)

        genome = choices[1] if choices[0] == 'Yes' else None
        
        try:
            complementGtrackFileAndWriteToFile(fnSource, fnDB, galaxyFn, intersectingFactor, colsToAdd, genome)
        except Exception, e:
            import sys
            print >> sys.stderr, e
 def execute(choices, galaxyFn=None, username=''):
     '''Is called when execute-button is pushed by web-user.
     Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr
     If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
     choices is a list of selections made by web-user in each options box.
     '''
     outputFile=open(galaxyFn,"w")
     fnSource = ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(':'))
     inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(':')), 'r')
     
     if choices[2] == 'Filter on exact values':    
         if choices[3]!='Select column..':
             column = int(choices[3][7:])
             filterSet = set([key for key,val in choices[4].items() if val])
             for i in inputFile:
                 if i.split('\t')[column] in filterSet:
                     print>>outputFile, i
             
     else:
         for i in inputFile:
             temptab = i.split('\t')
             for index in range(len(temptab)):
                 locals()['c'+str(index)] = temptab[index]
             if eval(choices[5]):
                 print>>outputFile, i
                 
     inputFile.close()
     outputFile.close()    
    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.
        '''
        # get population format
        if choices.format == 'File':
            pop = [];
            popfile = choices.population;
            inFn = ExternalTrackManager.extractFnFromGalaxyTN(popfile.split(":"));
            infile = open(inFn);
            for line in infile:
                pop.append(line.rstrip('\n'));
        else:
            pop = map(str.strip,choices.population.split(","));

        # read in file
        inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices.vcf.split(":"));
        data = open(inFn).read();

        # convert and write to GTrack file
        outfile = open(galaxyFn, 'w');
        outfile.write(addHeader(choices.genome));
        outfile.write(convertToGtrackFile(data, pop, choices.genome));
        outfile.close();
示例#6
0
 def execute(cls, choices, galaxyFn=None, username=''):
     '''
     Is called when execute-button is pushed by web-user. Should print
     output as HTML to standard out, which will be directed to a results page
     in Galaxy history. If getOutputFormat is anything else than HTML, the
     output should be written to the file with path galaxyFn. If needed,
     StaticFile can be used to get a path where additional files can be put
     (e.g. generated image files). choices is a list of selections made by
     web-user in each options box.
     '''
     
     try:
         historyInputTN = choices[0].split(':') #from history
         historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( historyInputTN) #same as galaxyFn in execute of create benchmark..
         randomStatic = RunSpecificPickleFile(historyGalaxyFn) #finds path to static file created for a previous history element, and directs to a pickle file
         myInfo = randomStatic.loadPickledObject()
     except:
         return None
     
     galaxyTN = myInfo[3].split(':')
     myFileName = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN)
     genome = myInfo[0]
     
     gtrackSource = GtrackGenomeElementSource(myFileName, genome)
     regionList = []
     
     for obj in gtrackSource:
         regionList.append(GenomeRegion(obj.genome, obj.chr, obj.start, obj.end))
     
     extractor = TrackExtractor()
             
     fn = extractor.extract(GenomeInfo.getSequenceTrackName(genome), regionList, galaxyFn, 'fasta')
示例#7
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 = 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 getOptionsBox6(prevChoices):
     if prevChoices[3]:
         extraDbColumnsDict = OrderedDict()
         fnSource = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[2].split(':'))
         fnDB = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[3].split(':'))
         
         gtrackDB = GtrackGenomeElementSource(fnDB)
         gtrackSource = GtrackGenomeElementSource(fnSource)
         
         extraDbColumns = [v for v in gtrackDB.getColumns() if not v in gtrackSource.getColumns()] #list(set(gtrackDBColumnSpec) - set(gtrackSourceColumnSpec))
         for column in extraDbColumns:
             extraDbColumnsDict[column] = False
         return extraDbColumnsDict
    def createLinkedPointTrack(cls, rsids, isUndirected, trackFn, r2):
        from quick.webtools.clustering.CreateLDTrack import CreateLDTrack
        from quick.application.ExternalTrackManager import ExternalTrackManager

        # Create file for GTrack
        galaxyTN = ExternalTrackManager.constructGalaxyTnFromSuitedFn(trackFn, fileEnding='gtrack', name='ld_graph')
        fn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN)
        f = open(fn, 'w')

        # Get LD information and create linked point track
        ldDict = CreateLDTrack.getLDDict(r2)
        expansionDict = CreateLDTrack.getExpansionDict(rsids, ldDict)
        f.write(CreateLDTrack.formatLinkedPointTrack(expansionDict, isUndirected))
    def execute(choices, galaxyFn=None, username=''):
        '''Is called when execute-button is pushed by web-user.
        Should print output as HTML to standard out, which will be directed to a results page in Galaxy history.
        If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.
        If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
        choices is a list of selections made by web-user in each options box.
        '''
        from time import time
        startTime = time()
        from quick.application.ExternalTrackManager import ExternalTrackManager
        from quick.util.StaticFile import GalaxyRunSpecificFile
        import os

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

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

        #motifFn, observedFasta, randomFastaPath = '/Users/sandve/egne_dokumenter/_faglig/NullModels/DnaSeqExample/liver.pwm', 'liver.fa', 'randomFastas'
        testStatistic = choices[3]
        if testStatistic == 'Average of max score per sequence':
            scoreFunc = scoreMotifOnFastaAsAvgOfBestScores
        elif testStatistic == 'Sum of scores across all positions of all sequences':
            scoreFunc = scoreMotifOnFastaAsSumOfAllScores
        elif testStatistic == 'Score of Frith et al. (2004)':
            scoreFunc = lr4
        elif testStatistic == 'Product of max per sequence':
            scoreFunc = scoreMotifOnFastaAsProductOfBestScores
        else:
            raise
        
        pvals = mcPvalFromMotifAndFastas(motifFn, observedFasta, randomFastaPath, scoreFunc)
        print 'Pvals for motifs (%s) against observed (%s) vs random (%s - %s) sequences.' % (motifFn, observedFasta, randomName, randomFastaPath)
        for motif,pval in sorted(pvals.items()):
            print motif+'\t'+('%.4f'%pval)
            
        from quick.util.StaticFile import GalaxyRunSpecificFile
        from gold.application.RSetup import r, robjects
        histStaticFile = GalaxyRunSpecificFile(['pvalHist.png'],galaxyFn)
        #histStaticFile.openRFigure()
        histStaticFile.plotRHist(pvals.values(), [x/40.0 for x in range(41)], 'Histogram of p-values', xlim=robjects.FloatVector([0.0, 1.0]))
        #r.hist(robjects.FloatVector(pvals.values()), breaks=robjects.FloatVector([x/40.0 for x in range(41)]), xlim=robjects.FloatVector([0.0, 1.0]), main='Histogram of p-values' )
        #histStaticFile.closeRFigure()
        print histStaticFile.getLink('Histogram')
        print 'Time (s):', time()-startTime
 def getRedirectURL(choices):
     
     genome = choices[0]
     track1file = ExternalTrackManager.createSelectValueFromGalaxyTN(choices[1].split(':'))
     track2file = ExternalTrackManager.createSelectValueFromGalaxyTN(choices[2].split(':'))
     return createHyperBrowserURL(genome, trackName1=['galaxy'], trackName2=['galaxy'], \
                                  track1file=track1file, track2file=track2file, \
                                  analysis='Category pairs differentially co-located?', \
                                  configDict={'Method of counting points': 'Only 1 count per bin (binary)',\
                                              'Normalize counts': 'Differentially in both directions' if \
                                              choices[3] == 'Both rows and columns (focusing on column differences)'\
                                              else 'Differentially for points only',\
                                              'P-value threshold for significance': '0.01'},\
                                  method='__chrs__')
 def execute(choices, galaxyFn=None, username=''):
     '''Is called when execute-button is pushed by web-user.
     Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr
     If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
     choices is a list of selections made by web-user in each options box.
     '''
     genome = choices[1] if choices[0] == 'Yes' else None
     suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(choices[2].split(':'))
     inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices[2].split(':'))
     
     try:
         standardizeGtrackFileAndWriteToFile(inFn, galaxyFn, genome, suffix=suffix)
     except Exception, e:
         import sys
         print >> sys.stderr, e
    def execute(choices, galaxyFn=None, username=''):
        '''Is called when execute-button is pushed by web-user.
        Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr
        If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
        choices is a list of selections made by web-user in each options box.
        '''
        fnSource = ExternalTrackManager.extractFnFromGalaxyTN(choices[2].split(':'))
        
        core = HtmlCore()
        core.begin()
        
        valid = False
        try:
            core.header('Validating GTrack headers')
            core.styleInfoBegin(styleClass='debug')

            print str(core)
            core = HtmlCore()

            gtrackSource = GtrackGenomeElementSource(fnSource, choices[1] if choices[0]=='Yes' else None, printWarnings=True)
            
            core.append('Done')
            core.styleInfoEnd()
            core.header('Validating complete GTrack file')
            core.styleInfoBegin(styleClass='debug')
            
            print str(core)
            core = HtmlCore()
            
            try:
                for ge in gtrackSource:
                    pass
            except Exception, e:
                pass
            else:    
    def execute(choices, galaxyFn=None, username=''):
        #'Genome:','Source of seed TF:','Seed TF: ','Flank size: ', 'Source of potentially cooperative TFs:'
        #genome
        genome = choices[0]
        seedSource = choices[1]
        seedTfTnInput = choices[2]
        if seedSource == 'TFBS from history':
            seedFn = ExternalTrackManager.extractFnFromGalaxyTN(seedTfTnInput.split(':'))
        else:
            tfTrackNameMappings = TfInfo.getTfTrackNameMappings(genome)
            seedTfTn = tfTrackNameMappings[seedSource] + [seedTfTnInput]
            #tfTrackName = tfTrackNameMappings[tfSource] + [selectedTF]
            seedFns = getOrigFns(genome, seedTfTn, '')
            assert len(seedFns) == 1
            seedFn = seedFns[0]
            
        flankSize = choices[3]
        flankSize = int(flankSize) if flankSize != '' else 0
        cooperativeTfSource = choices[4]
        #flankSize = int(choices[4])
        #TFsFromGenes.findOverrepresentedTFsFromGeneSet('hg18', 'UCSC tfbs conserved', ['ENSGflankSizeflankSizeflankSizeflankSizeflankSize2flankSize8234','ENSGflankSizeflankSizeflankSizeflankSizeflankSize199674'],flankSize, flankSize, galaxyFn)
        #TFsFromGenes.findOverrepresentedTFsFromGeneSet('hg18', tfSource, choices[5].split(','),flankSize, flankSize, 'Ensembl', galaxyFn)
        
        #TFsFromRegions.findOverrepresentedTFsFromGeneSet(genome, tfSource, ensembleGeneIdList,upFlankSize, downFlankSize, geneSource, galaxyFn)
        
        #TFsFromGenes.findTFsTargetingGenes('hg18', tfSource, choices[5].split(','),flankSize, flankSize, 'Ensembl', galaxyFn)

        TFsFromRegions.findTFsOccurringInRegions(genome, cooperativeTfSource, seedFn, flankSize, flankSize, galaxyFn)
 def _getHeaders(prevChoices):
     numCols = TabularToGtrackTool._getFileContentsInfo(prevChoices).numCols
     if prevChoices.columnSelection == 'Select individual columns':
         header = []
         for i in xrange(numCols):
             if hasattr(prevChoices, 'column%s' % i):
                 colHeader = getattr(prevChoices, 'column%s' % i)
                 if colHeader is None or colHeader == '-- ignore --':
                     header.append('')
                 elif colHeader == '-- custom --':
                     header.append(getattr(prevChoices, 'customColumn%s' % i).strip())
                 else:
                     header.append(colHeader)
             else:
                 header.append('')
         return header
     else:
         genome = prevChoices.genome if prevChoices.selectGenome == 'Yes' else None
         inFn = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices.colSpecFile.split(':'))
         try:
             geSource = GtrackGenomeElementSource(inFn, genome=genome)
             geSource.parseFirstDataLine()
             return geSource.getColumns()[:numCols]
         except Exception, e:
             return []
    def getOptionsBoxFileContentsInfo(prevChoices):
        if prevChoices.history or prevChoices.input:
            if prevChoices.history:
                inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(prevChoices.history.split(':')), 'r')
            else:
                inputFile = StringIO(prevChoices.input)
            
            for i in xrange(TabularToGtrackTool._getNumSkipLines(prevChoices)):
                inputFile.readline()
            
            table = []
            splitChar = TabularToGtrackTool._getSplitChar(prevChoices)
            numCols = None
            error = None
            for i,line in enumerate(inputFile):
                row = [x.strip() for x in line.strip().split(splitChar)]
                if numCols == None:
                    numCols = len(row)
                elif numCols != len(row):
                    numCols = max(numCols, len(row))
#                    error = 'Error: the number of columns varies over the rows of the tabular file.'
                    
                table.append(row)
                if i == TabularToGtrackTool.NUM_ROWS_IN_TABLE:
                    break
            
            numCols = max(len(row) for row in table) if len(table) > 0 else 0
            
            if error is None:
                if numCols > TabularToGtrackTool.NUM_COLUMN_FUNCTIONS:
                    error = 'Error: the tabular file has more columns than is allowed by the tool (%s > %s).' % (numCols, TabularToGtrackTool.NUM_COLUMN_FUNCTIONS)
                
            return ('__hidden__', FileContentsInfo(table=table, numCols=numCols, error=error))
 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.
     '''
     
     # Retrieve the pickled benchmark object from history
     try:
         historyInputTN = choices[0].split(':')
         #same as galaxyFn in execute of create benchmark..
         historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN(historyInputTN) 
         #finds path to static file created for a previous history element, and directs to a pickle file
         randomStatic = RunSpecificPickleFile(historyGalaxyFn) 
         benchmarkSpecification = randomStatic.loadPickledObject()
     except:
         return None
     
     genome = benchmarkSpecification[0]
     benchmarkLevel = benchmarkSpecification[2]
     regionTrackName = benchmarkSpecification[3]
     benchmarkUtil = BenchmarkUtil(galaxyFn, genome)
     
     if benchmarkLevel == 'Base pair probability level':
         return benchmarkUtil.retrieveFeatureTrack(genome, galaxyFn, regionTrackName, benchmarkSpecification[5])
     elif type(regionTrackName) is str: # If string, we're dealing with a single track so just retrieve it
         return benchmarkUtil.retrieveTrack(regionTrackName, galaxyFn)
     elif type(regionTrackName) is list: # If list, we're dealing with a benchmark suite which will have to be zipped
         print benchmarkUtil.retrieveBenchmarkSuiteAsZipFile(regionTrackName)
     else:
         raise Exception('Invalid benchmark')
 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 needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
     choices is a list of selections made by web-user in each options box.
     '''
     print 'Executing...'
     
     tempinfofile=ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(":"))
     abbrv=GenomeImporter.getGenomeAbbrv(tempinfofile)
     gi = GenomeInfo(abbrv)
     chrNamesInFasta=gi.sourceChrNames
     
     chromNamesDict={}
     chrDict = InstallGenomeTool._getRenamedChrDictWithSelection(choices)
         
     for i, key in enumerate(chrDict.keys()):
         if chrDict[key]:
             chromNamesDict[chrNamesInFasta[i]]=key
     print 'All chromosomes chosen: ' + str(chromNamesDict)
         
     stdChrDict = InstallGenomeTool._getRenamedChrDictWithSelection(choices, stdChrs=True)
     stdChrs = [x for x in stdChrDict if stdChrDict[x]]
     print 'Standard chromosomes chosen: ' + ", ".join(stdChrs)
     
     GenomeImporter.createGenome(abbrv, gi.fullName, chromNamesDict, stdChrs, username=username)
     
     gi.installedBy = username
     gi.timeOfInstallation = datetime.now()
     gi.store()
 def _getTempChromosomeNames(galaxyTn):
     if isinstance(galaxyTn, str):
         galaxyTn = galaxyTn.split(":")
     tempinfofile=ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn)
     abbrv=GenomeImporter.getGenomeAbbrv(tempinfofile)
     
     return os.linesep.join(GenomeInfo(abbrv).sourceChrNames)
 def execute(choices, galaxyFn=None, username=''):
     '''Is called when execute-button is pushed by web-user.
     Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr
     If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files).
     choices is a list of selections made by web-user in each options box.
     '''
     genome = choices.genome if choices.selectGenome == 'Yes' else None
     onlyNonDefault = choices.allHeaders == 'Only non-default headers'
     
     
     try:
         if choices.history:
             inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices.history.split(':'))
             expandHeadersOfGtrackFileAndWriteToFile(inFn, galaxyFn, genome, onlyNonDefault)
         else:
             if choices.whitespace == 'Keep whitespace exact':
                 input = choices.input
             else:
                 input = ''
                 for line in choices.input.split(os.linesep):
                     line = line.strip()
                     if (line.startswith('###') and len(line) > 3 and line[3] != '#') \
                         or not line.startswith('#'):
                         line = line.replace(' ', '\t')
                     else:
                         line = line.replace('\t', ' ')
                     input += line + os.linesep
         
             composer = expandHeadersOfGtrackFileAndReturnComposer('', genome, strToUseInsteadOfFn=input)
             composer.composeToFile(galaxyFn, onlyNonDefault=onlyNonDefault)
     except Exception, e:
         import sys
         print >> sys.stderr, e
    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 getRegsAndBinsSpec(choices): 
     '''
     Returns teh regSpec and binSpec for the choices made on the gui.
     '''
     regsMapper = {'Chromosome arms':'__chrArms__','Chromosomes':'__chrs__','Cytobands':'__chrBands__','Genes(Ensembl)':'__genes__','ENCODE Pilot regions':'__encode__'}
     if choices.CompareIn == 'Custom specification':
         regSpec = choices.CustomRegion
         binSpec = choices.BinSize
     elif regsMapper.get(choices.CompareIn):
         regSpec = regsMapper[choices.CompareIn]
         binSpec = choices.Bins
     else:
         histItem = choices.HistoryBins.split(':')
         binSpec = ExternalTrackManager.extractFnFromGalaxyTN(histItem)
         regSpec = ExternalTrackManager.extractFileSuffixFromGalaxyTN(histItem)
     
     return regSpec, binSpec
示例#23
0
 def __new__(cls, trackName):
     if len(trackName) == 0 or trackName is None:
         return None
     else:
         if ExternalTrackManager.isVirtualTrack(trackName):
             return VirtualMinimalPlainTrack.__new__(VirtualMinimalPlainTrack)
         else:
             return object.__new__(cls)
示例#24
0
 def retrieveFeatureTrack(self, genome, galaxyFn, regionTrackName, featureTrack):
     
     regionTrackName = regionTrackName.split(':')
     regionFileName = ExternalTrackManager.extractFnFromGalaxyTN(regionTrackName)
     
     bins = GalaxyInterface._getUserBinSource('gtrack', regionFileName, genome, featureTrack.split(':'))
     
     return self._extractor.extract(featureTrack.split(':'), bins, galaxyFn, 'gtrack')
示例#25
0
    def _getRawTrackView(self, region, borderHandling, allowOverlaps):
        assert region.start == 0 and region.end == 1
        
        from collections import OrderedDict
        from gold.track.CommonMemmapFunctions import findEmptyVal
        from gold.track.TrackView import TrackView
        import numpy as np
        
        geSource = ExternalTrackManager.getGESourceFromGalaxyOrVirtualTN(self.trackName, region.genome)
        prefixList = geSource.getPrefixList()
        valDataType = geSource.getValDataType()
        valDim = geSource.getValDim()
        weightDataType = geSource.getEdgeWeightDataType()
        weightDim = geSource.getEdgeWeightDim()

        startList, endList, valList, strandList, idList, edgesList, weightsList = [None]*7
        extraLists=OrderedDict()
        
        tf = TrackFormat.createInstanceFromPrefixList(prefixList, valDataType, valDim, \
                                                      weightDataType, weightDim)
        if allowOverlaps and (tf.isDense() or geSource.hasNoOverlappingElements()):
            raise IncompatibleTracksError(prettyPrintTrackName(self.trackName) + ' with format: '\
                                          + str(tf) + ' does not satisfy ' + str(self._trackFormatReq))
        
        denseAndInterval = tf.isDense() and tf.isInterval()
        numEls = 2 if denseAndInterval else 1
        
        if valDataType == 'S':
            valDataType = 'S2'
        if weightDataType == 'S':
            weightDataType = 'S2'
        
        for prefix in prefixList:
            if prefix == 'start':
                startList = np.array([-1], dtype='int32')
            elif prefix == 'end':
                if denseAndInterval:
                    endList = np.array([0, 1], dtype='int32')
                else:
                    endList = np.array([0], dtype='int32')
            elif prefix == 'val':
                valList = np.array([findEmptyVal(valDataType)] * valDim * numEls, \
                                   dtype=valDataType).reshape((numEls, valDim) if valDim > 1 else numEls)
            elif prefix == 'strand':
                strandList = np.array([1] * numEls, dtype='int8')
            elif prefix == 'id':
                idList = np.array([''] * numEls, dtype='S1')
            elif prefix == 'edges':
                edgesList = np.array([['']] * numEls, dtype='S1')
            elif prefix == 'weights':
                weightsList = np.array([[[findEmptyVal(weightDataType)]]] * weightDim * numEls, \
                                       dtype=weightDataType).reshape((numEls, 1, weightDim) if weightDim > 1 else (numEls, 1))
            else:
                extraLists[prefix] = np.array([''] * numEls, dtype='S1')
        
        return TrackView(region, startList, endList, valList, strandList, idList, edgesList, weightsList, borderHandling, allowOverlaps, extraLists)
 def getOptionsBox2(prevChoices):
     try:
         historyInputTN = prevChoices[0].split(':') #from history
         historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( historyInputTN) #same as galaxyFn in execute of create benchmark..
         randomStatic = RunSpecificPickleFile(historyGalaxyFn) #finds path to static file created for a previous history element, and directs to a pickle file
         myInfo = randomStatic.loadPickledObject()
     
         return [myInfo[0]]
     except:
         return None
 def getOptionsBox3(prevChoices): 
     '''Returns a list of options to be displayed in the second options box, which will be displayed after a selection is made in the first box.
     prevChoices is a list of selections made by the web-user in the previous input boxes (that is, list containing only one element for this case)        
     '''
     if prevChoices[-2]!=None and prevChoices[0]:
         try:
             fnSource = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[0].split(':'))
             return (open(fnSource,'r').readlines()[int(prevChoices[-2])], 2, True)
         except:
             pass
    def _getResultsLists(histChoices):
        if len([x for x in histChoices.values() if x!=None])==0:
            return [],[]
        #print 'histChoices',histChoices
        #return []

        galaxyTNs = [x.split(':') for x in histChoices.values() if x!=None]
                
        galaxyFns = [ExternalTrackManager.extractFnFromGalaxyTN(tn) for tn in galaxyTNs]
        historyNames= [ExternalTrackManager.extractNameFromHistoryTN(tn) for tn in galaxyTNs]
        staticFiles = [GalaxyRunSpecificFile(['results.pickle'], gfn) for gfn in galaxyFns]
        fileSpecificFile = [GalaxyRunSpecificFile([], gfn) for gfn in galaxyFns]
        print 'Using pickles: ', [x.getDiskPath() for x in staticFiles]
        
        paths = [x.getDiskPath()+'/0' for x in fileSpecificFile]
        pngList = [[v for v in x[2] if v.find('.png')>0] for x in os.walk(paths[0])]
        
        resultsLists = [load(sf.getFile('r')) for sf in staticFiles]
        return resultsLists, historyNames
    def execute(choices, galaxyFn=None, username=''):
        historyInputTN = choices[0].split(':') #from history
        historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( historyInputTN) #same as galaxyFn in execute of create benchmark..
        randomStatic = RunSpecificPickleFile(historyGalaxyFn) #finds path to static file created for a previous history element, and directs to a pickle file
        myInfo = randomStatic.loadPickledObject()

        if myInfo[1] == choices[2]:
            print "Your answer was correct!"
        else:
            print "You answered %s but the right answer was %s" % (myInfo[1], choices[2])
 def _getBoundingRegionShelve(self, trackName):
     if trackName in [None, []] or ExternalTrackManager.isVirtualTrack(trackName):
         brShelve = None
     else:
         brShelve = BoundingRegionShelve(self.genome, trackName, allowOverlaps=False)
         if not brShelve.fileExists():
             raise BoundingRegionsNotAvailableError('Bounding regions not available for track: ' + \
                 prettyPrintTrackName(trackName))
     
     return brShelve
示例#31
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]
     trackList =  choices[2].values()
     print trackList
     
     from quick.application.ExternalTrackManager import ExternalTrackManager
     for track in trackList:
         ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, track.split(':'))
     print 'Executing...'
示例#32
0
    def execute(cls, choices, galaxyFn=None, username=''):
        from gold.util.RandomUtil import random

        outputFile = open(galaxyFn, 'w')
        genome = choices[0]
        histItem = choices[2]
        trackItem = choices[3]
        chromRegsPath = GenomeInfo.getChrRegsFn(genome)

        chrSizeDict = dict([(chrom, GenomeInfo.getChrLen(genome, chrom))
                            for chrom in GenomeInfo.getChrList(genome)])
        geSource = headLinesStr = None
        if choices[1] == 'history':

            trackType = choices[2].split(':')[1]
            username = ''.join(
                [chr(random.randint(97, 122)) for i in range(6)])
            tempFn = createCollectedPath(
                genome, [],
                username + '_'.join([str(v) for v in time.localtime()[:6]]) +
                '.' + trackType)
            fnSource = ExternalTrackManager.extractFnFromGalaxyTN(
                choices[2].split(':'))
            open(tempFn, 'w').write(open(fnSource, 'r').read())

            if trackType in ['valued.bed', 'category.bed', 'bed']:
                geSource = GenomeElementSorter(
                    BedGenomeElementSource(tempFn, genome=genome)).__iter__()

            #elif trackType == 'gtrack':
            #    geSource = GenomeElementSorter(GtrackGenomeElementSource(tempFn, genome=genome)).__iter__()
            #    headLinesStr = geSource.getHeaderLines().replace('##','\n##')

            cls.WriteExpandedElementsToFile(geSource,
                                            chrSizeDict,
                                            outputFile,
                                            headLinesStr,
                                            writeHeaderFlag=True)
            os.remove(tempFn)

        else:
            writeHeaderFlag = True
            for chrom in GenomeInfo.getChrList(genome):
                gRegion = GenomeRegion(genome, chrom, 0, chrSizeDict[chrom])
                plTrack = PlainTrack(trackItem.split(':'))
                geSource = GenomeElementTvWrapper(
                    plTrack.getTrackView(gRegion)).__iter__()
                cls.WriteExpandedElementsToFile(geSource, chrSizeDict,
                                                outputFile, headLinesStr,
                                                writeHeaderFlag)
                writeHeaderFlag = False
        outputFile.close()
示例#33
0
    def validateAndReturnErrors(choices):
        '''
        Should validate the selected input parameters. If the parameters are not
        valid, an error text explaining the problem should be returned. The GUI
        then shows this text to the user (if not empty) and greys out the
        execute button (even if the text is empty). If all parameters are valid,
        the method should return None, which enables the execute button.
        '''

        if not choices.resultsFile:
            return 'Please select a results file from history'

        resultsFN = ExternalTrackManager.extractFnFromGalaxyTN(
            choices.resultsFile)
        errors = ExamResultsValidator.validateExamResultsDataFile(
            resultsFN, IndividualTaskAnalysis.REQUIRED_COLUMNS,
            IndividualTaskAnalysis.OPTIONAL_COLUMNS)
        if errors:
            if len(errors) > 5:
                errors = errors[0:5]
            return 'First 5 errors reported:<br><br>' + '<br>'.join(
                [str(x) for x in errors]).replace('\n', '<br>')

        selectedTasks = [key for key, val in choices.tasks.iteritems() if val]
        if not selectedTasks:
            return 'Please select at least one task'

        if choices.analysis == IndividualTaskAnalysis.ANALYSIS_BIN_AVG_SMOOTHED_PLOT:
            if not choices.bins:
                return 'Please specify number of bins'
            try:
                bins = int(choices.bins)
                if bins < 1:
                    return "Number of bins must be 1 or more"
            except:
                return "Bins must be an integer number"

        if choices.analysis in [
                IndividualTaskAnalysis.ANALYSIS_BIN_AVG_SMOOTHED_PLOT,
                IndividualTaskAnalysis.ANALYSIS_MOVING_AVG_SMOOTHED_PLOT
        ]:
            if not choices.spar:
                return 'Please specify the smoothing parameter'
            else:
                try:
                    spar = float(choices.spar)
                    if spar < 0 or spar > 1:
                        return 'The smoothing parameter must be between 0.0 and 1.0'
                except:
                    return 'The smoothing parameter must be a floating point number between 0.0 and 1.0'

        return None
示例#34
0
    def visitGalaxyGSuiteTrack(self, gSuiteTrack):
        self.genericVisit(gSuiteTrack)

        from quick.application.ExternalTrackManager import ExternalTrackManager
        from gold.description.TrackInfo import TrackInfo

        if gSuiteTrack.hasExtraFileName():
            baseFileName = os.path.basename(gSuiteTrack.uriWithoutSuffix)
        else:
            baseFileName = gSuiteTrack.title
        
        galaxyTN = ExternalTrackManager.constructGalaxyTnFromSuitedFn(
            gSuiteTrack.path, fileEnding=gSuiteTrack.suffix, name=baseFileName)
        trackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(
            gSuiteTrack.genome, galaxyTN, printErrors=False, printProgress=False,
            renameExistingTracksIfNeeded=False)

        trackType = TrackInfo(gSuiteTrack.genome, trackName).trackFormatName.lower()
        hbUri = HbGSuiteTrack.generateURI(trackName=trackName)

        return GSuiteTrack(hbUri, title=gSuiteTrack.title, trackType=trackType,
                           genome=gSuiteTrack.genome, attributes=gSuiteTrack.attributes,
                           comment=gSuiteTrack.comment)