Exemplo n.º 1
0
        public void test1()
        {
            Run run = new XCaliburRun(xcaliburTestfile);

            Task msgen        = new GenericMSGenerator();
            Task peakDetector = new DeconToolsPeakDetector();

            run.CurrentScanSet = new ScanSet(6005);

            msgen.Execute(run.ResultCollection);
            peakDetector.Execute(run.ResultCollection);

            MSPeak testPeak = (MSPeak)run.PeakList[500];



            PattersonChargeStateCalculator chargeCalc = new PattersonChargeStateCalculator();
            short chargeState = chargeCalc.GetChargeState(run.XYData, run.PeakList, testPeak);

            Console.WriteLine(testPeak.XValue + "; Charge = " + chargeState);
        }
        public void HardCase1()
        {
            // https://jira.pnnl.gov/jira/browse/OMCS-647

            Run run = new XCaliburRun2(xcaliburTestfile);

            var msgen        = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);
            var peakDetector = new DeconToolsPeakDetectorV2();

            peakDetector.PeakToBackgroundRatio = 1.3;

            run.CurrentScanSet = new ScanSet(6005);

            msgen.Execute(run.ResultCollection);
            peakDetector.Execute(run.ResultCollection);


            //579.535
            //TestUtilities.DisplayPeaks(run.PeakList);

            var peak        = run.PeakList.First(n => n.XValue > 579.53 && n.XValue < 579.54);
            var chargeState = PattersonChargeStateCalculator.GetChargeState(run.XYData, run.PeakList, peak as MSPeak);

            Assert.AreEqual(4, chargeState);
            Console.WriteLine("charge State: " + chargeState);
            var times = new List <long>();

            // return;
            for (var i = 0; i < 500; i++)
            {
                var sw = new Stopwatch();
                sw.Start();
                chargeState = PattersonChargeStateCalculator.GetChargeState(run.XYData, run.PeakList, peak as MSPeak);
                sw.Stop();
                times.Add(sw.ElapsedMilliseconds);
            }

            Console.WriteLine("average time in ms= " + times.Average());
        }
        public void EasyCase1()
        {
            // https://jira.pnnl.gov/jira/browse/OMCS-647

            Run run = new XCaliburRun2(xcaliburTestfile);

            var msgen        = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);
            var peakDetector = new DeconToolsPeakDetectorV2();

            peakDetector.PeakToBackgroundRatio = 1.3;

            run.CurrentScanSet = new ScanSet(6005);

            msgen.Execute(run.ResultCollection);
            peakDetector.Execute(run.ResultCollection);

            var peak = run.PeakList.First(n => n.XValue > 903.94 && n.XValue < 903.95);

            var chargeState = PattersonChargeStateCalculator.GetChargeState(run.XYData, run.PeakList, peak as MSPeak);

            Assert.AreEqual(2, chargeState);
        }
        /// <summary>
        /// The main Thrash algorithm.
        /// </summary>
        /// <param name="originalXYData">Mass spec XY data</param>
        /// <param name="mspeakList">Mass spec peak data</param>
        /// <param name="backgroundIntensity"></param>
        /// <param name="minPeptideIntensity"></param>
        /// <param name="minMSFeatureToBackgroundRatio"></param>
        /// <returns>List of isotopic profiles</returns>
        public List <IsotopicProfile> PerformThrash(XYData originalXYData,
                                                    List <Peak> mspeakList,
                                                    double backgroundIntensity    = 0,
                                                    double minPeptideIntensity    = 0, double
                                                    minMSFeatureToBackgroundRatio = 3)
        {
            var isotopicProfiles = new List <IsotopicProfile>();

            #region Paul Addition

            var myIsotopicProfiles    = new List <IsotopicProfile>();
            var otherIsotopicProfiles = new List <IsotopicProfile>();
            #endregion

            if (Parameters.AreAllTheoreticalProfilesCachedBeforeStarting)
            {
                CreateAllTheoreticalProfilesForMassRange();
            }


            var minMSFeatureIntensity = backgroundIntensity * minMSFeatureToBackgroundRatio;


            var xyData = new XYData();
            xyData.Xvalues = originalXYData.Xvalues;
            xyData.Yvalues = originalXYData.Yvalues;


            var peaksThatWereProcessedInfo = new Dictionary <Peak, bool>();

            var sortedPeaklist        = new List <Peak>(mspeakList).OrderByDescending(p => p.Height).ToList();
            var peaksAlreadyProcessed = new HashSet <Peak>();


            var stringBuilder = new StringBuilder();

            var listOfMonoMZs = new SortedDictionary <int, double>();
            var currentUniqueMSFeatureIDNum = 0;


            var peakCounter = -1;
            foreach (var msPeak in sortedPeaklist)
            {
                //if (msPeak.XValue > 579.53 && msPeak.XValue < 579.54)
                //{
                //    int x = 90;
                //}
                var indexOfCurrentPeak = mspeakList.IndexOf(msPeak);

                if (peaksAlreadyProcessed.Contains(msPeak))
                {
                    continue;
                }

                var peakIsBelowIntensityThreshold = (msPeak.Height < minMSFeatureIntensity);
                if (peakIsBelowIntensityThreshold)
                {
                    break;
                }



                peakCounter++;

                if (peakCounter == 465)
                {
                    // Console.WriteLine(peakCounter);
                }


                //get potential charge states
                var ppmTolerance = (msPeak.Width / 2.35) / msPeak.XValue * 1e6;    //   peak's sigma value / mz * 1e6

                HashSet <int> potentialChargeStates;
                // HashSet<int> potentialChargeStatesbyPaul;
                if (UseAutocorrelationChargeDetermination && false)
                {
                    var chargeState = PattersonChargeStateCalculator.GetChargeState(xyData, mspeakList, msPeak as MSPeak);
                    potentialChargeStates = new HashSet <int>();
                    potentialChargeStates.Add(chargeState);
                }
                else
                {   //Paul subtraction
                    IqLogger.Log.Debug("MZ value: " + msPeak.XValue + "\n");
                    potentialChargeStates = GetPotentialChargeStates(indexOfCurrentPeak, mspeakList, ppmTolerance);
                    #region Paul Addition
                    // ChromCorrelatingChargeDecider chargeDecider= new ChromCorrelatingChargeDecider(_run);
                    // potentialChargeStatesbyPaul=chargeDecider.GetPotentialChargeState(indexOfCurrentPeak, mspeakList, ppmTolerance);

                    //potentialChargeStates = potentialChargeStatesbyPaul;
                    #endregion
                }
                var reportString201 = "potentialChargeStates: ";
                foreach (var charge in potentialChargeStates)
                {
                    reportString201 += charge + "\t";
                }
                IqLogger.Log.Debug(reportString201 + "\n");

                var potentialMSFeaturesForGivenChargeState = new List <IsotopicProfile>();
                foreach (var potentialChargeState in potentialChargeStates)
                {
                    var bestFitVal = 1.0;   // 1.0 is worst fit value. Start with 1.0 and see if we can find better fit value

                    IsotopicProfile theorIso;

                    //TODO: there could be a problem here
                    var msFeature = GetMSFeature(mspeakList, xyData, potentialChargeState, msPeak, ref bestFitVal, out theorIso);

                    if (msFeature != null)
                    {
                        msFeature.Score = bestFitVal;
                        msFeature.IntensityMostAbundant = msFeature.getMostIntensePeak().Height;

                        var indexMostAbundantPeakTheor = theorIso.GetIndexOfMostIntensePeak();

                        //Paul edit. "&& indexMostAbundantPeakTheor>=0"
                        if (msFeature.Peaklist.Count > indexMostAbundantPeakTheor && indexMostAbundantPeakTheor >= 0)
                        {
                            msFeature.IntensityMostAbundantTheor = msFeature.Peaklist[indexMostAbundantPeakTheor].Height;
                        }
                        else
                        {
                            msFeature.IntensityMostAbundantTheor = msFeature.IntensityMostAbundant;
                        }


                        var msFeatureAlreadyPresentInAnotherChargeState = listOfMonoMZs.ContainsValue(msFeature.MonoPeakMZ);

                        if (!msFeatureAlreadyPresentInAnotherChargeState)
                        {
                            potentialMSFeaturesForGivenChargeState.Add(msFeature);
                        }
                        else
                        {
                            //Console.WriteLine( "Nope... not using this charge state... MSFeature already found with same MonoMZ. \tcurrent peak= \t" +msPeak.XValue.ToString("0.0000") + "\tmsfeature= " + msFeature);
                        }
                    }
                }

                IsotopicProfile msfeature = null;//Paul Addition "=null"
                if (potentialMSFeaturesForGivenChargeState.Count == 0)
                {
                    stringBuilder.Append(msPeak.XValue.ToString("0.00000") + "\tNo profile found.\n");
                    msfeature = null;
                }
                else if (potentialMSFeaturesForGivenChargeState.Count == 1)
                {
                    msfeature = potentialMSFeaturesForGivenChargeState[0];

                    stringBuilder.Append(msPeak.XValue.ToString("0.00000") + "\t" +
                                         msfeature.MonoPeakMZ.ToString("0.0000") + "\t" +
                                         msfeature.ChargeState + "\t" + msfeature.Score + "\t" + ppmTolerance + "\n");
                }
                else
                {
                    stringBuilder.Append("Multiple candidates found...." + "\n");

                    foreach (var isotopicProfile in potentialMSFeaturesForGivenChargeState)
                    {
                        stringBuilder.Append(msPeak.XValue.ToString("0.00000") + "\t" +
                                             isotopicProfile.MonoPeakMZ.ToString("0.0000") + "\t" +
                                             isotopicProfile.ChargeState + "\t" + isotopicProfile.Score + "\t" + ppmTolerance + "\n");
                    }
                    stringBuilder.Append(Environment.NewLine);


                    if (Parameters.CheckAllPatternsAgainstChargeState1)
                    {
                        msfeature = potentialMSFeaturesForGivenChargeState.FirstOrDefault(n => n.ChargeState == 1);
                    }
                    else
                    {
                        #region Paul addition
                        //TODO: [Paul]  This is the major means of deciding between charge states and where we need to do better.
                        //We need some test cases to capture this problem.
                        var stopwatch = new Stopwatch();

                        if (doPaulMethod)
                        {
                            var peaksNotLoaded = _run.ResultCollection.MSPeakResultList == null ||
                                                 _run.ResultCollection.MSPeakResultList.Count == 0;
                            if (peaksNotLoaded)// || !importedFULLPeaks)
                            {
                                stopwatch.Start();
                                LoadPeaks(_run);
                                //importedFULLPeaks = true;
                                stopwatch.Stop();
                                IqLogger.Log.Debug("stopwatch: " + stopwatch.Elapsed);
                            }
                            var brain = new ChromCorrelatingChargeDecider(_run);
                            msfeature = brain.DetermineCorrectIsotopicProfile(potentialMSFeaturesForGivenChargeState.Where(n => n.Score < .50).ToList());
                            //if (msfeature==null)
                            //{
                            //    msfeature = brain.DetermineCorrectIsotopicProfile(potentialMSFeaturesForGivenChargeState);
                            //}
                            //hitcounter2++;
                        }
                        else//do it the regular way.
                        {
                            #endregion
                            msfeature = (from n in potentialMSFeaturesForGivenChargeState
                                         where n.Score < 0.15
                                         orderby n.ChargeState descending
                                         select n).FirstOrDefault();
                            if (msfeature == null)
                            {
                                msfeature = (from n in potentialMSFeaturesForGivenChargeState
                                             orderby n.Score
                                             select n).First();
                            }


                            #region Paul Addition
                        }
                        //line outputs.
                        if (null != msfeature)
                        {
                            var reportString309 = "\nM/Z = " + msfeature.MonoPeakMZ +
                                                  "\nCHOSEN CHARGE: " + msfeature.ChargeState + "\n\n";
                            IqLogger.Log.Debug(reportString309);

                            //tabular output
                            //string reportString309 = "\tM/Z = \t" + msfeature.MonoPeakMZ +
                            //        "\tCHOSEN CHARGE: \t" + msfeature.ChargeState+ "\n";
                            //IqLogger.Log.Debug(reportString309);
                        }

                        #endregion
                    }
                }

                if (msfeature != null)
                {
                    listOfMonoMZs.Add(currentUniqueMSFeatureIDNum, msfeature.MonoPeakMZ);
                    currentUniqueMSFeatureIDNum++;

                    isotopicProfiles.Add(msfeature);
                    //hitcounter++;//Paul Addition


                    foreach (var peak in msfeature.Peaklist)
                    {
                        //For debugging
                        //if (peak.XValue > 534.76515 && peak.XValue < 534.78515) //(peak.XValue>579.62 && peak.XValue<579.65) || (peak.XValue>579.75 && peak.XValue<579.8))
                        //{
                        //    int x = 39843;
                        //}
                        peaksAlreadyProcessed.Add(peak);
                    }
                }
            }//end of foreach peak loop

            //Console.WriteLine(stringBuilder.ToString());

            var uniqueIsotopicProfiles = removeDuplicatesFromFoundMSFeatures(isotopicProfiles);
            #region Paul Addition
            //IqLogger.Log.Debug("Hit counter: " + hitcounter);
            //IqLogger.Log.Debug("Hit counter2: " + hitcounter2);
            //var uniqueOtherIsotopicProfiles = removeDuplicatesFromFoundMSFeatures(isotopicProfiles);

            //IqLogger.Log.Debug("old non unique count: " + otherIsotopicProfiles.Count + "\n" +
            //    "new non unique count: " + myIsotopicProfiles.Count + "\n");
            //var uniqueMyIsotopicProfiles = removeDuplicatesFromFoundMSFeatures(myIsotopicProfiles);

            //IqLogger.Log.Debug("\nOld unique profile count: " + uniqueOtherIsotopicProfiles.Count + "\n" +
            //    "New unique profile count: " + uniqueMyIsotopicProfiles.Count);
            //IqLogger.Log.Debug("\nunique profile count: " + uniqueIsotopicProfiles.Count + "\n");

            #endregion
            //NOTE: we don't need to do the reordering, but I do this so I can compare to the old THRASH
            uniqueIsotopicProfiles = uniqueIsotopicProfiles.OrderByDescending(p => p.IntensityMostAbundantTheor).ToList();
            return(uniqueIsotopicProfiles);
        }