Skip to content

Commit

Permalink
fix test bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanguage committed Jul 14, 2024
1 parent 9f0c24f commit 73896b0
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 104 deletions.
2 changes: 0 additions & 2 deletions coolbox/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ def source(self) -> str:
def print_source(self):
"""Print the browser composing code."""
print(self.source())
return self

def gen_notebook(self, notebook_path, notes=True, figsave=True):
"""Generate The notebook contain codes for run coolbox browser.
Expand Down Expand Up @@ -288,7 +287,6 @@ def gen_notebook(self, notebook_path, notes=True, figsave=True):
)
nb['cells'] = cells
nbf.write(nb, notebook_path)
return self

def run_jupyter(self, jupyter_args="--ip=0.0.0.0"):
"""Create a notebook according to command line, then start a jupyter process.
Expand Down
8 changes: 4 additions & 4 deletions coolbox/core/coverage/highlights.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def __init__(self, file_, **kwargs):
"file": file_,
"color": "bed_rgb",
"alpha": 0.1,
"border_line": True,
"border_line": False,
"border_line_style": "dashed",
"border_line_width": 0,
"border_line_width": 2,
"border_line_color": "#000000",
"border_line_alpha": 0.8,
}
Expand Down Expand Up @@ -168,9 +168,9 @@ def __init__(self, highlight_regions, **kwargs):
"highlight_regions": highlight_regions,
"color": HighLights.DEFAULT_COLOR,
"alpha": 0.25,
"border_line": True,
"border_line": False,
"border_line_style": "dashed",
"border_line_width": 0,
"border_line_width": 2,
"border_line_color": "#000000",
"border_line_alpha": 0.8,
}
Expand Down
5 changes: 3 additions & 2 deletions coolbox/core/frame/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def get_tracks_height(self, default_height=3):
heights.append(default_height)
return heights

def plot(self, *args):
def plot(self, *args, close_fig=True):
"""
Plot all tracks.
Expand Down Expand Up @@ -231,7 +231,8 @@ def plot(self, *args):
bottom=margins['bottom'],
top=margins['top'])

plt.close()
if close_fig:
plt.close()

return fig

Expand Down
9 changes: 5 additions & 4 deletions coolbox/core/track/ideogram.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pandas as pd
from matplotlib.collections import BrokenBarHCollection
import matplotlib.pyplot as plt

from coolbox.utilities import (
get_logger, GenomeRange, file_to_intervaltree, hex2rgb,
Expand Down Expand Up @@ -102,9 +102,10 @@ def plot(self, ax, gr: GenomeRange, **kwargs):
and gr.length < 80_000_000
):
self.plot_text(band_name, start, end, gr, band_color)
coll = BrokenBarHCollection(xranges, (0, band_height), facecolors=colors,
linewidths=self.properties['border_width'],
edgecolors=self.properties['border_color'])
coll = plt.broken_barh(
xranges, (0, band_height), facecolors=colors,
linewidth=self.properties['border_width'],
edgecolor=self.properties['border_color'])
ax.add_collection(coll)
ax.set_ylim(-0.1, band_height + 0.1)
ax.set_xlim(gr.start, gr.end)
Expand Down
46 changes: 46 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import os.path as osp
import pytest
from coolbox.utilities import GenomeRange


HERE = osp.dirname(osp.abspath(__file__))
Expand All @@ -24,3 +26,47 @@ def pytest_sessionstart(session):
def pytest_sessionfinish(session):
clear_bgz_and_indexes()


@pytest.fixture
def data_dir():
return DATA_DIR


@pytest.fixture
def test_interval():
test_interval = GenomeRange("chr9:4000000-6000000")
return test_interval


@pytest.fixture
def test_itv(test_interval):
return str(test_interval).replace(':', '_').replace('-', '_')


@pytest.fixture
def empty_interval():
empty_interval = GenomeRange("chr10:4000000-6000000")
return empty_interval


@pytest.fixture
def sub_interval1():
sub_interval1 = GenomeRange("chr9:4500000-5000000")
return sub_interval1


@pytest.fixture
def sub_interval2():
sub_interval2 = GenomeRange("chr9:5200000-5850000")
return sub_interval2


@pytest.fixture
def tmp_dir():
# yield a temporary directory
# delete it after the test
from tempfile import mkdtemp
import shutil
tmpdir = mkdtemp()
yield tmpdir
shutil.rmtree(tmpdir)
45 changes: 24 additions & 21 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import os.path as osp
import subprocess as subp


HERE = osp.dirname(osp.abspath(__file__))
DATA_DIR = f"{HERE}/test_data"
test_interval = "chr9:4000000-6000000"
empty_interval = "chr10:4000000-6000000"
test_itv = test_interval.replace(':', '_').replace('-', '_')


def test_cli_plot():
def test_cli_plot(data_dir, tmp_dir):
cmd = [
"python", "-m", "coolbox.cli",
"add", "XAxis", "-",
"add", "BigWig", f"{DATA_DIR}/bigwig_{test_itv}.bw", "-",
"add", "BedGraph", f"{DATA_DIR}/bedgraph_{test_itv}.bg", "-",
"add", "GTF", f"{DATA_DIR}/gtf_{test_itv}.gtf", "-",
"add", "BigWig", f"{data_dir}/bigwig_{test_itv}.bw", "-",
"add", "BedGraph", f"{data_dir}/bedgraph_{test_itv}.bg", "-",
"add", "GTF", f"{data_dir}/gtf_{test_itv}.gtf", "-",
"goto", test_interval, "-",
"plot", "/tmp/test_coolbox.pdf",
]
Expand All @@ -25,43 +23,48 @@ def test_cli_plot():
python -m coolbox.cli
joint_view top -
add XAxis -
add GTF {DATA_DIR}/gtf_{test_itv}.gtf -
add GTF {data_dir}/gtf_{test_itv}.gtf -
add Title GTF -
joint_view right -
add XAxis -
add BigWig {DATA_DIR}/bigwig_{test_itv}.bw -
add BigWig {data_dir}/bigwig_{test_itv}.bw -
add TrackHeight 2 -
add MinValue 0 -
joint_view center -
add Cool {DATA_DIR}/cool_{test_itv}.mcool -
add Cool {data_dir}/cool_{test_itv}.mcool -
goto 'chr9:4500000-5000000' 'chr9:5200000-5850000' -
plot /tmp/test_coolbox_joint_view.svg
plot {tmp_dir}/test_coolbox_joint_view.svg
"""

subp.check_call(cmd.replace("\n", ""), shell=True)
cmd = cmd.replace("\n", "")
print(cmd)
subp.check_call(cmd, shell=True)


def test_cli_gen_notebook():
def test_cli_gen_notebook(data_dir, tmp_dir):
cmd = [
"python", "-m", "coolbox.cli",
"add", "XAxis", "-",
"add", "BigWig", f"{DATA_DIR}/bigwig_{test_itv}.bw", "-",
"add", "BedGraph", f"{DATA_DIR}/bedgraph_{test_itv}.bg", "-",
"add", "GTF", f"{DATA_DIR}/gtf_{test_itv}.gtf", "-",
"add", "BigWig", f"{data_dir}/bigwig_{test_itv}.bw", "-",
"add", "BedGraph", f"{data_dir}/bedgraph_{test_itv}.bg", "-",
"add", "GTF", f"{data_dir}/gtf_{test_itv}.gtf", "-",
"goto", test_interval, "-",
"gen_notebook", "/tmp/test_coolbox.ipynb",
"gen_notebook", f"{tmp_dir}/test_coolbox.ipynb",
]
subp.check_call(cmd)
cmd = " ".join(cmd)
print(cmd)
subp.check_call(cmd, shell=True)


def test_cli_import_custom():
def test_cli_import_custom(tmp_dir):
custom_path = osp.join(HERE, "custom_track.py")
cmd = [
"python", "-m", "coolbox.cli",
"load_module", custom_path, "-",
"add", "XAxis", "-",
"add", "CustomTrack", "-",
"goto", test_interval, "-",
"plot", "/tmp/test_coolbox_custom.pdf"
"plot", f"{tmp_dir}/test_coolbox_custom.pdf"
]
subp.check_call(cmd)
cmd = " ".join(cmd)
print(cmd)
subp.check_call(cmd, shell=True)
48 changes: 23 additions & 25 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
import os.path as osp
from coolbox.api import *

HERE = osp.dirname(osp.abspath(__file__))
DATA_DIR = f"{HERE}/test_data"
test_interval = "chr9:4000000-6000000"
empty_interval = "chr10:4000000-6000000"
test_itv = test_interval.replace(':', '_').replace('-', '_')


def test_vlines():
def test_vlines(test_interval, tmp_dir):
vlines = Vlines([("chr9", 4000000),
"chr9:5000000-6000000",
("chr1", 4000000)])
with vlines:
frame = XAxis() + XAxis()
fig = frame.plot(test_interval)
fig.savefig("/tmp/test_coolbox_vline.pdf")
fig.savefig(f"{tmp_dir}/test_coolbox_vline.pdf")


def test_highlights():
def test_highlights(test_interval, tmp_dir):
highlights = HighLights([
("chr9", 4000000, 5000000),
"chr9:5000000-6000000",
])
with highlights:
frame = XAxis() + XAxis()
fig = frame.plot(test_interval)
fig.savefig("/tmp/test_coolbox_highlights.pdf")
fig = frame.plot(test_interval, close_fig=False)
fig.savefig(f"{tmp_dir}/test_coolbox_highlights.pdf")


def test_bigwig_coverage():
def test_bigwig_coverage(data_dir, test_itv, test_interval, tmp_dir):
frame = XAxis() +\
BigWig(f"{DATA_DIR}/bigwig_{test_itv}.bw", style="line:1") + \
BigWigCoverage(f"{DATA_DIR}/bigwig_{test_itv}.bw", style="fill", color="red", alpha=0.3) +\
BigWigCoverage(f"{DATA_DIR}/bigwig_{test_itv}.bw", style="fill", color="blue", alpha=0.3)
BigWig(f"{data_dir}/bigwig_{test_itv}.bw", style="line:1") + \
BigWigCoverage(f"{data_dir}/bigwig_{test_itv}.bw", style="fill", color="red", alpha=0.3) +\
BigWigCoverage(f"{data_dir}/bigwig_{test_itv}.bw", style="fill", color="blue", alpha=0.3)
fig = frame.plot(test_interval)
fig.savefig("/tmp/test_coolbox_bwcov.pdf")
fig.savefig(f"{tmp_dir}/test_coolbox_bwcov.pdf")


def test_arcs_coverage():
def test_arcs_coverage(data_dir, test_itv, test_interval, tmp_dir):
frame = XAxis() + \
BigWig(f"{DATA_DIR}/bigwig_{test_itv}.bw", style="fill", alpha=0.5) + \
ArcsCoverage(f"{DATA_DIR}/bedpe_{test_itv}.bedpe")
BigWig(f"{data_dir}/bigwig_{test_itv}.bw", style="fill", alpha=0.5) + \
ArcsCoverage(f"{data_dir}/bedpe_{test_itv}.bedpe")
fig = frame.plot(test_interval)
fig.savefig("/tmp/test_coolbox_arcscov.pdf")
fig.savefig(f"{tmp_dir}/test_coolbox_arcscov.pdf")


def test_tad_coverage():
def test_tad_coverage(data_dir, test_itv, test_interval, tmp_dir):
frame = XAxis() + \
Cool(f"{DATA_DIR}/cool_{test_itv}.mcool") + \
TADCoverage(f"{DATA_DIR}/tad_{test_itv}.bed")
Cool(f"{data_dir}/cool_{test_itv}.mcool") + \
TADCoverage(f"{data_dir}/tad_{test_itv}.bed")
fig = frame.plot(test_interval)
fig.savefig("/tmp/test_coobox_tadcov.pdf")
fig.savefig(f"{tmp_dir}/test_coobox_tadcov.pdf")


if __name__ == '__main__':
test_highlights(GenomeRange("chr9:3000000-7000000"), "/tmp")
#test_vlines(GenomeRange("chr9:4000000-6000000"), "/tmp")
Loading

0 comments on commit 73896b0

Please sign in to comment.