public void TestFilterData() { const int MAX_ALLOWABLE_ION_COUNT = 2000; var options = new clsMASICOptions(string.Empty, string.Empty); var peakFinder = new MASICPeakFinder.clsMASICPeakFinder(); var reporterIons = new clsReporterIons(); var parentIonProcessor = new clsParentIonProcessing(reporterIons); var scanTracking = new clsScanTracking(reporterIons, peakFinder); var dataImportUtilities = new MASIC.DataInput.clsDataImportThermoRaw(options, peakFinder, parentIonProcessor, scanTracking); var mReporterIons = new clsReporterIons(); var msSpectrum = new clsMSSpectrum(1000); for (var mz = 500.0; mz < 1000; mz += 0.1) { msSpectrum.IonsMZ.Add(mz); msSpectrum.IonsIntensity.Add(100 + Math.Sin(mz / 10) * 50 - Math.Abs(750 - mz) / 10); } dataImportUtilities.DiscardDataToLimitIonCount(msSpectrum, mReporterIons.MZIntensityFilterIgnoreRangeStart, mReporterIons.MZIntensityFilterIgnoreRangeEnd, MAX_ALLOWABLE_ION_COUNT); var pointsToCheck = new Dictionary <int, KeyValuePair <double, double> > { { 0, new KeyValuePair <double, double>(508.500, 103.43985) }, { 10, new KeyValuePair <double, double>(509.500, 107.56497) }, { 250, new KeyValuePair <double, double>(574.500, 121.65772) }, { 500, new KeyValuePair <double, double>(637.800, 129.39681) }, { 750, new KeyValuePair <double, double>(698.500, 128.37793) }, { 1000, new KeyValuePair <double, double>(756.800, 113.22312) }, { 1500, new KeyValuePair <double, double>(841.500, 122.01680) }, { 1999, new KeyValuePair <double, double>(968.600, 103.38958) } }; foreach (var item in pointsToCheck) { var index = item.Key; Console.WriteLine( "Data point {0,4}: {1:F4} m/z, {2:F4} counts", item.Key, msSpectrum.IonsMZ[index], msSpectrum.IonsIntensity[index]); Assert.AreEqual(item.Value.Key, msSpectrum.IonsMZ[index], 0.00001); Assert.AreEqual(item.Value.Value, msSpectrum.IonsIntensity[index], 0.00001); } }
public bool ExtractScanInfoFromMGFandCDF( string filePath, clsScanList scanList, clsSpectraCache spectraCache, DataOutput.clsDataOutput dataOutputHandler, bool keepRawSpectra, bool keepMSMSSpectra) { // Returns True if Success, False if failure // Note: This function assumes filePath exists // // This function can be used to read a pair of MGF and NetCDF files that contain MS/MS and MS-only parent ion scans, respectively // Typically, this will apply to LC-MS/MS analyses acquired using an Agilent mass spectrometer running DataAnalysis software // filePath can contain the path to the MGF or to the CDF file; the extension will be removed in order to determine the base file name, // then the two files will be looked for separately var scanTime = 0.0; var cdfReader = new NetCDFReader.clsMSNetCdf(); var mgfReader = new MSDataFileReader.clsMGFFileReader(); try { Console.Write("Reading CDF/MGF data files "); ReportMessage("Reading CDF/MGF data files"); UpdateProgress(0, "Opening data file: " + Environment.NewLine + Path.GetFileName(filePath)); // Obtain the full path to the file var mgfFileInfo = new FileInfo(filePath); var mgfInputFilePathFull = mgfFileInfo.FullName; // Make sure the extension for mgfInputFilePathFull is .MGF mgfInputFilePathFull = Path.ChangeExtension(mgfInputFilePathFull, AGILENT_MSMS_FILE_EXTENSION); var cdfInputFilePathFull = Path.ChangeExtension(mgfInputFilePathFull, AGILENT_MS_FILE_EXTENSION); var datasetID = mOptions.SICOptions.DatasetID; var sicOptions = mOptions.SICOptions; var success = UpdateDatasetFileStats(mgfFileInfo, datasetID); mDatasetFileInfo.ScanCount = 0; // Open a handle to each data file if (!cdfReader.OpenMSCdfFile(cdfInputFilePathFull)) { ReportError("Error opening input data file: " + cdfInputFilePathFull); SetLocalErrorCode(clsMASIC.eMasicErrorCodes.InputFileAccessError); return(false); } if (!mgfReader.OpenFile(mgfInputFilePathFull)) { ReportError("Error opening input data file: " + mgfInputFilePathFull); SetLocalErrorCode(clsMASIC.eMasicErrorCodes.InputFileAccessError); return(false); } var msScanCount = cdfReader.GetScanCount(); mDatasetFileInfo.ScanCount = msScanCount; if (msScanCount <= 0) { // No scans found ReportError("No scans found in the input file: " + cdfInputFilePathFull); SetLocalErrorCode(clsMASIC.eMasicErrorCodes.InputFileAccessError); return(false); } // Reserve memory for all of the Survey Scan data scanList.Initialize(); var scanCount = mOptions.SICOptions.ScanRangeCount; if (scanCount <= 0) { scanCount = msScanCount * 3; } scanList.ReserveListCapacity(scanCount, Math.Min(msScanCount, scanCount / 3)); // Assume there are 2 frag scans for every survey scan mScanTracking.ReserveListCapacity(scanCount); spectraCache.SpectrumCount = Math.Max(spectraCache.SpectrumCount, scanCount); UpdateProgress("Reading CDF/MGF data (" + msScanCount.ToString() + " scans)" + Environment.NewLine + Path.GetFileName(filePath)); ReportMessage("Reading CDF/MGF data; Total MS scan count: " + msScanCount.ToString()); // Read all of the Survey scans from the CDF file // CDF files created by the Agilent XCT list the first scan number as 0; use scanNumberCorrection to correct for this var scanNumberCorrection = 0; for (var msScanIndex = 0; msScanIndex < msScanCount; msScanIndex++) { success = cdfReader.GetScanInfo(msScanIndex, out var scanNumber, out var scanTotalIntensity, out scanTime, out _, out _); if (msScanIndex == 0 && scanNumber == 0) { scanNumberCorrection = 1; } if (!success) { // Error reading CDF file ReportError("Error obtaining data from CDF file: " + cdfInputFilePathFull); SetLocalErrorCode(clsMASIC.eMasicErrorCodes.InputFileDataReadError); return(false); } if (scanNumberCorrection > 0) { scanNumber += scanNumberCorrection; } if (mScanTracking.CheckScanInRange(scanNumber, scanTime, sicOptions)) { var newSurveyScan = new clsScanInfo { ScanNumber = scanNumber, TotalIonIntensity = (float)scanTotalIntensity, // Copy the Total Scan Intensity to .TotalIonIntensity ScanHeaderText = string.Empty, ScanTypeName = "MS", }; if (mOptions.CDFTimeInSeconds) { newSurveyScan.ScanTime = (float)(scanTime / 60); } else { newSurveyScan.ScanTime = (float)scanTime; } // Survey scans typically lead to multiple parent ions; we do not record them here newSurveyScan.FragScanInfo.ParentIonInfoIndex = -1; scanList.SurveyScans.Add(newSurveyScan); success = cdfReader.GetMassSpectrum(msScanIndex, out var mzData, out var intensityData, out var intIonCount, out _); if (success && intIonCount > 0) { var msSpectrum = new clsMSSpectrum(scanNumber, mzData, intensityData, intIonCount); double msDataResolution; newSurveyScan.IonCount = msSpectrum.IonCount; newSurveyScan.IonCountRaw = newSurveyScan.IonCount; // Find the base peak ion mass and intensity newSurveyScan.BasePeakIonMZ = FindBasePeakIon(msSpectrum.IonsMZ, msSpectrum.IonsIntensity, out var basePeakIonIntensity, out var mzMin, out var mzMax); newSurveyScan.BasePeakIonIntensity = basePeakIonIntensity; // Determine the minimum positive intensity in this scan newSurveyScan.MinimumPositiveIntensity = mPeakFinder.FindMinimumPositiveValue(msSpectrum.IonsIntensity, 0); if (sicOptions.SICToleranceIsPPM) { // Define MSDataResolution based on the tolerance value that will be used at the lowest m/z in this spectrum, divided by COMPRESS_TOLERANCE_DIVISOR // However, if the lowest m/z value is < 100, then use 100 m/z if (mzMin < 100) { msDataResolution = clsParentIonProcessing.GetParentIonToleranceDa(sicOptions, 100) / sicOptions.CompressToleranceDivisorForPPM; } else { msDataResolution = clsParentIonProcessing.GetParentIonToleranceDa(sicOptions, mzMin) / sicOptions.CompressToleranceDivisorForPPM; } } else { msDataResolution = sicOptions.SICTolerance / sicOptions.CompressToleranceDivisorForDa; } mScanTracking.ProcessAndStoreSpectrum( newSurveyScan, this, spectraCache, msSpectrum, sicOptions.SICPeakFinderOptions.MassSpectraNoiseThresholdOptions, clsMASIC.DISCARD_LOW_INTENSITY_MS_DATA_ON_LOAD, sicOptions.CompressMSSpectraData, msDataResolution, keepRawSpectra); } else { newSurveyScan.IonCount = 0; newSurveyScan.IonCountRaw = 0; } // Note: Since we're reading all of the Survey Scan data, we cannot update .MasterScanOrder() at this time } // Note: We need to take msScanCount * 2 since we have to read two different files if (msScanCount > 1) { UpdateProgress((short)(msScanIndex / (double)(msScanCount * 2 - 1) * 100)); } else { UpdateProgress(0); } UpdateCacheStats(spectraCache); if (mOptions.AbortProcessing) { scanList.ProcessingIncomplete = true; break; } if (msScanIndex % 100 == 0) { ReportMessage("Reading MS scan index: " + msScanIndex.ToString()); Console.Write("."); } } // Record the current memory usage (before we close the .CDF file) OnUpdateMemoryUsage(); cdfReader.CloseMSCdfFile(); // We loaded all of the survey scan data above // We can now initialize .MasterScanOrder() var lastSurveyScanIndex = 0; scanList.AddMasterScanEntry(clsScanList.eScanTypeConstants.SurveyScan, lastSurveyScanIndex); var surveyScansRecorded = new SortedSet <int>() { lastSurveyScanIndex }; // Reset scanNumberCorrection; we might also apply it to MS/MS data scanNumberCorrection = 0; // Now read the MS/MS data from the MGF file do { var fragScanFound = mgfReader.ReadNextSpectrum(out var spectrumInfo); if (!fragScanFound) { break; } mDatasetFileInfo.ScanCount += 1; while (spectrumInfo.ScanNumber < scanList.SurveyScans[lastSurveyScanIndex].ScanNumber) { // The scan number for the current MS/MS spectrum is less than the last survey scan index scan number // This can happen, due to oddities with combining scans when creating the .MGF file // Need to decrement lastSurveyScanIndex until we find the appropriate survey scan lastSurveyScanIndex -= 1; if (lastSurveyScanIndex == 0) { break; } } if (scanNumberCorrection == 0) { // See if udtSpectrumHeaderInfo.ScanNumberStart is equivalent to one of the survey scan numbers, yielding conflicting scan numbers // If it is, then there is an indexing error in the .MGF file; this error was present in .MGF files generated with // an older version of Agilent Chemstation. These files typically have lines like ###MSMS: #13-29 instead of ###MSMS: #13/29/ // If this indexing error is found, then we'll set scanNumberCorrection = 1 and apply it to all subsequent MS/MS scans; // we'll also need to correct prior MS/MS scans for (var surveyScanIndex = lastSurveyScanIndex; surveyScanIndex < scanList.SurveyScans.Count; surveyScanIndex++) { if (scanList.SurveyScans[surveyScanIndex].ScanNumber == spectrumInfo.ScanNumber) { // Conflicting scan numbers were found scanNumberCorrection = 1; // Need to update prior MS/MS scans foreach (var fragScan in scanList.FragScans) { fragScan.ScanNumber += scanNumberCorrection; var scanTimeInterpolated = InterpolateRTandFragScanNumber( scanList.SurveyScans, 0, fragScan.ScanNumber, out var fragScanIterationOut); fragScan.FragScanInfo.FragScanNumber = fragScanIterationOut; fragScan.ScanTime = scanTimeInterpolated; } break; } if (scanList.SurveyScans[surveyScanIndex].ScanNumber > spectrumInfo.ScanNumber) { break; } } } if (scanNumberCorrection > 0) { spectrumInfo.ScanNumber += scanNumberCorrection; spectrumInfo.ScanNumberEnd += scanNumberCorrection; } scanTime = InterpolateRTandFragScanNumber( scanList.SurveyScans, lastSurveyScanIndex, spectrumInfo.ScanNumber, out var fragScanIteration); // Make sure this fragmentation scan isn't present yet in scanList.FragScans // This can occur in Agilent .MGF files if the scan is listed both singly and grouped with other MS/MS scans var validFragScan = true; foreach (var fragScan in scanList.FragScans) { if (fragScan.ScanNumber == spectrumInfo.ScanNumber) { // Duplicate found validFragScan = false; break; } } if (!(validFragScan && mScanTracking.CheckScanInRange(spectrumInfo.ScanNumber, scanTime, sicOptions))) { continue; } // See if lastSurveyScanIndex needs to be updated // At the same time, populate .MasterScanOrder while (lastSurveyScanIndex < scanList.SurveyScans.Count - 1 && spectrumInfo.ScanNumber > scanList.SurveyScans[lastSurveyScanIndex + 1].ScanNumber) { lastSurveyScanIndex += 1; // Add the given SurveyScan to .MasterScanOrder, though only if it hasn't yet been added if (!surveyScansRecorded.Contains(lastSurveyScanIndex)) { surveyScansRecorded.Add(lastSurveyScanIndex); scanList.AddMasterScanEntry(clsScanList.eScanTypeConstants.SurveyScan, lastSurveyScanIndex); } } scanList.AddMasterScanEntry(clsScanList.eScanTypeConstants.FragScan, scanList.FragScans.Count, spectrumInfo.ScanNumber, (float)scanTime); var newFragScan = new clsScanInfo { ScanNumber = spectrumInfo.ScanNumber, ScanTime = (float)scanTime, ScanHeaderText = string.Empty, ScanTypeName = "MSn", }; newFragScan.FragScanInfo.FragScanNumber = fragScanIteration; newFragScan.FragScanInfo.MSLevel = 2; newFragScan.MRMScanInfo.MRMMassCount = 0; scanList.FragScans.Add(newFragScan); var msSpectrum = new clsMSSpectrum(newFragScan.ScanNumber, spectrumInfo.MZList, spectrumInfo.IntensityList, spectrumInfo.DataCount); if (spectrumInfo.DataCount > 0) { newFragScan.IonCount = msSpectrum.IonCount; newFragScan.IonCountRaw = newFragScan.IonCount; // Find the base peak ion mass and intensity newFragScan.BasePeakIonMZ = FindBasePeakIon(msSpectrum.IonsMZ, msSpectrum.IonsIntensity, out var basePeakIonIntensity, out _, out _); newFragScan.BasePeakIonIntensity = basePeakIonIntensity; // Compute the total scan intensity newFragScan.TotalIonIntensity = 0; for (var ionIndex = 0; ionIndex < newFragScan.IonCount; ionIndex++) { newFragScan.TotalIonIntensity += msSpectrum.IonsIntensity[ionIndex]; } // Determine the minimum positive intensity in this scan newFragScan.MinimumPositiveIntensity = mPeakFinder.FindMinimumPositiveValue(msSpectrum.IonsIntensity, 0); var msDataResolution = mOptions.BinningOptions.BinSize / sicOptions.CompressToleranceDivisorForDa; var keepRawSpectrum = keepRawSpectra && keepMSMSSpectra; mScanTracking.ProcessAndStoreSpectrum( newFragScan, this, spectraCache, msSpectrum, sicOptions.SICPeakFinderOptions.MassSpectraNoiseThresholdOptions, clsMASIC.DISCARD_LOW_INTENSITY_MSMS_DATA_ON_LOAD, sicOptions.CompressMSMSSpectraData, msDataResolution, keepRawSpectrum); } else { newFragScan.IonCount = 0; newFragScan.IonCountRaw = 0; newFragScan.TotalIonIntensity = 0; } mParentIonProcessor.AddUpdateParentIons(scanList, lastSurveyScanIndex, spectrumInfo.ParentIonMZ, scanList.FragScans.Count - 1, spectraCache, sicOptions); // Note: We need to take msScanCount * 2, in addition to adding msScanCount to lastSurveyScanIndex, since we have to read two different files if (msScanCount > 1) { UpdateProgress((short)((lastSurveyScanIndex + msScanCount) / (double)(msScanCount * 2 - 1) * 100)); } else { UpdateProgress(0); } UpdateCacheStats(spectraCache); if (mOptions.AbortProcessing) { scanList.ProcessingIncomplete = true; break; } if (scanList.FragScans.Count % 100 == 0) { ReportMessage("Reading MSMS scan index: " + scanList.FragScans.Count); Console.Write("."); } }while (true); // Record the current memory usage (before we close the .MGF file) OnUpdateMemoryUsage(); mgfReader.CloseFile(); // Check for any other survey scans that need to be added to MasterScanOrder // See if lastSurveyScanIndex needs to be updated // At the same time, populate .MasterScanOrder while (lastSurveyScanIndex < scanList.SurveyScans.Count - 1) { lastSurveyScanIndex += 1; // Note that scanTime is the scan time of the most recent survey scan processed in the above Do loop, so it's not accurate if (mScanTracking.CheckScanInRange(scanList.SurveyScans[lastSurveyScanIndex].ScanNumber, scanTime, sicOptions)) { // Add the given SurveyScan to .MasterScanOrder, though only if it hasn't yet been added if (!surveyScansRecorded.Contains(lastSurveyScanIndex)) { surveyScansRecorded.Add(lastSurveyScanIndex); scanList.AddMasterScanEntry(clsScanList.eScanTypeConstants.SurveyScan, lastSurveyScanIndex); } } } // Make sure that MasterScanOrder really is sorted by scan number ValidateMasterScanOrderSorting(scanList); // Now that all of the data has been read, write out to the scan stats file, in order of scan number for (var scanIndex = 0; scanIndex < scanList.MasterScanOrderCount; scanIndex++) { var eScanType = scanList.MasterScanOrder[scanIndex].ScanType; clsScanInfo currentScan; if (eScanType == clsScanList.eScanTypeConstants.SurveyScan) { // Survey scan currentScan = scanList.SurveyScans[scanList.MasterScanOrder[scanIndex].ScanIndexPointer]; } else { // Frag Scan currentScan = scanList.FragScans[scanList.MasterScanOrder[scanIndex].ScanIndexPointer]; } SaveScanStatEntry(dataOutputHandler.OutputFileHandles.ScanStats, eScanType, currentScan, sicOptions.DatasetID); } Console.WriteLine(); scanList.SetListCapacityToCount(); mScanTracking.SetListCapacityToCount(); return(success); } catch (Exception ex) { ReportError("Error in ExtractScanInfoFromMGFandCDF", ex, clsMASIC.eMasicErrorCodes.InputFileDataReadError); return(false); } }
public void DiscardDataToLimitIonCount( clsMSSpectrum msSpectrum, double mzIgnoreRangeStart, double mzIgnoreRangeEnd, int maxIonCountToRetain) { // When this is true, will write a text file of the mass spectrum before and after it is filtered // Enable this for debugging var writeDebugData = false; StreamWriter writer = null; try { int ionCountNew; if (msSpectrum.IonCount > maxIonCountToRetain) { var filterDataArray = new clsFilterDataArrayMaxCount { MaximumDataCountToKeep = maxIonCountToRetain, TotalIntensityPercentageFilterEnabled = false }; writeDebugData = false; if (writeDebugData) { writer = new StreamWriter(new FileStream(Path.Combine(mOptions.OutputDirectoryPath, "DataDump_" + msSpectrum.ScanNumber.ToString() + "_BeforeFilter.txt"), FileMode.Create, FileAccess.Write, FileShare.Read)); writer.WriteLine("m/z" + "\t" + "Intensity"); } // Store the intensity values in filterDataArray for (var ionIndex = 0; ionIndex < msSpectrum.IonCount; ionIndex++) { filterDataArray.AddDataPoint(msSpectrum.IonsIntensity[ionIndex], ionIndex); if (writeDebugData) { writer.WriteLine(msSpectrum.IonsMZ[ionIndex].ToString() + "\t" + msSpectrum.IonsIntensity[ionIndex]); } } if (writeDebugData) { writer.Close(); } // Call .FilterData, which will determine which data points to keep filterDataArray.FilterData(); ionCountNew = 0; for (var ionIndex = 0; ionIndex < msSpectrum.IonCount; ionIndex++) { // Always keep points in the m/z ignore range // If CheckPointInMZIgnoreRange returns true, set pointPassesFilter to true var pointPassesFilter = clsUtilities.CheckPointInMZIgnoreRange(msSpectrum.IonsMZ[ionIndex], mzIgnoreRangeStart, mzIgnoreRangeEnd); if (!pointPassesFilter) { // See if the point's intensity is negative if (filterDataArray.GetAbundanceByIndex(ionIndex) >= 0) { pointPassesFilter = true; } } if (pointPassesFilter) { msSpectrum.IonsMZ[ionCountNew] = msSpectrum.IonsMZ[ionIndex]; msSpectrum.IonsIntensity[ionCountNew] = msSpectrum.IonsIntensity[ionIndex]; ionCountNew += 1; } } } else { ionCountNew = msSpectrum.IonCount; } if (ionCountNew < msSpectrum.IonCount) { msSpectrum.ShrinkArrays(ionCountNew); } if (writeDebugData) { using (var postFilterWriter = new StreamWriter(new FileStream(Path.Combine(mOptions.OutputDirectoryPath, "DataDump_" + msSpectrum.ScanNumber.ToString() + "_PostFilter.txt"), FileMode.Create, FileAccess.Write, FileShare.Read))) { postFilterWriter.WriteLine("m/z" + "\t" + "Intensity"); // Store the intensity values in filterDataArray for (var ionIndex = 0; ionIndex < msSpectrum.IonCount; ionIndex++) { postFilterWriter.WriteLine(msSpectrum.IonsMZ[ionIndex].ToString() + "\t" + msSpectrum.IonsIntensity[ionIndex]); } } } } catch (Exception ex) { ReportError("Error limiting the number of data points to " + maxIonCountToRetain, ex, clsMASIC.eMasicErrorCodes.UnspecifiedError); } }
private bool LoadSpectraForThermoRawFile( XRawFileIO xcaliburAccessor, clsSpectraCache spectraCache, clsScanInfo scanInfo, clsBaselineNoiseOptions noiseThresholdOptions, bool discardLowIntensityData, bool compressSpectraData, double msDataResolution, bool keepRawSpectrum) { var lastKnownLocation = "Start"; try { // Load the ions for this scan lastKnownLocation = "xcaliburAccessor.GetScanData for scan " + scanInfo.ScanNumber; // Retrieve the m/z and intensity values for the given scan // We retrieve the profile-mode data, since that's required for determining spectrum noise scanInfo.IonCountRaw = xcaliburAccessor.GetScanData(scanInfo.ScanNumber, out var mzList, out var intensityList); if (scanInfo.IonCountRaw > 0) { var ionCountVerified = VerifyDataSorted(scanInfo.ScanNumber, scanInfo.IonCountRaw, mzList, intensityList); if (ionCountVerified != scanInfo.IonCountRaw) { scanInfo.IonCountRaw = ionCountVerified; } } scanInfo.IonCount = scanInfo.IonCountRaw; lastKnownLocation = "Instantiate new clsMSSpectrum"; var msSpectrum = new clsMSSpectrum(scanInfo.ScanNumber, mzList, intensityList, scanInfo.IonCountRaw); lastKnownLocation = "Manually determine the base peak m/z and base peak intensity"; // ReSharper disable once CommentTypo // Regarding BPI, comparison of data read via the ThermoRawFileReader vs. // that read from the .mzML file for dataset QC_Shew_18_02-run1_02Mar19_Arwen_18-11-02 // showed that 25% of the spectra had incorrect BPI values double totalIonIntensity = 0; double basePeakIntensity = 0; double basePeakMz = 0; for (var ionIndex = 0; ionIndex < scanInfo.IonCountRaw; ionIndex++) { totalIonIntensity += intensityList[ionIndex]; if (intensityList[ionIndex] > basePeakIntensity) { basePeakIntensity = intensityList[ionIndex]; basePeakMz = mzList[ionIndex]; } } if (Math.Abs(scanInfo.BasePeakIonMZ - basePeakMz) > 0.1) { mBpiUpdateCount += 1; if (mBpiUpdateCount < 10) { ConsoleMsgUtils.ShowDebug("Updating BPI in scan {0} from {1:F3} m/z to {2:F3} m/z, and BPI Intensity from {3:F0} to {4:F0}", scanInfo.ScanNumber, scanInfo.BasePeakIonMZ, basePeakMz, scanInfo.BasePeakIonIntensity, basePeakIntensity); } scanInfo.BasePeakIonMZ = basePeakMz; scanInfo.BasePeakIonIntensity = basePeakIntensity; } // Determine the minimum positive intensity in this scan lastKnownLocation = "Call mMASICPeakFinder.FindMinimumPositiveValue"; scanInfo.MinimumPositiveIntensity = mPeakFinder.FindMinimumPositiveValue(msSpectrum.IonsIntensity, 0); if (msSpectrum.IonCount > 0) { if (scanInfo.TotalIonIntensity < float.Epsilon) { scanInfo.TotalIonIntensity = totalIonIntensity; } } else { scanInfo.TotalIonIntensity = 0; } bool discardLowIntensityDataWork; bool compressSpectraDataWork; if (scanInfo.MRMScanType == MRMScanTypeConstants.NotMRM) { discardLowIntensityDataWork = discardLowIntensityData; compressSpectraDataWork = compressSpectraData; } else { discardLowIntensityDataWork = false; compressSpectraDataWork = false; } lastKnownLocation = "Call ProcessAndStoreSpectrum"; mScanTracking.ProcessAndStoreSpectrum( scanInfo, this, spectraCache, msSpectrum, noiseThresholdOptions, discardLowIntensityDataWork, compressSpectraDataWork, msDataResolution, keepRawSpectrum); } catch (Exception ex) { ReportError("Error in LoadSpectraForThermoRawFile (LastKnownLocation: " + lastKnownLocation + ")", ex, clsMASIC.eMasicErrorCodes.InputFileDataReadError); return(false); } return(true); }
/// <summary> /// Discard data below the noise threshold /// </summary> /// <param name="msSpectrum"></param> /// <param name="noiseThresholdIntensity"></param> /// <param name="mzIgnoreRangeStart"></param> /// <param name="mzIgnoreRangeEnd"></param> /// <param name="noiseThresholdOptions"></param> public void DiscardDataBelowNoiseThreshold( clsMSSpectrum msSpectrum, double noiseThresholdIntensity, double mzIgnoreRangeStart, double mzIgnoreRangeEnd, MASICPeakFinder.clsBaselineNoiseOptions noiseThresholdOptions) { var ionCountNew = 0; try { switch (noiseThresholdOptions.BaselineNoiseMode) { case MASICPeakFinder.clsMASICPeakFinder.eNoiseThresholdModes.AbsoluteThreshold: if (noiseThresholdOptions.BaselineNoiseLevelAbsolute > 0) { ionCountNew = 0; for (var ionIndex = 0; ionIndex < msSpectrum.IonCount; ionIndex++) { // Always keep points in the m/z ignore range // If CheckPointInMZIgnoreRange returns true, set pointPassesFilter to true var pointPassesFilter = clsUtilities.CheckPointInMZIgnoreRange(msSpectrum.IonsMZ[ionIndex], mzIgnoreRangeStart, mzIgnoreRangeEnd); if (!pointPassesFilter) { // Check the point's intensity against .BaselineNoiseLevelAbsolute if (msSpectrum.IonsIntensity[ionIndex] >= noiseThresholdOptions.BaselineNoiseLevelAbsolute) { pointPassesFilter = true; } } if (pointPassesFilter) { msSpectrum.IonsMZ[ionCountNew] = msSpectrum.IonsMZ[ionIndex]; msSpectrum.IonsIntensity[ionCountNew] = msSpectrum.IonsIntensity[ionIndex]; ionCountNew += 1; } } } else { ionCountNew = msSpectrum.IonCount; } break; case MASICPeakFinder.clsMASICPeakFinder.eNoiseThresholdModes.TrimmedMeanByAbundance: case MASICPeakFinder.clsMASICPeakFinder.eNoiseThresholdModes.TrimmedMeanByCount: case MASICPeakFinder.clsMASICPeakFinder.eNoiseThresholdModes.TrimmedMedianByAbundance: if (noiseThresholdOptions.MinimumSignalToNoiseRatio > 0) { ionCountNew = 0; for (var ionIndex = 0; ionIndex < msSpectrum.IonCount; ionIndex++) { // Always keep points in the m/z ignore range // If CheckPointInMZIgnoreRange returns true, set pointPassesFilter to true var pointPassesFilter = clsUtilities.CheckPointInMZIgnoreRange(msSpectrum.IonsMZ[ionIndex], mzIgnoreRangeStart, mzIgnoreRangeEnd); if (!pointPassesFilter) { // Check the point's intensity against .BaselineNoiseLevelAbsolute if (MASICPeakFinder.clsMASICPeakFinder.ComputeSignalToNoise(msSpectrum.IonsIntensity[ionIndex], noiseThresholdIntensity) >= noiseThresholdOptions.MinimumSignalToNoiseRatio) { pointPassesFilter = true; } } if (pointPassesFilter) { msSpectrum.IonsMZ[ionCountNew] = msSpectrum.IonsMZ[ionIndex]; msSpectrum.IonsIntensity[ionCountNew] = msSpectrum.IonsIntensity[ionIndex]; ionCountNew += 1; } } } else { ionCountNew = msSpectrum.IonCount; } break; default: ReportError("Unknown BaselineNoiseMode encountered in DiscardDataBelowNoiseThreshold: " + noiseThresholdOptions.BaselineNoiseMode.ToString()); break; } if (ionCountNew < msSpectrum.IonCount) { msSpectrum.ShrinkArrays(ionCountNew); } } catch (Exception ex) { ReportError("Error discarding data below the noise threshold", ex, clsMASIC.eMasicErrorCodes.UnspecifiedError); } }