def test_remove_duplicates_singlethread(testdir, mock_testclass):
    bam = 'POLR2A.bam'
    output = 'POLR2A-out.bam'
    threads = 1
    subprocess.run = MagicMock(side_effect=[
        create_file, create_file_fixmate, create_file, create_file_markdup,
        create_file
    ])
    fb.remove_duplicates(bam, output, threads)
    subprocess.run.assert_any_call(['samtools', 'sort', '-n', '-o', ANY, bam],
                                   check=True)
    subprocess.run.assert_any_call(['samtools', 'fixmate', '-m', ANY, ANY],
                                   check=True)
    subprocess.run.assert_any_call(['samtools', 'sort', '-o', ANY, ANY],
                                   check=True)
    subprocess.run.assert_any_call(['samtools', 'markdup', '-r', ANY, ANY],
                                   check=True)
    subprocess.run.assert_any_call(['samtools', 'sort', '-o', output, ANY],
                                   check=True)
    assert subprocess.run.call_args_list[0].args[0][
        4] == subprocess.run.call_args_list[1].args[0][3]
    assert subprocess.run.call_args_list[1].args[0][
        4] == subprocess.run.call_args_list[2].args[0][4]
    assert subprocess.run.call_args_list[2].args[0][
        3] == subprocess.run.call_args_list[3].args[0][3]
    assert subprocess.run.call_args_list[3].args[0][
        4] == subprocess.run.call_args_list[3].args[0][4]
def test_filter_bam(testdir, mock_testclass):
    samples = Path(__file__).parent.joinpath('samples.txt')
    fb.filter_bam_sample = MagicMock()
    fb.filter_bam(samples)
    fb.filter_bam_sample.assert_any_call('POLR2A', True, True, None, '', '')
    fb.filter_bam_sample.assert_any_call('ASDURF', True, True, None, '', '')
    fb.filter_bam_sample.assert_any_call('POLR1C', True, True, None, '', '')
def test_sort(testdir, mock_testclass):
    bam = 'POLR2A.bam'
    output = 'POLR2A-out.bam'
    subprocess.run = MagicMock()
    fb.sort(bam, output)
    subprocess.run.assert_any_call(['samtools', 'sort', '-o', output, bam],
                                   check=True)
def test_filter_bam_second(testdir, mock_testclass):
    samples = Path(__file__).parent.joinpath('samples.txt')
    threads = 2
    fb.filter_bam_sample = MagicMock()
    fb.filter_bam(samples, False, True, threads, index=1)
    fb.filter_bam_sample.assert_called_once_with('ASDURF', False, True,
                                                 threads, '', '')
def test_sort_threads(testdir, mock_testclass):
    bam = 'POLR2A.bam'
    output = 'POLR2A-out.bam'
    threads = 3
    subprocess.run = MagicMock()
    fb.sort(bam, output, threads)
    subprocess.run.assert_any_call(
        ['samtools', 'sort', '--threads',
         str(threads - 1), '-o', output, bam],
        check=True)
def test_filter_mapped_paired(testdir, mock_testclass):
    bam = 'POLR2A.bam'
    output = 'POLR2A-out.bam'
    subprocess.run = MagicMock(side_effect=create_file)
    fb.filter_mapped(bam, output, True)
    subprocess.run.assert_any_call(
        ['samtools', 'view', '-b', '-F', '2048', '-f', '2', '-o', ANY, bam],
        check=True)
    subprocess.run.assert_any_call(['samtools', 'sort', '-o', output, ANY],
                                   check=True)
    assert subprocess.run.call_args_list[0].args[0][
        8] == subprocess.run.call_args_list[1].args[0][4]
def test_filter_bam_sample_paired_nodedup(testdir, mock_testclass):
    sample = 'POLR2A'
    bam = sample + '.bam'
    bam_filtered = sample + '-filtered.bam'
    bam_dedup = sample + '-dedup.bam'
    fb.filter_mapped = MagicMock(create_file(['-o', bam_filtered]))
    fb.remove_duplicates = MagicMock()
    fb.filter_bam_sample(sample, True, False)
    fb.filter_mapped.assert_called_with(bam, bam_filtered, True, None)
    fb.remove_duplicates.assert_not_called()
    assert os.path.exists(bam_filtered)
    assert not os.path.exists(bam_dedup)
def test_filter_bam_parameters(testdir, mock_testclass):
    samples = Path(__file__).parent.joinpath('samples.txt')
    threads = 2
    input_suffix = '-i-sacCer'
    output_suffix = '-sacCer'
    fb.filter_bam_sample = MagicMock()
    fb.filter_bam(samples, False, False, threads, input_suffix, output_suffix)
    fb.filter_bam_sample.assert_any_call('POLR2A', False, False, threads,
                                         input_suffix, output_suffix)
    fb.filter_bam_sample.assert_any_call('ASDURF', False, False, threads,
                                         input_suffix, output_suffix)
    fb.filter_bam_sample.assert_any_call('POLR1C', False, False, threads,
                                         input_suffix, output_suffix)
def test_filter_bam_sample_paired_parameters(testdir, mock_testclass):
    sample = 'POLR2A'
    input_suffix = '-i-sacCer'
    output_suffix = '-sacCer'
    bam = sample + input_suffix + '.bam'
    bam_filtered = sample + output_suffix + '-filtered.bam'
    bam_dedup = sample + output_suffix + '-dedup.bam'
    threads = 3
    fb.filter_mapped = MagicMock(create_file(['-o', bam_filtered]))
    fb.remove_duplicates = MagicMock(create_file(['-o', bam_dedup]))
    fb.filter_bam_sample(sample, True, True, threads, input_suffix,
                         output_suffix)
    fb.filter_mapped.assert_called_with(bam, bam_filtered, True, threads)
    fb.remove_duplicates.assert_called_with(bam_filtered, bam_dedup, threads)
    assert os.path.exists(bam_filtered)
    assert os.path.exists(bam_dedup)
def test_filter_mapped_single_threads(testdir, mock_testclass):
    bam = 'POLR2A.bam'
    output = 'POLR2A-out.bam'
    threads = 3
    subprocess.run = MagicMock(side_effect=create_file)
    fb.filter_mapped(bam, output, False, threads)
    subprocess.run.assert_any_call([
        'samtools', 'view', '-b', '-F', '2048', '-F', '4', '--threads',
        str(threads - 1), '-o', ANY, bam
    ],
                                   check=True)
    subprocess.run.assert_any_call(
        ['samtools', 'sort', '--threads',
         str(threads - 1), '-o', output, ANY],
        check=True)
    assert subprocess.run.call_args_list[0].args[0][
        10] == subprocess.run.call_args_list[1].args[0][6]