Fix array indexing bug in uint8_to_bed_parallel.py #26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Problem: An IndexError can be raised on line 384 of uint8_to_bed_parallel.py when trying to access an out-of-bounds index of
ar_quant:Fix: this commit both simplifies the logic of the code and fixes the bug.
Details
Relevant variables
uniquely_mappable: single read mappability array of length Nar_quant: multi-read mappability array of length N (length of chromosome)unimap_diff: array of length N - 1poses_start: start position of non-zero "runs"; initially (line 367) calculated as a value 1 less than the desired 0-based (start-open, end-closed) indexposes_end: end position of non-zero "runs"; initially (line 368) calculated as a value 1 less than the desired 0-based (start-open, end-closed) indexHere, I annotate the original code to clarify its logic.
Note that whether the length of the arrays
poses_endandposes_startare the same is ultimately not relevant. We simply need to add a start position of 0 if the first element ofar_quantis non-zero, and add an end position oflen(uniquely_mappable) - 1if the last element ofar_quantis non-zero. This entire code block can therefore be simplifed as follows:Example for a kmer size of 1
uniquely_mappable = [ 0, 0, 1, 1, 1, 0, 0, 1, 1 ]ar_quant = [ 0, 0, 1, 1, 1, 0, 0, 1, 1 ]unimap_diff = [ 0, 1, 0, 0, -1, 0, 1, 0]poses_start = [1, 6]poses_end = [4]--> extended toposes_end = [4, 8]Desired wiggle output
Working backwards, we need
pos_st = [2, 7]andpos_end = [5, 9]for the following loop (lines 383-384) to work as intended:Lines 378 and 379 add a value of 1 to each of the positions from poses_start and poses_end, therefore giving the desired result.