-
Notifications
You must be signed in to change notification settings - Fork 83
Add Relative Log Expression (RLE) Plots #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
8d9dfef
7012b83
9c60aee
5141dea
6e797e2
d563a3e
6c93fed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1608,3 +1608,64 @@ def lowess( | |
| delta = (1 - delta**2) ** 2 | ||
|
|
||
| return yest | ||
|
|
||
|
|
||
| def make_rle_plot( | ||
| count_matrix: np.array, | ||
| sample_ids: np.array, | ||
| normalize: bool = False, | ||
| save_path: Optional[str] = None, | ||
| **kwargs, | ||
| ) -> None: | ||
| """ | ||
| Create a ratio of log expression plot using matplotlib. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| count_matrix : ndarray | ||
| An mxn matrix of count data, where m is the number of samples (rows), | ||
| and n is the number of genes (columns). | ||
|
|
||
| sample_ids : ndarray | ||
| An array of sample identifiers. | ||
|
|
||
| normalize : bool | ||
| Whether to normalize the count matrix before plotting. (default: ``False``). | ||
|
|
||
| save_path : str or None | ||
| The path where to save the plot. If left None, the plot won't be saved | ||
| (default: ``None``). | ||
|
|
||
| **kwargs : | ||
| Additional keyword arguments passed to matplotlib's boxplot function. | ||
| """ | ||
| if normalize: | ||
| print("Plotting normalized RLE plot...") | ||
| geometric_mean = np.exp(np.mean(np.log(count_matrix + 1), axis=0)) | ||
| size_factors = np.median(count_matrix / geometric_mean, axis=1) | ||
| count_matrix = count_matrix / size_factors[:, np.newaxis] | ||
|
Comment on lines
+1642
to
+1645
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is recomputing median-of-ratios size factors, right? (up to the +1 in the log) In that case, it might be better to add a From there, in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @BorisMuzellec , For some reason when I use the size factors computed in dds.fit_size_factors() I do not get an RLE plot with the sample medians centered around 0. But when I compute the sizefactors internally I do get an RLE plot with the sample medians centered around 0 Do you know what could be causing this?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why we would expect the sample medians to be zero given that RLE plot subtracts the gene medians. Also, I would be wary of making any conclusion from the test data. That being said what you wrote is what is implemented in R's
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonathjd I think we may still have some consistency issues though: if we're using
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BorisMuzellec Happy holidays! Sorry for the delay, I was on vacation. I added the psuedocount and ran some tests and the psuedocount alters the gene medians distance from the sample medians and skews the plot significantly. |
||
|
|
||
| plt.rcParams.update({"font.size": 10}) | ||
|
|
||
| fig, ax = plt.subplots(figsize=(15, 8), dpi=600) | ||
|
|
||
| # Calculate median expression across samples | ||
| gene_medians = np.median(count_matrix, axis=0) | ||
| rle_values = np.log2(count_matrix / gene_medians) | ||
|
|
||
| kwargs.setdefault("alpha", 0.5) | ||
| boxprops = {"facecolor": "lightgray", "alpha": kwargs.pop("alpha")} | ||
|
|
||
| ax.boxplot(rle_values.T, patch_artist=True, boxprops=boxprops, **kwargs) | ||
|
|
||
| ax.axhline(0, color="red", linestyle="--", linewidth=1, alpha=0.5, zorder=3) | ||
| ax.set_xlabel("Sample") | ||
| ax.set_ylabel("Relative Log Expression") | ||
| ax.set_xticks(np.arange(len(sample_ids))) | ||
| ax.set_xticklabels(sample_ids, rotation=90) | ||
| plt.tight_layout() | ||
|
|
||
| if save_path: | ||
| plt.savefig(save_path, bbox_inches="tight") | ||
| else: | ||
| plt.show() | ||


Uh oh!
There was an error while loading. Please reload this page.