@@ -95,7 +95,8 @@ def write_single_read_bed(bed_file: BinaryIO,
95
95
96
96
def write_multi_read_wig (wig_file : BinaryIO ,
97
97
multi_read_mappability : npt .NDArray [np .float64 ],
98
- chr_name : str ):
98
+ chr_name : str ,
99
+ decimal_places : int = 2 ):
99
100
100
101
# Write out the fixedStep declaration
101
102
wig_file .write (WIG_FIXED_STEP_DECLARATION_FORMAT
@@ -104,8 +105,19 @@ def write_multi_read_wig(wig_file: BinaryIO,
104
105
105
106
for mappability_chunk in np .nditer (multi_read_mappability ,
106
107
flags = ['external_loop' , 'buffered' ]):
107
- wig_file .writelines (f"{ value } \n " .encode ()
108
- for value in mappability_chunk )
108
+ wig_file .writelines (
109
+ float_format (value , decimal_places ) + b'\n ' # type: ignore
110
+ for value in mappability_chunk )
111
+
112
+
113
+ def float_format (value : float ,
114
+ decimal_places : int ) -> bytes :
115
+ # Special case for 0, ignore trailing decimal places
116
+ if value == 0.0 :
117
+ return b"0.0"
118
+
119
+ # Otherwise print the specified number of decimal places
120
+ return f"{ value :.{decimal_places }f} " .encode ()
109
121
110
122
111
123
def safe_remove (filename : str ):
@@ -199,16 +211,21 @@ def write_mappability_files(unique_count_filenames: list[Path],
199
211
verbose_print (verbose , f"Appending multi-read mappability regions"
200
212
f" to { multi_read_wig_filename } " )
201
213
214
+ # Calculate the number of decimal places
215
+ decimal_places = int (np .ceil (np .log10 (kmer_length )))
216
+
202
217
if multi_read_wig_filename == STDOUT_FILENAME :
203
218
write_multi_read_wig (sys .stdout .buffer ,
204
219
multi_read_mappability ,
205
- chr_name )
220
+ chr_name ,
221
+ decimal_places )
206
222
else :
207
223
with open (multi_read_wig_filename , "ab" ) as \
208
224
multi_read_wig_file :
209
225
write_multi_read_wig (multi_read_wig_file ,
210
226
multi_read_mappability ,
211
- chr_name )
227
+ chr_name ,
228
+ decimal_places )
212
229
213
230
214
231
def main (args ):
0 commit comments