def setUp(self):
     self.prevCompBinSize = gold.util.CompBinManager.COMP_BIN_SIZE
     gold.util.CompBinManager.COMP_BIN_SIZE = 100
     #self.prevCompBinSize = gold.track.TrackViewLoader.COMP_BIN_SIZE
     #gold.track.TrackViewLoader.COMP_BIN_SIZE = 100
     from gold.track.TrackViewLoader import TrackViewLoader 
     self.trackViewLoader = TrackViewLoader()
Exemple #2
0
 def __init__(self, trackName, trackTitle=None):
     self.trackName = trackName
     self.trackTitle = trackTitle
     self._trackSource = TrackSource()
     self._trackViewLoader = TrackViewLoader()
     self._trackFormatReq = NeutralTrackFormatReq()
     self.formatConverters = None
     self._trackId = None
Exemple #3
0
 def __init__(self, trackName):
     self.trackName = trackName
     self._trackSource = TrackSource()
     self._trackViewLoader = TrackViewLoader()
     self._trackFormatReq = NeutralTrackFormatReq()
     self.formatConverters = None
     self._trackId = None
 def setUp(self):
     self.prevCompBinSize = gold.util.CompBinManager.COMP_BIN_SIZE
     gold.util.CompBinManager.COMP_BIN_SIZE = 100
     #self.prevCompBinSize = gold.track.TrackViewLoader.COMP_BIN_SIZE
     #gold.track.TrackViewLoader.COMP_BIN_SIZE = 100
     from gold.track.TrackViewLoader import TrackViewLoader 
     self.trackViewLoader = TrackViewLoader()
Exemple #5
0
class Track(object):
    IS_MEMOIZABLE = True
    def __new__(cls, trackName):
        if trackName == [] or trackName is None:
            return None
        else:
            if ExternalTrackManager.isVirtualTrack(trackName):
                return VirtualMinimalTrack.__new__(VirtualMinimalTrack)
            else:
                return object.__new__(cls)
    
    def __init__(self, trackName):
        self.trackName = trackName
        self._trackSource = TrackSource()
        self._trackViewLoader = TrackViewLoader()
        self._trackFormatReq = NeutralTrackFormatReq()
        self.formatConverters = None
        self._trackId = None
        
    def _getRawTrackView(self, region, borderHandling, allowOverlaps):
        trackData = self._trackSource.getTrackData(self.trackName, region.genome, region.chr, allowOverlaps)
        return self._trackViewLoader.loadTrackView(trackData, region, borderHandling, allowOverlaps, self.trackName)
    
    def getTrackView(self, region):
        allowOverlaps = self._trackFormatReq.allowOverlaps()
        borderHandling = self._trackFormatReq.borderHandling()
        assert(allowOverlaps is not None) 
        assert(borderHandling is not None) 
        
        origTrackView = self._getRawTrackView(region, borderHandling, allowOverlaps)
        
        if self.formatConverters is None:
            self.formatConverters = getFormatConverters(origTrackView.trackFormat, self._trackFormatReq)
        
        if self.formatConverters == []:
            raise IncompatibleTracksError(prettyPrintTrackName(self.trackName) + ' with format: '\
                                          + str(origTrackView.trackFormat) +
                                          ('(' + origTrackView.trackFormat._val + ')' if origTrackView.trackFormat._val else '') + \
                                          ' does not satisfy ' + str(self._trackFormatReq))
        
        if not self.formatConverters[0].canHandle(origTrackView.trackFormat, self._trackFormatReq):
            raise IncompatibleTracksError(getClassName(self.formatConverters[0]) +\
                                          ' does not support conversion from ' + str(origTrackView.trackFormat) + \
                                          ' to ' + str(self._trackFormatReq))
        return self.formatConverters[0].convert(origTrackView)

    def addFormatReq(self, requestedTrackFormat):
        prevFormatReq = self._trackFormatReq
        self._trackFormatReq = TrackFormatReq.merge(self._trackFormatReq, requestedTrackFormat)
        if self._trackFormatReq is None:
            raise IncompatibleTracksError(str(prevFormatReq ) + \
                                          ' is incompatible with additional ' + str(requestedTrackFormat))
    
    def setFormatConverter(self, converterClassName):
        assert( self.formatConverters is None )
        if converterClassName is not None:        
            self.formatConverters = [getFormatConverterByName(converterClassName)]
    
    def getUniqueKey(self, genome):
        assert self.formatConverters is not None and len(self.formatConverters) == 1, 'FC: '+str(self.formatConverters)
        assert( not None in [self._trackFormatReq.allowOverlaps(), \
                             self._trackFormatReq.borderHandling()] )
        
        if not self._trackId:
            self._trackId = TrackInfo(genome, self.trackName).id
            
        return hash((tuple(self.trackName), self._trackId, getClassName(self.formatConverters[0]), \
                     self.formatConverters[0].VERSION, self._trackFormatReq.allowOverlaps(), \
                     self._trackFormatReq.borderHandling()))
class TestTrackViewLoader(TestCaseWithImprovedAsserts):
    def setUp(self):
        self.prevCompBinSize = gold.util.CompBinManager.COMP_BIN_SIZE
        gold.util.CompBinManager.COMP_BIN_SIZE = 100
        #self.prevCompBinSize = gold.track.TrackViewLoader.COMP_BIN_SIZE
        #gold.track.TrackViewLoader.COMP_BIN_SIZE = 100
        from gold.track.TrackViewLoader import TrackViewLoader 
        self.trackViewLoader = TrackViewLoader()

    def tearDown(self):
        gold.util.CompBinManager.COMP_BIN_SIZE = self.prevCompBinSize
        #gold.track.TrackViewLoader.COMP_BIN_SIZE = self.prevCompBinSize        

    def _assertTrackViewLoading_Numbers(self, trackData, start, end):
        trackView = self.trackViewLoader.loadTrackView(trackData, GenomeRegion(genome='TestGenome', start=start, end=end), 'crop', False)
        self.assertListsOrDicts(trackData['val'][start:end], [el.val() for el in trackView])
        self.assertListsOrDicts(trackData['strand'][start:end], [el.strand() for el in trackView])
        self.assertListsOrDicts(trackData['id'][start:end], [el.id() for el in trackView])
        self.assertListsOrDicts(trackData['edges'][start:end], [el.edges() for el in trackView])
        self.assertListsOrDicts(trackData['weights'][start:end], [el.weights() for el in trackView])
        self.assertListsOrDicts(trackData['a'][start:end], [el.a() for el in trackView])
        self.assertListsOrDicts(trackData['b'][start:end], [el.b() for el in trackView])
        
    def _getTrackData_Numbers(self, size):
        id, edges, weights = getRandGraphLists(size, maxNumEdges=10)
        return TrackData({'val': list(getRandValList(size)), \
                          'strand': list(getRandStrandList(size)), \
                          'id': list(id), \
                          'edges': list(edges), \
                          'weights': list(weights), \
                          'a': [str(x) for x in xrange(size)], \
                          'b': [str(x) for x in xrange(size, 0, -1)]})
    
    def testLoadTrackView_Numbers(self):
        trackData = self._getTrackData_Numbers(900)
        self._assertTrackViewLoading_Numbers(trackData, 0, 100)
        self._assertTrackViewLoading_Numbers(trackData, 0, 900)
        self._assertTrackViewLoading_Numbers(trackData, 300, 700)

        self._assertTrackViewLoading_Numbers(trackData, 312, 700)
        self._assertTrackViewLoading_Numbers(trackData, 300, 687)
        self._assertTrackViewLoading_Numbers(trackData, 312, 687)
        
        self._assertTrackViewLoading_Numbers(trackData, 0, 0)
        self._assertTrackViewLoading_Numbers(trackData, 300, 300)
        self._assertTrackViewLoading_Numbers(trackData, 400, 300)
        
        trackData = self._getTrackData_Numbers(891)
        self._assertTrackViewLoading_Numbers(trackData, 800, 880)
        self._assertTrackViewLoading_Numbers(trackData, 800, 891)
        self._assertTrackViewLoading_Numbers(trackData, 700, 880)
        
    def _assertTrackViewLoading_Segments(self, trackData, indexList, start, end):
        trackView = self.trackViewLoader.loadTrackView(trackData, GenomeRegion(genome='TestGenome', start=start, end=end),'crop',False)
        i = -1
        for i,el in enumerate(trackView):
            if i < len(indexList):
                index = indexList[i]
            else:
                self.fail()
            self.assertEqual(max(0, trackData['start'][index] - start), el.start())
            self.assertEqual(min(end, trackData['end'][index]) - start, el.end())
            self.assertAlmostEqual(trackData['val'][index], el.val())
            self.assertEqual(trackData['strand'][index], el.strand())
            self.assertEqual(trackData['id'][index], el.id())
            self.assertListsOrDicts(trackData['edges'][index], el.edges())
            self.assertListsOrDicts(trackData['weights'][index], el.weights())
            self.assertEqual(trackData['a'][index], el.a())
            self.assertEqual(trackData['b'][index], el.b())
            self.assertRaises(AttributeError, lambda: el.leftIndex)
            self.assertRaises(AttributeError, lambda: el.rightIndex)
        self.assertEqual(len(indexList), i+1)
    
    def testLoadTrackView_Segments(self):
        id, edges, weights = getRandGraphLists(4)
        trackData = TrackData({'start' : [10, 210, 260, 410],\
                               'end' : [20, 240, 310, 710],\
                               'val' : list(getRandValList(4)),\
                               'strand' : list(getRandStrandList(4)),\
                               'id': list(id), \
                               'edges': list(edges), \
                               'weights': list(weights), \
                               'a': ['A', 'B', 'C', 'D'], \
                               'b': ['1.0', '2.0', '3.0', '4.0'], \
                               'leftIndex' : [0, 1, 1, 1, 3, 3, 3, 3, 4],\
                               'rightIndex' : [1, 1, 3, 3, 4, 4, 4, 4, 4]})
        
        self._assertTrackViewLoading_Segments(trackData, [0], 0, 100)
        self._assertTrackViewLoading_Segments(trackData, [1, 2], 200, 300)
        self._assertTrackViewLoading_Segments(trackData, [0, 1, 2, 3], 0, 900)
        self._assertTrackViewLoading_Segments(trackData, [2, 3], 300, 700)

        self._assertTrackViewLoading_Segments(trackData, [3], 310, 700)
        self._assertTrackViewLoading_Segments(trackData, [2], 300, 410)
        self._assertTrackViewLoading_Segments(trackData, [], 310, 410)

        self._assertTrackViewLoading_Segments(trackData, [], 0, 0)
        self._assertTrackViewLoading_Segments(trackData, [], 300, 300)
        self._assertTrackViewLoading_Segments(trackData, [], 400, 400)
        
    def runTest(self):
        self.testLoadTrackView_Numbers()
Exemple #7
0
class Track(object):
    IS_MEMOIZABLE = True
    
    def __new__(cls, trackName, trackTitle=None):
        if trackName == [] or trackName is None:
            return None
        else:
            if ExternalTrackManager.isVirtualTrack(trackName):
                return VirtualMinimalTrack.__new__(VirtualMinimalTrack)
            else:
                return object.__new__(cls)

    def __init__(self, trackName, trackTitle=None):
        self.trackName = trackName
        self.trackTitle = trackTitle
        self._trackSource = TrackSource()
        self._trackViewLoader = TrackViewLoader()
        self._trackFormatReq = NeutralTrackFormatReq()
        self.formatConverters = None
        self._trackId = None
        self._randIndex = None

    def _getRawTrackView(self, region, borderHandling, allowOverlaps):
        trackData = self._trackSource.getTrackData(self.trackName, region.genome, region.chr, allowOverlaps)
        return self._trackViewLoader.loadTrackView(trackData, region, borderHandling, allowOverlaps, self.trackName)

    def getTrackView(self, region):
        allowOverlaps = self._trackFormatReq.allowOverlaps()
        borderHandling = self._trackFormatReq.borderHandling()
        assert(allowOverlaps is not None)
        assert(borderHandling is not None)

        origTrackView = self._getRawTrackView(region, borderHandling, allowOverlaps)

        if self.formatConverters is None:
            self.formatConverters = getFormatConverters(origTrackView.trackFormat, self._trackFormatReq)

        if self.formatConverters == []:
            raise IncompatibleTracksError(prettyPrintTrackName(self.trackName) + ' with format: '\
                                          + str(origTrackView.trackFormat) +
                                          ('(' + origTrackView.trackFormat._val + ')' if origTrackView.trackFormat._val else '') + \
                                          ' does not satisfy ' + str(self._trackFormatReq))

        if not self.formatConverters[0].canHandle(origTrackView.trackFormat, self._trackFormatReq):
            raise IncompatibleTracksError(getClassName(self.formatConverters[0]) +\
                                          ' does not support conversion from ' + str(origTrackView.trackFormat) + \
                                          ' to ' + str(self._trackFormatReq))
        return self.formatConverters[0].convert(origTrackView)

    def addFormatReq(self, requestedTrackFormat):
        prevFormatReq = self._trackFormatReq
        self._trackFormatReq = TrackFormatReq.merge(self._trackFormatReq, requestedTrackFormat)
        if self._trackFormatReq is None:
            raise IncompatibleTracksError(str(prevFormatReq ) + \
                                          ' is incompatible with additional ' + str(requestedTrackFormat))

    def setFormatConverter(self, converterClassName):
        assert( self.formatConverters is None )
        if converterClassName is not None:
            self.formatConverters = [getFormatConverterByName(converterClassName)]

    def getUniqueKey(self, genome):
        if not self._trackId:
            self._trackId = TrackInfo(genome, self.trackName).id

        return hash((tuple(self.trackName),
                     self._trackId if self._trackId else '',
                     getClassName(self.formatConverters[0]) if self.formatConverters else '',
                     self.formatConverters[0].VERSION if self.formatConverters else '',
                     self._trackFormatReq.allowOverlaps() if self._trackFormatReq.allowOverlaps() else '',
                     self._trackFormatReq.borderHandling() if self._trackFormatReq.borderHandling() else ''))

    def resetTrackSource(self):
        self._trackSource = TrackSource()

    def setRandIndex(self, randIndex):
        pass #used only by TsBasedRandomTrack
Exemple #8
0
class Track(object):
    IS_MEMOIZABLE = True

    def __new__(cls, trackName, trackTitle=None):
        if trackName == [] or trackName is None:
            return None
        else:
            if ExternalTrackManager.isVirtualTrack(trackName):
                return VirtualMinimalTrack.__new__(VirtualMinimalTrack)
            else:
                return object.__new__(cls)

    def __init__(self, trackName, trackTitle=None):
        self.trackName = trackName
        self.trackTitle = trackTitle
        self._trackSource = TrackSource()
        self._trackViewLoader = TrackViewLoader()
        self._trackFormatReq = NeutralTrackFormatReq()
        self.formatConverters = None
        self._trackId = None
        self._hasBeenRead = False

    def _getRawTrackView(self, region, borderHandling, allowOverlaps):
        trackData = self._trackSource.getTrackData(self.trackName, region.genome, region.chr, allowOverlaps)
        return self._trackViewLoader.loadTrackView(trackData, region, borderHandling, allowOverlaps, self.trackName)

    def getTrackView(self, region):
        allowOverlaps = self._trackFormatReq.allowOverlaps()
        borderHandling = self._trackFormatReq.borderHandling()
        assert(allowOverlaps is not None)
        assert(borderHandling is not None)

        origTrackView = self._getRawTrackView(region, borderHandling, allowOverlaps)

        if self.formatConverters is None:
            self.formatConverters = getFormatConverters(origTrackView.trackFormat, self._trackFormatReq)

        self._hasBeenRead = True

        if self.formatConverters == []:
            raise IncompatibleTracksError(prettyPrintTrackName(self.trackName) + ' with format: '\
                                          + str(origTrackView.trackFormat) +
                                          ('(' + origTrackView.trackFormat._val + ')' if origTrackView.trackFormat._val else '') + \
                                          ' does not satisfy ' + str(self._trackFormatReq))

        if not self.formatConverters[0].canHandle(origTrackView.trackFormat, self._trackFormatReq):
            raise IncompatibleTracksError(getClassName(self.formatConverters[0]) +\
                                          ' does not support conversion from ' + str(origTrackView.trackFormat) + \
                                          ' to ' + str(self._trackFormatReq))
        return self.formatConverters[0].convert(origTrackView)

    def addFormatReq(self, requestedTrackFormat):
        prevFormatReq = self._trackFormatReq
        self._trackFormatReq = TrackFormatReq.merge(self._trackFormatReq, requestedTrackFormat)
        if self._trackFormatReq is None:
            raise IncompatibleTracksError(str(prevFormatReq ) + \
                                          ' is incompatible with additional ' + str(requestedTrackFormat))

    # TODO: track.formatConverters needs a complete overhaul. It is currently used:
    # 1) to link tracks with possible choices for track conversion
    # 2) to store the choice of format converter made by the user
    # 3) to manage the default choice of format converters [=always the first item in the list]
    # 4) to hold the currently selected format converter class until needed by getTrackView
    def setFormatConverter(self, converterClassName):
        assert( self.formatConverters is None )
        if converterClassName is not None:
            self.formatConverters = [getFormatConverterByName(converterClassName)]

    def hasBeenFlaggedAsRead(self):
        return self._hasBeenRead

    def getUniqueKey(self, genome):
        itemsToBeHashed = [tuple(self.trackName)]
        itemsToBeHashed.append(self._getTrackId(genome))

        if self._trackFormatReq is not None:
            if self._trackFormatReq.allowOverlaps() is not None:
                itemsToBeHashed.append(self._trackFormatReq.allowOverlaps())
            itemsToBeHashed.append(self._trackFormatReq.borderHandling())

        itemsToBeHashed.append(getClassName(self.formatConverters[0]) if self.formatConverters else '')
        # TODO: Move away from fixed VERSION, as these in practice are never updated. Also for statistics.
        itemsToBeHashed.append(self.formatConverters[0].VERSION if self.formatConverters else '')

        from config.DebugConfig import DebugConfig
        if DebugConfig.VERBOSE:
            from gold.application.LogSetup import logMessage
            logMessage('Unique key items for track "{}": '.format(self.trackName) +
                       ', '.join(str(_) for _ in itemsToBeHashed))

        return hash(tuple(itemsToBeHashed))

    def _getTrackId(self, genome):
        if not self._trackId:
            trackInfo = TrackInfo(genome, self.trackName)
            self._trackId = trackInfo.id
        return self._trackId if self._trackId else ''

    def resetTrackSource(self):
        self._trackSource = TrackSource()
class TestTrackViewLoader(TestCaseWithImprovedAsserts):
    def setUp(self):
        self.prevCompBinSize = gold.util.CompBinManager.COMP_BIN_SIZE
        gold.util.CompBinManager.COMP_BIN_SIZE = 100
        #self.prevCompBinSize = gold.track.TrackViewLoader.COMP_BIN_SIZE
        #gold.track.TrackViewLoader.COMP_BIN_SIZE = 100
        from gold.track.TrackViewLoader import TrackViewLoader 
        self.trackViewLoader = TrackViewLoader()

    def tearDown(self):
        gold.util.CompBinManager.COMP_BIN_SIZE = self.prevCompBinSize
        #gold.track.TrackViewLoader.COMP_BIN_SIZE = self.prevCompBinSize        

    def _assertTrackViewLoading_Numbers(self, trackData, start, end):
        trackView = self.trackViewLoader.loadTrackView(trackData, GenomeRegion(genome='TestGenome', start=start, end=end), 'crop', False)
        self.assertListsOrDicts(trackData['val'][start:end], [el.val() for el in trackView])
        self.assertListsOrDicts(trackData['strand'][start:end], [el.strand() for el in trackView])
        self.assertListsOrDicts(trackData['id'][start:end], [el.id() for el in trackView])
        self.assertListsOrDicts(trackData['edges'][start:end], [el.edges() for el in trackView])
        self.assertListsOrDicts(trackData['weights'][start:end], [el.weights() for el in trackView])
        self.assertListsOrDicts(trackData['a'][start:end], [el.a() for el in trackView])
        self.assertListsOrDicts(trackData['b'][start:end], [el.b() for el in trackView])
        
    def _getTrackData_Numbers(self, size):
        id, edges, weights = getRandGraphLists(size, maxNumEdges=10)
        return TrackData({'val': list(getRandValList(size)), \
                          'strand': list(getRandStrandList(size)), \
                          'id': list(id), \
                          'edges': list(edges), \
                          'weights': list(weights), \
                          'a': [str(x) for x in xrange(size)], \
                          'b': [str(x) for x in xrange(size, 0, -1)]})
    
    def testLoadTrackView_Numbers(self):
        trackData = self._getTrackData_Numbers(900)
        self._assertTrackViewLoading_Numbers(trackData, 0, 100)
        self._assertTrackViewLoading_Numbers(trackData, 0, 900)
        self._assertTrackViewLoading_Numbers(trackData, 300, 700)

        self._assertTrackViewLoading_Numbers(trackData, 312, 700)
        self._assertTrackViewLoading_Numbers(trackData, 300, 687)
        self._assertTrackViewLoading_Numbers(trackData, 312, 687)
        
        self._assertTrackViewLoading_Numbers(trackData, 0, 0)
        self._assertTrackViewLoading_Numbers(trackData, 300, 300)
        self._assertTrackViewLoading_Numbers(trackData, 400, 300)
        
        trackData = self._getTrackData_Numbers(891)
        self._assertTrackViewLoading_Numbers(trackData, 800, 880)
        self._assertTrackViewLoading_Numbers(trackData, 800, 891)
        self._assertTrackViewLoading_Numbers(trackData, 700, 880)
        
    def _assertTrackViewLoading_Segments(self, trackData, indexList, start, end):
        trackView = self.trackViewLoader.loadTrackView(trackData, GenomeRegion(genome='TestGenome', start=start, end=end),'crop',False)
        i = -1
        for i,el in enumerate(trackView):
            if i < len(indexList):
                index = indexList[i]
            else:
                self.fail()
            self.assertEqual(max(0, trackData['start'][index] - start), el.start())
            self.assertEqual(min(end, trackData['end'][index]) - start, el.end())
            self.assertAlmostEqual(trackData['val'][index], el.val())
            self.assertEqual(trackData['strand'][index], el.strand())
            self.assertEqual(trackData['id'][index], el.id())
            self.assertListsOrDicts(trackData['edges'][index], el.edges())
            self.assertListsOrDicts(trackData['weights'][index], el.weights())
            self.assertEqual(trackData['a'][index], el.a())
            self.assertEqual(trackData['b'][index], el.b())
            self.assertRaises(AttributeError, lambda: el.leftIndex)
            self.assertRaises(AttributeError, lambda: el.rightIndex)
        self.assertEqual(len(indexList), i+1)
    
    def testLoadTrackView_Segments(self):
        id, edges, weights = getRandGraphLists(4)
        trackData = TrackData({'start' : [10, 210, 260, 410],\
                               'end' : [20, 240, 310, 710],\
                               'val' : list(getRandValList(4)),\
                               'strand' : list(getRandStrandList(4)),\
                               'id': list(id), \
                               'edges': list(edges), \
                               'weights': list(weights), \
                               'a': ['A', 'B', 'C', 'D'], \
                               'b': ['1.0', '2.0', '3.0', '4.0'], \
                               'leftIndex' : [0, 1, 1, 1, 3, 3, 3, 3, 4],\
                               'rightIndex' : [1, 1, 3, 3, 4, 4, 4, 4, 4]})
        
        self._assertTrackViewLoading_Segments(trackData, [0], 0, 100)
        self._assertTrackViewLoading_Segments(trackData, [1, 2], 200, 300)
        self._assertTrackViewLoading_Segments(trackData, [0, 1, 2, 3], 0, 900)
        self._assertTrackViewLoading_Segments(trackData, [2, 3], 300, 700)

        self._assertTrackViewLoading_Segments(trackData, [3], 310, 700)
        self._assertTrackViewLoading_Segments(trackData, [2], 300, 410)
        self._assertTrackViewLoading_Segments(trackData, [], 310, 410)

        self._assertTrackViewLoading_Segments(trackData, [], 0, 0)
        self._assertTrackViewLoading_Segments(trackData, [], 300, 300)
        self._assertTrackViewLoading_Segments(trackData, [], 400, 400)
        
    def runTest(self):
        self.testLoadTrackView_Numbers()