Exemple #1
0
        private MZAnalyzerType GetMzAnalyzer(int spectrumNumber)
        {
            int mzanalyzer = 0;

            _rawConnection.GetMassAnalyzerTypeForScanNum(spectrumNumber, ref mzanalyzer);

            switch ((ThermoMzAnalyzer)mzanalyzer)
            {
            case ThermoMzAnalyzer.FTMS:
                return(MZAnalyzerType.Orbitrap);

            default:
                return(MZAnalyzerType.Unknown);
            }
        }
Exemple #2
0
        public override MZAnalyzerType GetMzAnalyzer(int spectrumNumber)
        {
            int mzanalyzer = 0;

            _rawConnection.GetMassAnalyzerTypeForScanNum(spectrumNumber, ref mzanalyzer);

            switch ((ThermoMzAnalyzer)mzanalyzer)
            {
            case ThermoMzAnalyzer.FTMS:
                return(MZAnalyzerType.Orbitrap);

            case ThermoMzAnalyzer.ITMS:
                return(MZAnalyzerType.IonTrap2D);

            case ThermoMzAnalyzer.Sector:
                return(MZAnalyzerType.Sector);

            case ThermoMzAnalyzer.TOFMS:
                return(MZAnalyzerType.TOF);

            default:
                return(MZAnalyzerType.Unknown);
            }
        }
Exemple #3
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));
            }
        }
        public void Convert(TaskProgress progress)
        {
            // temporarily keep the previous MS1 and MS2 spectra for possible speeding up;
            MassSpectrum lastMS1Spec    = null;
            int          lastMS1ScanNum = -1;
            MassSpectrum lastMS2Spec    = null;
            int          lastMS2ScanNum = -1;

            int spectrumProcessed = 0;

            // get instrument method names;
            int    pnNumInstMethods = 0;
            object pvarNames        = null;

            _rawReader.GetInstMethodNames(ref pnNumInstMethods, ref pvarNames);

            // create instance for data preprocessing;
            PrecursorPredictor pp = null;

            if (expType == ExperimentType.DIA && predictPrecursors)
            {
                pp = new PrecursorPredictor(DEFAULT_DIA_ISO_WIN_SIZE, 1, 6, 0);
            }

            //Scan whole raw file to remember scan Number of MS1
            if (correctPrecMz)
            {
                for (int scanNum = FirstScanNum; scanNum <= LastScanNum; scanNum++)
                {
                    int MSOrder = 0;
                    _rawReader.GetNumberOfMSOrdersFromScanNum(scanNum, ref MSOrder);

                    // check the MS level;
                    if (MSOrder == 0)
                    {
                        MS1ScanNum.Add(scanNum);
                    }
                }
            }


            for (int scanNum = FirstScanNum; scanNum <= LastScanNum; scanNum++)
            {
                MassSpectrum spec = GetSpectrumByScanNum(scanNum);
                // Console.Write("^^^^^" + spec.Peaks.Count);
                if (spec == null)
                {
                    continue;
                }

                // get the mass analyzer type;
                int pnMassAnalyzerType = 0;
                _rawReader.GetMassAnalyzerTypeForScanNum(scanNum, ref pnMassAnalyzerType);

                // get the precursors;
                //if (spec.MsLevel > 1)
                //{
                //    Object pvarPrecursorInfos = null;
                //    int pnArraySize = 0;
                //    _rawReader.GetPrecursorInfoFromScanNum(scanNum, ref pvarPrecursorInfos, ref pnArraySize);
                //}

                // if the spectrum has an empty peak list, abandon this spectrum;
                if (spec.Peaks.Count > 0)
                {
                    if (spec.MsLevel > 1)
                    {
                        if (spec.PrecursorScanNumber == 0)
                        {
                            if (spec.MsLevel == 2)
                            {
                                spec.PrecursorScanNumber = lastMS1Spec == null ? 0 : lastMS1ScanNum;
                            }
                            else if (spec.MsLevel == 3)
                            {
                                spec.PrecursorScanNumber = lastMS2Spec == null ? 0 : lastMS2ScanNum;
                            }
                        }
                    }

                    if (spec.MsLevel == 2)
                    {
                        if (expType == ExperimentType.DDA)
                        {
                            if (spec.PrecursorScanNumber == lastMS1ScanNum)
                            {
                                // double check the precursor m/z value in the MS1 scan;
                                ExtractPrecursorMz(spec, lastMS1Spec);
                                if (correctPrecMz)
                                {
                                    spec = CalCorrectPrecursor(spec, lastMS1Spec);
                                }
                            }
                            else
                            {
                                // check whether the precursor scan number is available;
                                if (spec.PrecursorScanNumber > 0)
                                {
                                    MassSpectrum precSpec = GetSpectrumByScanNum(spec.PrecursorScanNumber);

                                    // double check the precursor m/z value in the MS1 scan;
                                    ExtractPrecursorMz(spec, precSpec);

                                    if (correctPrecMz)
                                    {
                                        spec = CalCorrectPrecursor(spec, lastMS1Spec);
                                    }
                                }
                            }
                        }
                        else if (expType == ExperimentType.DIA && predictPrecursors)
                        {
                            if (spec.PrecursorScanNumber == lastMS1ScanNum)
                            {
                                pp.PredictPrecursors(ref spec, ref lastMS1Spec);
                            }
                            else
                            {
                                // check whether the precursor scan number is available;
                                if (spec.PrecursorScanNumber > 0)
                                {
                                    MassSpectrum precSpec = GetSpectrumByScanNum(spec.PrecursorScanNumber);
                                    pp.PredictPrecursors(ref spec, ref precSpec);
                                }
                            }
                        }
                    }
                    if (spec.MsLevel == 3)
                    {
                        if (expType == ExperimentType.DDA)
                        {
                            if (spec.PrecursorScanNumber == lastMS2ScanNum)
                            {
                                // double check the precursor m/z value in the MS2 scan;
                                ExtractPrecursorMz(spec, lastMS2Spec);
                            }
                            else
                            {
                                // check whether the precursor scan number is available;
                                if (spec.PrecursorScanNumber > 0)
                                {
                                    // double check the precursor m/z value in the MS2 scan;
                                    MassSpectrum precSpec = GetSpectrumByScanNum(spec.PrecursorScanNumber);
                                    ExtractPrecursorMz(spec, precSpec);
                                }
                            }
                        }
                        else if (expType == ExperimentType.DIA && predictPrecursors)
                        {
                            if (spec.PrecursorScanNumber == lastMS1ScanNum)
                            {
                                pp.PredictPrecursors(ref spec, ref lastMS1Spec);
                            }
                            else
                            {
                                // check whether the precursor scan number is available;
                                if (spec.PrecursorScanNumber > 0)
                                {
                                    MassSpectrum precSpec = GetSpectrumByScanNum(spec.PrecursorScanNumber);
                                    pp.PredictPrecursors(ref spec, ref precSpec);
                                }
                            }
                        }
                    }


                    if (spec.MsLevel == 2 && exportChargeState)
                    {
                        if (expType == ExperimentType.DDA)
                        {
                            //Collection charge of MS2
                            if (chargeNum.ContainsKey(spec.Precursors[0].Item2))
                            {
                                chargeNum[spec.Precursors[0].Item2] += 1;
                            }
                            else
                            {
                                chargeNum.Add(spec.Precursors[0].Item2, 1);
                            }
                        }
                    }

                    // write into the output files;
                    WriteToOutFiles(spec);
                }

                if (spec.MsLevel == 1)
                {
                    lastMS1ScanNum = spec.ScanNumber;
                    lastMS1Spec    = spec;
                }
                else if (spec.MsLevel == 2)
                {
                    lastMS2ScanNum = spec.ScanNumber;
                    lastMS2Spec    = spec;
                }

                if (progress.Aborted)
                {
                    break;
                }
                spectrumProcessed++;
                progress.CurrentProgress = (int)((double)spectrumProcessed / (LastScanNum - FirstScanNum + 1) * 100);
                if (progress.CurrentProgress > lastProgress)
                {
                    try
                    {
                        int currentLineCursor = Console.CursorTop;
                        Console.SetCursorPosition(0, Console.CursorTop);
                        lastProgress = progress.CurrentProgress;
                        Console.WriteLine(" Reading RAW File: " + lastProgress + "%");
                        Console.SetCursorPosition(0, currentLineCursor);
                    }
                    catch (IOException ex)
                    {
                        if (progress.CurrentProgress - lastProgress >= 10 || progress.CurrentProgress == 100)
                        {
                            lastProgress = progress.CurrentProgress;
                            Console.WriteLine(" Reading RAW File: " + lastProgress + "%");
                        }
                    }
                }
            }

            if (expType == ExperimentType.DDA && exportChargeState)
            {
                _chargeNumWriter = new StreamWriter(outFileWithoutExtentionName + "txt");
                //export charge states to file
                ExportChargeStatesToFile(chargeNum);
            }


            // complete mzXML file writing if mzXMLWriter is available;
            if (_mzXMLWriter != null)
            {
                _mzXMLWriter.WriteIndex();
                _mzXMLWriter.WriteEnd();
            }
        }