Exemple #1
0
        internal List <Ion> GetSpectrum(string rawFile, int scanNo)
        {
            List <Ion> returnIons = new List <Ion>();

            MSFileReaderLib.MSFileReader_XRawfile m2 = new MSFileReaderLib.MSFileReader_XRawfile();

            m2.Open(rawFile);
            m2.SetCurrentController(0, 1);

            string filter = null;

            m2.GetFilterForScanNum(scanNo, ref filter);



            double scanCentroidPeakWidth = 0.005;
            int    arraySize             = maxNoPeaks;
            object massList  = null;
            object peakFlags = null;

            m2.GetMassListFromScanNum(
                ref scanNo,
                filter,
                scanIntensityCutoffType,
                scanIntensityCutoffValue,
                maxNoPeaks,
                scanCentroidResult,
                ref scanCentroidPeakWidth,
                ref massList,
                ref peakFlags,
                ref arraySize);

            double[,] thisMassList = (double[, ])massList;

            int l = thisMassList.GetLength(1);

            for (int counter = 0; counter < thisMassList.GetLength(1); counter++)
            {
                double intensity = Math.Round(thisMassList[1, counter], 1);

                if (intensity > 0)
                {
                    Ion ion = new Ion(Math.Round(thisMassList[0, counter], 5), intensity, 0, 0);
                    returnIons.Add(ion);
                }
            }

            return(returnIons);
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rawFile">The Thermo RAW File</param>
        /// <param name="scanNumbers">If no scan numbers are in the array the filter will search for all scan numbers</param>
        /// <returns></returns>
        public List <MSLight> GetSpectra(string rawFile, List <int> scanNumbers, bool onlyMS1)
        {
            Console.WriteLine("Parsing " + rawFile);
            MSFileReaderLib.MSFileReader_XRawfile m2 = new MSFileReaderLib.MSFileReader_XRawfile();

            m2.Open(rawFile);
            m2.SetCurrentController(0, 1);

            //string filter2 = "";
            //m2.GetFilterForScanNum(50, ref filter2);

            int noSpectra = 0;

            m2.GetNumSpectra(ref noSpectra);

            MSLight        lastMS1   = new MSLight();
            List <MSLight> mySpectra = new List <MSLight>(noSpectra);

            for (int i = 1; i <= noSpectra; i++)
            {
                string filter = null;
                if (scanNumbers.Count == 0 || scanNumbers.Contains(i))
                {
                    m2.GetFilterForScanNum(i, ref filter);


                    MSLight thisMS = new MSLight();

                    Ion precursor = new Ion(-1, -1, -1, -1);
                    if (isMS1.IsMatch(filter))
                    {
                        lastMS1 = thisMS;
                    }
                    else
                    {
                        if (onlyMS1)
                        {
                            continue;
                        }
                        if (!MyParams.UseThermoMonoIsotopicPrediction)
                        {
                            precursor = GetPrecursor(filter, lastMS1);
                        }
                    }



                    //-------------

                    double retentionTime = 0;
                    m2.RTFromScanNum(i, ref retentionTime);


                    string thisFilter = null;
                    m2.GetFilterForScanNum(i, ref thisFilter);

                    string flags = null;
                    m2.GetFlags(ref flags);


                    //------------

                    double scanCentroidPeakWidth = 0.005;
                    int    arraySize             = maxNoPeaks;
                    object massList  = null;
                    object peakFlags = null;

                    m2.GetMassListFromScanNum(
                        ref i,
                        filter,
                        scanIntensityCutoffType,
                        scanIntensityCutoffValue,
                        maxNoPeaks,
                        scanCentroidResult,
                        ref scanCentroidPeakWidth,
                        ref massList,
                        ref peakFlags,
                        ref arraySize);


                    thisMS.ScanNumber = i;

                    object trailerLabels = null;
                    object trailerValues = null;
                    int    cc            = 0;

                    m2.GetTrailerExtraForScanNum(i, ref trailerLabels, ref trailerValues, ref cc);

                    object chargeStateObject = null;
                    m2.GetTrailerExtraValueForScanNum(i, "Charge State:", ref chargeStateObject);
                    double chargeState = double.Parse(chargeStateObject.ToString());


                    string[] theStringValues = (string[])trailerValues;

                    if (theStringValues[1].Contains("="))
                    {
                        string [] split      = Regex.Split(theStringValues[1], "=");
                        string [] correction = Regex.Split(split[1], ";");
                        thisMS.IonInjectionTime = double.Parse(correction[0]);
                    }
                    else
                    {
                        thisMS.IonInjectionTime = double.Parse(theStringValues[2]);
                    }



                    Match instrument = captureInstrumentType.Match(filter);
                    thisMS.InstrumentType = instrument.Groups[1].ToString();
                    thisMS.ILines.Add("I\tFilter\t" + filter);
                    Match activationType = captureActivationType.Match(filter);
                    thisMS.ActivationType = activationType.Groups[1].ToString().ToUpper();


                    if (!thisMS.ActivationType.Equals(""))
                    {
                        if (MyParams.UseThermoMonoIsotopicPrediction)
                        {
                            object monoisotopicObject = null;
                            m2.GetTrailerExtraValueForScanNum(i, "Monoisotopic M/Z:", ref monoisotopicObject);
                            double objectValue = Math.Round(double.Parse(monoisotopicObject.ToString()), 4);

                            if (objectValue == 0)
                            {
                                //Thermo was unable to determine precursor so we will try to get it.
                                precursor = GetPrecursor(filter, lastMS1);
                                thisMS.ChargedPrecursor = precursor.MZ;
                            }
                            else
                            {
                                precursor = new Ion(objectValue, 1, 1, i);
                                //Console.WriteLine("Monoisotopic Thermo: " + precursor.MZ);
                                thisMS.ChargedPrecursor = precursor.MZ;
                            }
                        }
                        else
                        {
                            thisMS.ChargedPrecursor = precursor.MZ;
                        }



                        if (chargeState == 0)
                        {
                            thisMS.ZLines.Add("Z\t2\t" + Math.Round(PatternTools.pTools.DechargeMSPeakToPlus1(precursor.MZ, 2), 4));
                            thisMS.ZLines.Add("Z\t3\t" + Math.Round(PatternTools.pTools.DechargeMSPeakToPlus1(precursor.MZ, 3), 4));
                        }
                        else
                        {
                            thisMS.ZLines.Add("Z\t" + int.Parse(chargeState.ToString()).ToString() + "\t" + Math.Round(PatternTools.pTools.DechargeMSPeakToPlus1(precursor.MZ, chargeState), 4));
                        }

                        thisMS.ILines.Add("I\tPrecursorScan\t" + lastMS1.ScanNumber);
                        thisMS.ILines.Add("I\tPrecursorInt\t" + precursor.Intensity);
                    }


                    double[,] thisMassList = (double[, ])massList;

                    int l = thisMassList.GetLength(1);

                    double[] mz        = new double[thisMassList.GetLength(1)];
                    double[] intensity = new double[thisMassList.GetLength(1)];


                    for (int counter = 0; counter < thisMassList.GetLength(1); counter++)
                    {
                        if (thisMassList[1, counter] > 0.5) //check if we have a minimum intensity
                        {
                            mz[counter]        = Math.Round(thisMassList[0, counter], 5);
                            intensity[counter] = Math.Round(thisMassList[1, counter], 1);

                            //thisMS.MZ.Add(Math.Round(thisMassList[0, counter], 5));
                            //thisMS.Intensity.Add(Math.Round(thisMassList[1, counter], 1));
                        }
                    }

                    if (mz.Length == 0) //There is no use in saving an empty MS
                    {
                        continue;
                    }

                    thisMS.MZ        = mz.ToList();
                    thisMS.Intensity = intensity.ToList();

                    thisMS.CromatographyRetentionTime = Math.Round(retentionTime, 3);

                    //----------------------------

                    mySpectra.Add(thisMS);
                }
            }
            Console.WriteLine(".");

            m2.Close();

            //Some final cleanup
            mySpectra.RemoveAll(a => a.MZ.Count < 5);

            if (MyParams.ActivateSpectraCleaner)
            {
                CleanSpectra(mySpectra);
            }

            return(mySpectra);
        }
Exemple #3
0
        public override XYData GetMassSpectrum(ScanSet scanset, double minMZ, double maxMZ)
        {
            // Note that we're using function attribute HandleProcessCorruptedStateExceptions
            // to force .NET to properly catch critical errors thrown by the XRawfile DLL

            Check.Require(scanset != null, "Can't get mass spectrum; inputted set of scans is null");
            if (scanset == null)
            {
                return(null);
            }

            Check.Require(scanset.IndexValues.Count > 0, "Can't get mass spectrum; no scan numbers inputted");

            double[,] vals = null;
            var spectraAreSummed = scanset.IndexValues.Count > 1;
            var scanNumFirst     = scanset.IndexValues[0];
            var scanNumLast      = scanset.IndexValues[scanset.IndexValues.Count - 1];

            string scanDescription;

            if (spectraAreSummed)
            {
                scanDescription = "scan " + scanset.PrimaryScanNumber + "( summing scans " + scanNumFirst + " to " + scanNumLast + ")";
            }
            else
            {
                scanDescription = "scan " + scanset.PrimaryScanNumber;
            }

            try
            {
                if (spectraAreSummed)
                {
                    var          backgroundScan1First = 0;
                    var          backgroundScan1Last  = 0;
                    var          backgroundScan2First = 0;
                    var          backgroundScan2Last  = 0;
                    const string filter = "p full ms"; //only sum MS1 data

                    const int intensityCutoffType  = 0;
                    const int intensityCutoffValue = 0;
                    const int maxNumberOfPeaks     = 0;
                    const int centroidResult       = 0;
                    double    centVal   = 0;
                    object    massList  = null;
                    object    peakFlags = null;
                    var       arraySize = 0;

                    _msfileReader.GetAverageMassList(
                        ref scanNumFirst,
                        ref scanNumLast,
                        ref backgroundScan1First,
                        ref backgroundScan1Last,
                        ref backgroundScan2First,
                        ref backgroundScan2Last,
                        filter,
                        intensityCutoffType,
                        intensityCutoffValue,
                        maxNumberOfPeaks,
                        centroidResult,
                        ref centVal,
                        ref massList,
                        ref peakFlags,
                        ref arraySize);

                    vals = (double[, ])massList;
                }
                else
                {
                    var       scanNum              = scanset.PrimaryScanNumber;
                    string    filter               = null;
                    const int intensityCutoffType  = 0;
                    const int intensityCutoffValue = 0;
                    const int maxNumberOfPeaks     = 0;
                    const int centroidResult       = 0;

                    double centVal   = 0;
                    object massList  = null;
                    object peakFlags = null;
                    var    arraySize = 0;

                    _msfileReader.GetMassListFromScanNum(
                        ref scanNum,
                        filter,
                        intensityCutoffType,
                        intensityCutoffValue,
                        maxNumberOfPeaks,
                        centroidResult,
                        ref centVal,
                        ref massList,
                        ref peakFlags,
                        ref arraySize);

                    vals = (double[, ])massList;
                }
            }
            catch (System.AccessViolationException)
            {
                var errorMessage = "XCaliburRun2.GetMassSpectrum: Unable to load data for " + scanDescription +
                                   "; possibly a corrupt .Raw file";
                Console.WriteLine(errorMessage);
                Logger.Instance.AddEntry(errorMessage);
            }
            catch (Exception ex)
            {
                Logger.Instance.AddEntry("XCaliburRun2.GetMassSpectrum: Unable to load data for " + scanDescription +
                                         ": " + ex.Message + "; possibly a corrupt .Raw file");
            }

            if (vals == null)
            {
                return(null);
            }

            var length = vals.GetLength(1);

            var xvals = new List <double>();
            var yvals = new List <double>();

            // Note from MEM (October 2013)
            // GetMassListFromScanNum generally returns the data sorted by m/z ascending
            // However, there are edge cases for certain spectra in certain datasets where adjacent data points are out of order and need to be swapped
            // Therefore, we must validate that the data is truly sorted, and if we find a discrepancy, sort it after populating xydata.Xvalues and xydata.Yvalues
            var sortRequired = false;

            for (var i = 0; i < length; i++)
            {
                var xValue = vals[0, i];

                if (xValue < minMZ || xValue > maxMZ)
                {
                    continue;
                }

                var yValue = vals[1, i];

                if (i > 0 && xValue < vals[0, i - 1])
                {
                    // Points are out of order; this rarely occurs but it is possible and has been observed
                    sortRequired = true;
                }

                xvals.Add(xValue);
                yvals.Add(yValue);
            }

            var xydata = new XYData
            {
                Xvalues = xvals.ToArray(),
                Yvalues = yvals.ToArray()
            };

            if (sortRequired)
            {
                Array.Sort(xydata.Xvalues, xydata.Yvalues);
            }

            return(xydata);
        }