Skip to content

Commit

Permalink
Merge pull request #102 from Kudostoy0u/main
Browse files Browse the repository at this point in the history
Add test for coverage function, fixed return value
  • Loading branch information
jamesli124 authored Aug 22, 2024
2 parents 01e2694 + 614da97 commit 03f369c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pip-wheel-metadata
build/*
tests/tmp/*
.coverage*
test.py
test2.py
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ The format is based on
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.7.3] - 2024-08-20

### Changed
- Used "not" instead of "~" in an if statement
- Added a test for the coverage function

### Fixed
- Ensured that the coverage value returns the expected value (previously returned an empty generator)

## [0.7.2] - 2024-08-17

### Changed
Expand Down
7 changes: 4 additions & 3 deletions src/finaletoolkit/frag/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def coverage(
coverages : Iterable[tuple[str, int, int, str, float]]
Fragment coverages over intervals.
"""
returnVal = []
if (verbose):
start_time = time.time()
sys.stderr.write(
Expand Down Expand Up @@ -209,7 +210,6 @@ def coverage(
) if verbose else intervals,
min(len(intervals) // 2 // workers + 1, 40)
)

if verbose:
tqdm.write('Retrieving total coverage for file\n')
total_coverage = total_coverage_results.get()
Expand Down Expand Up @@ -244,13 +244,15 @@ def coverage(
f'{contig}\t{start}\t{stop}\t'
f'{coverage/total_coverage[4]*scale_factor}\n'
)
returnVal.append((contig,start,stop,name,coverage/total_coverage[4]*scale_factor))
else:
for contig, start, stop, name, coverage in coverages:
output.write(
f'{contig}\t{start}\t{stop}\t'
f'{name}\t'
f'{coverage/total_coverage[4]*scale_factor}\n'
)
returnVal.append((contig,start,stop,name,coverage/total_coverage[4]*scale_factor))
finally:
if output_is_file:
output.close()
Expand All @@ -262,6 +264,5 @@ def coverage(
sys.stderr.write(
f'coverage took {end_time - start_time} s to complete\n'
)

return coverages
return returnVal

2 changes: 1 addition & 1 deletion src/finaletoolkit/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def _get_intervals(

with open(interval_file) as bed:
for line in bed:
if ~line.startswith('#'):
if not line.startswith('#'):
if line != '':
contig, start, stop, *name = line.split()
start = int(start)
Expand Down
2 changes: 1 addition & 1 deletion src/finaletoolkit/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Single-source module for the package version number.
"""

__version__ = "0.7.2"
__version__ = "0.7.3"
2 changes: 2 additions & 0 deletions tests/data/intervals.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
12 34443118 34443538
12 34444968 34446115
20 changes: 18 additions & 2 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,21 @@ def test_coverage_interval_midpoints(self, request):
assert cov == pytest.approx(2)

class TestCoverage:
def coverage(self, request):
pass
def test_coverage(self, request):
input_file = request.path.parent / 'data' / '12.3444.b37.frag.gz'
intervals = request.path.parent / 'data' / 'intervals.bed'
results = coverage(input_file,intervals,"-")
for i in range(2):
chrom, start, stop, name, cov = results[i]
if i == 0:
assert chrom == '12'
assert start == 34443118
assert stop == 34443538
assert name == '.'
assert cov == pytest.approx(312500.0)
elif i == 1:
assert chrom == '12'
assert start == 34444968
assert stop == 34446115
assert name == '.'
assert cov == pytest.approx(437500.0)

0 comments on commit 03f369c

Please sign in to comment.