11import csv
22import logging
3- import warnings
43import os
54import sys
5+ import warnings
66from collections import defaultdict
77from itertools import groupby
88
@@ -99,8 +99,10 @@ def get_traces(data, samples, outliers, distance_threshold, slop):
9999 )
100100 except IndexError as e :
101101 # x_values[0] is the first data point
102- print ("index error: %s\n index_values[0]: %d, distance_idx: %d" % (
103- e , index_values [0 ], distance_idx ))
102+ print (
103+ "index error: %s\n index_values[0]: %d, distance_idx: %d"
104+ % (e , index_values [0 ], distance_idx )
105+ )
104106 break
105107 extension_length -= (
106108 data ["x" ][index_values [0 ] - distance_idx + 1 ]
@@ -167,6 +169,7 @@ def parse_sex_groups(filename, sample_col, sex_col):
167169 groups [row [sex_col ]].append (row [sample_col ])
168170 return groups
169171
172+
170173def normalize_depths (path , sex_chroms , median_window = 7 ):
171174 filename , ext = os .path .splitext (path )
172175 if ext == ".gz" :
@@ -183,32 +186,37 @@ def normalize_depths(path, sex_chroms, median_window=7):
183186
184187 extras = []
185188 for s in sex_chroms :
186- if s .startswith ("chr" ): extras .append (s [3 :])
187- else : extras .append ("chr" + s )
189+ if s .startswith ("chr" ):
190+ extras .append (s [3 :])
191+ else :
192+ extras .append ("chr" + s )
188193 sex_chroms = sex_chroms + extras
189194
190195 # median per site
191196 autosome = ~ np .asarray (df .iloc [:, 0 ].isin (sex_chroms ))
192197
193198 with warnings .catch_warnings ():
194- warnings .filterwarnings (' ignore' , r' All-NaN (slice|axis) encountered' )
199+ warnings .filterwarnings (" ignore" , r" All-NaN (slice|axis) encountered" )
195200 site_median = np .nanmedian (df .iloc [:, 3 :], axis = 1 )
196201
197- with np .errstate (invalid = ' ignore' ):
202+ with np .errstate (invalid = " ignore" ):
198203 site_median [np .isnan (site_median ) | (site_median < 0.03 )] = 1
199204 # divide autosomes by median at each site so a given block is centered at
200205 # middle sample.
201206 for i in range (3 , df .shape [1 ]):
202207 df .iloc [:, i ] = np .where (autosome , df .iloc [:, i ] / site_median , df .iloc [:, i ])
203208 if median_window > 1 :
204- df .iloc [:, i ] = df .iloc [:, i ].rolling (median_window ).median () #pd.rolling_median(df.iloc[:, i], median_window)
209+ df .iloc [:, i ] = (
210+ df .iloc [:, i ].rolling (median_window ).median ()
211+ ) # pd.rolling_median(df.iloc[:, i], median_window)
205212 inan = np .asarray (np .isnan (df .iloc [:, i ]))
206213 df .iloc [inan , i ] = 0.0
207- #df.to_csv(path_or_buf=output_bed, sep="\t", na_rep=0.0, index=False,
214+ # df.to_csv(path_or_buf=output_bed, sep="\t", na_rep=0.0, index=False,
208215 # compression='gzip',
209216 # float_format="%.2f")
210217 return df
211- #return output_bed
218+ # return output_bed
219+
212220
213221def identify_outliers (a , threshold = 3.5 ):
214222 a = np .asarray (a , dtype = np .float32 )
@@ -269,7 +277,7 @@ def parse_bed(
269277 slop = 500000 ,
270278 min_samples = 8 ,
271279 skip_norm = False ,
272- window = 1
280+ window = 1 ,
273281):
274282 bed_traces = dict ()
275283 # chromosomes, in order of appearance
@@ -286,15 +294,13 @@ def parse_bed(
286294 else :
287295 df = normalize_depths (path , sex_chroms , median_window = window )
288296
289- if True : # temporary to get sane diff.
297+ if True : # temporary to get sane diff.
290298 header = list (df .columns )
291299 samples = header [3 :]
292300 if groups :
293301 valid = validate_samples (samples , groups )
294302 if not valid :
295- logger .critical (
296- "sample ID mismatches exist between ped and bed"
297- )
303+ logger .critical ("sample ID mismatches exist between ped and bed" )
298304 sys .exit (1 )
299305 for chr , entries in df .groupby (header [0 ], as_index = False , sort = False ):
300306
@@ -334,8 +340,9 @@ def parse_bed(
334340 bounds ["upper" ].append ([])
335341 bounds ["lower" ].append ([])
336342 if all_points :
337- sample_values = np .minimum (3 , np .asarray (row [3 :],
338- dtype = np .float32 ))
343+ sample_values = np .minimum (
344+ 3 , np .asarray (row [3 :], dtype = np .float32 )
345+ )
339346 for i , s in enumerate (samples ):
340347 data [s ].append (float (sample_values [i ]))
341348 else :
0 commit comments