def draw_trace(self, dotplot): vis = [] # get current view view = dotplot.win.get_visible() for region in self.regions1: if region in dotplot.layout1: start = dotplot.layout1[region] else: continue # only draw regions in view end = start + region.length() if util.overlap(view[0], view[2], start, end): vis.extend([color(* self.trace_color), lines(start, 0, start, dotplot.plot_size[1]), shapes.box(start, 0, end, dotplot.plot_size[1])]) for region in self.regions2: if region in dotplot.layout2: start = dotplot.layout2[region] else: continue # only draw regions in view end = start + region.length() if util.overlap(view[1], view[3], start, end): vis.extend([color(* self.trace_color), lines(0, start, dotplot.plot_size[0], start), shapes.box(0, start, dotplot.plot_size[0], end)]) return group(*vis)
def overlap(region1, region2): """Tests whether two regions overlap""" return region1.seqname == region2.seqname and \ region1.species == region2.species and \ util.overlap(region1.start, region1.end, region2.start, region2.end)
def find_syntenic_neighbors(hits, radius, radius2=None): """ For each hit find the neighboring hits that are syntenic. hits -- iterable of tuples (region1, region2, extra) radius -- radius of window in query genome radius2 -- radius of window in subject genome (default=radius) hits must be sorted by query region species, chrom, and start """ if radius2 is None: radius2 = radius for hits2 in iter_chroms(hits): for center, upstream, downstream in iter_windows(hits2, radius): start = center[1].start - radius2 end = center[1].end + radius2 syntenic = [] for hit in chain(upstream, downstream): # determine which subjects are wihtin the window of if (hit[1].species == center[1].species and hit[1].seqname == center[1].seqname and util.overlap(start, end, hit[1].start, hit[1].end)): syntenic.append(hit) yield (center, syntenic)
def get(self, species, chrom, start, end): records = [] for record in self.lookup[(species, chrom)]: if util.overlap(start, end, record["start"], record["end"]): records.append(record) return records
def layout_regions(self, layout, chromLookup, regions, chromLayout): for region in regions: chrom = chromLookup.get((region.species, region.seqname), None) if chrom in chromLayout and \ util.overlap(chrom.start, chrom.end, region.start, region.end): chrompos = chromLayout[chrom] layout[region] = chrompos + region.start - chrom.start
def filter_blocks(self, blocks, ref_chrom, start, end): """Filter blocks for those that contain the chromosome. Also the matching side of the block is returned """ for block in blocks: if ((block.region1.species, block.region1.seqname) not in self.chroms_lookup or (block.region2.species, block.region2.seqname) not in self.chroms_lookup): continue if (block.region1.seqname == ref_chrom.seqname and util.overlap( block.region1.start, block.region1.end, start, end)): yield (block, 0) elif (block.region2.seqname == ref_chrom.seqname and util.overlap( block.region2.start, block.region2.end, start, end)): yield (block, 1)
def filter_blocks(self, blocks, ref_chrom, start, end): """Filter blocks for those that contain the chromosome. Also the matching side of the block is returned """ for block in blocks: if ((block.region1.species, block.region1.seqname) not in self.chroms_lookup or (block.region2.species, block.region2.seqname) not in self.chroms_lookup): continue if (block.region1.seqname == ref_chrom.seqname and util.overlap(block.region1.start, block.region1.end, start, end)): yield (block, 0) elif (block.region2.seqname == ref_chrom.seqname and util.overlap(block.region2.start, block.region2.end, start, end)): yield (block, 1)
def draw_trace(self, dotplot): vis = [] # get current view view = dotplot.win.get_visible() for region in self.regions1: if region in dotplot.layout1: start = dotplot.layout1[region] else: continue # only draw regions in view end = start + region.length() if util.overlap(view[0], view[2], start, end): vis.extend([ color(*self.trace_color), lines(start, 0, start, dotplot.plot_size[1]), shapes.box(start, 0, end, dotplot.plot_size[1]) ]) for region in self.regions2: if region in dotplot.layout2: start = dotplot.layout2[region] else: continue # only draw regions in view end = start + region.length() if util.overlap(view[1], view[3], start, end): vis.extend([ color(*self.trace_color), lines(0, start, dotplot.plot_size[0], start), shapes.box(0, start, dotplot.plot_size[0], end) ]) return group(*vis)
def draw(self): assert self.view != None, "Track view not initialized" species = self.view.species chrom = self.view.seqname start = self.view.start end = self.view.end height = self.height regions = filter(lambda x: x.seqname == chrom and x.species == species, self.regions) def click_region(region): return lambda: self.on_click(region) if self.style == 'box': # box style vis = [] vis2 = [] hotspots = [] names = [] for reg in regions: if util.overlap(start, end, reg.start, reg.end): if reg.strand == 1: # positive strand bot = 0 top = height elif reg.strand == -1: # negative strand bot = -height top = 0 else: bot = -height top = height vis.extend([reg.start-start, bot, reg.end-start+1, bot, reg.end-start+1, top, reg.start-start, top]) vis2.extend([reg.start-start, bot, reg.start-start, top]) if 'ID' in reg.data: names.append(text_clip(reg.data['ID'], reg.start-start, bot, reg.end-start+1, top, 4, self.textSize)) if self.on_click: hotspots.append(hotspot("click", reg.start-start, top, reg.end-start+1, bot, click_region(reg))) return group(translate( self.pos[0], self.pos[1] + self.size[1] / 2.0, color(0,0,0), lines(0, 0, end-start+1, 0), self.color, quads(*vis), lines(*vis2), group(*hotspots), self.text_color, *names)) elif self.style == 'line': # line style vis = [] for reg in regions: if util.overlap(start, end, reg.start, reg.end): if reg.strand != 0: vis.extend([reg.start-start, 0, reg.start-start, reg.strand*height]) else: vis.extent([reg.start-start, -.5 * height, reg.start-start, .5 * height]) return group(translate(self.pos[0], self.pos[1] + self.size[1] / 2.0, color(0,0,0), lines(0, 0, end-start+1, 0), self.color, lines(* vis))) else: # custom style regions2 = (reg for reg in regions if util.overlap(start, end, reg.start, reg.end)) return group(translate(self.pos[0], self.pos[1] + self.size[1] / 2.0, self.style(regions2, start)))
def get_region_pos(self, reg): if reg.seqname == self.view.seqname and \ util.overlap(self.view.start, self.view.end, reg.start, reg.end): return (self.pos[0] + reg.start - self.view.start, self.pos[1]) else: return None
def draw(self): assert self.view != None, "Track view not initialized" species = self.view.species chrom = self.view.seqname start = self.view.start end = self.view.end height = self.height regions = filter(lambda x: x.seqname == chrom and x.species == species, self.regions) def click_region(region): return lambda: self.on_click(region) if self.style == 'box': # box style vis = [] vis2 = [] hotspots = [] names = [] for reg in regions: if util.overlap(start, end, reg.start, reg.end): if reg.strand == 1: # positive strand bot = 0 top = height elif reg.strand == -1: # negative strand bot = -height top = 0 else: bot = -height top = height vis.extend([ reg.start - start, bot, reg.end - start + 1, bot, reg.end - start + 1, top, reg.start - start, top ]) vis2.extend( [reg.start - start, bot, reg.start - start, top]) if 'ID' in reg.data: names.append( text_clip(reg.data['ID'], reg.start - start, bot, reg.end - start + 1, top, 4, self.textSize)) if self.on_click: hotspots.append( hotspot("click", reg.start - start, top, reg.end - start + 1, bot, click_region(reg))) return group( translate(self.pos[0], self.pos[1] + self.size[1] / 2.0, color(0, 0, 0), lines(0, 0, end - start + 1, 0), self.color, quads(*vis), lines(*vis2), group(*hotspots), self.text_color, *names)) elif self.style == 'line': # line style vis = [] for reg in regions: if util.overlap(start, end, reg.start, reg.end): if reg.strand != 0: vis.extend([ reg.start - start, 0, reg.start - start, reg.strand * height ]) else: vis.extent([ reg.start - start, -.5 * height, reg.start - start, .5 * height ]) return group( translate(self.pos[0], self.pos[1] + self.size[1] / 2.0, color(0, 0, 0), lines(0, 0, end - start + 1, 0), self.color, lines(*vis))) else: # custom style regions2 = (reg for reg in regions if util.overlap(start, end, reg.start, reg.end)) return group( translate(self.pos[0], self.pos[1] + self.size[1] / 2.0, self.style(regions2, start)))