def testExpandAndUnion(self):

        track1 = createSimpleTestTrackContent(startList=[10], endList=[15])

        track2 = createSimpleTestTrackContent(startList=[18], endList=[30])

        s = Expand(track1, downstream=5, useStrands=False, debug=True)

        u = Union(s, track2, debug=True)

        self._runNestedTest(u, expStarts=[5,18], expEnds=[10,30])
    def testUnionAndTrack(self):
        """
        Test if a union between another union and a track works.
        :return: None
        """
        track1 = createSimpleTestTrackContent(startList=[2], endList=[4])
        track2 = createSimpleTestTrackContent(startList=[6], endList=[8])
        track3 = createSimpleTestTrackContent(startList=[10], endList=[12])

        u1 = Union(track1, track2)
        u2 = Union(u1, track3)

        self._runNestedTest(u2, expStarts=[2, 6, 10], expEnds=[4, 8, 12])
Example #3
0
    def _runTest(self, starts=None, ends=None, strands=None, values=None,
                 ids=None, edges=None, weights=None, expAverage=None,
                 customAverageFunction=None, customChrLength=None,
                 allowOverlap=True, debug=False):

        track = createSimpleTestTrackContent(startList=starts, endList=ends,
                                             valList=values,
                                             strandList=strands,
                                             idList=ids, edgeList=edges,
                                             weightsList=weights,
                                             customChrLength=customChrLength)

        a = AverageLength(track, customAverageFunction=customAverageFunction,
                          debug=debug)

        self.assertTrue((a is not None))
        result = a.calculate()
        self.assertTrue(result is not None)

        resFound = False

        for (k, v) in result.iteritems():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                self.assertTrue(v == expAverage)
                resFound = True

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        self.assertTrue(resFound)
Example #4
0
    def _runTest(self, starts=None, ends=None, strands=None, values=None,
                 ids=None, edges=None, weights=None, expCount=None,
                 customChrLength=None, allowOverlap=True, debug=False):

        track = createSimpleTestTrackContent(startList=starts, endList=ends,
                                             valList=values,
                                             strandList=strands,
                                             idList=ids, edgeList=edges,
                                             weightsList=weights,
                                             customChrLength=customChrLength)

        c = CountElements(track, debug=debug)

        self.assertTrue((c is not None))
        result = c.calculate()
        self.assertTrue(result is not None)

        resFound = False

        for (k, v) in result.iteritems():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                if debug:
                    print("count: {}".format(v))
                    print("expCount: {}".format(expCount))
                self.assertTrue(v == expCount)
                resFound = True

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        self.assertTrue(resFound)
Example #5
0
    def _runTest(self, ids, edges, expIds, expEdges, starts=None,
                 ends=None, values=None, strands=None, weights=None,
                 expStarts=None, expEnds=None, expValues=None, expStrands=None,
                 expWeights=None, expExtras=None, expTrackFormatType=None,
                 trackIdentifier=None, customChrLength=None, debug=False):

        track = createSimpleTestTrackContent(startList=starts, endList=ends,
                                             valList=values,
                                             strandList=strands,
                                             idList=ids, edgeList=edges,
                                             weightsList=weights,
                                             customChrLength=customChrLength)

        u = UniquifyLinks(track, identifier=trackIdentifier, debug=debug)

        result = u.calculate()
        self.assertTrue(result is not None)

        resFound = False

        for (k, v) in result.getTrackViews().iteritems():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                resFound = True

                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newValues = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()
                newWeights = v.weightsAsNumpyArray()
                newExtras = v.allExtrasAsDictOfNumpyArrays()

                if debug:
                    print("newTrackFormatType: {}".format(v.trackFormat))
                    print("expTrackFormatType: {}".format(expTrackFormatType))
                    print("newStarts: {}".format(newStarts))
                    print("expStarts: {}".format(expStarts))
                    print("newEnds: {}".format(newEnds))
                    print("expEnds: {}".format(expEnds))
                    print("newStrands: {}".format(newStrands))
                    print("expStrands: {}".format(expStrands))
                    print("newIds: {}".format(newIds))
                    print("expIds: {}".format(expIds))
                    print("newEdges: {}".format(newEdges))
                    print("expEdges: {}".format(expEdges))
                    print("newWeights: {}".format(newWeights))
                    print("expWeights: {}".format(expWeights))

                if expTrackFormatType is not None:
                    self.assertTrue(v.trackFormat.getFormatName() ==
                                    expTrackFormatType)

                if expEnds is None and expStarts is not None:
                    # Assuming a point type track. Creating the expected ends.
                    expEnds = np.array(expStarts) + 1

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expValues is not None:
                    self.assertTrue(newValues is not None)
                    self.assertTrue(np.array_equal(newValues, expValues))
                else:
                    self.assertTrue(newValues is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    self.assertTrue(newWeights is not None)
                    self.assertTrue(np.array_equal(newWeights, expWeights))
                else:
                    self.assertTrue(newWeights is None)

                if expExtras is not None:
                    for key in expExtras.keys():
                        self.assertTrue(v.hasExtra(key))

                        expExtra = expExtras[key]
                        newExtra = newExtras[key]
                        self.assertTrue(np.array_equal(newExtra, expExtra))
                else:
                    self.assertTrue(len(newExtras) == 0)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        self.assertTrue(resFound)
Example #6
0
    def _runTest(self, starts=None, ends=None, strands=None, values=None,
                 ids=None, edges=None, weights=None, extras=None,
                 expStarts=None, expEnds=None,expValues=None,
                 expStrands=None, expIds=None, expEdges=None,
                 expWeights=None, expExtras=None, expTrackFormatType=None,
                 useStrands=False, treatMissingAsNegative=False,
                 mergeValuesFunction=None, debug=False):

        track = createSimpleTestTrackContent(startList=starts, endList=ends,
                                             strandList=strands,
                                             valList=values, idList=ids,
                                             edgeList=edges,
                                             weightsList=weights)

        m = Merge(track, useStrands=useStrands,
                  treatMissingAsNegative=treatMissingAsNegative,
                  mergeValuesFunction=mergeValuesFunction, debug=debug)

        self.assertTrue((m is not None))

        result = m.calculate()

        resFound = False

        for (k, v) in result.getTrackViews().items():

            newStarts = v.startsAsNumpyArray()
            newEnds = v.endsAsNumpyArray()
            newVals = v.valsAsNumpyArray()
            newStrands = v.strandsAsNumpyArray()
            newIds = v.idsAsNumpyArray()
            newEdges = v.edgesAsNumpyArray()
            newWeights = v.weightsAsNumpyArray()
            #newExtras = v.extrasAsNumpyArray()

            if cmp(k, self.chr1) == 0:

                if expTrackFormatType is not None:
                    points = ['Points', 'Valued points', 'Linked points',
                              'Linked valued points']

                    # Todo fix for segments and partitions
                    if expTrackFormatType in points:
                        # Point type track, we create the expected "virtual"
                        # ends.
                        self.assertTrue(v.trackFormat.getFormatName() == \
                               expTrackFormatType)
                        self.assertTrue(expEnds is None)
                        expEnds = np.array(expStarts) + 1

                # All test tracks are in chr1
                if debug:
                    print("**************************************")
                    print("Result and expected results:")
                    print("expStarts: {}".format(expStarts))
                    print("newStarts: {}".format(newStarts))
                    print("expEnds: {}".format(expEnds))
                    print("newEnds: {}".format(newStarts))
                    print("expStrands: {}".format(expStrands))
                    print("newStrands: {}".format(newStrands))
                    print("expValues: {}".format(expValues))
                    print("newValues: {}".format(newVals))
                    print("expIds: {}".format(expIds))
                    print("newIds: {}".format(newIds))
                    print("expEdges: {}".format(expEdges))
                    print("newEdges: {}".format(newEdges))
                    print("expWeights: {}".format(expWeights))
                    print("newWeights: {}".format(newWeights))
                    print("**************************************")

                resFound = True

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expValues is not None:
                    self.assertTrue(newVals is not None)
                    self.assertTrue(np.array_equal(newVals, expValues))
                else:
                    self.assertTrue(newVals is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    self.assertTrue(newWeights is not None)
                    self.assertTrue(np.array_equal(newWeights, expWeights))
                else:
                    self.assertTrue(newWeights is None)

                #if expExtras is not None:
                #    self.assertTrue(newExtras is not None)
                #    self.assertTrue(np.array_equal(newExtras, expExtras))
                #else:
                #    self.assertTrue(newExtras is None)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                if newStarts is not None:
                    self.assertEqual(newStarts, 0)
                if newEnds is not None:
                    self.assertEqual(newEnds, 0)
                if newStrands is not None:
                    self.assertEqual(newStrands, 0)
                if newVals is not None:
                    self.assertEqual(newVals, 0)
                if newIds is not None:
                    self.assertEqual(newIds, 0)
                if newEdges is not None:
                    self.assertEqual(newEdges, 0)
                if newWeights is not None:
                    self.assertEqual(newWeights, 0)
                #if newExtras is not None:
                #    self.assertEqual(newExtras, 0)

        self.assertTrue(resFound)
Example #7
0
    def _runTest(self, startsA=None, startsB=None, endsA=None, endsB=None,
                 strandsA=None, strandsB=None, valsA=None, valsB=None,
                 idsA=None, idsB=None, edgesA=None, edgesB=None,
                 weightsA=None, weightsB=None, extrasA=None, extrasB=None,
                 expStarts=None, expEnds=None, expStrands=None, expVals=None,
                 expIds=None, expEdges=None, expWeights=None, expExtras=None,
                 resultAllowOverlap=False, useStrands=True,
                 treatMissingAsNegative=True,
                 expTrackFormatType=None, debug=False):

        track1 = createSimpleTestTrackContent(startList=startsA,
                                              endList=endsA, valList=valsA,
                                              strandList=strandsA,
                                              idList=idsA, edgeList=edgesA,
                                              weightsList=weightsA,
                                              extraLists=extrasA)
        track2 = createSimpleTestTrackContent(startList=startsB,
                                              endList=endsB, valList=valsB,
                                              strandList=strandsB,
                                              idList=idsB, edgeList=edgesB,
                                              weightsList=weightsB,
                                              extraLists=extrasB)

        u = Union(track1, track2, useStrands=useStrands,
                  treatMissingAsNegative=treatMissingAsNegative,
                  resultAllowOverlap=resultAllowOverlap)

        tc = u.calculate()

        for (k, v) in tc.getTrackViews().items():
            if cmp(k, self.chr1) == 0:
                # All test tracks are in chr1
                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newVals = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()
                newWeights = v.weightsAsNumpyArray()
                #newExtras = v.extrasAsNumpyArray()

                if expTrackFormatType is not None:
                    points = ['Points', 'Valued points', 'Linked points',
                              'Linked valued points']

                    print(v.trackFormat.getFormatName())
                    assert v.trackFormat.getFormatName() == \
                           expTrackFormatType
                     # Todo fix for segments and partitions

                    if expTrackFormatType in points:
                        print("In points!")
                        # Create the expected ends for a point type track.
                        assert expEnds is None
                        expEnds = np.array(expStarts) + 1

                if debug:
                    print("**************************************")
                    print("Result and expected results:")
                    print("TrackFormat: {}".format(v.trackFormat.getFormatName()))
                    print("expStarts: {}".format(expStarts))
                    print("newStarts: {}".format(v.startsAsNumpyArray()))
                    print("expEnds: {}".format(expEnds))
                    print("newEnds: {}".format(v.endsAsNumpyArray()))
                    print("expStrands: {}".format(expStrands))
                    print("newStrands: {}".format(v.strandsAsNumpyArray()))
                    print("expValues: {}".format(expVals))
                    print("newValues: {}".format(v.valsAsNumpyArray()))
                    print("expIds: {}".format(expIds))
                    print("newIds: {}".format(v.idsAsNumpyArray()))
                    print("expEdges: {}".format(expEdges))
                    print("newEdges: {}".format(v.edgesAsNumpyArray()))
                    print("**************************************")
                # All test tracks are in chr1

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expVals is not None:
                    self.assertTrue(newVals is not None)
                    self.assertTrue(np.array_equal(newVals, expVals))
                else:
                    self.assertTrue(newVals is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    self.assertTrue(newWeights is not None)
                    self.assertTrue(np.array_equal(newWeights, expWeights))
                else:
                    self.assertTrue(newWeights is None)

                #if expExtras is not None:
                #    self.assertTrue(newExtras is not None)
                #    self.assertTrue(np.array_equal(newExtras, expExtras))
                #else:
                #    self.assertTrue(newExtras is None)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.startsAsNumpyArray().size, 0)
                self.assertEqual(v.endsAsNumpyArray().size, 0)
Example #8
0
    def _runTest(
        self,
        starts=None,
        ends=None,
        values=None,
        strands=None,
        ids=None,
        edges=None,
        weights=None,
        expStarts=None,
        expEnds=None,
        expValues=None,
        expStrands=None,
        expIds=None,
        expEdges=None,
        expWeights=None,
        expExtras=None,
        customChrLength=None,
        allowOverlap=True,
        resultAllowOverlap=False,
        downstream=None,
        upstream=None,
        both=None,
        useFraction=False,
        useStrands=False,
        treatMissingAsNegative=False,
        debug=False,
        expTrackFormatType=None,
    ):

        track = createSimpleTestTrackContent(
            startList=starts,
            endList=ends,
            valList=values,
            strandList=strands,
            idList=ids,
            edgeList=edges,
            weightsList=weights,
            customChrLength=customChrLength,
        )

        s = Expand(
            track,
            both=both,
            downstream=downstream,
            upstream=upstream,
            useFraction=useFraction,
            useStrands=useStrands,
            treatMissingAsNegative=treatMissingAsNegative,
            resultAllowOverlap=resultAllowOverlap,
            debug=debug,
        )

        result = s.calculate()
        self.assertTrue(result is not None)

        resFound = False

        for (k, v) in result.getTrackViews().iteritems():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                resFound = True

                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newValues = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()
                newWeights = v.weightsAsNumpyArray()
                newExtras = v.allExtrasAsDictOfNumpyArrays()

                if debug:
                    print("expTrackFormat: {}".format(expTrackFormatType))
                    print("newTrackFormat: {}".format(v.trackFormat.getFormatName()))
                    print("newStarts: {}".format(newStarts))
                    print("expStarts: {}".format(expStarts))
                    print("newEnds: {}".format(newEnds))
                    print("expEnds: {}".format(expEnds))
                    print("newStrands: {}".format(newStrands))
                    print("expStrands: {}".format(expStrands))
                    print("newIds: {}".format(newIds))
                    print("expIds: {}".format(expIds))
                    print("newEdges: {}".format(newEdges))
                    print("expEdges: {}".format(expEdges))

                if expTrackFormatType is not None:
                    # Check that the track is of the expected type.
                    self.assertTrue(v.trackFormat.getFormatName() == expTrackFormatType)

                if expEnds is None:
                    # Assuming a point type track. Creating the expected ends.
                    expEnds = np.array(expStarts) + 1

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expValues is not None:
                    self.assertTrue(newValues is not None)
                    self.assertTrue(np.array_equal(newValues, expValues))
                else:
                    self.assertTrue(newValues is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    self.assertTrue(newWeights is not None)
                    self.assertTrue(np.array_equal(newWeights, expWeights))
                else:
                    self.assertTrue(newWeights is None)

                # if expExtras is not None:
                #    self.assertTrue(newExtras is not None)
                #    self.assertTrue(np.array_equal(newExtras, expExtras))
                # else:
                #    self.assertTrue(newExtras is None)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        self.assertTrue(resFound)
Example #9
0
    def _runTest(self, starts=None, ends=None, values=None, strands=None,
                 ids=None, edges=None, weights=None, expStarts=None,
                 expEnds=None, expValues=None, expStrands=None, expIds=None,
                 expEdges=None, expWeights=None, customChrLength=None,
                 removeStrands=False, removeValues=False, removeLinks=False,
                 removeWeights=False, removeExtras=False,
                 debug=False, expTrackFormatType=None):

        track = createSimpleTestTrackContent(startList=starts, endList=ends,
                                             valList=values,
                                             strandList=strands,
                                             idList=ids, edgeList=edges,
                                             weightsList=weights,
                                             customChrLength=customChrLength)

        f = Filter(track, removeStrands=removeStrands,
                   removeValues=removeValues, removeLinks=removeLinks,
                   removeWeights=removeWeights, removeExtras=removeExtras,
                   debug=debug)

        result = f.calculate()
        self.assertTrue(result is not None)

        resFound = False

        for (k, v) in result.getTrackViews().iteritems():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                resFound = True

                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newValues = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()
                newWeights = v.weightsAsNumpyArray()
                #newExtras = v.extrasAsNumpyArray()

                if debug:
                    print("newStarts: {}".format(newStarts))
                    print("expStarts: {}".format(expStarts))
                    print("newEnds: {}".format(newEnds))
                    print("expEnds: {}".format(expEnds))
                    print("newStrands: {}".format(newStrands))
                    print("expStrands: {}".format(expStrands))
                    print("newIds: {}".format(newIds))
                    print("expIds: {}".format(expIds))
                    print("newEdges: {}".format(newEdges))
                    print("expEdges: {}".format(expEdges))

                if expTrackFormatType is not None:
                    # Check that the track is of the expected type.
                    print(expTrackFormatType)
                    print(v.trackFormat.getFormatName())
                    self.assertTrue(v.trackFormat.getFormatName() ==
                                    expTrackFormatType)

                if expEnds is None:
                    # Assuming a point type track. Creating the expected ends.
                    expEnds = np.array(expStarts) + 1

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expValues is not None:
                    self.assertTrue(newValues is not None)
                    self.assertTrue(np.array_equal(newValues, expValues))
                else:
                    self.assertTrue(newValues is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    self.assertTrue(newWeights is not None)
                    self.assertTrue(np.array_equal(newWeights, expWeights))
                else:
                    self.assertTrue(newWeights is None)

                #if expExtras is not None:
                #    self.assertTrue(newExtras is not None)
                #    self.assertTrue(np.array_equal(newExtras, expExtras))
                #else:
                #    self.assertTrue(newExtras is None)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        self.assertTrue(resFound)
Example #10
0
    def _runTest(self, startsA=None, startsB=None, endsA=None, endsB=None,
                 strandsA=None, strandsB=None, valsA=None, valsB=None,
                 idsA=None, idsB=None, edgesA=None, edgesB=None,
                 weightsA=None, weightsB=None, extrasA=None, extrasB=None,
                 expStarts=None, expEnds=None, expStrands=None, expVals=None,
                 expIds=None, expEdges=None, expWeights=None, expExtras=None,
                 expNoResult=False, expTrackFormatType=None,
                 customChrLength=None, resultAllowOverlap=False,
                 useStrands=True, treatMissingAsNegative=False, debug=False):

        track1 = createSimpleTestTrackContent(startList=startsA,
                                              endList=endsA, valList=valsA,
                                              strandList=strandsA,
                                              idList=idsA, edgeList=edgesA,
                                              weightsList=weightsA,
                                              extraLists=extrasA,
                                              customChrLength=customChrLength)

        track2 = createSimpleTestTrackContent(startList=startsB,
                                              endList=endsB, valList=valsB,
                                              strandList=strandsB,
                                              idList=idsB, edgeList=edgesB,
                                              weightsList=weightsB,
                                              extraLists=extrasB,
                                              customChrLength=customChrLength)

        s = Subtract(track1, track2, resultAllowOverlap=resultAllowOverlap,
                     useStrands=useStrands,
                     treatMissingAsNegative=treatMissingAsNegative,
                     debug=debug)

        tc = s.calculate()

        resFound = False

        for (k, v) in tc.getTrackViews().items():
            if cmp(k, self.chr1) == 0:
                # All test tracks are in chr1
                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newVals = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()
                newWeights = v.weightsAsNumpyArray()
                newExtras = v.allExtrasAsDictOfNumpyArrays()

                resFound = True

                if expEnds is None:
                    # Assuming a point type track. Creating the expected ends.
                    expEnds = np.array(expStarts) + 1

                if debug:
                    print("**********************")
                    print("DEBUG: Subtract result")
                    print("expTrackFormatType: {}".format(expTrackFormatType))
                    print("newTrackFormatType: {}".format(
                        v.trackFormat.getFormatName()))
                    print("expStarts: {}".format(expStarts))
                    print("newStarts: {}".format(newStarts))
                    print("expEnds: {}".format(expEnds))
                    print("newEnds: {}".format(newEnds))
                    print("expStrands: {}".format(expStrands))
                    print("newStrands: {}".format(newStrands))
                    print("expVals:: {}".format(expVals))
                    print("newVals:: {}".format(newVals))
                    print("expIds: {}".format(expIds))
                    print("newIds: {}".format(newIds))
                    print("expEdges: {}".format(expEdges))
                    print("newEdges: {}".format(newEdges))
                    print("expeights: {}".format(expWeights))
                    print("newWeights: {}".format(newWeights))
                    print("**********************")

                if expTrackFormatType is not None:
                    # Check that the track is of the expected type.
                    self.assertTrue(v.trackFormat.getFormatName() ==
                                    expTrackFormatType)

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expVals is not None:
                    self.assertTrue(newVals is not None)
                    self.assertTrue(np.array_equal(newVals, expVals))
                else:
                    self.assertTrue(newVals is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    self.assertTrue(newWeights is not None)
                    self.assertTrue(np.array_equal(newWeights, expWeights))
                else:
                    self.assertTrue(newWeights is None)

                if expExtras is not None:
                    for key in expExtras.keys():
                        self.assertTrue(v.hasExtra(key))

                        expExtra = expExtras[key]
                        newExtra = newExtras[key]
                        self.assertTrue(np.array_equal(newExtra, expExtra))
                else:
                    self.assertTrue(len(newExtras) == 0)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.startsAsNumpyArray().size, 0)
                self.assertEqual(v.endsAsNumpyArray().size, 0)

        if expNoResult:
            self.assertFalse(resFound)
        else:
            self.assertTrue(resFound)
    def _runTest(self, starts=None, ends=None, values=None, strands=None,
                 ids=None, edges=None, weights=None, newId=None,
                 expStarts=None, expEnds=None, expValues=None,
                 expStrands=None, expIds=None, expEdges=None,
                 expWeights=None, expExtras=None, customChrLength=None,
                 allowOverlap=True, resultAllowOverlap=False,
                 expTrackFormatType=None, debug=False):

        track = createSimpleTestTrackContent(startList=starts, endList=ends,
                                             valList=values,
                                             strandList=strands,
                                             idList=ids, edgeList=edges,
                                             weightsList=weights,
                                             customChrLength=customChrLength)

        r = RemoveDeadLinks(track, newId=newId,
                            resultAllowOverlap=resultAllowOverlap,
                            debug=debug)

        result = r.calculate()
        self.assertTrue(result is not None)

        resFound = False

        for (k, v) in result.getTrackViews().iteritems():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                resFound = True

                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newValues = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()
                newWeights = v.weightsAsNumpyArray()
                newExtras = v.allExtrasAsDictOfNumpyArrays()

                if debug:
                    print("newTrackFormatType: {}".format(v.trackFormat))
                    print("expTrackFormatType: {}".format(expTrackFormatType))
                    print("newStarts: {}".format(newStarts))
                    print("expStarts: {}".format(expStarts))
                    print("newEnds: {}".format(newEnds))
                    print("expEnds: {}".format(expEnds))
                    print("newStrands: {}".format(newStrands))
                    print("expStrands: {}".format(expStrands))
                    print("newIds: {}".format(newIds))
                    print("expIds: {}".format(expIds))
                    print("newEdges: {}".format(newEdges))
                    print("expEdges: {}".format(expEdges))
                    print("newWeights: {}".format(newWeights))
                    print("expWeights: {}".format(expWeights))

                if expTrackFormatType is not None:
                    self.assertTrue(v.trackFormat.getFormatName() ==
                                    expTrackFormatType)

                if expEnds is None and expStarts is not None:
                    # Assuming a point type track. Creating the expected ends.
                    expEnds = np.array(expStarts) + 1

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expValues is not None:
                    self.assertTrue(newValues is not None)
                    self.assertTrue(np.array_equal(newValues, expValues))
                else:
                    self.assertTrue(newValues is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    # As weights can contain numpy.nan, we can not use the
                    # normal array_equal method.
                    # (np.nan == np.nan) == False
                    # Using the assert_equal instead which.

                    self.assertTrue(newWeights is not None)
                    try:
                        np.testing.assert_equal(newWeights, expWeights)
                    except AssertionError:
                        self.fail("Weights are not equal")
                else:
                    self.assertTrue(newWeights is None)

                if expExtras is not None:
                    for key in expExtras.keys():
                        self.assertTrue(v.hasExtra(key))

                        expExtra = expExtras[key]
                        newExtra = newExtras[key]
                        self.assertTrue(np.array_equal(newExtra, expExtra))
                else:
                    self.assertTrue(len(newExtras) == 0)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        self.assertTrue(resFound)
Example #12
0
    def _runTest(self, t1Starts=None, t2Starts=None, t1Ends=None, t2Ends=None,
                 t1Strands=None, t2Strands=None, t1Vals=None, t2Vals=None,
                 t1Ids=None, t2Ids=None, t1Edges=None, t2Edges=None,
                 t1Weights=None, t2Weights=None, expStarts=None, expEnds=None,
                 expValues=None, expStrands=None, expIds=None, expEdges=None,
                 expWeights=None, expNoResult=False, customChrLength=None,
                 resultAllowOverlap=True,
                 debug=False, expTrackFormatType=None):

        t1 = createSimpleTestTrackContent(startList=t1Starts, endList=t1Ends,
                                          valList=t1Vals, strandList=t1Strands,
                                          idList=t1Ids, edgeList=t1Edges,
                                          weightsList=t1Weights,
                                          customChrLength=customChrLength)

        t2 = createSimpleTestTrackContent(startList=t2Starts, endList=t2Ends,
                                          valList=t2Vals, strandList=t2Strands,
                                          idList=t2Ids, edgeList=t2Edges,
                                          weightsList=t2Weights,
                                          customChrLength=customChrLength)

        i = Intersect(t1, t2, resultAllowOverlap=resultAllowOverlap,
                      debug=debug)

        result = i.calculate()
        self.assertTrue(result is not None)

        print(result.trackViews)

        resFound = False

        for (k, v) in result.trackViews.iteritems():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                resFound = True

                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newValues = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()
                newWeights = v.weightsAsNumpyArray()
                #newExtras = v.extrasAsNumpyArray()

                if debug:
                    print("expFormatName: {}".format(expTrackFormatType))
                    print("newFormatName: {}".format(v.trackFormat.getFormatName()))
                    print("newStarts: {}".format(newStarts))
                    print("expStarts: {}".format(expStarts))
                    print("newEnds: {}".format(newEnds))
                    print("expEnds: {}".format(expEnds))
                    print("newStrands: {}".format(newStrands))
                    print("expStrands: {}".format(expStrands))
                    print("newIds: {}".format(newIds))
                    print("expIds: {}".format(expIds))
                    print("newEdges: {}".format(newEdges))
                    print("expEdges: {}".format(expEdges))

                if expTrackFormatType is not None:
                    # Check that the track is of the expected type.
                    self.assertTrue(v.trackFormat.getFormatName() ==
                                    expTrackFormatType)

                if expEnds is None:
                    # Assuming a point type track. Creating the expected ends.
                    expEnds = np.array(expStarts) + 1

                if expStarts is not None:
                    self.assertTrue(newStarts is not None)
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                else:
                    self.assertTrue(newStarts is None)

                if expEnds is not None:
                    self.assertTrue(newEnds is not None)
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                else:
                    self.assertTrue(newEnds is None)

                if expValues is not None:
                    self.assertTrue(newValues is not None)
                    self.assertTrue(np.array_equal(newValues, expValues))
                else:
                    self.assertTrue(newValues is None)

                if expStrands is not None:
                    self.assertTrue(newStrands is not None)
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                else:
                    self.assertTrue(newStrands is None)

                if expIds is not None:
                    print("newIds: {}".format(newIds))
                    print("expIds: {}".format(expIds))
                    self.assertTrue(newIds is not None)
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(newIds is None)

                if expEdges is not None:
                    print("newEdges: {}".format(newEdges))
                    print("expEdges: {}".format(expEdges))
                    self.assertTrue(newEdges is not None)
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(newEdges is None)

                if expWeights is not None:
                    self.assertTrue(newWeights is not None)
                    self.assertTrue(np.array_equal(newWeights, expWeights))
                else:
                    self.assertTrue(newWeights is None)

                #if expExtras is not None:
                #    self.assertTrue(newExtras is not None)
                #    self.assertTrue(np.array_equal(newExtras, expExtras))
                #else:
                #    self.assertTrue(newExtras is None)

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        if expNoResult:
            self.assertFalse(resFound)
        else:
            self.assertTrue(resFound)
Example #13
0
    def _runTest(self, values, expValues, limit, starts=None, ends=None,
                 expStarts=None, expEnds=None, strands=None, ids=None,
                 edges=None, expStrands=None, expIds=None, expEdges=None,
                 compareFunction=None, customChrLength=None,
                 allowOverlap=True, debug=False):

        track = createSimpleTestTrackContent(startList=starts, endList=ends,
                                             valList=values, idList=ids,
                                             edgeList=edges,
                                             customChrLength=customChrLength)

        vs = ValueSelect(track, limit=limit, compareFunction=compareFunction,
                         allowOverlap=allowOverlap, debug=debug)

        self.assertTrue((vs is not None))
        result = vs.calculate()
        self.assertTrue(result is not None)

        resFound = False

        if len(expValues) == 0:
            resFound = True

        for (k, v) in result.getTrackViews().items():
            if cmp(k, self.chr1) == 0 or cmp(k, self.chr1Small) == 0:
                # All test tracks are in chr1
                newStarts = v.startsAsNumpyArray()
                newEnds = v.endsAsNumpyArray()
                newVals = v.valsAsNumpyArray()
                newStrands = v.strandsAsNumpyArray()
                newIds = v.idsAsNumpyArray()
                newEdges = v.edgesAsNumpyArray()

                if debug:
                    print("newStarts: {}".format(newStarts))
                    print("newEnds: {}".format(newEnds))
                    print("newVals: {}".format(newVals))
                    print("expStarts: {}".format(expStarts))
                    print("expEnds: {}".format(expEnds))
                    print("expValues: {}".format(expValues))
                if newStarts is not None and starts is not None:
                    self.assertTrue(np.array_equal(newStarts, expStarts))
                if newEnds is not None and ends is not None:
                    self.assertTrue(np.array_equal(newEnds, expEnds))
                if newVals is not None:
                    self.assertTrue(np.array_equal(newVals, expValues))
                else:
                    self.assertTrue(expValues is None)
                if strands is not None:
                    self.assertTrue(np.array_equal(newStrands, expStrands))
                if newIds is not None:
                    self.assertTrue(np.array_equal(newIds, expIds))
                else:
                    self.assertTrue(expIds is None)
                if newEdges is not None:
                    self.assertTrue(np.array_equal(newEdges, expEdges))
                else:
                    self.assertTrue(expEdges is None)

                resFound = True

            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.startsAsNumpyArray().size, 0)
                self.assertEqual(v.endsAsNumpyArray().size, 0)
                self.assertEqual(v.vasAsNumpyArray().size, 0)
                self.assertEqual(v.idsAsNumpyArray().size, 0)
                self.assertEqual(v.edgesAsNumpyArray().size, 0)

        self.assertTrue(resFound)