Exemple #1
0
        public override MsDataScan GetOneBasedScan(int oneBasedScanNumber)
        {
            if (Scans[oneBasedScanNumber - 1] == null)
            {
                var representativeScanNumber = oneBasedScanNumber + (numScansToAverage - 1) / 2;
                var representative           = raw.GetOneBasedScan(representativeScanNumber);
                if (representative.MsnOrder != 1)
                {
                    throw new MzLibException("Scan " + representativeScanNumber + " is not MS1 scan");
                }
                int      msnOrder = 1;
                Polarity polarity = representative.Polarity;
                if (!representative.IsCentroid)
                {
                    throw new MzLibException("Scan " + representativeScanNumber + " is not centroid scan");
                }
                bool           isCentroid    = true;
                double         retentionTime = representative.RetentionTime;
                MZAnalyzerType mzAnalyzer    = representative.MzAnalyzer;

                MzSpectrum peaks = CombinePeaks(raw.GetAllScansList().Where(b => b.OneBasedScanNumber >= oneBasedScanNumber && b.OneBasedScanNumber <= oneBasedScanNumber + numScansToAverage - 1).Select(b => b.MassSpectrum).ToList(), ppmToleranceForPeakCombination);

                MzRange scanWindowRange = representative.ScanWindowRange;

                double totalIonCurrent = peaks.SumOfAllY;
                double injectionTime   = double.NaN;
                double[,] noiseData = null;

                Scans[oneBasedScanNumber - 1] = new MsDataScan(peaks, oneBasedScanNumber, msnOrder, isCentroid, polarity, retentionTime, scanWindowRange, null, mzAnalyzer, totalIonCurrent, injectionTime, noiseData, "scan=" + oneBasedScanNumber);
            }
            return(Scans[oneBasedScanNumber - 1]);
        }
Exemple #2
0
 public MsDataScan(MzSpectrum massSpectrum, int oneBasedScanNumber, int msnOrder, bool isCentroid, Polarity polarity, double retentionTime, MzRange scanWindowRange, string scanFilter, MZAnalyzerType mzAnalyzer,
                   double totalIonCurrent, double?injectionTime, double[,] noiseData, string nativeId, double?selectedIonMz = null, int?selectedIonChargeStateGuess = null, double?selectedIonIntensity = null, double?isolationMZ = null,
                   double?isolationWidth = null, DissociationType?dissociationType = null, int?oneBasedPrecursorScanNumber = null, double?selectedIonMonoisotopicGuessMz = null, string hcdEnergy = null)
 {
     OneBasedScanNumber             = oneBasedScanNumber;
     MsnOrder                       = msnOrder;
     IsCentroid                     = isCentroid;
     Polarity                       = polarity;
     RetentionTime                  = retentionTime;
     ScanWindowRange                = scanWindowRange;
     ScanFilter                     = scanFilter;
     MzAnalyzer                     = mzAnalyzer;
     TotalIonCurrent                = totalIonCurrent;
     InjectionTime                  = injectionTime;
     NoiseData                      = noiseData;
     MassSpectrum                   = massSpectrum;
     NativeId                       = nativeId;
     OneBasedPrecursorScanNumber    = oneBasedPrecursorScanNumber;
     IsolationMz                    = isolationMZ;
     IsolationWidth                 = isolationWidth;
     DissociationType               = dissociationType;
     SelectedIonMZ                  = selectedIonMz;
     SelectedIonIntensity           = selectedIonIntensity;
     SelectedIonChargeStateGuess    = selectedIonChargeStateGuess;
     SelectedIonMonoisotopicGuessMz = selectedIonMonoisotopicGuessMz;
     HcdEnergy                      = hcdEnergy;
 }
Exemple #3
0
 protected MsDataScan(int oneBasedScanNumber, int msnOrder, bool isCentroid, Polarity polarity, double retentionTime, MzRange scanWindowRange, string scanFilter, MZAnalyzerType mzAnalyzer, double totalIonCurrent, double?injectionTime, double[,] noiseData)
 {
     OneBasedScanNumber = oneBasedScanNumber;
     MsnOrder           = msnOrder;
     IsCentroid         = isCentroid;
     Polarity           = polarity;
     RetentionTime      = retentionTime;
     ScanWindowRange    = scanWindowRange;
     ScanFilter         = scanFilter;
     MzAnalyzer         = mzAnalyzer;
     TotalIonCurrent    = totalIonCurrent;
     InjectionTime      = injectionTime;
     NoiseData          = noiseData;
 }
Exemple #4
0
 public MsDataScan(TSpectrum massSpectrum, int oneBasedScanNumber, int msnOrder, bool isCentroid, Polarity polarity, double retentionTime, MzRange scanWindowRange, string scanFilter, MZAnalyzerType mzAnalyzer, double totalIonCurrent, double?injectionTime, double[,] noiseData, string nativeId)
 {
     OneBasedScanNumber = oneBasedScanNumber;
     MsnOrder           = msnOrder;
     IsCentroid         = isCentroid;
     Polarity           = polarity;
     RetentionTime      = retentionTime;
     ScanWindowRange    = scanWindowRange;
     ScanFilter         = scanFilter;
     MzAnalyzer         = mzAnalyzer;
     TotalIonCurrent    = totalIonCurrent;
     InjectionTime      = injectionTime;
     NoiseData          = noiseData;
     MassSpectrum       = massSpectrum;
     NativeId           = nativeId;
 }
Exemple #5
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
                       ));
        }
Exemple #6
0
        public override double GetResolution(int spectrumNumber)
        {
            int    arraySize = 0;
            object keys      = null;
            object values    = null;

            _rawConnection.GetTrailerExtraForScanNum(spectrumNumber, ref keys, ref values, ref arraySize);

            MZAnalyzerType analyzer   = GetMzAnalyzer(spectrumNumber);
            double         resolution = 0;

            switch (analyzer)
            {
            case MZAnalyzerType.FTICR:
            case MZAnalyzerType.Orbitrap:
                string name = GetInstrumentName();
                if (name == "Orbitrap Fusion" || name == "Q Exactive")
                {
                    object obj = GetExtraValue(spectrumNumber, "Orbitrap Resolution:");
                    resolution = Convert.ToDouble(obj);
                    if (resolution <= 0)
                    {
                        // Find first peak with S/N greater than 3 to use for resolution calculation
                        double[,] data = GetLabeledData(spectrumNumber);
                        int           totalPeaks    = data.GetLength(1);
                        List <double> avgResolution = new List <double>();

                        for (int i = 0; i < totalPeaks; i++)
                        {
                            double signalToNoise = data[1, i] / data[4, i];
                            if (signalToNoise >= 5)
                            {
                                double mz                  = data[0, i];
                                double peakRes             = data[2, i];
                                double correctedResolution = peakRes * Math.Sqrt(mz / 200);
                                avgResolution.Add(correctedResolution);
                            }
                        }

                        double meanResolution = avgResolution.Median();
                        if (meanResolution <= 25000)
                        {
                            return(15000);
                        }
                        if (meanResolution <= 45000)
                        {
                            return(30000);
                        }
                        if (meanResolution <= 100000)
                        {
                            return(60000);
                        }
                        if (meanResolution <= 200000)
                        {
                            return(120000);
                        }
                        if (meanResolution <= 400000)
                        {
                            return(240000);
                        }
                        return(450000);
                    }
                    return(resolution);
                }
                else
                {
                    object obj = GetExtraValue(spectrumNumber, "FT Resolution:");
                    resolution = Convert.ToDouble(obj);
                    if (resolution > 300000)
                    {
                        return(480000);
                    }
                    return(resolution);
                }
            }
            return(resolution);
        }
Exemple #7
0
        protected MsDataScanWithPrecursor(TSpectrum massSpectrum, int ScanNumber, int MsnOrder, bool isCentroid, Polarity Polarity, double RetentionTime, MzRange MzRange, string ScanFilter, MZAnalyzerType MzAnalyzer, double TotalIonCurrent, double selectedIonMZ, int?selectedIonChargeStateGuess, double?selectedIonIntensity, double?isolationMZ, double?isolationWidth, DissociationType dissociationType, int?oneBasedPrecursorScanNumber, double?selectedIonMonoisotopicGuessMz, double?injectionTime, double[,] noiseData, string nativeId)
            : base(massSpectrum, ScanNumber, MsnOrder, isCentroid, Polarity, RetentionTime, MzRange, ScanFilter, MzAnalyzer, TotalIonCurrent, injectionTime, noiseData, nativeId)
        {
            this.OneBasedPrecursorScanNumber = oneBasedPrecursorScanNumber;

            this.IsolationMz    = isolationMZ;
            this.IsolationWidth = isolationWidth;

            this.DissociationType = dissociationType;

            this.SelectedIonMZ                  = selectedIonMZ;
            this.SelectedIonIntensity           = selectedIonIntensity;
            this.SelectedIonChargeStateGuess    = selectedIonChargeStateGuess;
            this.SelectedIonMonoisotopicGuessMz = selectedIonMonoisotopicGuessMz;
        }
Exemple #8
0
        public MzmlScan(int oneBasedScanNumber, MzmlMzSpectrum massSpectrum, int msnOrder, bool isCentroid, Polarity polarity, double retentionTime, MzRange scanWindowRange, string scanFilter, MZAnalyzerType mzAnalyzer, double totalIonCurrent, double?injectionTime)
            : base(oneBasedScanNumber, msnOrder, isCentroid, polarity, retentionTime, scanWindowRange, scanFilter, mzAnalyzer, totalIonCurrent, injectionTime, null)

        {
            this.MassSpectrum = massSpectrum;
        }
Exemple #9
0
        /// <summary>
        /// Gets the scan with the specified one-based scan number.
        /// </summary>
        public override MsDataScan GetOneBasedScanFromDynamicConnection(int oneBasedScanNumber, IFilteringParams filterParams = null)
        {
            MsDataScan scan = null;

            if (ScanNumberToByteOffset.TryGetValue(oneBasedScanNumber, out long byteOffset))
            {
                // seek to the byte of the scan
                reader.BaseStream.Position = byteOffset;
                reader.DiscardBufferedData();

                // DO NOT USE THIS METHOD! it does not seek reliably
                //stream.BaseStream.Seek(byteOffset, SeekOrigin.Begin);

                // read the scan
                using (XmlReader xmlReader = XmlReader.Create(reader))
                {
                    string nativeId = null;
                    while (xmlReader.Read())
                    {
                        // this skips whitespace
                        string upperName = xmlReader.Name.ToUpper();
                        if (upperName == "SPECTRUM" && xmlReader.IsStartElement())
                        {
                            nativeId = xmlReader["id"];
                            break;
                        }
                    }

                    // deserializing the scan's data doesn't work well. the spectrum type is deserialized
                    // but sub-elements aren't. this is probably because we're trying to deserialize only
                    // a part of the XML file... deserialization would probably be cleaner code than
                    // using the current implementation but I couldn't get it to work
                    //var deserializedSpectrum = (IO.MzML.Generated.SpectrumType)serializer.Deserialize(xmlReader.ReadSubtree());

                    MzSpectrum     spectrum       = null;
                    int?           msOrder        = 0;
                    bool?          isCentroid     = false;
                    Polarity       polarity       = Polarity.Unknown;
                    double         retentionTime  = double.NaN;
                    MzRange        range          = null;
                    string         scanFilter     = null;
                    MZAnalyzerType mzAnalyzerType = MZAnalyzerType.Unknown;
                    double         tic            = 0;
                    double?        injTime        = null;
                    double[,] noiseData = null; // TODO: read this
                    double?          selectedIonMz                  = null;
                    int?             selectedCharge                 = null;
                    double?          selectedIonIntensity           = null;
                    double?          isolationMz                    = null; // TODO: should this be refined? or taken from the scan header?
                    double?          isolationWidth                 = null;
                    DissociationType?dissociationType               = null;
                    int?             oneBasedPrecursorScanNumber    = null;
                    double?          selectedIonMonoisotopicGuessMz = null;

                    double scanLowerLimit             = double.NaN;
                    double scanUpperLimit             = double.NaN;
                    double isolationWindowLowerOffset = double.NaN;
                    double isolationWindowUpperOffset = double.NaN;

                    bool     compressed         = false;
                    bool     readingMzs         = false;
                    bool     readingIntensities = false;
                    bool     is32bit            = true;
                    double[] mzs         = null;
                    double[] intensities = null;

                    while (xmlReader.Read())
                    {
                        switch (xmlReader.Name.ToUpper())
                        {
                        // controlled vocabulary parameter
                        case "CVPARAM":
                            string cvParamAccession = xmlReader["accession"];

                            if (Mzml.DissociationDictionary.ContainsKey(cvParamAccession))
                            {
                                dissociationType = Mzml.DissociationDictionary[cvParamAccession];
                                break;
                            }

                            if (Mzml.PolarityDictionary.ContainsKey(cvParamAccession))
                            {
                                polarity = Mzml.PolarityDictionary[cvParamAccession];
                                break;
                            }

                            switch (cvParamAccession)
                            {
                            // MS order
                            case "MS:1000511":
                                msOrder = int.Parse(xmlReader["value"]);
                                break;

                            // centroid mode
                            case "MS:1000127":
                                isCentroid = true;
                                break;

                            // profile mode
                            case "MS:1000128":
                                isCentroid = false;
                                throw new MzLibException("Reading profile mode mzmls not supported");
                                break;

                            // total ion current
                            case "MS:1000285":
                                tic = double.Parse(xmlReader["value"]);
                                break;

                            // retention time
                            case "MS:1000016":
                                retentionTime = double.Parse(xmlReader["value"]);
                                break;

                            // filter string
                            case "MS:1000512":
                                scanFilter = xmlReader["value"];
                                break;

                            // ion injection time
                            case "MS:1000927":
                                injTime = double.Parse(xmlReader["value"]);
                                break;

                            // scan lower limit
                            case "MS:1000501":
                                scanLowerLimit = double.Parse(xmlReader["value"]);
                                break;

                            // scan upper limit
                            case "MS:1000500":
                                scanUpperLimit = double.Parse(xmlReader["value"]);
                                break;

                            // isolation window lower offset
                            case "MS:1000828":
                                isolationWindowLowerOffset = double.Parse(xmlReader["value"]);
                                break;

                            // isolation window upper offset
                            case "MS:1000829":
                                isolationWindowUpperOffset = double.Parse(xmlReader["value"]);
                                break;

                            // isolated m/z
                            case "MS:1000827":
                                isolationMz = double.Parse(xmlReader["value"]);
                                break;

                            // selected ion m/z
                            case "MS:1000744":
                                selectedIonMz = double.Parse(xmlReader["value"]);
                                break;

                            // selected charge state
                            case "MS:1000041":
                                selectedCharge = int.Parse(xmlReader["value"]);
                                break;

                            // selected intensity
                            case "MS:1000042":
                                selectedIonIntensity = double.Parse(xmlReader["value"]);
                                break;

                            // mass analyzer types
                            case "MS:1000081":
                                mzAnalyzerType = MZAnalyzerType.Quadrupole;
                                break;

                            case "MS:1000291":
                                mzAnalyzerType = MZAnalyzerType.IonTrap2D;
                                break;

                            case "MS:1000082":
                                mzAnalyzerType = MZAnalyzerType.IonTrap3D;
                                break;

                            case "MS:1000484":
                                mzAnalyzerType = MZAnalyzerType.Orbitrap;
                                break;

                            case "MS:1000084":
                                mzAnalyzerType = MZAnalyzerType.TOF;
                                break;

                            case "MS:1000079":
                                mzAnalyzerType = MZAnalyzerType.FTICR;
                                break;

                            case "MS:1000080":
                                mzAnalyzerType = MZAnalyzerType.Sector;
                                break;

                            case "MS:1000523":
                                is32bit = false;
                                break;

                            case "MS:1000574":
                                compressed = true;
                                break;

                            case "MS:1000514":
                                readingMzs = true;
                                break;

                            case "MS:1000515":
                                readingIntensities = true;
                                break;
                            }
                            break;

                        // binary data array (e.g., m/z or intensity array)
                        case "BINARY":
                            if (!readingMzs && !readingIntensities)
                            {
                                break;
                            }

                            while (string.IsNullOrWhiteSpace(xmlReader.Value))
                            {
                                xmlReader.Read();
                            }

                            string binaryString = xmlReader.Value;

                            byte[] binaryData = null;

                            if (!is32bit)
                            {
                                binaryData = Convert.FromBase64String(binaryString);
                            }
                            else
                            {
                                // todo: check. not sure if this is right
                                binaryData = Encoding.UTF8.GetBytes(binaryString);
                            }

                            double[] data = Mzml.ConvertBase64ToDoubles(binaryData, compressed, is32bit);

                            if (readingMzs)
                            {
                                mzs        = data;
                                readingMzs = false;
                            }
                            else if (readingIntensities)
                            {
                                intensities        = data;
                                readingIntensities = false;
                            }

                            break;

                        case "PRECURSOR":
                            if (xmlReader.IsStartElement())
                            {
                                string precursorScanInfo = xmlReader["spectrumRef"];

                                if (precursorScanInfo != null)
                                {
                                    oneBasedPrecursorScanNumber = NativeIdToScanNumber[precursorScanInfo];
                                }
                            }
                            break;

                        case "USERPARAM":
                            if (xmlReader.IsStartElement() && xmlReader["name"] != null && xmlReader["name"] == "[mzLib]Monoisotopic M/Z:")
                            {
                                selectedIonMonoisotopicGuessMz = double.Parse(xmlReader["value"]);
                            }
                            break;

                        // done reading spectrum
                        case "SPECTRUM":
                            if (!xmlReader.IsStartElement())
                            {
                                if (msOrder > 1)
                                {
                                    isolationWidth = isolationWindowUpperOffset + isolationWindowLowerOffset;

                                    if (dissociationType == null)
                                    {
                                        dissociationType = DissociationType.Unknown;
                                    }
                                }

                                if (!msOrder.HasValue || !isCentroid.HasValue)
                                {
                                    throw new MzLibException("Could not determine the MS order or centroid/profile status");
                                }

                                // peak filtering
                                if (filterParams != null && intensities.Length > 0 &&
                                    ((filterParams.ApplyTrimmingToMs1 && msOrder.Value == 1) || (filterParams.ApplyTrimmingToMsMs && msOrder.Value > 1)))
                                {
                                    MsDataFile.WindowModeHelper(ref intensities, ref mzs, filterParams, scanLowerLimit, scanUpperLimit);
                                }

                                Array.Sort(mzs, intensities);

                                range    = new MzRange(scanLowerLimit, scanUpperLimit);
                                spectrum = new MzSpectrum(mzs, intensities, false);

                                scan = new MsDataScan(spectrum, oneBasedScanNumber, msOrder.Value, isCentroid.Value, polarity,
                                                      retentionTime, range, scanFilter, mzAnalyzerType, tic, injTime, noiseData,
                                                      nativeId, selectedIonMz, selectedCharge, selectedIonIntensity, isolationMz, isolationWidth,
                                                      dissociationType, oneBasedPrecursorScanNumber, selectedIonMonoisotopicGuessMz);

                                return(scan);
                            }
                            else
                            {
                                throw new MzLibException("Spectrum data is malformed");
                            }
                        }
                    }
                }
            }

            return(scan);
        }
Exemple #10
0
 public MzmlScanWithPrecursor(int ScanNumber, MzmlMzSpectrum massSpectrum, int MsnOrder, bool isCentroid, Polarity Polarity, double RetentionTime, MzRange MzRange, string ScanFilter, MZAnalyzerType MzAnalyzer, double TotalIonCurrent, double selectedIonMz, int?selectedIonChargeStateGuess, double?selectedIonIntensity, double isolationMZ, double?isolationWidth, DissociationType dissociationType, int oneBasedPrecursorScanNumber, double?selectedIonGuessMonoisotopicMZ, double?injectionTime)
     : base(ScanNumber, MsnOrder, isCentroid, Polarity, RetentionTime, MzRange, ScanFilter, MzAnalyzer, TotalIonCurrent, selectedIonMz, selectedIonChargeStateGuess, selectedIonIntensity, isolationMZ, isolationWidth, dissociationType, oneBasedPrecursorScanNumber, selectedIonGuessMonoisotopicMZ, injectionTime, null)
 {
     this.MassSpectrum = massSpectrum;
 }
Exemple #11
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
                       ));
        }
Exemple #12
0
        public ThermoScan(int oneBasedScanNumber, ThermoSpectrum massSpectrum, int msnOrder, Polarity polarity, double retentionTime, MzRange scanWindowRange, string scanFilter, MZAnalyzerType mzAnalyzer, double totalIonCurrent, double?injectionTime, double[,] noiseData, string nativeId)
            : base(massSpectrum, oneBasedScanNumber, msnOrder, true, polarity, retentionTime, scanWindowRange, scanFilter, mzAnalyzer, totalIonCurrent, injectionTime, noiseData, nativeId)

        {
        }
Exemple #13
0
 public ThermoScanWithPrecursor(int ScanNumber, ThermoSpectrum massSpectrum, int MsnOrder, Polarity Polarity, double RetentionTime, MzRange MzRange, string ScanFilter, MZAnalyzerType MzAnalyzer, double TotalIonCurrent, double selectedIonGuessMZ, int?selectedIonGuessChargeStateGuess, double?isolationWidth, DissociationType dissociationType, int?oneBasedPrecursorScanNumber, double?selectedIonGuessMonoisotopicMZ, double?injectionTime, double[,] noiseData, string nativeId)
     : base(massSpectrum, ScanNumber, MsnOrder, true, Polarity, RetentionTime, MzRange, ScanFilter, MzAnalyzer, TotalIonCurrent, selectedIonGuessMZ, selectedIonGuessChargeStateGuess, null, selectedIonGuessMZ, isolationWidth, dissociationType, oneBasedPrecursorScanNumber, selectedIonGuessMonoisotopicMZ, injectionTime, noiseData, nativeId)
 {
 }