def test_calc_coverage_4(self):
     """If uniqueley_aligned_only is True skip any mapping of read
     that are aligned to more than on location.
     """
     self.coverage_calculator = CoverageCalculator(
         uniquely_aligned_only=True)
     self._generate_bam_file(self.example_data.sam_content_3,
                             self._sam_bam_prefix)
     self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
     self.coverage_calculator._calc_coverage("chrom", self._bam)
     self.assertListEqual(
         self.coverage_calculator._coverages["forward"][0:15].tolist(),
         [
             3.0,
             3.0,
             3.0,
             3.0,
             3.0,
             3.0,
             3.0,
             3.0,
             3.0,
             3.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
         ],
     )
 def test_calc_coverage_3(self):
     """If read_count_splitting is set to False then every
     mapping is counted as one to each of the matching position
     independent how often its read is mapped in in total.
     """
     self.coverage_calculator = CoverageCalculator(
         read_count_splitting=False)
     self._generate_bam_file(self.example_data.sam_content_2,
                             self._sam_bam_prefix)
     self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
     self.coverage_calculator._calc_coverage("chrom", self._bam)
     self.assertListEqual(
         self.coverage_calculator._coverages["forward"][0:15].tolist(),
         [
             1.0,
             1.0,
             1.0,
             1.0,
             1.0,
             1.0,
             1.0,
             1.0,
             1.0,
             1.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
         ],
     )
 def test_calc_coverage_5(self):
     """If first_base_only is True only the first nucleotide of a
     mapping is considered.
     """
     self.coverage_calculator = CoverageCalculator(
         coverage_style="first_base_only")
     self._generate_bam_file(self.example_data.sam_content_1,
                             self._sam_bam_prefix)
     self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
     self.coverage_calculator._calc_coverage("chrom", self._bam)
     self.assertListEqual(
         self.coverage_calculator._coverages["forward"][0:15].tolist(),
         [
             5.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
         ],
     )
     self.assertListEqual(
         self.coverage_calculator._coverages["reverse"][0:15].tolist(),
         [
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
             -5.0,
             0.0,
             0.0,
             0.0,
             0.0,
             0.0,
         ],
     )
Example #4
0
 def _create_coverage_files_for_lib(self, lib_name, bam_path,
                                    no_of_aligned_reads,
                                    min_no_of_aligned_reads):
     """Perform the coverage calculation for a given library."""
     if not self._args.non_strand_specific:
         strands = ["forward", "reverse"]
     else:
         strands = ["forward_and_reverse"]
     if self._all_coverage_file_exist(lib_name, strands,
                                      no_of_aligned_reads,
                                      min_no_of_aligned_reads):
         return
     read_count_splitting = True
     if self._args.skip_read_count_splitting:
         read_count_splitting = False
     coverage_calculator = CoverageCalculator(
         read_count_splitting=read_count_splitting,
         uniquely_aligned_only=self._args.unique_only,
         coverage_style=self._args.coverage_style,
         clip_length=self._args.clip_length,
         non_strand_specific=self._args.non_strand_specific,
     )
     (
         coverage_writers_raw,
         coverage_writers_tnoar_min_norm,
         coverage_writers_tnoar_mil_norm,
     ) = self._wiggle_writers(lib_name, strands, no_of_aligned_reads,
                              min_no_of_aligned_reads)
     for ref_seq, coverages in coverage_calculator.ref_seq_and_coverages(
             bam_path):
         for strand in strands:
             coverage_writers_raw[strand].write_replicons_coverages(
                 ref_seq, coverages[strand])
             coverage_writers_tnoar_min_norm[
                 strand].write_replicons_coverages(
                     ref_seq,
                     coverages[strand],
                     factor=min_no_of_aligned_reads / no_of_aligned_reads,
                 )
             coverage_writers_tnoar_mil_norm[
                 strand].write_replicons_coverages(
                     ref_seq,
                     coverages[strand],
                     factor=1000000 / no_of_aligned_reads,
                 )
     for strand in strands:
         coverage_writers_raw[strand].close_file()
Example #5
0
 def _create_coverage_files_for_lib(
     self, lib_name, bam_path, no_of_aligned_reads,
         min_no_of_aligned_reads):
     """Perform the coverage calculation for a given library."""
     if not self._args.non_strand_specific:
         strands = ["forward", "reverse"]
     else:
         strands = ["forward_and_reverse"]
     if self._all_coverage_file_exist(
             lib_name, strands, no_of_aligned_reads,
             min_no_of_aligned_reads):
         return
     read_count_splitting = True
     if self._args.skip_read_count_splitting:
         read_count_splitting = False
     coverage_calculator = CoverageCalculator(
         read_count_splitting=read_count_splitting,
         uniquely_aligned_only=self._args.unique_only,
         coverage_style=self._args.coverage_style,
         clip_length=self._args.clip_length,
         non_strand_specific=self._args.non_strand_specific)
     (coverage_writers_raw, coverage_writers_tnoar_min_norm,
      coverage_writers_tnoar_mil_norm) = self._wiggle_writers(
         lib_name, strands, no_of_aligned_reads, min_no_of_aligned_reads)
     for ref_seq, coverages in coverage_calculator.ref_seq_and_coverages(
             bam_path):
         for strand in strands:
             coverage_writers_raw[strand].write_replicons_coverages(
                 ref_seq, coverages[strand])
             coverage_writers_tnoar_min_norm[
                 strand].write_replicons_coverages(
                 ref_seq, coverages[strand],
                 factor=min_no_of_aligned_reads/no_of_aligned_reads)
             coverage_writers_tnoar_mil_norm[
                 strand].write_replicons_coverages(
                 ref_seq, coverages[strand],
                 factor=1000000/no_of_aligned_reads)
     for strand in strands:
         coverage_writers_raw[strand].close_file()
Example #6
0
 def create_coverage_files_TK(
         self, no_of_aligned_reads, min_no_of_aligned_reads):
     if not self._args.non_strand_specific:
         strands = ["forward", "reverse"]
     else:
         strands = ["forward_and_reverse"]
     if self._args.skip_read_count_splitting:
         read_count_splitting = False
     else:
         read_count_splitting = True
     from reademptionlib.coveragecalculator import CoverageCalculator
     coverage_calculator = CoverageCalculator(
         read_count_splitting=read_count_splitting,
         uniquely_aligned_only=self._args.unique_only,
         coverage_style=self._args.coverage_style,
         clip_length=self._args.clip_length,
         non_strand_specific=self._args.non_strand_specific)
     (coverage_writers_raw, coverage_writers_tnoar_min_norm,
      coverage_writers_tnoar_mil_norm) = self._wiggle_writers(
          strands, no_of_aligned_reads, min_no_of_aligned_reads)
     for ref_seq, coverages in coverage_calculator.ref_seq_and_coverages(
             self._args.input_coverage):
         for strand in strands:
             coverage_writers_raw[strand].write_replicons_coverages(
                 ref_seq, coverages[strand])
         for strand in strands:
             coverage_writers_tnoar_min_norm[
                 strand].write_replicons_coverages(
                 ref_seq, coverages[strand],
                 factor=min_no_of_aligned_reads/no_of_aligned_reads)
         for strand in strands:
             coverage_writers_tnoar_mil_norm[
                 strand].write_replicons_coverages(
                 ref_seq, coverages[strand],
                 factor=self._args.factor/no_of_aligned_reads)
     for strand in strands:
         coverage_writers_raw[strand].close_file()
 def test_calc_coverage_4(self):
     """If uniqueley_aligned_only is True skip any mapping of read
     that are aligned to more than on location.
     """
     self.coverage_calculator = CoverageCalculator(
         uniqueley_aligned_only=True)
     self._generate_bam_file(
         self.example_data.sam_content_3, self._sam_bam_prefix)
     self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
     self.coverage_calculator._calc_coverage("chrom", self._bam)
     self.assertListEqual(
         self.coverage_calculator._coverages[
             "forward"][0:15],
         [3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
          0.0, 0.0, 0.0, 0.0, 0.0])
 def test_calc_coverage_3(self):
     """If read_count_splitting is set to False then every
     mapping is counted as one to each of the matching position
     independent how often its read is mapped in in total.
     """
     self.coverage_calculator = CoverageCalculator(
         read_count_splitting=False)
     self._generate_bam_file(
         self.example_data.sam_content_2, self._sam_bam_prefix)
     self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
     self.coverage_calculator._calc_coverage("chrom", self._bam)
     self.assertListEqual(
         self.coverage_calculator._coverages[
             "forward"][0:15],
         [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
          0.0, 0.0, 0.0, 0.0, 0.0])
Example #9
0
def test_calc_coverage_4():
    """If uniqueley_aligned_only is True skip any mapping of read
        that are aligned to more than on location.
    """
    coverage_calculator = CoverageCalculator(uniquely_aligned_only=True)
    bam_file_4 = generate_bam_file(ccd.sam_content_3, ccd.sam_bam_prefix)
    coverage_calculator._init_coverage_list(bam_file_4.lengths[0])
    coverage_calculator._calc_coverage("chrom", bam_file_4)
    assert (coverage_calculator._coverages["forward"][0:15]).all() == (
        np.array([
            3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 0.0, 0.0, 0.0,
            0.0, 0.0
        ])).all()
 def test_calc_coverage_5(self):
     """If first_base_only is True only the first nucleotide of a
     mapping is considered.
     """
     self.coverage_calculator = CoverageCalculator(first_base_only=True)
     self._generate_bam_file(
         self.example_data.sam_content_1, self._sam_bam_prefix)
     self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
     self.coverage_calculator._calc_coverage("chrom", self._bam)
     self.assertListEqual(
         self.coverage_calculator._coverages[
             "forward"][0:15],
             [5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
              0.0, 0.0, 0.0, 0.0, 0.0])
     self.assertListEqual(
         self.coverage_calculator._coverages[
             "reverse"][0:15],
         [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -5.0,
          0.0, 0.0, 0.0, 0.0, 0.0])
Example #11
0
def test_calc_coverage_3():
    """If read_count_splitting is set to False then every
        mapping is counted as one to each of the matching position
        independent how often its read is mapped in in total.
    """
    coverage_calculator = CoverageCalculator(read_count_splitting=False)
    bam_file_3 = generate_bam_file(ccd.sam_content_2, ccd.sam_bam_prefix)
    coverage_calculator._init_coverage_list(bam_file_3.lengths[0])
    coverage_calculator._calc_coverage("chrom", bam_file_3)
    assert (coverage_calculator._coverages["forward"][0:15]).all() == (
        np.array([
            1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0,
            0.0, 0.0
        ])).all()
def data_coverage_calculator():
    coverage_calculator = CoverageCalculator()
    sam_bam_prefix = "dummy"
    sam_content_1 = """@HD	VN:1.0
@SQ	SN:chrom	LN:1500
@SQ	SN:plasmid1	LN:100
@SQ	SN:plasmid2	LN:200
myread:001	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:002	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:003	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:004	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:005	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:006	16	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:007	16	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:008	16	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:009	16	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:010	16	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
"""
    sam_content_2 = """@HD	VN:1.0
@SQ	SN:chrom	LN:1500
@SQ	SN:plasmid1	LN:100
@SQ	SN:plasmid2	LN:200
myread:001	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:2	XI:i:1	XA:Z:Q
"""

    sam_content_3 = """@HD	VN:1.0
@SQ	SN:chrom	LN:1500
@SQ	SN:plasmid1	LN:100
@SQ	SN:plasmid2	LN:200
myread:001	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:002	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:003	0	chrom	1	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:1	XI:i:1	XA:Z:Q
myread:004	0	chrom	5	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:9	XI:i:1	XA:Z:Q
myread:005	0	chrom	5	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:9	XI:i:1	XA:Z:Q
myread:006	0	chrom	5	255	10M	*	0	0	GTGGACAACC	*	NM:i:1	MD:Z:11T3	NH:i:9	XI:i:1	XA:Z:Q
"""
    global coverage_calculator
    global sam_bam_prefix
    global sam_content_1
    global sam_content_2
    global sam_content_3
Example #13
0
def test_calc_coverage_5():
    """If first_base_only is True only the first nucleotide of a
        mapping is considered.
    """
    coverage_calculator = CoverageCalculator(coverage_style="first_base_only")
    bam_file_5 = generate_bam_file(ccd.sam_content_1, ccd.sam_bam_prefix)
    coverage_calculator._init_coverage_list(bam_file_5.lengths[0])
    coverage_calculator._calc_coverage("chrom", bam_file_5)
    assert (coverage_calculator._coverages["forward"][0:15]).all() == (
        np.array([
            5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0
        ])).all()
    assert (coverage_calculator._coverages["reverse"][0:15]).all() == (
        np.array([
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -5.0, 0.0, 0.0, 0.0,
            0.0, 0.0
        ])).all()
class TestCoverageCalculator(unittest.TestCase):

    def setUp(self):
        self.coverage_calculator = CoverageCalculator()
        self.example_data = ExampleData()
        self._sam_bam_prefix = "dummy"

    def tearDown(self):
        for suffix in [".sam", ".bam", ".bam.bai"]:
            if os.path.exists(self._sam_bam_prefix + suffix) is True:
                os.remove(self._sam_bam_prefix + suffix)

    def _generate_bam_file(self, sam_content, file_prefix):
        sam_file = "%s.sam" % file_prefix
        bam_file = "%s.bam" % file_prefix
        sam_fh = open(sam_file, "w")
        sam_fh.write(sam_content)
        sam_fh.close()
        pysam.view("-Sb", "-o%s" % bam_file, sam_file)
        pysam.index(bam_file)
        self._bam = pysam.Samfile(bam_file)

    def test_init_coverage_list(self):
        self.coverage_calculator._init_coverage_list(10)
        self.assertListEqual(
            sorted(self.coverage_calculator._coverages.keys()),
            ["forward", "reverse"])
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"], [0.0] * 10)
        self.assertListEqual(
            self.coverage_calculator._coverages["reverse"], [0.0] * 10)

    def test_calc_coverage_1(self):
        """Check correct start at first list element"""
        self._generate_bam_file(
            self.example_data.sam_content_1, self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertEqual(
            len(self.coverage_calculator._coverages["forward"]), 1500)
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"][0:15],
            [5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0,
             0.0, 0.0, 0.0, 0.0, 0.0])
        self.assertListEqual(
            self.coverage_calculator._coverages["reverse"][0:15],
            [-5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0,
             0.0, 0.0, 0.0, 0.0, 0.0])

    def test_calc_coverage_2(self):
        """Consider how often a read is mapped. Mappings of reads that
        are aligned to several location contribute only fractions to
        the counting.

        """
        self._generate_bam_file(
            self.example_data.sam_content_2, self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages[
                "forward"][0:15],
            [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
             0.0, 0.0, 0.0, 0.0, 0.0])

    def test_calc_coverage_3(self):
        """If read_count_splitting is set to False then every
        mapping is counted as one to each of the matching position
        independent how often its read is mapped in in total.
        """
        self.coverage_calculator = CoverageCalculator(
            read_count_splitting=False)
        self._generate_bam_file(
            self.example_data.sam_content_2, self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages[
                "forward"][0:15],
            [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
             0.0, 0.0, 0.0, 0.0, 0.0])

    def test_calc_coverage_4(self):
        """If uniqueley_aligned_only is True skip any mapping of read
        that are aligned to more than on location.
        """
        self.coverage_calculator = CoverageCalculator(
            uniqueley_aligned_only=True)
        self._generate_bam_file(
            self.example_data.sam_content_3, self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages[
                "forward"][0:15],
            [3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
             0.0, 0.0, 0.0, 0.0, 0.0])

    def test_calc_coverage_5(self):
        """If first_base_only is True only the first nucleotide of a
        mapping is considered.
        """
        self.coverage_calculator = CoverageCalculator(first_base_only=True)
        self._generate_bam_file(
            self.example_data.sam_content_1, self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages[
                "forward"][0:15],
                [5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                 0.0, 0.0, 0.0, 0.0, 0.0])
        self.assertListEqual(
            self.coverage_calculator._coverages[
                "reverse"][0:15],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -5.0,
             0.0, 0.0, 0.0, 0.0, 0.0])
 def setUp(self):
     self.coverage_calculator = CoverageCalculator()
     self.example_data = ExampleData()
     self._sam_bam_prefix = "dummy"
class TestCoverageCalculator(unittest.TestCase):
    def setUp(self):
        self.coverage_calculator = CoverageCalculator()
        self.example_data = ExampleData()
        self._sam_bam_prefix = "dummy"

    def tearDown(self):
        for suffix in [".sam", ".bam", ".bam.bai"]:
            if os.path.exists(self._sam_bam_prefix + suffix) is True:
                os.remove(self._sam_bam_prefix + suffix)

    def _generate_bam_file(self, sam_content, file_prefix):
        sam_file = "{}.sam".format(file_prefix)
        bam_file = "{}.bam".format(file_prefix)
        sam_fh = open(sam_file, "w")
        sam_fh.write(sam_content)
        sam_fh.close()
        pysam.view("-Sb",
                   "-o{}".format(bam_file),
                   sam_file,
                   catch_stdout=False)
        pysam.index(bam_file)
        self._bam = pysam.Samfile(bam_file)

    def test_init_coverage_list(self):
        self.coverage_calculator._init_coverage_list(10)
        self.assertListEqual(
            sorted(self.coverage_calculator._coverages.keys()),
            ["forward", "reverse"],
        )
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"].tolist(),
            [0.0] * 10)
        self.assertListEqual(
            self.coverage_calculator._coverages["reverse"].tolist(),
            [0.0] * 10)

    def test_calc_coverage_1(self):
        """Check correct start at first list element"""
        self._generate_bam_file(self.example_data.sam_content_1,
                                self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertEqual(len(self.coverage_calculator._coverages["forward"]),
                         1500)
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"][0:15].tolist(),
            [
                5.0,
                5.0,
                5.0,
                5.0,
                5.0,
                5.0,
                5.0,
                5.0,
                5.0,
                5.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
        )
        self.assertListEqual(
            self.coverage_calculator._coverages["reverse"][0:15].tolist(),
            [
                -5.0,
                -5.0,
                -5.0,
                -5.0,
                -5.0,
                -5.0,
                -5.0,
                -5.0,
                -5.0,
                -5.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
        )

    def test_calc_coverage_2(self):
        """Consider how often a read is mapped. Mappings of reads that
        are aligned to several location contribute only fractions to
        the counting.

        """
        self._generate_bam_file(self.example_data.sam_content_2,
                                self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"][0:15].tolist(),
            [
                0.5,
                0.5,
                0.5,
                0.5,
                0.5,
                0.5,
                0.5,
                0.5,
                0.5,
                0.5,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
        )

    def test_calc_coverage_3(self):
        """If read_count_splitting is set to False then every
        mapping is counted as one to each of the matching position
        independent how often its read is mapped in in total.
        """
        self.coverage_calculator = CoverageCalculator(
            read_count_splitting=False)
        self._generate_bam_file(self.example_data.sam_content_2,
                                self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"][0:15].tolist(),
            [
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                1.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
        )

    def test_calc_coverage_4(self):
        """If uniqueley_aligned_only is True skip any mapping of read
        that are aligned to more than on location.
        """
        self.coverage_calculator = CoverageCalculator(
            uniquely_aligned_only=True)
        self._generate_bam_file(self.example_data.sam_content_3,
                                self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"][0:15].tolist(),
            [
                3.0,
                3.0,
                3.0,
                3.0,
                3.0,
                3.0,
                3.0,
                3.0,
                3.0,
                3.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
        )

    def test_calc_coverage_5(self):
        """If first_base_only is True only the first nucleotide of a
        mapping is considered.
        """
        self.coverage_calculator = CoverageCalculator(
            coverage_style="first_base_only")
        self._generate_bam_file(self.example_data.sam_content_1,
                                self._sam_bam_prefix)
        self.coverage_calculator._init_coverage_list(self._bam.lengths[0])
        self.coverage_calculator._calc_coverage("chrom", self._bam)
        self.assertListEqual(
            self.coverage_calculator._coverages["forward"][0:15].tolist(),
            [
                5.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
        )
        self.assertListEqual(
            self.coverage_calculator._coverages["reverse"][0:15].tolist(),
            [
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                -5.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ],
        )
 def setUp(self):
     self.coverage_calculator = CoverageCalculator()
     self.example_data = ExampleData()
     self._sam_bam_prefix = "dummy"