Auto rescale plots

An attempt at creating a semi-automated way of scaling the plots to different widths and heights. Unfortunately scaling legend items is really tricky with plotly :/

[1]:
import RNApysoforms as RNApy
import polars as pl
[2]:
## Define your scaling factors
plot_width = 600
plot_height = 250
scaling_factor = 0.5



## Path to your ENSEMBL GTF file, counts matrix file, and metadata file
ensembl_gtf_path = "../../tests/test_data/Homo_sapiens_chr21_and_Y.GRCh38.110.gtf"
counts_matrix_path = "../../tests/test_data/counts_matrix_chr21_and_Y.tsv"
metadata_path = "../../tests/test_data/sample_metadata.tsv"


## Read ENSEMBL GTF and counts matrix with metadata and normalizations
annotation = RNApy.read_ensembl_gtf(ensembl_gtf_path)
counts_matrix = RNApy.read_expression_matrix(expression_matrix_path=counts_matrix_path,
                                          metadata_path=metadata_path,
                                           cpm_normalization=True, relative_abundance=True)


## Filter APP gene and keep only top 5 expressed transcripts
app_annotation, app_counts_matrix = RNApy.gene_filtering(annotation=annotation, expression_matrix=counts_matrix, target_gene="APP",
                                                        order_by_expression=True, keep_top_expressed_transcripts=5,
                                                        order_by_expression_column="counts")


# Rescale introns
app_annotation = RNApy.shorten_gaps(app_annotation)


# Define a mapping from transcript_biotype to colors
biotype_colors = {
    'protein_coding': '#F8766D',
    'protein_coding_CDS_not_defined': '#00BFC4'
}

# Define a mapping for "AD Status" from expression matrix
ad_status_colors = {
    'AD': '#7CAE00',
    'Control': '#C77CFF',
}



"""
Notice the `marker_size` and `arrow_size` parameters being scaled by the scaling factor
"""
traces = RNApy.make_traces(annotation=app_annotation,  expression_matrix=app_counts_matrix,
                        x_start="rescaled_start", x_end="rescaled_end",
                         y='transcript_id', annotation_hue="transcript_biotype",
                         hover_start="start", hover_end="end",
                         expression_columns=["counts", "CPM", "relative_abundance"],
                         expression_hue="AD status", marker_size=(5*scaling_factor), arrow_size=(10*scaling_factor),
                         annotation_color_map=biotype_colors,
                         expression_color_map=ad_status_colors)

## Put traces into a figure
fig = RNApy.make_plot(traces=traces, subplot_titles=["Transcript Structure", "Counts", "CPM", "Relative Abundance"],
                   width=1200, height=600)


"""
Update layout of plot using `plot_height` and `plot_width` defined at the top of the function
as well as changing font sizes and plot margins using the scaling_factor
"""
fig.update_layout(
    height=plot_height,
    width=plot_width,
    font_size=(12*scaling_factor),
    legend_tracegroupgap=(7*scaling_factor),
    yaxis=dict(tickfont=dict(size=(12*scaling_factor))),
    legend=dict(font=dict(size=(12*scaling_factor)), grouptitlefont=dict(size=(12*scaling_factor))),
    annotations=[dict(font=dict(size=(12*scaling_factor))) for annotation in fig['layout']['annotations']],
    margin=dict(l=(100*scaling_factor),
                r=(50*scaling_factor), t=(100*scaling_factor), b=(50*scaling_factor)))

"""
There is a bug in plotly and the tickfont for the x-axis has to be updated in this manner,
it can't be done using the `update_layout` function. See: https://github.com/plotly/plotly.py/issues/2922
"""
fig.update_xaxes(tickfont_size=(12*scaling_factor))

## Show figure
fig.show()

Notes:

You can click on the legend items to make figure elements appear and disappear.

The legend title will get grayed out when clicking on the first legend item. I could not find a workaround for that with the current plotly release (version 5).

The hovering for exons and CDS works best if you hover your mouse over the corners of the CDS/exon boxes.