-
Notifications
You must be signed in to change notification settings - Fork 18
Simplifying MultiPlot Interface #83 #87
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 all commits
64f58ae
9c4a5f1
4e9485c
0f478a5
f873b43
a33fee3
8ffab60
ae057f9
3cc3963
29dbd1e
93af845
c2aa90c
f35157d
82dfd03
76d56a3
ad2104a
f37941f
6785a32
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 |
|---|---|---|
| @@ -1,16 +1,20 @@ | ||
| """ | ||
| Plot Spyogenes subplots ms_matplotlib | ||
| ======================================= | ||
| ==================================================== | ||
|
|
||
| Here we show how we can plot multiple chromatograms across runs together | ||
| """ | ||
|
|
||
| import pandas as pd | ||
| import requests | ||
| import zipfile | ||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
| import sys | ||
| import os | ||
| # sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) | ||
|
|
||
|
|
||
| # Set the plotting backend | ||
| pd.options.plotting.backend = "ms_matplotlib" | ||
|
|
||
| ###### Load Data ####### | ||
|
|
@@ -21,76 +25,51 @@ | |
|
|
||
| # Download the zip file | ||
| try: | ||
| # Save the zip file to the current directory | ||
| print(f"Downloading {zip_filename}...") | ||
| response = requests.get(url) | ||
| response.raise_for_status() # Check for any HTTP errors | ||
|
|
||
| # Save the zip file to the current directory | ||
| with open(zip_filename, "wb") as out: | ||
| out.write(response.content) | ||
| print(f"Downloaded {zip_filename} successfully.") | ||
| except requests.RequestException as e: | ||
| print(f"Error downloading zip file: {e}") | ||
| except IOError as e: | ||
| print(f"Error writing zip file: {e}") | ||
|
|
||
| # Unzipping the file | ||
| # Unzip the file | ||
| try: | ||
| with zipfile.ZipFile(zip_filename, "r") as zip_ref: | ||
| # Extract all files to the current directory | ||
| zip_ref.extractall() | ||
| print("Unzipped files successfully.") | ||
| except zipfile.BadZipFile as e: | ||
|
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. Why is this changed from BadZipFile
Contributor
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. I encountered an error while downloading the ZIP file and wanted to understand the reason, but I forgot to revert the changes. |
||
| print(f"Error unzipping file: {e}") | ||
|
|
||
| annotation_bounds = pd.read_csv( | ||
| "spyogenes/AADGQTVSGGSILYR3_manual_annotations.tsv", sep="\t" | ||
| ) # contain annotations across all runs | ||
| chrom_df = pd.read_csv( | ||
| "spyogenes/chroms_AADGQTVSGGSILYR3.tsv", sep="\t" | ||
| ) # contains chromatogram for precursor across all runs | ||
|
|
||
| ##### Set Plotting Variables ##### | ||
| pd.options.plotting.backend = "ms_matplotlib" | ||
| RUN_NAMES = [ | ||
| "Run #0 Spyogenes 0% human plasma", | ||
| "Run #1 Spyogenes 0% human plasma", | ||
| "Run #2 Spyogenes 0% human plasma", | ||
| "Run #3 Spyogenes 10% human plasma", | ||
| "Run #4 Spyogenes 10% human plasma", | ||
| "Run #5 Spyogenes 10% human plasma", | ||
| ] | ||
|
|
||
| fig, axs = plt.subplots(len(np.unique(chrom_df["run"])), 1, figsize=(10, 15)) | ||
|
|
||
| # plt.close ### required for running in jupyter notebook setting | ||
|
|
||
| # For each run fill in the axs object with the corresponding chromatogram | ||
| plot_list = [] | ||
| for i, run in enumerate(RUN_NAMES): | ||
| run_df = chrom_df[chrom_df["run_name"] == run] | ||
| current_bounds = annotation_bounds[annotation_bounds["run"] == run] | ||
| # Load the data | ||
| annotation_bounds = pd.read_csv("spyogenes/AADGQTVSGGSILYR3_manual_annotations.tsv", sep="\t") | ||
| chrom_df = pd.read_csv("spyogenes/chroms_AADGQTVSGGSILYR3.tsv", sep="\t") | ||
|
|
||
| run_df.plot( | ||
| kind="chromatogram", | ||
| x="rt", | ||
| y="int", | ||
| grid=False, | ||
| by="ion_annotation", | ||
| title=run_df.iloc[0]["run_name"], | ||
| title_font_size=16, | ||
| xaxis_label_font_size=14, | ||
| yaxis_label_font_size=14, | ||
| xaxis_tick_font_size=12, | ||
| yaxis_tick_font_size=12, | ||
| canvas=axs[i], | ||
| relative_intensity=True, | ||
| annotation_data=current_bounds, | ||
| xlabel="Retention Time (sec)", | ||
| ylabel="Relative\nIntensity", | ||
| annotation_legend_config=dict(show=False), | ||
| legend_config={"show": False}, | ||
| ) | ||
| ##### Plotting Using Tile By ##### | ||
| # Instead of pre-creating subplots and looping over RUN_NAMES, | ||
| # we call the plot method once and provide a facet_column parameter. | ||
| fig = chrom_df.plot( | ||
| kind="chromatogram", | ||
| x="rt", | ||
| y="int", | ||
| facet_column="run_name", # Automatically groups data by run_name and creates subplots | ||
| facet_col_wrap=1, # Layout: 1 column (one subplot per row) | ||
| grid=False, | ||
| by="ion_annotation", | ||
| title_font_size=16, | ||
| xaxis_label_font_size=14, | ||
| yaxis_label_font_size=14, | ||
| xaxis_tick_font_size=12, | ||
| yaxis_tick_font_size=12, | ||
| relative_intensity=True, | ||
| annotation_data=annotation_bounds, | ||
| xlabel="Retention Time (sec)", | ||
| ylabel="Relative\nIntensity", | ||
| annotation_legend_config={"show": False}, | ||
| legend_config={"show": False}, | ||
| ) | ||
|
|
||
| fig.tight_layout() | ||
| fig | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add this back