Exemplo n.º 1
0
Arquivo: Mzml.cs Projeto: NRTDP/mzLib
        private static IMzmlScan GetMsDataOneBasedScanFromConnection(Generated.mzMLType _mzMLConnection, int oneBasedSpectrumNumber)
        {
            double[] masses      = null;
            double[] intensities = null;

            foreach (Generated.BinaryDataArrayType binaryData in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].binaryDataArrayList.binaryDataArray)
            {
                bool compressed     = false;
                bool mzArray        = false;
                bool intensityArray = false;
                bool is32bit        = true;
                foreach (Generated.CVParamType cv in binaryData.cvParam)
                {
                    compressed     |= cv.accession.Equals(_zlibCompression);
                    is32bit        &= !cv.accession.Equals(_64bit);
                    is32bit        |= cv.accession.Equals(_32bit);
                    mzArray        |= cv.accession.Equals(_mzArray);
                    intensityArray |= cv.accession.Equals(_intensityArray);
                }

                double[] data = ConvertBase64ToDoubles(binaryData.binary, compressed, is32bit);
                if (mzArray)
                {
                    masses = data;
                }

                if (intensityArray)
                {
                    intensities = data;
                }
            }

            var ok = new MzmlMzSpectrum(masses, intensities, false);

            int?     msOrder    = null;
            bool?    isCentroid = null;
            Polarity polarity   = Polarity.Unknown;
            double   tic        = double.NaN;

            foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].cvParam)
            {
                if (cv.accession.Equals(_msnOrderAccession))
                {
                    msOrder = int.Parse(cv.value);
                }
                if (cv.accession.Equals(_centroidSpectrum))
                {
                    isCentroid = true;
                }
                if (cv.accession.Equals(_profileSpectrum))
                {
                    isCentroid = false;
                }
                if (cv.accession.Equals(_totalIonCurrent))
                {
                    tic = double.Parse(cv.value);
                }
                polarityDictionary.TryGetValue(cv.accession, out polarity);
            }

            double rtInMinutes   = double.NaN;
            string scanFilter    = null;
            double?injectionTime = null;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].cvParam != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].cvParam)
                {
                    if (cv.accession.Equals(_retentionTime))
                    {
                        rtInMinutes = double.Parse(cv.value);
                        if (cv.unitName == "second")
                        {
                            rtInMinutes /= 60;
                        }
                    }
                    if (cv.accession.Equals(_filterString))
                    {
                        scanFilter = cv.value;
                    }
                    if (cv.accession.Equals(_ionInjectionTime))
                    {
                        injectionTime = double.Parse(cv.value);
                    }
                }
            }

            double high = double.NaN;
            double low  = double.NaN;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].scanWindowList != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].scanWindowList.scanWindow[0].cvParam)
                {
                    if (cv.accession.Equals(_scanWindowLowerLimit))
                    {
                        low = double.Parse(cv.value);
                    }
                    if (cv.accession.Equals(_scanWindowUpperLimit))
                    {
                        high = double.Parse(cv.value);
                    }
                }
            }

            if (msOrder.Value == 1)
            {
                return(new MzmlScan(oneBasedSpectrumNumber, ok, msOrder.Value, isCentroid.Value, polarity, rtInMinutes, new MzRange(low, high), scanFilter, GetMzAnalyzer(_mzMLConnection, scanFilter), tic, injectionTime));
            }

            double selectedIonMz        = double.NaN;
            int?   selectedIonCharge    = null;
            double?selectedIonIntensity = null;

            foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam)
            {
                if (cv.accession.Equals(_selectedIonMz))
                {
                    selectedIonMz = double.Parse(cv.value);
                }
                if (cv.accession.Equals(_precursorCharge))
                {
                    selectedIonCharge = int.Parse(cv.value);
                }
                if (cv.accession.Equals(_peakIntensity))
                {
                    selectedIonIntensity = double.Parse(cv.value);
                }
            }

            double?isolationMz   = null;
            double lowIsolation  = double.NaN;
            double highIsolation = double.NaN;

            foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].isolationWindow.cvParam)
            {
                if (cv.accession.Equals(_isolationWindowTargetMZ))
                {
                    isolationMz = double.Parse(cv.value);
                }
                if (cv.accession.Equals(_isolationWindowLowerOffset))
                {
                    lowIsolation = double.Parse(cv.value);
                }
                if (cv.accession.Equals(_isolationWindowUpperOffset))
                {
                    highIsolation = double.Parse(cv.value);
                }
            }
            DissociationType dissociationType = DissociationType.Unknown;

            foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].activation.cvParam)
            {
                dissociationDictionary.TryGetValue(cv.accession, out dissociationType);
            }
            double?monoisotopicMz = null;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].userParam != null)
            {
                foreach (var userParam in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].userParam)
                {
                    if (userParam.name.EndsWith("Monoisotopic M/Z:"))
                    {
                        monoisotopicMz = double.Parse(userParam.value);
                    }
                }
            }

            return(new MzmlScanWithPrecursor(oneBasedSpectrumNumber,
                                             ok,
                                             msOrder.Value,
                                             isCentroid.Value,
                                             polarity,
                                             rtInMinutes,
                                             new MzRange(low, high),
                                             scanFilter,
                                             GetMzAnalyzer(_mzMLConnection, scanFilter),
                                             tic,
                                             selectedIonMz,
                                             selectedIonCharge,
                                             selectedIonIntensity,
                                             isolationMz.Value,
                                             lowIsolation + highIsolation,
                                             dissociationType,
                                             GetOneBasedPrecursorScanNumber(_mzMLConnection, oneBasedSpectrumNumber),
                                             monoisotopicMz,
                                             injectionTime));
        }
Exemplo n.º 2
0
        private static IMzmlScan GetMsDataOneBasedScanFromConnection(Generated.mzMLType _mzMLConnection, int oneBasedSpectrumNumber, IFilteringParams filterParams)
        {
            // Read in the instrument configuration types from connection (in mzml it's at the start)

            Generated.InstrumentConfigurationType[] configs = new Generated.InstrumentConfigurationType[_mzMLConnection.instrumentConfigurationList.instrumentConfiguration.Length];
            for (int i = 0; i < _mzMLConnection.instrumentConfigurationList.instrumentConfiguration.Length; i++)
            {
                configs[i] = _mzMLConnection.instrumentConfigurationList.instrumentConfiguration[i];
            }

            var defaultInstrumentConfig = _mzMLConnection.run.defaultInstrumentConfigurationRef;
            // May be null!
            var scanSpecificInsturmentConfig = _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].instrumentConfigurationRef;

            MZAnalyzerType analyzer = default(MZAnalyzerType);

            // use default
            if (scanSpecificInsturmentConfig == null || scanSpecificInsturmentConfig == defaultInstrumentConfig)
            {
                if (configs[0].componentList == null)
                {
                    analyzer = default(MZAnalyzerType);
                }
                else if (analyzerDictionary.TryGetValue(configs[0].componentList.analyzer[0].cvParam[0].accession, out MZAnalyzerType returnVal))
                {
                    analyzer = returnVal;
                }
            }
            // use scan-specific
            else
            {
                for (int i = 0; i < _mzMLConnection.instrumentConfigurationList.instrumentConfiguration.Length; i++)
                {
                    if (configs[i].id.Equals(scanSpecificInsturmentConfig))
                    {
                        analyzerDictionary.TryGetValue(configs[i].componentList.analyzer[0].cvParam[0].accession, out MZAnalyzerType returnVal);
                        analyzer = returnVal;
                    }
                }
            }

            string nativeId = _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].id;

            int?     msOrder    = null;
            bool?    isCentroid = null;
            Polarity polarity   = Polarity.Unknown;
            double   tic        = double.NaN;

            foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].cvParam)
            {
                if (cv.accession.Equals(_msnOrderAccession))
                {
                    msOrder = int.Parse(cv.value);
                }
                if (cv.accession.Equals(_centroidSpectrum))
                {
                    isCentroid = true;
                }
                if (cv.accession.Equals(_profileSpectrum))
                {
                    throw new MzLibException("Reading profile mode mzmls not supported");
                }
                if (cv.accession.Equals(_totalIonCurrent))
                {
                    tic = double.Parse(cv.value);
                }
                if (polarity.Equals(Polarity.Unknown))
                {
                    polarityDictionary.TryGetValue(cv.accession, out polarity);
                }
            }

            if (!msOrder.HasValue || !isCentroid.HasValue)
            {
                throw new MzLibException("!msOrder.HasValue || !isCentroid.HasValue");
            }

            double[] masses      = new double[0];
            double[] intensities = new double[0];

            foreach (Generated.BinaryDataArrayType binaryData in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].binaryDataArrayList.binaryDataArray)
            {
                bool compressed     = false;
                bool mzArray        = false;
                bool intensityArray = false;
                bool is32bit        = true;
                foreach (Generated.CVParamType cv in binaryData.cvParam)
                {
                    compressed     |= cv.accession.Equals(_zlibCompression);
                    is32bit        &= !cv.accession.Equals(_64bit);
                    is32bit        |= cv.accession.Equals(_32bit);
                    mzArray        |= cv.accession.Equals(_mzArray);
                    intensityArray |= cv.accession.Equals(_intensityArray);
                }

                double[] data = ConvertBase64ToDoubles(binaryData.binary, compressed, is32bit);
                if (mzArray)
                {
                    masses = data;
                }

                if (intensityArray)
                {
                    intensities = data;
                }
            }

            if (filterParams != null && intensities.Length > 0 && (filterParams.MinimumAllowedIntensityRatioToBasePeakM.HasValue || filterParams.NumberOfPeaksToKeepPerWindow.HasValue) &&
                ((filterParams.ApplyTrimmingToMs1 && msOrder.Value == 1) || (filterParams.ApplyTrimmingToMsMs && msOrder.Value > 1)))
            {
                if (filterParams.NumberOfWindows == null)
                {
                    int numPeaks = TopNpeakHelper(intensities, masses, filterParams);
                    Array.Resize(ref intensities, numPeaks);
                    Array.Resize(ref masses, numPeaks);
                }
                else
                {
                    WindowModeHelper(ref intensities, ref masses, filterParams);
                }
            }
            Array.Sort(masses, intensities);
            var mzmlMzSpectrum = new MzmlMzSpectrum(masses, intensities, false);

            double rtInMinutes   = double.NaN;
            string scanFilter    = null;
            double?injectionTime = null;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].cvParam != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].cvParam)
                {
                    if (cv.accession.Equals(_retentionTime))
                    {
                        rtInMinutes = double.Parse(cv.value);
                        if (cv.unitName == "second")
                        {
                            rtInMinutes /= 60;
                        }
                    }
                    if (cv.accession.Equals(_filterString))
                    {
                        scanFilter = cv.value;
                    }
                    if (cv.accession.Equals(_ionInjectionTime))
                    {
                        injectionTime = double.Parse(cv.value);
                    }
                }
            }

            double high = double.NaN;
            double low  = double.NaN;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].scanWindowList != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].scanWindowList.scanWindow[0].cvParam)
                {
                    if (cv.accession.Equals(_scanWindowLowerLimit))
                    {
                        low = double.Parse(cv.value);
                    }
                    if (cv.accession.Equals(_scanWindowUpperLimit))
                    {
                        high = double.Parse(cv.value);
                    }
                }
            }

            if (msOrder.Value == 1)
            {
                return(new MzmlScan(
                           oneBasedSpectrumNumber,
                           mzmlMzSpectrum,
                           msOrder.Value,
                           isCentroid.Value,
                           polarity,
                           rtInMinutes,
                           new MzRange(low, high),
                           scanFilter,
                           analyzer,
                           tic,
                           injectionTime,
                           nativeId));
            }

            double selectedIonMz        = double.NaN;
            int?   selectedIonCharge    = null;
            double?selectedIonIntensity = null;

            foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam)
            {
                if (cv.accession.Equals(_selectedIonMz))
                {
                    selectedIonMz = double.Parse(cv.value);
                }
                if (cv.accession.Equals(_precursorCharge))
                {
                    selectedIonCharge = int.Parse(cv.value);
                }
                if (cv.accession.Equals(_peakIntensity))
                {
                    selectedIonIntensity = double.Parse(cv.value);
                }
            }

            double?isolationMz   = null;
            double lowIsolation  = double.NaN;
            double highIsolation = double.NaN;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].isolationWindow != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].isolationWindow.cvParam)
                {
                    if (cv.accession.Equals(_isolationWindowTargetMZ))
                    {
                        isolationMz = double.Parse(cv.value);
                    }
                    if (cv.accession.Equals(_isolationWindowLowerOffset))
                    {
                        lowIsolation = double.Parse(cv.value);
                    }
                    if (cv.accession.Equals(_isolationWindowUpperOffset))
                    {
                        highIsolation = double.Parse(cv.value);
                    }
                }
            }

            DissociationType dissociationType = DissociationType.Unknown;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].activation.cvParam != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].activation.cvParam)
                {
                    dissociationDictionary.TryGetValue(cv.accession, out dissociationType);
                }
            }
            double?monoisotopicMz = null;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].userParam != null)
            {
                foreach (var userParam in _mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].scanList.scan[0].userParam)
                {
                    if (userParam.name.EndsWith("Monoisotopic M/Z:"))
                    {
                        monoisotopicMz = double.Parse(userParam.value);
                    }
                }
            }
            int?precursorScanNumber;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedSpectrumNumber - 1].precursorList.precursor[0].spectrumRef == null)
            {
                precursorScanNumber = null;
            }
            else
            {
                precursorScanNumber = GetOneBasedPrecursorScanNumber(_mzMLConnection, oneBasedSpectrumNumber);
            }
            return(new MzmlScanWithPrecursor(
                       oneBasedSpectrumNumber,
                       mzmlMzSpectrum,
                       msOrder.Value,
                       isCentroid.Value,
                       polarity,
                       rtInMinutes,
                       new MzRange(low, high),
                       scanFilter,
                       analyzer,
                       tic,
                       selectedIonMz,
                       selectedIonCharge,
                       selectedIonIntensity,
                       isolationMz,
                       lowIsolation + highIsolation,
                       dissociationType,
                       precursorScanNumber,
                       monoisotopicMz,
                       injectionTime,
                       nativeId));
        }