Skip to content

Commit

Permalink
test: more merge helper/merge main testing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Dec 14, 2023
1 parent 028e9b8 commit bd12b68
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 28 deletions.
17 changes: 17 additions & 0 deletions tests/shared_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pathlib

TEST_BIN_DIR = pathlib.Path(__file__).parent / "bin"
TEST_DATA_DIR = pathlib.Path(__file__).parent / "data"
TEST_OUT_DIR = pathlib.Path(__file__).parent / "out"

BIGWIG_TO_BEDGRAPH_BIN = TEST_BIN_DIR / "bigWigToBedGraph.linux.x86_64"
BIGWIG_MERGE_PLUS_BIN = TEST_BIN_DIR / "bigWigMergePlus"

INPUT_FILES = (
TEST_DATA_DIR / "25574.Blueprint.ERS487305.RNA-Seq.signal_reverse.bigWig",
TEST_DATA_DIR / "25575.Blueprint.ERS487305.RNA-Seq.signal_forward.bigWig",
TEST_DATA_DIR / "25582.Blueprint.ERS487306.RNA-Seq.signal_reverse.bigWig",
)
INPUT_FILES_STR = [*map(str, INPUT_FILES)]

TEST_POS = "chr1:0-500000"
41 changes: 13 additions & 28 deletions tests/test_against_bwmp.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
# Test against the software this replaces: bigWigMergePlus
import pathlib
import subprocess

from bw_merge_window.entry import entry

TEST_BIN_DIR = pathlib.Path(__file__).parent / "bin"
TEST_DATA_DIR = pathlib.Path(__file__).parent / "data"
TEST_OUT_DIR = pathlib.Path(__file__).parent / "out"

BIGWIG_TO_BEDGRAPH_BIN = TEST_BIN_DIR / "bigWigToBedGraph.linux.x86_64"
BIGWIG_MERGE_PLUS_BIN = TEST_BIN_DIR / "bigWigMergePlus"

INPUT_FILES = [
TEST_DATA_DIR / "25574.Blueprint.ERS487305.RNA-Seq.signal_reverse.bigWig",
TEST_DATA_DIR / "25575.Blueprint.ERS487305.RNA-Seq.signal_forward.bigWig",
TEST_DATA_DIR / "25582.Blueprint.ERS487306.RNA-Seq.signal_reverse.bigWig",
]
INPUT_FILES_STR = [*map(str, INPUT_FILES)]

TEST_POS = "chr1:0-500000"
from . import shared_data as sd


def parse_bedgraph_line(line: str) -> tuple[str, int, int, float]:
Expand All @@ -27,34 +12,34 @@ def parse_bedgraph_line(line: str) -> tuple[str, int, int, float]:


def test_against_bwmp():
out_new = str(TEST_OUT_DIR / "merged-new.bw")
out_new_bedgraph = str(TEST_OUT_DIR / "merged-new.bedGraph")
out_new = str(sd.TEST_OUT_DIR / "merged-new.bw")
out_new_bedgraph = str(sd.TEST_OUT_DIR / "merged-new.bedGraph")
entry(
(
TEST_POS,
*INPUT_FILES_STR,
sd.TEST_POS,
*sd.INPUT_FILES_STR,
"--treat-missing-as-zero",
"--range",
"0-1000",
"--output",
out_new,
)
)
subprocess.run((str(BIGWIG_TO_BEDGRAPH_BIN), out_new, out_new_bedgraph))
subprocess.run((str(sd.BIGWIG_TO_BEDGRAPH_BIN), out_new, out_new_bedgraph))

out_old = str(TEST_OUT_DIR / "merged-old.bw")
out_old_bedgraph = str(TEST_OUT_DIR / "merged-old.bedGraph")
out_old = str(sd.TEST_OUT_DIR / "merged-old.bw")
out_old_bedgraph = str(sd.TEST_OUT_DIR / "merged-old.bedGraph")
subprocess.run(
(
str(BIGWIG_MERGE_PLUS_BIN),
str(sd.BIGWIG_MERGE_PLUS_BIN),
"-range=0-1000",
"-compress",
f"-position={TEST_POS}",
*INPUT_FILES_STR,
str(TEST_OUT_DIR / "merged-old.bw"),
f"-position={sd.TEST_POS}",
*sd.INPUT_FILES_STR,
str(sd.TEST_OUT_DIR / "merged-old.bw"),
)
)
subprocess.run((str(BIGWIG_TO_BEDGRAPH_BIN), out_old, out_old_bedgraph))
subprocess.run((str(sd.BIGWIG_TO_BEDGRAPH_BIN), out_old, out_old_bedgraph))

with open(out_old_bedgraph, "r") as fho:
fho_lines = fho.readlines()
Expand Down
52 changes: 52 additions & 0 deletions tests/test_merge_helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import pyBigWig
import pytest

from bw_merge_window import merge as m

from . import shared_data as sd

logger = logging.getLogger(__name__)


Expand All @@ -15,12 +18,24 @@ def test_int_cast():
def test_parse_window():
assert m.get_window_contig_start_end("chr1:0-10000", logger) == ("chr1", 0, 10000)
assert m.get_window_contig_start_end("chr1:-5-10000", logger) == ("chr1", 0, 10000)
assert m.get_window_contig_start_end("chr1:0-", logger) == ("chr1", 0, None)
assert m.get_window_contig_start_end("chr1", logger) == ("chr1", 0, None)

with pytest.raises(ValueError):
m.get_window_contig_start_end("chr1:x-y", logger)

with pytest.raises(ValueError):
m.get_window_contig_start_end("", logger)

with pytest.raises(ValueError):
m.get_window_contig_start_end("chr1:-", logger)

with pytest.raises(ValueError):
m.get_window_contig_start_end("chr1:-10000", logger)


def test_parse_range():
assert m.get_range_values(None) is None
assert m.get_range_values("0-1000") == (0, 1000)
assert m.get_range_values("-5-1000") == (-5, 1000)
assert m.get_range_values("500-1000") == (500, 1000)
Expand All @@ -34,3 +49,40 @@ def test_parse_range():

with pytest.raises(ValueError):
m.get_range_values("10-5")


def test_open_handles():
assert m.get_bigwig_file_handles(()) == ()

fhs = m.get_bigwig_file_handles(sd.INPUT_FILES) # TODO: fixture
try:
assert len(fhs) == len(sd.INPUT_FILES)
finally:
for fh in fhs:
fh.close()


def test_contig_presence():
h = pyBigWig.open(str(sd.INPUT_FILES[0]))

try:
assert m.verify_contig_presence_and_get_length(h, 0, "chr1") == 249250621 # GRCh37 chr1 length

with pytest.raises(ValueError):
m.verify_contig_presence_and_get_length(h, 0, "chrDNE")

finally:
h.close()


def test_contig_consensus_length():
with pytest.raises(ValueError) as e:
m.get_consensus_contig_length_or_raise((), "chr1")
assert "At least one bigWig file must be specified" in str(e)

fhs = m.get_bigwig_file_handles(sd.INPUT_FILES)
try:
assert m.get_consensus_contig_length_or_raise(fhs, "chr1") == 249250621 # GRCh37 chr1 length
finally:
for fh in fhs:
fh.close()
24 changes: 24 additions & 0 deletions tests/test_merge_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from bw_merge_window.entry import entry

from . import shared_data as sd

ALL_TEST_ARGS = (
(sd.TEST_POS, *sd.INPUT_FILES_STR),
(sd.TEST_POS, *sd.INPUT_FILES_STR, "--range", "0-1000"),
(sd.TEST_POS, *sd.INPUT_FILES_STR, "--treat-missing-as-zero"),
(sd.TEST_POS, *sd.INPUT_FILES_STR, "--treat-missing-as-zero", "--range", "0-1000"),
(sd.TEST_POS, *sd.INPUT_FILES_STR, "--treat-missing-as-zero", "--range=-500-1000"),
(sd.TEST_POS, *sd.INPUT_FILES_STR, "--treat-missing-as-zero", "--range", "10-50"),
# TODO: these should be tested, but it's too slow...
# ("chr1", *sd.INPUT_FILES_STR, "--treat-missing-as-zero"),
# ("chr1:0-", *sd.INPUT_FILES_STR, "--treat-missing-as-zero"),
# ("chr1:5000-", *sd.INPUT_FILES_STR, "--treat-missing-as-zero"),
)


@pytest.mark.parametrize("args", ALL_TEST_ARGS)
def test_merge_conditions(args):
out_test = str(sd.TEST_OUT_DIR / "merged-test.bw")
entry((*args, "--output", out_test))

0 comments on commit bd12b68

Please sign in to comment.