def read_track(self, bedgraph, start=None, end=None, empty=np.nan, flank=None): """Read track values from BigWig file handle""" if start: self.start = start if end: self.end = end if flank: self.start = self.start - flank self.end = self.end + flank handle = BedGraphFile(bedgraph) self.vals = handle.read(self.chrom, self.start, self.end, empty=empty) handle.close()
def read_track(self, bedgraph, start = None, end = None, empty = np.nan, flank = None): """Read track values from BigWig file handle""" if start: self.start = start if end: self.end = end if flank: self.start = self.start - flank self.end = self.end + flank handle = BedGraphFile(bedgraph) self.vals = handle.read(self.chrom, self.start, self.end, empty = empty) handle.close()
def _signalHelper(arg): """Get aggregate signal around a set of sites """ (chunks, params) = arg bg = BedGraphFile(params.bg) agg = np.zeros(params.up + params.down + 1) if params.all: counter = 0 mat = np.zeros((len(chunks), params.up + params.down + 1)) else: agg = np.zeros(params.up + params.down + 1) for chunk in chunks: try: chunk.center() if params.up != 0 and params.down != 0: chunk.slop(chromDict=params.chrs, up=params.up, down=params.down) sig = bg.read(chunk.chrom, chunk.start, chunk.end) if params.up != 0 and params.down != 0 and len(sig) != ( params.up + params.down + 1): if chunk.start == 0: sig = np.hstack( (np.zeros(params.up + params.down + 1 - len(sig)), sig)) else: sig = np.hstack( (sig, np.zeros(params.up + params.down + 1 - len(sig)))) if chunk.strand == "-": sig = sig[::-1] if params.exp: sig = np.exp(sig) if params.positive: sig[sig < 0] = 0 if params.scale: tmp = sig tmp[np.isnan(tmp)] = 0 sig = sig / (np.sum(abs(sig)) + (np.sum(abs(sig)) == 0)) if params.all: mat[counter] = sig counter += 1 else: sig[np.isnan(sig)] = 0 agg += sig except Exception as e: print( ('Caught exception when processing:\n' + chunk.asBed() + "\n")) traceback.print_exc() print() bg.close() raise e bg.close() if params.all: return mat else: return agg
def _signalHelper(arg): """Get aggregate signal around a set of sites """ (chunks, params) = arg bg = BedGraphFile(params.bg) agg = np.zeros(params.up + params.down + 1) if params.all: counter = 0 mat = np.zeros((len(chunks),params.up + params.down + 1)) else: agg = np.zeros(params.up + params.down + 1) for chunk in chunks: try: chunk.center() if params.up!=0 and params.down!=0: chunk.slop(chromDict = params.chrs, up = params.up, down = params.down) sig = bg.read(chunk.chrom, chunk.start, chunk.end) if params.up!=0 and params.down!=0 and len(sig) != (params.up + params.down + 1): if chunk.start == 0: sig = np.hstack((np.zeros(params.up + params.down + 1 - len(sig)), sig)) else: sig = np.hstack((sig,np.zeros(params.up + params.down + 1 - len(sig)))) if chunk.strand == "-": sig = sig[::-1] if params.exp: sig = np.exp(sig) if params.positive: sig[sig<0]=0 if params.scale: tmp = sig tmp[np.isnan(tmp)]=0 sig = sig / (np.sum(abs(sig))+ (np.sum(abs(sig))==0)) if params.all: mat[counter] = sig counter += 1 else: sig[np.isnan(sig)]=0 agg += sig except Exception as e: print('Caught exception when processing:\n' + chunk.asBed() + "\n") traceback.print_exc() print() bg.close() raise e bg.close() if params.all: return mat else: return agg