def _bigwig_extractor(datafile, intervals, **kwargs): width = intervals[0].stop - intervals[0].start data = np.zeros((len(intervals), 1, 1, width)) wWigIO.open(datafile) for index, interval in enumerate(intervals): wWigIO.getData(datafile, interval.chrom, interval.start, interval.stop, data[index, 0, 0, :]) wWigIO.close(datafile) return data
def main(): try: infile = sys.argv[1] COUNT_U = int(sys.argv[2]) COUNT_M = int(sys.argv[3]) CUTOFF = int(sys.argv[4]) except IndexError: show_help() sys.exit(1) # l_line= f_cntfile.readlines() CHROM = infile.split("/")[-1].split(".")[0] DEPTH = 3 tb_file = tabix.open(infile) m_NDR = NDR(CHROM, COUNT_U, COUNT_M, tb_file, CUTOFF, DEPTH, bin_len=40, step_len=20, dist_len=140) f_infile = gzip.open(infile, "rb") for line in f_infile: line = line.strip("\n") f = line.split() pos_beg = int(f[1]) cnt_umt = int(f[3]) cnt_met = int(f[4]) cnt_tot = cnt_umt + cnt_met if cnt_tot < DEPTH: continue m_NDR.parse_line(pos_beg, cnt_umt, cnt_met) f_infile.close() wWigIO.close()
def extract_bigwig_to_npy(bigwig, output_dir, dtype=np.float32): wWigIO.open(bigwig) chrom_sizes = wWigIO.getChromSize(bigwig) file_shapes = {} for chrom, size in zip(*chrom_sizes): data = np.empty(size) wWigIO.getData(bigwig, chrom, 0, size, data) np.save('{}.npy'.format(os.path.join(output_dir, chrom)), data.astype(dtype)) file_shapes[chrom] = data.shape wWigIO.close(bigwig) with open(os.path.join(output_dir, 'metadata.json'), 'w') as fp: json.dump( { 'file_shapes': file_shapes, 'type': 'array', 'source': bigwig }, fp)
def getChromSizesWigIO(self): wWigIO.open(self.path) out = wWigIO.getChromSize(self.path) out = dict(zip(out[0], out[1])) wWigIO.close(self.path) return out
# ------------------------------------ # Misc functions # ------------------------------------ # ------------------------------------ # Classes # ------------------------------------ # ------------------------------------ # Main # ------------------------------------ if __name__=="__main__": # get information from bigwig file wWigIO.open('test.bw') chroms = wWigIO.getChromSize('test.bw') wigs = wWigIO.getIntervals('test.bw', 'chr1', 10, 200) wWigIO.close('test.bw') print wigs # bigwig -> wig wWigIO.bigWigToWig('test.bw','test.wig') # write the chrom sizes into test.sizes with open('test.sizes','w') as fh: for chrom in chroms: print >>fh, chrom+"\t"+str(chroms[chrom]) # wig -> bigwig wWigIO.wigToBigWig('test.wig','test.sizes','test2.bw')
def __del__(self): ''' Close BigWig file. Avoid memory leaks.''' wWigIO.close(self.fname)
def close(self): ''' Close BigWig file. ''' wWigIO.close(self.fname)
# ------------------------------------ # Misc functions # ------------------------------------ # ------------------------------------ # Classes # ------------------------------------ # ------------------------------------ # Main # ------------------------------------ if __name__ == "__main__": # get information from bigwig file wWigIO.open('test.bw') chroms = wWigIO.getChromSize('test.bw') wigs = wWigIO.getIntervals('test.bw', 'chr1', 10, 200) wWigIO.close('test.bw') print wigs # bigwig -> wig wWigIO.bigWigToWig('test.bw', 'test.wig') # write the chrom sizes into test.sizes with open('test.sizes', 'w') as fh: for chrom in chroms: print >> fh, chrom + "\t" + str(chroms[chrom]) # wig -> bigwig wWigIO.wigToBigWig('test.wig', 'test.sizes', 'test2.bw')