示例#1
0
def plot_counts(counts: pd.DataFrame):
	counts.bc_l = pd.Categorical(counts.bc_l, bc_range(counts.bc_l))
	counts.bc_r = pd.Categorical(counts.bc_r, bc_range(counts.bc_r))
	log = np.log
	return (
		ggplot(counts, aes('bc_r', 'bc_l', fill='log(Count)'))
		+ geom_tile()
		# + scale_y_reverse()
		+ coord_fixed()
		+ scale_fill_cmap('inferno')
		+ theme(axis_text_x=element_text(angle=90, vjust=.5))
		+ labs(x='Right Barcode', y='Left Barcode')
	)
示例#2
0
def user_theme(
    plot: p9.ggplot,
    x_axis_label: str = "",
    y_axis_label: str = "",
    title: str = "",
    **plot_theme,
) -> p9.ggplot:
    """Render the specified plot in the current theme with common attributes to all plots eg. legend_position etc. The themed plot is
    returned. Saves code in each plot by handled all the standard stuff here."""
    theme_args = {  # TODO FIXME... make defaults chosen from user profile
        "axis_text_x": p9.element_text(size=7),
        "axis_text_y": p9.element_text(size=7),
        "figure_size": (12, 6),
        "legend_position": "none",
    }
    theme_args.update(**plot_theme)

    # remove asxtrade kwargs
    want_cmap_d = theme_args.pop("asxtrade_want_cmap_d", True)
    want_fill_d = theme_args.pop(
        "asxtrade_want_fill_d",
        False)  # most graphs dont fill, so False by default
    want_fill_continuous = theme_args.pop("asxtrade_want_fill_continuous",
                                          False)
    plot = (
        plot +
        p9.theme_bw()  # TODO FIXME... make chosen theme from user profile
        + p9.labs(x=x_axis_label, y=y_axis_label, title=title) +
        p9.theme(**theme_args))
    if want_cmap_d:
        plot += p9.scale_colour_cmap_d()
    if want_fill_d:
        plot += p9.scale_fill_cmap_d()
    elif want_fill_continuous:
        plot += p9.scale_fill_cmap()
    return plot
示例#3
0
def log_HR_plot(inFile, label_unit=10, log_scale_color=True):
    par = get_params(inFile)
    pca_components = par["means"]["logHR"].shape[0] >> 1
    components = range(pca_components)
    tf_components = slice(pca_components, 2 * pca_components)

    logHR_df = pd.DataFrame(index=[f"{i+1}" for i in components])
    logHR_df["tumor logHR"] = par["means"]["logHR"][components, 0]
    logHR_df["non-tumor logHR"] = par["means"]["logHR"][tf_components, 0]
    logHR_df["component"] = components
    logHR_df["label"] = [
        logHR_df.index[i] if i <= label_unit else "" for i in components
    ]
    logHR_df["tumor logHR sd"] = par["stds"]["logHR"][components, 0]
    logHR_df["non-tumor logHR sd"] = par["stds"]["logHR"][tf_components, 0]
    logHR_df["tumor Z"] = logHR_df["tumor logHR"] / logHR_df["tumor logHR sd"]
    logHR_df["non-tumor Z"] = (logHR_df["non-tumor logHR"] /
                               logHR_df["tumor logHR sd"])
    logHR_df["tumor p-value"] = norm.sf(abs(logHR_df["tumor Z"])) * 2
    logHR_df["non-tumor p-value"] = norm.sf(abs(logHR_df["non-tumor Z"])) * 2
    logHR_df["tumor -log10(p-value)"] = -np.log10(logHR_df["tumor p-value"])
    logHR_df["non-tumor -log10(p-value)"] = -np.log10(
        logHR_df["non-tumor p-value"])

    lb = min(logHR_df["non-tumor logHR"].min(), logHR_df["tumor logHR"].min())
    ub = max(logHR_df["non-tumor logHR"].max(), logHR_df["tumor logHR"].max())
    pl = (pn.ggplot(
        pn.aes(
            "non-tumor logHR",
            "tumor logHR",
            color="non-tumor p-value",
            fill="tumor p-value",
            label="label",
        ),
        logHR_df,
    ) + pn.xlim(lb, ub) + pn.ylim(lb, ub) + pn.geom_abline() +
          pn.geom_point() + pn.theme_minimal() +
          pn.geom_text(ha="left", va="bottom", color="black"))
    if log_scale_color:
        pl += pn.scale_color_cmap(trans="log")
        pl += pn.scale_fill_cmap(trans="log")

    lb = min(
        logHR_df["non-tumor -log10(p-value)"].min(),
        logHR_df["tumor -log10(p-value)"].min(),
    )
    ub = max(
        logHR_df["non-tumor -log10(p-value)"].max(),
        logHR_df["tumor -log10(p-value)"].max(),
    )
    pl_p = (pn.ggplot(
        pn.aes(
            "non-tumor -log10(p-value)",
            "tumor -log10(p-value)",
            color="component",
            label="label",
        ),
        logHR_df,
    ) + pn.geom_point() + pn.xlim(lb, ub) + pn.ylim(lb, ub) +
            pn.theme_minimal() +
            pn.geom_text(ha="left", va="bottom", color="black"))
    return pl, pl_p, logHR_df
    ["x_loc", "y_loc", "well", "site_location",
     "site"])["total_cell_count"].mean().reset_index())

plate = cell_count_df["plate"].unique()[0]

os.makedirs(output_figuresdir, exist_ok=True)
by_well_gg = (
    gg.ggplot(cell_count_totalcells_df, gg.aes(x="x_loc", y="y_loc")) +
    gg.geom_point(gg.aes(fill="total_cell_count"), size=10) +
    gg.geom_text(gg.aes(label="site_location"), color="lightgrey") +
    gg.facet_wrap("~well") + gg.coord_fixed() + gg.theme_bw() +
    gg.ggtitle(f"Total Cells/Well\n{plate}") + gg.theme(
        axis_text=gg.element_blank(),
        axis_title=gg.element_blank(),
        strip_background=gg.element_rect(colour="black", fill="#fdfff4"),
    ) + gg.labs(fill="Cells") + gg.scale_fill_cmap(name="magma"))

output_file = pathlib.Path(output_figuresdir,
                           "plate_layout_cells_count_per_well.png")
if check_if_write(output_file, force, throw_warning=True):
    by_well_gg.save(output_file, dpi=300, verbose=False)

# Plot cell category ratios per well
ratio_df = pd.pivot_table(
    cell_count_df,
    values="cell_count",
    index=["site", "plate", "well", "site_location", "x_loc", "y_loc"],
    columns=["Cell_Quality"],
)
ratio_df = ratio_df.assign(Sum=ratio_df.sum(axis=1),
                           Pass_Filter=ratio_df[cell_filter].sum(axis=1))
示例#5
0
os.makedirs(output_figuresdir, exist_ok=True)
by_well_gg = (
    gg.ggplot(cell_count_totalcells_df, gg.aes(x="x_loc", y="y_loc"))
    + gg.geom_point(gg.aes(fill="total_cell_count"), size=10)
    + gg.geom_text(gg.aes(label="site_location"), color="lightgrey")
    + gg.facet_wrap("~well")
    + gg.coord_fixed()
    + gg.theme_bw()
    + gg.ggtitle(f"Total Cells/Well\n{plate}")
    + gg.theme(
        axis_text=gg.element_blank(),
        axis_title=gg.element_blank(),
        strip_background=gg.element_rect(colour="black", fill="#fdfff4"),
    )
    + gg.labs(fill="Cells")
    + gg.scale_fill_cmap(name="magma")
)

output_file = pathlib.Path(output_figuresdir, "plate_layout_cells_count_per_well.png")
if check_if_write(output_file, force, throw_warning=True):
    by_well_gg.save(output_file, dpi=300, verbose=False)

# Plot cell category ratios per well
ratio_df = pd.pivot_table(
    cell_count_df,
    values="cell_count",
    index=["site", "plate", "well", "site_location", "x_loc", "y_loc"],
    columns=["Cell_Quality"],
)
ratio_df = ratio_df.assign(
    Sum=ratio_df.sum(axis=1), Pass_Filter=ratio_df[cell_filter].sum(axis=1)
            ],
            gg.aes(x="x_loc", y="y_loc"),
        )
        + gg.geom_point(gg.aes(fill="total_cell_count"), shape="s", size=6)
        + gg.geom_text(gg.aes(label="site_location"), color="lightgrey", size=6)
        + gg.facet_wrap("~well")
        + gg.coord_fixed()
        + gg.theme_bw()
        + gg.ggtitle(f"Total Cells/Well\n{plate}")
        + gg.theme(
            axis_text=gg.element_blank(),
            axis_title=gg.element_blank(),
            strip_background=gg.element_rect(colour="black", fill="#fdfff4"),
        )
        + gg.labs(fill="Cells")
        + gg.scale_fill_cmap(name="Number of Cells")
    )

    output_file = pathlib.Path(
        output_figuresdir, f"plate_layout_cells_count_per_well_{plate}.png"
    )
    if check_if_write(output_file, force, throw_warning=True):
        by_well_gg.save(output_file, dpi=300, verbose=False)

# Plot cell category ratios per well and empty cells per well
ratio_df = pd.pivot_table(
    cell_count_df,
    values="cell_count",
    index=["site", "plate", "well", "site_location", "x_loc", "y_loc"],
    columns=["Cell_Quality"],
)