예제 #1
0
        public double ScoreLipidDissimilarity(LipidGroupSearchResult lipidGroupSearchResult)
        {
            var relatedScoreModelUnits = GetRelatedScoreModelUnits(lipidGroupSearchResult);

            var spectrumSearchResult = lipidGroupSearchResult.SpectrumSearchResult;
            var cidResultList        = spectrumSearchResult.CidSearchResultList;
            var hcdResultList        = spectrumSearchResult.HcdSearchResultList;

            var cidMaxIntensity = spectrumSearchResult.CidSpectrum.Peaks.Any() ? spectrumSearchResult.CidSpectrum.Peaks.Max(x => x.Intensity) : 1;
            var hcdMaxIntensity = spectrumSearchResult.HcdSpectrum.Peaks.Any() ? spectrumSearchResult.HcdSpectrum.Peaks.Max(x => x.Intensity) : 1;

            double lipidScore = 0;

            if (cidMaxIntensity > 1)
            {
                // Score CID Results
                lipidScore += ScoreSingleFragmentationTypeDissimilarity(cidResultList, relatedScoreModelUnits, FragmentationType.CID, cidMaxIntensity);
            }

            if (hcdMaxIntensity > 1)
            {
                // Score CID Results
                lipidScore += ScoreSingleFragmentationTypeDissimilarity(hcdResultList, relatedScoreModelUnits, FragmentationType.HCD, hcdMaxIntensity);
            }

            return(lipidScore);
        }
예제 #2
0
        public static void AddHeaderForScoring(LipidGroupSearchResult lipidGroupSearchResult, TextWriter textWriter)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.Append("Dataset,Lipid,");

            var spectrumSearchResult = lipidGroupSearchResult.SpectrumSearchResult;
            var cidResultList        = spectrumSearchResult.CidSearchResultList;
            var hcdResultList        = spectrumSearchResult.HcdSearchResultList;

            foreach (var cidResult in cidResultList)
            {
                stringBuilder.Append("CID-" + cidResult.TheoreticalPeak.Description + ",");
            }

            foreach (var hcdResult in hcdResultList)
            {
                stringBuilder.Append("HCD-" + hcdResult.TheoreticalPeak.Description + ",");
            }

            textWriter.WriteLine(stringBuilder.ToString());
        }
예제 #3
0
        private List <ScoreModelUnit> GetRelatedScoreModelUnits(LipidGroupSearchResult lipidGroupSearchResult)
        {
            var lipidTarget = lipidGroupSearchResult.LipidTarget;

            return(GetRelatedScoreModelUnits(lipidTarget));
        }
예제 #4
0
        public static List <LipidGroupSearchResult> RunGlobalWorkflow(IEnumerable <Lipid> lipidList, LcMsRun lcmsRun, double hcdMassError, double cidMassError, ScoreModel scoreModel, IProgress <int> progress = null)
        {
            //TextWriter textWriter = new StreamWriter("outputNeg.tsv");
            var lipidGroupSearchResultList = new List <LipidGroupSearchResult>();

            var hcdTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm);
            var cidTolerance = new Tolerance(cidMassError, ToleranceUnit.Ppm);

            //var lipidsGroupedByTarget = lipidList.OrderBy(x => x.LipidTarget.Composition.Mass).GroupBy(x => x.LipidTarget).ToList(); //order by mz
            var lipidsGroupedByTarget = lipidList.OrderBy(x => x.LipidTarget.MzRounded).GroupBy(x => x.LipidTarget).ToList();

            var    minLcScan = lcmsRun.MinLcScan;
            double maxLcScan = lcmsRun.MaxLcScan;



            var activationMethodCombination = FigureOutActivationMethodCombination(lcmsRun);

            if (activationMethodCombination == ActivationMethodCombination.Unsupported)
            {
                throw new SystemException("Unsupported activation method.");
            }
            var useTwoScans = activationMethodCombination == ActivationMethodCombination.CidThenHcd || activationMethodCombination == ActivationMethodCombination.HcdThenCid;

            for (var i = minLcScan; i <= maxLcScan; i++)
            {
                // Lookup the MS/MS Spectrum
                var firstMsMsSpectrum = lcmsRun.GetSpectrum(i) as ProductSpectrum;
                if (firstMsMsSpectrum == null)
                {
                    continue;
                }

                // Lookup the MS/MS Spectrum
                ProductSpectrum secondMsMsSpectrum = null;
                if (useTwoScans)
                {
                    secondMsMsSpectrum = lcmsRun.GetSpectrum(i + 1) as ProductSpectrum;
                    if (secondMsMsSpectrum == null)
                    {
                        continue;
                    }

                    // If m/z values of the MS/MS spectrums do not match, just move on
                    var deltaMz = firstMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz -
                                  secondMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                    if (Math.Abs(deltaMz) > 0.01)
                    {
                        continue;
                    }
                }

                //textWriter.WriteLine(i);
                //Console.WriteLine(DateTime.Now + "\tProcessing Scan" + i);

                // Grab Precursor Spectrum

                var precursorScanNumber = 0;
                if (lcmsRun.MinMsLevel == 1) //Make sure there are precursor scans in file
                {
                    precursorScanNumber = lcmsRun.GetPrecursorScanNum(i);
                }

                var precursorSpectrum = lcmsRun.GetSpectrum(precursorScanNumber);

                // Assign each MS/MS spectrum to HCD or CID
                ProductSpectrum hcdSpectrum;
                ProductSpectrum cidSpectrum;
                if (firstMsMsSpectrum.ActivationMethod == ActivationMethod.HCD)
                {
                    hcdSpectrum = firstMsMsSpectrum;
                    cidSpectrum = secondMsMsSpectrum;
                }
                else
                {
                    hcdSpectrum = secondMsMsSpectrum;
                    cidSpectrum = firstMsMsSpectrum;
                }

                var msMsPrecursorMz     = firstMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                var mzToSearchTolerance = hcdMassError * msMsPrecursorMz / 1000000;
                var lowMz  = msMsPrecursorMz - mzToSearchTolerance;
                var highMz = msMsPrecursorMz + mzToSearchTolerance;

                foreach (var grouping in lipidsGroupedByTarget)
                {
                    var lipidTarget = grouping.Key;
                    var lipidMz     = lipidTarget.MzRounded;

                    // If we reached the point where the m/z is too high, we can exit
                    if (lipidMz > highMz)
                    {
                        break;
                    }

                    if (lipidMz > lowMz)
                    {
                        // Find the MS1 data
                        //Xic xic = lcmsRun.GetPrecursorExtractedIonChromatogram(lipidMz, hcdTolerance, i);
                        var xic = lcmsRun.GetFullPrecursorIonExtractedIonChromatogram(lipidMz, hcdTolerance);

                        // Bogus data
                        if (precursorSpectrum != null && (xic.GetApexScanNum() < 0 || xic.GetSumIntensities() <= 0))
                        {
                            continue;
                        }

                        // Grab the MS/MS peak to search for
                        IEnumerable <MsMsSearchUnit> msMsSearchUnits = lipidTarget.GetMsMsSearchUnits();

                        // Get all matching peaks
                        var hcdSearchResultList = hcdSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = hcdSpectrum.FindPeak(msMsSearchUnit.Mz, hcdTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();
                        var cidSearchResultList = cidSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = cidSpectrum.FindPeak(msMsSearchUnit.Mz, cidTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();

                        // Create spectrum search results
                        SpectrumSearchResult   spectrumSearchResult;
                        LipidGroupSearchResult lipidGroupSearchResult;

                        if (precursorSpectrum != null)
                        {
                            spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, precursorSpectrum, hcdSearchResultList, cidSearchResultList, xic, lcmsRun)
                            {
                                PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                            };
                            lipidGroupSearchResult = new LipidGroupSearchResult(lipidTarget, grouping.ToList(), spectrumSearchResult, scoreModel);
                        }
                        else //If there are no precursor scans in this file
                        {
                            spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, hcdSearchResultList, cidSearchResultList, lcmsRun)
                            {
                                PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                            };
                            lipidGroupSearchResult = new LipidGroupSearchResult(lipidTarget, grouping.ToList(), spectrumSearchResult, scoreModel);
                        }


                        lipidGroupSearchResultList.Add(lipidGroupSearchResult);
                    }
                }

                // Skip an extra scan if we look at 2 at a time
                if (useTwoScans)
                {
                    i++;
                }

                // Report progress
                if (progress != null)
                {
                    var currentProgress = (int)(i / maxLcScan * 100);
                    progress.Report(currentProgress);
                }
            }

            //textWriter.Close();
            return(lipidGroupSearchResultList);
        }
예제 #5
0
        /*
         * public static List<LipidGroupSearchResult> RunGlobalWorkflow(IEnumerable<Lipid> lipidList, DataReader ImsRun, IEnumerable<ImsFeature> FeatureTargets, double hcdMassError,double cidMassError, ScoreModel scoreModel, IProgress<int> progress = null)
         * {
         *  Tolerance hcdTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm);
         *  Tolerance cidTolerance = new Tolerance(cidMassError, ToleranceUnit.Ppm);
         *
         *  var lipidsGroupedByTarget = lipidList.OrderBy(x => x.LipidTarget.Composition.Mass).GroupBy(x => x.LipidTarget).ToList();
         *  int MS1Frames = ImsRun.GetNumberOfFrames(DataReader.FrameType.MS1);
         *  int MS2Frames = ImsRun.GetNumberOfFrames(DataReader.FrameType.MS2);
         *
         *  var gp = ImsRun.GetGlobalParams();
         *  var framelist = ImsRun.GetMasterFrameList();
         *
         *  ActivationMethodCombination activationMethodCombination = MS2Frames > 0 ? ActivationMethodCombination.CidOnly : ActivationMethodCombination.Unsupported;
         *  if (activationMethodCombination == ActivationMethodCombination.Unsupported) throw new SystemException("Unsupported activation method.");
         *
         *  List<Spectrum> Spectra = new List<Spectrum>();
         *  foreach (var feature in FeatureTargets)
         *  {
         *      double mzToSearchTolerance = hcdMassError * feature.Mz / 1000000;
         *      double lowMz = feature.Mz - mzToSearchTolerance;
         *      double highMz = feature.Mz + mzToSearchTolerance;
         *
         *      double[] mzArray;
         *      int[] intensityArray;
         *      ImsRun.GetSpectrum(feature.LcStart, feature.LcEnd, DataReader.FrameType.MS1, feature.ImsStart,
         *          feature.ImsEnd, out mzArray, out intensityArray);
         *
         *      foreach (var mz in mzArray)
         *      {
         *          if (mz > highMz) break;
         *          if (mz > lowMz)
         *          {
         *              //Target IMS feature found in this scan
         *              double[] MS2Mz;
         *              int[] MS2Ints;
         *              ImsRun.GetSpectrum(feature.LcStart, feature.LcEnd, DataReader.FrameType.MS2, feature.ImsStart,
         *                  feature.ImsEnd, out MS2Mz, out MS2Ints);
         *              var MS2Intensity = (from intensity in MS2Ints select (double) (intensity)).ToArray();
         *              Spectrum spec = new ProductSpectrum(MS2Mz, MS2Intensity, feature.ImsScan);
         *              spec.MsLevel = 2;
         *              Spectra.Add(spec);
         *          }
         *      }
         *
         *
         *      /*
         *      for (int lcScan = feature.LcStart; lcScan <= feature.LcEnd; lcScan++)
         *      {
         *          if (framelist[lcScan] == DataReader.FrameType.MS1)
         *          {
         *              for (int imsScan = feature.ImsStart; imsScan <= feature.ImsEnd; imsScan++)
         *              {
         *                  double[] mzArray;
         *                  int[] intensityArray;
         *                  ImsRun.GetSpectrum(lcScan, lcScan, DataReader.FrameType.MS1, imsScan, imsScan, out mzArray, out intensityArray);
         *
         *                  foreach (var mz in mzArray)
         *                  {
         *                      if (mz > highMz) break;
         *                      if (mz > lowMz)
         *                      {
         *                          //Target IMS feature found in this scan
         *                          feature.AddCoord(lcScan, imsScan);
         *                      }
         *                  }
         *              }
         *          }
         *      }
         *  }
         *
         *  return new List<LipidGroupSearchResult>();
         * }
         */

        public static List <LipidGroupSearchResult> RunGlobalWorkflowAvgSpec(
            IEnumerable <Lipid> lipidList,
            LcMsRun lcmsRun,
            double hcdMassError,
            double cidMassError,
            ScoreModel scoreModel,
            IProgress <int> progress = null)
        {
            var lipidGroupSearchResultList = new List <LipidGroupSearchResult>();

            var hcdTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm);
            var cidTolerance = new Tolerance(cidMassError, ToleranceUnit.Ppm);

            var lipidsGroupedByTarget = lipidList.OrderBy(x => x.LipidTarget.MzRounded).GroupBy(x => x.LipidTarget).ToList();

            var    minLcScan = lcmsRun.MinLcScan;
            double maxLcScan = lcmsRun.MaxLcScan;

            var ms2scans   = lcmsRun.GetScanNumbers(2);
            var ms2spectra = ms2scans.Select(scan => lcmsRun.GetSpectrum(scan) as ProductSpectrum).ToList();
            var uniqueMz   = (from spectrum in ms2spectra select spectrum.IsolationWindow.IsolationWindowTargetMz).ToList().Distinct().ToList();

            foreach (var mz in uniqueMz)
            {
                var hcdScans      = ms2spectra.Where(x => Math.Abs(x.IsolationWindow.IsolationWindowTargetMz - mz) < float.Epsilon && x.ActivationMethod == ActivationMethod.HCD).Select(x => x.ScanNum).ToList();
                var summedSpec    = lcmsRun.GetSummedSpectrum(hcdScans);
                var summedHcdSpec = new ProductSpectrum(summedSpec.Peaks, 0)
                {
                    ActivationMethod = ActivationMethod.HCD
                };

                var cidScans = ms2spectra.Where(x => Math.Abs(x.IsolationWindow.IsolationWindowTargetMz - mz) < float.Epsilon && x.ActivationMethod == ActivationMethod.CID).Select(x => x.ScanNum).ToList();
                summedSpec = lcmsRun.GetSummedSpectrum(cidScans);
                var summedCidSpec = new ProductSpectrum(summedSpec.Peaks, 0)
                {
                    ActivationMethod = ActivationMethod.CID
                };

                var HcdPresent = summedHcdSpec.Peaks.Any();
                var CidPresent = summedCidSpec.Peaks.Any();

                if (HcdPresent)
                {
                    summedHcdSpec.IsolationWindow = new IsolationWindow(mz, hcdMassError, hcdMassError);
                }
                if (CidPresent)
                {
                    summedCidSpec.IsolationWindow = new IsolationWindow(mz, cidMassError, cidMassError);
                }

                var mzToSearchTolerance = hcdMassError * mz / 1000000;
                var lowMz  = mz - mzToSearchTolerance;
                var highMz = mz + mzToSearchTolerance;

                var      hcdSpectrum       = summedHcdSpec;
                var      cidSpectrum       = summedCidSpec;
                Spectrum precursorSpectrum = null;

                foreach (var grouping in lipidsGroupedByTarget)
                {
                    var lipidTarget = grouping.Key;
                    var lipidMz     = lipidTarget.MzRounded;

                    // If we reached the point where the m/z is too high, we can exit
                    if (lipidMz > highMz)
                    {
                        break;
                    }

                    if (lipidMz > lowMz)
                    {
                        // Find the MS1 data
                        //Xic xic = lcmsRun.GetPrecursorExtractedIonChromatogram(lipidMz, hcdTolerance, i);
                        var xic = lcmsRun.GetFullPrecursorIonExtractedIonChromatogram(lipidMz, hcdTolerance);

                        // Bogus data
                        if (precursorSpectrum != null && (xic.GetApexScanNum() < 0 || xic.GetSumIntensities() <= 0))
                        {
                            continue;
                        }

                        // Grab the MS/MS peak to search for
                        IEnumerable <MsMsSearchUnit> msMsSearchUnits = lipidTarget.GetMsMsSearchUnits();

                        // Get all matching peaks
                        var hcdSearchResultList = hcdSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = hcdSpectrum.FindPeak(msMsSearchUnit.Mz, hcdTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();
                        var cidSearchResultList = cidSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = cidSpectrum.FindPeak(msMsSearchUnit.Mz, cidTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();

                        // Create spectrum search results
                        SpectrumSearchResult   spectrumSearchResult;
                        LipidGroupSearchResult lipidGroupSearchResult;
                        if (precursorSpectrum != null)
                        {
                            spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, precursorSpectrum, hcdSearchResultList, cidSearchResultList, xic, lcmsRun)
                            {
                                PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                            };
                            lipidGroupSearchResult = new LipidGroupSearchResult(lipidTarget, grouping.ToList(), spectrumSearchResult, scoreModel);
                        }
                        else //If there are no precursor scans in this file
                        {
                            spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, hcdSearchResultList, cidSearchResultList, lcmsRun)
                            {
                                PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                            };
                            lipidGroupSearchResult = new LipidGroupSearchResult(lipidTarget, grouping.ToList(), spectrumSearchResult, scoreModel);
                        }


                        lipidGroupSearchResultList.Add(lipidGroupSearchResult);

                        //textWriter.WriteLine(lipidTarget.CommonName + "\t" + spectrumSearchResult.Score);
                        //Console.WriteLine(lipidTarget.CommonName + "\t" + spectrumSearchResult.Score);
                    }
                }
            }



            //var summedHcdSpectra = uniqueMz.Select(mz => new Tuple<double, ProductSpectrum>(mz, lcmsRun.GetSummedMs2Spectrum(mz, minLcScan, (int)maxLcScan, 1, 2, ActivationMethod.HCD))).ToDictionary(x => x.Item1, x => x.Item2);
            //if (summedHcdSpectra != null) { foreach (var spec in summedHcdSpectra) { spec.Value.IsolationWindow = new IsolationWindow(spec.Key, spec.Key, spec.Key); } }
            //var summedCidSpectra = uniqueMz.Select(mz => lcmsRun.GetSummedMs2Spectrum(mz, minLcScan, (int)maxLcScan, 1, 2, ActivationMethod.CID)).ToList();



            return(lipidGroupSearchResultList);
        }