Example #1
0
        private double DeterminePurity(MSDataScan scan, double mz, int charge, DoubleRange isolationRange)
        {
            var miniSpectrum = scan.MassSpectrum.Extract(isolationRange);

            double expectedSpacing = Constants.C13C12Difference / charge;
            double min             = expectedSpacing * 0.95;
            double max             = expectedSpacing * 1.05;

            double totalIntensity     = 0;
            double precursorIntensity = 0;

            foreach (MZPeak peak in miniSpectrum)
            {
                double difference          = (peak.MZ - mz) * charge;
                double difference_Rounded  = Math.Round(difference);
                double expected_difference = difference_Rounded * Constants.C13C12Difference;
                double Difference_ppm      = (Math.Abs((expected_difference - difference) / (mz * charge))) * 1000000;

                if (Difference_ppm <= 25)
                {
                    precursorIntensity += peak.Intensity;
                }

                totalIntensity += peak.Intensity;
            }


            return(precursorIntensity / totalIntensity);
        }
Example #2
0
        protected virtual MSDataScan <TSpectrum> GetMSDataScan(int spectrumNumber)
        {
            int msn = GetMsnOrder(spectrumNumber);

            MSDataScan <TSpectrum> scan = msn > 1 ? new MsnDataScan <TSpectrum>(spectrumNumber, msn, this) : new MSDataScan <TSpectrum>(spectrumNumber, msn, this);

            return(scan);
        }
Example #3
0
        public virtual IEnumerable <MSDataScan <TSpectrum> > GetMsScans(double firstRT, double lastRT)
        {
            int spectrumNumber = GetSpectrumNumber(firstRT - 0.0000001);

            while (spectrumNumber <= LastSpectrumNumber)
            {
                MSDataScan <TSpectrum> scan = GetMsScan(spectrumNumber++);
                double rt = scan.RetentionTime;
                if (rt < firstRT)
                {
                    continue;
                }
                if (rt > lastRT)
                {
                    yield break;
                }
                yield return(scan);
            }
        }
Example #4
0
        private double DeterminePurity(MSDataScan scan, double mz, int charge, DoubleRange isolationRange)
        {
            var miniSpectrum = scan.GetReadOnlySpectrum().Extract(isolationRange);

            double expectedSpacing = Constants.C13C12Difference/charge;
            double min = expectedSpacing*0.95;
            double max = expectedSpacing*1.05;

            double totalIntensity = 0;
            double precursorIntensity = 0;
            foreach (MZPeak peak in miniSpectrum)
            {
                double difference = (peak.MZ - mz) * charge;
                double difference_Rounded = Math.Round(difference);
                double expected_difference = difference_Rounded * Constants.C13C12Difference;
                double Difference_ppm = (Math.Abs((expected_difference - difference) / (mz * charge))) * 1000000;

                if (Difference_ppm <= 25)
                {
                    precursorIntensity += peak.Intensity;
                }

                totalIntensity += peak.Intensity;
            }

            return precursorIntensity / totalIntensity;
        }
Example #5
0
        //private MSScan GetScanFromFile(int argScanNo)
        //{
        //    return GetScanFromFile(argScanNo);
        //}
        private MSScan GetScanFromFile(int argScanNo, float argMinSN = 2)//, float argPPM = 6, int argMinPeakCount=3)
        {
            MSDataScan DataScan = _raw.GetMsScan(argScanNo);

            tolSN = argMinSN;
            //tolMinPeakCount = argMinPeakCount;
            //tolPPM = argPPM;
            MZPeak[] peaks;
            List <ThermoLabeledPeak> TPeaks = new List <ThermoLabeledPeak>();

            peaks = DataScan.MassSpectrum.GetPeaks();

            //Start Read Scan
            MSScan scan = new MSScan(argScanNo);

            scan.MsLevel    = GetMsLevel(argScanNo);
            scan.Time       = _raw.GetRetentionTime(argScanNo);
            scan.ScanHeader = _raw.GetScanFilter(argScanNo);


            List <MSPeak> MSPsks = new List <MSPeak>();

            #region MSScan
            if (scan.MsLevel == 1)
            {
                float[] RawMz        = new float[peaks.Length];
                float[] RawIntensity = new float[peaks.Length];

                for (int i = 0; i < peaks.Length; i++)
                {
                    RawMz[i]        = Convert.ToSingle(peaks[i].MZ);
                    RawIntensity[i] = Convert.ToSingle(peaks[i].Intensity);
                    ThermoLabeledPeak TPeak = (ThermoLabeledPeak)peaks[i];
                    if (TPeak.SN >= tolSN)
                    {
                        TPeaks.Add(TPeak);
                    }
                }
                float[] Mz        = new float[TPeaks.Count];
                float[] Intensity = new float[TPeaks.Count];
                for (int i = 0; i < TPeaks.Count; i++)
                {
                    Mz[i]        = Convert.ToSingle(TPeaks[i].MZ);
                    Intensity[i] = Convert.ToSingle(TPeaks[i].Intensity);
                }
                scan.RawMZs         = RawMz;
                scan.RawIntensities = RawIntensity;
                scan.MZs            = Mz;
                scan.Intensities    = Intensity;
                //************************Peak Picking
                //scan.MSPeaks = MSPsks;
                //List<float> TPeaksMZ = new List<float>();
                //for (int i = 0; i < TPeaks.Count; i++)
                //{
                //    TPeaksMZ.Add(Convert.ToSingle(TPeaks[i].MZ));
                //}
                //do
                //{
                //    ThermoLabeledPeak BasePeak = TPeaks[0];
                //    List<ThermoLabeledPeak> clusterPeaks = new List<ThermoLabeledPeak>();
                //    List<int> RemoveIdx = new List<int>();
                //    RemoveIdx.Add(0);
                //    clusterPeaks.Add(BasePeak);
                //    double Interval = 1 / (double)BasePeak.Charge;
                //    double FirstMZ = BasePeak.MZ;
                //    for (int i = 1; i < TPeaks.Count; i++)
                //    {
                //        if (TPeaks[i].MZ - FirstMZ > Interval * 10)
                //        {
                //            break;
                //        }

                //        List<int> ClosePeaks = MassUtility.GetClosestMassIdxsWithinPPM(TPeaksMZ.ToArray(), Convert.ToSingle(BasePeak.MZ + Interval), argPPM);
                //        if (ClosePeaks.Count == 1 && TPeaks[ClosePeaks[0]].Charge == BasePeak.Charge)
                //        {
                //            BasePeak = TPeaks[i];
                //            clusterPeaks.Add(TPeaks[i]);
                //            RemoveIdx.Add(i);
                //        }
                //        else if (ClosePeaks.Count > 1)
                //        {
                //            double minPPM = 100;
                //            int minPPMIdx = 0;
                //            float maxIntensity = 0;
                //            int maxIntensityIdx = 0;
                //            for (int j = 0; j < ClosePeaks.Count; j++)
                //            {
                //                if (MassUtility.GetMassPPM(BasePeak.MZ + Interval, mz[ClosePeaks[j]]) < minPPM)
                //                {
                //                    minPPMIdx = ClosePeaks[j];
                //                    minPPM = MassUtility.GetMassPPM(BasePeak.MZ + Interval, mz[ClosePeaks[j]]);
                //                }
                //                if (intensity[ClosePeaks[j]] > maxIntensity)
                //                {
                //                    maxIntensity = intensity[ClosePeaks[j]];
                //                    maxIntensityIdx = ClosePeaks[j];
                //                }
                //            }
                //            BasePeak = TPeaks[minPPMIdx];
                //            clusterPeaks.Add(TPeaks[minPPMIdx]);
                //            RemoveIdx.Add(minPPMIdx);
                //        }
                //    }
                //    if (clusterPeaks.Count < argMinPeakCount)
                //    {
                //        TPeaks.RemoveAt(RemoveIdx[0]);
                //        TPeaksMZ.RemoveAt(RemoveIdx[0]);
                //    }
                //    else
                //    {
                //        float MostIntenseMZ = 0.0f;
                //        double MostIntenseIntneisty = 0;
                //        double ClusterIntensity = 0;
                //        RemoveIdx.Reverse();
                //        for (int i = 0; i < RemoveIdx.Count; i++)
                //        {
                //            if (TPeaks[RemoveIdx[i]].Intensity > MostIntenseIntneisty)
                //            {
                //                MostIntenseIntneisty = TPeaks[RemoveIdx[i]].Intensity;
                //                MostIntenseMZ = Convert.ToSingle(TPeaks[RemoveIdx[i]].MZ);
                //            }
                //            ClusterIntensity = ClusterIntensity + TPeaks[RemoveIdx[i]].Intensity;
                //            TPeaks.RemoveAt(RemoveIdx[i]);
                //            TPeaksMZ.RemoveAt(RemoveIdx[i]);
                //        }
                //        scan.MSPeaks.Add(new MSPeak(
                //                                                                 Convert.ToSingle(clusterPeaks[0].MZ),
                //                                                                 Convert.ToSingle(clusterPeaks[0].Intensity),
                //                                                                 clusterPeaks[0].Charge,
                //                                                                 Convert.ToSingle(clusterPeaks[0].MZ),
                //                                                                 Convert.ToSingle(clusterPeaks[0].SN),
                //                                                                 MostIntenseMZ,
                //                                                                 MostIntenseIntneisty,
                //                                                                 ClusterIntensity));

                //    }
                //} while (TPeaks.Count != 0);
            }
            #endregion
            #region MS/MS Scan
            else
            {
                float[] mz        = new float[peaks.Length];
                float[] intensity = new float[peaks.Length];
                for (int i = 0; i < peaks.Length; i++)
                {
                    mz[i]        = Convert.ToSingle(peaks[i].MZ);
                    intensity[i] = Convert.ToSingle(peaks[i].Intensity);
                    //MSPsks.Add(new MSPeak(mz[i], intensity[i]));
                }
                scan.MZs         = mz;
                scan.Intensities = intensity;
                //scan.MSPeaks = MSPsks;
                // Get parent information

                scan.ParentScanNo = _raw.GetParentSpectrumNumber(argScanNo);
                scan.ParentMZ     = Convert.ToSingle(_raw.GetPrecusorMz(argScanNo));
                scan.ParentCharge = _raw.GetPrecusorCharge(argScanNo);

                //Parent Mono
                if (scan.ParentScanNo != 0)
                {
                    MSScan     ParentScan = GetScanFromFile(scan.ParentScanNo);
                    int        ClosedIdx  = MassUtility.GetClosestMassIdx(ParentScan.MZs, scan.ParentMZ);
                    List <int> Peaks      = FindPeakIdx(ParentScan.MZs, ClosedIdx, scan.ParentCharge, 10);
                    scan.ParentMonoMz = ParentScan.MZs[Peaks[0]];
                }
            }
            #endregion
            scan.IsCIDScan = IsCIDScan(argScanNo);
            scan.IsHCDScan = IsHCDScan(argScanNo);
            scan.IsFTScan  = IsFTScan(argScanNo);
            return(scan);
        }
Example #6
0
        private IEnumerable <QuantFile> LoadFiles(IEnumerable <string> filePaths, bool ms3Quant = false)
        {
            MSDataFile.CacheScans = false;
            //int largestQuantPeak = 0;
            int i = 0;

            foreach (TagInformation tag in UsedTags.Values)
            {
                tag.UniqueTagNumber       = i++;
                tag.TotalSignal           = 0;
                tag.NormalizedTotalSignal = 0;
            }
            int largestQuantPeak = i - 1;

            //int largestQuantPeak = UsedTags.Values.Select(tag => tag.UniqueTagNumber).Concat(new[] {0}).Max();

            foreach (string filePath in filePaths)
            {
                Log("Processing file:\t" + filePath);
                OnUpdateLog("Processing File " + filePath + "...");
                QuantFile    quantFile        = new QuantFile(filePath);
                StreamReader basestreamReader = new StreamReader(filePath);
                int          oldProgress      = -1;
                using (CsvReader reader = new CsvReader(basestreamReader, true))
                {
                    while (reader.ReadNextRecord()) // go through csv and raw file to extract the info we want
                    {
                        int           scanNumber  = int.Parse(reader["Spectrum number"]);
                        string        filenameID  = reader["Filename/id"];
                        string        rawFileName = filenameID.Split('.')[0];
                        bool          isDecoy     = reader["DEFLINE"].StartsWith("DECOY_");
                        ThermoRawFile rawFile;
                        if (!RawFiles.TryGetValue(rawFileName, out rawFile))
                        {
                            throw new ArgumentException("Cannot find this raw file: " + rawFileName + ".raw");
                        }
                        if (!rawFile.IsOpen)
                        {
                            rawFile.Open();
                        }

                        int progress = (int)(100 * (double)basestreamReader.BaseStream.Position / basestreamReader.BaseStream.Length);
                        if (progress != oldProgress)
                        {
                            OnProgressUpdate(progress);
                            oldProgress = progress;
                        }

                        //// Set default fragmentation to CAD / HCD
                        //FragmentationMethod ScanFragMethod = filenameID.Contains(".ETD.")
                        //    ? FragmentationMethod.ETD
                        //    : FragmentationMethod.CAD;

                        //if (ScanFragMethod == FragmentationMethod.ETD)
                        //{
                        //    ScanFragMethod = FragmentationMethod.CAD;
                        //    scanNumber += ETDQuantPosition;
                        //}

                        // Get the scan object for the sequence ms2 scan
                        MsnDataScan quantitationMsnScan = rawFile[scanNumber] as MsnDataScan;

                        double purity = 1;
                        if (CalculatePurity)
                        {
                            double      mz             = quantitationMsnScan.PrecursorMz;
                            int         charge         = quantitationMsnScan.PrecursorCharge;
                            DoubleRange isolationRange = MzRange.FromDa(mz, PurityWindowInTh);

                            MSDataScan parentScan = rawFile[quantitationMsnScan.ParentScanNumber];
                            purity = DeterminePurity(parentScan, mz, charge, isolationRange);
                        }

                        if (quantitationMsnScan == null)
                        {
                            OnUpdateLog("Spectrum Number " + scanNumber + " is not a valid MS2 scan from: " + rawFile.FilePath + ". Skipping PSM...");
                            continue;
                        }

                        if (MS3Quant)
                        {
                            quantitationMsnScan = null;
                            // Look forward to find associated MS3 quant scan (based on parent scan number)
                            int ms3ScanNumber = scanNumber + 1;
                            while (ms3ScanNumber < rawFile.LastSpectrumNumber)
                            {
                                if (rawFile.GetParentSpectrumNumber(ms3ScanNumber) == scanNumber)
                                {
                                    quantitationMsnScan = rawFile[ms3ScanNumber] as MsnDataScan;
                                    break;
                                }
                                ms3ScanNumber++;
                            }

                            if (quantitationMsnScan == null)
                            {
                                OnUpdateLog("Cannot find a MS3 spectrum associated with spectrum number " + scanNumber + ". Skipping PSM...");
                                continue;
                            }
                        }

                        Tolerance Tolerance = quantitationMsnScan.MzAnalyzer == MZAnalyzerType.IonTrap2D ? ItMassTolerance : FtMassTolerance;
                        bool      isETD     = quantitationMsnScan.DissociationType == DissociationType.ETD;

                        double injectionTime = quantitationMsnScan.InjectionTime;
                        //var massSpectrum = quantitationMsnScan.MassSpectrum;
                        var thermoSpectrum = rawFile.GetLabeledSpectrum(quantitationMsnScan.SpectrumNumber);

                        double noise = 0;
                        if (NoisebandCap)
                        {
                            // Noise is pretty constant over a small region, find the noise of the center of all isobaric tags
                            MassRange range = new MassRange(UsedTags.Keys[0], UsedTags.Keys[UsedTags.Count - 1]);

                            if (thermoSpectrum != null)
                            {
                                var peak = thermoSpectrum.GetClosestPeak(range.Mean, 500);
                                if (peak != null)
                                {
                                    noise = peak.Noise;
                                }
                                else
                                {
                                    OnUpdateLog("Spectrum (#" + quantitationMsnScan.SpectrumNumber + ") has no m/z peaks. Skipping PSM...");
                                    continue;
                                }
                            }
                            else
                            {
                                OnUpdateLog("Spectrum (#" + quantitationMsnScan.SpectrumNumber + ") is low-resolution data without noise information. Skipping PSM...");
                                continue;
                            }
                        }

                        //Dictionary<TagInformation, QuantPeak> peaks = new Dictionary<TagInformation, QuantPeak>();
                        QuantPeak[] peaks = new QuantPeak[largestQuantPeak + 1];
                        // Read in the peak data
                        foreach (TagInformation tag in UsedTags.Values)
                        {
                            double tagMz = isETD
                                ? tag.MassEtd
                                : tag.MassCAD;

                            var peak = thermoSpectrum.GetClosestPeak(Tolerance.GetRange(tagMz));

                            QuantPeak qPeak = new QuantPeak(tag, peak, injectionTime, quantitationMsnScan, noise, peak == null && NoisebandCap);

                            peaks[tag.UniqueTagNumber] = qPeak;
                        }

                        PurityCorrect(peaks, isDecoy);

                        PSM psm = new PSM(filenameID, scanNumber, peaks, purity);
                        quantFile.AddPSM(psm);
                    }
                }

                // Dispose of all raw files
                foreach (ThermoRawFile rawFile in RawFiles.Values)
                {
                    rawFile.Dispose();
                }
                OnUpdateLog("PSMs loaded " + quantFile.Psms.Count);
                Log("PSMs Loaded:\t" + quantFile.Psms.Count);
                yield return(quantFile);
            }
        }
Example #7
0
 public bool Equals(MSDataScan other)
 {
     return(Scan.Equals(other));
 }