示例#1
0
 def test_downgrade_read_edges_binary(self):
     binary = os.path.join(BAM_BIN_DIR, "downgrade_bam_edge_qual")
     bam_fpath = os.path.join(TEST_DATA_DIR, "sample_rev.bam")
     with NamedTemporaryFile() as out_fhand:
         cmd = [binary, "-o", out_fhand.name, bam_fpath]
         check_call(cmd)
         sam = AlignmentFile(out_fhand.name)
         res = [0, 0]
         read = sam.next()
         assert list(read.query_qualities[:2]) == res
         assert read.get_tag("dl") == "8)5B"
         assert read.get_tag("dr") == "8?>>"
示例#2
0
 def test_downgrade_read_edges_binary(self):
     binary = os.path.join(BAM_BIN_DIR, 'downgrade_bam_edge_qual')
     bam_fpath = os.path.join(TEST_DATA_DIR, 'sample_rev.bam')
     with NamedTemporaryFile() as out_fhand:
         cmd = [binary, '-o', out_fhand.name, bam_fpath]
         check_call(cmd)
         sam = AlignmentFile(out_fhand.name)
         res = [0, 0]
         read = sam.next()
         assert list(read.query_qualities[:2]) == res
         assert read.get_tag('dl') == '8)5B'
         assert read.get_tag('dr') == '8?>>'
示例#3
0
    def test_downngrade_read_edges(self):
        # With softclip
        bam_fpath = os.path.join(TEST_DATA_DIR, 'sample.bam')
        sam = AlignmentFile(bam_fpath)

        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        res = [
            9, 9, 9, 9, 9, 9, 3, 9, 8, 8, 9, 9, 9, 9, 9, 39, 39, 39, 38, 38,
            36, 33, 36, 38, 36, 38, 38, 38, 38, 39, 39, 38, 38, 38, 9, 9, 9, 9
        ]
        assert list(aligned_read.query_qualities) == res

        # without softclip
        sam = AlignmentFile(os.path.join(TEST_DATA_DIR, 'seqs.bam'))

        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        expected = [
            11, 13, 11, 11, 37, 43, 43, 46, 46, 57, 57, 48, 57, 57, 42, 41, 32,
            35, 38, 38, 38, 38, 41, 41, 39, 37, 37, 44, 42, 48, 47, 57, 47, 47,
            48, 47, 57, 57, 54, 48, 57, 48, 54, 50, 50, 50, 50, 50, 57, 59, 54,
            54, 54, 57, 57, 59, 57, 52, 52, 52, 52, 57, 57, 57, 57, 52, 52, 52,
            52, 29, 27, 27, 22
        ]

        assert list(aligned_read.query_qualities) == expected

        # reverse
        # rev seqs (sam specification puts all the alignment query
        # forward(cigar, seq, qual, ...). Reverse is inly noted in the flag
        bam_fpath = os.path.join(TEST_DATA_DIR, 'sample_rev.bam')
        sam = AlignmentFile(bam_fpath)

        aligned_read = sam.next()
        aligned_read = sam.next()
        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        res = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0]
        assert list(aligned_read.query_qualities[:14]) == res
示例#4
0
    def test_downgrade_read_edges_binary(self):
        binary = os.path.join(BAM_BIN_DIR, 'downgrade_bam_edge_qual')
        bam_fpath = os.path.join(TEST_DATA_DIR, 'sample_rev.bam')
        with NamedTemporaryFile() as out_fhand:
            cmd = [binary, '-o', out_fhand.name, bam_fpath]
            check_call(cmd)
            sam = AlignmentFile(out_fhand.name)
            res = [0, 0]
            read = sam.next()
            assert list(read.query_qualities[:2]) == res
            assert read.get_tag('dl') == '8)5B'
            assert read.get_tag('dr') == '8?>>'

        # check bam substitution
        with TemporaryDir() as tmp_dir:
            dirname = tmp_dir.name
            shutil.copy(bam_fpath, dirname)
            bam_fpath = os.path.join(dirname, os.path.basename(bam_fpath))
            cmd = [binary, bam_fpath, '-t', '/home/peio/']
            check_call(cmd)
            sam = AlignmentFile(bam_fpath)
            res = [0, 0]
            read = sam.next()
            assert list(read.query_qualities[:2]) == res
            assert read.get_tag('dl') == '8)5B'
            assert read.get_tag('dr') == '8?>>'

            # we can not downgrade an already downgraded bam
            try:
                cmd = [binary, bam_fpath]
                stderr_fhand = NamedTemporaryFile()
                check_call(cmd, stderr=stderr_fhand)
                self.fail('CalledProcessError expected')
            except CalledProcessError:
                stderr_fhand.flush()
                msg = 'RuntimeError: Edge qualities already downgraded'
                if msg not in open(stderr_fhand.name).read():
                    raise
示例#5
0
    def test_calmd_bam(self):
        ref_fpath = os.path.join(TEST_DATA_DIR, "CUUC00007_TC01.fasta")
        bam_fpath = os.path.join(TEST_DATA_DIR, "sample.bam")
        orig_qual = AlignmentFile(bam_fpath).next().qual
        try:
            out_bam = NamedTemporaryFile()
            calmd_bam(bam_fpath, ref_fpath, out_bam.name)

            samfile = AlignmentFile(out_bam.name)
            calmd_qual = samfile.next().qual
            assert orig_qual != calmd_qual
            assert calmd_qual == "HHHHHHBHGGH!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        finally:
            if os.path.exists(out_bam.name):
                out_bam.close()
示例#6
0
    def test_calmd_bam(self):
        ref_fpath = os.path.join(TEST_DATA_DIR, 'CUUC00007_TC01.fasta')
        bam_fpath = os.path.join(TEST_DATA_DIR, 'sample.bam')
        orig_qual = AlignmentFile(bam_fpath).next().qual
        try:
            out_bam = NamedTemporaryFile()
            calmd_bam(bam_fpath, ref_fpath, out_bam.name)

            samfile = AlignmentFile(out_bam.name)
            calmd_qual = samfile.next().qual
            assert orig_qual != calmd_qual
            assert calmd_qual == 'HHHHHHBHGGH!!!!!!!!!!!!!!!!!!!!!!!!!!!'
        finally:
            if os.path.exists(out_bam.name):
                out_bam.close()
示例#7
0
    def test_downngrade_read_edges(self):
        # With softclip
        bam_fpath = os.path.join(TEST_DATA_DIR, "sample.bam")
        sam = AlignmentFile(bam_fpath)

        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        res = [
            9,
            9,
            9,
            9,
            9,
            9,
            3,
            9,
            8,
            8,
            9,
            9,
            9,
            9,
            9,
            39,
            39,
            39,
            38,
            38,
            36,
            33,
            36,
            38,
            36,
            38,
            38,
            38,
            38,
            39,
            39,
            38,
            38,
            38,
            9,
            9,
            9,
            9,
        ]
        assert list(aligned_read.query_qualities) == res

        # without softclip
        sam = AlignmentFile(os.path.join(TEST_DATA_DIR, "seqs.bam"))

        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        expected = [
            11,
            13,
            11,
            11,
            37,
            43,
            43,
            46,
            46,
            57,
            57,
            48,
            57,
            57,
            42,
            41,
            32,
            35,
            38,
            38,
            38,
            38,
            41,
            41,
            39,
            37,
            37,
            44,
            42,
            48,
            47,
            57,
            47,
            47,
            48,
            47,
            57,
            57,
            54,
            48,
            57,
            48,
            54,
            50,
            50,
            50,
            50,
            50,
            57,
            59,
            54,
            54,
            54,
            57,
            57,
            59,
            57,
            52,
            52,
            52,
            52,
            57,
            57,
            57,
            57,
            52,
            52,
            52,
            52,
            29,
            27,
            27,
            22,
        ]

        assert list(aligned_read.query_qualities) == expected

        # reverse
        # rev seqs (sam specification puts all the alignment query
        # forward(cigar, seq, qual, ...). Reverse is inly noted in the flag
        bam_fpath = os.path.join(TEST_DATA_DIR, "sample_rev.bam")
        sam = AlignmentFile(bam_fpath)

        aligned_read = sam.next()
        aligned_read = sam.next()
        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        res = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0]
        assert list(aligned_read.query_qualities[:14]) == res
示例#8
0
    def test_downgrade_read_edges(self):
        # With softclip
        bam_fpath = os.path.join(TEST_DATA_DIR, 'sample.bam')
        sam = AlignmentFile(bam_fpath)

        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        res = [
            9, 9, 9, 9, 9, 9, 3, 9, 8, 8, 9, 9, 9, 9, 9, 39, 39, 39, 38, 38,
            36, 33, 36, 38, 36, 38, 38, 38, 38, 39, 39, 38, 38, 38, 9, 9, 9, 9
        ]
        assert list(aligned_read.query_qualities) == res

        # without softclip
        sam = AlignmentFile(os.path.join(TEST_DATA_DIR, 'seqs.bam'))

        aligned_read = sam.next()
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        expected = [
            11, 13, 11, 11, 37, 43, 43, 46, 46, 57, 57, 48, 57, 57, 42, 41, 32,
            35, 38, 38, 38, 38, 41, 41, 39, 37, 37, 44, 42, 48, 47, 57, 47, 47,
            48, 47, 57, 57, 54, 48, 57, 48, 54, 50, 50, 50, 50, 50, 57, 59, 54,
            54, 54, 57, 57, 59, 57, 52, 52, 52, 52, 57, 57, 57, 57, 52, 52, 52,
            52, 29, 27, 27, 22
        ]

        assert list(aligned_read.query_qualities) == expected

        # reverse
        # rev seqs (sam specification puts all the alignment query
        # forward(cigar, seq, qual, ...). Reverse is inly noted in the flag
        bam_fpath = os.path.join(TEST_DATA_DIR, 'sample_rev.bam')
        sam = AlignmentFile(bam_fpath)
        aligned_read = sam.next()
        aligned_read = sam.next()
        aligned_read = sam.next()
        original_qual = aligned_read.query_qualities
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        res = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0]
        assert list(aligned_read.query_qualities[:14]) == res

        # check that we can restore the cuals from the tag
        _restore_qual_from_tag(aligned_read)
        assert original_qual == aligned_read.query_qualities

        # only restore left quals
        aligned_read = sam.next()
        original_qual = aligned_read.query_qualities
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        aligned_read.set_tag(RIGTH_DOWNGRADED_TAG, None)
        changed_rquals = aligned_read.query_qualities[-5:]
        _restore_qual_from_tag(aligned_read)
        assert aligned_read.query_qualities[-5:] == changed_rquals
        assert aligned_read.query_qualities[:10] == original_qual[:10]

        # only restore rigth quals
        sam = AlignmentFile(os.path.join(TEST_DATA_DIR, 'seqs.bam'))
        aligned_read = sam.next()
        original_qual = aligned_read.query_qualities
        _downgrade_edge_qualities(aligned_read, size=4, qual_to_substract=30)
        aligned_read.set_tag(LEFT_DOWNGRADED_TAG, None)
        changed_lquals = aligned_read.query_qualities[:5]
        _restore_qual_from_tag(aligned_read)
        assert aligned_read.query_qualities[:5] == changed_lquals
        assert aligned_read.query_qualities[10:] == original_qual[10:]