@@ -64,85 +64,90 @@ def get_df(file: Union[str, Path]) -> pd.DataFrame:
6464
6565
6666def plot_bpc_tic () -> go .Figure :
67- """Plot the base peak and total ion chromatogram (TIC).
67+ """Plot TIC, BPC, and (optionally) XIC, overlaying the chromatograms
68+ using the ms_plotly backend.
6869
6970 Returns:
70- A plotly Figure object containing the BPC and TIC plot .
71+ A plotly Figure object containing the overlaid chromatogram(s) .
7172 """
72- fig = go .Figure ()
73+ # List to store DataFrames for each chromatogram, so we can concatenate them later.
74+ # This is necessary because we need to plot them all together in one figure.
75+ dfs = []
7376 max_int = 0
77+
7478 if st .session_state .view_tic :
75- df = st .session_state .view_ms1 .groupby ("RT" ).sum ().reset_index ()
76- df ["type" ] = "TIC"
77- if df ["inty" ].max () > max_int :
78- max_int = df ["inty" ].max ()
79- fig = df .plot (
80- backend = "ms_plotly" ,
81- kind = "chromatogram" ,
82- x = "RT" ,
83- y = "inty" ,
84- by = "type" ,
85- color = "#f24c5c" ,
86- show_plot = False ,
87- grid = False ,
88- aggregate_duplicates = True ,
89- )
79+ df_tic = st .session_state .view_ms1 .groupby ("RT" ).sum ().reset_index ()
80+ df_tic ["type" ] = "TIC"
81+ if df_tic ["inty" ].max () > max_int :
82+ max_int = df_tic ["inty" ].max ()
83+ dfs .append (df_tic )
84+
9085 if st .session_state .view_bpc :
91- df = st .session_state .view_ms1 .groupby ("RT" ).max ().reset_index ()
92- df ["type" ] = "BPC"
93- if df ["inty" ].max () > max_int :
94- max_int = df ["inty" ].max ()
95- fig = df .plot (
96- backend = "ms_plotly" ,
97- kind = "chromatogram" ,
98- x = "RT" ,
99- y = "inty" ,
100- by = "type" ,
101- color = "#2d3a9d" ,
102- show_plot = False ,
103- grid = False ,
104- aggregate_duplicates = True ,
105- )
86+ df_bpc = st .session_state .view_ms1 .groupby ("RT" ).max ().reset_index ()
87+ df_bpc ["type" ] = "BPC"
88+ if df_bpc ["inty" ].max () > max_int :
89+ max_int = df_bpc ["inty" ].max ()
90+ dfs .append (df_bpc )
91+
10692 if st .session_state .view_eic :
10793 df = st .session_state .view_ms1
108- target_value = st .session_state .view_eic_mz .strip ().replace ("," , "." )
94+ target_value_str = st .session_state .view_eic_mz .strip ().replace ("," , "." )
10995 try :
110- target_value = float (target_value )
96+ target_value = float (target_value_str )
11197 ppm_tolerance = st .session_state .view_eic_ppm
11298 tolerance = (target_value * ppm_tolerance ) / 1e6
11399
114- # Filter the DataFrame
100+ # Filter for m/z values within tolerance
115101 df_eic = df [
116- (df ["mz" ] >= target_value - tolerance )
117- & (df ["mz" ] <= target_value + tolerance )
102+ (df ["mz" ] >= target_value - tolerance ) &
103+ (df ["mz" ] <= target_value + tolerance )
118104 ].copy ()
105+
119106 if not df_eic .empty :
120- df_eic .loc [:, "type" ] = "XIC"
121- if df_eic ["inty" ].max () > max_int :
122- max_int = df_eic ["inty" ].max ()
123- fig = df_eic .plot (
124- backend = "ms_plotly" ,
125- kind = "chromatogram" ,
126- x = "RT" ,
127- y = "inty" ,
128- by = "type" ,
129- color = "#f6bf26" ,
130- show_plot = False ,
131- grid = False ,
132- aggregate_duplicates = True ,
133- )
107+ # Aggregate duplicate RT entries (summing intensities)
108+ df_eic_agg = df_eic .groupby ("RT" , as_index = False )["inty" ].sum ()
109+ df_eic_agg ["type" ] = "XIC"
110+ if df_eic_agg ["inty" ].max () > max_int :
111+ max_int = df_eic_agg ["inty" ].max ()
112+ dfs .append (df_eic_agg )
134113 except ValueError :
135114 st .error ("Invalid m/z value for XIC provided. Please enter a valid number." )
136115
116+ if not dfs :
117+ st .error ("No chromatogram data to plot!" )
118+ return go .Figure ()
119+
120+ # Concatenate all chromatogram DataFrames
121+ df_combined = pd .concat (dfs , ignore_index = True )
122+
123+ df_combined ["type" ] = pd .Categorical (df_combined ["type" ], categories = ["TIC" , "BPC" , "XIC" ], ordered = True )
124+ df_combined = df_combined .sort_values ("type" )
125+
126+ colors_map = {"TIC" : "#f24c5c" , "BPC" : "#2d3a9d" , "XIC" : "#f6bf26" }
127+ color_list = [colors_map [t ] for t in ["TIC" , "BPC" , "XIC" ]]
128+
129+ fig = df_combined .plot (
130+ backend = "ms_plotly" ,
131+ kind = "chromatogram" ,
132+ x = "RT" ,
133+ y = "inty" ,
134+ by = "type" ,
135+ color = iter (color_list ),
136+ show_plot = False ,
137+ grid = False ,
138+ aggregate_duplicates = True ,
139+ )
140+
137141 fig .update_yaxes (range = [0 , max_int ])
138142 fig .update_layout (
139143 title = f"{ st .session_state .view_selected_file } " ,
140144 xaxis_title = "retention time (s)" ,
141145 yaxis_title = "intensity" ,
142146 plot_bgcolor = "rgb(255,255,255)" ,
143147 height = 500 ,
148+ template = "plotly_white" ,
144149 )
145- fig . layout . template = "plotly_white"
150+
146151 return fig
147152
148153
0 commit comments