<p>Introduccion</p>

<%
import pandas as pd
import numpy as np
import scipy

groups = [ row[4] for row in plotter.hash_vars["boxplot_factor_long.txt"] ]
groups = groups[1:]
groups = pd.Series(groups)
lut = dict(zip(groups.unique(), "rbg"))
row_colors = groups.map(lut)
row_colors = list(pd.Series(list(row_colors), index=[ row[0] for row in plotter.hash_vars["boxplot_factor_long.txt"] ][1:]))
linkages = scipy.cluster.hierarchy.linkage( np.array([row[1:-1] for row in plotter.hash_vars["boxplot_factor_long.txt"]][1:]), method="complete" )

chromosome_lengths = { "1": 247249719, "2": 242951149, "3": 199501827, "4": 191273063, "5": 180857866, 
"6": 170899992, "7": 158821424, "8": 146274826, "9": 140273252, "10": 135374737, "11": 134452384, 
"12": 132349534, "13": 114142980, "14": 106368585, "15": 100338915, "16": 88827254, "17": 78774742, 
"18": 76117153, "19": 63811651, "20": 62435964, "21": 46944323, "22": 49691432, "X": 154913754, "Y": 57772954}

def delete_outliers(data):
    indexes = list(range(1,400))
    for index in indexes[::-1]:
        del data[index]

def plot_coverage(data, plotter_list):
    data["chr"] = pd.Categorical(data["chr"], chromosome_lengths.keys())
    sortedd = data.sort_values(by=["chr", "pos"])
    chromosomes_order = sortedd["chr"].unique()
    
    g = plotter_list["sns"].FacetGrid(data=sortedd, col="chr", hue="chr", col_wrap=2).map(plotter_list["plt"].plot, 'pos', 'cov').map(plotter_list["plt"].fill_between, 'pos', 'cov')
    for ax, chrm in zip(g.axes.flat, chromosomes_order):
        ax.axvline(x=chromosome_lengths[chrm], color='r', linestyle=':')
    return g
%>


<div>

<p>descripcion</p>

<table width="1100">
    <tr>
        <td>
           ${ plotter.static_plot_main( id="coverage_data_modified.txt", header=True, row_names=False, smp_attr=[0], 
                height= 1800, width=500, dpi=400, tight= True,
                plotting_function= plot_coverage )}
        </td>
    </tr>
    <tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                rezisable=True, height=500, width=500, dpi=100,
                plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}
        </td>
    </tr>
    <tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], height=400, width=400,
                                        plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}
        </td>
    </tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4],
                                        plotting_function= lambda data, plotter_list: plotter_list["plt"].boxplot(x=data), whole=True)}
        </td>
    </tr>
    <tr>
        <p>No x_label and y_label options used (default values from seaborn axis labels are usen then, which equals to columns names for the factors)</p>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].boxplot(data=data, x='liver', y='pathway') )}
        </td>
        <p>Using x_label and y_label options as empty string(overrides seaborn behaviour and make the axis labels empty)</p>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], x_label = "", y_label = "",
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].boxplot(data=data, x='liver', y='pathway') )}
        </td>
        <p>Using x_label and y_label options as not empty strings(overrides seaborn behaviour)</p>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], x_label = "x_axis", y_label = "y_axis",
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].boxplot(data=data, x='liver', y='pathway') )}
        </td>
    </tr>
    <tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].violinplot(data=data, x='liver', y='pathway') )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].swarmplot(data=data, x='liver', y='pathway') )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="x_y_crowded.txt", header=True, row_names=True, 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].boxenplot(data=data, y='brain_exp') )}
        </td>
    </tr>
    <tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].ecdfplot(data=data, x='liver', hue='pathway') )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].displot(data=data, x='liver', kde=True) )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="x_y_crowded.txt", header=True, row_names=False, fields=[0,1], func = delete_outliers,
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].displot(data=data, x='n_genes', y='brain_exp') )}
        </td>
    </tr>
    <tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].displot(data=data, x='liver', hue='pathway', multiple="stack") )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], melt = [["liver", "brain", "lung"],["tissue", "values"]],
                                        plotting_function=  lambda data, plotter_list: plotter_list["sns"].displot(data=data, x='values', hue='tissue', col='pathway', kind="kde")  )}        
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].displot(data=data, x='liver', y='brain', kind="kde", rug=True) )}
        </td>
    </tr>
    <tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["plt"].hist(data=data, x='liver') )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].heatmap(data=data), whole=True)}
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4],
                                        plotting_function= lambda data, plotter_list: plotter_list["sns"].clustermap(data=data, row_colors=row_colors, row_linkage=linkages) , whole=True)}
        </td>
    </tr>
    <tr>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], melt = [["liver", "brain", "lung"],["factor", "values"]],
                                        plotting_function=  lambda data, plotter_list: plotter_list["sns"].boxplot(data=data, x='values', y='factor', hue='pathway')  )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function=  lambda data, plotter_list: plotter_list["sns"].scatterplot(data=data, x='liver', y='brain', hue='pathway', size='lung') )}
        </td>
        <td>
            ${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
                                        plotting_function=  lambda data, plotter_list: plotter_list["sns"].jointplot(data=data, x='liver', y='brain', hue='pathway') )}
        </td>
    </tr>
</table>

</div>

<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h1>Using functions with additional arguments and keyword arguments</h1>

<% 
import matplotlib.pyplot as plt
def scatter_with_labels(data, plotter_list, **kwargs):
    ax =  plotter_list["sns"].scatterplot(data=data, x='liver', y='brain', hue='pathway')
    plt.xlabel(kwargs["x_label"])
    plt.ylabel(kwargs["y_label"])
    return ax

text_to_show = """
def scatter_with_labels(data, plotter_list, **kwargs): <br>
&nbsp;&nbsp;&nbsp;&nbsp;ax =  plotter_list["sns"].scatterplot(data=data, x='liver', y='brain', hue='pathway', ) <br>
&nbsp;&nbsp;&nbsp;&nbsp;plt.xlabel(kwargs["x_label"]) <br>
&nbsp;&nbsp;&nbsp;&nbsp;plt.ylabel(kwargs["y_label"]) <br>
&nbsp;&nbsp;&nbsp;&nbsp;return ax
"""
%>

${text_to_show}
<br><br>
${"""plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], plotting_function= scatter_with_labels, kwargs={"x_label": "Liver Expression", "y_label": "Brain Expression"} )"""}

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], plotting_function= scatter_with_labels, kwargs={"x_label": "Liver Expression", "y_label": "Brain Expression"} ) }

</div>



<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h1>Playing with size images</h1>
<h3>All default parameters</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}
</div>


<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h3>Specifing only matplotlib figure size (300x300) in pixels (default option) and dpi of 50</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    height=300, width=300, dpi=50, #units = "pixels", this option is optional in this case, as it is the default value
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}

</div>


<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h3>Specifing only matplotlib figure size Using inches (6x6) instead of pixels (which is the default) and dpi of 50 (img tag size returned in pixels which are the default)</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    height=6, width=6, dpi=50, units = "inches",
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}

</div>    


<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h3>Specifing only matplotlib figure size Using cm (10x10) instead of pixels (which is the default) and dpi of 50 (img tag size returned in pixels which are the default)</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    height=10, width=10, dpi=50, units = "cm",
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}

</div>


<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h3>Using default parameters for matplotlib width and height (600,600) and units (pixels) and dpi 100, <br>
 but changing img tag output size (height='30%' width='30%')</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    img_properties = "height='30%' width='30%'",
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}

</div>


<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h3>Using default parameters for matplotlib width and height (600,600) and units (pixels) and dpi 100, <br>
 but changing img tag output size (height='30vh' width='30vw')</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    img_properties = "height='30vh' width='30vw'",
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}
</div>


<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h3>Using default parameters for matplotlib width and height (600,600) and units (pixels) but dpi 200, <br>
 and changing img tag output size (height='1000px' width='1000px') and also making it resizable</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    img_properties = "height='1000px' width='1000px'", rezisable=True,
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}
</div>


<div style="width: 100vw; height: 20vh; background-color: grey"></div>
<div>
<h3>Using matplotlib image size of 20x20 inches and a img tag size of 20x20 cm and also making it resizable</h3>

${ plotter.static_plot_main( id="boxplot_factor_long.txt", header=True, row_names=True, smp_attr=[4], 
    img_properties = "height='20cm' width='20cm'", rezisable=True,
    height=20, width=20, units = "inches",
    plotting_function= lambda data, plotter_list: plotter_list["plt"].hexbin(data=data, x='liver', y='brain', gridsize=20) )}
</div>