Skip to content

Commit

Permalink
Fixed tests and updated bed hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Oct 30, 2023
1 parent bd0cdf4 commit a00c5b7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
30 changes: 3 additions & 27 deletions bedboss/bedstat/bedstat.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion requirements/requirements-all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ piper>=0.13.3a1
bbconf>=0.4.0a5
refgenconf>=0.12.2
pandas>=1.5.3
ubiquerg>=0.6.2
ubiquerg>=0.6.2
geniml
2 changes: 1 addition & 1 deletion test/bash_requirements_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
36 changes: 27 additions & 9 deletions test/test_bedboss.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from bedboss.bedboss import main
import os
import warnings
import subprocess
import pytest
from bbconf import BedBaseConf
Expand All @@ -14,28 +15,37 @@
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():
# Check if the database is 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(
Expand All @@ -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",
[
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -212,7 +230,7 @@ def test_check_file_exists(self, file, output_temp_dir):
output_temp_dir,
"output",
"bedstat_output",
"c557c915a9901ce377ef724806ff7a2c",
"49a72983ca9ddcf6692c5ec8b51c3d92",
file,
)
)

0 comments on commit a00c5b7

Please sign in to comment.