예제 #1
0
        private static void TestReader(string rawFilePath, bool centroid = false, bool testSumming = false, int scanStart = 0, int scanEnd = 0)
        {
            try
            {
                var rawFile = ResolveDataFile(rawFilePath);
                if (rawFile == null)
                {
                    return;
                }

                var options = new ThermoReaderOptions {
                    LoadMSMethodInfo = mLoadMethods
                };

                using (var reader = new XRawFileIO(rawFile.FullName, options, mTraceMode))
                {
                    RegisterEvents(reader);

                    var numScans = reader.GetNumScans();

                    var collisionEnergyList = string.Empty;

                    ShowMethod(reader);

                    var scanStep = 1;

                    if (scanStart < 1)
                    {
                        scanStart = 1;
                    }

                    if (scanEnd < 1)
                    {
                        scanEnd  = numScans;
                        scanStep = 21;
                    }
                    else
                    {
                        if (scanEnd < scanStart)
                        {
                            scanEnd = scanStart;
                        }
                    }

                    if (scanEnd > numScans)
                    {
                        scanEnd = numScans;
                    }

                    var msLevelStats = new Dictionary <int, int>();

                    Console.WriteLine();
                    Console.WriteLine("Reading data for scans {0} to {1}, step {2}", scanStart, scanEnd, scanStep);

                    for (var scanNum = scanStart; scanNum <= scanEnd; scanNum += scanStep)
                    {
                        if (scanNum > reader.ScanEnd)
                        {
                            ConsoleMsgUtils.ShowWarning("Exiting for loop since scan number {0} is greater than the max scan number, {1}", scanNum, reader.ScanEnd);
                            break;
                        }

                        if (mOnlyLoadMSLevelInfo)
                        {
                            var msLevel = reader.GetMSLevel(scanNum);

                            if (msLevelStats.TryGetValue(msLevel, out var msLevelObsCount))
                            {
                                msLevelStats[msLevel] = msLevelObsCount + 1;
                            }
                            else
                            {
                                msLevelStats.Add(msLevel, 1);
                            }
                            if (mScanInfoInterval <= 0 || scanNum % mScanInfoInterval == 0)
                            {
                                Console.WriteLine("Scan " + scanNum);
                            }

                            continue;
                        }

                        var success = reader.GetScanInfo(scanNum, out clsScanInfo scanInfo);

                        if (!success)
                        {
                            continue;
                        }

                        if (mScanInfoInterval <= 0 || scanNum % mScanInfoInterval == 0)
                        {
                            Console.WriteLine("Scan " + scanNum + " at " + scanInfo.RetentionTime.ToString("0.00") + " minutes: " + scanInfo.FilterText);
                        }

                        if (mLoadCollisionEnergies)
                        {
                            var collisionEnergies = reader.GetCollisionEnergy(scanNum);

                            if (collisionEnergies.Count == 0)
                            {
                                collisionEnergyList = string.Empty;
                            }
                            else if (collisionEnergies.Count >= 1)
                            {
                                collisionEnergyList = collisionEnergies[0].ToString("0.0");

                                if (collisionEnergies.Count > 1)
                                {
                                    for (var index = 1; index <= collisionEnergies.Count - 1; index++)
                                    {
                                        collisionEnergyList += ", " + collisionEnergies[index].ToString("0.0");
                                    }
                                }
                            }

                            if (string.IsNullOrEmpty(collisionEnergyList))
                            {
                                Console.WriteLine();
                            }
                            else
                            {
                                Console.WriteLine("; CE " + collisionEnergyList);
                            }
                        }

                        if (mGetScanEvents)
                        {
                            if (scanInfo.TryGetScanEvent("Monoisotopic M/Z:", out var monoMZ))
                            {
                                Console.WriteLine("Monoisotopic M/Z: " + monoMZ);
                            }

                            if (scanInfo.TryGetScanEvent("Charge State", out var chargeState, true))
                            {
                                Console.WriteLine("Charge State: " + chargeState);
                            }

                            if (scanInfo.TryGetScanEvent("MS2 Isolation Width", out var isolationWidth, true))
                            {
                                Console.WriteLine("MS2 Isolation Width: " + isolationWidth);
                            }
                        }

                        if (!mLoadScanData || (scanNum % 50 != 0 && scanEnd - scanStart > 50))
                        {
                            continue;
                        }

                        // Get the data for scan scanNum
                        Console.WriteLine();
                        Console.WriteLine("Spectrum for scan " + scanNum);

                        reader.GetScanData(scanNum, out var mzList, out var intensityList, 0, centroid);

                        var mzDisplayStepSize = 50;
                        if (centroid)
                        {
                            mzDisplayStepSize = 1;
                        }

                        for (var i = 0; i <= mzList.Length - 1; i += mzDisplayStepSize)
                        {
                            Console.WriteLine("  " + mzList[i].ToString("0.000") + " mz   " + intensityList[i].ToString("0"));
                        }
                        Console.WriteLine();

                        const int scansToSum = 15;

                        if (scanNum + scansToSum < numScans && testSumming)
                        {
                            // Get the data for scan scanNum through scanNum + 15

#pragma warning disable 618
                            reader.GetScanDataSumScans(scanNum, scanNum + scansToSum, out var massIntensityPairs, 0, centroid);
#pragma warning restore 618

                            Console.WriteLine("Summed spectrum, scans " + scanNum + " through " + (scanNum + scansToSum));

                            for (var i = 0; i <= massIntensityPairs.GetLength(1) - 1; i += 50)
                            {
                                Console.WriteLine("  " + massIntensityPairs[0, i].ToString("0.000") + " mz   " +
                                                  massIntensityPairs[1, i].ToString("0"));
                            }

                            Console.WriteLine();
                        }

                        if (!scanInfo.IsFTMS)
                        {
                            continue;
                        }

                        var dataCount = reader.GetScanLabelData(scanNum, out var ftLabelData);

                        Console.WriteLine();
                        Console.WriteLine("{0,12}{1,12}{2,12}{3,12}{4,12}{5,12}", "Mass", "Intensity", "Resolution", "Baseline", "Noise", "Charge");


                        for (var i = 0; i <= dataCount - 1; i += 50)
                        {
                            Console.WriteLine("{0,12:F3}{1,12:0}{2,12:0}{3,12:F1}{4,12:0}{5,12:0}",
                                              ftLabelData[i].Mass,
                                              ftLabelData[i].Intensity,
                                              ftLabelData[i].Resolution,
                                              ftLabelData[i].Baseline,
                                              ftLabelData[i].Noise,
                                              ftLabelData[i].Charge);
                        }

                        dataCount = reader.GetScanPrecisionData(scanNum, out var ftPrecisionData);

                        Console.WriteLine();
                        Console.WriteLine("{0,12}{1,12}{2,12}{3,12}{4,12}", "Mass", "Intensity", "AccuracyMMU", "AccuracyPPM", "Resolution");


                        for (var i = 0; i <= dataCount - 1; i += 50)
                        {
                            Console.WriteLine("{0,12:F3}{1,12:0}{2,12:F3}{3,12:F3}{4,12:0}",
                                              ftPrecisionData[i].Mass,
                                              ftPrecisionData[i].Intensity, ftPrecisionData[i].AccuracyMMU,
                                              ftPrecisionData[i].AccuracyPPM,
                                              ftPrecisionData[i].Resolution);
                        }
                    }

                    if (mOnlyLoadMSLevelInfo)
                    {
                        Console.WriteLine();
                        Console.WriteLine("{0,-10} {1}", "MSLevel", "Scans");
                        foreach (var item in msLevelStats)
                        {
                            Console.WriteLine("{0, -10} {1}", item.Key, item.Value);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ShowError("Error in TestReader: " + ex.Message, ex);
            }
        }
예제 #2
0
        public override XYData GetMassSpectrum(ScanSet scanSet, double minMZ, double maxMZ)
        {
            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");

            if (Math.Abs(minMZ) < float.Epsilon && Math.Abs(maxMZ) < float.Epsilon)
            {
                maxMZ = 10000000;
            }

            double[,] massIntensityPairs;

            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)
                {
                    // Note that GetScanDataSumScans uses the scan filter of the first scan to assure that similar scans are summed
                    mRawFileReader.GetScanDataSumScans(scanNumFirst, scanNumLast, out massIntensityPairs, 0, false);
                }
                else
                {
                    var scanNum = scanSet.PrimaryScanNumber;
                    mRawFileReader.GetScanData2D(scanNum, out massIntensityPairs);
                }
            }
            catch (AccessViolationException)
            {
                var errorMessage = "XCaliburRun2.GetMassSpectrum: Unable to load data for " + scanDescription +
                                   "; possibly a corrupt .Raw file";
                Console.WriteLine(errorMessage);
                Logger.Instance.AddEntry(errorMessage);
                return(new XYData());
            }
            catch (Exception ex)
            {
                Logger.Instance.AddEntry("XCaliburRun2.GetMassSpectrum: Unable to load data for " + scanDescription +
                                         ": " + ex.Message + "; possibly a corrupt .Raw file");
                return(new XYData());
            }

            var dataPointCount = massIntensityPairs.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 < dataPointCount; i++)
            {
                var xValue = massIntensityPairs[0, i];

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

                var yValue = massIntensityPairs[1, i];

                if (i > 0 && xValue < massIntensityPairs[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);
        }