def plot_scatter_matrix(n_clicks, data, columns, selected, ndxs): if n_clicks is None: raise PreventUpdate df = pd.DataFrame(data) df["DateAcquired"] = pd.to_datetime(df["DateAcquired"]) numeric_columns = df.select_dtypes(include=np.number).columns n_dimensions = len(numeric_columns) if ndxs is not None: df = df.reindex(ndxs) fig = px.scatter_matrix(df, dimensions=columns) fig.update_layout( autosize=True, height=1200, showlegend=False, margin=dict(l=50, r=10, b=200, t=50, pad=0), hovermode="closest", ) config = T.gen_figure_config(filename="PQC-scatter-matrix") marker_color = df["Use Downstream"].replace({ True: C.colors["use_downstream"], False: C.colors["dont_use_downstream"] }) marker_line_color = df["Flagged"].replace({ True: C.colors["flagged"], False: C.colors["not_flagged"] }) marker_symbol = [0] * len(df) for i, ndx in enumerate(ndxs): if ndx in selected: marker_color[i] = C.colors["selected"] marker_symbol[i] = 1 fig.update_traces( marker_symbol=marker_symbol, marker_line_color=marker_line_color, marker_line_width=2, opacity=0.8, ) fig.update_traces(marker_color=marker_color) fig.update_traces(marker_size=20) return fig, config
def plot_shapley(shapley_values, qc_data, options, ndxs, tab): if tab != "anomaly": raise PreventUpdate df_shap = pd.read_json(shapley_values) qc_data = pd.DataFrame(qc_data) qc_data = qc_data.iloc[ndxs] if 'hide_rejected' in options: qc_data = qc_data[qc_data['Use Downstream'] != False] fns = qc_data["RawFile"] df_shap = df_shap.loc[fns] fig = T.px_heatmap( df_shap.T, layout_kws=dict(title="Anomaly feature score (shapley values)", height=1200), ) config = T.gen_figure_config( filename="Anomaly-Detection-Shapley-values") return fig, config
def plot_protein_figure( n_clicks, data, ndxs, plot_column, project, pipeline, data_range, qc_data, derived_virtual_indices, ): """Create the protein groups figure.""" if (project is None) or (pipeline is None): raise PreventUpdate if (ndxs is None) or (ndxs == []): raise PreventUpdate if plot_column == "Reporter intensity corrected (normalized)": plot_column = "Reporter intensity corrected" normalized = True else: normalized = False protein_names = list(pd.DataFrame(ndxs)["protein_names"]) qc_data = pd.DataFrame(qc_data) if derived_virtual_indices is not None: qc_data = qc_data.reindex(derived_virtual_indices) raw_files = list(qc_data.RawFile.values) data = T.get_protein_groups( project, pipeline, protein_names=protein_names, columns=[plot_column], data_range=data_range, raw_files=raw_files, ) df = pd.read_json(data) color = None if plot_column == "Reporter intensity corrected": id_vars = ["RawFile", "Majority protein IDs"] df = (df.set_index(id_vars).filter( regex=plot_column).reset_index().melt(id_vars=id_vars, var_name="TMT Channel", value_name=plot_column)) df["TMT Channel"] = df["TMT Channel"].apply( lambda x: f"{int(x.split()[3]):02.0f}") if normalized: df[plot_column] = ( df[plot_column] / df.groupby(["RawFile", "Majority protein IDs" ]).transform("sum")[plot_column]) color = "TMT Channel" df = df.sort_values(["RawFile", "TMT Channel"]) else: df = df.sort_values("RawFile") n_rows = len(df["Majority protein IDs"].drop_duplicates()) height = 300 * n_rows + (100 * n_rows) if n_rows <= 1: facet_row_spacing = 0.04 else: facet_row_spacing = min(0.04, (1 / (n_rows - 1))) fig = px.bar( data_frame=df, x="RawFile", y=plot_column, facet_col="Majority protein IDs", facet_col_wrap=1, color=color, color_discrete_sequence=px.colors.qualitative.Dark24, color_continuous_scale=px.colors.sequential.Rainbow, facet_row_spacing=facet_row_spacing, height=height, category_orders={"RawFile": raw_files}, ) fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1])) fig.update(layout_showlegend=True) fig.update_layout(hovermode="closest") fig.update_xaxes(matches="x") fig.update_xaxes(automargin=True) fig.update_yaxes(automargin=True) fig.update_yaxes(matches=None) if normalized: fig.update_layout(yaxis=dict(range=[0, 1])) config = T.gen_figure_config(filename="PQC-protein-quant") return fig, config
options=list_to_dropdown_options(plot_columns), placeholder="Select data columns", value="Reporter intensity corrected", style={ "width": "100%", "max-width": "100%", "margin-top": "50px", "margin-bottom": "50px", }, ), html.Button("Update figure", id="proteins-fig-update"), dcc.Loading([ html.Div(style={"min-width": 400}), dcc.Graph( id="protein-figure", config=T.gen_figure_config(filename="protein-groups"), ), ]), ]) def callbacks(app): @app.callback( Output("proteins-table", "data"), Input("proteins-update", "n_clicks"), State("project", "value"), State("pipeline", "value"), State("qc-table", "data"), State("tabs", "value"), State("proteins-options", "value"), )
def plot_qc_figure(refresh, selected, ndxs, x, data, optional_columns): """Creates the bar-plot figure""" if (data is None) or (ndxs is None) or (len(ndxs) == 0): raise PreventUpdate if x is None: x = "RawFile" titles = [el["title"] for el in plot_map] df = pd.DataFrame(data) assert pd.value_counts(df.columns).max() == 1, pd.value_counts(df.columns) df["DateAcquired"] = pd.to_datetime(df["DateAcquired"]) if ndxs is not None: df = df.reindex(ndxs) numeric_columns = df[optional_columns].head(1)._get_numeric_data().columns fig = make_subplots( cols=1, rows=len(numeric_columns), subplot_titles=numeric_columns, shared_xaxes=True, # vertical_spacing=0.05, print_grid=True, ) for i, col in enumerate(numeric_columns): trace = go.Bar( x=df[x], y=df[col], name=col, text=None if x == "RawFile" else df["RawFile"], ) fig.add_trace(trace, row=1 + i, col=1) fig.update_layout( hovermode="closest", hoverlabel_namelength=-1, height=200 + 250 * (i + 1), showlegend=False, margin=dict(l=50, r=10, b=200, t=50, pad=0), ) marker_color = df["Use Downstream"].replace({ True: C.colors["accepted"], False: C.colors["rejected"], None: C.colors["unassigned"] }) marker_line_color = df["Flagged"].replace({ True: C.colors["flagged"], False: C.colors["not_flagged"] }) for ndx in selected: marker_color[ndx] = C.colors["selected"] fig.update_traces( marker_color=marker_color, marker_line_color=marker_line_color, marker_line_width=1, opacity=0.8, ) fig.update_xaxes(matches="x") if x == "RawFile": fig.update_layout(xaxis5=dict(tickmode="array", tickvals=tuple(range(len(df))), ticktext=tuple(df[x]))) config = T.gen_figure_config(filename="QC-barplot") return fig, config
def explorer_plot(n_clicks, x, y, color, size, facet_row, facet_col, data, selected, ndxs): columns = [ x, y, color, size, facet_row, facet_col, ] if y is None: raise PreventUpdate if None in columns: columns.remove(None) columns = [c for c in columns if (c is not None)] df = pd.DataFrame(data) df["DateAcquired"] = pd.to_datetime(df["DateAcquired"]) if ndxs is not None: df = df.reindex(ndxs) if facet_row is not None: n_rows = len(df[facet_row].value_counts()) else: n_rows = 2 facet_col_wrap = 3 if facet_col is not None: n_cols = len(df[facet_col].value_counts()) n_rows = min(2, int(n_cols / facet_col_wrap) + 2) if size is not None: # Plotly crashes if size column has NaNs df[size] = df[size].fillna(0) fig = px.scatter( data_frame=df, x=x, y=y, color=color, size=size, facet_row=facet_row, facet_col=facet_col, hover_data=["Index", "RawFile"], facet_col_wrap=facet_col_wrap, ) fig.update_layout( autosize=True, height=300 * n_rows + 200, showlegend=False, margin=dict(l=50, r=10, b=200, t=50, pad=0), hovermode="closest", ) marker_color = df["Use Downstream"].replace({ True: C.colors["accepted"], False: C.colors["rejected"] }) marker_line_color = df["Flagged"].replace({ True: C.colors["flagged"], False: C.colors["not_flagged"] }) marker_symbol = [0] * len(df) for i, ndx in enumerate(ndxs): if ndx in selected: marker_color[i] = C.colors["selected"] marker_symbol[i] = 1 fig.update_traces( marker_symbol=marker_symbol, marker_line_color=marker_line_color, marker_line_width=2, opacity=0.8, ) if color is None: fig.update_traces(marker_color=marker_color) if size is None: fig.update_traces(marker_size=20) config = T.gen_figure_config(filename="PQC-explorer") return fig, config