def getGsuiteFromRandomRows(self, count=10):
     random.seed(9001)
     rItem = self.VOCABULARY[random.randint(0, len(self.VOCABULARY) - 1)]
     rRows = self.getRandomRows(rItem, count)
     gSuite = GSuite()
     self.appendGsuiteFromRows(gSuite, rRows, rItem)
     return gSuite
    def getTrackView(self, region):
        assert self._origRegion == region
        allChrArmRegs = GenomeInfo.getContainingChrArms(region)
        if len(allChrArmRegs) != 1:
            raise CentromerError
        chrArm = allChrArmRegs[0]
        
        buffer = self._getIndepencyBufferSize(region)
        sourceRegs = chrArm.exclude( copy(region).extend(-buffer).extend(buffer) )
        assert len(sourceRegs) in [1,2]
        
        if not any(len(sourceReg) >= self.MIN_SOURCE_TO_SAMPLE_SIZE_RATIO * len(region) for sourceReg in sourceRegs):
            raise TooLargeBinError('Source region lengths of ' + str([len(x) for x in sourceRegs]) +
                                   ' are too small compared to region length of ' + str(len(region)) +
                                   ' according to MIN_SOURCE_TO_SAMPLE_SIZE_RATIO: ' + str(self.MIN_SOURCE_TO_SAMPLE_SIZE_RATIO))
        
        if len(sourceRegs) == 1:
            sourceReg = sourceRegs[0]
        else:
            firstSourceProportion = (len(sourceRegs[0])-len(region)) / sum(len(sourceRegs[i])-len(region) for i in range(2))
            sourceReg = sourceRegs[0] if random.random() < firstSourceProportion else sourceRegs[1]

        randOffset = random.randint( 0, len(sourceReg) - len(region) )
        start = sourceReg.start + randOffset
        end = start + len(region)
        randRegion = GenomeRegion(region.genome, region.chr, start, end)

        rawData = RawDataStat(randRegion, self._origTrack, self._trackFormatReq)
        tv = rawData.getResult()
        assert region != tv.genomeAnchor        
        return tv
Example #3
0
    def _getTrackView(self, region):
        from gold.util.RandomUtil import random

        #if not hasattr(self, '_minimalRegion'):
        #    from quick.application.UserBinSource import MinimalBinSource
        #    minimalBinList = MinimalBinSource(region.genome)
        #    self._minimalRegion = minimalBinList[0] if minimalBinList is not None else None
        #
        #if  self._minimalRegion == region:
        #    return self._origTrack.getTrackView(region)

        allChrArmRegs = GenomeInfo.getContainingChrArms(region)
        if len(allChrArmRegs) != 1:
            raise CentromerError
        chrArm = allChrArmRegs[0]

        buffer = self._getIndependencyBufferSize(region)
        sourceRegs = chrArm.exclude(
            region.getCopy().extend(-buffer).extend(buffer))
        assert len(sourceRegs) in [
            1, 2
        ], "Source region must be smaller than a tenth of a chromosome arm: %s" % region

        if not any(
                len(sourceReg) >= self.MIN_SOURCE_TO_SAMPLE_SIZE_RATIO *
                len(region) for sourceReg in sourceRegs):
            raise TooLargeBinError(
                'Source region lengths of ' + str([len(x)
                                                   for x in sourceRegs]) +
                ' are too small compared to region length of ' +
                str(len(region)) +
                ' according to MIN_SOURCE_TO_SAMPLE_SIZE_RATIO: ' +
                str(self.MIN_SOURCE_TO_SAMPLE_SIZE_RATIO))

        if len(sourceRegs) == 1:
            sourceReg = sourceRegs[0]
        else:
            firstSourceProportion = (len(sourceRegs[0]) - len(region)) / sum(
                len(sourceRegs[i]) - len(region) for i in range(2))
            sourceReg = sourceRegs[0] if random.random(
            ) < firstSourceProportion else sourceRegs[1]

        randOffset = random.randint(0, len(sourceReg) - len(region))
        start = sourceReg.start + randOffset
        end = start + len(region)
        randRegion = GenomeRegion(region.genome, region.chr, start, end)

        #rawData = RawDataStat(randRegion, self._origTrack, self._trackFormatReq)
        #tv = rawData.getResult()
        tv = self._origTrack.getTrackView(randRegion)
        assert tv.genomeAnchor != region, (region, tv.genomeAnchor,
                                           getClassName(region),
                                           getClassName(tv.genomeAnchor))
        return tv
Example #4
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()
    def execute(choices, galaxyFn=None, username=''):
        if username =='':
            username = ''.join([chr(random.randint(97,122)) for i in range(6)]) 
        dirForDataset = UploadDataToStorebio.STOREBIO_ROOT_PATH+username + '_'.join([str(v) for v in time.localtime()[:6]]) + str(time.time()).split('.')[-1]
        print dirForDataset
        makedirs(dirForDataset)
        
        
        #if choices[0] == 'from tracks':
        #    
        #    sourceLink = createOrigPath(genome=choices[1], trackName=choices[2].split(':'))
        #    #symboliclink = dirForDataset + sep + choices[2].split(':')[-1]
        #    #copytree(sourceLink, symboliclink)# symlink(sourceLink, symboliclink)
        #    
        #else:
        #    pass
        #    sourceLink = dirForDataset
        #    galaxyTnList = [unquote(v).split(':') for v in choices[1].values() if v]
        #
        #    for galaxyTn in galaxyTnList:
        #        
        #        fnSource = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn)
        #        fnDestination = dirForDataset+'/' + galaxyTn[-1].replace(' ','_')+'.'+galaxyTn[1]
        #        print fnSource, fnDestination
        #        copyfile(fnSource, fnDestination)
        
        projectList = UploadDataToStorebio.ProjectList
        print choices
        projName = choices[3]
        projInfo = [ (i[1], i[2]) for i in projectList if i[0] == projName  ][0]
        #(self, name, quotaId, type, url ,resourceType, userName, password, description=None, projectAccessControlList=None):
        url = dirForDataset #'scp://invitro.titan.uio.no:'+ dirForDataset
        print 'Url: ', url
        print projectList
        print projInfo
        name, quotaId, dsType = choices[-3], projInfo[1], choices[-1]
        userName, password = choices[0], choices[1]
        projectAccessControlList = [projInfo[0], 'true']
        description = choices[-2] if choices[-2] not in ['', None] else ''
        
        params = ['username:='******'password:='******'operation:=CreateDataSet', 'class:=dataStorageService',\
                  'params:='+'<#>'.join([name, quotaId, dsType, description, repr(projectAccessControlList)])]

        from quick.extra.WsStoreBioInfo import messageSep
        UploadDataToStorebio.socket.send_unicode(messageSep.join(params))
        message = UploadDataToStorebio.socket.recv_unicode().encode('utf-8')#, 'ignore'
        if message != 'something went wrong...':
            print 'Dataset successfully upploaded to Storebio server'
        else:
            print message
        print '<a href="http://hyperbrowser.uio.no/dev2/hyper?mako=generictool&tool_id=hb_add_files_to_storebioinfo_dataset">Start adding data to newly created dataset</a>'
 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.
     '''
     if username =='':
         username = ''.join([chr(random.randint(97,122)) for i in range(6)]) 
     dirForDataset = AddFilesToStorebioinfoDataset.STOREBIO_ROOT_PATH+username + '_'.join([str(v) for v in time.localtime()[:6]]) + str(time.time()).split('.')[-1]
     print dirForDataset
     makedirs(dirForDataset)
     url = 'scp://invitro.titan.uio.no:'
     if choices[0] == 'from tracks':
         
         sourceLink = createOrigPath(genome=choices[1], trackName=choices[2].split(':'))
         symboliclink = dirForDataset + sep + choices[2].split(':')[-1]
         copytree(sourceLink, symboliclink)# symlink(sourceLink, symboliclink)
         
     else:
         sourceLink = dirForDataset
         galaxyTnList = [unquote(v).split(':') for v in choices[1].values() if v]
     
         for galaxyTn in galaxyTnList:
             
             fnSource = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn)
             fnDestination = dirForDataset+'/' + galaxyTn[-1].replace(' ','_')+'.'+galaxyTn[1]
             
             copyfile(fnSource, fnDestination)
             url = url + fnDestination
             
             
     
     #AddFileToDataSet(dataSetId, subtype, pathInSubtype, url, userName, password)
     #['Username','password', 'Select dataset', 'select subtype', 'write path in subtype', 'UIO username', 'UIO password']
     
     userName, password = choices[3], choices[4]
     #uioUser, uioPwd = choices[-2], choices[-1]
     dataSetId, subtype = choices[5].split('(')[-1].split(')')[0] , choices[6]
     pathInSubtype = choices[7] if choices[7] else ''
     
     params = ['username:='******'password:='******'operation:=AddFileToDataSet', 'class:=dataStorageService',\
               'params:='+'<#>'.join([dataSetId, subtype, pathInSubtype, url])]
     print params
     
     AddFilesToStorebioinfoDataset.socket.send(messageSep.join(params))
     message = AddFilesToStorebioinfoDataset.socket.recv_unicode().encode('utf-8')#, 'ignore'
     if message != 'something went wrong...':
         print 'Dataset successfully upploaded to Storebio server'
     else:
         print message
Example #7
0
def getRandomTracks(gSuite, number, seed=9001):
    from gold.util.RandomUtil import random
    randomTrackList = []
    trackList = [t for t in gSuite.allTracks()]
    trackIndexList = [i for i in range(len(trackList))]

    random.seed(seed)
    for i in range(number):
        if len(trackIndexList) == 0:
            break
        index = random.randint(0, len(trackIndexList)-1)
        randomTrackList.append(trackList[trackIndexList[index]])
        trackIndexList.pop(index)

    return randomTrackList
    def getRandomRows(self, item, count=10, fileTypes=[]):
        #rows = self.getRows(item)
        #rows = []
        #for item in items:
        #    rows.extend(self.filterRowsByFileTypes(self.getRows(item),fileTypes))
        rows = self.filterRowsByFileTypes(self.getRows(item), fileTypes)

        if len(rows) == 0:
            return []
        rRows = []
        random.seed(9001)
        for i in range(count):
            if len(rows) == 0:
                break
            index = random.randint(0, len(rows) - 1)
            rRows.append(rows[index])
            rows.pop(index)
        return rRows
 def execute(cls, choices, galaxyFn=None, username=''):
     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 ['marked.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()
 def getRandomNode(self):
     from gold.util.RandomUtil import random
     index = random.randint(0, len(self._id2index))
     return self.getNode(self._id2index.keys()[index])
def getRandStrandList(size):
   return array([random.randint(0, 1) for i in xrange(size)], dtype='bool8')
Example #12
0
 def getRandomNode(self):
     from gold.util.RandomUtil import random
     index = random.randint(0, len(self._id2index))
     return self.getNode( self._id2index.keys()[index])
    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.extra.WsStoreBioInfo import *

        if username == '':
            username = ''.join(
                [chr(random.randint(97, 122)) for i in range(6)])
        dirForDataset = cls.STOREBIO_ROOT_PATH + username + '_'.join(
            [str(v)
             for v in time.localtime()[:6]]) + str(time.time()).split('.')[-1]
        print dirForDataset
        makedirs(dirForDataset)
        url = ''  #'scp://invitro.titan.uio.no:'
        if choices[0] == 'from tracks':

            sourceLink = createOrigPath(genome=choices[1],
                                        trackName=choices[2].split(':'))
            folder = (dirForDataset + '/' + choices[-1] + '/').replace(
                '//', '/')

            symboliclink = folder + choices[2].split(':')[-1]
            if not os.path.isdir(folder):
                os.makedirs(folder)
            copytree(sourceLink,
                     symboliclink)  # symlink(sourceLink, symboliclink)

        else:
            sourceLink = dirForDataset
            galaxyTnList = [
                unquote(v).split(':') for v in choices[1].values() if v
            ]

            for galaxyTn in galaxyTnList:

                fnSource = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn)
                folder = (dirForDataset + '/' + choices[-1] + '/').replace(
                    '//', '/')
                fnDestination = folder + galaxyTn[-1].replace(
                    ' ', '_') + '.' + galaxyTn[1]
                if not os.path.isdir(folder):
                    os.makedirs(folder)
                copyfile(fnSource, fnDestination)
                url = url + fnDestination

        #AddFileToDataSet(dataSetId, subtype, pathInSubtype, url, userName, password)
        #['Username','password', 'Select dataset', 'select subtype', 'write path in subtype', 'UIO username', 'UIO password']

        userName, password = choices[3], choices[4]
        #uioUser, uioPwd = choices[-2], choices[-1]
        dataSetId, subtype = choices[5].split('(')[-1].split(
            ')')[0], choices[6]
        pathInSubtype = choices[7] if choices[7] else ''

        params = ['username:='******'password:='******'operation:=AddFileToDataSet', 'class:=dataStorageService',\
                  'params:='+'<#>'.join([dataSetId, subtype, pathInSubtype, dirForDataset])]
        print params

        cls.socket.send(messageSep.join(params))
        message = cls.socket.recv_unicode().encode('utf-8')  #, 'ignore'
        if message != 'something went wrong...':
            print 'Dataset successfully upploaded to Storebio server'
        else:
            print message
Example #14
0
def getRandStrandList(size):
    from gold.util.RandomUtil import random
    return array([random.randint(0, 1) for i in xrange(size)], dtype='bool8')
Example #15
0
def getRandStrandList(size):
    return array([random.randint(0, 1) for i in xrange(size)], dtype='bool8')