Ejemplo n.º 1
0
 def __init__(self, trackName):
     self.trackName = trackName
     self._trackSource = TrackSource()
     self._trackViewLoader = TrackViewLoader()
     self._trackFormatReq = NeutralTrackFormatReq()
     self.formatConverters = None
     self._trackId = None
Ejemplo n.º 2
0
class RandomizedTrack(Track):
    IS_MEMOIZABLE = False

    def __new__(cls, *args, **kwArgs):
        return object.__new__(cls)
    
    def __init__(self, origTrack, origRegion, randIndex, **kwArgs ):
        self._origTrack = origTrack
        self.trackName = origTrack.trackName + ['Randomized', str(randIndex)]        
        self._origRegion = origRegion
        self._trackFormatReq = NeutralTrackFormatReq()
        self._cachedTV = None

    def _checkTrackFormat(self, origTV):
        pass
    
    def getTrackView(self, region):
        #print 'get tv for reg: ',region
        #print str(type(self._origRegion)) + " and " + str(type(region))
        if Config.USE_SLOW_DEFENSIVE_ASSERTS:
            assert (not isIter(self._origRegion) and self._origRegion  == region) or \
                    (isIter(self._origRegion) and region in self._origRegion) 
        
        #if self._cachedTV is None:
        self._origTrack.addFormatReq(self._trackFormatReq)
        origTV = self._origTrack.getTrackView(region)     
        self._checkTrackFormat(origTV)
        assert(not origTV.allowOverlaps)
        assert(origTV.borderHandling == 'crop')
        assert region == origTV.genomeAnchor
        starts, ends, vals, strands, ids, edges, weights, extras = \
            self._createRandomizedNumpyArrays(len(origTV.genomeAnchor), origTV.startsAsNumpyArray(), \
                                              origTV.endsAsNumpyArray(), origTV.valsAsNumpyArray(), \
                                              origTV.strandsAsNumpyArray(), origTV.idsAsNumpyArray(), \
                                              origTV.edgesAsNumpyArray(), origTV.weightsAsNumpyArray(), \
                                              origTV.allExtrasAsDictOfNumpyArrays(), origTV.trackFormat, region)
        
        from gtrackcore_memmap.util.CommonFunctions import getClassName
        self._cachedTV = TrackView(origTV.genomeAnchor, \
                                   (starts + origTV.genomeAnchor.start if starts is not None else None), \
                                   (ends + origTV.genomeAnchor.start if ends is not None else None), \
                                   vals, strands, ids, edges, weights, origTV.borderHandling, origTV.allowOverlaps, extraLists=extras)
        assert self._trackFormatReq.isCompatibleWith(self._cachedTV.trackFormat), 'Incompatible track-format: '\
               + str(self._trackFormatReq) + ' VS ' + str(self._cachedTV.trackFormat)
        return self._cachedTV
        
    def _createRandomizedNumpyArrays(self, binLen, starts, ends, vals, strands, ids, edges, weights, extras, origTrackFormat, region):
        raise AbstractClassError
Ejemplo n.º 3
0
 def __init__(self, origTrack, origRegion, randIndex, **kwArgs ):
     self._origTrack = origTrack
     self.trackName = origTrack.trackName + ['Randomized', str(randIndex)]        
     self._origRegion = origRegion
     self._trackFormatReq = NeutralTrackFormatReq()
     self._cachedTV = None
Ejemplo n.º 4
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(),
            )
        )