Esempio n. 1
0
    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)
Esempio n. 2
0
    def _runMultipleTest(self, t1Starts, t1Ends, t1Ids, t1Edges, t2Starts,
                         t2Ends, t2Ids, t2Edges, t1ExpStarts, t1ExpEnds,
                         t1ExpIds, t1ExpEdges, t2ExpStarts, t2ExpEnds,
                         t2ExpIds, t2ExpEdges, debug=False,
                         expTrackFormatType=None):
        """
        Run test on multiple chromosomes.
        :return:
        """

        chr2 = (GenomeRegion('hg19', 'chr2', 0,
                             GenomeInfo.GENOMES['hg19']['size']['chr2']))

        hg19 = Genome('hg19', GenomeInfo.GENOMES['hg19'])

        t1Starts = np.array(t1Starts)
        t1Ends = np.array(t1Ends)
        t1Ids = np.array(t1Ids)
        t1Edges = np.array(t1Edges)
        t2Starts = np.array(t2Starts)
        t2Ends = np.array(t2Ends)
        t2Ids = np.array(t2Ids)
        t2Edges = np.array(t2Edges)

        tv1 = TrackView(self.chr1, t1Starts, t1Ends, None, None, t1Ids,
                        t1Edges, None, borderHandling='crop',
                        allowOverlaps=False, extraLists=OrderedDict())

        tv2 = TrackView(chr2, t2Starts, t2Ends, None, None, t2Ids, t2Edges,
                        None, borderHandling='crop', allowOverlaps=False,
                        extraLists=OrderedDict())
        tvs = OrderedDict()
        tvs[self.chr1] = tv1
        tvs[chr2] = tv2
        track = TrackContents(hg19, tvs)

        r = RemoveDeadLinks(track, useGlobal=True)

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

        chr1Found = False
        chr2Found = False
        for (k, v) in result.trackViews.iteritems():
            starts = v.startsAsNumpyArray()
            ends = v.endsAsNumpyArray()
            ids = v.idsAsNumpyArray()
            edges = v.edgesAsNumpyArray()

            if cmp(k, self.chr1) == 0:
                if debug:
                    print("newStarts: {}".format(starts))
                    print("t1ExpStarts: {}".format(t1ExpStarts))
                    print("newEnds: {}".format(ends))
                    print("t1ExpEnds: {}".format(t1ExpEnds))
                    print("newIds: {}".format(ids))
                    print("t1ExpIds: {}".format(t1ExpIds))
                    print("newEdges: {}".format(edges))
                    print("t1ExpEdges: {}".format(t1ExpEdges))

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


                self.assertTrue(np.array_equal(starts, t1ExpStarts))
                self.assertTrue(np.array_equal(ends, t1ExpEnds))
                self.assertTrue(np.array_equal(ids, t1ExpIds))
                self.assertTrue(np.array_equal(edges, t1ExpEdges))
                chr1Found = True

            elif cmp(k, chr2) == 0:
                if debug:
                    print("newStarts: {}".format(starts))
                    print("t2ExpStarts: {}".format(t2ExpStarts))
                    print("newEnds: {}".format(ends))
                    print("t2ExpEnds: {}".format(t2ExpEnds))
                    print("newIds: {}".format(ids))
                    print("t2ExpIds: {}".format(t2ExpIds))
                    print("newEdges: {}".format(edges))
                    print("t2ExpEdges: {}".format(t2ExpEdges))

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


                self.assertTrue(np.array_equal(starts, t2ExpStarts))
                self.assertTrue(np.array_equal(ends, t2ExpEnds))
                self.assertTrue(np.array_equal(ids, t2ExpIds))
                self.assertTrue(np.array_equal(edges, t2ExpEdges))
                chr2Found = True
            else:
                # Tests if all tracks no in chr1 have a size of 0.
                self.assertEqual(v.size, 0)

        self.assertTrue(chr1Found)
        self.assertTrue(chr2Found)