diff --git a/bedboss/bedstat/bedstat.py b/bedboss/bedstat/bedstat.py index fd07925..e2d05c0 100755 --- a/bedboss/bedstat/bedstat.py +++ b/bedboss/bedstat/bedstat.py @@ -1,13 +1,13 @@ -from hashlib import md5 from typing import NoReturn import json import yaml import os import requests -import gzip import pypiper import bbconf import logging +from geniml.io import RegionSet + _LOGGER = logging.getLogger("bedboss") @@ -16,30 +16,6 @@ ) -def digest_bedfile(filepath: str) -> str: - """ - Generate digest for bedfile - - :param str filepath: path to the bed file - :return str: digest of the files - """ - with gzip.open(filepath, "rb") as f: - # concate column values - chrs = ",".join([row.split()[0].decode("utf-8") for row in f]) - starts = ",".join([row.split()[1].decode("utf-8") for row in f]) - ends = ",".join([row.split()[2].decode("utf-8") for row in f]) - # hash column values - chr_digest = md5(chrs.encode("utf-8")).hexdigest() - start_digest = md5(starts.encode("utf-8")).hexdigest() - end_digest = md5(ends.encode("utf-8")).hexdigest() - # hash column digests - bed_digest = md5( - ",".join([chr_digest, start_digest, end_digest]).encode("utf-8") - ).hexdigest() - - return bed_digest - - def convert_unit(size_in_bytes: int) -> str: """ Convert the size from bytes to other units like KB, MB or GB @@ -106,7 +82,7 @@ def bedstat( pass bbc = bbconf.BedBaseConf(config_path=bedbase_config, database_only=True) - bed_digest = digest_bedfile(bedfile) + bed_digest = RegionSet(bedfile).identifier bedfile_name = os.path.split(bedfile)[1] fileid = os.path.splitext(os.path.splitext(bedfile_name)[0])[0] diff --git a/requirements/requirements-all.txt b/requirements/requirements-all.txt index 4c1590b..04b9560 100644 --- a/requirements/requirements-all.txt +++ b/requirements/requirements-all.txt @@ -7,4 +7,5 @@ piper>=0.13.3a1 bbconf>=0.4.0a5 refgenconf>=0.12.2 pandas>=1.5.3 -ubiquerg>=0.6.2 \ No newline at end of file +ubiquerg>=0.6.2 +geniml diff --git a/test/bash_requirements_test.sh b/test/bash_requirements_test.sh index 8950daf..f5cc81f 100755 --- a/test/bash_requirements_test.sh +++ b/test/bash_requirements_test.sh @@ -121,7 +121,7 @@ if is_executable "R"; then echo -e "-----------------------------------------------------------" echo -e "Checking required R packages for bedstat... " echo -e "-----------------------------------------------------------" - declare -a requiredRPackages=("optparse ""devtools" "ensembldb" "ExperimentHub" "AnnotationHub" "AnnotationFilter" "BSgenome" "GenomicFeatures" "GenomicDistributions" "GenomicDistributionsData" "GenomeInfoDb" "ensembldb" "tools" "R.utils" "LOLA") + declare -a requiredRPackages=("optparse ""devtools" "ensembldb" "ExperimentHub" "AnnotationHub" "AnnotationFilter" "BSgenome" "GenomicFeatures" "GenomicDistributions" "GenomicDistributionsData" "GenomeInfoDb" "ensembldb" "tools" "R.utils" "LOLA" "conflicted") for package in "${requiredRPackages[@]}"; do if ! r_check_req $package; then INSTALL_ERROR=$((INSTALL_ERROR+1)) diff --git a/test/test_bedboss.py b/test/test_bedboss.py index e103e84..a27bd23 100644 --- a/test/test_bedboss.py +++ b/test/test_bedboss.py @@ -1,5 +1,6 @@ from bedboss.bedboss import main import os +import warnings import subprocess import pytest from bbconf import BedBaseConf @@ -14,14 +15,23 @@ BEDBASE_CONFIG = os.path.join(FILE_DIR, "test_dependencies", "bedbase_config_test.yaml") DEPENDENCIES_TEST_SCRIPT = f"{FILE_DIR}/bash_requirements_test.sh" +pytest_db_skip_reason = "Database is not set up... To run this test, set up the database. Go to test/README.md for more information." -def test_dependencies(): + +def check_dependencies_installed() -> bool: # Make sure bedToBigBed etc is in your PATH. print("Testing dependencies...") key = "PATH" value = os.getenv(key) test_dep_return_code = subprocess.run([DEPENDENCIES_TEST_SCRIPT], shell=True) - assert 1 > test_dep_return_code.returncode + if not (1 > test_dep_return_code.returncode): + warnings.warn(UserWarning(f"{pytest_db_skip_reason}")) + return False + return True + # return 1 > test_dep_return_code.returncode + + +dependencies_installed = check_dependencies_installed() def db_setup(): @@ -29,13 +39,13 @@ def db_setup(): try: BedBaseConf(BEDBASE_CONFIG) except Exception as err: - print(f"Error: {err}") - BedBaseConf(BEDBASE_CONFIG) + warnings.warn(UserWarning(f"{pytest_db_skip_reason}")) return False return True -pytest_db_skip_reason = "Database is not set up... To run this test, set up the database. Go to test/README.md for more information." +def test_dependencies(): + assert dependencies_installed @pytest.mark.parametrize( @@ -55,6 +65,14 @@ def test_qc(bedfile, tmpdir): assert qc_passed is None +@pytest.mark.skipif( + not db_setup() or not dependencies_installed, + reason=pytest_db_skip_reason, +) +@pytest.mark.skipif( + not db_setup() or not dependencies_installed, + reason=pytest_db_skip_reason, +) @pytest.mark.parametrize( "bedfile", [ @@ -80,7 +98,7 @@ def test_make(bedfile, tmpdir): @pytest.mark.skipif( - not db_setup(), + not db_setup() or not dependencies_installed, reason=pytest_db_skip_reason, ) class TestStat: @@ -142,14 +160,14 @@ def test_check_file_exists(self, file, output_temp_dir): output_temp_dir, "output", "bedstat_output", - "c557c915a9901ce377ef724806ff7a2c", + "49a72983ca9ddcf6692c5ec8b51c3d92", file, ) ) @pytest.mark.skipif( - not db_setup(), + not db_setup() or not dependencies_installed, reason=pytest_db_skip_reason, ) class TestAll: @@ -212,7 +230,7 @@ def test_check_file_exists(self, file, output_temp_dir): output_temp_dir, "output", "bedstat_output", - "c557c915a9901ce377ef724806ff7a2c", + "49a72983ca9ddcf6692c5ec8b51c3d92", file, ) )