def _biasVplotHelper(arg): """function to make vplot for a particular set of bed regions """ (chunks, params) = arg mat = np.zeros((params.upper-params.lower,2*params.flank+1)) for chunk in chunks: try: chunk.center() biastrack = InsertionBiasTrack(chunk.chrom, chunk.start - params.flank - 1 - (params.upper/2), chunk.end + params.flank + params.upper/2+1) if params.bg is not None: biastrack.read_track(params.bg, empty = 0) else: biastrack.computeBias(params.fasta, params.chrs, params.pwm) biasmat = BiasMat2D(chunk.chrom, chunk.start - params.flank - 1, chunk.end + params.flank, params.lower, params.upper) biasmat.makeBiasMat(biastrack) biasmat.normByInsertDist(params.fragmentsizes) add = biasmat.get(start = chunk.start - params.flank, end = chunk.end + params.flank, flip = (chunk.strand == "-")) if params.scale: mat += add/np.sum(add) else: mat += add except Exception as e: print('Caught exception when processing:\n' + chunk.asBed() + "\n") traceback.print_exc() print() raise e return mat
def makeBiasMat(self): self.bias_mat = BiasMat2D(self.chrom, self.start - self.params.flank, self.end + self.params.flank, 0, self.params.upper) if self.params.fasta is not None: bias_track = InsertionBiasTrack(self.chrom, self.start - self.params.window - self.params.upper/2, self.end + self.params.window + self.params.upper/2 + 1, log = True) bias_track.computeBias(self.params.fasta, self.params.chrs, self.params.pwm) self.bias_mat.makeBiasMat(bias_track)
def getBias(self): """get bias""" self.bias = InsertionBiasTrack(self.chrom, self.start, self.end, log=True) if self.params.fasta is not None: self.bias.computeBias(self.params.fasta, self.params.chrs, self.params.pwm)
def setUp(self): """setup Test_BiasMat class with construction of a biasmat""" bed_list = ChunkList.read('example/example.bed') self.chunk = bed_list[0] self.biastrack = InsertionBiasTrack(self.chunk.chrom, self.chunk.start, self.chunk.end) self.biastrack.read_track('example/example.Scores.bedgraph.gz') self.biasmat = BiasMat2D(self.chunk.chrom, self.chunk.start + 100, self.chunk.end - 100, 100, 200) self.biasmat.makeBiasMat(self.biastrack)
def setUp(self): """ set up class for testing variance calculation for background signal """ bed_list = ChunkList.read('example/example.bed') chunk = bed_list[0] vmat = V.VMat.open('example/example.VMat') biastrack = InsertionBiasTrack(chunk.chrom, chunk.start, chunk.end) biastrack.read_track('example/example.Scores.bedgraph.gz') biasmat = BiasMat2D(chunk.chrom,chunk.start+200,chunk.end-200,100,250) biasmat.makeBiasMat(biastrack) self.signaldist = Nuc.SignalDistribution(chunk.start+300,vmat,biasmat,35)
def _biasHelper(arg): """Helper function to multiprocess computation of bias tracks""" (chunk, params) = arg try: bias = InsertionBiasTrack(chunk.chrom, chunk.start, chunk.end) bias.computeBias(params.fasta, params.chrs, params.pwm) except Exception as e: print("Caught exception when processing:\n" + chunk.asBed() + "\n") traceback.print_exc() print() raise e return bias
def _biasHelper(arg): """Helper function to multiprocess computation of bias tracks""" (chunk, params) = arg try: bias = InsertionBiasTrack(chunk.chrom, chunk.start, chunk.end) bias.computeBias(params.fasta, params.chrs, params.pwm) except Exception as e: print(('Caught exception when processing:\n' + chunk.asBed() + "\n")) traceback.print_exc() print() raise e return bias
def makeBiasMat(self): self.bias_mat = BiasMat2D(self.chrom, self.start - self.params.window, self.end + self.params.window, 0, self.params.upper) bias_track = InsertionBiasTrack(self.chrom, self.start - self.params.window - self.params.upper/2, self.end + self.params.window + self.params.upper/2 + 1, log = True) if self.params.fasta is not None: bias_track.computeBias(self.params.fasta, self.params.chrs, self.params.pwm) self.bias_mat.makeBiasMat(bias_track) self.bias_mat_prenorm = BiasMat2D(self.chrom, self.start - self.params.window, self.end + self.params.window, 0, self.params.upper) self.bias_mat_prenorm.mat = copy(self.bias_mat.mat) self.bias_mat.normByInsertDist(self.params.fragmentsizes)
def setUp(self): """setup Test_BiasMat class with construction of a biasmat""" bed_list = ChunkList.read('example/example.bed') self.chunk = bed_list[0] self.biastrack = InsertionBiasTrack(self.chunk.chrom, self.chunk.start, self.chunk.end) self.biastrack.read_track('example/example.Scores.bedgraph.gz') self.biasmat = BiasMat2D(self.chunk.chrom,self.chunk.start+100,self.chunk.end-100,100,200) self.biasmat.makeBiasMat(self.biastrack)
def _biasVplotHelper(arg): """function to make vplot for a particular set of bed regions """ (chunks, params) = arg mat = np.zeros((params.upper - params.lower, 2 * params.flank + 1)) for chunk in chunks: try: chunk.center() biastrack = InsertionBiasTrack( chunk.chrom, chunk.start - params.flank - 1 - (params.upper / 2), chunk.end + params.flank + params.upper / 2 + 1) if params.bg is not None: biastrack.read_track(params.bg, empty=0) else: biastrack.computeBias(params.fasta, params.chrs, params.pwm) biasmat = BiasMat2D(chunk.chrom, chunk.start - params.flank - 1, chunk.end + params.flank, params.lower, params.upper) biasmat.makeBiasMat(biastrack) biasmat.normByInsertDist(params.fragmentsizes) add = biasmat.get(start=chunk.start - params.flank, end=chunk.end + params.flank, flip=(chunk.strand == "-")) if params.scale: mat += add / np.sum(add) else: mat += add except Exception as e: print('Caught exception when processing:\n' + chunk.asBed() + "\n") traceback.print_exc() print() raise e return mat
class Test_BiasMat(TestCase): """test construction of BiasMat""" def setUp(self): """setup Test_BiasMat class with construction of a biasmat""" bed_list = ChunkList.read('example/example.bed') self.chunk = bed_list[0] self.biastrack = InsertionBiasTrack(self.chunk.chrom, self.chunk.start, self.chunk.end) self.biastrack.read_track('example/example.Scores.bedgraph.gz') self.biasmat = BiasMat2D(self.chunk.chrom,self.chunk.start+100,self.chunk.end-100,100,200) self.biasmat.makeBiasMat(self.biastrack) def test_biasmat1(self): """test case1 for biasmat""" a1 = self.biastrack.get(pos = self.biasmat.start - 49) a2 = self.biastrack.get(pos = self.biasmat.start + 50) correct = np.exp(a1+a2) self.assertTrue(abs(correct - self.biasmat.mat[0,0])<0.01*correct) def test_biasmat2(self): """test case2 for biasmat""" a1 = self.biastrack.get(pos = self.biasmat.start + 145) a2 = self.biastrack.get(pos = self.biasmat.start + 295) correct = np.exp(a1+a2) self.assertTrue(abs(correct - self.biasmat.mat[51,220]) < 0.01*correct) def test_normByInsertDist(self): """test that normalization by insert distribution works as expected""" isizes = FragmentSizes(lower=100,upper=200, vals = np.array(range(100,200))) self.biasmat.normByInsertDist(isizes) a1 = self.biastrack.get(pos = self.biasmat.start -50) a2 = self.biastrack.get(pos = self.biasmat.start + 50) correct = np.exp(a1+a2)*isizes.get(size = 101) self.assertTrue(abs(correct - self.biasmat.mat[1,0])<0.01*correct)
class Test_BiasMat(TestCase): """test construction of BiasMat""" def setUp(self): """setup Test_BiasMat class with construction of a biasmat""" bed_list = ChunkList.read('example/example.bed') self.chunk = bed_list[0] self.biastrack = InsertionBiasTrack(self.chunk.chrom, self.chunk.start, self.chunk.end) self.biastrack.read_track('example/example.Scores.bedgraph.gz') self.biasmat = BiasMat2D(self.chunk.chrom, self.chunk.start + 100, self.chunk.end - 100, 100, 200) self.biasmat.makeBiasMat(self.biastrack) def test_biasmat1(self): """test case1 for biasmat""" a1 = self.biastrack.get(pos=self.biasmat.start - 49) a2 = self.biastrack.get(pos=self.biasmat.start + 50) correct = np.exp(a1 + a2) self.assertTrue(abs(correct - self.biasmat.mat[0, 0]) < 0.01 * correct) def test_biasmat2(self): """test case2 for biasmat""" a1 = self.biastrack.get(pos=self.biasmat.start + 145) a2 = self.biastrack.get(pos=self.biasmat.start + 295) correct = np.exp(a1 + a2) self.assertTrue( abs(correct - self.biasmat.mat[51, 220]) < 0.01 * correct) def test_normByInsertDist(self): """test that normalization by insert distribution works as expected""" isizes = FragmentSizes(lower=100, upper=200, vals=np.array(range(100, 200))) self.biasmat.normByInsertDist(isizes) a1 = self.biastrack.get(pos=self.biasmat.start - 50) a2 = self.biastrack.get(pos=self.biasmat.start + 50) correct = np.exp(a1 + a2) * isizes.get(size=101) self.assertTrue(abs(correct - self.biasmat.mat[1, 0]) < 0.01 * correct)
class NFRChunk(Chunk): """Class for storing and determining collection of nfr positions """ def __init__(self, chunk): self.start = chunk.start self.end = chunk.end self.chrom = chunk.chrom self.nfrs = [] def initialize(self, parameters): self.params = parameters def getOcc(self): """gets occupancy track-- reads in from bedgraph""" self.occ = Track(self.chrom, self.start, self.end, "Occupancy") self.occ.read_track(self.params.occ_track) #lower_file = self.params.occ_track[:-11] + 'lower_bound.bedgraph.gz' #self.occ_lower = Track(self.chrom,self.start,self.end,"Occupancy") #self.occ_lower.read_track(lower_file) upper_file = self.params.occ_track[:-11] + 'upper_bound.bedgraph.gz' self.occ_upper = Track(self.chrom, self.start, self.end, "Occupancy") self.occ_upper.read_track(upper_file) def getIns(self): """gets insertion track-- reads in from bedgraph""" if self.params.ins_track is None: self.ins = InsertionTrack(self.chrom, self.start, self.end) self.ins.calculateInsertions(self.params.bam) else: self.ins = Track(self.chrom, self.start, self.end, "Insertion") self.ins.read_track(self.params.ins_track) def getBias(self): """get bias""" self.bias = InsertionBiasTrack(self.chrom, self.start, self.end, log=True) if self.params.fasta is not None: self.bias.computeBias(self.params.fasta, self.params.chrs, self.params.pwm) def findNFRs(self): """find NFR regions""" region = np.ones(self.length()) tbx = pysam.TabixFile(self.params.calls) nucs = [] if self.chrom in tbx.contigs: for row in tbx.fetch(self.chrom, self.start, self.end, parser=pysam.asTuple()): nucs.append(int(row[1])) for j in xrange(1, len(nucs)): left = nucs[j - 1] + 73 right = nucs[j] - 72 if right <= left: continue candidate = NFR(left, right, self) if candidate.min_upper < self.params.max_occ_upper and candidate.occ < self.params.max_occ: self.nfrs.append(candidate) def process(self, params): """wrapper to carry out all methods needed to call nucleosomes and nfrs""" self.initialize(params) self.getOcc() self.getIns() self.getBias() self.findNFRs() def removeData(self): """remove data from chunk-- deletes all attributes""" names = self.__dict__.keys() for name in names: delattr(self, name)