예제 #1
0
    def sync(self, conflictResolution='raise', readOnly=False):
        """
        Two-way sync with the pickled file object on disk. Supports syncing contents of objects implementing an
        'update()' method (e.g. dicts). Only reads the stored file if its modification time has been changed.
        :param conflictResolution: One of 'raise', 'local', 'remote'. A conflict happens if both the local object and
            the file stored object has changed and is not equal. If 'raise`, a SyncConflictError is raised, if
            'local', the locally stored object replaces the object stored in the file, and if 'remote', the object
            stored in the file replaces the locally stored object.
        :param readOnly: If True, only syncs and remote changes into the local state (according to conflimtResolution)
            but the local state is not written to the file until the next time sync is called with readOnly=False.
        :return: None
        """
        self._checkIfClosed()
        assert conflictResolution in ['raise', 'local', 'remote']
        assert readOnly in [False, True]

        ensurePathExists(self._fn)

        fileModTime = self._getFileModTime()
        fileDirty = fileModTime != self._prevFileModTime

        if fileDirty or self._dirty() or self._postponeWrite:
            with FileLock(self._fn, 'w' if self._dirty() else 'r'):
                if fileDirty:
                    self._readFileAndSyncWithLocalObject(fileModTime, conflictResolution)

                if self._dirty() or self._postponeWrite:
                    if readOnly:
                        self._cleanEditHistory(postponeWrite=True)
                    else:
                        self._writeSyncedObjectToFile(fileModTime)
                        self._cleanEditHistory()
예제 #2
0
    def __enter__(self):
        if self._mode == 'r':
            lockflags = LOCK_SH | LOCK_NB
        else:
            lockflags = LOCK_EX

        ensurePathExists(self._lockFilePath)
        # os.open with os.O_SHLOCK instead?
        self._lckFile = open(self._lockFilePath, self._mode)
        fcntl.flock(self._lckFile.fileno(), lockflags)
예제 #3
0
    def getDiskPath(self, ensurePath=False, relativeToBase=False):
        path = self._galaxyFn
        if relativeToBase:
            if path.startswith(GALAXY_BASE_DIR):
                path = path[len(GALAXY_BASE_DIR):]

        fn = getGalaxyFilesFilename(path, self._relativeId)
        #fn = os.sep.join([GALAXY_FILE_PATH] + [self._id[1], 'dataset_'+self._id[2]+'_files'] + self._id[3:])
        if ensurePath:
            ensurePathExists(fn)
        return fn
예제 #4
0
    def _produceTableWithGsuiteButton(core,
                                      gsuiteFn=None,
                                      gsuiteHistElementName=None,
                                      origProduceTableCallbackFunc=None,
                                      **kwArgsToCallback):
        assert gsuiteFn is not None
        from proto.CommonFunctions import ensurePathExists
        ensurePathExists(gsuiteFn)
        core.importFileToHistoryButton("Import GSuite with results to history",
                                       gsuiteFn, 'gsuite',
                                       gsuiteHistElementName)

        return origProduceTableCallbackFunc(core, **kwArgsToCallback)
예제 #5
0
    def _compute(self):
        ensurePathExists(self._trackFilePath)
        outputFile = open(self._trackFilePath, 'a')

        trackView = self._children[0].getResult()
        starts = trackView.startsAsNumpyArray()
        ends = trackView.endsAsNumpyArray()

        for segmentIndex in range(0, len(starts)):
            outputFile.write('\t'.join([
                self._region.chr,
                str(starts[segmentIndex]),
                str(ends[segmentIndex])
            ]) + '\n')

        outputFile.close()

        # this return is not entirely necessary, as the filenames have already been added to the trackstructure
        return quote(self._trackFilePath)
예제 #6
0
    def tableWithTabularImportButton(self,
                                     tabularFn=None,
                                     tabularHistElementName='Raw table',
                                     produceTableCallbackFunc=None,
                                     **kwArgsToCallback):
        assert produceTableCallbackFunc is not None
        assert tabularFn is not None

        textCore = self._getTextCoreCls()()
        textCore = produceTableCallbackFunc(textCore, **kwArgsToCallback)

        from proto.CommonFunctions import ensurePathExists
        ensurePathExists(tabularFn)
        open(tabularFn, 'w').write(str(textCore))

        self.importFileToHistoryButton("Import table to history (tabular)",
                                       tabularFn, 'tabular',
                                       tabularHistElementName)

        return produceTableCallbackFunc(self, **kwArgsToCallback)
예제 #7
0
 def getDiskPath(self, ensurePath=False, relativeToBase=False):
     path = self.STATIC_DIR if relativeToBase else self.STATIC_PATH
     fn = os.sep.join([path] + self._id)
     if ensurePath:
         ensurePathExists(fn)
     return fn
예제 #8
0
    def execute(cls, choices, galaxyFn=None, username=''):
        genome = choices.genome
         

        from quick.multitrack.MultiTrackCommon import getGSuiteDataFromGalaxyTN
        trackTitles, refTrackNameList, genome = getGSuiteDataFromGalaxyTN(choices.gsuite)
        
        queryTrackName = ExternalTrackManager.extractFnFromGalaxyTN(choices.targetTrack)
        if choices.isBasic:
            suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(choices.targetTrack, False)
            regSpec = suffix
            binSpec = queryTrackName
        else:
            regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices)
        
        #targetTrack = choices.targetTrack.split(':')
        #targetTrackTitle = targetTrack[-1]
        #print targetTrackTitle
        #
        #binSpec = targetTrackTitle
        #Phenotype and disease associations:Assorted experiments:Virus integration, HPV specific, Kraus and Schmitz, including 50kb flanks

        from gold.gsuite.GSuiteConstants import TITLE_COL
        from gold.gsuite.GSuite import GSuite
        from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
        from gold.gsuite.GSuiteEditor import selectColumnsFromGSuite
        staticFile=[]
        
        results = []
        for refTrack in refTrackNameList:
            analysisDef = '-> ProportionCountStat' #ProportionCountStat #CountStat
            res = GalaxyInterface.runManual([refTrack], analysisDef, regSpec, binSpec, genome, username=username, galaxyFn=galaxyFn, printRunDescription=False, printResults=False, printProgress=False)
            segCoverageProp = [res[seg]['Result'] for seg in res.getAllRegionKeys()]
            results.append(segCoverageProp)
            
            regFileNamer = GalaxyRunSpecificFile(refTrack, galaxyFn)
            staticFile.append([regFileNamer.getLink('Download bed-file'), regFileNamer.getLoadToHistoryLink('Download bed-file to History')])

        refGSuite = getGSuiteFromGalaxyTN(choices.gsuite)

        if TITLE_COL == choices.selectColumns:
            selected = trackTitles
        else:
            selected = refGSuite.getAttributeValueList(choices.selectColumns)

        yAxisNameOverMouse=[]
        metadataAll =[]

        for x in range(0, len(selected)):
            if selected[x] == None:
                yAxisNameOverMouse.append(str(trackTitles[x]) + ' --- ' + 'None')
            else:
                if TITLE_COL == choices.selectColumns:
                    yAxisNameOverMouse.append(selected[x].replace('\'', '').replace('"', ''))
                else:
                    metadata = str(selected[x].replace('\'', '').replace('"', ''))
                    yAxisNameOverMouse.append(str(trackTitles[x]) + ' --- ' + metadata)
                    metadataAll.append(metadata)

        colorListForYAxisNameOverMouse = []
        if len(metadataAll) > 0:
            import quick.webtools.restricted.visualization.visualizationGraphs as vg
            cList = vg.colorList().fullColorList()
            uniqueCList = list(set(metadataAll))

            for m in metadataAll:
                colorListForYAxisNameOverMouse.append(cList[uniqueCList.index(m)])

        #startEnd - order in res
        startEndInterval = []
        startEnd = []
        i=0
        

        extraX=[]
        rowLabel = []
        for ch in res.getAllRegionKeys():
            rowLabel.append(str(ch.chr) + ":" + str(ch.start) + "-" + str(ch.end) + str(' (Pos)' if ch.strand else ' (Neg)'))
            if not i==0 and not i==len(res.getAllRegionKeys())-1:
                start = ch.start
                if start-end > 0:
                    startEnd.append(start-end)
                else:
                    startEnd.append('null')
                    extraX.append("""{ color: 'orange', width: 5, value: '""" + str(i-0.5) + """' }""")
                startEndInterval.append(ch.end - ch.start)
            else:
                startEndInterval.append(ch.end - ch.start)
            end = ch.end
            i+=1

        extraXAxis='plotLines: [ '
        extraXAxis = extraXAxis + ",".join(extraX)
        extraXAxis = extraXAxis + """ ],  """

        #rowLabel = res.getAllRegionKeys()
        #rowLabel = [str(x) for x in rowLabel]
        

        import quick.webtools.restricted.visualization.visualizationPlots as vp

        htmlCore = HtmlCore()
        htmlCore.begin()
        htmlCore.divBegin(divId='results-page')
        htmlCore.divBegin(divClass='results-section')
        htmlCore.divBegin('plotDiv')
        htmlCore.line(vp.addJSlibs())
        htmlCore.line(vp.useThemePlot())
        htmlCore.line(vp.addJSlibsExport())
        htmlCore.line(vp.axaddJSlibsOverMouseAxisisPopup())
        #vp.addGuideline(htmlCore)
        htmlCore.line(vp._addGuidelineV1())

        htmlCore.line(vp.addJSlibsHeatmap())

        from config.Config import DATA_FILES_PATH

        from proto.StaticFile import StaticFile, GalaxyRunSpecificFile

        #sf = GalaxyRunSpecificFile(['result.txt'], galaxyFn)
        #outFile = sf.getDiskPath(ensurePath=True)

        htmlCore.divBegin()
        writeFile = open(
            cls.makeHistElement(galaxyExt='tabular',
                                title='result'), 'w')
        # htmlCore.link('Get all results', sf.getURL())
        htmlCore.divEnd()

        i = 0

        writeFile.write('Track' + '\t' + '\t'.join(rowLabel)+ '\n')
        for rList in results:
            writeFile.write(str(yAxisNameOverMouse[i]) + '\t' + '\t'.join([str(r) for r in rList]) + '\n')
            i+=1




        fileOutput = GalaxyRunSpecificFile(['heatmap.png'],
                                           galaxyFn)
        ensurePathExists(fileOutput.getDiskPath())

        fileOutputPdf = GalaxyRunSpecificFile(['heatmap.pdf'],
                                              galaxyFn)
        ensurePathExists(fileOutputPdf.getDiskPath())

        cls.generateStaticRPlot(results, colorListForYAxisNameOverMouse, rowLabel, yAxisNameOverMouse,
                                colorMaps[choices.colorMapSelectList],
                                fileOutput.getDiskPath(), fileOutputPdf.getDiskPath())


        htmlCore.divBegin(divId='heatmap', style="padding: 10px 0 px 10 px 0px;margin: 10px 0 px 10 px 0px")
        htmlCore.link('Download heatmap image', fileOutputPdf.getURL())
        htmlCore.divEnd()

        if len(results) * len(results[1]) >= 10000:
            htmlCore.image(fileOutput.getURL())


        else:

            min = 1000000000
            max = -1000000000
            for rList in results:
                for r in rList:
                    if min > r:
                        min = r
                    if max < r:
                        max = r




            if max-min != 0:
                resultNormalised = []
                for rList in results:
                    resultNormalisedPart = []
                    for r in rList:
                        resultNormalisedPart.append((r-min)/(max-min))
                    resultNormalised.append(resultNormalisedPart)

                addText = '(normalised to [0, 1])'
            else:
                resultNormalised = results
                addText = ''


            hm, heatmapPlotNumber, heatmapPlot = vp.drawHeatMap(
                                                    resultNormalised,
                                                    colorMaps[choices.colorMapSelectList],
                                                    label='this.series.xAxis.categories[this.point.x] + ' + "'<br >'" + ' + yAxisNameOverMouse[this.point.y] + ' + "'<br>Overlap proportion" + str(addText) + ": <b>'" + ' + this.point.value + ' + "'</b>'",
                                                    yAxisTitle= 'Reference tracks',
                                                    categories=rowLabel,
                                                    tickInterval=1,
                                                    plotNumber=3,
                                                    interaction=True,
                                                    otherPlotNumber=1,
                                                    titleText='Overlap with reference tracks for each local region',
                                                    otherPlotData=[startEnd, startEndInterval],
                                                    overMouseAxisX=True,
                                                    overMouseAxisY=True,
                                                    yAxisNameOverMouse=yAxisNameOverMouse,
                                                    overMouseLabelY=" + 'Track: '" + ' + this.value + ' + "' '" + ' + yAxisNameOverMouse[this.value] + ',
                                                    overMouseLabelX = ' + this.value.substring(0, 20) +',
                                                    extrOp = staticFile
                                                    )
            htmlCore.line(hm)
            htmlCore.line(vp.drawChartInteractionWithHeatmap(
                [startEndInterval, startEnd],
                tickInterval=1,
                type='line',
                categories=[rowLabel, rowLabel],
                seriesType=['line', 'column'],
                minWidth=300,
                height=500,
                lineWidth=3,
                titleText=['Lengths of segments (local regions)','Gaps between consecutive segments'],
                label=['<b>Length: </b>{point.y}<br/>', '<b>Gap length: </b>{point.y}<br/>'],
                subtitleText=['',''],
                yAxisTitle=['Lengths','Gap lengths'],
                seriesName=['Lengths','Gap lengths'],
                xAxisRotation=90,
                legend=False,
                extraXAxis=extraXAxis,
                heatmapPlot=heatmapPlot,
                heatmapPlotNumber=heatmapPlotNumber,
                overMouseAxisX=True,
                overMouseLabelX = ' + this.value.substring(0, 20) +'
                ))


        htmlCore.divEnd()
        htmlCore.divEnd()
        htmlCore.divEnd()
        htmlCore.end()

        htmlCore.hideToggle(styleClass='debug')

        print htmlCore