public bool SaveExtendedScanStatsFiles( clsScanList scanList, string inputFileName, string outputDirectoryPath, bool includeHeaders) { // Writes out a flat file containing the extended scan stats var extendedNonConstantHeaderOutputFilePath = string.Empty; const char TAB_DELIMITER = '\t'; try { UpdateProgress(0, "Saving extended scan stats to flat file"); var extendedConstantHeaderOutputFilePath = clsDataOutput.ConstructOutputFilePath(inputFileName, outputDirectoryPath, clsDataOutput.eOutputFileTypeConstants.ScanStatsExtendedConstantFlatFile); extendedNonConstantHeaderOutputFilePath = clsDataOutput.ConstructOutputFilePath(inputFileName, outputDirectoryPath, clsDataOutput.eOutputFileTypeConstants.ScanStatsExtendedFlatFile); ReportMessage("Saving extended scan stats flat file to disk: " + Path.GetFileName(extendedNonConstantHeaderOutputFilePath)); if (mExtendedHeaderNameMap.Count == 0) { // No extended stats to write; exit the function UpdateProgress(100); return(true); } // Lookup extended stats values that are constants for all scans // The following will also remove the constant header values from mExtendedHeaderNameMap var constantExtendedHeaderValues = ExtractConstantExtendedHeaderValues(out var nonConstantHeaderIDs, scanList.SurveyScans, scanList.FragScans, TAB_DELIMITER); if (constantExtendedHeaderValues == null) { constantExtendedHeaderValues = string.Empty; } // Write the constant extended stats values to a text file using (var writer = new StreamWriter(extendedConstantHeaderOutputFilePath, false)) { writer.WriteLine(constantExtendedHeaderValues); } // Now open another output file for the non-constant extended stats using (var writer = new StreamWriter(extendedNonConstantHeaderOutputFilePath, false)) { if (includeHeaders) { var headerNames = ConstructExtendedStatsHeaders(); writer.WriteLine(string.Join(TAB_DELIMITER.ToString(), headerNames)); } for (var scanIndex = 0; scanIndex < scanList.MasterScanOrderCount; scanIndex++) { var currentScan = GetScanByMasterScanIndex(scanList, scanIndex); var dataColumns = ConcatenateExtendedStats(nonConstantHeaderIDs, mOptions.SICOptions.DatasetID, currentScan.ScanNumber, currentScan.ExtendedHeaderInfo); writer.WriteLine(string.Join(TAB_DELIMITER.ToString(), dataColumns)); if (scanIndex % 100 == 0) { UpdateProgress((short)(scanIndex / (double)(scanList.MasterScanOrderCount - 1) * 100)); } } } } catch (Exception ex) { ReportError("Error writing the Extended Scan Stats to: " + extendedNonConstantHeaderOutputFilePath, ex, clsMASIC.eMasicErrorCodes.OutputFileWriteError); return(false); } UpdateProgress(100); return(true); }
protected void SaveScanStatEntry( StreamWriter writer, clsScanList.eScanTypeConstants eScanType, clsScanInfo currentScan, int datasetID) { const char TAB_DELIMITER = '\t'; var scanStatsEntry = new ScanStatsEntry() { ScanNumber = currentScan.ScanNumber }; if (eScanType == clsScanList.eScanTypeConstants.SurveyScan) { scanStatsEntry.ScanType = 1; scanStatsEntry.ScanTypeName = string.Copy(currentScan.ScanTypeName); } else { if (currentScan.FragScanInfo.MSLevel <= 1) { // This is a fragmentation scan, so it must have a scan type of at least 2 scanStatsEntry.ScanType = 2; } else { // .MSLevel is 2 or higher, record the actual MSLevel value scanStatsEntry.ScanType = currentScan.FragScanInfo.MSLevel; } scanStatsEntry.ScanTypeName = string.Copy(currentScan.ScanTypeName); } scanStatsEntry.ScanFilterText = currentScan.ScanHeaderText; scanStatsEntry.ElutionTime = currentScan.ScanTime.ToString("0.0000"); scanStatsEntry.TotalIonIntensity = StringUtilities.ValueToString(currentScan.TotalIonIntensity, 5); scanStatsEntry.BasePeakIntensity = StringUtilities.ValueToString(currentScan.BasePeakIonIntensity, 5); scanStatsEntry.BasePeakMZ = StringUtilities.DblToString(currentScan.BasePeakIonMZ, 4); // Base peak signal to noise ratio scanStatsEntry.BasePeakSignalToNoiseRatio = StringUtilities.ValueToString(MASICPeakFinder.clsMASICPeakFinder.ComputeSignalToNoise(currentScan.BasePeakIonIntensity, currentScan.BaselineNoiseStats.NoiseLevel), 4); scanStatsEntry.IonCount = currentScan.IonCount; scanStatsEntry.IonCountRaw = currentScan.IonCountRaw; mScanTracking.ScanStats.Add(scanStatsEntry); var dataColumns = new List <string>() { datasetID.ToString(), // Dataset ID scanStatsEntry.ScanNumber.ToString(), // Scan number scanStatsEntry.ElutionTime, // Scan time (minutes) scanStatsEntry.ScanType.ToString(), // Scan type (1 for MS, 2 for MS2, etc.) scanStatsEntry.TotalIonIntensity, // Total ion intensity scanStatsEntry.BasePeakIntensity, // Base peak ion intensity scanStatsEntry.BasePeakMZ, // Base peak ion m/z scanStatsEntry.BasePeakSignalToNoiseRatio, // Base peak signal to noise ratio scanStatsEntry.IonCount.ToString(), // Number of peaks (aka ions) in the spectrum scanStatsEntry.IonCountRaw.ToString(), // Number of peaks (aka ions) in the spectrum prior to any filtering scanStatsEntry.ScanTypeName // Scan type name }; writer.WriteLine(string.Join(TAB_DELIMITER.ToString(), dataColumns)); }
/// <summary> /// Looks for the reporter ion peaks using FindReporterIonsWork /// </summary> /// <param name="scanList"></param> /// <param name="spectraCache"></param> /// <param name="inputFilePathFull">Full path to the input file</param> /// <param name="outputDirectoryPath"></param> /// <returns></returns> /// <remarks></remarks> public bool FindReporterIons( clsScanList scanList, clsSpectraCache spectraCache, string inputFilePathFull, string outputDirectoryPath) { const char TAB_DELIMITER = '\t'; var outputFilePath = "??"; try { // Use Xraw to read the .Raw files var readerOptions = new ThermoReaderOptions() { LoadMSMethodInfo = false, LoadMSTuneInfo = false }; var rawFileReader = new XRawFileIO(readerOptions); RegisterEvents(rawFileReader); var includeFtmsColumns = false; if (inputFilePathFull.ToUpper().EndsWith(DataInput.clsDataImport.THERMO_RAW_FILE_EXTENSION.ToUpper())) { // Processing a thermo .Raw file // Check whether any of the frag scans has IsFTMS true for (var masterOrderIndex = 0; masterOrderIndex < scanList.MasterScanOrderCount; masterOrderIndex++) { var scanPointer = scanList.MasterScanOrder[masterOrderIndex].ScanIndexPointer; if (scanList.MasterScanOrder[masterOrderIndex].ScanType == clsScanList.eScanTypeConstants.SurveyScan) { // Skip survey scans continue; } if (scanList.FragScans[scanPointer].IsFTMS) { includeFtmsColumns = true; break; } } if (includeFtmsColumns) { rawFileReader.OpenRawFile(inputFilePathFull); } } if (mOptions.ReporterIons.ReporterIonList.Count == 0) { // No reporter ions defined; default to ITraq mOptions.ReporterIons.SetReporterIonMassMode(clsReporterIons.eReporterIonMassModeConstants.ITraqFourMZ); } // Populate array reporterIons, which we will sort by m/z var reporterIons = new clsReporterIonInfo[mOptions.ReporterIons.ReporterIonList.Count]; var reporterIonIndex = 0; foreach (var reporterIon in mOptions.ReporterIons.ReporterIonList) { reporterIons[reporterIonIndex] = reporterIon; reporterIonIndex += 1; } Array.Sort(reporterIons, new clsReportIonInfoComparer()); outputFilePath = clsDataOutput.ConstructOutputFilePath( Path.GetFileName(inputFilePathFull), outputDirectoryPath, clsDataOutput.eOutputFileTypeConstants.ReporterIonsFile); using (var writer = new StreamWriter(outputFilePath)) { // Write the file headers var reporterIonMZsUnique = new SortedSet <string>(); var headerColumns = new List <string>(7 + reporterIons.Length + 1) { "Dataset", "ScanNumber", "Collision Mode", "ParentIonMZ", "BasePeakIntensity", "BasePeakMZ", "ReporterIonIntensityMax" }; var obsMZHeaders = new List <string>(reporterIons.Length); var uncorrectedIntensityHeaders = new List <string>(reporterIons.Length); var ftmsSignalToNoise = new List <string>(reporterIons.Length); var ftmsResolution = new List <string>(reporterIons.Length); //var ftmsLabelDataMz = new List<string>(reporterIons.Length); var saveUncorrectedIntensities = mOptions.ReporterIons.ReporterIonApplyAbundanceCorrection && mOptions.ReporterIons.ReporterIonSaveUncorrectedIntensities; var dataAggregation = new clsDataAggregation(); RegisterEvents(dataAggregation); foreach (var reporterIon in reporterIons) { if (!reporterIon.ContaminantIon || saveUncorrectedIntensities) { // Construct the reporter ion intensity header // We skip contaminant ions, unless saveUncorrectedIntensities is True, then we include them string mzValue; if (mOptions.ReporterIons.ReporterIonMassMode == clsReporterIons.eReporterIonMassModeConstants.TMTTenMZ || mOptions.ReporterIons.ReporterIonMassMode == clsReporterIons.eReporterIonMassModeConstants.TMTElevenMZ || mOptions.ReporterIons.ReporterIonMassMode == clsReporterIons.eReporterIonMassModeConstants.TMTSixteenMZ) { mzValue = reporterIon.MZ.ToString("#0.000"); } else { mzValue = ((int)Math.Round(reporterIon.MZ, 0)).ToString(); } if (reporterIonMZsUnique.Contains(mzValue)) { // Uniquify the m/z value mzValue += "_" + reporterIonIndex; } try { reporterIonMZsUnique.Add(mzValue); } catch (Exception ex) { // Error updating the SortedSet; // this shouldn't happen based on the .ContainsKey test above } // Append the reporter ion intensity title to the headers headerColumns.Add("Ion_" + mzValue); // This string will only be included in the header line if mOptions.ReporterIons.ReporterIonSaveObservedMasses is true obsMZHeaders.Add("Ion_" + mzValue + "_ObsMZ"); // This string will be included in the header line if saveUncorrectedIntensities is true uncorrectedIntensityHeaders.Add("Ion_" + mzValue + "_OriginalIntensity"); // This string will be included in the header line if includeFtmsColumns is true ftmsSignalToNoise.Add("Ion_" + mzValue + "_SignalToNoise"); ftmsResolution.Add("Ion_" + mzValue + "_Resolution"); // Uncomment to include the label data m/z value in the _ReporterIons.txt file // This string will only be included in the header line if mOptions.ReporterIons.ReporterIonSaveObservedMasses is true //ftmsLabelDataMz.Add("Ion_" + mzValue + "_LabelDataMZ"); } } headerColumns.Add("Weighted Avg Pct Intensity Correction"); if (mOptions.ReporterIons.ReporterIonSaveObservedMasses) { headerColumns.AddRange(obsMZHeaders); } if (saveUncorrectedIntensities) { headerColumns.AddRange(uncorrectedIntensityHeaders); } if (includeFtmsColumns) { headerColumns.AddRange(ftmsSignalToNoise); headerColumns.AddRange(ftmsResolution); // Uncomment to include the label data m/z value in the _ReporterIons.txt file // If mOptions.ReporterIons.ReporterIonSaveObservedMasses Then // headerColumns.AddRange(ftmsLabelDataMz) // End If } // Write the headers to the output file, separated by tabs writer.WriteLine(string.Join(TAB_DELIMITER.ToString(), headerColumns)); UpdateProgress(0, "Searching for reporter ions"); for (var masterOrderIndex = 0; masterOrderIndex < scanList.MasterScanOrderCount; masterOrderIndex++) { var scanPointer = scanList.MasterScanOrder[masterOrderIndex].ScanIndexPointer; if (scanList.MasterScanOrder[masterOrderIndex].ScanType == clsScanList.eScanTypeConstants.SurveyScan) { // Skip Survey Scans continue; } FindReporterIonsWork( rawFileReader, dataAggregation, includeFtmsColumns, mOptions.SICOptions, scanList, spectraCache, scanList.FragScans[scanPointer], writer, reporterIons, TAB_DELIMITER, saveUncorrectedIntensities, mOptions.ReporterIons.ReporterIonSaveObservedMasses); if (scanList.MasterScanOrderCount > 1) { UpdateProgress((short)(masterOrderIndex / (double)(scanList.MasterScanOrderCount - 1) * 100)); } else { UpdateProgress(0); } UpdateCacheStats(spectraCache); if (mOptions.AbortProcessing) { break; } } } if (includeFtmsColumns) { // Close the handle to the data file rawFileReader.CloseRawFile(); } return(true); } catch (Exception ex) { ReportError("Error writing the reporter ions to: " + outputFilePath, ex, clsMASIC.eMasicErrorCodes.OutputFileWriteError); return(false); } }
/// <summary> /// Export MRM data to disk /// </summary> /// <param name="scanList"></param> /// <param name="spectraCache"></param> /// <param name="mrmSettings"></param> /// <param name="srmList"></param> /// <param name="inputFileName"></param> /// <param name="outputDirectoryPath"></param> /// <returns>True if the MRM data is successfully written to disk, or if the mrmSettings list is empty</returns> private bool ExportMRMDataToDisk( clsScanList scanList, clsSpectraCache spectraCache, IReadOnlyList <clsMRMScanInfo> mrmSettings, IReadOnlyList <udtSRMListType> srmList, string inputFileName, string outputDirectoryPath) { const char TAB_DELIMITER = '\t'; StreamWriter dataWriter = null; StreamWriter crosstabWriter = null; bool success; try { // Only write this data if 1 or more fragmentation spectra are of type SRM if (mrmSettings == null || mrmSettings.Count == 0) { return(true); } UpdateProgress(0, "Exporting MRM data"); // Write out the MRM Settings var mrmSettingsFilePath = clsDataOutput.ConstructOutputFilePath( inputFileName, outputDirectoryPath, clsDataOutput.eOutputFileTypeConstants.MRMSettingsFile); using (var settingsWriter = new StreamWriter(mrmSettingsFilePath)) { settingsWriter.WriteLine(mDataOutputHandler.GetHeadersForOutputFile(scanList, clsDataOutput.eOutputFileTypeConstants.MRMSettingsFile)); var dataColumns = new List <string>(7); for (var mrmInfoIndex = 0; mrmInfoIndex < mrmSettings.Count; mrmInfoIndex++) { var mrmSetting = mrmSettings[mrmInfoIndex]; for (var mrmMassIndex = 0; mrmMassIndex < mrmSetting.MRMMassCount; mrmMassIndex++) { dataColumns.Clear(); dataColumns.Add(mrmInfoIndex.ToString()); dataColumns.Add(mrmSetting.ParentIonMZ.ToString("0.000")); dataColumns.Add(mrmSetting.MRMMassList[mrmMassIndex].CentralMass.ToString("0.000")); dataColumns.Add(mrmSetting.MRMMassList[mrmMassIndex].StartMass.ToString("0.000")); dataColumns.Add(mrmSetting.MRMMassList[mrmMassIndex].EndMass.ToString("0.000")); dataColumns.Add(mrmSetting.ScanCount.ToString()); settingsWriter.WriteLine(string.Join(TAB_DELIMITER.ToString(), dataColumns)); } } if (mOptions.WriteMRMDataList || mOptions.WriteMRMIntensityCrosstab) { // Populate srmKeyToIndexMap var srmKeyToIndexMap = new Dictionary <string, int>(); for (var srmIndex = 0; srmIndex < srmList.Count; srmIndex++) { srmKeyToIndexMap.Add(ConstructSRMMapKey(srmList[srmIndex]), srmIndex); } if (mOptions.WriteMRMDataList) { // Write out the raw MRM Data var dataFilePath = clsDataOutput.ConstructOutputFilePath(inputFileName, outputDirectoryPath, clsDataOutput.eOutputFileTypeConstants.MRMDatafile); dataWriter = new StreamWriter(dataFilePath); // Write the file headers dataWriter.WriteLine(mDataOutputHandler.GetHeadersForOutputFile(scanList, clsDataOutput.eOutputFileTypeConstants.MRMDatafile)); } if (mOptions.WriteMRMIntensityCrosstab) { // Write out the raw MRM Data var crosstabFilePath = clsDataOutput.ConstructOutputFilePath(inputFileName, outputDirectoryPath, clsDataOutput.eOutputFileTypeConstants.MRMCrosstabFile); crosstabWriter = new StreamWriter(crosstabFilePath); // Initialize the crosstab header variable using the data in udtSRMList() var headerNames = new List <string>(srmList.Count + 2) { "Scan_First", "ScanTime" }; for (var srmIndex = 0; srmIndex < srmList.Count; srmIndex++) { headerNames.Add(ConstructSRMMapKey(srmList[srmIndex])); } crosstabWriter.WriteLine(string.Join(TAB_DELIMITER.ToString(), headerNames)); } var scanFirst = int.MinValue; float scanTimeFirst = 0; var srmIndexLast = 0; var crosstabColumnValue = new double[srmList.Count]; var crosstabColumnFlag = new bool[srmList.Count]; // For scanIndex = 0 To scanList.FragScanCount - 1 foreach (var fragScan in scanList.FragScans) { if (fragScan.MRMScanType != MRMScanTypeConstants.SRM) { continue; } if (scanFirst == int.MinValue) { scanFirst = fragScan.ScanNumber; scanTimeFirst = fragScan.ScanTime; } // Look for each of the m/z values specified in fragScan.MRMScanInfo.MRMMassList for (var mrmMassIndex = 0; mrmMassIndex < fragScan.MRMScanInfo.MRMMassCount; mrmMassIndex++) { // Find the maximum value between fragScan.StartMass and fragScan.EndMass // Need to define a tolerance to account for numeric rounding artifacts in the variables var mzStart = fragScan.MRMScanInfo.MRMMassList[mrmMassIndex].StartMass; var mzEnd = fragScan.MRMScanInfo.MRMMassList[mrmMassIndex].EndMass; var mrmToleranceHalfWidth = Math.Round((mzEnd - mzStart) / 2, 6); if (mrmToleranceHalfWidth < 0.001) { mrmToleranceHalfWidth = 0.001; } var matchFound = mDataAggregation.FindMaxValueInMZRange( spectraCache, fragScan, mzStart - mrmToleranceHalfWidth, mzEnd + mrmToleranceHalfWidth, out _, out var matchIntensity); if (mOptions.WriteMRMDataList) { dataColumns.Clear(); dataColumns.Add(fragScan.ScanNumber.ToString()); dataColumns.Add(fragScan.MRMScanInfo.ParentIonMZ.ToString("0.000")); if (matchFound) { dataColumns.Add(fragScan.MRMScanInfo.MRMMassList[mrmMassIndex].CentralMass.ToString("0.000")); dataColumns.Add(matchIntensity.ToString("0.000")); } else { dataColumns.Add(fragScan.MRMScanInfo.MRMMassList[mrmMassIndex].CentralMass.ToString("0.000")); dataColumns.Add("0"); } dataWriter?.WriteLine(string.Join(TAB_DELIMITER.ToString(), dataColumns)); } if (!mOptions.WriteMRMIntensityCrosstab) { continue; } var srmMapKey = ConstructSRMMapKey(fragScan.MRMScanInfo.ParentIonMZ, fragScan.MRMScanInfo.MRMMassList[mrmMassIndex].CentralMass); // Use srmKeyToIndexMap to determine the appropriate column index for srmMapKey if (srmKeyToIndexMap.TryGetValue(srmMapKey, out var srmIndex)) { if (crosstabColumnFlag[srmIndex] || srmIndex == 0 && srmIndexLast == srmList.Count - 1) { // Either the column is already populated, or the SRMIndex has cycled back to zero // Write out the current crosstab line and reset the crosstab column arrays ExportMRMDataWriteLine(crosstabWriter, scanFirst, scanTimeFirst, crosstabColumnValue, crosstabColumnFlag, TAB_DELIMITER, true); scanFirst = fragScan.ScanNumber; scanTimeFirst = fragScan.ScanTime; } if (matchFound) { crosstabColumnValue[srmIndex] = matchIntensity; } crosstabColumnFlag[srmIndex] = true; srmIndexLast = srmIndex; } else { // Unknown combination of parent ion m/z and daughter m/z; this is unexpected // We won't write this entry out } } UpdateCacheStats(spectraCache); if (mOptions.AbortProcessing) { break; } } if (mOptions.WriteMRMIntensityCrosstab) { // Write out any remaining crosstab values ExportMRMDataWriteLine(crosstabWriter, scanFirst, scanTimeFirst, crosstabColumnValue, crosstabColumnFlag, TAB_DELIMITER, false); } } } success = true; } catch (Exception ex) { ReportError("Error writing the SRM data to disk", ex, clsMASIC.eMasicErrorCodes.OutputFileWriteError); success = false; } finally { dataWriter?.Close(); crosstabWriter?.Close(); } return(success); }