Exemplo n.º 1
0
        public override string ToString()
        {
            string result = string.Empty;

            if (double.IsNaN(Score) && !double.IsNaN(Confidence))
            {
                result = string.Format("{0},{1}", Polarity, Confidence.ToString("F"));
            }
            else
            {
                result = string.Format("{0},{1}", Polarity, Score.ToString("F"));
            }

            if (!string.IsNullOrEmpty(ReferencePolarity))
            {
                result += string.Format(",{0}", (Polarity.Equals(ReferencePolarity) ? "agrees" : "disagrees"));
            }

            return(result);
        }
Exemplo n.º 2
0
        private static MsDataScan GetMsDataOneBasedScanFromConnection(Generated.mzMLType _mzMLConnection, int oneBasedIndex, 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[oneBasedIndex - 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[oneBasedIndex - 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[oneBasedIndex - 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, CultureInfo.InvariantCulture);
                }
                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[oneBasedIndex - 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;
                }
            }

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

            var aScanWindowList = _mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].scanList.scan[0].scanWindowList;

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

            if (filterParams != null && intensities.Length > 0 && ((filterParams.ApplyTrimmingToMs1 && msOrder.Value == 1) || (filterParams.ApplyTrimmingToMsMs && msOrder.Value > 1)))
            {
                WindowModeHelper(ref intensities, ref masses, filterParams, low, high);
            }

            Array.Sort(masses, intensities);
            var mzmlMzSpectrum = new MzSpectrum(masses, intensities, false);

            double rtInMinutes        = double.NaN;
            string scanFilter         = null;
            double?injectionTime      = null;
            int    oneBasedScanNumber = oneBasedIndex;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].scanList.scan[0].cvParam != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].scanList.scan[0].cvParam)
                {
                    if (cv.accession.Equals(_retentionTime))
                    {
                        rtInMinutes = double.Parse(cv.value, CultureInfo.InvariantCulture);
                        if (cv.unitName == "second")
                        {
                            rtInMinutes /= 60;
                        }
                    }
                    if (cv.accession.Equals(_filterString))
                    {
                        scanFilter = cv.value;
                    }
                    if (cv.accession.Equals(_ionInjectionTime))
                    {
                        injectionTime = double.Parse(cv.value, CultureInfo.InvariantCulture);
                    }
                    if (cv.accession.Equals(_oneBasedScanNumber)) //get the real one based spectrum number (if available), the other assumes they are in order. This is present in .mgf->.mzml conversions from MSConvert
                    {
                        oneBasedScanNumber = int.Parse(cv.value);
                    }
                }
            }

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

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

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

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

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

            DissociationType dissociationType = DissociationType.Unknown;

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

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

            int?precursorScanNumber;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].precursorList.precursor[0].spectrumRef == null)
            {
                precursorScanNumber = null;
            }
            else
            {
                precursorScanNumber = GetOneBasedPrecursorScanNumber(_mzMLConnection, oneBasedIndex);
            }

            return(new MsDataScan(
                       mzmlMzSpectrum,
                       oneBasedIndex,
                       msOrder.Value,
                       isCentroid.Value,
                       polarity,
                       rtInMinutes,
                       new MzRange(low, high),
                       scanFilter,
                       analyzer,
                       tic,
                       injectionTime,
                       null,
                       nativeId,
                       selectedIonMz,
                       selectedIonCharge,
                       selectedIonIntensity,
                       isolationMz,
                       lowIsolation + highIsolation,
                       dissociationType,
                       precursorScanNumber,
                       monoisotopicMz
                       ));
        }
Exemplo n.º 3
0
        private static MsDataScan GetMsDataOneBasedScanFromConnection(Generated.mzMLType _mzMLConnection, int oneBasedIndex, 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[oneBasedIndex - 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[oneBasedIndex - 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[oneBasedIndex - 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, CultureInfo.InvariantCulture);
                }
                if (polarity.Equals(Polarity.Unknown))
                {
                    PolarityDictionary.TryGetValue(cv.accession, out polarity);
                }
            }

            double rtInMinutes        = double.NaN;
            string scanFilter         = null;
            double?injectionTime      = null;
            int    oneBasedScanNumber = oneBasedIndex;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].scanList.scan[0].cvParam != null)
            {
                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].scanList.scan[0].cvParam)
                {
                    if (cv.accession.Equals(_retentionTime))
                    {
                        rtInMinutes = double.Parse(cv.value, CultureInfo.InvariantCulture);
                        if (cv.unitName == "second")
                        {
                            rtInMinutes /= 60;
                        }
                    }
                    if (cv.accession.Equals(_filterString))
                    {
                        scanFilter = cv.value;
                    }
                    if (cv.accession.Equals(_ionInjectionTime))
                    {
                        injectionTime = double.Parse(cv.value, CultureInfo.InvariantCulture);
                    }
                    if (cv.accession.Equals(_oneBasedScanNumber)) //get the real one based spectrum number (if available), the other assumes they are in order. This is present in .mgf->.mzml conversions from MSConvert
                    {
                        oneBasedScanNumber = int.Parse(cv.value);
                    }
                }
            }

            if (!msOrder.HasValue || !isCentroid.HasValue)
            {
                //one instance when this if statment is true (i.e. not false) is when there is no mz/intensity data
                //so, we return the MsDataScan object with a null spectrum
                //scans w/ null spectra are checked later and the scan numbers associated w those scans are returned to the reader.
                return(new MsDataScan(
                           null,
                           oneBasedScanNumber,
                           msOrder.Value,
                           false, //have to return a value here b/c it is not nullable
                           polarity,
                           rtInMinutes,
                           null,
                           scanFilter,
                           analyzer,
                           tic,
                           injectionTime,
                           null,
                           nativeId));
            }

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

            foreach (Generated.BinaryDataArrayType binaryData in _mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 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);
                }

                //in the futurem we may see scass w/ no data and there will be a crash here. if that happens, you can retrun an MsDataScan with null as the mzSpectrum
                //the scans with no spectra will be reported to the reader and left out of the scan list.
                double[] data = ConvertBase64ToDoubles(binaryData.binary, compressed, is32bit);
                if (mzArray)
                {
                    masses = data;
                }

                if (intensityArray)
                {
                    intensities = data;
                }
            }

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

            var aScanWindowList = _mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].scanList.scan[0].scanWindowList;

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

            //Remove Zero Intensity Peaks
            double zeroEquivalentIntensity = 0.01;
            int    zeroIntensityCount      = intensities.Count(i => i < zeroEquivalentIntensity);
            int    intensityValueCount     = intensities.Count();

            if (zeroIntensityCount > 0 && zeroIntensityCount < intensityValueCount)
            {
                Array.Sort(intensities, masses);
                double[] nonZeroIntensities = new double[intensityValueCount - zeroIntensityCount];
                double[] nonZeroMzs         = new double[intensityValueCount - zeroIntensityCount];
                intensities = intensities.SubArray(zeroIntensityCount, intensityValueCount - zeroIntensityCount);
                masses      = masses.SubArray(zeroIntensityCount, intensityValueCount - zeroIntensityCount);
                Array.Sort(masses, intensities);
            }

            if (filterParams != null && intensities.Length > 0 && ((filterParams.ApplyTrimmingToMs1 && msOrder.Value == 1) || (filterParams.ApplyTrimmingToMsMs && msOrder.Value > 1)))
            {
                WindowModeHelper(ref intensities, ref masses, filterParams, low, high);
            }

            Array.Sort(masses, intensities);
            var mzmlMzSpectrum = new MzSpectrum(masses, intensities, false);

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

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

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

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

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

            DissociationType dissociationType = DissociationType.Unknown;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].precursorList.precursor[0].activation.cvParam != null)
            {
                // for EThcD scans, the dissociation type will not be listed as EThcD. it will be 2 different dissociation types
                // in the list, one as ETD and one with HCD. so we need to check for that case and interpret it as EThcD.
                List <DissociationType> scanDissociationTypes = new List <DissociationType>();

                foreach (Generated.CVParamType cv in _mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].precursorList.precursor[0].activation.cvParam)
                {
                    if (DissociationDictionary.TryGetValue(cv.accession, out var scanDissociationType))
                    {
                        scanDissociationTypes.Add(scanDissociationType);
                    }
                }

                if (scanDissociationTypes.Contains(DissociationType.ETD) && scanDissociationTypes.Contains(DissociationType.HCD))
                {
                    dissociationType = DissociationType.EThcD;
                }
                else if (scanDissociationTypes.Any())
                {
                    dissociationType = scanDissociationTypes.First();
                }
                else
                {
                    dissociationType = DissociationType.Unknown;
                }
            }
            double?monoisotopicMz = null;

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

            int?precursorScanNumber;

            if (_mzMLConnection.run.spectrumList.spectrum[oneBasedIndex - 1].precursorList.precursor[0].spectrumRef == null)
            {
                precursorScanNumber = null;
            }
            else
            {
                precursorScanNumber = GetOneBasedPrecursorScanNumber(_mzMLConnection, oneBasedIndex);
            }

            return(new MsDataScan(
                       mzmlMzSpectrum,
                       oneBasedIndex,
                       msOrder.Value,
                       isCentroid.Value,
                       polarity,
                       rtInMinutes,
                       new MzRange(low, high),
                       scanFilter,
                       analyzer,
                       tic,
                       injectionTime,
                       null,
                       nativeId,
                       selectedIonMz,
                       selectedIonCharge,
                       selectedIonIntensity,
                       isolationMz,
                       lowIsolation + highIsolation,
                       dissociationType,
                       precursorScanNumber,
                       monoisotopicMz
                       ));
        }