Skip to content

Commit 4d3c044

Browse files
committed
Replace manual array mappability buffer with numpy oriented buffering
1 parent a552447 commit 4d3c044

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

newmap/unique_counts_conversion.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,14 @@ def write_multi_read_wig(wig_file: BinaryIO,
9696
wig_file.write(WIG_FIXED_STEP_DECLARATION_FORMAT
9797
.format(chr_name, 1)
9898
.encode())
99-
write_floats_buffered(multi_read_mappability, wig_file,
100-
MULTREAD_WRITE_BUFFER_SIZE)
10199

100+
for mappability_chunk in np.nditer(multi_read_mappability,
101+
flags=['external_loop', 'buffered']):
102+
wig_file.writelines(f"{value}\n".encode()
103+
for value in mappability_chunk)
102104

103-
def write_floats_buffered(float_array: npt.NDArray[np.float64],
104-
file: BinaryIO,
105-
buffer_size: int):
106-
# Split the array into buffer_size chunks.
107-
# NB: Ideally this would be based on a byte count but it depends on how the
108-
# floats are converted and formatted
109105

110-
# To estimate the number of splits, take the array size, divide by the
111-
# buffer size (floored) and add 1
112-
113-
num_splits = (float_array.size // buffer_size) + 1
114-
file.writelines(
115-
float_buffer.astype(bytes) + b'\n'
116-
for float_buffer in np.array_split(float_array, num_splits)
117-
)
118-
119-
120-
def save_remove(filename: str):
106+
def safe_remove(filename: str):
121107
if (filename and
122108
filename != STDOUT_FILENAME and
123109
Path(filename).exists()):
@@ -141,8 +127,8 @@ def write_mappability_files(unique_count_filenames: list[Path],
141127
raise ValueError("Must specify at least one output file")
142128

143129
# Delete any existing mappability files if they exist
144-
save_remove(single_read_bed_filename)
145-
save_remove(multi_read_wig_filename)
130+
safe_remove(single_read_bed_filename)
131+
safe_remove(multi_read_wig_filename)
146132

147133
# For every unique length file specified
148134
for unique_count_filename in unique_count_filenames:

0 commit comments

Comments
 (0)