class TestIndexedBam(_IndexedAlnFileReaderTests):

    READER_CONSTRUCTOR = IndexedBamReader
    CONSTRUCTOR_ARGS = (data.getAlignedBam(), data.getLambdaFasta())

    def test_empty_bam(self):
        fn = data.getEmptyBam()
        bam = IndexedBamReader(fn)
        assert len(bam) == 0

    def test_alignment_identity(self):
        """
        Check that the values of the 'identity' property are consistent
        between IndexedBamReader (numpy array) and BamAlignment (float)
        """
        fn = data.getAlignedBam()
        with IndexedBamReader(fn) as bam_in:
            i1 = bam_in.identity
            i2 = np.array([rec.identity for rec in bam_in])
            assert (i2 == i1).all()

    def test_alignment_identity_unindexed(self):
        """
        Check that the value of the 'identity' property is the same whether
        or not the .pbi index was used to calculate it.
        """
        fn1 = data.getAlignedBam()
        fn2 = tempfile.NamedTemporaryFile(suffix=".bam").name
        shutil.copyfile(fn1, fn2)
        with IndexedBamReader(fn1) as bam_pbi:
            with BamReader(fn2) as bam_noindex:
                i1 = np.array([rec.identity for rec in bam_pbi])
                i2 = np.array([rec.identity for rec in bam_noindex])
                assert (i2 == i1).all()
 def test_alignment_identity(self):
     """
     Check that the values of the 'identity' property are consistent
     between IndexedBamReader (numpy array) and BamAlignment (float)
     """
     fn = data.getAlignedBam()
     with IndexedBamReader(fn) as bam_in:
         i1 = bam_in.identity
         i2 = np.array([rec.identity for rec in bam_in])
         assert (i2 == i1).all()
 def test_alignment_identity_unindexed(self):
     """
     Check that the value of the 'identity' property is the same whether
     or not the .pbi index was used to calculate it.
     """
     fn1 = data.getAlignedBam()
     fn2 = tempfile.NamedTemporaryFile(suffix=".bam").name
     shutil.copyfile(fn1, fn2)
     with IndexedBamReader(fn1) as bam_pbi:
         with BamReader(fn2) as bam_noindex:
             i1 = np.array([rec.identity for rec in bam_pbi])
             i2 = np.array([rec.identity for rec in bam_noindex])
             assert (i2 == i1).all()
class TestBasicBam(_BasicAlnFileReaderTests):

    READER_CONSTRUCTOR = BamReader
    CONSTRUCTOR_ARGS = (data.getAlignedBam(), data.getLambdaFasta())

    def testSpecVersion(self):
        assert "3.0.1" == self.f.version

    def testReadScore(self):
        assert 0.904 == pytest.approx(self.fwdAln.readScore)

    def test_sample_name_default(self):
        assert "UnnamedSample" == self.f.readGroupTable[0].SampleName
    def test_movie_filter(self):
        # unaligned bam
        bam0 = ("/pbi/dept/secondary/siv/testdata/"
                "SA3-DS/ecoli/2590956/0003/"
                "Analysis_Results/m140913_222218_42240_c10069"
                "9952400000001823139203261564_s1_p0.all.subreadset.xml")
        bam1 = ("/pbi/dept/secondary/siv/testdata/"
                "SA3-DS/ecoli/2590953/0001/"
                "Analysis_Results/m140913_005018_42139_c10071"
                "3652400000001823152404301534_s1_p0.all.subreadset.xml")
        aln = SubreadSet(bam0, bam1)
        assert len(set(aln.readGroupTable['ID'])) == len(
            aln.readGroupTable['ID'])
        assert len(set(aln.readGroupTable['ID'])) == 2
        assert len(set(aln.readGroupTable['ID'])) == len(set(aln.index.qId))
        assert len(aln) == 178570
        aln.filters.addRequirement(movie=[(
            '=',
            'm140913_005018_42139_c100713652400000001823152404301534_s1_p0')])
        assert len(SubreadSet(bam1)) == len(aln)

        # aligned bam
        # bam0 = ("/pbi/dept/secondary/siv/testdata/"
        #        "SA3-DS/ecoli/2590956/0003/Alignment_Results/"
        #        "m140913_222218_42240_c1006999524000000018231"
        #        "39203261564_s1_p0.all.alignmentset.xml")
        bam0 = upstreamdata.getAlignedBam()
        bam1 = ("/pbi/dept/secondary/siv/testdata/"
                "SA3-DS/ecoli/2590953/0001/Alignment_Results/"
                "m140913_005018_42139_c1007136524000000018231"
                "52404301534_s1_p0.all.alignmentset.xml")
        aln = AlignmentSet(bam0, bam1)
        assert len(set(aln.readGroupTable['ID'])) == len(
            aln.readGroupTable['ID'])
        assert len(set(aln.readGroupTable['ID'])) == 2
        assert len(set(aln.readGroupTable['ID'])) == len(set(aln.index.qId))
        assert len(aln) == 103144
        aln.filters.addRequirement(movie=[(
            '=',
            'm140913_005018_42139_c100713652400000001823152404301534_s1_p0')])
        assert len(AlignmentSet(bam1)) == len(aln)