def findGeneTargets(genome, regionsTn, upFlankSize, downFlankSize, galaxyFn):
        assert genome in ['hg18','hg19','mm9']
        #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 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 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')
예제 #3
0
 def storePickledResults(self):
     try:
         from cPickle import dump
         pickleStaticFile = GalaxyRunSpecificFile(['results.pickle'],
                                                  self._galaxyFn)
         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')
예제 #4
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()
    def execute_batch(cls, choices, galaxyFn=None, username=''):
        print GalaxyInterface.getHtmlBeginForRuns(galaxyFn)
        html = HtmlCore()
        html.header('Batch run results')

        refSnps = cls.get_ref_snp(choices)
        #print refSnps

        batchMal = "$Tool[hb_variant_melting_profiles](" + '|'.join(
            ["'%s'"] * len(choices)) + ")"
        cmdList = []
        for rs in refSnps:
            #if len(rs[4]) > 1:
            #    rs = list(rs)
            #    rs[4] = list(rs[4])[0]
            #    rs = tuple(rs)
            fakeChoices = (choices.genome, 'Single',
                           '__batch__') + rs + choices[8:]
            #print rs
            cmdList.append(batchMal % fakeChoices)

        #print cmdList
        GalaxyInterface.runBatchLines(cmdList,
                                      galaxyFn,
                                      username=username,
                                      printResults=False,
                                      printProgress=True)
        #print HtmlCore().styleInfoEnd()

        results_tsv = GalaxyRunSpecificFile(['results.tsv'], galaxyFn)
        results = results_tsv.getFile()
        dir = os.path.dirname(results_tsv.getDiskPath())
        for i in range(0, len(cmdList)):
            header = True
            ri = 0
            for resultline in open(os.path.join(dir, str(i), 'results.tsv')):
                if header:
                    header = False
                    if i == 0:
                        headertxt = '#run\t' + resultline
                        results.write(headertxt)
                        html.tableHeader(headertxt.split('\t'))
                else:
                    results.write(str(i) + '\t' + resultline)
                    if resultline.count('?') == 0:
                        link = '<a href="%d/html/chart-%d.html">%d (graph)</a>' % (
                            i, ri, i)
                    else:
                        link = str(i)
                    html.tableLine([link] + resultline.split('\t'))
                    ri += 1

        results.close()
        html.tableFooter()

        # XXX: temp fix for HB/stable bug
        if URL_PREFIX == '/hb':
            print '</div>'

        print '<p><b>' + results_tsv.getLink('Download results') + '</b></p>'
        print html
        print GalaxyInterface.getHtmlEndForRuns()