/// <summary> /// Construct a spectrum element for the given scan /// </summary> /// <param name="scanNumber">the scan number</param> /// <returns>The SpectrumType object</returns> private SpectrumType ConstructSpectrum(int scanNumber) { // Get each scan from the RAW file var scan = Scan.FromFile(_rawFile, scanNumber); // Get the scan filter for this scan number var scanFilter = _rawFile.GetFilterForScanNumber(scanNumber); // Get the scan event for this scan number var scanEvent = _rawFile.GetScanEventForScanNumber(scanNumber); var spectrum = new SpectrumType { id = ConstructSpectrumTitle(scanNumber), defaultArrayLength = 0 }; // Add the ionization type if necessary if (!_ionizationTypes.ContainsKey(scanFilter.IonizationMode)) { _ionizationTypes.Add(scanFilter.IonizationMode, OntologyMapping.IonizationTypes[scanFilter.IonizationMode]); } // Add the mass analyzer if necessary if (!_massAnalyzers.ContainsKey(scanFilter.MassAnalyzer) && OntologyMapping.MassAnalyzerTypes.ContainsKey(scanFilter.MassAnalyzer)) { _massAnalyzers.Add(scanFilter.MassAnalyzer, "IC" + (_massAnalyzers.Count + 1)); } // Keep the CV params in a list and convert to array afterwards var spectrumCvParams = new List <CVParamType> { new CVParamType { name = "ms level", accession = "MS:1000511", value = ((int)scanFilter.MSOrder).ToString(CultureInfo.InvariantCulture), cvRef = "MS" } }; // Trailer extra data list var trailerData = _rawFile.GetTrailerExtraInformation(scanNumber); int? charge = null; double?monoisotopicMass = null; for (var i = 0; i < trailerData.Length; i++) { if (trailerData.Labels[i] == "Charge State:") { if (Convert.ToInt32(trailerData.Values[i]) > 0) { charge = Convert.ToInt32(trailerData.Values[i]); } } if (trailerData.Labels[i] == "Monoisotopic M/Z:") { monoisotopicMass = double.Parse(trailerData.Values[i]); } } // Construct and set the scan list element of the spectrum var scanListType = ConstructScanList(scanNumber, scan, scanFilter, scanEvent, monoisotopicMass); spectrum.scanList = scanListType; switch (scanFilter.MSOrder) { case MSOrderType.Ms: spectrumCvParams.Add(new CVParamType { accession = "MS:1000579", cvRef = "MS", name = "MS1 spectrum", value = "" }); // Keep track of scan number for precursor reference _precursorScanNumber = scanNumber; break; case MSOrderType.Ms2: spectrumCvParams.Add(new CVParamType { accession = "MS:1000580", cvRef = "MS", name = "MSn spectrum", value = "" }); // Construct and set the precursor list element of the spectrum var precursorListType = ConstructPrecursorList(scanEvent, charge); spectrum.precursorList = precursorListType; break; case MSOrderType.Ng: break; case MSOrderType.Nl: break; case MSOrderType.Par: break; case MSOrderType.Any: break; case MSOrderType.Ms3: break; case MSOrderType.Ms4: break; case MSOrderType.Ms5: break; case MSOrderType.Ms6: break; case MSOrderType.Ms7: break; case MSOrderType.Ms8: break; case MSOrderType.Ms9: break; case MSOrderType.Ms10: break; default: throw new ArgumentOutOfRangeException(); } // Scan polarity var polarityType = scanFilter.Polarity; switch (polarityType) { case PolarityType.Positive: spectrumCvParams.Add(new CVParamType { accession = "MS:1000130", cvRef = "MS", name = "positive scan", value = "" }); break; case PolarityType.Negative: spectrumCvParams.Add(new CVParamType { accession = "MS:1000129", cvRef = "MS", name = "negative scan", value = "" }); break; case PolarityType.Any: break; default: throw new ArgumentOutOfRangeException(); } // Total ion current spectrumCvParams.Add(new CVParamType { name = "total ion current", accession = "MS:1000285", value = scan.ScanStatistics.TIC.ToString(CultureInfo.InvariantCulture), cvRef = "MS" }); double?basePeakMass = null; double?basePeakIntensity = null; double?lowestObservedMz = null; double?highestObservedMz = null; double[] masses = null; double[] intensities = null; if (scan.HasCentroidStream) { var centroidStream = _rawFile.GetCentroidStream(scanNumber, false); if (scan.CentroidScan.Length > 0) { basePeakMass = centroidStream.BasePeakMass; basePeakIntensity = centroidStream.BasePeakIntensity; lowestObservedMz = centroidStream.Masses[0]; highestObservedMz = centroidStream.Masses[centroidStream.Masses.Length - 1]; masses = centroidStream.Masses; intensities = centroidStream.Intensities; // Note that although the scan data type is profile, // centroid data might be available spectrumCvParams.Add(new CVParamType { accession = "MS:1000127", cvRef = "MS", name = "centroid spectrum", value = "" }); } } else { // Get the scan statistics from the RAW file for this scan number var scanStatistics = _rawFile.GetScanStatsForScanNumber(scanNumber); basePeakMass = scanStatistics.BasePeakMass; basePeakIntensity = scanStatistics.BasePeakIntensity; // Get the segmented (low res and profile) scan data var segmentedScan = _rawFile.GetSegmentedScanFromScanNumber(scanNumber, scanStatistics); if (segmentedScan.Positions.Length > 0) { lowestObservedMz = segmentedScan.Positions[0]; highestObservedMz = segmentedScan.Positions[segmentedScan.Positions.Length - 1]; masses = segmentedScan.Positions; intensities = segmentedScan.Intensities; spectrumCvParams.Add(new CVParamType { accession = "MS:1000128", cvRef = "MS", name = "profile spectrum", value = "" }); } } // Base peak m/z if (basePeakMass != null) { spectrumCvParams.Add(new CVParamType { name = "base peak m/z", accession = "MS:1000504", value = basePeakMass.ToString(), unitCvRef = "MS", unitName = "m/z", unitAccession = "MS:1000040", cvRef = "MS" }); } // Base peak intensity if (basePeakMass != null) { spectrumCvParams.Add(new CVParamType { name = "base peak intensity", accession = "MS:1000505", value = basePeakIntensity.ToString(), unitCvRef = "MS", unitName = "number of detector counts", unitAccession = "MS:1000131", cvRef = "MS" }); } // Lowest observed mz if (lowestObservedMz != null) { spectrumCvParams.Add(new CVParamType { name = "lowest observed m/z", accession = "MS:1000528", value = lowestObservedMz.ToString(), unitCvRef = "MS", unitAccession = "MS:1000040", unitName = "m/z", cvRef = "MS" }); } // Highest observed mz if (highestObservedMz != null) { spectrumCvParams.Add(new CVParamType { name = "highest observed m/z", accession = "MS:1000527", value = highestObservedMz.ToString(), unitAccession = "MS:1000040", unitName = "m/z", unitCvRef = "MS", cvRef = "MS" }); } // Add the CV params to the spectrum spectrum.cvParam = spectrumCvParams.ToArray(); // Binary data array list var binaryData = new List <BinaryDataArrayType>(); // M/Z Data if (!masses.IsNullOrEmpty()) { // Set the spectrum default array length spectrum.defaultArrayLength = masses.Length; var massesBinaryData = new BinaryDataArrayType { binary = GetZLib64BitArray(masses) }; massesBinaryData.encodedLength = (4 * Math.Ceiling((double)massesBinaryData .binary.Length / 3)).ToString(CultureInfo.InvariantCulture); massesBinaryData.cvParam = new CVParamType[3]; massesBinaryData.cvParam[0] = new CVParamType { accession = "MS:1000514", name = "m/z array", cvRef = "MS", unitName = "m/z", value = "", unitCvRef = "MS", unitAccession = "MS:1000040" }; massesBinaryData.cvParam[1] = new CVParamType { accession = "MS:1000523", name = "64-bit float", cvRef = "MS", value = "" }; massesBinaryData.cvParam[2] = new CVParamType { accession = "MS:1000574", name = "zlib compression", cvRef = "MS", value = "" }; binaryData.Add(massesBinaryData); } // Intensity Data if (!intensities.IsNullOrEmpty()) { // Set the spectrum default array length if necessary if (spectrum.defaultArrayLength == 0) { spectrum.defaultArrayLength = masses.Length; } var intensitiesBinaryData = new BinaryDataArrayType { binary = GetZLib64BitArray(intensities) }; intensitiesBinaryData.encodedLength = (4 * Math.Ceiling((double)intensitiesBinaryData .binary.Length / 3)).ToString(CultureInfo.InvariantCulture); intensitiesBinaryData.cvParam = new CVParamType[3]; intensitiesBinaryData.cvParam[0] = new CVParamType { accession = "MS:1000515", name = "intensity array", cvRef = "MS", unitCvRef = "MS", unitAccession = "MS:1000131", unitName = "number of counts", value = "" }; intensitiesBinaryData.cvParam[1] = new CVParamType { accession = "MS:1000523", name = "64-bit float", cvRef = "MS", value = "" }; intensitiesBinaryData.cvParam[2] = new CVParamType { accession = "MS:1000574", name = "zlib compression", cvRef = "MS", value = "" }; binaryData.Add(intensitiesBinaryData); } if (!binaryData.IsNullOrEmpty()) { spectrum.binaryDataArrayList = new BinaryDataArrayListType { count = binaryData.Count.ToString(), binaryDataArray = binaryData.ToArray() }; } return(spectrum); }
/// <summary> /// Construct the chromatogram element(s) /// </summary> /// <param name="firstScanNumber">the first scan number</param> /// <param name="lastScanNumber">the last scan number</param> /// <returns>a list of chromatograms</returns> private List <ChromatogramType> ConstructChromatograms(int firstScanNumber, int lastScanNumber) { var chromatograms = new List <ChromatogramType>(); // Define the settings for getting the Base Peak chromatogram var settings = new ChromatogramTraceSettings(TraceType.BasePeak); // Get the chromatogram from the RAW file. var data = _rawFile.GetChromatogramData(new IChromatogramSettings[] { settings }, firstScanNumber, lastScanNumber); // Split the data into the chromatograms var trace = ChromatogramSignal.FromChromatogramData(data); for (var i = 0; i < trace.Length; i++) { if (trace[i].Length > 0) { // Binary data array list var binaryData = new List <BinaryDataArrayType>(); var chromatogram = new ChromatogramType { index = i.ToString(), id = "base_peak_" + i, defaultArrayLength = 0, binaryDataArrayList = new BinaryDataArrayListType { count = "2", binaryDataArray = new BinaryDataArrayType[2] }, cvParam = new CVParamType[1] }; chromatogram.cvParam[0] = new CVParamType { accession = "MS:1000235", name = "total ion current chromatogram", cvRef = "MS", value = "" }; // Chromatogram times if (!trace[i].Times.IsNullOrEmpty()) { // Set the chromatogram default array length chromatogram.defaultArrayLength = trace[i].Times.Count; var timesBinaryData = new BinaryDataArrayType { binary = GetZLib64BitArray(trace[i].Times) }; timesBinaryData.encodedLength = (4 * Math.Ceiling((double)timesBinaryData .binary.Length / 3)).ToString(CultureInfo.InvariantCulture); timesBinaryData.cvParam = new CVParamType[3]; timesBinaryData.cvParam[0] = new CVParamType { accession = "MS:1000595", name = "time array", cvRef = "MS", unitName = "minute", value = "", unitCvRef = "UO", unitAccession = "UO:0000031" }; timesBinaryData.cvParam[1] = new CVParamType { accession = "MS:1000523", name = "64-bit float", cvRef = "MS", value = "" }; timesBinaryData.cvParam[2] = new CVParamType { accession = "MS:1000574", name = "zlib compression", cvRef = "MS", value = "" }; binaryData.Add(timesBinaryData); } // Chromatogram intensities if (!trace[i].Times.IsNullOrEmpty()) { // Set the spectrum default array length if necessary if (chromatogram.defaultArrayLength == 0) { chromatogram.defaultArrayLength = trace[i].Intensities.Count; } var intensitiesBinaryData = new BinaryDataArrayType { binary = GetZLib64BitArray(trace[i].Intensities) }; intensitiesBinaryData.encodedLength = (4 * Math.Ceiling((double)intensitiesBinaryData .binary.Length / 3)).ToString(CultureInfo.InvariantCulture); intensitiesBinaryData.cvParam = new CVParamType[3]; intensitiesBinaryData.cvParam[0] = new CVParamType { accession = "MS:1000515", name = "intensity array", cvRef = "MS", unitName = "number of counts", value = "", unitCvRef = "MS", unitAccession = "MS:1000131" }; intensitiesBinaryData.cvParam[1] = new CVParamType { accession = "MS:1000523", name = "64-bit float", cvRef = "MS", value = "" }; intensitiesBinaryData.cvParam[2] = new CVParamType { accession = "MS:1000574", name = "zlib compression", cvRef = "MS", value = "" }; binaryData.Add(intensitiesBinaryData); } if (!binaryData.IsNullOrEmpty()) { chromatogram.binaryDataArrayList = new BinaryDataArrayListType { count = binaryData.Count.ToString(), binaryDataArray = binaryData.ToArray() }; } chromatograms.Add(chromatogram); } } return(chromatograms); }