def findGeneTargets(genome, regionsTn, upFlankSize, downFlankSize, galaxyFn):
        assert genome == 'hg18'
        #tfTrackNameMappings = TfInfo.getTfTrackNameMappings(genome)
        #tfTrackName = tfTrackNameMappings[tfSource] + [selectedTF]
        
        geneIntersection = GeneIntersection(genome, 'Ensembl', regionsTn, galaxyFn)
        geneIntersection.expandReferenceTrack(upFlankSize, downFlankSize)
        expansionStr = ' flanked' if not (upFlankSize == downFlankSize == 0) else ''                
        #print '<p>There are %i Ensemble genes being targets of your selected TF (%s), based on intersecting TF target positions with%s %sgene regions.</p>' % (geneIntersection.getNumberOfIntersectedBins(), selectedTF, expansionStr, 'Ensembl')
        print '<p>There are %i Ensemble genes being targets of your selected regions, based on intersecting your supplied regions with%s %sgene regions.</p>' % (geneIntersection.getNumberOfIntersectedBins(), expansionStr, 'Ensembl')
        
        idFileNamer = geneIntersection.getGeneIdStaticFileWithContent()
        print '<p>', idFileNamer.getLink('Download list'), ' of all Ensemble IDs with 1 or more hits.</p>' 

        regFileNamer = geneIntersection.getIntersectedRegionsStaticFileWithContent()
        print '<p>', regFileNamer.getLink('Download bed-file'), ' of all Ensembl gene regions with 1 or more hits.</p>' 
        
        targetBins = geneIntersection.getIntersectedReferenceBins()
        res = geneIntersection.getIntersectionResult()
        resDictKey = geneIntersection.getUniqueResDictKey()
        setOfNumOccurrences = set([res[bin][resDictKey] for bin in targetBins])

        byNumOccurrencesStaticFile = GalaxyRunSpecificFile(['genes_by_num_occurrences.html'], galaxyFn)
        f = byNumOccurrencesStaticFile.getFile()
        for numOccurrences in reversed(sorted(setOfNumOccurrences)):                    
            f.write('Gene regions having %i occurrences:<br>' % numOccurrences + '<br>' + os.linesep)
            f.write(', '.join([ '<a href=http://www.ensembl.org/Homo_sapiens/Gene/Summary?g='+str(bin.val).split('|')[0]+'>'+str(bin.val).split('|')[0]+'</a>' for bin in targetBins if res[bin][resDictKey]==numOccurrences]) + '<br><br>' + os.linesep)
        f.close()
        
        print '</p>Inspect list of all intersected genes (by ID), ', byNumOccurrencesStaticFile.getLink('ordered by number of occurrences') + ' inside, and with links to gene descriptions.<br>'
    def convertToGTrack(self, filePath, fileFormat, galaxyFn, fastaFilePath=None, normalizeValues=False):
        
        predictionFile = open(filePath, 'r')
        
        out = GalaxyRunSpecificFile(['%smodified.gtrack' % filePath.split('/')[-1]], galaxyFn)
        gtrackFile = out.getFile('w')

        if fileFormat == 'weeder':
            self._convertFromWeederToGTrack(predictionFile, gtrackFile)
        elif fileFormat == 'meme':
            self._convertFromMemeToGTrack(predictionFile, gtrackFile)
        elif fileFormat == 'glimmer':
            self._convertFromGlimmerToGTrack(predictionFile, gtrackFile)
        elif fileFormat == 'prodigal':
            self._convertFromProdigalToGTrack(predictionFile, gtrackFile)
        elif fileFormat == 'genemark':
            self._convertFromGenemarkToGTrack(predictionFile, gtrackFile)
        elif fileFormat == 'blasthit':
            self._convertFromBlastToGTrack(predictionFile, gtrackFile)
        elif fileFormat == 'ymf':
            fastaFile = open(fastaFilePath, 'r')
            self._convertFromYMFToGTrack(fastaFile, predictionFile, gtrackFile)
        elif fileFormat == 'gtrack' and normalizeValues == True:
            self._normalizeGTrackValues(filePath, gtrackFile)
        return out.getDiskPath(True)
 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.
     '''
     if choices[2]=='Transfac TF ids':
         mappingFn = 'pwm2TFids.shelf'
         mapping = safeshelve.open(Tool1.MAPPING_SHELVES_PATH + os.sep + mappingFn )
     elif choices[2]== 'Transfac TF readable names':
         mappingFn = 'pwm2TFnamesNew.shelf'
         mapping = safeshelve.open(Tool1.MAPPING_SHELVES_PATH + os.sep + mappingFn )
     elif choices[2]== 'HGNC gene symbols':
         mappingFn = 'PWM_to_HGNC.txt'
         mapping = dict([line.strip().split() for line in open(Tool1.MAPPING_SHELVES_PATH + os.sep + mappingFn).readlines()])
     else:
         raise Exception(choices[2])
         
     if galaxyFn==None:
         for key in sorted(mapping.keys()):
             print key + ':' + ','.join(mapping[key]) + os.linesep,
     else:
         mappingStaticFile = GalaxyRunSpecificFile(['mapping.txt'], galaxyFn)
         f = mappingStaticFile.getFile()
         for key in sorted(mapping.keys()):
             if type(mapping[key]) in (list,tuple):
                 mapping[key] = ','.join(mapping[key])
             f.write( key + ':' + mapping[key] + os.linesep )
         f.close()
         print mappingStaticFile.getLink('View/download mapping')
 def storePickledResults(self):
     try:
         from cPickle import dump
         pickleStaticFile = GalaxyRunSpecificFile(['results.pickle'],self._galaxyFn)
         #print 'TEMP1: PATH: ',pickleStaticFile.getDiskPath(True)
         from copy import copy
         pickleList = [copy(res) for res in self._resultsList]
         for res in pickleList:
             res._analysis=None
         dump(pickleList, pickleStaticFile.getFile())
         #dump(self._resultsList, pickleStaticFile.getFile())
     except Exception, e:
         logException(e, message='Not able to pickle results object')
Exemple #5
0
    def singleSimulation(self, numH0, numH1, replicateIndex, verbose=False):
        tests = MultipleTestCollection(numH0, numH1, self._maxNumSamples, self._h, self._fdrThreshold,self._a,self._b)
        tests.addSamples(self.NUM_SAMPLES_INITIALLY)
        while not tests.allTestsAreDetermined():            
            tests.addSamples(self.NUM_SAMPLES_PER_CHUNK)
            #if verbose:
                #print tests.getTotalNumSamples()
        #As sampling is now anyway over, we set fdrThreshold to a threshold used after computations are finished (i.e. affects final rejection/acception, but not stopping of samples)
        tests.setFdrThresholdAtAllCounters(self._postFdrThreshold)
        
        #print 'FINALLY, #samples: ',
        if self._galaxyFn is not None:
            if self._h is None:
                scheme = 'Basic'
            elif self._fdrThreshold is None:
                scheme = 'Sequential'
            else:
                scheme = 'McFdr'
            staticFile = GalaxyRunSpecificFile([scheme,str(numH1),str(replicateIndex),'PandQvals.txt'], self._galaxyFn)              
            tests.writeAllPandQVals(staticFile.getFile() )                        
            linkToRaw = staticFile.getLink('Raw p and q-vals') + ' under %s scheme with %i true H1, (replication %i)' % (scheme, numH1, replicateIndex)
            
            figStaticFile = GalaxyRunSpecificFile([scheme,str(numH1),str(replicateIndex),'PandQvals.png'], self._galaxyFn)
            figStaticFile.openRFigure()
            tests.makeAllPandQValsFigure()
            figStaticFile.closeRFigure()
            linkToFig = figStaticFile.getLink(' (p/q-figure) ') + '<br>'

            figNumSamplesStaticFile = GalaxyRunSpecificFile([scheme,str(numH1),str(replicateIndex),'NumSamples.png'], self._galaxyFn)
            figNumSamplesStaticFile.openRFigure()
            tests.makeNumSamplesFigure()
            figNumSamplesStaticFile.closeRFigure()
            linkToNumSamplesFig = figNumSamplesStaticFile.getLink(' (numSamples-figure) ') + '<br>'

            catalogStaticFile = GalaxyRunSpecificFile([str(numH1),'cat.html'], self._galaxyFn)
            catalogStaticFile.writeTextToFile(linkToRaw + linkToFig + linkToNumSamplesFig, mode='a')

                        
        #if verbose:
            #print sorted(tests.getFdrVals())
            #print 'NumS ign Below 0.2: ', sum([1 if t<0.2 else 0 for t in tests.getFdrVals()])
        #return tests.getTotalNumSamples(), tests.getTotalNumRejected()
        return tests.getTotalNumSamples(), tests.getTotalNumRejected(), tests.getClassificationSummaries()