Skip to content

Commit

Permalink
Merge pull request #1257 from lldelisle/fix_error_handling
Browse files Browse the repository at this point in the history
Fix error handling + cases where with bigwigAverage with more than 2 bigwigs
  • Loading branch information
WardDeb authored Sep 27, 2023
2 parents aca3def + f0c8223 commit d56b016
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 12 additions & 0 deletions deeptools/test/test_bigwigAverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ def test_bigwigAverageWithScale():
expected = ['3R\t0\t50\t0\n', '3R\t50\t100\t0.25\n', '3R\t100\t150\t0.75\n', '3R\t150\t200\t1\n']
assert f"{resp}" == f"{expected}", f"{resp} != {expected}"
unlink(outfile)


def test_bigwigAverageThree():
outfile = '/tmp/result.bg'
args = "--bigwigs {} {} {} -o {} --outFileFormat bedgraph --scaleFactors 0.75:0.75:.75".format(BIGWIG_A, BIGWIG_A, BIGWIG_B, outfile).split()
bwAve.main(args)
_foo = open(outfile, 'r')
resp = _foo.readlines()
_foo.close()
expected = ['3R\t0\t50\t0\n', '3R\t50\t100\t0.25\n', '3R\t100\t150\t0.75\n', '3R\t150\t200\t1\n']
assert f"{resp}" == f"{expected}", f"{resp} != {expected}"
unlink(outfile)
10 changes: 6 additions & 4 deletions deeptools/writeBedGraph_bam_and_bw.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def getCoverageFromBigwig(bigwigHandle, chrom, start, end, tileSize,
missingDataAsZero=False):
try:
coverage = np.asarray(bigwigHandle.values(chrom, start, end))
except TypeError:
except RuntimeError:
# this error happens when chromosome
# is not into the bigwig file
return []
Expand Down Expand Up @@ -170,7 +170,7 @@ def writeBedGraph(
chromNamesAndSize, __ = getCommonChrNames(bamHandles, verbose=verbose)
else:
genomeChunkLength = int(10e6)
cCommon = []
cCommon_number = {}
chromNamesAndSize = {}
for fileName, fileFormat in bamOrBwFileList:
if fileFormat == 'bigwig':
Expand All @@ -180,7 +180,7 @@ def writeBedGraph(

for chromName, size in list(fh.chroms().items()):
if chromName in chromNamesAndSize:
cCommon.append(chromName)
cCommon_number[chromName] += 1
if chromNamesAndSize[chromName] != size:
print("\nWARNING\n"
"Chromosome {} length reported in the "
Expand All @@ -193,14 +193,16 @@ def writeBedGraph(
chromNamesAndSize[chromName], size)
else:
chromNamesAndSize[chromName] = size
cCommon_number[chromName] = 1
fh.close()

# get the list of common chromosome names and sizes
if len(bamOrBwFileList) == 1:
chromNamesAndSize = [(k, v) for k, v in chromNamesAndSize.items()]
else:
chromNamesAndSize = [(k, v) for k, v in chromNamesAndSize.items()
if k in cCommon]
if k in cCommon_number and
cCommon_number[k] == len(bamOrBwFileList)]

if region:
# in case a region is used, append the tilesize
Expand Down

0 comments on commit d56b016

Please sign in to comment.