def uniqueCombosResponse(form): # print "content-type: text/json\n" tl = twolocus.TwoLocus("/csbiodata/public/www.csbio.unc.edu/htdocs/sgreens/pairwise_origins/") strains = [[], []] for set_num, set_id in enumerate(["background", "foreground"]): for _, _, value, _ in helper.STRAIN_SETS: new_strains = form.getvalue(value + set_id) if type(new_strains) is list: strains[set_num] += new_strains elif new_strains is not None: strains[set_num].append(new_strains) # print '\n'.join(hex(line[0]) + ' ' + ' '.join(map(str, line[1:])) for line in tl.unique_combos(strains[0], strains[1])) data = json.dumps(tl.unique_combos(strains[0], strains[1]), cls=helper.NumpyEncoder) # with open('unique.json', "w+") as fp: # with open('unique.json', "r") as fp: # json.dump(tl.unique_combos(strains[0], strains[1]), fp, cls=helper.NumpyEncoder) # data = json.load(fp) # data = '0' # panel = markup.page() # panel.iframe(src='../sgreens/pairwise_origins/pairwiseGenome.html', chartWidth="100%", chartHeight="1000px") # return panel # print "content-type: text/html\n" # with open('../sgreens/pairwise_origins/pairwiseGenome.html') as fp: # print fp.read()<!DOCTYPE html> helper.visualize_genome(data, tl)
def originsVisualizationResponse(form): # print "content-type: text/json\n" coarse_cutoff = 1e7 panel = markup.page() plot_file = "ss_origins.html" bokeh.plotting.output_file(plot_file) tl = twolocus.TwoLocus("/csbiodata/public/www.csbio.unc.edu/htdocs/sgreens/pairwise_origins/") strains = [] for _, _, value, _ in helper.STRAIN_SETS: new_strains = form.getvalue(value) if type(new_strains) is list: strains += new_strains elif new_strains is not None: strains.append(new_strains) data, colors = tl.pairwise_frequencies(strains) absent_regions = tl.absent_regions(strains) plot = bokeh.plotting.figure( y_range=bokeh.models.Range1d(start=tl.offsets[-1] + 10e7, end=0), height=750, width=750, background_fill_color="black", tools=[ "tap", "resize", bokeh.models.HoverTool(names=["chroms"], tooltips=[("Proximal", "@proximal"), ("Distal", "@distal")]), ], ) plot.axis.visible = False plot.grid.grid_line_color = None for combo_regions, color in zip(data, colors): region_widths = np.subtract(combo_regions[1], combo_regions[0]) region_heights = np.subtract(combo_regions[3], combo_regions[2]) coarse_indices = np.logical_or(region_widths > coarse_cutoff, region_heights > coarse_cutoff) region_widths = region_widths[coarse_indices] region_heights = region_heights[coarse_indices] x_positions = np.add(np.array(combo_regions[0])[coarse_indices], region_widths / 2) y_positions = np.add(np.array(combo_regions[2])[coarse_indices], region_heights / 2) plot.rect( x_positions, y_positions, region_widths, region_heights, color="#" + hex(color)[2:].zfill(6), fill_alpha=1.0 / len(strains), line_alpha=0, ) break for combo_regions in absent_regions: region_widths = np.subtract(combo_regions[1], combo_regions[0]) region_heights = np.subtract(combo_regions[3], combo_regions[2]) coarse_indices = np.logical_or(region_widths > coarse_cutoff, region_heights > coarse_cutoff) region_widths = region_widths[coarse_indices] region_heights = region_heights[coarse_indices] x_positions = np.add(np.array(combo_regions[0])[coarse_indices], region_widths / 2) y_positions = np.add(np.array(combo_regions[2])[coarse_indices], region_heights / 2) plot.rect(x_positions, y_positions, region_widths, region_heights, color="white") break chrom_data = dict(x=[], y=[], width=[], height=[], proximal=[], distal=[]) for i in xrange(len(tl.sizes)): for j in xrange(i, len(tl.sizes)): chrom_data["x"].append(tl.offsets[i] + tl.sizes[i] / 2) chrom_data["y"].append(tl.offsets[j] + tl.sizes[j] / 2) chrom_data["width"].append(tl.sizes[i]) chrom_data["height"].append(tl.sizes[j]) chrom_data["proximal"].append(twolocus.INT_TO_CHROMO[i + 1]) chrom_data["distal"].append(twolocus.INT_TO_CHROMO[j + 1]) chrom_data_source = bokeh.models.ColumnDataSource(data=chrom_data) plot.rect( x="x", y="y", width="width", height="height", fill_alpha=0, line_color="grey", hover_alpha=0.5, name="chroms", source=chrom_data_source, ) text_offsets = tl.offsets.copy()[:-1] text_offsets[-3] += 5e7 plot.text( x=-15e7, y=[offset + 10e7 for offset in text_offsets], text=twolocus.INT_TO_CHROMO[1:], text_color="white", text_font_size="10pt", ) plot.text( x=text_offsets, y=tl.offsets[-1] + 10e7, text=twolocus.INT_TO_CHROMO[1:], text_color="white", text_font_size="8pt", ) inset_plot = bokeh.plotting.figure() inset_plot.x_range.bounds = "auto" inset_plot.y_range.bounds = "auto" inset_data_source = bokeh.models.ColumnDataSource(data=dict(x=[], y=[], width=[], height=[])) inset_plot.rect("x", "y", "width", "height", color="black") bokeh.plotting.show(bokeh.io.hplot(plot, inset_plot)) with open(plot_file) as fp: panel.add(fp.read()) return panel helper.visualize_genome(data, tl, len(strains))