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); } }
/// <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); } }