Skip to content

Commit

Permalink
Expand mutect2 parallel tests to snakemake preamble
Browse files Browse the repository at this point in the history
  • Loading branch information
eudesbarbosa committed Aug 8, 2022
1 parent d32ca44 commit 7bf81fb
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions tests/snappy_wrappers/wrappers/test_mutect2_par_run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# -*- coding: utf-8 -*-
"""Code for testing mutect2_par/run wrapper"""
from importlib.machinery import SourceFileLoader
import importlib.util
import inspect
import os
from pathlib import Path
import sys
import tempfile
import textwrap

import pytest
Expand Down Expand Up @@ -142,6 +148,38 @@ def snakemake_obj(minimal_config, snakemake_output_dict):
)


@pytest.fixture
def construct_preamble_module(snakemake_obj, variant_caller_fake_fs, mocker):
"""Returns ParallelMutect2Wrapper.construct_preamble() as a module"""
# Patch out file-system
patch_module_fs("snappy_wrappers.wrapper_parallel", variant_caller_fake_fs, mocker)

# Get methods as string
wrapper_par = ParallelMutect2Wrapper(snakemake=snakemake_obj)
preamble_str = wrapper_par.construct_preamble()
print(preamble_str)

# Remove Snakemake syntax
clear_preamble_str = ""
for line in preamble_str.split("\n"):
if "rule all:" in line:
continue
elif "input: **" in line:
continue
elif "shell." in line:
continue
clear_preamble_str += line + "\n"

# Push content to temp file
tmp = tempfile.NamedTemporaryFile(suffix=".py")
with open(tmp.name, "w") as f:
f.write(clear_preamble_str)

# Load as module
module_name = os.path.basename(tmp.name).replace(".py", "")
return SourceFileLoader(module_name, tmp.name).load_module()


def test_mutect2_wrapper_run_construct_merge_rule(snakemake_obj, variant_caller_fake_fs, mocker):
"""Tests ParallelMutect2Wrapper.construct_merge_rule()"""
# Patch out file-system
Expand All @@ -154,3 +192,139 @@ def test_mutect2_wrapper_run_construct_merge_rule(snakemake_obj, variant_caller_
# Get actual and assert
actual = wrapper_par.construct_merge_rule()
assert actual == expected


def test_mutect2_wrapper_run_preamble_multiply_time(construct_preamble_module):
"""Tests Parallel Preamble multiply_time()"""
# Constant factor
factor = 10
# Define (input, expected) pair
input_expected_pairs = (
("00:01:00", "0-0:10:0"),
("01:00:00", "0-10:0:0"),
("12:00:00", "0-120:0:0"), # TODO: Should instead return "5-00:00:00"
("01-00:00:00", "1-240:0:0"), # TODO: Should instead return "10-00:00:00"
)

# Test all pairs
for pair in input_expected_pairs:
_input = pair[0]
_expected = pair[1]
actual = construct_preamble_module.multiply_time(day_time_str=_input, factor=factor)
msg = (
f"For input '{_input}' * {factor} expected output '{_expected}'. "
f"Received instead: {actual}"
)
assert actual == _expected, msg

# Test invalid time
with pytest.raises(ValueError):
construct_preamble_module.multiply_time(day_time_str="_not_a_valid_time", factor=factor)


def test_mutect2_wrapper_run_preamble_multiply_memory(construct_preamble_module):
"""Tests Parallel Preamble multiply_memory()"""
# Constant factor
factor = 10
# Define (input, expected) pair
input_expected_pairs = (
("1024k", 10),
("1M", 10),
("16G", 160000),
("1T", 10000000),
("12", 120),
)
# Test all pairs
for pair in input_expected_pairs:
_input = pair[0]
_expected = pair[1]
actual = construct_preamble_module.multiply_memory(memory_str=_input, factor=factor)
msg = (
f"For input '{_input}' * {factor} expected output '{_expected}'. "
f"Received instead: {actual}"
)
assert actual == _expected, msg


def test_mutect2_wrapper_run_preamble_resource_chunk_threads(construct_preamble_module):
"""Tests Parallel Preamble resource_chunk_threads() - Chunks always get a single thread"""
expected = 1
actual = construct_preamble_module.resource_chunk_threads(wildcards=None)
assert actual == expected


def test_mutect2_wrapper_run_preamble_resource_chunk_memory(construct_preamble_module):
"""
Tests Parallel Preamble resource_chunk_memory() - Baseline memory defined in Snakemake object
"""
# Define (input, expected) pair
input_expected_pairs = (
(1, 28000),
(2, 56000),
(3, 84000),
(4, 112000),
(5, 140000),
)
# Test all pairs
for pair in input_expected_pairs:
_input = pair[0]
_expected = pair[1]
actual = construct_preamble_module.resource_chunk_memory(wildcards=None, attempt=_input)
msg = f"For input '{_input}' expected output '{_expected}'. " f"Received instead: {actual}"
assert actual == _expected, msg


def test_mutect2_wrapper_run_preamble_resource_chunk_time(construct_preamble_module):
"""
Tests Parallel Preamble resource_chunk_time() - Baseline time defined in Snakemake object
"""
# Define (input, expected) pair
input_expected_pairs = (
(1, "0-3:0:0"),
(2, "0-6:0:0"),
(3, "0-9:0:0"),
(4, "0-12:0:0"),
(5, "0-15:0:0"),
)
# Test all pairs
for pair in input_expected_pairs:
_input = pair[0]
_expected = pair[1]
actual = construct_preamble_module.resource_chunk_time(wildcards=None, attempt=_input)
msg = f"For input '{_input}' expected output '{_expected}'. " f"Received instead: {actual}"
assert actual == _expected, msg


def test_mutect2_wrapper_run_preamble_resource_chunk_partition(construct_preamble_module):
"""Tests Parallel Preamble resource_chunk_partition()"""
expected = "medium"
actual = construct_preamble_module.resource_chunk_partition(wildcards=None)
assert actual == expected


def test_mutect2_wrapper_run_preamble_resource_merge_threads(construct_preamble_module):
"""Tests Parallel Preamble resource_merge_threads() - Merge always get a single thread"""
expected = 1
actual = construct_preamble_module.resource_merge_threads(wildcards=None)
assert actual == expected


def test_mutect2_wrapper_run_preamble_resource_merge_memory(construct_preamble_module):
"""Tests Parallel Preamble resource_merge_memory() - Merge always get the same value"""
expected = "64G"
actual = construct_preamble_module.resource_merge_memory(wildcards=None)
assert actual == expected


def test_mutect2_wrapper_run_preamble_resource_merge_time(construct_preamble_module):
"""Tests Parallel Preamble resource_merge_time() - Merge always get the same value"""
expected = "5:00:00"
actual = construct_preamble_module.resource_merge_time(wildcards=None)
assert actual == expected


def test_mutect2_wrapper_run_preamble_resource_merge_partition(construct_preamble_module):
"""Tests Parallel Preamble resource_merge_partition()"""
expected = "medium"
actual = construct_preamble_module.resource_merge_partition(wildcards=None)
assert actual == expected

0 comments on commit 7bf81fb

Please sign in to comment.