Exemplo n.º 1
0
 def _appendBoundingRegionTuple(self):
     if self._genomeElement.chr is not None:
         brRegion = GenomeRegion(self._genome, self._genomeElement.chr, 0,
                                 self._elCount)
         self._boundingRegionTuples.append(
             BoundingRegionTuple(brRegion, self._elCount))
 def _getSequence(self):
     seqTv = fastaTrack.getTrackView(
         GenomeRegion(genome, self.chr, self.start, self.end))
     self.sequence = np.array(seqTv.valsAsNumpyArray())
Exemplo n.º 3
0
def _getRegion(chr, start, end):
    return GenomeRegion('hg18', 'chr' + str(chr), start, end)
Exemplo n.º 4
0
 def getBoundingRegionTuples(self):
     return [
         BoundingRegionTuple(
             GenomeRegion(genome='TestGenome', chr='chr21', start=0, end=1),
             1)
     ]
    def storeBoundingRegions(self, boundingRegionTuples, genomeElementChrList,
                             sparse):
        assert sparse in [False, True]

        tempContents = OrderedDict()

        genomeElementChrs = set(genomeElementChrList)
        lastRegion = None
        chrStartIdxs = OrderedDict()
        chrEndIdxs = OrderedDict()
        totElCount = 0
        totBinCount = 0

        for br in boundingRegionTuples:
            if lastRegion is None or br.region.chr != lastRegion.chr:
                if br.region.chr in tempContents:
                    raise InvalidFormatError(
                        "Error: bounding region (%s) is not grouped with previous bounding regions of the same chromosome (sequence)."
                        % br.region)

                lastRegion = None
                tempContents[br.region.chr] = OrderedDict()  #sorteddict()
                if sparse:
                    chrStartIdxs[br.region.chr] = totElCount
            else:
                if br.region < lastRegion:
                    raise InvalidFormatError(
                        "Error: bounding regions in the same chromosome (sequence) are unsorted: %s > %s."
                        % (lastRegion, br.region))
                if lastRegion.overlaps(br.region):
                    raise InvalidFormatError(
                        "Error: bounding regions '%s' and '%s' overlap." %
                        (lastRegion, br.region))
                if lastRegion.end == br.region.start:
                    raise InvalidFormatError(
                        "Error: bounding regions '%s' and '%s' are adjoining (there is no gap between them)."
                        % (lastRegion, br.region))

            if len(br.region) < 1:
                raise InvalidFormatError(
                    "Error: bounding region '%s' does not have positive length."
                    % br.region)

            if not sparse and len(br.region) != br.elCount:
                raise InvalidFormatError(
                    "Error: track type representation is dense, but the length of bounding region '%s' is not equal to the element count: %s != %s"
                    % (br.region, len(br.region), br.elCount))

            startIdx, endIdx = (totElCount, totElCount +
                                br.elCount) if not sparse else (None, None)
            totElCount += br.elCount
            if sparse:
                chrEndIdxs[br.region.chr] = totElCount

            tempContents[br.region.chr][br.region.start] = BoundingRegionInfo(
                br.region.start, br.region.end, startIdx, endIdx, 0, 0)

            lastRegion = br.region

        if sparse:
            totBinCount = 0
            for chr in tempContents:
                chrLen = GenomeInfo.getChrLen(self._genome, chr)
                numBinsInChr = CompBinManager.getNumOfBins(
                    GenomeRegion(start=0, end=chrLen))
                for key in tempContents[chr].keys():
                    startBinIdx = totBinCount
                    endBinIdx = totBinCount + numBinsInChr
                    brInfo = tempContents[chr][key]

                    if chr in genomeElementChrs:
                        tempContents[chr][key] = BoundingRegionInfo(brInfo.start, brInfo.end, \
                                                                    chrStartIdxs[chr], chrEndIdxs[chr], \
                                                                    startBinIdx, endBinIdx)
                    else:
                        if chrEndIdxs[chr] - chrStartIdxs[chr] > 0:
                            raise InvalidFormatError(
                                "Error: bounding region '%s' has incorrect element count: %s > 0"
                                % (GenomeRegion(chr=chr,
                                                start=brInfo.start,
                                                end=brInfo.end),
                                   chrEndIdxs[chr] - chrStartIdxs[chr]))
                        tempContents[chr][key] = BoundingRegionInfo(
                            brInfo.start, brInfo.end, 0, 0, 0, 0)

                if chr in genomeElementChrs:
                    totBinCount += numBinsInChr

        if len(genomeElementChrs - set(tempContents.keys())) > 0:
            raise InvalidFormatError(
                'Error: some chromosomes (sequences) contains data, but has no bounding regions: %s'
                % ', '.join(genomeElementChrs - set(tempContents.keys())))

        ensurePathExists(self._fn)

        for chr in tempContents:
            brInfoDict = tempContents[chr]
            tempContents[chr] = BrInfoHolder(tuple(brInfoDict.keys()),
                                             tuple(brInfoDict.values()))

        brShelve = safeshelve.open(self._fn, 'c', protocol=self.PROTOCOL)
        brShelve.update(tempContents)
        brShelve.close()

        while not self.fileExists():
            from gold.application.LogSetup import logMessage
            logMessage(
                "Bounding region shelve file '%s' has yet to be created" %
                self._fn)
            import time
            time.sleep(0.2)
from gold.track.Track import PlainTrack
from gold.track.GenomeRegion import GenomeRegion
from gold.statistic.CountStat import CountStat

#create a track
track = PlainTrack(['Genes and gene subsets', 'Genes', 'Refseq'])

#create a region of interest
region = GenomeRegion('hg18', 'chr1', 1000, 900000)

#create a statistic
stat = CountStat(region, track)

print stat.getResult()

#What happens now:
#CountStat inherits MagicStatFactory
#MagicStatFactory determines that region may be splitted to smaller bins and looks for a CountStatSplittable.
#CountStatSplittable exists, and is instantiated.
#getResults first calls createChildren. CountStatSplittable now creates a new CountStat for a smaller first region.
#This times, when MagicStatFactory handles CountStat-creation it sees that the region in question should not be splitted.
#MagicStatFactory thus instantiates a CountStatUnsplittable, which loads track data, and does the count for its small bin.
#This is repeated for each small bin, and results are collected by CountStatSplittable.
#Finally, the method combineResults (of CountStatSplittable) computes the total results for the queried region and returns this.
 def setUp(self):
     self.statParent = MyStatistic(
         GenomeRegion('TestGenome', 'chr21', 0, 1), Track(['dummy']))
Exemplo n.º 8
0
 def execute(cls, choices, galaxyFn=None, username=''):
     #val = strVal.split(':')[1].split('k')[0];
     htmlTemplate = '''<html><head>\n\n<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>\n  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>\n  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>\n
     <script type='text/javascript' src='https://www.google.com/jsapi'></script>
     <script type='text/javascript'> 
       google.load("visualization", "1", {packages:["corechart"]});\n google.setOnLoadCallback(drawLine);
       function drawLine(divId) {\n}
   </script>
     <style type="text/css">\n    #slider { margin: 10px; }\n  </style>\n  <script type="text/javascript">\n  jQuery(document).ready(function() {\n    jQuery("#slider").slider({min: 0, value: 370, max: %i });\n  });\n  </script>\n\n\n  <link rel="stylesheet" type="text/css" href="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/styles/stylesheet.css" />
                 \n<script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/scripts/mootools-1.2.1-core.js">\n</script><script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/scripts/mootools-1.2-more.js">\n</script><script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/gsuite/static/hyperbrowser/files/kaitre//image_zoom/scripts/ImageZoom.js"></script>\n
     \n\n\n\n<script type="text/javascript" >\nliste =%s;\ncounter = 0;\n\n\nfunction point_it2(event){\n
     document.myform.posAnchor.value = "";
     chrom = %s;\n
     trackNames = %s;
     pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("zoomer_image").offsetLeft;\n	pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("zoomer_image").offsetTop;\n        factor = %i;\n        pos_x = Math.floor(pos_x/factor);\n	pos_y = Math.floor(pos_y/factor);\n	counter++;\n
     var strVal = liste[pos_y][pos_x];
     var strTab = strVal.split(",");
     
     
     val = strTab[0];
     streng = chrom+":"+strTab[0]+"k | ";
     for(i=0; i<trackNames.length; i++) { 
         streng = streng + trackNames[i]+': '+strTab[i+1]+'%% | ';
        }
         
     document.myform.posAnchor.value = streng;\n
     jQuery( "#slider" ).slider( "option", "value", val );\n
     
             }\n</script>\n\n\n\n\n</head>
     <body>
     <h2 align="center" style="color:#FF7400;">Heatmap for chromosome %s</h2> 
     <div id="slider" ></div><br>
     \n<form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">\n<div align="center">\n\n<input type="text" name="posAnchor" size="250" value=".">\n<br>\n</div>\n</form>\n<br>
     <div id="container"><!-- Image zoom start --><div id="zoomer_big_container"></div><div id="zoomer_thumb">\n<a href="%s" target="_blank" >\n<img src="%s" /></a></div><!-- Image zoom end --></div>\n\n\n%s
      
      <br/>%s</body></html>''' # onchange="jQuery('zoomer_region').css({ 'left': '31px', 'top': '15px'});"
     
     tableRowEntryTemplate = """<div class="tabbertab"><h2>%s</h2><a href="%s"><img src="%s" /></a></div>"""
     htmlPageTemplate = """<html><head>\n<script type="text/javascript" src="/gsuite/static/scripts/tabber.js"></script>\n<link href="/gsuite/static/style/tabber.css" rel="stylesheet" type="text/css" />\n
                 </head><body>%s</body></html>"""
     
     #fileDict = dict()
     binsize = parseShortenedSizeSpec(choices[10])
         
     tnList = []
     trackNameList = []
     genome = choices[0]
     chrLength = GenomeInfo.getStdChrLengthDict(genome)
     
     for index in [1,4,7]:
         startTime = time.time()
         if choices[index] in ['-- No track --','',None]:
             tnList.append(None)
             trackNameList.append('.')
             continue
         elif choices[index] == 'history':
             #trackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(choices[0], choices[index+1].split(':'))
             trackName = choices[index+1].split(':')
             tnList.append(ExternalTrackManager.extractFnFromGalaxyTN(trackName))
             trackNameList.append(prettyPrintTrackName(trackName))
         else:
             trackName = choices[index+2].split(':')
             track = PlainTrack(trackName)
             regionList = [GenomeRegion(genome, chrom, 0, chrLength[chrom]) for chrom in GenomeInfo.getChrList(genome)]
             tnList.append((track, regionList))    
             trackNameList.append(prettyPrintTrackName(trackName))
             
             
     trackNames = repr([v for v in trackNameList if v!='.'])
     tr1, tr2, tr3 = tnList
     ResultDicts = []
     maxVals = []#list of the maximum coverage in a bin for each track Used for normalization purposes
     chrsWithData = set()# set of chromosomes with any data. No point in generating images with no data...
     microDictList = []
     counter = 0
     for tr,color in [(tr1, (1,0,0)),(tr2, (0,1,0)),(tr3, (0,0,1))]:
         
         maxVal = None
         if tr:
             if len([1 for v in tnList if v]) == 1:
                 color = (1,1,1)
             
             res, microDict, maxVal, trackChrs = cls.getValuesFromBedFile(genome, tr,color, binsize)
             microDictList.append((trackNames[counter],microDict))
             counter+=1
             chrsWithData = chrsWithData|trackChrs
             ResultDicts += [res]   
         maxVals.append(maxVal)
         
     
     htmlTableContent = []
     resultDict = cls.syncResultDict(ResultDicts)
     binfactor = binsize/1000
     for chrom in sorted(list(chrsWithData), cmp=alphanum):
         valList = resultDict[chrom]
         areaList = []
         #For doing recursive pattern picture
         bigFactor = int(10*(binsize/10000.0)**(0.5))
         smallFactor = bigFactor/3
         posMatrix = cls.getResult(len(valList), 2,2)
         javaScriptList = [[0 for v in xrange(len(posMatrix[0])*bigFactor) ] for t in xrange(len(posMatrix)*bigFactor)]
         rowLen = len(posMatrix[0])
         
         im = Image.new("RGB", (rowLen, len(posMatrix)), "white")
         for yIndex, row in enumerate(posMatrix):
             for xIndex, elem in enumerate(row):
                 im.putpixel((xIndex, yIndex), valList[elem])
                 region = yIndex*rowLen + xIndex
                 #for yVals in range(yIndex*bigFactor, (yIndex+1)*bigFactor):
                 #    for xVals in range(yIndex*bigFactor, (yIndex+1)*bigFactor):
                 #        javaScriptList[yVals][xVals] = chrom+':'+str(elem)+'-'+str(elem+1)+': '+repr([ v/255.0 for v in valList[elem]])
                 
                 #javaScriptList[yIndex][xIndex] = chrom+':'+str(elem*binfactor)+'k - '+str((elem+1)*binfactor)+'k : '+repr([ trackNameList[indx]+'='+str(round(v*100/255.0, 2))+'%' for indx, v in enumerate(valList[elem])])
                 javaScriptList[yIndex][xIndex] = ','.join([str(elem*binfactor)]+[ str(round(v*100/255.0, 2)) for indx, v in enumerate(valList[elem]) if trackNameList[indx] !='.'] )
         for i in range(len(javaScriptList)):
             javaScriptList[i] = [v for v in javaScriptList[i] if v !=0]
             
     
         imSmall = im.resize((len(posMatrix[0])*smallFactor, len(posMatrix)*smallFactor))
         im2 = im.resize((len(posMatrix[0])*bigFactor, len(posMatrix)*bigFactor))
         
         fileElements = [GalaxyRunSpecificFile(['Recursive', chrom+'.png' ], galaxyFn ), GalaxyRunSpecificFile(['Recursive', chrom+'Big.png' ], galaxyFn), GalaxyRunSpecificFile(['Recursive', chrom+'Zooming.html' ], galaxyFn)]
         #fileDict['Recursive/'+chrom] = fileElements
         imSmall.save(fileElements[0].getDiskPath(ensurePath=True))
         im2.save(fileElements[1].getDiskPath(ensurePath=True))
         
         trackAndValRangeTab = zip(trackNameList, maxVals)
         colorTab = []
         onlyOneTrack = True if len([v for v in maxVals if v]) ==1 else False
         for color, vals in [('Red_combination',[1,0,0]), ('Green_combination',[0,1,0]), ('Blue_combination',[0,0,1]),('Red-Green_combination',[1,1,0]), ('Red-Blue_combination',[1,0,1]), ('Green-Blue_combination',[0,1,1]), ('Red-Green-Blue_combination',[1,1,1])]:    
             
             if not None in [maxVals[i] for i in range(len(vals)) if vals[i]>0]:
                 im = Image.new("RGB", (256 , 1), "white")
                 tracksInvolved = ' & '.join([str(index+1) for index, v in enumerate(vals) if v>0])
                 if onlyOneTrack:
                     vals = [1,1,1]
                 for val in range(256):
                     colVal = [val*v for v in vals]
                     
                     im.putpixel((val,0), tuple(colVal))
                 imColFile = GalaxyRunSpecificFile(['Recursive', color+'.png' ], galaxyFn)
                 imCol = im.resize((256, 10))
                 imCol.save(imColFile.getDiskPath(ensurePath=True))
                 colorTab.append('<tr><td>Track %s</td><td>  <img src="%s" /></td></tr>'% (tracksInvolved, imColFile.getURL()))
                 
         
         htmlTnRangeVals= '<br/><br/><table align="center"  cellspacing="10"><tr><th>Track number</th><th>Track name</th><th>Value range</th></tr>\n'
         htmlTnRangeVals += '\n'.join(['<tr/><td>Track %i </td><td>%s</td><td> 0 - %i</td></tr>' % (index+1, v[0], v[1]) for index, v in  enumerate(trackAndValRangeTab) if v[1]] )
         htmlTnRangeVals+='</table> <br/><table align="center"  cellspacing="10"><tr><th>Track combination</th><th>Colour range</th></tr>' + '\n'.join(colorTab) + '</table>\n'
         lineTabStr= ''
         #if chrom == 'chr1':
         #    tempList = [range(100)]+[v[1]['chr1'][26] for v in microDictList]
         #    chartTemplate =  "['%i',  %i, %i, %i]"
         #    lineTab = [ chartTemplate % v for v in zip(*tempList)]    
         #    lineTemplate = """<div id="%s" onclick="{\nvar data = google.visualization.arrayToDataTable([\n    %s\n  ]);\nvar options = {  title: 'Detailed Graph'    };var chart = new google.visualization.LineChart(document.getElementById('%s'));chart.draw(data, options);}" style="width: 1000px; height: 700px;"></div>"""
         #    lineTabStr = lineTemplate % ('line_div', ', '.join(lineTab),'line_div')    
         open(fileElements[2].getDiskPath(ensurePath=True),'w').write(htmlTemplate % (int(GenomeInfo.getChrLen(genome, chrom)/1000.0)+1, repr(javaScriptList), repr(chrom), trackNames,bigFactor, chrom, fileElements[1].getURL(), fileElements[0].getURL(), htmlTnRangeVals, lineTabStr) )# 
         htmlTableContent.append(tableRowEntryTemplate % (chrom, fileElements[2].getURL(), fileElements[0].getURL()))
         
         # FOr doing normal picture
         #columns = int(round((len(valList)/1000)+0.5))
         #im = Image.new("RGB", (1000, columns), "white")        
         #y=-1    
         #for index, valuTuple in enumerate(valList):
         #    x = index%1000
         #
         #    if x == 0:
         #        y+=1
         #    try:
         #        im.putpixel((x, y), valuTuple)
         #    except:
         #        pass
         #im.save(chrom+'.png')
         #htmlTableContent.append(tableRowEntryTemplate % (chrom, chrom+'.png'))
     
     tabberMal = '<div class="tabber">%s</div>'
     #tempRes, res = [],[]
     res = [tabberMal % v for v in htmlTableContent]
     #for i in htmlTableContent:
     #    if len(tempRes) == 10:
     #        res.append(tabberMal % '\n'.join(tempRes))
     #        tempRes = []
     #    tempRes.append(i)
     #if len(tempRes)>0:
     #    res.append(tabberMal % '\n'.join(tempRes))
     open(galaxyFn,'w').write(htmlPageTemplate % ('<br/>'.join(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.
        '''
        from quick.application.ExternalTrackManager import ExternalTrackManager
        from gold.origdata.BedGenomeElementSource import BedCategoryGenomeElementSource
        from gold.origdata.GtrackGenomeElementSource import GtrackGenomeElementSource
        from gold.origdata.TrackGenomeElementSource import TrackGenomeElementSource
        from gold.track.GenomeRegion import GenomeRegion
        from quick.util.GenomeInfo import GenomeInfo
        from collections import defaultdict

        genome = choices[0]
        track = choices[2].split(':')
        allowOverlaps = True if choices[3] == 'Yes' else False

        regionList = []
        for chrom in GenomeInfo.getChrList(genome):
            start = 0
            chromSize = GenomeInfo.getChrLen(genome, chrom)
            regionList.append(GenomeRegion(genome, chrom, start, chromSize))

        if choices[1] == 'From Hyperbrowser repository':
            geSource = TrackGenomeElementSource(genome, track, regionList)
        else:
            fileType = ExternalTrackManager.extractFileSuffixFromGalaxyTN(
                track)
            fn = ExternalTrackManager.extractFnFromGalaxyTN(track)
            geSource = BedCategoryGenomeElementSource(
                fn
            ) if fileType == 'category.bed' else GtrackGenomeElementSource(fn)

        resultMinDict = defaultdict(dict)
        resultMaxDict = defaultdict(dict)
        for ge in geSource:
            if resultMaxDict[ge.chr].has_key(ge.val):
                if ge.end:
                    if resultMaxDict[ge.chr][ge.val] < ge.end:
                        resultMaxDict[ge.chr][ge.val] = ge.end
                elif resultMaxDict[ge.chr][ge.val] < ge.start:
                    resultMaxDict[ge.chr][ge.val] = ge.start

                if resultMinDict[ge.chr][ge.val] > ge.start:
                    resultMinDict[ge.chr][ge.val] = ge.start
            else:
                resultMaxDict[ge.chr][ge.val] = ge.end if ge.end else ge.start
                resultMinDict[ge.chr][ge.val] = ge.start

        utfil = open(galaxyFn, 'w')
        quitFlag = False
        errorMsg = 'Error, overlapping regions '
        catsConflicting = []
        for chrom in sorted(resultMinDict.keys()):

            for category in resultMinDict[chrom].keys():
                lower, upper = resultMinDict[chrom][category], resultMaxDict[
                    chrom][category]
                if not allowOverlaps:
                    for cat in resultMinDict[chrom]:
                        if cat != category:
                            l, u = resultMinDict[chrom][cat], resultMaxDict[
                                chrom][cat]
                            if l >= upper or u <= lower:
                                continue
                            if l > lower or u < upper:
                                quitFlag = True
                                catsConflicting.append(
                                    '(Category: %s,  Region: %i - %i) vs. (Category: %s, Region: %i - %i)'
                                    % (category, lower, upper, cat, l, u))
                                #break
                    #if quitFlag: break

                print >> utfil, '\t'.join(
                    [chrom, str(lower),
                     str(upper + 1), category])

            #if quitFlag: break
        utfil.close()

        if quitFlag:
            open(galaxyFn, 'w').write(
                'Error: overlapping resulting regions are not allowed with selected preferences:\n'
                + '\n'.join(catsConflicting))
Exemplo n.º 10
0
def _getRegion(chr, start, end):
    return GenomeRegion('old_NCBI', 'chr' + str(chr), start, end)
Exemplo n.º 11
0
def parseRegSpec(regSpec, genome=None):
    from gold.track.GenomeRegion import GenomeRegion
    from quick.util.GenomeInfo import GenomeInfo

    class SimpleUserBinSource(list):
        pass

    regions = []
    allRegSpecs = regSpec.strip().split(',')
    for curRegSpec in allRegSpecs:
        regParts = curRegSpec.strip().split(':')
        if genome == None:
            genome = regParts[0]
            assert GenomeInfo(genome).isInstalled(
            ), "Specified genome is not installed: %s" % genome

        if not (regParts[0] == '*'
                or regParts[0] in GenomeInfo.getExtendedChrList(genome)):
            assert regParts[0] == genome, \
                "Region specification does not start with one of '*' or correct chromosome or genome name. Region specification: %s. Genome: %s" % (curRegSpec, genome)
            #genome = regParts[0]
            regParts = regParts[1:]

        if regParts[0] == '*':
            assert len(regParts) == 1, \
                "Region specification starts with '*' but continues with ':'. Region specification: %s" % curRegSpec
            assert len(allRegSpecs) == 1, \
                "Region specification is '*', but is in a list with other region specifications: %s" % regSpec
            for chr in GenomeInfo.getChrList(genome):
                regions.append(
                    GenomeRegion(genome, chr, 0,
                                 GenomeInfo.getChrLen(genome, chr)))
        else:
            assert regParts[0] in GenomeInfo.getExtendedChrList(genome), \
                "Region specification does not start with chromosome specification. Region specification: %s " % curRegSpec
            chr = regParts[0]
            try:
                chrLen = GenomeInfo.getChrLen(genome, chr)
            except Exception, e:
                raise InvalidFormatError(
                    "Chromosome '%s' does not exist for genome '%s'" %
                    (chr, genome))

            if len(regParts) > 1:
                posParts = regParts[1]
                assert '-' in posParts, \
                    "Position specification does not include character '-'. Region specification: %s " % curRegSpec
                rawStart, rawEnd = posParts.split('-')

                start = int(
                    rawStart.replace('k', '001').replace('m', '000001'))
                end = int(rawEnd.replace('k', '000').replace(
                    'm', '000000')) if rawEnd != '' else chrLen
                assert start >= 1, \
                    "Start position is not positive. Region specification: %s " % curRegSpec
                assert end >= start, \
                    "End position is not larger than start position. Region specification: %s " % curRegSpec
                assert end <= chrLen, \
                    "End position is larger than chromosome size. Genome: %s. Chromosome size: %s. Region specification: %s" % (genome, chrLen, curRegSpec)
                #-1 for conversion from 1-indexing to 0-indexing end-exclusive
                start -= 1

            else:
                start, end = 0, chrLen
            regions.append(GenomeRegion(genome, chr, start, end))
Exemplo n.º 12
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.
     '''
     
     
     #matchPos = strVal.search(':');\n   if (matchPos !=-1)\n    {\n   valList = eval(strVal.split(:)[1]);\n                formVal ="%s: " +valList[0]+"\n%s: " +valList[1]+"\n%s: " +valList[2];\n                document.myform.posAnchor.value = liste[pos_y][pos_x] + counter;\n            }\n        else\n            {\n                document.myform.posAnchor.value = "0";\n            }\n\n
     htmlTemplate = '''<html><head>\n\n<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>\n  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>\n  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>\n    <style type="text/css">\n    #slider { margin: 10px; }\n  </style>\n  <script type="text/javascript">\n  jQuery(document).ready(function() {\n    jQuery("#slider").slider({min: 0, value: 370, max: %i });\n  });\n  </script>\n\n\n  <link rel="stylesheet" type="text/css" href="http://hyperbrowser.uio.no/dev2/static/hyperbrowser/files/kaitre//image_zoom/styles/stylesheet.css" />
                 \n<script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/dev2/static/hyperbrowser/files/kaitre//image_zoom/scripts/mootools-1.2.1-core.js">\n</script><script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/dev2/static/hyperbrowser/files/kaitre//image_zoom/scripts/mootools-1.2-more.js">\n</script><script language="javascript" type="text/javascript" src="http://hyperbrowser.uio.no/dev2/static/hyperbrowser/files/kaitre//image_zoom/scripts/ImageZoom.js"></script>\n
     \n\n\n\n<script type="text/javascript" >\nliste =%s;\ncounter = 0;\n\n\nfunction point_it2(event){\n	pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("zoomer_image").offsetLeft;\n	pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("zoomer_image").offsetTop;\n        pos_x = Math.floor(pos_x/10);\n	pos_y = Math.floor(pos_y/10);\n	counter++;\n	strVal = liste[pos_y][pos_x];\n   	document.myform.posAnchor.value = liste[pos_y][pos_x] + counter;\n         val = strVal.split(':')[1].split('-')[0];\n	jQuery( "#slider" ).slider( "option", "value", val );\n        }\n</script>\n\n\n\n\n</head>
     <body><div id="container"><!-- Image zoom start --><div id="zoomer_big_container"></div><div id="zoomer_thumb">\n<a href="%s" target="_blank" >\n<img src="%s" /></a></div><!-- Image zoom end --></div>\n\n\n<form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">\n<div align="center">\n\n<input type="text" name="posAnchor" size="50" value=".">\n<br>\n</div>\n</form>\n\n<div id="slider" onchange="jQuery('zoomer_region').css({ 'left': '31px', 'top': '15px'});"></div>\n%s</body></html>'''
     
     tableRowEntryTemplate = """<tr><td>%s</td><td><a href="%s"><img src="%s" /></a></td></tr>"""
     htmlPageTemplate = """<html><body><table border="1">%s</table></body></html>"""
     #fileDict = dict()
     tnList = []
     trackNameList = []
     genome = choices[0]
     for index in [1,3,5]:
         if choices[index] == 'no selection':
             tnList.append(None)
             trackNameList.append('.')
         elif choices[index] == 'from history':
             tnList.append(ExternalTrackManager.extractFnFromGalaxyTN(choices[index+1].split(':')))
             trackNameList.append(ExternalTrackManager.extractNameFromHistoryTN(choices[index+1].split(':')))
         else:
             regionList = []
             for chrom in GenomeInfo.getChrList(genome):
                 start = 0
                 binsize = 10000
                 chromSize = GenomeInfo.getChrLen(genome, chrom)
                 while start<=chromSize-binsize:
                     regionList.append(GenomeRegion(genome, chrom, start, start+binsize))
                     start+=binsize
                 if start<chromSize:
                     regionList.append(GenomeRegion(genome, chrom, start, chromSize))
             trackNameList.append(choices[index+1].split(':')[-1])        
             trackGESource = TrackGenomeElementSource(genome, choices[index+1].split(':'), regionList)
             bedgraphComp = BedGraphComposer(trackGESource)
             tnList.append(bedgraphComp)
             
     
     tr1, tr2, tr3 = tnList
     tName1, tName2, tName3 = trackNameList
     ResultDicts = []
     maxVals = []
     for tr,color in [(tr1, (1,0,0)),(tr2, (0,1,0)),(tr3, (0,0,1))]:
         logMessage(repr(tr))
         if tr:
             res, maxVal = cls.getValuesFromBedFile(genome, tr,color)
             logMessage('maxval:  '+str(maxVal))
             ResultDicts += [res]
             maxVals.append(maxVal)
         else:
             maxVals.append(None)
     htmlTnRangeVals = '\n'.join(['<br/>value range for track %i(%s): 0 - %i' % (index, v[0], v[1]) for index, v in  enumerate(zip(trackNameList, maxVals)) if v[0]!='.'])
     
     #ResultDicts = [cls.getValuesFromBedFile(genome, tr1,colorPattern=(1,0,0))]
     #ResultDicts += [cls.getValuesFromBedFile(genome, tr2,colorPattern=(0,1,0))] if tr2 else []
     #ResultDicts += [cls.getValuesFromBedFile(genome, tr3,colorPattern=(0,0,1))] if tr3 else []
 
 
     htmlTableContent = []
     resultDict = cls.syncResultDict(ResultDicts)
     
     for chrom, valList in resultDict.items():
         areaList = []
         #For doing recursive pattern picture
         posMatrix = cls.getResult(len(valList), 2,2)
         javaScriptList = [[0 for v in xrange(len(posMatrix[0])) ] for t in xrange(len(posMatrix))]
         rowLen = len(posMatrix[0])
         
         im = Image.new("RGB", (rowLen, len(posMatrix)), "white")
         for yIndex, row in enumerate(posMatrix):
             for xIndex, elem in enumerate(row):
                 im.putpixel((xIndex, yIndex), valList[elem])
                 region = yIndex*rowLen + xIndex
                 javaScriptList[yIndex][xIndex] = chrom+':'+str(elem*10)+'-'+str((elem+1)*10)+': '+repr([ round((255-v)/255.0 ,2 ) for v in valList[elem]])
                 #areaList.append(areaTemplate % (xIndex*10, yIndex*10, xIndex*11, yIndex*11, repr(valList[elem])))
         imSmall = im.resize((len(posMatrix[0])*3, len(posMatrix)*3))
         im2 = im.resize((len(posMatrix[0])*10, len(posMatrix)*10))
         
         fileElements = [GalaxyRunSpecificFile(['Recursive', chrom+'.png' ], galaxyFn),
                         GalaxyRunSpecificFile(['Recursive', chrom+'Big.png' ], galaxyFn),
                         GalaxyRunSpecificFile(['Recursive', chrom+'Zooming.html' ], galaxyFn)]
         #fileDict['Recursive/'+chrom] = fileElements
         imSmall.save(fileElements[0].getDiskPath(ensurePath=True))
         im2.save(fileElements[1].getDiskPath(ensurePath=True))
         
         #
         open(fileElements[2].getDiskPath(ensurePath=True),'w').write(htmlTemplate % (int(GenomeInfo.getChrLen(genome, chrom)/1000.0)+1, str(javaScriptList), fileElements[1].getURL(), fileElements[0].getURL(), htmlTnRangeVals) )#tName1, tName2, tName3, 
         htmlTableContent.append(tableRowEntryTemplate % (chrom, fileElements[2].getURL(), fileElements[0].getURL()))
         
         # FOr doing normal picture
         #columns = int(round((len(valList)/1000)+0.5))
         #im = Image.new("RGB", (1000, columns), "white")        
         #y=-1    
         #for index, valuTuple in enumerate(valList):
         #    x = index%1000
         #
         #    if x == 0:
         #        y+=1
         #    try:
         #        im.putpixel((x, y), valuTuple)
         #    except:
         #        pass
         #im.save(chrom+'.png')
         #htmlTableContent.append(tableRowEntryTemplate % (chrom, chrom+'.png'))
     
     
     open(galaxyFn,'w').write(htmlPageTemplate % ('\n'.join(htmlTableContent)))
 def _commonStoreBoundingRegions(self, sparse):
     brTuples = [BoundingRegionTuple(GenomeRegion('TestGenome', 'chr21', 0, 1000000), 10 if sparse else 1000000),\
                 BoundingRegionTuple(GenomeRegion('TestGenome', 'chr21', 2000000, 2500000), 20 if sparse else 500000),\
                 BoundingRegionTuple(GenomeRegion('TestGenome', 'chrM', 1000, 2000), 5 if sparse else 1000)]
     self._brShelve.storeBoundingRegions(brTuples, ['chr21', 'chrM'],
                                         sparse)
Exemplo n.º 14
0
    def __init__(self,
                 segments=None,
                 starts=True,
                 ends=True,
                 vals=False,
                 strands=False,
                 ids=False,
                 edges=False,
                 weights=False,
                 extras=False,
                 anchor=None,
                 numElements=None,
                 valDType='float64',
                 borderHandling='crop',
                 allowOverlaps=False):
        if type(starts) != bool and ends == True:
            ends = False
        if type(ends) != bool and starts == True:
            starts = False

        assert not (starts == False and ends == False)
        assert segments != False and segments != True
        assert starts is not None and ends is not None and vals is not None and strands is not None
        assert segments is None or (starts == True and ends == True)
        assert not (isIter(weights) and not isIter(edges))

        assert (any( type(x) not in [bool,type(None)] for x in [segments,starts,ends,vals,strands,ids,edges,weights,extras]) and numElements==None) \
            or numElements is not None
        #assert(( (type(segments)!=bool or type(starts)!=bool or type(ends)!=bool or \
        #        type(vals)!=bool or type(strands)!=bool) and numElements==None )\
        #        or numElements!=None)
        #
        if anchor is None:
            anchor = [10, 1000]

        if segments is not None:
            starts = []
            ends = []
            for seg in segments:
                starts.append(seg[0])
                ends.append(seg[1])

        if isIter(edges):
            maxNumEdges = self._findMaxNumEls(edges)
            edges = self._appendEmptyToEnd(edges, '', maxNumEdges)
            if isIter(weights):
                weights = self._appendEmptyToEnd(weights, numpy.nan,
                                                 maxNumEdges)

        [starts, ends, vals, strands, ids, edges, weights
         ] + ([x for x in extras.values()] if isinstance(extras, dict) else [])
        for list in [starts, ends, vals, strands, ids, edges, weights] + (
            [x for x in extras.values()] if isinstance(extras, dict) else []):
            if type(list) != bool and numElements is None:
                numElements = len(list)
            assert (type(list) == bool or len(list) == numElements)

        for coordList in [starts, ends]:
            if type(coordList) != bool:
                for j in range(len(coordList)):
                    coordList[j] += anchor[0]

        randSegmentLists = getRandSegments(numElements, anchor[0], anchor[1])
        starts = self._createList(starts, randSegmentLists[0], 'int32')
        ends = self._createList(ends, randSegmentLists[1], 'int32')

        vals = self._createList(vals, getRandValList(numElements, valDType),
                                valDType)
        strands = self._createList(strands, getRandStrandList(numElements),
                                   'bool8')

        randIds, randEdges, randWeights = getRandGraphLists(numElements)
        ids = self._createList(ids, randIds, randIds.dtype)
        edges = self._createList(edges, randEdges, randEdges.dtype)
        weights = self._createList(weights, randWeights, 'float64')

        if weights is not None and len(weights.shape) == 1:
            weights = weights.reshape(weights.shape + (0, ))

        extras = self._createExtraLists(extras, 'S', numElements)

        if starts is None:
            if ends[0] != 0:
                ends = numpy.append([anchor[0]], ends)
                if vals is not None:
                    vals = numpy.append([nan], vals)
                if strands is not None:
                    strands = numpy.append([True], strands)
            if ends[-1] != anchor[1]:
                ends[-1] = anchor[1]


#        print (starts, ends, vals, strands, anchor)
        TrackView.__init__(self, GenomeRegion('TestGenome', 'chr21', anchor[0], anchor[1]), starts, ends, vals, \
                           strands, ids, edges, weights, borderHandling, allowOverlaps, extraLists=extras)
Exemplo n.º 15
0
 def getStdChrRegionList(cls, genome):
     from gold.track.GenomeRegion import GenomeRegion
     return [GenomeRegion(genome, chr, 0, cls.getChrLen(genome, chr) ) \
             for chr in cls.getChrList(genome)]
Exemplo n.º 16
0
            tv = self._getTrackView(trackName, region, allowOverlaps)
            self.assertEquals(chrElCountDict[chr], len([x for x in tv]))

    def _getTrackView(self, trackName, region, allowOverlaps):
        track = Track(trackName)
        track.addFormatReq(TrackFormatReq(allowOverlaps=allowOverlaps))
        return track.getTrackView(region)

    def _getBoundingRegionTupleList(self, case, sortedAssertElList):
        boundingRegions = [
            br for br in sorted(case.boundingRegionsAssertList)
            if br.region.chr is not None
        ]
        if len(boundingRegions) > 0:
            return [BoundingRegionTuple(GenomeRegion(self.GENOME, chr=br.region.chr, \
                                                     start=br.region.start if br.region.start is not None else 0, \
                                                     end=br.region.end if br.region.end is not None else \
                                                         GenomeInfo.getChrLen(self.GENOME, br.region.chr)), br.elCount)
                    for br in boundingRegions]
        else:
            totChrList = [ge.chr for ge in sortedAssertElList]
            chrBrList = OrderedDict([(i, totChrList.count(i))
                                     for i in sorted(set(totChrList))])
            return [BoundingRegionTuple(GenomeRegion(self.GENOME, chr=chr, start=0, \
                                                     end=GenomeInfo.getChrLen(self.GENOME, chr)), elCount) \
                    for chr, elCount in chrBrList.iteritems()]

    def _getCaseTrackView(self, case, br, allowOverlaps):
        return self._getTrackView(self.TRACK_NAME_PREFIX + case.trackName, \
                                  GenomeRegion(genome=self.GENOME, chr=br.chr, start=br.start, end=br.end), \
                                  allowOverlaps=allowOverlaps)