bokeh.embed.file_html

Here are the examples of the python api bokeh.embed.file_html taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

32 Examples 7

3 View Source File : insights.py
License : MIT License
Project Creator : aavshr

    def __generate_chart(self, buckets: dict):
        p = figure(
            title="Average code review turnarounds",
            x_axis_type="datetime",
            x_axis_label="date",
            y_axis_label="average turnaround (mins)",
            plot_height=800,
            plot_width=800,
        )
        x = list(buckets.keys())
        y = list(buckets.values())
        p.scatter(x, y, color="red")
        p.line(x, y, color="red", legend_label="moving average code review turnaround")
        return file_html(p, CDN, "Average code review turnarounds")

    # get html chart
    def get_chart(self, duration: str, period: str):

3 View Source File : html.py
License : BSD 3-Clause "New" or "Revised" License
Project Creator : holzschu

    def file_contents(self, args, doc):
        '''

        '''
        resources = Resources(mode="cdn", root_dir=None)
        return file_html(doc, resources)

#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

3 View Source File : formatter.py
License : MIT License
Project Creator : massgh

def bokeh2html(bokeh_fig,title=""):
    """Write bokeh figure as HTML string to use in `ipyslide.utils.write`.
    **Parameters**
    
    - bokeh_fig : Bokeh figure instance.
    - title     : Title for figure.
    """
    from bokeh.resources import CDN
    from bokeh.embed import file_html
    return _HTML(file_html(bokeh_fig, CDN, title))

def _bokeh2htmlstr(bokeh_fig,title=""):

3 View Source File : _bokeh_utils.py
License : MIT License
Project Creator : metriculous-ml

def check_that_all_figures_can_be_rendered(figures: Sequence[Figure]) -> None:
    """ Generates HTML for each figure.

    In some cases this reveals issues that might not be noticed if we just instantiated the figures
    without showing them or generating their HTML representations.
    """
    for f in figures:
        html = file_html(f, resources=CDN)
        assert isinstance(html, str)


def hex_to_rgb(hex_color: str) -> Tuple[int, int, int]:

3 View Source File : _classification_figures_bokeh_test.py
License : MIT License
Project Creator : metriculous-ml

def test_bokeh_confusion_matrix__no_crash_when_class_is_never_predicted() -> None:
    lazy_figure = _bokeh_confusion_matrix(
        y_true=np.asarray([0, 1, 2]),
        y_pred=np.asarray([1, 1, 1]),
        class_names=["A", "B", "C"],
        title_rows=["Some", "Title"],
    )
    figure = lazy_figure()

    _ = file_html(figure, resources=CDN)


def test_bokeh_confusion_matrix__no_crash_when_class_is_never_predicted_and_not_in_y_true() -> None:

3 View Source File : _classification_figures_bokeh_test.py
License : MIT License
Project Creator : metriculous-ml

def test_bokeh_confusion_matrix__no_crash_when_class_is_never_predicted_and_not_in_y_true() -> None:
    lazy_figure = _bokeh_confusion_matrix(
        y_true=np.asarray([0, 1, 1]),  # no 2
        y_pred=np.asarray([1, 1, 1]),  # no 2
        class_names=["A", "B", "C"],
        title_rows=["Some", "Title"],
    )
    figure = lazy_figure()

    _ = file_html(figure, resources=CDN)


@pytest.mark.parametrize("use_sample_weights", [False, True])

3 View Source File : _comparison.py
License : MIT License
Project Creator : metriculous-ml

def _html_figure_table(
    model_evaluations: Sequence[Evaluation], include_spacer: Optional[bool]
) -> str:
    html_output = ""

    rows = _figure_rows(model_evaluations, include_spacer=include_spacer)

    if rows:
        html_output += file_html(
            models=column(rows, sizing_mode="scale_width"),
            resources=CDN,
            title="Comparison",
        )

    return html_output


def _figure_rows(

3 View Source File : images.py
License : Apache License 2.0
Project Creator : neptune-ai

def _export_bokeh_figure(chart):
    from bokeh.resources import CDN
    from bokeh.embed import file_html

    html = file_html(chart, CDN)
    buffer = StringIO(html)
    buffer.seek(0)
    return buffer.getvalue()


def _export_pickle(obj):

3 View Source File : chart.py
License : MIT License
Project Creator : neptune-ai

def export_bokeh_figure(chart):
    from io import StringIO
    from bokeh.resources import CDN
    from bokeh.embed import file_html

    html = file_html(chart, CDN)
    buffer = StringIO(html)
    buffer.seek(0)

    return buffer

3 View Source File : panGraph.py
License : MIT License
Project Creator : TF-Chan-Lab

    def drawOverlapGenes(self, geneId):
        #self.record.plot(figure_width=20)
        bokeh_figure = self.record.plot_with_bokeh(figure_width=13, figure_height=8)
        #bokeh.plotting.show(bokeh_figure)

        tool_objs, tool_map = process_tools_arg(bokeh_figure, "xpan,xwheel_zoom,reset,tap")
        bokeh_figure.tools = tool_objs

        drawOverlap = os.path.join(self.outdir, 'drawOverlap_with_Gene-%s.html' % geneId)

        with open(drawOverlap, 'w') as f:
            f.write(file_html(bokeh_figure, CDN, "Nodes overlap with Gene: %s" % geneId))

    def getNodeFromRGFA(self, nodeIdList):

3 View Source File : panGraph.py
License : MIT License
Project Creator : TF-Chan-Lab

    def drawOverlapGenes(self, geneId):
        self.record.plot(figure_width=20)
        bokeh_figure = self.record.plot_with_bokeh(figure_width=13, figure_height=8)
        #bokeh.plotting.show(bokeh_figure)

        tool_objs, tool_map = process_tools_arg(bokeh_figure, "xpan,xwheel_zoom,reset,tap")
        bokeh_figure.tools = tool_objs

        drawOverlap = os.path.join(self.outdir, 'drawOverlap_with_Gene-%s.html' % geneId)

        with open(drawOverlap, 'w') as f:
            f.write(file_html(bokeh_figure, CDN, "Nodes overlap with Gene: %s" % geneId))

    def getNodeFromRGFA(self, nodeIdList):

0 View Source File : pLannotate.py
License : GNU General Public License v3.0
Project Creator : barricklab

def main_batch(input,output,file_name,suffix,yaml_file,linear,html,csv,detailed,no_gbk):
    """
    Annotates engineered DNA sequences, primarily plasmids. Accepts a FASTA or GenBank file and outputs
    a GenBank file with annotations, as well as an optional interactive plasmid map as an HTLM file.
    """
    if not rsc.databases_exist():
        print("Databases not downloaded. Run 'plannotate setupdb' to download databases.")
        sys.exit()

    name, ext = rsc.get_name_ext(input)

    if file_name == "":
        file_name = name

    inSeq = rsc.validate_file(input, ext)

    recordDf = annotate(inSeq, yaml_file, linear, detailed)

    if no_gbk == False:
        gbk = rsc.get_gbk(recordDf, inSeq, linear)
        with open(f"{output}/{file_name}{suffix}.gbk", "w") as handle:
            handle.write(gbk)

    if html:
        bokeh_chart = get_bokeh(recordDf, linear)
        bokeh_chart.sizing_mode = "fixed"
        html = file_html(bokeh_chart, resources = CDN, title = f"{output}.html")
        with open(f"{output}/{file_name}{suffix}.html", "w") as handle:
            handle.write(html)

    if csv:
        csv_df = rsc.get_clean_csv_df(recordDf)
        csv_df.to_csv(f"{output}/{file_name}{suffix}.csv", index = None)


def streamlit_run():

0 View Source File : visual_midi.py
License : MIT License
Project Creator : dubreuia

    def save(self, pm: PrettyMIDI, filepath: str):
        """
        Saves the pretty midi object as a plot file (html) in the provided file. If
        the live reload option is activated, the opened page will periodically
        refresh.

          :param pm: the PrettyMIDI instance to plot
          :param filepath: the file path to save the resulting plot to
          :return: the bokeh plot layout
        """
        plot = self.plot(pm)
        if self._live_reload:
            html = file_html(plot, CDN)
            html = html.replace("  <  /head>", """
               < script type="text/javascript">
                var liveReloadInterval = window.setInterval(function(){
                  location.reload();
                }, 2000);
               < /script>
               < /head>""")
            with open(filepath, 'w') as file:
                file.write(html)
        else:
            output_file(filepath)
            save(plot)
        return plot

    def show(self, pm: PrettyMIDI, filepath: str):

0 View Source File : visual_midi.py
License : MIT License
Project Creator : dubreuia

    def show(self, pm: PrettyMIDI, filepath: str):
        """
        Shows the pretty midi object as a plot file (html) in the browser. If
        the live reload option is activated, the opened page will periodically
        refresh.

          :param pm: the PrettyMIDI instance to plot
          :param filepath: the file path to save the resulting plot to
          :return: the bokeh plot layout
        """
        plot = self.plot(pm)
        if self._live_reload:
            html = file_html(plot, CDN)
            html = html.replace("  <  /head>", """
               < script type="text/javascript">
                var liveReloadInterval = window.setInterval(function(){
                  location.reload();
                }, 2000);
               < /script>
               < /head>""")
            with open(filepath, 'w') as file:
                file.write(html)
            if self._show_counter == 0:
                import webbrowser
                webbrowser.open("file://" + os.path.realpath(filepath), new=2)
        else:
            output_file(filepath)
            show(plot)
        self._show_counter += 1
        return plot

    def show_notebook(self, pm: PrettyMIDI):

0 View Source File : app.py
License : GNU General Public License v3.0
Project Creator : happydasch

    def _output_plotfile(self, model, figid=0, filename=None,
                         template='basic.html.j2'):
        '''
        Outputs the plot file
        '''
        if filename is None:
            tmpdir = tempfile.gettempdir()
            filename = os.path.join(tmpdir, f'bt_bokeh_plot_{figid}.html')

        now = datetime.now()
        env = Environment(loader=PackageLoader('btplotting', 'templates'))
        templ = env.get_template(template)
        templ.globals['now'] = now.strftime('%Y-%m-%d %H:%M:%S')

        html = file_html(model,
                         template=templ,
                         resources=CDN,
                         template_variables=dict(
                             stylesheet=self._output_stylesheet(),
                             show_headline=self.scheme.show_headline,
                             headline=self.scheme.headline),
                         _always_new=True)

        with open(filename, 'w') as f:
            f.write(html)

        return filename

    def is_iplot(self):

0 View Source File : metainfo_plots.py
License : MIT License
Project Creator : skiniry

def single_tran_de(single_tran_de_transcript, sorted_master_list, study_master_list, organism, transcriptome,single_tran_de_study):
	xvals = []
	yvals = []
	labels = []
	range1counts = []
	file_ids = []
	mapped_reads = []
	file_descs = []
	study_names = []
	
	if single_tran_de_study == False:
		for tup in sorted_master_list:
			file_id = tup[0]
			filename = tup[1]
			r1 = float(tup[2])
			r2 = float(tup[3])
			file_desc = tup[4]
			study = tup[5]
			
			ratio = r1/r2
			avg = (r1+r2)/2
			
			xvals.append(avg)
			yvals.append(ratio)
			labels.append(filename)
			range1counts.append(r1)
			file_ids.append(file_id)
			mapped_reads.append(r2)
			file_descs.append(file_desc)
			study_names.append(study)
	else:
		for tup in study_master_list:
			file_id = tup[0]
			study = tup[1]
			r1 = float(tup[2])
			r2 = float(tup[3])

			
			ratio = log(r1/r2,2)
			avg = (r1+r2)/2
			
			xvals.append(avg)
			yvals.append(ratio)
			labels.append(study)
			range1counts.append(r1)
			file_ids.append(file_id)
			mapped_reads.append(r2)
			file_descs.append(study)
			study_names.append(study)
		
		
		
		
	

	full_title = "ORF TPMs ({})"
	x_lab = ''
	y_lab = 'TPM'
	p = figure(plot_width=1300, plot_height=750,x_axis_label=x_lab,  y_axis_label=y_lab,title= full_title,toolbar_location="below",
			   tools = "reset,pan,box_zoom,hover,tap",logo=None)
	p.title.align="center"
	p.xgrid.grid_line_color = "white"
	p.ygrid.grid_line_color = "white"

	hover = p.select(dict(type=HoverTool))
	hover.tooltips = [("Ratio", "@y"),("Max count","@x"),("File name","@labels"),("Range 1 count","@range1counts"),("Mapped reads","@mapped_reads"),("Description","@file_descs"),("Study","@study")]
	source = ColumnDataSource({'x':xvals,'y':yvals,'labels':labels,"range1counts":range1counts,"file_id":file_ids,"mapped_reads":mapped_reads,"file_descs":file_descs,"study":study_names})
	p.scatter('x','y',source=source, alpha=1,color="grey",size=9)
	output_file("scatter10k.html", title="Single transcript differential translation")
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'

	#/saccharomyces_cerevisiae/Gencode_v24/comparison/?files=227,%23ff1f00_228,%233BFF00_231,%23ffffff_232,%23000000_&transcript=YLR162W&normalize=F&cov=T&ambig=F&minread=25&maxread=100
	#/saccharomyces_cerevisiae/Gencode_v24/comparison/?files=227,228,229,%23ff1f00_230,%233BFF00&transcript=YDR003W&normalize=T&cov=T&ambig=T&minread=18&maxread=45
	#url = "http://143.239.109.139/tripsviz/{}/{}/interactive_plot/?transcript=@labels".format(organism, transcriptome)
	#file_string = ""
	#label_string="&labels=RIBO-Seq Cond 1,%23007a02_RIBO-Seq Cond 2,%23960000_mRNA-Seq Cond 1,%2374ed76_mRNA-seq Cond 2,%23ff6d6d"


	# remove the trailing _ in file_string if it's been populated
	#if file_string:
	#	file_string = file_string[:len(file_string)-1]

	url = "http://trips.ucc.ie/{}/{}/interactive_plot/?files=@file_id&tran={}&ambig=F&minread=25&maxread=150".format(organism, transcriptome,single_tran_de_transcript)

	taptool = p.select(type=TapTool)
	taptool.callback = OpenURL(url=url)


	#TODO FIX HARDCODED TMP FILE LINK
	#graph = "  <  div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://trips.ucc.ie/static/tmp/{1}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(short_code,filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)

	graph = file_html(p,CDN)
	return graph








def tran_corr(tran_corr_transcript1, tran_corr_transcript2,sorted_master_list,organism, transcriptome):

0 View Source File : metainfo_plots.py
License : MIT License
Project Creator : skiniry

def tran_corr(tran_corr_transcript1, tran_corr_transcript2,sorted_master_list,organism, transcriptome):
	xvals = []
	yvals = []
	labels = []
	range1counts = []
	range2counts = []
	file_ids = []

	for tup in sorted_master_list:
		xvals.append(tup[2])
		yvals.append(tup[3])
		labels.append(tup[1])
		range1counts.append(tup[4])
		range2counts.append(tup[3])
		file_ids.append(tup[0])

	full_title = "Transcript correllation ({})"


	x_lab = '{} count (log2)'.format(tran_corr_transcript1)
	y_lab = '{} count (log2)'.format(tran_corr_transcript2)

	p = figure(plot_width=2000, plot_height=1000,x_axis_label=x_lab,  y_axis_label=y_lab,title= full_title,toolbar_location="below",
			   tools = "reset,pan,box_zoom,hover,tap",logo=None)
	p.title.align="center"

	p.xgrid.grid_line_color = "white"
	p.ygrid.grid_line_color = "white"


	hover = p.select(dict(type=HoverTool))
	hover.tooltips = [("Ratio", "@y"),("Max count","@x"),("File name","@labels"),("Range 1 count","@range1counts"),("Range 2 count","@range2counts")]
	source = ColumnDataSource({'x':xvals,'y':yvals,'labels':labels,"range1counts":range1counts,"range2counts":range2counts,"file_id":file_ids})
	p.scatter('x','y',source=source, alpha=1,color="grey",size=12)
	output_file("scatter10k.html", title="Single transcript differential translation")
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'

	#/saccharomyces_cerevisiae/Gencode_v24/comparison/?files=227,%23ff1f00_228,%233BFF00_231,%23ffffff_232,%23000000_&transcript=YLR162W&normalize=F&cov=T&ambig=F&minread=25&maxread=100
	#/saccharomyces_cerevisiae/Gencode_v24/comparison/?files=227,228,229,%23ff1f00_230,%233BFF00&transcript=YDR003W&normalize=T&cov=T&ambig=T&minread=18&maxread=45
	#url = "http://143.239.109.139/tripsviz/{}/{}/interactive_plot/?transcript=@labels".format(organism, transcriptome)
	#file_string = ""
	#label_string="&labels=RIBO-Seq Cond 1,%23007a02_RIBO-Seq Cond 2,%23960000_mRNA-Seq Cond 1,%2374ed76_mRNA-seq Cond 2,%23ff6d6d"


	# remove the trailing _ in file_string if it's been populated
	#if file_string:
	#	file_string = file_string[:len(file_string)-1]

	url = "http://trips.ucc.ie/{}/{}/interactive_plot/?files=@file_id&tran={}&ambig=F&minread=25&maxread=150".format(organism, transcriptome,tran_corr_transcript1)

	taptool = p.select(type=TapTool)
	taptool.callback = OpenURL(url=url)


	#TODO FIX HARDCODED TMP FILE LINK
	#graph = "  <  div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://trips.ucc.ie/static/tmp/{1}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(short_code,filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)

	graph = file_html(p,CDN)
	return graph










def explore_offsets(f0_counts, f1_counts, f2_counts, labels,short_code,background_col,title_size, axis_label_size, subheading_size,marker_size):

0 View Source File : metainfo_plots.py
License : MIT License
Project Creator : skiniry

def heatplot(min_readlen, max_readlen, min_pos, max_pos, positions,readlengths,count_list,heatmap_metagene_type,title,reverse_scale,color_palette,short_code,background_col,maxscaleval,title_size, axis_label_size, subheading_size,marker_size):
	xlabs = []
	ylabs = []
	for i in range(min_readlen,max_readlen+1):
		ylabs.append(i)
	for i in range(min_pos, max_pos):
		xlabs.append(i)
	df = pd.DataFrame(np.column_stack([positions, readlengths, count_list]), columns=['positions',"readlengths","counts"])
	source = ColumnDataSource(df)

	if color_palette == "gist_rainbow":
		color_pallete_list = []
		colormap =cm.get_cmap("gist_rainbow") #choose any matplotlib colormap here
		start_val = 0.75
		for i in range(0,256):
			start_val -= 0.00293
			rgb = colormap(start_val)[:3]
			hexval = matplotlib.colors.rgb2hex(rgb)
			color_pallete_list.append(hexval)
	else:
		color_pallete_list = all_palettes[color_palette][max(all_palettes[color_palette].keys())]
	
	colormap =cm.get_cmap("gist_rainbow") #choose any matplotlib colormap here
	rgb = colormap(0.5)[:3]
	hexval = matplotlib.colors.rgb2hex(rgb)

	if reverse_scale == True:
		color_pallete_list = color_pallete_list[::-1]
	
	print (count_list)
	print (max(filter(lambda v: v is not None, count_list)))
	
	if maxscaleval == "None":
		color_mapper = LogColorMapper(palette=color_pallete_list, low=1, high=max(filter(lambda v: v is not None, count_list)),nan_color=background_col)
	else:
		color_mapper = LogColorMapper(palette=color_pallete_list, low=1, high=maxscaleval,nan_color=background_col)
	p = figure(plot_width=1300, plot_height=750,x_range=(min_pos,max_pos), y_range=(min_readlen,max_readlen),title="Heatmap ({})".format(short_code),
			   y_axis_label="Readlengths", toolbar_location="below",tools = "reset,pan,box_zoom")
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	p.background_fill_color = background_col
	if heatmap_metagene_type == "metagene_start":
		p.xaxis.axis_label = ('Position relative to start')
	elif heatmap_metagene_type == "metagene_stop":
		p.xaxis.axis_label = ('Position relative to stop')
	p.rect(x="positions", y="readlengths", width=1, height=1,source=source,fill_color={'field': 'counts',"transform":color_mapper},line_color=None)
	color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(),title="Counts", border_line_color=None, location=(0,0))
	p.add_layout(color_bar, 'right')
	output_file("scatter10k.html", title="Heatmap ({})".format(short_code))
	graph = "  <  style>.mpld3-xaxis {{font-size: {0}px;}} .mpld3-yaxis {{font-size: {0}px;}} < /style>".format(marker_size)
	graph += " < div style='padding-left: 55px;padding-top: 22px;'>  < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a>  < /div>".format(short_code)
	graph += file_html(p,CDN)
	return graph



def ticker(tick):

0 View Source File : metainfo_plots.py
License : MIT License
Project Creator : skiniry

def rust_dwell(codon_count_dict,short_code,background_col,title_size, axis_label_size, subheading_size,marker_size):
	aa_color_dict = {"gly":"white",
			"arg":"blue",
			"ser":"green",
			"trp":"lightgreen",
			"cys":"white",
			"glu":"red",
			"asp":"red",
			"lys":"blue",
			"asn":"green",
			"gln":"green",
			"his":"blue",
			"tyr":"lightgreen",
			"ala":"grey",
			"thr":"green",
			"pro":"white",
			"val":"grey",
			"met":"grey",
			"ile":"grey",
			"leu":"grey",
			"phe":"lightgreen"}
	
	min_count = float(min(codon_count_dict.values()))+0.000001
	max_count = float(max(codon_count_dict.values()))
	max_rust_ratio = (max_count/(min_count))
	aa_dict = {"gly":["GGT","GGC","GGA","GGG"],
			   "arg":["AGA","AGG","CGT","CGC","CGA","CGG"],
			   "ser":["AGT","AGC","TCT","TCC","TCA","TCG"],
			   "trp":["TGG"],
			   "cys":["TGT","TGC"],
			   "glu":["GAA","GAG"],
			   "asp":["GAT","GAC"],
			   "lys":["AAA","AAG"],
			   "asn":["AAT","AAC"],
			   "gln":["CAA","CAG"],
			   "his":["CAT","CAC"],
			   "tyr":["TAT","TAC"],
			   "ala":["GCT","GCC","GCA","GCG"],
			   "thr":["ACT","ACC","ACA","ACG"],
			   "pro":["CCT","CCC","CCA","CCG"],
			   "val":["GTT","GTC","GTA","GTG"],
			   "met":["ATG"],
			   "ile":["ATA","ATT","ATC"],
			   "leu":["CTT","CTC","CTA","CTG","TTA","TTG"],
			   "phe":["TTT","TTC"]}
	avg_dict = {}
	for aa in aa_dict:
		tot = 0.0
		for codon in aa_dict[aa]:
			tot += codon_count_dict[codon]
		avg = tot/len(aa_dict[aa])
		avg_dict[aa] = avg
	sorted_list = sorted(avg_dict.items(), key=lambda x: x[1])
	p = figure(plot_width=1300, plot_height=750, y_axis_label='RUST ratio (relative to min)',title="RUST: Relative codon dwell times ({})".format(short_code),toolbar_location="below",
			   tools = "reset,pan,box_zoom,hover",logo=None)#,y_range=Range1d(bounds=(0, max_rust_ratio)))
	p.y_range = Range1d(0,max_rust_ratio)
	p.x_range = Range1d(0,23)
	p.xaxis.visible = False
	p.title.align="center"
	p.title.text_font_size = "24pt"
	p.xaxis.axis_label_text_font_size = "22pt"
	p.xaxis.major_label_text_font_size = "17pt"
	p.yaxis.axis_label_text_font_size = "22pt"
	p.yaxis.major_label_text_font_size = "17pt"
	p.background_fill_color = background_col
	p.xgrid.grid_line_color = "white"
	p.ygrid.grid_line_color = "white"
	x_vals = []
	y_vals = []
	codons = []
	x_val = 1
	for tup in sorted_list:
		aa = tup[0]
		for codon in aa_dict[aa]:
			codons.append(codon)
			x_vals.append(x_val)
			rust_ratio = (float(codon_count_dict[codon]+0.000001)/min_count)
			y_vals.append(rust_ratio)
			mytext = Label(x=x_val-0.2, y=-10, text=aa,background_fill_color=aa_color_dict[aa],text_font_size="18pt")
			p.add_layout(mytext)
		x_val += 1
	p.legend.label_text_size = "30pt"
	p.scatter(-1,-1,color="red",legend="Acidic")
	p.scatter(-1,-1,color="lightgreen",legend="Aromatic")
	p.scatter(-1,-1,color="green",legend="Polar uncharged")
	p.scatter(-1,-1,color="grey",legend="Aliphatic")
	p.scatter(-1,-1,color="blue",legend="Basic")
	source = ColumnDataSource({'x': x_vals,'y':y_vals,'codons':codons})
	p.scatter('x','y',source=source, color="grey",size=16)
	p.y_range=Range1d(-10, max(y_vals)*1.1)
	hover = p.select(dict(type=HoverTool))
	hover.tooltips = [("Rust ratio", "@y"),
					  ("Codon","@codons")]
	output_file("scatter10k.html", title="RUST: Relative codon dwell times ({})".format(short_code))
	graph = "  <  style>.mpld3-xaxis {{font-size: {0}px;}} .mpld3-yaxis {{font-size: {0}px;}} < /style>".format(marker_size)
	graph += " < div style='padding-left: 55px;padding-top: 22px;'>  < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a>  < /div>".format(short_code)
	graph += file_html(p,CDN)
	return graph




def codon_usage(codon_dict,short_code,title_size, axis_label_size, marker_size):

0 View Source File : metainfo_plots.py
License : MIT License
Project Creator : skiniry

def codon_usage(codon_dict,short_code,title_size, axis_label_size, marker_size):
	allxvals = []
	allyvals = []
	alllabels = []
	amino_acids = []
	aa_dict = {"TTT":"Phenylalanine", "TTC":"Phenylalanine", "TTA":"Leucine", "TTG":"Leucine",
    "TCT":"Serine", "TCC":"Serine", "TCA":"Serine", "TCG":"Serine",
    "TAT":"Tyrosine", "TAC":"Tyrosine", "TAA":"*", "TAG":"*",
    "TGT":"Cysteine", "TGC":"Cysteine", "TGA":"*", "TGG":"Tryptophan",
    "CTT":"Leucine", "CTC":"Leucine", "CTA":"Leucine", "CTG":"Leucine",
    
    "CCT":"Proline", "CCC":"Proline", "CCA":"Proline", "CCG":"Proline",
    "CAT":"Histidine", "CAC":"Histidine", "CAA":"Glutamine", "CAG":"Glutamine",
    "CGT":"Arginine", "CGC":"Arginine", "CGA":"Arginine", "CGG":"Arginine",
    "ATT":"Isoleucine", "ATC":"Isoleucine", "ATA":"Isoleucine", "ATG":"Methionine",
    
    "ACT":"Threonine", "ACC":"Threonine", "ACA":"Threonine", "ACG":"Threonine",
    "AAT":"Asparagine", "AAC":"Asparagine", "AAA":"Lysine", "AAG":"Lysine",
    "AGT":"Serine", "AGC":"Serine", "AGA":"Arginine", "AGG":"Arginine",
    "GTT":"Valine", "GTC":"Valine", "GTA":"Valine", "GTG":"Valine",
    
    "GCT":"Alanine", "GCC":"Alanine", "GCA":"Alanine", "GCG":"Alanine",
    "GAT":"Aspartic Acid", "GAC":"Aspartic Acid", "GAA":"Glutamic Acid", "GAG":"Glutamic Acid",
    "GGT":"Glycine", "GGC":"Glycine", "GGA":"Glycine", "GGG":"Glycine"}
	
	codon_list = ["ATG","TTT","TTC","CTT", "CTC", "CTA", "CTG","TTA","TTG", "AGT", "AGC","TCT", "TCC", "TCA", "TCG","TAT", "TAC","TGT", "TGC","TGG","CCT", "CCC", "CCA", "CCG",
			   "CAT", "CAC", "CAA", "CAG","AGA", "AGG","CGT", "CGC", "CGA", "CGG","ATT", "ATC", "ATA","ACT", "ACC", "ACA", "ACG","AAT", "AAC", "AAA", "AAG", 
				"GTT", "GTC", "GTA", "GTG","GCT", "GCC", "GCA", "GCG","GAT", "GAC", "GAA", "GAG","GGT", "GGC", "GGA", "GGG","TAG","TAA","TGA"]

	curr_count = 0
	for codon in codon_list:
		curr_count+=1
		allxvals.append(curr_count)
		allyvals.append(codon_dict[codon]["ribo_count"]/codon_dict[codon]["codon_count"])
		alllabels.append(codon)
		amino_acids.append(aa_dict[codon])
	full_title = "Codon usage ({})".format(short_code)
	x_lab = ''
	y_lab = 'Normalised Count'
	min_y = min(0,min(allyvals))-.02
	max_y = max(allyvals)+.02
	p = figure(plot_width=1300, plot_height=750,x_axis_label=x_lab,  y_axis_label=y_lab,title= full_title,toolbar_location="below",
			   tools = "reset,pan,box_zoom,hover,tap",logo=None,y_range=(min_y,max_y))
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = "14pt"
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	#p.background_fill_color = background_color
	p.xgrid.grid_line_color = "white"
	p.ygrid.grid_line_color = "white"
	color_palette_list = []
	colormap =cm.get_cmap("gist_rainbow") #choose any matplotlib colormap here
	start_val = 0.75
	for i in range(0,21):
		start_val -= 0.0293
		rgb = colormap(start_val)[:3]
		hexval = matplotlib.colors.rgb2hex(rgb)
		color_palette_list.append(hexval)

	color_map = bmo.CategoricalColorMapper(factors=["Phenylalanine","Leucine","Serine","Tyrosine","*","Cysteine","Tryptophan","Proline","Histidine","Glutamine","Arginine","Isoleucine","Methionine","Threonine","Asparagine","Lysine","Valine","Alanine","Aspartic Acid","Glutamic Acid","Glycine"],
									palette=color_palette_list)
	p.quad(top=[max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y],
				bottom=[min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y], 
				left=[0.5,3.5,15.5,19.5,24.5,28.5,37.5,43.5,49.5,55.5,61.5],
				right=[1.5,9.5,17.5,20.5,26.5,34.5,41.5,45.5,53.5,57.5,64.5],
				color="#e0e0e0")
	
	
	source = ColumnDataSource({'x': allxvals,'y':allyvals,'labels':alllabels,'amino_acids':amino_acids})
	p.scatter('x','y',source=source, alpha=1,color={'field': 'amino_acids', 'transform': color_map},size=16,line_color="black")
	p.xaxis.ticker = [1,2.5,6.5,12.5,16.5,18.5,20,22.5,25.5,27.5,31.5,36,39.5,42.5,44.5,47.5,51.5,54.5,56.5,59.5,63]
	p.xaxis.major_label_overrides = {1:"Met",2.5:"Phe",6.5:"Leu",12.5:"Ser",16.5:"Tyr",18.5:"Cys",20:"Trp",22.5:"Pro",25.5:"His",27.5:"Gln",31.5:"Arg",
								 36:"Ile",39.5:"Thr",42.5:"Asn",44.5:"Lys",47.5:"Val",51.5:"Ala",54.5:"Asp",56.5:"Glu",59.5:"Gly",63:"Stop"}
	#p.vbar(x=[1], width=1,bottom=0,color="gray",top=[1])

	hover = p.select(dict(type=HoverTool))
	hover.tooltips = [("Count", "@y"),("Codon","@labels"),("Amino acid","@amino_acids")]

	output_file("scatter10k.html", title="Codon usage")
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'

	graph = file_html(p,CDN)
	return graph



def calc_mrnadist_factor(mrna_dist_dict):

0 View Source File : riboflask_diff.py
License : MIT License
Project Creator : skiniry

def generate_plot(sorted_min_exp_list,bin_list,organism, label,transcriptome,riboseq1,riboseq2,rnaseq1,rnaseq2,background_color, short_code,normalized,filename,no_groups,title_size, axis_label_size, subheading_size,marker_size,ambiguous,gene_list):
	#Convert gene_list from string to list
	logging.debug("generate plot called")
	#logging.debug("sorted_min_exp_list: {}".format(sorted_min_exp_list))
	logging.debug("bin_list: {}".format(bin_list))
	if gene_list != "":
		gene_list = gene_list.replace(","," ").replace("\t"," ")
		split_list = gene_list.split(" ")
		gene_list = []
		for item in split_list:
			gene_list.append(item.strip(" ").upper())
	posallxvals = []
	posallyvals = []
	posalllabels = []
	posallgenes = []
	poszscores = []

	negallxvals = []
	negallyvals = []
	negalllabels = []
	negallgenes = []
	negzscores = []

	nonde_xvals = []
	nonde_yvals = []
	nonde_labels = []
	nonde_allgenes = []
	nonde_zscores = []
	


	upper_thresholds_y = [bin_list[0][2]]
	upper_thresholds_x = [(sorted_min_exp_list[0][1])*0.99]
	lower_thresholds_y = [bin_list[0][3]]
	lower_thresholds_x = [(sorted_min_exp_list[0][1])*0.99]

	# Easiest way to plot threshold lines is to just plot the threshold and min_exp for each bin, but this will result in slanted lines, i.ie if my first bin threshold is 8 and my second is 4
	# there would be a slanted line between 8 and 4, but this makes no sense as the threshold is the same across an entire bin, so instead we plot two y values at every step (except first and last)

	#d((transcript, log(min_exp,2), fold_change,gene))
	cur_count = 0
	bin_count = 0
	for i in range(0,len(sorted_min_exp_list)):
		cur_count+=1
		if cur_count == 300:
			#To x we add the log2(min exp) of the 300th (or multiple of) item in min exp list
			bin_count += 1
			try:
				tst = bin_list[bin_count]
			except:
				bin_count -= 1
			cur_count = 0
			upper_thresholds_x.append(sorted_min_exp_list[(bin_count)*300][1])
			upper_thresholds_x.append(sorted_min_exp_list[(bin_count)*300][1])
			upper_thresholds_y.append(bin_list[bin_count-1][2])
			upper_thresholds_y.append(bin_list[bin_count][2])
			lower_thresholds_x.append(sorted_min_exp_list[(bin_count)*300][1])
			lower_thresholds_x.append(sorted_min_exp_list[(bin_count)*300][1])
			lower_thresholds_y.append(bin_list[bin_count-1][3])
			lower_thresholds_y.append(bin_list[bin_count][3])
		if bin_list[bin_count][1] == 0.0:
			continue
		z_score = (sorted_min_exp_list[i][2]-bin_list[bin_count][0])/(bin_list[bin_count][1])
		
		if gene_list == "":
			if sorted_min_exp_list[i][2]   <  = bin_list[bin_count][2] and sorted_min_exp_list[i][2] >= bin_list[bin_count][3]:
				nonde_xvals.append(sorted_min_exp_list[i][1])
				nonde_yvals.append(sorted_min_exp_list[i][2])
				nonde_labels.append(sorted_min_exp_list[i][0])
				nonde_allgenes.append(sorted_min_exp_list[i][3])
				nonde_zscores.append((sorted_min_exp_list[i][2]-bin_list[bin_count][0])/(bin_list[bin_count][1]))
			elif sorted_min_exp_list[i][2] >  bin_list[bin_count][2]:
				posallxvals.append(sorted_min_exp_list[i][1])
				posallyvals.append(sorted_min_exp_list[i][2])
				posalllabels.append(sorted_min_exp_list[i][0])
				posallgenes.append(sorted_min_exp_list[i][3])
				poszscores.append((sorted_min_exp_list[i][2]-bin_list[bin_count][0])/(bin_list[bin_count][1]))
			elif sorted_min_exp_list[i][2]  <   (bin_list[bin_count][3]):
				negallxvals.append(sorted_min_exp_list[i][1])
				negallyvals.append(sorted_min_exp_list[i][2])
				negalllabels.append(sorted_min_exp_list[i][0])
				negallgenes.append(sorted_min_exp_list[i][3])
				negzscores.append((sorted_min_exp_list[i][2]-bin_list[bin_count][0])/(bin_list[bin_count][1]))
		else:
			#If the user passes a gene list only highlight those genes red/green depending on if fold changes is >/ <  0
			if sorted_min_exp_list[i][3] not in gene_list:
				nonde_xvals.append(sorted_min_exp_list[i][1])
				nonde_yvals.append(sorted_min_exp_list[i][2])
				nonde_labels.append(sorted_min_exp_list[i][0])
				nonde_allgenes.append(sorted_min_exp_list[i][3])
				nonde_zscores.append((sorted_min_exp_list[i][2]-bin_list[bin_count][0])/(bin_list[bin_count][1]))
			elif sorted_min_exp_list[i][3] in gene_list and sorted_min_exp_list[i][2] > 0:
				posallxvals.append(sorted_min_exp_list[i][1])
				posallyvals.append(sorted_min_exp_list[i][2])
				posalllabels.append(sorted_min_exp_list[i][0])
				posallgenes.append(sorted_min_exp_list[i][3])
				poszscores.append((sorted_min_exp_list[i][2]-bin_list[bin_count][0])/(bin_list[bin_count][1]))
			elif sorted_min_exp_list[i][3] in gene_list and sorted_min_exp_list[i][2]  <  0:
				negallxvals.append(sorted_min_exp_list[i][1])
				negallyvals.append(sorted_min_exp_list[i][2])
				negalllabels.append(sorted_min_exp_list[i][0])
				negallgenes.append(sorted_min_exp_list[i][3])
				negzscores.append((sorted_min_exp_list[i][2]-bin_list[bin_count][0])/(bin_list[bin_count][1]))


	# Add z score thresholds for the last bin, which wouldn't have been covered in the above loop
	upper_thresholds_x.append((sorted_min_exp_list[-1][1])*0.99)
	upper_thresholds_y.append(bin_list[bin_count][2])
	lower_thresholds_x.append((sorted_min_exp_list[-1][1])*0.99)
	lower_thresholds_y.append(bin_list[bin_count][3])

	full_title = "Differential translation ({})".format(short_code)

	if no_groups == 1:
		x_lab = 'Geometric mean (log2)'
		y_lab = 'Fold change (log2)'
	else:
		x_lab = 'Average Geometric mean (log2)'
		y_lab = 'Average Fold change (log2)'
	p = figure(plot_width=1300, plot_height=750,x_axis_label=x_lab,  y_axis_label=y_lab,title= full_title,toolbar_location="below",
			   tools = "reset,pan,box_zoom,hover,tap")
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	p.background_fill_color = background_color
	p.xgrid.grid_line_color = "white"
	p.ygrid.grid_line_color = "white"
	if gene_list == "":
		p.line(upper_thresholds_x, upper_thresholds_y, color="yellow",line_width=3)
		p.line(lower_thresholds_x, lower_thresholds_y, color="yellow",line_width=3)
	source = ColumnDataSource({'x': nonde_xvals,'y':nonde_yvals,'labels':nonde_labels, 'genes':nonde_allgenes})
	sct = p.scatter('x','y',source=source, alpha=1,color="grey",size=5)
	#source = ColumnDataSource({'x': posallxvals,'y':posallyvals,'labels':posalllabels, 'genes':posallgenes,"zscores":poszscores})
	source = ColumnDataSource({'x': posallxvals,'y':posallyvals,'labels':posalllabels, 'genes':posallgenes})
	p.scatter('x','y',source=source, alpha=1,color="green",size=10)
	hover = p.select(dict(type=HoverTool))
	#hover.tooltips = [("Fold change", "@y"),("Geometric mean","@x"),("Transcript","@labels"),("Genes","@genes"), ("Z score","@zscores")]
	hover.tooltips = [("Fold change", "@y"),("Geometric mean","@x"),("Transcript","@labels"),("Genes","@genes")]
	#source = ColumnDataSource({'x': negallxvals,'y':negallyvals,'labels':negalllabels, 'genes':negallgenes,"zscores":negzscores})
	source = ColumnDataSource({'x': negallxvals,'y':negallyvals,'labels':negalllabels, 'genes':negallgenes})
	p.scatter('x','y',source=source, alpha=1,color="red",size=10)
	#source = ColumnDataSource({'x': nonde_xvals,'y':nonde_yvals,'labels':nonde_labels, 'genes':nonde_allgenes,"zscores":nonde_zscores})

	output_file("scatter10k.html", title="Differential translation")
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'

	#/saccharomyces_cerevisiae/Gencode_v24/comparison/?files=227,%23ff1f00_228,%233BFF00_231,%23ffffff_232,%23000000_&transcript=YLR162W&normalize=F&cov=T&ambig=F&minread=25&maxread=100
	#/saccharomyces_cerevisiae/Gencode_v24/comparison/?files=227,228,229,%23ff1f00_230,%233BFF00&transcript=YDR003W&normalize=T&cov=T&ambig=T&minread=18&maxread=45
	
	file_string = ""
	label_string="&labels=RIBO-Seq Cond 1,%23007a02_RIBO-Seq Cond 2,%23960000_mRNA-Seq Cond 1,%2374ed76_mRNA-seq Cond 2,%23ff6d6d"
	if riboseq1:
		if riboseq1[0] != "":
			for file_id in riboseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%23007a02_")

	if riboseq2:
		if riboseq2[0] != "":
			for file_id in riboseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23960000_")
	if rnaseq1:
		if rnaseq1[0] != "":
			for file_id in rnaseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%2374ed76_")
	if rnaseq2:
		if rnaseq2[0] != "":
			for file_id in rnaseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23ff6d6d_")

	# remove the trailing _ in file_string if it's been populated
	if file_string:
		file_string = file_string[:len(file_string)-1]

	if ambiguous == True:
		ambig = "T"
	else:
		ambig = "F"

	url = "http://0.0.0.0:5000/{}/{}/comparison/?files={}{}&transcript=@labels&normalize={}&cov=T&ambig={}&minread=25&maxread=150".format(organism, transcriptome,file_string,label_string,str(normalized)[0],ambig)


	'''
	hili_gene = CustomJS(args=dict(sct=sct,source=source), code="""
			console.log("Called hili gene");
			var f = cb_obj['value']
			var x = source["genes"][f]
			console.log("x is" + x);
			console.log("F is "+f);
			sct.glyph.fill_color = '#4286f4';
		""")
	'''

	taptool = p.select(type=TapTool)
	taptool.callback = OpenURL(url=url)
	#text_input = TextInput(value="B2M",title="Gene",callback=hili_gene)


	#TODO FIX HARDCODED TMP FILE LINK
	graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://0.0.0.0:5000/static/tmp/{1}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(short_code,filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	logging.debug("Returning plot")
	return graph




def ribo_vs_rna(ribo_rna_dict,organism,transcriptome,riboseq1,riboseq2,rnaseq1,rnaseq2,background_col,short_code,normalized,filename,no_groups,title_size, axis_label_size, subheading_size,marker_size,ambiguous,gene_list,label):

0 View Source File : riboflask_diff.py
License : MIT License
Project Creator : skiniry

def ribo_vs_rna(ribo_rna_dict,organism,transcriptome,riboseq1,riboseq2,rnaseq1,rnaseq2,background_col,short_code,normalized,filename,no_groups,title_size, axis_label_size, subheading_size,marker_size,ambiguous,gene_list,label):
	#Convert gene_list from string to list
	if gene_list != "":
		gene_list = gene_list.replace(","," ").replace("\t"," ")
		split_list = gene_list.split(" ")
		gene_list = []
		for item in split_list:
			gene_list.append(item.strip(" ").upper())
	x_values = []
	y_values = []
	genes = []
	trans = []
	
	hili_x_values = []
	hili_y_values = []
	hili_genes = []
	hili_trans = []
	for gene in ribo_rna_dict:
		if label == "TE":
			if gene not in gene_list:
				y_values.append(log(ribo_rna_dict[gene]["ribo2"]/ribo_rna_dict[gene]["ribo1"],2))
				x_values.append(log(ribo_rna_dict[gene]["rna2"]/ribo_rna_dict[gene]["rna1"],2))
				genes.append(gene)
				trans.append(ribo_rna_dict[gene]["tran"])
			else:
				hili_y_values.append(log(ribo_rna_dict[gene]["ribo2"]/ribo_rna_dict[gene]["ribo1"],2))
				hili_x_values.append(log(ribo_rna_dict[gene]["rna2"]/ribo_rna_dict[gene]["rna1"],2))
				hili_genes.append(gene)
				hili_trans.append(ribo_rna_dict[gene]["tran"])
		elif label == "Riboseq":
			if gene not in gene_list:
				if ribo_rna_dict[gene]["ribo1"] >= 1:
					y_values.append(log(ribo_rna_dict[gene]["ribo1"],2))
				else:
					y_values.append(0)
				if ribo_rna_dict[gene]["ribo2"] >=  1:
					x_values.append(log(ribo_rna_dict[gene]["ribo2"],2))
				else:
					x_values.append(0)
				genes.append(gene)
				trans.append(ribo_rna_dict[gene]["tran"])
			else:
				if ribo_rna_dict[gene]["ribo1"] >= 1:
					hili_y_values.append(log(ribo_rna_dict[gene]["ribo1"],2))
				else:
					hili_y_values.append(0)
				if ribo_rna_dict[gene]["ribo2"] >= 1:
					hili_x_values.append(log(ribo_rna_dict[gene]["ribo2"],2))
				else:
					hili_x_values.append(0)
				hili_genes.append(gene)
				hili_trans.append(ribo_rna_dict[gene]["tran"])
		elif label == "Rnaseq":
			if gene not in gene_list:
				if ribo_rna_dict[gene]["rna1"] != 0:
					y_values.append(log(ribo_rna_dict[gene]["rna1"],2))
				else:
					y_values.append(0)
				if ribo_rna_dict[gene]["rna2"] != 0:
					x_values.append(log(ribo_rna_dict[gene]["rna2"],2))
				else:
					x_values.append(0)
				genes.append(gene)
				trans.append(ribo_rna_dict[gene]["tran"])
			else:
				if ribo_rna_dict[gene]["rna1"] != 0:
					hili_y_values.append(log(ribo_rna_dict[gene]["rna1"],2))
				else:
					hili_y_values.append(0)
				if ribo_rna_dict[gene]["rna2"] != 0:
					hili_x_values.append(log(ribo_rna_dict[gene]["rna2"],2))
				else:
					hili_x_values.append(0)
				hili_genes.append(gene)
				hili_trans.append(ribo_rna_dict[gene]["tran"])
	source = ColumnDataSource({'x': x_values,'y':y_values,'trans':trans, 'genes':genes})
	if label == "TE":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="RNA-Seq FC (log2)",  y_axis_label='Ribo-Seq FC (log2)',title="Ribo-Seq FC vs RNA-Seq FC ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	elif label == "Riboseq":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="Ribo-Seq Cond2 count (log2)",  y_axis_label='Ribo-Seq Cond1 count (log2)',title="Ribo-Seq correlation ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	elif label == "Rnaseq":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="RNA-Seq Cond2 count (log2)",  y_axis_label='RNA-Seq Cond1 count (log2)',title="RNA-Seq correlation ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	p.background_fill_color = background_col
	p.xgrid.grid_line_color = "#cccccc"
	p.ygrid.grid_line_color = "#cccccc"


	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='grey')
	source = ColumnDataSource({'x':hili_x_values, 'y':hili_y_values, 'trans':hili_trans,'genes':hili_genes})
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#4286f4')
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'
	
	if label == "TE":
		p.line([-8,8], [-8,8], color="#cccccc",line_width=1)
		hover.tooltips = [("Ribo fc", "@y"),("RNA fc","@x"),("Genes","@genes"),("Transcript","@trans")]
	elif label == "Riboseq":
			p.line([0,16], [0,16], color="#cccccc",line_width=1)
			hover.tooltips = [("Ribo Cond 1 count (log2)", "@y"),("Ribo Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans")]
			#corr = spearmanr(x_values, y_values)
			pearson_corr = pearsonr(x_values, y_values)
			mytext = Label(x=0.1,y=max(y_values),text="Pearson correlation: {}".format(round(pearson_corr[0],2)),background_fill_color="white",text_font_size="13pt")
			p.add_layout(mytext)
	else:
			p.line([0,16], [0,16], color="#cccccc",line_width=1)
			hover.tooltips = [("Rna-seq Cond 1 count (log2)", "@y"),("Rna-seq Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans")]
			hover.tooltips = [("Ribo Cond 1 count (log2)", "@y"),("Ribo Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans")]
			#corr = spearmanr(x_values, y_values)
			pearson_corr = pearsonr(x_values, y_values)
			mytext = Label(x=0.1,y=max(y_values),text="Pearson correlation: {}".format(round(pearson_corr[0],2)),background_fill_color="white",text_font_size="13pt")
			p.add_layout(mytext)
	file_string = ""
	label_string="&labels=RIBO-Seq Cond 1,%23007a02_RIBO-Seq Cond 2,%23960000_mRNA-Seq Cond 1,%2374ed76_mRNA-seq Cond 2,%23ff6d6d"
	
	if riboseq1:
		if riboseq1[0] != "":
			for file_id in riboseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%23007a02_")

	if riboseq2:
		if riboseq2[0] != "":
			for file_id in riboseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23960000_")
	if rnaseq1:
		if rnaseq1[0] != "":
			for file_id in rnaseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%2374ed76_")
	if rnaseq2:
		if rnaseq2[0] != "":
			for file_id in rnaseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23ff6d6d_")
	# remove the trailing _ in file_string if it's been populated
	if file_string:
		file_string = file_string[:len(file_string)-1]
	if ambiguous == True:
		ambig = "T"
	else:
		ambig = "F"
	url = "http://0.0.0.0:5000/{}/{}/comparison/?files={}{}&transcript=@trans&normalize={}&cov=T&ambig={}&minread=25&maxread=150".format(organism, transcriptome,file_string,label_string,str(normalized)[0],ambig)
	taptool = p.select(type=TapTool)
	taptool.callback = OpenURL(url=url)
	graph = "  <  div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://0.0.0.0:5000/static/tmp/{1}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(short_code,filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	return graph






def deseq2_plot(ribo_rna_dict,organism,transcriptome,riboseq1,riboseq2,rnaseq1,rnaseq2,background_col,short_code,normalized,filename,no_groups,title_size, axis_label_size, subheading_size,marker_size,ambiguous,gene_list,label,minzscore):

0 View Source File : riboflask_diff.py
License : MIT License
Project Creator : skiniry

def deseq2_plot(ribo_rna_dict,organism,transcriptome,riboseq1,riboseq2,rnaseq1,rnaseq2,background_col,short_code,normalized,filename,no_groups,title_size, axis_label_size, subheading_size,marker_size,ambiguous,gene_list,label,minzscore):
	#Convert gene_list from string to list
	if gene_list != "":
		gene_list = gene_list.replace(","," ").replace("\t"," ")
		split_list = gene_list.split(" ")
		gene_list = []
		for item in split_list:
			gene_list.append(item.strip(" ").upper())
	x_values = []
	y_values = []
	basemeans = []
	lfcses = []
	genes = []
	trans = []
	
	highlight_x_values = []
	highlight_y_values = []
	highlight_basemeans = []
	highlight_lfcses = []
	highlight_genes = []
	highlight_trans = []
	
	teup_x_values = []
	teup_y_values = []
	teup_basemeans = []
	teup_lfcses = []
	teup_genes = []
	teup_trans = []
	tedown_x_values = []
	tedown_y_values = []
	tedown_basemeans = []
	tedown_lfcses = []
	tedown_genes = []
	tedown_trans = []	
	
	rnaup_x_values = []
	rnaup_y_values = []
	rnaup_basemeans = []
	rnaup_lfcses = []
	rnaup_genes = []
	rnaup_trans = []
	rnadown_x_values = []
	rnadown_y_values = []
	rnadown_basemeans = []
	rnadown_lfcses = []
	rnadown_genes = []
	rnadown_trans = []	

	largest_fc = 0

	for gene in ribo_rna_dict:
		if label == "TE":
			x = ribo_rna_dict[gene]["rna_fc"]
			y = ribo_rna_dict[gene]["ribo_fc"]
			basemean = ribo_rna_dict[gene]["te_basemean"]
			lfcSE = ribo_rna_dict[gene]["te_lfcSE"]
			te_padj = ribo_rna_dict[gene]["te_padj"]
			ribo_padj = ribo_rna_dict[gene]["ribo_padj"]
			rna_padj = ribo_rna_dict[gene]["rna_padj"]
			
			if x != "NA" and y != "NA":
				x = float(x)
				y = float(y)
				if abs(x) > largest_fc:
					largest_fc = abs(x)
				if abs(y) > largest_fc:
					largest_fc = abs(y)
				if "NA" in te_padj:
					te_padj = 1
				if "NA" in ribo_padj:
					ribo_padj = 1
				if "NA" in rna_padj:
					rna_padj = 1
				te_padj = float(te_padj)
				ribo_padj = float(ribo_padj)
				rna_padj = float(rna_padj)
				if gene not in gene_list:
					if te_padj > (minzscore/100) and ribo_padj > (minzscore/100) and rna_padj > (minzscore/100):
						y_values.append(y)
						x_values.append(x)
						basemeans.append(basemean)
						lfcses.append(lfcSE)
						genes.append(gene)
						trans.append(ribo_rna_dict[gene]["tran"])
					elif te_padj   <   (minzscore/100) and x  <  y :
						teup_y_values.append(y)
						teup_x_values.append(x)
						teup_basemeans.append(basemean)
						teup_lfcses.append(lfcSE)
						teup_genes.append(gene)
						teup_trans.append(ribo_rna_dict[gene]["tran"])
					elif te_padj  <  (minzscore/100) and y  <  x :
						tedown_y_values.append(y)
						tedown_x_values.append(x)
						tedown_basemeans.append(basemean)
						tedown_lfcses.append(lfcSE)
						tedown_genes.append(gene)
						tedown_trans.append(ribo_rna_dict[gene]["tran"])
					elif rna_padj  <  (minzscore/100):
						if x > 0:
							rnaup_y_values.append(y)
							rnaup_x_values.append(x)
							rnaup_basemeans.append(basemean)
							rnaup_lfcses.append(lfcSE)
							rnaup_genes.append(gene)
							rnaup_trans.append(ribo_rna_dict[gene]["tran"])
						elif x  <  0 :
							rnadown_y_values.append(y)
							rnadown_x_values.append(x)
							rnadown_basemeans.append(basemean)
							rnadown_lfcses.append(lfcSE)
							rnadown_genes.append(gene)
							rnadown_trans.append(ribo_rna_dict[gene]["tran"])
				else:
					highlight_y_values.append(y)
					highlight_x_values.append(x)
					highlight_basemeans.append(basemean)
					highlight_lfcses.append(lfcSE)
					highlight_genes.append(gene)
					highlight_trans.append(ribo_rna_dict[gene]["tran"])
		elif label == "Riboseq":
			ribo_padj = ribo_rna_dict[gene]["ribo_padj"]
			basemean = ribo_rna_dict[gene]["ribo_basemean"]
			lfcSE = ribo_rna_dict[gene]["ribo_lfcSE"]
			
			if "NA" in ribo_padj:
				ribo_padj = 1
			ribo_padj = float(ribo_padj)
			if gene not in gene_list:
				if ribo_padj > (minzscore/100):
					if ribo_rna_dict[gene]["ribo1"] != 0:
						y_values.append(log(ribo_rna_dict[gene]["ribo1"],2))
					else:
						y_values.append(0)
					if ribo_rna_dict[gene]["ribo2"] != 0:
						x_values.append(log(ribo_rna_dict[gene]["ribo2"],2))
					else:
						x_values.append(0)
					genes.append(gene)
					trans.append(ribo_rna_dict[gene]["tran"])
					basemeans.append(basemean)
					lfcses.append(lfcSE)
				else:
					if ribo_rna_dict[gene]["ribo1"] != 0:
						teup_y_values.append(log(ribo_rna_dict[gene]["ribo1"],2))
					else:
						teup_y_values.append(0)
						
					if ribo_rna_dict[gene]["ribo2"] != 0:
						teup_x_values.append(log(ribo_rna_dict[gene]["ribo2"],2))
					else:
						teup_x_values.append(0)
					teup_genes.append(gene)
					teup_trans.append(ribo_rna_dict[gene]["tran"])
					teup_basemeans.append(basemean)
					teup_lfcses.append(lfcSE)
			else:
				if ribo_rna_dict[gene]["ribo1"] != 0:
					highlight_y_values.append(log(ribo_rna_dict[gene]["ribo1"],2))
				else:
					highlight_y_values.append(0)
				if ribo_rna_dict[gene]["ribo2"] != 0:
					highlight_x_values.append(log(ribo_rna_dict[gene]["ribo2"],2))
				else:
					highlight_x_values.append(0)
				highlight_basemeans.append(basemean)
				highlight_lfcses.append(lfcSE)
				highlight_genes.append(gene)
				highlight_trans.append(ribo_rna_dict[gene]["tran"])
		elif label == "Rnaseq":
			rna_padj = ribo_rna_dict[gene]["rna_padj"]
			basemean = ribo_rna_dict[gene]["rna_basemean"]
			lfcSE = ribo_rna_dict[gene]["rna_lfcSE"]
			
			if "NA" in rna_padj:
				rna_padj = 1
			rna_padj = float(rna_padj)
			if gene not in gene_list:
				if rna_padj > (minzscore/100):
					basemeans.append(basemean)
					lfcses.append(lfcSE)
					if ribo_rna_dict[gene]["rna1"] != 0:
						y_values.append(log(ribo_rna_dict[gene]["rna1"],2))
					else:
						y_values.append(0)
					if ribo_rna_dict[gene]["rna2"] != 0:
						x_values.append(log(ribo_rna_dict[gene]["rna2"],2))
					else:
						x_values.append(0)

					genes.append(gene)
					trans.append(ribo_rna_dict[gene]["tran"])
				else:
					if ribo_rna_dict[gene]["rna1"] != 0:
						teup_y_values.append(log(ribo_rna_dict[gene]["rna1"],2))
					else:
						teup_y_values.append(0)
					if ribo_rna_dict[gene]["rna2"] != 0:
						teup_x_values.append(log(ribo_rna_dict[gene]["rna2"],2))
					else:
						teup_x_values.append(ribo_rna_dict[gene]["rna2"])
					teup_genes.append(gene)
					teup_trans.append(ribo_rna_dict[gene]["tran"])
					teup_basemeans.append(basemean)
					teup_lfcses.append(lfcSE)
			else:
				if ribo_rna_dict[gene]["rna1"] != 0:
					highlight_y_values.append(log(ribo_rna_dict[gene]["rna1"],2))
				else:
					highlight_y_values.append(0)
				if ribo_rna_dict[gene]["rna2"] != 0:
					highlight_x_values.append(log(ribo_rna_dict[gene]["rna2"],2))
				else:
					highlight_x_values.append(0)
				highlight_basemeans.append(basemean)
				highlight_lfcses.append(lfcSE)
				highlight_genes.append(gene)
				highlight_trans.append(ribo_rna_dict[gene]["tran"])
				
	source = ColumnDataSource({'x': x_values,'y':y_values,'trans':trans, 'genes':genes,'basemeans':basemeans,'lfcses':lfcses})
	if label == "TE":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="RNA-Seq FC (log2)",  y_axis_label='Ribo-Seq FC (log2)',title="Ribo-Seq FC vs RNA-Seq FC ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	elif label == "Riboseq":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="Ribo-Seq Cond2 count (log2)",  y_axis_label='Ribo-Seq Cond1 count (log2)',title="Ribo-Seq correlation ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	elif label == "Rnaseq":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="RNA-Seq Cond2 count (log2)",  y_axis_label='RNA-Seq Cond1 count (log2)',title="RNA-Seq correlation ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	p.background_fill_color = background_col
	p.xgrid.grid_line_color = "#cccccc"
	p.ygrid.grid_line_color = "#cccccc"
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='gray')


	if highlight_genes != []:
		source = ColumnDataSource({'x':highlight_x_values, 'y':highlight_y_values, 'trans':highlight_trans,'genes':highlight_genes,'basemeans':highlight_basemeans,'lfcses':highlight_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#000000',legend="Highlighted Genes")
	
	if label == "TE":
		source = ColumnDataSource({'x':teup_x_values, 'y':teup_y_values, 'trans':teup_trans,'genes':teup_genes,'basemeans':teup_basemeans,'lfcses':teup_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#00ff99',legend="Translation up")
		source = ColumnDataSource({'x':tedown_x_values, 'y':tedown_y_values, 'trans':tedown_trans,'genes':tedown_genes,'basemeans':tedown_basemeans,'lfcses':tedown_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#ff5050',legend="Translation down")

		source = ColumnDataSource({'x':rnaup_x_values, 'y':rnaup_y_values, 'trans':rnaup_trans,'genes':rnaup_genes,'basemeans':rnaup_basemeans,'lfcses':rnaup_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#cc0099',legend="mRNA up")
		source = ColumnDataSource({'x':rnadown_x_values, 'y':rnadown_y_values, 'trans':rnadown_trans,'genes':rnadown_genes,'basemeans':rnadown_basemeans,'lfcses':rnadown_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#ffff00',legend="mRNA down")
	elif label == "Riboseq":
		source = ColumnDataSource({'x':teup_x_values, 'y':teup_y_values, 'trans':teup_trans,'genes':teup_genes,'basemeans':teup_basemeans,'lfcses':teup_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#00ff99',legend="Significant genes")
		source = ColumnDataSource({'x':tedown_x_values, 'y':tedown_y_values, 'trans':tedown_trans,'genes':tedown_genes,'basemeans':tedown_basemeans,'lfcses':tedown_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#00ff99',legend="Significant genes")
	elif label == "Rnaseq":
		source = ColumnDataSource({'x':teup_x_values, 'y':teup_y_values, 'trans':teup_trans,'genes':teup_genes,'basemeans':teup_basemeans,'lfcses':teup_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#00ff99',legend="Significant genes")
		source = ColumnDataSource({'x':rnadown_x_values, 'y':rnadown_y_values, 'trans':rnadown_trans,'genes':rnadown_genes,'basemeans':rnadown_basemeans,'lfcses':rnadown_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#00ff99',legend="Significant genes")
	

	p.legend.location = "top_left"
	p.legend.click_policy="hide"
	p.legend.label_text_font_size = "28px"
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'
	plot_limit = largest_fc*1.1
	if label == "TE":
		p.line([plot_limit*-1,plot_limit], [plot_limit*-1,plot_limit], color="#cccccc",line_width=1)
		hover.tooltips = [("Ribo fc", "@y"),("RNA fc","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
	elif label == "Riboseq":
		p.line([0,16], [0,16], color="#cccccc",line_width=1)
		hover.tooltips = [("Ribo Cond 1 count (log2)", "@y"),("Ribo Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
		#corr = spearmanr(x_values, y_values)
		#pearson_corr = pearsonr(x_values, y_values)
		#mytext = Label(x=0.1,y=max(y_values),text="Pearson correlation: {}".format(round(pearson_corr[0],2)),background_fill_color="white",text_font_size="13pt")
		#p.add_layout(mytext)
	else:
		p.line([0,16], [0,16], color="#cccccc",line_width=1)
		hover.tooltips = [("Rna-seq Cond 1 count (log2)", "@y"),("Rna-seq Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
		hover.tooltips = [("Ribo Cond 1 count (log2)", "@y"),("Ribo Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
		#corr = spearmanr(x_values, y_values)
		#pearson_corr = pearsonr(x_values, y_values)
		#mytext = Label(x=0.1,y=max(y_values),text="Pearson correlation: {}".format(round(pearson_corr[0],2)),background_fill_color="white",text_font_size="13pt")
		#p.add_layout(mytext)
	
	file_string = ""
	label_string="&labels=RIBO-Seq Cond 1,%23007a02_RIBO-Seq Cond 2,%23960000_mRNA-Seq Cond 1,%2374ed76_mRNA-seq Cond 2,%23ff6d6d"
	
	if riboseq1:
		if riboseq1[0] != "":
			for file_id in riboseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%23007a02_")
	if riboseq2:
		if riboseq2[0] != "":
			for file_id in riboseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23960000_")
	if rnaseq1:
		if rnaseq1[0] != "":
			for file_id in rnaseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%2374ed76_")
	if rnaseq2:
		if rnaseq2[0] != "":
			for file_id in rnaseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23ff6d6d_")

	# remove the trailing _ in file_string if it's been populated
	if file_string:
		file_string = file_string[:len(file_string)-1]

	if ambiguous == True:
		ambig = "T"
	else:
		ambig = "F"
	
	url = "http://0.0.0.0:5000/{}/{}/comparison/?files={}{}&transcript=@trans&normalize={}&cov=T&ambig={}&minread=25&maxread=150".format(organism, transcriptome,file_string,label_string,str(normalized)[0],ambig)
	taptool = p.select(type=TapTool)
	taptool.callback = OpenURL(url=url)

	graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://0.0.0.0:5000/static/tmp/{1}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download DESeq2 output files < /b> < /button> < /a>  < /div>".format(short_code,filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	return graph









def anota2seq_plot(ribo_rna_dict,organism,transcriptome,riboseq1,riboseq2,rnaseq1,rnaseq2,background_col,short_code,normalized,filename,no_groups,title_size, axis_label_size, subheading_size,marker_size,ambiguous,gene_list,label,minzscore,sig_translated,sig_rna,sig_buffering):

0 View Source File : riboflask_diff.py
License : MIT License
Project Creator : skiniry

def anota2seq_plot(ribo_rna_dict,organism,transcriptome,riboseq1,riboseq2,rnaseq1,rnaseq2,background_col,short_code,normalized,filename,no_groups,title_size, axis_label_size, subheading_size,marker_size,ambiguous,gene_list,label,minzscore,sig_translated,sig_rna,sig_buffering):
	#Convert gene_list from string to list
	# ("gene list passed to deseq2_plot", gene_list)
	if gene_list != "":
		gene_list = gene_list.replace(","," ").replace("\t"," ")
		split_list = gene_list.split(" ")
		gene_list = []
		for item in split_list:
			gene_list.append(item.strip(" ").upper())
	x_values = []
	y_values = []
	basemeans = []
	lfcses = []
	genes = []
	trans = []
	
	highlight_x_values = []
	highlight_y_values = []
	highlight_basemeans = []
	highlight_lfcses = []
	highlight_genes = []
	highlight_trans = []
	
	teup_x_values = []
	teup_y_values = []
	teup_basemeans = []
	teup_lfcses = []
	teup_genes = []
	teup_trans = []
	tedown_x_values = []
	tedown_y_values = []
	tedown_basemeans = []
	tedown_lfcses = []
	tedown_genes = []
	tedown_trans = []	
	
	bufferedup_x_values = []
	bufferedup_y_values = []
	bufferedup_basemeans = []
	bufferedup_lfcses = []
	bufferedup_genes = []
	bufferedup_trans = []
	buffereddown_x_values = []
	buffereddown_y_values = []
	buffereddown_basemeans = []
	buffereddown_lfcses = []
	buffereddown_genes = []
	buffereddown_trans = []		
	
	rnaup_x_values = []
	rnaup_y_values = []
	rnaup_basemeans = []
	rnaup_lfcses = []
	rnaup_genes = []
	rnaup_trans = []
	rnadown_x_values = []
	rnadown_y_values = []
	rnadown_basemeans = []
	rnadown_lfcses = []
	rnadown_genes = []
	rnadown_trans = []	
	
	largest_fc = 0
	for gene in ribo_rna_dict:
		if label == "TE":
			x = ribo_rna_dict[gene]["rna_fc"]
			y = ribo_rna_dict[gene]["ribo_fc"]
			basemean = ribo_rna_dict[gene]["te_basemean"]
			lfcSE = ribo_rna_dict[gene]["te_lfcSE"]
			te_padj = ribo_rna_dict[gene]["te_padj"]
			ribo_padj = ribo_rna_dict[gene]["ribo_padj"]
			rna_padj = ribo_rna_dict[gene]["rna_padj"]
			
			if x != "NA" and y != "NA":
				x = float(x)
				y = float(y)
				if abs(x) > largest_fc:
					largest_fc = abs(x)
				if abs(y) > largest_fc:
					largest_fc = abs(y)
				if "NA" in te_padj:
					te_padj = 1
				if "NA" in ribo_padj:
					ribo_padj = 1
				if "NA" in rna_padj:
					rna_padj = 1
				te_padj = float(te_padj)
				ribo_padj = float(ribo_padj)
				rna_padj = float(rna_padj)
				if gene not in gene_list:
					if gene not in sig_translated and gene not in sig_rna and gene not in sig_buffering:
						y_values.append(y)
						x_values.append(x)
						basemeans.append(basemean)
						lfcses.append(lfcSE)
						genes.append(gene)
						trans.append(ribo_rna_dict[gene]["tran"])
					elif gene in sig_translated:
						if x   <   y :
							teup_y_values.append(y)
							teup_x_values.append(x)
							teup_basemeans.append(basemean)
							teup_lfcses.append(lfcSE)
							teup_genes.append(gene)
							teup_trans.append(ribo_rna_dict[gene]["tran"])
						elif y  <  x :
							tedown_y_values.append(y)
							tedown_x_values.append(x)
							tedown_basemeans.append(basemean)
							tedown_lfcses.append(lfcSE)
							tedown_genes.append(gene)
							tedown_trans.append(ribo_rna_dict[gene]["tran"])
					elif gene in sig_rna:
							if x > 0:
								rnaup_y_values.append(y)
								rnaup_x_values.append(x)
								rnaup_basemeans.append(basemean)
								rnaup_lfcses.append(lfcSE)
								rnaup_genes.append(gene)
								rnaup_trans.append(ribo_rna_dict[gene]["tran"])
							elif x  <  0 :
								rnadown_y_values.append(y)
								rnadown_x_values.append(x)
								rnadown_basemeans.append(basemean)
								rnadown_lfcses.append(lfcSE)
								rnadown_genes.append(gene)
								rnadown_trans.append(ribo_rna_dict[gene]["tran"])
					elif gene in sig_buffering:
						#If no change in ribo, then this is buffered
						if x > 0:
							bufferedup_y_values.append(y)
							bufferedup_x_values.append(x)
							bufferedup_basemeans.append(basemean)
							bufferedup_lfcses.append(lfcSE)
							bufferedup_genes.append(gene)
							bufferedup_trans.append(ribo_rna_dict[gene]["tran"])
						else:
							buffereddown_y_values.append(y)
							buffereddown_x_values.append(x)
							buffereddown_basemeans.append(basemean)
							buffereddown_lfcses.append(lfcSE)
							buffereddown_genes.append(gene)
							buffereddown_trans.append(ribo_rna_dict[gene]["tran"])
				else:
					highlight_y_values.append(y)
					highlight_x_values.append(x)
					highlight_basemeans.append(basemean)
					highlight_lfcses.append(lfcSE)
					highlight_genes.append(gene)
					highlight_trans.append(ribo_rna_dict[gene]["tran"])
	source = ColumnDataSource({'x': x_values,'y':y_values,'trans':trans, 'genes':genes,'basemeans':basemeans,'lfcses':lfcses})
	if label == "TE":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="RNA-Seq FC (log2)",  y_axis_label='Ribo-Seq FC (log2)',title="Ribo-Seq FC vs RNA-Seq FC ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	elif label == "Riboseq":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="Ribo-Seq Cond2 count (log2)",  y_axis_label='Ribo-Seq Cond1 count (log2)',title="Ribo-Seq correlation ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	elif label == "Rnaseq":
		p = figure(plot_width=1300, plot_height=1300,x_axis_label="RNA-Seq Cond2 count (log2)",  y_axis_label='RNA-Seq Cond1 count (log2)',title="RNA-Seq correlation ({})".format(short_code),toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	p.background_fill_color = background_col
	p.xgrid.grid_line_color = "#cccccc"
	p.ygrid.grid_line_color = "#cccccc"
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='gray')

	
	if highlight_genes != []:
		source = ColumnDataSource({'x':highlight_x_values, 'y':highlight_y_values, 'trans':highlight_trans,'genes':highlight_genes,'basemeans':highlight_basemeans,'lfcses':highlight_lfcses})
		p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#000000',legend="Highlighted Genes")
	
	source = ColumnDataSource({'x':teup_x_values, 'y':teup_y_values, 'trans':teup_trans,'genes':teup_genes,'basemeans':teup_basemeans,'lfcses':teup_lfcses})
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#00ff99',legend="Translation up")
	source = ColumnDataSource({'x':tedown_x_values, 'y':tedown_y_values, 'trans':tedown_trans,'genes':tedown_genes,'basemeans':tedown_basemeans,'lfcses':tedown_lfcses})
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#ff5050',legend="Translation down")
	
	source = ColumnDataSource({'x':bufferedup_x_values, 'y':bufferedup_y_values, 'trans':bufferedup_trans,'genes':bufferedup_genes,'basemeans':bufferedup_basemeans,'lfcses':bufferedup_lfcses})
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#6699ff',legend="Buffered up")
	source = ColumnDataSource({'x':buffereddown_x_values, 'y':buffereddown_y_values, 'trans':buffereddown_trans,'genes':buffereddown_genes,'basemeans':buffereddown_basemeans,'lfcses':buffereddown_lfcses})
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#ffcc66',legend="Buffered down")
	
	source = ColumnDataSource({'x':rnaup_x_values, 'y':rnaup_y_values, 'trans':rnaup_trans,'genes':rnaup_genes,'basemeans':rnaup_basemeans,'lfcses':rnaup_lfcses})
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#cc0099',legend="mRNA abundance up")
	source = ColumnDataSource({'x':rnadown_x_values, 'y':rnadown_y_values, 'trans':rnadown_trans,'genes':rnadown_genes,'basemeans':rnadown_basemeans,'lfcses':rnadown_lfcses})
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='#ffff00',legend="mRNA abundance down")
	
	#legend_label=name)

	p.legend.location = "top_left"
	p.legend.click_policy="hide"
	p.legend.label_text_font_size = "28px"
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'
	plot_limit = largest_fc*1.1
	if label == "TE":
		p.line([plot_limit*-1,plot_limit], [plot_limit*-1,plot_limit], color="#cccccc",line_width=1)
		hover.tooltips = [("Ribo fc", "@y"),("RNA fc","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
	elif label == "Riboseq":
		p.line([0,16], [0,16], color="#cccccc",line_width=1)
		hover.tooltips = [("Ribo Cond 1 count (log2)", "@y"),("Ribo Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
		#corr = spearmanr(x_values, y_values)
		#pearson_corr = pearsonr(x_values, y_values)
		#mytext = Label(x=0.1,y=max(y_values),text="Pearson correlation: {}".format(round(pearson_corr[0],2)),background_fill_color="white",text_font_size="13pt")
		#p.add_layout(mytext)
	else:
		p.line([0,16], [0,16], color="#cccccc",line_width=1)
		hover.tooltips = [("Rna-seq Cond 1 count (log2)", "@y"),("Rna-seq Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
		hover.tooltips = [("Ribo Cond 1 count (log2)", "@y"),("Ribo Cond 2 count (log2)","@x"),("Genes","@genes"),("Transcript","@trans"),("Basemean","@basemeans"),("lfcSE","@lfcses")]
		#corr = spearmanr(x_values, y_values)
		#pearson_corr = pearsonr(x_values, y_values)
		#mytext = Label(x=0.1,y=max(y_values),text="Pearson correlation: {}".format(round(pearson_corr[0],2)),background_fill_color="white",text_font_size="13pt")
		#p.add_layout(mytext)
	
	file_string = ""
	label_string="&labels=RIBO-Seq Cond 1,%23007a02_RIBO-Seq Cond 2,%23960000_mRNA-Seq Cond 1,%2374ed76_mRNA-seq Cond 2,%23ff6d6d"
	
	if riboseq1:
		if riboseq1[0] != "":
			for file_id in riboseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%23007a02_")
	if riboseq2:
		if riboseq2[0] != "":
			for file_id in riboseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23960000_")
	if rnaseq1:
		if rnaseq1[0] != "":
			for file_id in rnaseq1:
				file_string += ("{},".format(file_id))
			file_string += ("%2374ed76_")
	if rnaseq2:
		if rnaseq2[0] != "":
			for file_id in rnaseq2:
				file_string += ("{},".format(file_id))
			file_string += ("%23ff6d6d_")

	# remove the trailing _ in file_string if it's been populated
	if file_string:
		file_string = file_string[:len(file_string)-1]

	if ambiguous == True:
		ambig = "T"
	else:
		ambig = "F"
	
	url = "http://0.0.0.0:5000/{}/{}/comparison/?files={}{}&transcript=@trans&normalize={}&cov=T&ambig={}&minread=25&maxread=150".format(organism, transcriptome,file_string,label_string,str(normalized)[0],ambig)
	taptool = p.select(type=TapTool)
	taptool.callback = OpenURL(url=url)

	graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://0.0.0.0:5000/static/tmp/{1}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download DESeq2 output files < /b> < /button> < /a>  < /div>".format(short_code,filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://0.0.0.0:5000/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	return graph































0 View Source File : traninfo_plots.py
License : MIT License
Project Creator : skiniry

def nuc_comp_scatter(master_dict,filename,title_size,axis_label_size,marker_size,nucleotide,short_code):
	x_values = []
	gc_list = master_dict[1]["lengths"]
	tran_list = master_dict[1]["trans"]
	for i in range(1,len(gc_list)+1):
		x_values.append(i)
	source = ColumnDataSource({'x': x_values,'y':gc_list,'trans':tran_list})	
	x_num = len(gc_list)	
		
	x_values2 = []
	gc_list2 = master_dict[2]["lengths"]
	tran_list2 = master_dict[2]["trans"]
	for i in range(1,len(gc_list2)+1):
		x_values2.append(x_num+i)
	source2 = ColumnDataSource({'x': x_values2,'y':gc_list2,'trans':tran_list2})
	x_num += len(gc_list2)
	
	x_values3 = []
	gc_list3 = master_dict[3]["lengths"]
	tran_list3 = master_dict[3]["trans"]
	for i in range(1,len(gc_list3)+1):
		x_values3.append(x_num+i)
	source3 = ColumnDataSource({'x': x_values3,'y':gc_list3,'trans':tran_list3})
	x_num += len(gc_list3)
	
	x_values4 = []
	gc_list4 = master_dict[4]["lengths"]
	tran_list4 = master_dict[4]["trans"]
	for i in range(1,len(gc_list4)+1):
		x_values4.append(x_num+i)
	source4 = ColumnDataSource({'x': x_values4,'y':gc_list4,'trans':tran_list4})
	x_num += len(gc_list4)	
	full_title = "{}% ({})".format(nucleotide,short_code)
	y_lab = "{} %".format(nucleotide)

	p = figure(plot_width=1300, plot_height=1300,x_axis_label="", title=full_title,y_axis_label=y_lab,toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	p.title.align="center"
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	#p.title.text_font_size = title_size
	#p.xaxis.axis_label_text_font_size = axis_label_size
	#p.xaxis.major_label_text_font_size = marker_size
	#p.yaxis.axis_label_text_font_size = axis_label_size
	#p.yaxis.major_label_text_font_size = marker_size
	#p.background_fill_color = background_col
	p.xgrid.grid_line_color = "#cccccc"
	p.ygrid.grid_line_color = "#cccccc"
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='green')
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source2,fill_color='red')
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source3,fill_color='blue')
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source4,fill_color='yellow')
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'
	hover.tooltips = [("GC%", "@y"),("Count","@x"),("Transcript","@trans")]
	graph = "  <  div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://trips.ucc.ie/static/tmp/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	return graph


def lengths_scatter(master_dict,filename,title_size,axis_label_size,marker_size,short_code):

0 View Source File : traninfo_plots.py
License : MIT License
Project Creator : skiniry

def lengths_scatter(master_dict,filename,title_size,axis_label_size,marker_size,short_code):
	x_values = []
	gc_list = master_dict[1]["lengths"]
	tran_list = master_dict[1]["trans"]
	for i in range(1,len(gc_list)+1):
		x_values.append(i)
	source = ColumnDataSource({'x': x_values,'y':gc_list,'trans':tran_list})	
	x_num = len(gc_list)	
		
	x_values2 = []
	gc_list2 = master_dict[2]["lengths"]
	tran_list2 = master_dict[2]["trans"]
	for i in range(1,len(gc_list2)+1):
		x_values2.append(x_num+i)
	source2 = ColumnDataSource({'x': x_values2,'y':gc_list2,'trans':tran_list2})
	x_num += len(gc_list2)
	
	x_values3 = []
	gc_list3 = master_dict[3]["lengths"]
	tran_list3 = master_dict[3]["trans"]
	for i in range(1,len(gc_list3)+1):
		x_values3.append(x_num+i)
	source3 = ColumnDataSource({'x': x_values3,'y':gc_list3,'trans':tran_list3})
	x_num += len(gc_list3)
	
	x_values4 = []
	gc_list4 = master_dict[4]["lengths"]
	tran_list4 = master_dict[4]["trans"]
	for i in range(1,len(gc_list4)+1):
		x_values4.append(x_num+i)
	source4 = ColumnDataSource({'x': x_values4,'y':gc_list4,'trans':tran_list4})
	x_num += len(gc_list4)	
	full_title = "Lengths ({})".format(short_code)
	y_lab = "Length"

	p = figure(plot_width=1300, plot_height=1300,x_axis_label="", title=full_title,y_axis_label=y_lab,toolbar_location="below",
			tools = "reset,pan,box_zoom,save,hover,tap")
	p.title.align="center"
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	#p.title.text_font_size = title_size
	#p.xaxis.axis_label_text_font_size = axis_label_size
	#p.xaxis.major_label_text_font_size = marker_size
	#p.yaxis.axis_label_text_font_size = axis_label_size
	#p.yaxis.major_label_text_font_size = marker_size
	#p.background_fill_color = background_col
	p.xgrid.grid_line_color = "#cccccc"
	p.ygrid.grid_line_color = "#cccccc"
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source,fill_color='green')
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source2,fill_color='red')
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source3,fill_color='blue')
	p.scatter('x','y', alpha=0.2,color="black",fill_alpha=1,size=12,source=source4,fill_color='yellow')
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'
	hover.tooltips = [("GC%", "@y"),("Count","@x"),("Transcript","@trans")]
	graph = "  <  div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://trips.ucc.ie/static/tmp/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	return graph



def nuc_comp_box(master_dict,filename,nucleotide,title_size,box_colour,axis_label_size,marker_size,short_code):

0 View Source File : traninfo_plots.py
License : MIT License
Project Creator : skiniry

def nuc_comp_box(master_dict,filename,nucleotide,title_size,box_colour,axis_label_size,marker_size,short_code):
	gc_list = master_dict[1]["gc"]
	gc_list2 = master_dict[2]["gc"]
	gc_list3 = master_dict[3]["gc"]
	gc_list4 = master_dict[4]["gc"]
	
	a_list = master_dict[1]["a"]
	a_list2 = master_dict[2]["a"]
	a_list3 = master_dict[3]["a"]
	a_list4 = master_dict[4]["a"]
	
	t_list = master_dict[1]["t"]
	t_list2 = master_dict[2]["t"]
	t_list3 = master_dict[3]["t"]
	t_list4 = master_dict[4]["t"]
	
	g_list = master_dict[1]["g"]
	g_list2 = master_dict[2]["g"]
	g_list3 = master_dict[3]["g"]
	g_list4 = master_dict[4]["g"]
	
	c_list = master_dict[1]["c"]
	c_list2 = master_dict[2]["c"]
	c_list3 = master_dict[3]["c"]
	c_list4 = master_dict[4]["c"]
	
	cats = []
	grouplist = []
	gclist = []
	
	
	if nucleotide == "A":
		cats.append("Group 1")
		for item in a_list:
			grouplist.append("Group 1")
			gclist.append(item)
		if a_list2 != []:
			cats.append("Group 2")
			for item in a_list2:
				grouplist.append("Group 2")
				gclist.append(item)
		if a_list3 != []:
			cats.append("Group 3")
			for item in a_list3:
				grouplist.append("Group 3")
				gclist.append(item)
		if a_list4 != []:
			cats.append("Group 4")
			for item in a_list4:
				grouplist.append("Group 4")
				gclist.append(item)
	if nucleotide == "C":
		cats.append("Group 1")
		for item in c_list:
			grouplist.append("Group 1")
			gclist.append(item)
		if c_list2 != []:
			cats.append("Group 2")
			for item in c_list2:
				grouplist.append("Group 2")
				gclist.append(item)
		if c_list3 != []:
			cats.append("Group 3")
			for item in c_list3:
				grouplist.append("Group 3")
				gclist.append(item)
		if c_list4 != []:
			cats.append("Group 4")
			for item in c_list4:
				grouplist.append("Group 4")
				gclist.append(item)
	if nucleotide == "GC":
		cats.append("Group 1")
		for item in gc_list:
			grouplist.append("Group 1")
			gclist.append(item)
		if gc_list2 != []:
			cats.append("Group 2")
			for item in gc_list2:
				grouplist.append("Group 2")
				gclist.append(item)
		if gc_list3 != []:
			cats.append("Group 3")
			for item in gc_list3:
				grouplist.append("Group 3")
				gclist.append(item)
		if gc_list4 != []:
			cats.append("Group 4")
			for item in gc_list4:
				grouplist.append("Group 4")
				gclist.append(item)
	if nucleotide == "G":
		cats.append("Group 1")
		for item in g_list:
			grouplist.append("Group 1")
			gclist.append(item)
		if g_list2 != []:
			cats.append("Group 2")
			for item in g_list2:
				grouplist.append("Group 2")
				gclist.append(item)
		if g_list3 != []:
			cats.append("Group 3")
			for item in g_list3:
				grouplist.append("Group 3")
				gclist.append(item)
		if g_list4 != []:
			cats.append("Group 4")
			for item in g_list4:
				grouplist.append("Group 4")
				gclist.append(item)	
	if nucleotide == "T":
		cats.append("Group 1")
		for item in t_list:
			grouplist.append("Group 1")
			gclist.append(item)
		if t_list2 != []:
			cats.append("Group 2")
			for item in t_list2:
				grouplist.append("Group 2")
				gclist.append(item)
		if t_list3 != []:
			cats.append("Group 3")
			for item in t_list3:
				grouplist.append("Group 3")
				gclist.append(item)
		if t_list4 != []:
			cats.append("Group 4")
			for item in t_list4:
				grouplist.append("Group 4")
				gclist.append(item)
	df = pd.DataFrame({"group":grouplist,"gc":gclist})
	groups = df.groupby('group')
	q1 = groups.quantile(q=0.25)
	q2 = groups.quantile(q=0.5)
	q3 = groups.quantile(q=0.75)
	iqr = q3 - q1
	upper = q3 + 1.5*iqr
	lower = q1 - 1.5*iqr

	# find the outliers for each category
	def outliers(group):
		cat = group.name
		return group[(group.gc > upper.loc[cat]['gc']) | (group.gc   <   lower.loc[cat]['gc'])]['gc']
	out = groups.apply(outliers).dropna()
		
	# find the outliers for each category
	def outliers(group):
		cat = group.name
		return group[(group.gc > upper.loc[cat]['gc']) | (group.gc  <  lower.loc[cat]['gc'])]['gc']
			
	# prepare outlier data for plotting, we need coordinates for every outlier.
	if not out.empty:
		outx = []
		outy = []
		for keys in out.index:
			try:
				outx.append(keys[0])
				outy.append(out.loc[keys[0]].loc[keys[1]])
			except:
				pass
	full_title = "{}% ({})".format(nucleotide,short_code)
	y_lab = '{} %'.format(nucleotide)
	p = figure(plot_width=1300, plot_height=1300,tools="reset,pan,box_zoom,save,hover,tap", background_fill_color="#efefef", x_range=cats, toolbar_location="below",title=full_title,y_axis_label=y_lab)
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	#p.background_fill_color = background_color
	p.xgrid.grid_line_color = "white"
	p.ygrid.grid_line_color = "white"
	# if no outliers, shrink gcs of stems to be no longer than the minimums or maximums
	qmin = groups.quantile(q=0.00)
	qmax = groups.quantile(q=1.00)
	upper.gc = [min([x,y]) for (x,y) in zip(list(qmax.loc[:,'gc']),upper.gc)]
	lower.gc = [max([x,y]) for (x,y) in zip(list(qmin.loc[:,'gc']),lower.gc)]

	# stems
	p.segment(cats, upper.gc, cats, q3.gc, line_color="black")
	p.segment(cats, lower.gc, cats, q1.gc, line_color="black")

	# boxes
	p.vbar(cats, 0.7, q2.gc, q3.gc, fill_color=box_colour, line_color="black")
	p.vbar(cats, 0.7, q1.gc, q2.gc, fill_color=box_colour, line_color="black")

	# whiskers (almost-0 height rects simpler than segments)
	p.rect(cats, lower.gc, 0.2, 0.01, line_color="black")
	p.rect(cats, upper.gc, 0.2, 0.01, line_color="black")
	if not out.empty:
		p.circle(outx, outy, size=6, color="#F38630", fill_alpha=0.6)

	p.xgrid.grid_line_color = None
	p.ygrid.grid_line_color = "white"
	p.grid.grid_line_width = 2
	p.xaxis.major_label_text_font_size="12pt"
	
	graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://trips.ucc.ie/static/tmp/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	return graph


def lengths_box(master_dict,filename,box_colour,short_code,title_size,marker_size, axis_label_size):

0 View Source File : traninfo_plots.py
License : MIT License
Project Creator : skiniry

def lengths_box(master_dict,filename,box_colour,short_code,title_size,marker_size, axis_label_size):
	gc_list = master_dict[1]["lengths"]
	gc_list2 = master_dict[2]["lengths"]
	gc_list3 = master_dict[3]["lengths"]
	gc_list4 = master_dict[4]["lengths"]
	cats = []
	grouplist = []
	gclist = []
		
	cats.append("Lengths_G1")
	for item in gc_list:
		grouplist.append("Lengths_G1")
		gclist.append(item)
	if gc_list2 != []:
		cats.append("Lengths_G2")
		for item in gc_list2:
			grouplist.append("Lengths_G2")
			gclist.append(item)
	if gc_list3 != []:
		cats.append("Lengths_G3")
		for item in gc_list3:
			grouplist.append("Lengths_G3")
			gclist.append(item)
	if gc_list4 != []:
		cats.append("Lengths_G4")
		for item in gc_list4:
			grouplist.append("Lengths_G4")
			gclist.append(item)

	df = pd.DataFrame({"group":grouplist,"lengths":gclist})
	groups = df.groupby('group')
	q1 = groups.quantile(q=0.25)
	q2 = groups.quantile(q=0.5)
	q3 = groups.quantile(q=0.75)
	iqr = q3 - q1
	upper = q3 + 1.5*iqr
	lower = q1 - 1.5*iqr

	# find the outliers for each category
	def outliers(group):
		cat = group.name
		return group[(group.lengths > upper.loc[cat]['lengths']) | (group.lengths   <   lower.loc[cat]['lengths'])]['lengths']
	out = groups.apply(outliers).dropna()
		
	# find the outliers for each category
	def outliers(group):
		cat = group.name
		return group[(group.lengths > upper.loc[cat]['lengths']) | (group.lengths  <  lower.loc[cat]['lengths'])]['lengths']
			
	# prepare outlier data for plotting, we need coordinates for every outlier.
	if not out.empty:
		outx = []
		outy = []
		for keys in out.index:
			outx.append(keys[0])
			outy.append(out.loc[keys[0]].loc[keys[1]])
		
	full_title = "Lengths ({})".format(short_code)
	p = figure(plot_width=1300, plot_height=1300,tools="reset,pan,box_zoom,save,hover,tap",title=full_title, background_fill_color="#efefef", x_range=cats, toolbar_location="below")
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = marker_size
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	# if no outliers, shrink gcs of stems to be no longer than the minimums or maximums
	qmin = groups.quantile(q=0.00)
	qmax = groups.quantile(q=1.00)
	upper.lengths = [min([x,y]) for (x,y) in zip(list(qmax.loc[:,'lengths']),upper.lengths)]
	lower.lengths = [max([x,y]) for (x,y) in zip(list(qmin.loc[:,'lengths']),lower.lengths)]

	# stems
	p.segment(cats, upper.lengths, cats, q3.lengths, line_color="black")
	p.segment(cats, lower.lengths, cats, q1.lengths, line_color="black")

	# boxes
	p.vbar(cats, 0.7, q2.lengths, q3.lengths, fill_color=box_colour, line_color="black")
	p.vbar(cats, 0.7, q1.lengths, q2.lengths, fill_color=box_colour, line_color="black")

	# whiskers (almost-0 height rects simpler than segments)
	p.rect(cats, lower.lengths, 0.2, 0.01, line_color="black")
	p.rect(cats, upper.lengths, 0.2, 0.01, line_color="black")
	if not out.empty:
		p.circle(outx, outy, size=6, color="#F38630", fill_alpha=0.6)

	p.xgrid.grid_line_color = None
	p.ygrid.grid_line_color = "white"
	p.grid.grid_line_width = 2
	p.xaxis.major_label_text_font_size="12pt"
	
	graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://trips.ucc.ie/static/tmp/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(filename)
	#graph = " < div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br>  < /div>".format(short_code)
	#layout = column(text_input, p)
	graph += file_html(p,CDN)
	return graph




def codon_usage(codon_dict,short_code,title_size, axis_label_size, marker_size,filename):

0 View Source File : traninfo_plots.py
License : MIT License
Project Creator : skiniry

def codon_usage(codon_dict,short_code,title_size, axis_label_size, marker_size,filename):
	allxvals = []
	allyvals = []
	alllabels = []
	amino_acids = []
	aa_dict = {"TTT":"Phenylalanine", "TTC":"Phenylalanine", "TTA":"Leucine", "TTG":"Leucine",
    "TCT":"Serine", "TCC":"Serine", "TCA":"Serine", "TCG":"Serine",
    "TAT":"Tyrosine", "TAC":"Tyrosine", "TAA":"*", "TAG":"*",
    "TGT":"Cysteine", "TGC":"Cysteine", "TGA":"*", "TGG":"Tryptophan",
    "CTT":"Leucine", "CTC":"Leucine", "CTA":"Leucine", "CTG":"Leucine",

    "CCT":"Proline", "CCC":"Proline", "CCA":"Proline", "CCG":"Proline",
    "CAT":"Histidine", "CAC":"Histidine", "CAA":"Glutamine", "CAG":"Glutamine",
    "CGT":"Arginine", "CGC":"Arginine", "CGA":"Arginine", "CGG":"Arginine",
    "ATT":"Isoleucine", "ATC":"Isoleucine", "ATA":"Isoleucine", "ATG":"Methionine",

    "ACT":"Threonine", "ACC":"Threonine", "ACA":"Threonine", "ACG":"Threonine",
    "AAT":"Asparagine", "AAC":"Asparagine", "AAA":"Lysine", "AAG":"Lysine",
    "AGT":"Serine", "AGC":"Serine", "AGA":"Arginine", "AGG":"Arginine",
    "GTT":"Valine", "GTC":"Valine", "GTA":"Valine", "GTG":"Valine",

    "GCT":"Alanine", "GCC":"Alanine", "GCA":"Alanine", "GCG":"Alanine",
    "GAT":"Aspartic Acid", "GAC":"Aspartic Acid", "GAA":"Glutamic Acid", "GAG":"Glutamic Acid",
    "GGT":"Glycine", "GGC":"Glycine", "GGA":"Glycine", "GGG":"Glycine"}

	codon_list = ["ATG","TTT","TTC","CTT", "CTC", "CTA", "CTG","TTA","TTG", "AGT", "AGC","TCT", "TCC", "TCA", "TCG","TAT", "TAC","TGT", "TGC","TGG","CCT", "CCC", "CCA",
			   "CCG","CAT", "CAC", "CAA", "CAG","AGA", "AGG","CGT", "CGC", "CGA", "CGG","ATT", "ATC", "ATA","ACT", "ACC", "ACA", "ACG","AAT", "AAC", "AAA", "AAG","GTT",
			   "GTC", "GTA", "GTG","GCT", "GCC", "GCA", "GCG","GAT", "GAC", "GAA", "GAG","GGT", "GGC", "GGA", "GGG","TAG","TAA","TGA"]

	curr_count = 0
	for codon in codon_list:
		curr_count+=1
		allxvals.append(curr_count)
		if codon in codon_dict:
			allyvals.append(codon_dict[codon])
		else:
			allyvals.append(0)
		alllabels.append(codon)
		amino_acids.append(aa_dict[codon])
	full_title = "Codon usage ({})".format(short_code)
	x_lab = ''
	y_lab = 'Count'
	min_y = min(0,min(allyvals))-.02
	max_y = max(allyvals)*1.05
	p = figure(plot_width=1300, plot_height=1300,x_axis_label=x_lab,  y_axis_label=y_lab,title= full_title,toolbar_location="below",
			   tools = "reset,pan,box_zoom,hover,tap,save",y_range=(min_y,max_y))
	p.title.align="center"
	p.title.text_font_size = title_size
	p.xaxis.axis_label_text_font_size = axis_label_size
	p.xaxis.major_label_text_font_size = "14pt"
	p.yaxis.axis_label_text_font_size = axis_label_size
	p.yaxis.major_label_text_font_size = marker_size
	#p.background_fill_color = background_color
	p.xgrid.grid_line_color = "white"
	p.ygrid.grid_line_color = "white"
	color_palette_list = []
	colormap =cm.get_cmap("gist_rainbow") #choose any matplotlib colormap here
	start_val = 0.75
	for i in range(0,21):
		start_val -= 0.0293
		rgb = colormap(start_val)[:3]
		hexval = matplotlib.colors.rgb2hex(rgb)
		color_palette_list.append(hexval)

	color_map = bmo.CategoricalColorMapper(factors=["Phenylalanine","Leucine","Serine","Tyrosine","*","Cysteine","Tryptophan","Proline","Histidine","Glutamine","Arginine","Isoleucine","Methionine","Threonine","Asparagine","Lysine","Valine","Alanine","Aspartic Acid","Glutamic Acid","Glycine"],
									palette=color_palette_list)
	p.quad(top=[max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y,max_y],
				bottom=[min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y,min_y],
				left=[0.5,3.5,15.5,19.5,24.5,28.5,37.5,43.5,49.5,55.5,61.5],
				right=[1.5,9.5,17.5,20.5,26.5,34.5,41.5,45.5,53.5,57.5,64.5],
				color="#e0e0e0")
	source = ColumnDataSource({'x': allxvals,'y':allyvals,'labels':alllabels,'amino_acids':amino_acids})
	p.scatter('x','y',source=source, alpha=1,color={'field': 'amino_acids', 'transform': color_map},size=16,line_color="black")
	p.xaxis.ticker = [1,2.5,6.5,12.5,16.5,18.5,20,22.5,25.5,27.5,31.5,36,39.5,42.5,44.5,47.5,51.5,54.5,56.5,59.5,63]
	p.xaxis.major_label_overrides = {1:"Met",2.5:"Phe",6.5:"Leu",12.5:"Ser",16.5:"Tyr",18.5:"Cys",20:"Trp",22.5:"Pro",25.5:"His",27.5:"Gln",31.5:"Arg",
								 36:"Ile",39.5:"Thr",42.5:"Asn",44.5:"Lys",47.5:"Val",51.5:"Ala",54.5:"Asp",56.5:"Glu",59.5:"Gly",63:"Stop"}
	#p.vbar(x=[1], width=1,bottom=0,color="gray",top=[1])

	hover = p.select(dict(type=HoverTool))
	hover.tooltips = [("Count", "@y"),("Codon","@labels"),("Amino acid","@amino_acids")]

	output_file("scatter10k.html", title="Codon usage")
	hover = p.select(dict(type=HoverTool))
	hover.mode = 'mouse'
	graph = "  <  div style='padding-left: 55px;padding-top: 22px;'> < a href='https://trips.ucc.ie/short/' target='_blank' > < button class='button centerbutton' type='submit'> < b>Direct link to this plot < /b> < /button> < /a> < br> < a href='https://trips.ucc.ie/static/tmp/{0}' target='_blank' > < button class='button centerbutton' type='submit'> < b>Download results as csv file < /b> < /button> < /a>  < /div>".format(filename)
	graph += file_html(p,CDN)
	return graph




def make_autopct(values):

0 View Source File : chart.py
License : Apache License 2.0
Project Creator : spotify

    def _figure_to_png(self):
        """Convert figure object to PNG
        Bokeh can only save figure objects as html.
        To convert to PNG the HTML file is opened in a headless browser.
        """
        driver = self._initialize_webdriver()
        # Save figure as HTML
        html = file_html(self.figure, resources=INLINE, title="")
        fp = tempfile.NamedTemporaryFile(
            'w', prefix='chartify', suffix='.html', encoding='utf-8')
        fp.write(html)
        fp.flush()
        # Open html file in the browser.
        driver.get("file:///" + fp.name)
        driver.execute_script("document.body.style.margin = '0px';")
        png = driver.get_screenshot_as_png()
        driver.quit()
        fp.close()
        # Resize image if necessary.
        image = Image.open(BytesIO(png))
        target_dimensions = (self.style.plot_width, self.style.plot_height)
        if image.size != target_dimensions:
            image = image.resize(target_dimensions, resample=Image.LANCZOS)
        return image

    def _set_svg_backend_decorator(f):

0 View Source File : chart.py
License : Apache License 2.0
Project Creator : spotify

    def _figure_to_svg(self):
        """
        Convert the figure to an svg so that it can be saved to a file.
        https://github.com/bokeh/bokeh/blob/master/bokeh/io/export.py
        """
        driver = self._initialize_webdriver()
        html = file_html(self.figure, resources=INLINE, title="")

        fp = tempfile.NamedTemporaryFile(
            'w', prefix='chartify', suffix='.html', encoding='utf-8')
        fp.write(html)
        fp.flush()
        driver.get("file:///" + fp.name)
        wait_until_render_complete(driver, 5)
        svgs = driver.execute_script(_SVG_SCRIPT)
        fp.close()

        driver.quit()
        return svgs[0]

    def _save_svg(self, svg, filename):

0 View Source File : bokeh_utils.py
License : BSD 3-Clause "New" or "Revised" License
Project Creator : XENONnT

def bokeh_to_wiki(fig, outputfile=None):
    """
    Function which converts bokeh HTML code to a wiki readable code.

    :param fig: Figure to be conerted
    :param outputfile: String of absolute file path. If specified output
        is writen to the file. Else output is print to the notebook and
        can be simply copied into the wiki.
    """
    # convert plot to wiki format:
    html = file_html(fig, CDN)
    html = '\n'.join((['  <  html>'] + html.split('\n')[6:]))

    if outputfile:
        with open(outputfile, mode='w') as file:
            file.write(html)
    else:
        print(html)


def get_peaks_source(peaks, relative_start=0, time_scaler=1, keep_amplitude_per_sample=True):