class StrandedBigWigExtractor: """Big-wig file extractor NOTE: The extractor is not thread-save. If you with to use it with multiprocessing, create a new extractor object in each process. # Arguments bigwig_file: path to the bigwig file """ def __init__(self, bigwig_file, interval_transform=None, use_strand=False, nan_as_zero=True): self.nan_as_zero = nan_as_zero self.use_strand = use_strand self.bigwig_file = bigwig_file self.interval_transform = interval_transform self.batch_extractor = None def extract_single(self, interval): if self.batch_extractor is None: from genomelake.extractors import BigwigExtractor self.batch_extractor = BigwigExtractor(self.bigwig_file) if self.interval_transform is not None: interval = self.interval_transform(interval) arr = self.batch_extractor([interval], nan_as_zero=self.nan_as_zero)[0] if self.use_strand and interval.strand == '-': arr = arr[::-1] return arr def extract(self, intervals, progbar=False): return np.stack([ self.extract_single(interval) for interval in tqdm(intervals, disable=not progbar) ]) def close(self): return self.batch_extractor.close()
def test_bigwig_extractor(test_bigwig_and_intervals): bw_path, intervals, expected_data = test_bigwig_and_intervals extractor = BigwigExtractor(bw_path) data = extractor(intervals) extractor.close() assert (data == expected_data).all()