Exemple #1
0
        public override double GetPrecusorMz(int spectrumNumber, int msnOrder = 2)
        {
            object labels_obj = null;
            object values_obj = null;
            int    array_size = -1;

            _rawConnection.GetTrailerExtraForScanNum(spectrumNumber, ref labels_obj, ref values_obj, ref array_size);
            string[] labels = (string[])labels_obj;
            string[] values = (string[])values_obj;
            for (int i = labels.GetLowerBound(0); i <= labels.GetUpperBound(0); i++)
            {
                if (labels[i].StartsWith("Monoisotopic M/Z"))
                {
                    double monoisotopic_mz = double.Parse(values[i], CultureInfo.InvariantCulture);
                    if (monoisotopic_mz > 0.0)
                    {
                        return(monoisotopic_mz);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //double mz = double.NaN;
            //_rawConnection.GetPrecursorMassForScanNum(spectrumNumber, msnOrder, ref mz);
            //return mz;
            string scanFilter = GetScanFilter(spectrumNumber);

            return(GetIsolationMZ(scanFilter));
        }
Exemple #2
0
        private double GetPrecursorMonoisotopicMZfromTrailierExtra(int scanNumber)
        {
            object labels_obj = null;
            object values_obj = null;
            int    array_size = -1;

            _rawConnection.GetTrailerExtraForScanNum(scanNumber, ref labels_obj, ref values_obj, ref array_size);
            string[] labels = (string[])labels_obj;
            string[] values = (string[])values_obj;
            for (int i = labels.GetLowerBound(0); i <= labels.GetUpperBound(0); i++)
            {
                if (labels[i].StartsWith("Monoisotopic M/Z"))
                {
                    double monoisotopic_mz = double.Parse(values[i], CultureInfo.InvariantCulture);
                    if (monoisotopic_mz > 0.0)
                    {
                        return(monoisotopic_mz);
                    }
                    else
                    {
                        return(-1);
                    }
                }
            }
            return(-1);
        }
Exemple #3
0
        /// <summary>
        /// Reads raw data. Puts spectra into a thread safe queue. Closes queue when done. Parses the rawfile-log into the statusLog object
        /// </summary>
        /// <param name="raw">Raw file</param>
        /// <param name="rawScanQueue">Thread safe queue</param>
        private void ReadRawFile(IXRawfile5 raw, BlockingCollection <RawScan> rawScanQueue)
        {
            int firstScanNumber = -1;

            raw.GetFirstSpectrumNumber(ref firstScanNumber);
            int lastScanNumber = -1;

            raw.GetLastSpectrumNumber(ref lastScanNumber);

            for (int scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
            {
                string scanFilter = null;
                raw.GetFilterForScanNum(scanNumber, ref scanFilter);

                //read scan header into key/value arrays
                object keyRef   = null;
                object valueRef = null;
                int    indexRef = 0;
                raw.GetTrailerExtraForScanNum(scanNumber, ref keyRef, ref valueRef, ref indexRef);
                string[] trailerKeys   = (string[])keyRef;
                string[] trailerValues = (string[])valueRef;

                //read tic and base peak mass using dummy variables
                double tic          = 0.0;
                int    int1         = 0;
                int    int2         = 0;
                int    int3         = 0;
                double double1      = 0.0;
                double double2      = 0.0;
                double double3      = 0.0;
                double basePeakMass = 0.0;
                double double5      = 0.0;
                double double6      = 0.0;
                raw.GetScanHeaderInfoForScanNum(scanNumber, ref int1, ref double1, ref double2, ref double3, ref tic, ref basePeakMass, ref double5, ref int2, ref int3, ref double6);

                //read data (mz,int,...) using dummy variables
                object labels_obj = null;
                object flags_obj  = null;
                raw.GetLabelData(ref labels_obj, ref flags_obj, ref scanNumber);
                double[,] data = (double[, ])labels_obj;

                //read precursorMz
                double precursorMz = 0.0;
                raw.GetPrecursorMassForScanNum(scanNumber, 2, ref precursorMz);

                RawScan rawScan = new RawScan(scanNumber, scanFilter, tic, trailerKeys, trailerValues, data, precursorMz, basePeakMass);
                rawScanQueue.Add(rawScan);

                //read status log
                double rtLog           = 0.0;
                object statuslogKeys   = null;
                object statuslogValues = null;
                int    statuslogN      = 0;
                raw.GetStatusLogForScanNum(scanNumber, ref rtLog, ref statuslogKeys, ref statuslogValues, ref statuslogN);
                statusLog.Add(rtLog, (string[])statuslogKeys, (string[])statuslogValues);
            }
            rawScanQueue.CompleteAdding();
            Log.Information("Read all spectra in base module");
        }
        public string[,] GetScanHeaderData(int spectrumNumber)
        {
            object pvarLabels  = null;
            object pvarValues  = null;
            int    pnArraySize = -1;

            _rawConnection.GetTrailerExtraForScanNum(spectrumNumber, ref pvarLabels, ref pvarValues, ref pnArraySize);

            string[,] returnArray = new string[2, pnArraySize];

            for (int i = 0; i < pnArraySize; i++)
            {
                returnArray[0, i] = ((string[])pvarLabels)[i];
                returnArray[1, i] = ((string[])pvarValues)[i];
            }

            return(returnArray);
        }
Exemple #5
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 #6
0
        public static MsDataScan GetMsDataOneBasedScanFromThermoFile(IXRawfile5 theConnection, int nScanNumber, ThermoGlobalParams globalParams, IFilteringParams filterParams = null)
        {
            int    pnNumPackets        = 0;
            double pdLowMass           = 0;
            double pdHighMass          = 0;
            double pdTIC               = 0;
            double pdBasePeakMass      = 0;
            double pdBasePeakIntensity = 0;
            int    pnNumChannels       = 0;
            int    pbUniformTime       = 0;
            double pdFrequency         = 0;
            double pdStartTime         = 0;

            theConnection.GetScanHeaderInfoForScanNum(nScanNumber, ref pnNumPackets, ref pdStartTime, ref pdLowMass, ref pdHighMass, ref pdTIC, ref pdBasePeakMass, ref pdBasePeakIntensity, ref pnNumChannels, ref pbUniformTime, ref pdFrequency);

            double?ms2isolationWidthFromTrailerExtra        = null;
            double?injectionTimeFromTrailerExtra            = null;
            double?precursorMonoisotopicMZfromTrailierExtra = null;
            int?   chargeStatefromTrailierExtra             = null;
            int?   masterScanfromTrailierExtra = null;

            object pvarValues  = null;
            object pvarLables  = null;
            int    pnArraySize = 0;

            theConnection.GetTrailerExtraForScanNum(nScanNumber, ref pvarLables, ref pvarValues, ref pnArraySize);

            string[] labels = (string[])pvarLables;
            string[] values = (string[])pvarValues;
            for (int i = labels.GetLowerBound(0); i <= labels.GetUpperBound(0); i++)
            {
                if (labels[i].StartsWith("MS2 Isolation Width", StringComparison.Ordinal))
                {
                    ms2isolationWidthFromTrailerExtra = double.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                        (double?)null :
                                                        double.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Ion Injection Time (ms)", StringComparison.Ordinal))
                {
                    injectionTimeFromTrailerExtra = double.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                    (double?)null :
                                                    double.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Monoisotopic M/Z", StringComparison.Ordinal))
                {
                    precursorMonoisotopicMZfromTrailierExtra = double.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                               (double?)null :
                                                               double.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Charge State", StringComparison.Ordinal))
                {
                    chargeStatefromTrailierExtra = int.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                   (int?)null :
                                                   int.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Master Scan Number", StringComparison.Ordinal))
                {
                    masterScanfromTrailierExtra = int.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                  (int?)null :
                                                  int.Parse(values[i], CultureInfo.InvariantCulture);
                }
            }

            string pbstrFilter = null;

            theConnection.GetFilterForScanNum(nScanNumber, ref pbstrFilter);

            int pnMSOrder = 0;

            theConnection.GetMSOrderForScanNum(nScanNumber, ref pnMSOrder);

            int pnMassAnalyzerType = 0;

            theConnection.GetMassAnalyzerTypeForScanNum(nScanNumber, ref pnMassAnalyzerType);

            object pvarNoisePacket = null;

            try //if there is no noise data
            {
                theConnection.GetNoiseData(ref pvarNoisePacket, nScanNumber);
            }
            catch
            {
                //pvarNoisePAcket is already null
            }
            double[,] noiseData = pvarNoisePacket as double[, ];

            double[,] data;
            try
            {
                object pvarFlags  = null;
                object pvarLabels = null;
                theConnection.GetLabelData(ref pvarLabels, ref pvarFlags, ref nScanNumber);
                data = pvarLabels as double[, ];
                if (data == null || data.Length == 0)
                {
                    throw new MzLibException("For spectrum number " + nScanNumber + " the data is null!");
                }
            }
            catch (MzLibException)
            {
                // Warning: the masses reported by GetMassListFromScanNum when centroiding are not properly calibrated and thus could be off by 0.3 m/z or more

                double pdCentroidPeakWidth = 0;
                object pvarnMassList       = null;
                object pvarPeakFlags       = null;
                theConnection.GetMassListFromScanNum(ref nScanNumber, null, 0, 0, 0, 1, ref pdCentroidPeakWidth, ref pvarnMassList, ref pvarPeakFlags, ref pnArraySize);
                data = (double[, ])pvarnMassList;
            }

            MzSpectrum thermoSpectrum;

            if (filterParams != null && data.GetLength(1) > 0 && (filterParams.MinimumAllowedIntensityRatioToBasePeakM.HasValue || filterParams.NumberOfPeaksToKeepPerWindow.HasValue) && ((filterParams.ApplyTrimmingToMs1 && pnMSOrder == 1) || (filterParams.ApplyTrimmingToMsMs && pnMSOrder > 1)))
            {
                var count = data.GetLength(1);

                var mzArray        = new double[count];
                var intensityArray = new double[count];
                Buffer.BlockCopy(data, 0, mzArray, 0, sizeof(double) * count);
                Buffer.BlockCopy(data, sizeof(double) * count, intensityArray, 0, sizeof(double) * count);
                if (filterParams.NumberOfWindows == null)
                {
                    int numPeaks = TopNpeakHelper(ref intensityArray, ref mzArray, filterParams);
                    //the following arrays are modified after TopN helper
                    Array.Resize(ref intensityArray, numPeaks);
                    Array.Resize(ref mzArray, numPeaks);
                }
                //Array reference passed by value, array calues will be modified after calling
                else
                {
                    WindowModeHelper(ref intensityArray, ref mzArray, filterParams);
                }
                Array.Sort(mzArray, intensityArray);
                thermoSpectrum = new MzSpectrum(mzArray, intensityArray, false);
            }
            else
            {
                thermoSpectrum = new MzSpectrum(data);
            }
            MZAnalyzerType mzAnalyzerType;

            if ((ThermoMzAnalyzer)pnMassAnalyzerType == ThermoMzAnalyzer.FTMS)
            {
                mzAnalyzerType = MZAnalyzerType.Orbitrap;
            }
            else
            {
                mzAnalyzerType = MZAnalyzerType.Unknown;
            }
            string nativeId = "controllerType=0 controllerNumber=1 scan=" + nScanNumber;

            if (pnMSOrder > 1)
            {
                int pnActivationType = 0;
                theConnection.GetActivationTypeForScanNum(nScanNumber, pnMSOrder, ref pnActivationType);

                // INITIALIZE globalParams.couldBePrecursor[nScanNumber - 1] (for dynamic connections that don't have it initialized yet)
                if (globalParams.CouldBePrecursor[nScanNumber - 1].Equals(default(ManagedThermoHelperLayer.PrecursorInfo)))
                {
                    var ok = new ManagedThermoHelperLayer.HelperClass();
                    globalParams.CouldBePrecursor[nScanNumber - 1] = ok.GetSingleScanPrecursorInfo(nScanNumber, globalParams.FilePath);
                }

                var precursorInfo = globalParams.CouldBePrecursor[nScanNumber - 1];

                // THIS METHOD IS BUGGY!!! DO NOT USE
                //theConnection.FindPrecursorMassInFullScan(nScanNumber, ref pnMasterScan, ref pdFoundMass, ref pdHeaderMass, ref pnChargeState);

                int oneBasedPrecursorScanNumber = -1;
                if (precursorInfo.nScanNumber > 0)
                {
                    oneBasedPrecursorScanNumber = precursorInfo.nScanNumber;
                }
                else if (masterScanfromTrailierExtra.HasValue && masterScanfromTrailierExtra > 0)
                {
                    oneBasedPrecursorScanNumber = masterScanfromTrailierExtra.Value;
                }
                else
                {
                    // we weren't able to get the precursor scan number, so we'll have to guess;
                    // loop back to find precursor scan
                    // (assumed to be the first scan before this scan with an MS order of this scan's MS order - 1)
                    // e.g., if this is an MS2 scan, find the first MS1 scan before this and assume that's the precursor scan
                    int scanOrder          = globalParams.MsOrderByScan[nScanNumber - 1];
                    int precursorScanOrder = scanOrder - 1;

                    for (int i = nScanNumber - 1; i >= 0; i--)
                    {
                        int msOrder = globalParams.MsOrderByScan[i];

                        if (msOrder == precursorScanOrder)
                        {
                            oneBasedPrecursorScanNumber = i + 1;
                            break;
                        }
                    }
                }
                if (oneBasedPrecursorScanNumber == -1)
                {
                    throw new MzLibException("Could not find precursor info for scan #" + nScanNumber);
                }

                int?selectedIonGuessChargeStateGuess = null;
                if (precursorInfo.nChargeState > 0)
                {
                    selectedIonGuessChargeStateGuess = precursorInfo.nChargeState;
                }
                else if (chargeStatefromTrailierExtra.HasValue)
                {
                    selectedIonGuessChargeStateGuess = chargeStatefromTrailierExtra;
                }

                double?selectedIonGuessMonoisotopicMz = null;
                if (precursorMonoisotopicMZfromTrailierExtra.HasValue && precursorMonoisotopicMZfromTrailierExtra.Value > 0)
                {
                    selectedIonGuessMonoisotopicMz = precursorMonoisotopicMZfromTrailierExtra;
                }
                if (precursorInfo.dMonoIsoMass > 0 && !selectedIonGuessMonoisotopicMz.HasValue)
                {
                    selectedIonGuessMonoisotopicMz = precursorInfo.dMonoIsoMass;
                }

                Regex matcher;
                if (pbstrFilter.ToLower().Contains("msx"))
                {
                    matcher = mFindParentIonOnlyMsx;
                }
                else
                {
                    matcher = mFindParentIonOnlyNonMsx;
                }
                double selectedIonGuessMZ = double.Parse(matcher.Match(pbstrFilter).Groups["ParentMZ"].Value);

                //   int? selectedIonChargeStateGuess, double? selectedIonIntensity, double? isolationMZ, double? isolationWidth, DissociationType dissociationType, int? oneBasedPrecursorScanNumber, double? selectedIonMonoisotopicGuessMz, double? injectionTime, double[,] noiseData, string nativeId)
                // double TotalIonCurrent, double selectedIonMZ, int? selectedIonChargeStateGuess, double? selectedIonIntensity, double? isolationMZ, double? isolationWidth, DissociationType dissociationType, int? oneBasedPrecursorScanNumber, double? selectedIonMonoisotopicGuessMz, double? injectionTime, double[,] noiseData, string nativeId)
                return(new MsDataScan(
                           thermoSpectrum,
                           nScanNumber,
                           pnMSOrder,
                           true,
                           PolarityRegex.IsMatch(pbstrFilter) ? Polarity.Positive : Polarity.Negative,
                           pdStartTime,
                           new MzRange(pdLowMass, pdHighMass),
                           pbstrFilter,
                           mzAnalyzerType,
                           pdTIC,
                           injectionTimeFromTrailerExtra,
                           noiseData,
                           nativeId,
                           selectedIonGuessMZ,
                           selectedIonGuessChargeStateGuess,
                           null,
                           selectedIonGuessMZ,
                           ms2isolationWidthFromTrailerExtra,
                           (DissociationType)pnActivationType,
                           oneBasedPrecursorScanNumber,
                           selectedIonGuessMonoisotopicMz
                           ));
            }
            else
            {
                return(new MsDataScan(
                           thermoSpectrum,
                           nScanNumber,
                           1,
                           true,
                           PolarityRegex.IsMatch(pbstrFilter) ? Polarity.Positive : Polarity.Negative,
                           pdStartTime,
                           new MzRange(pdLowMass, pdHighMass),
                           pbstrFilter,
                           mzAnalyzerType,
                           pdTIC,
                           injectionTimeFromTrailerExtra,
                           noiseData,
                           nativeId));
            }
        }
        private MassSpectrum GetSpectrumByScanNum(int scanNum)
        {
            //if (scanNum >= 24021 && scanNum <= 24021)
            //{
            //    return null;
            //}
            string scanFilter = null;

            _rawReader.GetFilterForScanNum(scanNum, ref scanFilter);

            // get scan type;
            int nScanType = 0;

            _rawReader.GetScanTypeForScanNum(scanNum, ref nScanType);

            // get scan header information of the spectrum;
            int    numPackets = 0, numChannels = 0, uniformTime = 0;
            double startTime = 0, lowMass = 0, highMass = 0, tic = 0, basePeakMass = 0, basePeakIntensity = 0, frequency = 0;

            _rawReader.GetScanHeaderInfoForScanNum(scanNum, ref numPackets, ref startTime, ref lowMass, ref highMass,
                                                   ref tic, ref basePeakMass, ref basePeakIntensity, ref numChannels, ref uniformTime, ref frequency);

            // get the instrument type;
            InstrumentType instrumentType = InstrumentType.ELSE;

            if (scanFilter.StartsWith("FTMS"))
            {
                instrumentType = InstrumentType.FTMS;
            }
            else if (scanFilter.StartsWith("ITMS"))
            {
                instrumentType = InstrumentType.ITMS;
            }

            // get MS spectrum list;
            string spectrumId = "controllerType=0 controllerNumber=1 scan=" + scanNum.ToString();

            // get the retention time;
            double retentionTime = double.NaN;

            _rawReader.RTFromScanNum(scanNum, ref retentionTime);

            // get the ion injection time and the precursor information;
            object trailerLabelsObj   = null;
            object trailerValuesObj   = null;
            int    trailer_array_size = -1;

            _rawReader.GetTrailerExtraForScanNum(scanNum, ref trailerLabelsObj, ref trailerValuesObj, ref trailer_array_size);
            string[] trailerLabels = (string[])trailerLabelsObj;
            string[] trailerValues = (string[])trailerValuesObj;

            double ionInjectionTime    = 0;
            double precMz              = 0;
            int    precZ               = 0;
            int    precScanNum         = 0;
            double isolationWindowSize = 0;

            for (int trailerIdx = trailerLabels.GetLowerBound(0); trailerIdx <= trailerLabels.GetUpperBound(0); trailerIdx++)
            {
                if (trailerLabels[trailerIdx].StartsWith("Ion Injection Time"))
                {
                    ionInjectionTime = double.Parse(trailerValues[trailerIdx]);
                }
                if (trailerLabels[trailerIdx].StartsWith("Monoisotopic M/Z"))
                {
                    precMz = double.Parse(trailerValues[trailerIdx]);
                }
                if (trailerLabels[trailerIdx].StartsWith("Charge State"))
                {
                    precZ = int.Parse(trailerValues[trailerIdx]);
                }
                if (trailerLabels[trailerIdx].StartsWith("Master Scan Number"))
                {
                    precScanNum = int.Parse(trailerValues[trailerIdx]);
                }
                if (trailerLabels[trailerIdx].StartsWith("MS2 Isolation Width"))
                {
                    isolationWindowSize = double.Parse(trailerValues[trailerIdx]);
                }
            }

            // get the analyzer temperature from the status log;
            object logLabelsObj = null;
            object logValuesObj = null;
            int    pnArraySize  = 0;

            _rawReader.GetStatusLogForScanNum(scanNum, 1.0, ref logLabelsObj, ref logValuesObj, ref pnArraySize);
            double analyzerTemperature = -1.0;

            if (logLabelsObj != null && logValuesObj != null)
            {
                string[] logLabels = (string[])logLabelsObj;
                string[] logValues = (string[])logValuesObj;

                for (int idx = logLabels.GetLowerBound(0); idx <= logLabels.GetUpperBound(0); idx++)
                {
                    if (logLabels[idx].Contains("FT Analyzer Temp"))
                    {
                        analyzerTemperature = double.Parse(logValues[idx]);
                    }

                    if (logLabels[idx].Contains("Analyzer Temperature"))
                    {
                        analyzerTemperature = double.Parse(logValues[idx]);
                    }
                }
            }

            // create an instance of MassSpectrum;
            List <Ion>   peaks    = null;
            MassSpectrum spectrum = new MassSpectrum(
                (int)scanNum,
                spectrumId,
                retentionTime,
                peaks,
                ionInjectionTime,
                instrumentType,
                scanFilter,
                analyzerTemperature,
                correctPrecMz);

            // check the MS level;
            if (scanFilter.Contains(" ms "))
            {
                spectrum.MsLevel = 1;

                // do not filter peaks for MS1 as all peaks may be needed for later masss/charge correction;
                spectrum.Peaks = GetPeaks(scanNum, scanFilter, false);
            }
            else if (scanFilter.Contains(" ms2 "))
            {
                spectrum.MsLevel = 2;
                int activationType = 0;
                //int pnNumMSOrders = 0;
                _rawReader.GetActivationTypeForScanNum(scanNum, spectrum.MsLevel, ref activationType);
                //_rawReader.GetNumberOfMSOrdersFromScanNum(scanNum, ref pnNumMSOrders);
                //_rawReader.GetIsolationWidthForScanNum(scanNum, pnNumMSOrders, ref isolationWindowSize);
                spectrum.IsolationWindowSize = isolationWindowSize;
                spectrum.ActivationMethod    = (Activation)activationType;
                spectrum.Peaks               = GetPeaks(scanNum, scanFilter, false);
                spectrum.Precursors          = new List <Tuple <double, int> >();
                spectrum.PrecursorIntensity  = 0;
                spectrum.PrecursorScanNumber = precScanNum;

                // get the precMz from the filter;
                int begin = scanFilter.IndexOf(" ms2 ") + 5;
                int end = begin + 1, len = 1;
                while (end < scanFilter.Count() && (scanFilter[end] >= '0' && scanFilter[end] <= '9') || scanFilter[end] == '.')
                {
                    end++;
                    len++;
                }
                spectrum.PrecMzFromFilter = double.Parse(scanFilter.Substring(begin, len));

                //Console.Write(bypassThermoAlgorithm);
                //Console.Write(precMz);
                if (precMz == 0 || bypassThermoAlgorithm)
                {
                    precMz = spectrum.PrecMzFromFilter;
                }

                if (precZ == 0 && ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE)
                {
                    foreach (int z in MS2_PRECURSOR_CHARGES)
                    {
                        spectrum.Precursors.Add(new Tuple <double, int>(precMz, z));
                    }
                }
                else
                {
                    spectrum.Precursors.Add(new Tuple <double, int>(precMz, precZ));
                }
            }
            else if (scanFilter.Contains(" ms3 "))
            {
                spectrum.MsLevel = 3;
                if (precMz == 0)
                {
                    int end = scanFilter.Count() - 1;
                    while (end >= 0 && scanFilter[end] != '@')
                    {
                        end--;
                    }
                    int begin = end - 1, len = 1;
                    while (begin >= 0 && (scanFilter[begin] >= '0' && scanFilter[begin] <= '9') || scanFilter[begin] == '.')
                    {
                        begin--;
                        len++;
                    }
                    begin++;
                    len--;
                    precMz = double.Parse(scanFilter.Substring(begin, len));
                }

                // TODO: MS3 spectrum mass/charge correction need to be taken care;
                spectrum.Precursors = new List <Tuple <double, int> >();
                spectrum.Precursors.Add(new Tuple <double, int>(precMz, precZ));

                spectrum.Peaks = GetPeaks(scanNum, scanFilter, false);
                if (precMz < 0.01)
                {
                    for (int trailerIdx = trailerLabels.GetLowerBound(0); trailerIdx <= trailerLabels.GetUpperBound(0); trailerIdx++)
                    {
                        if (trailerLabels[trailerIdx].StartsWith("SPS Mass 1"))
                        {
                            precMz = double.Parse(trailerValues[trailerIdx]);
                            break;
                        }
                    }
                }
                spectrum.Precursors          = new List <Tuple <double, int> >();
                spectrum.PrecursorIntensity  = double.NaN;
                spectrum.PrecursorScanNumber = precScanNum;
                int activationType = 0;
                _rawReader.GetActivationTypeForScanNum(scanNum, spectrum.MsLevel, ref activationType);
                spectrum.ActivationMethod = (Activation)activationType;

                if (precZ == 0 && ALWAYS_USE_PRECURSOR_CHARGE_STATE_RANGE)
                {
                    int maxCharge = 0;
                    foreach (int z in MS2_PRECURSOR_CHARGES)
                    {
                        if (maxCharge < z)
                        {
                            maxCharge = z;
                        }
                    }
                    for (int z = 0; z <= maxCharge; z++)
                    {
                        spectrum.Precursors.Add(new Tuple <double, int>(precMz, z));
                    }
                }
                else
                {
                    spectrum.Precursors.Add(new Tuple <double, int>(precMz, precZ));
                }
            }

            // get the isolation window size;
            //double isoWinSize = 0;
            //int numMSOrders = 0;
            //_rawReader.GetNumberOfMSOrdersFromScanNum(scanNum, ref numMSOrders);
            //for (int i = 0; i <= 10; i++)
            //{
            //    _rawReader.GetIsolationWidthForScanNum(scanNum, i, ref isoWinSize);
            //    Console.WriteLine(i + "\t" + isoWinSize);
            //}
            //spectrum.IsolationWindowSize = isoWinSize;
            //_rawReader.GetIsolationWidthForScanNum(scanNum, spectrum.MsLevel, ref isoWinSize);
            //Console.WriteLine(spectrum.MsLevel + "\t" + isoWinSize);

            // set the scan header information;
            spectrum.TotIonCurrent     = tic;
            spectrum.LowMz             = lowMass;
            spectrum.HighMz            = highMass;
            spectrum.BasePeakMz        = basePeakMass;
            spectrum.BasePeakIntensity = basePeakIntensity;

            return(spectrum);
        }