def xic(self, start_time=0, stop_time=99999, start_mz=0, stop_mz=99999, filter='Full ms'): start_scan, stop_scan = list( map(self.scan_from_time, [start_time, stop_time])) #settings = ChromatogramTraceSettings(TraceType.BasePeak) settings = ChromatogramTraceSettings(filter, [Range.Create(start_mz, stop_mz)]) xic_data = self.source.GetChromatogramData([settings], start_scan, stop_scan) xic_trace = ChromatogramSignal.FromChromatogramData(xic_data)[0] return list(zip(xic_trace.Times, xic_trace.Intensities))
def GetChromatogram(self, scanrange=None, trace_number=0): '''Reads the base peak chromatogram for the RAW file. Args: scanrange = [ start scan for the chromatogram, end scan for the chromatogram.] ''' if scanrange is None: scanrange = [self.FirstSpectrumNumber, self.LastSpectrumNumber] # Define the settings for getting the Base Peak chromatogram #TraceType.BasePeak # Define the settings for getting the TIC chromatogram #TraceType.TIC settings = ChromatogramTraceSettings(TraceType.TIC) # Get the chromatogram from the RAW file. data = self.source.GetChromatogramData([settings], int(scanrange[0]), int(scanrange[1])) # Split the data into the chromatograms trace = ChromatogramSignal.FromChromatogramData(data) # Convert to Numpy Array self.ticdat = np.transpose( [DotNetArrayToNPArray(trace[trace_number].Times), DotNetArrayToNPArray(trace[trace_number].Intensities)]) return self.ticdat
def get_ei_chromatogram(IRawDataPlus, target_mz, ppm_tolerance=1000, start_scan=-1, end_scan=-1, ms_type='MS'): '''ms_type: str ('MS', MS2') start_scan: int default -1 will select the lowest available end_scan: int default -1 will select the highest available ''' options = MassOptions() options.ToleranceUnits = ToleranceUnits.ppm options.Tolerance = ppm_tolerance settings = ChromatogramTraceSettings(TraceType.MassRange) settings.Filter = ms_type settings.MassRanges = [Range(target_mz, target_mz)] chroma_settings = IChromatogramSettings(settings) settings2 = ChromatogramTraceSettings(TraceType.MassRange) settings2.Filter = ms_type settings2.MassRanges = [Range(target_mz, target_mz)] settings2.MassRanges = [Range(404.5, 404.5)] chroma_settings2 = IChromatogramSettings(settings2) # chroma_settings2 = IChromatogramSettings(settings) # print(chroma_settings.FragmentMass) # print(chroma_settings.FragmentMass) # print(chroma_settings) # print(chroma_settings) data = IRawDataPlus.GetChromatogramData( [chroma_settings, chroma_settings2], start_scan, end_scan, options) trace = ChromatogramSignal.FromChromatogramData(data) if trace[1].Length > 0: print("Base Peak chromatogram ({} points)".format(trace[0].Length)) rt = [] tic = [] scan = [] for i in range(trace[1].Length): # print(trace[0].HasBasePeakData,trace[0].EndTime ) # print(" {} - {}, {}".format( i, trace[0].Times[i], trace[0].Intensities[i] )) rt.append(trace[1].Times[i]) tic.append(trace[1].Intensities[i]) scan.append(trace[1].Scans[i]) plot_chroma(rt, tic) plt.show() if trace[0].Length > 0: print("Base Peak chromatogram ({} points)".format(trace[0].Length)) rt = [] tic = [] for i in range(trace[0].Length): # print(trace[0].HasBasePeakData,trace[0].EndTime ) # print(" {} - {}, {}".format( i, trace[0].Times[i], trace[0].Intensities[i] )) rt.append(trace[0].Times[i]) tic.append(trace[0].Intensities[i]) plot_chroma(rt, tic) plt.show()
def get_eics(self, tic_data: dict, ms_type='MS !d', peak_detection=True, smooth=True, plot=False, ax=None, legend=False) -> Tuple[Dict[float, EIC_Data], axes.Axes]: '''ms_type: str ('MS', MS2') start_scan: int default -1 will select the lowest available end_scan: int default -1 will select the highest available returns: chroma: dict{target_mz: EIC_Data( Scans: [int] original thermo scan numbers Time: [floats] list of retention times TIC: [floats] total ion chromatogram Apexes: [int] original thermo apex scan number after peak picking ) ''' target_mzs = self._parser.selected_mzs options = MassOptions() options.ToleranceUnits = ToleranceUnits.ppm options.Tolerance = self.chromatogram_settings.eic_tolerance_ppm all_chroma_settings = [] for target_mz in target_mzs: settings = ChromatogramTraceSettings(TraceType.MassRange) settings.Filter = ms_type settings.MassRanges = [Range(target_mz, target_mz)] chroma_settings = IChromatogramSettings(settings) all_chroma_settings.append(chroma_settings) # chroma_settings2 = IChromatogramSettings(settings) # print(chroma_settings.FragmentMass) # print(chroma_settings.FragmentMass) # print(chroma_settings) # print(chroma_settings) data = self._parser.iRawDataPlus.GetChromatogramData(all_chroma_settings, self.chromatogram_settings.start_scan, self.chromatogram_settings.end_scan, options) traces = ChromatogramSignal.FromChromatogramData(data) chroma = {} if plot: from matplotlib.transforms import Bbox import matplotlib.pyplot as plt if not ax: # ax = plt.gca() # ax.clear() fig, ax = plt.subplots() else: fig = plt.gcf() # plt.show() for i, trace in enumerate(traces): if trace.Length > 0: rt, eic, scans = self._parser.get_rt_time_from_trace(trace) if smooth: eic= self.smooth_tic(eic) chroma[target_mzs[i]] = EIC_Data(scans=scans, time=rt, eic= eic) if plot: ax.plot(rt, eic, label="{:.5f}".format(target_mzs[i])) if peak_detection: #max_eic = self.get_max_eic(chroma) max_signal = max(tic_data.tic) for eic_data in chroma.values(): eic = eic_data.eic time = eic_data.time if len(eic) != len(tic_data.tic): logging.warn("The software assumes same lenth of TIC and EIC, this does not seems to be the case and the results mass spectrum selected by the scan number might not be correct") centroid_eics = self.eic_centroid_detector(time, eic, max_signal) eic_data.apexes = [i for i in centroid_eics] if plot: for peak_indexes in eic_data.apexes: apex_index = peak_indexes[1] ax.plot(time[apex_index], eic[apex_index], marker='x', linewidth=0) if plot: ax.set_xlabel('Time (min)') ax.set_ylabel('a.u.') ax.set_title(ms_type + ' EIC') ax.tick_params(axis='both', which='major', labelsize=12) ax.axes.spines['top'].set_visible(False) ax.axes.spines['right'].set_visible(False) if legend: legend = ax.legend(loc="upper left", bbox_to_anchor=(1.02, 0, 0.07, 1)) fig.subplots_adjust(right=0.76) #ax.set_prop_cycle(color=plt.cm.gist_rainbow(np.linspace(0, 1, len(traces)))) d = {"down": 30, "up": -30} def func(evt): if legend.contains(evt): bbox = legend.get_bbox_to_anchor() bbox = Bbox.from_bounds(bbox.x0, bbox.y0 + d[evt.button], bbox.width, bbox.height) tr = legend.axes.transAxes.inverted() legend.set_bbox_to_anchor(bbox.transformed(tr)) fig.canvas.draw_idle() fig.canvas.mpl_connect("scroll_event", func) return chroma, ax else: return chroma, None rt = [] tic = [] scans = [] for i in range(traces[0].Length): # print(trace[0].HasBasePeakData,trace[0].EndTime ) # print(" {} - {}, {}".format( i, trace[0].Times[i], trace[0].Intensities[i] )) rt.append(traces[0].Times[i]) tic.append(traces[0].Intensities[i]) scans.append(traces[0].Scans[i]) return traces
def get_tic(self, ms_type='MS !d', peak_detection=True, smooth=True, plot=False, ax=None) -> Tuple[ TIC_Data, axes.Axes]: '''ms_type: str ('MS', MS2') start_scan: int default -1 will select the lowest available end_scan: int default -1 will select the highest available returns: chroma: dict { Scan: [int] original thermo scan numberMS Time: [floats] list of retention times TIC: [floats] total ion chromatogram Apexes: [int] original thermo apex scan number after peak picking } ''' settings = ChromatogramTraceSettings(TraceType.TIC) settings.Filter = ms_type chroma_settings = IChromatogramSettings(settings) data = self._parser.iRawDataPlus.GetChromatogramData([chroma_settings], self.parameters.lc_ms.start_scan, self.parameters.lc_ms.end_scan) trace = ChromatogramSignal.FromChromatogramData(data) data = TIC_Data(time= [], scans= [], tic= [], apexes= []) if trace[0].Length > 0: for i in range(trace[0].Length): # print(trace[0].HasBasePeakData,trace[0].EndTime ) # print(" {} - {}, {}".format( i, trace[0].Times[i], trace[0].Intensities[i] )) data.time.append(trace[0].Times[i]) data.tic.append(trace[0].Intensities[i]) data.scans.append(trace[0].Scans[i]) #print(trace[0].Scans[i]) if smooth: data.tic= self.smooth_tic(data.tic) else: data.tic= np.array(data.tic) if peak_detection: centroid_peak_indexes = [i for i in self.centroid_detector(data.time, data.tic)] data.apexes = centroid_peak_indexes if plot: if not ax: import matplotlib.pyplot as plt ax = plt.gca() # fig, ax = plt.subplots(figsize=(6, 3)) ax.plot(data.time, data.tic, label=' TIC') ax.set_xlabel('Time (min)') ax.set_ylabel('a.u.') if peak_detection: for peak_indexes in data.apexes: apex_index = peak_indexes[1] ax.plot(data.time[apex_index], data.tic[apex_index], marker='x', linewidth=0) # plt.show() return data, ax return data, None else: return None, None